Skip to content

[release/11.0] Respect AllowR2RForImage in Unix LoadedImageLayout - #137886

Open
hovik-aghajanyan wants to merge 1 commit into
dotnet:release/11.0from
hovik-aghajanyan:ios-loadedimagelayout-r2r
Open

hovik-aghajanyan wants to merge 1 commit into
dotnet:release/11.0from
hovik-aghajanyan:ios-loadedimagelayout-r2r

Conversation

@hovik-aghajanyan

Copy link
Copy Markdown

Fixes Issue #137837

main PR: none needed. #135902 removed the Unix LoadedImageLayout path on main, so there every image goes through ConvertedImageLayout, which already checks AllowR2RForImage().

Description

On iOS, tvOS and Mac Catalyst, AllowR2RForImage() returns false, so the runtime should not use ReadyToRun code from an image it maps itself. The Unix LoadedImageLayout constructor, used for assemblies that are plain files, doesn't check it: it relocates any image with a ReadyToRun header and calls SetRelocated(). ReadyToRunInfo::Initialize then accepts the image and runs its native code from pages the runtime mapped itself.

On an iOS device those pages are not executable. The first call into such an assembly kills the app with EXC_BAD_ACCESS (SIGKILL) / CODESIGNING Invalid Page. In #137837 the assembly is Microsoft.CodeAnalysis.Razor.Compiler.dll (ReadyToRun for Apple arm64). The ASP.NET Core and Microsoft.Extensions.* assemblies from the shared framework carry ReadyToRun code too.

This change relocates the image only when AllowR2RForImage() allows it. On those platforms the image stays unrelocated, so ReadyToRunInfo::Initialize ignores its native code ("module not loaded for execution") and the methods run in the interpreter. That matches what main does after #135902. Component assemblies of a composite image don't depend on the relocated flag, so they keep using the composite.

Customer Impact

An iOS app on .NET 11 with CoreCLR is killed on a physical device as soon as it calls into an assembly that already ships its own ReadyToRun code and isn't compiled into the app's composite. In #137837 that is Microsoft.CodeAnalysis.Razor.Compiler.dll, and the app also carries ASP.NET Core and Microsoft.Extensions.* assemblies with their own ReadyToRun code. I haven't checked whether a default SDK build ends up in this state. The Simulator doesn't show the problem, so it's found only on a device or TestFlight.

The workaround is to list each such assembly in DOTNET_ReadyToRunExcludeList.

Regression

Not in the usual sense: CoreCLR on iOS is new in .NET 11. The same app on .NET 10 (Mono) runs on the device.

Testing

  • Debugged a development build on an iPhone (iPhone19,2, iOS 27.2). At the fault, GetNativeCode() of the class constructor's MethodDesc is the faulting PC. That address is inside Microsoft.CodeAnalysis.Razor.Compiler.dll's .text, mapped read-write, and the bytes there match the DLL.
  • The same build with DOTNET_ReadyToRunExcludeList set to the 129 assemblies with their own ReadyToRun code starts on the device and reaches its main screen. The exclude list and this change take different routes, but both make ReadyToRunInfo::Initialize ignore those images' native code.
  • I haven't built this change for iOS locally, so I'm relying on CI for the build.

Risk

Low. The change only affects targets where AllowR2RForImage() returns false (iOS, tvOS, Mac Catalyst). Everywhere else AllowR2RForImage() is true for files, which is the only case that reaches this constructor, so behavior is unchanged.

On iOS and tvOS devices, those assemblies now run in the interpreter instead of crashing. On the Simulator and on Mac Catalyst, if their own ReadyToRun code ran before, it no longer does, so they may start more slowly there. That is also how main behaves since #135902. Composite ReadyToRun code isn't affected.

AllowR2RForImage returns false on iOS, tvOS and Mac Catalyst, but the
Unix LoadedImageLayout constructor relocated every image with a
ReadyToRun header and marked it relocated without checking it.
ReadyToRunInfo::Initialize then used the native code of an assembly
that ships its own ReadyToRun code, running it from pages the runtime
mapped itself. On an iOS device those pages are not executable, so
the app is killed (CODESIGNING Invalid Page) on the first call into
such an assembly.

Relocate only when AllowR2RForImage allows it, so those images stay
IL-only on these platforms. main already behaves this way since the
Unix LoadedImageLayout path was removed.

Fix dotnet#137837
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

// AllowR2RForImage is false on iOS, tvOS and Mac Catalyst, where native code in an image mapped here can't
// run (on an iOS device its pages are not executable). Leave the image unrelocated there, so that
// ReadyToRunInfo::Initialize ignores its native code. Component assemblies of a composite image are not affected.
if (AllowR2RForImage(pOwner))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it makes more sense to move this check next to HasReadyToRunHeader above

@BrzVlad

BrzVlad commented Nov 20, 2026

Copy link
Copy Markdown
Member

We shouldn't be including assemblies with r2r code on iOS, so a full fix, that might also result in some size gains, would be to trim this code out as part of the build. Seems like this was behaving the same on mono, so it should be fine to leave this improvement to .net12.

@jkoritzinsky Could you review this as well. Does it make sense to backport it like this to 11 directly ?

@jkotas

jkotas commented Nov 21, 2026

Copy link
Copy Markdown
Member

Does it make sense to backport it like this

We should understand where the offending assembly came from first - #137837 (comment)

This branch has not been deployed

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

Labels

area-ReadyToRun community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants