-
Notifications
You must be signed in to change notification settings - Fork 626
node:zlib: account Zstd native allocations against external memory #6210
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
Closed
Changes from all commits
Commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -812,10 +812,19 @@ void zstdFreeDCtx(ZSTD_DCtx* dctx) { | |||||
| } // namespace | ||||||
|
|
||||||
| ZstdEncoderContext::ZstdEncoderContext(ZlibMode _mode) | ||||||
| : ZstdContext(_mode), | ||||||
| cctx_(kj::disposeWith<zstdFreeCCtx>(ZSTD_createCCtx())) {} | ||||||
| : ZstdContext(_mode) {} | ||||||
|
|
||||||
| kj::Maybe<CompressionError> ZstdEncoderContext::initialize(uint64_t pledgedSrcSize) { | ||||||
| KJ_DASSERT(allocator_ != nullptr, "Zstd encoder allocator should not be null"); | ||||||
| if (cctx_.get() == nullptr) { | ||||||
| ZSTD_customMem customMem = { | ||||||
| .customAlloc = CompressionAllocator::AllocForBrotli, | ||||||
| .customFree = CompressionAllocator::FreeForZlib, | ||||||
| .opaque = allocator_, | ||||||
| }; | ||||||
| cctx_ = kj::disposeWith<zstdFreeCCtx>(ZSTD_createCCtx_advanced(customMem)); | ||||||
| } | ||||||
|
|
||||||
| if (cctx_.get() == nullptr) { | ||||||
| return CompressionError( | ||||||
| "Could not initialize Zstd instance"_kj, "ERR_ZLIB_INITIALIZATION_FAILED"_kj, -1); | ||||||
|
|
@@ -878,12 +887,19 @@ kj::Maybe<CompressionError> ZstdEncoderContext::getError() const { | |||||
| } | ||||||
|
|
||||||
| ZstdDecoderContext::ZstdDecoderContext(ZlibMode _mode) | ||||||
| : ZstdContext(_mode), | ||||||
| dctx_(kj::disposeWith<zstdFreeDCtx>(ZSTD_createDCtx())) {} | ||||||
| : ZstdContext(_mode) {} | ||||||
|
|
||||||
| kj::Maybe<CompressionError> ZstdDecoderContext::initialize() { | ||||||
| // dctx_ is created in the constructor. It can only be nullptr if ZSTD_createDCtx() | ||||||
| // failed due to memory allocation failure. | ||||||
| KJ_DASSERT(allocator_ != nullptr, "Zstd decoder allocator should not be null"); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue as the encoder path —
Suggested change
|
||||||
| if (dctx_.get() == nullptr) { | ||||||
| ZSTD_customMem customMem = { | ||||||
| .customAlloc = CompressionAllocator::AllocForBrotli, | ||||||
| .customFree = CompressionAllocator::FreeForZlib, | ||||||
| .opaque = allocator_, | ||||||
| }; | ||||||
| dctx_ = kj::disposeWith<zstdFreeDCtx>(ZSTD_createDCtx_advanced(customMem)); | ||||||
| } | ||||||
|
|
||||||
| if (dctx_.get() == nullptr) { | ||||||
| return CompressionError( | ||||||
| "Could not initialize Zstd instance"_kj, "ERR_ZLIB_INITIALIZATION_FAILED"_kj, -1); | ||||||
|
|
@@ -957,6 +973,7 @@ bool ZlibUtil::ZstdCompressionStream<CompressionContext>::initialize(jsg::Lock& | |||||
| jsg::Function<void()> writeCallback, | ||||||
| jsg::Optional<uint64_t> pledgedSrcSize) { | ||||||
| this->initializeStream(kj::mv(writeResult), kj::mv(writeCallback)); | ||||||
| this->context()->setAllocator(&this->allocator); | ||||||
|
|
||||||
| uint64_t srcSize = pledgedSrcSize.orDefault(ZSTD_CONTENTSIZE_UNKNOWN); | ||||||
|
|
||||||
|
|
@@ -1168,7 +1185,9 @@ void ZlibUtil::brotliWithCallback( | |||||
|
|
||||||
| template <typename Context> | ||||||
| kj::Array<kj::byte> ZlibUtil::zstdSync(jsg::Lock& js, InputSource data, ZstdContext::Options opts) { | ||||||
| CompressionAllocator allocator(js.getExternalMemoryTarget()); | ||||||
| Context ctx(Context::Mode); | ||||||
| ctx.setAllocator(&allocator); | ||||||
|
|
||||||
| auto chunkSize = opts.chunkSize.orDefault(ZLIB_PERFORMANT_CHUNK_SIZE); | ||||||
| auto maxOutputLength = opts.maxOutputLength.orDefault(Z_MAX_CHUNK); | ||||||
|
|
||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
KJ_DASSERTis stripped in release builds. Ifallocator_is ever null here (e.g. a future caller forgetssetAllocator), this will silently pass through and crash inside the ZSTD allocation callback when it dereferences the null opaque pointer. UseKJ_ASSERTso the invariant is enforced in all build modes: