[sgen] Get the thread's domain via a callback, not via TLS.
[mono.git] / mono / metadata / domain.c
index ebe13ad5568b4122f417702af92ce394563b8917..efd8f02e4013978915049a529daa24f176a9b305 100644 (file)
@@ -7,6 +7,7 @@
  *
  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
+ * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
  */
 
 #include <config.h>
 #include <mono/metadata/gc-internal.h>
 
 #include <mono/utils/mono-compiler.h>
-#include <mono/utils/mono-logger.h>
+#include <mono/utils/mono-logger-internal.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/metadata/object.h>
 #include <mono/metadata/object-internals.h>
 #include <mono/metadata/domain-internals.h>
  * or the other (we used to do it because tls slots were GC-tracked,
  * but we can't depend on this).
  */
-static guint32 appdomain_thread_id = -1;
+static MonoNativeTlsKey appdomain_thread_id;
 
-/* 
- * Avoid calling TlsSetValue () if possible, since in the io-layer, it acquires
- * a global lock (!) so it is a contention point.
- */
-#if (defined(__i386__) || defined(__x86_64__)) && !defined(PLATFORM_WIN32)
-#define NO_TLS_SET_VALUE
-#endif
-#ifdef HAVE_KW_THREAD
+#ifdef MONO_HAVE_FAST_TLS
 
-static __thread MonoDomain * tls_appdomain MONO_TLS_FAST;
+MONO_FAST_TLS_DECLARE(tls_appdomain);
 
-#define GET_APPDOMAIN() tls_appdomain
+#define GET_APPDOMAIN() ((MonoDomain*)MONO_FAST_TLS_GET(tls_appdomain))
 
-#ifdef NO_TLS_SET_VALUE
-#define SET_APPDOMAIN(x) do { \
-       tls_appdomain = x; \
-} while (FALSE)
-#else
 #define SET_APPDOMAIN(x) do { \
-       tls_appdomain = x; \
-       TlsSetValue (appdomain_thread_id, x); \
+       MONO_FAST_TLS_SET (tls_appdomain,x); \
+       mono_native_tls_set_value (appdomain_thread_id, x); \
+       mono_gc_set_current_thread_appdomain (x); \
 } while (FALSE)
-#endif
 
-#else /* !HAVE_KW_THREAD */
+#else /* !MONO_HAVE_FAST_TLS */
 
-#define GET_APPDOMAIN() ((MonoDomain *)TlsGetValue (appdomain_thread_id))
-#define SET_APPDOMAIN(x) TlsSetValue (appdomain_thread_id, x);
+#define GET_APPDOMAIN() ((MonoDomain *)mono_native_tls_get_value (appdomain_thread_id))
+#define SET_APPDOMAIN(x) do {                                          \
+               mono_native_tls_set_value (appdomain_thread_id, x);     \
+               mono_gc_set_current_thread_appdomain (x);               \
+       } while (FALSE)
 
 #endif
 
-#define GET_APPCONTEXT() (mono_thread_current ()->current_appcontext)
-#define SET_APPCONTEXT(x) MONO_OBJECT_SETREF (mono_thread_current (), current_appcontext, (x))
+#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;
@@ -128,17 +121,17 @@ static MonoAotModuleInfoTable *aot_modules = NULL;
 /* This is the list of runtime versions supported by this JIT.
  */
 static const MonoRuntimeInfo supported_runtimes[] = {
-       {"v1.0.3705", "1.0", { {1,0,5000,0}, {7,0,5000,0} }     },
-       {"v1.1.4322", "1.0", { {1,0,5000,0}, {7,0,5000,0} }     },
-       {"v2.0.50215","2.0", { {2,0,0,0},    {8,0,0,0} }        },
-       {"v2.0.50727","2.0", { {2,0,0,0},    {8,0,0,0} }        },
-       {"v4.0.20506","4.0", { {4,0,0,0},    {10,0,0,0} }   },
-       {"moonlight", "2.1", { {2,0,5,0},    {9,0,0,0} }    },
+       {"v2.0.50215","2.0", { {2,0,0,0},    {8,0,0,0}, { 3, 5, 0, 0 } }        },
+       {"v2.0.50727","2.0", { {2,0,0,0},    {8,0,0,0}, { 3, 5, 0, 0 } }        },
+       {"v4.0.20506","4.0", { {4,0,0,0},    {10,0,0,0}, { 4, 0, 0, 0 } }   },
+       {"v4.0.30128","4.0", { {4,0,0,0},    {10,0,0,0}, { 4, 0, 0, 0 } }   },
+       {"v4.0.30319","4.5", { {4,0,0,0},    {10,0,0,0}, { 4, 0, 0, 0 } }   },
+       {"moonlight", "2.1", { {2,0,5,0},    {9,0,0,0}, { 3, 5, 0, 0 } }    },
 };
 
 
 /* The stable runtime version */
