Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert failure in SetLoaderAllocator() when calling a DynamicMethod with collectible AssemblyLoadContext #110987

Open
steveharter opened this issue Dec 29, 2024 · 1 comment · May be fixed by #111038
Assignees
Labels
area-AssemblyLoader-coreclr in-pr There is an active PR which will close this issue when it is merged untriaged New issue has not been triaged by the area owner

Comments

@steveharter
Copy link
Member

Using a checked runtime, the sample below fails with Assert in "_ASSERTE(pAllocator == GetLoaderAllocator())" in MethodTable::SetLoaderAllocator.

using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Loader;

namespace ConsoleApp1
{
    public class Program
    {
        static void Main()
        {
            string tempFilePath = Path.GetTempFileName() + ".dll";

            // Create a simple type; this doesn't need to be run in the same process for the Assert to fail.
            {
                PersistedAssemblyBuilder ab = new PersistedAssemblyBuilder(new AssemblyName("MyAssembly"), typeof(object).Assembly);
                TypeBuilder typeBuilder = ab.DefineDynamicModule("MyModule").DefineType("MyType", TypeAttributes.Public | TypeAttributes.Class);
                MethodBuilder myMethod = typeBuilder.DefineMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard);
                ILGenerator ilGenerator = myMethod.GetILGenerator();
                ilGenerator.Emit(OpCodes.Ret);
                typeBuilder.CreateType();
                ab.Save(tempFilePath);
            }

            {
                TestAssemblyLoadContext tlc = new TestAssemblyLoadContext();

                Type typeFromDisk = tlc.LoadFromAssemblyPath(tempFilePath).GetType("MyType")!;
                // This also fails with the Assert (instead of above):
                //Type typeFromDisk = Assembly.LoadFrom(tempFilePath).GetType("MyType")!;

                MethodInfo methodFromDisk = typeFromDisk.GetMethod("MyMethod")!;

                DynamicMethod callIt = new DynamicMethod(
                    "CallIt",
                    MethodAttributes.Public | MethodAttributes.Static,
                    CallingConventions.Standard,
                    returnType: typeof(void),
                    parameterTypes: null,
                    m: typeFromDisk.Module, // Using typeof(object).Module works
                    skipVisibility: true);

                ILGenerator il = callIt.GetILGenerator();
                il.Emit(OpCodes.Call, methodFromDisk);
                il.Emit(OpCodes.Ret);

                Action invoker = (Action)callIt.CreateDelegate(typeof(Action));
 
                // This fails Assert in checked runtime "_ASSERTE(pAllocator == GetLoaderAllocator())" in MethodTable::SetLoaderAllocator:
                invoker();

                Console.WriteLine("Success");
                Console.ReadLine();
            }
        }
    }

    class TestAssemblyLoadContext : AssemblyLoadContext
    {
        public TestAssemblyLoadContext() : base(isCollectible: true) // Using "isCollectible : false" works
        {
        }
    }
}
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 29, 2024
Copy link
Contributor

Tagging subscribers to this area: @vitek-karas, @agocke, @VSadov
See info in area-owners.md if you want to be subscribed.

@steveharter steveharter changed the title Assert failure in SetLoaderAllocator() when calling a DynamicMethod in separate module and custom AssemblyLoadContext Assert failure in SetLoaderAllocator() when calling a DynamicMethod with collectible AssemblyLoadContext Dec 30, 2024
@janvorli janvorli self-assigned this Jan 2, 2025
janvorli added a commit to janvorli/runtime that referenced this issue Jan 2, 2025
There is one `DynamicMethodTable` allocated per `Module` if needed. When its
instance is being created, an incorrect `LoaderAllocator` is passed to the
`CreateMinimalMethodTable` function. Instead of the `Module`'s
`LoaderAllocator`, it passes in the default one.
That fires an assert in non-release builds of the runtime and it results in
incorrect marking of the `MethodTable` as not collectible in case the `Module`'s
LoaderAllocator is collectible.

This change fixes it by passing in the `LoaderAllocator` of the module
instead.

Close dotnet#110987
@janvorli janvorli linked a pull request Jan 2, 2025 that will close this issue
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-AssemblyLoader-coreclr in-pr There is an active PR which will close this issue when it is merged untriaged New issue has not been triaged by the area owner
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

2 participants