Skip to content

[NativeAOT] Enable caller-unsafe memory safety rules - #13440

Merged
simonrozsival merged 1 commit into
mainfrom
simonrozsival-nativeaot-memory-safety-e47
Nov 10, 2026
Merged

simonrozsival merged 1 commit into
mainfrom
simonrozsival-nativeaot-memory-safety-e47

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Context: https://devblogs.microsoft.com/dotnet/improving-csharp-memory-safety/

The NativeAOT support library already uses preview syntax and documents several unsafe contracts, but it does not opt into the new caller-unsafe rules. Enable MemorySafetyRulesVersion=2 so the compiler enforces those contracts instead of accepting the legacy safety model. Use the SDK property rather than a raw Features flag, which can propagate into satellite compilation without its required language version.

Mark the private JNI initialization entry points and the JNI-string directory helper as caller-unsafe, document their VM, current-thread environment, and borrowed-reference obligations, and retain explicit inner unsafe contexts. Native entry-point names, ABI signatures, ownership, and lifetimes remain unchanged; no released public API is newly marked unsafe.

Validation used the pinned .NET SDK 12.0.100-alpha.1.26551.101, Release/net12.0, Android API 37:

  • bin/Release/dotnet/dotnet build src/Microsoft.Android.Runtime.NativeAOT/Microsoft.Android.Runtime.NativeAOT.csproj -c Release -p:BuildProjectReferences=false -warnaserror -nr:false -v:minimal — passed with 0 warnings and 0 errors against locally built Mono.Android and Java.Interop references.
  • The saved normal-build Csc command contains /memorysafetyrulesversion:2 /langversion:preview /unsafe+. Read-only metadata inspection returned ModuleRules: [2] for both the normal implementation and reference DLLs. The reader matches attribute names only; independent PE/PDB pairing was not established.
  • make prepare CONFIGURATION=Release — passed. make all CONFIGURATION=Release — solution build completed with 48 upstream warnings, but workload configuration failed because documentation and extra API-level outputs were missing. After initializing external/android-api-docs, DOTNET_CLI_USE_MSBUILD_SERVER=0 make leeroy CONFIGURATION=Release MSBUILD_FLAGS='-m:1 -nr:false -p:UseSharedCompilation=false' generated the API 37 documentation, then was stopped when duplicate sessions were consolidated. Full SDK validation is not complete.

No on-device tests were run in this session. NativeAOT runtime/ABI coverage and passed/failed/skipped counts remain unvalidated; the managed support-library build is not evidence of an on-device NativeAOT run.

Declare JNI initialization entrypoint and string-reference obligations while preserving native ABI and reference lifetimes.

Co-authored-by: Copilot App <[email protected]>

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.

🔵 Needs a closer look

Incomplete full-SDK and on-device NativeAOT startup/ABI validation requires human review, as managed support-library compilation does not cover these integration paths.

No open issues 🎉

What changed in this PR

Enables compiler-enforced caller-unsafe contracts in the NativeAOT support library while preserving native entry points, ABI signatures, ownership, and lifetimes.

Changes:

  • Sets MemorySafetyRulesVersion=2 alongside the existing preview language setting.
  • Marks JNI initialization callbacks and the directory helper as caller-unsafe, documents their obligations, and retains explicit inner unsafe contexts.
  • Normalizes spacing around unsafe expressions.

The PR reports a successful managed support-library build. Full SDK validation remains incomplete, and on-device NativeAOT behavior has not been validated.

File Description
src/​Microsoft.Android.Runtime.NativeAOT/​Microsoft.Android.Runtime.NativeAOT.csproj Enables memory safety rules version 2.
src/​Microsoft.Android.Runtime.NativeAOT/​Java.Interop/​JreRuntime.cs Normalizes unsafe-expression spacing.
src/​Microsoft.Android.Runtime.NativeAOT/​Android.Runtime.NativeAOT/​JavaInteropRuntime.cs Adds caller-unsafe declarations, safety documentation, and explicit unsafe contexts.

Review effort: Balanced

@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 10, 2026
@simonrozsival
simonrozsival enabled auto-merge (squash) November 10, 2026 01:00
@simonrozsival

Copy link
Copy Markdown
Member Author

@dalexsoto review

@dalexsoto dalexsoto left a comment

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.

Reviewed all three changed files and the material integration paths (the other source files in the NativeAOT support library, the Java.Interop and Mono.Android project settings and their callee signatures, the pinned SDK version, and the existing CI results for this head), followed by a separate final search for independent blockers. The unsafe (...) expression forms, the caller-unsafe JNI_OnLoad, init and SetAppContextBaseDirectory declarations with their documented obligations, and the MemorySafetyRulesVersion=2 property match how Java.Interop already opts in; native entry-point names and signatures are unchanged. No high-confidence blocking issue remains in this static review. No builds, tests or on-device NativeAOT runs were independently executed.

@simonrozsival
simonrozsival merged commit 4748320 into main Nov 10, 2026
41 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-nativeaot-memory-safety-e47 branch November 10, 2026 01:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants