Coverage Report

Created: 2025-08-09 07:01

/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.9k
{
29
12.9k
    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.9k
    FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
34
12.9k
    size = FUZZ_dataProducer_reserveDataPrefix(producer);
35
36
12.9k
    FUZZ_dict_t dict;
37
12.9k
    ZSTD_DDict* ddict = NULL;
38
39
12.9k
    if (!dctx) {
40
12.9k
        dctx = ZSTD_createDCtx();
41
12.9k
        FUZZ_ASSERT(dctx);
42
12.9k
    }
43
12.9k
    dict = FUZZ_train(src, size, producer);
44
12.9k
    if (FUZZ_dataProducer_uint32Range(producer, 0, 1) == 0) {
45
11.3k
        ddict = ZSTD_createDDict(dict.buff, dict.size);
46
11.3k
        FUZZ_ASSERT(ddict);
47
11.3k
    } else {
48
1.52k
        if (FUZZ_dataProducer_uint32Range(producer, 0, 1) == 0)
49
1.52k
            FUZZ_ZASSERT(ZSTD_DCtx_loadDictionary_advanced(
50
1.15k
                dctx, dict.buff, dict.size,
51
1.15k
                (ZSTD_dictLoadMethod_e)FUZZ_dataProducer_uint32Range(producer, 0, 1),
52
1.15k
                (ZSTD_dictContentType_e)FUZZ_dataProducer_uint32Range(producer, 0, 2)));
53
1.15k
        else
54
1.52k
            FUZZ_ZASSERT(ZSTD_DCtx_refPrefix_advanced(
55
1.52k
                dctx, dict.buff, dict.size,
56
1.52k
                (ZSTD_dictContentType_e)FUZZ_dataProducer_uint32Range(producer, 0, 2)));
57
1.52k
    }
58
59
12.9k
    {
60
12.9k
        size_t const bufSize = FUZZ_dataProducer_uint32Range(producer, 0, 10 * size);
61
12.9k
        void* rBuf = FUZZ_malloc(bufSize);
62
12.9k
        if (ddict) {
63
11.3k
            ZSTD_decompress_usingDDict(dctx, rBuf, bufSize, src, size, ddict);
64
11.3k
        } else {
65
1.52k
            ZSTD_decompressDCtx(dctx, rBuf, bufSize, src, size);
66
1.52k
        }
67
12.9k
        free(rBuf);
68
12.9k
    }
69
12.9k
    free(dict.buff);
70
12.9k
    FUZZ_dataProducer_free(producer);
71
12.9k
    ZSTD_freeDDict(ddict);
72
12.9k
#ifndef STATEFUL_FUZZING
73
12.9k
    ZSTD_freeDCtx(dctx); dctx = NULL;
74
12.9k
#endif
75
12.9k
    FUZZ_SEQ_PROD_TEARDOWN();
76
12.9k
    return 0;
77
12.9k
}