Repository navigation
[Unsafe] Guard primitive array lease indexers - #13053
Merged
Merged
Conversation
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]>
Contributor
There was a problem hiding this comment.
🟡 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
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
enabled auto-merge (squash)
October 9, 2026 22:05
Member
Author
|
@dalexsoto review |
dalexsoto
approved these changes
Oct 9, 2026
dalexsoto
left a comment
Member
There was a problem hiding this comment.
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.
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Pull Request
title and
description
should follow the
commit-messages.mdworkflow documentation, and in particular should include:ObjectDisposedExceptionfor disposed leases.make allpassed 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/TestResultsT4 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.