Repository navigation
Description
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 139Expected 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(SDK11.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:
-
The importer appends
STOREIND(&error, SWIFT_ERROR)after the call (impAppendSwiftErrorStore, importercalls.cpp#L2787-L2797). TheSWIFT_ERRORnode carriesGTF_ORDER_SIDEEFF | GTF_CALL | GTF_GLOB_REF. -
Local morph turns it into
STORE_LCL_VARof the promoted fieldSwiftError.Value, because the local's address is only taken for the call. -
The local is never read, so
TryRemoveDeadStoreEarlycallsgtExtractSideEffList(data)with the defaultGTF_SIDE_EFFECTmask.gtNodeHasSideEffectshonoursGTF_CALLonly on a realGT_CALL/GT_RET_EXPR, andGTF_ORDER_SIDEEFFis not part ofGTF_SIDE_EFFECT(gentree.h#L369-L378). The dump printsStore [000009] is dead and has no side effects, removing statement, andGT_SWIFT_ERRORgoes with it. -
The call still reports
HasSwiftErrorHandling(). The loop inLowerNonvirtPinvokeCall(lower.cpp#L7326-L7343) walksgtNextlooking forGT_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->gtNextisGT_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
dotnet-policy-service commentedon Oct 27, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
JIT: preserve unused Swift error results (dotnet#137526)