Coverage Report

Created: 2026-06-10 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/lz4/ossfuzz/round_trip_stream_fuzzer.c
Line
Count
Source
1
/**
2
 * This fuzz target performs a lz4 streaming round-trip test
3
 * (compress & decompress), compares the result with the original, and calls
4
 * abort() on corruption.
5
 */
6
7
#include <stddef.h>
8
#include <stdint.h>
9
#include <stdlib.h>
10
#include <string.h>
11
12
#include "fuzz_helpers.h"
13
#define LZ4_STATIC_LINKING_ONLY
14
#include "lz4.h"
15
#define LZ4_HC_STATIC_LINKING_ONLY
16
#include "lz4hc.h"
17
18
typedef struct {
19
  char const* buf;
20
  size_t size;
21
  size_t pos;
22
} const_cursor_t;
23
24
typedef struct {
25
  char* buf;
26
  size_t size;
27
  size_t pos;
28
} cursor_t;
29
30
typedef struct {
31
  LZ4_stream_t* cstream;
32
  LZ4_streamHC_t* cstreamHC;
33
  LZ4_streamDecode_t* dstream;
34
  const_cursor_t data;
35
  cursor_t compressed;
36
  cursor_t roundTrip;
37
  uint32_t seed;
38
  int level;
39
} state_t;
40
41
cursor_t cursor_create(size_t size)
42
116k
{
43
116k
  cursor_t cursor;
44
116k
  cursor.buf = (char*)malloc(size);
45
116k
  cursor.size = size;
46
116k
  cursor.pos = 0;
47
116k
  FUZZ_ASSERT(cursor.buf);
48
116k
  return cursor;
49
116k
}
50
51
typedef void (*round_trip_t)(state_t* state);
52
53
void cursor_free(cursor_t cursor)
54
116k
{
55
116k
    free(cursor.buf);
56
116k
}
57
58
state_t state_create(char const* data, size_t size, uint32_t seed)
59
17.9k
{
60
17.9k
    state_t state;
61
62
17.9k
    state.seed = seed;
63
64
17.9k
    state.data.buf = (char const*)data;
65
17.9k
    state.data.size = size;
66
17.9k
    state.data.pos = 0;
67
68
    /* Extra margin because we are streaming. */
69
17.9k
    state.compressed = cursor_create(1024 + 2 * LZ4_compressBound(size));
70
17.9k
    state.roundTrip = cursor_create(size);
71
72
17.9k
    state.cstream = LZ4_createStream();
73
17.9k
    FUZZ_ASSERT(state.cstream);
74
17.9k
    state.cstreamHC = LZ4_createStreamHC();
75
17.9k
    FUZZ_ASSERT(state.cstream);
76
17.9k
    state.dstream = LZ4_createStreamDecode();
77
17.9k
    FUZZ_ASSERT(state.dstream);
78
79
17.9k
    return state;
80
17.9k
}
81
82
void state_free(state_t state)
83
17.9k
{
84
17.9k
    cursor_free(state.compressed);
85
17.9k
    cursor_free(state.roundTrip);
86
17.9k
    LZ4_freeStream(state.cstream);
87
17.9k
    LZ4_freeStreamHC(state.cstreamHC);
88
17.9k
    LZ4_freeStreamDecode(state.dstream);
89
17.9k
}
90
91
static void state_reset(state_t* state, uint32_t seed)
92
143k
{
93
143k
    state->level = FUZZ_rand32(&seed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX);
94
143k
    LZ4_resetStream_fast(state->cstream);
95
143k
    LZ4_resetStreamHC_fast(state->cstreamHC, state->level);
96
143k
    LZ4_setStreamDecode(state->dstream, NULL, 0);
97
143k
    state->data.pos = 0;
98
143k
    state->compressed.pos = 0;
99
143k
    state->roundTrip.pos = 0;
100
143k
    state->seed = seed;
101
143k
}
102
103
static void state_decompress(state_t* state, char const* src, int srcSize)
104
775k
{
105
775k
    char* dst = state->roundTrip.buf + state->roundTrip.pos;
106
775k
    int const dstCapacity = state->roundTrip.size - state->roundTrip.pos;
107
775k
    int const dSize = LZ4_decompress_safe_continue(state->dstream, src, dst,
108
775k
                                                   srcSize, dstCapacity);
109
775k
    FUZZ_ASSERT(dSize >= 0);
110
775k
    state->roundTrip.pos += dSize;
111
775k
}
112
113
static void state_checkRoundTrip(state_t const* state)
114
143k
{
115
143k
    char const* data = state->data.buf;
116
143k
    size_t const size = state->data.size;
117
143k
    FUZZ_ASSERT_MSG(size == state->roundTrip.pos, "Incorrect size!");
118
143k
    FUZZ_ASSERT_MSG(!memcmp(data, state->roundTrip.buf, size), "Corruption!");
119
143k
}
120
121
/**
122
 * Picks a dictionary size and trims the dictionary off of the data.
123
 * We copy the dictionary to the roundTrip so our validation passes.
124
 */
