Skip to content

CoreCLR rejects [UnmanagedCallersOnly] with the AssociatedSourceType named argument (0x801311C4) #130245

Description

@rolfbjarne

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:

dotnet run -c Release

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.

Metadata

Metadata

Type

No type

Fields

No fields configured for issues without a type.

Projects

Status
No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions