MonoMod
MonoMod copied to clipboard
Import different assemblies with the same name breaks MonoMod Reflection Importer/Resolver
Description
see title.
Here is what I've found: https://github.com/MonoMod/MonoMod/blob/bc177577c97ff9d52074f258f2d0735cc2416603/src/MonoMod.Utils/MMReflectionImporter.cs#L121 Imported assembly name is cached. If a different assembliy with the same name was imported, it would modify the previous hash.
Example
class MyClass
{
public static void Main(string[] args)
{
AssemblyLoadContext a = new("a");
var x = a.LoadFromAssemblyPath(typeof(MyClass).Assembly.Location);
DynamicMethodDefinition dmd = new("", typeof(object), []);
ILContext il = new(dmd.Definition);
var c = il.Import(x.EntryPoint);
var d = il.Import(typeof(MyClass).Assembly.EntryPoint);
var e = c.ResolveReflection() == d.ResolveReflection();
if (e)
{
throw new Exception();
}
}
}
Intended: nothing happen (c and d corresponding to the different methodinfo)
Actual: crash
MonoMod doesn't work well with ALCs in general. The linked PR has tests for this.