125
static size_t state_trimDict(state_t* state)
126
71.8k
{
127
    /* 64 KB is the max dict size, allow slightly beyond that to test trim. */
128
71.8k
    uint32_t maxDictSize = MIN(70 * 1024, state->data.size);
129
71.8k
    size_t const dictSize = FUZZ_rand32(&state->seed, 0, maxDictSize);
130
71.8k
    DEBUGLOG(2, "dictSize = %zu", dictSize);
131
71.8k
    FUZZ_ASSERT(state->data.pos == 0);
132
71.8k
    FUZZ_ASSERT(state->roundTrip.pos == 0);
133
71.8k
    memcpy(state->roundTrip.buf, state->data.buf, dictSize);
134
71.8k
    state->data.pos += dictSize;
135
71.8k
    state->roundTrip.pos += dictSize;
136
71.8k
    return dictSize;
137
71.8k
}
138
139
static void state_prefixRoundTrip(state_t* state)
140
31.7k
{
141
213k
    while (state->data.pos != state->data.size) {
142
181k
        char const* src = state->data.buf + state->data.pos;
143
181k
        char* dst = state->compressed.buf + state->compressed.pos;
144
181k
        int const srcRemaining = state->data.size - state->data.pos;
145
181k
        int const srcSize = FUZZ_rand32(&state->seed, 0, srcRemaining);
146
181k
        int const dstCapacity = state->compressed.size - state->compressed.pos;
147
181k
        int const cSize = LZ4_compress_fast_continue(state->cstream, src, dst,
148
181k
                                                     srcSize, dstCapacity, 0);
149
181k
        FUZZ_ASSERT(cSize > 0);
150
181k
        state->data.pos += srcSize;
151
181k
        state->compressed.pos += cSize;
152
181k
        state_decompress(state, dst, cSize);
153
181k
    }
154
31.7k
}
155
156
static void state_extDictRoundTrip(state_t* state)
157
40.0k
{
158
40.0k
    int i = 0;
159
40.0k
    cursor_t data2 = cursor_create(state->data.size);
160
40.0k
    memcpy(data2.buf, state->data.buf, state->data.size);
161
245k
    while (state->data.pos != state->data.size) {
162
205k
        char const* data = (i++ & 1) ? state->data.buf : data2.buf;
163
205k
        char const* src = data + state->data.pos;
164
205k
        char* dst = state->compressed.buf + state->compressed.pos;
165
205k
        int const srcRemaining = state->data.size - state->data.pos;
166
205k
        int const srcSize = FUZZ_rand32(&state->seed, 0, srcRemaining);
167
205k
        int const dstCapacity = state->compressed.size - state->compressed.pos;
168
205k
        int const cSize = LZ4_compress_fast_continue(state->cstream, src, dst,
169
205k
                                                     srcSize, dstCapacity, 0);
170
205k
        FUZZ_ASSERT(cSize > 0);
171
205k
        state->data.pos += srcSize;
172
205k
        state->compressed.pos += cSize;
173
205k
        state_decompress(state, dst, cSize);
174
205k
    }
175
40.0k
    cursor_free(data2);
176
40.0k
}
177
178
static void state_randomRoundTrip(state_t* state, round_trip_t rt0,
179
                                  round_trip_t rt1)
180
71.8k
{
181
71.8k
    if (FUZZ_rand32(&state->seed, 0, 1)) {
182
27.6k
      rt0(state);
183
44.1k
    } else {
184
44.1k
      rt1(state);
185
44.1k
    }
186
71.8k
}
187
188
static void state_loadDictRoundTrip(state_t* state)
189
17.9k
{
190
17.9k
    char const* dict = state->data.buf;
191
17.9k
    size_t const dictSize = state_trimDict(state);
192
17.9k
    LZ4_loadDict(state->cstream, dict, dictSize);
193
17.9k
    LZ4_setStreamDecode(state->dstream, dict, dictSize);
194
17.9k
    state_randomRoundTrip(state, state_prefixRoundTrip, state_extDictRoundTrip);
195
17.9k
}
196
197
static void state_attachDictRoundTrip(state_t* state)
198
17.9k
{
199
17.9k
    char const* dict = state->data.buf;
200
17.9k
    size_t const dictSize = state_trimDict(state);
201
17.9k
    LZ4_stream_t* dictStream = LZ4_createStream();
202
17.9k
    LZ4_loadDict(dictStream, dict, dictSize);
203
17.9k
    LZ4_attach_dictionary(state->cstream, dictStream);
204
17.9k
    LZ4_setStreamDecode(state->dstream, dict, dictSize);
205
17.9k
    state_randomRoundTrip(state, state_prefixRoundTrip, state_extDictRoundTrip);
206
17.9k
    LZ4_freeStream(dictStream);
207
17.9k
}
208
209
static void state_prefixHCRoundTrip(state_t* state)
210
31.7k
{
211
213k
    while (state->data.pos != state->data.size) {
212
181k
        char const* src = state->data.buf + state->data.pos;
213
181k
        char* dst = state->compressed.buf + state->compressed.pos;
214
181k
        int const srcRemaining = state->data.size - state->data.pos;
215
181k
        int const srcSize = FUZZ_rand32(&state->seed, 0, srcRemaining);
216
181k
        int const dstCapacity = state->compressed.size - state->compressed.pos;
217
181k
        int const cSize = LZ4_compress_HC_continue(state->cstreamHC, src, dst,
218
181k
                                                   srcSize, dstCapacity);
219
181k
        FUZZ_ASSERT(cSize > 0);
220
181k
        state->data.pos += srcSize;
221
181k
        state->compressed.pos += cSize;
222
181k
        state_decompress(state, dst, cSize);
223
181k
    }
224
31.7k
}
225
226
static void state_extDictHCRoundTrip(state_t* state)
227
40.0k
{
228
40.0k
    int i = 0;
229
40.0k
    cursor_t data2 = cursor_create(state->data.size);
230
40.0k
    DEBUGLOG(2, "extDictHC");
231
40.0k
    memcpy(data2.buf, state->data.buf, state->data.size);
232
245k
    while (state->data.pos != state->data.size) {
233
205k
        char const* data = (i++ & 1) ? state->data.buf : data2.buf;
234
205k
        char const* src = data + state->data.pos;
235
205k
        char* dst = state->compressed.buf + state->compressed.pos;
236
205k
        int const srcRemaining = state->data.size - state->data.pos;
237
205k
        int const srcSize = FUZZ_rand32(&state->seed, 0, srcRemaining);
238
205k
        int const dstCapacity = state->compressed.size - state->compressed.pos;
239
205k
        int const cSize = LZ4_compress_HC_continue(state->cstreamHC, src, dst,
240
205k
                                                   srcSize, dstCapacity);
241
205k
        FUZZ_ASSERT(cSize > 0);
242
205k
        DEBUGLOG(2, "srcSize = %d", srcSize);
243
205k
        state->data.pos += srcSize;
244
205k
        state->compressed.pos += cSize;
245
205k
        state_decompress(state, dst, cSize);
246
205k
    }
247
40.0k
    cursor_free(data2);
248
40.0k
}
249
250
static void state_loadDictHCRoundTrip(state_t* state)
251
17.9k
{
252
17.9k
    char const* dict = state->data.buf;
253
17.9k
    size_t const dictSize = state_trimDict(state);
254
17.9k
    LZ4_loadDictHC(state->cstreamHC, dict, dictSize);
255
17.9k
    LZ4_setStreamDecode(state->dstream, dict, dictSize);
256
17.9k
    state_randomRoundTrip(state, state_prefixHCRoundTrip,
257
17.9k
                          state_extDictHCRoundTrip);
258
17.9k
}
259
260
static void state_attachDictHCRoundTrip(state_t* state)
261
17.9k
{
262
17.9k
    char const* dict = state->data.buf;
263
17.9k
    size_t const dictSize = state_trimDict(state);
264
17.9k
    LZ4_streamHC_t* dictStream = LZ4_createStreamHC();
265
17.9k
    LZ4_setCompressionLevel(dictStream, state->level);
266
17.9k
    LZ4_loadDictHC(dictStream, dict, dictSize);
267
17.9k
    LZ4_attach_HC_dictionary(state->cstreamHC, dictStream);
268
17.9k
    LZ4_setStreamDecode(state->dstream, dict, dictSize);
269
17.9k
    state_randomRoundTrip(state, state_prefixHCRoundTrip,
270
17.9k
                          state_extDictHCRoundTrip);
271
17.9k
    LZ4_freeStreamHC(dictStream);
272
17.9k
}
273
274
round_trip_t roundTrips[] = {
275
  &state_prefixRoundTrip,
276
  &state_extDictRoundTrip,
277
  &state_loadDictRoundTrip,
278
  &state_attachDictRoundTrip,
279
  &state_prefixHCRoundTrip,
280
  &state_extDictHCRoundTrip,
281
  &state_loadDictHCRoundTrip,
282
  &state_attachDictHCRoundTrip,
283
};
284
285
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
286
17.9k
{
287
17.9k
    uint32_t seed = FUZZ_seed(&data, &size);
288
17.9k
    state_t state = state_create((char const*)data, size, seed);
289
17.9k
    const int n = sizeof(roundTrips) / sizeof(round_trip_t);
290
17.9k
    int i;
291
292
161k
    for (i = 0; i < n; ++i) {
293
143k
        DEBUGLOG(2, "Round trip %d", i);
294
143k
        state_reset(&state, seed);
295
143k
        roundTrips[i](&state);
296
143k
        state_checkRoundTrip(&state);
297
143k
    }
298
299
17.9k
    state_free(state);
300
301
17.9k
    return 0;
302
17.9k
}