Skip to content

Commit

Permalink
Fix dotnet#53181. AssemblyLoadContext incorrectly validates name of d…
Browse files Browse the repository at this point in the history
…ynamic assembly when returned from Load
  • Loading branch information
Ap0k committed May 25, 2021
1 parent 7d2c493 commit f9056b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private static IntPtr ResolveUnmanagedDllUsingEvent(string unmanagedDllName, Ass

AssemblyLoadContext? loadContextForAssembly = null;

RuntimeAssembly? rtAsm = assembly as RuntimeAssembly;
RuntimeAssembly? rtAsm = GetRuntimeAssembly(assembly);

// We only support looking up load context for runtime assemblies.
if (rtAsm != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ private static Assembly ValidateAssemblyNameWithSimpleName(Assembly assembly, st
// Derived type's Load implementation is expected to use one of the LoadFrom* methods to get the assembly
// which is a RuntimeAssembly instance. However, since Assembly type can be used build any other artifact (e.g. AssemblyBuilder),
// we need to check for RuntimeAssembly.
RuntimeAssembly? rtLoadedAssembly = assembly as RuntimeAssembly;
RuntimeAssembly? rtLoadedAssembly = GetRuntimeAssembly(assembly);
if (rtLoadedAssembly != null)
{
loadedSimpleName = rtLoadedAssembly.GetSimpleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Loader;
using System.IO;
using System.Linq;
using My;

namespace My
{
Expand Down Expand Up @@ -200,6 +203,33 @@ public static void CustomName()
}
}

public static void GetLoadContextForDynamicAssembly(bool isCollectible)
{
try
{
Console.WriteLine($"{nameof(GetLoadContextForDynamicAssembly)}; isCollectible={isCollectible}");

AssemblyLoadContext alc = new AssemblyLoadContext($"ALC - {isCollectible}", isCollectible);
AssemblyBuilder assemblyBuilder;
AssemblyName assemblyName = new AssemblyName($"DynamicAssembly_{Guid.NewGuid():N}");

using (alc.EnterContextualReflection())
{
assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndCollect);
}

AssemblyLoadContext? context = AssemblyLoadContext.GetLoadContext(assemblyBuilder);

Assert(context != null);
Assert(alc == context);
Assert(alc.Assemblies.Any(a => AssemblyName.ReferenceMatchesDefinition(a.GetName(), assemblyName)));
}
catch (Exception e)
{
Assert(false, e.ToString());
}
}

public static int Main()
{
foreach (AssemblyLoadContext alc in AssemblyLoadContext.All)
Expand All @@ -216,6 +246,8 @@ public static int Main()
AssemblyLoadByteArrayName();
CustomWOName();
CustomName();
GetLoadContextForDynamicAssembly(true);
GetLoadContextForDynamicAssembly(false);

foreach (AssemblyLoadContext alc in AssemblyLoadContext.All)
{
Expand Down

0 comments on commit f9056b8

Please sign in to comment.