Skip to content

zlib: fix crash when async buffers are detached or resized - #67603

Open
ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:zlib-pin-async-buffers
Open

ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:zlib-pin-async-buffers

Conversation

@ShogunPanda

Copy link
Copy Markdown
Contributor

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 ArrayBuffer is detached (for example with ArrayBuffer.prototype.transfer() or a transferred postMessage()) 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, because resize() 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.

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>
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. zlib Issues and PRs related to the zlib module and its compression dependencies. labels Nov 19, 2026
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.31868% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.70%. Comparing base (cdd0687) to head (2bd9d84).
⚠️ Report is 30 commits behind head on main.

Files with missing lines Patch % Lines
src/node_zlib.cc 82.75% 2 Missing and 8 partials ⚠️
lib/zlib.js 78.78% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67603      +/-   ##
==========================================
- Coverage   90.73%   90.70%   -0.03%     
==========================================
  Files         801      801              
  Lines      280794   280869      +75     
  Branches    54202    54218      +16     
==========================================
- Hits       254771   254757      -14     
- Misses      16202    16268      +66     
- Partials     9821     9844      +23     
Files with missing lines Coverage Δ
lib/zlib.js 97.94% <78.78%> (-0.48%) ⬇️
src/node_zlib.cc 80.85% <82.75%> (+0.12%) ⬆️

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. zlib Issues and PRs related to the zlib module and its compression dependencies.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants