Repository navigation
node:zlib: free zstd's workers before the dictionary they read #44421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
robobun
wants to merge
6
commits into
main
from
robobun/1d2a0765/zstd-free-workers-before-dictionaries
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1f60c84
node:zlib: free zstd's workers before the dictionary they read
robobun e6523f8
zlib: drop the comment about the removed reset
robobun c757cc6
zlib: drop the comment about the removed reset, and the extern with n…
robobun bd42e3f
build: register the zstd patch in the workaround registry
robobun 781d5d2
build: read the zstd pin through the dependency, keep its line as it was
robobun 3b10e05
ci: retrigger
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| Free the worker threads of a context before the dictionaries they read. | ||
|
|
||
| ZSTD_freeCCtxContent() calls ZSTD_clearAllDicts() and then ZSTDMT_freeCCtx(). | ||
| With nbWorkers >= 1, ZSTD_compressStream2() posts a job to a worker thread and | ||
| returns. The first job of a frame reads the dictionary of the context through | ||
| a pointer that it does not own (job->cdict, the localDict of the CCtx). A job | ||
| stops only in ZSTDMT_freeCCtx(), which joins the threads (POOL_free). | ||
|
|
||
| So ZSTD_freeCCtx() of a context that has a dictionary and a running job frees | ||
| the dictionary under that job: heap-use-after-free in ZSTDMT_compressionJob(). | ||
| destroy() and close() of a node:zlib zstd stream make that call. | ||
|
|
||
| ZSTDMT_freeCCtx() has this order for its own dictionary. It joins the threads | ||
| and then frees mtctx->cdictLocal. | ||
|
|
||
| Not covered: ZSTD_CCtx_reset() with parameters, ZSTD_CCtx_loadDictionary(), | ||
| ZSTD_CCtx_refCDict() and ZSTD_CCtx_refPrefix() on a context whose jobs still | ||
| run. They free the dictionaries too. Bun does not call them on such a context. | ||
|
|
||
| facebook/zstd has the same order on its dev branch (01b7154f11), and no issue | ||
| there reports it (checked 2026-10-01). | ||
|
|
||
| --- a/lib/compress/zstd_compress.c | ||
| +++ b/lib/compress/zstd_compress.c | ||
| @@ -171,10 +171,11 @@ | ||
| { | ||
| assert(cctx != NULL); | ||
| assert(cctx->staticSize == 0); | ||
| - ZSTD_clearAllDicts(cctx); | ||
| #ifdef ZSTD_MULTITHREAD | ||
| ZSTDMT_freeCCtx(cctx->mtctx); cctx->mtctx = NULL; | ||
| #endif | ||
| + /* Bun: after the join above. A job that still runs reads these dictionaries. */ | ||
| + ZSTD_clearAllDicts(cctx); | ||
| ZSTD_cwksp_free(&cctx->workspace, cctx->customMem); | ||
| } | ||
|
|
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.