Skip to content

[Java.Interop] Convert dot-separated class names before calling FindClass#1409

Merged
jonathanpeppers merged 1 commit intomainfrom
fix-checkjni-findclass-abort
Apr 20, 2026
Merged

[Java.Interop] Convert dot-separated class names before calling FindClass#1409
jonathanpeppers merged 1 commit intomainfrom
fix-checkjni-findclass-abort

Conversation

@simonrozsival
Copy link
Copy Markdown
Member

On Android userdebug emulators with CheckJNI enabled, ART validates class name format in FindClass at the native level. Passing a dot-separated name (e.g. "java.lang.Object") causes SIGABRT before the managed Class.forName() fallback can execute.

This was introduced in #1407 — the fallback logic works correctly, but the raw FindClass JNI call fires first with the invalid name format, and CheckJNI kills the process before the exception-based fallback path runs.

Fix

Convert . to / in the class name before calling raw FindClass, so CheckJNI always sees valid JNI-format names (java/lang/Object). The Class.forName() fallback remains unchanged and still handles classes not findable by FindClass (e.g. custom class loaders).

Changes

  • String overload (TryFindClass(string, bool)): classname.Replace('.', '/') before TryRawFindClass
  • UTF-8 overload (TryFindClass(ReadOnlySpan<byte>, bool)): stackalloc copy with ./ conversion before JniNativeMethods.FindClass

Fixes: dotnet/android#11157 (comment)

Copilot AI review requested due to automatic review settings April 20, 2026 14:22
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates JniEnvironment.Types.FindClass to pre-normalize dot-separated Java class names into JNI slash form before invoking raw FindClass, preventing ART CheckJNI from aborting the process before the managed fallback path can run.

Changes:

  • Normalize java.lang.Foojava/lang/Foo before the raw FindClass call in the string overload.
  • Normalize UTF-8 class name spans (ReadOnlySpan<byte>) by converting ./ into a null-terminated buffer before calling JniNativeMethods.FindClass.

Comment thread src/Java.Interop/Java.Interop/JniEnvironment.Types.cs
Comment thread src/Java.Interop/Java.Interop/JniEnvironment.Types.cs Outdated
@simonrozsival simonrozsival force-pushed the fix-checkjni-findclass-abort branch 2 times, most recently from 3396167 to 5c04fb2 Compare April 20, 2026 14:36
…lass

On Android userdebug emulators with CheckJNI enabled, ART validates
class name format in FindClass at the native level. Passing a
dot-separated name (e.g. "java.lang.Object") causes SIGABRT before
the managed Class.forName() fallback can execute.

Fix by converting '.' to '/' in the class name before calling raw
FindClass, so CheckJNI always sees valid JNI-format names. The
Class.forName() fallback still handles classes not findable by
FindClass (e.g. custom class loaders).

Fixes: dotnet/android#11157 (comment)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@simonrozsival simonrozsival force-pushed the fix-checkjni-findclass-abort branch from 5c04fb2 to 56045a8 Compare April 20, 2026 14:41
@simonrozsival simonrozsival added the copilot `copilot-cli` or other AIs were used to author this label Apr 20, 2026
@jonathanpeppers jonathanpeppers merged commit 644d549 into main Apr 20, 2026
2 checks passed
@jonathanpeppers jonathanpeppers deleted the fix-checkjni-findclass-abort branch April 20, 2026 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

copilot `copilot-cli` or other AIs were used to author this

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants