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 ZSTD_CCtx *cctx = NULL;
28 static ZSTD_DCtx *dctx = NULL;
29 static uint8_t* cBuf = NULL;
30 static uint8_t* rBuf = NULL;
31 static size_t bufSize = 0;
33 static ZSTD_outBuffer makeOutBuffer(uint8_t *dst, size_t capacity,
34 FUZZ_dataProducer_t *producer)
36 ZSTD_outBuffer buffer = { dst, 0, 0 };
38 FUZZ_ASSERT(capacity > 0);
39 buffer.size = (FUZZ_dataProducer_uint32Range(producer, 1, capacity));
40 FUZZ_ASSERT(buffer.size <= capacity);
45 static ZSTD_inBuffer makeInBuffer(const uint8_t **src, size_t *size,
46 FUZZ_dataProducer_t *producer)
48 ZSTD_inBuffer buffer = { *src, 0, 0 };
50 FUZZ_ASSERT(*size > 0);
51 buffer.size = (FUZZ_dataProducer_uint32Range(producer, 1, *size));
52 FUZZ_ASSERT(buffer.size <= *size);
59 static size_t compress(uint8_t *dst, size_t capacity,
60 const uint8_t *src, size_t srcSize,
61 FUZZ_dataProducer_t *producer)
64 ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
65 FUZZ_setRandomParameters(cctx, srcSize, producer);
67 FUZZ_ZASSERT(ZSTD_CCtx_getParameter(cctx, ZSTD_c_maxBlockSize, &maxBlockSize));
70 ZSTD_inBuffer in = makeInBuffer(&src, &srcSize, producer);
71 /* Mode controls the action. If mode == -1 we pick a new mode */
73 while (in.pos < in.size || mode != -1) {
74 ZSTD_outBuffer out = makeOutBuffer(dst, capacity, producer);
75 /* Previous action finished, pick a new mode. */
76 if (mode == -1) mode = FUZZ_dataProducer_uint32Range(producer, 0, 9);
78 case 0: /* fall-through */
79 case 1: /* fall-through */
82 ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_flush);
90 ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
92 /* Reset the compressor when the frame is finished */
94 ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only);
95 if (FUZZ_dataProducer_uint32Range(producer, 0, 7) == 0) {
96 size_t const remaining = in.size - in.pos;
97 FUZZ_setRandomParameters(cctx, remaining, producer);
98 /* Always use the same maxBlockSize */
99 FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_maxBlockSize, maxBlockSize));
106 ZSTD_inBuffer nullIn = { NULL, 0, 0 };
107 ZSTD_outBuffer nullOut = { NULL, 0, 0 };
108 size_t const ret = ZSTD_compressStream2(cctx, &nullOut, &nullIn, ZSTD_e_continue);
114 ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_continue);
125 ZSTD_inBuffer in = {NULL, 0, 0};
126 ZSTD_outBuffer out = makeOutBuffer(dst, capacity, producer);
127 size_t const ret = ZSTD_compressStream2(cctx, &out, &in, ZSTD_e_end);
139 static size_t decompress(void* dst, size_t dstCapacity, void const* src, size_t srcSize, FUZZ_dataProducer_t* producer)
141 ZSTD_inBuffer in = {src, srcSize, 0};
142 ZSTD_outBuffer out = {dst, dstCapacity, 0};
144 FUZZ_ZASSERT(ZSTD_CCtx_getParameter(cctx, ZSTD_c_maxBlockSize, &maxBlockSize));
145 if (FUZZ_dataProducer_uint32Range(producer, 0, 1)) {
146 FUZZ_ZASSERT(ZSTD_DCtx_setParameter(dctx, ZSTD_d_maxBlockSize, maxBlockSize));
148 while (in.pos < in.size) {
149 size_t const ret = ZSTD_decompressStream(dctx, &out, &in);
151 FUZZ_ASSERT(ret == 0);
156 int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
158 FUZZ_SEQ_PROD_SETUP();
159 size_t neededBufSize;
161 /* Give a random portion of src data to the producer, to use for
162 parameter generation. The rest will be used for (de)compression */
163 FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
164 size = FUZZ_dataProducer_reserveDataPrefix(producer);
166 neededBufSize = ZSTD_compressBound(size) * 15;
168 /* Allocate all buffers and contexts if not already allocated */
169 if (neededBufSize > bufSize) {
172 cBuf = (uint8_t*)FUZZ_malloc(neededBufSize);
173 rBuf = (uint8_t*)FUZZ_malloc(neededBufSize);
174 bufSize = neededBufSize;
177 cctx = ZSTD_createCCtx();
181 dctx = ZSTD_createDCtx();
186 size_t const cSize = compress(cBuf, neededBufSize, src, size, producer);
187 size_t const rSize = decompress(rBuf, neededBufSize, cBuf, cSize, producer);
189 FUZZ_ASSERT_MSG(rSize == size, "Incorrect regenerated size");
190 FUZZ_ASSERT_MSG(!FUZZ_memcmp(src, rBuf, size), "Corruption!");
192 /* Test in-place decompression (note the macro doesn't work in this case) */
194 size_t const margin = ZSTD_decompressionMargin(cBuf, cSize);
195 size_t const outputSize = size + margin;
196 char* const output = (char*)FUZZ_malloc(outputSize);
197 char* const input = output + outputSize - cSize;
199 FUZZ_ASSERT(outputSize >= cSize);
200 memcpy(input, cBuf, cSize);
202 dSize = ZSTD_decompressDCtx(dctx, output, outputSize, input, cSize);
204 FUZZ_ASSERT_MSG(dSize == size, "Incorrect regenerated size");
205 FUZZ_ASSERT_MSG(!FUZZ_memcmp(src, output, size), "Corruption!");
211 FUZZ_dataProducer_free(producer);
212 #ifndef STATEFUL_FUZZING
213 ZSTD_freeCCtx(cctx); cctx = NULL;
214 ZSTD_freeDCtx(dctx); dctx = NULL;
216 FUZZ_SEQ_PROD_TEARDOWN();