2 * assembly.c: Routines for loading assemblies.
5 * Miguel de Icaza (miguel@ximian.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
19 #include "object-internals.h"
20 #include <mono/metadata/loader.h>
21 #include <mono/metadata/tabledefs.h>
22 #include <mono/metadata/metadata-internals.h>
23 #include <mono/metadata/profiler-private.h>
24 #include <mono/metadata/class-internals.h>
25 #include <mono/metadata/domain-internals.h>
26 #include <mono/metadata/reflection-internals.h>
27 #include <mono/metadata/mono-endian.h>
28 #include <mono/metadata/mono-debug.h>
29 #include <mono/io-layer/io-layer.h>
30 #include <mono/utils/mono-uri.h>
31 #include <mono/metadata/mono-config.h>
32 #include <mono/metadata/mono-config-dirs.h>
33 #include <mono/utils/mono-digest.h>
34 #include <mono/utils/mono-logger-internals.h>
35 #include <mono/utils/mono-path.h>
36 #include <mono/metadata/reflection.h>
37 #include <mono/metadata/coree.h>
38 #include <mono/metadata/cil-coff.h>
39 #include <mono/utils/mono-io-portability.h>
40 #include <mono/utils/atomic.h>
41 #include <mono/utils/mono-os-mutex.h>
44 #include <sys/types.h>
49 #ifdef PLATFORM_MACOSX
50 #include <mach-o/dyld.h>
53 /* AssemblyVersionMap: an assembly name and the assembly version set on which it is based */
55 const char* assembly_name;
56 guint8 version_set_index;
59 /* the default search path is empty, the first slot is replaced with the computed value */
67 /* Contains the list of directories to be searched for assemblies (MONO_PATH) */
68 static char **assemblies_path = NULL;
70 /* Contains the list of directories that point to auxiliary GACs */
71 static char **extra_gac_paths = NULL;
73 #ifndef DISABLE_ASSEMBLY_REMAPPING
74 /* The list of system assemblies what will be remapped to the running
75 * runtime version. WARNING: this list must be sorted.
76 * The integer number is an index in the MonoRuntimeInfo structure, whose
77 * values can be found in domain.c - supported_runtimes. Look there
78 * to understand what remapping will be made.
80 * .NET version can be found at https://github.com/dotnet/coreclr/blob/master/src/inc/fxretarget.h#L99
83 static const AssemblyVersionMap framework_assemblies [] = {
85 {"Commons.Xml.Relaxng", 0},
92 {"Microsoft.Build.Engine", 2},
93 {"Microsoft.Build.Framework", 2},
94 {"Microsoft.VisualBasic", 1},
95 {"Microsoft.VisualC", 1},
97 {"Mono.CompilerServices.SymbolWriter", 0},
99 {"Mono.Data.SybaseClient", 0},
100 {"Mono.Data.Tds", 0},
101 {"Mono.Data.TdsClient", 0},
102 {"Mono.GetOptions", 0},
105 {"Mono.Security", 0},
106 {"Mono.Security.Win32", 0},
108 {"Novell.Directory.Ldap", 0},
111 {"System.ComponentModel.Composition", 2},
112 {"System.ComponentModel.DataAnnotations", 2},
113 {"System.Configuration", 0},
114 {"System.Configuration.Install", 0},
117 {"System.Data.Linq", 2},
118 {"System.Data.OracleClient", 0},
119 {"System.Data.Services", 2},
120 {"System.Data.Services.Client", 2},
121 {"System.Data.SqlXml", 0},
122 {"System.Design", 0},
123 {"System.DirectoryServices", 0},
124 {"System.Drawing", 0},
125 {"System.Drawing.Design", 0},
126 {"System.EnterpriseServices", 0},
127 {"System.IdentityModel", 3},
128 {"System.IdentityModel.Selectors", 3},
129 {"System.Management", 0},
130 {"System.Messaging", 0},
132 {"System.Runtime.Remoting", 0},
133 {"System.Runtime.Serialization", 3},
134 {"System.Runtime.Serialization.Formatters.Soap", 0},
135 {"System.Security", 0},
136 {"System.ServiceModel", 3},
137 {"System.ServiceModel.Web", 2},
138 {"System.ServiceProcess", 0},
139 {"System.Transactions", 0},
141 {"System.Web.Abstractions", 2},
142 {"System.Web.DynamicData", 2},
143 {"System.Web.Extensions", 2},
144 {"System.Web.Mobile", 0},
145 {"System.Web.Routing", 2},
146 {"System.Web.Services", 0},
147 {"System.Windows.Forms", 0},
149 {"System.Xml.Linq", 2},
156 * keeps track of loaded assemblies
158 static GList *loaded_assemblies = NULL;
159 static MonoAssembly *corlib;
161 #if defined(__native_client__)
163 /* On Native Client, allow mscorlib to be loaded from memory */
164 /* instead of loaded off disk. If these are not set, default */
165 /* mscorlib loading will take place */
167 /* NOTE: If mscorlib data is passed to mono in this way then */
168 /* it needs to remain allocated during the use of mono. */
170 static void *corlibData = NULL;
171 static size_t corlibSize = 0;
174 mono_set_corlib_data (void *data, size_t size)
182 static char* unquote (const char *str);
184 /* This protects loaded_assemblies and image->references */
185 #define mono_assemblies_lock() mono_os_mutex_lock (&assemblies_mutex)
186 #define mono_assemblies_unlock() mono_os_mutex_unlock (&assemblies_mutex)
187 static mono_mutex_t assemblies_mutex;
189 /* If defined, points to the bundled assembly information */
190 const MonoBundledAssembly **bundles;
192 static mono_mutex_t assembly_binding_mutex;
194 /* Loaded assembly binding info */
195 static GSList *loaded_assembly_bindings = NULL;
197 /* Class lazy loading functions */
198 static GENERATE_TRY_GET_CLASS_WITH_CACHE (internals_visible, System.Runtime.CompilerServices, InternalsVisibleToAttribute)
201 mono_assembly_invoke_search_hook_internal (MonoAssemblyName *aname, MonoAssembly *requesting, gboolean refonly, gboolean postload);
203 mono_assembly_load_full_internal (MonoAssemblyName *aname, MonoAssembly *requesting, const char *basedir, MonoImageOpenStatus *status, gboolean refonly);
205 mono_assembly_is_in_gac (const gchar *filanem);
208 encode_public_tok (const guchar *token, gint32 len)
210 const static gchar allowed [] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
214 res = (gchar *)g_malloc (len * 2 + 1);
215 for (i = 0; i < len; i++) {
216 res [i * 2] = allowed [token [i] >> 4];
217 res [i * 2 + 1] = allowed [token [i] & 0xF];
224 * mono_public_tokens_are_equal:
225 * @pubt1: first public key token
226 * @pubt2: second public key token
228 * Compare two public key tokens and return #TRUE is they are equal and #FALSE
232 mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2)
234 return memcmp (pubt1, pubt2, 16) == 0;
238 * mono_set_assemblies_path:
239 * @path: list of paths that contain directories where Mono will look for assemblies
241 * Use this method to override the standard assembly lookup system and
242 * override any assemblies coming from the GAC. This is the method
243 * that supports the MONO_PATH variable.
245 * Notice that MONO_PATH and this method are really a very bad idea as
246 * it prevents the GAC from working and it prevents the standard
247 * resolution mechanisms from working. Nonetheless, for some debugging
248 * situations and bootstrapping setups, this is useful to have.
251 mono_set_assemblies_path (const char* path)
253 char **splitted, **dest;
255 splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
257 g_strfreev (assemblies_path);
258 assemblies_path = dest = splitted;
260 char *tmp = *splitted;
262 *dest++ = mono_path_canonicalize (tmp);
268 if (g_getenv ("MONO_DEBUG") == NULL)
271 splitted = assemblies_path;
273 if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
274 g_warning ("'%s' in MONO_PATH doesn't exist or has wrong permissions.", *splitted);
280 /* Native Client can't get this info from an environment variable so */
281 /* it's passed in to the runtime, or set manually by embedding code. */
282 #ifdef __native_client__
283 char* nacl_mono_path = NULL;
287 check_path_env (void)
290 path = g_getenv ("MONO_PATH");
291 #ifdef __native_client__
293 path = nacl_mono_path;
295 if (!path || assemblies_path != NULL)
298 mono_set_assemblies_path(path);
302 check_extra_gac_path_env (void) {
304 char **splitted, **dest;
306 path = g_getenv ("MONO_GAC_PREFIX");
310 splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
312 g_strfreev (extra_gac_paths);
313 extra_gac_paths = dest = splitted;
321 if (g_getenv ("MONO_DEBUG") == NULL)
325 if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
326 g_warning ("'%s' in MONO_GAC_PREFIX doesn't exist or has wrong permissions.", *splitted);
333 assembly_binding_maps_name (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname)
335 if (!info || !info->name)
338 if (strcmp (info->name, aname->name))
341 if (info->major != aname->major || info->minor != aname->minor)
344 if ((info->culture != NULL && info->culture [0]) != (aname->culture != NULL && aname->culture [0]))
347 if (info->culture && aname->culture && strcmp (info->culture, aname->culture))
350 if (!mono_public_tokens_are_equal (info->public_key_token, aname->public_key_token))
357 mono_assembly_binding_info_free (MonoAssemblyBindingInfo *info)
363 g_free (info->culture);
367 get_publisher_policy_info (MonoImage *image, MonoAssemblyName *aname, MonoAssemblyBindingInfo *binding_info)
370 guint32 cols [MONO_MANIFEST_SIZE];
371 const gchar *filename;
372 gchar *subpath, *fullpath;
374 t = &image->tables [MONO_TABLE_MANIFESTRESOURCE];
375 /* MS Impl. accepts policy assemblies with more than
376 * one manifest resource, and only takes the first one */
378 binding_info->is_valid = FALSE;
382 mono_metadata_decode_row (t, 0, cols, MONO_MANIFEST_SIZE);
383 if ((cols [MONO_MANIFEST_IMPLEMENTATION] & MONO_IMPLEMENTATION_MASK) != MONO_IMPLEMENTATION_FILE) {
384 binding_info->is_valid = FALSE;
388 filename = mono_metadata_string_heap (image, cols [MONO_MANIFEST_NAME]);
389 g_assert (filename != NULL);
391 subpath = g_path_get_dirname (image->name);
392 fullpath = g_build_path (G_DIR_SEPARATOR_S, subpath, filename, NULL);
393 mono_config_parse_publisher_policy (fullpath, binding_info);
397 /* Define the optional elements/attributes before checking */
398 if (!binding_info->culture)
399 binding_info->culture = g_strdup ("");
401 /* Check that the most important elements/attributes exist */
402 if (!binding_info->name || !binding_info->public_key_token [0] || !binding_info->has_old_version_bottom ||
403 !binding_info->has_new_version || !assembly_binding_maps_name (binding_info, aname)) {
404 mono_assembly_binding_info_free (binding_info);
405 binding_info->is_valid = FALSE;
409 binding_info->is_valid = TRUE;
413 compare_versions (AssemblyVersionSet *v, MonoAssemblyName *aname)
415 if (v->major > aname->major)
417 else if (v->major < aname->major)
420 if (v->minor > aname->minor)
422 else if (v->minor < aname->minor)
425 if (v->build > aname->build)
427 else if (v->build < aname->build)
430 if (v->revision > aname->revision)
432 else if (v->revision < aname->revision)
439 check_policy_versions (MonoAssemblyBindingInfo *info, MonoAssemblyName *name)
444 /* If has_old_version_top doesn't exist, we don't have an interval */
445 if (!info->has_old_version_top) {
446 if (compare_versions (&info->old_version_bottom, name) == 0)
452 /* Check that the version defined by name is valid for the interval */
453 if (compare_versions (&info->old_version_top, name) < 0)
456 /* We should be greater or equal than the small version */
457 if (compare_versions (&info->old_version_bottom, name) > 0)
464 * mono_assembly_names_equal:
466 * @r: second assembly.
468 * Compares two MonoAssemblyNames and returns whether they are equal.
470 * This compares the names, the cultures, the release version and their
473 * Returns: TRUE if both assembly names are equal.
476 mono_assembly_names_equal (MonoAssemblyName *l, MonoAssemblyName *r)
478 if (!l->name || !r->name)
481 if (strcmp (l->name, r->name))
484 if (l->culture && r->culture && strcmp (l->culture, r->culture))
487 if (l->major != r->major || l->minor != r->minor ||
488 l->build != r->build || l->revision != r->revision)
489 if (! ((l->major == 0 && l->minor == 0 && l->build == 0 && l->revision == 0) || (r->major == 0 && r->minor == 0 && r->build == 0 && r->revision == 0)))
492 if (!l->public_key_token [0] || !r->public_key_token [0])
495 if (!mono_public_tokens_are_equal (l->public_key_token, r->public_key_token))
501 static MonoAssembly *
502 load_in_path (const char *basename, const char** search_path, MonoImageOpenStatus *status, MonoBoolean refonly)
506 MonoAssembly *result;
508 for (i = 0; search_path [i]; ++i) {
509 fullpath = g_build_filename (search_path [i], basename, NULL);
510 result = mono_assembly_open_full (fullpath, status, refonly);
519 * mono_assembly_setrootdir:
520 * @root_dir: The pathname of the root directory where we will locate assemblies
522 * This routine sets the internal default root directory for looking up
525 * This is used by Windows installations to compute dynamically the
526 * place where the Mono assemblies are located.
530 mono_assembly_setrootdir (const char *root_dir)
533 * Override the MONO_ASSEMBLIES directory configured at compile time.
535 /* Leak if called more than once */
536 default_path [0] = g_strdup (root_dir);
540 * mono_assembly_getrootdir:
542 * Obtains the root directory used for looking up assemblies.
544 * Returns: a string with the directory, this string should not be freed.
546 G_CONST_RETURN gchar *
547 mono_assembly_getrootdir (void)
549 return default_path [0];
554 * @assembly_dir: the base directory for assemblies
555 * @config_dir: the base directory for configuration files
557 * This routine is used internally and by developers embedding
558 * the runtime into their own applications.
560 * There are a number of cases to consider: Mono as a system-installed
561 * package that is available on the location preconfigured or Mono in
562 * a relocated location.
564 * If you are using a system-installed Mono, you can pass NULL
565 * to both parameters. If you are not, you should compute both
566 * directory values and call this routine.
568 * The values for a given PREFIX are:
570 * assembly_dir: PREFIX/lib
571 * config_dir: PREFIX/etc
573 * Notice that embedders that use Mono in a relocated way must
574 * compute the location at runtime, as they will be in control
575 * of where Mono is installed.
578 mono_set_dirs (const char *assembly_dir, const char *config_dir)
580 if (assembly_dir == NULL)
581 assembly_dir = mono_config_get_assemblies_dir ();
582 if (config_dir == NULL)
583 config_dir = mono_config_get_cfg_dir ();
584 mono_assembly_setrootdir (assembly_dir);
585 mono_set_config_dir (config_dir);
591 compute_base (char *path)
593 char *p = strrchr (path, '/');
597 /* Not a well known Mono executable, we are embedded, cant guess the base */
598 if (strcmp (p, "/mono") && strcmp (p, "/mono-boehm") && strcmp (p, "/mono-sgen") && strcmp (p, "/pedump") && strcmp (p, "/monodis"))
602 p = strrchr (path, '/');
606 if (strcmp (p, "/bin") != 0)
615 mono_set_dirs (mono_config_get_assemblies_dir (), mono_config_get_cfg_dir ());
618 static G_GNUC_UNUSED void
622 char *config, *lib, *mono;
627 * Only /usr prefix is treated specially
629 bindir = mono_config_get_bin_dir ();
631 if (strncmp (exe, bindir, strlen (bindir)) == 0 || (base = compute_base (exe)) == NULL){
636 config = g_build_filename (base, "etc", NULL);
637 lib = g_build_filename (base, "lib", NULL);
638 mono = g_build_filename (lib, "mono/4.5", NULL); // FIXME: stop hardcoding 4.5 here
639 if (stat (mono, &buf) == -1)
642 mono_set_dirs (lib, config);
650 #endif /* HOST_WIN32 */
655 * Registers the root directory for the Mono runtime, for Linux and Solaris 10,
656 * this auto-detects the prefix where Mono was installed.
659 mono_set_rootdir (void)
661 #if defined(HOST_WIN32) || (defined(PLATFORM_MACOSX) && !defined(TARGET_ARM))
662 gchar *bindir, *installdir, *root, *name, *resolvedname, *config;
665 name = mono_get_module_file_name ((HMODULE) &__ImageBase);
669 * _NSGetExecutablePath may return -1 to indicate buf is not large
670 * enough, but we ignore that case to avoid having to do extra dynamic
671 * allocation for the path and hope that 4096 is enough - this is
672 * ok in the Linux/Solaris case below at least...
676 guint buf_size = sizeof (buf);
679 if (_NSGetExecutablePath (buf, &buf_size) == 0)
680 name = g_strdup (buf);
689 resolvedname = mono_path_resolve_symlinks (name);
691 bindir = g_path_get_dirname (resolvedname);
692 installdir = g_path_get_dirname (bindir);
693 root = g_build_path (G_DIR_SEPARATOR_S, installdir, "lib", NULL);
695 config = g_build_filename (root, "..", "etc", NULL);
697 mono_set_dirs (root, config);
699 if (g_file_test (root, G_FILE_TEST_EXISTS) && g_file_test (config, G_FILE_TEST_EXISTS))
700 mono_set_dirs (root, config);
710 g_free (resolvedname);
711 #elif defined(DISABLE_MONO_AUTODETECTION)
719 s = readlink ("/proc/self/exe", buf, sizeof (buf)-1);
727 /* Solaris 10 style */
728 str = g_strdup_printf ("/proc/%d/path/a.out", getpid ());
729 s = readlink (str, buf, sizeof (buf)-1);
741 * mono_assemblies_init:
743 * Initialize global variables used by this module.
746 mono_assemblies_init (void)
749 * Initialize our internal paths if we have not been initialized yet.
750 * This happens when embedders use Mono.
752 if (mono_assembly_getrootdir () == NULL)
756 check_extra_gac_path_env ();
758 mono_os_mutex_init_recursive (&assemblies_mutex);
759 mono_os_mutex_init (&assembly_binding_mutex);
763 mono_assembly_binding_lock (void)
765 mono_locks_os_acquire (&assembly_binding_mutex, AssemblyBindingLock);
769 mono_assembly_binding_unlock (void)
771 mono_locks_os_release (&assembly_binding_mutex, AssemblyBindingLock);
775 mono_assembly_fill_assembly_name (MonoImage *image, MonoAssemblyName *aname)
777 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
778 guint32 cols [MONO_ASSEMBLY_SIZE];
779 gint32 machine, flags;
784 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
787 aname->hash_value = NULL;
788 aname->name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_NAME]);
789 aname->culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_CULTURE]);
790 aname->flags = cols [MONO_ASSEMBLY_FLAGS];
791 aname->major = cols [MONO_ASSEMBLY_MAJOR_VERSION];
792 aname->minor = cols [MONO_ASSEMBLY_MINOR_VERSION];
793 aname->build = cols [MONO_ASSEMBLY_BUILD_NUMBER];
794 aname->revision = cols [MONO_ASSEMBLY_REV_NUMBER];
795 aname->hash_alg = cols [MONO_ASSEMBLY_HASH_ALG];
796 if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
797 guchar* token = (guchar *)g_malloc (8);
802 pkey = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
803 len = mono_metadata_decode_blob_size (pkey, &pkey);
804 aname->public_key = (guchar*)pkey;
806 mono_digest_get_public_token (token, aname->public_key, len);
807 encoded = encode_public_tok (token, 8);
808 g_strlcpy ((char*)aname->public_key_token, encoded, MONO_PUBLIC_KEY_TOKEN_LENGTH);
814 aname->public_key = NULL;
815 memset (aname->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
818 if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
819 aname->public_key = (guchar*)mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
822 aname->public_key = 0;
824 machine = ((MonoCLIImageInfo*)(image->image_info))->cli_header.coff.coff_machine;
825 flags = ((MonoCLIImageInfo*)(image->image_info))->cli_cli_header.ch_flags;
827 case COFF_MACHINE_I386:
828 /* https://bugzilla.xamarin.com/show_bug.cgi?id=17632 */
829 if (flags & (CLI_FLAGS_32BITREQUIRED|CLI_FLAGS_PREFERRED32BIT))
830 aname->arch = MONO_PROCESSOR_ARCHITECTURE_X86;
831 else if ((flags & 0x70) == 0x70)
832 aname->arch = MONO_PROCESSOR_ARCHITECTURE_NONE;
834 aname->arch = MONO_PROCESSOR_ARCHITECTURE_MSIL;
836 case COFF_MACHINE_IA64:
837 aname->arch = MONO_PROCESSOR_ARCHITECTURE_IA64;
839 case COFF_MACHINE_AMD64:
840 aname->arch = MONO_PROCESSOR_ARCHITECTURE_AMD64;
842 case COFF_MACHINE_ARM:
843 aname->arch = MONO_PROCESSOR_ARCHITECTURE_ARM;
853 * mono_stringify_assembly_name:
854 * @aname: the assembly name.
856 * Convert @aname into its string format. The returned string is dynamically
857 * allocated and should be freed by the caller.
859 * Returns: a newly allocated string with a string representation of
863 mono_stringify_assembly_name (MonoAssemblyName *aname)
865 const char *quote = (aname->name && g_ascii_isspace (aname->name [0])) ? "\"" : "";
867 return g_strdup_printf (
868 "%s%s%s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
869 quote, aname->name, quote,
870 aname->major, aname->minor, aname->build, aname->revision,
871 aname->culture && *aname->culture? aname->culture: "neutral",
872 aname->public_key_token [0] ? (char *)aname->public_key_token : "null",
873 (aname->flags & ASSEMBLYREF_RETARGETABLE_FLAG) ? ", Retargetable=Yes" : "");
877 assemblyref_public_tok (MonoImage *image, guint32 key_index, guint32 flags)
879 const gchar *public_tok;
882 public_tok = mono_metadata_blob_heap (image, key_index);
883 len = mono_metadata_decode_blob_size (public_tok, &public_tok);
885 if (flags & ASSEMBLYREF_FULL_PUBLIC_KEY_FLAG) {
887 mono_digest_get_public_token (token, (guchar*)public_tok, len);
888 return encode_public_tok (token, 8);
891 return encode_public_tok ((guchar*)public_tok, len);
895 * mono_assembly_addref:
896 * @assemnly: the assembly to reference
898 * This routine increments the reference count on a MonoAssembly.
899 * The reference count is reduced every time the method mono_assembly_close() is
903 mono_assembly_addref (MonoAssembly *assembly)
905 InterlockedIncrement (&assembly->ref_count);
909 * CAUTION: This table must be kept in sync with
910 * ivkm/reflect/Fusion.cs
913 #define SILVERLIGHT_KEY "7cec85d7bea7798e"
914 #define WINFX_KEY "31bf3856ad364e35"
915 #define ECMA_KEY "b77a5c561934e089"
916 #define MSFINAL_KEY "b03f5f7f11d50a3a"
924 static KeyRemapEntry key_remap_table[] = {
925 { "Microsoft.CSharp", WINFX_KEY, MSFINAL_KEY },
926 { "System", SILVERLIGHT_KEY, ECMA_KEY },
927 { "System.ComponentModel.Composition", WINFX_KEY, ECMA_KEY },
928 { "System.ComponentModel.DataAnnotations", "ddd0da4d3e678217", WINFX_KEY },
929 { "System.Core", SILVERLIGHT_KEY, ECMA_KEY },
930 // FIXME: MS uses MSFINAL_KEY for .NET 4.5
931 { "System.Net", SILVERLIGHT_KEY, MSFINAL_KEY },
932 { "System.Numerics", WINFX_KEY, ECMA_KEY },
933 { "System.Runtime.Serialization", SILVERLIGHT_KEY, ECMA_KEY },
934 { "System.ServiceModel", WINFX_KEY, ECMA_KEY },
935 { "System.ServiceModel.Web", SILVERLIGHT_KEY, WINFX_KEY },
936 { "System.Windows", SILVERLIGHT_KEY, MSFINAL_KEY },
937 { "System.Xml", SILVERLIGHT_KEY, ECMA_KEY },
938 { "System.Xml.Linq", WINFX_KEY, ECMA_KEY },
939 { "System.Xml.Serialization", WINFX_KEY, ECMA_KEY }
943 remap_keys (MonoAssemblyName *aname)
946 for (i = 0; i < G_N_ELEMENTS (key_remap_table); i++) {
947 const KeyRemapEntry *entry = &key_remap_table [i];
949 if (strcmp (aname->name, entry->name) ||
950 !mono_public_tokens_are_equal (aname->public_key_token, (const unsigned char*) entry->from))
953 memcpy (aname->public_key_token, entry->to, MONO_PUBLIC_KEY_TOKEN_LENGTH);
955 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
956 "Remapped public key token of retargetable assembly %s from %s to %s",
957 aname->name, entry->from, entry->to);
962 static MonoAssemblyName *
963 mono_assembly_remap_version (MonoAssemblyName *aname, MonoAssemblyName *dest_aname)
965 const MonoRuntimeInfo *current_runtime;
966 int pos, first, last;
968 if (aname->name == NULL) return aname;
970 current_runtime = mono_get_runtime_info ();
972 if (aname->flags & ASSEMBLYREF_RETARGETABLE_FLAG) {
973 const AssemblyVersionSet* vset;
975 /* Remap to current runtime */
976 vset = ¤t_runtime->version_sets [0];
978 memcpy (dest_aname, aname, sizeof(MonoAssemblyName));
979 dest_aname->major = vset->major;
980 dest_aname->minor = vset->minor;
981 dest_aname->build = vset->build;
982 dest_aname->revision = vset->revision;
983 dest_aname->flags &= ~ASSEMBLYREF_RETARGETABLE_FLAG;
985 /* Remap assembly name */
986 if (!strcmp (aname->name, "System.Net"))
987 dest_aname->name = g_strdup ("System");
989 remap_keys (dest_aname);
991 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
992 "The request to load the retargetable assembly %s v%d.%d.%d.%d was remapped to %s v%d.%d.%d.%d",
994 aname->major, aname->minor, aname->build, aname->revision,
996 vset->major, vset->minor, vset->build, vset->revision
1002 #ifndef DISABLE_ASSEMBLY_REMAPPING
1004 last = G_N_ELEMENTS (framework_assemblies) - 1;
1006 while (first <= last) {
1008 pos = first + (last - first) / 2;
1009 res = strcmp (aname->name, framework_assemblies[pos].assembly_name);
1011 const AssemblyVersionSet* vset;
1012 int index = framework_assemblies[pos].version_set_index;
1013 g_assert (index < G_N_ELEMENTS (current_runtime->version_sets));
1014 vset = ¤t_runtime->version_sets [index];
1016 if (aname->major == vset->major && aname->minor == vset->minor &&
1017 aname->build == vset->build && aname->revision == vset->revision)
1020 if (compare_versions (vset, aname) < 0) {
1021 // requested version is newer than current
1022 // runtime version, don't remap
1026 if ((aname->major | aname->minor | aname->build | aname->revision) != 0)
1027 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY,
1028 "The request to load the assembly %s v%d.%d.%d.%d was remapped to v%d.%d.%d.%d",
1030 aname->major, aname->minor, aname->build, aname->revision,
1031 vset->major, vset->minor, vset->build, vset->revision
1034 memcpy (dest_aname, aname, sizeof(MonoAssemblyName));
1035 dest_aname->major = vset->major;
1036 dest_aname->minor = vset->minor;
1037 dest_aname->build = vset->build;
1038 dest_aname->revision = vset->revision;
1040 } else if (res < 0) {
1052 * mono_assembly_get_assemblyref:
1053 * @image: pointer to the MonoImage to extract the information from.
1054 * @index: index to the assembly reference in the image.
1055 * @aname: pointer to a `MonoAssemblyName` that will hold the returned value.
1057 * Fills out the @aname with the assembly name of the @index assembly reference in @image.
1060 mono_assembly_get_assemblyref (MonoImage *image, int index, MonoAssemblyName *aname)
1063 guint32 cols [MONO_ASSEMBLYREF_SIZE];
1066 t = &image->tables [MONO_TABLE_ASSEMBLYREF];
1068 mono_metadata_decode_row (t, index, cols, MONO_ASSEMBLYREF_SIZE);
1070 hash = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLYREF_HASH_VALUE]);
1071 aname->hash_len = mono_metadata_decode_blob_size (hash, &hash);
1072 aname->hash_value = hash;
1073 aname->name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_NAME]);
1074 aname->culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_CULTURE]);
1075 aname->flags = cols [MONO_ASSEMBLYREF_FLAGS];
1076 aname->major = cols [MONO_ASSEMBLYREF_MAJOR_VERSION];
1077 aname->minor = cols [MONO_ASSEMBLYREF_MINOR_VERSION];
1078 aname->build = cols [MONO_ASSEMBLYREF_BUILD_NUMBER];
1079 aname->revision = cols [MONO_ASSEMBLYREF_REV_NUMBER];
1081 if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]) {
1082 gchar *token = assemblyref_public_tok (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], aname->flags);
1083 g_strlcpy ((char*)aname->public_key_token, token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1086 memset (aname->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1091 mono_assembly_load_reference (MonoImage *image, int index)
1093 MonoAssembly *reference;
1094 MonoAssemblyName aname;
1095 MonoImageOpenStatus status;
1098 * image->references is shared between threads, so we need to access
1099 * it inside a critical section.
1101 mono_assemblies_lock ();
1102 if (!image->references) {
1103 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
1105 image->references = g_new0 (MonoAssembly *, t->rows + 1);
1106 image->nreferences = t->rows;
1108 reference = image->references [index];
1109 mono_assemblies_unlock ();
1113 mono_assembly_get_assemblyref (image, index, &aname);
1115 if (image->assembly && image->assembly->ref_only) {
1116 /* We use the loaded corlib */
1117 if (!strcmp (aname.name, "mscorlib"))
1118 reference = mono_assembly_load_full_internal (&aname, image->assembly, image->assembly->basedir, &status, FALSE);
1120 reference = mono_assembly_loaded_full (&aname, TRUE);
1122 /* Try a postload search hook */
1123 reference = mono_assembly_invoke_search_hook_internal (&aname, image->assembly, TRUE, TRUE);
1127 * Here we must advice that the error was due to
1128 * a non loaded reference using the ReflectionOnly api
1131 reference = (MonoAssembly *)REFERENCE_MISSING;
1133 /* we first try without setting the basedir: this can eventually result in a ResolveAssembly
1134 * event which is the MS .net compatible behaviour (the assemblyresolve_event3.cs test has been fixed
1135 * accordingly, it would fail on the MS runtime before).
1136 * The second load attempt has the basedir set to keep compatibility with the old mono behavior, for
1137 * example bug-349190.2.cs and who knows how much more code in the wild.
1139 reference = mono_assembly_load_full_internal (&aname, image->assembly, NULL, &status, FALSE);
1140 if (!reference && image->assembly)
1141 reference = mono_assembly_load_full_internal (&aname, image->assembly, image->assembly->basedir, &status, FALSE);
1144 if (reference == NULL){
1147 if (status == MONO_IMAGE_ERROR_ERRNO && errno == ENOENT) {
1148 extra_msg = g_strdup_printf ("The assembly was not found in the Global Assembly Cache, a path listed in the MONO_PATH environment variable, or in the location of the executing assembly (%s).\n", image->assembly != NULL ? image->assembly->basedir : "" );
1149 } else if (status == MONO_IMAGE_ERROR_ERRNO) {
1150 extra_msg = g_strdup_printf ("System error: %s\n", strerror (errno));
1151 } else if (status == MONO_IMAGE_MISSING_ASSEMBLYREF) {
1152 extra_msg = g_strdup ("Cannot find an assembly referenced from this one.\n");
1153 } else if (status == MONO_IMAGE_IMAGE_INVALID) {
1154 extra_msg = g_strdup ("The file exists but is not a valid assembly.\n");
1156 extra_msg = g_strdup ("");
1159 mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY, "The following assembly referenced from %s could not be loaded:\n"
1160 " Assembly: %s (assemblyref_index=%d)\n"
1161 " Version: %d.%d.%d.%d\n"
1162 " Public Key: %s\n%s",
1163 image->name, aname.name, index,
1164 aname.major, aname.minor, aname.build, aname.revision,
1165 strlen ((char*)aname.public_key_token) == 0 ? "(none)" : (char*)aname.public_key_token, extra_msg);
1169 mono_assemblies_lock ();
1170 if (reference == NULL) {
1171 /* Flag as not found */
1172 reference = (MonoAssembly *)REFERENCE_MISSING;
1175 if (!image->references [index]) {
1176 if (reference != REFERENCE_MISSING){
1177 mono_assembly_addref (reference);
1178 if (image->assembly)
1179 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly Ref addref %s[%p] -> %s[%p]: %d",
1180 image->assembly->aname.name, image->assembly, reference->aname.name, reference, reference->ref_count);
1182 if (image->assembly)
1183 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Failed to load assembly %s[%p]\n",
1184 image->assembly->aname.name, image->assembly);
1187 image->references [index] = reference;
1189 mono_assemblies_unlock ();
1191 if (image->references [index] != reference) {
1192 /* Somebody loaded it before us */
1193 mono_assembly_close (reference);
1198 * mono_assembly_load_references:
1201 * @deprecated: There is no reason to use this method anymore, it does nothing
1203 * This method is now a no-op, it does nothing other than setting the @status to #MONO_IMAGE_OK
1206 mono_assembly_load_references (MonoImage *image, MonoImageOpenStatus *status)
1208 /* This is a no-op now but it is part of the embedding API so we can't remove it */
1209 *status = MONO_IMAGE_OK;
1212 typedef struct AssemblyLoadHook AssemblyLoadHook;
1213 struct AssemblyLoadHook {
1214 AssemblyLoadHook *next;
1215 MonoAssemblyLoadFunc func;
1219 AssemblyLoadHook *assembly_load_hook = NULL;
1222 mono_assembly_invoke_load_hook (MonoAssembly *ass)
1224 AssemblyLoadHook *hook;
1226 for (hook = assembly_load_hook; hook; hook = hook->next) {
1227 hook->func (ass, hook->user_data);
1232 mono_install_assembly_load_hook (MonoAssemblyLoadFunc func, gpointer user_data)
1234 AssemblyLoadHook *hook;
1236 g_return_if_fail (func != NULL);
1238 hook = g_new0 (AssemblyLoadHook, 1);
1240 hook->user_data = user_data;
1241 hook->next = assembly_load_hook;
1242 assembly_load_hook = hook;
1246 free_assembly_load_hooks (void)
1248 AssemblyLoadHook *hook, *next;
1250 for (hook = assembly_load_hook; hook; hook = next) {
1256 typedef struct AssemblySearchHook AssemblySearchHook;
1257 struct AssemblySearchHook {
1258 AssemblySearchHook *next;
1259 MonoAssemblySearchFunc func;
1265 AssemblySearchHook *assembly_search_hook = NULL;
1267 static MonoAssembly*
1268 mono_assembly_invoke_search_hook_internal (MonoAssemblyName *aname, MonoAssembly *requesting, gboolean refonly, gboolean postload)
1270 AssemblySearchHook *hook;
1272 for (hook = assembly_search_hook; hook; hook = hook->next) {
1273 if ((hook->refonly == refonly) && (hook->postload == postload)) {
1276 * A little explanation is in order here.
1278 * The default postload search hook needs to know the requesting assembly to report it to managed code.
1279 * The embedding API exposes a search hook that doesn't take such argument.
1281 * The original fix would call the default search hook before all the registered ones and pass
1282 * the requesting assembly to it. It works but broke a very suddle embedding API aspect that some users
1283 * rely on. Which is the ordering between user hooks and the default runtime hook.
1285 * Registering the hook after mono_jit_init would let your hook run before the default one and
1286 * when using it to handle non standard app layouts this could save your app from a massive amount
1287 * of syscalls that the default hook does when probing all sorts of places. Slow targets with horrible IO
1288 * are all using this trick and if we broke this assumption they would be very disapointed at us.
1290 * So what's the fix? We register the default hook using regular means and special case it when iterating
1291 * over the registered hooks. This preserves ordering and enables managed resolve hooks to get the requesting
1294 if (hook->func == (void*)mono_domain_assembly_postload_search)
1295 ass = mono_domain_assembly_postload_search (aname, requesting, refonly);
1297 ass = hook->func (aname, hook->user_data);
1307 mono_assembly_invoke_search_hook (MonoAssemblyName *aname)
1309 return mono_assembly_invoke_search_hook_internal (aname, NULL, FALSE, FALSE);
1313 mono_install_assembly_search_hook_internal (MonoAssemblySearchFunc func, gpointer user_data, gboolean refonly, gboolean postload)
1315 AssemblySearchHook *hook;
1317 g_return_if_fail (func != NULL);
1319 hook = g_new0 (AssemblySearchHook, 1);
1321 hook->user_data = user_data;
1322 hook->refonly = refonly;
1323 hook->postload = postload;
1324 hook->next = assembly_search_hook;
1325 assembly_search_hook = hook;
1329 mono_install_assembly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1331 mono_install_assembly_search_hook_internal (func, user_data, FALSE, FALSE);
1335 free_assembly_search_hooks (void)
1337 AssemblySearchHook *hook, *next;
1339 for (hook = assembly_search_hook; hook; hook = next) {
1346 mono_install_assembly_refonly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1348 mono_install_assembly_search_hook_internal (func, user_data, TRUE, FALSE);
1352 mono_install_assembly_postload_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1354 mono_install_assembly_search_hook_internal (func, user_data, FALSE, TRUE);
1358 mono_install_assembly_postload_refonly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
1360 mono_install_assembly_search_hook_internal (func, user_data, TRUE, TRUE);
1363 typedef struct AssemblyPreLoadHook AssemblyPreLoadHook;
1364 struct AssemblyPreLoadHook {
1365 AssemblyPreLoadHook *next;
1366 MonoAssemblyPreLoadFunc func;
1370 static AssemblyPreLoadHook *assembly_preload_hook = NULL;
1371 static AssemblyPreLoadHook *assembly_refonly_preload_hook = NULL;
1373 static MonoAssembly *
1374 invoke_assembly_preload_hook (MonoAssemblyName *aname, gchar **assemblies_path)
1376 AssemblyPreLoadHook *hook;
1377 MonoAssembly *assembly;
1379 for (hook = assembly_preload_hook; hook; hook = hook->next) {
1380 assembly = hook->func (aname, assemblies_path, hook->user_data);
1381 if (assembly != NULL)
1388 static MonoAssembly *
1389 invoke_assembly_refonly_preload_hook (MonoAssemblyName *aname, gchar **assemblies_path)
1391 AssemblyPreLoadHook *hook;
1392 MonoAssembly *assembly;
1394 for (hook = assembly_refonly_preload_hook; hook; hook = hook->next) {
1395 assembly = hook->func (aname, assemblies_path, hook->user_data);
1396 if (assembly != NULL)
1404 mono_install_assembly_preload_hook (MonoAssemblyPreLoadFunc func, gpointer user_data)
1406 AssemblyPreLoadHook *hook;
1408 g_return_if_fail (func != NULL);
1410 hook = g_new0 (AssemblyPreLoadHook, 1);
1412 hook->user_data = user_data;
1413 hook->next = assembly_preload_hook;
1414 assembly_preload_hook = hook;
1418 mono_install_assembly_refonly_preload_hook (MonoAssemblyPreLoadFunc func, gpointer user_data)
1420 AssemblyPreLoadHook *hook;
1422 g_return_if_fail (func != NULL);
1424 hook = g_new0 (AssemblyPreLoadHook, 1);
1426 hook->user_data = user_data;
1427 hook->next = assembly_refonly_preload_hook;
1428 assembly_refonly_preload_hook = hook;
1432 free_assembly_preload_hooks (void)
1434 AssemblyPreLoadHook *hook, *next;
1436 for (hook = assembly_preload_hook; hook; hook = next) {
1441 for (hook = assembly_refonly_preload_hook; hook; hook = next) {
1448 absolute_dir (const gchar *filename)
1459 if (g_path_is_absolute (filename)) {
1460 part = g_path_get_dirname (filename);
1461 res = g_strconcat (part, G_DIR_SEPARATOR_S, NULL);
1466 cwd = g_get_current_dir ();
1467 mixed = g_build_filename (cwd, filename, NULL);
1468 parts = g_strsplit (mixed, G_DIR_SEPARATOR_S, 0);
1473 for (i = 0; (part = parts [i]) != NULL; i++) {
1474 if (!strcmp (part, "."))
1477 if (!strcmp (part, "..")) {
1478 if (list && list->next) /* Don't remove root */
1479 list = g_list_delete_link (list, list);
1481 list = g_list_prepend (list, part);
1485 result = g_string_new ("");
1486 list = g_list_reverse (list);
1488 /* Ignores last data pointer, which should be the filename */
1489 for (tmp = list; tmp && tmp->next != NULL; tmp = tmp->next){
1491 g_string_append_printf (result, "%s%c", (char *) tmp->data,
1496 g_string_free (result, FALSE);
1501 return g_strdup (".");
1508 * mono_assembly_open_from_bundle:
1509 * @filename: Filename requested
1510 * @status: return status code
1512 * This routine tries to open the assembly specified by `filename' from the
1513 * defined bundles, if found, returns the MonoImage for it, if not found
1517 mono_assembly_open_from_bundle (const char *filename, MonoImageOpenStatus *status, gboolean refonly)
1521 MonoImage *image = NULL;
1524 * we do a very simple search for bundled assemblies: it's not a general
1525 * purpose assembly loading mechanism.
1531 name = g_path_get_basename (filename);
1533 mono_assemblies_lock ();
1534 for (i = 0; !image && bundles [i]; ++i) {
1535 if (strcmp (bundles [i]->name, name) == 0) {
1536 image = mono_image_open_from_data_with_name ((char*)bundles [i]->data, bundles [i]->size, FALSE, status, refonly, name);
1540 mono_assemblies_unlock ();
1542 mono_image_addref (image);
1543 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly Loader loaded assembly from bundle: '%s'.", name);
1552 * mono_assemblies_open_full:
1553 * @filename: the file to load
1554 * @status: return status code
1555 * @refonly: Whether this assembly is being opened in "reflection-only" mode.
1557 * This loads an assembly from the specified @filename. The @filename allows
1558 * a local URL (starting with a file:// prefix). If a file prefix is used, the
1559 * filename is interpreted as a URL, and the filename is URL-decoded. Otherwise the file
1560 * is treated as a local path.
1562 * First, an attempt is made to load the assembly from the bundled executable (for those
1563 * deployments that have been done with the `mkbundle` tool or for scenarios where the
1564 * assembly has been registered as an embedded assembly). If this is not the case, then
1565 * the assembly is loaded from disk using `api:mono_image_open_full`.
1567 * If the pointed assembly does not live in the Global Assembly Cache, a shadow copy of
1568 * the assembly is made.
1570 * If @refonly is set to true, then the assembly is loaded purely for inspection with
1571 * the `System.Reflection` API.
1573 * Returns: NULL on error, with the @status set to an error code, or a pointer
1577 mono_assembly_open_full (const char *filename, MonoImageOpenStatus *status, gboolean refonly)
1581 MonoImageOpenStatus def_status;
1584 gboolean loaded_from_bundle;
1586 g_return_val_if_fail (filename != NULL, NULL);
1589 status = &def_status;
1590 *status = MONO_IMAGE_OK;
1592 if (strncmp (filename, "file://", 7) == 0) {
1593 GError *error = NULL;
1594 gchar *uri = (gchar *) filename;
1598 * MS allows file://c:/... and fails on file://localhost/c:/...
1599 * They also throw an IndexOutOfRangeException if "file://"
1602 uri = g_strdup_printf ("file:///%s", uri + 7);
1605 uri = mono_escape_uri_string (tmpuri);
1606 fname = g_filename_from_uri (uri, NULL, &error);
1609 if (tmpuri != filename)
1612 if (error != NULL) {
1613 g_warning ("%s\n", error->message);
1614 g_error_free (error);
1615 fname = g_strdup (filename);
1618 fname = g_strdup (filename);
1621 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
1622 "Assembly Loader probing location: '%s'.", fname);
1625 if (!mono_assembly_is_in_gac (fname)) {
1627 new_fname = mono_make_shadow_copy (fname, &error);
1628 mono_error_raise_exception (&error); /* FIXME don't raise here */
1630 if (new_fname && new_fname != fname) {
1633 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
1634 "Assembly Loader shadow-copied assembly to: '%s'.", fname);
1639 // If VM built with mkbundle
1640 loaded_from_bundle = FALSE;
1641 if (bundles != NULL) {
1642 image = mono_assembly_open_from_bundle (fname, status, refonly);
1643 loaded_from_bundle = image != NULL;
1647 image = mono_image_open_full (fname, status, refonly);
1650 if (*status == MONO_IMAGE_OK)
1651 *status = MONO_IMAGE_ERROR_ERRNO;
1656 if (image->assembly) {
1657 /* Already loaded by another appdomain */
1658 mono_assembly_invoke_load_hook (image->assembly);
1659 mono_image_close (image);
1661 return image->assembly;
1664 ass = mono_assembly_load_from_full (image, fname, status, refonly);
1667 if (!loaded_from_bundle)
1668 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
1669 "Assembly Loader loaded assembly from location: '%s'.", filename);
1671 mono_config_for_assembly (ass->image);
1674 /* Clear the reference added by mono_image_open */
1675 mono_image_close (image);
1683 free_item (gpointer val, gpointer user_data)
1689 * mono_assembly_load_friends:
1692 * Load the list of friend assemblies that are allowed to access
1693 * the assembly's internal types and members. They are stored as assembly
1694 * names in custom attributes.
1696 * This is an internal method, we need this because when we load mscorlib
1697 * we do not have the internals visible cattr loaded yet,
1698 * so we need to load these after we initialize the runtime.
1700 * LOCKING: Acquires the assemblies lock plus the loader lock.
1703 mono_assembly_load_friends (MonoAssembly* ass)
1707 MonoCustomAttrInfo* attrs;
1710 if (ass->friend_assembly_names_inited)
1713 attrs = mono_custom_attrs_from_assembly_checked (ass, &error);
1714 mono_error_assert_ok (&error);
1716 mono_assemblies_lock ();
1717 ass->friend_assembly_names_inited = TRUE;
1718 mono_assemblies_unlock ();
1722 mono_assemblies_lock ();
1723 if (ass->friend_assembly_names_inited) {
1724 mono_assemblies_unlock ();
1727 mono_assemblies_unlock ();
1731 * We build the list outside the assemblies lock, the worse that can happen
1732 * is that we'll need to free the allocated list.
1734 for (i = 0; i < attrs->num_attrs; ++i) {
1735 MonoCustomAttrEntry *attr = &attrs->attrs [i];
1736 MonoAssemblyName *aname;
1738 /* Do some sanity checking */
1739 if (!attr->ctor || attr->ctor->klass != mono_class_try_get_internals_visible_class ())
1741 if (attr->data_size < 4)
1743 data = (const char*)attr->data;
1744 /* 0xFF means null string, see custom attr format */
1745 if (data [0] != 1 || data [1] != 0 || (data [2] & 0xFF) == 0xFF)
1747 mono_metadata_decode_value (data + 2, &data);
1748 aname = g_new0 (MonoAssemblyName, 1);
1749 /*g_print ("friend ass: %s\n", data);*/
1750 if (mono_assembly_name_parse_full (data, aname, TRUE, NULL, NULL)) {
1751 list = g_slist_prepend (list, aname);
1756 mono_custom_attrs_free (attrs);
1758 mono_assemblies_lock ();
1759 if (ass->friend_assembly_names_inited) {
1760 mono_assemblies_unlock ();
1761 g_slist_foreach (list, free_item, NULL);
1762 g_slist_free (list);
1765 ass->friend_assembly_names = list;
1767 /* Because of the double checked locking pattern above */
1768 mono_memory_barrier ();
1769 ass->friend_assembly_names_inited = TRUE;
1770 mono_assemblies_unlock ();
1774 * mono_assembly_open:
1775 * @filename: Opens the assembly pointed out by this name
1776 * @status: return status code
1778 * This loads an assembly from the specified @filename. The @filename allows
1779 * a local URL (starting with a file:// prefix). If a file prefix is used, the
1780 * filename is interpreted as a URL, and the filename is URL-decoded. Otherwise the file
1781 * is treated as a local path.
1783 * First, an attempt is made to load the assembly from the bundled executable (for those
1784 * deployments that have been done with the `mkbundle` tool or for scenarios where the
1785 * assembly has been registered as an embedded assembly). If this is not the case, then
1786 * the assembly is loaded from disk using `api:mono_image_open_full`.
1788 * If the pointed assembly does not live in the Global Assembly Cache, a shadow copy of
1789 * the assembly is made.
1791 * Return: a pointer to the MonoAssembly if @filename contains a valid
1792 * assembly or NULL on error. Details about the error are stored in the
1796 mono_assembly_open (const char *filename, MonoImageOpenStatus *status)
1798 return mono_assembly_open_full (filename, status, FALSE);
1802 * mono_assembly_load_from_full:
1803 * @image: Image to load the assembly from
1804 * @fname: assembly name to associate with the assembly
1805 * @status: returns the status condition
1806 * @refonly: Whether this assembly is being opened in "reflection-only" mode.
1808 * If the provided @image has an assembly reference, it will process the given
1809 * image as an assembly with the given name.
1811 * Most likely you want to use the `api:mono_assembly_load_full` method instead.
1813 * Returns: A valid pointer to a `MonoAssembly*` on success and the @status will be
1814 * set to #MONO_IMAGE_OK; or NULL on error.
1816 * If there is an error loading the assembly the @status will indicate the
1817 * reason with @status being set to `MONO_IMAGE_INVALID` if the
1818 * image did not contain an assembly reference table.
1821 mono_assembly_load_from_full (MonoImage *image, const char*fname,
1822 MonoImageOpenStatus *status, gboolean refonly)
1824 MonoAssembly *ass, *ass2;
1827 if (!image->tables [MONO_TABLE_ASSEMBLY].rows) {
1828 /* 'image' doesn't have a manifest -- maybe someone is trying to Assembly.Load a .netmodule */
1829 *status = MONO_IMAGE_IMAGE_INVALID;
1833 #if defined (HOST_WIN32)
1838 tmp_fn = g_strdup (fname);
1839 for (i = strlen (tmp_fn) - 1; i >= 0; i--) {
1840 if (tmp_fn [i] == '/')
1844 base_dir = absolute_dir (tmp_fn);
1848 base_dir = absolute_dir (fname);
1852 * Create assembly struct, and enter it into the assembly cache
1854 ass = g_new0 (MonoAssembly, 1);
1855 ass->basedir = base_dir;
1856 ass->ref_only = refonly;
1859 mono_profiler_assembly_event (ass, MONO_PROFILE_START_LOAD);
1861 mono_assembly_fill_assembly_name (image, &ass->aname);
1863 if (mono_defaults.corlib && strcmp (ass->aname.name, "mscorlib") == 0) {
1864 // MS.NET doesn't support loading other mscorlibs
1867 mono_image_addref (mono_defaults.corlib);
1868 *status = MONO_IMAGE_OK;
1869 return mono_defaults.corlib->assembly;
1872 /* Add a non-temporary reference because of ass->image */
1873 mono_image_addref (image);
1875 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Image addref %s[%p] -> %s[%p]: %d", ass->aname.name, ass, image->name, image, image->ref_count);
1878 * The load hooks might take locks so we can't call them while holding the
1881 if (ass->aname.name) {
1882 ass2 = mono_assembly_invoke_search_hook_internal (&ass->aname, NULL, refonly, FALSE);
1886 mono_image_close (image);
1887 *status = MONO_IMAGE_OK;
1892 mono_assemblies_lock ();
1894 if (image->assembly) {
1896 * This means another thread has already loaded the assembly, but not yet
1897 * called the load hooks so the search hook can't find the assembly.
1899 mono_assemblies_unlock ();
1900 ass2 = image->assembly;
1903 mono_image_close (image);
1904 *status = MONO_IMAGE_OK;
1908 image->assembly = ass;
1910 loaded_assemblies = g_list_prepend (loaded_assemblies, ass);
1911 mono_assemblies_unlock ();
1914 if (image->is_module_handle)
1915 mono_image_fixup_vtable (image);
1918 mono_assembly_invoke_load_hook (ass);
1920 mono_profiler_assembly_loaded (ass, MONO_PROFILE_OK);
1926 * mono_assembly_load_from:
1927 * @image: Image to load the assembly from
1928 * @fname: assembly name to associate with the assembly
1929 * @status: return status code
1931 * If the provided @image has an assembly reference, it will process the given
1932 * image as an assembly with the given name.
1934 * Most likely you want to use the `api:mono_assembly_load_full` method instead.
1936 * This is equivalent to calling `api:mono_assembly_load_from_full` with the
1937 * @refonly parameter set to FALSE.
1938 * Returns: A valid pointer to a `MonoAssembly*` on success and the @status will be
1939 * set to #MONO_IMAGE_OK; or NULL on error.
1941 * If there is an error loading the assembly the @status will indicate the
1942 * reason with @status being set to `MONO_IMAGE_INVALID` if the
1943 * image did not contain an assembly reference table.
1947 mono_assembly_load_from (MonoImage *image, const char *fname,
1948 MonoImageOpenStatus *status)
1950 return mono_assembly_load_from_full (image, fname, status, FALSE);
1954 * mono_assembly_name_free:
1955 * @aname: assembly name to free
1957 * Frees the provided assembly name object.
1958 * (it does not frees the object itself, only the name members).
1961 mono_assembly_name_free (MonoAssemblyName *aname)
1966 g_free ((void *) aname->name);
1967 g_free ((void *) aname->culture);
1968 g_free ((void *) aname->hash_value);
1972 parse_public_key (const gchar *key, gchar** pubkey, gboolean *is_ecma)
1975 gchar header [16], val, *arr;
1976 gint i, j, offset, bitlen, keylen, pkeylen;
1978 keylen = strlen (key) >> 1;
1982 /* allow the ECMA standard key */
1983 if (strcmp (key, "00000000000000000400000000000000") == 0) {
1985 *pubkey = g_strdup (key);
1991 val = g_ascii_xdigit_value (key [0]) << 4;
1992 val |= g_ascii_xdigit_value (key [1]);
1997 val = g_ascii_xdigit_value (key [24]);
1998 val |= g_ascii_xdigit_value (key [25]);
2010 /* We need the first 16 bytes
2011 * to check whether this key is valid or not */
2012 pkeylen = strlen (pkey) >> 1;
2016 for (i = 0, j = 0; i < 16; i++) {
2017 header [i] = g_ascii_xdigit_value (pkey [j++]) << 4;
2018 header [i] |= g_ascii_xdigit_value (pkey [j++]);
2021 if (header [0] != 0x06 || /* PUBLICKEYBLOB (0x06) */
2022 header [1] != 0x02 || /* Version (0x02) */
2023 header [2] != 0x00 || /* Reserved (word) */
2024 header [3] != 0x00 ||
2025 (guint)(read32 (header + 8)) != 0x31415352) /* DWORD magic = RSA1 */
2028 /* Based on this length, we _should_ be able to know if the length is right */
2029 bitlen = read32 (header + 12) >> 3;
2030 if ((bitlen + 16 + 4) != pkeylen)
2033 /* parsing is OK and the public key itself is not requested back */
2037 /* Encode the size of the blob */
2039 if (keylen <= 127) {
2040 arr = (gchar *)g_malloc (keylen + 1);
2041 arr [offset++] = keylen;
2043 arr = (gchar *)g_malloc (keylen + 2);
2044 arr [offset++] = 0x80; /* 10bs */
2045 arr [offset++] = keylen;
2048 for (i = offset, j = 0; i < keylen + offset; i++) {
2049 arr [i] = g_ascii_xdigit_value (key [j++]) << 4;
2050 arr [i] |= g_ascii_xdigit_value (key [j++]);
2059 build_assembly_name (const char *name, const char *version, const char *culture, const char *token, const char *key, guint32 flags, guint32 arch, MonoAssemblyName *aname, gboolean save_public_key)
2061 gint major, minor, build, revision;
2064 gchar *pkey, *pkeyptr, *encoded, tok [8];
2066 memset (aname, 0, sizeof (MonoAssemblyName));
2069 version_parts = sscanf (version, "%u.%u.%u.%u", &major, &minor, &build, &revision);
2070 if (version_parts < 2 || version_parts > 4)
2073 /* FIXME: we should set build & revision to -1 (instead of 0)
2074 if these are not set in the version string. That way, later on,
2075 we can still determine if these were specified. */
2076 aname->major = major;
2077 aname->minor = minor;
2078 if (version_parts >= 3)
2079 aname->build = build;
2082 if (version_parts == 4)
2083 aname->revision = revision;
2085 aname->revision = 0;
2088 aname->flags = flags;
2090 aname->name = g_strdup (name);
2093 if (g_ascii_strcasecmp (culture, "neutral") == 0)
2094 aname->culture = g_strdup ("");
2096 aname->culture = g_strdup (culture);
2099 if (token && strncmp (token, "null", 4) != 0) {
2102 /* the constant includes the ending NULL, hence the -1 */
2103 if (strlen (token) != (MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)) {
2104 mono_assembly_name_free (aname);
2107 lower = g_ascii_strdown (token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
2108 g_strlcpy ((char*)aname->public_key_token, lower, MONO_PUBLIC_KEY_TOKEN_LENGTH);
2114 if (strcmp (key, "null") == 0 || !parse_public_key (key, &pkey, &is_ecma)) {
2115 mono_assembly_name_free (aname);
2120 if (save_public_key)
2121 aname->public_key = (guint8*)pkey;
2124 g_strlcpy ((gchar*)aname->public_key_token, "b77a5c561934e089", MONO_PUBLIC_KEY_TOKEN_LENGTH);
2128 len = mono_metadata_decode_blob_size ((const gchar *) pkey, (const gchar **) &pkeyptr);
2129 // We also need to generate the key token
2130 mono_digest_get_public_token ((guchar*) tok, (guint8*) pkeyptr, len);
2131 encoded = encode_public_tok ((guchar*) tok, 8);
2132 g_strlcpy ((gchar*)aname->public_key_token, encoded, MONO_PUBLIC_KEY_TOKEN_LENGTH);
2135 if (save_public_key)
2136 aname->public_key = (guint8*) pkey;
2145 parse_assembly_directory_name (const char *name, const char *dirname, MonoAssemblyName *aname)
2150 parts = g_strsplit (dirname, "_", 3);
2151 if (!parts || !parts[0] || !parts[1] || !parts[2]) {
2156 res = build_assembly_name (name, parts[0], parts[1], parts[2], NULL, 0, 0, aname, FALSE);
2162 split_key_value (const gchar *pair, gchar **key, guint32 *keylen, gchar **value)
2164 char *eqsign = strchr (pair, '=');
2172 *key = (gchar*)pair;
2173 *keylen = eqsign - *key;
2174 while (*keylen > 0 && g_ascii_isspace ((*key) [*keylen - 1]))
2176 *value = g_strstrip (eqsign + 1);
2181 mono_assembly_name_parse_full (const char *name, MonoAssemblyName *aname, gboolean save_public_key, gboolean *is_version_defined, gboolean *is_token_defined)
2185 gchar *version = NULL;
2187 gchar *culture = NULL;
2189 gchar *token = NULL;
2193 gchar *retargetable = NULL;
2194 gchar *retargetable_uq;
2198 gchar *value, *part_name;
2199 guint32 part_name_len;
2202 gboolean version_defined;
2203 gboolean token_defined;
2205 guint32 arch = MONO_PROCESSOR_ARCHITECTURE_NONE;
2207 if (!is_version_defined)
2208 is_version_defined = &version_defined;
2209 *is_version_defined = FALSE;
2210 if (!is_token_defined)
2211 is_token_defined = &token_defined;
2212 *is_token_defined = FALSE;
2214 parts = tmp = g_strsplit (name, ",", 6);
2215 if (!tmp || !*tmp) {
2220 dllname = g_strstrip (*tmp);
2225 if (!split_key_value (g_strstrip (*tmp), &part_name, &part_name_len, &value))
2226 goto cleanup_and_fail;
2228 if (part_name_len == 7 && !g_ascii_strncasecmp (part_name, "Version", part_name_len)) {
2229 *is_version_defined = TRUE;
2231 if (strlen (version) == 0) {
2232 goto cleanup_and_fail;
2238 if (part_name_len == 7 && !g_ascii_strncasecmp (part_name, "Culture", part_name_len)) {
2240 if (strlen (culture) == 0) {
2241 goto cleanup_and_fail;
2247 if (part_name_len == 14 && !g_ascii_strncasecmp (part_name, "PublicKeyToken", part_name_len)) {
2248 *is_token_defined = TRUE;
2250 if (strlen (token) == 0) {
2251 goto cleanup_and_fail;
2257 if (part_name_len == 9 && !g_ascii_strncasecmp (part_name, "PublicKey", part_name_len)) {
2259 if (strlen (key) == 0) {
2260 goto cleanup_and_fail;
2266 if (part_name_len == 12 && !g_ascii_strncasecmp (part_name, "Retargetable", part_name_len)) {
2267 retargetable = value;
2268 retargetable_uq = unquote (retargetable);
2269 if (retargetable_uq != NULL)
2270 retargetable = retargetable_uq;
2272 if (!g_ascii_strcasecmp (retargetable, "yes")) {
2273 flags |= ASSEMBLYREF_RETARGETABLE_FLAG;
2274 } else if (g_ascii_strcasecmp (retargetable, "no")) {
2275 free (retargetable_uq);
2276 goto cleanup_and_fail;
2279 free (retargetable_uq);
2284 if (part_name_len == 21 && !g_ascii_strncasecmp (part_name, "ProcessorArchitecture", part_name_len)) {
2286 procarch_uq = unquote (procarch);
2287 if (procarch_uq != NULL)
2288 procarch = procarch_uq;
2290 if (!g_ascii_strcasecmp (procarch, "MSIL"))
2291 arch = MONO_PROCESSOR_ARCHITECTURE_MSIL;
2292 else if (!g_ascii_strcasecmp (procarch, "X86"))
2293 arch = MONO_PROCESSOR_ARCHITECTURE_X86;
2294 else if (!g_ascii_strcasecmp (procarch, "IA64"))
2295 arch = MONO_PROCESSOR_ARCHITECTURE_IA64;
2296 else if (!g_ascii_strcasecmp (procarch, "AMD64"))
2297 arch = MONO_PROCESSOR_ARCHITECTURE_AMD64;
2300 goto cleanup_and_fail;
2312 /* if retargetable flag is set, then we must have a fully qualified name */
2313 if (retargetable != NULL && (version == NULL || culture == NULL || (key == NULL && token == NULL))) {
2314 goto cleanup_and_fail;
2317 dllname_uq = unquote (dllname);
2318 version_uq = unquote (version);
2319 culture_uq = unquote (culture);
2320 token_uq = unquote (token);
2321 key_uq = unquote (key);
2323 res = build_assembly_name (
2324 dllname_uq == NULL ? dllname : dllname_uq,
2325 version_uq == NULL ? version : version_uq,
2326 culture_uq == NULL ? culture : culture_uq,
2327 token_uq == NULL ? token : token_uq,
2328 key_uq == NULL ? key : key_uq,
2329 flags, arch, aname, save_public_key);
2346 unquote (const char *str)
2354 slen = strlen (str);
2358 if (*str != '\'' && *str != '\"')
2361 end = str + slen - 1;
2365 return g_strndup (str + 1, slen - 2);
2369 * mono_assembly_name_parse:
2370 * @name: name to parse
2371 * @aname: the destination assembly name
2373 * Parses an assembly qualified type name and assigns the name,
2374 * version, culture and token to the provided assembly name object.
2376 * Returns: TRUE if the name could be parsed.
2379 mono_assembly_name_parse (const char *name, MonoAssemblyName *aname)
2381 return mono_assembly_name_parse_full (name, aname, FALSE, NULL, NULL);
2385 * mono_assembly_name_new:
2386 * @name: name to parse
2388 * Allocate a new MonoAssemblyName and fill its values from the
2391 * Returns: a newly allocated structure or NULL if there was any failure.
2394 mono_assembly_name_new (const char *name)
2396 MonoAssemblyName *aname = g_new0 (MonoAssemblyName, 1);
2397 if (mono_assembly_name_parse (name, aname))
2404 mono_assembly_name_get_name (MonoAssemblyName *aname)
2410 mono_assembly_name_get_culture (MonoAssemblyName *aname)
2412 return aname->culture;
2416 mono_assembly_name_get_pubkeytoken (MonoAssemblyName *aname)
2418 if (aname->public_key_token [0])
2419 return aname->public_key_token;
2424 mono_assembly_name_get_version (MonoAssemblyName *aname, uint16_t *minor, uint16_t *build, uint16_t *revision)
2427 *minor = aname->minor;
2429 *build = aname->build;
2431 *revision = aname->revision;
2432 return aname->major;
2435 static MonoAssembly*
2436 probe_for_partial_name (const char *basepath, const char *fullname, MonoAssemblyName *aname, MonoImageOpenStatus *status)
2438 gchar *fullpath = NULL;
2440 const char* direntry;
2441 MonoAssemblyName gac_aname;
2442 gint major=-1, minor=0, build=0, revision=0;
2443 gboolean exact_version;
2445 dirhandle = g_dir_open (basepath, 0, NULL);
2449 exact_version = (aname->major | aname->minor | aname->build | aname->revision) != 0;
2451 while ((direntry = g_dir_read_name (dirhandle))) {
2452 gboolean match = TRUE;
2454 if(!parse_assembly_directory_name (aname->name, direntry, &gac_aname))
2457 if (aname->culture != NULL && strcmp (aname->culture, gac_aname.culture) != 0)
2460 if (match && strlen ((char*)aname->public_key_token) > 0 &&
2461 !mono_public_tokens_are_equal (aname->public_key_token, gac_aname.public_key_token))
2465 if (exact_version) {
2466 match = (aname->major == gac_aname.major && aname->minor == gac_aname.minor &&
2467 aname->build == gac_aname.build && aname->revision == gac_aname.revision);
2469 else if (gac_aname.major < major)
2471 else if (gac_aname.major == major) {
2472 if (gac_aname.minor < minor)
2474 else if (gac_aname.minor == minor) {
2475 if (gac_aname.build < build)
2477 else if (gac_aname.build == build && gac_aname.revision <= revision)
2484 major = gac_aname.major;
2485 minor = gac_aname.minor;
2486 build = gac_aname.build;
2487 revision = gac_aname.revision;
2489 fullpath = g_build_path (G_DIR_SEPARATOR_S, basepath, direntry, fullname, NULL);
2492 mono_assembly_name_free (&gac_aname);
2495 g_dir_close (dirhandle);
2497 if (fullpath == NULL)
2500 MonoAssembly *res = mono_assembly_open (fullpath, status);
2507 * mono_assembly_load_with_partial_name:
2508 * @name: an assembly name that is then parsed by `api:mono_assembly_name_parse`.
2509 * @status: return status code
2511 * Loads a Mono Assembly from a name. The name is parsed using `api:mono_assembly_name_parse`,
2512 * so it might contain a qualified type name, version, culture and token.
2514 * This will load the assembly from the file whose name is derived from the assembly name
2515 * by appending the .dll extension.
2517 * The assembly is loaded from either one of the extra Global Assembly Caches specified
2518 * by the extra GAC paths (specified by the `MONO_GAC_PREFIX` environment variable) or
2519 * if that fails from the GAC.
2521 * Returns: NULL on failure, or a pointer to a MonoAssembly on success.
2524 mono_assembly_load_with_partial_name (const char *name, MonoImageOpenStatus *status)
2528 MonoAssemblyName *aname, base_name;
2529 MonoAssemblyName mapped_aname;
2530 gchar *fullname, *gacpath;
2533 memset (&base_name, 0, sizeof (MonoAssemblyName));
2536 if (!mono_assembly_name_parse (name, aname))
2540 * If no specific version has been requested, make sure we load the
2541 * correct version for system assemblies.
2543 if ((aname->major | aname->minor | aname->build | aname->revision) == 0)
2544 aname = mono_assembly_remap_version (aname, &mapped_aname);
2546 res = mono_assembly_loaded (aname);
2548 mono_assembly_name_free (aname);
2552 res = invoke_assembly_preload_hook (aname, assemblies_path);
2554 res->in_gac = FALSE;
2555 mono_assembly_name_free (aname);
2559 fullname = g_strdup_printf ("%s.dll", aname->name);
2561 if (extra_gac_paths) {
2562 paths = extra_gac_paths;
2563 while (!res && *paths) {
2564 gacpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "lib", "mono", "gac", aname->name, NULL);
2565 res = probe_for_partial_name (gacpath, fullname, aname, status);
2574 mono_assembly_name_free (aname);
2578 gacpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (), "mono", "gac", aname->name, NULL);
2579 res = probe_for_partial_name (gacpath, fullname, aname, status);
2585 MonoDomain *domain = mono_domain_get ();
2586 MonoReflectionAssembly *refasm;
2588 refasm = mono_try_assembly_resolve (domain, mono_string_new (domain, name), NULL, FALSE, &error);
2589 if (!mono_error_ok (&error)) {
2591 mono_assembly_name_free (aname);
2592 mono_error_raise_exception (&error); /* FIXME don't raise here */
2596 res = refasm->assembly;
2600 mono_assembly_name_free (aname);
2606 mono_assembly_is_in_gac (const gchar *filename)
2608 const gchar *rootdir;
2612 if (filename == NULL)
2615 for (paths = extra_gac_paths; paths && *paths; paths++) {
2616 if (strstr (*paths, filename) != *paths)
2619 gp = (gchar *) (filename + strlen (*paths));
2620 if (*gp != G_DIR_SEPARATOR)
2623 if (strncmp (gp, "lib", 3))
2626 if (*gp != G_DIR_SEPARATOR)
2629 if (strncmp (gp, "mono", 4))
2632 if (*gp != G_DIR_SEPARATOR)
2635 if (strncmp (gp, "gac", 3))
2638 if (*gp != G_DIR_SEPARATOR)
2644 rootdir = mono_assembly_getrootdir ();
2645 if (strstr (filename, rootdir) != filename)
2648 gp = (gchar *) (filename + strlen (rootdir));
2649 if (*gp != G_DIR_SEPARATOR)
2652 if (strncmp (gp, "mono", 4))
2655 if (*gp != G_DIR_SEPARATOR)
2658 if (strncmp (gp, "gac", 3))
2661 if (*gp != G_DIR_SEPARATOR)
2667 mono_assembly_load_publisher_policy (MonoAssemblyName *aname)
2670 gchar *filename, *pname, *name, *culture, *version, *fullpath, *subpath;
2674 if (strstr (aname->name, ".dll")) {
2675 len = strlen (aname->name) - 4;
2676 name = (gchar *)g_malloc (len);
2677 strncpy (name, aname->name, len);
2679 name = g_strdup (aname->name);
2682 culture = g_utf8_strdown (aname->culture, -1);
2684 culture = g_strdup ("");
2686 pname = g_strdup_printf ("policy.%d.%d.%s", aname->major, aname->minor, name);
2687 version = g_strdup_printf ("0.0.0.0_%s_%s", culture, aname->public_key_token);
2691 filename = g_strconcat (pname, ".dll", NULL);
2692 subpath = g_build_path (G_DIR_SEPARATOR_S, pname, version, filename, NULL);
2698 if (extra_gac_paths) {
2699 paths = extra_gac_paths;
2700 while (!image && *paths) {
2701 fullpath = g_build_path (G_DIR_SEPARATOR_S, *paths,
2702 "lib", "mono", "gac", subpath, NULL);
2703 image = mono_image_open (fullpath, NULL);
2714 fullpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (),
2715 "mono", "gac", subpath, NULL);
2716 image = mono_image_open (fullpath, NULL);
2723 static MonoAssemblyName*
2724 mono_assembly_bind_version (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname, MonoAssemblyName *dest_name)
2726 memcpy (dest_name, aname, sizeof (MonoAssemblyName));
2727 dest_name->major = info->new_version.major;
2728 dest_name->minor = info->new_version.minor;
2729 dest_name->build = info->new_version.build;
2730 dest_name->revision = info->new_version.revision;
2735 /* LOCKING: assembly_binding lock must be held */
2736 static MonoAssemblyBindingInfo*
2737 search_binding_loaded (MonoAssemblyName *aname)
2741 for (tmp = loaded_assembly_bindings; tmp; tmp = tmp->next) {
2742 MonoAssemblyBindingInfo *info = (MonoAssemblyBindingInfo *)tmp->data;
2743 if (assembly_binding_maps_name (info, aname))
2750 static inline gboolean
2751 info_compare_versions (AssemblyVersionSet *left, AssemblyVersionSet *right)
2753 if (left->major != right->major || left->minor != right->minor ||
2754 left->build != right->build || left->revision != right->revision)
2760 static inline gboolean
2761 info_versions_equal (MonoAssemblyBindingInfo *left, MonoAssemblyBindingInfo *right)
2763 if (left->has_old_version_bottom != right->has_old_version_bottom)
2766 if (left->has_old_version_top != right->has_old_version_top)
2769 if (left->has_new_version != right->has_new_version)
2772 if (left->has_old_version_bottom && !info_compare_versions (&left->old_version_bottom, &right->old_version_bottom))
2775 if (left->has_old_version_top && !info_compare_versions (&left->old_version_top, &right->old_version_top))
2778 if (left->has_new_version && !info_compare_versions (&left->new_version, &right->new_version))
2784 /* LOCKING: assumes all the necessary locks are held */
2786 assembly_binding_info_parsed (MonoAssemblyBindingInfo *info, void *user_data)
2788 MonoAssemblyBindingInfo *info_copy;
2790 MonoAssemblyBindingInfo *info_tmp;
2791 MonoDomain *domain = (MonoDomain*)user_data;
2796 for (tmp = domain->assembly_bindings; tmp; tmp = tmp->next) {
2797 info_tmp = (MonoAssemblyBindingInfo *)tmp->data;
2798 if (strcmp (info->name, info_tmp->name) == 0 && info_versions_equal (info, info_tmp))
2802 info_copy = (MonoAssemblyBindingInfo *)mono_mempool_alloc0 (domain->mp, sizeof (MonoAssemblyBindingInfo));
2803 memcpy (info_copy, info, sizeof (MonoAssemblyBindingInfo));
2805 info_copy->name = mono_mempool_strdup (domain->mp, info->name);
2807 info_copy->culture = mono_mempool_strdup (domain->mp, info->culture);
2809 domain->assembly_bindings = g_slist_append_mempool (domain->mp, domain->assembly_bindings, info_copy);
2813 get_version_number (int major, int minor)
2815 return major * 256 + minor;
2818 static inline gboolean
2819 info_major_minor_in_range (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname)
2821 int aname_version_number = get_version_number (aname->major, aname->minor);
2822 if (!info->has_old_version_bottom)
2825 if (get_version_number (info->old_version_bottom.major, info->old_version_bottom.minor) > aname_version_number)
2828 if (info->has_old_version_top && get_version_number (info->old_version_top.major, info->old_version_top.minor) < aname_version_number)
2831 /* This is not the nicest way to do it, but it's a by-product of the way parsing is done */
2832 info->major = aname->major;
2833 info->minor = aname->minor;
2838 /* LOCKING: Assumes that we are already locked - both loader and domain locks */
2839 static MonoAssemblyBindingInfo*
2840 get_per_domain_assembly_binding_info (MonoDomain *domain, MonoAssemblyName *aname)
2842 MonoAssemblyBindingInfo *info;
2845 if (!domain->assembly_bindings)
2849 for (list = domain->assembly_bindings; list; list = list->next) {
2850 info = (MonoAssemblyBindingInfo *)list->data;
2851 if (info && !strcmp (aname->name, info->name) && info_major_minor_in_range (info, aname))
2857 if (info->name && info->public_key_token [0] && info->has_old_version_bottom &&
2858 info->has_new_version && assembly_binding_maps_name (info, aname))
2859 info->is_valid = TRUE;
2861 info->is_valid = FALSE;
2867 static MonoAssemblyName*
2868 mono_assembly_apply_binding (MonoAssemblyName *aname, MonoAssemblyName *dest_name)
2870 MonoAssemblyBindingInfo *info, *info2;
2874 if (aname->public_key_token [0] == 0)
2877 domain = mono_domain_get ();
2879 mono_assembly_binding_lock ();
2880 info = search_binding_loaded (aname);
2881 mono_assembly_binding_unlock ();
2884 mono_domain_lock (domain);
2885 info = get_per_domain_assembly_binding_info (domain, aname);
2886 mono_domain_unlock (domain);
2890 if (!check_policy_versions (info, aname))
2893 mono_assembly_bind_version (info, aname, dest_name);
2897 if (domain && domain->setup && domain->setup->configuration_file) {
2898 mono_domain_lock (domain);
2899 if (!domain->assembly_bindings_parsed) {
2900 gchar *domain_config_file_name = mono_string_to_utf8 (domain->setup->configuration_file);
2901 gchar *domain_config_file_path = mono_portability_find_file (domain_config_file_name, TRUE);
2903 if (!domain_config_file_path)
2904 domain_config_file_path = domain_config_file_name;
2906 mono_config_parse_assembly_bindings (domain_config_file_path, aname->major, aname->minor, domain, assembly_binding_info_parsed);
2907 domain->assembly_bindings_parsed = TRUE;
2908 if (domain_config_file_name != domain_config_file_path)
2909 g_free (domain_config_file_name);
2910 g_free (domain_config_file_path);
2913 info2 = get_per_domain_assembly_binding_info (domain, aname);
2916 info = (MonoAssemblyBindingInfo *)g_memdup (info2, sizeof (MonoAssemblyBindingInfo));
2917 info->name = g_strdup (info2->name);
2918 info->culture = g_strdup (info2->culture);
2919 info->domain_id = domain->domain_id;
2922 mono_domain_unlock (domain);
2926 info = g_new0 (MonoAssemblyBindingInfo, 1);
2927 info->major = aname->major;
2928 info->minor = aname->minor;
2931 if (!info->is_valid) {
2932 ppimage = mono_assembly_load_publisher_policy (aname);
2934 get_publisher_policy_info (ppimage, aname, info);
2935 mono_image_close (ppimage);
2939 /* Define default error value if needed */
2940 if (!info->is_valid) {
2941 info->name = g_strdup (aname->name);
2942 info->culture = g_strdup (aname->culture);
2943 g_strlcpy ((char *)info->public_key_token, (const char *)aname->public_key_token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
2946 mono_assembly_binding_lock ();
2947 info2 = search_binding_loaded (aname);
2949 /* This binding was added by another thread
2951 mono_assembly_binding_info_free (info);
2956 loaded_assembly_bindings = g_slist_prepend (loaded_assembly_bindings, info);
2958 mono_assembly_binding_unlock ();
2960 if (!info->is_valid || !check_policy_versions (info, aname))
2963 mono_assembly_bind_version (info, aname, dest_name);
2968 * mono_assembly_load_from_gac
2970 * @aname: The assembly name object
2972 static MonoAssembly*
2973 mono_assembly_load_from_gac (MonoAssemblyName *aname, gchar *filename, MonoImageOpenStatus *status, MonoBoolean refonly)
2975 MonoAssembly *result = NULL;
2976 gchar *name, *version, *culture, *fullpath, *subpath;
2981 if (aname->public_key_token [0] == 0) {
2985 if (strstr (aname->name, ".dll")) {
2986 len = strlen (filename) - 4;
2987 name = (gchar *)g_malloc (len);
2988 strncpy (name, aname->name, len);
2990 name = g_strdup (aname->name);
2993 if (aname->culture) {
2994 culture = g_utf8_strdown (aname->culture, -1);
2996 culture = g_strdup ("");
2999 pubtok = g_ascii_strdown ((char*)aname->public_key_token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
3000 version = g_strdup_printf ("%d.%d.%d.%d_%s_%s", aname->major,
3001 aname->minor, aname->build, aname->revision,
3005 subpath = g_build_path (G_DIR_SEPARATOR_S, name, version, filename, NULL);
3010 if (extra_gac_paths) {
3011 paths = extra_gac_paths;
3012 while (!result && *paths) {
3013 fullpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "lib", "mono", "gac", subpath, NULL);
3014 result = mono_assembly_open_full (fullpath, status, refonly);
3021 result->in_gac = TRUE;
3026 fullpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (),
3027 "mono", "gac", subpath, NULL);
3028 result = mono_assembly_open_full (fullpath, status, refonly);
3032 result->in_gac = TRUE;
3040 mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status)
3043 MonoAssemblyName *aname;
3046 /* g_print ("corlib already loaded\n"); */
3050 // In native client, Corlib is embedded in the executable as static variable corlibData
3051 #if defined(__native_client__)
3052 if (corlibData != NULL && corlibSize != 0) {
3054 /* First "FALSE" instructs mono not to make a copy. */
3055 /* Second "FALSE" says this is not just a ref. */
3056 MonoImage* image = mono_image_open_from_data_full (corlibData, corlibSize, FALSE, &status, FALSE);
3057 if (image == NULL || status != 0)
3058 g_print("mono_image_open_from_data_full failed: %d\n", status);
3059 corlib = mono_assembly_load_from_full (image, "mscorlib", &status, FALSE);
3060 if (corlib == NULL || status != 0)
3061 g_print ("mono_assembly_load_from_full failed: %d\n", status);
3067 // A nonstandard preload hook may provide a special mscorlib assembly
3068 aname = mono_assembly_name_new ("mscorlib.dll");
3069 corlib = invoke_assembly_preload_hook (aname, assemblies_path);
3070 mono_assembly_name_free (aname);
3073 goto return_corlib_and_facades;
3075 // This unusual directory layout can occur if mono is being built and run out of its own source repo
3076 if (assemblies_path) { // Custom assemblies path set via MONO_PATH or mono_set_assemblies_path
3077 corlib = load_in_path ("mscorlib.dll", (const char**)assemblies_path, status, FALSE);
3079 goto return_corlib_and_facades;
3082 /* Normal case: Load corlib from mono/<version> */
3083 corlib_file = g_build_filename ("mono", runtime->framework_version, "mscorlib.dll", NULL);
3084 if (assemblies_path) { // Custom assemblies path
3085 corlib = load_in_path (corlib_file, (const char**)assemblies_path, status, FALSE);
3087 g_free (corlib_file);
3088 goto return_corlib_and_facades;
3091 corlib = load_in_path (corlib_file, default_path, status, FALSE);
3092 g_free (corlib_file);
3094 return_corlib_and_facades:
3095 if (corlib && !strcmp (runtime->framework_version, "4.5")) // FIXME: stop hardcoding 4.5 here
3096 default_path [1] = g_strdup_printf ("%s/Facades", corlib->basedir);
3102 mono_assembly_load_full_nosearch (MonoAssemblyName *aname,
3103 const char *basedir,
3104 MonoImageOpenStatus *status,
3107 MonoAssembly *result;
3108 char *fullpath, *filename;
3109 MonoAssemblyName maped_aname;
3110 MonoAssemblyName maped_name_pp;
3115 aname = mono_assembly_remap_version (aname, &maped_aname);
3117 /* Reflection only assemblies don't get assembly binding */
3119 aname = mono_assembly_apply_binding (aname, &maped_name_pp);
3121 result = mono_assembly_loaded_full (aname, refonly);
3125 result = refonly ? invoke_assembly_refonly_preload_hook (aname, assemblies_path) : invoke_assembly_preload_hook (aname, assemblies_path);
3127 result->in_gac = FALSE;
3131 /* Currently we retrieve the loaded corlib for reflection
3132 * only requests, like a common reflection only assembly
3134 if (strcmp (aname->name, "mscorlib") == 0 || strcmp (aname->name, "mscorlib.dll") == 0) {
3135 return mono_assembly_load_corlib (mono_get_runtime_info (), status);
3138 len = strlen (aname->name);
3139 for (ext_index = 0; ext_index < 2; ext_index ++) {
3140 ext = ext_index == 0 ? ".dll" : ".exe";
3141 if (len > 4 && (!strcmp (aname->name + len - 4, ".dll") || !strcmp (aname->name + len - 4, ".exe"))) {
3142 filename = g_strdup (aname->name);
3143 /* Don't try appending .dll/.exe if it already has one of those extensions */
3146 filename = g_strconcat (aname->name, ext, NULL);
3149 result = mono_assembly_load_from_gac (aname, filename, status, refonly);
3156 fullpath = g_build_filename (basedir, filename, NULL);
3157 result = mono_assembly_open_full (fullpath, status, refonly);
3160 result->in_gac = FALSE;
3166 result = load_in_path (filename, default_path, status, refonly);
3168 result->in_gac = FALSE;
3178 mono_assembly_load_full_internal (MonoAssemblyName *aname, MonoAssembly *requesting, const char *basedir, MonoImageOpenStatus *status, gboolean refonly)
3180 MonoAssembly *result = mono_assembly_load_full_nosearch (aname, basedir, status, refonly);
3183 /* Try a postload search hook */
3184 result = mono_assembly_invoke_search_hook_internal (aname, requesting, refonly, TRUE);
3189 * mono_assembly_load_full:
3190 * @aname: A MonoAssemblyName with the assembly name to load.
3191 * @basedir: A directory to look up the assembly at.
3192 * @status: a pointer to a MonoImageOpenStatus to return the status of the load operation
3193 * @refonly: Whether this assembly is being opened in "reflection-only" mode.
3195 * Loads the assembly referenced by @aname, if the value of @basedir is not NULL, it
3196 * attempts to load the assembly from that directory before probing the standard locations.
3198 * If the assembly is being opened in reflection-only mode (@refonly set to TRUE) then no
3199 * assembly binding takes place.
3201 * Returns: the assembly referenced by @aname loaded or NULL on error. On error the
3202 * value pointed by status is updated with an error code.
3205 mono_assembly_load_full (MonoAssemblyName *aname, const char *basedir, MonoImageOpenStatus *status, gboolean refonly)
3207 return mono_assembly_load_full_internal (aname, NULL, basedir, status, refonly);
3211 * mono_assembly_load:
3212 * @aname: A MonoAssemblyName with the assembly name to load.
3213 * @basedir: A directory to look up the assembly at.
3214 * @status: a pointer to a MonoImageOpenStatus to return the status of the load operation
3216 * Loads the assembly referenced by @aname, if the value of @basedir is not NULL, it
3217 * attempts to load the assembly from that directory before probing the standard locations.
3219 * Returns: the assembly referenced by @aname loaded or NULL on error. On error the
3220 * value pointed by status is updated with an error code.
3223 mono_assembly_load (MonoAssemblyName *aname, const char *basedir, MonoImageOpenStatus *status)
3225 return mono_assembly_load_full_internal (aname, NULL, basedir, status, FALSE);
3229 * mono_assembly_loaded_full:
3230 * @aname: an assembly to look for.
3231 * @refonly: Whether this assembly is being opened in "reflection-only" mode.
3233 * This is used to determine if the specified assembly has been loaded
3234 * Returns: NULL If the given @aname assembly has not been loaded, or a pointer to
3235 * a `MonoAssembly` that matches the `MonoAssemblyName` specified.
3238 mono_assembly_loaded_full (MonoAssemblyName *aname, gboolean refonly)
3241 MonoAssemblyName maped_aname;
3243 aname = mono_assembly_remap_version (aname, &maped_aname);
3245 res = mono_assembly_invoke_search_hook_internal (aname, NULL, refonly, FALSE);
3251 * mono_assembly_loaded:
3252 * @aname: an assembly to look for.
3254 * This is used to determine if the specified assembly has been loaded
3256 * Returns: NULL If the given @aname assembly has not been loaded, or a pointer to
3257 * a `MonoAssembly` that matches the `MonoAssemblyName` specified.
3260 mono_assembly_loaded (MonoAssemblyName *aname)
3262 return mono_assembly_loaded_full (aname, FALSE);
3266 mono_assembly_release_gc_roots (MonoAssembly *assembly)
3268 if (assembly == NULL || assembly == REFERENCE_MISSING)
3271 if (assembly_is_dynamic (assembly)) {
3273 MonoDynamicImage *dynimg = (MonoDynamicImage *)assembly->image;
3274 for (i = 0; i < dynimg->image.module_count; ++i)
3275 mono_dynamic_image_release_gc_roots ((MonoDynamicImage *)dynimg->image.modules [i]);
3276 mono_dynamic_image_release_gc_roots (dynimg);
3281 * Returns whether mono_assembly_close_finish() must be called as
3282 * well. See comment for mono_image_close_except_pools() for why we
3283 * unload in two steps.
3286 mono_assembly_close_except_image_pools (MonoAssembly *assembly)
3289 g_return_val_if_fail (assembly != NULL, FALSE);
3291 if (assembly == REFERENCE_MISSING)
3294 /* Might be 0 already */
3295 if (InterlockedDecrement (&assembly->ref_count) > 0)
3298 mono_profiler_assembly_event (assembly, MONO_PROFILE_START_UNLOAD);
3300 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading assembly %s [%p].", assembly->aname.name, assembly);
3302 mono_debug_close_image (assembly->image);
3304 mono_assemblies_lock ();
3305 loaded_assemblies = g_list_remove (loaded_assemblies, assembly);
3306 mono_assemblies_unlock ();
3308 assembly->image->assembly = NULL;
3310 if (!mono_image_close_except_pools (assembly->image))
3311 assembly->image = NULL;
3313 for (tmp = assembly->friend_assembly_names; tmp; tmp = tmp->next) {
3314 MonoAssemblyName *fname = (MonoAssemblyName *)tmp->data;
3315 mono_assembly_name_free (fname);
3318 g_slist_free (assembly->friend_assembly_names);
3319 g_free (assembly->basedir);
3321 mono_profiler_assembly_event (assembly, MONO_PROFILE_END_UNLOAD);
3327 mono_assembly_close_finish (MonoAssembly *assembly)
3329 g_assert (assembly && assembly != REFERENCE_MISSING);
3331 if (assembly->image)
3332 mono_image_close_finish (assembly->image);
3334 if (assembly_is_dynamic (assembly)) {
3335 g_free ((char*)assembly->aname.culture);
3342 * mono_assembly_close:
3343 * @assembly: the assembly to release.
3345 * This method releases a reference to the @assembly. The assembly is
3346 * only released when all the outstanding references to it are released.
3349 mono_assembly_close (MonoAssembly *assembly)
3351 if (mono_assembly_close_except_image_pools (assembly))
3352 mono_assembly_close_finish (assembly);
3356 mono_assembly_load_module (MonoAssembly *assembly, guint32 idx)
3358 return mono_image_load_file_for_image (assembly->image, idx);
3362 * mono_assembly_foreach:
3363 * @func: function to invoke for each assembly loaded
3364 * @user_data: data passed to the callback
3366 * Invokes the provided @func callback for each assembly loaded into
3367 * the runtime. The first parameter passed to the callback is the
3368 * `MonoAssembly*`, and the second parameter is the @user_data.
3370 * This is done for all assemblies loaded in the runtime, not just
3371 * those loaded in the current application domain.
3374 mono_assembly_foreach (GFunc func, gpointer user_data)
3379 * We make a copy of the list to avoid calling the callback inside the
3380 * lock, which could lead to deadlocks.
3382 mono_assemblies_lock ();
3383 copy = g_list_copy (loaded_assemblies);
3384 mono_assemblies_unlock ();
3386 g_list_foreach (loaded_assemblies, func, user_data);
3392 * mono_assemblies_cleanup:
3394 * Free all resources used by this module.
3397 mono_assemblies_cleanup (void)
3401 mono_os_mutex_destroy (&assemblies_mutex);
3402 mono_os_mutex_destroy (&assembly_binding_mutex);
3404 for (l = loaded_assembly_bindings; l; l = l->next) {
3405 MonoAssemblyBindingInfo *info = (MonoAssemblyBindingInfo *)l->data;
3407 mono_assembly_binding_info_free (info);
3410 g_slist_free (loaded_assembly_bindings);
3412 free_assembly_load_hooks ();
3413 free_assembly_search_hooks ();
3414 free_assembly_preload_hooks ();
3417 /*LOCKING takes the assembly_binding lock*/
3419 mono_assembly_cleanup_domain_bindings (guint32 domain_id)
3423 mono_assembly_binding_lock ();
3424 iter = &loaded_assembly_bindings;
3427 MonoAssemblyBindingInfo *info = (MonoAssemblyBindingInfo *)l->data;
3429 if (info->domain_id == domain_id) {
3431 mono_assembly_binding_info_free (info);
3438 mono_assembly_binding_unlock ();
3442 * Holds the assembly of the application, for
3443 * System.Diagnostics.Process::MainModule
3445 static MonoAssembly *main_assembly=NULL;
3448 mono_assembly_set_main (MonoAssembly *assembly)
3450 main_assembly = assembly;
3454 * mono_assembly_get_main:
3456 * Returns: the assembly for the application, the first assembly that is loaded by the VM
3459 mono_assembly_get_main (void)
3461 return (main_assembly);
3465 * mono_assembly_get_image:
3466 * @assembly: The assembly to retrieve the image from
3468 * Returns: the MonoImage associated with this assembly.
3471 mono_assembly_get_image (MonoAssembly *assembly)
3473 return assembly->image;
3477 * mono_assembly_get_name:
3478 * @assembly: The assembly to retrieve the name from
3480 * The returned name's lifetime is the same as @assembly's.
3482 * Returns: the MonoAssemblyName associated with this assembly.
3485 mono_assembly_get_name (MonoAssembly *assembly)
3487 return &assembly->aname;
3491 mono_register_bundled_assemblies (const MonoBundledAssembly **assemblies)
3493 bundles = assemblies;
3496 #define MONO_DECLSEC_FORMAT_10 0x3C
3497 #define MONO_DECLSEC_FORMAT_20 0x2E
3498 #define MONO_DECLSEC_FIELD 0x53
3499 #define MONO_DECLSEC_PROPERTY 0x54
3501 #define SKIP_VISIBILITY_XML_ATTRIBUTE ("\"SkipVerification\"")
3502 #define SKIP_VISIBILITY_ATTRIBUTE_NAME ("System.Security.Permissions.SecurityPermissionAttribute")
3503 #define SKIP_VISIBILITY_ATTRIBUTE_SIZE (sizeof (SKIP_VISIBILITY_ATTRIBUTE_NAME) - 1)
3504 #define SKIP_VISIBILITY_PROPERTY_NAME ("SkipVerification")
3505 #define SKIP_VISIBILITY_PROPERTY_SIZE (sizeof (SKIP_VISIBILITY_PROPERTY_NAME) - 1)
3508 mono_assembly_try_decode_skip_verification_param (const char *p, const char **resp, gboolean *abort_decoding)
3512 case MONO_DECLSEC_PROPERTY:
3514 case MONO_DECLSEC_FIELD:
3516 *abort_decoding = TRUE;
3521 if (*p++ != MONO_TYPE_BOOLEAN) {
3522 *abort_decoding = TRUE;
3526 /* property name length */
3527 len = mono_metadata_decode_value (p, &p);
3529 if (len >= SKIP_VISIBILITY_PROPERTY_SIZE && !memcmp (p, SKIP_VISIBILITY_PROPERTY_NAME, SKIP_VISIBILITY_PROPERTY_SIZE)) {
3540 mono_assembly_try_decode_skip_verification (const char *p, const char *endn)
3542 int i, j, num, len, params_len;
3544 if (*p == MONO_DECLSEC_FORMAT_10) {
3545 gsize read, written;
3546 char *res = g_convert (p, endn - p, "UTF-8", "UTF-16LE", &read, &written, NULL);
3548 gboolean found = strstr (res, SKIP_VISIBILITY_XML_ATTRIBUTE) != NULL;
3554 if (*p++ != MONO_DECLSEC_FORMAT_20)
3557 /* number of encoded permission attributes */
3558 num = mono_metadata_decode_value (p, &p);
3559 for (i = 0; i < num; ++i) {
3560 gboolean is_valid = FALSE;
3561 gboolean abort_decoding = FALSE;
3563 /* attribute name length */
3564 len = mono_metadata_decode_value (p, &p);
3566 /* We don't really need to fully decode the type. Comparing the name is enough */
3567 is_valid = len >= SKIP_VISIBILITY_ATTRIBUTE_SIZE && !memcmp (p, SKIP_VISIBILITY_ATTRIBUTE_NAME, SKIP_VISIBILITY_ATTRIBUTE_SIZE);
3571 /*size of the params table*/
3572 params_len = mono_metadata_decode_value (p, &p);
3574 const char *params_end = p + params_len;
3576 /* number of parameters */
3577 len = mono_metadata_decode_value (p, &p);
3579 for (j = 0; j < len; ++j) {
3580 if (mono_assembly_try_decode_skip_verification_param (p, &p, &abort_decoding))
3596 mono_assembly_has_skip_verification (MonoAssembly *assembly)
3599 guint32 cols [MONO_DECL_SECURITY_SIZE];
3603 if (MONO_SECMAN_FLAG_INIT (assembly->skipverification))
3604 return MONO_SECMAN_FLAG_GET_VALUE (assembly->skipverification);
3606 t = &assembly->image->tables [MONO_TABLE_DECLSECURITY];
3608 for (i = 0; i < t->rows; ++i) {
3609 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
3610 if ((cols [MONO_DECL_SECURITY_PARENT] & MONO_HAS_DECL_SECURITY_MASK) != MONO_HAS_DECL_SECURITY_ASSEMBLY)
3612 if (cols [MONO_DECL_SECURITY_ACTION] != SECURITY_ACTION_REQMIN)
3615 blob = mono_metadata_blob_heap (assembly->image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
3616 len = mono_metadata_decode_blob_size (blob, &blob);
3620 if (mono_assembly_try_decode_skip_verification (blob, blob + len)) {
3621 MONO_SECMAN_FLAG_SET_VALUE (assembly->skipverification, TRUE);
3626 MONO_SECMAN_FLAG_SET_VALUE (assembly->skipverification, FALSE);