Add a new 'MONO_DEBUGGER_EVENT_TRAMPOLINE' notification for the debugger which also...
[mono.git] / mono / metadata / image.c
index 47e85ea3690f288ec47d69d71eecf955f1c90f19..852349fd3a99c29d8d3d2493cc53a7730c67c784 100644 (file)
@@ -52,8 +52,9 @@ static GHashTable *loaded_images_refonly_hash;
 
 static gboolean debug_assembly_unload = FALSE;
 
-#define mono_images_lock() EnterCriticalSection (&images_mutex)
-#define mono_images_unlock() LeaveCriticalSection (&images_mutex)
+#define mono_images_lock() if (mutex_inited) EnterCriticalSection (&images_mutex)
+#define mono_images_unlock() if (mutex_inited) LeaveCriticalSection (&images_mutex)
+static gboolean mutex_inited;
 static CRITICAL_SECTION images_mutex;
 
 /* returns offset relative to image->raw_data */
@@ -131,6 +132,8 @@ mono_images_init (void)
        loaded_images_refonly_hash = g_hash_table_new (g_str_hash, g_str_equal);
 
        debug_assembly_unload = g_getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
+
+       mutex_inited = TRUE;
 }
 
 /**
@@ -145,6 +148,8 @@ mono_images_cleanup (void)
 
        g_hash_table_destroy (loaded_images_hash);
        g_hash_table_destroy (loaded_images_refonly_hash);
+
+       mutex_inited = FALSE;
 }
 
 /**
@@ -328,6 +333,7 @@ load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
                return FALSE;
        image->raw_metadata = image->raw_data + offset;
 
+       /* 24.2.1: Metadata root starts here */
        ptr = image->raw_metadata;
 
        if (strncmp (ptr, "BSJB", 4) == 0){
@@ -1391,24 +1397,43 @@ free_hash (GHashTable *hash)
                g_hash_table_destroy (hash);
 }
 
-/**
- * mono_image_close:
- * @image: The image file we wish to close
- *
- * Closes an image file, deallocates all memory consumed and
- * unmaps all possible sections of the file
+/*
+ * Returns whether mono_image_close_finish() must be called as well.
+ * We must unload images in two steps because clearing the domain in
+ * SGen requires the class metadata to be intact, but we need to free
+ * the mono_g_hash_tables in case a collection occurs during domain
+ * unloading and the roots would trip up the GC.
  */
-void
-mono_image_close (MonoImage *image)
+gboolean
+mono_image_close_except_pools (MonoImage *image)
 {
        MonoImage *image2;
        GHashTable *loaded_images;
        int i;
 
-       g_return_if_fail (image != NULL);
+       g_return_val_if_fail (image != NULL, FALSE);
 
-       if (InterlockedDecrement (&image->ref_count) > 0)
-               return;
+       /* 
+        * Atomically decrement the refcount and remove ourselves from the hash tables, so
+        * register_image () can't grab an image which is being closed.
+        */
+       mono_images_lock ();
+
+       if (InterlockedDecrement (&image->ref_count) > 0) {
+               mono_images_unlock ();
+               return FALSE;
+       }
+
+       loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
+       image2 = g_hash_table_lookup (loaded_images, image->name);
+       if (image == image2) {
+               /* This is not true if we are called from mono_image_open () */
+               g_hash_table_remove (loaded_images, image->name);
+       }
+       if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == image))
+               g_hash_table_remove (loaded_images, (char *) image->assembly_name);     
+
+       mono_images_unlock ();
 
 #ifdef PLATFORM_WIN32
        if (image->is_module_handle && image->has_entry_point) {
@@ -1417,7 +1442,7 @@ mono_image_close (MonoImage *image)
                        /* Image will be closed by _CorDllMain. */
                        FreeLibrary ((HMODULE) image->raw_data);
                        mono_images_unlock ();
-                       return;
+                       return FALSE;
                }
                mono_images_unlock ();
        }
@@ -1439,30 +1464,24 @@ mono_image_close (MonoImage *image)
                int i;
 
                for (i = 0; i < t->rows; i++) {
-                       if (image->references [i])
-                               mono_assembly_close (image->references [i]);
+                       if (image->references [i]) {
+                               if (!mono_assembly_close_except_image_pools (image->references [i]))
+                                       image->references [i] = NULL;
+                       }
+               }
+       } else {
+               if (image->references) {
+                       g_free (image->references);
+                       image->references = NULL;
                }
-
-               g_free (image->references);
-               image->references = NULL;
-       }
-
-       mono_images_lock ();
-       loaded_images = image->ref_only ? loaded_images_refonly_hash : loaded_images_hash;
-       image2 = g_hash_table_lookup (loaded_images, image->name);
-       if (image == image2) {
-               /* This is not true if we are called from mono_image_open () */
-               g_hash_table_remove (loaded_images, image->name);
        }
-       if (image->assembly_name && (g_hash_table_lookup (loaded_images, image->assembly_name) == image))
-               g_hash_table_remove (loaded_images, (char *) image->assembly_name);     
 
 #ifdef PLATFORM_WIN32
+       mono_images_lock ();
        if (image->is_module_handle && !image->has_entry_point)
                FreeLibrary ((HMODULE) image->raw_data);
-#endif
-
        mono_images_unlock ();
+#endif
 
        if (image->raw_buffer_used) {
                if (image->raw_data != NULL)
@@ -1555,6 +1574,8 @@ mono_image_close (MonoImage *image)
        if (image->property_hash)
                mono_property_hash_destroy (image->property_hash);
 
+       g_slist_free (image->reflection_info_unregister_classes);
+
        if (image->interface_bitset) {
                mono_unload_interface_ids (image->interface_bitset);
                mono_bitset_free (image->interface_bitset);
@@ -1570,21 +1591,56 @@ mono_image_close (MonoImage *image)
        }
 
        for (i = 0; i < image->module_count; ++i) {
-               if (image->modules [i])
-                       mono_image_close (image->modules [i]);
+               if (image->modules [i]) {
+                       if (!mono_image_close_except_pools (image->modules [i]))
+                               image->modules [i] = NULL;
+               }
        }
-       if (image->modules)
-               g_free (image->modules);
        if (image->modules_loaded)
                g_free (image->modules_loaded);
-       if (image->references)
-               g_free (image->references);
-       mono_perfcounters->loader_bytes -= mono_mempool_get_allocated (image->mempool);
 
        DeleteCriticalSection (&image->szarray_cache_lock);
        DeleteCriticalSection (&image->lock);
 
        /*g_print ("destroy image %p (dynamic: %d)\n", image, image->dynamic);*/
+       if (image->dynamic) {
+               /* Dynamic images are GC_MALLOCed */
+               g_free ((char*)image->module_name);
+               mono_dynamic_image_free ((MonoDynamicImage*)image);
+       }
+
+       mono_profiler_module_event (image, MONO_PROFILE_END_UNLOAD);
+
+       return TRUE;
+}
+
+void
+mono_image_close_finish (MonoImage *image)
+{
+       int i;
+
+       if (image->references && !image->dynamic) {
+               MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
+               int i;
+
+               for (i = 0; i < t->rows; i++) {
+                       if (image->references [i])
+                               mono_assembly_close_finish (image->references [i]);
+               }
+
+               g_free (image->references);
+               image->references = NULL;
+       }
+
+       for (i = 0; i < image->module_count; ++i) {
+               if (image->modules [i])
+                       mono_image_close_finish (image->modules [i]);
+       }
+       if (image->modules)
+               g_free (image->modules);
+
+       mono_perfcounters->loader_bytes -= mono_mempool_get_allocated (image->mempool);
+
        if (!image->dynamic) {
                if (debug_assembly_unload)
                        mono_mempool_invalidate (image->mempool);
@@ -1593,16 +1649,25 @@ mono_image_close (MonoImage *image)
                        g_free (image);
                }
        } else {
-               /* Dynamic images are GC_MALLOCed */
-               g_free ((char*)image->module_name);
-               mono_dynamic_image_free ((MonoDynamicImage*)image);
                if (debug_assembly_unload)
                        mono_mempool_invalidate (image->mempool);
                else
                        mono_mempool_destroy (image->mempool);
        }
+}
 
-       mono_profiler_module_event (image, MONO_PROFILE_END_UNLOAD);
+/**
+ * mono_image_close:
+ * @image: The image file we wish to close
+ *
+ * Closes an image file, deallocates all memory consumed and
+ * unmaps all possible sections of the file
+ */
+void
+mono_image_close (MonoImage *image)
+{
+       if (mono_image_close_except_pools (image))
+               mono_image_close_finish (image);
 }
 
 /**