Skip to content

[CodeGen-Binding] Enable C# memory-safety rules v2 - #13737

Merged
simonrozsival merged 1 commit into
mainfrom
simonrozsival-memory-safety-codegen-tests
Nov 24, 2026
Merged

simonrozsival merged 1 commit into
mainfrom
simonrozsival-memory-safety-codegen-tests

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

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.

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

@simonrozsival
simonrozsival force-pushed the simonrozsival-memory-safety-codegen-tests branch 2 times, most recently from 4001485 to 43ad051 Compare November 23, 2026 22:37
@simonrozsival
simonrozsival force-pushed the simonrozsival-memory-safety-codegen-tests branch from 43ad051 to 0fa7a00 Compare November 24, 2026 07:52
Base automatically changed from simonrozsival-memory-safety-runtime-tests to main November 24, 2026 18:58
<!--
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]>
@simonrozsival
simonrozsival force-pushed the simonrozsival-memory-safety-codegen-tests branch from 0fa7a00 to a255311 Compare November 24, 2026 18:58
@simonrozsival

Copy link
Copy Markdown
Member Author

/review

@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 24, 2026
@simonrozsival
simonrozsival enabled stacked auto-merge (squash) November 24, 2026 20:03
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13737

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

Code assessment: no blocking defects found; CI pending. Findings: 0 ❌ errors, 0 ⚠️ warnings, 1 💡 non-blocking documentation suggestion posted inline.

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 ();

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.

🤖 💡 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}

@simonrozsival
simonrozsival merged commit 3b306c8 into main Nov 24, 2026
38 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-memory-safety-codegen-tests branch November 24, 2026 21:17
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