Enforce the ArrayBuffer borrow contract for Java TurboModules (#57982)

Summary:
Pull Request resolved: https://github.com/react/react-native/pull/57982

Changelog: [ANDROID][FIXED]  Enforce the ArrayBuffer borrow contract for Java TurboModules

Follow-up hardening for the Android `ArrayBuffer` TurboModule type. Three problems:

**1. Borrowed JS-heap bytes outlived the call that lent them.** For a synchronous
method, `convertJSIArgsToJNIArgs` hands the module a `ByteBuffer` aliasing the JS
`ArrayBuffer`'s bytes without copying. Nothing stopped a module from stashing that
`ArrayBuffer` in a field and reading it later, after the JS heap may have moved,
freed, or reused the memory — a use-after-free that reads as intermittent data
corruption rather than a crash.

The borrow is now explicitly scoped to the call frame. `JNIArgs` records every
borrowed `ArrayBuffer` and revokes it in its destructor — including when the call
throws — via the new `JArrayBuffer::invalidate`, which drops the C++ side's
reference to the bytes. `ArrayBuffer.bytes` and `ArrayBuffer.size` then throw,
with a message pointing at `ArrayBuffer.arrayBufferWithCopiedBytes`, and
`JArrayBuffer::toJSBuffer` throws rather than aliasing revoked memory. Modules
that need the bytes past the call copy them; modules that don't keep the
zero-copy fast path.

Revocation lives entirely on the C++ side: the peer is the single source of
truth, and Kotlin asks it through `isBytesValid`. The destructor runs while the
stack unwinds, possibly with a Java exception pending, so it resolves each peer
pointer at borrow time — the `global_ref` alongside it keeps the Java object, and
therefore the peer, alive — and calls only the `noexcept`
`JArrayBuffer::invalidate`. No JNI calls are made from the destructor, which is
what lets it stay `noexcept` honestly.

**2. Argument conversion aborted under runtimes that refuse `tryGetMutableBuffer`.**
`jsi::Runtime::tryGetMutableBuffer` and `detached` are not universally
implemented: tracing and replay runtimes throw from `tryGetMutableBuffer`, and
`detached` throws a `JSINativeException` if the JS-side property isn't a bool.
`ArrayBuffer` argument conversion is not wrapped in a try/catch, so either throw
propagated out of a JNI frame. Both calls now go through exception-tolerant
helpers in `react/bridging/ArrayBuffer.h`; a runtime that refuses to answer is
treated as "no native buffer available", which selects the copy path. Routing
`AsyncArrayBuffer::acquire` and `::borrow` through the same helper fixes the
identical latent bug on the shared C++/ObjC path.

**3. A wrong return type from a module crashed instead of raising a JS error.**
The `ArrayBufferKind` return path cast the returned `jobject` to `JArrayBuffer`
unconditionally. A module returning any other object type produced undefined
behavior. The cast is now guarded by an `isInstanceOf` check that throws a
`jsi::JSError` naming the offending module and method.

Also in this change:
- `JByteBufferMutableBuffer::data()` reports null for a zero-capacity direct
  buffer instead of calling `getDirectBytes()`, which throws for one. That made
  `createArrayBuffer` throw for an empty `ArrayBuffer`.
- Dropped two dead zero-size branches in `JArrayBuffer`: `JByteBuffer::wrapBytes`
  already routes `size == 0` to an empty buffer.
- `JArrayBuffer.cpp` reuses the shared `detail::OwnedBytesBuffer` from
  `react/bridging/ArrayBuffer.h` instead of a second local copy.
- `ArrayBuffer.kt` KDoc corrected: the returned JS `ArrayBuffer` is a new object
  over the same bytes rather than the identical one, `size` is the capacity and
  not a view's remaining bytes, and `arrayBufferWithOwnedBytes` documents the
  caller's lifetime obligation.
- `ArrayBuffer.kt` moves from the `bridge` target to `native-types`, alongside the
  other JNI-backed bridge types.

Changelog:
[Android][Breaking] - TurboModule methods taking or returning an `ArrayBuffer`
now use `com.facebook.react.bridge.ArrayBuffer` instead of
`java.nio.ByteBuffer`, and an `ArrayBuffer` argument must not be retained past
the method that receives it unless its bytes are copied with
`ArrayBuffer.arrayBufferWithCopiedBytes()`.

Reviewed By: javache

Differential Revision: D115794808

fbshipit-source-id: 26f5d863469cc14a3f1bffc2cbc3302f3e983ecb
This commit is contained in:
Christoph Purrer authored and meta-codesync[bot] committed 2026-08-17 17:57:28 -07:00
1 parent 5bb9639594
commit d84c13d511
20 files changed
+629 -68

No files matched your search

@@ -1652,6 +1652,7 @@ class facebook::react::AsyncArrayBuffer {
public static facebook::react::AsyncArrayBuffer copy(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer);
public static facebook::react::AsyncArrayBuffer wrap(std::shared_ptr<facebook::jsi::MutableBuffer> buffer) noexcept;
public static facebook::react::AsyncArrayBuffer wrap(std::vector<uint8_t> bytes);
public static void throwIfDetached(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer, const char* callerName);
public std::shared_ptr<facebook::jsi::MutableBuffer> getMutableBuffer() const noexcept;
public uint8_t* data() noexcept;
public ~AsyncArrayBuffer() = default;
@@ -2794,12 +2795,16 @@ class facebook::react::IntersectionObserverState {
class facebook::react::JArrayBuffer : public jni::HybridClass<facebook::react::JArrayBuffer> {
public JArrayBuffer(std::shared_ptr<facebook::jsi::MutableBuffer> buffer, bool owningBytes) noexcept;
public bool hasBytes() const noexcept;
public bool isOwningBytes() const noexcept;
public const std::shared_ptr<facebook::jsi::MutableBuffer>& mutableBuffer() const;
public static constexpr auto kJavaDescriptor;
public static jni::local_ref<javaobject> createOwned(const void* bytes, size_t size);
public static jni::local_ref<javaobject> createOwning(std::shared_ptr<facebook::jsi::MutableBuffer> buffer);
public static jni::local_ref<javaobject> createUnowned(void* bytes, size_t size);
public static std::shared_ptr<facebook::jsi::MutableBuffer> toJSBuffer(jni::alias_ref<javaobject> arrayBuffer);
public static std::shared_ptr<facebook::jsi::MutableBuffer> toJSBuffer(facebook::jsi::Runtime& runtime, jni::alias_ref<javaobject> arrayBuffer);
public static void registerNatives();
public void invalidate() noexcept;
}
class facebook::react::JBindingsInstaller : public jni::HybridClass<facebook::react::JBindingsInstaller>, public facebook::react::BindingsInstaller {
@@ -1646,6 +1646,7 @@ class facebook::react::AsyncArrayBuffer {
public static facebook::react::AsyncArrayBuffer copy(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer);
public static facebook::react::AsyncArrayBuffer wrap(std::shared_ptr<facebook::jsi::MutableBuffer> buffer) noexcept;
public static facebook::react::AsyncArrayBuffer wrap(std::vector<uint8_t> bytes);
public static void throwIfDetached(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer, const char* callerName);
public std::shared_ptr<facebook::jsi::MutableBuffer> getMutableBuffer() const noexcept;
public uint8_t* data() noexcept;
public ~AsyncArrayBuffer() = default;
@@ -2752,12 +2753,16 @@ class facebook::react::IntersectionObserverState {
class facebook::react::JArrayBuffer : public jni::HybridClass<facebook::react::JArrayBuffer> {
public JArrayBuffer(std::shared_ptr<facebook::jsi::MutableBuffer> buffer, bool owningBytes) noexcept;
public bool hasBytes() const noexcept;
public bool isOwningBytes() const noexcept;
public const std::shared_ptr<facebook::jsi::MutableBuffer>& mutableBuffer() const;
public static constexpr auto kJavaDescriptor;
public static jni::local_ref<javaobject> createOwned(const void* bytes, size_t size);
public static jni::local_ref<javaobject> createOwning(std::shared_ptr<facebook::jsi::MutableBuffer> buffer);
public static jni::local_ref<javaobject> createUnowned(void* bytes, size_t size);
public static std::shared_ptr<facebook::jsi::MutableBuffer> toJSBuffer(jni::alias_ref<javaobject> arrayBuffer);
public static std::shared_ptr<facebook::jsi::MutableBuffer> toJSBuffer(facebook::jsi::Runtime& runtime, jni::alias_ref<javaobject> arrayBuffer);
public static void registerNatives();
public void invalidate() noexcept;
}
class facebook::react::JBindingsInstaller : public jni::HybridClass<facebook::react::JBindingsInstaller>, public facebook::react::BindingsInstaller {
@@ -1650,6 +1650,7 @@ class facebook::react::AsyncArrayBuffer {
public static facebook::react::AsyncArrayBuffer copy(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer);
public static facebook::react::AsyncArrayBuffer wrap(std::shared_ptr<facebook::jsi::MutableBuffer> buffer) noexcept;
public static facebook::react::AsyncArrayBuffer wrap(std::vector<uint8_t> bytes);
public static void throwIfDetached(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer, const char* callerName);
public std::shared_ptr<facebook::jsi::MutableBuffer> getMutableBuffer() const noexcept;
public uint8_t* data() noexcept;
public ~AsyncArrayBuffer() = default;
@@ -2791,12 +2792,16 @@ class facebook::react::IntersectionObserverState {
class facebook::react::JArrayBuffer : public jni::HybridClass<facebook::react::JArrayBuffer> {
public JArrayBuffer(std::shared_ptr<facebook::jsi::MutableBuffer> buffer, bool owningBytes) noexcept;
public bool hasBytes() const noexcept;
public bool isOwningBytes() const noexcept;
public const std::shared_ptr<facebook::jsi::MutableBuffer>& mutableBuffer() const;
public static constexpr auto kJavaDescriptor;
public static jni::local_ref<javaobject> createOwned(const void* bytes, size_t size);
public static jni::local_ref<javaobject> createOwning(std::shared_ptr<facebook::jsi::MutableBuffer> buffer);
public static jni::local_ref<javaobject> createUnowned(void* bytes, size_t size);
public static std::shared_ptr<facebook::jsi::MutableBuffer> toJSBuffer(jni::alias_ref<javaobject> arrayBuffer);
public static std::shared_ptr<facebook::jsi::MutableBuffer> toJSBuffer(facebook::jsi::Runtime& runtime, jni::alias_ref<javaobject> arrayBuffer);
public static void registerNatives();
public void invalidate() noexcept;
}
class facebook::react::JBindingsInstaller : public jni::HybridClass<facebook::react::JBindingsInstaller>, public facebook::react::BindingsInstaller {
@@ -4244,6 +4244,7 @@ class facebook::react::AsyncArrayBuffer {
public static facebook::react::AsyncArrayBuffer copy(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer);
public static facebook::react::AsyncArrayBuffer wrap(std::shared_ptr<facebook::jsi::MutableBuffer> buffer) noexcept;
public static facebook::react::AsyncArrayBuffer wrap(std::vector<uint8_t> bytes);
public static void throwIfDetached(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer, const char* callerName);
public std::shared_ptr<facebook::jsi::MutableBuffer> getMutableBuffer() const noexcept;
public uint8_t* data() noexcept;
public ~AsyncArrayBuffer() = default;
@@ -4231,6 +4231,7 @@ class facebook::react::AsyncArrayBuffer {
public static facebook::react::AsyncArrayBuffer copy(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer);
public static facebook::react::AsyncArrayBuffer wrap(std::shared_ptr<facebook::jsi::MutableBuffer> buffer) noexcept;
public static facebook::react::AsyncArrayBuffer wrap(std::vector<uint8_t> bytes);
public static void throwIfDetached(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer, const char* callerName);
public std::shared_ptr<facebook::jsi::MutableBuffer> getMutableBuffer() const noexcept;
public uint8_t* data() noexcept;
public ~AsyncArrayBuffer() = default;
@@ -4242,6 +4242,7 @@ class facebook::react::AsyncArrayBuffer {
public static facebook::react::AsyncArrayBuffer copy(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer);
public static facebook::react::AsyncArrayBuffer wrap(std::shared_ptr<facebook::jsi::MutableBuffer> buffer) noexcept;
public static facebook::react::AsyncArrayBuffer wrap(std::vector<uint8_t> bytes);
public static void throwIfDetached(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer, const char* callerName);
public std::shared_ptr<facebook::jsi::MutableBuffer> getMutableBuffer() const noexcept;
public uint8_t* data() noexcept;
public ~AsyncArrayBuffer() = default;
@@ -976,6 +976,7 @@ class facebook::react::AsyncArrayBuffer {
public static facebook::react::AsyncArrayBuffer copy(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer);
public static facebook::react::AsyncArrayBuffer wrap(std::shared_ptr<facebook::jsi::MutableBuffer> buffer) noexcept;
public static facebook::react::AsyncArrayBuffer wrap(std::vector<uint8_t> bytes);
public static void throwIfDetached(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer, const char* callerName);
public std::shared_ptr<facebook::jsi::MutableBuffer> getMutableBuffer() const noexcept;
public uint8_t* data() noexcept;
public ~AsyncArrayBuffer() = default;
@@ -971,6 +971,7 @@ class facebook::react::AsyncArrayBuffer {
public static facebook::react::AsyncArrayBuffer copy(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer);
public static facebook::react::AsyncArrayBuffer wrap(std::shared_ptr<facebook::jsi::MutableBuffer> buffer) noexcept;
public static facebook::react::AsyncArrayBuffer wrap(std::vector<uint8_t> bytes);
public static void throwIfDetached(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer, const char* callerName);
public std::shared_ptr<facebook::jsi::MutableBuffer> getMutableBuffer() const noexcept;
public uint8_t* data() noexcept;
public ~AsyncArrayBuffer() = default;
@@ -974,6 +974,7 @@ class facebook::react::AsyncArrayBuffer {
public static facebook::react::AsyncArrayBuffer copy(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer);
public static facebook::react::AsyncArrayBuffer wrap(std::shared_ptr<facebook::jsi::MutableBuffer> buffer) noexcept;
public static facebook::react::AsyncArrayBuffer wrap(std::vector<uint8_t> bytes);
public static void throwIfDetached(facebook::jsi::Runtime& rt, const facebook::jsi::ArrayBuffer& buffer, const char* callerName);
public std::shared_ptr<facebook::jsi::MutableBuffer> getMutableBuffer() const noexcept;
public uint8_t* data() noexcept;
public ~AsyncArrayBuffer() = default;