Skip to content

DMDEmitMethodBuilderGenerator crashes Mono when saving generated assembly #299

@Ved-s

Description

@Ved-s

Description

Saving methods generated by DMDEmitMethodBuilderGenerator on Mono crashes Mono due to passing empty array to DefineMethod type modifiers parameters instead of nulls

Mono crashes with * Assertion at sre-encode.c:290, condition 'count > 0' not met

MonoMod logs:

Detected runtime: Mono 6.8.0.105 using Framework corelib

Examples

var assemblyName = new AssemblyName("Example");
var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
var dynamicModule = assemblyBuilder.DefineDynamicModule(assemblyName.Name, assemblyName.Name + ".dll");
var typeBuilder = dynamicModule.DefineType("Test");

var dmd = new DynamicMethodDefinition("Test", typeof(void), []);

var il = dmd.GetILGenerator();
il.Emit(OpCodes.Ret);

DMDEmitMethodBuilderGenerator.GenerateMethodBuilder(dmd, typeBuilder);

typeBuilder.CreateType();
assemblyBuilder.Save("Example.dll");
Switches.SetSwitchValue("DMDType", "methodbuilder");
Switches.SetSwitchValue("DMDDumpTo", "Example");
DynamicMethodDefinition dmd = new("Test", typeof(void), []);

var il = dmd.GetILGenerator();
il.Emit(OpCodes.Ret);

dmd.Generate();

Example of working DefineMethod call

typeBuilder.DefineMethod(
    "Test", 
    MethodAttributes.Public | MethodAttributes.Static, 
    CallingConventions.Standard,
    typeof(void), null, null, [], [], []
);

and example of crashing DefineMethod call

typeBuilder.DefineMethod(
    "Test", 
    MethodAttributes.Public | MethodAttributes.Static, 
    CallingConventions.Standard,
    typeof(void), [], [], [], [], []
);

do note that parameter modifiers are also affected, so this works

typeBuilder.DefineMethod(
    "Test", 
    MethodAttributes.Public | MethodAttributes.Static, 
    CallingConventions.Standard,
    typeof(void), 
    null, null, 
    [typeof(int)], /* parameter types */
    [null], [null]
);

and this crashes

typeBuilder.DefineMethod(
    "Test", 
    MethodAttributes.Public | MethodAttributes.Static, 
    CallingConventions.Standard,
    typeof(void), 
    null, null, 
    [typeof(int)], /* parameter types */
    [[]], [[]]
);

Fix suggestion

I suggest making _DMDEmit.ResolveWithModifiers return nulls instead of empty arrays on Mono

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions