1 /*
2 * Copyright (c) 2016-present, Facebook, Inc.
3 * All rights reserved.
4 *
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 */
9
10 /**
11 * This fuzz target performs a zstd round-trip test (compress & decompress) with
12 * a dictionary, compares the result with the original, and calls abort() on
13 * corruption.
14 */
15
16 #include <stddef.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include "fuzz_helpers.h"
21 #include "zstd_helpers.h"
22
23 static const int kMaxClevel = 19;
24
25 static ZSTD_CCtx *cctx = NULL;
26 static ZSTD_DCtx *dctx = NULL;
27 static uint32_t seed;
28
roundTripTest(void * result,size_t resultCapacity,void * compressed,size_t compressedCapacity,const void * src,size_t srcSize)29 static size_t roundTripTest(void *result, size_t resultCapacity,
30 void *compressed, size_t compressedCapacity,
31 const void *src, size_t srcSize)
32 {
33 ZSTD_dictContentType_e dictContentType = ZSTD_dct_auto;
34 FUZZ_dict_t dict = FUZZ_train(src, srcSize, &seed);
35 size_t cSize;
36 if ((FUZZ_rand(&seed) & 15) == 0) {
37 int const cLevel = FUZZ_rand(&seed) % kMaxClevel;
38
39 cSize = ZSTD_compress_usingDict(cctx,
40 compressed, compressedCapacity,
41 src, srcSize,
42 dict.buff, dict.size,
43 cLevel);
44 } else {
45 dictContentType = FUZZ_rand32(&seed, 0, 2);
46 FUZZ_setRandomParameters(cctx, srcSize, &seed);
47 /* Disable checksum so we can use sizes smaller than compress bound. */
48 FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 0));
49 FUZZ_ZASSERT(ZSTD_CCtx_loadDictionary_advanced(
50 cctx, dict.buff, dict.size,
51 (ZSTD_dictLoadMethod_e)FUZZ_rand32(&seed, 0, 1),
52 dictContentType));
53 cSize = ZSTD_compress2(cctx, compressed, compressedCapacity, src, srcSize);
54 }
55 FUZZ_ZASSERT(cSize);
56 FUZZ_ZASSERT(ZSTD_DCtx_loadDictionary_advanced(
57 dctx, dict.buff, dict.size,
58 (ZSTD_dictLoadMethod_e)FUZZ_rand32(&seed, 0, 1),
59 dictContentType));
60 {
61 size_t const ret = ZSTD_decompressDCtx(
62 dctx, result, resultCapacity, compressed, cSize);
63 free(dict.buff);
64 return ret;
65 }
66 }
67
LLVMFuzzerTestOneInput(const uint8_t * src,size_t size)68 int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
69 {
70 size_t const rBufSize = size;
71 void* rBuf = malloc(rBufSize);
72 size_t cBufSize = ZSTD_compressBound(size);
73 void* cBuf;
74
75 seed = FUZZ_seed(&src, &size);
76 /* Half of the time fuzz with a 1 byte smaller output size.
77 * This will still succeed because we force the checksum to be disabled,
78 * giving us 4 bytes of overhead.
79 */
80 cBufSize -= FUZZ_rand32(&seed, 0, 1);
81 cBuf = malloc(cBufSize);
82
83 if (!cctx) {
84 cctx = ZSTD_createCCtx();
85 FUZZ_ASSERT(cctx);
86 }
87 if (!dctx) {
88 dctx = ZSTD_createDCtx();
89 FUZZ_ASSERT(dctx);
90 }
91
92 {
93 size_t const result =
94 roundTripTest(rBuf, rBufSize, cBuf, cBufSize, src, size);
95 FUZZ_ZASSERT(result);
96 FUZZ_ASSERT_MSG(result == size, "Incorrect regenerated size");
97 FUZZ_ASSERT_MSG(!memcmp(src, rBuf, size), "Corruption!");
98 }
99 free(rBuf);
100 free(cBuf);
101 #ifndef STATEFUL_FUZZING
102 ZSTD_freeCCtx(cctx); cctx = NULL;
103 ZSTD_freeDCtx(dctx); dctx = NULL;
104 #endif
105 return 0;
106 }
107