Repository navigation
[CodeGen-Binding] Enable C# memory-safety rules v2 - #13737
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the test-fixture updates to memory safety rules version 2.
No open issues 🎉
What changed in this PR
Updates .NET for Android’s code-generation test projects to use memory safety rules version 2 and adapts existing JNI and native-library fixtures to those rules.
Changes:
- Enables preview C# and memory safety rules version 2 in three test projects.
- Moves unsafe JNI operations into local blocks with safety comments, keeping constructors and methods safely callable.
- Explicitly marks two native imports as
safe.
| File | Description |
|---|---|
tests/CodeGen-Binding/Xamarin.Android.LibraryProjectZip-LibBinding/Xamarin.Android.LibraryProjectZip-LibBinding.csproj |
Enables preview C# and memory safety rules version 2. |
tests/CodeGen-Binding/Xamarin.Android.LibraryProjectZip-LibBinding/NativeLib.cs |
Marks both native imports as safely callable. |
tests/CodeGen-Binding/Xamarin.Android.LegacyThresholdBinding/Xamarin.Android.LegacyThresholdBinding.csproj |
Enables the new compiler settings for legacy binding fixtures. |
tests/CodeGen-Binding/Xamarin.Android.LegacyThresholdBinding/LegacyThresholdTypes.cs |
Localizes unsafe constructor completion and method invocation. |
tests/CodeGen-Binding/Xamarin.Android.LegacyThresholdBinding/LegacyThresholdThrowableDerived.cs |
Localizes unsafe constructor completion for the derived throwable. |
tests/CodeGen-Binding/Xamarin.Android.LegacyThresholdBinding/LegacyThresholdThrowable.cs |
Localizes unsafe throwable construction and method invocation. |
tests/CodeGen-Binding/Xamarin.Android.JcwGen-Tests/Xamarin.Android.JcwGen-Tests.csproj |
Enables the new compiler settings for the test application. |
tests/CodeGen-Binding/Xamarin.Android.JcwGen-Tests/ModernThresholdDerived.cs |
Localizes unsafe constructor completion for the modern binding fixture. |
Review effort: Balanced
4001485 to
43ad051
Compare
43ad051 to
0fa7a00
Compare
<!-- 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 binding test app and its two hand-written fixture libraries into memory-safety rules v2 and preview language mode. The libraries need their own settings because an app's compiler properties do not activate referenced projects. Leave the repository-wide defaults unchanged. Keep the fixture constructors and virtual methods safe-callable, use local unsafe contexts for their owned no-argument JNI calls, and classify the two primitive-returning fixture P/Invokes as safe. Validation: * `./dotnet-local.sh build tests/CodeGen-Binding/Xamarin.Android.JcwGen-Tests/Xamarin.Android.JcwGen-Tests.csproj -c Debug -m:2 -nr:false -p:UseSharedCompilation=false` - passed, including both fixture libraries and APK generation. * The build reported 11 binding/nullability/analyzer warnings; no diagnostics were suppressed. * Evaluated all three binding projects as v2/preview and verified that an unrelated test library remains unactivated. Root Directory.Build.props is still identical to main. On-device execution was not run; this layer changes compiler activation and implementation contexts, not runtime behavior. Co-authored-by: Copilot App <[email protected]>
0fa7a00 to
a255311
Compare
|
/review |
|
✅ Android PR Reviewer completed successfully!
|
There was a problem hiding this comment.
Code assessment: no blocking defects found; CI pending. Findings: 0 ❌ errors, 0
Reviewed all eight changed files in full, the JNI implementations and safety contracts, the native exports, project imports, and existing constructor/virtual-dispatch/P/Invoke tests. The v2/preview settings are scoped to the three projects, Configuration.props preserves their explicit language version, and the local unsafe contexts preserve the original calls, ownership-transfer order, and virtual dispatch. Both safe extern declarations match native implementations that take no arguments and return constants.
CI: All 38 surfaced checks were inspected: 14 succeeded, 23 were in progress, and the aggregate check was queued; none had reported failure. Azure build 1697740 is therefore not confirmed green. Direct Azure build/timeline retrieval was blocked by a login requirement and a CLI initialization error, so the overall Azure result and detailed job status could not be independently verified. No local build or device tests were run during this review; the Debug build result is author-reported evidence, not independently reproduced here.
Generated by Android PR Reviewer for #13737 · copilot · gpt61 · 28.4 AIC · ⌖ 7.33 AIC · ⊞ 20.3K
Comment /review to run again
| { | ||
| [DllImport("simple")] | ||
| public static extern int SampleFunction (); | ||
| public static safe extern int SampleFunction (); |
There was a problem hiding this comment.
🤖 💡 Documentation — Consider adding a brief // SAFETY: rationale for both safe extern declarations, pointing to jni/simple-lib.c and simple2/jni/simple2-lib.c: the implementations take no arguments and only return integer constants, so callers have no memory-safety obligations. The classification is correct today; recording the native implementation behind that promise would make it easier to reassess if these fixtures later change. A primitive return type alone would not establish that an arbitrary native function is safe.
{Rule: Ground safety explanations in the actual implementation}
Opt the binding test app and its two hand-written fixture libraries into memory-safety rules v2 and preview language mode. The libraries need their own settings because an app's compiler properties do not activate referenced projects. Leave the repository-wide defaults unchanged.
Keep the fixture constructors and virtual methods safe-callable, use local unsafe contexts for their owned no-argument JNI calls, and classify the two primitive-returning fixture P/Invokes as safe.
Validation:
./dotnet-local.sh build tests/CodeGen-Binding/Xamarin.Android.JcwGen-Tests/Xamarin.Android.JcwGen-Tests.csproj -c Debug -m:2 -nr:false -p:UseSharedCompilation=false- passed, including both fixture libraries and APK generation.On-device execution was not run; this layer changes compiler activation and implementation contexts, not runtime behavior.