Skip to content

Commit

Permalink
Use same pattern for loading unmanaged assemblies as managed assemblies.
Browse files Browse the repository at this point in the history
Loading of unmanaged assemblies suffers from the same issues as loading managed assemblies in that the wrong deps.json is used by default.  This updates the TestAssemblyLoadContext to use the exact same pattern for loading unmanaged assemblies as managed assemblies.
1. Use the default assembly loading logic (I'm unsure if we actually need this since, in both managed and unmanaged, the base AssemblyLoadContext logic is a no-op and it's actually the VM that has some logic for loading assemblies).
2. Use an AssemblyDependencyResolver for the test assembly.
3. Check in the same folder as the test assembly (in case the dependencies are not fully specified in deps.json).

Resolves #1253
  • Loading branch information
veleek committed Dec 15, 2023
1 parent 945090c commit 462d9d2
Showing 1 changed file with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if NETCOREAPP3_1_OR_GREATER

using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.IO;
using System;
Expand Down Expand Up @@ -56,7 +57,7 @@ protected override Assembly Load(AssemblyName name)
if (loadedAssembly != null)
{
log.Info("Assembly {0} ({1}) is loaded using the TestAssembliesResolver", name, GetAssemblyLocationInfo(loadedAssembly));

return loadedAssembly;
}

Expand All @@ -79,6 +80,45 @@ protected override Assembly Load(AssemblyName name)
return loadedAssembly;
}

protected override IntPtr LoadUnmanagedDll(string name)
{
log.Debug("Loading {0} unmanaged dll", name);

IntPtr loadedDllHandle = base.LoadUnmanagedDll(name);
if (loadedDllHandle != IntPtr.Zero)
{
log.Info("Unmanaged DLL {0} is loaded using default base.LoadUnmanagedDll()", name);
return loadedDllHandle;
}

string runtimeResolverPath = _runtimeResolver.ResolveUnmanagedDllToPath(name);
if (string.IsNullOrEmpty(runtimeResolverPath) == false &&
File.Exists(runtimeResolverPath))
{
loadedDllHandle = LoadUnmanagedDllFromPath(runtimeResolverPath);
}

if (loadedDllHandle != IntPtr.Zero)
{
log.Info("Unmanaged DLL {0} ({1}) is loaded using the deps.json info", name, runtimeResolverPath);
return loadedDllHandle;
}

string unmanagedDllPath = Path.Combine(_basePath, name + ".dll");
if (File.Exists(unmanagedDllPath))
{
loadedDllHandle = LoadUnmanagedDllFromPath(unmanagedDllPath);
}

if (loadedDllHandle != IntPtr.Zero)
{
log.Info("Unmanaged DLL {0} ({1}) is loaded using base path", name, unmanagedDllPath);
return loadedDllHandle;
}

return IntPtr.Zero;
}

private static string GetAssemblyLocationInfo(Assembly assembly)
{
if (assembly.IsDynamic)
Expand Down

0 comments on commit 462d9d2

Please sign in to comment.