2009-05-25 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / appdomain.c
index 0d765da703fd713d06b1d0021f13f502b210c31d..0451dee78ab4ac86deb540017c5c9edafdc93e82 100644 (file)
@@ -6,7 +6,8 @@
  *     Patrik Torstensson
  *     Gonzalo Paniagua Javier (gonzalo@ximian.com)
  *
- * (c) 2001-2003 Ximian, Inc. (http://www.ximian.com)
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 #undef ASSEMBLY_LOAD_DEBUG
 #include <config.h>
@@ -18,6 +19,9 @@
 #include <errno.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
 #ifdef HAVE_UTIME_H
 #include <utime.h>
 #else
 #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>
@@ -63,7 +72,7 @@
  * Changes which are already detected at runtime, like the addition
  * of icalls, do not require an increment.
  */
-#define MONO_CORLIB_VERSION 69
+#define MONO_CORLIB_VERSION 76
 
 typedef struct
 {
@@ -110,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
@@ -226,10 +238,15 @@ mono_runtime_init (MonoDomain *domain, MonoThreadStartCB start_cb,
        /* GC init has to happen after thread init */
        mono_gc_init ();
 
+#ifndef DISABLE_SOCKETS
        mono_network_init ();
-
+#endif
+       
+       mono_console_init ();
        mono_attach_init ();
 
+       mono_locks_tracer_init ();
+
        /* mscorlib is loaded before we install the load hook */
        mono_domain_fire_assembly_load (mono_defaults.corlib->assembly, NULL);
        
@@ -298,8 +315,9 @@ mono_runtime_cleanup (MonoDomain *domain)
 
        mono_thread_cleanup ();
 
+#ifndef DISABLE_SOCKETS
        mono_network_cleanup ();
-
+#endif
        mono_marshal_cleanup ();
 
        mono_type_initialization_cleanup ();
@@ -382,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;
 
@@ -412,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;
 }
 
@@ -704,7 +729,6 @@ mono_set_private_bin_path_from_config (MonoDomain *domain)
 {
        gchar *config_file, *text;
        gsize len;
-       struct stat sbuf;
        GMarkupParseContext *context;
        RuntimeConfig runtime_config;
        
@@ -712,10 +736,6 @@ mono_set_private_bin_path_from_config (MonoDomain *domain)
                return;
 
        config_file = mono_string_to_utf8 (domain->setup->configuration_file);
-       if (stat (config_file, &sbuf) != 0) {
-               g_free (config_file);
-               return;
-       }
 
        if (!g_file_get_contents (config_file, &text, &len, NULL)) {
                g_free (config_file);
@@ -771,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;
@@ -1089,6 +1109,19 @@ set_domain_search_path (MonoDomain *domain)
        mono_domain_assemblies_unlock (domain);
 }
 
+#ifdef DISABLE_SHADOW_COPY
+gboolean
+mono_is_shadow_copy_enabled (MonoDomain *domain, const gchar *dir_name)
+{
+       return FALSE;
+}
+
+char *
+mono_make_shadow_copy (const char *filename)
+{
+       return (char *) filename;
+}
+#else
 static gboolean
 shadow_copy_sibling (gchar *src, gint srclen, const char *extension, gchar *target, gint targetlen, gint tail_len)
 {
@@ -1131,6 +1164,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)
 {
@@ -1181,6 +1217,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;
 }
 
@@ -1362,7 +1400,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;
        }
@@ -1396,14 +1434,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);
@@ -1459,7 +1507,7 @@ mono_make_shadow_copy (const char *filename)
        
        return shadow;
 }
-
+#endif /* DISABLE_SHADOW_COPY */
 
 MonoDomain *
 mono_domain_from_appdomain (MonoAppDomain *appdomain)
@@ -1694,11 +1742,8 @@ ves_icall_System_AppDomain_LoadAssembly (MonoAppDomain *ad,  MonoString *assRef,
        g_free (name);
 
        if (!parsed) {
-               MonoException *exc;
-
                /* This is a parse error... */
-               exc = mono_get_exception_file_not_found2 (NULL, assRef);
-               mono_raise_exception (exc);
+               return NULL;
        }
 
        ass = mono_assembly_load_full_nosearch (&aname, NULL, &status, refOnly);
@@ -1711,9 +1756,7 @@ ves_icall_System_AppDomain_LoadAssembly (MonoAppDomain *ad,  MonoString *assRef,
                else
                        refass = NULL;
                if (!refass) {
-                       /* FIXME: it doesn't make much sense since we really don't have a filename ... */
-                       MonoException *exc = mono_get_exception_file_not_found2 (NULL, assRef);
-                       mono_raise_exception (exc);
+                       return NULL;
                }
        }
 
@@ -1910,12 +1953,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 {
@@ -1923,6 +1969,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;
+
+               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)
@@ -1957,11 +2046,15 @@ unload_thread_main (void *arg)
         * We also hold the loader lock because we're going to change
         * class->runtime_info.
         */
-       mono_domain_lock (domain);
+
        mono_loader_lock ();
+       mono_domain_lock (domain);
        g_hash_table_foreach (domain->class_vtable_hash, clear_cached_vtable, domain);
-       mono_loader_unlock ();
+#ifdef HAVE_SGEN_GC
+       deregister_reflection_info_roots (domain);
+#endif
        mono_domain_unlock (domain);
+       mono_loader_unlock ();
 
        mono_threads_clear_cached_culture (domain);
 
@@ -2023,6 +2116,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); 
@@ -2036,6 +2131,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;
@@ -2052,9 +2148,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;
        }
@@ -2071,15 +2167,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);