Repository navigation
buffer: support buffers over 4 GiB in string writes - #67987
Open
rajendra0000 wants to merge 2 commits into
Open
rajendra0000 wants to merge 2 commits into
rajendra0000 wants to merge 2 commits into
Conversation
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>
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: nodejs#55422 Signed-off-by: Rajendra Singh Asoliya <121512381+rajendra0000@users.noreply.github.com>
Contributor
|
Welcome to Node.js, and thank you for your first contribution! Before review, please take a moment to read:
Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Writing a string into a Buffer bigger than 4 GiB crashes the process - but only once V8 optimizes the call and switches to the fast API path. Reading into a BYOB view at an offset past 4 GiB crashes the same way.
Reason: the bindings take offset and length as uint32_t. On the fast path V8 wraps a larger double into uint32_t, so the offset wraps and
CHECK(dst_length - offset <= UINT32_MAX)kills the process. Before optimization the same call returns fine, which is why it only shows up after a lot of iterations. Just removing the CHECK would be worse: the write would silently land at the wrong address.Repro:
Aborts on current main with
Assertion failed: dst_length - offset <= std::numeric_limits<uint32_t>::max().The fix passes offsets and lengths as doubles across the fast/slow API boundary and uses size_t internally - the same pattern #63828 used for Buffer.prototype.copy:
Tests go in test/pummel since they need >4 GiB allocations; they skip on 32-bit and on allocation failure:
Build and tests: Linux x64, gcc,
--error-on-warn, running in CI in my fork. Before it, both tests abort. After it, both pass, along with parallel/test-buffer-, parallel/test-whatwg-readablebytestream, parallel/test-stream-* and pummel/test-buffer-* (325 tests). Logs: https://github.com/rajendra0000/node/actions/runs/47101442812 (fix) and https://github.com/rajendra0000/node/actions/runs/47101441759 (tests only, aborting).I used an AI coding assistant to help investigate and draft this change. The builds and tests above ran in CI in my own fork, and I reviewed the results before submitting.
Refs: #55422