From c6636afbbb0d545347b17565b007195efaa3764e Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Mon, 11 May 2020 18:53:32 -0400 Subject: [PATCH] Fix ZSTD_estimateCCtxSize() Under ASAN `ZSTD_estimateCCtxSize()` provides estimates for one-shot compression, which is guaranteed not to buffer inputs or outputs. So it ignores the sizes of the buffers, assuming they'll be zero. However, the actual workspace allocation logic always allocates those buffers, and when running under ASAN, the workspace surrounds every allocation with 256 bytes of redzone. So the 0-sized buffers end up consuming 512 bytes of space, which is accounted for in the actual allocation path through the use of `ZSTD_cwksp_alloc_size()` but isn't in the estimation path, since it ignores the buffers entirely. This commit fixes this. --- lib/compress/zstd_compress.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index ff8a00e6b..538f8e9e2 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -1144,13 +1144,26 @@ size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params) size_t const ldmSpace = ZSTD_ldm_getTableSize(params->ldmParams); size_t const ldmSeqSpace = ZSTD_cwksp_alloc_size(ZSTD_ldm_getMaxNbSeq(params->ldmParams, blockSize) * sizeof(rawSeq)); - size_t const neededSpace = entropySpace + blockStateSpace + tokenSpace + - matchStateSize + ldmSpace + ldmSeqSpace; + /* estimateCCtxSize is for one-shot compression. So no buffers should + * be needed. However, we still allocate two 0-sized buffers, which can + * take space under ASAN. */ + size_t const bufferSpace = ZSTD_cwksp_alloc_size(0) + + ZSTD_cwksp_alloc_size(0); + size_t const cctxSpace = ZSTD_cwksp_alloc_size(sizeof(ZSTD_CCtx)); - DEBUGLOG(5, "sizeof(ZSTD_CCtx) : %u", (U32)cctxSpace); + size_t const neededSpace = + cctxSpace + + entropySpace + + blockStateSpace + + ldmSpace + + ldmSeqSpace + + matchStateSize + + tokenSpace + + bufferSpace; + DEBUGLOG(5, "estimate workspace : %u", (U32)neededSpace); - return cctxSpace + neededSpace; + return neededSpace; } } -- 2.47.2