Skip to content

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

Closed

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.

Activity

  1. neuecc commented on Mar 2, 2020

    @neuecc
    Member

    Normally generated resolve includes required enum types.
    Maybe your GCResolver does not include it.

  2. clarvalon commented on Mar 3, 2020

    @clarvalon
    Author

    I went through all my enums and most have their own formatter but some do not. I'm not yet sure of the reason. Yet when I temporarily marked them with [IgnoreMember] and regenerated the AOT Resolver the problem still occurred.

    This may be that this is a duplicate of #797 but I'm finding it tricky to diagnose due it being several levels removed (using mpc.exe, having benchmarkdotnet generate projects and debugging with CoreRT is already tricky). I'll see if I can figure out the minimum steps required to reproduce the issue.

  3. clarvalon commented on Mar 7, 2020

    @clarvalon
    Author

    I was able to fix my issue by compiling MessagePack manually using the following conditional compilation symbols:

    • NET_STANDARD_2_0
    • UNITY_2018_3_OR_NEWER
    • ENABLE_IL2CPP

    This removed all the Emit references. It looks like milestone v2.2 focuses on getting MessagePack working better with AOT platforms. Does this mean at some point there will be a seperate nuget package, or will a manual build always be required?

    As an aside, I wrote a bit about my MessagePack benchmarks here - I was astonished at the CoreRT performance: https://clarvalon.blogspot.com/2020/03/experimenting-with-messagepack.html

  4. AArnott commented on Mar 7, 2020

    @AArnott
    Collaborator

    I'd personally like to see the one nuget package carry everything it needs to be successful everywhere (except Unity, I guess, since ... that's always the odd one out).

  5. clarvalon commented on Mar 11, 2020

    @clarvalon
    Author

    I'll wait and see what the long term approach is - in the meantime it's simple enough to maintain a fork for my own purposes, and have it as a submodule. So I'll close this off.

    I did encounter a secondary issue when publishing using CoreRT, as when the mpc MSBuild task runs it breaks some of the formatters (weirdly only anything using DateTime or TimeSpan, where the full namespace would become absent). This may be related to the linker somehow - manually disabling the MSBuild task for CoreRT builds was a temporary workaround.

    Otherwise I was able to demonstrate the benefits in a real world example - pretty pleased with how it has gone: http://clarvalon.blogspot.com/2020/03/corert-and-messagepack-real-world.html

  6. cubed-it commented on Jul 13, 2021

    @cubed-it

    Why was this issue closed? It is still a problem. Or how can I create an instance of MessagePackSerializerOptions without also accessing MessagePackSerializerOptions.Standard?

  7. added this to the v2.4 milestone on Aug 22, 2021
  8. added a commit that references this issue on Aug 22, 2021
    7b84994
  9. self-assigned this
    on Aug 22, 2021
  10. AArnott commented on Aug 22, 2021

    @AArnott
    Collaborator

    @cubed-it I've re-opened it and submitted a PR that I think should unblock you.

  11. added a commit that references this issue on Aug 22, 2021
    49fa946
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions