Skip to content
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

[fuzz] Superblock fuzz issues #1891

Merged
merged 15 commits into from
Dec 10, 2019
Merged
112 changes: 60 additions & 52 deletions lib/compress/zstd_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -2450,65 +2450,73 @@ static size_t ZSTD_compressBlock_internal(ZSTD_CCtx* zc,
return cSize;
}

static void ZSTD_confirmRepcodesAndEntropyTables(ZSTD_CCtx* zc)
{
ZSTD_compressedBlockState_t* const tmp = zc->blockState.prevCBlock;
zc->blockState.prevCBlock = zc->blockState.nextCBlock;
zc->blockState.nextCBlock = tmp;
}

static size_t ZSTD_compressBlock_targetCBlockSize_body(ZSTD_CCtx* zc,
const size_t bss, void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
U32 lastBlock)
{
/* Attempt superblock compression and return early if successful */
{
bimbashrestha marked this conversation as resolved.
Show resolved Hide resolved
if (bss == ZSTDbss_compress) {
size_t cSize = ZSTD_compressSuperBlock(zc, dst, dstCapacity, lastBlock);
bimbashrestha marked this conversation as resolved.
Show resolved Hide resolved
FORWARD_IF_ERROR(cSize);
if (cSize != 0) {
ZSTD_confirmRepcodesAndEntropyTables(zc);
return cSize;
}
}
}

/* Superblock compression failed, attempt to emit noCompress superblocks
* and return early if that is successful and we have enough room for checksum */
{
size_t cSize = ZSTD_noCompressSuperBlock(dst, dstCapacity, src, srcSize, zc->appliedParams.targetCBlockSize, lastBlock);
if (cSize != ERROR(dstSize_tooSmall) && (dstCapacity - cSize) >= 4)
return cSize;
bimbashrestha marked this conversation as resolved.
Show resolved Hide resolved
}

/* noCompress superblock emission failed. Attempt to compress normally
* and return early if that is successful */
{
size_t cSize = ZSTD_compressSequences(&zc->seqStore,
&zc->blockState.prevCBlock->entropy, &zc->blockState.nextCBlock->entropy,
&zc->appliedParams, (BYTE*)dst+ZSTD_blockHeaderSize, dstCapacity-ZSTD_blockHeaderSize,
srcSize, zc->entropyWorkspace, HUF_WORKSPACE_SIZE, zc->bmi2);
FORWARD_IF_ERROR(cSize);
if (cSize != 0) {
U32 const cBlockHeader24 = lastBlock + (((U32)bt_compressed)<<1) + (U32)(cSize << 3);
MEM_writeLE24((BYTE*)dst, cBlockHeader24);
cSize += ZSTD_blockHeaderSize;
ZSTD_confirmRepcodesAndEntropyTables(zc);
return cSize;
}
}

/* Everything failed. Just emit a regular noCompress block */
return ZSTD_noCompressBlock(dst, dstCapacity, src, srcSize, lastBlock);
}

static size_t ZSTD_compressBlock_targetCBlockSize(ZSTD_CCtx* zc,
void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
U32 lastBlock) {
U32 lastBlock)
{
size_t cSize = 0;
const size_t bss = ZSTD_buildSeqStore(zc, src, srcSize);
DEBUGLOG(5, "ZSTD_compressBlock_targetCBlockSize (dstCapacity=%u, dictLimit=%u, nextToUpdate=%u, srcSize=%zu)",
(unsigned)dstCapacity, (unsigned)zc->blockState.matchState.window.dictLimit, (unsigned)zc->blockState.matchState.nextToUpdate, srcSize);
FORWARD_IF_ERROR(bss);

{ const size_t bss = ZSTD_buildSeqStore(zc, src, srcSize);
FORWARD_IF_ERROR(bss);
if (bss == ZSTDbss_compress) {
cSize = ZSTD_compressSuperBlock(zc, dst, dstCapacity, lastBlock);
} }

/* Superblock compression may fail, in which case
* encode using ZSTD_noCompressSuperBlock writing sub blocks
* in uncompressed mode.
*/
if (cSize == 0) {
cSize = ZSTD_noCompressSuperBlock(dst, dstCapacity, src, srcSize, zc->appliedParams.targetCBlockSize, lastBlock);
/* In compression, there is an assumption that a compressed block is always
* within the size of ZSTD_compressBound(). However, SuperBlock compression
* can exceed the limit due to overhead of headers from SubBlocks.
* This breaks in streaming mode where output buffer in compress context is
* allocated ZSTD_compressBound() amount of memory, which may not be big
* enough for SuperBlock compression.
* In such case, fall back to normal compression. This is possible because
* targetCBlockSize is best effort not a guarantee. */
if (cSize != ERROR(dstSize_tooSmall)) return cSize;
else {
BYTE* const ostart = (BYTE*)dst;
/* If ZSTD_noCompressSuperBlock fails with dstSize_tooSmall,
* compress normally.
*/
cSize = ZSTD_compressSequences(&zc->seqStore,
&zc->blockState.prevCBlock->entropy, &zc->blockState.nextCBlock->entropy,
&zc->appliedParams,
ostart+ZSTD_blockHeaderSize, dstCapacity-ZSTD_blockHeaderSize,
srcSize,
zc->entropyWorkspace, HUF_WORKSPACE_SIZE /* statically allocated in resetCCtx */,
zc->bmi2);
if (!ZSTD_isError(cSize) && cSize != 0) {
U32 const cBlockHeader24 = lastBlock + (((U32)bt_compressed)<<1) + (U32)(cSize << 3);
MEM_writeLE24(ostart, cBlockHeader24);
cSize += ZSTD_blockHeaderSize;
}
}
}
cSize = ZSTD_compressBlock_targetCBlockSize_body(zc, bss, dst, dstCapacity, src, srcSize, lastBlock);
bimbashrestha marked this conversation as resolved.
Show resolved Hide resolved
FORWARD_IF_ERROR(cSize);

if (!ZSTD_isError(cSize) && cSize != 0) {
/* confirm repcodes and entropy tables when emitting a compressed block */
ZSTD_compressedBlockState_t* const tmp = zc->blockState.prevCBlock;
zc->blockState.prevCBlock = zc->blockState.nextCBlock;
zc->blockState.nextCBlock = tmp;
}
/* We check that dictionaries have offset codes available for the first
* block. After the first block, the offcode table might not have large
* enough codes to represent the offsets in the data.
*/
if (zc->blockState.prevCBlock->entropy.fse.offcode_repeatMode == FSE_repeat_valid)
zc->blockState.prevCBlock->entropy.fse.offcode_repeatMode = FSE_repeat_check;

Expand Down Expand Up @@ -2853,7 +2861,7 @@ static size_t ZSTD_checkDictNCount(short* normalizedCounter, unsigned dictMaxSym

size_t ZSTD_loadCEntropy(ZSTD_compressedBlockState_t* bs, void* workspace,
short* offcodeNCount, unsigned* offcodeMaxValue,
const void* const dict, size_t dictSize)
const void* const dict, size_t dictSize)
{
const BYTE* dictPtr = (const BYTE*)dict; /* skip magic num and dict ID */
const BYTE* const dictEnd = dictPtr + dictSize;
Expand Down
14 changes: 13 additions & 1 deletion tests/fuzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ static int basicUnitTests(U32 const seed, double compressibility)
} }
DISPLAYLEVEL(3, "OK \n");


DISPLAYLEVEL(3, "test%3i : decompress with null dict : ", testNb++);
{ ZSTD_DCtx* const dctx = ZSTD_createDCtx(); assert(dctx != NULL);
{ size_t const r = ZSTD_decompress_usingDict(dctx,
Expand Down Expand Up @@ -490,6 +489,19 @@ static int basicUnitTests(U32 const seed, double compressibility)
}
DISPLAYLEVEL(3, "OK \n");

DISPLAYLEVEL(3, "test%3d: superblock enough room for checksum : ", testNb++)
{
/* This tests whether or not we leave enough room for the checksum at the end
* of the dst buffer. The bug that motivated this test was found by the
* stream_round_trip fuzzer but this crashes for the same reason and is
* far more compact than re-creating the stream_round_trip fuzzer's code path */
ZSTD_CCtx *cctx = ZSTD_createCCtx();
ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetCBlockSize, 64);
assert(!ZSTD_isError(ZSTD_compress2(cctx, compressedBuffer, 1339, CNBuffer, 1278)));
ZSTD_freeCCtx(cctx);
}
DISPLAYLEVEL(3, "OK \n");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked that this fails before this patch.


DISPLAYLEVEL(3, "test%3i : compress a NULL input with each level : ", testNb++);
{ int level = -1;
ZSTD_CCtx* cctx = ZSTD_createCCtx();
Expand Down