/src/zxc/tests/fuzz/fuzz_seekable.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: BSD-3-Clause |
2 | | /* |
3 | | * ZXC - High-performance lossless compression |
4 | | * |
5 | | * Copyright (c) Bertrand Lebonnois and contributors. |
6 | | */ |
7 | | |
8 | | /** |
9 | | * @file fuzz_seekable.c |
10 | | * @brief Fuzzer for the seekable random-access decompression API. |
11 | | * |
12 | | * Strategy: compress fuzzed input with seekable=1, then exercise the full |
13 | | * seekable read path (open, metadata getters, single-threaded decompress, |
14 | | * multi-threaded decompress) and verify data integrity. |
15 | | */ |
16 | | |
17 | | #include <assert.h> |
18 | | #include <stddef.h> |
19 | | #include <stdint.h> |
20 | | #include <stdlib.h> |
21 | | #include <string.h> |
22 | | |
23 | | #include "../../include/zxc_buffer.h" |
24 | | #include "../../include/zxc_error.h" |
25 | | #include "../../include/zxc_seekable.h" |
26 | | |
27 | 10.6k | #define FUZZ_SEEKABLE_MAX_INPUT (4 << 20) /* 4 MiB */ |
28 | | |
29 | 10.6k | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
30 | 10.6k | if (size < 2) return 0; |
31 | | |
32 | | /* Save original input for phase 7 (raw parser fuzzing) */ |
33 | 10.6k | const uint8_t* const raw_data = data; |
34 | 10.6k | const size_t raw_size = size; |
35 | | |
36 | | /* Use first byte as level, second as flags */ |
37 | 10.6k | const int level = (data[0] % 5) + 1; |
38 | 10.6k | const int use_checksum = data[1] & 1; |
39 | 10.6k | const int use_mt = data[1] & 2; |
40 | 10.6k | const int small_blocks = data[1] & 0x40; /* many blocks: in place, several batches */ |
41 | 10.6k | data += 2; |
42 | 10.6k | size -= 2; |
43 | | |
44 | 10.6k | if (size == 0 || size > FUZZ_SEEKABLE_MAX_INPUT) return 0; |
45 | | |
46 | | /* Persistent buffers - reused across iterations to reduce allocator pressure */ |
47 | 10.6k | static uint8_t* comp_buf = NULL; |
48 | 10.6k | static size_t comp_cap = 0; |
49 | 10.6k | static uint8_t* decomp_buf = NULL; |
50 | 10.6k | static size_t decomp_cap = 0; |
51 | | |
52 | | /* ------------------------------------------------------------------ */ |
53 | | /* Phase 1: Compress with seekable=1 */ |
54 | | /* ------------------------------------------------------------------ */ |
55 | 10.6k | const size_t bound = zxc_compress_bound(size); |
56 | 10.6k | if (bound == 0) return 0; |
57 | 10.6k | if (bound > comp_cap) { |
58 | 5.63k | void* new_buf = realloc(comp_buf, bound); |
59 | 5.63k | if (!new_buf) return 0; |
60 | 5.63k | comp_buf = (uint8_t*)new_buf; |
61 | 5.63k | comp_cap = bound; |
62 | 5.63k | } |
63 | | |
64 | 10.6k | zxc_cctx* const cctx = zxc_create_cctx(); |
65 | 10.6k | if (!cctx) return 0; |
66 | 10.6k | int64_t csize = -1; |
67 | 10.6k | if (zxc_cctx_set_param(cctx, ZXC_C_LEVEL, level) == ZXC_OK && |
68 | 10.6k | zxc_cctx_set_param(cctx, ZXC_C_CHECKSUM, use_checksum) == ZXC_OK && |
69 | 10.6k | zxc_cctx_set_param(cctx, ZXC_C_SEEKABLE, 1) == ZXC_OK && |
70 | 10.6k | (!small_blocks || zxc_cctx_set_param(cctx, ZXC_C_BLOCK_SIZE, 4096) == ZXC_OK)) { |
71 | 10.6k | csize = zxc_compress_cctx(cctx, data, size, comp_buf, bound); |
72 | 10.6k | } |
73 | 10.6k | zxc_free_cctx(cctx); |
74 | 10.6k | if (csize < 0) return 0; |
75 | | |
76 | | /* ------------------------------------------------------------------ */ |
77 | | /* Phase 2: Open seekable handle */ |
78 | | /* ------------------------------------------------------------------ */ |
79 | 10.6k | zxc_seekable* s = zxc_seekable_open(comp_buf, (size_t)csize); |
80 | 10.6k | if (!s) return 0; |
81 | | |
82 | | /* ------------------------------------------------------------------ */ |
83 | | /* Phase 3: Exercise metadata getters */ |
84 | | /* ------------------------------------------------------------------ */ |
85 | 10.6k | const uint64_t num_blocks = zxc_seekable_get_num_blocks(s); |
86 | 10.6k | const uint64_t total_decomp = zxc_seekable_get_decompressed_size(s); |
87 | 10.6k | assert(total_decomp == size); |
88 | | |
89 | 159k | for (uint64_t i = 0; i < num_blocks; i++) { |
90 | 148k | const uint32_t csz = zxc_seekable_get_block_comp_size(s, i); |
91 | 148k | const uint32_t dsz = zxc_seekable_get_block_decomp_size(s, i); |
92 | 148k | assert(csz > 0); |
93 | 148k | assert(dsz > 0); |
94 | 148k | (void)csz; |
95 | 148k | (void)dsz; |
96 | 148k | } |
97 | | /* Out-of-range access should return 0 */ |
98 | 10.6k | assert(zxc_seekable_get_block_comp_size(s, num_blocks) == 0); |
99 | 10.6k | assert(zxc_seekable_get_block_decomp_size(s, num_blocks) == 0); |
100 | | |
101 | | /* ------------------------------------------------------------------ */ |
102 | | /* Phase 4: Full decompression via seekable range */ |
103 | | /* ------------------------------------------------------------------ */ |
104 | 10.6k | if (size > decomp_cap) { |
105 | 5.63k | void* new_buf = realloc(decomp_buf, size); |
106 | 5.63k | if (!new_buf) { |
107 | 0 | zxc_seekable_free(s); |
108 | 0 | return 0; |
109 | 0 | } |
110 | 5.63k | decomp_buf = (uint8_t*)new_buf; |
111 | 5.63k | decomp_cap = size; |
112 | 5.63k | } |
113 | | |
114 | 10.6k | int64_t dec_result; |
115 | 10.6k | if (use_mt && size > 4096) { |
116 | 2.00k | dec_result = zxc_seekable_decompress_range_mt(s, decomp_buf, size, 0, size, 2); |
117 | 8.66k | } else { |
118 | 8.66k | dec_result = zxc_seekable_decompress_range(s, decomp_buf, size, 0, size); |
119 | 8.66k | } |
120 | 10.6k | assert(dec_result == (int64_t)size); |
121 | 10.6k | (void)dec_result; |
122 | 10.6k | assert(memcmp(data, decomp_buf, size) == 0); |
123 | | |
124 | | /* ------------------------------------------------------------------ */ |
125 | | /* Phase 5: Partial range decompression (sub-block extraction) */ |
126 | | /* ------------------------------------------------------------------ */ |
127 | 10.6k | if (size >= 4) { |
128 | | /* Extract a range from the middle */ |
129 | 10.6k | const size_t off = size / 4; |
130 | 10.6k | const size_t len = size / 2; |
131 | 10.6k | dec_result = zxc_seekable_decompress_range(s, decomp_buf, len, off, len); |
132 | 10.6k | assert(dec_result == (int64_t)len); |
133 | 10.6k | assert(memcmp(data + off, decomp_buf, len) == 0); |
134 | 10.6k | } |
135 | | |
136 | | /* ------------------------------------------------------------------ */ |
137 | | /* Phase 6: Edge cases */ |
138 | | /* ------------------------------------------------------------------ */ |
139 | | /* Zero-length read */ |
140 | 10.6k | dec_result = zxc_seekable_decompress_range(s, decomp_buf, size, 0, 0); |
141 | 10.6k | assert(dec_result == 0); |
142 | | |
143 | | /* Out-of-bounds read */ |
144 | 10.6k | dec_result = zxc_seekable_decompress_range(s, decomp_buf, size, total_decomp, 1); |
145 | 10.6k | assert(dec_result < 0); |
146 | | |
147 | | /* NULL handle */ |
148 | 10.6k | assert(zxc_seekable_get_num_blocks(NULL) == 0); |
149 | 10.6k | assert(zxc_seekable_get_decompressed_size(NULL) == 0); |
150 | | |
151 | | /* ------------------------------------------------------------------ */ |
152 | | /* Phase 8: Corrupted blocks, decoded into an exact buffer */ |
153 | | /* ------------------------------------------------------------------ */ |
154 | | /* Bytes flipped inside the blocks, the seek table and footer intact so that it |
155 | | * opens: no decode may write past len. */ |
156 | 10.6k | size_t blocks_end = ZXC_FILE_HEADER_SIZE; |
157 | 159k | for (uint64_t i = 0; i < num_blocks; i++) blocks_end += zxc_seekable_get_block_comp_size(s, i); |
158 | 10.6k | if (blocks_end > ZXC_FILE_HEADER_SIZE && blocks_end <= (size_t)csize) { |
159 | 10.6k | uint8_t* const bad = (uint8_t*)malloc((size_t)csize); |
160 | 10.6k | uint8_t* const out = (uint8_t*)malloc(size); |
161 | 10.6k | if (bad && out) { |
162 | 10.6k | memcpy(bad, comp_buf, (size_t)csize); |
163 | 10.6k | const size_t span = blocks_end - ZXC_FILE_HEADER_SIZE; |
164 | 10.6k | const int flips = 1 + ((raw_data[1] >> 2) & 3); |
165 | 36.4k | for (int k = 0; k < flips; k++) { |
166 | 25.8k | const uint8_t a = data[(size_t)k % size], b = data[(size_t)(k * 7 + 3) % size]; |
167 | 25.8k | bad[ZXC_FILE_HEADER_SIZE + ((size_t)a * 257 + b) * 2654435761U % span] ^= |
168 | 25.8k | (uint8_t)(1U << (b & 7)); |
169 | 25.8k | } |
170 | 10.6k | zxc_seekable* const sc = zxc_seekable_open(bad, (size_t)csize); |
171 | 10.6k | if (sc) { |
172 | 10.6k | zxc_seekable_decompress_range_mt(sc, out, size, 0, size, |
173 | 10.6k | 2 + ((raw_data[1] >> 4) & 3)); |
174 | 10.6k | zxc_seekable_decompress_range(sc, out, size, 0, size); |
175 | 10.6k | zxc_seekable_free(sc); |
176 | 10.6k | } |
177 | 10.6k | } |
178 | 10.6k | free(bad); |
179 | 10.6k | free(out); |
180 | 10.6k | } |
181 | | |
182 | | /* ------------------------------------------------------------------ */ |
183 | | /* Cleanup */ |
184 | | /* ------------------------------------------------------------------ */ |
185 | 10.6k | zxc_seekable_free(s); |
186 | | |
187 | | /* ------------------------------------------------------------------ */ |
188 | | /* Phase 7: Fuzz the parser with raw data (malformed seekable archives) */ |
189 | | /* ------------------------------------------------------------------ */ |
190 | 10.6k | zxc_seekable* s2 = zxc_seekable_open(raw_data, raw_size); |
191 | 10.6k | if (s2) { |
192 | | /* If it parsed, try a read - should not crash */ |
193 | 0 | const uint64_t td = zxc_seekable_get_decompressed_size(s2); |
194 | 0 | if (td > 0 && td <= FUZZ_SEEKABLE_MAX_INPUT) { |
195 | 0 | uint8_t* tmp = (uint8_t*)malloc((size_t)td); |
196 | 0 | if (tmp) { |
197 | 0 | zxc_seekable_decompress_range(s2, tmp, (size_t)td, 0, (size_t)td); |
198 | 0 | free(tmp); |
199 | 0 | } |
200 | 0 | } |
201 | 0 | zxc_seekable_free(s2); |
202 | 0 | } |
203 | | |
204 | 10.6k | return 0; |
205 | 10.6k | } |