xref: /aosp_15_r20/external/zstd/tests/fuzz/stream_decompress.c (revision 01826a4963a0d8a59bc3812d29bdf0fb76416722)
1*01826a49SYabin Cui /*
2*01826a49SYabin Cui  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui  * All rights reserved.
4*01826a49SYabin Cui  *
5*01826a49SYabin Cui  * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui  * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui  * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui  */
10*01826a49SYabin Cui 
11*01826a49SYabin Cui /**
12*01826a49SYabin Cui  * This fuzz target attempts to decompress the fuzzed data with the simple
13*01826a49SYabin Cui  * decompression function to ensure the decompressor never crashes.
14*01826a49SYabin Cui  */
15*01826a49SYabin Cui 
16*01826a49SYabin Cui #define ZSTD_STATIC_LINKING_ONLY
17*01826a49SYabin Cui 
18*01826a49SYabin Cui #include <stddef.h>
19*01826a49SYabin Cui #include <stdlib.h>
20*01826a49SYabin Cui #include <stdio.h>
21*01826a49SYabin Cui #include "fuzz_helpers.h"
22*01826a49SYabin Cui #include "zstd.h"
23*01826a49SYabin Cui #include "fuzz_data_producer.h"
24*01826a49SYabin Cui 
25*01826a49SYabin Cui static ZSTD_DStream *dstream = NULL;
26*01826a49SYabin Cui uint32_t seed;
27*01826a49SYabin Cui 
makeOutBuffer(FUZZ_dataProducer_t * producer,void * buf,size_t bufSize)28*01826a49SYabin Cui static ZSTD_outBuffer makeOutBuffer(FUZZ_dataProducer_t *producer, void* buf, size_t bufSize)
29*01826a49SYabin Cui {
30*01826a49SYabin Cui   ZSTD_outBuffer buffer = { buf, 0, 0 };
31*01826a49SYabin Cui 
32*01826a49SYabin Cui   if (FUZZ_dataProducer_empty(producer)) {
33*01826a49SYabin Cui     buffer.size = bufSize;
34*01826a49SYabin Cui   } else {
35*01826a49SYabin Cui     buffer.size = (FUZZ_dataProducer_uint32Range(producer, 0, bufSize));
36*01826a49SYabin Cui   }
37*01826a49SYabin Cui   FUZZ_ASSERT(buffer.size <= bufSize);
38*01826a49SYabin Cui 
39*01826a49SYabin Cui   if (buffer.size == 0) {
40*01826a49SYabin Cui     buffer.dst = NULL;
41*01826a49SYabin Cui   }
42*01826a49SYabin Cui 
43*01826a49SYabin Cui   return buffer;
44*01826a49SYabin Cui }
45*01826a49SYabin Cui 
makeInBuffer(const uint8_t ** src,size_t * size,FUZZ_dataProducer_t * producer)46*01826a49SYabin Cui static ZSTD_inBuffer makeInBuffer(const uint8_t **src, size_t *size,
47*01826a49SYabin Cui                                   FUZZ_dataProducer_t *producer)
48*01826a49SYabin Cui {
49*01826a49SYabin Cui   ZSTD_inBuffer buffer = { *src, 0, 0 };
50*01826a49SYabin Cui 
51*01826a49SYabin Cui   FUZZ_ASSERT(*size > 0);
52*01826a49SYabin Cui   if (FUZZ_dataProducer_empty(producer)) {
53*01826a49SYabin Cui     buffer.size = *size;
54*01826a49SYabin Cui   } else {
55*01826a49SYabin Cui     buffer.size = (FUZZ_dataProducer_uint32Range(producer, 0, *size));
56*01826a49SYabin Cui   }
57*01826a49SYabin Cui   FUZZ_ASSERT(buffer.size <= *size);
58*01826a49SYabin Cui   *src += buffer.size;
59*01826a49SYabin Cui   *size -= buffer.size;
60*01826a49SYabin Cui 
61*01826a49SYabin Cui   if (buffer.size == 0) {
62*01826a49SYabin Cui     buffer.src = NULL;
63*01826a49SYabin Cui   }
64*01826a49SYabin Cui 
65*01826a49SYabin Cui   return buffer;
66*01826a49SYabin Cui }
67*01826a49SYabin Cui 
LLVMFuzzerTestOneInput(const uint8_t * src,size_t size)68*01826a49SYabin Cui int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
69*01826a49SYabin Cui {
70*01826a49SYabin Cui     /* Give a random portion of src data to the producer, to use for
71*01826a49SYabin Cui     parameter generation. The rest will be used for (de)compression */
72*01826a49SYabin Cui     FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
73*01826a49SYabin Cui     int stableOutBuffer;
74*01826a49SYabin Cui     ZSTD_outBuffer out;
75*01826a49SYabin Cui     void* buf;
76*01826a49SYabin Cui     size_t bufSize;
77*01826a49SYabin Cui     size = FUZZ_dataProducer_reserveDataPrefix(producer);
78*01826a49SYabin Cui     bufSize = MAX(10 * size, ZSTD_BLOCKSIZE_MAX);
79*01826a49SYabin Cui 
80*01826a49SYabin Cui     /* Allocate all buffers and contexts if not already allocated */
81*01826a49SYabin Cui     buf = FUZZ_malloc(bufSize);
82*01826a49SYabin Cui 
83*01826a49SYabin Cui     if (!dstream) {
84*01826a49SYabin Cui         dstream = ZSTD_createDStream();
85*01826a49SYabin Cui         FUZZ_ASSERT(dstream);
86*01826a49SYabin Cui     } else {
87*01826a49SYabin Cui         FUZZ_ZASSERT(ZSTD_DCtx_reset(dstream, ZSTD_reset_session_only));
88*01826a49SYabin Cui     }
89*01826a49SYabin Cui 
90*01826a49SYabin Cui     stableOutBuffer = FUZZ_dataProducer_uint32Range(producer, 0, 10) == 5;
91*01826a49SYabin Cui     if (stableOutBuffer) {
92*01826a49SYabin Cui       FUZZ_ZASSERT(ZSTD_DCtx_setParameter(dstream, ZSTD_d_stableOutBuffer, 1));
93*01826a49SYabin Cui       out.dst = buf;
94*01826a49SYabin Cui       out.size = bufSize;
95*01826a49SYabin Cui       out.pos = 0;
96*01826a49SYabin Cui     } else {
97*01826a49SYabin Cui       out = makeOutBuffer(producer, buf, bufSize);
98*01826a49SYabin Cui     }
99*01826a49SYabin Cui 
100*01826a49SYabin Cui     while (size > 0) {
101*01826a49SYabin Cui         ZSTD_inBuffer in = makeInBuffer(&src, &size, producer);
102*01826a49SYabin Cui         do {
103*01826a49SYabin Cui             size_t const rc = ZSTD_decompressStream(dstream, &out, &in);
104*01826a49SYabin Cui             if (ZSTD_isError(rc)) goto error;
105*01826a49SYabin Cui             if (out.pos == out.size) {
106*01826a49SYabin Cui                 if (stableOutBuffer) goto error;
107*01826a49SYabin Cui                 out = makeOutBuffer(producer, buf, bufSize);
108*01826a49SYabin Cui             }
109*01826a49SYabin Cui         } while (in.pos != in.size);
110*01826a49SYabin Cui     }
111*01826a49SYabin Cui 
112*01826a49SYabin Cui error:
113*01826a49SYabin Cui #ifndef STATEFUL_FUZZING
114*01826a49SYabin Cui     ZSTD_freeDStream(dstream); dstream = NULL;
115*01826a49SYabin Cui #endif
116*01826a49SYabin Cui     FUZZ_dataProducer_free(producer);
117*01826a49SYabin Cui     free(buf);
118*01826a49SYabin Cui     return 0;
119*01826a49SYabin Cui }
120