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) with
13 * a raw content dictionary, compares the result with the original, and calls
14 * abort() on corruption.
21 #include "fuzz_helpers.h"
22 #include "zstd_helpers.h"
23 #include "fuzz_data_producer.h"
24 #include "fuzz_third_party_seq_prod.h"
26 static ZSTD_CCtx *cctx = NULL;
27 static ZSTD_DCtx *dctx = NULL;
29 static size_t roundTripTest(void *result, size_t resultCapacity,
30 void *compressed, size_t compressedCapacity,
31 const void *src, size_t srcSize,
32 const void *dict, size_t dictSize,
33 FUZZ_dataProducer_t *producer)
35 ZSTD_dictContentType_e const dictContentType = ZSTD_dct_rawContent;
36 int const refPrefix = FUZZ_dataProducer_uint32Range(producer, 0, 1) != 0;
39 FUZZ_setRandomParameters(cctx, srcSize, producer);
40 /* Disable checksum so we can use sizes smaller than compress bound. */
41 FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 0));
43 FUZZ_ZASSERT(ZSTD_CCtx_refPrefix_advanced(
45 ZSTD_dct_rawContent));
47 FUZZ_ZASSERT(ZSTD_CCtx_loadDictionary_advanced(
49 (ZSTD_dictLoadMethod_e)FUZZ_dataProducer_uint32Range(producer, 0, 1),
50 ZSTD_dct_rawContent));
51 cSize = ZSTD_compress2(cctx, compressed, compressedCapacity, src, srcSize);
55 FUZZ_ZASSERT(ZSTD_DCtx_refPrefix_advanced(
59 FUZZ_ZASSERT(ZSTD_DCtx_loadDictionary_advanced(
61 (ZSTD_dictLoadMethod_e)FUZZ_dataProducer_uint32Range(producer, 0, 1),
64 size_t const ret = ZSTD_decompressDCtx(
65 dctx, result, resultCapacity, compressed, cSize);
70 int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
72 FUZZ_SEQ_PROD_SETUP();
74 /* Give a random portion of src data to the producer, to use for
75 parameter generation. The rest will be used for (de)compression */
76 FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
77 size = FUZZ_dataProducer_reserveDataPrefix(producer);
79 uint8_t const* const srcBuf = src;
80 size_t const srcSize = FUZZ_dataProducer_uint32Range(producer, 0, size);
81 uint8_t const* const dictBuf = srcBuf + srcSize;
82 size_t const dictSize = size - srcSize;
83 size_t const decompSize = srcSize;
84 void* const decompBuf = FUZZ_malloc(decompSize);
85 size_t compSize = ZSTD_compressBound(srcSize);
87 /* Half of the time fuzz with a 1 byte smaller output size.
88 * This will still succeed because we force the checksum to be disabled,
89 * giving us 4 bytes of overhead.
91 compSize -= FUZZ_dataProducer_uint32Range(producer, 0, 1);
92 compBuf = FUZZ_malloc(compSize);
95 cctx = ZSTD_createCCtx();
99 dctx = ZSTD_createDCtx();
104 size_t const result =
105 roundTripTest(decompBuf, decompSize, compBuf, compSize, srcBuf, srcSize, dictBuf, dictSize, producer);
106 FUZZ_ZASSERT(result);
107 FUZZ_ASSERT_MSG(result == srcSize, "Incorrect regenerated size");
108 FUZZ_ASSERT_MSG(!FUZZ_memcmp(src, decompBuf, srcSize), "Corruption!");
112 FUZZ_dataProducer_free(producer);
113 #ifndef STATEFUL_FUZZING
114 ZSTD_freeCCtx(cctx); cctx = NULL;
115 ZSTD_freeDCtx(dctx); dctx = NULL;
117 FUZZ_SEQ_PROD_TEARDOWN();