Fix jni aborts when turbomodule creation throws#54849
Closed
RSNara wants to merge 1 commit intofacebook:mainfrom
Closed
Fix jni aborts when turbomodule creation throws#54849RSNara wants to merge 1 commit intofacebook:mainfrom
RSNara wants to merge 1 commit intofacebook:mainfrom
Conversation
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
|
This pull request has been merged in d67fc70. |
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? |
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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:
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:
After:
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