/src/zxc/tests/fuzz/fuzz_decompress.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_decompress.c |
10 | | * @brief Fuzzer for the one-shot decompressor on untrusted input. |
11 | | * |
12 | | * The decoder is the security-critical surface: it parses attacker-controlled |
13 | | * frames, so it must never crash, over-read, or over-write regardless of how |
14 | | * malformed the input is. This is a one-way target -- the fuzzer bytes are fed |
15 | | * straight to zxc_decompress() with no preceding compression step. |
16 | | * |
17 | | * Strategy: query zxc_get_decompressed_size() to size the output buffer when |
18 | | * the header advertises a plausible length (clamped to the static cap), then |
19 | | * call zxc_decompress() on the raw bytes. Correctness is enforced by the |
20 | | * sanitizers (ASan/UBSan) catching any out-of-bounds access; truncated, |
21 | | * corrupt, and adversarial frames are all expected to return an error rather |
22 | | * than misbehave. |
23 | | */ |
24 | | |
25 | | #include <stddef.h> |
26 | | #include <stdint.h> |
27 | | #include <stdlib.h> |
28 | | |
29 | | #include "../../include/zxc_buffer.h" |
30 | | |
31 | 7.94k | int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
32 | 7.94k | static uint8_t out_buf[4 << 20]; /* 4 MiB */ |
33 | 7.94k | size_t out_capacity = sizeof(out_buf); |
34 | | |
35 | 7.94k | uint64_t expected_size = zxc_get_decompressed_size(data, size); |
36 | | |
37 | 7.94k | if (expected_size > 0 && expected_size <= out_capacity) out_capacity = (size_t)expected_size; |
38 | | |
39 | 7.94k | zxc_decompress(data, size, out_buf, out_capacity); |
40 | | |
41 | 7.94k | return 0; |
42 | 7.94k | } |