/
domain.c
2054 lines (1753 loc) · 55.9 KB
/
domain.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
/*
* domain.c: MonoDomain functions
*
* Author:
* Dietmar Maurer (dietmar@ximian.com)
* Patrik Torstensson
*
* Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
* Copyright 2004-2009 Novell, Inc (http://www.novell.com)
* Copyright 2011-2012 Xamarin, Inc (http://www.xamarin.com)
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
#include <config.h>
#include <glib.h>
#include <string.h>
#include <sys/stat.h>
#include <mono/metadata/gc-internals.h>
#include <mono/utils/atomic.h>
#include <mono/utils/mono-compiler.h>
#include <mono/utils/mono-logger-internals.h>
#include <mono/utils/mono-membar.h>
#include <mono/utils/mono-counters.h>
#include <mono/utils/hazard-pointer.h>
#include <mono/utils/mono-tls.h>
#include <mono/utils/mono-mmap.h>
#include <mono/utils/mono-threads.h>
#include <mono/metadata/object.h>
#include <mono/metadata/object-internals.h>
#include <mono/metadata/domain-internals.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/metadata-internals.h>
#include <mono/metadata/gc-internals.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/mono-debug-debugger.h>
#include <mono/metadata/mono-config.h>
#include <mono/metadata/threads-types.h>
#include <mono/metadata/runtime.h>
#include <metadata/threads.h>
#include <metadata/profiler-private.h>
#include <mono/metadata/coree.h>
#include <mono/utils/w32handle.h>
//#define DEBUG_DOMAIN_UNLOAD 1
/* we need to use both the Tls* functions and __thread because
* some archs may generate faster jit code with one meachanism
* or the other (we used to do it because tls slots were GC-tracked,
* but we can't depend on this).
*/
static MonoNativeTlsKey appdomain_thread_id;
#ifdef MONO_HAVE_FAST_TLS
MONO_FAST_TLS_DECLARE(tls_appdomain);
#define GET_APPDOMAIN() ((MonoDomain*)MONO_FAST_TLS_GET(tls_appdomain))
#define SET_APPDOMAIN(x) do { \
MonoThreadInfo *info; \
MONO_FAST_TLS_SET (tls_appdomain,x); \
mono_native_tls_set_value (appdomain_thread_id, x); \
mono_gc_set_current_thread_appdomain (x); \
info = mono_thread_info_current (); \
if (info) \
mono_thread_info_tls_set (info, TLS_KEY_DOMAIN, (x)); \
} while (FALSE)
#else /* !MONO_HAVE_FAST_TLS */
#define GET_APPDOMAIN() ((MonoDomain *)mono_native_tls_get_value (appdomain_thread_id))
#define SET_APPDOMAIN(x) do { \
MonoThreadInfo *info; \
mono_native_tls_set_value (appdomain_thread_id, x); \
mono_gc_set_current_thread_appdomain (x); \
info = mono_thread_info_current (); \
if (info) \
mono_thread_info_tls_set (info, TLS_KEY_DOMAIN, (x)); \
} while (FALSE)
#endif
#define GET_APPCONTEXT() (mono_thread_internal_current ()->current_appcontext)
#define SET_APPCONTEXT(x) MONO_OBJECT_SETREF (mono_thread_internal_current (), current_appcontext, (x))
static guint16 appdomain_list_size = 0;
static guint16 appdomain_next = 0;
static MonoDomain **appdomains_list = NULL;
static MonoImage *exe_image;
static gboolean debug_domain_unload;
gboolean mono_dont_free_domains;
#define mono_appdomains_lock() mono_coop_mutex_lock (&appdomains_mutex)
#define mono_appdomains_unlock() mono_coop_mutex_unlock (&appdomains_mutex)
static MonoCoopMutex appdomains_mutex;
static MonoDomain *mono_root_domain = NULL;
/* some statistics */
static int max_domain_code_size = 0;
static int max_domain_code_alloc = 0;
static int total_domain_code_alloc = 0;
/* AppConfigInfo: Information about runtime versions supported by an
* aplication.
*/
typedef struct {
GSList *supported_runtimes;
char *required_runtime;
int configuration_count;
int startup_count;
} AppConfigInfo;
static const MonoRuntimeInfo *current_runtime = NULL;
/* This is the list of runtime versions supported by this JIT.
*/
static const MonoRuntimeInfo supported_runtimes[] = {
{"v4.0.30319","4.5", { {4,0,0,0}, {10,0,0,0}, {4,0,0,0}, {4,0,0,0} } },
{"mobile", "2.1", { {2,0,5,0}, {10,0,0,0}, {2,0,5,0}, {2,0,5,0} } },
{"moonlight", "2.1", { {2,0,5,0}, { 9,0,0,0}, {3,5,0,0}, {3,0,0,0} } },
};
/* The stable runtime version */
#define DEFAULT_RUNTIME_VERSION "v4.0.30319"
/* Callbacks installed by the JIT */
static MonoCreateDomainFunc create_domain_hook;
static MonoFreeDomainFunc free_domain_hook;
/* AOT cache configuration */
static MonoAotCacheConfig aot_cache_config;
static void
get_runtimes_from_exe (const char *exe_file, MonoImage **exe_image, const MonoRuntimeInfo** runtimes);
static const MonoRuntimeInfo*
get_runtime_by_version (const char *version);
MonoNativeTlsKey
mono_domain_get_tls_key (void)
{
return appdomain_thread_id;
}
gint32
mono_domain_get_tls_offset (void)
{
int offset = -1;
#ifdef HOST_WIN32
if (appdomain_thread_id)
offset = appdomain_thread_id;
#else
MONO_THREAD_VAR_OFFSET (tls_appdomain, offset);
#endif
return offset;
}
#define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
#define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
static LockFreeMempool*
lock_free_mempool_new (void)
{
return g_new0 (LockFreeMempool, 1);
}
static void
lock_free_mempool_free (LockFreeMempool *mp)
{
LockFreeMempoolChunk *chunk, *next;
chunk = mp->chunks;
while (chunk) {
next = (LockFreeMempoolChunk *)chunk->prev;
mono_vfree (chunk, mono_pagesize ());
chunk = next;
}
g_free (mp);
}
/*
* This is async safe
*/
static LockFreeMempoolChunk*
lock_free_mempool_chunk_new (LockFreeMempool *mp, int len)
{
LockFreeMempoolChunk *chunk, *prev;
int size;
size = mono_pagesize ();
while (size - sizeof (LockFreeMempoolChunk) < len)
size += mono_pagesize ();
chunk = (LockFreeMempoolChunk *)mono_valloc (0, size, MONO_MMAP_READ|MONO_MMAP_WRITE);
g_assert (chunk);
chunk->mem = (guint8 *)ALIGN_PTR_TO ((char*)chunk + sizeof (LockFreeMempoolChunk), 16);
chunk->size = ((char*)chunk + size) - (char*)chunk->mem;
chunk->pos = 0;
/* Add to list of chunks lock-free */
while (TRUE) {
prev = mp->chunks;
if (InterlockedCompareExchangePointer ((volatile gpointer*)&mp->chunks, chunk, prev) == prev)
break;
}
chunk->prev = prev;
return chunk;
}
/*
* This is async safe
*/
static gpointer
lock_free_mempool_alloc0 (LockFreeMempool *mp, guint size)
{
LockFreeMempoolChunk *chunk;
gpointer res;
int oldpos;
// FIXME: Free the allocator
size = ALIGN_TO (size, 8);
chunk = mp->current;
if (!chunk) {
chunk = lock_free_mempool_chunk_new (mp, size);
mono_memory_barrier ();
/* Publish */
mp->current = chunk;
}
/* The code below is lock-free, 'chunk' is shared state */
oldpos = InterlockedExchangeAdd (&chunk->pos, size);
if (oldpos + size > chunk->size) {
chunk = lock_free_mempool_chunk_new (mp, size);
g_assert (chunk->pos + size <= chunk->size);
res = chunk->mem;
chunk->pos += size;
mono_memory_barrier ();
mp->current = chunk;
} else {
res = (char*)chunk->mem + oldpos;
}
return res;
}
void
mono_install_create_domain_hook (MonoCreateDomainFunc func)
{
create_domain_hook = func;
}
void
mono_install_free_domain_hook (MonoFreeDomainFunc func)
{
free_domain_hook = func;
}
/**
* mono_string_equal:
* @s1: First string to compare
* @s2: Second string to compare
*
* Compares two `MonoString*` instances ordinally for equality.
*
* Returns FALSE if the strings differ.
*/
gboolean
mono_string_equal (MonoString *s1, MonoString *s2)
{
int l1 = mono_string_length (s1);
int l2 = mono_string_length (s2);
if (s1 == s2)
return TRUE;
if (l1 != l2)
return FALSE;
return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1 * 2) == 0;
}
/**
* mono_string_hash:
* @s: the string to hash
*
* Compute the hash for a `MonoString*`
* Returns the hash for the string.
*/
guint
mono_string_hash (MonoString *s)
{
const guint16 *p = mono_string_chars (s);
int i, len = mono_string_length (s);
guint h = 0;
for (i = 0; i < len; i++) {
h = (h << 5) - h + *p;
p++;
}
return h;
}
static gboolean
mono_ptrarray_equal (gpointer *s1, gpointer *s2)
{
int len = GPOINTER_TO_INT (s1 [0]);
if (len != GPOINTER_TO_INT (s2 [0]))
return FALSE;
return memcmp (s1 + 1, s2 + 1, len * sizeof(gpointer)) == 0;
}
static guint
mono_ptrarray_hash (gpointer *s)
{
int i;
int len = GPOINTER_TO_INT (s [0]);
guint hash = 0;
for (i = 1; i < len; i++)
hash += GPOINTER_TO_UINT (s [i]);
return hash;
}
/*
* Allocate an id for domain and set domain->domain_id.
* LOCKING: must be called while holding appdomains_mutex.
* We try to assign low numbers to the domain, so it can be used
* as an index in data tables to lookup domain-specific info
* with minimal memory overhead. We also try not to reuse the
* same id too quickly (to help debugging).
*/
static int
domain_id_alloc (MonoDomain *domain)
{
int id = -1, i;
if (!appdomains_list) {
appdomain_list_size = 2;
appdomains_list = (MonoDomain **)mono_gc_alloc_fixed (appdomain_list_size * sizeof (void*), MONO_GC_DESCRIPTOR_NULL, MONO_ROOT_SOURCE_DOMAIN, "domains list");
}
for (i = appdomain_next; i < appdomain_list_size; ++i) {
if (!appdomains_list [i]) {
id = i;
break;
}
}
if (id == -1) {
for (i = 0; i < appdomain_next; ++i) {
if (!appdomains_list [i]) {
id = i;
break;
}
}
}
if (id == -1) {
MonoDomain **new_list;
int new_size = appdomain_list_size * 2;
if (new_size >= (1 << 16))
g_assert_not_reached ();
id = appdomain_list_size;
new_list = (MonoDomain **)mono_gc_alloc_fixed (new_size * sizeof (void*), MONO_GC_DESCRIPTOR_NULL, MONO_ROOT_SOURCE_DOMAIN, "domains list");
memcpy (new_list, appdomains_list, appdomain_list_size * sizeof (void*));
mono_gc_free_fixed (appdomains_list);
appdomains_list = new_list;
appdomain_list_size = new_size;
}
domain->domain_id = id;
appdomains_list [id] = domain;
appdomain_next++;
if (appdomain_next > appdomain_list_size)
appdomain_next = 0;
return id;
}
static gsize domain_gc_bitmap [sizeof(MonoDomain)/4/32 + 1];
static MonoGCDescriptor domain_gc_desc = MONO_GC_DESCRIPTOR_NULL;
static guint32 domain_shadow_serial = 0L;
/**
* mono_domain_create:
*
* Creates a new application domain, the unmanaged representation
* of the actual domain. Usually you will want to create the
*
* Application domains provide an isolation facilty for assemblies. You
* can load assemblies and execute code in them that will not be visible
* to other application domains. This is a runtime-based virtualization
* technology.
*
* It is possible to unload domains, which unloads the assemblies and
* data that was allocated in that domain.
*
* When a domain is created a mempool is allocated for domain-specific
* structures, along a dedicated code manager to hold code that is
* associated with the domain.
*
* Returns: New initialized MonoDomain, with no configuration or assemblies
* loaded into it.
*/
MonoDomain *
mono_domain_create (void)
{
MonoDomain *domain;
guint32 shadow_serial;
mono_appdomains_lock ();
shadow_serial = domain_shadow_serial++;
if (!domain_gc_desc) {
unsigned int i, bit = 0;
for (i = G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_FIRST_OBJECT); i < G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_FIRST_GC_TRACKED); i += sizeof (gpointer)) {
bit = i / sizeof (gpointer);
domain_gc_bitmap [bit / 32] |= (gsize) 1 << (bit % 32);
}
domain_gc_desc = mono_gc_make_descr_from_bitmap ((gsize*)domain_gc_bitmap, bit + 1);
}
mono_appdomains_unlock ();
#ifdef HAVE_BOEHM_GC
/*
* Boehm doesn't like roots inside GC allocated objects, and alloc_fixed returns
* a GC_MALLOC-ed object, contrary to the api docs. This causes random crashes when
* running the corlib test suite.
* To solve this, we pass a NULL descriptor, and don't register roots.
*/
domain = (MonoDomain *)mono_gc_alloc_fixed (sizeof (MonoDomain), NULL, MONO_ROOT_SOURCE_DOMAIN, "domain object");
#else
domain = (MonoDomain *)mono_gc_alloc_fixed (sizeof (MonoDomain), domain_gc_desc, MONO_ROOT_SOURCE_DOMAIN, "domain object");
mono_gc_register_root ((char*)&(domain->MONO_DOMAIN_FIRST_GC_TRACKED), G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_LAST_GC_TRACKED) - G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_FIRST_GC_TRACKED), MONO_GC_DESCRIPTOR_NULL, MONO_ROOT_SOURCE_DOMAIN, "misc domain fields");
#endif
domain->shadow_serial = shadow_serial;
domain->domain = NULL;
domain->setup = NULL;
domain->friendly_name = NULL;
domain->search_path = NULL;
mono_profiler_appdomain_event (domain, MONO_PROFILE_START_LOAD);
domain->mp = mono_mempool_new ();
domain->code_mp = mono_code_manager_new ();
domain->lock_free_mp = lock_free_mempool_new ();
domain->env = mono_g_hash_table_new_type ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal, MONO_HASH_KEY_VALUE_GC, MONO_ROOT_SOURCE_DOMAIN, "domain environment variables table");
domain->domain_assemblies = NULL;
domain->assembly_bindings = NULL;
domain->assembly_bindings_parsed = FALSE;
domain->class_vtable_array = g_ptr_array_new ();
domain->proxy_vtable_hash = g_hash_table_new ((GHashFunc)mono_ptrarray_hash, (GCompareFunc)mono_ptrarray_equal);
domain->static_data_array = NULL;
mono_jit_code_hash_init (&domain->jit_code_hash);
domain->ldstr_table = mono_g_hash_table_new_type ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal, MONO_HASH_KEY_VALUE_GC, MONO_ROOT_SOURCE_DOMAIN, "domain string constants table");
domain->num_jit_info_tables = 1;
domain->jit_info_table = mono_jit_info_table_new (domain);
domain->jit_info_free_queue = NULL;
domain->finalizable_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
domain->ftnptrs_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
mono_coop_mutex_init_recursive (&domain->lock);
mono_os_mutex_init_recursive (&domain->assemblies_lock);
mono_os_mutex_init_recursive (&domain->jit_code_hash_lock);
mono_os_mutex_init_recursive (&domain->finalizable_objects_hash_lock);
domain->method_rgctx_hash = NULL;
mono_appdomains_lock ();
domain_id_alloc (domain);
mono_appdomains_unlock ();
#ifndef DISABLE_PERFCOUNTERS
mono_perfcounters->loader_appdomains++;
mono_perfcounters->loader_total_appdomains++;
#endif
mono_debug_domain_create (domain);
if (create_domain_hook)
create_domain_hook (domain);
mono_profiler_appdomain_loaded (domain, MONO_PROFILE_OK);
return domain;
}
/**
* mono_init_internal:
*
* Creates the initial application domain and initializes the mono_defaults
* structure.
* This function is guaranteed to not run any IL code.
* If exe_filename is not NULL, the method will determine the required runtime
* from the exe configuration file or the version PE field.
* If runtime_version is not NULL, that runtime version will be used.
* Either exe_filename or runtime_version must be provided.
*
* Returns: the initial domain.
*/
static MonoDomain *
mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
{
static MonoDomain *domain = NULL;
MonoAssembly *ass = NULL;
MonoImageOpenStatus status = MONO_IMAGE_OK;
const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
int n, dummy;
#ifdef DEBUG_DOMAIN_UNLOAD
debug_domain_unload = TRUE;
#endif
if (domain)
g_assert_not_reached ();
#ifdef HOST_WIN32
/* Avoid system error message boxes. */
SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
#endif
#ifndef HOST_WIN32
mono_w32handle_init ();
wapi_init ();
#endif
#ifndef DISABLE_PERFCOUNTERS
mono_perfcounters_init ();
#endif
mono_counters_init ();
mono_counters_register ("Max native code in a domain", MONO_COUNTER_INT|MONO_COUNTER_JIT, &max_domain_code_size);
mono_counters_register ("Max code space allocated in a domain", MONO_COUNTER_INT|MONO_COUNTER_JIT, &max_domain_code_alloc);
mono_counters_register ("Total code space allocated", MONO_COUNTER_INT|MONO_COUNTER_JIT, &total_domain_code_alloc);
mono_gc_base_init ();
mono_thread_info_attach (&dummy);
MONO_FAST_TLS_INIT (tls_appdomain);
mono_native_tls_alloc (&appdomain_thread_id, NULL);
mono_coop_mutex_init_recursive (&appdomains_mutex);
mono_metadata_init ();
mono_images_init ();
mono_assemblies_init ();
mono_classes_init ();
mono_loader_init ();
mono_reflection_init ();
mono_runtime_init_tls ();
/* FIXME: When should we release this memory? */
MONO_GC_REGISTER_ROOT_FIXED (appdomains_list, MONO_ROOT_SOURCE_DOMAIN, "domains list");
domain = mono_domain_create ();
mono_root_domain = domain;
SET_APPDOMAIN (domain);
/* Get a list of runtimes supported by the exe */
if (exe_filename != NULL) {
/*
* This function will load the exe file as a MonoImage. We need to close it, but
* that would mean it would be reloaded later. So instead, we save it to
* exe_image, and close it during shutdown.
*/
get_runtimes_from_exe (exe_filename, &exe_image, runtimes);
#ifdef HOST_WIN32
if (!exe_image) {
exe_image = mono_assembly_open_from_bundle (exe_filename, NULL, FALSE);
if (!exe_image)
exe_image = mono_image_open (exe_filename, NULL);
}
mono_fixup_exe_image (exe_image);
#endif
} else if (runtime_version != NULL) {
runtimes [0] = get_runtime_by_version (runtime_version);
runtimes [1] = NULL;
}
if (runtimes [0] == NULL) {
const MonoRuntimeInfo *default_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
runtimes [0] = default_runtime;
runtimes [1] = NULL;
g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
g_print ("Using default runtime: %s\n", default_runtime->runtime_version);
}
/* The selected runtime will be the first one for which there is a mscrolib.dll */
for (n = 0; runtimes [n] != NULL && ass == NULL; n++) {
current_runtime = runtimes [n];
ass = mono_assembly_load_corlib (current_runtime, &status);
if (status != MONO_IMAGE_OK && status != MONO_IMAGE_ERROR_ERRNO)
break;
}
if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
switch (status){
case MONO_IMAGE_ERROR_ERRNO: {
char *corlib_file = g_build_filename (mono_assembly_getrootdir (), "mono", current_runtime->framework_version, "mscorlib.dll", NULL);
g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
g_print ("It should have been installed in the `%s' directory.\n", corlib_file);
g_free (corlib_file);
break;
}
case MONO_IMAGE_IMAGE_INVALID:
g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
mono_assembly_getrootdir ());
break;
case MONO_IMAGE_MISSING_ASSEMBLYREF:
g_print ("Missing assembly reference in %s/mscorlib.dll\n",
mono_assembly_getrootdir ());
break;
case MONO_IMAGE_OK:
/* to suppress compiler warning */
break;
}
exit (1);
}
mono_defaults.corlib = mono_assembly_get_image (ass);
mono_defaults.object_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Object");
mono_defaults.void_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Void");
mono_defaults.boolean_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Boolean");
mono_defaults.byte_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Byte");
mono_defaults.sbyte_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "SByte");
mono_defaults.int16_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Int16");
mono_defaults.uint16_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "UInt16");
mono_defaults.int32_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Int32");
mono_defaults.uint32_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "UInt32");
mono_defaults.uint_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "UIntPtr");
mono_defaults.int_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "IntPtr");
mono_defaults.int64_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Int64");
mono_defaults.uint64_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "UInt64");
mono_defaults.single_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Single");
mono_defaults.double_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Double");
mono_defaults.char_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Char");
mono_defaults.string_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "String");
mono_defaults.enum_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Enum");
mono_defaults.array_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Array");
mono_defaults.delegate_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Delegate");
mono_defaults.multicastdelegate_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "MulticastDelegate");
mono_defaults.asyncresult_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Runtime.Remoting.Messaging",
"AsyncResult");
mono_defaults.manualresetevent_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Threading", "ManualResetEvent");
mono_defaults.typehandle_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "RuntimeTypeHandle");
mono_defaults.methodhandle_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "RuntimeMethodHandle");
mono_defaults.fieldhandle_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "RuntimeFieldHandle");
mono_defaults.systemtype_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Type");
mono_defaults.runtimetype_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "RuntimeType");
mono_defaults.exception_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Exception");
mono_defaults.threadabortexception_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Threading", "ThreadAbortException");
mono_defaults.thread_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Threading", "Thread");
mono_defaults.internal_thread_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Threading", "InternalThread");
mono_defaults.appdomain_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "AppDomain");
#ifndef DISABLE_REMOTING
mono_defaults.transparent_proxy_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
mono_defaults.real_proxy_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
mono_defaults.marshalbyrefobject_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "MarshalByRefObject");
mono_defaults.iremotingtypeinfo_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
#endif
mono_defaults.mono_method_message_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
mono_defaults.field_info_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Reflection", "FieldInfo");
mono_defaults.method_info_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Reflection", "MethodInfo");
mono_defaults.stringbuilder_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Text", "StringBuilder");
mono_defaults.math_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Math");
mono_defaults.stack_frame_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Diagnostics", "StackFrame");
mono_defaults.stack_trace_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Diagnostics", "StackTrace");
mono_defaults.marshal_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
mono_defaults.typed_reference_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "TypedReference");
mono_defaults.argumenthandle_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "RuntimeArgumentHandle");
mono_defaults.monitor_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Threading", "Monitor");
/*
Not using GENERATE_TRY_GET_CLASS_WITH_CACHE_DECL as this type is heavily checked by sgen when computing finalization.
*/
mono_defaults.critical_finalizer_object = mono_class_try_load_from_name (mono_defaults.corlib,
"System.Runtime.ConstrainedExecution", "CriticalFinalizerObject");
mono_assembly_load_friends (ass);
mono_defaults.handleref_class = mono_class_try_load_from_name (
mono_defaults.corlib, "System.Runtime.InteropServices", "HandleRef");
mono_defaults.attribute_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Attribute");
mono_defaults.customattribute_data_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Reflection", "CustomAttributeData");
mono_class_init (mono_defaults.array_class);
mono_defaults.generic_nullable_class = mono_class_load_from_name (
mono_defaults.corlib, "System", "Nullable`1");
mono_defaults.generic_ilist_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Collections.Generic", "IList`1");
mono_defaults.generic_ireadonlylist_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Collections.Generic", "IReadOnlyList`1");
mono_defaults.threadpool_wait_callback_class = mono_class_load_from_name (
mono_defaults.corlib, "System.Threading", "_ThreadPoolWaitCallback");
mono_defaults.threadpool_perform_wait_callback_method = mono_class_get_method_from_name (
mono_defaults.threadpool_wait_callback_class, "PerformWaitCallback", 0);
domain->friendly_name = g_path_get_basename (filename);
mono_profiler_appdomain_name (domain, domain->friendly_name);
return domain;
}
/**
* mono_init:
*
* Creates the initial application domain and initializes the mono_defaults
* structure.
*
* This function is guaranteed to not run any IL code.
* The runtime is initialized using the default runtime version.
*
* Returns: the initial domain.
*/
MonoDomain *
mono_init (const char *domain_name)
{
return mono_init_internal (domain_name, NULL, DEFAULT_RUNTIME_VERSION);
}
/**
* mono_init_from_assembly:
* @domain_name: name to give to the initial domain
* @filename: filename to load on startup
*
* Used by the runtime, users should use mono_jit_init instead.
*
* Creates the initial application domain and initializes the mono_defaults
* structure.
* This function is guaranteed to not run any IL code.
* The runtime is initialized using the runtime version required by the
* provided executable. The version is determined by looking at the exe
* configuration file and the version PE field)
*
* Returns: the initial domain.
*/
MonoDomain *
mono_init_from_assembly (const char *domain_name, const char *filename)
{
return mono_init_internal (domain_name, filename, NULL);
}
/**
* mono_init_version:
*
* Used by the runtime, users should use mono_jit_init instead.
*
* Creates the initial application domain and initializes the mono_defaults
* structure.
*
* This function is guaranteed to not run any IL code.
* The runtime is initialized using the provided rutime version.
*
* Returns: the initial domain.
*/
MonoDomain *
mono_init_version (const char *domain_name, const char *version)
{
return mono_init_internal (domain_name, NULL, version);
}
/**
* mono_cleanup:
*
* Cleans up all metadata modules.
*/
void
mono_cleanup (void)
{
mono_close_exe_image ();
mono_defaults.corlib = NULL;
mono_config_cleanup ();
mono_loader_cleanup ();
mono_classes_cleanup ();
mono_assemblies_cleanup ();
mono_debug_cleanup ();
mono_images_cleanup ();
mono_metadata_cleanup ();
mono_native_tls_free (appdomain_thread_id);
mono_coop_mutex_destroy (&appdomains_mutex);
#ifndef HOST_WIN32
wapi_cleanup ();
mono_w32handle_cleanup ();
#endif
}
void
mono_close_exe_image (void)
{
if (exe_image)
mono_image_close (exe_image);
}
/**
* mono_get_root_domain:
*
* The root AppDomain is the initial domain created by the runtime when it is
* initialized. Programs execute on this AppDomain, but can create new ones
* later. Currently there is no unmanaged API to create new AppDomains, this
* must be done from managed code.
*
* Returns: the root appdomain, to obtain the current domain, use mono_domain_get ()
*/
MonoDomain*
mono_get_root_domain (void)
{
return mono_root_domain;
}
/**
* mono_domain_get:
*
* This method returns the value of the current MonoDomain that this thread
* and code are running under. To obtain the root domain use
* mono_get_root_domain() API.
*
* Returns: the current domain
*/
MonoDomain *
mono_domain_get ()
{
return GET_APPDOMAIN ();
}
void
mono_domain_unset (void)
{
SET_APPDOMAIN (NULL);
}
void
mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception)
{
MonoInternalThread *thread;
if (mono_domain_get () == domain)
return;
SET_APPDOMAIN (domain);
SET_APPCONTEXT (domain->default_context);
if (migrate_exception) {
thread = mono_thread_internal_current ();
if (!thread->abort_exc)
return;
g_assert (thread->abort_exc->object.vtable->domain != domain);
MONO_OBJECT_SETREF (thread, abort_exc, mono_get_exception_thread_abort ());
g_assert (thread->abort_exc->object.vtable->domain == domain);
}
}
/**
* mono_domain_set_internal:
* @domain: the new domain
*
* Sets the current domain to @domain.
*/
void
mono_domain_set_internal (MonoDomain *domain)
{
mono_domain_set_internal_with_options (domain, TRUE);
}
/**
* mono_domain_foreach:
* @func: function to invoke with the domain data
* @user_data: user-defined pointer that is passed to the supplied @func fo reach domain
*
* Use this method to safely iterate over all the loaded application
* domains in the current runtime. The provided @func is invoked with a
* pointer to the MonoDomain and is given the value of the @user_data
* parameter which can be used to pass state to your called routine.
*/
void
mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
{
int i, size;
MonoDomain **copy;
/*
* Create a copy of the data to avoid calling the user callback
* inside the lock because that could lead to deadlocks.
* We can do this because this function is not perf. critical.
*/