3 * domain.c: MonoDomain functions
6 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2001 Ximian, Inc.
17 #include <mono/os/gc_wrapper.h>
19 #include <mono/utils/mono-compiler.h>
20 #include <mono/utils/mono-logger.h>
21 #include <mono/metadata/object.h>
22 #include <mono/metadata/object-internals.h>
23 #include <mono/metadata/domain-internals.h>
24 #include <mono/metadata/class-internals.h>
25 #include <mono/metadata/assembly.h>
26 #include <mono/metadata/exception.h>
27 #include <mono/metadata/cil-coff.h>
28 #include <mono/metadata/rawbuffer.h>
29 #include <mono/metadata/metadata-internals.h>
30 #include <mono/metadata/gc-internal.h>
31 #include <mono/metadata/appdomain.h>
32 #include <mono/metadata/mono-debug-debugger.h>
33 #include <metadata/threads.h>
35 /* #define DEBUG_DOMAIN_UNLOAD */
37 /* we need to use both the Tls* functions and __thread because
38 * some archs may generate faster jit code with one meachanism
39 * or the other (we used to do it because tls slots were GC-tracked,
40 * but we can't depend on this).
42 static guint32 appdomain_thread_id = -1;
45 static __thread MonoDomain * tls_appdomain MONO_TLS_FAST;
46 #define GET_APPDOMAIN() tls_appdomain
47 #define SET_APPDOMAIN(x) do { \
49 TlsSetValue (appdomain_thread_id, x); \
54 #define GET_APPDOMAIN() ((MonoDomain *)TlsGetValue (appdomain_thread_id))
55 #define SET_APPDOMAIN(x) TlsSetValue (appdomain_thread_id, x);
59 #define GET_APPCONTEXT() (mono_thread_current ()->current_appcontext)
60 #define SET_APPCONTEXT(x) MONO_OBJECT_SETREF (mono_thread_current (), current_appcontext, (x))
62 static guint16 appdomain_list_size = 0;
63 static guint16 appdomain_next = 0;
64 static MonoDomain **appdomains_list = NULL;
66 #define mono_appdomains_lock() EnterCriticalSection (&appdomains_mutex)
67 #define mono_appdomains_unlock() LeaveCriticalSection (&appdomains_mutex)
68 static CRITICAL_SECTION appdomains_mutex;
70 static MonoDomain *mono_root_domain = NULL;
72 /* AppConfigInfo: Information about runtime versions supported by an
76 GSList *supported_runtimes;
77 char *required_runtime;
78 int configuration_count;
83 * AotModuleInfo: Contains information about AOT modules.
90 static const MonoRuntimeInfo *current_runtime = NULL;
92 static MonoJitInfoFindInAot jit_info_find_in_aot_func = NULL;
95 * Contains information about AOT loaded code.
97 static MonoJitInfoTable *aot_modules = NULL;
99 /* This is the list of runtime versions supported by this JIT.
101 static const MonoRuntimeInfo supported_runtimes[] = {
102 {"v1.0.3705", "1.0", { {1,0,5000,0}, {7,0,5000,0} } },
103 {"v1.1.4322", "1.0", { {1,0,5000,0}, {7,0,5000,0} } },
104 {"v2.0.50215","2.0", { {2,0,0,0}, {8,0,0,0} } },
105 {"v2.0.50727","2.0", { {2,0,0,0}, {8,0,0,0} } }
109 /* The stable runtime version */
110 #define DEFAULT_RUNTIME_VERSION "v1.1.4322"
113 get_runtimes_from_exe (const char *exe_file, const MonoRuntimeInfo** runtimes);
115 static const MonoRuntimeInfo*
116 get_runtime_by_version (const char *version);
119 mono_jit_info_find_aot_module (guint8* addr);
122 mono_domain_get_tls_key (void)
124 return appdomain_thread_id;
128 mono_domain_get_tls_offset (void)
131 MONO_THREAD_VAR_OFFSET (tls_appdomain, offset);
132 /* __asm ("jmp 1f; .section writetext, \"awx\"; 1: movl $tls_appdomain@ntpoff, %0; jmp 2f; .previous; 2:"
137 static MonoJitInfoTable *
138 mono_jit_info_table_new (void)
140 return g_array_new (FALSE, FALSE, sizeof (gpointer));
144 mono_jit_info_table_free (MonoJitInfoTable *table)
146 g_array_free (table, TRUE);
150 mono_jit_info_table_index (MonoJitInfoTable *table, char *addr)
152 int left = 0, right = table->len;
154 while (left < right) {
155 int pos = (left + right) / 2;
156 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
157 char *start = ji->code_start;
158 char *end = start + ji->code_size;
162 else if (addr >= end)
172 mono_jit_info_table_find (MonoDomain *domain, char *addr)
174 MonoJitInfoTable *table = domain->jit_info_table;
176 guint left = 0, right;
178 mono_domain_lock (domain);
181 while (left < right) {
182 guint pos = (left + right) / 2;
183 ji = g_array_index (table, gpointer, pos);
185 if (addr < (char*)ji->code_start)
187 else if (addr >= (char*)ji->code_start + ji->code_size)
190 mono_domain_unlock (domain);
194 mono_domain_unlock (domain);
196 /* maybe it is shared code, so we also search in the root domain */
198 if (domain != mono_root_domain)
199 ji = mono_jit_info_table_find (mono_root_domain, addr);
202 /* Maybe its an AOT module */
203 MonoImage *image = mono_jit_info_find_aot_module ((guint8*)addr);
205 ji = jit_info_find_in_aot_func (domain, image, addr);
212 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
214 MonoJitInfoTable *table = domain->jit_info_table;
215 gpointer start = ji->code_start;
218 mono_domain_lock (domain);
219 pos = mono_jit_info_table_index (table, start);
221 g_array_insert_val (table, pos, ji);
222 mono_domain_unlock (domain);
226 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji)
228 MonoJitInfoTable *table = domain->jit_info_table;
229 gpointer start = ji->code_start;
232 mono_domain_lock (domain);
233 pos = mono_jit_info_table_index (table, start);
234 if (g_array_index (table, gpointer, pos) != ji) {
235 MonoJitInfo *ji2 = g_array_index (table, gpointer, pos);
236 g_assert (ji == ji2);
238 g_assert (g_array_index (table, gpointer, pos) == ji);
240 g_array_remove_index (table, pos);
241 mono_domain_unlock (domain);
245 aot_info_table_index (MonoJitInfoTable *table, char *addr)
247 int left = 0, right = table->len;
249 while (left < right) {
250 int pos = (left + right) / 2;
251 AotModuleInfo *ainfo = g_array_index (table, gpointer, pos);
252 char *start = ainfo->start;
253 char *end = ainfo->end;
257 else if (addr >= end)
267 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end)
269 AotModuleInfo *ainfo = g_new0 (AotModuleInfo, 1);
272 ainfo->image = image;
273 ainfo->start = start;
276 mono_appdomains_lock ();
279 aot_modules = mono_jit_info_table_new ();
281 pos = aot_info_table_index (aot_modules, start);
283 g_array_insert_val (aot_modules, pos, ainfo);
285 mono_appdomains_unlock ();
289 mono_jit_info_find_aot_module (guint8* addr)
291 guint left = 0, right;
296 mono_appdomains_lock ();
298 right = aot_modules->len;
299 while (left < right) {
300 guint pos = (left + right) / 2;
301 AotModuleInfo *ai = g_array_index (aot_modules, gpointer, pos);
303 if (addr < (guint8*)ai->start)
305 else if (addr >= (guint8*)ai->end)
308 mono_appdomains_unlock ();
313 mono_appdomains_unlock ();
319 mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func)
321 jit_info_find_in_aot_func = func;
325 mono_jit_info_get_code_start (MonoJitInfo* ji)
327 return ji->code_start;
331 mono_jit_info_get_code_size (MonoJitInfo* ji)
333 return ji->code_size;
337 mono_jit_info_get_method (MonoJitInfo* ji)
343 mono_string_equal (MonoString *s1, MonoString *s2)
345 int l1 = mono_string_length (s1);
346 int l2 = mono_string_length (s2);
353 return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1 * 2) == 0;
357 mono_string_hash (MonoString *s)
359 const guint16 *p = mono_string_chars (s);
360 int i, len = mono_string_length (s);
363 for (i = 0; i < len; i++) {
364 h = (h << 5) - h + *p;
372 mono_ptrarray_equal (gpointer *s1, gpointer *s2)
374 int len = GPOINTER_TO_INT (s1 [0]);
375 if (len != GPOINTER_TO_INT (s2 [0]))
378 return memcmp (s1 + 1, s2 + 1, len * sizeof(gpointer)) == 0;
382 mono_ptrarray_hash (gpointer *s)
385 int len = GPOINTER_TO_INT (s [0]);
388 for (i = 1; i < len; i++)
389 hash += GPOINTER_TO_UINT (s [i]);
395 * Allocate an id for domain and set domain->domain_id.
396 * LOCKING: must be called while holding appdomains_mutex.
397 * We try to assign low numbers to the domain, so it can be used
398 * as an index in data tables to lookup domain-specific info
399 * with minimal memory overhead. We also try not to reuse the
400 * same id too quickly (to help debugging).
403 domain_id_alloc (MonoDomain *domain)
406 if (!appdomains_list) {
407 appdomain_list_size = 2;
408 appdomains_list = mono_gc_alloc_fixed (appdomain_list_size * sizeof (void*), NULL);
410 for (i = appdomain_next; i < appdomain_list_size; ++i) {
411 if (!appdomains_list [i]) {
417 for (i = 0; i < appdomain_next; ++i) {
418 if (!appdomains_list [i]) {
425 MonoDomain **new_list;
426 int new_size = appdomain_list_size * 2;
427 if (new_size >= (1 << 16))
428 g_assert_not_reached ();
429 id = appdomain_list_size;
430 new_list = mono_gc_alloc_fixed (new_size * sizeof (void*), NULL);
431 memcpy (new_list, appdomains_list, appdomain_list_size * sizeof (void*));
432 mono_gc_free_fixed (appdomains_list);
433 appdomains_list = new_list;
434 appdomain_list_size = new_size;
436 domain->domain_id = id;
437 appdomains_list [id] = domain;
439 if (appdomain_next > appdomain_list_size)
445 mono_domain_create (void)
449 domain = mono_gc_alloc_fixed (sizeof (MonoDomain), NULL);
450 domain->domain = NULL;
451 domain->setup = NULL;
452 domain->friendly_name = NULL;
453 domain->search_path = NULL;
455 domain->mp = mono_mempool_new ();
456 domain->code_mp = mono_code_manager_new ();
457 domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
458 domain->domain_assemblies = NULL;
459 domain->class_vtable_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
460 domain->proxy_vtable_hash = g_hash_table_new ((GHashFunc)mono_ptrarray_hash, (GCompareFunc)mono_ptrarray_equal);
461 domain->static_data_array = NULL;
462 domain->jit_code_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
463 domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
464 domain->jit_info_table = mono_jit_info_table_new ();
465 domain->class_init_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
466 domain->jump_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
467 domain->finalizable_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
468 domain->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
469 domain->delegate_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
471 InitializeCriticalSection (&domain->lock);
472 InitializeCriticalSection (&domain->assemblies_lock);
474 mono_appdomains_lock ();
475 domain_id_alloc (domain);
476 mono_appdomains_unlock ();
482 * mono_init_internal:
484 * Creates the initial application domain and initializes the mono_defaults
486 * This function is guaranteed to not run any IL code.
487 * If exe_filename is not NULL, the method will determine the required runtime
488 * from the exe configuration file or the version PE field.
489 * If runtime_version is not NULL, that runtime version will be used.
490 * Either exe_filename or runtime_version must be provided.
492 * Returns: the initial domain.
495 mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
497 static MonoDomain *domain = NULL;
498 MonoAssembly *ass = NULL;
499 MonoImageOpenStatus status = MONO_IMAGE_OK;
500 const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
504 g_assert_not_reached ();
508 appdomain_thread_id = TlsAlloc ();
510 InitializeCriticalSection (&appdomains_mutex);
512 mono_metadata_init ();
513 mono_raw_buffer_init ();
515 mono_assemblies_init ();
516 mono_classes_init ();
519 /* FIXME: When should we release this memory? */
520 MONO_GC_REGISTER_ROOT (appdomains_list);
522 domain = mono_domain_create ();
523 mono_root_domain = domain;
525 SET_APPDOMAIN (domain);
527 /* Get a list of runtimes supported by the exe */
528 if (exe_filename != NULL) {
529 get_runtimes_from_exe (exe_filename, runtimes);
530 } else if (runtime_version != NULL) {
531 runtimes [0] = get_runtime_by_version (runtime_version);
535 if (runtimes [0] == NULL) {
536 const MonoRuntimeInfo *default_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
537 runtimes [0] = default_runtime;
539 g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
540 g_print ("Using default runtime: %s\n", default_runtime->runtime_version);
543 /* The selected runtime will be the first one for which there is a mscrolib.dll */
544 for (n = 0; runtimes [n] != NULL && ass == NULL; n++) {
545 current_runtime = runtimes [n];
546 ass = mono_assembly_load_corlib (current_runtime, &status);
547 if (status != MONO_IMAGE_OK && status != MONO_IMAGE_ERROR_ERRNO)
551 if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
553 case MONO_IMAGE_ERROR_ERRNO: {
554 char *corlib_file = g_build_filename (mono_assembly_getrootdir (), "mono", current_runtime->framework_version, "mscorlib.dll", NULL);
555 g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
556 g_print ("It should have been installed in the `%s' directory.\n", corlib_file);
557 g_free (corlib_file);
560 case MONO_IMAGE_IMAGE_INVALID:
561 g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
562 mono_assembly_getrootdir ());
564 case MONO_IMAGE_MISSING_ASSEMBLYREF:
565 g_print ("Missing assembly reference in %s/mscorlib.dll\n",
566 mono_assembly_getrootdir ());
569 /* to suppress compiler warning */
575 mono_defaults.corlib = mono_assembly_get_image (ass);
577 mono_defaults.object_class = mono_class_from_name (
578 mono_defaults.corlib, "System", "Object");
579 g_assert (mono_defaults.object_class != 0);
581 mono_defaults.void_class = mono_class_from_name (
582 mono_defaults.corlib, "System", "Void");
583 g_assert (mono_defaults.void_class != 0);
585 mono_defaults.boolean_class = mono_class_from_name (
586 mono_defaults.corlib, "System", "Boolean");
587 g_assert (mono_defaults.boolean_class != 0);
589 mono_defaults.byte_class = mono_class_from_name (
590 mono_defaults.corlib, "System", "Byte");
591 g_assert (mono_defaults.byte_class != 0);
593 mono_defaults.sbyte_class = mono_class_from_name (
594 mono_defaults.corlib, "System", "SByte");
595 g_assert (mono_defaults.sbyte_class != 0);
597 mono_defaults.int16_class = mono_class_from_name (
598 mono_defaults.corlib, "System", "Int16");
599 g_assert (mono_defaults.int16_class != 0);
601 mono_defaults.uint16_class = mono_class_from_name (
602 mono_defaults.corlib, "System", "UInt16");
603 g_assert (mono_defaults.uint16_class != 0);
605 mono_defaults.int32_class = mono_class_from_name (
606 mono_defaults.corlib, "System", "Int32");
607 g_assert (mono_defaults.int32_class != 0);
609 mono_defaults.uint32_class = mono_class_from_name (
610 mono_defaults.corlib, "System", "UInt32");
611 g_assert (mono_defaults.uint32_class != 0);
613 mono_defaults.uint_class = mono_class_from_name (
614 mono_defaults.corlib, "System", "UIntPtr");
615 g_assert (mono_defaults.uint_class != 0);
617 mono_defaults.int_class = mono_class_from_name (
618 mono_defaults.corlib, "System", "IntPtr");
619 g_assert (mono_defaults.int_class != 0);
621 mono_defaults.int64_class = mono_class_from_name (
622 mono_defaults.corlib, "System", "Int64");
623 g_assert (mono_defaults.int64_class != 0);
625 mono_defaults.uint64_class = mono_class_from_name (
626 mono_defaults.corlib, "System", "UInt64");
627 g_assert (mono_defaults.uint64_class != 0);
629 mono_defaults.single_class = mono_class_from_name (
630 mono_defaults.corlib, "System", "Single");
631 g_assert (mono_defaults.single_class != 0);
633 mono_defaults.double_class = mono_class_from_name (
634 mono_defaults.corlib, "System", "Double");
635 g_assert (mono_defaults.double_class != 0);
637 mono_defaults.char_class = mono_class_from_name (
638 mono_defaults.corlib, "System", "Char");
639 g_assert (mono_defaults.char_class != 0);
641 mono_defaults.string_class = mono_class_from_name (
642 mono_defaults.corlib, "System", "String");
643 g_assert (mono_defaults.string_class != 0);
645 mono_defaults.enum_class = mono_class_from_name (
646 mono_defaults.corlib, "System", "Enum");
647 g_assert (mono_defaults.enum_class != 0);
649 mono_defaults.array_class = mono_class_from_name (
650 mono_defaults.corlib, "System", "Array");
651 g_assert (mono_defaults.array_class != 0);
653 mono_defaults.delegate_class = mono_class_from_name (
654 mono_defaults.corlib, "System", "Delegate");
655 g_assert (mono_defaults.delegate_class != 0 );
657 mono_defaults.multicastdelegate_class = mono_class_from_name (
658 mono_defaults.corlib, "System", "MulticastDelegate");
659 g_assert (mono_defaults.multicastdelegate_class != 0 );
661 mono_defaults.asyncresult_class = mono_class_from_name (
662 mono_defaults.corlib, "System.Runtime.Remoting.Messaging",
664 g_assert (mono_defaults.asyncresult_class != 0 );
666 mono_defaults.waithandle_class = mono_class_from_name (
667 mono_defaults.corlib, "System.Threading", "WaitHandle");
668 g_assert (mono_defaults.waithandle_class != 0 );
670 mono_defaults.typehandle_class = mono_class_from_name (
671 mono_defaults.corlib, "System", "RuntimeTypeHandle");
672 g_assert (mono_defaults.typehandle_class != 0);
674 mono_defaults.methodhandle_class = mono_class_from_name (
675 mono_defaults.corlib, "System", "RuntimeMethodHandle");
676 g_assert (mono_defaults.methodhandle_class != 0);
678 mono_defaults.fieldhandle_class = mono_class_from_name (
679 mono_defaults.corlib, "System", "RuntimeFieldHandle");
680 g_assert (mono_defaults.fieldhandle_class != 0);
682 mono_defaults.systemtype_class = mono_class_from_name (
683 mono_defaults.corlib, "System", "Type");
684 g_assert (mono_defaults.systemtype_class != 0);
686 mono_defaults.monotype_class = mono_class_from_name (
687 mono_defaults.corlib, "System", "MonoType");
688 g_assert (mono_defaults.monotype_class != 0);
690 mono_defaults.exception_class = mono_class_from_name (
691 mono_defaults.corlib, "System", "Exception");
692 g_assert (mono_defaults.exception_class != 0);
694 mono_defaults.threadabortexception_class = mono_class_from_name (
695 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
696 g_assert (mono_defaults.threadabortexception_class != 0);
698 mono_defaults.thread_class = mono_class_from_name (
699 mono_defaults.corlib, "System.Threading", "Thread");
700 g_assert (mono_defaults.thread_class != 0);
702 mono_defaults.appdomain_class = mono_class_from_name (
703 mono_defaults.corlib, "System", "AppDomain");
704 g_assert (mono_defaults.appdomain_class != 0);
706 mono_defaults.transparent_proxy_class = mono_class_from_name (
707 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
708 g_assert (mono_defaults.transparent_proxy_class != 0);
710 mono_defaults.real_proxy_class = mono_class_from_name (
711 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
712 g_assert (mono_defaults.real_proxy_class != 0);
714 mono_defaults.mono_method_message_class = mono_class_from_name (
715 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
716 g_assert (mono_defaults.mono_method_message_class != 0);
718 mono_defaults.field_info_class = mono_class_from_name (
719 mono_defaults.corlib, "System.Reflection", "FieldInfo");
720 g_assert (mono_defaults.field_info_class != 0);
722 mono_defaults.method_info_class = mono_class_from_name (
723 mono_defaults.corlib, "System.Reflection", "MethodInfo");
724 g_assert (mono_defaults.method_info_class != 0);
726 mono_defaults.stringbuilder_class = mono_class_from_name (
727 mono_defaults.corlib, "System.Text", "StringBuilder");
728 g_assert (mono_defaults.stringbuilder_class != 0);
730 mono_defaults.math_class = mono_class_from_name (
731 mono_defaults.corlib, "System", "Math");
732 g_assert (mono_defaults.math_class != 0);
734 mono_defaults.stack_frame_class = mono_class_from_name (
735 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
736 g_assert (mono_defaults.stack_frame_class != 0);
738 mono_defaults.stack_trace_class = mono_class_from_name (
739 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
740 g_assert (mono_defaults.stack_trace_class != 0);
742 mono_defaults.marshal_class = mono_class_from_name (
743 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
744 g_assert (mono_defaults.marshal_class != 0);
746 mono_defaults.iserializeable_class = mono_class_from_name (
747 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
748 g_assert (mono_defaults.iserializeable_class != 0);
750 mono_defaults.serializationinfo_class = mono_class_from_name (
751 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
752 g_assert (mono_defaults.serializationinfo_class != 0);
754 mono_defaults.streamingcontext_class = mono_class_from_name (
755 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
756 g_assert (mono_defaults.streamingcontext_class != 0);
758 mono_defaults.typed_reference_class = mono_class_from_name (
759 mono_defaults.corlib, "System", "TypedReference");
760 g_assert (mono_defaults.typed_reference_class != 0);
762 mono_defaults.argumenthandle_class = mono_class_from_name (
763 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
764 g_assert (mono_defaults.argumenthandle_class != 0);
766 mono_defaults.marshalbyrefobject_class = mono_class_from_name (
767 mono_defaults.corlib, "System", "MarshalByRefObject");
768 g_assert (mono_defaults.marshalbyrefobject_class != 0);
770 mono_defaults.monitor_class = mono_class_from_name (
771 mono_defaults.corlib, "System.Threading", "Monitor");
772 g_assert (mono_defaults.monitor_class != 0);
774 mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
775 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
776 g_assert (mono_defaults.iremotingtypeinfo_class != 0);
778 mono_defaults.runtimesecurityframe_class = mono_class_from_name (
779 mono_defaults.corlib, "System.Security", "RuntimeSecurityFrame");
781 mono_defaults.executioncontext_class = mono_class_from_name (
782 mono_defaults.corlib, "System.Threading", "ExecutionContext");
784 mono_defaults.internals_visible_class = mono_class_from_name (
785 mono_defaults.corlib, "System.Runtime.CompilerServices", "InternalsVisibleToAttribute");
788 * Note that mono_defaults.generic_*_class is only non-NULL if we're
789 * using the 2.0 corlib.
791 mono_class_init (mono_defaults.array_class);
792 mono_defaults.generic_array_class = mono_class_from_name (
793 mono_defaults.corlib, "System", "Array/InternalArray`1");
794 mono_defaults.generic_nullable_class = mono_class_from_name (
795 mono_defaults.corlib, "System", "Nullable`1");
797 domain->friendly_name = g_path_get_basename (filename);
805 * Creates the initial application domain and initializes the mono_defaults
807 * This function is guaranteed to not run any IL code.
808 * The runtime is initialized using the default runtime version.
810 * Returns: the initial domain.
813 mono_init (const char *domain_name)
815 return mono_init_internal (domain_name, NULL, DEFAULT_RUNTIME_VERSION);
819 * mono_init_from_assembly:
821 * Creates the initial application domain and initializes the mono_defaults
823 * This function is guaranteed to not run any IL code.
824 * The runtime is initialized using the runtime version required by the
825 * provided executable. The version is determined by looking at the exe
826 * configuration file and the version PE field)
828 * Returns: the initial domain.
831 mono_init_from_assembly (const char *domain_name, const char *filename)
833 return mono_init_internal (domain_name, filename, NULL);
839 * Creates the initial application domain and initializes the mono_defaults
841 * This function is guaranteed to not run any IL code.
842 * The runtime is initialized using the provided rutime version.
844 * Returns: the initial domain.
847 mono_init_version (const char *domain_name, const char *version)
849 return mono_init_internal (domain_name, NULL, version);
855 * Cleans up all metadata modules.
860 mono_loader_cleanup ();
861 mono_classes_cleanup ();
862 mono_assemblies_cleanup ();
863 mono_images_cleanup ();
864 mono_raw_buffer_cleanup ();
865 mono_metadata_cleanup ();
869 * mono_get_root_domain:
871 * Returns: the root appdomain.
874 mono_get_root_domain (void)
876 return mono_root_domain;
882 * Returns: the current domain.
887 return GET_APPDOMAIN ();
891 * mono_domain_set_internal:
892 * @domain: the new domain
894 * Sets the current domain to @domain.
897 mono_domain_set_internal (MonoDomain *domain)
899 SET_APPDOMAIN (domain);
900 SET_APPCONTEXT (domain->default_context);
904 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
910 * Create a copy of the data to avoid calling the user callback
911 * inside the lock because that could lead to deadlocks.
912 * We can do this because this function is not perf. critical.
914 mono_appdomains_lock ();
915 size = appdomain_list_size;
916 copy = mono_gc_alloc_fixed (appdomain_list_size * sizeof (void*), NULL);
917 memcpy (copy, appdomains_list, appdomain_list_size * sizeof (void*));
918 mono_appdomains_unlock ();
920 for (i = 0; i < size; ++i) {
922 func (copy [i], user_data);
925 mono_gc_free_fixed (copy);
929 * mono_domain_assembly_open:
930 * @domain: the application domain
931 * @name: file name of the assembly
933 * fixme: maybe we should integrate this with mono_assembly_open ??
936 mono_domain_assembly_open (MonoDomain *domain, const char *name)
941 mono_domain_assemblies_lock (domain);
942 for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
944 if (strcmp (name, ass->aname.name) == 0) {
945 mono_domain_assemblies_unlock (domain);
949 mono_domain_assemblies_unlock (domain);
951 if (!(ass = mono_assembly_open (name, NULL)))
958 dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
960 MonoJitDynamicMethodInfo *di = value;
961 mono_code_manager_destroy (di->code_mp);
966 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
968 g_slist_free (value);
972 mono_domain_free (MonoDomain *domain, gboolean force)
975 if ((domain == mono_root_domain) && !force) {
976 g_warning ("cant unload root domain");
980 mono_appdomains_lock ();
981 appdomains_list [domain->domain_id] = NULL;
982 mono_appdomains_unlock ();
984 /* FIXME: free delegate_hash_table when it's used */
985 if (domain->search_path) {
986 g_strfreev (domain->search_path);
987 domain->search_path = NULL;
989 domain->create_proxy_for_type_method = NULL;
990 domain->private_invoke_method = NULL;
991 domain->default_context = NULL;
992 domain->out_of_memory_ex = NULL;
993 domain->null_reference_ex = NULL;
994 domain->stack_overflow_ex = NULL;
995 domain->entry_assembly = NULL;
996 for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
997 MonoAssembly *ass = tmp->data;
998 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);
999 mono_assembly_close (ass);
1001 g_slist_free (domain->domain_assemblies);
1002 domain->domain_assemblies = NULL;
1004 g_free (domain->friendly_name);
1005 domain->friendly_name = NULL;
1006 mono_g_hash_table_destroy (domain->env);
1008 g_hash_table_destroy (domain->class_vtable_hash);
1009 domain->class_vtable_hash = NULL;
1010 g_hash_table_destroy (domain->proxy_vtable_hash);
1011 domain->proxy_vtable_hash = NULL;
1012 if (domain->static_data_array) {
1013 mono_gc_free_fixed (domain->static_data_array);
1014 domain->static_data_array = NULL;
1016 g_hash_table_destroy (domain->jit_code_hash);
1017 domain->jit_code_hash = NULL;
1018 if (domain->dynamic_code_hash) {
1019 g_hash_table_foreach (domain->dynamic_code_hash, dynamic_method_info_free, NULL);
1020 g_hash_table_destroy (domain->dynamic_code_hash);
1021 domain->dynamic_code_hash = NULL;
1023 mono_g_hash_table_destroy (domain->ldstr_table);
1024 domain->ldstr_table = NULL;
1025 mono_jit_info_table_free (domain->jit_info_table);
1026 domain->jit_info_table = NULL;
1027 #ifdef DEBUG_DOMAIN_UNLOAD
1028 mono_mempool_invalidate (domain->mp);
1029 mono_code_manager_invalidate (domain->code_mp);
1031 mono_mempool_destroy (domain->mp);
1033 mono_code_manager_destroy (domain->code_mp);
1034 domain->code_mp = NULL;
1036 if (domain->jump_target_hash) {
1037 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
1038 g_hash_table_destroy (domain->jump_target_hash);
1039 domain->jump_target_hash = NULL;
1041 if (domain->type_hash) {
1042 mono_g_hash_table_destroy (domain->type_hash);
1043 domain->type_hash = NULL;
1045 if (domain->refobject_hash) {
1046 mono_g_hash_table_destroy (domain->refobject_hash);
1047 domain->refobject_hash = NULL;
1049 g_hash_table_destroy (domain->class_init_trampoline_hash);
1050 domain->class_init_trampoline_hash = NULL;
1051 g_hash_table_destroy (domain->jump_trampoline_hash);
1052 domain->jump_trampoline_hash = NULL;
1053 g_hash_table_destroy (domain->finalizable_objects_hash);
1054 domain->finalizable_objects_hash = NULL;
1055 g_hash_table_destroy (domain->jit_trampoline_hash);
1056 domain->jit_trampoline_hash = NULL;
1057 g_hash_table_destroy (domain->delegate_trampoline_hash);
1058 domain->delegate_trampoline_hash = NULL;
1059 if (domain->special_static_fields) {
1060 g_hash_table_destroy (domain->special_static_fields);
1061 domain->special_static_fields = NULL;
1063 DeleteCriticalSection (&domain->assemblies_lock);
1064 DeleteCriticalSection (&domain->lock);
1065 domain->setup = NULL;
1067 /* FIXME: anything else required ? */
1069 mono_gc_free_fixed (domain);
1071 if ((domain == mono_root_domain))
1072 mono_root_domain = NULL;
1076 * mono_domain_get_id:
1078 * Returns: the a domain for a specific domain id.
1081 mono_domain_get_by_id (gint32 domainid)
1083 MonoDomain * domain;
1085 mono_appdomains_lock ();
1086 if (domainid < appdomain_list_size)
1087 domain = appdomains_list [domainid];
1090 mono_appdomains_unlock ();
1096 mono_domain_get_id (MonoDomain *domain)
1098 return domain->domain_id;
1102 mono_context_set (MonoAppContext * new_context)
1104 SET_APPCONTEXT (new_context);
1108 mono_context_get (void)
1110 return GET_APPCONTEXT ();
1113 /* LOCKING: the caller holds the lock for this domain */
1115 mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointer data, guint32 *bitmap)
1117 /* The first entry in the array is the index of the next free slot
1118 * and the total size of the array
1121 if (domain->static_data_array) {
1122 int size = GPOINTER_TO_INT (domain->static_data_array [1]);
1123 next = GPOINTER_TO_INT (domain->static_data_array [0]);
1125 gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * (size * 2), NULL);
1126 memcpy (new_array, domain->static_data_array, sizeof (gpointer) * size);
1128 new_array [1] = GINT_TO_POINTER (size);
1129 mono_gc_free_fixed (domain->static_data_array);
1130 domain->static_data_array = new_array;
1134 gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * size, NULL);
1136 new_array [0] = GINT_TO_POINTER (next);
1137 new_array [1] = GINT_TO_POINTER (size);
1138 domain->static_data_array = new_array;
1140 domain->static_data_array [next++] = data;
1141 domain->static_data_array [0] = GINT_TO_POINTER (next);
1145 mono_get_corlib (void)
1147 return mono_defaults.corlib;
1151 mono_get_object_class (void)
1153 return mono_defaults.object_class;
1157 mono_get_byte_class (void)
1159 return mono_defaults.byte_class;
1163 mono_get_void_class (void)
1165 return mono_defaults.void_class;
1169 mono_get_boolean_class (void)
1171 return mono_defaults.boolean_class;
1175 mono_get_sbyte_class (void)
1177 return mono_defaults.sbyte_class;
1181 mono_get_int16_class (void)
1183 return mono_defaults.int16_class;
1187 mono_get_uint16_class (void)
1189 return mono_defaults.uint16_class;
1193 mono_get_int32_class (void)
1195 return mono_defaults.int32_class;
1199 mono_get_uint32_class (void)
1201 return mono_defaults.uint32_class;
1205 mono_get_intptr_class (void)
1207 return mono_defaults.int_class;
1211 mono_get_uintptr_class (void)
1213 return mono_defaults.uint_class;
1217 mono_get_int64_class (void)
1219 return mono_defaults.int64_class;
1223 mono_get_uint64_class (void)
1225 return mono_defaults.uint64_class;
1229 mono_get_single_class (void)
1231 return mono_defaults.single_class;
1235 mono_get_double_class (void)
1237 return mono_defaults.double_class;
1241 mono_get_char_class (void)
1243 return mono_defaults.char_class;
1247 mono_get_string_class (void)
1249 return mono_defaults.string_class;
1253 mono_get_enum_class (void)
1255 return mono_defaults.enum_class;
1259 mono_get_array_class (void)
1261 return mono_defaults.array_class;
1265 mono_get_thread_class (void)
1267 return mono_defaults.thread_class;
1271 mono_get_exception_class (void)
1273 return mono_defaults.exception_class;
1277 static char* get_attribute_value (const gchar **attribute_names,
1278 const gchar **attribute_values,
1279 const char *att_name)
1282 for (n=0; attribute_names[n] != NULL; n++) {
1283 if (strcmp (attribute_names[n], att_name) == 0)
1284 return g_strdup (attribute_values[n]);
1289 static void start_element (GMarkupParseContext *context,
1290 const gchar *element_name,
1291 const gchar **attribute_names,
1292 const gchar **attribute_values,
1296 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1298 if (strcmp (element_name, "configuration") == 0) {
1299 app_config->configuration_count++;
1302 if (strcmp (element_name, "startup") == 0) {
1303 app_config->startup_count++;
1307 if (app_config->configuration_count != 1 || app_config->startup_count != 1)
1310 if (strcmp (element_name, "requiredRuntime") == 0) {
1311 app_config->required_runtime = get_attribute_value (attribute_names, attribute_values, "version");
1312 } else if (strcmp (element_name, "supportedRuntime") == 0) {
1313 char *version = get_attribute_value (attribute_names, attribute_values, "version");
1314 app_config->supported_runtimes = g_slist_append (app_config->supported_runtimes, version);
1318 static void end_element (GMarkupParseContext *context,
1319 const gchar *element_name,
1323 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1325 if (strcmp (element_name, "configuration") == 0) {
1326 app_config->configuration_count--;
1327 } else if (strcmp (element_name, "startup") == 0) {
1328 app_config->startup_count--;
1332 static const GMarkupParser
1341 static AppConfigInfo *
1342 app_config_parse (const char *filename)
1344 AppConfigInfo *app_config;
1345 GMarkupParseContext *context;
1350 if (stat (filename, &buf) != 0)
1353 app_config = g_new0 (AppConfigInfo, 1);
1355 if (!g_file_get_contents (filename, &text, &len, NULL))
1358 context = g_markup_parse_context_new (&mono_parser, 0, app_config, NULL);
1359 if (g_markup_parse_context_parse (context, text, len, NULL)) {
1360 g_markup_parse_context_end_parse (context, NULL);
1362 g_markup_parse_context_free (context);
1368 app_config_free (AppConfigInfo* app_config)
1371 GSList *list = app_config->supported_runtimes;
1372 while (list != NULL) {
1373 rt = (char*)list->data;
1375 list = g_slist_next (list);
1377 g_slist_free (app_config->supported_runtimes);
1378 g_free (app_config->required_runtime);
1379 g_free (app_config);
1383 static const MonoRuntimeInfo*
1384 get_runtime_by_version (const char *version)
1387 int max = G_N_ELEMENTS (supported_runtimes);
1389 for (n=0; n<max; n++) {
1390 if (strcmp (version, supported_runtimes[n].runtime_version) == 0)
1391 return &supported_runtimes[n];
1397 get_runtimes_from_exe (const char *exe_file, const MonoRuntimeInfo** runtimes)
1399 AppConfigInfo* app_config;
1402 const MonoRuntimeInfo* runtime = NULL;
1403 MonoImage *image = NULL;
1405 config_name = g_strconcat (exe_file, ".config", NULL);
1406 app_config = app_config_parse (config_name);
1407 g_free (config_name);
1409 if (app_config != NULL) {
1410 /* Check supportedRuntime elements, if none is supported, fail.
1411 * If there are no such elements, look for a requiredRuntime element.
1413 if (app_config->supported_runtimes != NULL) {
1415 GSList *list = app_config->supported_runtimes;
1416 while (list != NULL) {
1417 version = (char*) list->data;
1418 runtime = get_runtime_by_version (version);
1419 if (runtime != NULL)
1420 runtimes [n++] = runtime;
1421 list = g_slist_next (list);
1423 runtimes [n] = NULL;
1424 app_config_free (app_config);
1428 /* Check the requiredRuntime element. This is for 1.0 apps only. */
1429 if (app_config->required_runtime != NULL) {
1430 runtimes [0] = get_runtime_by_version (app_config->required_runtime);
1431 runtimes [1] = NULL;
1432 app_config_free (app_config);
1435 app_config_free (app_config);
1438 /* Look for a runtime with the exact version */
1439 image = mono_image_open (exe_file, NULL);
1440 if (image == NULL) {
1441 /* The image is wrong or the file was not found. In this case return
1442 * a default runtime and leave to the initialization method the work of
1443 * reporting the error.
1445 runtimes [0] = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
1446 runtimes [1] = NULL;
1450 runtimes [0] = get_runtime_by_version (image->version);
1451 runtimes [1] = NULL;
1456 * mono_get_runtime_info:
1458 * Returns: the version of the current runtime instance.
1460 const MonoRuntimeInfo*
1461 mono_get_runtime_info (void)
1463 return current_runtime;
1467 mono_debugger_check_runtime_version (const char *filename)
1469 const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
1470 const MonoRuntimeInfo *rinfo;
1472 get_runtimes_from_exe (filename, runtimes);
1473 rinfo = runtimes [0];
1476 return g_strdup_printf ("Cannot get runtime version from assembly `%s'", filename);
1478 if (rinfo != current_runtime)
1479 return g_strdup_printf ("The Mono Debugger is currently using the `%s' runtime, but "
1480 "the assembly `%s' requires version `%s'", current_runtime->runtime_version,
1481 filename, rinfo->runtime_version);