Description
The CoreCLR runtime has a hand-written parser for the [UnmanagedCallersOnly] attribute that only accepts the named arguments CallConvs and EntryPoint. If the attribute specifies any other named argument, the runtime throws when the method is set up as an unmanaged callback.
System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute gained an AssociatedSourceType property (recognized by the NativeAOT compiler / ILC). However, setting it makes the same assembly crash at runtime under CoreCLR:
Unhandled exception. System.Runtime.InteropServices.COMException (0x801311C4): Known custom attribute named argument not recognized. (0x801311C4)
So an assembly that compiles and works under NativeAOT crashes when run on CoreCLR instead.
This surfaced in dotnet/macios: the managed static registrar emits [UnmanagedCallersOnly(AssociatedSourceType = ...)] on its trampolines for the NativeAOT app-size benefit, which broke every CoreCLR/Mono app at startup.
Reproduction
App.csproj (plain net11.0, no workloads):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net11.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
Program.cs:
using System;
using System.Runtime.InteropServices;
unsafe {
delegate* unmanaged<void> fn = &Callbacks.Callback;
Console.WriteLine ("Calling the [UnmanagedCallersOnly] method through a function pointer...");
fn ();
Console.WriteLine ("Success - the bug did not reproduce.");
}
static class Callbacks {
[UnmanagedCallersOnly (AssociatedSourceType = typeof (Callbacks))]
public static void Callback ()
{
Console.WriteLine (" inside the callback");
}
}
Then:
Actual behavior
Calling the [UnmanagedCallersOnly] method through a function pointer...
Unhandled exception. System.Runtime.InteropServices.COMException (0x801311C4): Known custom attribute named argument not recognized. (0x801311C4)
at Program.<Main>$(String[] args)
The process aborts (exit code 134 / Abort trap: 6).
Removing the AssociatedSourceType = typeof (Callbacks) named argument makes the app run fine and print Success - the bug did not reproduce.
Expected behavior
CoreCLR should ignore the AssociatedSourceType property that it doesn't recognize (as ILC does), rather than throwing.
Configuration
- .NET SDK/runtime:
11.0.100-preview.7.26351.108 (.NET 11 preview 7)
- OS: macOS 27.0
- Architecture: arm64
- Runtime: CoreCLR (default)
Regression?
The AssociatedSourceType property is new, so this is only reachable on recent builds. The runtime's rejection of unknown [UnmanagedCallersOnly] named arguments is long-standing behavior.
Description
The CoreCLR runtime has a hand-written parser for the
[UnmanagedCallersOnly]attribute that only accepts the named argumentsCallConvsandEntryPoint. If the attribute specifies any other named argument, the runtime throws when the method is set up as an unmanaged callback.System.Runtime.InteropServices.UnmanagedCallersOnlyAttributegained anAssociatedSourceTypeproperty (recognized by the NativeAOT compiler / ILC). However, setting it makes the same assembly crash at runtime under CoreCLR:So an assembly that compiles and works under NativeAOT crashes when run on CoreCLR instead.
This surfaced in dotnet/macios: the managed static registrar emits
[UnmanagedCallersOnly(AssociatedSourceType = ...)]on its trampolines for the NativeAOT app-size benefit, which broke every CoreCLR/Mono app at startup.Reproduction
App.csproj(plainnet11.0, no workloads):Program.cs:Then:
Actual behavior
The process aborts (exit code 134 /
Abort trap: 6).Removing the
AssociatedSourceType = typeof (Callbacks)named argument makes the app run fine and printSuccess - the bug did not reproduce.Expected behavior
CoreCLR should ignore the
AssociatedSourceTypeproperty that it doesn't recognize (as ILC does), rather than throwing.Configuration
11.0.100-preview.7.26351.108(.NET 11 preview 7)Regression?
The
AssociatedSourceTypeproperty is new, so this is only reachable on recent builds. The runtime's rejection of unknown[UnmanagedCallersOnly]named arguments is long-standing behavior.