From 19a31849817b8cf0eaef56305330c4b39d826fe8 Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Mon, 21 Sep 2026 12:17:17 +0100 Subject: [PATCH] Revert "Use NSData for the JS->ObjC ArrayBuffer argument path (#57596)" This reverts commit 5e86c323cefb728bba0a21c3b2a65af93e5868d6. # Conflicts: # packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap # packages/react-native/ReactCommon/react/nativemodule/core/iostests/RCTTurboModuleTests.mm --- .../GenerateModuleObjCpp/serializeMethod.js | 2 +- .../GenerateModuleHObjCpp-test.js.snap | 4 ++-- .../core/iostests/RCTTurboModuleTests.mm | 23 ++++++++++--------- .../ios/ReactCommon/RCTTurboModule.mm | 19 +++++++-------- .../RCTTurboModuleArrayBufferTests.mm | 15 ++++++------ 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js index 6811646078e..e5dcb5e52c1 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js @@ -227,7 +227,7 @@ function getParamObjCType( return notStruct(wrapOptional('NSArray *', !nullable)); } case 'ArrayBufferTypeAnnotation': { - return notStruct(wrapOptional('NSData *', !nullable)); + return notStruct(wrapOptional('NSMutableData *', !nullable)); } } diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap index 8ed200aa17d..a1bdcd93e58 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap @@ -103,8 +103,8 @@ Map { @protocol NativeSampleTurboModuleSpec - (NSMutableData *)getArrayBuffer; -- (void)voidArrayBuffer:(NSData *)arg; -- (void)voidNullableArrayBuffer:(NSData * _Nullable)arg; +- (void)voidArrayBuffer:(NSMutableData *)arg; +- (void)voidNullableArrayBuffer:(NSMutableData * _Nullable)arg; @end diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/iostests/RCTTurboModuleTests.mm b/packages/react-native/ReactCommon/react/nativemodule/core/iostests/RCTTurboModuleTests.mm index 0c6705ffcfc..fa58d4b4a8c 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/iostests/RCTTurboModuleTests.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/core/iostests/RCTTurboModuleTests.mm @@ -319,11 +319,12 @@ class ExceptionCapturingNativeMethodCallInvoker : public NativeMethodCallInvoker XCTAssertNotNil(original.callStackSymbols); } -// A JS ArrayBuffer converts (on the argument path) to an immutable NSData that -// owns an independent copy of the bytes, so the result stays valid after the -// source buffer is gone. This covers the ArrayBuffer-backed-by-native-MutableBuffer -// case, which is the one that could in principle have been aliased zero-copy. -- (void)testArrayBufferConvertsToIndependentNSData +// A JS ArrayBuffer converts to an NSMutableData that owns an independent copy of +// the bytes — NSMutableData cannot alias a foreign buffer, so the result stays +// valid and is safe to mutate after the source buffer is gone. This covers the +// ArrayBuffer-backed-by-native-MutableBuffer case, which is the one that could in +// principle have been aliased zero-copy. +- (void)testArrayBufferConvertsToIndependentNSMutableData { constexpr size_t kBufferSize = 64 * 1024; @@ -337,15 +338,15 @@ class ExceptionCapturingNativeMethodCallInvoker : public NativeMethodCallInvoker id converted = TurboModuleConvertUtils::convertJSIValueToObjCObject(*rt, facebook::jsi::Value(*rt, arrayBuffer), nullptr); - XCTAssertTrue([converted isKindOfClass:[NSData class]]); - NSData *data = (NSData *)converted; + XCTAssertTrue([converted isKindOfClass:[NSMutableData class]]); + NSMutableData *data = (NSMutableData *)converted; XCTAssertEqual(data.length, (NSUInteger)kBufferSize); XCTAssertEqual(*static_cast(data.bytes), 0xAB); - // Independent copy: mutating the source MutableBuffer must not write through to - // the NSData. - *buffer->data() = 0xCD; - XCTAssertEqual(*static_cast(data.bytes), 0xAB, @"NSData must not alias the source buffer"); + // Independent copy: mutating the NSMutableData must not write through to the + // source MutableBuffer. + *static_cast(data.mutableBytes) = 0xCD; + XCTAssertEqual(*buffer->data(), 0xAB, @"NSMutableData must not alias the source buffer"); } @end diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm index 9f7877c5f8a..f3ec9bc7215 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm @@ -201,15 +201,16 @@ convertJSIFunctionToCallback(jsi::Runtime &rt, jsi::Function &&function, const s }; } -// Copy the ArrayBuffer's bytes into an immutable NSData. An inbound buffer is -// owned by the caller, not the native module, so NSData (not NSMutableData) is -// the correct read-only contract. Copying makes the NSData self-contained and -// safe to retain in a block, store, or dispatch to another thread, regardless of -// whether the bytes were owned by JS (valid only for this callstack) or by a -// native MutableBuffer (which the JS ArrayBuffer may GC concurrently). -static NSData *convertJSIArrayBufferToNSData(jsi::Runtime &rt, const jsi::ArrayBuffer &value) +// Copy the ArrayBuffer's bytes into an NSMutableData. A zero-copy wrap is not +// possible here: NSMutableData needs its own resizable backing store and cannot +// alias a foreign buffer (even via initWithBytesNoCopy:length:deallocator:, which +// copies eagerly for the mutable subclass). Copying also makes the result safe to +// retain in a block, store, or dispatch to another thread, regardless of whether +// the bytes were owned by JS (valid only for this callstack) or by a native +// MutableBuffer (which the JS ArrayBuffer may GC concurrently). +static NSMutableData *convertJSIArrayBufferToNSMutableData(jsi::Runtime &rt, const jsi::ArrayBuffer &value) { - return [NSData dataWithBytes:value.data(rt) length:value.size(rt)]; + return [NSMutableData dataWithBytes:value.data(rt) length:value.size(rt)]; } id convertJSIValueToObjCObject( @@ -242,7 +243,7 @@ id convertJSIValueToObjCObject( return convertJSIFunctionToCallback(runtime, o.getFunction(runtime), jsInvoker); } if (o.isArrayBuffer(runtime)) { - return convertJSIArrayBufferToNSData(runtime, o.getArrayBuffer(runtime)); + return convertJSIArrayBufferToNSMutableData(runtime, o.getArrayBuffer(runtime)); } return convertJSIObjectToNSDictionary(runtime, o, jsInvoker, useNSNull); } diff --git a/packages/rn-tester/RNTesterUnitTests/RCTTurboModuleArrayBufferTests.mm b/packages/rn-tester/RNTesterUnitTests/RCTTurboModuleArrayBufferTests.mm index 156af187618..fd242c2de83 100644 --- a/packages/rn-tester/RNTesterUnitTests/RCTTurboModuleArrayBufferTests.mm +++ b/packages/rn-tester/RNTesterUnitTests/RCTTurboModuleArrayBufferTests.mm @@ -105,17 +105,16 @@ class QueueingNativeMethodCallInvoker final : public NativeMethodCallInvoker { RCT_EXPORT_MODULE() -RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSMutableData *, testMethodWhichTransformsArrayBuffer : (NSData *)buffer) +RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSMutableData *, testMethodWhichMutatesArrayBuffer : (NSMutableData *)buffer) { - NSMutableData *result = [buffer mutableCopy]; - auto *bytes = static_cast(result.mutableBytes); - for (NSUInteger i = 0; i < result.length; ++i) { + auto *bytes = static_cast(buffer.mutableBytes); + for (NSUInteger i = 0; i < buffer.length; ++i) { bytes[i] = static_cast((i + 1) * 10); } - return result; + return buffer; } -RCT_EXPORT_METHOD(testMethodWhichStoresArrayBuffer : (NSData *)payload) +RCT_EXPORT_METHOD(testMethodWhichStoresArrayBuffer : (NSMutableData *)payload) { self.lastReceivedPayload = [payload copy]; } @@ -170,8 +169,8 @@ RCT_EXPORT_METHOD( auto result = module.invokeObjCMethod( *rt, ArrayBufferKind, - "testMethodWhichTransformsArrayBuffer", - @selector(testMethodWhichTransformsArrayBuffer:), + "testMethodWhichMutatesArrayBuffer", + @selector(testMethodWhichMutatesArrayBuffer:), args, 1);