2009-07-30 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / appdomain.c
index 45456b934d398245b1a9610b1ec0124590c863fd..f90562f88f19f5af4ae9300608abf883640ac59b 100644 (file)
 #include <mono/metadata/monitor.h>
 #include <mono/metadata/threadpool.h>
 #include <mono/metadata/mono-debug.h>
+#include <mono/metadata/mono-debug-debugger.h>
 #include <mono/metadata/attach.h>
 #include <mono/metadata/file-io.h>
 #include <mono/metadata/lock-tracer.h>
+#include <mono/metadata/console-io.h>
+#include <mono/metadata/threads-types.h>
+#include <mono/metadata/tokentype.h>
 #include <mono/utils/mono-uri.h>
 #include <mono/utils/mono-logger.h>
 #include <mono/utils/mono-path.h>
@@ -68,7 +72,7 @@
  * Changes which are already detected at runtime, like the addition
  * of icalls, do not require an increment.
  */
-#define MONO_CORLIB_VERSION 70
+#define MONO_CORLIB_VERSION 79
 
 typedef struct
 {
@@ -115,6 +119,9 @@ add_assemblies_to_domain (MonoDomain *domain, MonoAssembly *ass, GHashTable *has
 static MonoAppDomain *
 mono_domain_create_appdomain_internal (char *friendly_name, MonoAppDomainSetup *setup);
 
+static char *
+get_shadow_assembly_location_base (MonoDomain *domain);
+
 static MonoLoadFunc load_function = NULL;
 
 void
@@ -235,6 +242,7 @@ mono_runtime_init (MonoDomain *domain, MonoThreadStartCB start_cb,
        mono_network_init ();
 #endif
        
+       mono_console_init ();
        mono_attach_init ();
 
        mono_locks_tracer_init ();
@@ -392,6 +400,7 @@ mono_domain_create_appdomain_internal (char *friendly_name, MonoAppDomainSetup *
        MonoClass *adclass;
        MonoAppDomain *ad;
        MonoDomain *data;
+       char *shadow_location;
        
        MONO_ARCH_SAVE_REGS;
 
@@ -422,6 +431,12 @@ mono_domain_create_appdomain_internal (char *friendly_name, MonoAppDomainSetup *
        
        add_assemblies_to_domain (data, mono_defaults.corlib->assembly, NULL);
 
+#ifndef DISABLE_SHADOW_COPY
+       shadow_location = get_shadow_assembly_location_base (data);
+       mono_debugger_event_create_appdomain (data, shadow_location);
+       g_free (shadow_location);
+#endif
+       
        return ad;
 }
 
@@ -776,7 +791,7 @@ ves_icall_System_AppDomain_GetAssemblies (MonoAppDomain *ad, MonoBoolean refonly
        mono_domain_assemblies_lock (domain);
        for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
                ass = tmp->data;
-               if (refonly && !ass->ref_only)
+               if (refonly != ass->ref_only)
                        continue;
                if (ass->corlib_internal)
                        continue;
@@ -803,6 +818,9 @@ mono_try_assembly_resolve (MonoDomain *domain, MonoString *fname, gboolean refon
        MonoBoolean isrefonly;
        gpointer params [2];
 
+       if (mono_runtime_get_no_exec ())
+               return NULL;
+
        g_assert (domain != NULL && fname != NULL);
 
        klass = domain->domain->mbr.obj.vtable->klass;
@@ -1149,6 +1167,9 @@ get_cstring_hash (const char *str)
        return h;
 }
 
+/*
+ * Returned memory is malloc'd. Called must free it 
+ */
 static char *
 get_shadow_assembly_location_base (MonoDomain *domain)
 {
@@ -1199,6 +1220,8 @@ get_shadow_assembly_location (const char *filename)
        tmploc = get_shadow_assembly_location_base (domain);
        location = g_build_filename (tmploc, name_hash, path_hash, bname, NULL);
        g_free (tmploc);
+       g_free (bname);
+       g_free (dirname);
        return location;
 }
 
@@ -1380,7 +1403,7 @@ mono_is_shadow_copy_enabled (MonoDomain *domain, const gchar *dir_name)
 
        /* Is dir_name a shadow_copy destination already? */
        base_dir = get_shadow_assembly_location_base (domain);
-       if (strstr (dir_name, base_dir) == dir_name) {
+       if (strstr (dir_name, base_dir)) {
                g_free (base_dir);
                return TRUE;
        }
@@ -1414,14 +1437,24 @@ mono_make_shadow_copy (const char *filename)
        struct utimbuf utbuf;
        char *dir_name = g_path_get_dirname (filename);
        MonoDomain *domain = mono_domain_get ();
+       char *shadow_dir;
+
        set_domain_search_path (domain);
 
        if (!mono_is_shadow_copy_enabled (domain, dir_name)) {
                g_free (dir_name);
                return (char *) filename;
        }
+       /* Is dir_name a shadow_copy destination already? */
+       shadow_dir = get_shadow_assembly_location_base (domain);
+       if (strstr (dir_name, shadow_dir)) {
+               g_free (shadow_dir);
+               g_free (dir_name);
+               return (char *) filename;
+       }
+       g_free (shadow_dir);
        g_free (dir_name);
-       
+
        shadow = get_shadow_assembly_location (filename);
        if (ensure_directory_exists (shadow) == FALSE) {
                g_free (shadow);
@@ -1923,12 +1956,15 @@ static void
 clear_cached_vtable (gpointer key, gpointer value, gpointer user_data)
 {
        MonoClass *klass = (MonoClass*)key;
+       MonoVTable *vtable = value;
        MonoDomain *domain = (MonoDomain*)user_data;
        MonoClassRuntimeInfo *runtime_info;
 
        runtime_info = klass->runtime_info;
        if (runtime_info && runtime_info->max_domain >= domain->domain_id)
                runtime_info->domain_vtables [domain->domain_id] = NULL;
+       if (vtable->data && klass->has_static_refs)
+               mono_gc_free_fixed (vtable->data);
 }
 
 typedef struct unload_data {
@@ -1936,6 +1972,49 @@ typedef struct unload_data {
        char *failure_reason;
 } unload_data;
 
+#ifdef HAVE_SGEN_GC
+static void
+deregister_reflection_info_roots_nspace_table (gpointer key, gpointer value, gpointer image)
+{
+       guint32 index = GPOINTER_TO_UINT (value);
+       MonoClass *class = mono_class_get (image, MONO_TOKEN_TYPE_DEF | index);
+
+       g_assert (class);
+
+       if (class->reflection_info)
+               mono_gc_deregister_root ((char*) &class->reflection_info);
+}
+
+static void
+deregister_reflection_info_roots_name_space (gpointer key, gpointer value, gpointer user_data)
+{
+       g_hash_table_foreach (value, deregister_reflection_info_roots_nspace_table, user_data);
+}
+
+static void
+deregister_reflection_info_roots (MonoDomain *domain)
+{
+       GSList *list;
+
+       mono_loader_lock ();
+       mono_domain_assemblies_lock (domain);
+       for (list = domain->domain_assemblies; list; list = list->next) {
+               MonoAssembly *assembly = list->data;
+               MonoImage *image = assembly->image;
+               int i;
+               /*No need to take the image lock here since dynamic images are appdomain bound and at this point the mutator is gone.*/
+               if (image->dynamic && image->name_cache)
+                       g_hash_table_foreach (image->name_cache, deregister_reflection_info_roots_name_space, image);
+               for (i = 0; i < image->module_count; ++i) {
+                       MonoImage *module = image->modules [i];
+                       if (module && module->dynamic && module->name_cache)
+                               g_hash_table_foreach (module->name_cache, deregister_reflection_info_roots_name_space, module);
+               }
+       }
+       mono_domain_assemblies_unlock (domain);
+       mono_loader_unlock ();
+}
+#endif
 
 static guint32 WINAPI
 unload_thread_main (void *arg)
@@ -1974,6 +2053,9 @@ unload_thread_main (void *arg)
        mono_loader_lock ();
        mono_domain_lock (domain);
        g_hash_table_foreach (domain->class_vtable_hash, clear_cached_vtable, domain);
+#ifdef HAVE_SGEN_GC
+       deregister_reflection_info_roots (domain);
+#endif
        mono_domain_unlock (domain);
        mono_loader_unlock ();
 
@@ -2037,6 +2119,8 @@ mono_domain_unload (MonoDomain *domain)
                }
        }
 
+       mono_debugger_event_unload_appdomain (domain);
+
        mono_domain_set (domain, FALSE);
        /* Notify OnDomainUnload listeners */
        method = mono_class_get_method_from_name (domain->domain->mbr.obj.vtable->klass, "DoDomainUnload", -1); 
@@ -2050,6 +2134,7 @@ mono_domain_unload (MonoDomain *domain)
                mono_domain_set (caller_domain, FALSE);
                mono_raise_exception ((MonoException*)exc);
        }
+       mono_domain_set (caller_domain, FALSE);
 
        thread_data.domain = domain;
        thread_data.failure_reason = NULL;
@@ -2066,9 +2151,9 @@ mono_domain_unload (MonoDomain *domain)
         * http://bugzilla.ximian.com/show_bug.cgi?id=27663
         */ 
 #if 0
-       thread_handle = CreateThread (NULL, 0, unload_thread_main, &thread_data, 0, &tid);
+       thread_handle = mono_create_thread (NULL, 0, unload_thread_main, &thread_data, 0, &tid);
 #else
-       thread_handle = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE)unload_thread_main, &thread_data, CREATE_SUSPENDED, &tid);
+       thread_handle = mono_create_thread (NULL, 0, (LPTHREAD_START_ROUTINE)unload_thread_main, &thread_data, CREATE_SUSPENDED, &tid);
        if (thread_handle == NULL) {
                return;
        }
@@ -2085,15 +2170,13 @@ mono_domain_unload (MonoDomain *domain)
        }
        CloseHandle (thread_handle);
 
-       mono_domain_set (caller_domain, FALSE);
-
        if (thread_data.failure_reason) {
                MonoException *ex;
 
                /* Roll back the state change */
                domain->state = MONO_APPDOMAIN_CREATED;
 
-               g_warning (thread_data.failure_reason);
+               g_warning ("%s", thread_data.failure_reason);
 
                ex = mono_get_exception_cannot_unload_appdomain (thread_data.failure_reason);