Coverage Report

Created: 2025-07-11 06:35

/src/zstd/tests/fuzz/fse_read_ncount.c
Line
Count
Source (jump to first uncovered line)
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 round trips the FSE normalized count with FSE_writeNCount()
13
 * and FSE_readNcount() to ensure that it can always round trip correctly.
14
 */
15
16
#define FSE_STATIC_LINKING_ONLY
17
#define ZSTD_STATIC_LINKING_ONLY
18
19
#include <stddef.h>
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include <string.h>
23
#include "fuzz_helpers.h"
24
#include "zstd_helpers.h"
25
#include "fuzz_data_producer.h"
26
#include "fse.h"
27
28
int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
29
239
{
30
239
    FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
31
32
    /* Pick a random tableLog and maxSymbolValue */
33
239
    unsigned const tableLog = FUZZ_dataProducer_uint32Range(producer, FSE_MIN_TABLELOG, FSE_MAX_TABLELOG);
34
239
    unsigned const maxSymbolValue = FUZZ_dataProducer_uint32Range(producer, 0, 255);
35
36
239
    unsigned remainingWeight = (1u << tableLog) - 1;
37
239
    size_t dataSize;
38
239
    BYTE data[512];
39
239
    short ncount[256];
40
41
    /* Randomly fill the normalized count */
42
239
    memset(ncount, 0, sizeof(ncount));
43
239
    {
44
239
        unsigned s;
45
13.4k
        for (s = 0; s < maxSymbolValue && remainingWeight > 0; ++s) {
46
13.2k
            short n = (short)FUZZ_dataProducer_int32Range(producer, -1, remainingWeight);
47
13.2k
            ncount[s] = n;
48
13.2k
            if (n < 0) {
49
9.58k
                remainingWeight -= 1;
50
9.58k
            } else {
51
3.65k
                assert((unsigned)n <= remainingWeight);
52
3.65k
                remainingWeight -= n;
53
3.65k
            }
54
13.2k
        }
55
        /* Ensure ncount[maxSymbolValue] != 0 and the sum is (1<<tableLog) */
56
239
        ncount[maxSymbolValue] = remainingWeight + 1;
57
239
        if (ncount[maxSymbolValue] == 1 && FUZZ_dataProducer_uint32Range(producer, 0, 1) == 1) {
58
10
            ncount[maxSymbolValue] = -1;
59
10
        }
60
239
    }
61
    /* Write the normalized count */
62
0
    {
63
239
        FUZZ_ASSERT(sizeof(data) >= FSE_NCountWriteBound(maxSymbolValue, tableLog));
64
239
        dataSize = FSE_writeNCount(data, sizeof(data), ncount, maxSymbolValue, tableLog);
65
239
        FUZZ_ZASSERT(dataSize);
66
239
    }
67
    /* Read & validate the normalized count */
68
239
    {
69
239
        short rtNcount[256];
70
239
        unsigned rtMaxSymbolValue = 255;
71
239
        unsigned rtTableLog;
72
        /* Copy into a buffer with a random amount of random data at the end */
73
239
        size_t const buffSize = (size_t)FUZZ_dataProducer_uint32Range(producer, dataSize, sizeof(data));
74
239
        BYTE* const buff = FUZZ_malloc(buffSize);
75
239
        size_t rtDataSize;
76
239
        memcpy(buff, data, dataSize); 
77
239
        {
78
239
            size_t b;
79
11.8k
            for (b = dataSize; b < buffSize; ++b) {
80
11.6k
                buff[b] = (BYTE)FUZZ_dataProducer_uint32Range(producer, 0, 255);
81
11.6k
            }
82
239
        }
83
84
239
        rtDataSize = FSE_readNCount(rtNcount, &rtMaxSymbolValue, &rtTableLog, buff, buffSize);
85
239
        FUZZ_ZASSERT(rtDataSize);
86
239
        FUZZ_ASSERT(rtDataSize == dataSize);
87
239
        FUZZ_ASSERT(rtMaxSymbolValue == maxSymbolValue);
88
239
        FUZZ_ASSERT(rtTableLog == tableLog);
89
239
        {
90
239
            unsigned s;
91
25.9k
            for (s = 0; s <= maxSymbolValue; ++s) {
92
25.7k
                FUZZ_ASSERT(ncount[s] == rtNcount[s]);
93
25.7k
            }
94
239
        }
95
239
        free(buff);
96
239
    }
97
98
0
    FUZZ_dataProducer_free(producer);
99
239
    return 0;
100
239
}