[sgen] Binary protocol for every write barrier, not just the generic store one.
[mono.git] / mono / metadata / appdomain.c
index 6b10fb56295fb9b8d99ed9ee3618198206c424e9..f9cb246a676807b76f62da4f0d147a42a6f1c98e 100644 (file)
 #include <config.h>
 #include <glib.h>
 #include <string.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 #include <errno.h>
-#include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 #ifdef HAVE_UTIME_H
 #include <utime.h>
 #else
@@ -73,7 +73,7 @@
  * Changes which are already detected at runtime, like the addition
  * of icalls, do not require an increment.
  */
-#define MONO_CORLIB_VERSION 89
+#define MONO_CORLIB_VERSION 96
 
 typedef struct
 {
@@ -85,12 +85,7 @@ typedef struct
 
 CRITICAL_SECTION mono_delegate_section;
 
-#ifdef _EGLIB_MAJOR
-/* Need to lock here because EGLIB has locking defined as no-ops, we can not depend on mono_strtod do the right locking */
-/* Ideally this will be fixed in eglib */
 CRITICAL_SECTION mono_strtod_mutex;
-#endif
-
 
 static gunichar2 process_guid [36];
 static gboolean process_guid_set = FALSE;
@@ -165,7 +160,7 @@ mono_runtime_get_no_exec (void)
 }
 
 static void
