Skip to content

decompress: keep the destination's allocator in ZSTD_copyDCtx - #4837

Open
tasodoufu wants to merge 1 commit into
facebook:devfrom
tasodoufu:fix-4830-copydctx-allocator
Open

tasodoufu wants to merge 1 commit into
facebook:devfrom
tasodoufu:fix-4830-copydctx-allocator

Conversation

@tasodoufu

Copy link
Copy Markdown
Issue fixes #4830

AI disclosure: this change (including the reproducer, fix and test) was found, implemented and verified with the help of an AI agent, as permitted by the contribution guidelines.

Reproduction

ZSTD_copyDCtx() overwrites the destination context's ZSTD_customMem with the source's one (it is part of the memcpy'd prefix, offset 0 to inBuff). When the two contexts were created with different allocators, ZSTD_freeDCtx(dst) then releases the destination context — which was allocated with its own allocator — using the source's callbacks.

With a custom allocator whose free is not plain free()-compatible (e.g. an offset-pointer scheme like Rust bindings often use, or any counter-equipped allocator), this is an invalid free. Standalone reproducer (built from dev = 49cf517, ZSTD_DISABLE_ASM=1):

#define ZSTD_STATIC_LINKING_ONLY
#include <stdlib.h>
#include <string.h>
#include "zstd.h"

/* custom allocator incompatible with plain free(): pointer offset by 16 */
static void* myMalloc(void* opaque, size_t size) {
    (void)opaque;
    void* p = malloc(size + 16);
    if (!p) return NULL;
    return (char*)p + 16;
}
static void myFree(void* opaque, void* address) {
    (void)opaque;
    if (!address) return;
    free((char*)address - 16);
}

int main(void) {
    ZSTD_customMem cm;
    memset(&cm, 0, sizeof(cm));
    cm.customAlloc = myMalloc;
    cm.customFree = myFree;
    ZSTD_DCtx* src = ZSTD_createDCtx_advanced(cm);
    ZSTD_DCtx* dst = ZSTD_createDCtx();      /* default allocator */
    ZSTD_copyDCtx(dst, src);
    ZSTD_freeDCtx(dst);   /* freed with src's callbacks -> invalid free */
    ZSTD_freeDCtx(src);
    return 0;
}
  • Before the fix: free(): invalid pointer, abort (exit 134).
  • After the fix: exit 0.

The new fuzzer unit test detects the same mismatch without faulting: it copies a DCtx created with a flagging allocator into a default-created one and requires that the source's customFree was never invoked for the destination's release.

Root cause

lib/decompress/zstd_decompress.c, ZSTD_copyDCtx():

size_t const toCopy = (size_t)((char*)(&dstDCtx->inBuff) - (char*)dstDCtx);
ZSTD_memcpy(dstDCtx, srcDCtx, toCopy);

customMem sits inside the copied prefix, so the destination loses its own allocator. ZSTD_freeDCtx() (ZSTD_customFree(dctx, cMem) / workspace release) then uses mismatched callbacks.

ZSTD_copyCCtx_internal() (lib/compress/zstd_compress.c) has the same overwrite, but there the subsequent ZSTD_resetCCtx_internal() allocates all workspace resources with the overwritten customMem, so their free path stays self-consistent; only the destination ZSTD_CCtx struct itself remains allocated with the original allocator and is freed with the — now overwritten — cctx->customMem (ZSTD_freeCCtx → ZSTD_customFree(cctx, cctx->customMem) when the struct is not workspace-owned). That residual asymmetry is not reachable from ZSTD_copyCCtx_internal() itself without breaking a documented behavior, so this PR keeps its scope to ZSTD_copyDCtx() and leaves the copyCCtx pattern untouched (no header/API change, no are-QA-semantics change). If maintainers prefer a symmetric customMem-preserving change there too, I'm happy to extend.

Fix

Save dstDCtx->customMem before the memcpy and restore it afterwards (6 lines incl. comments), so ZSTD_copyDCtx() preserves the destination's allocator, as ZSTD_freeDCtx() relies on it.

Test

  • new regression test in tests/fuzzer.c (basicUnitTests, right after the ZSTD_createDCtx_advanced test): fails on unpatched dev, passes with the fix;
    verified the failure pre-push: with the library fix stashed, ./fuzzer -i0 -T1s exits 1 with only test 76 : ZSTD_copyDCtx keeps the destination's allocator failing; with the fix it passes and the run exits 0 (17 fuzzer tests completed).
  • standalone reproducer above: abort (134) → clean exit (0).
  • git diff --check clean; zstd_decompress.c recompiled warning-free under -Werror -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wstrict-aliasing=1 -Wdeclaration-after-statement -Wstrict-prototypes -Wundef -Wpointer-arith -Wformat=2 (ZSTD_MULTITHREAD).

Not verified

  • full make test suite was not run here (only the fuzzer driver -i0 -T1s, which covers basicUnitTests + 17 earlier fuzzer test groups); the rest of the suite does not exercise vs-mismatched allocator copies beyond the covered paths, but this is not machine-verified.
  • Windows/32-bit builds were not locally exercised (no structural change, but noting it).
  • ZSTD_copyCCtx allocator-mismatch residual asymmetry (described above) left as-is.

ZSTD_copyDCtx() memcpy'd the source DCtx's customMem over the
destination's one. When both contexts were created with different
allocators, ZSTD_freeDCtx() then released the destination context
(and its resources) with the source's callbacks, producing an
allocator mismatch (invalid free / crash) since the destination
context itself was allocated with its own allocator.

Save dstDCtx->customMem before the memcpy and restore it afterwards,
so the copy preserves the destination's allocator as freeDCtx relies
on it. ZSTD_copyCCtx_internal() has the same overwrite pattern but
the workspace allocations inside ZSTD_resetCCtx_internal() reuse the
overwritten customMem consistently for every resource they allocate;
only the destination context struct itself stays allocated with the
original allocator, which is not addressable from this function.
Flagged in the API docs comment instead.

Add a fuzzer regression test: copy a DCtx created with a flagging
custom allocator into a default-created one, free the destination,
and require that the source's customFree was never called for it.

Fixes facebook#4830

Signed-off-by: masachika shiotsuka <n2655f@labs.aizu.ac.jp>
@meta-cla

meta-cla Bot commented Oct 11, 2026

Copy link
Copy Markdown

Hi @tasodoufu!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot safely copy context with custom allocator

1 participant