Skip to content

[Unsafe][Java.Interop] Prepare primitive-array borrowing - #13186

Merged
simonrozsival merged 6 commits into
mainfrom
simonrozsival-primitive-array-borrowing
Oct 27, 2026
Merged

simonrozsival merged 6 commits into
mainfrom
simonrozsival-primitive-array-borrowing

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Context: #11467

Under the updated C# memory-safety rules, an unsafe member declaration places obligations on its caller; it no longer merely permits pointer operations in the implementation.

Primitive-array acquisition returns a managed owner, while the element indexers return references which can outlive that owner. Keep all nine GetElements() methods safe-callable and mark the eight ref-return indexers unsafe, documenting the array/acquisition lifetime and aliasing requirements.

Remove implementation-only unsafe modifiers from search, clear, copy, and synchronization overrides. Put unchecked operations in explicit unsafe blocks with SAFETY comments explaining their invariants.

Store acquired buffers as void* and pass typed pointers directly to element owners. Preserve the public IntPtr getter, ref-return ABI, bounds/disposed checks, allocations, and JNI release policy.

Retain pointer-declaration modifiers required by the C#14 baseline, with notes to remove them during the memory-safety opt-in. Update the T4 template and generated source together. This change does not enable the new rules.


Pull Request title and description should follow the commit-messages.md workflow documentation, and in particular should include:

  • Useful description of why the change is necessary.
  • Links to issues fixed
  • Unit tests

Copilot AI balanced review requested due to automatic review settings October 19, 2026 09:32

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.

🔵 Needs a closer look

JNI buffer-lifetime changes warrant human confirmation that the separately reported final-stack validation adequately covers this layer.

No open issues 🎉

What changed in this PR

Prepares Java.Interop primitive-array ownership and borrowing contracts for #11467 without enabling updated memory-safety rules.

Changes:

  • Stores acquired buffers as pointers while preserving compatibility getters and release behavior.
  • Documents unsafe ref borrowing while keeping acquisition safe-callable.
  • Adds regression tests for all eight primitive types.
File Description
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JavaPrimitiveArraySafetyTests.cs Tests borrowing, bounds, disposal, and copy-back.
external/​Java.Interop/​src/​Java.Interop/​Java.Interop/​JavaPrimitiveArrays.tt Updates pointer forwarding and borrowing contracts.
external/​Java.Interop/​src/​Java.Interop/​Java.Interop/​JavaPrimitiveArrays.cs Applies template changes across eight primitive types.
external/​Java.Interop/​src/​Java.Interop/​Java.Interop/​JavaArray.cs Introduces pointer-backed buffer storage and internal access.

Review effort: Balanced

Base automatically changed from simonrozsival-utf8-remapping-contracts to main October 23, 2026 07:43
simonrozsival and others added 5 commits October 23, 2026 12:34
Keep JNI primitive-array ownership pointer-backed without changing the
public IntPtr compatibility getter, ref-return ABI, or release policy.
Document acquisition and array lifetimes for borrowed references while
keeping typed and generic GetElements acquisition safe-callable.

Replay standalone f8dd583bebb1c6a645927e12197ea595c744d3ef onto the
verified UTF8 parent for the user-directed strict stack.  Preserve the
inherited callback, constructor, and UTF8 preparation unchanged.

Canonical T4 regeneration/parity, parent and integrated compiler-only
checks, persisted test compilation, and public ABI comparison passed.
Prior SDK/APK evidence belongs to the standalone snapshot, not this
integration commit.  Device execution remains pending permission.

Co-authored-by: Copilot App <[email protected]>
Keep the primitive ref-return indexers explicitly unsafe for callers,
and put their pointer dereferences in local unsafe blocks as well.
The updated memory-safety mode does not use the declaration modifier
as the implementation body context.

Keep disposed/bounds guards outside the local block and preserve safe
GetElements acquisition, ref-return ABI, and pointer owner lifetimes.
Regenerate the paired source canonically from JavaPrimitiveArrays.tt.

