Skip to content

JIT: SIGSEGV in Lowering::LowerNonvirtPinvokeCall when a SwiftError local is not read after a CallConvSwift P/Invoke #136523

Closed

Description

@justinwojo

Description

If a CallConvSwift P/Invoke takes SwiftError* pointing at a local that is never read after the call, the optimizing JIT crashes with a null dereference in Lowering::LowerNonvirtPinvokeCall. Tier0 compiles the method. Under default settings the process crashes once the method tiers up. crossgen2 exits with SIGSEGV too, so PublishReadyToRun fails with NETSDK1096. Reading the error after the call avoids the crash.

Ignoring the error is unusual, but the code is valid, and it occurs naturally when a binding knows a function cannot throw in practice. A JIT crash also takes the whole process down.

Reproduction Steps

Repro repo: https://github.com/justinwojo/dotnet-swift-interop-repros (folder desktop-jit-crash, osx-arm64, no iOS tooling needed).

// swiftc -emit-library -enable-library-evolution -O -module-name SwiftErrorProbe Probe.swift
// IR: swiftcc i64 @"$s15SwiftErrorProbe11plainThrowsys5Int64VADKF"(i64, ptr swiftself, ptr swifterror)
@inline(never) public func plainThrows(_ a: Int64) throws -> Int64 { a &+ 1 }
// <AllowUnsafeBlocks>true</AllowUnsafeBlocks>, [assembly: DisableRuntimeMarshalling]
[DllImport("libSwiftErrorProbe.dylib", EntryPoint = "$s15SwiftErrorProbe11plainThrowsys5Int64VADKF")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvSwift)])]
static extern long CallErrorLast(long a, SwiftError* error);

[MethodImpl(MethodImplOptions.NoInlining)]
static long Unobserved()
{
    SwiftError error = default;
    return CallErrorLast(41, &error);        // crashes the JIT
}

[MethodImpl(MethodImplOptions.NoInlining)]
static long Observed()
{
    SwiftError error = default;
    long value = CallErrorLast(41, &error);
    return error.Value == null ? value : -1; // compiles and runs
}
cd desktop-jit-crash
export DOTNET=/path/to/net11/dotnet                # DOTNET10=/path/to/net10/dotnet is optional
./run.sh                                           # builds and runs the JIT matrix
./publish.sh                                       # PublishReadyToRun / PublishAot per target framework
DOTNET_TieredCompilation=0 "$DOTNET" bin/Release/net11.0/SwiftErrorJitCrash.dll unobserved   # exit 139

Expected behavior

result=42 expected=42, exit 0, for every case.

Actual behavior

Case .NET 10.0.7 default .NET 10.0.7 TC=0 .NET 11.0 RC2 default .NET 11.0 RC2 TC=0
observed 0 0 0 0
unobserved ((long, SwiftError*)) 0 139 0 139
unobserved-error-first ((SwiftError*, long)) 0 139 0 139
unobserved-hot (default tiering, loops until tier-up) 139 139 139 139
dotnet publish -r osx-arm64 -f <tfm> (evidence/publish.txt) .NET 10 .NET 11 RC2
-p:PublishReadyToRun=true crossgen2 exits 139, NETSDK1096 crossgen2 exits 139, NETSDK1096
-p:PublishAot=true succeeds; the three tested cases exit 0 succeeds; the three tested cases exit 0

publish.sh runs observed, unobserved and unobserved-error-first from each publish (not unobserved-hot, which only exercises tier-up).

* thread #1, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: libclrjit.dylib`Lowering::LowerNonvirtPinvokeCall(GenTreeCall*) + 588
    frame #1: libclrjit.dylib`Lowering::LowerCall(GenTree*) + 984
    frame #2: libclrjit.dylib`Lowering::LowerNode(GenTree*) + 1568
    frame #3: libclrjit.dylib`Lowering::DoPhase() + 192
    frame #4: libclrjit.dylib`Phase::Run() + 44
    frame #5: libclrjit.dylib`Compiler::compCompile(void**, unsigned int*, JitFlags*) + 4996
    ...
    frame #10: libcoreclr.dylib`UnsafeJitFunction(...)

->  ldrb   w8, [x21]          ; x21 == 0
    cmp    w8, #0x7d
    b.ne   <+584>

On .NET 10.0.7 the fault is the same ldrb / cmp / b.ne loop at LowerNonvirtPinvokeCall + 672 (compared against #0x76).

Regression?

No, at least not relative to .NET 10: 10.0.7 crashes the same way.

Known Workarounds

Read the SwiftError after the call (for example if (error.Value != null) …).

Configuration

  • .NET 10.0.7 (SDK 10.0.107) and .NET 11.0.0-rc.2.26504.105 (SDK 11.0.100-rc.2.26504.105)
  • osx-arm64, macOS 26.6.2, Apple M3 Pro; Swift dylib built with Xcode 26.3
  • We first hit this in a .NET 11 iOS Release build, where crossgen2 compiles user assemblies into the ReadyToRun composite
  • x64: not run on x64 hardware. The x64 altjit (libclrjit_unix_x64_arm64.dylib) hits the same assert on main (below).
  • Status on main (9f06f271): reproduces. A Checked build asserts (below), and a Release build exits with SIGSEGV (139). crossgen2 was not re-run on main.

Other information

Checked assert on main (standalone repro equivalent to the desktop-jit-crash cases):

Assertion failed 'swiftErrorNode != nullptr' in 'Program:PlainUnobserved():long' during 'Lowering nodeinfo' (IL size 20; hash 0x1af67085; FullOpts)
    File: src/coreclr/jit/lower.cpp:7337

The same assert fires for a SwiftIndirectResult P/Invoke with the error declared first and with it declared last. The recorded observed controls (plain, and SwiftIndirectResult with the error last) pass. Logs: runtime-main/logs/D-assert-checked.txt.

Where the store goes. A trimmed JitDump on main (D-jitdump-arm64-trimmed.txt) shows it is removed by Early liveness. Links are pinned to main 9f06f271:

  1. The importer appends STOREIND(&error, SWIFT_ERROR) after the call (impAppendSwiftErrorStore, importercalls.cpp#L2787-L2797). The SWIFT_ERROR node carries GTF_ORDER_SIDEEFF | GTF_CALL | GTF_GLOB_REF.

  2. Local morph turns it into STORE_LCL_VAR of the promoted field SwiftError.Value, because the local's address is only taken for the call.

  3. The local is never read, so TryRemoveDeadStoreEarly calls gtExtractSideEffList(data) with the default GTF_SIDE_EFFECT mask. gtNodeHasSideEffects honours GTF_CALL only on a real GT_CALL / GT_RET_EXPR, and GTF_ORDER_SIDEEFF is not part of GTF_SIDE_EFFECT (gentree.h#L369-L378). The dump prints Store [000009] is dead and has no side effects, removing statement, and GT_SWIFT_ERROR goes with it.

  4. The call still reports HasSwiftErrorHandling(). The loop in LowerNonvirtPinvokeCall (lower.cpp#L7326-L7343) walks gtNext looking for GT_SWIFT_ERROR, runs off the end of the range, and trips the assert (Checked) or dereferences null (Release):

    if (call->HasSwiftErrorHandling())
    {
        GenTree* swiftErrorNode = call->gtNext;
        assert(swiftErrorNode != nullptr);
    
        while (!swiftErrorNode->OperIs(GT_SWIFT_ERROR))
        {
            swiftErrorNode = swiftErrorNode->gtNext;
            assert(swiftErrorNode != nullptr);
        }
        ...

    LSRA relies on the same invariant (call->gtNext is GT_SWIFT_ERROR, lsrabuild.cpp#L4649-L4658).

Possible fix directions, offered only as suggestions: treat GT_SWIFT_ERROR as a side effect in gtNodeHasSideEffects / gtExtractSideEffList (for example by honouring GTF_ORDER_SIDEEFF for this oper), or let Lowering/LSRA materialise a consumer when the store is gone so the error register is still treated as defined by the call.

NativeAOT compiles the same method without trouble. We haven't looked into why.

Regression test. runtime-main/tests.patch adds TestSwiftErrorNotObserved to src/tests/Interop/Swift/SwiftErrorHandling (it applies with git apply on 9f06f271). Under the JIT it takes down the whole merged SwiftErrorHandling test process, so it may belong in its own project or under ActiveIssue until fixed.

Related: #98586 (introduced the importer store and the Lowering move), #99414, and #99120 / #99236 / #99237 (earlier cases of GT_SWIFT_ERROR missing from a generic JIT utility). We found no existing report of this crash.

Activity

  1. added
    untriaged
    on Oct 27, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 27, 2026
  3. dotnet-policy-service commented on Oct 27, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. added this to the 12.0.0 milestone on Nov 6, 2026
  5. removed
    untriaged
    on Nov 10, 2026
  6. self-assigned this
    on Nov 14, 2026
  7. added a commit that references this issue on Nov 19, 2026
    4ab5617
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions