Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / metadata / assembly.c
index 5bc9486f897e85703bbf689875c78867e671f786..684a401c95919b0ccce2a70159eb022884260ffb 100644 (file)
@@ -50,7 +50,7 @@
 #include <sys/stat.h>
 #endif
 
-#ifdef PLATFORM_MACOSX
+#ifdef HOST_DARWIN
 #include <mach-o/dyld.h>
 #endif
 
@@ -332,27 +332,6 @@ static const AssemblyVersionMap framework_assemblies [] = {
 static GList *loaded_assemblies = NULL;
 static MonoAssembly *corlib;
 
-#if defined(__native_client__)
-
-/* On Native Client, allow mscorlib to be loaded from memory  */
-/* instead of loaded off disk.  If these are not set, default */
-/* mscorlib loading will take place                           */
-
-/* NOTE: If mscorlib data is passed to mono in this way then */
-/* it needs to remain allocated during the use of mono.      */
-
-static void *corlibData = NULL;
-static size_t corlibSize = 0;
-
-void
-mono_set_corlib_data (void *data, size_t size)
-{
-  corlibData = data;
-  corlibSize = size;
-}
-
-#endif
-
 static char* unquote (const char *str);
 
 /* This protects loaded_assemblies and image->references */
@@ -462,12 +441,6 @@ mono_set_assemblies_path (const char* path)
        }
 }
 
-/* Native Client can't get this info from an environment variable so */
-/* it's passed in to the runtime, or set manually by embedding code. */
-#ifdef __native_client__
-char* nacl_mono_path = NULL;
-#endif
-
 static void
 check_path_env (void)
 {
@@ -475,10 +448,6 @@ check_path_env (void)
                return;
 
        char* path = g_getenv ("MONO_PATH");
-#ifdef __native_client__
-       if (!path)
-               path = strdup (nacl_mono_path);
-#endif
        if (!path)
                return;
 
@@ -871,7 +840,7 @@ set_dirs (char *exe)
 void
 mono_set_rootdir (void)
 {
-#if defined(HOST_WIN32) || (defined(PLATFORM_MACOSX) && !defined(TARGET_ARM))
+#if defined(HOST_WIN32) || (defined(HOST_DARWIN) && !defined(TARGET_ARM))
        gchar *bindir, *installdir, *root, *name, *resolvedname, *config;
 
 #ifdef HOST_WIN32
@@ -1980,11 +1949,24 @@ mono_assembly_open_predicate (const char *filename, gboolean refonly,
        }
 
        if (image->assembly) {
-               /* Already loaded by another appdomain */
-               mono_assembly_invoke_load_hook (image->assembly);
-               mono_image_close (image);
-               g_free (fname);
-               return image->assembly;
+               /* We want to return the MonoAssembly that's already loaded,
+                * but if we're using the strict assembly loader, we also need
+                * to check that the previously loaded assembly matches the
+                * predicate.  It could be that we previously loaded a
+                * different version that happens to have the filename that
+                * we're currently probing. */
+               if (mono_loader_get_strict_strong_names () &&
+                   predicate && !predicate (image->assembly, user_data)) {
+                       mono_image_close (image);
+                       g_free (fname);
+                       return NULL;
+               } else {
+                       /* Already loaded by another appdomain */
+                       mono_assembly_invoke_load_hook (image->assembly);
+                       mono_image_close (image);
+                       g_free (fname);
+                       return image->assembly;
+               }
        }
 
        ass = mono_assembly_load_from_predicate (image, fname, refonly, predicate, user_data, status);
@@ -2126,6 +2108,10 @@ has_reference_assembly_attribute_iterator (MonoImage *image, guint32 typeref_sco
 gboolean
 mono_assembly_has_reference_assembly_attribute (MonoAssembly *assembly, MonoError *error)
 {
+       g_assert (assembly && assembly->image);
+       /* .NET Framework appears to ignore the attribute on dynamic
+        * assemblies, so don't call this function for dynamic assemblies. */
+       g_assert (!image_is_dynamic (assembly->image));
        error_init (error);
 
        /*
@@ -2236,7 +2222,7 @@ mono_assembly_load_from_predicate (MonoImage *image, const char *fname,
        ass->ref_only = refonly;
        ass->image = image;
 
-       mono_profiler_assembly_event (ass, MONO_PROFILE_START_LOAD);
+       MONO_PROFILER_RAISE (assembly_loading, (ass));
 
        mono_assembly_fill_assembly_name (image, &ass->aname);
 
@@ -2328,7 +2314,7 @@ mono_assembly_load_from_predicate (MonoImage *image, const char *fname,
 
        mono_assembly_invoke_load_hook (ass);
 
-       mono_profiler_assembly_loaded (ass, MONO_PROFILE_OK);
+       MONO_PROFILER_RAISE (assembly_loaded, (ass));
        
        return ass;
 }
@@ -2384,19 +2370,20 @@ static gboolean
 parse_public_key (const gchar *key, gchar** pubkey, gboolean *is_ecma)
 {
        const gchar *pkey;
-       gchar header [16], val, *arr;
+       gchar header [16], val, *arr, *endp;
        gint i, j, offset, bitlen, keylen, pkeylen;
-       
+
+       //both pubkey and is_ecma are required arguments
+       g_assert (pubkey && is_ecma);
+
        keylen = strlen (key) >> 1;
        if (keylen < 1)
                return FALSE;
 
        /* allow the ECMA standard key */
        if (strcmp (key, "00000000000000000400000000000000") == 0) {
-               if (pubkey) {
-                       *pubkey = g_strdup (key);
-                       *is_ecma = TRUE;
-               }
+               *pubkey = NULL;
+               *is_ecma = TRUE;
                return TRUE;
        }
        *is_ecma = FALSE;
@@ -2441,21 +2428,11 @@ parse_public_key (const gchar *key, gchar** pubkey, gboolean *is_ecma)
        bitlen = read32 (header + 12) >> 3;
        if ((bitlen + 16 + 4) != pkeylen)
                return FALSE;
-
-       /* parsing is OK and the public key itself is not requested back */
-       if (!pubkey)
-               return TRUE;
                
+       arr = (gchar *)g_malloc (keylen + 4);
        /* Encode the size of the blob */
-       offset = 0;
-       if (keylen <= 127) {
-               arr = (gchar *)g_malloc (keylen + 1);
-               arr [offset++] = keylen;
-       } else {
-               arr = (gchar *)g_malloc (keylen + 2);
-               arr [offset++] = 0x80; /* 10bs */
-               arr [offset++] = keylen;
-       }
+       mono_metadata_encode_value (keylen, &arr[0], &endp);
+       offset = (gint)(endp-arr);
                
        for (i = offset, j = 0; i < keylen + offset; i++) {
                arr [i] = g_ascii_xdigit_value (key [j++]) << 4;
@@ -2473,7 +2450,7 @@ build_assembly_name (const char *name, const char *version, const char *culture,
        gint major, minor, build, revision;
        gint len;
        gint version_parts;
-       gchar *pkey, *pkeyptr, *encoded, tok [8];
+       gchar *pkeyptr, *encoded, tok [8];
 
        memset (aname, 0, sizeof (MonoAssemblyName));
 
@@ -2522,17 +2499,16 @@ build_assembly_name (const char *name, const char *version, const char *culture,
        }
 
        if (key) {
-               gboolean is_ecma;
+               gboolean is_ecma = FALSE;
+               gchar *pkey = NULL;
                if (strcmp (key, "null") == 0 || !parse_public_key (key, &pkey, &is_ecma)) {
                        mono_assembly_name_free (aname);
                        return FALSE;
                }
 
                if (is_ecma) {
-                       if (save_public_key)
-                               aname->public_key = (guint8*)pkey;
-                       else
-                               g_free (pkey);
+                       g_assert (pkey == NULL);
+                       aname->public_key = NULL;
                        g_strlcpy ((gchar*)aname->public_key_token, "b77a5c561934e089", MONO_PUBLIC_KEY_TOKEN_LENGTH);
                        return TRUE;
                }
@@ -3492,23 +3468,6 @@ mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *
                return corlib;
        }
 
-       // In native client, Corlib is embedded in the executable as static variable corlibData
-#if defined(__native_client__)
-       if (corlibData != NULL && corlibSize != 0) {
-               int status = 0;
-               /* First "FALSE" instructs mono not to make a copy. */
-               /* Second "FALSE" says this is not just a ref.      */
-               MonoImage* image = mono_image_open_from_data_full (corlibData, corlibSize, FALSE, &status, FALSE);
-               if (image == NULL || status != 0)
-                       g_print("mono_image_open_from_data_full failed: %d\n", status);
-               corlib = mono_assembly_load_from_full (image, "mscorlib", &status, FALSE);
-               if (corlib == NULL || status != 0)
-                       g_print ("mono_assembly_load_from_full failed: %d\n", status);
-               if (corlib)
-                       return corlib;
-       }
-#endif
-
        // A nonstandard preload hook may provide a special mscorlib assembly
        aname = mono_assembly_name_new ("mscorlib.dll");
        corlib = invoke_assembly_preload_hook (aname, assemblies_path);
@@ -3548,8 +3507,11 @@ prevent_reference_assembly_from_running (MonoAssembly* candidate, gboolean refon
 {
        MonoError refasm_error;
        error_init (&refasm_error);
-       if (candidate && !refonly && mono_assembly_has_reference_assembly_attribute (candidate, &refasm_error)) {
-               candidate = NULL;
+       if (candidate && !refonly) {
+               /* .NET Framework seems to not check for ReferenceAssemblyAttribute on dynamic assemblies */
+               if (!image_is_dynamic (candidate->image) &&
+                   mono_assembly_has_reference_assembly_attribute (candidate, &refasm_error))
+                       candidate = NULL;
        }
        mono_error_cleanup (&refasm_error);
        return candidate;
@@ -3832,7 +3794,7 @@ mono_assembly_close_except_image_pools (MonoAssembly *assembly)
        if (InterlockedDecrement (&assembly->ref_count) > 0)
                return FALSE;
 
-       mono_profiler_assembly_event (assembly, MONO_PROFILE_START_UNLOAD);
+       MONO_PROFILER_RAISE (assembly_unloading, (assembly));
 
        mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading assembly %s [%p].", assembly->aname.name, assembly);
 
@@ -3855,7 +3817,7 @@ mono_assembly_close_except_image_pools (MonoAssembly *assembly)
        g_slist_free (assembly->friend_assembly_names);
        g_free (assembly->basedir);
 
-       mono_profiler_assembly_event (assembly, MONO_PROFILE_END_UNLOAD);
+       MONO_PROFILER_RAISE (assembly_unloaded, (assembly));
 
        return TRUE;
 }