Skip to content

[iOS][CoreCLR] 11.0: app killed on device when an assembly with its own ReadyToRun code is loaded through the Unix LoadedImageLayout #137837

Open

Description

@hovik-aghajanyan

Description

On a physical iPhone, a .NET 11 iOS app (CoreCLR, composite ReadyToRun + interpreter) is killed by the kernel during startup with EXC_BAD_ACCESS (SIGKILL) / CODESIGNING 2 Invalid Page ("Failed to fault in a page with execute permissions").

The crash happens when interpreted code triggers a class constructor. InitHelpers.InitClass (R2R) → InitClassHelper → MethodTable::RunClassInitEx → UnmanagedCallersOnlyCaller::InvokeThrowing → InitHelpers.CallClassConstructor (R2R), which calls the cctor slot. The slot leads to ThePreStub. ThePreStub ends with br x9 to the address returned by PreStubWorker, and that address is in a read-write, non-executable region. The same build running on the iOS Simulator does not crash. The device and Simulator libcoreclr builds export identical symbols.

The crash is deterministic. Two builds crashed at the same offset (569600 bytes) into a region of the same size (3712 KB, rw-/rwx, guard pages on both sides).

Reproduction Steps

  1. iOS app on net11.0-ios (Microsoft.iOS.Sdk 27.2.11039, runtime 11.0.0), Release, ios-arm64, App Store / TestFlight signing (no get-task-allow, no JIT entitlement).
  2. Composite ReadyToRun with a subset of assemblies as PublishReadyToRunCompositeRoots (System.Private.CoreLib, Microsoft.CodeAnalysis*, several app assemblies and some BCL). The other app and package assemblies are in PublishReadyToRunExclude, so they run in the interpreter.
  3. At startup, interpreted code (an excluded assembly) calls into the Razor compiler (Microsoft.CodeAnalysis.Razor.Compiler, also interpreted). That causes a class constructor of an interpreted type to run (RazorProjectEngine.Create(...) → engine.Process(...)).
  4. Launch on a physical device (seen on iPhone19,2, iOS 27.2 (24B119)).

I don't have a standalone repro project yet. I can try to reduce one if that helps.

Expected behavior

The class constructor runs in the interpreter, the same way it does on the iOS Simulator.

Actual behavior

The process is killed:

Exception Type:  EXC_BAD_ACCESS (SIGKILL)
Exception Subtype: KERN_PROTECTION_FAILURE at 0x000000011ad3b100
VM Region Info: 0x11ad3b100 is in 0x11acb0000-0x11b050000;  bytes after start: 569600  bytes before end: 3231487
      REGION TYPE                   START - END        [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      Untagged                   11aca4000-11acb0000   [   48K] ---/rwx SM=NUL
--->  Untagged                   11acb0000-11b050000   [ 3712K] rw-/rwx SM=COW
      Untagged                   11b050000-11b06c000   [  112K] ---/rwx SM=NUL
Termination Reason: CODESIGNING 2 Invalid Page
Kernel Triage: VM - (arg = 0x0) Failed to fault in a page with execute permissions

Thread 0 Crashed:
0   ???                0x000000011ad3b100
1   libcoreclr         void UnmanagedCallersOnlyCaller::InvokeThrowing<unsigned long, MethodTable*>(unsigned long, MethodTable*) + 172
2   libcoreclr         MethodTable::RunClassInitEx(Object**) + 420
3   libcoreclr         MethodTable::DoRunClassInitThrowing() + 412
4   libcoreclr         InitClassHelper + 104
5   App.r2r            S_P_CoreLib_System_Runtime_CompilerServices_InitHelpers__InitClassSlow + 80
6   App.r2r            S_P_CoreLib_System_Runtime_CompilerServices_InitHelpers__InitClass + 36
7   libcoreclr         InterpExecMethod(...) + 8872
8   libcoreclr         ExecuteInterpretedMethod + 480
9   libcoreclr         InterpreterStubRetI8 + 20
10  libcoreclr         CallJittedMethodRetI8 + 36
11  libcoreclr         InvokeCalliStub(...) + 168
12  libcoreclr         InterpExecMethod(...) + 18644
13  libcoreclr         ExecuteInterpretedMethod + 480
14  libcoreclr         InterpreterStubRetI8 + 20
15  App.r2r            S_P_CoreLib_System_Lazy_1<System___Canon>__ViaFactory + 40
...

Thread 0 crashed with ARM Thread State (64-bit):
    x9: 0x000000011ad3b100   x19: 0x0000000164960048   x20: 0x0000000000000000
    lr: 0x000000010f7734ec   pc: 0x000000011ad3b100
    esr: 0x8200000f (Instruction Abort) Permission fault

The call path, from the registers and disassembly:

  • lr is in InitHelpers.CallClassConstructor (composite R2R), right after blr x19. x20 (the instantiating arg) is 0.
  • x19, the cctor entry, is not in any loaded image. The code it reached returned without a call, so lr is unchanged, and the faulting branch is through x9.
  • ThePreStub on ios-arm64 ends with bl PreStubWorker; mov x9, x0; ...; br x9. The StubPrecode and FixupPrecode templates branch through x10 and x11, not x9. So the bad target is the value PreStubWorker returned for the interpreted class constructor.
  • That value points into a rw- region with max protection rwx. On the device it can't be made executable. On the Simulator it can, which would explain why the Simulator works.

This may be related to #131903 (StubLinker stub memory moved to the JIT code allocator). I haven't been able to confirm that.

Regression?

This is the first .NET 11 build of the app on a device. The same app on .NET 10 (Mono, MtouchInterpreter) runs on the same device.

Known Workarounds

None found so far. DOTNET_BackpatchEntryPointSlots=0, set through the bundler environment before runtime start, crashes the same way at the same offset. A build with every assembly in the ReadyToRun composite is being tested and I'll report the result.

Configuration

  • .NET SDK 11.0.100, runtime 11.0.0 (microsoft.netcore.app.runtime.ios-arm64 11.0.0)
  • Microsoft.iOS.Sdk.net11.0_27.2 27.2.11039
  • Xcode 27.2, macOS 27.2 (Apple silicon build host)
  • Device: iPhone19,2, iOS 27.2 (24B119), TestFlight build
  • Composite ReadyToRun (PublishReadyToRunCompositeRoots + PublishReadyToRunExclude), interpreter for the rest
  • Not reproducible on the iOS Simulator (iossimulator-arm64), in either Debug or Release with the same roots

Other information

On the Simulator, an lldb breakpoint on InitHelpers.CallClassConstructor during the same startup recorded 1734 cctor calls. Every target was either R2R code or a FixupPrecode in an r-x page; none was in a writable region.

Activity

  1. added
    untriaged
    on Nov 18, 2026
  2. dotnet-policy-service commented on Nov 18, 2026

    @dotnet-policy-service
    Contributor

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

  3. hovik-aghajanyan commented on Nov 18, 2026

    @hovik-aghajanyan
    Author

    More findings. (Edited: an earlier version of this comment said some cctor precodes target interpreter bytecode on the Simulator. That was my mistake: I read the StubPrecode data at the FixupPrecode offset. The corrected results are below.)

    The cctor slot holds a normal FixupPrecode. In both device crashes, x19 (the value CallClassConstructor branches to) is on a 24-byte boundary inside its 16 KB page: 0x164960048 (slot 3) and 0x16674db30 (slot 290). The fault address is not on a slot boundary (0x...3100). So the slot is an ordinary template-mapped FixupPrecode. The bad address is the value PreStubWorker returns, which ThePreStub branches to with br x9.

    The Simulator never reaches that state. I used the same Release configuration and the same composite roots as the device build. During startup, an lldb script recorded the x0 argument of every InitHelpers.CallClassConstructor call. After startup it decoded each precode, ldr x11 for Fixup and ldr x10 for Stub, and read its target field:

    count entry / precode target after startup
    516 R2R code in the composite (no precode)
    837 FixupPrecode, target is still its own fixup path
    54 StubPrecode, target is in libcoreclr

    No target points outside executable code. I also hooked MethodDesc::SetCodeEntryPoint, SetNativeCodeInterlocked and SetStableEntryPointInterlocked (about 1,900 calls each during startup). None was passed an address whose first word is not an instruction. On the Simulator, PreStubWorker never produces an entry point like the one seen on the device, so this does not reproduce there.

    Correction to the original report. I have no evidence for #131903. As far as I can tell, the old executable LoaderHeap and the new code-heap path commit pages the same way through ExecutableAllocator::Commit (PAGE_READWRITE without FEATURE_DYNAMIC_CODE_COMPILED). Nothing shows that the target is a StubLinker stub. Please disregard that pointer.

    What remains: on the device, PreStubWorker returns an address in a read-write region of 3712 KB (max protection rwx) for an interpreted class constructor that is reached from R2R code. The address is the same every run. I can't attach a debugger to the device build right now, but I can run any Simulator experiment that would help.

  4. added a commit that references this issue on Nov 19, 2026
    7158c12
  5. hovik-aghajanyan commented on Nov 19, 2026

    @hovik-aghajanyan
    Author

    I found the root cause by debugging a development build on the device (iPhone19,2, iOS 27.2). It is not the interpreter: the class constructor's own ReadyToRun code is run from an image the runtime mapped itself.

    What happens. Microsoft.CodeAnalysis.Razor.Compiler.dll ships with its own ReadyToRun code for Apple arm64 (machine 0xEC20). The app leaves it out of the composite (PublishReadyToRunExclude), so the file keeps that code. At the crash:

    • MethodDesc::GetName() of the cctor slot's MethodDesc returns .cctor, and GetNativeCode() returns the faulting PC.
    • The PC holds a normal arm64 prologue (stp x29, x30, [sp, #-0x30]! …), not data.
    • The region is the DLL's .text: .text is 3,786,752 bytes at RVA 0x10200, which rounds up to the 3712 KB rw- region in the crash report. The fault offset 569600 is RVA 0x9B100, and the 32 bytes at the faulting PC are identical to the bytes at that RVA in the DLL.

    Why the runtime accepts that code. AllowR2RForImage() in src/coreclr/vm/peimagelayout.cpp returns false on iOS, tvOS and Mac Catalyst, and LoadConverted/ConvertedImageLayout respect that. A file that isn't in a bundle goes through LoadedImageLayout on Unix, though. There, PAL_LOADLoadPEFile maps the file, and the constructor relocates it and calls SetRelocated() whenever it has a ReadyToRun header, without asking AllowR2RForImage(). ReadyToRunInfo::Initialize then sees a relocated image and uses its native code. On a device those pages are not executable, so the first call into the image is a CODESIGNING Invalid Page kill. I assume the Simulator can execute them, which would explain why it works there, but I haven't checked that.

    Confirmation. The same build with DOTNET_ReadyToRunExcludeList set to the 129 assemblies in the app that carry their own ReadyToRun code (Razor compiler, ASP.NET Core and Microsoft.Extensions.* from the shared framework) starts on the device and reaches its main screen.

    main. As far as I can tell, main isn't affected: #135902 removed the Unix LoadedImageLayout path, and Unix now always goes through ConvertedImageLayout, which only relocates when AllowR2RForImage() allows it. So this affects release/11.0 (11.0.0). I opened a PR against release/11.0 that applies the same AllowR2RForImage() check in the Unix LoadedImageLayout constructor: #137886

    Workaround until then: set DOTNET_ReadyToRunExcludeList to the assemblies that have their own (non-component) ReadyToRun code.

    I've retitled the issue to match.

  6. changed the title [-][iOS][CoreCLR] App killed at launch on device (CODESIGNING "Invalid Page") when R2R code runs an interpreted class constructor through ThePreStub[/-] [+][iOS][CoreCLR] 11.0: app killed on device when an assembly with its own ReadyToRun code is loaded through the Unix LoadedImageLayout[/+] on Nov 19, 2026
  7. added
    os-ios
    and removed
    untriaged
    on Nov 19, 2026
  8. self-assigned this
    on Nov 19, 2026
  9. jkotas commented on Nov 21, 2026

    @jkotas
    Member

    Microsoft.CodeAnalysis.Razor.Compiler.dll ships with its own ReadyToRun code

    Where did you get the Microsoft.CodeAnalysis.Razor.Compiler.dll binary from?

    We do not really support constructing apps by manually copying binaries from different places.

  10. hovik-aghajanyan commented on Nov 21, 2026

    @hovik-aghajanyan
    Author

    Where did you get the Microsoft.CodeAnalysis.Razor.Compiler.dll binary from?

    From the .NET SDK installed on the build Mac. The app is a C# IDE that compiles and runs Razor and ASP.NET Core code on the device. I couldn't find a package with these binaries, so the project references them by path:

    • Microsoft.CodeAnalysis.Razor.Compiler.dll and Microsoft.AspNetCore.Razor.Utilities.Shared.dll come from sdk/11.0.100/Sdks/Microsoft.NET.Sdk.Razor/source-generators/.
    • The ASP.NET Core assemblies, including the Microsoft.Extensions.* ones that ship with them, come from shared/Microsoft.AspNetCore.App/11.0.0/. An iOS app can't take a FrameworkReference to Microsoft.AspNetCore.App.

    These are the osx-arm64 builds. They're ReadyToRun-compiled for machine 0xEC20, the same value ios-arm64 uses, so the runtime accepts their code.

    I understand this isn't a supported way to build an app, and I'm not asking you to support it. On our side I'll keep the DOTNET_ReadyToRunExcludeList workaround. If there's a supported source for these assemblies that works for an iOS app, please point me to it and I'll switch.

    About the PR: on iOS the runtime already chooses not to use an image's own ReadyToRun code (AllowR2RForImage()). ConvertedImageLayout and main follow that choice, and only the Unix LoadedImageLayout path in release/11.0 skips it. When an assembly like this ends up in an app, the result is a CODESIGNING kill with no managed stack instead of the assembly running as IL. I think the check is worth having for that reason, but if you'd rather not service it, feel free to close the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

  • Status
    No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions