Repository navigation
Description
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
-
With SDK 11.0.100-rc.2.26504.105 and the
wasm-toolsworkload:dotnet new blazorwasm --empty -n AotRepro(targetsnet11.0) -
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(); }
-
Reference it from
Program.csso that it is kept (it is never called):if (args.Length == 42) { Console.WriteLine(Native.FcInit()); }
-
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-wasm11.0.0-rc.2.26504.105,wasm-toolsworkload 11.0.100-rc.2 (Emscripten 6.0.2) - Also .NET SDK 10.0.402 with the 10.0.13 packs,
wasm-toolsworkload (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
Draw the viewer's pages as SVG, edit through a kept package, and depl…
dotnet-policy-service commentedon Oct 23, 2026
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.
Draw the viewer's pages as SVG, edit through a kept package, and depl…
docs: the AOT assertion is dotnet/runtime#136351, with a fix on its w…
[release/11.0] [mono] Decode UnmanagedCallConvAttribute without alloc…
EricGarnier commentedon Nov 20, 2026
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 withMicrosoft.NET.Runtime.WebAssembly.Sdk11.0.0 -dotnet publish -c Releaseof a browser-wasm project fails AOT-compiling SkiaSharp.dll withsgen-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.SkiaSharp を 4.156.2 に留め、SRC#Web の AOT の発行が落ちるのを直す
Drop the calling convention attributes from the native imports