From 85dd81f4dba1e6321672becbe6e1bcc66ce7f836 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Wed, 5 Jun 2024 10:00:44 -0700 Subject: [PATCH 1/4] Add comments about .NET unload --- core/hosting/src/NativeHost/nativehost.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/hosting/src/NativeHost/nativehost.cpp b/core/hosting/src/NativeHost/nativehost.cpp index 0fd473025fd..385418913fa 100644 --- a/core/hosting/src/NativeHost/nativehost.cpp +++ b/core/hosting/src/NativeHost/nativehost.cpp @@ -309,6 +309,9 @@ namespace return false; // Load hostfxr and get desired exports + // NOTE: The .NET Runtime does not support unloading any of its native libraries. Running + // dlclose/FreeLibrary on any .NET libraries produces undefined behavior. To unload managed assemblies, + // use an Unloadable AssemblyLoadContext. void *lib = load_library(buffer); init_for_cmd_line_fptr = (hostfxr_initialize_for_dotnet_command_line_fn)get_export(lib, "hostfxr_initialize_for_dotnet_command_line"); init_for_config_fptr = (hostfxr_initialize_for_runtime_config_fn)get_export(lib, "hostfxr_initialize_for_runtime_config"); From be5858a4eb0b887898663a970d46fda32b361d61 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Wed, 5 Jun 2024 14:13:36 -0700 Subject: [PATCH 2/4] Update LoadLibrary.c --- core/nativeaot/NativeLibrary/LoadLibrary.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/nativeaot/NativeLibrary/LoadLibrary.c b/core/nativeaot/NativeLibrary/LoadLibrary.c index bae153b8295..e4dedbab995 100644 --- a/core/nativeaot/NativeLibrary/LoadLibrary.c +++ b/core/nativeaot/NativeLibrary/LoadLibrary.c @@ -68,8 +68,7 @@ int callSumFunc(char *path, char *funcName, int firstInt, int secondInt) int result = MyImport(firstInt, secondInt); - // CoreRT libraries do not support unloading - // See https://github.com/dotnet/corert/issues/7887 + // NOTE: Native AOT libraries do not support unloading return result; } From 998f96c1a16820c2efc607fdb5e26d48d9b293f9 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Thu, 6 Jun 2024 10:28:06 -0700 Subject: [PATCH 3/4] Respond to PR comments --- core/hosting/src/NativeHost/nativehost.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/hosting/src/NativeHost/nativehost.cpp b/core/hosting/src/NativeHost/nativehost.cpp index 385418913fa..981594689ee 100644 --- a/core/hosting/src/NativeHost/nativehost.cpp +++ b/core/hosting/src/NativeHost/nativehost.cpp @@ -310,8 +310,7 @@ namespace // Load hostfxr and get desired exports // NOTE: The .NET Runtime does not support unloading any of its native libraries. Running - // dlclose/FreeLibrary on any .NET libraries produces undefined behavior. To unload managed assemblies, - // use an Unloadable AssemblyLoadContext. + // dlclose/FreeLibrary on any .NET libraries produces undefined behavior. void *lib = load_library(buffer); init_for_cmd_line_fptr = (hostfxr_initialize_for_dotnet_command_line_fn)get_export(lib, "hostfxr_initialize_for_dotnet_command_line"); init_for_config_fptr = (hostfxr_initialize_for_runtime_config_fn)get_export(lib, "hostfxr_initialize_for_runtime_config"); From 2eaa262820493604c8af0a69d125b93e4cc7b5c4 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Thu, 6 Jun 2024 17:33:41 +0000 Subject: [PATCH 4/4] Also update README --- core/nativeaot/NativeLibrary/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/nativeaot/NativeLibrary/README.md b/core/nativeaot/NativeLibrary/README.md index 2a4a7ca5ad2..01aa97e1f3b 100644 --- a/core/nativeaot/NativeLibrary/README.md +++ b/core/nativeaot/NativeLibrary/README.md @@ -65,6 +65,8 @@ The last thing to do is to actually call the method we have imported. int result = MyImport(5,3); ``` +Note that the .NET Runtime does not support unloading. Once a handle to the shared library is created, the library cannot be closed with `dlclose/FreeLibrary`. + ## Exporting methods For a C# method in the native library to be consumable by external programs, it has to be explicitly exported using the `[UnmanagedCallersOnly]` attribute.