Commit 49dbfc5
Fix jni aborts when turbomodule creation throws
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: D889105161 parent 80e384a commit 49dbfc5
File tree
1 file changed
+34
-8
lines changed- packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon
1 file changed
+34
-8
lines changedLines changed: 34 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
176 | 176 | | |
177 | 177 | | |
178 | 178 | | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
179 | 192 | | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
184 | 197 | | |
185 | 198 | | |
186 | 199 | | |
| |||
243 | 256 | | |
244 | 257 | | |
245 | 258 | | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
246 | 272 | | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
251 | 277 | | |
252 | 278 | | |
253 | 279 | | |
| |||
0 commit comments