Skip to content

[release/10.0] Initialize NativeAOT library GS cookie before first managed call - #136343

Closed
LaborJos wants to merge 1 commit into
dotnet:release/10.0from
LaborJos:backport-136265-release-10.0
Closed

LaborJos wants to merge 1 commit into
dotnet:release/10.0from
LaborJos:backport-136265-release-10.0

Conversation

@LaborJos

@LaborJos LaborJos commented Oct 23, 2026 •

Copy link
Copy Markdown

Backport of #136265 to release/10.0.
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:

  • Preserve this branch's PalGetModuleHandleFromPointer signature and PopulateDebugHeaders call.
  • 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.
  • Use the branch's existing new Type[] syntax for export calling conventions.

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 10.0.13, as well as .NET 8 and other 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.

Regression

No known regression specific to .NET 10. The issue was also reproduced on .NET 8 and .NET 9.

Testing

  • Added the upstream SharedLibrary smoke regression test, including its first-call ordering requirement.
  • Locally compiled the adapted .NET 10 SharedLibrary.cs source in a standalone Release net10.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.

Third-party attribution

Submission containing materials of a third party: Michal Strehovský (@MichalStrehovsky), the author of the original fix in #136265 (commit 53ac1a9), and the .NET Foundation and contributors as identified in the source-file notices. The original code is licensed under the MIT License: https://github.com/dotnet/runtime/blob/main/LICENSE.TXT.

This PR backports that existing fix with the branch-specific adaptations described above; it does not claim ownership of the original work. Original source-file license notices, commit authorship, and upstream commit attribution are retained.

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

@LaborJos

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@MichalStrehovsky

Copy link
Copy Markdown
Member

@LaborJos we need a good justification for backports because they add risk. We don't blanket backport all fixed. The obvious backports are corruptions, reliability issues, regressions, etc.

Something that didn't work since at least .NET 8, has a clear failure mode and possible workarounds (do the stackalloc in a method called from the UnmanagedCallersOnly method) is a tough sell.

Can you share more about your scenario and why backport is needed? The decision is not on me, we have a council that decides. But I'll not bring forward a weak case for consideration.

@MichalStrehovsky

Copy link
Copy Markdown
Member

Closing per #136343 (comment).

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

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.

2 participants