/src/xz/tests/ossfuzz/fuzz_decode_stream.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: 0BSD |
2 | | |
3 | | /////////////////////////////////////////////////////////////////////////////// |
4 | | // |
5 | | /// \file fuzz_decode_stream.c |
6 | | /// \brief Fuzz test program for single threaded .xz decoding |
7 | | // |
8 | | // Authors: Lasse Collin |
9 | | // Maksym Vatsyk |
10 | | // |
11 | | /////////////////////////////////////////////////////////////////////////////// |
12 | | |
13 | | #include <inttypes.h> |
14 | | #include <stdlib.h> |
15 | | #include <stdio.h> |
16 | | #include "lzma.h" |
17 | | #include "fuzz_common.h" |
18 | | |
19 | | |
20 | | extern int |
21 | | LLVMFuzzerTestOneInput(const uint8_t *inbuf, size_t inbuf_size) |
22 | 4.98k | { |
23 | 4.98k | lzma_stream strm = LZMA_STREAM_INIT; |
24 | 4.98k | prepare_stream(&strm, inbuf, inbuf_size); |
25 | | |
26 | 4.98k | lzma_ret ret; |
27 | | |
28 | 19.9k | for (int i = 0; i < 3; ++i) { |
29 | | // Initialize a .xz decoder using the memory usage limit |
30 | | // defined in fuzz_common.h |
31 | | // |
32 | | // After the first two iterations, enable support for |
33 | | // concatenated .xz files which is used when decompressing |
34 | | // regular .xz files (instead of data embedded inside some |
35 | | // other file format). This way the first iteration won't |
36 | | // consume all the input until the input is invalid. |
37 | | // |
38 | | // Integrity checks on the uncompressed data are ignored to |
39 | | // make fuzzing more effective (incorrect check values won't |
40 | | // prevent the decoder from processing more input). |
41 | | // |
42 | | // The flag LZMA_IGNORE_CHECK doesn't disable verification |
43 | | // of header CRC32 values. Those checks are disabled when |
44 | | // liblzma is built with the |
45 | | // #define FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION. |
46 | 14.9k | ret = lzma_stream_decoder(&strm, MEM_LIMIT, LZMA_IGNORE_CHECK |
47 | 14.9k | | (i >= 2 ? LZMA_CONCATENATED : 0)); |
48 | | |
49 | 14.9k | if (ret == LZMA_MEM_ERROR) |
50 | 128 | continue; |
51 | | |
52 | 14.8k | if (ret != LZMA_OK) { |
53 | | // This should never happen unless the system has |
54 | | // no free memory or address space to allow the small |
55 | | // allocations that the initialization requires. |
56 | 0 | fprintf(stderr, "lzma_stream_decoder() failed (%d)\n", |
57 | 0 | ret); |
58 | 0 | abort(); |
59 | 0 | } |
60 | | |
61 | 14.8k | fuzz_code(&strm, inbuf, inbuf_size); |
62 | 14.8k | } |
63 | | |
64 | | // Free the allocated memory. |
65 | 4.98k | lzma_end(&strm); |
66 | | |
67 | 4.98k | return 0; |
68 | 4.98k | } |