Skip to content

[Unsafe][Java.Interop] Prepare UTF-8 remapping contracts - #13184

Merged
simonrozsival merged 4 commits into
mainfrom
simonrozsival-utf8-remapping-contracts
Oct 23, 2026
Merged

simonrozsival merged 4 commits into
mainfrom
simonrozsival-utf8-remapping-contracts

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Prepare UTF-8 remapping metadata for the updated C# memory-safety rules without breaking the APIs shipped in .NET 11 RC2. Related to #11467.

Store borrowed JNI type names, method names, and signatures as byte*, while keeping JNI handles and method IDs as opaque IntPtr values. Add typed remapping properties and the GetReplacementTypeInfoUnsafe / GetReplacementMethodInfoUnsafe hooks, and use them throughout peer-member lookup and the runtime remapping consumers. Document the NUL-termination and lifetime requirements for retained buffers, preserving managed-string alternatives, lazy caching, pointer precedence, and zero-copy UTF-8 literals.

Keep the six released native-int accessors and both legacy virtual hooks safe-callable and warning-obsolete. The typed hooks continue to dispatch through the legacy virtual slots so existing overrides and base calls still work.

Make remapping diagnostics format pointer addresses rather than dereference borrowed memory. Include coverage for diagnostic formatting, legacy compatibility, override dispatch, and borrowed metadata reads.

This PR does not enable the updated memory-safety rules; activation is handled separately in #13192.


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:24

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

Retained native-pointer lifetimes and released virtual-hook compatibility warrant final human verification despite no confirmed defects.

No open issues 🎉

What changed in this PR

Prepares Java.Interop’s borrowed UTF-8 remapping contracts for future memory-safety activation while preserving released API compatibility.

Changes:

  • Adds typed pointer properties and hooks with explicit storage-lifetime contracts.
  • Propagates typed pointers through caches, lookups, and product remapping consumers.
  • Adds compatibility, diagnostics, and lazy-caching regression tests without enabling updated compiler rules.
File Description
tests/​Mono.Android-Tests/​Java.Interop-Tests/​Java.Interop/​JniUtf8RemappingTests.cs Tests compatibility, pointer diagnostics, and caching.
src/​Mono.Android/​Microsoft.Android.Runtime/​TrimmableTypeMapTypeManager.cs Adopts typed remapping hooks.
src/​Mono.Android/​Microsoft.Android.Runtime/​JniRemappingLookup.cs Uses typed UTF-8 pointers throughout remapping.
external/​Java.Interop/​src/​Java.Interop/​PublicAPI.Unshipped.txt Records additive pointer APIs.
external/​Java.Interop/​src/​Java.Interop/​Java.Interop/​JniType.cs Types native lookup inputs and documents lifetimes.
external/​Java.Interop/​src/​Java.Interop/​Java.Interop/​JniRuntime.JniTypeManager.cs Adds typed contracts while retaining legacy dispatch.
external/​Java.Interop/​src/​Java.Interop/​Java.Interop/​JniPeerMembers.cs Separates managed and borrowed peer-name storage.
external/​Java.Interop/​src/​Java.Interop/​Java.Interop/​JniMethodInfo.cs Types borrowed Debug metadata storage.
external/​Java.Interop/​Documentation/​Architecture.md Documents ownership and compatibility boundaries.

Review effort: Balanced

Base automatically changed from simonrozsival-managed-jni-construction to main October 22, 2026 12:52
simonrozsival and others added 4 commits October 22, 2026 15:04
Retain borrowed UTF-8 names as typed pointers with explicit lifetime
contracts while preserving the released RC2 native-int accessors and
virtual dispatch slots as safe warning-obsolete compatibility APIs.

Keep the preparation buildable before global memory-safety activation.
Add focused remapping, lazy metadata, and legacy override coverage.

Co-authored-by: Copilot App <[email protected]>
Keep the UTF-8 compatibility preparation ready for memory-safety
activation by explicitly scoping native string scans and raw hook calls.
A member unsafe declaration does not provide an unsafe implementation
context under the updated rules. Preserve all existing API contracts.

Co-authored-by: Copilot App <[email protected]>
Keep managed remapping wrappers safe for callers while documenting the
bounds, lifetime, aliasing, and failure obligations of borrowed UTF-8.
Add explicit native implementation regions with concrete safety proofs.

Preserve the released RC2 safe compatibility accessors and virtual hooks,
zero-copy literals, existing lookups, allocations, and dispatch behavior.

