2009-05-26 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mono / metadata / image.c
index be83f5e16d2243b0928b8d5cefe13184c76367df..da552e6fa65c971a1674302873f03a585e4633f8 100644 (file)
@@ -34,6 +34,8 @@
 #include <mono/metadata/class-internals.h>
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/object-internals.h>
+#include <mono/metadata/security-core-clr.h>
+#include <mono/metadata/verify-internals.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #ifdef HAVE_UNISTD_H
@@ -128,7 +130,7 @@ mono_images_init (void)
        loaded_images_hash = g_hash_table_new (g_str_hash, g_str_equal);
        loaded_images_refonly_hash = g_hash_table_new (g_str_hash, g_str_equal);
 
-       debug_assembly_unload = getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
+       debug_assembly_unload = g_getenv ("MONO_DEBUG_ASSEMBLY_UNLOAD") != NULL;
 }
 
 /**
@@ -333,9 +335,9 @@ load_metadata_ptrs (MonoImage *image, MonoCLIImageInfo *iinfo)
 
                ptr += 4;
                image->md_version_major = read16 (ptr);
-               ptr += 4;
+               ptr += 2;
                image->md_version_minor = read16 (ptr);
-               ptr += 4;
+               ptr += 6;
 
                version_string_len = read32 (ptr);
                ptr += 4;
@@ -615,6 +617,7 @@ mono_image_init (MonoImage *image)
 
        image->property_hash = mono_property_hash_new ();
        InitializeCriticalSection (&image->lock);
+       InitializeCriticalSection (&image->szarray_cache_lock);
 }
 
 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
@@ -766,24 +769,16 @@ do_load_header (MonoImage *image, MonoDotNetHeader *header, int offset)
        return offset;
 }
 
-static MonoImage *
-do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
-                   gboolean care_about_cli)
+gboolean
+mono_image_load_pe_data (MonoImage *image)
 {
        MonoCLIImageInfo *iinfo;
        MonoDotNetHeader *header;
        MonoMSDOSHeader msdos;
        gint32 offset = 0;
 
-       mono_profiler_module_event (image, MONO_PROFILE_START_LOAD);
-
-       mono_image_init (image);
-
        iinfo = image->image_info;
        header = &iinfo->cli_header;
-               
-       if (status)
-               *status = MONO_IMAGE_IMAGE_INVALID;
 
 #ifdef PLATFORM_WIN32
        if (!image->is_module_handle)
@@ -832,16 +827,66 @@ do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
        
        if (!load_section_tables (image, iinfo, offset))
                goto invalid_image;
+
+       return TRUE;
+
+invalid_image:
+       return FALSE;
+}
+
+gboolean
+mono_image_load_cli_data (MonoImage *image)
+{
+       MonoCLIImageInfo *iinfo;
+       MonoDotNetHeader *header;
+
+       iinfo = image->image_info;
+       header = &iinfo->cli_header;
+
+       /* Load the CLI header */
+       if (!load_cli_header (image, iinfo))
+               return FALSE;
+
+       if (!load_metadata (image, iinfo))
+               return FALSE;
+
+       return TRUE;
+}
+
+static MonoImage *
+do_mono_image_load (MonoImage *image, MonoImageOpenStatus *status,
+                   gboolean care_about_cli, gboolean care_about_pecoff)
+{
+       MonoCLIImageInfo *iinfo;
+       MonoDotNetHeader *header;
+
+       mono_profiler_module_event (image, MONO_PROFILE_START_LOAD);
+
+       mono_image_init (image);
+
+       iinfo = image->image_info;
+       header = &iinfo->cli_header;
+               
+       if (status)
+               *status = MONO_IMAGE_IMAGE_INVALID;
+
+       if (care_about_pecoff == FALSE)
+               goto done;
+
+       if (!mono_verifier_verify_pe_data (image, NULL))
+               goto invalid_image;
+
+       if (!mono_image_load_pe_data (image))
+               goto invalid_image;
        
        if (care_about_cli == FALSE) {
                goto done;
        }
-       
-       /* Load the CLI header */
-       if (!load_cli_header (image, iinfo))
+
+       if (!mono_verifier_verify_cli_data (image, NULL))
                goto invalid_image;
 
-       if (!load_metadata (image, iinfo))
+       if (!mono_image_load_cli_data (image))
                goto invalid_image;
 
        /* modules don't have an assembly table row */
@@ -872,7 +917,7 @@ invalid_image:
 
 static MonoImage *
 do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
-                   gboolean care_about_cli, gboolean refonly)
+                   gboolean care_about_cli, gboolean care_about_pecoff, gboolean refonly)
 {
        MonoCLIImageInfo *iinfo;
        MonoImage *image;
@@ -910,9 +955,11 @@ do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
        image->name = mono_path_resolve_symlinks (fname);
        image->ref_only = refonly;
        image->ref_count = 1;
+       /* if MONO_SECURITY_MODE_CORE_CLR is set then determine if this image is platform code */
+       image->core_clr_platform_code = mono_security_core_clr_determine_platform_image (image);
 
        mono_file_map_close (filed);
-       return do_mono_image_load (image, status, care_about_cli);
+       return do_mono_image_load (image, status, care_about_cli, care_about_pecoff);
 }
 
 MonoImage *
@@ -1035,7 +1082,7 @@ mono_image_open_from_data_full (char *data, guint32 data_len, gboolean need_copy
        image->image_info = iinfo;
        image->ref_only = refonly;
 
-       image = do_mono_image_load (image, status, TRUE);
+       image = do_mono_image_load (image, status, TRUE, TRUE);
        if (image == NULL)
                return NULL;
 
@@ -1065,7 +1112,7 @@ mono_image_open_from_module_handle (HMODULE module_handle, char* fname, gboolean
        image->ref_count = has_entry_point ? 0 : 1;
        image->has_entry_point = has_entry_point;
 
-       image = do_mono_image_load (image, status, TRUE);
+       image = do_mono_image_load (image, status, TRUE, TRUE);
        if (image == NULL)
                return NULL;
 
@@ -1168,7 +1215,7 @@ mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean r
        }
        mono_images_unlock ();
 
-       image = do_mono_image_open (fname, status, TRUE, refonly);
+       image = do_mono_image_open (fname, status, TRUE, TRUE, refonly);
        if (image == NULL)
                return NULL;
 
@@ -1207,7 +1254,24 @@ mono_pe_file_open (const char *fname, MonoImageOpenStatus *status)
 {
        g_return_val_if_fail (fname != NULL, NULL);
        
-       return(do_mono_image_open (fname, status, FALSE, FALSE));
+       return(do_mono_image_open (fname, status, FALSE, TRUE, FALSE));
+}
+
+/**
+ * mono_image_open_raw
+ * @fname: filename that points to the module we want to open
+ * @status: An error condition is returned in this field
+ * 
+ * Returns an image without loading neither pe or cli data.
+ * 
+ * Use mono_image_load_pe_data and mono_image_load_cli_data to load them.  
+ */
+MonoImage *
+mono_image_open_raw (const char *fname, MonoImageOpenStatus *status)
+{
+       g_return_val_if_fail (fname != NULL, NULL);
+       
+       return(do_mono_image_open (fname, status, FALSE, FALSE, FALSE));
 }
 
 void
@@ -1244,12 +1308,12 @@ mono_image_fixup_vtable (MonoImage *image)
                if (slot_type & VTFIXUP_TYPE_32BIT)
                        while (slot_count--) {
                                *((guint32*) slot) = (guint32) mono_marshal_get_vtfixup_ftnptr (image, *((guint32*) slot), slot_type);
-                               ((guint32*) slot)++;
+                               slot = ((guint32*) slot) + 1;
                        }
                else if (slot_type & VTFIXUP_TYPE_64BIT)
                        while (slot_count--) {
                                *((guint64*) slot) = (guint64) mono_marshal_get_vtfixup_ftnptr (image, *((guint64*) slot), slot_type);
-                               ((guint64*) slot)++;
+                               slot = ((guint32*) slot) + 1;
                        }
                else
                        g_assert_not_reached();
@@ -1432,6 +1496,8 @@ mono_image_close (MonoImage *image)
                g_hash_table_foreach (image->array_cache, free_array_cache_entry, NULL);
                g_hash_table_destroy (image->array_cache);
        }
