PageRenderTime 67ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/mono/metadata/assembly.c

https://bitbucket.org/danipen/mono
C | 3268 lines | 2365 code | 507 blank | 396 comment | 658 complexity | 6d695da8da7d53c9e558f3341855ae41 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. /*
  2. * assembly.c: Routines for loading assemblies.
  3. *
  4. * Author:
  5. * Miguel de Icaza (miguel@ximian.com)
  6. *
  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)
  10. */
  11. #include <config.h>
  12. #include <stdio.h>
  13. #include <glib.h>
  14. #include <errno.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "assembly.h"
  18. #include "image.h"
  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/mono-endian.h>
  27. #include <mono/metadata/mono-debug.h>
  28. #include <mono/io-layer/io-layer.h>
  29. #include <mono/utils/mono-uri.h>
  30. #include <mono/metadata/mono-config.h>
  31. #include <mono/utils/mono-digest.h>
  32. #include <mono/utils/mono-logger-internal.h>
  33. #include <mono/utils/mono-path.h>
  34. #include <mono/metadata/reflection.h>
  35. #include <mono/metadata/coree.h>
  36. #include <mono/utils/mono-io-portability.h>
  37. #ifndef HOST_WIN32
  38. #include <sys/types.h>
  39. #include <unistd.h>
  40. #include <sys/stat.h>
  41. #endif
  42. #ifdef PLATFORM_MACOSX
  43. #include <mach-o/dyld.h>
  44. #endif
  45. /* AssemblyVersionMap: an assembly name and the assembly version set on which it is based */
  46. typedef struct {
  47. const char* assembly_name;
  48. guint8 version_set_index;
  49. } AssemblyVersionMap;
  50. /* the default search path is empty, the first slot is replaced with the computed value */
  51. static const char*
  52. default_path [] = {
  53. NULL,
  54. NULL
  55. };
  56. /* Contains the list of directories to be searched for assemblies (MONO_PATH) */
  57. static char **assemblies_path = NULL;
  58. /* Contains the list of directories that point to auxiliary GACs */
  59. static char **extra_gac_paths = NULL;
  60. #ifndef DISABLE_ASSEMBLY_REMAPPING
  61. /* The list of system assemblies what will be remapped to the running
  62. * runtime version. WARNING: this list must be sorted.
  63. * The integer number is an index in the MonoRuntimeInfo structure, whose
  64. * values can be found in domain.c - supported_runtimes. Look there
  65. * to understand what remapping will be made.
  66. */
  67. static const AssemblyVersionMap framework_assemblies [] = {
  68. {"Accessibility", 0},
  69. {"Commons.Xml.Relaxng", 0},
  70. {"I18N", 0},
  71. {"I18N.CJK", 0},
  72. {"I18N.MidEast", 0},
  73. {"I18N.Other", 0},
  74. {"I18N.Rare", 0},
  75. {"I18N.West", 0},
  76. {"Microsoft.VisualBasic", 1},
  77. {"Microsoft.VisualC", 1},
  78. {"Mono.Cairo", 0},
  79. {"Mono.CompilerServices.SymbolWriter", 0},
  80. {"Mono.Data", 0},
  81. {"Mono.Data.SybaseClient", 0},
  82. {"Mono.Data.Tds", 0},
  83. {"Mono.Data.TdsClient", 0},
  84. {"Mono.GetOptions", 0},
  85. {"Mono.Http", 0},
  86. {"Mono.Posix", 0},
  87. {"Mono.Security", 0},
  88. {"Mono.Security.Win32", 0},
  89. {"Mono.Xml.Ext", 0},
  90. {"Novell.Directory.Ldap", 0},
  91. {"Npgsql", 0},
  92. {"PEAPI", 0},
  93. {"System", 0},
  94. {"System.ComponentModel.DataAnnotations", 2},
  95. {"System.Configuration", 0},
  96. {"System.Configuration.Install", 0},
  97. {"System.Core", 2},
  98. {"System.Data", 0},
  99. {"System.Data.Linq", 2},
  100. {"System.Data.OracleClient", 0},
  101. {"System.Data.SqlXml", 0},
  102. {"System.Design", 0},
  103. {"System.DirectoryServices", 0},
  104. {"System.Drawing", 0},
  105. {"System.Drawing.Design", 0},
  106. {"System.EnterpriseServices", 0},
  107. {"System.Management", 0},
  108. {"System.Messaging", 0},
  109. {"System.Runtime.Remoting", 0},
  110. {"System.Runtime.Serialization", 3},
  111. {"System.Runtime.Serialization.Formatters.Soap", 0},
  112. {"System.Security", 0},
  113. {"System.ServiceProcess", 0},
  114. {"System.Transactions", 0},
  115. {"System.Web", 0},
  116. {"System.Web.Abstractions", 2},
  117. {"System.Web.Mobile", 0},
  118. {"System.Web.Routing", 2},
  119. {"System.Web.Services", 0},
  120. {"System.Windows.Forms", 0},
  121. {"System.Xml", 0},
  122. {"mscorlib", 0}
  123. };
  124. #endif
  125. /*
  126. * keeps track of loaded assemblies
  127. */
  128. static GList *loaded_assemblies = NULL;
  129. static MonoAssembly *corlib;
  130. #if defined(__native_client__)
  131. /* On Native Client, allow mscorlib to be loaded from memory */
  132. /* instead of loaded off disk. If these are not set, default */
  133. /* mscorlib loading will take place */
  134. /* NOTE: If mscorlib data is passed to mono in this way then */
  135. /* it needs to remain allocated during the use of mono. */
  136. static void *corlibData = NULL;
  137. static size_t corlibSize = 0;
  138. void
  139. mono_set_corlib_data (void *data, size_t size)
  140. {
  141. corlibData = data;
  142. corlibSize = size;
  143. }
  144. #endif
  145. /* This protects loaded_assemblies and image->references */
  146. #define mono_assemblies_lock() EnterCriticalSection (&assemblies_mutex)
  147. #define mono_assemblies_unlock() LeaveCriticalSection (&assemblies_mutex)
  148. static CRITICAL_SECTION assemblies_mutex;
  149. /* If defined, points to the bundled assembly information */
  150. const MonoBundledAssembly **bundles;
  151. /* Loaded assembly binding info */
  152. static GSList *loaded_assembly_bindings = NULL;
  153. static MonoAssembly*
  154. mono_assembly_invoke_search_hook_internal (MonoAssemblyName *aname, gboolean refonly, gboolean postload);
  155. static MonoBoolean
  156. mono_assembly_is_in_gac (const gchar *filanem);
  157. static gchar*
  158. encode_public_tok (const guchar *token, gint32 len)
  159. {
  160. const static gchar allowed [] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  161. gchar *res;
  162. int i;
  163. res = g_malloc (len * 2 + 1);
  164. for (i = 0; i < len; i++) {
  165. res [i * 2] = allowed [token [i] >> 4];
  166. res [i * 2 + 1] = allowed [token [i] & 0xF];
  167. }
  168. res [len * 2] = 0;
  169. return res;
  170. }
  171. /**
  172. * mono_public_tokens_are_equal:
  173. * @pubt1: first public key token
  174. * @pubt2: second public key token
  175. *
  176. * Compare two public key tokens and return #TRUE is they are equal and #FALSE
  177. * otherwise.
  178. */
  179. gboolean
  180. mono_public_tokens_are_equal (const unsigned char *pubt1, const unsigned char *pubt2)
  181. {
  182. return memcmp (pubt1, pubt2, 16) == 0;
  183. }
  184. /**
  185. * mono_set_assemblies_path:
  186. * @path: list of paths that contain directories where Mono will look for assemblies
  187. *
  188. * Use this method to override the standard assembly lookup system and
  189. * override any assemblies coming from the GAC. This is the method
  190. * that supports the MONO_PATH variable.
  191. *
  192. * Notice that MONO_PATH and this method are really a very bad idea as
  193. * it prevents the GAC from working and it prevents the standard
  194. * resolution mechanisms from working. Nonetheless, for some debugging
  195. * situations and bootstrapping setups, this is useful to have.
  196. */
  197. void
  198. mono_set_assemblies_path (const char* path)
  199. {
  200. char **splitted, **dest;
  201. splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
  202. if (assemblies_path)
  203. g_strfreev (assemblies_path);
  204. assemblies_path = dest = splitted;
  205. while (*splitted){
  206. if (**splitted)
  207. *dest++ = *splitted;
  208. splitted++;
  209. }
  210. *dest = *splitted;
  211. if (g_getenv ("MONO_DEBUG") == NULL)
  212. return;
  213. splitted = assemblies_path;
  214. while (*splitted) {
  215. if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
  216. g_warning ("'%s' in MONO_PATH doesn't exist or has wrong permissions.", *splitted);
  217. splitted++;
  218. }
  219. }
  220. /* Native Client can't get this info from an environment variable so */
  221. /* it's passed in to the runtime, or set manually by embedding code. */
  222. #ifdef __native_client__
  223. char* nacl_mono_path = NULL;
  224. #endif
  225. static void
  226. check_path_env (void)
  227. {
  228. const char* path;
  229. #ifdef __native_client__
  230. path = nacl_mono_path;
  231. #else
  232. path = g_getenv ("MONO_PATH");
  233. #endif
  234. if (!path || assemblies_path != NULL)
  235. return;
  236. mono_set_assemblies_path(path);
  237. }
  238. static void
  239. check_extra_gac_path_env (void) {
  240. const char *path;
  241. char **splitted, **dest;
  242. path = g_getenv ("MONO_GAC_PREFIX");
  243. if (!path)
  244. return;
  245. splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
  246. if (extra_gac_paths)
  247. g_strfreev (extra_gac_paths);
  248. extra_gac_paths = dest = splitted;
  249. while (*splitted){
  250. if (**splitted)
  251. *dest++ = *splitted;
  252. splitted++;
  253. }
  254. *dest = *splitted;
  255. if (g_getenv ("MONO_DEBUG") == NULL)
  256. return;
  257. while (*splitted) {
  258. if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
  259. g_warning ("'%s' in MONO_GAC_PREFIX doesn't exist or has wrong permissions.", *splitted);
  260. splitted++;
  261. }
  262. }
  263. static gboolean
  264. assembly_binding_maps_name (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname)
  265. {
  266. if (!info || !info->name)
  267. return FALSE;
  268. if (strcmp (info->name, aname->name))
  269. return FALSE;
  270. if (info->major != aname->major || info->minor != aname->minor)
  271. return FALSE;
  272. if ((info->culture != NULL && info->culture [0]) != (aname->culture != NULL && aname->culture [0]))
  273. return FALSE;
  274. if (info->culture && aname->culture && strcmp (info->culture, aname->culture))
  275. return FALSE;
  276. if (!mono_public_tokens_are_equal (info->public_key_token, aname->public_key_token))
  277. return FALSE;
  278. return TRUE;
  279. }
  280. static void
  281. mono_assembly_binding_info_free (MonoAssemblyBindingInfo *info)
  282. {
  283. if (!info)
  284. return;
  285. g_free (info->name);
  286. g_free (info->culture);
  287. }
  288. static void
  289. get_publisher_policy_info (MonoImage *image, MonoAssemblyName *aname, MonoAssemblyBindingInfo *binding_info)
  290. {
  291. MonoTableInfo *t;
  292. guint32 cols [MONO_MANIFEST_SIZE];
  293. const gchar *filename;
  294. gchar *subpath, *fullpath;
  295. t = &image->tables [MONO_TABLE_MANIFESTRESOURCE];
  296. /* MS Impl. accepts policy assemblies with more than
  297. * one manifest resource, and only takes the first one */
  298. if (t->rows < 1) {
  299. binding_info->is_valid = FALSE;
  300. return;
  301. }
  302. mono_metadata_decode_row (t, 0, cols, MONO_MANIFEST_SIZE);
  303. if ((cols [MONO_MANIFEST_IMPLEMENTATION] & MONO_IMPLEMENTATION_MASK) != MONO_IMPLEMENTATION_FILE) {
  304. binding_info->is_valid = FALSE;
  305. return;
  306. }
  307. filename = mono_metadata_string_heap (image, cols [MONO_MANIFEST_NAME]);
  308. g_assert (filename != NULL);
  309. subpath = g_path_get_dirname (image->name);
  310. fullpath = g_build_path (G_DIR_SEPARATOR_S, subpath, filename, NULL);
  311. mono_config_parse_publisher_policy (fullpath, binding_info);
  312. g_free (subpath);
  313. g_free (fullpath);
  314. /* Define the optional elements/attributes before checking */
  315. if (!binding_info->culture)
  316. binding_info->culture = g_strdup ("");
  317. /* Check that the most important elements/attributes exist */
  318. if (!binding_info->name || !binding_info->public_key_token [0] || !binding_info->has_old_version_bottom ||
  319. !binding_info->has_new_version || !assembly_binding_maps_name (binding_info, aname)) {
  320. mono_assembly_binding_info_free (binding_info);
  321. binding_info->is_valid = FALSE;
  322. return;
  323. }
  324. binding_info->is_valid = TRUE;
  325. }
  326. static int
  327. compare_versions (AssemblyVersionSet *v, MonoAssemblyName *aname)
  328. {
  329. if (v->major > aname->major)
  330. return 1;
  331. else if (v->major < aname->major)
  332. return -1;
  333. if (v->minor > aname->minor)
  334. return 1;
  335. else if (v->minor < aname->minor)
  336. return -1;
  337. if (v->build > aname->build)
  338. return 1;
  339. else if (v->build < aname->build)
  340. return -1;
  341. if (v->revision > aname->revision)
  342. return 1;
  343. else if (v->revision < aname->revision)
  344. return -1;
  345. return 0;
  346. }
  347. static gboolean
  348. check_policy_versions (MonoAssemblyBindingInfo *info, MonoAssemblyName *name)
  349. {
  350. if (!info->is_valid)
  351. return FALSE;
  352. /* If has_old_version_top doesn't exist, we don't have an interval */
  353. if (!info->has_old_version_top) {
  354. if (compare_versions (&info->old_version_bottom, name) == 0)
  355. return TRUE;
  356. return FALSE;
  357. }
  358. /* Check that the version defined by name is valid for the interval */
  359. if (compare_versions (&info->old_version_top, name) < 0)
  360. return FALSE;
  361. /* We should be greater or equal than the small version */
  362. if (compare_versions (&info->old_version_bottom, name) > 0)
  363. return FALSE;
  364. return TRUE;
  365. }
  366. /**
  367. * mono_assembly_names_equal:
  368. * @l: first assembly
  369. * @r: second assembly.
  370. *
  371. * Compares two MonoAssemblyNames and returns whether they are equal.
  372. *
  373. * This compares the names, the cultures, the release version and their
  374. * public tokens.
  375. *
  376. * Returns: TRUE if both assembly names are equal.
  377. */
  378. gboolean
  379. mono_assembly_names_equal (MonoAssemblyName *l, MonoAssemblyName *r)
  380. {
  381. if (!l->name || !r->name)
  382. return FALSE;
  383. if (strcmp (l->name, r->name))
  384. return FALSE;
  385. if (l->culture && r->culture && strcmp (l->culture, r->culture))
  386. return FALSE;
  387. if (l->major != r->major || l->minor != r->minor ||
  388. l->build != r->build || l->revision != r->revision)
  389. 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)))
  390. return FALSE;
  391. if (!l->public_key_token [0] || !r->public_key_token [0])
  392. return TRUE;
  393. if (!mono_public_tokens_are_equal (l->public_key_token, r->public_key_token))
  394. return FALSE;
  395. return TRUE;
  396. }
  397. static MonoAssembly *
  398. load_in_path (const char *basename, const char** search_path, MonoImageOpenStatus *status, MonoBoolean refonly)
  399. {
  400. int i;
  401. char *fullpath;
  402. MonoAssembly *result;
  403. for (i = 0; search_path [i]; ++i) {
  404. fullpath = g_build_filename (search_path [i], basename, NULL);
  405. result = mono_assembly_open_full (fullpath, status, refonly);
  406. g_free (fullpath);
  407. if (result)
  408. return result;
  409. }
  410. return NULL;
  411. }
  412. /**
  413. * mono_assembly_setrootdir:
  414. * @root_dir: The pathname of the root directory where we will locate assemblies
  415. *
  416. * This routine sets the internal default root directory for looking up
  417. * assemblies.
  418. *
  419. * This is used by Windows installations to compute dynamically the
  420. * place where the Mono assemblies are located.
  421. *
  422. */
  423. void
  424. mono_assembly_setrootdir (const char *root_dir)
  425. {
  426. /*
  427. * Override the MONO_ASSEMBLIES directory configured at compile time.
  428. */
  429. /* Leak if called more than once */
  430. default_path [0] = g_strdup (root_dir);
  431. }
  432. /**
  433. * mono_assembly_getrootdir:
  434. *
  435. * Obtains the root directory used for looking up assemblies.
  436. *
  437. * Returns: a string with the directory, this string should not be freed.
  438. */
  439. G_CONST_RETURN gchar *
  440. mono_assembly_getrootdir (void)
  441. {
  442. return default_path [0];
  443. }
  444. /**
  445. * mono_set_dirs:
  446. * @assembly_dir: the base directory for assemblies
  447. * @config_dir: the base directory for configuration files
  448. *
  449. * This routine is used internally and by developers embedding
  450. * the runtime into their own applications.
  451. *
  452. * There are a number of cases to consider: Mono as a system-installed
  453. * package that is available on the location preconfigured or Mono in
  454. * a relocated location.
  455. *
  456. * If you are using a system-installed Mono, you can pass NULL
  457. * to both parameters. If you are not, you should compute both
  458. * directory values and call this routine.
  459. *
  460. * The values for a given PREFIX are:
  461. *
  462. * assembly_dir: PREFIX/lib
  463. * config_dir: PREFIX/etc
  464. *
  465. * Notice that embedders that use Mono in a relocated way must
  466. * compute the location at runtime, as they will be in control
  467. * of where Mono is installed.
  468. */
  469. void
  470. mono_set_dirs (const char *assembly_dir, const char *config_dir)
  471. {
  472. #if defined (MONO_ASSEMBLIES)
  473. if (assembly_dir == NULL)
  474. assembly_dir = MONO_ASSEMBLIES;
  475. #endif
  476. #if defined (MONO_CFG_DIR)
  477. if (config_dir == NULL)
  478. config_dir = MONO_CFG_DIR;
  479. #endif
  480. mono_assembly_setrootdir (assembly_dir);
  481. mono_set_config_dir (config_dir);
  482. }
  483. #ifndef HOST_WIN32
  484. static char *
  485. compute_base (char *path)
  486. {
  487. char *p = strrchr (path, '/');
  488. if (p == NULL)
  489. return NULL;
  490. /* Not a well known Mono executable, we are embedded, cant guess the base */
  491. if (strcmp (p, "/mono") && strcmp (p, "/mono-sgen") && strcmp (p, "/pedump") && strcmp (p, "/monodis") && strcmp (p, "/mint") && strcmp (p, "/monodiet"))
  492. return NULL;
  493. *p = 0;
  494. p = strrchr (path, '/');
  495. if (p == NULL)
  496. return NULL;
  497. if (strcmp (p, "/bin") != 0)
  498. return NULL;
  499. *p = 0;
  500. return path;
  501. }
  502. static void
  503. fallback (void)
  504. {
  505. mono_set_dirs (MONO_ASSEMBLIES, MONO_CFG_DIR);
  506. }
  507. static G_GNUC_UNUSED void
  508. set_dirs (char *exe)
  509. {
  510. char *base;
  511. char *config, *lib, *mono;
  512. struct stat buf;
  513. /*
  514. * Only /usr prefix is treated specially
  515. */
  516. if (strncmp (exe, MONO_BINDIR, strlen (MONO_BINDIR)) == 0 || (base = compute_base (exe)) == NULL){
  517. fallback ();
  518. return;
  519. }
  520. config = g_build_filename (base, "etc", NULL);
  521. lib = g_build_filename (base, "lib", NULL);
  522. mono = g_build_filename (lib, "mono/2.0", NULL);
  523. if (stat (mono, &buf) == -1)
  524. fallback ();
  525. else {
  526. mono_set_dirs (lib, config);
  527. }
  528. g_free (config);
  529. g_free (lib);
  530. g_free (mono);
  531. }
  532. #endif /* HOST_WIN32 */
  533. /**
  534. * mono_set_rootdir:
  535. *
  536. * Registers the root directory for the Mono runtime, for Linux and Solaris 10,
  537. * this auto-detects the prefix where Mono was installed.
  538. */
  539. void
  540. mono_set_rootdir (void)
  541. {
  542. #if defined(HOST_WIN32) || (defined(PLATFORM_MACOSX) && !defined(TARGET_ARM))
  543. gchar *bindir, *installdir, *root, *name, *resolvedname, *config;
  544. #ifdef HOST_WIN32
  545. name = mono_get_module_file_name ((HMODULE) &__ImageBase);
  546. #else
  547. {
  548. /*
  549. * _NSGetExecutablePath may return -1 to indicate buf is not large
  550. * enough, but we ignore that case to avoid having to do extra dynamic
  551. * allocation for the path and hope that 4096 is enough - this is
  552. * ok in the Linux/Solaris case below at least...
  553. */
  554. gchar buf[4096];
  555. guint buf_size = sizeof (buf);
  556. if (_NSGetExecutablePath (buf, &buf_size) == 0)
  557. name = g_strdup (buf);
  558. if (name == NULL) {
  559. fallback ();
  560. return;
  561. }
  562. }
  563. #endif
  564. resolvedname = mono_path_resolve_symlinks (name);
  565. bindir = g_path_get_dirname (resolvedname);
  566. installdir = g_path_get_dirname (bindir);
  567. root = g_build_path (G_DIR_SEPARATOR_S, installdir, "lib", NULL);
  568. config = g_build_filename (root, "..", "etc", NULL);
  569. #ifdef HOST_WIN32
  570. mono_set_dirs (root, config);
  571. #else
  572. if (g_file_test (root, G_FILE_TEST_EXISTS) && g_file_test (config, G_FILE_TEST_EXISTS))
  573. mono_set_dirs (root, config);
  574. else
  575. fallback ();
  576. #endif
  577. g_free (config);
  578. g_free (root);
  579. g_free (installdir);
  580. g_free (bindir);
  581. g_free (name);
  582. g_free (resolvedname);
  583. #elif defined(DISABLE_MONO_AUTODETECTION)
  584. fallback ();
  585. #else
  586. char buf [4096];
  587. int s;
  588. char *str;
  589. /* Linux style */
  590. s = readlink ("/proc/self/exe", buf, sizeof (buf)-1);
  591. if (s != -1){
  592. buf [s] = 0;
  593. set_dirs (buf);
  594. return;
  595. }
  596. /* Solaris 10 style */
  597. str = g_strdup_printf ("/proc/%d/path/a.out", getpid ());
  598. s = readlink (str, buf, sizeof (buf)-1);
  599. g_free (str);
  600. if (s != -1){
  601. buf [s] = 0;
  602. set_dirs (buf);
  603. return;
  604. }
  605. fallback ();
  606. #endif
  607. }
  608. /**
  609. * mono_assemblies_init:
  610. *
  611. * Initialize global variables used by this module.
  612. */
  613. void
  614. mono_assemblies_init (void)
  615. {
  616. /*
  617. * Initialize our internal paths if we have not been initialized yet.
  618. * This happens when embedders use Mono.
  619. */
  620. if (mono_assembly_getrootdir () == NULL)
  621. mono_set_rootdir ();
  622. check_path_env ();
  623. check_extra_gac_path_env ();
  624. InitializeCriticalSection (&assemblies_mutex);
  625. }
  626. gboolean
  627. mono_assembly_fill_assembly_name (MonoImage *image, MonoAssemblyName *aname)
  628. {
  629. MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
  630. guint32 cols [MONO_ASSEMBLY_SIZE];
  631. if (!t->rows)
  632. return FALSE;
  633. mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
  634. aname->hash_len = 0;
  635. aname->hash_value = NULL;
  636. aname->name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_NAME]);
  637. aname->culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLY_CULTURE]);
  638. aname->flags = cols [MONO_ASSEMBLY_FLAGS];
  639. aname->major = cols [MONO_ASSEMBLY_MAJOR_VERSION];
  640. aname->minor = cols [MONO_ASSEMBLY_MINOR_VERSION];
  641. aname->build = cols [MONO_ASSEMBLY_BUILD_NUMBER];
  642. aname->revision = cols [MONO_ASSEMBLY_REV_NUMBER];
  643. aname->hash_alg = cols [MONO_ASSEMBLY_HASH_ALG];
  644. if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
  645. guchar* token = g_malloc (8);
  646. gchar* encoded;
  647. const gchar* pkey;
  648. int len;
  649. pkey = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
  650. len = mono_metadata_decode_blob_size (pkey, &pkey);
  651. aname->public_key = (guchar*)pkey;
  652. mono_digest_get_public_token (token, aname->public_key, len);
  653. encoded = encode_public_tok (token, 8);
  654. g_strlcpy ((char*)aname->public_key_token, encoded, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  655. g_free (encoded);
  656. g_free (token);
  657. }
  658. else {
  659. aname->public_key = NULL;
  660. memset (aname->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  661. }
  662. if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
  663. aname->public_key = (guchar*)mono_metadata_blob_heap (image, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
  664. }
  665. else
  666. aname->public_key = 0;
  667. return TRUE;
  668. }
  669. /**
  670. * mono_stringify_assembly_name:
  671. * @aname: the assembly name.
  672. *
  673. * Convert @aname into its string format. The returned string is dynamically
  674. * allocated and should be freed by the caller.
  675. *
  676. * Returns: a newly allocated string with a string representation of
  677. * the assembly name.
  678. */
  679. char*
  680. mono_stringify_assembly_name (MonoAssemblyName *aname)
  681. {
  682. const char *quote = (aname->name && g_ascii_isspace (aname->name [0])) ? "\"" : "";
  683. return g_strdup_printf (
  684. "%s%s%s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
  685. quote, aname->name, quote,
  686. aname->major, aname->minor, aname->build, aname->revision,
  687. aname->culture && *aname->culture? aname->culture: "neutral",
  688. aname->public_key_token [0] ? (char *)aname->public_key_token : "null",
  689. (aname->flags & ASSEMBLYREF_RETARGETABLE_FLAG) ? ", Retargetable=Yes" : "");
  690. }
  691. static gchar*
  692. assemblyref_public_tok (MonoImage *image, guint32 key_index, guint32 flags)
  693. {
  694. const gchar *public_tok;
  695. int len;
  696. public_tok = mono_metadata_blob_heap (image, key_index);
  697. len = mono_metadata_decode_blob_size (public_tok, &public_tok);
  698. if (flags & ASSEMBLYREF_FULL_PUBLIC_KEY_FLAG) {
  699. guchar token [8];
  700. mono_digest_get_public_token (token, (guchar*)public_tok, len);
  701. return encode_public_tok (token, 8);
  702. }
  703. return encode_public_tok ((guchar*)public_tok, len);
  704. }
  705. /**
  706. * mono_assembly_addref:
  707. * @assemnly: the assembly to reference
  708. *
  709. * This routine increments the reference count on a MonoAssembly.
  710. * The reference count is reduced every time the method mono_assembly_close() is
  711. * invoked.
  712. */
  713. void
  714. mono_assembly_addref (MonoAssembly *assembly)
  715. {
  716. InterlockedIncrement (&assembly->ref_count);
  717. }
  718. #define SILVERLIGHT_KEY "7cec85d7bea7798e"
  719. #define WINFX_KEY "31bf3856ad364e35"
  720. #define ECMA_KEY "b77a5c561934e089"
  721. #define MSFINAL_KEY "b03f5f7f11d50a3a"
  722. typedef struct {
  723. const char *name;
  724. const char *from;
  725. const char *to;
  726. } KeyRemapEntry;
  727. static KeyRemapEntry key_remap_table[] = {
  728. { "Microsoft.CSharp", WINFX_KEY, MSFINAL_KEY },
  729. { "System", SILVERLIGHT_KEY, ECMA_KEY },
  730. { "System.ComponentModel.Composition", WINFX_KEY, ECMA_KEY },
  731. { "System.ComponentModel.DataAnnotations", "ddd0da4d3e678217", WINFX_KEY },
  732. { "System.Core", SILVERLIGHT_KEY, ECMA_KEY },
  733. { "System.Numerics", WINFX_KEY, ECMA_KEY },
  734. { "System.Runtime.Serialization", SILVERLIGHT_KEY, ECMA_KEY },
  735. { "System.ServiceModel", WINFX_KEY, ECMA_KEY },
  736. { "System.ServiceModel.Web", SILVERLIGHT_KEY, WINFX_KEY },
  737. { "System.Windows", SILVERLIGHT_KEY, MSFINAL_KEY },
  738. { "System.Xml", SILVERLIGHT_KEY, ECMA_KEY },
  739. { "System.Xml.Linq", WINFX_KEY, ECMA_KEY },
  740. { "System.Xml.Serialization", WINFX_KEY, MSFINAL_KEY }
  741. };
  742. static void
  743. remap_keys (MonoAssemblyName *aname)
  744. {
  745. int i;
  746. for (i = 0; i < G_N_ELEMENTS (key_remap_table); i++) {
  747. const KeyRemapEntry *entry = &key_remap_table [i];
  748. if (strcmp (aname->name, entry->name) ||
  749. !mono_public_tokens_are_equal (aname->public_key_token, (const unsigned char*) entry->from))
  750. continue;
  751. memcpy (aname->public_key_token, entry->to, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  752. mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY,
  753. "Remapped public key token of retargetable assembly %s from %s to %s",
  754. aname->name, entry->from, entry->to);
  755. return;
  756. }
  757. }
  758. static MonoAssemblyName *
  759. mono_assembly_remap_version (MonoAssemblyName *aname, MonoAssemblyName *dest_aname)
  760. {
  761. const MonoRuntimeInfo *current_runtime;
  762. int pos, first, last;
  763. if (aname->name == NULL) return aname;
  764. current_runtime = mono_get_runtime_info ();
  765. if (aname->flags & ASSEMBLYREF_RETARGETABLE_FLAG) {
  766. const AssemblyVersionSet* vset;
  767. /* Remap to current runtime */
  768. vset = &current_runtime->version_sets [0];
  769. memcpy (dest_aname, aname, sizeof(MonoAssemblyName));
  770. dest_aname->major = vset->major;
  771. dest_aname->minor = vset->minor;
  772. dest_aname->build = vset->build;
  773. dest_aname->revision = vset->revision;
  774. dest_aname->flags &= ~ASSEMBLYREF_RETARGETABLE_FLAG;
  775. /* Remap assembly name */
  776. if (!strcmp (aname->name, "System.Net"))
  777. dest_aname->name = g_strdup ("System");
  778. remap_keys (dest_aname);
  779. mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY,
  780. "The request to load the retargetable assembly %s v%d.%d.%d.%d was remapped to %s v%d.%d.%d.%d",
  781. aname->name,
  782. aname->major, aname->minor, aname->build, aname->revision,
  783. dest_aname->name,
  784. vset->major, vset->minor, vset->build, vset->revision
  785. );
  786. return dest_aname;
  787. }
  788. #ifndef DISABLE_ASSEMBLY_REMAPPING
  789. first = 0;
  790. last = G_N_ELEMENTS (framework_assemblies) - 1;
  791. while (first <= last) {
  792. int res;
  793. pos = first + (last - first) / 2;
  794. res = strcmp (aname->name, framework_assemblies[pos].assembly_name);
  795. if (res == 0) {
  796. const AssemblyVersionSet* vset;
  797. int index = framework_assemblies[pos].version_set_index;
  798. g_assert (index < G_N_ELEMENTS (current_runtime->version_sets));
  799. vset = &current_runtime->version_sets [index];
  800. if (aname->major == vset->major && aname->minor == vset->minor &&
  801. aname->build == vset->build && aname->revision == vset->revision)
  802. return aname;
  803. if ((aname->major | aname->minor | aname->build | aname->revision) != 0)
  804. mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY,
  805. "The request to load the assembly %s v%d.%d.%d.%d was remapped to v%d.%d.%d.%d",
  806. aname->name,
  807. aname->major, aname->minor, aname->build, aname->revision,
  808. vset->major, vset->minor, vset->build, vset->revision
  809. );
  810. memcpy (dest_aname, aname, sizeof(MonoAssemblyName));
  811. dest_aname->major = vset->major;
  812. dest_aname->minor = vset->minor;
  813. dest_aname->build = vset->build;
  814. dest_aname->revision = vset->revision;
  815. return dest_aname;
  816. } else if (res < 0) {
  817. last = pos - 1;
  818. } else {
  819. first = pos + 1;
  820. }
  821. }
  822. #endif
  823. return aname;
  824. }
  825. /*
  826. * mono_assembly_get_assemblyref:
  827. *
  828. * Fill out ANAME with the assembly name of the INDEXth assembly reference in IMAGE.
  829. */
  830. void
  831. mono_assembly_get_assemblyref (MonoImage *image, int index, MonoAssemblyName *aname)
  832. {
  833. MonoTableInfo *t;
  834. guint32 cols [MONO_ASSEMBLYREF_SIZE];
  835. const char *hash;
  836. t = &image->tables [MONO_TABLE_ASSEMBLYREF];
  837. mono_metadata_decode_row (t, index, cols, MONO_ASSEMBLYREF_SIZE);
  838. hash = mono_metadata_blob_heap (image, cols [MONO_ASSEMBLYREF_HASH_VALUE]);
  839. aname->hash_len = mono_metadata_decode_blob_size (hash, &hash);
  840. aname->hash_value = hash;
  841. aname->name = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_NAME]);
  842. aname->culture = mono_metadata_string_heap (image, cols [MONO_ASSEMBLYREF_CULTURE]);
  843. aname->flags = cols [MONO_ASSEMBLYREF_FLAGS];
  844. aname->major = cols [MONO_ASSEMBLYREF_MAJOR_VERSION];
  845. aname->minor = cols [MONO_ASSEMBLYREF_MINOR_VERSION];
  846. aname->build = cols [MONO_ASSEMBLYREF_BUILD_NUMBER];
  847. aname->revision = cols [MONO_ASSEMBLYREF_REV_NUMBER];
  848. if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]) {
  849. gchar *token = assemblyref_public_tok (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], aname->flags);
  850. g_strlcpy ((char*)aname->public_key_token, token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  851. g_free (token);
  852. } else {
  853. memset (aname->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  854. }
  855. }
  856. void
  857. mono_assembly_load_reference (MonoImage *image, int index)
  858. {
  859. MonoAssembly *reference;
  860. MonoAssemblyName aname;
  861. MonoImageOpenStatus status;
  862. /*
  863. * image->references is shared between threads, so we need to access
  864. * it inside a critical section.
  865. */
  866. mono_assemblies_lock ();
  867. if (!image->references) {
  868. MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
  869. image->references = g_new0 (MonoAssembly *, t->rows + 1);
  870. }
  871. reference = image->references [index];
  872. mono_assemblies_unlock ();
  873. if (reference)
  874. return;
  875. mono_assembly_get_assemblyref (image, index, &aname);
  876. if (image->assembly && image->assembly->ref_only) {
  877. /* We use the loaded corlib */
  878. if (!strcmp (aname.name, "mscorlib"))
  879. reference = mono_assembly_load_full (&aname, image->assembly->basedir, &status, FALSE);
  880. else {
  881. reference = mono_assembly_loaded_full (&aname, TRUE);
  882. if (!reference)
  883. /* Try a postload search hook */
  884. reference = mono_assembly_invoke_search_hook_internal (&aname, TRUE, TRUE);
  885. }
  886. /*
  887. * Here we must advice that the error was due to
  888. * a non loaded reference using the ReflectionOnly api
  889. */
  890. if (!reference)
  891. reference = REFERENCE_MISSING;
  892. } else
  893. reference = mono_assembly_load (&aname, image->assembly? image->assembly->basedir: NULL, &status);
  894. if (reference == NULL){
  895. char *extra_msg;
  896. if (status == MONO_IMAGE_ERROR_ERRNO && errno == ENOENT) {
  897. 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 : "" );
  898. } else if (status == MONO_IMAGE_ERROR_ERRNO) {
  899. extra_msg = g_strdup_printf ("System error: %s\n", strerror (errno));
  900. } else if (status == MONO_IMAGE_MISSING_ASSEMBLYREF) {
  901. extra_msg = g_strdup ("Cannot find an assembly referenced from this one.\n");
  902. } else if (status == MONO_IMAGE_IMAGE_INVALID) {
  903. extra_msg = g_strdup ("The file exists but is not a valid assembly.\n");
  904. } else {
  905. extra_msg = g_strdup ("");
  906. }
  907. mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_ASSEMBLY, "The following assembly referenced from %s could not be loaded:\n"
  908. " Assembly: %s (assemblyref_index=%d)\n"
  909. " Version: %d.%d.%d.%d\n"
  910. " Public Key: %s\n%s",
  911. image->name, aname.name, index,
  912. aname.major, aname.minor, aname.build, aname.revision,
  913. strlen ((char*)aname.public_key_token) == 0 ? "(none)" : (char*)aname.public_key_token, extra_msg);
  914. g_free (extra_msg);
  915. }
  916. mono_assemblies_lock ();
  917. if (reference == NULL) {
  918. /* Flag as not found */
  919. reference = REFERENCE_MISSING;
  920. }
  921. if (!image->references [index]) {
  922. if (reference != REFERENCE_MISSING){
  923. mono_assembly_addref (reference);
  924. if (image->assembly)
  925. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Assembly Ref addref %s[%p] -> %s[%p]: %d",
  926. image->assembly->aname.name, image->assembly, reference->aname.name, reference, reference->ref_count);
  927. } else {
  928. if (image->assembly)
  929. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Failed to load assembly %s[%p]\n",
  930. image->assembly->aname.name, image->assembly);
  931. }
  932. image->references [index] = reference;
  933. }
  934. mono_assemblies_unlock ();
  935. if (image->references [index] != reference) {
  936. /* Somebody loaded it before us */
  937. mono_assembly_close (reference);
  938. }
  939. }
  940. void
  941. mono_assembly_load_references (MonoImage *image, MonoImageOpenStatus *status)
  942. {
  943. /* This is a no-op now but it is part of the embedding API so we can't remove it */
  944. *status = MONO_IMAGE_OK;
  945. }
  946. typedef struct AssemblyLoadHook AssemblyLoadHook;
  947. struct AssemblyLoadHook {
  948. AssemblyLoadHook *next;
  949. MonoAssemblyLoadFunc func;
  950. gpointer user_data;
  951. };
  952. AssemblyLoadHook *assembly_load_hook = NULL;
  953. void
  954. mono_assembly_invoke_load_hook (MonoAssembly *ass)
  955. {
  956. AssemblyLoadHook *hook;
  957. for (hook = assembly_load_hook; hook; hook = hook->next) {
  958. hook->func (ass, hook->user_data);
  959. }
  960. }
  961. void
  962. mono_install_assembly_load_hook (MonoAssemblyLoadFunc func, gpointer user_data)
  963. {
  964. AssemblyLoadHook *hook;
  965. g_return_if_fail (func != NULL);
  966. hook = g_new0 (AssemblyLoadHook, 1);
  967. hook->func = func;
  968. hook->user_data = user_data;
  969. hook->next = assembly_load_hook;
  970. assembly_load_hook = hook;
  971. }
  972. static void
  973. free_assembly_load_hooks (void)
  974. {
  975. AssemblyLoadHook *hook, *next;
  976. for (hook = assembly_load_hook; hook; hook = next) {
  977. next = hook->next;
  978. g_free (hook);
  979. }
  980. }
  981. typedef struct AssemblySearchHook AssemblySearchHook;
  982. struct AssemblySearchHook {
  983. AssemblySearchHook *next;
  984. MonoAssemblySearchFunc func;
  985. gboolean refonly;
  986. gboolean postload;
  987. gpointer user_data;
  988. };
  989. AssemblySearchHook *assembly_search_hook = NULL;
  990. static MonoAssembly*
  991. mono_assembly_invoke_search_hook_internal (MonoAssemblyName *aname, gboolean refonly, gboolean postload)
  992. {
  993. AssemblySearchHook *hook;
  994. for (hook = assembly_search_hook; hook; hook = hook->next) {
  995. if ((hook->refonly == refonly) && (hook->postload == postload)) {
  996. MonoAssembly *ass = hook->func (aname, hook->user_data);
  997. if (ass)
  998. return ass;
  999. }
  1000. }
  1001. return NULL;
  1002. }
  1003. MonoAssembly*
  1004. mono_assembly_invoke_search_hook (MonoAssemblyName *aname)
  1005. {
  1006. return mono_assembly_invoke_search_hook_internal (aname, FALSE, FALSE);
  1007. }
  1008. static void
  1009. mono_install_assembly_search_hook_internal (MonoAssemblySearchFunc func, gpointer user_data, gboolean refonly, gboolean postload)
  1010. {
  1011. AssemblySearchHook *hook;
  1012. g_return_if_fail (func != NULL);
  1013. hook = g_new0 (AssemblySearchHook, 1);
  1014. hook->func = func;
  1015. hook->user_data = user_data;
  1016. hook->refonly = refonly;
  1017. hook->postload = postload;
  1018. hook->next = assembly_search_hook;
  1019. assembly_search_hook = hook;
  1020. }
  1021. void
  1022. mono_install_assembly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
  1023. {
  1024. mono_install_assembly_search_hook_internal (func, user_data, FALSE, FALSE);
  1025. }
  1026. static void
  1027. free_assembly_search_hooks (void)
  1028. {
  1029. AssemblySearchHook *hook, *next;
  1030. for (hook = assembly_search_hook; hook; hook = next) {
  1031. next = hook->next;
  1032. g_free (hook);
  1033. }
  1034. }
  1035. void
  1036. mono_install_assembly_refonly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
  1037. {
  1038. mono_install_assembly_search_hook_internal (func, user_data, TRUE, FALSE);
  1039. }
  1040. void
  1041. mono_install_assembly_postload_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
  1042. {
  1043. mono_install_assembly_search_hook_internal (func, user_data, FALSE, TRUE);
  1044. }
  1045. void
  1046. mono_install_assembly_postload_refonly_search_hook (MonoAssemblySearchFunc func, gpointer user_data)
  1047. {
  1048. mono_install_assembly_search_hook_internal (func, user_data, TRUE, TRUE);
  1049. }
  1050. typedef struct AssemblyPreLoadHook AssemblyPreLoadHook;
  1051. struct AssemblyPreLoadHook {
  1052. AssemblyPreLoadHook *next;
  1053. MonoAssemblyPreLoadFunc func;
  1054. gpointer user_data;
  1055. };
  1056. static AssemblyPreLoadHook *assembly_preload_hook = NULL;
  1057. static AssemblyPreLoadHook *assembly_refonly_preload_hook = NULL;
  1058. static MonoAssembly *
  1059. invoke_assembly_preload_hook (MonoAssemblyName *aname, gchar **assemblies_path)
  1060. {
  1061. AssemblyPreLoadHook *hook;
  1062. MonoAssembly *assembly;
  1063. for (hook = assembly_preload_hook; hook; hook = hook->next) {
  1064. assembly = hook->func (aname, assemblies_path, hook->user_data);
  1065. if (assembly != NULL)
  1066. return assembly;
  1067. }
  1068. return NULL;
  1069. }
  1070. static MonoAssembly *
  1071. invoke_assembly_refonly_preload_hook (MonoAssemblyName *aname, gchar **assemblies_path)
  1072. {
  1073. AssemblyPreLoadHook *hook;
  1074. MonoAssembly *assembly;
  1075. for (hook = assembly_refonly_preload_hook; hook; hook = hook->next) {
  1076. assembly = hook->func (aname, assemblies_path, hook->user_data);
  1077. if (assembly != NULL)
  1078. return assembly;
  1079. }
  1080. return NULL;
  1081. }
  1082. void
  1083. mono_install_assembly_preload_hook (MonoAssemblyPreLoadFunc func, gpointer user_data)
  1084. {
  1085. AssemblyPreLoadHook *hook;
  1086. g_return_if_fail (func != NULL);
  1087. hook = g_new0 (AssemblyPreLoadHook, 1);
  1088. hook->func = func;
  1089. hook->user_data = user_data;
  1090. hook->next = assembly_preload_hook;
  1091. assembly_preload_hook = hook;
  1092. }
  1093. void
  1094. mono_install_assembly_refonly_preload_hook (MonoAssemblyPreLoadFunc func, gpointer user_data)
  1095. {
  1096. AssemblyPreLoadHook *hook;
  1097. g_return_if_fail (func != NULL);
  1098. hook = g_new0 (AssemblyPreLoadHook, 1);
  1099. hook->func = func;
  1100. hook->user_data = user_data;
  1101. hook->next = assembly_refonly_preload_hook;
  1102. assembly_refonly_preload_hook = hook;
  1103. }
  1104. static void
  1105. free_assembly_preload_hooks (void)
  1106. {
  1107. AssemblyPreLoadHook *hook, *next;
  1108. for (hook = assembly_preload_hook; hook; hook = next) {
  1109. next = hook->next;
  1110. g_free (hook);
  1111. }
  1112. for (hook = assembly_refonly_preload_hook; hook; hook = next) {
  1113. next = hook->next;
  1114. g_free (hook);
  1115. }
  1116. }
  1117. static gchar *
  1118. absolute_dir (const gchar *filename)
  1119. {
  1120. gchar *cwd;
  1121. gchar *mixed;
  1122. gchar **parts;
  1123. gchar *part;
  1124. GList *list, *tmp;
  1125. GString *result;
  1126. gchar *res;
  1127. gint i;
  1128. if (g_path_is_absolute (filename)) {
  1129. part = g_path_get_dirname (filename);
  1130. res = g_strconcat (part, G_DIR_SEPARATOR_S, NULL);
  1131. g_free (part);
  1132. return res;
  1133. }
  1134. cwd = g_get_current_dir ();
  1135. mixed = g_build_filename (cwd, filename, NULL);
  1136. parts = g_strsplit (mixed, G_DIR_SEPARATOR_S, 0);
  1137. g_free (mixed);
  1138. g_free (cwd);
  1139. list = NULL;
  1140. for (i = 0; (part = parts [i]) != NULL; i++) {
  1141. if (!strcmp (part, "."))
  1142. continue;
  1143. if (!strcmp (part, "..")) {
  1144. if (list && list->next) /* Don't remove root */
  1145. list = g_list_delete_link (list, list);
  1146. } else {
  1147. list = g_list_prepend (list, part);
  1148. }
  1149. }
  1150. result = g_string_new ("");
  1151. list = g_list_reverse (list);
  1152. /* Ignores last data pointer, which should be the filename */
  1153. for (tmp = list; tmp && tmp->next != NULL; tmp = tmp->next){
  1154. if (tmp->data)
  1155. g_string_append_printf (result, "%s%c", (char *) tmp->data,
  1156. G_DIR_SEPARATOR);
  1157. }
  1158. res = result->str;
  1159. g_string_free (result, FALSE);
  1160. g_list_free (list);
  1161. g_strfreev (parts);
  1162. if (*res == '\0') {
  1163. g_free (res);
  1164. return g_strdup (".");
  1165. }
  1166. return res;
  1167. }
  1168. /**
  1169. * mono_assembly_open_from_bundle:
  1170. * @filename: Filename requested
  1171. * @status: return value
  1172. *
  1173. * This routine tries to open the assembly specified by `filename' from the
  1174. * defined bundles, if found, returns the MonoImage for it, if not found
  1175. * returns NULL
  1176. */
  1177. MonoImage *
  1178. mono_assembly_open_from_bundle (const char *filename, MonoImageOpenStatus *status, gboolean refonly)
  1179. {
  1180. int i;
  1181. char *name;
  1182. MonoImage *image = NULL;
  1183. /*
  1184. * we do a very simple search for bundled assemblies: it's not a general
  1185. * purpose assembly loading mechanism.
  1186. */
  1187. if (!bundles)
  1188. return NULL;
  1189. name = g_path_get_basename (filename);
  1190. mono_assemblies_lock ();
  1191. for (i = 0; !image && bundles [i]; ++i) {
  1192. if (strcmp (bundles [i]->name, name) == 0) {
  1193. image = mono_image_open_from_data_with_name ((char*)bundles [i]->data, bundles [i]->size, FALSE, status, refonly, name);
  1194. break;
  1195. }
  1196. }
  1197. mono_assemblies_unlock ();
  1198. g_free (name);
  1199. if (image) {
  1200. mono_image_addref (image);
  1201. return image;
  1202. }
  1203. return NULL;
  1204. }
  1205. MonoAssembly *
  1206. mono_assembly_open_full (const char *filename, MonoImageOpenStatus *status, gboolean refonly)
  1207. {
  1208. MonoImage *image;
  1209. MonoAssembly *ass;
  1210. MonoImageOpenStatus def_status;
  1211. gchar *fname;
  1212. gchar *new_fname;
  1213. g_return_val_if_fail (filename != NULL, NULL);
  1214. if (!status)
  1215. status = &def_status;
  1216. *status = MONO_IMAGE_OK;
  1217. if (strncmp (filename, "file://", 7) == 0) {
  1218. GError *error = NULL;
  1219. gchar *uri = (gchar *) filename;
  1220. gchar *tmpuri;
  1221. /*
  1222. * MS allows file://c:/... and fails on file://localhost/c:/...
  1223. * They also throw an IndexOutOfRangeException if "file://"
  1224. */
  1225. if (uri [7] != '/')
  1226. uri = g_strdup_printf ("file:///%s", uri + 7);
  1227. tmpuri = uri;
  1228. uri = mono_escape_uri_string (tmpuri);
  1229. fname = g_filename_from_uri (uri, NULL, &error);
  1230. g_free (uri);
  1231. if (tmpuri != filename)
  1232. g_free (tmpuri);
  1233. if (error != NULL) {
  1234. g_warning ("%s\n", error->message);
  1235. g_error_free (error);
  1236. fname = g_strdup (filename);
  1237. }
  1238. } else {
  1239. fname = g_strdup (filename);
  1240. }
  1241. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
  1242. "Assembly Loader probing location: '%s'.", fname);
  1243. new_fname = NULL;
  1244. if (!mono_assembly_is_in_gac (fname))
  1245. new_fname = mono_make_shadow_copy (fname);
  1246. if (new_fname && new_fname != fname) {
  1247. g_free (fname);
  1248. fname = new_fname;
  1249. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
  1250. "Assembly Loader shadow-copied assembly to: '%s'.", fname);
  1251. }
  1252. image = NULL;
  1253. if (bundles != NULL)
  1254. image = mono_assembly_open_from_bundle (fname, status, refonly);
  1255. if (!image)
  1256. image = mono_image_open_full (fname, status, refonly);
  1257. if (!image){
  1258. if (*status == MONO_IMAGE_OK)
  1259. *status = MONO_IMAGE_ERROR_ERRNO;
  1260. g_free (fname);
  1261. return NULL;
  1262. }
  1263. if (image->assembly) {
  1264. /* Already loaded by another appdomain */
  1265. mono_assembly_invoke_load_hook (image->assembly);
  1266. mono_image_close (image);
  1267. g_free (fname);
  1268. return image->assembly;
  1269. }
  1270. ass = mono_assembly_load_from_full (image, fname, status, refonly);
  1271. if (ass) {
  1272. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY,
  1273. "Assembly Loader loaded assembly from location: '%s'.", filename);
  1274. if (!refonly)
  1275. mono_config_for_assembly (ass->image);
  1276. }
  1277. /* Clear the reference added by mono_image_open */
  1278. mono_image_close (image);
  1279. g_free (fname);
  1280. return ass;
  1281. }
  1282. static void
  1283. free_item (gpointer val, gpointer user_data)
  1284. {
  1285. g_free (val);
  1286. }
  1287. /*
  1288. * mono_assembly_load_friends:
  1289. * @ass: an assembly
  1290. *
  1291. * Load the list of friend assemblies that are allowed to access
  1292. * the assembly's internal types and members. They are stored as assembly
  1293. * names in custom attributes.
  1294. *
  1295. * This is an internal method, we need this because when we load mscorlib
  1296. * we do not have the mono_defaults.internals_visible_class loaded yet,
  1297. * so we need to load these after we initialize the runtime.
  1298. *
  1299. * LOCKING: Acquires the assemblies lock plus the loader lock.
  1300. */
  1301. void
  1302. mono_assembly_load_friends (MonoAssembly* ass)
  1303. {
  1304. int i;
  1305. MonoCustomAttrInfo* attrs;
  1306. GSList *list;
  1307. if (ass->friend_assembly_names_inited)
  1308. return;
  1309. attrs = mono_custom_attrs_from_assembly (ass);
  1310. if (!attrs) {
  1311. mono_assemblies_lock ();
  1312. ass->friend_assembly_names_inited = TRUE;
  1313. mono_assemblies_unlock ();
  1314. return;
  1315. }
  1316. mono_assemblies_lock ();
  1317. if (ass->friend_assembly_names_inited) {
  1318. mono_assemblies_unlock ();
  1319. return;
  1320. }
  1321. mono_assemblies_unlock ();
  1322. list = NULL;
  1323. /*
  1324. * We build the list outside the assemblies lock, the worse that can happen
  1325. * is that we'll need to free the allocated list.
  1326. */
  1327. for (i = 0; i < attrs->num_attrs; ++i) {
  1328. MonoCustomAttrEntry *attr = &attrs->attrs [i];
  1329. MonoAssemblyName *aname;
  1330. const gchar *data;
  1331. guint slen;
  1332. /* Do some sanity checking */
  1333. if (!attr->ctor || attr->ctor->klass != mono_defaults.internals_visible_class)
  1334. continue;
  1335. if (attr->data_size < 4)
  1336. continue;
  1337. data = (const char*)attr->data;
  1338. /* 0xFF means null string, see custom attr format */
  1339. if (data [0] != 1 || data [1] != 0 || (data [2] & 0xFF) == 0xFF)
  1340. continue;
  1341. slen = mono_metadata_decode_value (data + 2, &data);
  1342. aname = g_new0 (MonoAssemblyName, 1);
  1343. /*g_print ("friend ass: %s\n", data);*/
  1344. if (mono_assembly_name_parse_full (data, aname, TRUE, NULL, NULL)) {
  1345. list = g_slist_prepend (list, aname);
  1346. } else {
  1347. g_free (aname);
  1348. }
  1349. }
  1350. mono_custom_attrs_free (attrs);
  1351. mono_assemblies_lock ();
  1352. if (ass->friend_assembly_names_inited) {
  1353. mono_assemblies_unlock ();
  1354. g_slist_foreach (list, free_item, NULL);
  1355. g_slist_free (list);
  1356. return;
  1357. }
  1358. ass->friend_assembly_names = list;
  1359. /* Because of the double checked locking pattern above */
  1360. mono_memory_barrier ();
  1361. ass->friend_assembly_names_inited = TRUE;
  1362. mono_assemblies_unlock ();
  1363. }
  1364. /**
  1365. * mono_assembly_open:
  1366. * @filename: Opens the assembly pointed out by this name
  1367. * @status: where a status code can be returned
  1368. *
  1369. * mono_assembly_open opens the PE-image pointed by @filename, and
  1370. * loads any external assemblies referenced by it.
  1371. *
  1372. * Return: a pointer to the MonoAssembly if @filename contains a valid
  1373. * assembly or NULL on error. Details about the error are stored in the
  1374. * @status variable.
  1375. */
  1376. MonoAssembly *
  1377. mono_assembly_open (const char *filename, MonoImageOpenStatus *status)
  1378. {
  1379. return mono_assembly_open_full (filename, status, FALSE);
  1380. }
  1381. MonoAssembly *
  1382. mono_assembly_load_from_full (MonoImage *image, const char*fname,
  1383. MonoImageOpenStatus *status, gboolean refonly)
  1384. {
  1385. MonoAssembly *ass, *ass2;
  1386. char *base_dir;
  1387. if (!image->tables [MONO_TABLE_ASSEMBLY].rows) {
  1388. /* 'image' doesn't have a manifest -- maybe someone is trying to Assembly.Load a .netmodule */
  1389. *status = MONO_IMAGE_IMAGE_INVALID;
  1390. return NULL;
  1391. }
  1392. #if defined (HOST_WIN32)
  1393. {
  1394. gchar *tmp_fn;
  1395. int i;
  1396. tmp_fn = g_strdup (fname);
  1397. for (i = strlen (tmp_fn) - 1; i >= 0; i--) {
  1398. if (tmp_fn [i] == '/')
  1399. tmp_fn [i] = '\\';
  1400. }
  1401. base_dir = absolute_dir (tmp_fn);
  1402. g_free (tmp_fn);
  1403. }
  1404. #else
  1405. base_dir = absolute_dir (fname);
  1406. #endif
  1407. /*
  1408. * Create assembly struct, and enter it into the assembly cache
  1409. */
  1410. ass = g_new0 (MonoAssembly, 1);
  1411. ass->basedir = base_dir;
  1412. ass->ref_only = refonly;
  1413. ass->image = image;
  1414. mono_profiler_assembly_event (ass, MONO_PROFILE_START_LOAD);
  1415. mono_assembly_fill_assembly_name (image, &ass->aname);
  1416. if (mono_defaults.corlib && strcmp (ass->aname.name, "mscorlib") == 0) {
  1417. // MS.NET doesn't support loading other mscorlibs
  1418. g_free (ass);
  1419. g_free (base_dir);
  1420. mono_image_addref (mono_defaults.corlib);
  1421. *status = MONO_IMAGE_OK;
  1422. return mono_defaults.corlib->assembly;
  1423. }
  1424. /* Add a non-temporary reference because of ass->image */
  1425. mono_image_addref (image);
  1426. 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);
  1427. /*
  1428. * The load hooks might take locks so we can't call them while holding the
  1429. * assemblies lock.
  1430. */
  1431. if (ass->aname.name) {
  1432. ass2 = mono_assembly_invoke_search_hook_internal (&ass->aname, refonly, FALSE);
  1433. if (ass2) {
  1434. g_free (ass);
  1435. g_free (base_dir);
  1436. mono_image_close (image);
  1437. *status = MONO_IMAGE_OK;
  1438. return ass2;
  1439. }
  1440. }
  1441. mono_assemblies_lock ();
  1442. if (image->assembly) {
  1443. /*
  1444. * This means another thread has already loaded the assembly, but not yet
  1445. * called the load hooks so the search hook can't find the assembly.
  1446. */
  1447. mono_assemblies_unlock ();
  1448. ass2 = image->assembly;
  1449. g_free (ass);
  1450. g_free (base_dir);
  1451. mono_image_close (image);
  1452. *status = MONO_IMAGE_OK;
  1453. return ass2;
  1454. }
  1455. image->assembly = ass;
  1456. loaded_assemblies = g_list_prepend (loaded_assemblies, ass);
  1457. mono_assemblies_unlock ();
  1458. #ifdef HOST_WIN32
  1459. if (image->is_module_handle)
  1460. mono_image_fixup_vtable (image);
  1461. #endif
  1462. mono_assembly_invoke_load_hook (ass);
  1463. mono_profiler_assembly_loaded (ass, MONO_PROFILE_OK);
  1464. return ass;
  1465. }
  1466. MonoAssembly *
  1467. mono_assembly_load_from (MonoImage *image, const char *fname,
  1468. MonoImageOpenStatus *status)
  1469. {
  1470. return mono_assembly_load_from_full (image, fname, status, FALSE);
  1471. }
  1472. /**
  1473. * mono_assembly_name_free:
  1474. * @aname: assembly name to free
  1475. *
  1476. * Frees the provided assembly name object.
  1477. * (it does not frees the object itself, only the name members).
  1478. */
  1479. void
  1480. mono_assembly_name_free (MonoAssemblyName *aname)
  1481. {
  1482. if (aname == NULL)
  1483. return;
  1484. g_free ((void *) aname->name);
  1485. g_free ((void *) aname->culture);
  1486. g_free ((void *) aname->hash_value);
  1487. }
  1488. static gboolean
  1489. parse_public_key (const gchar *key, gchar** pubkey, gboolean *is_ecma)
  1490. {
  1491. const gchar *pkey;
  1492. gchar header [16], val, *arr;
  1493. gint i, j, offset, bitlen, keylen, pkeylen;
  1494. keylen = strlen (key) >> 1;
  1495. if (keylen < 1)
  1496. return FALSE;
  1497. /* allow the ECMA standard key */
  1498. if (strcmp (key, "00000000000000000400000000000000") == 0) {
  1499. if (pubkey) {
  1500. *pubkey = g_strdup (key);
  1501. *is_ecma = TRUE;
  1502. }
  1503. return TRUE;
  1504. }
  1505. *is_ecma = FALSE;
  1506. val = g_ascii_xdigit_value (key [0]) << 4;
  1507. val |= g_ascii_xdigit_value (key [1]);
  1508. switch (val) {
  1509. case 0x00:
  1510. if (keylen < 13)
  1511. return FALSE;
  1512. val = g_ascii_xdigit_value (key [24]);
  1513. val |= g_ascii_xdigit_value (key [25]);
  1514. if (val != 0x06)
  1515. return FALSE;
  1516. pkey = key + 24;
  1517. break;
  1518. case 0x06:
  1519. pkey = key;
  1520. break;
  1521. default:
  1522. return FALSE;
  1523. }
  1524. /* We need the first 16 bytes
  1525. * to check whether this key is valid or not */
  1526. pkeylen = strlen (pkey) >> 1;
  1527. if (pkeylen < 16)
  1528. return FALSE;
  1529. for (i = 0, j = 0; i < 16; i++) {
  1530. header [i] = g_ascii_xdigit_value (pkey [j++]) << 4;
  1531. header [i] |= g_ascii_xdigit_value (pkey [j++]);
  1532. }
  1533. if (header [0] != 0x06 || /* PUBLICKEYBLOB (0x06) */
  1534. header [1] != 0x02 || /* Version (0x02) */
  1535. header [2] != 0x00 || /* Reserved (word) */
  1536. header [3] != 0x00 ||
  1537. (guint)(read32 (header + 8)) != 0x31415352) /* DWORD magic = RSA1 */
  1538. return FALSE;
  1539. /* Based on this length, we _should_ be able to know if the length is right */
  1540. bitlen = read32 (header + 12) >> 3;
  1541. if ((bitlen + 16 + 4) != pkeylen)
  1542. return FALSE;
  1543. /* parsing is OK and the public key itself is not requested back */
  1544. if (!pubkey)
  1545. return TRUE;
  1546. /* Encode the size of the blob */
  1547. offset = 0;
  1548. if (keylen <= 127) {
  1549. arr = g_malloc (keylen + 1);
  1550. arr [offset++] = keylen;
  1551. } else {
  1552. arr = g_malloc (keylen + 2);
  1553. arr [offset++] = 0x80; /* 10bs */
  1554. arr [offset++] = keylen;
  1555. }
  1556. for (i = offset, j = 0; i < keylen + offset; i++) {
  1557. arr [i] = g_ascii_xdigit_value (key [j++]) << 4;
  1558. arr [i] |= g_ascii_xdigit_value (key [j++]);
  1559. }
  1560. *pubkey = arr;
  1561. return TRUE;
  1562. }
  1563. static gboolean
  1564. 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)
  1565. {
  1566. gint major, minor, build, revision;
  1567. gint len;
  1568. gint version_parts;
  1569. gchar *pkey, *pkeyptr, *encoded, tok [8];
  1570. memset (aname, 0, sizeof (MonoAssemblyName));
  1571. if (version) {
  1572. version_parts = sscanf (version, "%u.%u.%u.%u", &major, &minor, &build, &revision);
  1573. if (version_parts < 2 || version_parts > 4)
  1574. return FALSE;
  1575. /* FIXME: we should set build & revision to -1 (instead of 0)
  1576. if these are not set in the version string. That way, later on,
  1577. we can still determine if these were specified. */
  1578. aname->major = major;
  1579. aname->minor = minor;
  1580. if (version_parts >= 3)
  1581. aname->build = build;
  1582. else
  1583. aname->build = 0;
  1584. if (version_parts == 4)
  1585. aname->revision = revision;
  1586. else
  1587. aname->revision = 0;
  1588. }
  1589. aname->flags = flags;
  1590. aname->arch = arch;
  1591. aname->name = g_strdup (name);
  1592. if (culture) {
  1593. if (g_ascii_strcasecmp (culture, "neutral") == 0)
  1594. aname->culture = g_strdup ("");
  1595. else
  1596. aname->culture = g_strdup (culture);
  1597. }
  1598. if (token && strncmp (token, "null", 4) != 0) {
  1599. char *lower;
  1600. /* the constant includes the ending NULL, hence the -1 */
  1601. if (strlen (token) != (MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)) {
  1602. mono_assembly_name_free (aname);
  1603. return FALSE;
  1604. }
  1605. lower = g_ascii_strdown (token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  1606. g_strlcpy ((char*)aname->public_key_token, lower, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  1607. g_free (lower);
  1608. }
  1609. if (key) {
  1610. gboolean is_ecma;
  1611. if (strcmp (key, "null") == 0 || !parse_public_key (key, &pkey, &is_ecma)) {
  1612. mono_assembly_name_free (aname);
  1613. return FALSE;
  1614. }
  1615. if (is_ecma) {
  1616. if (save_public_key)
  1617. aname->public_key = (guint8*)pkey;
  1618. else
  1619. g_free (pkey);
  1620. g_strlcpy ((gchar*)aname->public_key_token, "b77a5c561934e089", MONO_PUBLIC_KEY_TOKEN_LENGTH);
  1621. return TRUE;
  1622. }
  1623. len = mono_metadata_decode_blob_size ((const gchar *) pkey, (const gchar **) &pkeyptr);
  1624. // We also need to generate the key token
  1625. mono_digest_get_public_token ((guchar*) tok, (guint8*) pkeyptr, len);
  1626. encoded = encode_public_tok ((guchar*) tok, 8);
  1627. g_strlcpy ((gchar*)aname->public_key_token, encoded, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  1628. g_free (encoded);
  1629. if (save_public_key)
  1630. aname->public_key = (guint8*) pkey;
  1631. else
  1632. g_free (pkey);
  1633. }
  1634. return TRUE;
  1635. }
  1636. static gboolean
  1637. parse_assembly_directory_name (const char *name, const char *dirname, MonoAssemblyName *aname)
  1638. {
  1639. gchar **parts;
  1640. gboolean res;
  1641. parts = g_strsplit (dirname, "_", 3);
  1642. if (!parts || !parts[0] || !parts[1] || !parts[2]) {
  1643. g_strfreev (parts);
  1644. return FALSE;
  1645. }
  1646. res = build_assembly_name (name, parts[0], parts[1], parts[2], NULL, 0, 0, aname, FALSE);
  1647. g_strfreev (parts);
  1648. return res;
  1649. }
  1650. static gboolean
  1651. split_key_value (const gchar *pair, gchar **key, guint32 *keylen, gchar **value)
  1652. {
  1653. char *eqsign = strchr (pair, '=');
  1654. if (!eqsign) {
  1655. *key = NULL;
  1656. *keylen = 0;
  1657. *value = NULL;
  1658. return FALSE;
  1659. }
  1660. *key = (gchar*)pair;
  1661. *keylen = eqsign - *key;
  1662. while (*keylen > 0 && g_ascii_isspace ((*key) [*keylen - 1]))
  1663. (*keylen)--;
  1664. *value = g_strstrip (eqsign + 1);
  1665. return TRUE;
  1666. }
  1667. gboolean
  1668. mono_assembly_name_parse_full (const char *name, MonoAssemblyName *aname, gboolean save_public_key, gboolean *is_version_defined, gboolean *is_token_defined)
  1669. {
  1670. gchar *dllname;
  1671. gchar *version = NULL;
  1672. gchar *culture = NULL;
  1673. gchar *token = NULL;
  1674. gchar *key = NULL;
  1675. gchar *retargetable = NULL;
  1676. gboolean res;
  1677. gchar *value, *part_name;
  1678. guint32 part_name_len;
  1679. gchar **parts;
  1680. gchar **tmp;
  1681. gboolean version_defined;
  1682. gboolean token_defined;
  1683. guint32 flags = 0;
  1684. guint32 arch = MONO_PROCESSOR_ARCHITECTURE_NONE;
  1685. if (!is_version_defined)
  1686. is_version_defined = &version_defined;
  1687. *is_version_defined = FALSE;
  1688. if (!is_token_defined)
  1689. is_token_defined = &token_defined;
  1690. *is_token_defined = FALSE;
  1691. parts = tmp = g_strsplit (name, ",", 6);
  1692. if (!tmp || !*tmp) {
  1693. g_strfreev (tmp);
  1694. return FALSE;
  1695. }
  1696. dllname = g_strstrip (*tmp);
  1697. tmp++;
  1698. while (*tmp) {
  1699. if (!split_key_value (g_strstrip (*tmp), &part_name, &part_name_len, &value))
  1700. goto cleanup_and_fail;
  1701. if (part_name_len == 7 && !g_ascii_strncasecmp (part_name, "Version", part_name_len)) {
  1702. *is_version_defined = TRUE;
  1703. version = value;
  1704. if (strlen (version) == 0) {
  1705. goto cleanup_and_fail;
  1706. }
  1707. tmp++;
  1708. continue;
  1709. }
  1710. if (part_name_len == 7 && !g_ascii_strncasecmp (part_name, "Culture", part_name_len)) {
  1711. culture = value;
  1712. if (strlen (culture) == 0) {
  1713. goto cleanup_and_fail;
  1714. }
  1715. tmp++;
  1716. continue;
  1717. }
  1718. if (part_name_len == 14 && !g_ascii_strncasecmp (part_name, "PublicKeyToken", part_name_len)) {
  1719. *is_token_defined = TRUE;
  1720. token = value;
  1721. if (strlen (token) == 0) {
  1722. goto cleanup_and_fail;
  1723. }
  1724. tmp++;
  1725. continue;
  1726. }
  1727. if (part_name_len == 9 && !g_ascii_strncasecmp (part_name, "PublicKey", part_name_len)) {
  1728. key = value;
  1729. if (strlen (key) == 0) {
  1730. goto cleanup_and_fail;
  1731. }
  1732. tmp++;
  1733. continue;
  1734. }
  1735. if (part_name_len == 12 && !g_ascii_strncasecmp (part_name, "Retargetable", part_name_len)) {
  1736. retargetable = value;
  1737. if (strlen (retargetable) == 0) {
  1738. goto cleanup_and_fail;
  1739. }
  1740. if (!g_ascii_strcasecmp (retargetable, "yes")) {
  1741. flags |= ASSEMBLYREF_RETARGETABLE_FLAG;
  1742. } else if (g_ascii_strcasecmp (retargetable, "no")) {
  1743. goto cleanup_and_fail;
  1744. }
  1745. tmp++;
  1746. continue;
  1747. }
  1748. if (part_name_len == 21 && !g_ascii_strncasecmp (part_name, "ProcessorArchitecture", part_name_len)) {
  1749. if (!g_ascii_strcasecmp (value, "None"))
  1750. arch = MONO_PROCESSOR_ARCHITECTURE_NONE;
  1751. else if (!g_ascii_strcasecmp (value, "MSIL"))
  1752. arch = MONO_PROCESSOR_ARCHITECTURE_MSIL;
  1753. else if (!g_ascii_strcasecmp (value, "X86"))
  1754. arch = MONO_PROCESSOR_ARCHITECTURE_X86;
  1755. else if (!g_ascii_strcasecmp (value, "IA64"))
  1756. arch = MONO_PROCESSOR_ARCHITECTURE_IA64;
  1757. else if (!g_ascii_strcasecmp (value, "AMD64"))
  1758. arch = MONO_PROCESSOR_ARCHITECTURE_AMD64;
  1759. else
  1760. goto cleanup_and_fail;
  1761. tmp++;
  1762. continue;
  1763. }
  1764. g_strfreev (parts);
  1765. return FALSE;
  1766. }
  1767. /* if retargetable flag is set, then we must have a fully qualified name */
  1768. if (retargetable != NULL && (version == NULL || culture == NULL || (key == NULL && token == NULL))) {
  1769. goto cleanup_and_fail;
  1770. }
  1771. res = build_assembly_name (dllname, version, culture, token, key, flags, arch,
  1772. aname, save_public_key);
  1773. g_strfreev (parts);
  1774. return res;
  1775. cleanup_and_fail:
  1776. g_strfreev (parts);
  1777. return FALSE;
  1778. }
  1779. /**
  1780. * mono_assembly_name_parse:
  1781. * @name: name to parse
  1782. * @aname: the destination assembly name
  1783. *
  1784. * Parses an assembly qualified type name and assigns the name,
  1785. * version, culture and token to the provided assembly name object.
  1786. *
  1787. * Returns: true if the name could be parsed.
  1788. */
  1789. gboolean
  1790. mono_assembly_name_parse (const char *name, MonoAssemblyName *aname)
  1791. {
  1792. return mono_assembly_name_parse_full (name, aname, FALSE, NULL, NULL);
  1793. }
  1794. /**
  1795. * mono_assembly_name_new:
  1796. * @name: name to parse
  1797. *
  1798. * Allocate a new MonoAssemblyName and fill its values from the
  1799. * passed @name.
  1800. *
  1801. * Returns: a newly allocated structure or NULL if there was any failure.
  1802. */
  1803. MonoAssemblyName*
  1804. mono_assembly_name_new (const char *name)
  1805. {
  1806. MonoAssemblyName *aname = g_new0 (MonoAssemblyName, 1);
  1807. if (mono_assembly_name_parse (name, aname))
  1808. return aname;
  1809. g_free (aname);
  1810. return NULL;
  1811. }
  1812. const char*
  1813. mono_assembly_name_get_name (MonoAssemblyName *aname)
  1814. {
  1815. return aname->name;
  1816. }
  1817. const char*
  1818. mono_assembly_name_get_culture (MonoAssemblyName *aname)
  1819. {
  1820. return aname->culture;
  1821. }
  1822. mono_byte*
  1823. mono_assembly_name_get_pubkeytoken (MonoAssemblyName *aname)
  1824. {
  1825. if (aname->public_key_token [0])
  1826. return aname->public_key_token;
  1827. return NULL;
  1828. }
  1829. uint16_t
  1830. mono_assembly_name_get_version (MonoAssemblyName *aname, uint16_t *minor, uint16_t *build, uint16_t *revision)
  1831. {
  1832. if (minor)
  1833. *minor = aname->minor;
  1834. if (build)
  1835. *build = aname->build;
  1836. if (revision)
  1837. *revision = aname->revision;
  1838. return aname->major;
  1839. }
  1840. static MonoAssembly*
  1841. probe_for_partial_name (const char *basepath, const char *fullname, MonoAssemblyName *aname, MonoImageOpenStatus *status)
  1842. {
  1843. gchar *fullpath = NULL;
  1844. GDir *dirhandle;
  1845. const char* direntry;
  1846. MonoAssemblyName gac_aname;
  1847. gint major=-1, minor=0, build=0, revision=0;
  1848. gboolean exact_version;
  1849. dirhandle = g_dir_open (basepath, 0, NULL);
  1850. if (!dirhandle)
  1851. return NULL;
  1852. exact_version = (aname->major | aname->minor | aname->build | aname->revision) != 0;
  1853. while ((direntry = g_dir_read_name (dirhandle))) {
  1854. gboolean match = TRUE;
  1855. if(!parse_assembly_directory_name (aname->name, direntry, &gac_aname))
  1856. continue;
  1857. if (aname->culture != NULL && strcmp (aname->culture, gac_aname.culture) != 0)
  1858. match = FALSE;
  1859. if (match && strlen ((char*)aname->public_key_token) > 0 &&
  1860. !mono_public_tokens_are_equal (aname->public_key_token, gac_aname.public_key_token))
  1861. match = FALSE;
  1862. if (match) {
  1863. if (exact_version) {
  1864. match = (aname->major == gac_aname.major && aname->minor == gac_aname.minor &&
  1865. aname->build == gac_aname.build && aname->revision == gac_aname.revision);
  1866. }
  1867. else if (gac_aname.major < major)
  1868. match = FALSE;
  1869. else if (gac_aname.major == major) {
  1870. if (gac_aname.minor < minor)
  1871. match = FALSE;
  1872. else if (gac_aname.minor == minor) {
  1873. if (gac_aname.build < build)
  1874. match = FALSE;
  1875. else if (gac_aname.build == build && gac_aname.revision <= revision)
  1876. match = FALSE;
  1877. }
  1878. }
  1879. }
  1880. if (match) {
  1881. major = gac_aname.major;
  1882. minor = gac_aname.minor;
  1883. build = gac_aname.build;
  1884. revision = gac_aname.revision;
  1885. g_free (fullpath);
  1886. fullpath = g_build_path (G_DIR_SEPARATOR_S, basepath, direntry, fullname, NULL);
  1887. }
  1888. mono_assembly_name_free (&gac_aname);
  1889. }
  1890. g_dir_close (dirhandle);
  1891. if (fullpath == NULL)
  1892. return NULL;
  1893. else {
  1894. MonoAssembly *res = mono_assembly_open (fullpath, status);
  1895. g_free (fullpath);
  1896. return res;
  1897. }
  1898. }
  1899. MonoAssembly*
  1900. mono_assembly_load_with_partial_name (const char *name, MonoImageOpenStatus *status)
  1901. {
  1902. MonoAssembly *res;
  1903. MonoAssemblyName *aname, base_name;
  1904. MonoAssemblyName mapped_aname;
  1905. gchar *fullname, *gacpath;
  1906. gchar **paths;
  1907. memset (&base_name, 0, sizeof (MonoAssemblyName));
  1908. aname = &base_name;
  1909. if (!mono_assembly_name_parse (name, aname))
  1910. return NULL;
  1911. /*
  1912. * If no specific version has been requested, make sure we load the
  1913. * correct version for system assemblies.
  1914. */
  1915. if ((aname->major | aname->minor | aname->build | aname->revision) == 0)
  1916. aname = mono_assembly_remap_version (aname, &mapped_aname);
  1917. res = mono_assembly_loaded (aname);
  1918. if (res) {
  1919. mono_assembly_name_free (aname);
  1920. return res;
  1921. }
  1922. res = invoke_assembly_preload_hook (aname, assemblies_path);
  1923. if (res) {
  1924. res->in_gac = FALSE;
  1925. mono_assembly_name_free (aname);
  1926. return res;
  1927. }
  1928. fullname = g_strdup_printf ("%s.dll", aname->name);
  1929. if (extra_gac_paths) {
  1930. paths = extra_gac_paths;
  1931. while (!res && *paths) {
  1932. gacpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "lib", "mono", "gac", aname->name, NULL);
  1933. res = probe_for_partial_name (gacpath, fullname, aname, status);
  1934. g_free (gacpath);
  1935. paths++;
  1936. }
  1937. }
  1938. if (res) {
  1939. res->in_gac = TRUE;
  1940. g_free (fullname);
  1941. mono_assembly_name_free (aname);
  1942. return res;
  1943. }
  1944. gacpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (), "mono", "gac", aname->name, NULL);
  1945. res = probe_for_partial_name (gacpath, fullname, aname, status);
  1946. g_free (gacpath);
  1947. if (res)
  1948. res->in_gac = TRUE;
  1949. else {
  1950. MonoDomain *domain = mono_domain_get ();
  1951. MonoReflectionAssembly *refasm = mono_try_assembly_resolve (domain, mono_string_new (domain, name), FALSE);
  1952. if (refasm)
  1953. res = refasm->assembly;
  1954. }
  1955. g_free (fullname);
  1956. mono_assembly_name_free (aname);
  1957. return res;
  1958. }
  1959. static MonoBoolean
  1960. mono_assembly_is_in_gac (const gchar *filename)
  1961. {
  1962. const gchar *rootdir;
  1963. gchar *gp;
  1964. gchar **paths;
  1965. if (filename == NULL)
  1966. return FALSE;
  1967. for (paths = extra_gac_paths; paths && *paths; paths++) {
  1968. if (strstr (*paths, filename) != *paths)
  1969. continue;
  1970. gp = (gchar *) (filename + strlen (*paths));
  1971. if (*gp != G_DIR_SEPARATOR)
  1972. continue;
  1973. gp++;
  1974. if (strncmp (gp, "lib", 3))
  1975. continue;
  1976. gp += 3;
  1977. if (*gp != G_DIR_SEPARATOR)
  1978. continue;
  1979. gp++;
  1980. if (strncmp (gp, "mono", 4))
  1981. continue;
  1982. gp += 4;
  1983. if (*gp != G_DIR_SEPARATOR)
  1984. continue;
  1985. gp++;
  1986. if (strncmp (gp, "gac", 3))
  1987. continue;
  1988. gp += 3;
  1989. if (*gp != G_DIR_SEPARATOR)
  1990. continue;
  1991. return TRUE;
  1992. }
  1993. rootdir = mono_assembly_getrootdir ();
  1994. if (strstr (filename, rootdir) != filename)
  1995. return FALSE;
  1996. gp = (gchar *) (filename + strlen (rootdir));
  1997. if (*gp != G_DIR_SEPARATOR)
  1998. return FALSE;
  1999. gp++;
  2000. if (strncmp (gp, "mono", 4))
  2001. return FALSE;
  2002. gp += 4;
  2003. if (*gp != G_DIR_SEPARATOR)
  2004. return FALSE;
  2005. gp++;
  2006. if (strncmp (gp, "gac", 3))
  2007. return FALSE;
  2008. gp += 3;
  2009. if (*gp != G_DIR_SEPARATOR)
  2010. return FALSE;
  2011. return TRUE;
  2012. }
  2013. static MonoImage*
  2014. mono_assembly_load_publisher_policy (MonoAssemblyName *aname)
  2015. {
  2016. MonoImage *image;
  2017. gchar *filename, *pname, *name, *culture, *version, *fullpath, *subpath;
  2018. gchar **paths;
  2019. gint32 len;
  2020. if (strstr (aname->name, ".dll")) {
  2021. len = strlen (aname->name) - 4;
  2022. name = g_malloc (len);
  2023. strncpy (name, aname->name, len);
  2024. } else
  2025. name = g_strdup (aname->name);
  2026. if (aname->culture)
  2027. culture = g_utf8_strdown (aname->culture, -1);
  2028. else
  2029. culture = g_strdup ("");
  2030. pname = g_strdup_printf ("policy.%d.%d.%s", aname->major, aname->minor, name);
  2031. version = g_strdup_printf ("0.0.0.0_%s_%s", culture, aname->public_key_token);
  2032. g_free (name);
  2033. g_free (culture);
  2034. filename = g_strconcat (pname, ".dll", NULL);
  2035. subpath = g_build_path (G_DIR_SEPARATOR_S, pname, version, filename, NULL);
  2036. g_free (pname);
  2037. g_free (version);
  2038. g_free (filename);
  2039. image = NULL;
  2040. if (extra_gac_paths) {
  2041. paths = extra_gac_paths;
  2042. while (!image && *paths) {
  2043. fullpath = g_build_path (G_DIR_SEPARATOR_S, *paths,
  2044. "lib", "mono", "gac", subpath, NULL);
  2045. image = mono_image_open (fullpath, NULL);
  2046. g_free (fullpath);
  2047. paths++;
  2048. }
  2049. }
  2050. if (image) {
  2051. g_free (subpath);
  2052. return image;
  2053. }
  2054. fullpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (),
  2055. "mono", "gac", subpath, NULL);
  2056. image = mono_image_open (fullpath, NULL);
  2057. g_free (subpath);
  2058. g_free (fullpath);
  2059. return image;
  2060. }
  2061. static MonoAssemblyName*
  2062. mono_assembly_bind_version (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname, MonoAssemblyName *dest_name)
  2063. {
  2064. memcpy (dest_name, aname, sizeof (MonoAssemblyName));
  2065. dest_name->major = info->new_version.major;
  2066. dest_name->minor = info->new_version.minor;
  2067. dest_name->build = info->new_version.build;
  2068. dest_name->revision = info->new_version.revision;
  2069. return dest_name;
  2070. }
  2071. /* LOCKING: Assumes that we are already locked */
  2072. static MonoAssemblyBindingInfo*
  2073. search_binding_loaded (MonoAssemblyName *aname)
  2074. {
  2075. GSList *tmp;
  2076. for (tmp = loaded_assembly_bindings; tmp; tmp = tmp->next) {
  2077. MonoAssemblyBindingInfo *info = tmp->data;
  2078. if (assembly_binding_maps_name (info, aname))
  2079. return info;
  2080. }
  2081. return NULL;
  2082. }
  2083. static inline gboolean
  2084. info_compare_versions (AssemblyVersionSet *left, AssemblyVersionSet *right)
  2085. {
  2086. if (left->major != right->major || left->minor != right->minor ||
  2087. left->build != right->build || left->revision != right->revision)
  2088. return FALSE;
  2089. return TRUE;
  2090. }
  2091. static inline gboolean
  2092. info_versions_equal (MonoAssemblyBindingInfo *left, MonoAssemblyBindingInfo *right)
  2093. {
  2094. if (left->has_old_version_bottom != right->has_old_version_bottom)
  2095. return FALSE;
  2096. if (left->has_old_version_top != right->has_old_version_top)
  2097. return FALSE;
  2098. if (left->has_new_version != right->has_new_version)
  2099. return FALSE;
  2100. if (left->has_old_version_bottom && !info_compare_versions (&left->old_version_bottom, &right->old_version_bottom))
  2101. return FALSE;
  2102. if (left->has_old_version_top && !info_compare_versions (&left->old_version_top, &right->old_version_top))
  2103. return FALSE;
  2104. if (left->has_new_version && !info_compare_versions (&left->new_version, &right->new_version))
  2105. return FALSE;
  2106. return TRUE;
  2107. }
  2108. /* LOCKING: assumes all the necessary locks are held */
  2109. static void
  2110. assembly_binding_info_parsed (MonoAssemblyBindingInfo *info, void *user_data)
  2111. {
  2112. MonoAssemblyBindingInfo *info_copy;
  2113. GSList *tmp;
  2114. MonoAssemblyBindingInfo *info_tmp;
  2115. MonoDomain *domain = (MonoDomain*)user_data;
  2116. if (!domain)
  2117. return;
  2118. for (tmp = domain->assembly_bindings; tmp; tmp = tmp->next) {
  2119. info_tmp = tmp->data;
  2120. if (strcmp (info->name, info_tmp->name) == 0 && info_versions_equal (info, info_tmp))
  2121. return;
  2122. }
  2123. info_copy = mono_mempool_alloc0 (domain->mp, sizeof (MonoAssemblyBindingInfo));
  2124. memcpy (info_copy, info, sizeof (MonoAssemblyBindingInfo));
  2125. if (info->name)
  2126. info_copy->name = mono_mempool_strdup (domain->mp, info->name);
  2127. if (info->culture)
  2128. info_copy->culture = mono_mempool_strdup (domain->mp, info->culture);
  2129. domain->assembly_bindings = g_slist_append_mempool (domain->mp, domain->assembly_bindings, info_copy);
  2130. }
  2131. static inline gboolean
  2132. info_major_minor_in_range (MonoAssemblyBindingInfo *info, MonoAssemblyName *aname)
  2133. {
  2134. if (!info->has_old_version_bottom)
  2135. return FALSE;
  2136. if (info->old_version_bottom.major > aname->major || info->old_version_bottom.minor > aname->minor)
  2137. return FALSE;
  2138. if (info->has_old_version_top && (info->old_version_top.major < aname->major || info->old_version_top.minor < aname->minor))
  2139. return FALSE;
  2140. /* This is not the nicest way to do it, but it's a by-product of the way parsing is done */
  2141. info->major = aname->major;
  2142. info->minor = aname->minor;
  2143. return TRUE;
  2144. }
  2145. /* LOCKING: Assumes that we are already locked - both loader and domain locks */
  2146. static MonoAssemblyBindingInfo*
  2147. get_per_domain_assembly_binding_info (MonoDomain *domain, MonoAssemblyName *aname)
  2148. {
  2149. MonoAssemblyBindingInfo *info;
  2150. GSList *list;
  2151. if (!domain->assembly_bindings)
  2152. return NULL;
  2153. info = NULL;
  2154. for (list = domain->assembly_bindings; list; list = list->next) {
  2155. info = list->data;
  2156. if (info && !strcmp (aname->name, info->name) && info_major_minor_in_range (info, aname))
  2157. break;
  2158. info = NULL;
  2159. }
  2160. if (info) {
  2161. if (info->name && info->public_key_token [0] && info->has_old_version_bottom &&
  2162. info->has_new_version && assembly_binding_maps_name (info, aname))
  2163. info->is_valid = TRUE;
  2164. else
  2165. info->is_valid = FALSE;
  2166. }
  2167. return info;
  2168. }
  2169. static MonoAssemblyName*
  2170. mono_assembly_apply_binding (MonoAssemblyName *aname, MonoAssemblyName *dest_name)
  2171. {
  2172. MonoAssemblyBindingInfo *info, *info2;
  2173. MonoImage *ppimage;
  2174. MonoDomain *domain;
  2175. if (aname->public_key_token [0] == 0)
  2176. return aname;
  2177. domain = mono_domain_get ();
  2178. mono_loader_lock ();
  2179. info = search_binding_loaded (aname);
  2180. if (!info) {
  2181. mono_domain_lock (domain);
  2182. info = get_per_domain_assembly_binding_info (domain, aname);
  2183. mono_domain_unlock (domain);
  2184. }
  2185. mono_loader_unlock ();
  2186. if (info) {
  2187. if (!check_policy_versions (info, aname))
  2188. return aname;
  2189. mono_assembly_bind_version (info, aname, dest_name);
  2190. return dest_name;
  2191. }
  2192. if (domain && domain->setup && domain->setup->configuration_file) {
  2193. mono_domain_lock (domain);
  2194. if (!domain->assembly_bindings_parsed) {
  2195. gchar *domain_config_file_name = mono_string_to_utf8 (domain->setup->configuration_file);
  2196. gchar *domain_config_file_path = mono_portability_find_file (domain_config_file_name, TRUE);
  2197. if (!domain_config_file_path)
  2198. domain_config_file_path = domain_config_file_name;
  2199. mono_config_parse_assembly_bindings (domain_config_file_path, aname->major, aname->minor, domain, assembly_binding_info_parsed);
  2200. domain->assembly_bindings_parsed = TRUE;
  2201. if (domain_config_file_name != domain_config_file_path)
  2202. g_free (domain_config_file_name);
  2203. g_free (domain_config_file_path);
  2204. }
  2205. mono_domain_unlock (domain);
  2206. mono_loader_lock ();
  2207. mono_domain_lock (domain);
  2208. info2 = get_per_domain_assembly_binding_info (domain, aname);
  2209. if (info2) {
  2210. info = g_memdup (info2, sizeof (MonoAssemblyBindingInfo));
  2211. info->name = g_strdup (info2->name);
  2212. info->culture = g_strdup (info2->culture);
  2213. info->domain_id = domain->domain_id;
  2214. }
  2215. mono_domain_unlock (domain);
  2216. mono_loader_unlock ();
  2217. }
  2218. if (!info) {
  2219. info = g_new0 (MonoAssemblyBindingInfo, 1);
  2220. info->major = aname->major;
  2221. info->minor = aname->minor;
  2222. }
  2223. if (!info->is_valid) {
  2224. ppimage = mono_assembly_load_publisher_policy (aname);
  2225. if (ppimage) {
  2226. get_publisher_policy_info (ppimage, aname, info);
  2227. mono_image_close (ppimage);
  2228. }
  2229. }
  2230. /* Define default error value if needed */
  2231. if (!info->is_valid) {
  2232. info->name = g_strdup (aname->name);
  2233. info->culture = g_strdup (aname->culture);
  2234. g_strlcpy ((char *)info->public_key_token, (const char *)aname->public_key_token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  2235. }
  2236. mono_loader_lock ();
  2237. info2 = search_binding_loaded (aname);
  2238. if (info2) {
  2239. /* This binding was added by another thread
  2240. * before us */
  2241. mono_assembly_binding_info_free (info);
  2242. g_free (info);
  2243. info = info2;
  2244. } else
  2245. loaded_assembly_bindings = g_slist_prepend (loaded_assembly_bindings, info);
  2246. mono_loader_unlock ();
  2247. if (!info->is_valid || !check_policy_versions (info, aname))
  2248. return aname;
  2249. mono_assembly_bind_version (info, aname, dest_name);
  2250. return dest_name;
  2251. }
  2252. /**
  2253. * mono_assembly_load_from_gac
  2254. *
  2255. * @aname: The assembly name object
  2256. */
  2257. static MonoAssembly*
  2258. mono_assembly_load_from_gac (MonoAssemblyName *aname, gchar *filename, MonoImageOpenStatus *status, MonoBoolean refonly)
  2259. {
  2260. MonoAssembly *result = NULL;
  2261. gchar *name, *version, *culture, *fullpath, *subpath;
  2262. gint32 len;
  2263. gchar **paths;
  2264. char *pubtok;
  2265. if (aname->public_key_token [0] == 0) {
  2266. return NULL;
  2267. }
  2268. if (strstr (aname->name, ".dll")) {
  2269. len = strlen (filename) - 4;
  2270. name = g_malloc (len);
  2271. strncpy (name, aname->name, len);
  2272. } else {
  2273. name = g_strdup (aname->name);
  2274. }
  2275. if (aname->culture) {
  2276. culture = g_utf8_strdown (aname->culture, -1);
  2277. } else {
  2278. culture = g_strdup ("");
  2279. }
  2280. pubtok = g_ascii_strdown ((char*)aname->public_key_token, MONO_PUBLIC_KEY_TOKEN_LENGTH);
  2281. version = g_strdup_printf ("%d.%d.%d.%d_%s_%s", aname->major,
  2282. aname->minor, aname->build, aname->revision,
  2283. culture, pubtok);
  2284. g_free (pubtok);
  2285. subpath = g_build_path (G_DIR_SEPARATOR_S, name, version, filename, NULL);
  2286. g_free (name);
  2287. g_free (version);
  2288. g_free (culture);
  2289. if (extra_gac_paths) {
  2290. paths = extra_gac_paths;
  2291. while (!result && *paths) {
  2292. fullpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "lib", "mono", "gac", subpath, NULL);
  2293. result = mono_assembly_open_full (fullpath, status, refonly);
  2294. g_free (fullpath);
  2295. paths++;
  2296. }
  2297. }
  2298. if (result) {
  2299. result->in_gac = TRUE;
  2300. g_free (subpath);
  2301. return result;
  2302. }
  2303. fullpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (),
  2304. "mono", "gac", subpath, NULL);
  2305. result = mono_assembly_open_full (fullpath, status, refonly);
  2306. g_free (fullpath);
  2307. if (result)
  2308. result->in_gac = TRUE;
  2309. g_free (subpath);
  2310. return result;
  2311. }
  2312. MonoAssembly*
  2313. mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *status)
  2314. {
  2315. char *corlib_file;
  2316. MonoAssemblyName *aname;
  2317. if (corlib) {
  2318. /* g_print ("corlib already loaded\n"); */
  2319. return corlib;
  2320. }
  2321. #if defined(__native_client__)
  2322. if (corlibData != NULL && corlibSize != 0) {
  2323. int status = 0;
  2324. /* First "FALSE" instructs mono not to make a copy. */
  2325. /* Second "FALSE" says this is not just a ref. */
  2326. MonoImage* image = mono_image_open_from_data_full (corlibData, corlibSize, FALSE, &status, FALSE);
  2327. if (image == NULL || status != 0)
  2328. g_print("mono_image_open_from_data_full failed: %d\n", status);
  2329. corlib = mono_assembly_load_from_full (image, "mscorlib", &status, FALSE);
  2330. if (corlib == NULL || status != 0)
  2331. g_print ("mono_assembly_load_from_full failed: %d\n", status);
  2332. if (corlib)
  2333. return corlib;
  2334. }
  2335. #endif
  2336. aname = mono_assembly_name_new ("mscorlib.dll");
  2337. corlib = invoke_assembly_preload_hook (aname, assemblies_path);
  2338. mono_assembly_name_free (aname);
  2339. g_free (aname);
  2340. if (corlib != NULL)
  2341. return corlib;
  2342. if (assemblies_path) {
  2343. corlib = load_in_path ("mscorlib.dll", (const char**)assemblies_path, status, FALSE);
  2344. if (corlib)
  2345. return corlib;
  2346. }
  2347. /* Load corlib from mono/<version> */
  2348. corlib_file = g_build_filename ("mono", runtime->framework_version, "mscorlib.dll", NULL);
  2349. if (assemblies_path) {
  2350. corlib = load_in_path (corlib_file, (const char**)assemblies_path, status, FALSE);
  2351. if (corlib) {
  2352. g_free (corlib_file);
  2353. return corlib;
  2354. }
  2355. }
  2356. corlib = load_in_path (corlib_file, default_path, status, FALSE);
  2357. g_free (corlib_file);
  2358. return corlib;
  2359. }
  2360. MonoAssembly*
  2361. mono_assembly_load_full_nosearch (MonoAssemblyName *aname,
  2362. const char *basedir,
  2363. MonoImageOpenStatus *status,
  2364. gboolean refonly)
  2365. {
  2366. MonoAssembly *result;
  2367. char *fullpath, *filename;
  2368. MonoAssemblyName maped_aname;
  2369. MonoAssemblyName maped_name_pp;
  2370. int ext_index;
  2371. const char *ext;
  2372. int len;
  2373. aname = mono_assembly_remap_version (aname, &maped_aname);
  2374. /* Reflection only assemblies don't get assembly binding */
  2375. if (!refonly)
  2376. aname = mono_assembly_apply_binding (aname, &maped_name_pp);
  2377. result = mono_assembly_loaded_full (aname, refonly);
  2378. if (result)
  2379. return result;
  2380. result = refonly ? invoke_assembly_refonly_preload_hook (aname, assemblies_path) : invoke_assembly_preload_hook (aname, assemblies_path);
  2381. if (result) {
  2382. result->in_gac = FALSE;
  2383. return result;
  2384. }
  2385. /* Currently we retrieve the loaded corlib for reflection
  2386. * only requests, like a common reflection only assembly
  2387. */
  2388. if (strcmp (aname->name, "mscorlib") == 0 || strcmp (aname->name, "mscorlib.dll") == 0) {
  2389. return mono_assembly_load_corlib (mono_get_runtime_info (), status);
  2390. }
  2391. len = strlen (aname->name);
  2392. for (ext_index = 0; ext_index < 2; ext_index ++) {
  2393. ext = ext_index == 0 ? ".dll" : ".exe";
  2394. if (len > 4 && (!strcmp (aname->name + len - 4, ".dll") || !strcmp (aname->name + len - 4, ".exe"))) {
  2395. filename = g_strdup (aname->name);
  2396. /* Don't try appending .dll/.exe if it already has one of those extensions */
  2397. ext_index++;
  2398. } else {
  2399. filename = g_strconcat (aname->name, ext, NULL);
  2400. }
  2401. result = mono_assembly_load_from_gac (aname, filename, status, refonly);
  2402. if (result) {
  2403. g_free (filename);
  2404. return result;
  2405. }
  2406. if (basedir) {
  2407. fullpath = g_build_filename (basedir, filename, NULL);
  2408. result = mono_assembly_open_full (fullpath, status, refonly);
  2409. g_free (fullpath);
  2410. if (result) {
  2411. result->in_gac = FALSE;
  2412. g_free (filename);
  2413. return result;
  2414. }
  2415. }
  2416. result = load_in_path (filename, default_path, status, refonly);
  2417. if (result)
  2418. result->in_gac = FALSE;
  2419. g_free (filename);
  2420. if (result)
  2421. return result;
  2422. }
  2423. return result;
  2424. }
  2425. /**
  2426. * mono_assembly_load_full:
  2427. * @aname: A MonoAssemblyName with the assembly name to load.
  2428. * @basedir: A directory to look up the assembly at.
  2429. * @status: a pointer to a MonoImageOpenStatus to return the status of the load operation
  2430. * @refonly: Whether this assembly is being opened in "reflection-only" mode.
  2431. *
  2432. * Loads the assembly referenced by @aname, if the value of @basedir is not NULL, it
  2433. * attempts to load the assembly from that directory before probing the standard locations.
  2434. *
  2435. * If the assembly is being opened in reflection-only mode (@refonly set to TRUE) then no
  2436. * assembly binding takes place.
  2437. *
  2438. * Returns: the assembly referenced by @aname loaded or NULL on error. On error the
  2439. * value pointed by status is updated with an error code.
  2440. */
  2441. MonoAssembly*
  2442. mono_assembly_load_full (MonoAssemblyName *aname, const char *basedir, MonoImageOpenStatus *status, gboolean refonly)
  2443. {
  2444. MonoAssembly *result = mono_assembly_load_full_nosearch (aname, basedir, status, refonly);
  2445. if (!result)
  2446. /* Try a postload search hook */
  2447. result = mono_assembly_invoke_search_hook_internal (aname, refonly, TRUE);
  2448. return result;
  2449. }
  2450. /**
  2451. * mono_assembly_load:
  2452. * @aname: A MonoAssemblyName with the assembly name to load.
  2453. * @basedir: A directory to look up the assembly at.
  2454. * @status: a pointer to a MonoImageOpenStatus to return the status of the load operation
  2455. *
  2456. * Loads the assembly referenced by @aname, if the value of @basedir is not NULL, it
  2457. * attempts to load the assembly from that directory before probing the standard locations.
  2458. *
  2459. * Returns: the assembly referenced by @aname loaded or NULL on error. On error the
  2460. * value pointed by status is updated with an error code.
  2461. */
  2462. MonoAssembly*
  2463. mono_assembly_load (MonoAssemblyName *aname, const char *basedir, MonoImageOpenStatus *status)
  2464. {
  2465. return mono_assembly_load_full (aname, basedir, status, FALSE);
  2466. }
  2467. MonoAssembly*
  2468. mono_assembly_loaded_full (MonoAssemblyName *aname, gboolean refonly)
  2469. {
  2470. MonoAssembly *res;
  2471. MonoAssemblyName maped_aname;
  2472. aname = mono_assembly_remap_version (aname, &maped_aname);
  2473. res = mono_assembly_invoke_search_hook_internal (aname, refonly, FALSE);
  2474. return res;
  2475. }
  2476. /**
  2477. * mono_assembly_loaded:
  2478. * @aname: an assembly to look for.
  2479. *
  2480. * Returns: NULL If the given @aname assembly has not been loaded, or a pointer to
  2481. * a MonoAssembly that matches the MonoAssemblyName specified.
  2482. */
  2483. MonoAssembly*
  2484. mono_assembly_loaded (MonoAssemblyName *aname)
  2485. {
  2486. return mono_assembly_loaded_full (aname, FALSE);
  2487. }
  2488. void
  2489. mono_assembly_release_gc_roots (MonoAssembly *assembly)
  2490. {
  2491. if (assembly == NULL || assembly == REFERENCE_MISSING)
  2492. return;
  2493. if (assembly->dynamic) {
  2494. int i;
  2495. MonoDynamicImage *dynimg = (MonoDynamicImage *)assembly->image;
  2496. for (i = 0; i < dynimg->image.module_count; ++i)
  2497. mono_dynamic_image_release_gc_roots ((MonoDynamicImage *)dynimg->image.modules [i]);
  2498. mono_dynamic_image_release_gc_roots (dynimg);
  2499. }
  2500. }
  2501. /*
  2502. * Returns whether mono_assembly_close_finish() must be called as
  2503. * well. See comment for mono_image_close_except_pools() for why we
  2504. * unload in two steps.
  2505. */
  2506. gboolean
  2507. mono_assembly_close_except_image_pools (MonoAssembly *assembly)
  2508. {
  2509. GSList *tmp;
  2510. g_return_val_if_fail (assembly != NULL, FALSE);
  2511. if (assembly == REFERENCE_MISSING)
  2512. return FALSE;
  2513. /* Might be 0 already */
  2514. if (InterlockedDecrement (&assembly->ref_count) > 0)
  2515. return FALSE;
  2516. mono_profiler_assembly_event (assembly, MONO_PROFILE_START_UNLOAD);
  2517. mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_ASSEMBLY, "Unloading assembly %s [%p].", assembly->aname.name, assembly);
  2518. mono_debug_close_image (assembly->image);
  2519. mono_assemblies_lock ();
  2520. loaded_assemblies = g_list_remove (loaded_assemblies, assembly);
  2521. mono_assemblies_unlock ();
  2522. assembly->image->assembly = NULL;
  2523. if (!mono_image_close_except_pools (assembly->image))
  2524. assembly->image = NULL;
  2525. for (tmp = assembly->friend_assembly_names; tmp; tmp = tmp->next) {
  2526. MonoAssemblyName *fname = tmp->data;
  2527. mono_assembly_name_free (fname);
  2528. g_free (fname);
  2529. }
  2530. g_slist_free (assembly->friend_assembly_names);
  2531. g_free (assembly->basedir);
  2532. mono_profiler_assembly_event (assembly, MONO_PROFILE_END_UNLOAD);
  2533. return TRUE;
  2534. }
  2535. void
  2536. mono_assembly_close_finish (MonoAssembly *assembly)
  2537. {
  2538. g_assert (assembly && assembly != REFERENCE_MISSING);
  2539. if (assembly->image)
  2540. mono_image_close_finish (assembly->image);
  2541. if (assembly->dynamic) {
  2542. g_free ((char*)assembly->aname.culture);
  2543. } else {
  2544. g_free (assembly);
  2545. }
  2546. }
  2547. /**
  2548. * mono_assembly_close:
  2549. * @assembly: the assembly to release.
  2550. *
  2551. * This method releases a reference to the @assembly. The assembly is
  2552. * only released when all the outstanding references to it are released.
  2553. */
  2554. void
  2555. mono_assembly_close (MonoAssembly *assembly)
  2556. {
  2557. if (mono_assembly_close_except_image_pools (assembly))
  2558. mono_assembly_close_finish (assembly);
  2559. }
  2560. MonoImage*
  2561. mono_assembly_load_module (MonoAssembly *assembly, guint32 idx)
  2562. {
  2563. return mono_image_load_file_for_image (assembly->image, idx);
  2564. }
  2565. void
  2566. mono_assembly_foreach (GFunc func, gpointer user_data)
  2567. {
  2568. GList *copy;
  2569. /*
  2570. * We make a copy of the list to avoid calling the callback inside the
  2571. * lock, which could lead to deadlocks.
  2572. */
  2573. mono_assemblies_lock ();
  2574. copy = g_list_copy (loaded_assemblies);
  2575. mono_assemblies_unlock ();
  2576. g_list_foreach (loaded_assemblies, func, user_data);
  2577. g_list_free (copy);
  2578. }
  2579. /**
  2580. * mono_assemblies_cleanup:
  2581. *
  2582. * Free all resources used by this module.
  2583. */
  2584. void
  2585. mono_assemblies_cleanup (void)
  2586. {
  2587. GSList *l;
  2588. DeleteCriticalSection (&assemblies_mutex);
  2589. for (l = loaded_assembly_bindings; l; l = l->next) {
  2590. MonoAssemblyBindingInfo *info = l->data;
  2591. mono_assembly_binding_info_free (info);
  2592. g_free (info);
  2593. }
  2594. g_slist_free (loaded_assembly_bindings);
  2595. free_assembly_load_hooks ();
  2596. free_assembly_search_hooks ();
  2597. free_assembly_preload_hooks ();
  2598. }
  2599. /*LOCKING assumes loader lock is held*/
  2600. void
  2601. mono_assembly_cleanup_domain_bindings (guint32 domain_id)
  2602. {
  2603. GSList **iter = &loaded_assembly_bindings;
  2604. while (*iter) {
  2605. GSList *l = *iter;
  2606. MonoAssemblyBindingInfo *info = l->data;
  2607. if (info->domain_id == domain_id) {
  2608. *iter = l->next;
  2609. mono_assembly_binding_info_free (info);
  2610. g_free (info);
  2611. g_slist_free_1 (l);
  2612. } else {
  2613. iter = &l->next;
  2614. }
  2615. }
  2616. }
  2617. /*
  2618. * Holds the assembly of the application, for
  2619. * System.Diagnostics.Process::MainModule
  2620. */
  2621. static MonoAssembly *main_assembly=NULL;
  2622. void
  2623. mono_assembly_set_main (MonoAssembly *assembly)
  2624. {
  2625. main_assembly = assembly;
  2626. }
  2627. /**
  2628. * mono_assembly_get_main:
  2629. *
  2630. * Returns: the assembly for the application, the first assembly that is loaded by the VM
  2631. */
  2632. MonoAssembly *
  2633. mono_assembly_get_main (void)
  2634. {
  2635. return (main_assembly);
  2636. }
  2637. /**
  2638. * mono_assembly_get_image:
  2639. * @assembly: The assembly to retrieve the image from
  2640. *
  2641. * Returns: the MonoImage associated with this assembly.
  2642. */
  2643. MonoImage*
  2644. mono_assembly_get_image (MonoAssembly *assembly)
  2645. {
  2646. return assembly->image;
  2647. }
  2648. void
  2649. mono_register_bundled_assemblies (const MonoBundledAssembly **assemblies)
  2650. {
  2651. bundles = assemblies;
  2652. }
  2653. #define MONO_DECLSEC_FORMAT_10 0x3C
  2654. #define MONO_DECLSEC_FORMAT_20 0x2E
  2655. #define MONO_DECLSEC_FIELD 0x53
  2656. #define MONO_DECLSEC_PROPERTY 0x54
  2657. #define SKIP_VISIBILITY_XML_ATTRIBUTE ("\"SkipVerification\"")
  2658. #define SKIP_VISIBILITY_ATTRIBUTE_NAME ("System.Security.Permissions.SecurityPermissionAttribute")
  2659. #define SKIP_VISIBILITY_ATTRIBUTE_SIZE (sizeof (SKIP_VISIBILITY_ATTRIBUTE_NAME) - 1)
  2660. #define SKIP_VISIBILITY_PROPERTY_NAME ("SkipVerification")
  2661. #define SKIP_VISIBILITY_PROPERTY_SIZE (sizeof (SKIP_VISIBILITY_PROPERTY_NAME) - 1)
  2662. static gboolean
  2663. mono_assembly_try_decode_skip_verification_param (const char *p, const char **resp, gboolean *abort_decoding)
  2664. {
  2665. int len;
  2666. switch (*p++) {
  2667. case MONO_DECLSEC_PROPERTY:
  2668. break;
  2669. case MONO_DECLSEC_FIELD:
  2670. default:
  2671. *abort_decoding = TRUE;
  2672. return FALSE;
  2673. break;
  2674. }
  2675. if (*p++ != MONO_TYPE_BOOLEAN) {
  2676. *abort_decoding = TRUE;
  2677. return FALSE;
  2678. }
  2679. /* property name length */
  2680. len = mono_metadata_decode_value (p, &p);
  2681. if (len >= SKIP_VISIBILITY_PROPERTY_SIZE && !memcmp (p, SKIP_VISIBILITY_PROPERTY_NAME, SKIP_VISIBILITY_PROPERTY_SIZE)) {
  2682. p += len;
  2683. return *p;
  2684. }
  2685. p += len + 1;
  2686. *resp = p;
  2687. return FALSE;
  2688. }
  2689. static gboolean
  2690. mono_assembly_try_decode_skip_verification (const char *p, const char *endn)
  2691. {
  2692. int i, j, num, len, params_len;
  2693. if (*p == MONO_DECLSEC_FORMAT_10) {
  2694. gsize read, written;
  2695. char *res = g_convert (p, endn - p, "UTF-8", "UTF-16LE", &read, &written, NULL);
  2696. if (res) {
  2697. gboolean found = strstr (res, SKIP_VISIBILITY_XML_ATTRIBUTE) != NULL;
  2698. g_free (res);
  2699. return found;
  2700. }
  2701. return FALSE;
  2702. }
  2703. if (*p++ != MONO_DECLSEC_FORMAT_20)
  2704. return FALSE;
  2705. /* number of encoded permission attributes */
  2706. num = mono_metadata_decode_value (p, &p);
  2707. for (i = 0; i < num; ++i) {
  2708. gboolean is_valid = FALSE;
  2709. gboolean abort_decoding = FALSE;
  2710. /* attribute name length */
  2711. len = mono_metadata_decode_value (p, &p);
  2712. /* We don't really need to fully decode the type. Comparing the name is enough */
  2713. is_valid = len >= SKIP_VISIBILITY_ATTRIBUTE_SIZE && !memcmp (p, SKIP_VISIBILITY_ATTRIBUTE_NAME, SKIP_VISIBILITY_ATTRIBUTE_SIZE);
  2714. p += len;
  2715. /*size of the params table*/
  2716. params_len = mono_metadata_decode_value (p, &p);
  2717. if (is_valid) {
  2718. const char *params_end = p + params_len;
  2719. /* number of parameters */
  2720. len = mono_metadata_decode_value (p, &p);
  2721. for (j = 0; j < len; ++j) {
  2722. if (mono_assembly_try_decode_skip_verification_param (p, &p, &abort_decoding))
  2723. return TRUE;
  2724. if (abort_decoding)
  2725. break;
  2726. }
  2727. p = params_end;
  2728. } else {
  2729. p += params_len;
  2730. }
  2731. }
  2732. return FALSE;
  2733. }
  2734. gboolean
  2735. mono_assembly_has_skip_verification (MonoAssembly *assembly)
  2736. {
  2737. MonoTableInfo *t;
  2738. guint32 cols [MONO_DECL_SECURITY_SIZE];
  2739. const char *blob;
  2740. int i, len;
  2741. if (MONO_SECMAN_FLAG_INIT (assembly->skipverification))
  2742. return MONO_SECMAN_FLAG_GET_VALUE (assembly->skipverification);
  2743. t = &assembly->image->tables [MONO_TABLE_DECLSECURITY];
  2744. for (i = 0; i < t->rows; ++i) {
  2745. mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
  2746. if ((cols [MONO_DECL_SECURITY_PARENT] & MONO_HAS_DECL_SECURITY_MASK) != MONO_HAS_DECL_SECURITY_ASSEMBLY)
  2747. continue;
  2748. if (cols [MONO_DECL_SECURITY_ACTION] != SECURITY_ACTION_REQMIN)
  2749. continue;
  2750. blob = mono_metadata_blob_heap (assembly->image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
  2751. len = mono_metadata_decode_blob_size (blob, &blob);
  2752. if (!len)
  2753. continue;
  2754. if (mono_assembly_try_decode_skip_verification (blob, blob + len)) {
  2755. MONO_SECMAN_FLAG_SET_VALUE (assembly->skipverification, TRUE);
  2756. return TRUE;
  2757. }
  2758. }
  2759. MONO_SECMAN_FLAG_SET_VALUE (assembly->skipverification, FALSE);
  2760. return FALSE;
  2761. }