Co-authored-by: Copilot App <[email protected]>
Keep only the borrowed-pointer diagnostics regression in the new fixture.
Archive annotation and preserved-behavior matrices outside the PR.

Narrow native regions around actual pointer operations and shorten
repeated contract documentation without changing lookup behavior,
allocations, or the released RC2 compatibility exceptions.

Co-authored-by: Copilot App <[email protected]>
@simonrozsival
simonrozsival force-pushed the simonrozsival-utf8-remapping-contracts branch from ca48554 to cceef77 Compare October 22, 2026 13:11
@simonrozsival

Copy link
Copy Markdown
Member Author

@dalexsoto review

@dalexsoto dalexsoto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typed UTF-8 metadata preserves the released native-int accessors, legacy virtual dispatch, pointer precedence and producer-owned buffer lifetimes in the inspected source paths.

The address-only diagnostic change is scoped to ReplacementMethodInfo value operations; preexisting borrowed-text diagnostic reads remain under the documented lifetime contract. The added tests cover address formatting, not targeted legacy-dispatch or lazy-cache regressions.

Source review only; no compilation, binary compatibility, runtime or device execution is claimed.

@simonrozsival
simonrozsival merged commit c5fe066 into main Oct 23, 2026
41 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-utf8-remapping-contracts branch October 23, 2026 07:43
simonrozsival added a commit that referenced this pull request Oct 31, 2026
Context: #13184, #13187, #13188, #13189.

The memory-safety audits separated caller obligations from unchecked operations inside an implementation.  Some resulting `unsafe` blocks contain only a return or assignment, however.  They add nesting and force locals to be declared separately from their initializers without making the unchecked operation any clearer.

Use `unsafe (...)` expressions for those single-value operations.  This keeps the safety proof beside the operation while removing the surrounding block.  The 122 compiled sites cover JNI lookup/invocation, pointer getters, borrowed-ref returns, value-returning callbacks, and embedded-resource reads.

  * Use expression-bodied properties/getters where the body is just that expression.  Keep disposed/bounds checks and other multi-statement bodies intact.
  * Restore the object initializers and `out var` arguments that used `var` before the audits, as confirmed with `git blame`.  Retain longstanding explicit scalar declarations.
  * Acquire resource streams directly in their `using` headers, preserving archive/stream disposal before the enclosing `PEReader` is disposed.
  * Update the canonical primitive-array, built-in marshaler, and callback T4 templates together with their generated outputs.

Caller contracts/modifiers, API metadata, evaluation order, ownership, and cleanup ordering are preserved.  This does not enable the updated memory-safety model or change project settings, dependencies, language versions, or customer-generated binding code.  Leave non-preview Mono.Android sources alone.  Pinning, void calls, loops, and multi-statement operations retain blocks; so does `PEMemoryBlock.Pointer`, whose legacy pointer-signature getter still rejects the expression form with CS9363.

### Validation

Built from pinned `main` source `a78439a3a4817cd8c56c886f4312ec2feda55513` with SDK `12.0.100-alpha.1.26523.103` / Csc `5.13.0-1.26523.103`.  Results are attributed to the revision tested, rather than treating an earlier full build as validation of later edits:

| Revision | Coverage | Result |
| --- | --- | --- |
| `9bff508212` | Complete SDK/local workload; Java.Interop/net10.0, legacy tasks/netstandard2.0, modern tasks/net11.0; generator and host task cases | Passed; 482 host tests passed |
| `530a60ee1b` | Property/`var` follow-up: core and legacy-task Debug/Release builds; generator suite | All four builds passed; 478/478 tests passed |
| `7d29a95c5a` | Using-header follow-up: legacy-task Debug/Release builds; CoreCLR and NativeAOT resource-import host cases | Both builds passed; 2/2 tests passed |

Type/member metadata and public XML documentation are unchanged.  At `530a60ee1b`, Release IL matched the baseline; Debug IL was equivalent after conservatively normalizing verified single-use getter return spills and type-preserving local numbering.  At `7d29a95c5a`, only `ResolveLibraryProjectImports.Extract` changes IL in either configuration: three redundant stream-capture load/store pairs disappear, with the member-call sequence and exception-handler kinds/order preserved.  These compiler-lowering differences are intentional; the final assemblies are **not claimed to be byte-identical**.

Canonical T4 regeneration is byte-stable, and original safety comments and BOMs are preserved.  The full SDK, modern/net11 build, and compression-metadata cases were not rerun after `9bff508212`; later results are limited to the focused coverage above.  No device tests were run.  This PR targets `main` independently of the memory-safety migration stack.
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 22, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants