MonoMod icon indicating copy to clipboard operation
MonoMod copied to clipboard

Import different assemblies with the same name breaks MonoMod Reflection Importer/Resolver

Open wuke32767 opened this issue 9 months ago • 1 comments

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

wuke32767 avatar Apr 11 '25 03:04 wuke32767

MonoMod doesn't work well with ALCs in general. The linked PR has tests for this.

nike4613 avatar Apr 11 '25 03:04 nike4613