-create_exceptions (MonoDomain *domain)
+create_domain_objects (MonoDomain *domain)
 {
        MonoDomain *old_domain = mono_domain_get ();
        MonoString *arg;
@@ -190,6 +185,9 @@ create_exceptions (MonoDomain *domain)
        arg = mono_string_new (domain, "The requested operation caused a stack overflow.");
        domain->stack_overflow_ex = mono_exception_from_name_two_strings (mono_defaults.corlib, "System", "StackOverflowException", arg, NULL);
 
+       /*The ephemeron tombstone i*/
+       domain->ephemeron_tombstone = mono_object_new (domain, mono_defaults.object_class);
+
        if (domain != old_domain) {
                mono_thread_pop_appdomain_ref ();
                mono_domain_set_internal_with_options (old_domain, FALSE);
@@ -251,10 +249,7 @@ mono_runtime_init (MonoDomain *domain, MonoThreadStartCB start_cb,
 
        InitializeCriticalSection (&mono_delegate_section);
 
-#ifdef _EGLIB_MAJOR
-       /* Needed until EGLIB is fixed #464316 */
        InitializeCriticalSection (&mono_strtod_mutex);
-#endif
        
        mono_thread_attach (domain);
        mono_context_init (domain);
@@ -263,7 +258,7 @@ mono_runtime_init (MonoDomain *domain, MonoThreadStartCB start_cb,
        mono_type_initialization_init ();
 
        if (!mono_runtime_get_no_exec ())
-               create_exceptions (domain);
+               create_domain_objects (domain);
 
        /* GC init has to happen after thread init */
        mono_gc_init ();
@@ -367,10 +362,6 @@ mono_runtime_cleanup (MonoDomain *domain)
        mono_type_initialization_cleanup ();
 
        mono_monitor_cleanup ();
-
-#ifndef HOST_WIN32
-       _wapi_cleanup ();
-#endif
 }
 
 static MonoDomainFunc quit_function = NULL;
@@ -516,7 +507,7 @@ mono_domain_create_appdomain_internal (char *friendly_name, MonoAppDomainSetup *
        g_free (shadow_location);
 #endif
 
-       create_exceptions (data);
+       create_domain_objects (data);
 
        return ad;
 }
@@ -539,6 +530,10 @@ mono_domain_has_type_resolve (MonoDomain *domain)
                g_assert (field);
        }
 
+       /*pedump doesn't create an appdomin, so the domain object doesn't exist.*/
+       if (!domain->domain)
+               return FALSE;
+
        mono_field_get_value ((MonoObject*)(domain->domain), field, &o);
        return o != NULL;
 }
@@ -821,7 +816,7 @@ void
 mono_set_private_bin_path_from_config (MonoDomain *domain)
 {
        MonoError error;
-       gchar *config_file, *text;
+       gchar *config_file_name = NULL, *text = NULL, *config_file_path = NULL;
        gsize len;
        GMarkupParseContext *context;
        RuntimeConfig runtime_config;
@@ -830,21 +825,23 @@ mono_set_private_bin_path_from_config (MonoDomain *domain)
        if (!domain || !domain->setup || !domain->setup->configuration_file)
                return;
 
-       config_file = mono_string_to_utf8_checked (domain->setup->configuration_file, &error); 
+       config_file_name = mono_string_to_utf8_checked (domain->setup->configuration_file, &error);
        if (!mono_error_ok (&error)) {
                mono_error_cleanup (&error);
-               return;
+               goto free_and_out;
        }
 
-       if (!g_file_get_contents (config_file, &text, &len, NULL)) {
-               g_free (config_file);
-               return;
-       }
+       config_file_path = mono_portability_find_file (config_file_name, TRUE);
+       if (!config_file_path)
+               config_file_path = config_file_name;
+
+       if (!g_file_get_contents (config_file_path, &text, &len, NULL))
+               goto free_and_out;
 
        runtime_config.runtime_count = 0;
        runtime_config.assemblybinding_count = 0;
        runtime_config.domain = domain;
-       runtime_config.filename = config_file;
+       runtime_config.filename = config_file_path;
        
        offset = 0;
        if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
@@ -854,8 +851,12 @@ mono_set_private_bin_path_from_config (MonoDomain *domain)
        if (g_markup_parse_context_parse (context, text + offset, len - offset, NULL))
                g_markup_parse_context_end_parse (context, NULL);
        g_markup_parse_context_free (context);
+
+  free_and_out:
        g_free (text);
-       g_free (config_file);
+       if (config_file_name != config_file_path)
+               g_free (config_file_name);
+       g_free (config_file_path);
 }
 
 MonoAppDomain *
@@ -950,12 +951,17 @@ mono_domain_assembly_postload_search (MonoAssemblyName *aname,
        MonoReflectionAssembly *assembly;
        MonoDomain *domain = mono_domain_get ();
        char *aname_str;
+       MonoString *str;
 
        aname_str = mono_stringify_assembly_name (aname);
 
        /* FIXME: We invoke managed code here, so there is a potential for deadlocks */
-       assembly = mono_try_assembly_resolve (domain, mono_string_new (domain, aname_str), refonly);
-
+       str = mono_string_new (domain, aname_str);
+       if (!str) {
+               g_free (aname_str);
+               return NULL;
+       }
+       assembly = mono_try_assembly_resolve (domain, str, refonly);
        g_free (aname_str);
 
        if (assembly)
@@ -990,7 +996,7 @@ add_assemblies_to_domain (MonoDomain *domain, MonoAssembly *ass, GHashTable *ht)
                mono_assembly_addref (ass);
                g_hash_table_insert (ht, ass, ass);
                domain->domain_assemblies = g_slist_prepend (domain->domain_assemblies, ass);
-               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly %s %p added to domain %s, ref_count=%d\n", ass->aname.name, ass, domain->friendly_name, ass->ref_count);
+               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly %s[%p] added to domain %s, ref_count=%d", ass->aname.name, ass, domain->friendly_name, ass->ref_count);
        }
 
        if (ass->image->references) {
@@ -1093,7 +1099,7 @@ set_domain_search_path (MonoDomain *domain)
        if (setup->private_bin_path) {
                search_path = mono_string_to_utf8_checked (setup->private_bin_path, &error);
                if (!mono_error_ok (&error)) { /*FIXME maybe we should bubble up the error.*/
-                       g_warning ("Could not decode AppDomain search path since it contains invalid caracters");
+                       g_warning ("Could not decode AppDomain search path since it contains invalid characters");
                        mono_error_cleanup (&error);
                        mono_domain_assemblies_unlock (domain);
                        return;
@@ -1254,8 +1260,18 @@ shadow_copy_sibling (gchar *src, gint srclen, const char *extension, gchar *targ
        gboolean copy_result;
        
        strcpy (src + srclen - tail_len, extension);
-       if (!g_file_test (src, G_FILE_TEST_IS_REGULAR))
+
+       if (IS_PORTABILITY_CASE) {
+               gchar *file = mono_portability_find_file (src, TRUE);
+
+               if (file == NULL)
+                       return TRUE;
+
+               g_free (file);
+       } else if (!g_file_test (src, G_FILE_TEST_IS_REGULAR)) {
                return TRUE;
+       }
+
        orig = g_utf8_to_utf16 (src, strlen (src), NULL, NULL, NULL);
 
        strcpy (target + targetlen - tail_len, extension);
@@ -1465,10 +1481,22 @@ static gboolean
 private_file_needs_copying (const char *src, struct stat *sbuf_src, char *dest)
 {
        struct stat sbuf_dest;
+       gchar *real_src = mono_portability_find_file (src, TRUE);
+
+       if (!real_src)
+               real_src = (gchar*)src;
        
-       if (stat (src, sbuf_src) == -1 || stat (dest, &sbuf_dest) == -1)
+       if (stat (real_src, sbuf_src) == -1) {
+               time_t tnow = time (NULL);
+               memset (sbuf_src, 0, sizeof (*sbuf_src));
+               sbuf_src->st_mtime = tnow;
+               sbuf_src->st_atime = tnow;
                return TRUE;
+       }
 
+       if (stat (dest, &sbuf_dest) == -1)
+               return TRUE;
+       
        if (sbuf_src->st_size == sbuf_dest.st_size &&
            sbuf_src->st_mtime == sbuf_dest.st_mtime)
                return FALSE;
@@ -2136,28 +2164,28 @@ mono_domain_is_unloading (MonoDomain *domain)
 }
 
 static void
-clear_cached_vtable (gpointer key, gpointer value, gpointer user_data)
+clear_cached_vtable (MonoVTable *vtable)
 {
-       MonoClass *klass = (MonoClass*)key;
-       MonoVTable *vtable = value;
-       MonoDomain *domain = (MonoDomain*)user_data;
+       MonoClass *klass = vtable->klass;
+       MonoDomain *domain = vtable->domain;
        MonoClassRuntimeInfo *runtime_info;
+       void *data;
 
        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);
+       if (klass->has_static_refs && (data = mono_vtable_get_static_field_data (vtable)))
+               mono_gc_free_fixed (data);
 }
 
 static G_GNUC_UNUSED void
-zero_static_data (gpointer key, gpointer value, gpointer user_data)
+zero_static_data (MonoVTable *vtable)
 {
-       MonoClass *klass = (MonoClass*)key;
-       MonoVTable *vtable = value;
+       MonoClass *klass = vtable->klass;
+       void *data;
 
-       if (vtable->data && klass->has_static_refs)
-               memset (vtable->data, 0, mono_class_data_size (klass));
+       if (klass->has_static_refs && (data = mono_vtable_get_static_field_data (vtable)))
+               mono_gc_bzero (data, mono_class_data_size (klass));
 }
 
 typedef struct unload_data {
@@ -2165,7 +2193,6 @@ 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)
 {
@@ -2229,7 +2256,6 @@ deregister_reflection_info_roots (MonoDomain *domain)
        mono_domain_assemblies_unlock (domain);
        mono_loader_unlock ();
 }
-#endif
 
 static guint32 WINAPI
 unload_thread_main (void *arg)
@@ -2237,6 +2263,7 @@ unload_thread_main (void *arg)
        unload_data *data = (unload_data*)arg;
        MonoDomain *domain = data->domain;
        MonoThread *thread;
+       int i;
 
        /* Have to attach to the runtime so shutdown can wait for this thread */
        thread = mono_thread_attach (mono_get_root_domain ());
@@ -2278,13 +2305,16 @@ unload_thread_main (void *arg)
         * now be null we won't do any unnecessary copies and after
         * the collection there won't be any more remsets.
         */
-       g_hash_table_foreach (domain->class_vtable_hash, zero_static_data, domain);
+       for (i = 0; i < domain->class_vtable_array->len; ++i)
+               zero_static_data (g_ptr_array_index (domain->class_vtable_array, i));
        mono_gc_collect (0);
 #endif
-       g_hash_table_foreach (domain->class_vtable_hash, clear_cached_vtable, domain);
-#ifdef HAVE_SGEN_GC
+       for (i = 0; i < domain->class_vtable_array->len; ++i)
+               clear_cached_vtable (g_ptr_array_index (domain->class_vtable_array, i));
        deregister_reflection_info_roots (domain);
-#endif
+
+       mono_assembly_cleanup_domain_bindings (domain->domain_id);
+
        mono_domain_unlock (domain);
        mono_loader_unlock ();
 
@@ -2301,7 +2331,7 @@ unload_thread_main (void *arg)
 
        mono_gc_collect (mono_gc_max_generation ());
 
-       mono_thread_detach  (thread);
+       mono_thread_detach (thread);
 
        return 0;
 }
@@ -2368,7 +2398,7 @@ mono_domain_try_unload (MonoDomain *domain, MonoObject **exc)
                        *exc = (MonoObject *) mono_get_exception_cannot_unload_appdomain ("Appdomain is already unloaded.");
                        return;
                default:
-                       g_warning ("Incalid appdomain state %d", prev_state);
+                       g_warning ("Invalid appdomain state %d", prev_state);
                        g_assert_not_reached ();
                }
        }