Repository navigation
[Mono.Android-Tests] Enable C# memory-safety rules v2 - #13736
Conversation
There was a problem hiding this comment.
🟢 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
65c12d4 to
a7a74f0
Compare
a7a74f0 to
7981f0a
Compare
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
⚠️ 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
<!-- 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]>
7981f0a to
4134fa2
Compare
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.On-device execution was not run; this is compiler activation and implementation-context coverage, not a runtime behavior change.