+       if (image->szarray_cache)
+               g_hash_table_destroy (image->szarray_cache);
        if (image->ptr_cache)
                g_hash_table_destroy (image->ptr_cache);
        if (image->name_cache) {
@@ -1450,6 +1516,7 @@ mono_image_close (MonoImage *image)
        free_hash (image->remoting_invoke_cache);
        free_hash (image->runtime_invoke_cache);
        free_hash (image->runtime_invoke_direct_cache);
+       free_hash (image->runtime_invoke_vcall_cache);
        free_hash (image->synchronized_cache);
        free_hash (image->unbox_wrapper_cache);
        free_hash (image->cominterop_invoke_cache);
@@ -1505,6 +1572,10 @@ mono_image_close (MonoImage *image)
        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) {
                if (debug_assembly_unload)
@@ -1520,8 +1591,6 @@ mono_image_close (MonoImage *image)
                mono_mempool_destroy (image->mempool);
        }
 
-       DeleteCriticalSection (&image->lock);
-
        mono_profiler_module_event (image, MONO_PROFILE_END_UNLOAD);
 }
 
@@ -1573,11 +1642,11 @@ mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
        dir_offset = MONO_PE_RES_DIR_ENTRY_DIR_OFFSET (*entry);
 
        if(level==0) {
-               if((is_string==FALSE && name_offset!=res_id) ||
-                  (is_string==TRUE)) {
-                       return(NULL);
-               }
+               if (is_string)
+                       return NULL;
        } else if (level==1) {
+               if (res_id != name_offset)
+                       return NULL;
 #if 0
                if(name!=NULL &&
                   is_string==TRUE && name!=lookup (name_offset)) {
@@ -1585,12 +1654,8 @@ mono_image_walk_resource_tree (MonoCLIImageInfo *info, guint32 res_id,
                }
 #endif
        } else if (level==2) {
-               if ((is_string == FALSE &&
-                   name_offset != lang_id &&
-                   lang_id != 0) ||
-                  (is_string == TRUE)) {
-                       return(NULL);
-               }
+               if (is_string == TRUE || (is_string == FALSE && lang_id != 0 && name_offset != lang_id))
+                       return NULL;
        } else {
                g_assert_not_reached ();
        }
@@ -1657,6 +1722,8 @@ mono_image_lookup_resource (MonoImage *image, guint32 res_id, guint32 lang_id, g
                return(NULL);
        }
 
+       mono_image_ensure_section_idx (image, MONO_SECTION_RSRC);
+
        info=image->image_info;
        if(info==NULL) {
                return(NULL);
@@ -2059,11 +2126,61 @@ g_slist_append_image (MonoImage *image, GSList *list, gpointer data)
 void
 mono_image_lock (MonoImage *image)
 {
-       EnterCriticalSection (&image->lock);
+       mono_locks_acquire (&image->lock, ImageDataLock);
 }
 
 void
 mono_image_unlock (MonoImage *image)
 {
-       LeaveCriticalSection (&image->lock);
+       mono_locks_release (&image->lock, ImageDataLock);
+}
+
+
+/**
+ * mono_image_property_lookup:
+ *
+ * Lookup a property on @image. Used to store very rare fields of MonoClass and MonoMethod.
+ *
+ * LOCKING: Takes the image lock
+ */
+gpointer 
+mono_image_property_lookup (MonoImage *image, gpointer subject, guint32 property)
+{
+       gpointer res;
+
+       mono_image_lock (image);
+       res = mono_property_hash_lookup (image->property_hash, subject, property);
+       mono_image_unlock (image);
+
+       return res;
+}
+
+/**
+ * mono_image_property_insert:
+ *
+ * Insert a new property @property with value @value on @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
+ *
+ * LOCKING: Takes the image lock
+ */
+void
+mono_image_property_insert (MonoImage *image, gpointer subject, guint32 property, gpointer value)
+{
+       mono_image_lock (image);
+       mono_property_hash_insert (image->property_hash, subject, property, value);
+       mono_image_unlock (image);
+}
+
+/**
+ * mono_image_property_remove:
+ *
+ * Remove all properties associated with @subject in @image. Used to store very rare fields of MonoClass and MonoMethod.
+ *
+ * LOCKING: Takes the image lock
+ */
+void
+mono_image_property_remove (MonoImage *image, gpointer subject)
+{
+       mono_image_lock (image);
+       mono_property_hash_remove_object (image->property_hash, subject);
+       mono_image_unlock (image);
 }