Skip to content

[wasm][AOT] mono-aot-cross asserts at sgen-alloc.c:409 on a P/Invoke with [UnmanagedCallConv(CallConvs = ...)] #136351

Closed

Description

@SimonCropp

Description

Publishing a Blazor WebAssembly app with RunAOTCompilation=true fails when an assembly contains a P/Invoke that carries [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })]. mono-aot-cross aborts while compiling that assembly. It reproduces on .NET 11 RC2 (11.0.0-rc.2.26504.105) and on 10.0.13:

error : * Assertion at D:\a\_work\1\s\src\runtime\src\mono\mono\sgen\sgen-alloc.c:409, condition `*p == NULL' not met
error : Mono Ahead of Time compiler - compiling assembly ...\obj\Release\net11.0\wasm\for-publish\aot-in\AotRepro.dll
error : Precompiling failed for ...\aot-in\AotRepro.dll with exit code -1073740791.
error : Precompiling failed for ...\aot-in\aot-instances.dll with exit code -1073740791.

This is hit in practice through SixLabors.Fonts 3.1 and later (and so ImageSharp.Drawing 3.x), whose LibraryImport declarations for CoreText and CoreFoundation carry this attribute (CoreText.cs). Any app that references it cannot be AOT-compiled for the browser. Fonts 3.0.0, which has no such declarations, compiles.

Reproduction Steps

  1. With SDK 11.0.100-rc.2.26504.105 and the wasm-tools workload: dotnet new blazorwasm --empty -n AotRepro (targets net11.0)

  2. Add this file:

    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    
    namespace AotRepro;
    
    static class Native
    {
        [DllImport("libfontconfig.so.1", EntryPoint = "FcInit")]
        [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
        public static extern int FcInit();
    }
  3. Reference it from Program.cs so that it is kept (it is never called):

    if (args.Length == 42)
    {
        Console.WriteLine(Native.FcInit());
    }
  4. dotnet publish -c Release -p:RunAOTCompilation=true

Expected behavior

The app publishes. A calling convention has no meaning on browser-wasm, and the P/Invoke is never called.

Actual behavior

AOT compilation of AotRepro.dll (and aot-instances.dll) fails with the assertion above. Deleting only the [UnmanagedCallConv] line makes the same app AOT-compile and publish (39 assemblies on RC2, 35 on 10.0.13).

Regression?

Not known. It reproduces on 10.0.13 and 11.0.0-rc.2; earlier versions were not tried.

Known Workarounds

Strip the attribute during trimming, before the AOT compiler sees it, with an ILLink link-attributes file:

<linker>
  <assembly fullname="System.Private.CoreLib">
    <type fullname="System.Runtime.InteropServices.UnmanagedCallConvAttribute">
      <attribute internal="RemoveAttributeInstances" />
    </type>
  </assembly>
</linker>
<PropertyGroup Condition="'$(RunAOTCompilation)' == 'true'">
  <_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) --link-attributes "$(MSBuildThisFileDirectory)AotLinkAttributes.xml"</_ExtraTrimmerArgs>
</PropertyGroup>

Configuration

  • .NET SDK 11.0.100-rc.2.26504.105, Microsoft.NET.Runtime.WebAssembly.Sdk / Microsoft.NETCore.App.Runtime.AOT.win-x64.Cross.browser-wasm 11.0.0-rc.2.26504.105, wasm-tools workload 11.0.100-rc.2 (Emscripten 6.0.2)
  • Also .NET SDK 10.0.402 with the 10.0.13 packs, wasm-tools workload (manifest 10.0.113, Emscripten 3.1.56)
  • Windows 11 x64 host; on 10.0.13 the same failure was also seen on a Linux x64 host
  • browser-wasm, Blazor WebAssembly

Other information

A backtrace of the failure on SixLabors.Fonts.dll, taken with Microsoft's symbols (dotnet-symbol), points at the attribute's decoding while the native wrappers are built, before any method is compiled:

add_managed_to_native_wrappers
  mono_marshal_get_native_wrapper
    mono_marshal_set_callconv_from_unmanaged_callconv_attribute
      load_cattr_value
        mono_array_new_specific_internal
          sgen_try_alloc_obj_nolock   <- assertion

Decoding the CallConvs argument allocates a managed Type[] inside the AOT compiler, and that allocation trips sgen's assertion.

Activity

  1. added
    untriaged
    on Oct 23, 2026
  2. added
    arch-wasm
    on Oct 23, 2026
  3. added this to the 11.0.0 milestone on Oct 23, 2026
  4. dotnet-policy-service commented on Oct 23, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
    See info in area-owners.md if you want to be subscribed.

  5. removed
    untriaged
    on Oct 23, 2026
  6. added a commit that references this issue on Nov 15, 2026
  7. EricGarnier commented on Nov 20, 2026

    @EricGarnier

    Another real-world hit, for anyone searching: SkiaSharp 4.157.0 (which added [UnmanagedCallConv] to its native bindings) aborts the same way on SDK 11.0.100 with Microsoft.NET.Runtime.WebAssembly.Sdk 11.0.0 - dotnet publish -c Release of a browser-wasm project fails AOT-compiling SkiaSharp.dll with sgen-alloc.c:409 / *p == NULL, and also fails aot-instances.dll, on Linux and Windows. 4.156.x compiles. Tracked on the SkiaSharp side as mono/SkiaSharp#5840.

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

    arch-wasmarea-Codegen-AOT-mono

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions