Skip to content

NativeAOT runtime crash with source-generated serialization ('Failed to create generic virtual method implementation') #113664

Closed

Description

@Nolram12345

While attempting to serialize a class containing a custom generic container containing an array of classes through a shared interface using the MemoryPack library for source-generated binary serialization, I have been running into a runtime crash I suspect may be a possible compiler bug:

Process terminated. Failed to create generic virtual method implementation

Declaring type: MemoryPack.Formatters.MemoryPackableFormatter`1<GameCore.Scenes.Scene>
Method name: Serialize
Instantiation:
  Argument 00000000: MemoryPack.Internal.ReusableLinkedArrayBufferWriter

   at System.RuntimeExceptionHelpers.FailFast(String, Exception, String, RhFailFastReason, IntPtr, IntPtr) + 0x249
   at Internal.Runtime.TypeLoader.TypeLoaderEnvironment.ResolveGenericVirtualMethodTarget(RuntimeTypeHandle, RuntimeMethodHandle) + 0x256
   at System.Runtime.TypeLoaderExports.<>c.<GVMLookupForSlotSlow>b__8_0(IntPtr, IntPtr, Object, IntPtr&) + 0x3e
   at System.Runtime.TypeLoaderExports.CacheMiss(IntPtr, IntPtr, RuntimeObjectFactory, Object) + 0x3c
   at System.Runtime.TypeLoaderExports.GVMLookupForSlotSlow(Object, RuntimeMethodHandle) + 0x68
   at System.Runtime.TypeLoaderExports.GVMLookupForSlot(Object, RuntimeMethodHandle) + 0x91
   at MemoryPack.MemoryPackWriter`1.WriteValue[T](T&) + 0x3d
   at MemoryPack.MemoryPackSerializer.Serialize[T](T&, MemoryPackSerializerOptions) + 0x342
   at Game.Main(String[] args) + 0xaa

I have attached an example for reproduction. (It is only a single source file, but due to GitHub limitations I've had to place it into a ZIP container).
Repro.zip

To build the sample:

  1. Place the file into a .NET project/solution.
  2. Acquire the MemoryPack package via NuGet
  3. Build & run the sample without NativeAOT - the sample runs as expected.
  4. Publish the project using NativeAOT. A crash similar to the one above should be reproduced.

I have been able to reproduce this on both .NET 8 and 9 on Windows 10 (22H2, 19045.5487) and Ubuntu 24.04.2.

Activity

  1. dotnet-policy-service commented on Mar 18, 2025

    @dotnet-policy-service
    Contributor

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

  2. added a commit that references this issue on Mar 19, 2025
    dd1daa0
  3. added
    in-prThere is an active PR which will close this issue when it is merged
    on Mar 19, 2025
  4. Nolram12345 commented on Mar 19, 2025

    @Nolram12345
    Author

    When can we expect this change to be available in the SDK?

  5. added a commit that references this issue on Mar 19, 2025
    7e69df0
  6. MichalStrehovsky commented on Mar 19, 2025

    @MichalStrehovsky
    Member

    It will be in .NET 10 SDK nightly builds in a couple days: https://github.com/dotnet/sdk?tab=readme-ov-file#installing-the-sdk, make sure the project targets .NET 10 when using the SDK.

  7. Nolram12345 commented on Mar 19, 2025

    @Nolram12345
    Author

    I see, so I'll need to wait for .NET 10 to be released to be able to have this fix in production?

  8. MichalStrehovsky commented on Mar 19, 2025

    @MichalStrehovsky
    Member

    I see, so I'll need to wait for .NET 10 to be released to be able to have this fix in production?

    Yes. We don't backport all fixes and this being broken on .NET 7/8/9 weakens the backporting argument significantly.

  9. locked and limited conversation to collaborators on Apr 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-NativeAOT-coreclrin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions