Coverage Report

Created: 2026-12-01 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/example_large_fuzzer.c
Line
Count
Source
1
/* Copyright 2018 Google LLC
2
Licensed under the Apache License, Version 2.0 (the "License");
3
you may not use this file except in compliance with the License.
4
You may obtain a copy of the License at
5
      http://www.apache.org/licenses/LICENSE-2.0
6
Unless required by applicable law or agreed to in writing, software
7
distributed under the License is distributed on an "AS IS" BASIS,
8
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
See the License for the specific language governing permissions and
10
limitations under the License.
11
*/
12
13
#include <stdio.h>
14
#include <stddef.h>
15
#include <stdint.h>
16
#include <string.h>
17
#include <assert.h>
18
#include <stdlib.h>
19
#include <inttypes.h>
20
#include "zlib.h"
21
22
10.3k
#define CHECK_ERR(err, msg) { \
23
10.3k
    if (err != Z_OK) { \
24
0
        fprintf(stderr, "%s error: %d\n", msg, err); \
25
0
        return 0; \
26
0
    } \
27
10.3k
}
28
29
static const uint8_t *data;
30
static size_t dataLen;
31
static alloc_func zalloc = NULL;
32
static free_func zfree = NULL;
33
static unsigned int diff;
34
35
/* ===========================================================================
36
 * Test deflate() with large buffers and dynamic change of compression level
37
 */
38
int test_large_deflate(unsigned char *compr, size_t comprLen,
39
1.15k
                        unsigned char *uncompr, size_t uncomprLen) {
40
1.15k
  z_stream c_stream; /* compression stream */
41
1.15k
  int err;
42
43
1.15k
  c_stream.zalloc = zalloc;
44
1.15k
  c_stream.zfree = zfree;
45
1.15k
  c_stream.opaque = (void *)0;
46
47
1.15k
  err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
48
1.15k
  CHECK_ERR(err, "deflateInit");
49
50
1.15k
  c_stream.next_out = compr;
51
1.15k
  c_stream.avail_out = (unsigned int)comprLen;
52
53
  /* At this point, uncompr is still mostly zeroes, so it should compress
54
   * very well:
55
   */
56
1.15k
  c_stream.next_in = uncompr;
57
1.15k
  c_stream.avail_in = (unsigned int)uncomprLen;
58
1.15k
  err = deflate(&c_stream, Z_NO_FLUSH);
59
1.15k
  CHECK_ERR(err, "deflate large 1");
60
1.15k
  if (c_stream.avail_in != 0) {
61
0
    fprintf(stderr, "deflate not greedy\n");
62
0
    return 0;
63
0
  }
64
65
  /* Feed in already compressed data and switch to no compression: */
66
1.15k
  deflateParams(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY);
67
1.15k
  c_stream.next_in = compr;
68
1.15k
  diff = (unsigned int)(c_stream.next_out - compr);
69
1.15k
  c_stream.avail_in = diff;
70
1.15k
  err = deflate(&c_stream, Z_NO_FLUSH);
71
1.15k
  CHECK_ERR(err, "deflate large 2");
72
73
  /* Switch back to compressing mode: */
74
1.15k
  deflateParams(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED);
75
1.15k
  c_stream.next_in = uncompr;
76
1.15k
  c_stream.avail_in = (unsigned int)uncomprLen;
77
1.15k
  err = deflate(&c_stream, Z_NO_FLUSH);
78
1.15k
  CHECK_ERR(err, "deflate large 3");
79
80
1.15k
  err = deflate(&c_stream, Z_FINISH);
81
1.15k
  if (err != Z_STREAM_END) {
82
0
    fprintf(stderr, "deflate large should report Z_STREAM_END\n");
83
0
    return 0;
84
0
  }
85
1.15k
  err = deflateEnd(&c_stream);
86
1.15k
  CHECK_ERR(err, "deflateEnd");
87
1.15k
  return 0;
88
1.15k
}
89
90
/* ===========================================================================
91
 * Test inflate() with large buffers
92
 */
93
int test_large_inflate(unsigned char *compr, size_t comprLen,
94
1.15k
                        unsigned char *uncompr, size_t uncomprLen) {
95
1.15k
  int err;
96
1.15k
  z_stream d_stream; /* decompression stream */
97
98
1.15k
  d_stream.zalloc = zalloc;
99
1.15k
  d_stream.zfree = zfree;
100
1.15k
  d_stream.opaque = (void *)0;
101
102
1.15k
  d_stream.next_in = compr;
103
1.15k
  d_stream.avail_in = (unsigned int)comprLen;
104
105
1.15k
  err = inflateInit(&d_stream);
106
1.15k
  CHECK_ERR(err, "inflateInit");
107
108
3.45k
  for (;;) {
109
3.45k
    d_stream.next_out = uncompr; /* discard the output */
110
3.45k
    d_stream.avail_out = (unsigned int)uncomprLen;
111
3.45k
    err = inflate(&d_stream, Z_NO_FLUSH);
112
3.45k
    if (err == Z_STREAM_END)
113
1.15k
      break;
114
2.30k
    CHECK_ERR(err, "large inflate");
115
2.30k
  }
116
117
1.15k
  err = inflateEnd(&d_stream);
118
1.15k
  CHECK_ERR(err, "inflateEnd");
119
120
1.15k
  if (d_stream.total_out != 2 * uncomprLen + diff) {
121
0
    fprintf(stderr, "bad large inflate: %zu\n", d_stream.total_out);
122
0
    return 0;
123
0
  }
124
1.15k
  return 0;
125
1.15k
}
126
127
1.15k
int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) {
128
1.15k
  size_t comprLen = 100 + 3 * size;
129
1.15k
  size_t uncomprLen = comprLen;
130
1.15k
  uint8_t *compr, *uncompr;
131
132
  /* Discard inputs larger than 512Kb. */
133
1.15k
  static size_t kMaxSize = 512 * 1024;
134
135
1.15k
  if (size < 1 || size > kMaxSize)
136
4
    return 0;
137
138
1.15k
  data = d;
139
1.15k
  dataLen = size;
140
1.15k
  compr = (uint8_t *)calloc(1, comprLen);
141
1.15k
  uncompr = (uint8_t *)calloc(1, uncomprLen);
142
1.15k
  if (compr == NULL || uncompr == NULL) {
143
0
    free(compr);
144
0
    free(uncompr);
145
0
    return 0;
146
0
  }
147
148
1.15k
  test_large_deflate(compr, comprLen, uncompr, uncomprLen);
149
1.15k
  test_large_inflate(compr, comprLen, uncompr, uncomprLen);
150
151
1.15k
  free(compr);
152
1.15k
  free(uncompr);
153
154
  /* This function must return 0. */
155
1.15k
  return 0;
156
1.15k
}