Coverage Report

Created: 2024-09-08 06:32

/src/zstd/tests/fuzz/dictionary_decompress.c
Line
Count
Source
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
 * You may select, at your option, one of the above-listed licenses.
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"
22
#include "fuzz_data_producer.h"
23
#include "fuzz_third_party_seq_prod.h"
24
25
static ZSTD_DCtx *dctx = NULL;
26
27
int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
28
12.1k
{
29
12.1k
    FUZZ_SEQ_PROD_SETUP();
30
31
    /* Give a random portion of src data to the producer, to use for
32
    parameter generation. The rest will be used for (de)compression */
33
12.1k
    FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
34
12.1k
    size = FUZZ_dataProducer_reserveDataPrefix(producer);
35
36
12.1k
    FUZZ_dict_t dict;
37
12.1k
    ZSTD_DDict* ddict = NULL;
38
39
12.1k
    if (!dctx) {
40
12.1k
        dctx = ZSTD_createDCtx();
41
12.1k
        FUZZ_ASSERT(dctx);
42
12.1k
    }
43
12.1k
    dict = FUZZ_train(src, size, producer);
44
12.1k
    if (FUZZ_dataProducer_uint32Range(producer, 0, 1) == 0) {
45
10.3k
        ddict = ZSTD_createDDict(dict.buff, dict.size);
46
10.3k
        FUZZ_ASSERT(ddict);
47
10.3k
    } else {
48
1.79k
        if (FUZZ_dataProducer_uint32Range(producer, 0, 1) == 0)
49
1.79k
            FUZZ_ZASSERT(ZSTD_DCtx_loadDictionary_advanced(
50
1.39k
                dctx, dict.buff, dict.size,
51
1.39k
                (ZSTD_dictLoadMethod_e)FUZZ_dataProducer_uint32Range(producer, 0, 1),
52
1.39k
                (ZSTD_dictContentType_e)FUZZ_dataProducer_uint32Range(producer, 0, 2)));
53
1.39k
        else
54
1.79k
            FUZZ_ZASSERT(ZSTD_DCtx_refPrefix_advanced(
55
1.79k
                dctx, dict.buff, dict.size,
56
1.79k
                (ZSTD_dictContentType_e)FUZZ_dataProducer_uint32Range(producer, 0, 2)));
57
1.79k
    }
58
59
12.1k
    {
60
12.1k
        size_t const bufSize = FUZZ_dataProducer_uint32Range(producer, 0, 10 * size);
61
12.1k
        void* rBuf = FUZZ_malloc(bufSize);
62
12.1k
        if (ddict) {
63
10.3k
            ZSTD_decompress_usingDDict(dctx, rBuf, bufSize, src, size, ddict);
64
10.3k
        } else {
65
1.79k
            ZSTD_decompressDCtx(dctx, rBuf, bufSize, src, size);
66
1.79k
        }
67
12.1k
        free(rBuf);
68
12.1k
    }
69
12.1k
    free(dict.buff);
70
12.1k
    FUZZ_dataProducer_free(producer);
71
12.1k
    ZSTD_freeDDict(ddict);
72
12.1k
#ifndef STATEFUL_FUZZING
73
12.1k
    ZSTD_freeDCtx(dctx); dctx = NULL;
74
12.1k
#endif
75
12.1k
    FUZZ_SEQ_PROD_TEARDOWN();
76
12.1k
    return 0;
77
12.1k
}