/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 | 221 | { |
30 | 221 | FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size); |
31 | | |
32 | | /* Pick a random tableLog and maxSymbolValue */ |
33 | 221 | unsigned const tableLog = FUZZ_dataProducer_uint32Range(producer, FSE_MIN_TABLELOG, FSE_MAX_TABLELOG); |
34 | 221 | unsigned const maxSymbolValue = FUZZ_dataProducer_uint32Range(producer, 0, 255); |
35 | | |
36 | 221 | unsigned remainingWeight = (1u << tableLog) - 1; |
37 | 221 | size_t dataSize; |
38 | 221 | BYTE data[512]; |
39 | 221 | short ncount[256]; |
40 | | |
41 | | /* Randomly fill the normalized count */ |
42 | 221 | memset(ncount, 0, sizeof(ncount)); |
43 | 221 | { |
44 | 221 | unsigned s; |
45 | 13.8k | for (s = 0; s < maxSymbolValue && remainingWeight > 0; ++s) { |
46 | 13.6k | short n = (short)FUZZ_dataProducer_int32Range(producer, -1, remainingWeight); |
47 | 13.6k | ncount[s] = n; |
48 | 13.6k | if (n < 0) { |
49 | 9.83k | remainingWeight -= 1; |
50 | 9.83k | } else { |
51 | 3.79k | assert((unsigned)n <= remainingWeight); |
52 | 3.79k | remainingWeight -= n; |
53 | 3.79k | } |
54 | 13.6k | } |
55 | | /* Ensure ncount[maxSymbolValue] != 0 and the sum is (1<<tableLog) */ |
56 | 221 | ncount[maxSymbolValue] = remainingWeight + 1; |
57 | 221 | if (ncount[maxSymbolValue] == 1 && FUZZ_dataProducer_uint32Range(producer, 0, 1) == 1) { |
58 | 12 | ncount[maxSymbolValue] = -1; |
59 | 12 | } |
60 | 221 | } |
61 | | /* Write the normalized count */ |
62 | 0 | { |
63 | 221 | FUZZ_ASSERT(sizeof(data) >= FSE_NCountWriteBound(maxSymbolValue, tableLog)); |
64 | 221 | dataSize = FSE_writeNCount(data, sizeof(data), ncount, maxSymbolValue, tableLog); |
65 | 221 | FUZZ_ZASSERT(dataSize); |
66 | 221 | } |
67 | | /* Read & validate the normalized count */ |
68 | 221 | { |
69 | 221 | short rtNcount[256]; |
70 | 221 | unsigned rtMaxSymbolValue = 255; |
71 | 221 | unsigned rtTableLog; |
72 | | /* Copy into a buffer with a random amount of random data at the end */ |
73 | 221 | size_t const buffSize = (size_t)FUZZ_dataProducer_uint32Range(producer, dataSize, sizeof(data)); |
74 | 221 | BYTE* const buff = FUZZ_malloc(buffSize); |
75 | 221 | size_t rtDataSize; |
76 | 221 | memcpy(buff, data, dataSize); |
77 | 221 | { |
78 | 221 | size_t b; |
79 | 9.85k | for (b = dataSize; b < buffSize; ++b) { |
80 | 9.63k | buff[b] = (BYTE)FUZZ_dataProducer_uint32Range(producer, 0, 255); |
81 | 9.63k | } |
82 | 221 | } |
83 | | |
84 | 221 | rtDataSize = FSE_readNCount(rtNcount, &rtMaxSymbolValue, &rtTableLog, buff, buffSize); |
85 | 221 | FUZZ_ZASSERT(rtDataSize); |
86 | 221 | FUZZ_ASSERT(rtDataSize == dataSize); |
87 | 221 | FUZZ_ASSERT(rtMaxSymbolValue == maxSymbolValue); |
88 | 221 | FUZZ_ASSERT(rtTableLog == tableLog); |
89 | 221 | { |
90 | 221 | unsigned s; |
91 | 25.7k | for (s = 0; s <= maxSymbolValue; ++s) { |
92 | 25.5k | FUZZ_ASSERT(ncount[s] == rtNcount[s]); |
93 | 25.5k | } |
94 | 221 | } |
95 | 221 | free(buff); |
96 | 221 | } |
97 | | |
98 | 0 | FUZZ_dataProducer_free(producer); |
99 | 221 | return 0; |
100 | 221 | } |