2 * domain.c: MonoDomain functions
5 * Dietmar Maurer (dietmar@ximian.com)
8 * (C) 2001 Ximian, Inc.
16 #include <mono/os/gc_wrapper.h>
18 #include <mono/utils/mono-compiler.h>
19 #include <mono/utils/mono-logger.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/object-internals.h>
22 #include <mono/metadata/domain-internals.h>
23 #include <mono/metadata/class-internals.h>
24 #include <mono/metadata/assembly.h>
25 #include <mono/metadata/exception.h>
26 #include <mono/metadata/cil-coff.h>
27 #include <mono/metadata/rawbuffer.h>
28 #include <mono/metadata/metadata-internals.h>
29 #include <mono/metadata/gc-internal.h>
30 #include <mono/metadata/appdomain.h>
31 #include <mono/metadata/mono-debug-debugger.h>
32 #include <mono/metadata/mono-config.h>
33 #include <mono/metadata/threads-types.h>
34 #include <metadata/threads.h>
36 /* #define DEBUG_DOMAIN_UNLOAD */
38 /* we need to use both the Tls* functions and __thread because
39 * some archs may generate faster jit code with one meachanism
40 * or the other (we used to do it because tls slots were GC-tracked,
41 * but we can't depend on this).
43 static guint32 appdomain_thread_id = -1;
46 static __thread MonoDomain * tls_appdomain MONO_TLS_FAST;
47 #define GET_APPDOMAIN() tls_appdomain
48 #define SET_APPDOMAIN(x) do { \
50 TlsSetValue (appdomain_thread_id, x); \
55 #define GET_APPDOMAIN() ((MonoDomain *)TlsGetValue (appdomain_thread_id))
56 #define SET_APPDOMAIN(x) TlsSetValue (appdomain_thread_id, x);
60 #define GET_APPCONTEXT() (mono_thread_current ()->current_appcontext)
61 #define SET_APPCONTEXT(x) MONO_OBJECT_SETREF (mono_thread_current (), current_appcontext, (x))
63 static guint16 appdomain_list_size = 0;
64 static guint16 appdomain_next = 0;
65 static MonoDomain **appdomains_list = NULL;
67 #define mono_appdomains_lock() EnterCriticalSection (&appdomains_mutex)
68 #define mono_appdomains_unlock() LeaveCriticalSection (&appdomains_mutex)
69 static CRITICAL_SECTION appdomains_mutex;
71 static MonoDomain *mono_root_domain = NULL;
73 /* AppConfigInfo: Information about runtime versions supported by an
77 GSList *supported_runtimes;
78 char *required_runtime;
79 int configuration_count;
84 * AotModuleInfo: Contains information about AOT modules.
91 static const MonoRuntimeInfo *current_runtime = NULL;
93 static MonoJitInfoFindInAot jit_info_find_in_aot_func = NULL;
96 * Contains information about AOT loaded code.
98 static MonoJitInfoTable *aot_modules = NULL;
100 /* This is the list of runtime versions supported by this JIT.
102 static const MonoRuntimeInfo supported_runtimes[] = {
103 {"v1.0.3705", "1.0", { {1,0,5000,0}, {7,0,5000,0} } },
104 {"v1.1.4322", "1.0", { {1,0,5000,0}, {7,0,5000,0} } },
105 {"v2.0.50215","2.0", { {2,0,0,0}, {8,0,0,0} } },
106 {"v2.0.50727","2.0", { {2,0,0,0}, {8,0,0,0} } }
110 /* The stable runtime version */
111 #define DEFAULT_RUNTIME_VERSION "v1.1.4322"
114 get_runtimes_from_exe (const char *exe_file, const MonoRuntimeInfo** runtimes);
116 static const MonoRuntimeInfo*
117 get_runtime_by_version (const char *version);
120 mono_jit_info_find_aot_module (guint8* addr);
123 mono_domain_get_tls_key (void)
125 return appdomain_thread_id;
129 mono_domain_get_tls_offset (void)
132 MONO_THREAD_VAR_OFFSET (tls_appdomain, offset);
133 /* __asm ("jmp 1f; .section writetext, \"awx\"; 1: movl $tls_appdomain@ntpoff, %0; jmp 2f; .previous; 2:"
138 static MonoJitInfoTable *
139 mono_jit_info_table_new (void)
141 return g_array_new (FALSE, FALSE, sizeof (gpointer));
145 mono_jit_info_table_free (MonoJitInfoTable *table)
147 g_array_free (table, TRUE);
151 mono_jit_info_table_index (MonoJitInfoTable *table, char *addr)
153 int left = 0, right = table->len;
155 while (left < right) {
156 int pos = (left + right) / 2;
157 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
158 char *start = ji->code_start;
159 char *end = start + ji->code_size;
163 else if (addr >= end)
173 mono_jit_info_table_find (MonoDomain *domain, char *addr)
175 MonoJitInfoTable *table = domain->jit_info_table;
177 guint left = 0, right;
179 mono_domain_lock (domain);
182 while (left < right) {
183 guint pos = (left + right) / 2;
184 ji = g_array_index (table, gpointer, pos);
186 if (addr < (char*)ji->code_start)
188 else if (addr >= (char*)ji->code_start + ji->code_size)
191 mono_domain_unlock (domain);
195 mono_domain_unlock (domain);
197 /* maybe it is shared code, so we also search in the root domain */
199 if (domain != mono_root_domain)
200 ji = mono_jit_info_table_find (mono_root_domain, addr);
203 /* Maybe its an AOT module */
204 MonoImage *image = mono_jit_info_find_aot_module ((guint8*)addr);
206 ji = jit_info_find_in_aot_func (domain, image, addr);
213 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
215 MonoJitInfoTable *table = domain->jit_info_table;
216 gpointer start = ji->code_start;
219 mono_domain_lock (domain);
220 pos = mono_jit_info_table_index (table, start);
222 g_array_insert_val (table, pos, ji);
223 mono_domain_unlock (domain);
227 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji)
229 MonoJitInfoTable *table = domain->jit_info_table;
230 gpointer start = ji->code_start;
233 mono_domain_lock (domain);
234 pos = mono_jit_info_table_index (table, start);
235 if (g_array_index (table, gpointer, pos) != ji) {
236 MonoJitInfo *ji2 = g_array_index (table, gpointer, pos);
237 g_assert (ji == ji2);
239 g_assert (g_array_index (table, gpointer, pos) == ji);
241 g_array_remove_index (table, pos);
242 mono_domain_unlock (domain);
246 aot_info_table_index (MonoJitInfoTable *table, char *addr)
248 int left = 0, right = table->len;
250 while (left < right) {
251 int pos = (left + right) / 2;
252 AotModuleInfo *ainfo = g_array_index (table, gpointer, pos);
253 char *start = ainfo->start;
254 char *end = ainfo->end;
258 else if (addr >= end)
268 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end)
270 AotModuleInfo *ainfo = g_new0 (AotModuleInfo, 1);
273 ainfo->image = image;
274 ainfo->start = start;
277 mono_appdomains_lock ();
280 aot_modules = mono_jit_info_table_new ();
282 pos = aot_info_table_index (aot_modules, start);
284 g_array_insert_val (aot_modules, pos, ainfo);
286 mono_appdomains_unlock ();
290 mono_jit_info_find_aot_module (guint8* addr)
292 guint left = 0, right;
297 mono_appdomains_lock ();
299 right = aot_modules->len;
300 while (left < right) {
301 guint pos = (left + right) / 2;
302 AotModuleInfo *ai = g_array_index (aot_modules, gpointer, pos);
304 if (addr < (guint8*)ai->start)
306 else if (addr >= (guint8*)ai->end)
309 mono_appdomains_unlock ();
314 mono_appdomains_unlock ();
320 mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func)
322 jit_info_find_in_aot_func = func;
326 mono_jit_info_get_code_start (MonoJitInfo* ji)
328 return ji->code_start;
332 mono_jit_info_get_code_size (MonoJitInfo* ji)
334 return ji->code_size;
338 mono_jit_info_get_method (MonoJitInfo* ji)
344 jit_info_key_extract (gpointer value)
346 MonoJitInfo *info = (MonoJitInfo*)value;
352 jit_info_next_value (gpointer value)
354 MonoJitInfo *info = (MonoJitInfo*)value;
356 return (gpointer*)&info->next_jit_code_hash;
360 mono_jit_code_hash_init (MonoInternalHashTable *jit_code_hash)
362 mono_internal_hash_table_init (jit_code_hash,
363 mono_aligned_addr_hash,
364 jit_info_key_extract,
365 jit_info_next_value);
370 * @s1: First string to compare
371 * @s2: Second string to compare
373 * Returns FALSE if the strings differ.
376 mono_string_equal (MonoString *s1, MonoString *s2)
378 int l1 = mono_string_length (s1);
379 int l2 = mono_string_length (s2);
386 return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1 * 2) == 0;
391 * @s: the string to hash
393 * Returns the hash for the string.
396 mono_string_hash (MonoString *s)
398 const guint16 *p = mono_string_chars (s);
399 int i, len = mono_string_length (s);
402 for (i = 0; i < len; i++) {
403 h = (h << 5) - h + *p;
411 mono_ptrarray_equal (gpointer *s1, gpointer *s2)
413 int len = GPOINTER_TO_INT (s1 [0]);
414 if (len != GPOINTER_TO_INT (s2 [0]))
417 return memcmp (s1 + 1, s2 + 1, len * sizeof(gpointer)) == 0;
421 mono_ptrarray_hash (gpointer *s)
424 int len = GPOINTER_TO_INT (s [0]);
427 for (i = 1; i < len; i++)
428 hash += GPOINTER_TO_UINT (s [i]);
434 * Allocate an id for domain and set domain->domain_id.
435 * LOCKING: must be called while holding appdomains_mutex.
436 * We try to assign low numbers to the domain, so it can be used
437 * as an index in data tables to lookup domain-specific info
438 * with minimal memory overhead. We also try not to reuse the
439 * same id too quickly (to help debugging).
442 domain_id_alloc (MonoDomain *domain)
445 if (!appdomains_list) {
446 appdomain_list_size = 2;
447 appdomains_list = mono_gc_alloc_fixed (appdomain_list_size * sizeof (void*), NULL);
449 for (i = appdomain_next; i < appdomain_list_size; ++i) {
450 if (!appdomains_list [i]) {
456 for (i = 0; i < appdomain_next; ++i) {
457 if (!appdomains_list [i]) {
464 MonoDomain **new_list;
465 int new_size = appdomain_list_size * 2;
466 if (new_size >= (1 << 16))
467 g_assert_not_reached ();
468 id = appdomain_list_size;
469 new_list = mono_gc_alloc_fixed (new_size * sizeof (void*), NULL);
470 memcpy (new_list, appdomains_list, appdomain_list_size * sizeof (void*));
471 mono_gc_free_fixed (appdomains_list);
472 appdomains_list = new_list;
473 appdomain_list_size = new_size;
475 domain->domain_id = id;
476 appdomains_list [id] = domain;
478 if (appdomain_next > appdomain_list_size)
483 static guint32 domain_gc_bitmap [sizeof(MonoDomain)/4/32 + 1];
484 static gpointer domain_gc_desc = NULL;
485 static guint32 domain_shadow_serial = 0L;
488 mono_domain_create (void)
491 guint32 shadow_serial;
493 mono_appdomains_lock ();
494 shadow_serial = domain_shadow_serial++;
496 if (!domain_gc_desc) {
497 unsigned int i, bit = 0;
498 for (i = G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_FIRST_OBJECT); i < G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_FIRST_GC_TRACKED); i += sizeof (gpointer)) {
499 bit = i / sizeof (gpointer);
500 domain_gc_bitmap [bit / 32] |= 1 << (bit % 32);
502 domain_gc_desc = mono_gc_make_descr_from_bitmap (domain_gc_bitmap, bit + 1);
504 mono_appdomains_unlock ();
506 domain = mono_gc_alloc_fixed (sizeof (MonoDomain), domain_gc_desc);
507 domain->shadow_serial = shadow_serial;
508 domain->domain = NULL;
509 domain->setup = NULL;
510 domain->friendly_name = NULL;
511 domain->search_path = NULL;
513 domain->mp = mono_mempool_new ();
514 domain->code_mp = mono_code_manager_new ();
515 domain->env = mono_g_hash_table_new_type ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal, MONO_HASH_KEY_VALUE_GC);
516 domain->domain_assemblies = NULL;
517 domain->class_vtable_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
518 domain->proxy_vtable_hash = g_hash_table_new ((GHashFunc)mono_ptrarray_hash, (GCompareFunc)mono_ptrarray_equal);
519 domain->static_data_array = NULL;
520 mono_jit_code_hash_init (&domain->jit_code_hash);
521 domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
522 domain->jit_info_table = mono_jit_info_table_new ();
523 domain->class_init_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
524 domain->jump_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
525 domain->finalizable_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
526 domain->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
527 domain->delegate_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
529 InitializeCriticalSection (&domain->lock);
530 InitializeCriticalSection (&domain->assemblies_lock);
532 mono_appdomains_lock ();
533 domain_id_alloc (domain);
534 mono_appdomains_unlock ();
540 * mono_init_internal:
542 * Creates the initial application domain and initializes the mono_defaults
544 * This function is guaranteed to not run any IL code.
545 * If exe_filename is not NULL, the method will determine the required runtime
546 * from the exe configuration file or the version PE field.
547 * If runtime_version is not NULL, that runtime version will be used.
548 * Either exe_filename or runtime_version must be provided.
550 * Returns: the initial domain.
553 mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
555 static MonoDomain *domain = NULL;
556 MonoAssembly *ass = NULL;
557 MonoImageOpenStatus status = MONO_IMAGE_OK;
558 const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
562 g_assert_not_reached ();
566 appdomain_thread_id = TlsAlloc ();
568 InitializeCriticalSection (&appdomains_mutex);
570 mono_metadata_init ();
571 mono_raw_buffer_init ();
573 mono_assemblies_init ();
574 mono_classes_init ();
576 mono_reflection_init ();
578 /* FIXME: When should we release this memory? */
579 MONO_GC_REGISTER_ROOT (appdomains_list);
581 domain = mono_domain_create ();
582 mono_root_domain = domain;
584 SET_APPDOMAIN (domain);
586 /* Get a list of runtimes supported by the exe */
587 if (exe_filename != NULL) {
588 get_runtimes_from_exe (exe_filename, runtimes);
589 } else if (runtime_version != NULL) {
590 runtimes [0] = get_runtime_by_version (runtime_version);
594 if (runtimes [0] == NULL) {
595 const MonoRuntimeInfo *default_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
596 runtimes [0] = default_runtime;
598 g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
599 g_print ("Using default runtime: %s\n", default_runtime->runtime_version);
602 /* The selected runtime will be the first one for which there is a mscrolib.dll */
603 for (n = 0; runtimes [n] != NULL && ass == NULL; n++) {
604 current_runtime = runtimes [n];
605 ass = mono_assembly_load_corlib (current_runtime, &status);
606 if (status != MONO_IMAGE_OK && status != MONO_IMAGE_ERROR_ERRNO)
611 if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
613 case MONO_IMAGE_ERROR_ERRNO: {
614 char *corlib_file = g_build_filename (mono_assembly_getrootdir (), "mono", current_runtime->framework_version, "mscorlib.dll", NULL);
615 g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
616 g_print ("It should have been installed in the `%s' directory.\n", corlib_file);
617 g_free (corlib_file);
620 case MONO_IMAGE_IMAGE_INVALID:
621 g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
622 mono_assembly_getrootdir ());
624 case MONO_IMAGE_MISSING_ASSEMBLYREF:
625 g_print ("Missing assembly reference in %s/mscorlib.dll\n",
626 mono_assembly_getrootdir ());
629 /* to suppress compiler warning */
635 mono_defaults.corlib = mono_assembly_get_image (ass);
637 mono_defaults.object_class = mono_class_from_name (
638 mono_defaults.corlib, "System", "Object");
639 g_assert (mono_defaults.object_class != 0);
641 mono_defaults.void_class = mono_class_from_name (
642 mono_defaults.corlib, "System", "Void");
643 g_assert (mono_defaults.void_class != 0);
645 mono_defaults.boolean_class = mono_class_from_name (
646 mono_defaults.corlib, "System", "Boolean");
647 g_assert (mono_defaults.boolean_class != 0);
649 mono_defaults.byte_class = mono_class_from_name (
650 mono_defaults.corlib, "System", "Byte");
651 g_assert (mono_defaults.byte_class != 0);
653 mono_defaults.sbyte_class = mono_class_from_name (
654 mono_defaults.corlib, "System", "SByte");
655 g_assert (mono_defaults.sbyte_class != 0);
657 mono_defaults.int16_class = mono_class_from_name (
658 mono_defaults.corlib, "System", "Int16");
659 g_assert (mono_defaults.int16_class != 0);
661 mono_defaults.uint16_class = mono_class_from_name (
662 mono_defaults.corlib, "System", "UInt16");
663 g_assert (mono_defaults.uint16_class != 0);
665 mono_defaults.int32_class = mono_class_from_name (
666 mono_defaults.corlib, "System", "Int32");
667 g_assert (mono_defaults.int32_class != 0);
669 mono_defaults.uint32_class = mono_class_from_name (
670 mono_defaults.corlib, "System", "UInt32");
671 g_assert (mono_defaults.uint32_class != 0);
673 mono_defaults.uint_class = mono_class_from_name (
674 mono_defaults.corlib, "System", "UIntPtr");
675 g_assert (mono_defaults.uint_class != 0);
677 mono_defaults.int_class = mono_class_from_name (
678 mono_defaults.corlib, "System", "IntPtr");
679 g_assert (mono_defaults.int_class != 0);
681 mono_defaults.int64_class = mono_class_from_name (
682 mono_defaults.corlib, "System", "Int64");
683 g_assert (mono_defaults.int64_class != 0);
685 mono_defaults.uint64_class = mono_class_from_name (
686 mono_defaults.corlib, "System", "UInt64");
687 g_assert (mono_defaults.uint64_class != 0);
689 mono_defaults.single_class = mono_class_from_name (
690 mono_defaults.corlib, "System", "Single");
691 g_assert (mono_defaults.single_class != 0);
693 mono_defaults.double_class = mono_class_from_name (
694 mono_defaults.corlib, "System", "Double");
695 g_assert (mono_defaults.double_class != 0);
697 mono_defaults.char_class = mono_class_from_name (
698 mono_defaults.corlib, "System", "Char");
699 g_assert (mono_defaults.char_class != 0);
701 mono_defaults.string_class = mono_class_from_name (
702 mono_defaults.corlib, "System", "String");
703 g_assert (mono_defaults.string_class != 0);
705 mono_defaults.enum_class = mono_class_from_name (
706 mono_defaults.corlib, "System", "Enum");
707 g_assert (mono_defaults.enum_class != 0);
709 mono_defaults.array_class = mono_class_from_name (
710 mono_defaults.corlib, "System", "Array");
711 g_assert (mono_defaults.array_class != 0);
713 mono_defaults.delegate_class = mono_class_from_name (
714 mono_defaults.corlib, "System", "Delegate");
715 g_assert (mono_defaults.delegate_class != 0 );
717 mono_defaults.multicastdelegate_class = mono_class_from_name (
718 mono_defaults.corlib, "System", "MulticastDelegate");
719 g_assert (mono_defaults.multicastdelegate_class != 0 );
721 mono_defaults.asyncresult_class = mono_class_from_name (
722 mono_defaults.corlib, "System.Runtime.Remoting.Messaging",
724 g_assert (mono_defaults.asyncresult_class != 0 );
726 mono_defaults.waithandle_class = mono_class_from_name (
727 mono_defaults.corlib, "System.Threading", "WaitHandle");
728 g_assert (mono_defaults.waithandle_class != 0 );
730 mono_defaults.typehandle_class = mono_class_from_name (
731 mono_defaults.corlib, "System", "RuntimeTypeHandle");
732 g_assert (mono_defaults.typehandle_class != 0);
734 mono_defaults.methodhandle_class = mono_class_from_name (
735 mono_defaults.corlib, "System", "RuntimeMethodHandle");
736 g_assert (mono_defaults.methodhandle_class != 0);
738 mono_defaults.fieldhandle_class = mono_class_from_name (
739 mono_defaults.corlib, "System", "RuntimeFieldHandle");
740 g_assert (mono_defaults.fieldhandle_class != 0);
742 mono_defaults.systemtype_class = mono_class_from_name (
743 mono_defaults.corlib, "System", "Type");
744 g_assert (mono_defaults.systemtype_class != 0);
746 mono_defaults.monotype_class = mono_class_from_name (
747 mono_defaults.corlib, "System", "MonoType");
748 g_assert (mono_defaults.monotype_class != 0);
750 mono_defaults.exception_class = mono_class_from_name (
751 mono_defaults.corlib, "System", "Exception");
752 g_assert (mono_defaults.exception_class != 0);
754 mono_defaults.threadabortexception_class = mono_class_from_name (
755 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
756 g_assert (mono_defaults.threadabortexception_class != 0);
758 mono_defaults.thread_class = mono_class_from_name (
759 mono_defaults.corlib, "System.Threading", "Thread");
760 g_assert (mono_defaults.thread_class != 0);
762 mono_defaults.appdomain_class = mono_class_from_name (
763 mono_defaults.corlib, "System", "AppDomain");
764 g_assert (mono_defaults.appdomain_class != 0);
766 mono_defaults.transparent_proxy_class = mono_class_from_name (
767 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
768 g_assert (mono_defaults.transparent_proxy_class != 0);
770 mono_defaults.real_proxy_class = mono_class_from_name (
771 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
772 g_assert (mono_defaults.real_proxy_class != 0);
774 mono_defaults.mono_method_message_class = mono_class_from_name (
775 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
776 g_assert (mono_defaults.mono_method_message_class != 0);
778 mono_defaults.field_info_class = mono_class_from_name (
779 mono_defaults.corlib, "System.Reflection", "FieldInfo");
780 g_assert (mono_defaults.field_info_class != 0);
782 mono_defaults.method_info_class = mono_class_from_name (
783 mono_defaults.corlib, "System.Reflection", "MethodInfo");
784 g_assert (mono_defaults.method_info_class != 0);
786 mono_defaults.stringbuilder_class = mono_class_from_name (
787 mono_defaults.corlib, "System.Text", "StringBuilder");
788 g_assert (mono_defaults.stringbuilder_class != 0);
790 mono_defaults.math_class = mono_class_from_name (
791 mono_defaults.corlib, "System", "Math");
792 g_assert (mono_defaults.math_class != 0);
794 mono_defaults.stack_frame_class = mono_class_from_name (
795 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
796 g_assert (mono_defaults.stack_frame_class != 0);
798 mono_defaults.stack_trace_class = mono_class_from_name (
799 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
800 g_assert (mono_defaults.stack_trace_class != 0);
802 mono_defaults.marshal_class = mono_class_from_name (
803 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
804 g_assert (mono_defaults.marshal_class != 0);
806 mono_defaults.iserializeable_class = mono_class_from_name (
807 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
808 g_assert (mono_defaults.iserializeable_class != 0);
810 mono_defaults.serializationinfo_class = mono_class_from_name (
811 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
812 g_assert (mono_defaults.serializationinfo_class != 0);
814 mono_defaults.streamingcontext_class = mono_class_from_name (
815 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
816 g_assert (mono_defaults.streamingcontext_class != 0);
818 mono_defaults.typed_reference_class = mono_class_from_name (
819 mono_defaults.corlib, "System", "TypedReference");
820 g_assert (mono_defaults.typed_reference_class != 0);
822 mono_defaults.argumenthandle_class = mono_class_from_name (
823 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
824 g_assert (mono_defaults.argumenthandle_class != 0);
826 mono_defaults.marshalbyrefobject_class = mono_class_from_name (
827 mono_defaults.corlib, "System", "MarshalByRefObject");
828 g_assert (mono_defaults.marshalbyrefobject_class != 0);
830 mono_defaults.monitor_class = mono_class_from_name (
831 mono_defaults.corlib, "System.Threading", "Monitor");
832 g_assert (mono_defaults.monitor_class != 0);
834 mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
835 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
836 g_assert (mono_defaults.iremotingtypeinfo_class != 0);
838 mono_defaults.runtimesecurityframe_class = mono_class_from_name (
839 mono_defaults.corlib, "System.Security", "RuntimeSecurityFrame");
841 mono_defaults.executioncontext_class = mono_class_from_name (
842 mono_defaults.corlib, "System.Threading", "ExecutionContext");
844 mono_defaults.internals_visible_class = mono_class_from_name (
845 mono_defaults.corlib, "System.Runtime.CompilerServices", "InternalsVisibleToAttribute");
848 * mscorlib needs a little help, only now it can load its friends list (after we have
849 * loaded the InternalsVisibleToAttribute), load it now
851 mono_assembly_load_friends (ass);
853 mono_defaults.safehandle_class = mono_class_from_name (
854 mono_defaults.corlib, "System.Runtime.InteropServices", "SafeHandle");
856 mono_defaults.handleref_class = mono_class_from_name (
857 mono_defaults.corlib, "System.Runtime.InteropServices", "HandleRef");
859 mono_defaults.attribute_class = mono_class_from_name (
860 mono_defaults.corlib, "System", "Attribute");
862 mono_defaults.customattribute_data_class = mono_class_from_name (
863 mono_defaults.corlib, "System.Reflection", "CustomAttributeData");
865 /* these are initialized lazily when COM features are used */
866 mono_defaults.variant_class = NULL;
867 mono_defaults.com_object_class = NULL;
868 mono_defaults.com_interop_proxy_class = NULL;
869 mono_defaults.iunknown_class = NULL;
870 mono_defaults.idispatch_class = NULL;
873 * Note that mono_defaults.generic_*_class is only non-NULL if we're
874 * using the 2.0 corlib.
876 mono_class_init (mono_defaults.array_class);
877 mono_defaults.generic_nullable_class = mono_class_from_name (
878 mono_defaults.corlib, "System", "Nullable`1");
879 mono_defaults.generic_ilist_class = mono_class_from_name (
880 mono_defaults.corlib, "System.Collections.Generic", "IList`1");
882 domain->friendly_name = g_path_get_basename (filename);
890 * Creates the initial application domain and initializes the mono_defaults
892 * This function is guaranteed to not run any IL code.
893 * The runtime is initialized using the default runtime version.
895 * Returns: the initial domain.
898 mono_init (const char *domain_name)
900 return mono_init_internal (domain_name, NULL, DEFAULT_RUNTIME_VERSION);
904 * mono_init_from_assembly:
906 * Creates the initial application domain and initializes the mono_defaults
908 * This function is guaranteed to not run any IL code.
909 * The runtime is initialized using the runtime version required by the
910 * provided executable. The version is determined by looking at the exe
911 * configuration file and the version PE field)
913 * Returns: the initial domain.
916 mono_init_from_assembly (const char *domain_name, const char *filename)
918 return mono_init_internal (domain_name, filename, NULL);
924 * Creates the initial application domain and initializes the mono_defaults
926 * This function is guaranteed to not run any IL code.
927 * The runtime is initialized using the provided rutime version.
929 * Returns: the initial domain.
932 mono_init_version (const char *domain_name, const char *version)
934 return mono_init_internal (domain_name, NULL, version);
938 * mono_init_com_types:
940 * Initializes all types needed for COM Interop in mono_defaults structure.
943 mono_init_com_types (void)
945 static gboolean initialized = FALSE;
950 /* FIXME: do I need some threading protection here */
952 g_assert (mono_defaults.corlib);
954 mono_defaults.variant_class = mono_class_from_name (
955 mono_defaults.corlib, "System", "Variant");
956 g_assert (mono_defaults.variant_class != 0);
958 mono_defaults.com_object_class = mono_class_from_name (
959 mono_defaults.corlib, "System", "__ComObject");
960 g_assert (mono_defaults.com_object_class != 0);
962 mono_defaults.com_interop_proxy_class = mono_class_from_name (
963 mono_defaults.corlib, "Mono.Interop", "ComInteropProxy");
964 g_assert (mono_defaults.com_interop_proxy_class != 0);
966 mono_defaults.iunknown_class = mono_class_from_name (
967 mono_defaults.corlib, "Mono.Interop", "IUnknown");
968 g_assert (mono_defaults.iunknown_class != 0);
970 mono_defaults.idispatch_class = mono_class_from_name (
971 mono_defaults.corlib, "Mono.Interop", "IDispatch");
972 g_assert (mono_defaults.idispatch_class != 0);
980 * Cleans up all metadata modules.
985 mono_loader_cleanup ();
986 mono_classes_cleanup ();
987 mono_assemblies_cleanup ();
988 mono_images_cleanup ();
989 mono_raw_buffer_cleanup ();
990 mono_metadata_cleanup ();
992 TlsFree (appdomain_thread_id);
993 DeleteCriticalSection (&appdomains_mutex);
997 * mono_get_root_domain:
999 * The root AppDomain is the initial domain created by the runtime when it is
1000 * initialized. Programs execute on this AppDomain, but can create new ones
1001 * later. Currently there is no unmanaged API to create new AppDomains, this
1002 * must be done from managed code.
1004 * Returns: the root appdomain, to obtain the current domain, use mono_domain_get ()
1007 mono_get_root_domain (void)
1009 return mono_root_domain;
1015 * Returns: the current domain, to obtain the root domain use
1016 * mono_get_root_domain().
1021 return GET_APPDOMAIN ();
1025 * mono_domain_set_internal:
1026 * @domain: the new domain
1028 * Sets the current domain to @domain.
1031 mono_domain_set_internal (MonoDomain *domain)
1033 SET_APPDOMAIN (domain);
1034 SET_APPCONTEXT (domain->default_context);
1038 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
1044 * Create a copy of the data to avoid calling the user callback
1045 * inside the lock because that could lead to deadlocks.
1046 * We can do this because this function is not perf. critical.
1048 mono_appdomains_lock ();
1049 size = appdomain_list_size;
1050 copy = mono_gc_alloc_fixed (appdomain_list_size * sizeof (void*), NULL);
1051 memcpy (copy, appdomains_list, appdomain_list_size * sizeof (void*));
1052 mono_appdomains_unlock ();
1054 for (i = 0; i < size; ++i) {
1056 func (copy [i], user_data);
1059 mono_gc_free_fixed (copy);
1063 * mono_domain_assembly_open:
1064 * @domain: the application domain
1065 * @name: file name of the assembly
1067 * fixme: maybe we should integrate this with mono_assembly_open ??
1070 mono_domain_assembly_open (MonoDomain *domain, const char *name)
1075 mono_domain_assemblies_lock (domain);
1076 for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
1078 if (strcmp (name, ass->aname.name) == 0) {
1079 mono_domain_assemblies_unlock (domain);
1083 mono_domain_assemblies_unlock (domain);
1085 if (!(ass = mono_assembly_open (name, NULL)))
1092 dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
1094 MonoJitDynamicMethodInfo *di = value;
1095 mono_code_manager_destroy (di->code_mp);
1100 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
1102 g_slist_free (value);
1106 mono_domain_free (MonoDomain *domain, gboolean force)
1109 if ((domain == mono_root_domain) && !force) {
1110 g_warning ("cant unload root domain");
1114 mono_debugger_event (MONO_DEBUGGER_EVENT_DOMAIN_UNLOAD, domain->domain_id, 0);
1116 mono_appdomains_lock ();
1117 appdomains_list [domain->domain_id] = NULL;
1118 mono_appdomains_unlock ();
1120 /* FIXME: free delegate_hash_table when it's used */
1121 if (domain->search_path) {
1122 g_strfreev (domain->search_path);
1123 domain->search_path = NULL;
1125 domain->create_proxy_for_type_method = NULL;
1126 domain->private_invoke_method = NULL;
1127 domain->default_context = NULL;
1128 domain->out_of_memory_ex = NULL;
1129 domain->null_reference_ex = NULL;
1130 domain->stack_overflow_ex = NULL;
1131 domain->entry_assembly = NULL;
1132 /* must do this early as it accesses fields and types */
1133 if (domain->special_static_fields) {
1134 mono_alloc_special_static_data_free (domain->special_static_fields);
1135 g_hash_table_destroy (domain->special_static_fields);
1136 domain->special_static_fields = NULL;
1138 for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
1139 MonoAssembly *ass = tmp->data;
1140 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading domain %s %p, assembly %s %p, refcount=%d\n", domain->friendly_name, domain, ass->aname.name, ass, ass->ref_count);
1141 mono_assembly_close (ass);
1143 g_slist_free (domain->domain_assemblies);
1144 domain->domain_assemblies = NULL;
1146 g_free (domain->friendly_name);
1147 domain->friendly_name = NULL;
1148 mono_g_hash_table_destroy (domain->env);
1150 g_hash_table_destroy (domain->class_vtable_hash);
1151 domain->class_vtable_hash = NULL;
1152 g_hash_table_destroy (domain->proxy_vtable_hash);
1153 domain->proxy_vtable_hash = NULL;
1154 if (domain->static_data_array) {
1155 mono_gc_free_fixed (domain->static_data_array);
1156 domain->static_data_array = NULL;
1158 mono_internal_hash_table_destroy (&domain->jit_code_hash);
1159 if (domain->dynamic_code_hash) {
1160 g_hash_table_foreach (domain->dynamic_code_hash, dynamic_method_info_free, NULL);
1161 g_hash_table_destroy (domain->dynamic_code_hash);
1162 domain->dynamic_code_hash = NULL;
1164 mono_g_hash_table_destroy (domain->ldstr_table);
1165 domain->ldstr_table = NULL;
1166 mono_jit_info_table_free (domain->jit_info_table);
1167 domain->jit_info_table = NULL;
1168 #ifdef DEBUG_DOMAIN_UNLOAD
1169 mono_mempool_invalidate (domain->mp);
1170 mono_code_manager_invalidate (domain->code_mp);
1172 mono_mempool_destroy (domain->mp);
1174 mono_code_manager_destroy (domain->code_mp);
1175 domain->code_mp = NULL;
1177 if (domain->jump_target_hash) {
1178 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
1179 g_hash_table_destroy (domain->jump_target_hash);
1180 domain->jump_target_hash = NULL;
1182 if (domain->type_hash) {
1183 mono_g_hash_table_destroy (domain->type_hash);
1184 domain->type_hash = NULL;
1186 if (domain->refobject_hash) {
1187 mono_g_hash_table_destroy (domain->refobject_hash);
1188 domain->refobject_hash = NULL;
1190 if (domain->type_init_exception_hash) {
1191 mono_g_hash_table_destroy (domain->type_init_exception_hash);
1192 domain->type_init_exception_hash = NULL;
1194 g_hash_table_destroy (domain->class_init_trampoline_hash);
1195 domain->class_init_trampoline_hash = NULL;
1196 g_hash_table_destroy (domain->jump_trampoline_hash);
1197 domain->jump_trampoline_hash = NULL;
1198 g_hash_table_destroy (domain->finalizable_objects_hash);
1199 domain->finalizable_objects_hash = NULL;
1200 g_hash_table_destroy (domain->jit_trampoline_hash);
1201 domain->jit_trampoline_hash = NULL;
1202 g_hash_table_destroy (domain->delegate_trampoline_hash);
1203 domain->delegate_trampoline_hash = NULL;
1204 if (domain->delegate_invoke_impl_with_target_hash) {
1205 g_hash_table_destroy (domain->delegate_invoke_impl_with_target_hash);
1206 domain->delegate_invoke_impl_with_target_hash = NULL;
1208 if (domain->delegate_invoke_impl_no_target_hash) {
1209 g_hash_table_destroy (domain->delegate_invoke_impl_no_target_hash);
1210 domain->delegate_invoke_impl_no_target_hash = NULL;
1212 DeleteCriticalSection (&domain->assemblies_lock);
1213 DeleteCriticalSection (&domain->lock);
1214 domain->setup = NULL;
1216 /* FIXME: anything else required ? */
1218 mono_gc_free_fixed (domain);
1220 if ((domain == mono_root_domain))
1221 mono_root_domain = NULL;
1225 * mono_domain_get_id:
1228 * Returns: the a domain for a specific domain id.
1231 mono_domain_get_by_id (gint32 domainid)
1233 MonoDomain * domain;
1235 mono_appdomains_lock ();
1236 if (domainid < appdomain_list_size)
1237 domain = appdomains_list [domainid];
1240 mono_appdomains_unlock ();
1246 mono_domain_get_id (MonoDomain *domain)
1248 return domain->domain_id;
1252 mono_context_set (MonoAppContext * new_context)
1254 SET_APPCONTEXT (new_context);
1258 mono_context_get (void)
1260 return GET_APPCONTEXT ();
1263 /* LOCKING: the caller holds the lock for this domain */
1265 mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap)
1267 /* The first entry in the array is the index of the next free slot
1268 * and the total size of the array
1271 if (domain->static_data_array) {
1272 int size = GPOINTER_TO_INT (domain->static_data_array [1]);
1273 next = GPOINTER_TO_INT (domain->static_data_array [0]);
1275 gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * (size * 2), NULL);
1276 memcpy (new_array, domain->static_data_array, sizeof (gpointer) * size);
1278 new_array [1] = GINT_TO_POINTER (size);
1279 mono_gc_free_fixed (domain->static_data_array);
1280 domain->static_data_array = new_array;
1284 gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * size, NULL);
1286 new_array [0] = GINT_TO_POINTER (next);
1287 new_array [1] = GINT_TO_POINTER (size);
1288 domain->static_data_array = new_array;
1290 domain->static_data_array [next++] = data;
1291 domain->static_data_array [0] = GINT_TO_POINTER (next);
1295 mono_get_corlib (void)
1297 return mono_defaults.corlib;
1301 mono_get_object_class (void)
1303 return mono_defaults.object_class;
1307 mono_get_byte_class (void)
1309 return mono_defaults.byte_class;
1313 mono_get_void_class (void)
1315 return mono_defaults.void_class;
1319 mono_get_boolean_class (void)
1321 return mono_defaults.boolean_class;
1325 mono_get_sbyte_class (void)
1327 return mono_defaults.sbyte_class;
1331 mono_get_int16_class (void)
1333 return mono_defaults.int16_class;
1337 mono_get_uint16_class (void)
1339 return mono_defaults.uint16_class;
1343 mono_get_int32_class (void)
1345 return mono_defaults.int32_class;
1349 mono_get_uint32_class (void)
1351 return mono_defaults.uint32_class;
1355 mono_get_intptr_class (void)
1357 return mono_defaults.int_class;
1361 mono_get_uintptr_class (void)
1363 return mono_defaults.uint_class;
1367 mono_get_int64_class (void)
1369 return mono_defaults.int64_class;
1373 mono_get_uint64_class (void)
1375 return mono_defaults.uint64_class;
1379 mono_get_single_class (void)
1381 return mono_defaults.single_class;
1385 mono_get_double_class (void)
1387 return mono_defaults.double_class;
1391 mono_get_char_class (void)
1393 return mono_defaults.char_class;
1397 mono_get_string_class (void)
1399 return mono_defaults.string_class;
1403 mono_get_enum_class (void)
1405 return mono_defaults.enum_class;
1409 mono_get_array_class (void)
1411 return mono_defaults.array_class;
1415 mono_get_thread_class (void)
1417 return mono_defaults.thread_class;
1421 mono_get_exception_class (void)
1423 return mono_defaults.exception_class;
1427 static char* get_attribute_value (const gchar **attribute_names,
1428 const gchar **attribute_values,
1429 const char *att_name)
1432 for (n=0; attribute_names[n] != NULL; n++) {
1433 if (strcmp (attribute_names[n], att_name) == 0)
1434 return g_strdup (attribute_values[n]);
1439 static void start_element (GMarkupParseContext *context,
1440 const gchar *element_name,
1441 const gchar **attribute_names,
1442 const gchar **attribute_values,
1446 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1448 if (strcmp (element_name, "configuration") == 0) {
1449 app_config->configuration_count++;
1452 if (strcmp (element_name, "startup") == 0) {
1453 app_config->startup_count++;
1457 if (app_config->configuration_count != 1 || app_config->startup_count != 1)
1460 if (strcmp (element_name, "requiredRuntime") == 0) {
1461 app_config->required_runtime = get_attribute_value (attribute_names, attribute_values, "version");
1462 } else if (strcmp (element_name, "supportedRuntime") == 0) {
1463 char *version = get_attribute_value (attribute_names, attribute_values, "version");
1464 app_config->supported_runtimes = g_slist_append (app_config->supported_runtimes, version);
1468 static void end_element (GMarkupParseContext *context,
1469 const gchar *element_name,
1473 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1475 if (strcmp (element_name, "configuration") == 0) {
1476 app_config->configuration_count--;
1477 } else if (strcmp (element_name, "startup") == 0) {
1478 app_config->startup_count--;
1482 static const GMarkupParser
1491 static AppConfigInfo *
1492 app_config_parse (const char *exe_filename)
1494 AppConfigInfo *app_config;
1495 GMarkupParseContext *context;
1499 const char *bundled_config;
1500 char *config_filename;
1502 bundled_config = mono_config_string_for_assembly_file (exe_filename);
1504 if (bundled_config) {
1505 text = g_strdup (bundled_config);
1506 len = strlen (text);
1508 config_filename = g_strconcat (exe_filename, ".config", NULL);
1510 if (stat (config_filename, &buf) != 0) {
1511 g_free (config_filename);
1515 if (!g_file_get_contents (config_filename, &text, &len, NULL)) {
1516 g_free (config_filename);
1519 g_free (config_filename);
1522 app_config = g_new0 (AppConfigInfo, 1);
1524 context = g_markup_parse_context_new (&mono_parser, 0, app_config, NULL);
1525 if (g_markup_parse_context_parse (context, text, len, NULL)) {
1526 g_markup_parse_context_end_parse (context, NULL);
1528 g_markup_parse_context_free (context);
1534 app_config_free (AppConfigInfo* app_config)
1537 GSList *list = app_config->supported_runtimes;
1538 while (list != NULL) {
1539 rt = (char*)list->data;
1541 list = g_slist_next (list);
1543 g_slist_free (app_config->supported_runtimes);
1544 g_free (app_config->required_runtime);
1545 g_free (app_config);
1549 static const MonoRuntimeInfo*
1550 get_runtime_by_version (const char *version)
1553 int max = G_N_ELEMENTS (supported_runtimes);
1555 for (n=0; n<max; n++) {
1556 if (strcmp (version, supported_runtimes[n].runtime_version) == 0)
1557 return &supported_runtimes[n];
1563 get_runtimes_from_exe (const char *exe_file, const MonoRuntimeInfo** runtimes)
1565 AppConfigInfo* app_config;
1567 const MonoRuntimeInfo* runtime = NULL;
1568 MonoImage *image = NULL;
1570 app_config = app_config_parse (exe_file);
1572 if (app_config != NULL) {
1573 /* Check supportedRuntime elements, if none is supported, fail.
1574 * If there are no such elements, look for a requiredRuntime element.
1576 if (app_config->supported_runtimes != NULL) {
1578 GSList *list = app_config->supported_runtimes;
1579 while (list != NULL) {
1580 version = (char*) list->data;
1581 runtime = get_runtime_by_version (version);
1582 if (runtime != NULL)
1583 runtimes [n++] = runtime;
1584 list = g_slist_next (list);
1586 runtimes [n] = NULL;
1587 app_config_free (app_config);
1591 /* Check the requiredRuntime element. This is for 1.0 apps only. */
1592 if (app_config->required_runtime != NULL) {
1593 runtimes [0] = get_runtime_by_version (app_config->required_runtime);
1594 runtimes [1] = NULL;
1595 app_config_free (app_config);
1598 app_config_free (app_config);
1601 /* Look for a runtime with the exact version */
1602 image = mono_assembly_open_from_bundle (exe_file, NULL, FALSE);
1605 image = mono_image_open (exe_file, NULL);
1607 if (image == NULL) {
1608 /* The image is wrong or the file was not found. In this case return
1609 * a default runtime and leave to the initialization method the work of
1610 * reporting the error.
1612 runtimes [0] = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
1613 runtimes [1] = NULL;
1618 * FIXME: This would cause us to unload the image, and it will be loaded again later.
1619 * Disabling it will mean the initial exe will not be unloaded on shutdown.
1621 //mono_image_close (image);
1623 runtimes [0] = get_runtime_by_version (image->version);
1624 runtimes [1] = NULL;
1629 * mono_get_runtime_info:
1631 * Returns: the version of the current runtime instance.
1633 const MonoRuntimeInfo*
1634 mono_get_runtime_info (void)
1636 return current_runtime;
1640 mono_debugger_check_runtime_version (const char *filename)
1642 const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
1643 const MonoRuntimeInfo *rinfo;
1645 get_runtimes_from_exe (filename, runtimes);
1646 rinfo = runtimes [0];
1649 return g_strdup_printf ("Cannot get runtime version from assembly `%s'", filename);
1651 if (rinfo != current_runtime)
1652 return g_strdup_printf ("The Mono Debugger is currently using the `%s' runtime, but "
1653 "the assembly `%s' requires version `%s'", current_runtime->runtime_version,
1654 filename, rinfo->runtime_version);