Repository navigation
zlib: fix crash when async buffers are detached or resized - #67603
ShogunPanda wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #67603 +/- ##
==========================================
- Coverage 90.73% 90.58% -0.15%
==========================================
Files 801 802 +1
Lines 280794 287117 +6323
Branches 54202 55440 +1238
==========================================
+ Hits 254771 260086 +5315
- Misses 16202 17111 +909
- Partials 9821 9920 +99
🚀 New features to boost your workflow:
|
|
Seems some lines are not covered by tests |
| // If the input/output buffer was detached or resized while the operation | ||
| // was in flight, the native bindings throw ERR_OUT_OF_RANGE. That must be | ||
| // surfaced as a stream error rather than an uncaught exception. | ||
| const reprocess = () => { |
There was a problem hiding this comment.
can you avoid to allocate this closure?
There was a problem hiding this comment.
Done. The closure is gone: the retry now goes through a top-level writeRemainingInput(self, handle) helper that wraps handle.write() in the try/catch, so nothing is allocated per pass.
| reprocess(); | ||
| } else { | ||
| const oldRead = self._read; | ||
| self._read = (n) => { |
There was a problem hiding this comment.
This branch isn't new: it's the existing backpressure path from 9052ef4 ("zlib: pause stream if outgoing buffer is full"). The diff only made it look new because the write moved into a helper. When push() returns false we defer the next pass until the consumer reads again, so a highly compressible input can't grow the readable buffer without bound. The only change is that the deferred write now goes through the same helper, so an input that was detached or resized while the stream was paused is reported as a stream error. I added a comment there, and a test (test-zlib-arraybuffer-detach-reprocess.js) that hits this deferred path and checks the error comes from it.
Asynchronous zlib, brotli and zstd operations run on the libuv threadpool, where the worker thread reads and writes through raw pointers into JavaScript-owned backing stores. Detaching or resizing the input or output ArrayBuffer while the operation is in flight frees or decommits the backing store the worker is still accessing, which crashes the process. Pin non-resizable input and output buffers with a std::shared_ptr<v8::BackingStore> for the duration of the operation, and copy resizable buffers into engine-owned memory because resize() decommits pages even when a backing store is pinned. Surface the resulting "buffer invalidated" errors as stream errors rather than uncaught exceptions. Assisted-By: OpenAI GPT-5.6 Sol <openai/gpt-5.6-sol> Signed-off-by: Matteo Collina <hello@matteocollina.com> Signed-off-by: Paolo Insogna <paolo@cowtech.it>
2bd9d84 to
8475775
Compare
|
Added |
Asynchronous zlib, brotli and zstd operations run on the libuv threadpool, where the worker thread accesses JavaScript-owned backing stores through raw pointers. If the input or output
ArrayBufferis detached (for example withArrayBuffer.prototype.transfer()or a transferredpostMessage()) or resized (ArrayBuffer.prototype.resize()) while the operation is in flight, the worker reads or writes invalid memory and the process crashes.Non-resizable input and output buffers are now pinned with a
std::shared_ptr<v8::BackingStore>for the duration of the operation, and resizable buffers are copied into engine-owned memory, becauseresize()decommits pages even when a backing store is pinned. The resulting "buffer invalidated" errors are surfaced as stream errors rather than uncaught exceptions. The documentation now states that these buffers must not be detached or resized until the operation completes.