Skip to content

Guard primitive array value-manager routing#12114

Draft
simonrozsival wants to merge 2 commits into
mainfrom
dev/simonrozsival/fix-valuemanager-array-routing
Draft

Guard primitive array value-manager routing#12114
simonrozsival wants to merge 2 commits into
mainfrom
dev/simonrozsival/fix-valuemanager-array-routing

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Jul 15, 2026

Copy link
Copy Markdown
Member

Summary

  • require the JNI runtime type to match the selected primitive-array marshaler before invoking array JNI APIs
  • route non-array IList<T> handles through JavaConvert only when JNI confirms the object implements java.util.List
  • apply the equivalent exact JNI signature and List-assignability guards to the trimmable PrimitiveArrayInfo path
  • honor JniObjectReferenceOptions before all new invalid-cast failures

CheckJNI evidence and safety boundary

The reproducer passed a java.util.ArrayList handle while requesting IList<bool>. The reflection value manager selected JavaBooleanArray.ArrayMarshaler from the managed target type alone, leading CheckJNI to abort the process with jarray argument has non-array type: java.util.ArrayList. This change establishes the safety boundary before any array JNI call: only the matching primitive array signature (for example, [Z) may enter that marshaler. A non-array reference may enter collection conversion only when JNI IsInstanceOf confirms java.util.List; other objects and mismatched arrays fail with InvalidCastException. Failure paths dispose and invalidate transferred references according to the requested ownership options.

Retained behavior

A genuine Java primitive array still marshals to IList<T>, and genuine Java List implementations still use JavaConvert. The guard compares the actual JNI runtime type rather than rejecting IList<T> generally.

Tests

Added on-device coverage in JavaConvertTest for:

  • java.util.ArrayList<Boolean> handle -> IList<bool>
  • Java boolean[] handle -> IList<bool>
  • Java String handle -> IList<bool> safely throws, preserving Copy and consuming CopyAndDispose
  • Java int[] handle -> IList<bool> safely throws and consumes CopyAndDispose

Validation:

  • dotnet build external/Java.Interop/src/Java.Interop/Java.Interop.csproj ... passed with 0 warnings/errors after the review fixes.
  • On-device tests were not run locally because this worktree has no prepared local SDK (make prepare is required).
  • A direct Mono.Android.csproj build reaches the repository prerequisite failure because bin/BuildDebug/net10.0/xa-prep-tasks.dll is absent.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 0e043be9-9070-43bc-aea4-a48438690bdf
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 0e043be9-9070-43bc-aea4-a48438690bdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant