Skip to content

Fix sporadic AccessViolation in compressed single-file apps on Apple Silicon - #127355

Merged
elinor-fung merged 4 commits into
dotnet:mainfrom
elinor-fung:fix123324
Apr 29, 2026
Merged

elinor-fung merged 4 commits into
dotnet:mainfrom
elinor-fung:fix123324

Conversation

@elinor-fung

@elinor-fung elinor-fung commented Apr 23, 2026 •

Copy link
Copy Markdown
Member

Note

This pull request was generated with AI assistance (GitHub Copilot CLI).

Fixes #123324.

cc @dotnet/appmodel @AaronRobinsonMSFT

Problem

On macOS Apple Silicon (observed on macOS 15), compressed self-contained single-file apps intermittently hit AccessViolationException crashes. With the repro provided in the issue, the failure rate was roughly 75% in a local run.

FlatImageLayout::LoadImageByCopyingParts allocates the image region with MEM_RESERVE_EXECUTABLE (which maps to MAP_JIT on macOS) as PAGE_READWRITE, copies in all section bytes, then promotes executable sections to PAGE_EXECUTE_READWRITE via a second mprotect. On Apple Silicon, this RW → RWX transition on MAP_JIT memory has been observed to intermittently succeed at the API level but leave pages non-executable at the kernel level, producing the sporadic AVs.

Fix

On Apple Silicon (HOST_OSX && HOST_ARM64), avoid the unreliable transition: reserve the whole image region as PAGE_NOACCESS up front, then commit each section directly with its final runtime protection (PAGE_EXECUTE_READWRITE for exec, PAGE_READWRITE otherwise), copying executable content under PAL_JitWriteProtect. The PROT_NONE → RWX direction via a fresh commit is reliable; the problematic RW → RWX transition no longer occurs. Read-only sections are still downgraded from PAGE_READWRITE to PAGE_READONLY after the copy, which is a W-removing transition and is not implicated in the original bug.

The non-Apple-Silicon code path is unchanged.

Testing

New test: AppHost.Bundle.Tests.AppLaunch.SelfContained_Compressed_SpawnsChildren (OSX-only). Adds a launch_self option to the HelloWorld test asset that spawns 5 copies of itself in parallel, and asserts the parent runs to completion.

Local validation on Apple Silicon:

  • Without fix: parent crash with AccessViolationException in ~74% of trials at N=5 children (rises to ~98% at N≥12).
  • With fix: 0 failures across 50+ trials at N=5.

Copilot AI review requested due to automatic review settings April 23, 2026 22:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a sporadic startup AccessViolationException on macOS Apple Silicon for compressed, self-contained single-file apps by changing how FlatImageLayout::LoadImageByCopyingParts reserves/commits and protects image memory, avoiding an unreliable protection transition on MAP_JIT memory.

Changes:

  • Adjust LoadImageByCopyingParts on HOST_OSX && HOST_ARM64 to reserve with PAGE_NOACCESS, then commit/copy headers/sections with final protections (exec committed as PAGE_EXECUTE_READWRITE and written under PAL_JitWriteProtect).
  • Extend the HelloWorld installer test asset with a launch_self mode that spawns multiple child processes in parallel.
  • Add an OSX regression test that runs a compressed self-contained single-file and exercises concurrent child launches.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/coreclr/vm/peimagelayout.cpp Implements the Apple Silicon-specific reserve/commit strategy to avoid unreliable executable permission transitions.
src/installer/tests/Assets/Projects/HelloWorld/Program.cs Adds launch_self to spawn multiple child processes in parallel for stressing startup image loading.
src/installer/tests/AppHost.Bundle.Tests/AppLaunch.cs Adds an OSX regression test that runs the compressed self-contained single-file with launch_self.

