2 * Copyright (c) Meta Platforms, Inc. and affiliates.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
12 * This fuzz target performs a zstd round-trip test (compress & decompress),
13 * compares the result with the original, and calls abort() on corruption.
16 #define ZSTD_STATIC_LINKING_ONLY
22 #include "fuzz_helpers.h"
23 #include "zstd_helpers.h"
24 #include "fuzz_data_producer.h"
25 #include "fuzz_third_party_seq_prod.h"
27 static ZSTD_CCtx *cctx = NULL;
28 static ZSTD_DCtx *dctx = NULL;
30 static size_t getDecompressionMargin(void const* compressed, size_t cSize, size_t srcSize, int hasSmallBlocks)
32 size_t margin = ZSTD_decompressionMargin(compressed, cSize);
33 if (!hasSmallBlocks) {
34 /* The macro should be correct in this case, but it may be smaller
35 * because of e.g. block splitting, so take the smaller of the two.
39 FUZZ_ZASSERT(ZSTD_getFrameHeader(&zfh, compressed, cSize));
40 marginM = ZSTD_DECOMPRESSION_MARGIN(srcSize, zfh.blockSizeMax);
47 static size_t roundTripTest(void *result, size_t resultCapacity,
48 void *compressed, size_t compressedCapacity,
49 const void *src, size_t srcSize,
50 FUZZ_dataProducer_t *producer)
54 int targetCBlockSize = 0;
55 if (FUZZ_dataProducer_uint32Range(producer, 0, 1)) {
56 size_t const remainingBytes = FUZZ_dataProducer_remainingBytes(producer);
57 FUZZ_setRandomParameters(cctx, srcSize, producer);
58 cSize = ZSTD_compress2(cctx, compressed, compressedCapacity, src, srcSize);
60 FUZZ_ZASSERT(ZSTD_CCtx_getParameter(cctx, ZSTD_c_targetCBlockSize, &targetCBlockSize));
61 // Compress a second time and check for determinism
63 size_t const cSize0 = cSize;
64 XXH64_hash_t const hash0 = XXH64(compressed, cSize, 0);
65 FUZZ_dataProducer_rollBack(producer, remainingBytes);
66 FUZZ_setRandomParameters(cctx, srcSize, producer);
67 cSize = ZSTD_compress2(cctx, compressed, compressedCapacity, src, srcSize);
68 FUZZ_ASSERT(cSize == cSize0);
69 FUZZ_ASSERT(XXH64(compressed, cSize, 0) == hash0);
72 int const cLevel = FUZZ_dataProducer_int32Range(producer, kMinClevel, kMaxClevel);
73 cSize = ZSTD_compressCCtx(
74 cctx, compressed, compressedCapacity, src, srcSize, cLevel);
76 // Compress a second time and check for determinism
78 size_t const cSize0 = cSize;
79 XXH64_hash_t const hash0 = XXH64(compressed, cSize, 0);
80 cSize = ZSTD_compressCCtx(
81 cctx, compressed, compressedCapacity, src, srcSize, cLevel);
82 FUZZ_ASSERT(cSize == cSize0);
83 FUZZ_ASSERT(XXH64(compressed, cSize, 0) == hash0);
86 dSize = ZSTD_decompressDCtx(dctx, result, resultCapacity, compressed, cSize);
88 FUZZ_ASSERT_MSG(dSize == srcSize, "Incorrect regenerated size");
89 FUZZ_ASSERT_MSG(!FUZZ_memcmp(src, result, dSize), "Corruption!");
92 size_t margin = getDecompressionMargin(compressed, cSize, srcSize, targetCBlockSize);
93 size_t const outputSize = srcSize + margin;
94 char* const output = (char*)FUZZ_malloc(outputSize);
95 char* const input = output + outputSize - cSize;
96 FUZZ_ASSERT(outputSize >= cSize);
97 memcpy(input, compressed, cSize);
99 dSize = ZSTD_decompressDCtx(dctx, output, outputSize, input, cSize);
101 FUZZ_ASSERT_MSG(dSize == srcSize, "Incorrect regenerated size");
102 FUZZ_ASSERT_MSG(!FUZZ_memcmp(src, output, srcSize), "Corruption!");
107 /* When superblock is enabled make sure we don't expand the block more than expected.
108 * NOTE: This test is currently disabled because superblock mode can arbitrarily
109 * expand the block in the worst case. Once superblock mode has been improved we can
110 * re-enable this test.
112 if (0 && targetCBlockSize != 0) {
114 FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetCBlockSize, 0));
115 normalCSize = ZSTD_compress2(cctx, compressed, compressedCapacity, src, srcSize);
116 FUZZ_ZASSERT(normalCSize);
118 size_t const bytesPerBlock = 3 /* block header */
119 + 5 /* Literal header */
120 + 6 /* Huffman jump table */
121 + 3 /* number of sequences */
122 + 1 /* symbol compression modes */;
123 size_t const expectedExpansion = bytesPerBlock * (1 + (normalCSize / MAX(1, targetCBlockSize)));
124 size_t const allowedExpansion = (srcSize >> 3) + 5 * expectedExpansion + 10;
125 FUZZ_ASSERT(cSize <= normalCSize + allowedExpansion);
131 int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
133 FUZZ_SEQ_PROD_SETUP();
135 size_t const rBufSize = size;
136 void* rBuf = FUZZ_malloc(rBufSize);
137 size_t cBufSize = ZSTD_compressBound(size);
140 /* Give a random portion of src data to the producer, to use for
141 parameter generation. The rest will be used for (de)compression */
142 FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
143 size = FUZZ_dataProducer_reserveDataPrefix(producer);
145 /* Half of the time fuzz with a 1 byte smaller output size.
146 * This will still succeed because we don't use a dictionary, so the dictID
147 * field is empty, giving us 4 bytes of overhead.
149 cBufSize -= FUZZ_dataProducer_uint32Range(producer, 0, 1);
151 cBuf = FUZZ_malloc(cBufSize);
154 cctx = ZSTD_createCCtx();
158 dctx = ZSTD_createDCtx();
162 roundTripTest(rBuf, rBufSize, cBuf, cBufSize, src, size, producer);
165 FUZZ_dataProducer_free(producer);
166 #ifndef STATEFUL_FUZZING
167 ZSTD_freeCCtx(cctx); cctx = NULL;
168 ZSTD_freeDCtx(dctx); dctx = NULL;
170 FUZZ_SEQ_PROD_TEARDOWN();