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 attempts to decompress the fuzzed data with the simple
12 * decompression function to ensure the decompressor never crashes.
13 */
14
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include "fuzz_helpers.h"
19 #include "zstd.h"
20
21 static ZSTD_DCtx *dctx = NULL;
22
LLVMFuzzerTestOneInput(const uint8_t * src,size_t size)23 int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
24 {
25
26 uint32_t seed = FUZZ_seed(&src, &size);
27 int i;
28 if (!dctx) {
29 dctx = ZSTD_createDCtx();
30 FUZZ_ASSERT(dctx);
31 }
32 /* Run it 10 times over 10 output sizes. Reuse the context. */
33 for (i = 0; i < 10; ++i) {
34 size_t const bufSize = FUZZ_rand32(&seed, 0, 2 * size);
35 void* rBuf = malloc(bufSize);
36 FUZZ_ASSERT(rBuf);
37 ZSTD_decompressDCtx(dctx, rBuf, bufSize, src, size);
38 free(rBuf);
39 }
40
41 #ifndef STATEFUL_FUZZING
42 ZSTD_freeDCtx(dctx); dctx = NULL;
43 #endif
44 return 0;
45 }
46