Repository navigation
[NativeAOT] Enable caller-unsafe memory safety rules - #13440
Conversation
Declare JNI initialization entrypoint and string-reference obligations while preserving native ABI and reference lifetimes. Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🔵 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=2alongside 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
|
@dalexsoto review |
dalexsoto
left a comment
There was a problem hiding this comment.
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.
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=2so the compiler enforces those contracts instead of accepting the legacy safety model. Use the SDK property rather than a rawFeaturesflag, 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./memorysafetyrulesversion:2 /langversion:preview /unsafe+. Read-only metadata inspection returnedModuleRules: [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 initializingexternal/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.