Skip to content

Align CoreCLR GS cookie protection with NativeAOT - #132925

Open
steveisok wants to merge 3 commits into
dotnet:mainfrom
steveisok:steveisok-fix-macos-gs-cookie-e28
Open

Align CoreCLR GS cookie protection with NativeAOT#132925
steveisok wants to merge 3 commits into
dotnet:mainfrom
steveisok:steveisok-fix-macos-gs-cookie-e28

Conversation

@steveisok

@steveisoksteveisok commented Aug 29, 2026

Copy link
Copy Markdown
Member

Fixes#99977

Hosting CoreCLR inside Apple's stock /usr/bin/lldb on macOS arm64 fails: coreclr_initialize
returns HRESULT 0x8007000C. InitGSCookie() temporarily calls ClrVirtualProtect(PAGE_READWRITE)
on s_gsCookie, which lives in Apple's __DATA_CONST segment (via const/READONLY_ATTR).
Apple marks that segment immutable once a Mach exception port owns the process — which happens
when CoreCLR is hosted inside LLDB with PAL_MachExceptionMode set to avoid Apple's guarded Mach
exception-port operations — so the underlying mprotect call fails.

NativeAOT hit and fixed the identical Apple problem in #99173. Its read-only GS cookie feature is
also disabled on WebAssembly, where changing page protections is unnecessary overhead, and on
OpenBSD, where ld.so marks read-only segments immutable at load time. This PR aligns CoreCLR with
that existing NativeAOT platform policy:

  • Define FEATURE_READONLY_GS_COOKIE on all targets except Apple, WebAssembly, and OpenBSD.
  • vars.hpp/vars.cpp: s_gsCookie keeps its read-only const/READONLY_ATTR declaration
    when the feature is enabled; otherwise it is plain writable data.
  • ceemain.cpp: InitGSCookie() skips both ClrVirtualProtect calls when the feature is
    disabled. Cookie generation and the write itself are unchanged on every platform.

On Apple and OpenBSD, this trades a narrow defense-in-depth mitigation for compatibility with the
platform's immutable read-only segments. On WebAssembly, it avoids page-protection overhead that
provides no benefit. Other platforms retain the existing read-only cookie behavior.

Testing

  • ./build.sh clr+libs+host -c Release — succeeded, 0 errors/0 warnings.
  • ./build.sh clr.runtime -c Debug — succeeded, 0 errors/0 warnings; the macOS binary places
    s_gsCookie in writable __DATA,__common.
  • Repro'd the failure and the fix directly: a minimal harness that dlopens libcoreclr.dylib
    and calls coreclr_initialize, run under Apple's stock LLDB with PAL_MachExceptionMode=7.
    Before: 0x8007000C. After: 0x00000000 (S_OK).
  • Validated end-to-end through dotnet/diagnostics's SOS test harness (stock LLDB +
    libsosplugin.dylib): ObjectInspectionTests.DumpObj_Mt_Class_Md_Chain passes against the
    patched runtime (dumpheap, dumpobj, dumpmt, dumpclass, dumpmd), and fails with the
    same 0x8007000C against an unpatched one.

Note

This description was drafted with the assistance of an AI coding agent (GitHub Copilot).

InitGSCookie() places s_gsCookie in Apple's __DATA_CONST,__const segment
(via READONLY_ATTR/const) and then temporarily calls
ClrVirtualProtect(PAGE_READWRITE) to initialize it. Apple marks the whole
__DATA_CONST segment immutable at load, so that mprotect call fails with
ERROR_INVALID_ACCESS, and coreclr_initialize returns HRESULT 0x8007000C.
This is most visible when hosting CoreCLR inside Apple's stock LLDB with
PAL_MachExceptionMode set to avoid Apple's guarded Mach exception ports.
NativeAOT hit the same issue and fixed it in dotnet#99173 by disabling
FEATURE_READONLY_GS_COOKIE on Apple (src/coreclr/nativeaot/Runtime). Mirror
that fix for CoreCLR: keep s_gsCookie in ordinary writable data and skip the
now-unnecessary protection transitions in InitGSCookie() on TARGET_APPLE,
while keeping the existing read-only placement and randomized initialization
on all other platforms.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 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: @agocke
See info in area-owners.md if you want to be subscribed.

@steveisok
steveisok requested review from a team and jkotasAugust 29, 2026 18:35

CopilotAI 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.

Copilot review overview

🟢 Approval recommended

Review tier: Lite
Findings: 1 Low severity

New issues introduced by this change (1)
SeverityFinding
Low severitysrc/​coreclr/​vm/​vars.hpp — The comment describing why const/volatile are used is now inaccurate on Apple builds: when…
What changed in this PR

This PR adjusts CoreCLR’s GS cookie storage and initialization to avoid relying on temporarily changing page protections on Apple platforms, where that pattern can fail at runtime. It does so by disabling the “read-only GS cookie” protection on TARGET_APPLE while preserving the existing behavior on non-Apple platforms.

Changes:

  • Introduces FEATURE_READONLY_GS_COOKIE, defined for all targets except TARGET_APPLE.
  • Makes s_gsCookie read-only (const + READONLY_ATTR) only when the feature is enabled; otherwise it is normal writable data.
  • Guards InitGSCookie()’s ClrVirtualProtect calls so they only run when the feature is enabled.
FileDescription
src/​coreclr/​vm/​vars.hppAdds FEATURE_READONLY_GS_COOKIE definition (disabled on Apple) and makes s_gsCookie declaration conditional on the feature.
src/​coreclr/​vm/​vars.cppMakes the s_gsCookie definition conditional (const vs writable) to match the updated declaration.
src/​coreclr/​vm/​ceemain.cppGuards the ClrVirtualProtect transitions around GS cookie initialization behind FEATURE_READONLY_GS_COOKIE.

Comment threadsrc/coreclr/vm/vars.hpp Outdated
Comment threadsrc/coreclr/vm/vars.hpp Outdated
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9556d45f-1583-4d9b-a093-81d771aa8349
CopilotAI review requested due to automatic review settings August 30, 2026 01:34

CopilotAI 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.

Copilot review overview

🟡 Changes recommended

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review tier: Lite
Findings: 1 Medium severity · 2 Low severity

New issues introduced by this change (2)
SeverityFinding
Low severitysrc/​coreclr/​vm/​vars.hpp — The comment above s_gsCookie is no longer accurate now that the declaration is conditional: in the…
Medium severitysrc/​coreclr/​vm/​CMakeLists.txt — This condition disables FEATURE_READONLY_GS_COOKIE not only on Apple but also on OpenBSD and WASM.…
Pre-existing issues (1)
SeverityFinding
Low severitysrc/​coreclr/​vm/​vars.hpp — The comment describing why const/volatile are used is now inaccurate on Apple builds: when… View comment

Comment threadsrc/coreclr/vm/vars.hpp Outdated
Comment threadsrc/coreclr/vm/CMakeLists.txt
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 9556d45f-1583-4d9b-a093-81d771aa8349
CopilotAI review requested due to automatic review settings August 30, 2026 21:15
@steveisoksteveisok changed the title Disable read-only GS cookie protection on Apple platforms (fixes coreclr_initialize under stock LLDB)Align CoreCLR GS cookie protection with NativeAOTAug 30, 2026

CopilotAI 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.

Copilot review overview

🟡 Changes recommended

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review tier: Lite
Findings: 1 Medium severity

New issues introduced by this change (1)
SeverityFinding
Medium severitysrc/​coreclr/​vm/​vars.cpp — When FEATURE_READONLY_GS_COOKIE is enabled, the declaration in vars.hpp includes READONLY_ATTR but…
Issues resolved since last review (3)
SeverityFinding
Medium severitysrc/​coreclr/​vm/​CMakeLists.txt — This condition disables FEATURE_READONLY_GS_COOKIE not only on Apple but also on OpenBSD and WASM.… View resolved comment
Low severitysrc/​coreclr/​vm/​vars.hpp — The comment above s_gsCookie is no longer accurate now that the declaration is conditional: in the… View resolved comment
Low severitysrc/​coreclr/​vm/​vars.hpp — The comment describing why const/volatile are used is now inaccurate on Apple builds: when… View resolved comment

Comment on lines +255 to +259
#ifdef FEATURE_READONLY_GS_COOKIE
extern "C" RAW_KEYWORD(volatile) const GSCookie s_gsCookie = 0;
#else
extern "C" RAW_KEYWORD(volatile) GSCookie s_gsCookie = 0;
#endif
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[macOS] Hosting CoreCLR inside an LLDB plugin fails to initialize the runtime

3 participants

@steveisok@jkotas