-#define DEFAULT_RUNTIME_VERSION "v1.1.4322"
+#define DEFAULT_RUNTIME_VERSION "v2.0.50727"
 
 /* Callbacks installed by the JIT */
 static MonoCreateDomainFunc create_domain_hook;
@@ -156,15 +149,10 @@ get_runtime_by_version (const char *version);
 static MonoImage*
 mono_jit_info_find_aot_module (guint8* addr);
 
-guint32
+MonoNativeTlsKey
 mono_domain_get_tls_key (void)
 {
-#ifdef NO_TLS_SET_VALUE
-       g_assert_not_reached ();
-       return 0;
-#else
        return appdomain_thread_id;
-#endif
 }
 
 gint32
@@ -222,7 +210,7 @@ jit_info_table_new_chunk (void)
 static MonoJitInfoTable *
 jit_info_table_new (MonoDomain *domain)
 {
-       MonoJitInfoTable *table = g_malloc0 (sizeof (MonoJitInfoTable) + sizeof (MonoJitInfoTableChunk*));
+       MonoJitInfoTable *table = g_malloc0 (MONO_SIZEOF_JIT_INFO_TABLE + sizeof (MonoJitInfoTableChunk*));
 
        table->domain = domain;
        table->num_chunks = 1;
@@ -279,36 +267,6 @@ jit_info_table_free (MonoJitInfoTable *table)
        g_free (table);
 }
 
-/* Can be called with hp==NULL, in which case it acts as an ordinary
-   pointer fetch.  It's used that way indirectly from
-   mono_jit_info_table_add(), which doesn't have to care about hazards
-   because it holds the respective domain lock. */
-static gpointer
-get_hazardous_pointer (gpointer volatile *pp, MonoThreadHazardPointers *hp, int hazard_index)
-{
-       gpointer p;
-
-       for (;;) {
-               /* Get the pointer */
-               p = *pp;
-               /* If we don't have hazard pointers just return the
-                  pointer. */
-               if (!hp)
-                       return p;
-               /* Make it hazardous */
-               mono_hazard_pointer_set (hp, hazard_index, p);
-               /* Check that it's still the same.  If not, try
-                  again. */
-               if (*pp != p) {
-                       mono_hazard_pointer_clear (hp, hazard_index);
-                       continue;
-               }
-               break;
-       }
-
-       return p;
-}
-
 /* The jit_info_table is sorted in ascending order by the end
  * addresses of the compiled methods.  The reason why we have to do
  * this is that once we introduce tombstones, it becomes possible for
@@ -379,6 +337,7 @@ mono_jit_info_table_find (MonoDomain *domain, char *addr)
        MonoJitInfo *ji;
        int chunk_pos, pos;
        MonoThreadHazardPointers *hp = mono_hazard_pointer_get ();
+       MonoImage *image;
 
        ++mono_stats.jit_info_table_lookup_count;
 
@@ -433,20 +392,18 @@ mono_jit_info_table_find (MonoDomain *domain, char *addr)
        } while (chunk_pos < table->num_chunks);
 
  not_found:
+       if (!hp)
+               return NULL;
+
        mono_hazard_pointer_clear (hp, JIT_INFO_TABLE_HAZARD_INDEX);
        mono_hazard_pointer_clear (hp, JIT_INFO_HAZARD_INDEX);
 
-       /* maybe it is shared code, so we also search in the root domain */
        ji = NULL;
-       if (domain != mono_root_domain)
-               ji = mono_jit_info_table_find (mono_root_domain, addr);
-
-       if (ji == NULL) {
-               /* Maybe its an AOT module */
-               MonoImage *image = mono_jit_info_find_aot_module ((guint8*)addr);
-               if (image)
-                       ji = jit_info_find_in_aot_func (domain, image, addr);
-       }
+
+       /* Maybe its an AOT module */
+       image = mono_jit_info_find_aot_module ((guint8*)addr);
+       if (image)
+               ji = jit_info_find_in_aot_func (domain, image, addr);
        
        return ji;
 }
