/src/zxc/tests/fuzz/fuzz_stream.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_stream.c |
10 | | * @brief Fuzzer for the multi-threaded FILE* streaming API (zxc_stream_*). |
11 | | * |
12 | | * The FILE* engine (reader, worker threads, reordering writer) walks frames with |
13 | | * its own code. Streams live in memory (fmemopen / open_memstream), as in the |
14 | | * CLI benchmark. |
15 | | * |
16 | | * 1. Feed the raw input to zxc_stream_get_decompressed_size and |
17 | | * zxc_stream_decompress. |
18 | | * 2. Compress the payload, tiled 1-8 times so short inputs still span several |
19 | | * blocks; level, block size, threads, checksum, seek table and dictionary |
20 | | * come from the header bytes. Decompress it back: bit-exact, footer size |
21 | | * included. |
22 | | */ |
23 | | |
24 | | #include <assert.h> |
25 | | #include <stddef.h> |
26 | | #include <stdint.h> |
27 | | #include <stdio.h> |
28 | | #include <stdlib.h> |
29 | | #include <string.h> |
30 | | |
31 | | #include "../../include/zxc_buffer.h" |
32 | | #include "../../include/zxc_constants.h" |
33 | | #include "../../include/zxc_error.h" |
34 | | #include "../../include/zxc_stream.h" |
35 | | |
36 | 18.4k | #define FUZZ_STREAM_MAX_INPUT (256 << 10) /* 256 KiB, before tiling */ |
37 | | |
38 | | static const size_t kBlockSizes[] = {4096, 8192, 16384, 65536}; |
39 | | |
40 | 111k | static void on_progress(uint64_t done, uint64_t total, const void* user_data) { |
41 | 111k | (void)done; |
42 | 111k | (void)total; |
43 | 111k | (void)user_data; |
44 | 111k | } |
45 | | |
46 | 18.4k | static void fuzz_raw(const uint8_t* src, const size_t n, const int n_threads, const int checksum) { |
47 | 18.4k | FILE* const in = fmemopen((void*)src, n, "rb"); |
48 | 18.4k | if (!in) return; |
49 | 18.4k | (void)zxc_stream_get_decompressed_size(in); /* restores the position */ |
50 | 18.4k | zxc_dctx* const cfg = zxc_create_dctx(); |
51 | 18.4k | if (cfg && zxc_dctx_set_param(cfg, ZXC_D_THREADS, n_threads) == ZXC_OK && |
52 | 18.4k | zxc_dctx_set_param(cfg, ZXC_D_CHECKSUM, checksum) == ZXC_OK) { |
53 | 18.4k | (void)zxc_stream_decompress(in, NULL, cfg); /* no output: a bomb costs CPU, not memory */ |
54 | 18.4k | } |
55 | 18.4k | zxc_free_dctx(cfg); |
56 | 18.4k | fclose(in); |
57 | 18.4k | } |
58 | | |
59 | | static void roundtrip(uint8_t* plain, const size_t total, const zxc_cctx* co, |
60 | 18.3k | const zxc_dctx* dopts) { |
61 | 18.3k | char* comp = NULL; |
62 | 18.3k | size_t comp_len = 0; |
63 | 18.3k | FILE* in = fmemopen(plain, total, "rb"); |
64 | 18.3k | FILE* out = open_memstream(&comp, &comp_len); |
65 | 18.3k | const int64_t csize = (in && out) ? zxc_stream_compress(in, out, co) : -1; |
66 | 18.3k | if (in) fclose(in); |
67 | 18.3k | if (out) fclose(out); /* comp / comp_len are final only now */ |
68 | 18.3k | if (csize < 0 || !comp) { |
69 | 0 | free(comp); |
70 | 0 | return; |
71 | 0 | } |
72 | 18.3k | assert((size_t)csize == comp_len); |
73 | | |
74 | 18.3k | char* dec = NULL; |
75 | 18.3k | size_t dec_len = 0; |
76 | 18.3k | in = fmemopen(comp, comp_len, "rb"); |
77 | 18.3k | out = open_memstream(&dec, &dec_len); |
78 | 18.3k | if (in && out) { |
79 | 18.3k | const int64_t reported = zxc_stream_get_decompressed_size(in); |
80 | 18.3k | const int64_t dsize = zxc_stream_decompress(in, out, dopts); |
81 | 18.3k | fclose(out); |
82 | 18.3k | out = NULL; |
83 | 18.3k | assert(reported == (int64_t)total); |
84 | 18.3k | assert(dsize == (int64_t)total); |
85 | 18.3k | assert(dec_len == total && memcmp(dec, plain, total) == 0); |
86 | 18.3k | (void)reported; |
87 | 18.3k | (void)dsize; |
88 | 18.3k | } |
89 | 18.3k | if (in) fclose(in); |
90 | 18.3k | if (out) fclose(out); |
91 | 18.3k | free(dec); |
92 | 18.3k | free(comp); |
93 | 18.3k | } |
94 | | |
95 | 18.4k | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
96 | 18.4k | if (size < 4) return 0; |
97 | | |
98 | 18.4k | const int level = (int)(data[0] % (unsigned)zxc_max_level()) + 1; |
99 | 18.4k | const int checksum = data[1] & 1; |
100 | 18.4k | const int seekable = (data[1] >> 1) & 1; |
101 | 18.4k | const int use_dict = (data[1] >> 2) & 1; |
102 | 18.4k | const int n_threads = ((data[1] >> 3) & 1) + 1; |
103 | 18.4k | const zxc_progress_callback_t cb = ((data[1] >> 4) & 1) ? on_progress : NULL; |
104 | 18.4k | const size_t block_size = kBlockSizes[(data[1] >> 5) & 3]; |
105 | 18.4k | const size_t tiles = (size_t)(data[2] & 7) + 1; |
106 | 18.4k | const int favor_decode_speed = (data[2] >> 3) & 1; |
107 | 18.4k | const int split_budget_pct = |
108 | 18.4k | 1 + (data[2] >> 4) * (ZXC_FAVOR_DECODE_SPEED_MAX - 1) / 15; /* 1..100 */ |
109 | 18.4k | const size_t dict_cap = ((size_t)data[3] + 1) * 256; |
110 | | |
111 | 18.4k | fuzz_raw(data, size, n_threads, checksum); |
112 | | |
113 | 18.4k | data += 4; |
114 | 18.4k | size -= 4; |
115 | 18.4k | if (size == 0 || size > FUZZ_STREAM_MAX_INPUT) return 0; |
116 | | |
117 | 18.3k | size_t dict_size = use_dict ? dict_cap : 0; |
118 | 18.3k | if (dict_size > size) dict_size = size; |
119 | 18.3k | if (dict_size > ZXC_DICT_SIZE_MAX) dict_size = ZXC_DICT_SIZE_MAX; |
120 | | |
121 | 18.3k | const size_t total = size * tiles; |
122 | 18.3k | uint8_t* const plain = (uint8_t*)malloc(total); |
123 | 18.3k | if (!plain) return 0; |
124 | 147k | for (size_t t = 0; t < tiles; t++) { |
125 | 129k | memcpy(plain + t * size, data, size); |
126 | 129k | plain[t * size] ^= (uint8_t)t; /* distinct blocks */ |
127 | 129k | } |
128 | | |
129 | 18.3k | const void* const dict = dict_size ? data : NULL; |
130 | 18.3k | zxc_cctx* const co = zxc_create_cctx(); |
131 | 18.3k | zxc_dctx* const dopts = zxc_create_dctx(); |
132 | 18.3k | const int configured = |
133 | 18.3k | co && dopts && zxc_cctx_set_param(co, ZXC_C_THREADS, n_threads) == ZXC_OK && |
134 | 18.3k | zxc_cctx_set_param(co, ZXC_C_LEVEL, level) == ZXC_OK && |
135 | 18.3k | zxc_cctx_set_param(co, ZXC_C_BLOCK_SIZE, (int64_t)block_size) == ZXC_OK && |
136 | 18.3k | zxc_cctx_set_param(co, ZXC_C_CHECKSUM, checksum) == ZXC_OK && |
137 | 18.3k | zxc_cctx_set_param(co, ZXC_C_SEEKABLE, seekable) == ZXC_OK && |
138 | 18.3k | zxc_cctx_set_param(co, ZXC_C_FAVOR_DECODE_SPEED, |
139 | 18.3k | favor_decode_speed ? split_budget_pct : 0) == ZXC_OK && |
140 | 18.3k | zxc_cctx_set_dict(co, dict, dict_size, NULL) == ZXC_OK && |
141 | 18.3k | zxc_cctx_set_progress(co, cb, NULL) == ZXC_OK && |
142 | 18.3k | zxc_dctx_set_param(dopts, ZXC_D_THREADS, n_threads) == ZXC_OK && |
143 | 18.3k | zxc_dctx_set_param(dopts, ZXC_D_CHECKSUM, checksum) == ZXC_OK && |
144 | 18.3k | zxc_dctx_set_dict(dopts, dict, dict_size, NULL) == ZXC_OK && |
145 | 18.3k | zxc_dctx_set_progress(dopts, cb, NULL) == ZXC_OK; |
146 | 18.3k | if (configured) roundtrip(plain, total, co, dopts); |
147 | 18.3k | zxc_free_cctx(co); |
148 | 18.3k | zxc_free_dctx(dopts); |
149 | | |
150 | 18.3k | free(plain); |
151 | 18.3k | return 0; |
152 | 18.3k | } |