mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Migrate BlobModule to TurboModuleWithJSIBindings for safe JSI access (#56272)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56272 BlobCollector::nativeInstall received a raw jlong pointer to jsi::Runtime from Java and cast it directly, with no thread safety. Migrated BlobModule to implement TurboModuleWithJSIBindings, which provides a safe getBindingsInstaller() callback guaranteed to run on the JS thread during module initialization. Removed the unsafe raw pointer path. Changelog: [Internal] Reviewed By: alanleedev Differential Revision: D98712509 fbshipit-source-id: 2220b854374090e2e2b9e4b3a8113c1c890a81e4
This commit is contained in:
1 parent
ca4ee56468
commit
b20d7fccc8
9 files changed
+75
-146
No files matched your search
@@ -2447,16 +2447,16 @@ public abstract interface class com/facebook/react/modules/appregistry/AppRegist
|
||||
public abstract fun unmountApplicationComponentAtRootTag (I)V
|
||||
}
|
||||
|
||||
public final class com/facebook/react/modules/blob/BlobModule : com/facebook/fbreact/specs/NativeBlobModuleSpec {
|
||||
public final class com/facebook/react/modules/blob/BlobModule : com/facebook/fbreact/specs/NativeBlobModuleSpec, com/facebook/react/turbomodule/core/interfaces/TurboModuleWithJSIBindings {
|
||||
public static final field Companion Lcom/facebook/react/modules/blob/BlobModule$Companion;
|
||||
public static final field NAME Ljava/lang/String;
|
||||
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;)V
|
||||
public fun addNetworkingHandler ()V
|
||||
public fun addWebSocketHandler (D)V
|
||||
public fun createFromParts (Lcom/facebook/react/bridge/ReadableArray;Ljava/lang/String;)V
|
||||
public fun getBindingsInstaller ()Lcom/facebook/react/turbomodule/core/interfaces/BindingsInstallerHolder;
|
||||
public final fun getLengthOfBlob (Ljava/lang/String;)J
|
||||
public fun getTypedExportedConstants ()Ljava/util/Map;
|
||||
public fun initialize ()V
|
||||
public fun release (Ljava/lang/String;)V
|
||||
public final fun remove (Ljava/lang/String;)V
|
||||
public fun removeWebSocketHandler (D)V
|
||||
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.modules.blob
|
||||
|
||||
import com.facebook.react.bridge.JavaScriptContextHolder
|
||||
import com.facebook.react.bridge.ReactContext
|
||||
import com.facebook.soloader.SoLoader
|
||||
|
||||
internal object BlobCollector {
|
||||
init {
|
||||
SoLoader.loadLibrary("reactnativeblob")
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun install(reactContext: ReactContext, blobModule: BlobModule) {
|
||||
reactContext.runOnJSQueueThread {
|
||||
val jsContext: JavaScriptContextHolder? = reactContext.getJavaScriptContextHolder()
|
||||
// When debugging in chrome the JS context is not available.
|
||||
if (jsContext != null && jsContext.get() != 0L) {
|
||||
nativeInstall(blobModule, jsContext.get())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private external fun nativeInstall(blobModule: Any, jsContext: Long)
|
||||
}
|
||||
+10
-5
@@ -23,6 +23,9 @@ import com.facebook.react.bridge.buildReadableMap
|
||||
import com.facebook.react.module.annotations.ReactModule
|
||||
import com.facebook.react.modules.network.NetworkingModule
|
||||
import com.facebook.react.modules.websocket.WebSocketModule
|
||||
import com.facebook.react.turbomodule.core.interfaces.BindingsInstallerHolder
|
||||
import com.facebook.react.turbomodule.core.interfaces.TurboModuleWithJSIBindings
|
||||
import com.facebook.soloader.SoLoader
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
@@ -39,7 +42,7 @@ import okio.ByteString
|
||||
|
||||
@ReactModule(name = NativeBlobModuleSpec.NAME)
|
||||
public class BlobModule(reactContext: ReactApplicationContext) :
|
||||
NativeBlobModuleSpec(reactContext) {
|
||||
NativeBlobModuleSpec(reactContext), TurboModuleWithJSIBindings {
|
||||
|
||||
private val blobs = HashMap<String, ByteArray>()
|
||||
|
||||
@@ -128,10 +131,6 @@ public class BlobModule(reactContext: ReactApplicationContext) :
|
||||
}
|
||||
}
|
||||
|
||||
public override fun initialize() {
|
||||
BlobCollector.install(reactApplicationContext, this)
|
||||
}
|
||||
|
||||
public override fun getTypedExportedConstants(): Map<String, Any> {
|
||||
val resources = reactApplicationContext.resources
|
||||
val packageName = reactApplicationContext.packageName
|
||||
@@ -331,7 +330,13 @@ public class BlobModule(reactContext: ReactApplicationContext) :
|
||||
remove(blobId)
|
||||
}
|
||||
|
||||
@DoNotStrip external override fun getBindingsInstaller(): BindingsInstallerHolder
|
||||
|
||||
public companion object {
|
||||
init {
|
||||
SoLoader.loadLibrary("reactnativeblob")
|
||||
}
|
||||
|
||||
public const val NAME: String = NativeBlobModuleSpec.NAME
|
||||
}
|
||||
}
|
||||
+37
-30
@@ -40,38 +40,45 @@ size_t BlobCollector::getBlobLength() {
|
||||
return static_cast<size_t>(length);
|
||||
}
|
||||
|
||||
void BlobCollector::nativeInstall(
|
||||
jni::alias_ref<jclass> /*unused*/,
|
||||
jni::alias_ref<jobject> blobModule,
|
||||
jlong jsContextNativePointer) {
|
||||
auto& runtime = *((jsi::Runtime*)jsContextNativePointer);
|
||||
auto blobModuleRef = jni::make_global(blobModule);
|
||||
runtime.global().setProperty(
|
||||
runtime,
|
||||
"__blobCollectorProvider",
|
||||
jsi::Function::createFromHostFunction(
|
||||
runtime,
|
||||
jsi::PropNameID::forAscii(runtime, "__blobCollectorProvider"),
|
||||
1,
|
||||
[blobModuleRef](
|
||||
jsi::Runtime& rt,
|
||||
const jsi::Value& /*thisVal*/,
|
||||
const jsi::Value* args,
|
||||
size_t /*count*/) {
|
||||
auto blobId = args[0].asString(rt).utf8(rt);
|
||||
auto blobCollector =
|
||||
std::make_shared<BlobCollector>(blobModuleRef, blobId);
|
||||
auto blobCollectorJsObject =
|
||||
jsi::Object::createFromHostObject(rt, blobCollector);
|
||||
blobCollectorJsObject.setExternalMemoryPressure(
|
||||
rt, blobCollector->getBlobLength());
|
||||
return blobCollectorJsObject;
|
||||
}));
|
||||
// static
|
||||
void BlobModuleJSIBindings::registerNatives() {
|
||||
javaClassLocal()->registerNatives({
|
||||
makeNativeMethod(
|
||||
"getBindingsInstaller", BlobModuleJSIBindings::getBindingsInstaller),
|
||||
});
|
||||
}
|
||||
|
||||
void BlobCollector::registerNatives() {
|
||||
registerHybrid(
|
||||
{makeNativeMethod("nativeInstall", BlobCollector::nativeInstall)});
|
||||
// static
|
||||
jni::local_ref<BindingsInstallerHolder::javaobject>
|
||||
BlobModuleJSIBindings::getBindingsInstaller(
|
||||
jni::alias_ref<BlobModuleJSIBindings> jobj) {
|
||||
auto blobModuleRef = jni::make_global(jobj);
|
||||
return BindingsInstallerHolder::newObjectCxxArgs(
|
||||
[blobModuleRef = std::move(blobModuleRef)](
|
||||
jsi::Runtime& runtime, const std::shared_ptr<CallInvoker>&) {
|
||||
runtime.global().setProperty(
|
||||
runtime,
|
||||
"__blobCollectorProvider",
|
||||
jsi::Function::createFromHostFunction(
|
||||
runtime,
|
||||
jsi::PropNameID::forAscii(runtime, "__blobCollectorProvider"),
|
||||
1,
|
||||
[blobModuleRef](
|
||||
jsi::Runtime& rt,
|
||||
const jsi::Value& /*thisVal*/,
|
||||
const jsi::Value* args,
|
||||
size_t /*count*/) {
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
|
||||
auto blobId = args[0].asString(rt).utf8(rt);
|
||||
auto blobCollector =
|
||||
std::make_shared<BlobCollector>(blobModuleRef, blobId);
|
||||
auto blobCollectorJsObject =
|
||||
jsi::Object::createFromHostObject(rt, blobCollector);
|
||||
blobCollectorJsObject.setExternalMemoryPressure(
|
||||
rt, blobCollector->getBlobLength());
|
||||
return blobCollectorJsObject;
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace facebook::react
|
||||
+13
-10
@@ -7,30 +7,33 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ReactCommon/BindingsInstallerHolder.h>
|
||||
#include <fbjni/fbjni.h>
|
||||
#include <jsi/jsi.h>
|
||||
|
||||
namespace facebook::react {
|
||||
|
||||
class BlobCollector : public jni::HybridClass<BlobCollector>, public jsi::HostObject {
|
||||
class BlobCollector : public jsi::HostObject {
|
||||
public:
|
||||
BlobCollector(jni::global_ref<jobject> blobModule, std::string blobId);
|
||||
~BlobCollector();
|
||||
|
||||
size_t getBlobLength();
|
||||
|
||||
static constexpr auto kJavaDescriptor = "Lcom/facebook/react/modules/blob/BlobCollector;";
|
||||
|
||||
static void
|
||||
nativeInstall(jni::alias_ref<jclass> /*unused*/, jni::alias_ref<jobject> blobModule, jlong jsContextNativePointer);
|
||||
|
||||
static void registerNatives();
|
||||
|
||||
private:
|
||||
friend HybridBase;
|
||||
|
||||
jni::global_ref<jobject> blobModule_;
|
||||
const std::string blobId_;
|
||||
};
|
||||
|
||||
class BlobModuleJSIBindings : public jni::JavaClass<BlobModuleJSIBindings> {
|
||||
public:
|
||||
static constexpr const char *kJavaDescriptor = "Lcom/facebook/react/modules/blob/BlobModule;";
|
||||
|
||||
static void registerNatives();
|
||||
|
||||
private:
|
||||
static jni::local_ref<BindingsInstallerHolder::javaobject> getBindingsInstaller(
|
||||
jni::alias_ref<BlobModuleJSIBindings> jobj);
|
||||
};
|
||||
|
||||
} // namespace facebook::react
|
||||
+2
-1
@@ -21,7 +21,8 @@ target_link_libraries(reactnativeblob
|
||||
fbjni
|
||||
folly_runtime
|
||||
jsi
|
||||
reactnativejni)
|
||||
reactnativejni
|
||||
turbomodulejsijni)
|
||||
|
||||
target_compile_reactnative_options(reactnativeblob PRIVATE)
|
||||
target_compile_options(reactnativeblob PRIVATE -fvisibility=hidden)
|
||||
@@ -11,5 +11,5 @@
|
||||
|
||||
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* /*unused*/) {
|
||||
return facebook::jni::initialize(
|
||||
vm, [] { facebook::react::BlobCollector::registerNatives(); });
|
||||
vm, [] { facebook::react::BlobModuleJSIBindings::registerNatives(); });
|
||||
}
|
||||
-66
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.modules.blob
|
||||
|
||||
import com.facebook.react.bridge.JavaScriptContextHolder
|
||||
import com.facebook.react.bridge.ReactContext
|
||||
import com.facebook.soloader.SoLoader
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mockito.MockedStatic
|
||||
import org.mockito.Mockito.mockStatic
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.verify
|
||||
import org.mockito.kotlin.whenever
|
||||
|
||||
class BlobCollectorTest {
|
||||
private lateinit var reactContext: ReactContext
|
||||
private lateinit var mockedStaticSoLoader: MockedStatic<SoLoader>
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
reactContext = mock()
|
||||
|
||||
mockedStaticSoLoader = mockStatic(SoLoader::class.java)
|
||||
mockedStaticSoLoader
|
||||
.`when`<Boolean> { SoLoader.loadLibrary("reactnativeblob") }
|
||||
.thenReturn(true)
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
mockedStaticSoLoader.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInstallWithValidJsContext() {
|
||||
val jsContextHolder = mock<JavaScriptContextHolder>()
|
||||
val jsContext = 1234L
|
||||
|
||||
whenever(reactContext.getJavaScriptContextHolder()).thenReturn(jsContextHolder)
|
||||
whenever(jsContextHolder.get()).thenReturn(jsContext)
|
||||
|
||||
BlobCollector.install(reactContext, mock())
|
||||
|
||||
verify(reactContext).runOnJSQueueThread(any())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInstallWithInvalidOrNullJsContext() {
|
||||
val jsContextHolder = mock<JavaScriptContextHolder>()
|
||||
|
||||
whenever(reactContext.getJavaScriptContextHolder()).thenReturn(jsContextHolder)
|
||||
whenever(jsContextHolder.get()).thenReturn(0L)
|
||||
|
||||
BlobCollector.install(reactContext, mock())
|
||||
|
||||
verify(reactContext).runOnJSQueueThread(any())
|
||||
}
|
||||
}
|
||||
+10
@@ -12,6 +12,7 @@ import com.facebook.react.bridge.JavaOnlyArray
|
||||
import com.facebook.react.bridge.JavaOnlyMap
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.bridge.ReactTestHelper
|
||||
import com.facebook.soloader.SoLoader
|
||||
import com.facebook.testutils.shadows.ShadowArguments
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.nio.ByteBuffer
|
||||
@@ -22,6 +23,8 @@ import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.MockedStatic
|
||||
import org.mockito.Mockito.mockStatic
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
@@ -33,9 +36,15 @@ class BlobModuleTest {
|
||||
private lateinit var blobId: String
|
||||
private lateinit var context: ReactApplicationContext
|
||||
private lateinit var blobModule: BlobModule
|
||||
private lateinit var mockedStaticSoLoader: MockedStatic<SoLoader>
|
||||
|
||||
@Before
|
||||
fun prepareModules() {
|
||||
mockedStaticSoLoader = mockStatic(SoLoader::class.java)
|
||||
mockedStaticSoLoader
|
||||
.`when`<Boolean> { SoLoader.loadLibrary("reactnativeblob") }
|
||||
.thenReturn(true)
|
||||
|
||||
bytes = ByteArray(120)
|
||||
Random.Default.nextBytes(bytes)
|
||||
|
||||
@@ -47,6 +56,7 @@ class BlobModuleTest {
|
||||
@After
|
||||
fun cleanUp() {
|
||||
blobModule.remove(blobId)
|
||||
mockedStaticSoLoader.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user