/src/zlib-ng/test/fuzz/fuzzer_example_flush.c
Line | Count | Source |
1 | | #include "zbuild.h" |
2 | | #include <assert.h> |
3 | | #ifdef ZLIB_COMPAT |
4 | | # include "zlib.h" |
5 | | #else |
6 | | # include "zlib-ng.h" |
7 | | #endif |
8 | | |
9 | 33.3k | #define CHECK_ERR(err, msg) { \ |
10 | 33.3k | if (err != Z_OK) { \ |
11 | 0 | fprintf(stderr, "%s error: %d\n", msg, err); \ |
12 | 0 | exit(1); \ |
13 | 0 | } \ |
14 | 33.3k | } |
15 | | |
16 | | static const uint8_t *data; |
17 | | static size_t dataLen; |
18 | | static alloc_func zalloc = NULL; |
19 | | static free_func zfree = NULL; |
20 | | |
21 | | /* =========================================================================== |
22 | | * Test deflate() with full flush |
23 | | */ |
24 | 4.77k | void test_flush(unsigned char *compr, z_size_t *comprLen) { |
25 | 4.77k | PREFIX3(stream) c_stream; /* compression stream */ |
26 | 4.77k | int err; |
27 | 4.77k | unsigned int len = (unsigned int)dataLen; |
28 | | |
29 | 4.77k | c_stream.zalloc = zalloc; |
30 | 4.77k | c_stream.zfree = zfree; |
31 | 4.77k | c_stream.opaque = (void *)0; |
32 | | |
33 | 4.77k | err = PREFIX(deflateInit)(&c_stream, Z_DEFAULT_COMPRESSION); |
34 | 4.77k | CHECK_ERR(err, "deflateInit"); |
35 | | |
36 | 4.77k | c_stream.next_in = (z_const unsigned char *)data; |
37 | 4.77k | c_stream.next_out = compr; |
38 | 4.77k | c_stream.avail_in = 3; |
39 | 4.77k | c_stream.avail_out = (unsigned int)*comprLen; |
40 | 4.77k | err = PREFIX(deflate)(&c_stream, Z_FULL_FLUSH); |
41 | 4.77k | CHECK_ERR(err, "deflate flush 1"); |
42 | | |
43 | 4.77k | compr[3] = (unsigned char)(compr[3] + 1); /* force an error in first compressed block */ |
44 | 4.77k | c_stream.avail_in = len - 3; |
45 | | |
46 | 4.77k | err = PREFIX(deflate)(&c_stream, Z_FINISH); |
47 | 4.77k | if (err != Z_STREAM_END) { |
48 | 0 | CHECK_ERR(err, "deflate flush 2"); |
49 | 0 | } |
50 | 4.77k | err = PREFIX(deflateEnd)(&c_stream); |
51 | 4.77k | CHECK_ERR(err, "deflateEnd"); |
52 | | |
53 | 4.77k | *comprLen = (z_size_t)c_stream.total_out; |
54 | 4.77k | } |
55 | | |
56 | | /* =========================================================================== |
57 | | * Test inflateSync() |
58 | | */ |
59 | 4.77k | void test_sync(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) { |
60 | 4.77k | int err; |
61 | 4.77k | PREFIX3(stream) d_stream; /* decompression stream */ |
62 | | |
63 | 4.77k | d_stream.zalloc = zalloc; |
64 | 4.77k | d_stream.zfree = zfree; |
65 | 4.77k | d_stream.opaque = (void *)0; |
66 | | |
67 | 4.77k | d_stream.next_in = compr; |
68 | 4.77k | d_stream.avail_in = 2; /* just read the zlib header */ |
69 | | |
70 | 4.77k | err = PREFIX(inflateInit)(&d_stream); |
71 | 4.77k | CHECK_ERR(err, "inflateInit"); |
72 | | |
73 | 4.77k | d_stream.next_out = uncompr; |
74 | 4.77k | d_stream.avail_out = (unsigned int)uncomprLen; |
75 | | |
76 | 4.77k | err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); |
77 | 4.77k | CHECK_ERR(err, "inflate"); |
78 | | |
79 | 4.77k | d_stream.avail_in = (unsigned int)comprLen - 2; /* read all compressed data */ |
80 | 4.77k | err = PREFIX(inflateSync)(&d_stream); /* but skip the damaged part */ |
81 | 4.77k | CHECK_ERR(err, "inflateSync"); |
82 | | |
83 | 4.77k | err = PREFIX(inflate)(&d_stream, Z_FINISH); |
84 | 4.77k | if (err != Z_STREAM_END) { |
85 | 0 | fprintf(stderr, "inflate should report Z_STREAM_END\n"); |
86 | 0 | exit(1); |
87 | 0 | } |
88 | 4.77k | err = PREFIX(inflateEnd)(&d_stream); |
89 | 4.77k | CHECK_ERR(err, "inflateEnd"); |
90 | 4.77k | } |
91 | | |
92 | 4.77k | int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) { |
93 | 4.77k | z_size_t comprLen = 100 + 2 * PREFIX(compressBound)((z_uintmax_t)size); |
94 | 4.77k | z_size_t uncomprLen = (z_size_t)size; |
95 | 4.77k | uint8_t *compr, *uncompr; |
96 | | |
97 | | /* Discard inputs larger than 1Mb. */ |
98 | 4.77k | static size_t kMaxSize = 1024 * 1024; |
99 | | |
100 | | // This test requires at least 3 bytes of input data. |
101 | 4.77k | if (size <= 3 || size > kMaxSize) |
102 | 3 | return 0; |
103 | | |
104 | 4.77k | data = d; |
105 | 4.77k | dataLen = size; |
106 | 4.77k | compr = (uint8_t *)calloc(1, comprLen); |
107 | 4.77k | uncompr = (uint8_t *)calloc(1, uncomprLen); |
108 | | |
109 | 4.77k | test_flush(compr, &comprLen); |
110 | 4.77k | test_sync(compr, comprLen, uncompr, uncomprLen); |
111 | | |
112 | 4.77k | free(compr); |
113 | 4.77k | free(uncompr); |
114 | | |
115 | | /* This function must return 0. */ |
116 | 4.77k | return 0; |
117 | 4.77k | } |