Skip to content

Fix jni aborts when turbomodule creation throws#54849

Closed
RSNara wants to merge 1 commit intofacebook:mainfrom
RSNara:export-D88910516
Closed

Fix jni aborts when turbomodule creation throws#54849
RSNara wants to merge 1 commit intofacebook:mainfrom
RSNara:export-D88910516

Conversation

@RSNara
Copy link
Copy Markdown
Contributor

@RSNara RSNara commented Dec 11, 2025

Summary:

Summary

This commit fixes a crash (JNI abort) that occurs when a Java TurboModule's creation throws an exception. The fix applies to two methods: getTurboJavaModule and getLegacyJavaModule.

Problem

When calling Java methods via fbjni with std::string arguments, fbjni creates temporary local_ref objects for the converted arguments.

https://www.internalfb.com/code/fbsource/[74f313eee086]/fbandroid/libraries/fbjni/cxx/fbjni/detail/Meta-inl.h?lines=78-89

If the Java method throws an exception:

  1. The JNI call returns with a pending exception flag set
  2. C++ destroys the temporary jstring arguments (at end of full-expression)
  3. The destructor calls GetObjectRefType() while there's a pending exception
  4. This violates JNI rules and causes ART's CheckJNI to abort the process

The Fix

Pre-convert string arguments to jstring before the method call, controlling the lifetime of the local_ref so it extends past the exception check.

Before:

static auto getTurboJavaModule =
    javaPart->getClass()
        ->getMethod<jni::alias_ref<JTurboModule>(const std::string&)>(
            "getTurboJavaModule");
auto moduleInstance = getTurboJavaModule(javaPart.get(), name);

After:

static auto getTurboJavaModule =
    javaPart->getClass()->getMethod<jni::alias_ref<JTurboModule>(jstring)>(
        "getTurboJavaModule");
auto jname = jni::make_jstring(name);
auto moduleInstance = getTurboJavaModule(javaPart.get(), jname.get());

The same change was applied to getLegacyJavaModule.

Long-term solution

This should be a system fix applied to fbjni. That is done in the subsequent diff.

Changelog: [Android][Fixed] - Fix jni aborts when turbomodule constructors throw

Differential Revision: D88910516

Summary:
## Summary
This commit fixes a crash (JNI abort) that occurs when a Java TurboModule's creation throws an exception. The fix applies to two methods: getTurboJavaModule and getLegacyJavaModule.

## Problem
When calling Java methods via fbjni with std::string arguments, fbjni creates temporary local_ref<JString> objects for the converted arguments.

https://www.internalfb.com/code/fbsource/[74f313eee086]/fbandroid/libraries/fbjni/cxx/fbjni/detail/Meta-inl.h?lines=78-89


If the Java method throws an exception:

1. The JNI call returns with a pending exception flag set
2. C++ destroys the temporary jstring arguments (at end of full-expression)
3. The destructor calls GetObjectRefType() while there's a pending exception
4. This violates JNI rules and causes ART's CheckJNI to abort the process

## The Fix
Pre-convert string arguments to jstring before the method call, controlling the lifetime of the local_ref<JString> so it extends past the exception check.

Before:

```
static auto getTurboJavaModule =
    javaPart->getClass()
        ->getMethod<jni::alias_ref<JTurboModule>(const std::string&)>(
            "getTurboJavaModule");
auto moduleInstance = getTurboJavaModule(javaPart.get(), name);
```

After:
```
static auto getTurboJavaModule =
    javaPart->getClass()->getMethod<jni::alias_ref<JTurboModule>(jstring)>(
        "getTurboJavaModule");
auto jname = jni::make_jstring(name);
auto moduleInstance = getTurboJavaModule(javaPart.get(), jname.get());
```

The same change was applied to getLegacyJavaModule.

## Long-term solution
This should be a system fix applied to fbjni. That is done in the subsequent diff.

Changelog: [Android][Fixed] - Fix jni aborts when turbomodule constructors throw

Differential Revision: D88910516
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 11, 2025
@meta-codesync
Copy link
Copy Markdown

meta-codesync bot commented Dec 11, 2025

@RSNara has exported this pull request. If you are a Meta employee, you can view the originating Diff in D88910516.

@meta-codesync
Copy link
Copy Markdown

meta-codesync bot commented Dec 11, 2025

This pull request has been merged in d67fc70.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Dec 11, 2025
@react-native-bot
Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @RSNara in d67fc70

When will my fix make it into a release? | How to file a pick request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported Merged This PR has been merged. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants