Skip to content

Commit edb161a

Browse files
authored
[mono][aot] Load AOT module of a container assembly using assembly name (#83711)
* Load AOT module of a container assembly using assembly name * Use mono_image_init to init the image * Implement mono_loader_lock on load_container_amodule * Avoid recursive invocation by setting container_assm_name to NULL
1 parent 93dda3b commit edb161a

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

src/mono/mono/mini/aot-runtime.c

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,35 +2433,46 @@ mono_aot_init (void)
24332433
/*
24342434
* load_container_amodule:
24352435
*
2436-
* Load the container assembly and its AOT image.
2436+
* Load AOT module of a container assembly
24372437
*/
24382438
static void
24392439
load_container_amodule (MonoAssemblyLoadContext *alc)
24402440
{
2441-
ERROR_DECL (error);
2442-
2441+
// If container_amodule loaded, don't lock the runtime
24432442
if (!container_assm_name || container_amodule)
24442443
return;
24452444

2446-
char *local_ref = container_assm_name;
2447-
container_assm_name = NULL;
2448-
MonoImageOpenStatus status = MONO_IMAGE_OK;
2449-
MonoAssemblyOpenRequest req;
2450-
gchar *dll = g_strdup_printf ( "%s.dll", local_ref);
2451-
/*
2452-
* Don't fire managed assembly load events whose execution
2453-
* might require this module to be already loaded.
2454-
*/
2455-
mono_assembly_request_prepare_open (&req, alc);
2456-
req.request.no_managed_load_event = TRUE;
2457-
MonoAssembly *assm = mono_assembly_request_open (dll, &req, &status);
2458-
if (!assm) {
2459-
gchar *exe = g_strdup_printf ("%s.exe", local_ref);
2460-
assm = mono_assembly_request_open (exe, &req, &status);
2461-
}
2462-
g_assert (assm);
2463-
load_aot_module (alc, assm, NULL, error);
2464-
container_amodule = assm->image->aot_module;
2445+
mono_loader_lock ();
2446+
// There might be several threads that passed the first check
2447+
// Adding another check to ensure single load of a container assembly due to race condition
2448+
if (!container_amodule) {
2449+
ERROR_DECL (error);
2450+
2451+
// This method is recursively invoked within the same thread during AOT module loads
2452+
// It avoids recursive invocation by setting container_assm_name to NULL
2453+
char *local_ref = container_assm_name;
2454+
container_assm_name = NULL;
2455+
2456+
// Create a fake MonoAssembly/MonoImage to retrieve its AOT module.
2457+
// Container MonoAssembly/MonoImage shouldn't be used during the runtime.
2458+
MonoAssembly *assm = g_new0 (MonoAssembly, 1);
2459+
assm->image = g_new0 (MonoImage, 1);
2460+
assm->image->dynamic = 0;
2461+
assm->image->alc = alc;
2462+
assm->aname.name = local_ref;
2463+
2464+
mono_image_init (assm->image);
2465+
MonoAotFileInfo* info = (MonoAotFileInfo *)g_hash_table_lookup (static_aot_modules, assm->aname.name);
2466+
assm->image->guid = (char*)info->assembly_guid;
2467+
mono_assembly_addref (assm);
2468+
2469+
load_aot_module(alc, assm, NULL, error);
2470+
mono_memory_barrier ();
2471+
g_assert (assm->image->aot_module);
2472+
container_amodule = assm->image->aot_module;
2473+
}
2474+
2475+
mono_loader_unlock ();
24652476
}
24662477

24672478
static gboolean

0 commit comments

Comments
 (0)