Skip to content

[Mono.Android-Tests] Enable C# memory-safety rules v2 - #13736

Merged
simonrozsival merged 2 commits into
mainfrom
simonrozsival-memory-safety-runtime-tests
Nov 24, 2026
Merged

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

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Opt the Mono.Android runtime test app into memory-safety rules v2 and preview language mode after the Java.Interop test library. Leave the repository-wide defaults and binding test app unchanged.

Keep generated-runner entry points and owned wrappers safe-callable. Add local unsafe contexts for raw JNI handles, terminated UTF-8 construction signatures, argument buffers and callback dispatch, and explicitly classify the native/accessor declarations without weakening product contracts.

The review follow-up adds two brief test comments: static redirection builds a receiver argument buffer for a zero-argument source call, and the native thread-reuse fixture joins its worker before the marshaled callback can outlive the call. No behavior or unsafe classification changes.

Validation:

  • ./dotnet-local.sh build tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -c Debug -m:2 -nr:false -p:UseSharedCompilation=false - passed, including APK generation.
  • The local SDK was built on the preceding layer; SDK product sources are unchanged by this layer. The review follow-up also clarifies one comment in the merged Java.Interop test library.
  • Evaluated the three test entry projects: Java.Interop-Tests and Mono.Android-Tests use v2, while the binding test app is still unactivated.
  • Follow-up comments were checked against the static redirect implementation and native worker join; no test rerun was needed for documentation-only edits.

On-device execution was not run; this is compiler activation and implementation-context coverage, not a runtime behavior change.

Copilot AI balanced review requested due to automatic review settings November 23, 2026 17:19
@simonrozsival simonrozsival changed the title simonrozsival memory safety runtime tests [Mono.Android-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.

🟢 Approval recommended

No active review issues remain for the runtime tests’ memory-safety rules v2 updates.

No open issues 🎉

What changed in this PR

This PR adapts the .NET for Android runtime tests to memory-safety rules version 2, making unsafe operations and their safety assumptions explicit without changing the intended test behavior.

Changes:

  • Enables preview C# and memory-safety rules version 2 in the runtime test project.
  • Narrows unsafe contexts to individual expressions or blocks around JNI and pointer operations.
  • Adds explicit safe/unsafe declarations and documents reference lifetimes, argument validity, and callback requirements.

Paths below are relative to tests/Mono.Android-Tests/Mono.Android-Tests/.

File Description
System/​ExceptionTest.cs Scopes unsafe throwable construction to a documented block.
System.Runtime.InteropServices/​DllImportTest.cs Marks the dlopen declaration explicitly safe.
Mono.Android.NET-Tests.csproj Enables preview C# and memory-safety rules version 2.
Java.Interop/​TrimmableTypeMapTypeManagerTests.cs Isolates stack-pointer operations in the conservative-stack helper.
Java.Interop/​TrimmableTypeMapRuntimeCoverageTests.cs Adds explicit unsafe contexts for construction and callback dispatch.
Java.Interop/​TrimmableTypeMapExportTests.cs Documents and scopes unsafe constructor invocation.
Java.Interop/​TransferredReferenceTests.cs Documents ownership assumptions around unsafe reference deletion.
Java.Interop/​ManagedObjectProxyTests.cs Isolates stack-pointer operations in the conservative-stack helper.
Java.Interop/​JniRuntimeExceptionPolicyTests.cs Scopes native calls and documents callback safety contracts.
Java.Interop/​JnienvTest.cs Annotates native declarations and scopes unsafe JNI calls.
Java.Interop/​JavaMarshalGCBridgeTests.cs Marks safe accessors and narrows unsafe JNI operations.
Java.Interop/​JavaConvertTest.cs Adds an unsafe expression with reference-lifetime justification.
Java.Interop/​ConstructorActivationTests.cs Documents and scopes unsafe constructor calls.
Android.Runtime/​JnienvArrayMarshaling.cs Narrows unsafe context to Java peer construction.

Review effort: Balanced

@simonrozsival
simonrozsival force-pushed the simonrozsival-memory-safety-runtime-tests branch from 65c12d4 to a7a74f0 Compare November 23, 2026 18:11
Base automatically changed from simonrozsival-memory-safety-jni-tests to main November 23, 2026 22:37
@simonrozsival
simonrozsival force-pushed the simonrozsival-memory-safety-runtime-tests branch from a7a74f0 to 7981f0a Compare November 23, 2026 22:37
@simonrozsival

Copy link
Copy Markdown
Member Author

/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 #13736

@simonrozsival
simonrozsival enabled stacked auto-merge (squash) November 23, 2026 22:38
@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 23, 2026

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

⚠️ Needs Changes — CI not yet verified green

Findings: 0 errors, 0 warnings, 1 non-blocking documentation suggestion inline. No blocking correctness issue found in the code reviewed.

Reviewed the diff and full source of all 14 changed files at 7981f0a98645c4ed016500ef8eb2737ffd0136f0, including the native thread helper, JNI callback dispatch contracts, and existing project-level opt-ins. The changes consistently localize unchecked operations while keeping test entry points safe-callable. JNI argument initialization, reference cleanup, and callback dispatch behavior are preserved, and the project opt-in follows the already-activated Java.Interop test library without changing repository-wide defaults.

CI: GitHub reports five Azure DevOps checks in progress and the aggregate check queued for build 1695919; license/cla passed. Azure build/timeline queries could not complete because CLI authentication was unavailable, so the final pipeline result is unverified. This verdict reflects pending CI, not a blocking code finding.

Static review only; I did not independently rerun the APK build reported in the PR description or execute device tests.

Generated by Android PR Reviewer for #13736 · copilot · gpt61 · 51.2 AIC · ⌖ 7.86 AIC · ⊞ 20.3K
Comment /review to run again

simonrozsival and others added 2 commits November 24, 2026 08:46
<!--
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 the Mono.Android runtime test app into memory-safety rules v2 and preview language mode after the Java.Interop test library. Leave the repository-wide defaults and binding test app unchanged.

Keep generated-runner entry points and owned wrappers safe-callable. Add local unsafe contexts for raw JNI handles, terminated UTF-8 construction signatures, argument buffers and callback dispatch, and explicitly classify the native/accessor declarations without weakening product contracts.

Validation:
  * `./dotnet-local.sh build tests/Mono.Android-Tests/Mono.Android-Tests/Mono.Android.NET-Tests.csproj -c Debug -m:2 -nr:false -p:UseSharedCompilation=false` - passed, including APK generation.
  * The local SDK was built on the preceding layer; this layer changes only the runtime test project and its sources.
  * Evaluated the three test entry projects: Java.Interop-Tests and Mono.Android-Tests use v2, while the binding test app is still unactivated.

On-device execution was not run; this is compiler activation and implementation-context coverage, not a runtime behavior change.

Co-authored-by: Copilot App <[email protected]>
Distinguish the no-argument source descriptor from the receiver argument synthesized for instance-to-static redirection. Document that the native thread-reuse fixture joins its worker before returning, so the marshaled callback is not retained past the call.

Documentation-only comments checked against TryInvokeInt32StaticRedirect and reuse-threads.c; no test behavior or unsafe classification changes.

Co-authored-by: Copilot App <[email protected]>
@simonrozsival
simonrozsival merged commit cd0e8cb into main Nov 24, 2026
38 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-memory-safety-runtime-tests branch November 24, 2026 18:58
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