From 6fc62e831cda220a21c95a8e06d3fd8200f662e2 Mon Sep 17 00:00:00 2001 From: Rajendra Singh Asoliya <121512381+rajendra0000@users.noreply.github.com> Date: Thu, 3 Dec 2026 17:30:42 +0530 Subject: [PATCH 1/2] test: add pummel tests for buffers and ArrayBuffers over 4 GiB Buffer.prototype.write() on a Buffer larger than 4 GiB aborts the process once V8 calls the fast API variant of the one-byte string writers, because offset and max_length are passed as uint32_t. Reading into a BYOB view at an offset above 4 GiB fails a CHECK in the native ArrayBuffer copy for the same reason. Signed-off-by: Rajendra Singh Asoliya <121512381+rajendra0000@users.noreply.github.com> --- .../test-buffer-large-size-write-string.js | 42 +++++++++++++++++++ .../test-webstream-byob-large-offset.js | 40 ++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 test/pummel/test-buffer-large-size-write-string.js create mode 100644 test/pummel/test-webstream-byob-large-offset.js diff --git a/test/pummel/test-buffer-large-size-write-string.js b/test/pummel/test-buffer-large-size-write-string.js new file mode 100644 index 00000000000..dd54078571a --- /dev/null +++ b/test/pummel/test-buffer-large-size-write-string.js @@ -0,0 +1,42 @@ +'use strict'; +const common = require('../common'); + +// Regression test for the string-write fast API with Buffers larger than +// 4 GiB. Offsets and lengths were passed to the fast call as uint32_t, which +// wrapped them, and a CHECK then aborted the process. +common.skipIf32Bits(); + +const assert = require('node:assert'); + +const size = 2 ** 32 + 4096; + +let buf; +try { + buf = Buffer.alloc(size); +} catch (e) { + if ( + e.code === 'ERR_MEMORY_ALLOCATION_FAILED' || + /Array buffer allocation failed/.test(e.message) + ) { + common.skip('insufficient memory for Buffer.alloc'); + } + throw e; +} + +// Run enough iterations for V8 to tier up and call the fast API function. +const hot = 200_000; +for (const encoding of ['utf8', 'latin1', 'ascii']) { + // More than 4 GiB remain after the offset. + for (let i = 0; i < hot; i++) { + assert.strictEqual(buf.write('abc', 0, encoding), 3); + } + assert.strictEqual(buf.toString('latin1', 0, 3), 'abc'); + + // The offset itself is larger than 4 GiB. A wrapped offset would write at + // index 32 instead. + for (let i = 0; i < hot; i++) { + assert.strictEqual(buf.write('xyz', 2 ** 32 + 32, encoding), 3); + } + assert.strictEqual(buf.toString('latin1', 2 ** 32 + 32, 2 ** 32 + 35), 'xyz'); + assert.strictEqual(buf.toString('latin1', 32, 35), '\0\0\0'); +} diff --git a/test/pummel/test-webstream-byob-large-offset.js b/test/pummel/test-webstream-byob-large-offset.js new file mode 100644 index 00000000000..37563081ad4 --- /dev/null +++ b/test/pummel/test-webstream-byob-large-offset.js @@ -0,0 +1,40 @@ +'use strict'; +const common = require('../common'); + +// Regression test for ReadableStream BYOB reads into a view whose offset into +// the underlying ArrayBuffer is larger than 4 GiB. The offset was passed to +// the native copy as a uint32_t and failed a CHECK. +common.skipIf32Bits(); + +const assert = require('node:assert'); +const { ReadableStream } = require('node:stream/web'); + +const offset = 2 ** 32 + 8; + +let ab; +try { + ab = new ArrayBuffer(2 ** 32 + 64); +} catch (e) { + if (/Array buffer allocation failed/.test(e.message)) { + common.skip('insufficient memory for ArrayBuffer'); + } + throw e; +} + +const stream = new ReadableStream({ + type: 'bytes', + start(controller) { + controller.enqueue(new Uint8Array([1, 2, 3, 4])); + controller.close(); + }, +}); + +(async () => { + const reader = stream.getReader({ mode: 'byob' }); + const { value } = await reader.read(new Uint8Array(ab, offset, 16)); + // The read transfers the buffer, so inspect the returned view. + assert.strictEqual(value.byteOffset, offset); + assert.strictEqual(value.length, 4); + assert.deepStrictEqual([...value], [1, 2, 3, 4]); + assert.strictEqual(value.buffer.byteLength, 2 ** 32 + 64); +})().then(common.mustCall()); From 68b5c77fb8d4c23ffce9ece1d2b148fa40ee605a Mon Sep 17 00:00:00 2001 From: Rajendra Singh Asoliya <121512381+rajendra0000@users.noreply.github.com> Date: Thu, 3 Dec 2026 15:25:02 +0530 Subject: [PATCH 2/2] buffer: support buffers over 4 GiB in string writes The fast API variants of asciiWrite, latin1Write and utf8Write, and of the ArrayBuffer copy binding used by web streams, took offsets and lengths as uint32_t. V8 wraps larger values, so writing into a Buffer larger than 4 GiB either aborted the process on CHECK(dst_length - offset <= UINT32_MAX) or targeted a wrapped offset. Pass offsets and lengths as doubles and use size_t internally, as buffer.copy() already does. Refs: https://github.com/nodejs/node/issues/55422 Signed-off-by: Rajendra Singh Asoliya <121512381+rajendra0000@users.noreply.github.com> --- src/node_buffer.cc | 94 ++++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 6c2321d5f1b..9a96a70357f 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -1776,12 +1776,24 @@ static void SetDetachKey(const FunctionCallbackInfo& args) { ab->SetDetachKey(key); } +// Converts a number parameter to size_t suitable for ArrayBuffer sizes +// Could be larger than uint32_t +// See v8::internal::TryNumberToSize and v8::internal::NumberToSize +inline size_t CheckNumberToSize(Local number) { + CHECK(number->IsNumber()); + double value = number.As()->Value(); + // See v8::internal::TryNumberToSize on this (and on < comparison) + double maxSize = static_cast(std::numeric_limits::max()); + CHECK(value >= 0 && value < maxSize); + return static_cast(value); +} + void CopyArrayBufferImpl(Local target, - uint32_t target_start, + size_t target_start, Local source, - uint32_t source_start, - uint32_t bytes_to_copy) { - uint32_t bytes_copied = source->CopyArrayBufferBytes( + size_t source_start, + size_t bytes_to_copy) { + size_t bytes_copied = source->CopyArrayBufferBytes( source_start, bytes_to_copy, target, target_start); CHECK_EQ(bytes_copied, bytes_to_copy); } @@ -1794,48 +1806,37 @@ void CopyArrayBuffer(const FunctionCallbackInfo& args) { // args[4] == bytesToCopy CHECK(args[0]->IsArrayBuffer()); - CHECK(args[1]->IsUint32()); + CHECK(args[1]->IsNumber()); CHECK(args[2]->IsArrayBuffer()); - CHECK(args[3]->IsUint32()); - CHECK(args[4]->IsUint32()); + CHECK(args[3]->IsNumber()); + CHECK(args[4]->IsNumber()); + // Offsets and lengths can exceed uint32 for ArrayBuffers larger than 4 GiB. CopyArrayBufferImpl(args[0].As(), - args[1].As()->Value(), + CheckNumberToSize(args[1]), args[2].As(), - args[3].As()->Value(), - args[4].As()->Value()); + CheckNumberToSize(args[3]), + CheckNumberToSize(args[4])); } void FastCopyArrayBuffer(Local receiver, Local target, - uint32_t target_start, + double target_start, Local source, - uint32_t source_start, - uint32_t bytes_to_copy) { + double source_start, + double bytes_to_copy) { CHECK(target->IsArrayBuffer()); CHECK(source->IsArrayBuffer()); CopyArrayBufferImpl(target.As(), - target_start, + static_cast(target_start), source.As(), - source_start, - bytes_to_copy); + static_cast(source_start), + static_cast(bytes_to_copy)); } static CFunction fast_copy_array_buffer(CFunction::Make(FastCopyArrayBuffer)); -// Converts a number parameter to size_t suitable for ArrayBuffer sizes -// Could be larger than uint32_t -// See v8::internal::TryNumberToSize and v8::internal::NumberToSize -inline size_t CheckNumberToSize(Local number) { - CHECK(number->IsNumber()); - double value = number.As()->Value(); - // See v8::internal::TryNumberToSize on this (and on < comparison) - double maxSize = static_cast(std::numeric_limits::max()); - CHECK(value >= 0 && value < maxSize); - return static_cast(value); -} - // Allocates an ArrayBuffer of `size` bytes. Its contents are left // uninitialized, unless zero-filling is required. MaybeLocal AllocateUnsafeArrayBuffer(Environment* env, @@ -1917,10 +1918,10 @@ void ArrayBufferAlignedOffset(const FunctionCallbackInfo& args) { } template -uint32_t WriteOneByteString(const char* src, - uint32_t src_len, - char* dst, - uint32_t dst_len) { +size_t WriteOneByteString(const char* src, + size_t src_len, + char* dst, + size_t dst_len) { if (dst_len == 0) { return 0; } @@ -1967,7 +1968,7 @@ void SlowWriteString(const FunctionCallbackInfo& args) { if (max_length == 0) return args.GetReturnValue().Set(0); - uint32_t written = 0; + size_t written = 0; if ((encoding == UTF8 || encoding == LATIN1 || encoding == ASCII) && str->IsExternalOneByte()) { @@ -1983,13 +1984,16 @@ void SlowWriteString(const FunctionCallbackInfo& args) { } template -uint32_t FastWriteString(Local receiver, - Local dst_obj, - const FastOneByteString& src, - uint32_t offset, - uint32_t max_length, - // NOLINTNEXTLINE(runtime/references) - FastApiCallbackOptions& options) { +double FastWriteString(Local receiver, + Local dst_obj, + const FastOneByteString& src, + // Offsets and lengths can exceed uint32 for buffers + // larger than 4 GiB, so they are passed as doubles + // (exact for integers < 2^53). + double offset, + double max_length, + // NOLINTNEXTLINE(runtime/references) + FastApiCallbackOptions& options) { // Just a heads up... this is a v8 fast api function. The use of // FastOneByteString has some caveats. Specifically, a GC occurring // between the time the FastOneByteString is created and the time @@ -2000,15 +2004,15 @@ uint32_t FastWriteString(Local receiver, // modifying this code to ensure that no operations would trigger a GC. HandleScope handle_scope(options.isolate); SPREAD_BUFFER_ARG(dst_obj, dst); - CHECK(offset <= dst_length); - CHECK(dst_length - offset <= std::numeric_limits::max()); + const size_t start = static_cast(offset); + CHECK(start <= dst_length); TRACK_V8_FAST_API_CALL("buffer.writeString"); - return WriteOneByteString( + return static_cast(WriteOneByteString( src.data, src.length, - reinterpret_cast(dst_data + offset), - std::min(dst_length - offset, max_length)); + reinterpret_cast(dst_data + start), + std::min(dst_length - start, static_cast(max_length)))); } static const CFunction fast_write_string_ascii(