Legacy compiler-only builds and persisted test compilation pass, with
all 1032 public signatures and eight ref-return ABIs unchanged.
T4 generation parity passes.  Earlier SDK/APK evidence remains tied
to its original snapshot; new-mode final activation and device
validation remain coordinator-owned and are not claimed here.

Co-authored-by: Copilot App <[email protected]>
Distinguish safe managed acquisition and address exposure from unsafe
ref borrowing.  Document the actual JNI extent, element representation,
array/acquisition lifetime, alias/release exclusion, and failure duties.

Add concrete SAFETY rationales and explicit implementation contexts for
raw ownership and existing ref-indexer regression calls.  Regenerate the
primitive arrays from the canonical T4 input.  Preserve existing method
contracts, guards, accepted calls, allocations, and release behavior.

Canonical generation and static checks pass.  No new compiler or runtime
validation is claimed; prior results retain their original source SHAs.

Co-authored-by: Copilot App <[email protected]>
Keep the unsafe audit focused on caller contracts and narrow pointer
contexts.  Shorten safety documentation without changing guards, ABI,
accepted calls, ownership, allocations, or release behavior.

Remove the newly added general array fixture and its private scaffolding.
This layer fixes no demonstrated behavioral bug; existing main array
tests remain unchanged.  Do not attribute the earlier bounds fix here.

Regenerate the canonical T4 output.  Generation/static checks pass;
compilation and runtime validation of this reduction remain pending.

Co-authored-by: Copilot App <[email protected]>
Remove implementation-only unsafe modifiers from the primitive array
search, clear, copy, and synchronization overrides.  Keep the unchecked
operations in narrow explicit blocks with concrete SAFETY explanations,
including operations inside caller-unsafe members.

Retain caller obligations on raw-buffer owners and escaped-ref indexers.
Document the remaining legacy pointer declaration modifiers for removal
with the memory-safety opt-in.  Regenerate the canonical T4 output without
changing guards, buffer ownership, allocations, or release behavior.

The net11 core build passes with zero warnings and errors; canonical
regeneration and whitespace checks pass.  No new runtime pass is claimed.

Co-authored-by: Copilot App <[email protected]>
@simonrozsival
simonrozsival force-pushed the simonrozsival-primitive-array-borrowing branch from 4d2f680 to ea22377 Compare October 23, 2026 13:01
@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 Oct 27, 2026
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

✅ Android PR Reviewer completed successfully!

Generated by Android PR Reviewer for #13186

@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 findings. CI verification is incomplete.

Reviewed all three changed files in full at ea223779, including the T4 template, generated implementations, project configuration, and existing array-test callers. Findings: 0 ❌ errors, 0 ⚠️ warnings, 1 💡 suggestion, posted inline.

The pointer-backed owner retains the public IntPtr compatibility getter, checked size calculation, disposed/bounds guards, and existing JNI release behavior. The template and generated implementations consistently separate acquisition from ref-return borrowing. I treated memory-safety enforcement as deferred to #13192, rather than assuming these legacy-mode modifiers enforce caller obligations today. The inline suggestion concerns direct typed-indexer regression coverage absent from this checked-in revision.

CI: all 41 returned GitHub checks report success, including the aggregate dotnet-android check for Azure build 1646890. However, the direct Azure build query failed because authentication is required, so I could not independently confirm its result or source revision. This is a verification limitation, not evidence of a PR regression; I am not claiming fully verified green CI.

Generated by Android PR Reviewer for #13186 · copilot · gpt61 · 30.7 AIC · ⌖ 4.26 AIC · ⊞ 20K
Comment /review to run again

Bring in the latest upstream changes, including the SDK resolution timing
test fix.  Preserve the primitive-array implementation and safety
contracts unchanged.

Co-authored-by: Copilot App <[email protected]>
@simonrozsival
simonrozsival merged commit 25bb630 into main Oct 27, 2026
41 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-primitive-array-borrowing branch October 27, 2026 19:21
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