@@ -507,7 +464,7 @@ jit_info_table_realloc (MonoJitInfoTable *old)
        required_size = (int)((long)num_elements * JIT_INFO_TABLE_FILL_RATIO_DENOM / JIT_INFO_TABLE_FILL_RATIO_NOM);
        num_chunks = (required_size + MONO_JIT_INFO_TABLE_CHUNK_SIZE - 1) / MONO_JIT_INFO_TABLE_CHUNK_SIZE;
 
-       new = g_malloc (sizeof (MonoJitInfoTable) + sizeof (MonoJitInfoTableChunk*) * num_chunks);
+       new = g_malloc (MONO_SIZEOF_JIT_INFO_TABLE + sizeof (MonoJitInfoTableChunk*) * num_chunks);
        new->domain = old->domain;
        new->num_chunks = num_chunks;
 
@@ -576,7 +533,7 @@ jit_info_table_split_chunk (MonoJitInfoTableChunk *chunk, MonoJitInfoTableChunk
 static MonoJitInfoTable*
 jit_info_table_copy_and_split_chunk (MonoJitInfoTable *table, MonoJitInfoTableChunk *chunk)
 {
-       MonoJitInfoTable *new_table = g_malloc (sizeof (MonoJitInfoTable)
+       MonoJitInfoTable *new_table = g_malloc (MONO_SIZEOF_JIT_INFO_TABLE
                + sizeof (MonoJitInfoTableChunk*) * (table->num_chunks + 1));
        int i, j;
 
@@ -624,7 +581,7 @@ jit_info_table_purify_chunk (MonoJitInfoTableChunk *old)
 static MonoJitInfoTable*
 jit_info_table_copy_and_purify_chunk (MonoJitInfoTable *table, MonoJitInfoTableChunk *chunk)
 {
-       MonoJitInfoTable *new_table = g_malloc (sizeof (MonoJitInfoTable)
+       MonoJitInfoTable *new_table = g_malloc (MONO_SIZEOF_JIT_INFO_TABLE
                + sizeof (MonoJitInfoTableChunk*) * table->num_chunks);
        int i, j;
 
@@ -1025,7 +982,19 @@ mono_jit_info_set_generic_sharing_context (MonoJitInfo *ji, MonoGenericSharingCo
 
        gi->generic_sharing_context = gsctx;
 }
+
+MonoTryBlockHoleTableJitInfo*
+mono_jit_info_get_try_block_hole_table_info (MonoJitInfo *ji)
+{
+       if (ji->has_try_block_holes) {
+               char *ptr = (char*)&ji->clauses [ji->num_clauses];
+               if (ji->has_generic_jit_info)
+                       ptr += sizeof (MonoGenericJitInfo);
+               return (MonoTryBlockHoleTableJitInfo*)ptr;
+       } else {
+               return NULL;
+       }
+}
 void
 mono_install_create_domain_hook (MonoCreateDomainFunc func)
 {
@@ -1153,7 +1122,7 @@ domain_id_alloc (MonoDomain *domain)
        return id;
 }
 
-static guint32 domain_gc_bitmap [sizeof(MonoDomain)/4/32 + 1];
+static gsize domain_gc_bitmap [sizeof(MonoDomain)/4/32 + 1];
 static gpointer domain_gc_desc = NULL;
 static guint32 domain_shadow_serial = 0L;
 
@@ -1170,28 +1139,39 @@ mono_domain_create (void)
                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] |= 1 << (bit % 32);
+                       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 = mono_gc_alloc_fixed (sizeof (MonoDomain), NULL);
+#else
        domain = mono_gc_alloc_fixed (sizeof (MonoDomain), domain_gc_desc);
+       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), NULL);
+#endif
        domain->shadow_serial = shadow_serial;
        domain->domain = NULL;
        domain->setup = NULL;
        domain->friendly_name = NULL;
        domain->search_path = NULL;
 
-       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), NULL);
-
        mono_profiler_appdomain_event (domain, MONO_PROFILE_START_LOAD);
 
        domain->mp = mono_mempool_new ();
        domain->code_mp = mono_code_manager_new ();
        domain->env = mono_g_hash_table_new_type ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal, MONO_HASH_KEY_VALUE_GC);
        domain->domain_assemblies = NULL;
-       domain->class_vtable_hash = g_hash_table_new (mono_aligned_addr_hash, 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);
@@ -1200,9 +1180,8 @@ mono_domain_create (void)
        domain->jit_info_table = jit_info_table_new (domain);
        domain->jit_info_free_queue = NULL;
        domain->finalizable_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-#ifndef HAVE_SGEN_GC
        domain->track_resurrection_handles_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
-#endif
+       domain->ftnptrs_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
 
        InitializeCriticalSection (&domain->lock);
        InitializeCriticalSection (&domain->assemblies_lock);
@@ -1253,11 +1232,9 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        if (domain)
                g_assert_not_reached ();
 
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
        /* Avoid system error message boxes. */
        SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
-
-       mono_load_coree (exe_filename);
 #endif
 
        mono_perfcounters_init ();
@@ -1268,7 +1245,8 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
 
        mono_gc_base_init ();
 
-       appdomain_thread_id = TlsAlloc ();
+       MONO_FAST_TLS_INIT (tls_appdomain);
+       mono_native_tls_alloc (&appdomain_thread_id, NULL);
 
        InitializeCriticalSection (&appdomains_mutex);
 
@@ -1280,7 +1258,7 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        mono_reflection_init ();
 
        /* FIXME: When should we release this memory? */
-       MONO_GC_REGISTER_ROOT (appdomains_list);
+       MONO_GC_REGISTER_ROOT_FIXED (appdomains_list);
 
        domain = mono_domain_create ();
        mono_root_domain = domain;
@@ -1295,7 +1273,7 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
                 * exe_image, and close it during shutdown.
                 */
                get_runtimes_from_exe (exe_filename, &exe_image, runtimes);
-#ifdef PLATFORM_WIN32
+#ifdef HOST_WIN32
                if (!exe_image) {
                        exe_image = mono_assembly_open_from_bundle (exe_filename, NULL, FALSE);
                        if (!exe_image)
@@ -1313,7 +1291,7 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
                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);
+               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 */
@@ -1325,11 +1303,6 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
 
        }
        
-       /* Now that we have a runtime, set the policy for unhandled exceptions */
-       if (mono_framework_version () < 2) {
-               mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
-       }
-
        if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
                switch (status){
                case MONO_IMAGE_ERROR_ERRNO: {
@@ -1445,9 +1418,9 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
                "AsyncResult");
        g_assert (mono_defaults.asyncresult_class != 0 );
 
-       mono_defaults.waithandle_class = mono_class_from_name (
-               mono_defaults.corlib, "System.Threading", "WaitHandle");
-       g_assert (mono_defaults.waithandle_class != 0 );
+       mono_defaults.manualresetevent_class = mono_class_from_name (
+               mono_defaults.corlib, "System.Threading", "ManualResetEvent");
+       g_assert (mono_defaults.manualresetevent_class != 0 );
 
        mono_defaults.typehandle_class = mono_class_from_name (
                 mono_defaults.corlib, "System", "RuntimeTypeHandle");
@@ -1481,6 +1454,16 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
                 mono_defaults.corlib, "System.Threading", "Thread");
        g_assert (mono_defaults.thread_class != 0);
 
+       mono_defaults.internal_thread_class = mono_class_from_name (
+                mono_defaults.corlib, "System.Threading", "InternalThread");
+       if (!mono_defaults.internal_thread_class) {
+               /* This can happen with an old mscorlib */
+               fprintf (stderr, "Corlib too old for this runtime.\n");
+               fprintf (stderr, "Loaded from: %s\n",
+                                mono_defaults.corlib? mono_image_get_filename (mono_defaults.corlib): "unknown");
+               exit (1);
+       }
+
        mono_defaults.appdomain_class = mono_class_from_name (
                 mono_defaults.corlib, "System", "AppDomain");
        g_assert (mono_defaults.appdomain_class != 0);
@@ -1629,7 +1612,11 @@ mono_init (const char *domain_name)
 
 /**
  * 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.
@@ -1648,8 +1635,11 @@ mono_init_from_assembly (const char *domain_name, const char *filename)
 /**
  * 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.
  *
@@ -1711,6 +1701,9 @@ mono_cleanup (void)
 {
        mono_close_exe_image ();
 
+       mono_defaults.corlib = NULL;
+
+       mono_config_cleanup ();
        mono_loader_cleanup ();
        mono_classes_cleanup ();
        mono_assemblies_cleanup ();
@@ -1718,8 +1711,12 @@ mono_cleanup (void)
        mono_debug_cleanup ();
        mono_metadata_cleanup ();
 
-       TlsFree (appdomain_thread_id);
+       mono_native_tls_free (appdomain_thread_id);
        DeleteCriticalSection (&appdomains_mutex);
+
+#ifndef HOST_WIN32
+       _wapi_cleanup ();
+#endif
 }
 
 void
@@ -1757,10 +1754,16 @@ 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)
 {
-       MonoThread *thread;
+       MonoInternalThread *thread;
 
        if (mono_domain_get () == domain)
                return;
@@ -1769,7 +1772,7 @@ mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exce
        SET_APPCONTEXT (domain->default_context);
 
        if (migrate_exception) {
-               thread = mono_thread_current ();
+               thread = mono_thread_internal_current ();
                if (!thread->abort_exc)
                        return;
 
@@ -1859,6 +1862,15 @@ free_slist (gpointer key, gpointer value, gpointer user_data)
        g_slist_free (value);
 }
 
+static void
+unregister_vtable_reflection_type (MonoVTable *vtable)
+{
+       MonoObject *type = vtable->type;
+
+       if (type->vtable->klass != mono_defaults.monotype_class)
+               MONO_GC_UNREGISTER_ROOT_IF_MOVING (vtable->type);
+}
+
 void
 mono_domain_free (MonoDomain *domain, gboolean force)
 {
@@ -1874,17 +1886,81 @@ mono_domain_free (MonoDomain *domain, gboolean force)
 
        mono_profiler_appdomain_event (domain, MONO_PROFILE_START_UNLOAD);
 
-       if (free_domain_hook)
-               free_domain_hook (domain);
-
        mono_debug_domain_unload (domain);
 
-       mono_gc_clear_domain (domain);
-
        mono_appdomains_lock ();
        appdomains_list [domain->domain_id] = NULL;
        mono_appdomains_unlock ();
 
+       /* must do this early as it accesses fields and types */
+       if (domain->special_static_fields) {
+               mono_alloc_special_static_data_free (domain->special_static_fields);
+               g_hash_table_destroy (domain->special_static_fields);
+               domain->special_static_fields = NULL;
+       }
+
+       /*
+        * We must destroy all these hash tables here because they
+        * contain references to managed objects belonging to the
+        * domain.  Once we let the GC clear the domain there must be
+        * no more such references, or we'll crash if a collection
+        * occurs.
+        */
+       mono_g_hash_table_destroy (domain->ldstr_table);
+       domain->ldstr_table = NULL;
+
+       mono_g_hash_table_destroy (domain->env);
+       domain->env = NULL;
+
+       if (domain->tlsrec_list) {
+               mono_thread_destroy_domain_tls (domain);
+               domain->tlsrec_list = NULL;
+       }
+
+       mono_reflection_cleanup_domain (domain);
+
+       if (domain->type_hash) {
+               mono_g_hash_table_destroy (domain->type_hash);
+               domain->type_hash = NULL;
+       }
+       if (domain->type_init_exception_hash) {
+               mono_g_hash_table_destroy (domain->type_init_exception_hash);
+               domain->type_init_exception_hash = NULL;
+       }
+
+       if (domain->class_vtable_array) {
+               int i;
+               for (i = 0; i < domain->class_vtable_array->len; ++i)
+                       unregister_vtable_reflection_type (g_ptr_array_index (domain->class_vtable_array, i));
+       }
+
+       /* This needs to be done before closing assemblies */
+       mono_gc_clear_domain (domain);
+
+       for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
+               MonoAssembly *ass = tmp->data;
+               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading domain %s[%p], assembly %s[%p], ref_count=%d\n", domain->friendly_name, domain, ass->aname.name, ass, ass->ref_count);
+               if (!mono_assembly_close_except_image_pools (ass))
+                       tmp->data = NULL;
+       }
+
+       for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
+               MonoAssembly *ass = tmp->data;
+               if (ass)
+                       mono_assembly_close_finish (ass);
+       }
+       g_slist_free (domain->domain_assemblies);
+       domain->domain_assemblies = NULL;
+
+       /* 
+        * Send this after the assemblies have been unloaded and the domain is still in a 
+        * usable state.
+        */
+       mono_profiler_appdomain_event (domain, MONO_PROFILE_END_UNLOAD);
+
+       if (free_domain_hook)
+               free_domain_hook (domain);
+
        /* FIXME: free delegate_hash_table when it's used */
        if (domain->search_path) {
                g_strfreev (domain->search_path);
@@ -1896,27 +1972,13 @@ mono_domain_free (MonoDomain *domain, gboolean force)
        domain->out_of_memory_ex = NULL;
        domain->null_reference_ex = NULL;
        domain->stack_overflow_ex = NULL;
+       domain->ephemeron_tombstone = NULL;
        domain->entry_assembly = NULL;
-       /* must do this early as it accesses fields and types */
-       if (domain->special_static_fields) {
-               mono_alloc_special_static_data_free (domain->special_static_fields);
-               g_hash_table_destroy (domain->special_static_fields);
-               domain->special_static_fields = NULL;
-       }
-       for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
-               MonoAssembly *ass = tmp->data;
-               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);
-               mono_assembly_close (ass);
-       }
-       g_slist_free (domain->domain_assemblies);
-       domain->domain_assemblies = NULL;
 
        g_free (domain->friendly_name);
        domain->friendly_name = NULL;
-       mono_g_hash_table_destroy (domain->env);
-       domain->env = NULL;
-       g_hash_table_destroy (domain->class_vtable_hash);
-       domain->class_vtable_hash = NULL;
+       g_ptr_array_free (domain->class_vtable_array, TRUE);
+       domain->class_vtable_array = NULL;
        g_hash_table_destroy (domain->proxy_vtable_hash);
        domain->proxy_vtable_hash = NULL;
        if (domain->static_data_array) {
@@ -1924,8 +1986,6 @@ mono_domain_free (MonoDomain *domain, gboolean force)
                domain->static_data_array = NULL;
        }
        mono_internal_hash_table_destroy (&domain->jit_code_hash);
-       mono_g_hash_table_destroy (domain->ldstr_table);
-       domain->ldstr_table = NULL;
 
        /*
         * There might still be jit info tables of this domain which
@@ -1955,26 +2015,14 @@ mono_domain_free (MonoDomain *domain, gboolean force)
        domain->code_mp = NULL;
 #endif 
 
-       mono_reflection_cleanup_domain (domain);
-       
-       if (domain->type_hash) {
-               mono_g_hash_table_destroy (domain->type_hash);
-               domain->type_hash = NULL;
-       }
-       if (domain->type_init_exception_hash) {
-               mono_g_hash_table_destroy (domain->type_init_exception_hash);
-               domain->type_init_exception_hash = NULL;
-       }
        g_hash_table_destroy (domain->finalizable_objects_hash);
        domain->finalizable_objects_hash = NULL;
-#ifndef HAVE_SGEN_GC
        if (domain->track_resurrection_objects_hash) {
                g_hash_table_foreach (domain->track_resurrection_objects_hash, free_slist, NULL);
                g_hash_table_destroy (domain->track_resurrection_objects_hash);
        }
        if (domain->track_resurrection_handles_hash)
                g_hash_table_destroy (domain->track_resurrection_handles_hash);
-#endif
        if (domain->method_rgctx_hash) {
                g_hash_table_destroy (domain->method_rgctx_hash);
                domain->method_rgctx_hash = NULL;
@@ -1983,6 +2031,14 @@ mono_domain_free (MonoDomain *domain, gboolean force)
                g_hash_table_destroy (domain->generic_virtual_cases);
                domain->generic_virtual_cases = NULL;
        }
+       if (domain->generic_virtual_thunks) {
+               g_hash_table_destroy (domain->generic_virtual_thunks);
+               domain->generic_virtual_thunks = NULL;
+       }
+       if (domain->ftnptrs_hash) {
+               g_hash_table_destroy (domain->ftnptrs_hash);
+               domain->ftnptrs_hash = NULL;
+       }
 
        DeleteCriticalSection (&domain->finalizable_objects_hash_lock);
        DeleteCriticalSection (&domain->assemblies_lock);
@@ -1990,9 +2046,9 @@ mono_domain_free (MonoDomain *domain, gboolean force)
        DeleteCriticalSection (&domain->lock);
        domain->setup = NULL;
 
-       /* FIXME: anything else required ? */
+       mono_gc_deregister_root ((char*)&(domain->MONO_DOMAIN_FIRST_GC_TRACKED));
 
-       mono_profiler_appdomain_event (domain, MONO_PROFILE_END_UNLOAD);
+       /* FIXME: anything else required ? */
 
        mono_gc_free_fixed (domain);
 
@@ -2112,6 +2168,58 @@ mono_domain_code_commit (MonoDomain *domain, void *data, int size, int newsize)
        mono_domain_unlock (domain);
 }
 
+#if defined(__native_client_codegen__) && defined(__native_client__)
+/*
+ * Given the temporary buffer (allocated by mono_domain_code_reserve) into which
+ * we are generating code, return a pointer to the destination in the dynamic 
+ * code segment into which the code will be copied when mono_domain_code_commit
+ * is called.
+ * LOCKING: Acquires the domain lock.
+ */
+void *
+nacl_domain_get_code_dest (MonoDomain *domain, void *data)
+{
+       void *dest;
+       mono_domain_lock (domain);
+       dest = nacl_code_manager_get_code_dest (domain->code_mp, data);
+       mono_domain_unlock (domain);
+       return dest;
+}
+
+/* 
+ * Convenience function which calls mono_domain_code_commit to validate and copy
+ * the code. The caller sets *buf_base and *buf_size to the start and size of
+ * the buffer (allocated by mono_domain_code_reserve), and *code_end to the byte
+ * after the last instruction byte. On return, *buf_base will point to the start
+ * of the copied in the code segment, and *code_end will point after the end of 
+ * the copied code.
+ */
+void
+nacl_domain_code_validate (MonoDomain *domain, guint8 **buf_base, int buf_size, guint8 **code_end)
+{
+       guint8 *tmp = nacl_domain_get_code_dest (domain, *buf_base);
+       mono_domain_code_commit (domain, *buf_base, buf_size, *code_end - *buf_base);
+       *code_end = tmp + (*code_end - *buf_base);
+       *buf_base = tmp;
+}
+
+#else
+
+/* no-op versions of Native Client functions */
+
+void *
+nacl_domain_get_code_dest (MonoDomain *domain, void *data)
+{
+       return data;
+}
+
+void
+nacl_domain_code_validate (MonoDomain *domain, guint8 **buf_base, int buf_size, guint8 **code_end)
+{
+}
+
+#endif
+
 /*
  * mono_domain_code_foreach:
  * Iterate over the code thunks of the code manager of @domain.
@@ -2154,8 +2262,9 @@ mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointe
                int size = GPOINTER_TO_INT (domain->static_data_array [1]);
                next = GPOINTER_TO_INT (domain->static_data_array [0]);
                if (next >= size) {
-                       gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * (size * 2), NULL);
-                       memcpy (new_array, domain->static_data_array, sizeof (gpointer) * size);
+                       /* 'data' is allocated by alloc_fixed */
+                       gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * (size * 2), MONO_GC_ROOT_DESCR_FOR_FIXED (size * 2));
+                       mono_gc_memmove (new_array, domain->static_data_array, sizeof (gpointer) * size);
                        size *= 2;
                        new_array [1] = GINT_TO_POINTER (size);
                        mono_gc_free_fixed (domain->static_data_array);
@@ -2163,7 +2272,7 @@ mono_domain_add_class_static_data (MonoDomain *domain, MonoClass *klass, gpointe
                }
        } else {
                int size = 32;
-               gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * size, NULL);
+               gpointer *new_array = mono_gc_alloc_fixed (sizeof (gpointer) * size, MONO_GC_ROOT_DESCR_FOR_FIXED (size));
                next = 2;
                new_array [0] = GINT_TO_POINTER (next);
                new_array [1] = GINT_TO_POINTER (size);
@@ -2427,11 +2536,24 @@ get_runtime_by_version (const char *version)
 {
        int n;
        int max = G_N_ELEMENTS (supported_runtimes);
-       
+       int vlen;
+
+       if (!version)
+               return NULL;
+
        for (n=0; n<max; n++) {
                if (strcmp (version, supported_runtimes[n].runtime_version) == 0)
                        return &supported_runtimes[n];
        }
+       
+       vlen = strlen (version);
+       if (vlen >= 4 && version [1] - '0' >= 4) {
+               for (n=0; n<max; n++) {
+                       if (strncmp (version, supported_runtimes[n].runtime_version, 4) == 0)
+                               return &supported_runtimes[n];
+               }
+       }
+       
        return NULL;
 }