Comment thread src/coreclr/vm/peimagelayout.cpp Resolved
Comment thread src/coreclr/vm/peimagelayout.cpp Resolved
Comment thread src/coreclr/vm/peimagelayout.cpp Resolved
Comment thread src/coreclr/vm/peimagelayout.cpp Resolved
Comment thread src/coreclr/vm/peimagelayout.cpp Resolved
elinor-fung and others added 2 commits April 24, 2026 13:39
…Silicon

In FlatImageLayout::LoadImageByCopyingParts, the image region is allocated
with MEM_RESERVE_EXECUTABLE (MAP_JIT on macOS) as PAGE_READWRITE, and then
executable sections are promoted to PAGE_EXECUTE_READ via mprotect after
the content is copied. On macOS 15 Apple Silicon, this RW->RX transition
on MAP_JIT memory has been observed to intermittently leave pages
non-executable at the kernel level, causing sporadic
AccessViolationException crashes.

Avoid the unreliable transition on Apple Silicon: reserve the whole region
as PAGE_NOACCESS first, then commit each section directly with its final
executable/writable protection, copying executable content under
PAL_JitWriteProtect. Going from PROT_NONE -> RWX via a fresh commit is
reliable; the problematic RW -> RWX transition no longer occurs.

Fixes dotnet#123324.

Co-authored-by: Copilot <[email protected]>
Add a 'launch_self' option to the HelloWorld test asset that spawns
5 copies of itself in parallel, and a test that invokes it on the
compressed self-contained bundle. Concurrent child process launches
reliably exercise FlatImageLayout::LoadImageByCopyingParts on macOS
Apple Silicon - without the fix, the parent process crashes with an
AccessViolationException in a large majority of runs.

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings April 24, 2026 20:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread src/coreclr/vm/peimagelayout.cpp Resolved
Comment thread src/coreclr/vm/peimagelayout.cpp Resolved
Copilot AI review requested due to automatic review settings April 24, 2026 20:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/coreclr/vm/peimagelayout.cpp Resolved
Empty sections (VirtualSize == 0) would pass 0 to ClrVirtualAlloc/ClrVirtualProtect,
which translates to mprotect with length 0 and fails with EINVAL. Skip such
sections entirely - there is nothing to commit, copy, or protect.

Co-authored-by: Copilot <[email protected]>
@elinor-fung
elinor-fung merged commit 0f01d22 into dotnet:main Apr 29, 2026
137 of 139 checks passed
@github-project-automation github-project-automation Bot moved this to Done in AppModel Apr 29, 2026
@elinor-fung
elinor-fung deleted the fix123324 branch April 29, 2026 20:45
@elinor-fung

Copy link
Copy Markdown
Member Author

/backport to release/10.0

@github-actions github-actions Bot unlocked this conversation Aug 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

@elinor-fung backporting to release/10.0 failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git cherry-pick 0f01d22be46fc2daa2cee0c86b467a60d654afc6

Auto-merging src/coreclr/vm/peimagelayout.cpp
CONFLICT (content): Merge conflict in src/coreclr/vm/peimagelayout.cpp
Auto-merging src/installer/tests/AppHost.Bundle.Tests/AppLaunch.cs
CONFLICT (content): Merge conflict in src/installer/tests/AppHost.Bundle.Tests/AppLaunch.cs
error: could not apply 0f01d22be46... Fix sporadic AccessViolation in compressed single-file apps on Apple Silicon (#127355)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"


$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix sporadic AccessViolation in compressed single-file apps on Apple Silicon
Using index info to reconstruct a base tree...
M	src/coreclr/vm/peimagelayout.cpp
Falling back to patching base and 3-way merge...
Auto-merging src/coreclr/vm/peimagelayout.cpp
CONFLICT (content): Merge conflict in src/coreclr/vm/peimagelayout.cpp
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Fix sporadic AccessViolation in compressed single-file apps on Apple Silicon
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

.NET 10, 9, 8, 7, osx-arm64: AccessViolationException in a single-file compressed app

3 participants