Skip to content

CoreRT: DynamicEnumResolver being used even when not using StandardResolver #832

@clarvalon

Description

@clarvalon

Bug description

I'm running some benchmarks using BenchmarkDotNet (Xml vs Protobuf-net vs MessagePack-CSharp AOT). Everything works fine in .NET Core 3.1. When I run the benchmarks using the CoreRT runtime, I get an 'Dynamic code generation is not supported on this platform' error due to DynamicEnumResolver, despite avoiding using the StandardResolver.

Repro steps

  1. Generate custom resolver using mpc.exe (here GCResolver via MSBuild task).
  2. Setup a resolver that does not include StandardResolver that should be friendly to AOT runtimes like CoreRT:
StaticCompositeResolver.Instance.Register(
    MessagePack.Resolvers.GCResolver.Instance,
    MessagePack.Resolvers.BuiltinResolver.Instance,
    MessagePack.Resolvers.AttributeFormatterResolver.Instance,
    MessagePack.Resolvers.PrimitiveObjectResolver.Instance
);

var aotOption = MessagePackSerializerOptions.Standard.WithResolver(StaticCompositeResolver.Instance);
MessagePackSerializer.DefaultOptions = aotOption;
  1. Deserialize messagepack payload:
var bytes = File.ReadAllBytes(GetPayloadFilename(MP_AOT));
var item = MessagePackSerializer.Deserialize<GContent>(bytes);

Expected behaviour

Item deserialised successfully.

Actual behaviour

System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
 ---> System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
 ---> System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
 ---> System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
 ---> System.PlatformNotSupportedException: Dynamic code generation is not supported on this platform.
   at System.Reflection.Emit.ReflectionEmitThrower.ThrowPlatformNotSupportedException() + 0x35
   at MessagePack.Internal.DynamicAssembly..ctor(String) + 0x40
   at MessagePack.Resolvers.DynamicEnumResolver..cctor() + 0x50
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xcc
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x17f
   at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd
   at MessagePack.Internal.StandardResolverHelper..cctor() + 0x47
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xcc
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x17f
   at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd
   at MessagePack.Resolvers.StandardResolver..cctor() + 0x13
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xcc
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x17f
   at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd
   at MessagePack.MessagePackSerializerOptions.MessagePackSerializerOptionsDefaultSettingsLazyInitializationHelper..cctor() + 0x19
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xcc
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x17f
   at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0xd
   at TestMPack.DeserializationTest.MessagePackCSharp_AOT() + 0xb1
   at BenchmarkDotNet.Autogenerated.Runnable_0.WorkloadActionNoUnroll(Int64) + 0x2d
   at BenchmarkDotNet.Engines.Engine.RunIteration(IterationData) + 0x1b2
   at BenchmarkDotNet.Engines.EngineFactory.Jit(Engine, Int32, Int32, Int32) + 0x18c
   at BenchmarkDotNet.Engines.EngineFactory.CreateReadyToRun(EngineParameters) + 0x14b
   at BenchmarkDotNet.Autogenerated.Runnable_0.Run(IHost, String) + 0x404
   at BenchmarkDotNet.Autogenerated.UniqueProgramName.AfterAssemblyLoadingAttached(String[]) + 0x12d
  • Version used: MessagePack (2.1.90), MessagePack.MSBuild.Tasks (2.1.90)
  • Runtime: CoreRT

Additional context

I've tried various other Resolver combinations (e.g. inserting DynamicEnumAsStringResolver) but nothing seems to be working for CoreRT.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions