-
Notifications
You must be signed in to change notification settings - Fork 528
/
monodroid-glue.c
4036 lines (3362 loc) · 111 KB
/
monodroid-glue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdarg.h>
#include <jni.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#ifdef ANDROID
#include <sys/system_properties.h>
#else
#define PROP_NAME_MAX 32
#define PROP_VALUE_MAX 92
#endif
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/time.h>
#include "mono_android_Runtime.h"
#if defined (DEBUG)
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#endif
#ifndef WINDOWS
#include <sys/mman.h>
#include <sys/utsname.h>
#else
#include <windef.h>
#include <winbase.h>
#include <shlobj.h>
#include <objbase.h>
#include <knownfolders.h>
#include <shlwapi.h>
#endif
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <pthread.h>
#include "monodroid.h"
#include "logger.h"
#include "dylib-mono.h"
#include "util.h"
#include "debug.h"
#include "embedded-assemblies.h"
#include "unzip.h"
#include "ioapi.h"
#include "monodroid-glue.h"
#ifndef WINDOWS
#include "xamarin_getifaddrs.h"
#endif
static pthread_mutex_t process_cmd_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t process_cmd_cond = PTHREAD_COND_INITIALIZER;
static int debugging_configured;
static int sdb_fd;
static int profiler_configured;
static int profiler_fd;
static char *profiler_description;
#ifdef DEBUG
static int config_timedout;
static struct timeval wait_tv;
static struct timespec wait_ts;
#endif // def DEBUG
static char *runtime_libdir;
static int register_debug_symbols;
static MonoMethod* registerType;
/*
* If set, monodroid will spin in a loop until the debugger breaks the wait by
* clearing monodroid_gdb_wait.
*/
static int wait_for_gdb;
static volatile int monodroid_gdb_wait = TRUE;
static int android_api_level = 0;
/* Can be called by a native debugger to break the wait on startup */
MONO_API void
monodroid_clear_gdb_wait (void)
{
monodroid_gdb_wait = FALSE;
}
#ifdef ANDROID64
#define SYSTEM_LIB_PATH "/system/lib64"
#elif ANDROID
#define SYSTEM_LIB_PATH "/system/lib"
#elif LINUX_FLATPAK
#define SYSTEM_LIB_PATH "/app/lib/mono"
#elif LINUX
#define SYSTEM_LIB_PATH "/usr/lib"
#elif APPLE_OS_X
#define SYSTEM_LIB_PATH "/Library/Frameworks/Xamarin.Android.framework/Libraries/"
#elif WINDOWS
#define SYSTEM_LIB_PATH get_xamarin_android_msbuild_path()
#else
#define SYSTEM_LIB_PATH ""
#endif
FILE *gref_log;
FILE *lref_log;
/* !DO NOT REMOVE! Used by Mono BCL */
MONO_API int
_monodroid_get_android_api_level (void)
{
return android_api_level;
}
/* Invoked by System.Core.dll!System.IO.MemoryMappedFiles.MemoryMapImpl.getpagesize */
MONO_API int
monodroid_getpagesize (void)
{
#ifndef WINDOWS
return getpagesize ();
#else
SYSTEM_INFO info;
GetSystemInfo (&info);
return info.dwPageSize;
#endif
}
/* Invoked by:
- System.Core.dll!System.TimeZoneInfo.Android.GetDefaultTimeZoneName
- Mono.Android.dll!Android.Runtime.AndroidEnvironment.GetDefaultTimeZone
*/
MONO_API void
monodroid_free (void *ptr)
{
free (ptr);
}
static int max_gref_count;
struct BundledProperty {
char *name;
char *value;
int value_len;
struct BundledProperty *next;
};
static struct BundledProperty* bundled_properties;
static struct BundledProperty*
lookup_system_property (const char *name)
{
struct BundledProperty *p = bundled_properties;
for ( ; p ; p = p->next)
if (strcmp (p->name, name) == 0)
return p;
return NULL;
}
static void
add_system_property (const char *name, const char *value)
{
int name_len, value_len;
struct BundledProperty* p = lookup_system_property (name);
if (p) {
char *n = monodroid_strdup_printf ("%s", value);
if (!n)
return;
free (p->value);
p->value = n;
p->value_len = strlen (p->value);
return;
}
name_len = strlen (name);
value_len = strlen (value);
p = malloc (sizeof (struct BundledProperty) + name_len + 1);
if (!p)
return;
p->name = ((char*) p) + sizeof (struct BundledProperty);
strncpy (p->name, name, name_len);
p->name [name_len] = '\0';
p->value = monodroid_strdup_printf ("%s", value);
p->value_len = value_len;
p->next = bundled_properties;
bundled_properties = p;
}
#ifndef ANDROID
static void
monodroid_strreplace (char *buffer, char old_char, char new_char)
{
if (buffer == NULL)
return;
while (*buffer != '\0') {
if (*buffer == old_char)
*buffer = new_char;
buffer++;
}
}
static int
_monodroid__system_property_get (const char *name, char *sp_value, size_t sp_value_len)
{
if (!name || !sp_value)
return -1;
char *env_name = monodroid_strdup_printf ("__XA_%s", name);
monodroid_strreplace (env_name, '.', '_');
char *env_value = getenv (env_name);
free (env_name);
size_t env_value_len = env_value ? strlen (env_value) : 0;
if (env_value_len == 0) {
sp_value[0] = '\0';
return 0;
}
if (env_value_len >= sp_value_len)
log_warn (LOG_DEFAULT, "System property buffer size too small by %u bytes", env_value_len == sp_value_len ? 1 : env_value_len - sp_value_len);
strncpy (sp_value, env_value, sp_value_len);
sp_value[sp_value_len] = '\0';
return strlen (sp_value);
}
#elif ANDROID64
/* __system_property_get was removed in Android 5.0/64bit
this is hopefully temporary replacement, until we find better
solution
sp_value buffer should be at least PROP_VALUE_MAX+1 bytes long
*/
static int
_monodroid__system_property_get (const char *name, char *sp_value, size_t sp_value_len)
{
if (!name || !sp_value)
return -1;
char *cmd = monodroid_strdup_printf ("getprop %s", name);
FILE* result = popen (cmd, "r");
int len = (int) fread (sp_value, 1, sp_value_len, result);
fclose (result);
sp_value [len] = 0;
if (len > 0 && sp_value [len - 1] == '\n') {
sp_value [len - 1] = 0;
len--;
} else {
if (len != 0)
len = 0;
sp_value [0] = 0;
}
log_info (LOG_DEFAULT, "_monodroid__system_property_get %s: '%s' len: %d", name, sp_value, len);
return len;
}
#else
static int
_monodroid__system_property_get (const char *name, char *sp_value, size_t sp_value_len)
{
if (!name || !sp_value)
return -1;
char *buf = NULL;
if (sp_value_len < PROP_VALUE_MAX + 1) {
log_warn (LOG_DEFAULT, "Buffer to store system property may be too small, will copy only %u bytes", sp_value_len);
buf = xmalloc (PROP_VALUE_MAX + 1);
}
int len = __system_property_get (name, buf ? buf : sp_value);
if (buf) {
strncpy (sp_value, buf, sp_value_len);
sp_value [sp_value_len] = '\0';
free (buf);
}
return len;
}
#endif
MONO_API int
monodroid_get_system_property (const char *name, char **value)
{
char *pvalue;
char sp_value [PROP_VALUE_MAX+1] = { 0, };
int len;
struct BundledProperty *p;
if (value)
*value = NULL;
pvalue = sp_value;
len = _monodroid__system_property_get (name, sp_value, sizeof (sp_value));
if (len <= 0 && (p = lookup_system_property (name)) != NULL) {
pvalue = p->value;
len = p->value_len;
}
if (len >= 0 && value) {
*value = malloc (len+1);
if (!*value)
return -len;
memcpy (*value, pvalue, len);
(*value)[len] = '\0';
}
return len;
}
#ifdef RELEASE
#define MAX_OVERRIDES 1
#else
#define MAX_OVERRIDES 3
#endif
static char* override_dirs [MAX_OVERRIDES];
static int
_monodroid_get_system_property_from_file (const char *path, char **value)
{
int i;
if (value)
*value = NULL;
FILE* fp = monodroid_fopen (path, "r");
if (fp == NULL)
return 0;
struct stat fileStat;
if (fstat (fileno (fp), &fileStat) < 0) {
fclose (fp);
return 0;
}
if (!value) {
fclose (fp);
return fileStat.st_size+1;
}
*value = malloc (fileStat.st_size+1);
if (!(*value)) {
fclose (fp);
return fileStat.st_size+1;
}
ssize_t len = fread (*value, 1, fileStat.st_size, fp);
fclose (fp);
for (i = 0; i < fileStat.st_size+1; ++i) {
if ((*value) [i] != '\n' && (*value) [i] != '\r')
continue;
(*value) [i] = 0;
break;
}
return len;
}
int
monodroid_get_system_property_from_overrides (const char *name, char ** value)
{
int result = -1;
int oi;
for (oi = 0; oi < MAX_OVERRIDES; ++oi) {
if (override_dirs [oi]) {
char *overide_file = path_combine (override_dirs [oi], name);
log_info (LOG_DEFAULT, "Trying to get property from %s", overide_file);
result = _monodroid_get_system_property_from_file (overide_file, value);
free (overide_file);
if (result <= 0 || value == NULL || (*value) == NULL || strlen (*value) == 0) {
continue;
}
log_info (LOG_DEFAULT, "Property '%s' from %s has value '%s'.", name, override_dirs [oi], *value);
return result;
}
}
return 0;
}
static char*
get_primary_override_dir (JNIEnv *env, jstring home)
{
const char *v;
char *p;
v = (*env)->GetStringUTFChars (env, home, NULL);
p = path_combine (v, ".__override__");
(*env)->ReleaseStringUTFChars (env, home, v);
return p;
}
static char *primary_override_dir;
static char *external_override_dir;
static char *external_legacy_override_dir;
static void
create_update_dir (char *override_dir)
{
#if defined(RELEASE)
/*
* Don't create .__override__ on Release builds, because Google requires
* that pre-loaded apps not create world-writable directories.
*
* However, if any logging is enabled (which should _not_ happen with
* pre-loaded apps!), we need the .__override__ directory...
*/
if (log_categories == 0 && monodroid_get_namespaced_system_property (DEBUG_MONO_PROFILE_PROPERTY, NULL) == 0) {
return;
}
#endif
override_dirs [0] = override_dir;
create_public_directory (override_dir);
log_warn (LOG_DEFAULT, "Creating public update directory: `%s`", override_dir);
}
static int
file_exists (const char *file)
{
monodroid_stat_t s;
if (monodroid_stat (file, &s) == 0 && (s.st_mode & S_IFMT) == S_IFREG)
return 1;
return 0;
}
static int
directory_exists (const char *directory)
{
monodroid_stat_t s;
if (monodroid_stat (directory, &s) == 0 && (s.st_mode & S_IFMT) == S_IFDIR)
return 1;
return 0;
}
static struct DylibMono mono;
struct DylibMono*
monodroid_get_dylib (void)
{
return &mono;
}
static const char *app_libdir;
int file_copy(const char *to, const char *from)
{
char buffer[BUFSIZ];
size_t n;
int saved_errno;
FILE *f1 = monodroid_fopen(from, "r");
FILE *f2 = monodroid_fopen(to, "w+");
while ((n = fread(buffer, sizeof(char), sizeof(buffer), f1)) > 0)
{
if (fwrite(buffer, sizeof(char), n, f2) != n)
{
saved_errno = errno;
fclose (f1);
fclose (f2);
errno = saved_errno;
return -1;
}
}
fclose (f1);
fclose (f2);
return 0;
}
/* Set of Windows-specific utility/reimplementation of Unix functions */
#ifdef WINDOWS
#define symlink file_copy
static char *msbuild_folder_path = NULL;
static const char*
get_xamarin_android_msbuild_path (void)
{
const char *suffix = "MSBuild\\Xamarin\\Android";
char *base_path = NULL;
wchar_t *buffer = NULL;
if (msbuild_folder_path != NULL)
return msbuild_folder_path;
// Get the base path for 'Program Files' on Windows
if (!SUCCEEDED (SHGetKnownFolderPath (&FOLDERID_ProgramFilesX86, 0, NULL, &buffer))) {
if (buffer != NULL)
CoTaskMemFree (buffer);
// returns current directory if a global one couldn't be found
return ".";
}
// Compute the final path
base_path = utf16_to_utf8 (buffer);
CoTaskMemFree (buffer);
msbuild_folder_path = path_combine (base_path, suffix);
free (base_path);
return msbuild_folder_path;
}
static char *libmonoandroid_directory_path = NULL;
// Returns the directory in which this library was loaded from
static char*
get_libmonoandroid_directory_path ()
{
wchar_t module_path[MAX_PATH];
HMODULE module = NULL;
if (libmonoandroid_directory_path != NULL)
return libmonoandroid_directory_path;
DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
if (!GetModuleHandleExW (flags, (void*)&libmonoandroid_directory_path, &module))
return NULL;
GetModuleFileNameW (module, module_path, sizeof (module_path) / sizeof (module_path[0]));
PathRemoveFileSpecW (module_path);
libmonoandroid_directory_path = utf16_to_utf8 (module_path);
return libmonoandroid_directory_path;
}
static int
setenv(const char *name, const char *value, int overwrite)
{
wchar_t *wname = utf8_to_utf16 (name);
wchar_t *wvalue = utf8_to_utf16 (value);
BOOL result = SetEnvironmentVariableW (wname, wvalue);
free (wname);
free (wvalue);
return result ? 0 : -1;
}
static pthread_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
static int
readdir_r (_WDIR *dirp, struct _wdirent *entry, struct _wdirent **result)
{
int error_code = 0;
pthread_mutex_lock (&readdir_mutex);
errno = 0;
entry = _wreaddir (dirp);
*result = entry;
if (entry == NULL && errno != 0)
error_code = -1;
pthread_mutex_unlock (&readdir_mutex);
return error_code;
}
#endif // def WINDOWS
#ifndef RELEASE
static void
copy_monosgen_to_internal_location(char *to, char *from)
{
char *from_libmonoso = path_combine (from, "libmonosgen-2.0.so");
if (!file_exists (from_libmonoso))
{
free (from_libmonoso);
return;
}
log_warn (LOG_DEFAULT, "Copying sgen from external location %s to internal location %s", from, to);
char *to_libmonoso = path_combine (to, "libmonosgen-2.0.so");
unlink (to_libmonoso);
if (file_copy (to_libmonoso, from_libmonoso) < 0)
log_warn (LOG_DEFAULT, "Copy failed: %s", strerror (errno));
free (from_libmonoso);
free (to_libmonoso);
}
#endif
#if ANDROID || LINUX
#define MONO_SGEN_SO "libmonosgen-2.0.so"
#define MONO_SGEN_ARCH_SO "libmonosgen-%s-2.0.so"
#elif APPLE_OS_X
#define MONO_SGEN_SO "libmonosgen-2.0.dylib"
#define MONO_SGEN_ARCH_SO "libmonosgen-%s-2.0.dylib"
#elif WINDOWS
#define MONO_SGEN_SO "libmonosgen-2.0.dll"
#define MONO_SGEN_ARCH_SO "libmonosgen-%s-2.0.dll"
#else
#define MONO_SGEN_SO "monosgen-2.0"
#define MONO_SGEN_ARCH_SO "monosgen-%s-2.0"
#endif
#define TRY_LIBMONOSGEN(dir) \
if (dir) { \
libmonoso = path_combine (dir, MONO_SGEN_SO); \
log_warn (LOG_DEFAULT, "Trying to load sgen from: %s", libmonoso); \
if (file_exists (libmonoso)) \
return libmonoso; \
free (libmonoso); \
}
static char*
get_libmonosgen_path ()
{
char *libmonoso;
#ifndef RELEASE
// Android 5 includes some restrictions on loading dynamic libraries via dlopen() from
// external storage locations so we need to file copy the shared object to an internal
// storage location before loading it.
copy_monosgen_to_internal_location (primary_override_dir, external_override_dir);
copy_monosgen_to_internal_location (primary_override_dir, external_legacy_override_dir);
int i;
for (i = 0; i < MAX_OVERRIDES; ++i)
TRY_LIBMONOSGEN (override_dirs [i])
#endif
TRY_LIBMONOSGEN (app_libdir)
libmonoso = runtime_libdir ? monodroid_strdup_printf ("%s" MONODROID_PATH_SEPARATOR MONO_SGEN_ARCH_SO, runtime_libdir, sizeof(void*) == 8 ? "64bit" : "32bit") : NULL;
if (libmonoso && file_exists (libmonoso)) {
char* links_dir = path_combine (primary_override_dir, "links");
char* link = path_combine (links_dir, MONO_SGEN_SO);
if (!directory_exists (links_dir)) {
if (!directory_exists (primary_override_dir))
create_public_directory (primary_override_dir);
create_public_directory (links_dir);
}
free (links_dir);
if (!file_exists (link))
symlink (libmonoso, link);
free (libmonoso);
libmonoso = link;
}
log_warn (LOG_DEFAULT, "Trying to load sgen from: %s", libmonoso);
if (libmonoso && file_exists (libmonoso))
return libmonoso;
free (libmonoso);
#ifdef WINDOWS
TRY_LIBMONOSGEN (get_libmonoandroid_directory_path ())
#endif
TRY_LIBMONOSGEN (SYSTEM_LIB_PATH)
#ifdef RELEASE
log_fatal (LOG_DEFAULT, "cannot find libmonosgen-2.0.so in app_libdir: %s nor in previously printed locations.", app_libdir);
#else
log_fatal (LOG_DEFAULT, "cannot find libmonosgen-2.0.so in override_dir: %s, app_libdir: %s nor in previously printed locations.", override_dirs[0], app_libdir);
#endif
log_fatal (LOG_DEFAULT, "Do you have a shared runtime build of your app with AndroidManifest.xml android:minSdkVersion < 10 while running on a 64-bit Android 5.0 target? This combination is not supported.");
log_fatal (LOG_DEFAULT, "Please either set android:minSdkVersion >= 10 or use a build without the shared runtime (like default Release configuration).");
exit (FATAL_EXIT_CANNOT_FIND_LIBMONOSGEN);
return libmonoso;
}
typedef void* (*mono_mkbundle_init_ptr) (void (*)(const MonoBundledAssembly **), void (*)(const char* assembly_name, const char* config_xml));
mono_mkbundle_init_ptr mono_mkbundle_init;
static void
setup_bundled_app (const char *libappso)
{
void *libapp;
libapp = dlopen (libappso, RTLD_LAZY);
if (!libapp) {
log_fatal (LOG_BUNDLE, "bundled app initialization error: %s", dlerror ());
exit (FATAL_EXIT_CANNOT_LOAD_BUNDLE);
}
mono_mkbundle_init = dlsym (libapp, "mono_mkbundle_init");
if (!mono_mkbundle_init)
log_error (LOG_BUNDLE, "Missing mono_mkbundle_init in the application");
log_info (LOG_BUNDLE, "Bundled app loaded: %s", libappso);
}
static char*
get_bundled_app (JNIEnv *env, jstring dir)
{
const char *v;
char *libapp;
#ifndef RELEASE
libapp = path_combine (override_dirs [0], "libmonodroid_bundle_app.so");
if (file_exists (libapp))
return libapp;
free (libapp);
#endif
if (dir) {
v = (*env)->GetStringUTFChars (env, dir, NULL);
if (v) {
libapp = path_combine (v, "libmonodroid_bundle_app.so");
(*env)->ReleaseStringUTFChars (env, dir, v);
if (file_exists (libapp))
return libapp;
}
}
return NULL;
}
static JavaVM *jvm;
typedef struct {
void *dummy;
} MonoDroidProfiler;
static MonoDroidProfiler monodroid_profiler;
typedef struct MonoJavaGCBridgeType {
const char *namespace;
const char *typename;
} MonoJavaGCBridgeType;
static const MonoJavaGCBridgeType mono_java_gc_bridge_types[] = {
{ "Java.Lang", "Object" },
{ "Java.Lang", "Throwable" },
};
#define NUM_GC_BRIDGE_TYPES (sizeof (mono_java_gc_bridge_types)/sizeof (mono_java_gc_bridge_types [0]))
/* `mono_java_gc_bridge_info` stores shared global data about the last Monodroid assembly loaded.
* Specifically it stores data about the `mono_java_gc_bridge_types` types.
* In order for this to work, two rules must be followed.
* 1. Only one Monodroid appdomain can be loaded at a time.
* 2. Since the Monodroid appdomain unload clears `mono_java_gc_bridge_info`, anything which
* could run at the same time as the domain unload (like gc_bridge_class_kind) must tolerate
* the structure fields being set to zero during run
*/
typedef struct MonoJavaGCBridgeInfo {
MonoClass *klass;
MonoClassField *handle;
MonoClassField *handle_type;
MonoClassField *refs_added;
MonoClassField *weak_handle;
} MonoJavaGCBridgeInfo;
static MonoJavaGCBridgeInfo mono_java_gc_bridge_info [NUM_GC_BRIDGE_TYPES];
static jclass weakrefClass;
static jmethodID weakrefCtor;
static jmethodID weakrefGet;
static jobject Runtime_instance;
static jmethodID Runtime_gc;
static jclass TimeZone_class;
static jmethodID TimeZone_getDefault;
static jmethodID TimeZone_getID;
static int gc_disabled = 0;
static int gc_gref_count;
static int gc_weak_gref_count;
static int is_running_on_desktop = 0;
// Do this instead of using memset so that individual pointers are set atomically
static void clear_mono_java_gc_bridge_info() {
for (int c = 0; c < NUM_GC_BRIDGE_TYPES; c++) {
MonoJavaGCBridgeInfo *info = &mono_java_gc_bridge_info [c];
info->klass = NULL;
info->handle = NULL;
info->handle_type = NULL;
info->refs_added = NULL;
info->weak_handle = NULL;
}
}
static int
get_gc_bridge_index (MonoClass *klass)
{
int i;
int f = 0;
for (i = 0; i < NUM_GC_BRIDGE_TYPES; ++i) {
MonoClass *k = mono_java_gc_bridge_info [i].klass;
if (k == NULL) {
f++;
continue;
}
if (klass == k || mono.mono_class_is_subclass_of (klass, k, 0))
return i;
}
return f == NUM_GC_BRIDGE_TYPES
? -(int) NUM_GC_BRIDGE_TYPES
: -1;
}
static MonoJavaGCBridgeInfo *
get_gc_bridge_info_for_class (MonoClass *klass)
{
int i;
if (klass == NULL)
return NULL;
i = get_gc_bridge_index (klass);
if (i < 0)
return NULL;
return &mono_java_gc_bridge_info [i];
}
static MonoJavaGCBridgeInfo *
get_gc_bridge_info_for_object (MonoObject *object)
{
if (object == NULL)
return NULL;
return get_gc_bridge_info_for_class (mono.mono_object_get_class (object));
}
static jobject
lref_to_gref (JNIEnv *env, jobject lref)
{
jobject g;
if (lref == 0)
return 0;
g = (*env)->NewGlobalRef (env, lref);
(*env)->DeleteLocalRef (env, lref);
return g;
}
static char
get_object_ref_type (JNIEnv *env, void *handle)
{
jobjectRefType value;
if (handle == NULL)
return 'I';
value = (*env)->GetObjectRefType (env, handle);
switch (value) {
case JNIInvalidRefType: return 'I';
case JNILocalRefType: return 'L';
case JNIGlobalRefType: return 'G';
case JNIWeakGlobalRefType: return 'W';
default: return '*';
}
}
MONO_API extern int
_monodroid_max_gref_get (void)
{
return max_gref_count;
}
MONO_API extern int
_monodroid_gref_get (void)
{
return gc_gref_count;
}
static int
_monodroid_gref_inc (void)
{
return __sync_add_and_fetch (&gc_gref_count, 1);
}
static int
_monodroid_gref_dec (void)
{
return __sync_fetch_and_sub (&gc_gref_count, 1);
}
static char*
_get_stack_trace_line_end (char *m)
{
while (*m && *m != '\n')
m++;
return m;
}
static void
_write_stack_trace (FILE *to, char *from)
{
char *n = from;
char c;
do {
char *m = n;
char *end = _get_stack_trace_line_end (m);
n = end + 1;
c = *end;
*end = '\0';
fprintf (to, "%s\n", m);
fflush (to);
*end = c;
} while (c);
}
MONO_API void
_monodroid_gref_log (const char *message)
{
if (!gref_log)
return;
fprintf (gref_log, "%s", message);
fflush (gref_log);
}
MONO_API int
_monodroid_gref_log_new (jobject curHandle, char curType, jobject newHandle, char newType, const char *threadName, int threadId, char *from, int from_writable)
{
int c = _monodroid_gref_inc ();
if ((log_categories & LOG_GREF) == 0)
return c;
log_info (LOG_GREF, "+g+ grefc %i gwrefc %i obj-handle %p/%c -> new-handle %p/%c from thread '%s'(%i)",
c,
gc_weak_gref_count,
curHandle,
curType,
newHandle,
newType,
threadName,
threadId);
if (!gref_log)
return c;
fprintf (gref_log, "+g+ grefc %i gwrefc %i obj-handle %p/%c -> new-handle %p/%c from thread '%s'(%i)\n",
c,
gc_weak_gref_count,
curHandle,
curType,
newHandle,
newType,
threadName,
threadId);
if (from_writable)
_write_stack_trace (gref_log, from);
else
fprintf (gref_log, "%s\n", from);
fflush (gref_log);
return c;
}
MONO_API void
_monodroid_gref_log_delete (jobject handle, char type, const char *threadName, int threadId, char *from, int from_writable)
{
int c = _monodroid_gref_dec ();
if ((log_categories & LOG_GREF) == 0)
return;
log_info (LOG_GREF, "-g- grefc %i gwrefc %i handle %p/%c from thread '%s'(%i)",
c,
gc_weak_gref_count,
handle,
type,
threadName,
threadId);
if (!gref_log)
return;
fprintf (gref_log, "-g- grefc %i gwrefc %i handle %p/%c from thread '%s'(%i)\n",
c,
gc_weak_gref_count,
handle,
type,
threadName,
threadId);
if (from_writable)
_write_stack_trace (gref_log, from);
else
fprintf (gref_log, "%s\n", from);
fflush (gref_log);
}
MONO_API void
_monodroid_weak_gref_new (jobject curHandle, char curType, jobject newHandle, char newType, const char *threadName, int threadId, char *from, int from_writable)
{
++gc_weak_gref_count;