Skip to content

[Unsafe] Guard primitive array lease indexers - #13053

Merged
simonrozsival merged 6 commits into
mainfrom
simonrozsival-jni-primitive-array-bounds
Oct 10, 2026
Merged

simonrozsival merged 6 commits into
mainfrom
simonrozsival-jni-primitive-array-bounds

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

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. Primitive-array lease indexers return refs into JNI memory; invalid indices can read or write outside the array. Validate against the element count derived from the lease's byte size, while preserving ObjectDisposedException for disposed leases.
  • Links to issues fixed: none provided.
  • Unit tests. Shared contract coverage exercises valid access, negative, end and beyond-end indices, empty arrays, and disposed behavior across all eight primitive array types. make all passed after ./dotnet-local.sh build Microsoft.Android.slnx -t:BuildExtraApiLevels (0 errors). The focused runtime build/install passed, then the emulator run passed 16 tests with 0 failures:
    cd tests/Mono.Android-Tests/Mono.Android-Tests && ANDROID_SERIAL=emulator-5554 ../../../dotnet-local.sh test Mono.Android.NET-Tests.csproj --no-build -c Debug --device emulator-5554 -p:Device=emulator-5554 -p:IncludeCategories=JniPrimitiveArrayBounds --report-trx --results-directory ../../../bin/TestDebug/TestResults

T4 note: all eight generated indexer blocks match the template output exactly. The full template output also emits pre-existing, unrelated trimming annotations absent from the checked-in generated file; those were left out to keep this PR scoped to bounds checks.

JNI array leases expose raw native memory.  A bad index can access memory
outside the array.  Validate against element count derived from the byte
size and preserve ObjectDisposedException on disposed leases.

Extend the shared contract tests to cover valid, negative, end and beyond
end indices, empty arrays, and disposed behavior for all eight types.

Co-authored-by: Copilot App <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 9, 2026 14:47

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.

🟡 Changes recommended

The element-count calculation overflows for valid JNI leases larger than 2 GiB.

1 open finding
What changed in this PR

Adds bounds checking to JNI primitive-array lease indexers while preserving disposed-lease behavior.

Changes:

  • Validates indices against lease element counts across eight primitive types.
  • Adds shared contract tests for valid, invalid, empty, and disposed access.
  • Updates both the T4 template and generated source.
File Description
JavaPrimitiveArrays.tt Generates index bounds checks.
JavaPrimitiveArrays.cs Applies checks to all primitive leases.
JavaPrimitiveArrayContract.cs Adds shared bounds tests.
JavaBooleanArrayContractTests.cs Enables Boolean coverage.
JavaSByteArrayContractTests.cs Enables SByte coverage.
JavaCharArrayContractTests.cs Enables Char coverage.
JavaInt16ArrayContractTests.cs Enables Int16 coverage.
JavaInt32ArrayContractTests.cs Enables Int32 coverage.
JavaInt64ArrayContractTests.cs Enables Int64 coverage.
JavaSingleArrayContractTests.cs Enables Single coverage.
JavaDoubleArrayContractTests.cs Enables Double coverage.

🧠 Review effort: Balanced

@simonrozsival simonrozsival changed the title [Java.Interop] Guard primitive array lease indexers [Unsafe] Guard primitive array lease indexers Oct 9, 2026
simonrozsival and others added 5 commits October 9, 2026 23:12
JNI lease byte sizes are stored as int and can overflow for valid
arrays larger than 2 GiB.  Keep the element count separately so index
validation remains correct independently of the byte-size representation.

Add a large Int64 lease regression check without dereferencing its
synthetic pointer.

Co-authored-by: Copilot App <[email protected]>
Drop the generic accessor overrides and helper-only bounds assertions
rather than adding test plumbing solely to reach each concrete ref indexer.

Co-authored-by: Copilot App <[email protected]>
Exercise valid and invalid indices through the shared Java primitive
array indexer contract instead of adding lease accessors for tests.

Co-authored-by: Copilot App <[email protected]>
Restore the original fixture formatting now that the test-only lease
accessors have been removed.  Keep the bounds coverage on the public API.

Co-authored-by: Copilot App <[email protected]>
Store the primitive element count and width in JniArrayElements, then
compute the public byte Size only when requested.  This keeps bounds
checks based on element count and reports byte-size overflow explicitly.

Co-authored-by: Copilot App <[email protected]>
@simonrozsival
simonrozsival enabled auto-merge (squash) October 9, 2026 22:05
@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.

All eight primitive lease indexers validate the separately stored element count before native ref access, preserving disposed-exception priority and valid ref behavior without byte-size overflow affecting bounds. The changed template and generated blocks agree; no blocking issue remains.

@simonrozsival
simonrozsival merged commit a7f55f0 into main Oct 10, 2026
42 checks passed
@simonrozsival
simonrozsival deleted the simonrozsival-jni-primitive-array-bounds branch October 10, 2026 00:10
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 9, 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