Skip to content

NativeAOT shared library on linux-x64: first call into an [UnmanagedCallersOnly] export whose frame has a GS cookie (e.g. stackalloc) aborts the process #136201

Closed

Description

@LaborJos

Description

A NativeAOT shared library (PublishAot=true, OutputType=Library) on linux-x64 aborts (exit code 134, no message) when the first call into the library is an [UnmanagedCallersOnly] method whose frame has a stack cookie check, e.g. a stackalloc or a struct containing a fixed buffer. The same method works if any other export was called first. Windows is not affected.

Repro

Minimal.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <OutputType>Library</OutputType>
    <AssemblyName>Minimal</AssemblyName>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <PublishAot>true</PublishAot>
    <InvariantGlobalization>true</InvariantGlobalization>
  </PropertyGroup>
</Project>

Exports.cs

using System.Runtime.InteropServices;

public static unsafe class Exports
{
    [UnmanagedCallersOnly(EntryPoint = "no_buffer")]
    public static int NoBuffer(int* result) { *result = 42; return 0; }

    [UnmanagedCallersOnly(EntryPoint = "with_stackalloc")]
    public static int WithStackalloc(int* result)
    {
        byte* local = stackalloc byte[64];
        local[0] = 42;
        *result = local[0];
        return 0;
    }
}

call.py (calls the given exports in order, in one process)

import ctypes, sys
dll = ctypes.CDLL(sys.argv[1])
for name in sys.argv[2:]:
    result = ctypes.c_int()
    rc = getattr(dll, name)(ctypes.byref(result))
    print(f"{name}: rc={rc} result={result.value}", flush=True)
dotnet publish -r linux-x64 -c Release -o out
python3 call.py out/Minimal.so no_buffer                  # ok
python3 call.py out/Minimal.so with_stackalloc            # Aborted, exit 134
python3 call.py out/Minimal.so no_buffer with_stackalloc  # ok

(The output is libMinimal.so on net11.0. A small C client using dlopen/dlsym behaves the same.)

Results

Ubuntu 24.04.5 (WSL2), glibc 2.39, clang 18.1.3.

Target (ILCompiler) with_stackalloc as first call
net8.0 (8.0.32) exit 134
net9.0 (9.0.21) exit 134
net10.0 (10.0.13) exit 134
net11.0 (11.0.0-rc.2.26504.105) exit 134

Variants (net10.0), each as the first call of a fresh process:

UCO frame contains first call
nothing / heap array / fixed statement / plain struct ok
stackalloc exit 134
struct with fixed byte[64] exit 134
stackalloc in a method called by the UCO (also with AggressiveInlining) ok

Analysis

With a hardware watchpoint on __security_cookie (gdb): at UCO entry the cookie is 0 and the prologue saves it. In the same call, ReversePInvokeAttachOrTrapThread -> EnsureRuntimeInitialized -> RhInitialize -> InitGSCookie writes the real value. The epilogue check then fails and RhpFallbackFailFast is called.

In dotnet/runtime main:

  • src/coreclr/nativeaot/Bootstrap/main.cpp: for shared libraries a static constructor only registers InitializeRuntime via RhSetRuntimeInitializationCallback. The runtime is initialized lazily on the first UCO call.
  • src/coreclr/nativeaot/Runtime/startup.cpp: for TARGET_UNIX, __security_cookie is 0 and is set only in InitGSCookie(), called from InitDLL (inside RhInitialize).

So the cookie is initialized after the prologue of the first UCO frame has already read it.

Expected behavior

The cookie is initialized before any managed UCO frame can read it (e.g. when the library is loaded), or the runtime is initialized before the first UCO prologue.

Notes

  • I tried initializing the runtime at load time with an ELF constructor linked into the .so. It crashed inside dlopen: the constructor ran before the bootstrapper's static constructor had registered the callback, and I found no supported way to order them. I found no ILC option to disable the stack cookie.
  • Related but different: [NativeAOT] Randomizing guard stack cookie not supported on some Unix platforms #73695 (randomizing the guard stack cookie on some Unix platforms).
  • Not tested: arm64, musl, other glibc versions.

This report and the reproduction were prepared with the assistance of GitHub Copilot.

Activity

  1. added
    untriaged
    on Oct 21, 2026
  2. dotnet-policy-service commented on Oct 21, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
    See info in area-owners.md if you want to be subscribed.

  3. added this to the 12.0.0 milestone on Oct 22, 2026
  4. removed
    untriaged
    on Oct 22, 2026
  5. added a commit that references this issue on Oct 22, 2026
    53ac1a9
  6. locked and limited conversation to collaborators on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions