Skip to content

[release/8.0] Initialize NativeAOT library GS cookie before first managed call - #136342

Closed
LaborJos wants to merge 1 commit into
dotnet:release/8.0-stagingfrom
LaborJos:backport-136265-release-8.0
Closed

LaborJos wants to merge 1 commit into
dotnet:release/8.0-stagingfrom
LaborJos:backport-136265-release-8.0

Conversation

@LaborJos

Copy link
Copy Markdown

Backport of #136265 to release/8.0-staging.
Related customer report: #136201.

Description

Initialize the Unix NativeAOT shared-library GS cookie before publishing the lazy runtime initialization callback. Otherwise the first managed export can save the initial zero cookie in its prologue, initialize the runtime (and change the cookie), then fail the cookie check in its epilogue.

Retain the executable initialization path and the regression test that calls an export with a stack-allocated buffer before any other managed export.

Branch-specific adaptations:

  • Apply the page-size fix to Runtime/unix/PalRedhawkUnix.cpp, the .NET 8 Unix PAL implementation, rather than adding the newer PalUnix.cpp file.
  • Obtain the page size with sysconf(_SC_PAGE_SIZE) in PalVirtualProtect, following the existing Unix PAL page-size query. This branch does not contain minipal/ospagesize.h; page-size lookup must not depend on GC/runtime initialization.
  • Preserve this branch's PalGetModuleHandleFromPointer signature, debug-header population, thunk-pool lock initialization, and reverse-P/Invoke implementation.
  • Use the branch's existing new Type[] syntax for export calling conventions. Do not bring in the unrelated module-initializer test from newer branches.

Customer Impact

Customer-reported NativeAOT shared libraries on linux-x64 abort the process (exit 134) when their first export call has a GS-protected frame, such as a stackalloc or a struct containing a fixed buffer. The report reproduces this with .NET 8.0.32, as well as newer versions.

Calling a different export first or moving the buffer into a separate non-inlined method can avoid the reported crash, but requires callers or library authors to know about the initialization-order issue.

.NET 8 is close to end of support; please consider whether this can still be included in remaining servicing.

Regression

No known recent servicing regression. The report reproduces the problem on .NET 8, 9, 10, and 11 RC2.

Testing

  • Added the upstream SharedLibrary smoke regression test, including its first-call ordering requirement.
  • Locally compiled the adapted .NET 8 SharedLibrary.cs source in a standalone Release net8.0 project: 0 warnings, 0 errors.
  • Locally syntax-checked the C++ smoke-test driver with Clang 18 on Ubuntu 24.04.
  • git diff --check passed.
  • These are source-level checks only. A full NativeAOT runtime build and execution of the smoke test against the patched runtime have NOT been performed locally. NativeAOT platform CI is requested before merge; the standalone managed compilation does not validate the native startup fix.

Risk

Low-to-moderate. This is a narrow backport of the reviewed mainline fix, but it changes Unix shared-library initialization and memory protection before normal runtime initialization. The page-size query is made independent of GC initialization, and executable cookie initialization remains explicit. The Unix initialization path and shared-library smoke test need CI coverage; Windows behavior should remain unchanged.

Package authoring

No library-package project changes are included: this fix is in the NativeAOT runtime and its smoke test. Please advise if any additional .NET 8 servicing packaging changes are required.

/cc @MichalStrehovsky @agocke

Prepared with assistance from GitHub Copilot.

…net#136265)

Fixes dotnet#136201.

Initialize the Unix GS cookie before registering the lazy runtime
initialization callback, and query the page size independently of GC
initialization. Add coverage for a first export call with a
stack-allocated buffer.

---------

Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot-Session: 417e95b3-e5d3-40a0-b723-4f2b70693851
(cherry picked from commit 53ac1a9)
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 23, 2026
@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, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

@jkotas

jkotas commented Oct 23, 2026

Copy link
Copy Markdown
Member

@jkotas jkotas closed this Oct 23, 2026
@LaborJos
LaborJos deleted the backport-136265-release-8.0 branch October 23, 2026 00:45
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-NativeAOT-coreclr 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