diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/ReadableMapBuffer.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/ReadableMapBuffer.kt index 875873bfb0c..de9adb5d7fa 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/ReadableMapBuffer.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/common/mapbuffer/ReadableMapBuffer.kt @@ -51,13 +51,11 @@ private constructor( ReadableMapBuffer(buffer.duplicate().apply { position(offset) }, offset) private fun readHeader() { - // byte order - val storedAlignment = buffer.short - if (storedAlignment.toInt() != ALIGNMENT) { - buffer.order(ByteOrder.LITTLE_ENDIAN) - } - // count - count = readUnsignedShort(buffer.position()).toInt() + // The C++ writer always serializes in little-endian byte order. ByteBuffer + // defaults to big-endian and duplicate() resets the order, so set it + // explicitly on every instance, including nested clones. + buffer.order(ByteOrder.LITTLE_ENDIAN) + count = readUnsignedShort(offsetToMapBuffer).toInt() } /** @@ -122,26 +120,27 @@ private constructor( return readIntValue(bufferPosition) == 1 } + // Dynamic-data entries store [offset][byteLength] in the bucket's 8-byte + // value: getInt(bufferPosition) is the offset, getInt(bufferPosition + 4) is + // the byte length. The dynamic data section itself carries no length prefix. private fun readStringValue(bufferPosition: Int): String { val offset = offsetForDynamicData + buffer.getInt(bufferPosition) - val sizeOfString = buffer.getInt(offset) + val sizeOfString = buffer.getInt(bufferPosition + Int.SIZE_BYTES) val result = ByteArray(sizeOfString) - val stringOffset = offset + Int.SIZE_BYTES - buffer.position(stringOffset) - buffer[result, 0, sizeOfString] + buffer.position(offset) + buffer.get(result, 0, sizeOfString) return String(result) } private fun readMapBufferValue(position: Int): ReadableMapBuffer { val offset = offsetForDynamicData + buffer.getInt(position) - return cloneWithOffset(offset + Int.SIZE_BYTES) + return cloneWithOffset(offset) } private fun readMapBufferListValue(position: Int): List { val readMapBufferList = arrayListOf() - var offset = offsetForDynamicData + buffer.getInt(position) - val sizeMapBufferList = buffer.getInt(offset) - offset += Int.SIZE_BYTES + val offset = offsetForDynamicData + buffer.getInt(position) + val sizeMapBufferList = buffer.getInt(position + Int.SIZE_BYTES) var curLen = 0 while (curLen < sizeMapBufferList) { val sizeMapBuffer = buffer.getInt(offset + curLen) @@ -153,18 +152,18 @@ private constructor( } private fun readIntBufferValue(bufferPosition: Int): IntArray { - var offset = offsetForDynamicData + buffer.getInt(bufferPosition) - val count = buffer.getInt(offset) - offset += Int.SIZE_BYTES + val offset = offsetForDynamicData + buffer.getInt(bufferPosition) + val byteLength = buffer.getInt(bufferPosition + Int.SIZE_BYTES) + val count = byteLength / Int.SIZE_BYTES val result = IntArray(count) buffer.duplicate().order(buffer.order()).apply { position(offset) }.asIntBuffer().get(result) return result } private fun readDoubleBufferValue(bufferPosition: Int): DoubleArray { - var offset = offsetForDynamicData + buffer.getInt(bufferPosition) - val count = buffer.getInt(offset) - offset += Int.SIZE_BYTES + val offset = offsetForDynamicData + buffer.getInt(bufferPosition) + val byteLength = buffer.getInt(bufferPosition + Int.SIZE_BYTES) + val count = byteLength / Double.SIZE_BYTES val result = DoubleArray(count) buffer.duplicate().order(buffer.order()).apply { position(offset) }.asDoubleBuffer().get(result) return result @@ -359,13 +358,10 @@ private constructor( } public companion object { - // Value used to verify if the data is serialized with LittleEndian order. - private const val ALIGNMENT = 0xFE + // 2 bytes = 2 (count) + private const val HEADER_SIZE = 2 - // 8 bytes = 2 (alignment) + 2 (count) + 4 (size) - private const val HEADER_SIZE = 8 - - // 10 bytes = 2 (key) + 2 (type) + 8 (value) + // 12 bytes = 2 (key) + 2 (type) + 8 (value) private const val BUCKET_SIZE = 12 // 2 bytes = 2 (key) diff --git a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.cpp b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.cpp index e7c0a97ffe5..e657cc5514f 100644 --- a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.cpp @@ -8,8 +8,43 @@ #include "MapBuffer.h" #include +#include +#include + namespace facebook::react { +namespace { +// Reads a value of type T from a (possibly unaligned) offset in the buffer. +// MapBuffer's packed layout places multi-byte values at offsets that are not +// naturally aligned for their type (e.g. an 8-byte value at a 2-byte boundary), +// so dereferencing a reinterpret_cast pointer there is undefined behavior and +// can fault on 32-bit ARM. memcpy compiles to a single unaligned load on +// arm64/x86 and to alignment-safe loads on armv7. +template +inline T readUnaligned(const uint8_t* data, int32_t offset) { + T value; + std::memcpy(&value, data + offset, sizeof(T)); + return value; +} + +// Debug-asserts on OOB (catches corrupt buffers early in dev) AND clamps in +// release so a corrupt bucket length can never drive an OOB memcpy read. +// react_native_assert is compiled out in release, so the runtime cost outside +// dev is the single min() call. +inline int32_t +clampToBufferBounds(int32_t offset, int32_t byteLength, size_t bufferSize) { + react_native_assert(offset >= 0 && byteLength >= 0); + react_native_assert( + static_cast(offset) + static_cast(byteLength) <= + bufferSize); + size_t maxLength = bufferSize > static_cast(offset) + ? bufferSize - static_cast(offset) + : 0; + return static_cast( + std::min(static_cast(std::max(byteLength, 0)), maxLength)); +} +} // namespace + static inline int32_t bucketOffset(int32_t index) { return sizeof(MapBuffer::Header) + sizeof(MapBuffer::Bucket) * index; } @@ -18,16 +53,20 @@ static inline int32_t valueOffset(int32_t bucketIndex) { return bucketOffset(bucketIndex) + offsetof(MapBuffer::Bucket, data); } +// Dynamic-data entries pack [offset (low 32 bits)][byteLength (high 32 bits)] +// into the bucket's 8-byte value, so the payload in the dynamic data section +// carries no in-band length prefix. This returns the position of the high +// 32 bits (the length). +static inline int32_t lengthOffset(int32_t bucketIndex) { + return valueOffset(bucketIndex) + static_cast(sizeof(int32_t)); +} + // TODO T83483191: Extend MapBuffer C++ implementation to support basic random // access MapBuffer::MapBuffer(std::vector data) : bytes_(std::move(data)) { - auto header = reinterpret_cast(bytes_.data()); - count_ = header->count; - - if (header->bufferSize != bytes_.size()) { - LOG(ERROR) << "Error: Data size does not match, expected " - << header->bufferSize << " found: " << bytes_.size(); - abort(); + if (bytes_.size() >= sizeof(Header)) { + auto header = reinterpret_cast(bytes_.data()); + count_ = header->count; } } @@ -37,8 +76,7 @@ int32_t MapBuffer::getKeyBucket(Key key) const { while (lo <= hi) { int32_t mid = (lo + hi) >> 1; - Key midVal = - *reinterpret_cast(bytes_.data() + bucketOffset(mid)); + Key midVal = readUnaligned(bytes_.data(), bucketOffset(mid)); if (midVal < key) { lo = mid + 1; @@ -53,8 +91,7 @@ int32_t MapBuffer::getKeyBucket(Key key) const { } inline int32_t MapBuffer::getIntAtBucket(int32_t bucketIndex) const { - return *reinterpret_cast( - bytes_.data() + valueOffset(bucketIndex)); + return readUnaligned(bytes_.data(), valueOffset(bucketIndex)); } int32_t MapBuffer::getInt(Key key) const { @@ -74,8 +111,7 @@ int64_t MapBuffer::getLong(Key key) const { return 0; } - return *reinterpret_cast( - bytes_.data() + valueOffset(bucketIndex)); + return readUnaligned(bytes_.data(), valueOffset(bucketIndex)); } bool MapBuffer::getBool(Key key) const { @@ -89,8 +125,7 @@ double MapBuffer::getDouble(Key key) const { return 0; } - return *reinterpret_cast( - bytes_.data() + valueOffset(bucketIndex)); + return readUnaligned(bytes_.data(), valueOffset(bucketIndex)); } int32_t MapBuffer::getDynamicDataOffset() const { @@ -107,9 +142,10 @@ std::string MapBuffer::getString(Key key) const { } int32_t offset = getDynamicDataOffset() + getIntAtBucket(bucketIndex); - int32_t stringLength = - *reinterpret_cast(bytes_.data() + offset); - const uint8_t* stringPtr = bytes_.data() + offset + sizeof(int); + auto stringLength = + readUnaligned(bytes_.data(), lengthOffset(bucketIndex)); + stringLength = clampToBufferBounds(offset, stringLength, bytes_.size()); + const uint8_t* stringPtr = bytes_.data() + offset; return {stringPtr, stringPtr + stringLength}; } @@ -122,17 +158,13 @@ MapBuffer MapBuffer::getMapBuffer(Key key) const { } int32_t offset = getDynamicDataOffset() + getIntAtBucket(bucketIndex); - int32_t mapBufferLength = - *reinterpret_cast(bytes_.data() + offset); - size_t maxLength = bytes_.size() - offset - sizeof(int32_t); - if (mapBufferLength > maxLength) { - mapBufferLength = maxLength; - } + auto mapBufferLength = + readUnaligned(bytes_.data(), lengthOffset(bucketIndex)); + mapBufferLength = clampToBufferBounds(offset, mapBufferLength, bytes_.size()); std::vector value(mapBufferLength); - memcpy( - value.data(), bytes_.data() + offset + sizeof(int32_t), mapBufferLength); + memcpy(value.data(), bytes_.data() + offset, mapBufferLength); return MapBuffer(std::move(value)); } @@ -146,19 +178,27 @@ std::vector MapBuffer::getMapBufferList(MapBuffer::Key key) const { std::vector mapBufferList; int32_t offset = getDynamicDataOffset() + getIntAtBucket(bucketIndex); - int32_t mapBufferListLength = - *reinterpret_cast(bytes_.data() + offset); - offset = offset + sizeof(uint32_t); + auto mapBufferListLength = + readUnaligned(bytes_.data(), lengthOffset(bucketIndex)); + mapBufferListLength = + clampToBufferBounds(offset, mapBufferListLength, bytes_.size()); int32_t curLen = 0; while (curLen < mapBufferListLength) { - int32_t mapBufferLength = - *reinterpret_cast(bytes_.data() + offset + curLen); - curLen = curLen + sizeof(uint32_t); + if (curLen + sizeof(int32_t) > mapBufferListLength) { + break; + } + + auto mapBufferLength = + readUnaligned(bytes_.data(), offset + curLen); + curLen += sizeof(int32_t); + + mapBufferLength = + clampToBufferBounds(offset + curLen, mapBufferLength, bytes_.size()); std::vector value(mapBufferLength); memcpy(value.data(), bytes_.data() + offset + curLen, mapBufferLength); mapBufferList.emplace_back(std::move(value)); - curLen = curLen + mapBufferLength; + curLen += mapBufferLength; } return mapBufferList; } @@ -171,13 +211,18 @@ std::vector MapBuffer::getIntBuffer(MapBuffer::Key key) const { } int32_t offset = getDynamicDataOffset() + getIntAtBucket(bucketIndex); - int32_t count = *reinterpret_cast(bytes_.data() + offset); + auto byteLength = + readUnaligned(bytes_.data(), lengthOffset(bucketIndex)); + byteLength = clampToBufferBounds(offset, byteLength, bytes_.size()); + int32_t count = byteLength / static_cast(sizeof(int32_t)); std::vector result(count); if (count > 0) { + // Copy only whole elements: a clamped byteLength may not be a multiple of + // sizeof(int32_t), and result holds exactly count elements. memcpy( result.data(), - bytes_.data() + offset + sizeof(int32_t), + bytes_.data() + offset, static_cast(count) * sizeof(int32_t)); } return result; @@ -191,13 +236,18 @@ std::vector MapBuffer::getDoubleBuffer(MapBuffer::Key key) const { } int32_t offset = getDynamicDataOffset() + getIntAtBucket(bucketIndex); - int32_t count = *reinterpret_cast(bytes_.data() + offset); + auto byteLength = + readUnaligned(bytes_.data(), lengthOffset(bucketIndex)); + byteLength = clampToBufferBounds(offset, byteLength, bytes_.size()); + int32_t count = byteLength / static_cast(sizeof(double)); std::vector result(count); if (count > 0) { + // Copy only whole elements: a clamped byteLength may not be a multiple of + // sizeof(double), and result holds exactly count elements. memcpy( result.data(), - bytes_.data() + offset + sizeof(int32_t), + bytes_.data() + offset, static_cast(count) * sizeof(double)); } return result; diff --git a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.h b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.h index ae89b674b6a..07b64bdfba9 100644 --- a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.h +++ b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBuffer.h @@ -40,11 +40,11 @@ class JReadableMapBuffer; * * MapBuffer data is stored in a continuous chunk of memory (bytes_ field below) with the following layout: * - * ┌─────────────────────Header──────────────────────┐ - * │ 10 bytes │ - * ├─Alignment─┬─Item count─┬──────Buffer size───────┤ - * │ 2 bytes │ 2 bytes │ 4 bytes │ - * └───────────┴────────────┴────────────────────────┘ + * ┌──────Header──────┐ + * │ 2 bytes │ + * ├────Item count────┤ + * │ 2 bytes │ + * └──────────────────┘ * ┌────────────────────────────────────────────────────────────────────────────────────────┐ * │ Buckets (one per item in the map) │ * │ │ @@ -69,14 +69,8 @@ class MapBuffer { public: using Key = uint16_t; - // The first value in the buffer, used to check correct encoding/endianness on - // JVM side. - constexpr static uint16_t HEADER_ALIGNMENT = 0xFE; - struct Header { - uint16_t alignment = HEADER_ALIGNMENT; // alignment of serialization uint16_t count; // amount of items in the map - uint32_t bufferSize; // Amount of bytes used to store the map in memory }; #pragma pack(push, 1) @@ -89,7 +83,7 @@ class MapBuffer { }; #pragma pack(pop) - static_assert(sizeof(Header) == 8, "MapBuffer header size is incorrect."); + static_assert(sizeof(Header) == 2, "MapBuffer header size is incorrect."); static_assert(sizeof(Bucket) == 12, "MapBuffer bucket size is incorrect."); /** @@ -105,15 +99,17 @@ class MapBuffer { String = 3, Map = 4, Long = 5, - // Homogeneous, length-prefixed arrays stored contiguously in the dynamic + // Homogeneous arrays of raw elements stored contiguously in the dynamic // data section. Unlike Map, they carry no per-element key/type overhead, so - // a batch of N values costs ~N*elementSize bytes plus a single 4-byte count - // prefix instead of N*12-byte buckets. The bucket value is the offset of the - // array within the dynamic data section. + // a batch of N values costs ~N*elementSize bytes instead of N*12-byte + // buckets. The bucket value packs [offset][byteLength]; the element count is + // recovered as byteLength / elementSize. IntBuffer = 6, DoubleBuffer = 7, - // A homogeneous, ordered array of nested MapBuffers. Distinct from `Map` so - // that a list of MapBuffers is self-describing (a single Map and a list are + // A homogeneous, ordered array of nested MapBuffers. The bucket value packs + // [offset][byteLength] for the whole list region; within it each child stays + // framed as [int32 childSize][child bytes]. Distinct from `Map` so that a + // list of MapBuffers is self-describing (a single Map and a list are // byte-distinct in payload but previously shared the `Map` type tag). MapBufferList = 8, }; diff --git a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp index 8ee37c1c01e..1944a01f261 100644 --- a/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mapbuffer/MapBufferBuilder.cpp @@ -15,6 +15,14 @@ constexpr uint32_t LONG_SIZE = sizeof(uint64_t); constexpr uint32_t DOUBLE_SIZE = sizeof(double); constexpr uint32_t MAX_BUCKET_VALUE_SIZE = sizeof(uint64_t); +// Dynamic-data entries store their location in the bucket's 8-byte value as +// [offset (low 32 bits)][byteLength (high 32 bits)], so the payload in the +// dynamic data section needs no in-band length prefix. +static inline uint64_t packOffsetAndLength(int32_t offset, int32_t length) { + return static_cast(static_cast(offset)) | + (static_cast(static_cast(length)) << 32); +} + MapBuffer MapBufferBuilder::EMPTY() { return MapBufferBuilder(0).build(); } @@ -22,7 +30,6 @@ MapBuffer MapBufferBuilder::EMPTY() { MapBufferBuilder::MapBufferBuilder(uint32_t initialSize) { buckets_.reserve(initialSize); header_.count = 0; - header_.bufferSize = 0; } void MapBufferBuilder::storeKeyValue( @@ -84,128 +91,111 @@ void MapBufferBuilder::putLong(MapBuffer::Key key, int64_t value) { } void MapBufferBuilder::putString(MapBuffer::Key key, const std::string& value) { - // The wire format encodes lengths and offsets as int32_t (see - // MapBuffer::getString). Without an explicit narrowing cast, `auto` deduces - // size_t (8 bytes on 64-bit) and `memcpy(&x, ..., INT_SIZE)` then copies only - // the first 4 bytes of an 8-byte value: silent truncation on little-endian, - // wrong (high) bytes on big-endian. auto strSize = static_cast(value.size()); - const char* strData = value.data(); - - // format [length of string (int)] + [Array of Characters in the string] auto offset = static_cast(dynamicData_.size()); - dynamicData_.resize(offset + INT_SIZE + strSize, 0); - memcpy(dynamicData_.data() + offset, &strSize, INT_SIZE); - memcpy(dynamicData_.data() + offset + INT_SIZE, strData, strSize); - // Store Key and pointer to the string + // The bucket stores [offset][byteLength]; the dynamic section holds only the + // raw string bytes. + dynamicData_.resize(offset + strSize, 0); + if (strSize > 0) { + memcpy(dynamicData_.data() + offset, value.data(), strSize); + } + + uint64_t data = packOffsetAndLength(offset, strSize); storeKeyValue( key, MapBuffer::DataType::String, - reinterpret_cast(&offset), - INT_SIZE); + reinterpret_cast(&data), + sizeof(data)); } void MapBufferBuilder::putMapBuffer(MapBuffer::Key key, const MapBuffer& map) { - // Wire format encodes lengths and offsets as int32_t (see - // MapBuffer::getMapBuffer). Cast explicitly so memcpy(&x, ..., INT_SIZE) - // copies the full value, not the first 4 bytes of an 8-byte size_t. auto mapBufferSize = static_cast(map.size()); - auto offset = static_cast(dynamicData_.size()); - // format [length of buffer (int)] + [bytes of MapBuffer] - dynamicData_.resize(offset + INT_SIZE + mapBufferSize, 0); - memcpy(dynamicData_.data() + offset, &mapBufferSize, INT_SIZE); - // Copy the content of the map into dynamicData_ - memcpy(dynamicData_.data() + offset + INT_SIZE, map.data(), mapBufferSize); + // The bucket stores [offset][byteLength]; the dynamic section holds only the + // serialized child MapBuffer bytes. + dynamicData_.resize(offset + mapBufferSize, 0); + memcpy(dynamicData_.data() + offset, map.data(), mapBufferSize); - // Store Key and pointer to the string + uint64_t data = packOffsetAndLength(offset, mapBufferSize); storeKeyValue( key, MapBuffer::DataType::Map, - reinterpret_cast(&offset), - INT_SIZE); + reinterpret_cast(&data), + sizeof(data)); } void MapBufferBuilder::putMapBufferList( MapBuffer::Key key, const std::vector& mapBufferList) { auto offset = static_cast(dynamicData_.size()); - int32_t dataSize = 0; - for (const MapBuffer& mapBuffer : mapBufferList) { - dataSize = dataSize + INT_SIZE + static_cast(mapBuffer.size()); - } - - dynamicData_.resize(offset + INT_SIZE, 0); - memcpy(dynamicData_.data() + offset, &dataSize, INT_SIZE); + // The bucket stores [offset][byteLength] for the whole list region; within it + // each child stays framed as [int32 childSize][child bytes] so the children + // remain individually delimited. for (const MapBuffer& mapBuffer : mapBufferList) { auto mapBufferSize = static_cast(mapBuffer.size()); - auto dynamicDataSize = static_cast(dynamicData_.size()); - dynamicData_.resize(dynamicDataSize + INT_SIZE + mapBufferSize, 0); - // format [length of buffer (int)] + [bytes of MapBuffer] - memcpy(dynamicData_.data() + dynamicDataSize, &mapBufferSize, INT_SIZE); - // Copy the content of the map into dynamicData_ + auto pos = static_cast(dynamicData_.size()); + dynamicData_.resize(pos + INT_SIZE + mapBufferSize, 0); + memcpy(dynamicData_.data() + pos, &mapBufferSize, INT_SIZE); memcpy( - dynamicData_.data() + dynamicDataSize + INT_SIZE, - mapBuffer.data(), - mapBufferSize); + dynamicData_.data() + pos + INT_SIZE, mapBuffer.data(), mapBufferSize); } - // Store Key and pointer to the list. Uses the dedicated MapBufferList type so - // the entry is self-describing and distinguishable from a single Map. + auto totalSize = static_cast(dynamicData_.size()) - offset; + uint64_t data = packOffsetAndLength(offset, totalSize); + // Uses the dedicated MapBufferList type so the entry is self-describing and + // distinguishable from a single Map. storeKeyValue( key, MapBuffer::DataType::MapBufferList, - reinterpret_cast(&offset), - INT_SIZE); + reinterpret_cast(&data), + sizeof(data)); } void MapBufferBuilder::putIntBuffer( MapBuffer::Key key, const std::vector& value) { - // Wire format: [element count (int32)] + [count * int32]. The count is the - // number of elements, not bytes; see MapBuffer::getIntBuffer. - auto count = static_cast(value.size()); + // The bucket stores [offset][byteLength]; the dynamic section holds the raw + // int32 elements. Element count is recovered as byteLength / sizeof(int32_t). auto payloadSize = static_cast(value.size() * sizeof(int32_t)); - auto offset = static_cast(dynamicData_.size()); - dynamicData_.resize(offset + INT_SIZE + payloadSize, 0); - memcpy(dynamicData_.data() + offset, &count, INT_SIZE); + dynamicData_.resize(offset + payloadSize, 0); if (payloadSize > 0) { - memcpy(dynamicData_.data() + offset + INT_SIZE, value.data(), payloadSize); + memcpy(dynamicData_.data() + offset, value.data(), payloadSize); } + uint64_t data = packOffsetAndLength(offset, payloadSize); storeKeyValue( key, MapBuffer::DataType::IntBuffer, - reinterpret_cast(&offset), - INT_SIZE); + reinterpret_cast(&data), + sizeof(data)); } void MapBufferBuilder::putDoubleBuffer( MapBuffer::Key key, const std::vector& value) { - // Wire format: [element count (int32)] + [count * double]. Doubles are copied - // byte-for-byte; the reader uses memcpy, so the payload needs no special - // alignment for correctness. A consumer that wants a zero-copy typed view on - // the JVM (ByteBuffer::asDoubleBuffer) must ensure 8-byte alignment itself. - auto count = static_cast(value.size()); + // The bucket stores [offset][byteLength]; the dynamic section holds the raw + // double elements. Element count is recovered as byteLength / sizeof(double). + // Doubles are copied byte-for-byte; the reader uses memcpy, so the payload + // needs no special alignment for correctness. A consumer that wants a + // zero-copy typed view on the JVM (ByteBuffer::asDoubleBuffer) must ensure + // 8-byte alignment itself. auto payloadSize = static_cast(value.size() * sizeof(double)); - auto offset = static_cast(dynamicData_.size()); - dynamicData_.resize(offset + INT_SIZE + payloadSize, 0); - memcpy(dynamicData_.data() + offset, &count, INT_SIZE); + dynamicData_.resize(offset + payloadSize, 0); if (payloadSize > 0) { - memcpy(dynamicData_.data() + offset + INT_SIZE, value.data(), payloadSize); + memcpy(dynamicData_.data() + offset, value.data(), payloadSize); } + uint64_t data = packOffsetAndLength(offset, payloadSize); storeKeyValue( key, MapBuffer::DataType::DoubleBuffer, - reinterpret_cast(&offset), - INT_SIZE); + reinterpret_cast(&data), + sizeof(data)); } static inline bool compareBuckets( @@ -220,8 +210,6 @@ MapBuffer MapBufferBuilder::build() { auto headerSize = sizeof(MapBuffer::Header); auto bufferSize = headerSize + bucketSize + dynamicData_.size(); - header_.bufferSize = static_cast(bufferSize); - if (needsSort_) { std::sort(buckets_.begin(), buckets_.end(), compareBuckets); } diff --git a/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp b/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp index ce4fd6237c0..05c70d203f5 100644 --- a/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/mapbuffer/tests/MapBufferTest.cpp @@ -49,7 +49,7 @@ TEST(MapBufferTest, testSimpleLongMap) { } TEST(MapBufferTest, testMapBufferExtension) { - // 26 = 2 buckets: 2*10 + 6 sizeof(header) + // initialSize is a reserve hint for the number of buckets int initialSize = 26; auto buffer = MapBufferBuilder(initialSize); diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api index 8022b30807e..ef70f42b27e 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api @@ -3247,7 +3247,6 @@ class facebook::react::MapBuffer { public int32_t getInt(facebook::react::MapBuffer::Key key) const; public int64_t getLong(facebook::react::MapBuffer::Key key) const; public size_t size() const; - public static constexpr uint16_t HEADER_ALIGNMENT; public std::string getString(facebook::react::MapBuffer::Key key) const; public std::vector getDoubleBuffer(facebook::react::MapBuffer::Key key) const; public std::vector getMapBufferList(facebook::react::MapBuffer::Key key) const; @@ -3276,9 +3275,7 @@ struct facebook::react::MapBuffer::Bucket { } struct facebook::react::MapBuffer::Header { - public uint16_t alignment; public uint16_t count; - public uint32_t bufferSize; } class facebook::react::MapBufferBuilder { diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api index c241745f95f..a7b27cd3aa8 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api @@ -3156,7 +3156,6 @@ class facebook::react::MapBuffer { public int32_t getInt(facebook::react::MapBuffer::Key key) const; public int64_t getLong(facebook::react::MapBuffer::Key key) const; public size_t size() const; - public static constexpr uint16_t HEADER_ALIGNMENT; public std::string getString(facebook::react::MapBuffer::Key key) const; public std::vector getDoubleBuffer(facebook::react::MapBuffer::Key key) const; public std::vector getMapBufferList(facebook::react::MapBuffer::Key key) const; @@ -3185,9 +3184,7 @@ struct facebook::react::MapBuffer::Bucket { } struct facebook::react::MapBuffer::Header { - public uint16_t alignment; public uint16_t count; - public uint32_t bufferSize; } class facebook::react::MapBufferBuilder { diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api index 73beff563c8..37597a3834e 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api @@ -3244,7 +3244,6 @@ class facebook::react::MapBuffer { public int32_t getInt(facebook::react::MapBuffer::Key key) const; public int64_t getLong(facebook::react::MapBuffer::Key key) const; public size_t size() const; - public static constexpr uint16_t HEADER_ALIGNMENT; public std::string getString(facebook::react::MapBuffer::Key key) const; public std::vector getDoubleBuffer(facebook::react::MapBuffer::Key key) const; public std::vector getMapBufferList(facebook::react::MapBuffer::Key key) const; @@ -3273,9 +3272,7 @@ struct facebook::react::MapBuffer::Bucket { } struct facebook::react::MapBuffer::Header { - public uint16_t alignment; public uint16_t count; - public uint32_t bufferSize; } class facebook::react::MapBufferBuilder { diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index 0522de96cd2..947870b8c66 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -5482,7 +5482,6 @@ class facebook::react::MapBuffer { public int32_t getInt(facebook::react::MapBuffer::Key key) const; public int64_t getLong(facebook::react::MapBuffer::Key key) const; public size_t size() const; - public static constexpr uint16_t HEADER_ALIGNMENT; public std::string getString(facebook::react::MapBuffer::Key key) const; public std::vector getDoubleBuffer(facebook::react::MapBuffer::Key key) const; public std::vector getMapBufferList(facebook::react::MapBuffer::Key key) const; @@ -5511,9 +5510,7 @@ struct facebook::react::MapBuffer::Bucket { } struct facebook::react::MapBuffer::Header { - public uint16_t alignment; public uint16_t count; - public uint32_t bufferSize; } class facebook::react::MapBufferBuilder { diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index e961ff1cf98..d60d97b5ac7 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -5406,7 +5406,6 @@ class facebook::react::MapBuffer { public int32_t getInt(facebook::react::MapBuffer::Key key) const; public int64_t getLong(facebook::react::MapBuffer::Key key) const; public size_t size() const; - public static constexpr uint16_t HEADER_ALIGNMENT; public std::string getString(facebook::react::MapBuffer::Key key) const; public std::vector getDoubleBuffer(facebook::react::MapBuffer::Key key) const; public std::vector getMapBufferList(facebook::react::MapBuffer::Key key) const; @@ -5435,9 +5434,7 @@ struct facebook::react::MapBuffer::Bucket { } struct facebook::react::MapBuffer::Header { - public uint16_t alignment; public uint16_t count; - public uint32_t bufferSize; } class facebook::react::MapBufferBuilder { diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index f5fd13c275c..325e0e4dd4c 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -5479,7 +5479,6 @@ class facebook::react::MapBuffer { public int32_t getInt(facebook::react::MapBuffer::Key key) const; public int64_t getLong(facebook::react::MapBuffer::Key key) const; public size_t size() const; - public static constexpr uint16_t HEADER_ALIGNMENT; public std::string getString(facebook::react::MapBuffer::Key key) const; public std::vector getDoubleBuffer(facebook::react::MapBuffer::Key key) const; public std::vector getMapBufferList(facebook::react::MapBuffer::Key key) const; @@ -5508,9 +5507,7 @@ struct facebook::react::MapBuffer::Bucket { } struct facebook::react::MapBuffer::Header { - public uint16_t alignment; public uint16_t count; - public uint32_t bufferSize; } class facebook::react::MapBufferBuilder { diff --git a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api index 5ec53a2f575..144dbea1f8e 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api @@ -2168,7 +2168,6 @@ class facebook::react::MapBuffer { public int32_t getInt(facebook::react::MapBuffer::Key key) const; public int64_t getLong(facebook::react::MapBuffer::Key key) const; public size_t size() const; - public static constexpr uint16_t HEADER_ALIGNMENT; public std::string getString(facebook::react::MapBuffer::Key key) const; public std::vector getDoubleBuffer(facebook::react::MapBuffer::Key key) const; public std::vector getMapBufferList(facebook::react::MapBuffer::Key key) const; @@ -2197,9 +2196,7 @@ struct facebook::react::MapBuffer::Bucket { } struct facebook::react::MapBuffer::Header { - public uint16_t alignment; public uint16_t count; - public uint32_t bufferSize; } class facebook::react::MapBufferBuilder { diff --git a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api index 2b67e1c5b4f..c4253f9a715 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api @@ -2104,7 +2104,6 @@ class facebook::react::MapBuffer { public int32_t getInt(facebook::react::MapBuffer::Key key) const; public int64_t getLong(facebook::react::MapBuffer::Key key) const; public size_t size() const; - public static constexpr uint16_t HEADER_ALIGNMENT; public std::string getString(facebook::react::MapBuffer::Key key) const; public std::vector getDoubleBuffer(facebook::react::MapBuffer::Key key) const; public std::vector getMapBufferList(facebook::react::MapBuffer::Key key) const; @@ -2133,9 +2132,7 @@ struct facebook::react::MapBuffer::Bucket { } struct facebook::react::MapBuffer::Header { - public uint16_t alignment; public uint16_t count; - public uint32_t bufferSize; } class facebook::react::MapBufferBuilder { diff --git a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api index 2f7f3c22e51..1cab650e5a9 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api @@ -2165,7 +2165,6 @@ class facebook::react::MapBuffer { public int32_t getInt(facebook::react::MapBuffer::Key key) const; public int64_t getLong(facebook::react::MapBuffer::Key key) const; public size_t size() const; - public static constexpr uint16_t HEADER_ALIGNMENT; public std::string getString(facebook::react::MapBuffer::Key key) const; public std::vector getDoubleBuffer(facebook::react::MapBuffer::Key key) const; public std::vector getMapBufferList(facebook::react::MapBuffer::Key key) const; @@ -2194,9 +2193,7 @@ struct facebook::react::MapBuffer::Bucket { } struct facebook::react::MapBuffer::Header { - public uint16_t alignment; public uint16_t count; - public uint32_t bufferSize; } class facebook::react::MapBufferBuilder {