]> git.proxmox.com Git - ceph.git/blame - ceph/src/zstd/tests/fuzz/dictionary_decompress.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / zstd / tests / fuzz / dictionary_decompress.c
CommitLineData
9f95a23c 1/*
f67539c2 2 * Copyright (c) 2016-2020, Facebook, Inc.
9f95a23c
TL
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).
f67539c2 8 * You may select, at your option, one of the above-listed licenses.
9f95a23c
TL
9 */
10
11/**
12 * This fuzz target attempts to decompress the fuzzed data with the dictionary
13 * decompression function to ensure the decompressor never crashes. It does not
14 * fuzz the dictionary.
15 */
16
17#include <stddef.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include "fuzz_helpers.h"
21#include "zstd_helpers.h"
f67539c2 22#include "fuzz_data_producer.h"
9f95a23c
TL
23
24static ZSTD_DCtx *dctx = NULL;
9f95a23c
TL
25
26int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
27{
f67539c2
TL
28 /* Give a random portion of src data to the producer, to use for
29 parameter generation. The rest will be used for (de)compression */
30 FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
31 size = FUZZ_dataProducer_reserveDataPrefix(producer);
9f95a23c 32
f67539c2
TL
33 FUZZ_dict_t dict;
34 ZSTD_DDict* ddict = NULL;
9f95a23c 35
9f95a23c
TL
36 if (!dctx) {
37 dctx = ZSTD_createDCtx();
38 FUZZ_ASSERT(dctx);
39 }
f67539c2
TL
40 dict = FUZZ_train(src, size, producer);
41 if (FUZZ_dataProducer_uint32Range(producer, 0, 1) == 0) {
42 ddict = ZSTD_createDDict(dict.buff, dict.size);
43 FUZZ_ASSERT(ddict);
9f95a23c 44 } else {
f67539c2
TL
45 if (FUZZ_dataProducer_uint32Range(producer, 0, 1) == 0)
46 FUZZ_ZASSERT(ZSTD_DCtx_loadDictionary_advanced(
9f95a23c 47 dctx, dict.buff, dict.size,
f67539c2
TL
48 (ZSTD_dictLoadMethod_e)FUZZ_dataProducer_uint32Range(producer, 0, 1),
49 (ZSTD_dictContentType_e)FUZZ_dataProducer_uint32Range(producer, 0, 2)));
50 else
51 FUZZ_ZASSERT(ZSTD_DCtx_refPrefix_advanced(
52 dctx, dict.buff, dict.size,
53 (ZSTD_dictContentType_e)FUZZ_dataProducer_uint32Range(producer, 0, 2)));
9f95a23c
TL
54 }
55
f67539c2
TL
56 {
57 size_t const bufSize = FUZZ_dataProducer_uint32Range(producer, 0, 10 * size);
58 void* rBuf = FUZZ_malloc(bufSize);
59 if (ddict) {
60 ZSTD_decompress_usingDDict(dctx, rBuf, bufSize, src, size, ddict);
61 } else {
62 ZSTD_decompressDCtx(dctx, rBuf, bufSize, src, size);
63 }
64 free(rBuf);
65 }
9f95a23c 66 free(dict.buff);
f67539c2
TL
67 FUZZ_dataProducer_free(producer);
68 ZSTD_freeDDict(ddict);
9f95a23c
TL
69#ifndef STATEFUL_FUZZING
70 ZSTD_freeDCtx(dctx); dctx = NULL;
71#endif
72 return 0;
73}