Skip to content

[Java.Interop-Tests] Enable C# memory-safety rules v2 - #13735

Merged
simonrozsival merged 2 commits into
mainfrom
simonrozsival-memory-safety-jni-tests
Nov 23, 2026
Merged

simonrozsival merged 2 commits into
mainfrom
simonrozsival-memory-safety-jni-tests

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Opt only the Java.Interop test library into memory-safety rules v2 and the required preview language mode. The repository-wide default remains unchanged.

Keep test entry points and owned typed marshaling wrappers safe-callable for the generated MSTest registry. Move unchecked JNI operations into local unsafe contexts with lifetime and signature proofs, and use the existing options-only base constructor instead of the obsolete null-reference sentinel in one test peer.

Remove the unused test-only JniPeerMembersExtensions class rather than retain callable wrappers over unchecked caller-provided JNI descriptors. The class has no callers or explicit project includes; product marshaling helpers are unchanged.

Validation:

  • make prepare CONFIGURATION=Debug MSBUILD_ARGS='-m:2 -nr:false -p:UseSharedCompilation=false' - passed.
  • make leeroy CONFIGURATION=Debug MSBUILD_ARGS='-m:2 -nr:false -p:UseSharedCompilation=false' - passed.
  • ./dotnet-local.sh build tests/Mono.Android-Tests/Java.Interop-Tests/Java.Interop-Tests.NET.csproj -c Debug -m:2 -nr:false -p:UseSharedCompilation=false - passed.
  • Evaluated the three test-app/library entry projects: only Java.Interop-Tests has the new v2 opt-in at this layer.
  • After the review fix, rebuilt the isolated JNI library and the dependent runtime and binding test projects; all passed. The binding project retains its existing 11 warnings.

On-device execution was not run; this layer changes compiler activation and implementation contexts, not runtime behavior.

<!--
Follow the title and description guidance:
https://github.com/dotnet/android/blob/main/Documentation/workflow/commit-messages.md

Explain the problem, why the change is needed, and how it fixes the problem.
Link to issues fixed, if applicable.
State what you tested and the results. If tests were not run, say why.

Keep the description concise. Do not copy these instructions into the
visible description or add a checklist that repeats them.
-->

Opt only the Java.Interop test library into memory-safety rules v2 and the required preview language mode. The repository-wide default remains unchanged.

Keep test entry points and owned typed marshaling wrappers safe-callable for the generated MSTest registry. Move unchecked JNI operations into local unsafe contexts with lifetime and signature proofs, and use the existing options-only base constructor instead of the obsolete null-reference sentinel in one test peer.

Validation:
  * `make prepare CONFIGURATION=Debug MSBUILD_ARGS='-m:2 -nr:false -p:UseSharedCompilation=false'` - passed.
  * `make leeroy CONFIGURATION=Debug MSBUILD_ARGS='-m:2 -nr:false -p:UseSharedCompilation=false'` - passed.
  * `./dotnet-local.sh build tests/Mono.Android-Tests/Java.Interop-Tests/Java.Interop-Tests.NET.csproj -c Debug -m:2 -nr:false -p:UseSharedCompilation=false` - passed.
  * Evaluated the three test-app/library entry projects: only Java.Interop-Tests has the new v2 opt-in at this layer.

On-device execution was not run; this layer changes compiler activation and implementation contexts, not runtime behavior.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 17:19
@simonrozsival simonrozsival changed the title simonrozsival memory safety jni tests [Java.Interop-Tests] Enable C# memory-safety rules v2 Nov 23, 2026

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.

🟡 Changes recommended

Four JNI helpers accept unchecked signatures while supplying only one marshaled argument, risking out-of-bounds stack reads unless they remain caller-unsafe with explicit safety contracts or validate compatibility and peer lifetime before dispatch.

1 open issue
What changed in this PR

This PR adapts the .NET for Android Java.Interop test suite to C#’s updated memory-safety model, making unsafe JNI operations explicit while retaining existing test coverage.

Changes:

  • Enables preview language features and memory-safety rules version 2.
  • Replaces method-wide unsafe contexts with focused expressions or blocks and safety comments.
  • Replaces a pointer-based base-constructor call with the options-only overload.
File Description
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JniTypeUtf8Test.cs Scopes unsafe UTF-8 lookups and JNI calls.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JniTypeTest.cs Narrows unsafe construction, invocation, and native registration.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JniTypeManagerTests.cs Isolates an unsafe constructor invocation.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JniRuntimeTest.cs Marks runtime construction as caller-unsafe.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JniPeerMembersTests.cs Scopes unsafe calls in remapping helpers.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JniPeerMembersExtensions.cs Moves marshaled argument dispatch into unsafe blocks.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JniNativeRegistrationTests.cs Adds explicit unsafe method lookups and construction.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JniEnvironmentTests.cs Narrows unsafe object creation.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JavaPeerControlBlockTests.cs Scopes unsafe peer creation in lifecycle tests.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JavaPeerableExtensionsTests.cs Removes pointer-based base construction and scopes JNI calls.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JavaObjectTest.cs Isolates unsafe constructor dispatch.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JavaExceptionTests.cs Scopes unsafe throwable argument buffers and calls.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​GetThis.cs Isolates unsafe JNI invocation in the getter.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​FinalizerHelpers.cs Narrows stack-pointer handling to an unsafe block.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​CallNonvirtualBase.cs Isolates unsafe virtual method dispatch.
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop-Tests.NET.csproj Enables preview syntax and memory-safety rules version 2.

Review effort: Balanced

The helpers accept arbitrary JNI descriptors but always allocate one marshaled argument slot. Exposing those unchecked dispatch paths as safe can permit mismatched argument counts or return types.

The internal test-only class and its five helpers have no callers or explicit project includes. Remove it instead of adding validation machinery or carrying speculative unsafe APIs. Product marshaling helpers are unchanged.

Validation: ./dotnet-local.sh build tests/Mono.Android-Tests/Java.Interop-Tests/Java.Interop-Tests.NET.csproj -c Debug -m:2 -nr:false -p:UseSharedCompilation=false passed with no warnings or errors.

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

Copy link
Copy Markdown
Member Author

/reviw

@jonathanpeppers

Copy link
Copy Markdown
Member

/review

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13735

@github-actions github-actions Bot 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.

Overall: no blocking code findings. 0 errors, 0 warnings, 1 non-blocking inline documentation suggestion.

Reviewed all 16 changed files, including the removed helper, and checked the constructor and remapping implementations. The project-local v2/preview opt-in keeps the wider defaults unchanged; test entry points remain safe-callable, argument buffers retain their synchronous lifetimes, and the options-only base constructor preserves the intended peer-construction path. The removed test-only marshaling helpers have no remaining callers or explicit project includes, resolving the earlier review concern without changing product helpers.

CI: All 38 returned GitHub checks report success, including the aggregate dotnet-android check for Azure build 1695517. Independent Azure build-result verification was blocked because az devops invoke requires authentication in this environment, so this is not an independently verified all-green CI verdict. This was a source review; local builds and device tests were not rerun.

Generated by Android PR Reviewer for #13735 · copilot · gpt61 · 43.9 AIC · ⌖ 7.8 AIC · ⊞ 20.3K
Comment /review to run again

@simonrozsival
simonrozsival merged commit c9277ec into main Nov 23, 2026
38 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-memory-safety-jni-tests branch November 23, 2026 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants