Coverage Report

Created: 2024-07-27 06:07

/src/miniz/tests/large_fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
/* Derived from zlib fuzzers at http://github.com/google/oss-fuzz/tree/master/projects/zlib,
2
 * see ossfuzz.sh for full license text.
3
 */
4
5
#include <stdio.h>
6
#include <stddef.h>
7
#include <stdint.h>
8
#include <string.h>
9
#include <stdlib.h>
10
#include <inttypes.h>
11
12
#include "miniz.h"
13
14
#define CHECK_ERR(err, msg)                              \
15
2.67k
    {                                                    \
16
2.67k
        if (err != Z_OK)                                 \
17
2.67k
        {                                                \
18
0
            fprintf(stderr, "%s error: %d\n", msg, err); \
19
0
            exit(1);                                     \
20
0
        }                                                \
21
2.67k
    }
22
23
static const uint8_t *data;
24
static size_t dataLen;
25
static alloc_func zalloc = NULL;
26
static free_func zfree = NULL;
27
static unsigned int diff;
28
29
/* Test deflate() with large buffers and dynamic change of compression level */
30
void test_large_deflate(unsigned char *compr, size_t comprLen,
31
                        unsigned char *uncompr, size_t uncomprLen)
32
535
{
33
535
    z_stream c_stream; /* compression stream */
34
535
    int err;
35
36
535
    c_stream.zalloc = zalloc;
37
535
    c_stream.zfree = zfree;
38
535
    c_stream.opaque = NULL;
39
40
535
    err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
41
535
    CHECK_ERR(err, "deflateInit");
42
43
535
    c_stream.next_out = compr;
44
535
    c_stream.avail_out = (unsigned int)comprLen;
45
46
    /* At this point, uncompr is still mostly zeroes, so it should compress
47
     * very well:
48
     */
49
535
    c_stream.next_in = uncompr;
50
535
    c_stream.avail_in = (unsigned int)uncomprLen;
51
535
    err = deflate(&c_stream, Z_NO_FLUSH);
52
535
    CHECK_ERR(err, "deflate large 1");
53
54
535
    if (c_stream.avail_in != 0)
55
0
    {
56
0
        fprintf(stderr, "deflate not greedy\n");
57
0
        exit(1);
58
0
    }
59
60
    /* Feed in already compressed data: */
61
535
    c_stream.next_in = compr;
62
535
    diff = (unsigned int)(c_stream.next_out - compr);
63
535
    c_stream.avail_in = diff;
64
65
535
    deflate(&c_stream, Z_NO_FLUSH);
66
535
    err = deflate(&c_stream, Z_FINISH);
67
68
535
    if (err != Z_STREAM_END)
69
0
    {
70
0
        fprintf(stderr, "deflate large should report Z_STREAM_END\n");
71
0
        exit(1);
72
0
    }
73
535
    err = deflateEnd(&c_stream);
74
535
    CHECK_ERR(err, "deflateEnd");
75
535
}
76
77
/* Test inflate() with large buffers */
78
void test_large_inflate(unsigned char *compr, size_t comprLen,
79
                        unsigned char *uncompr, size_t uncomprLen)
80
535
{
81
535
    int err;
82
535
    z_stream d_stream; /* decompression stream */
83
84
535
    d_stream.zalloc = zalloc;
85
535
    d_stream.zfree = zfree;
86
535
    d_stream.opaque = NULL;
87
88
535
    d_stream.next_in = compr;
89
535
    d_stream.avail_in = (unsigned int)comprLen;
90
91
535
    err = inflateInit(&d_stream);
92
535
    CHECK_ERR(err, "inflateInit");
93
94
535
    for (;;)
95
535
    {
96
535
        d_stream.next_out = uncompr; /* discard the output */
97
535
        d_stream.avail_out = (unsigned int)uncomprLen;
98
535
        err = inflate(&d_stream, Z_NO_FLUSH);
99
535
        if (err == Z_STREAM_END)
100
535
            break;
101
102
0
        CHECK_ERR(err, "large inflate");
103
0
    }
104
105
535
    err = inflateEnd(&d_stream);
106
535
    CHECK_ERR(err, "inflateEnd");
107
535
}
108
109
int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size)
110
5.89k
{
111
5.89k
    size_t comprLen = 100 + 3 * size;
112
5.89k
    size_t uncomprLen = comprLen;
113
5.89k
    uint8_t *compr, *uncompr;
114
115
    /* Discard inputs larger than 512Kb. */
116
5.89k
    static size_t kMaxSize = 512 * 1024;
117
118
5.89k
    if (size < 1 || size > kMaxSize)
119
4
        return 0;
120
121
5.89k
    data = d;
122
5.89k
    dataLen = size;
123
5.89k
    compr = calloc(1, comprLen);
124
5.89k
    uncompr = calloc(1, uncomprLen);
125
126
5.89k
    test_large_deflate(compr, comprLen, uncompr, uncomprLen);
127
5.89k
    test_large_inflate(compr, comprLen, uncompr, uncomprLen);
128
129
5.89k
    free(compr);
130
5.89k
    free(uncompr);
131
132
5.89k
    return 0;
133
5.89k
}