xref: /netbsd-src/external/bsd/zstd/dist/tests/fuzz/simple_decompress.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1*3117ece4Schristos /*
2*3117ece4Schristos  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*3117ece4Schristos  * All rights reserved.
4*3117ece4Schristos  *
5*3117ece4Schristos  * This source code is licensed under both the BSD-style license (found in the
6*3117ece4Schristos  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*3117ece4Schristos  * in the COPYING file in the root directory of this source tree).
8*3117ece4Schristos  * You may select, at your option, one of the above-listed licenses.
9*3117ece4Schristos  */
10*3117ece4Schristos 
11*3117ece4Schristos /**
12*3117ece4Schristos  * This fuzz target attempts to decompress the fuzzed data with the simple
13*3117ece4Schristos  * decompression function to ensure the decompressor never crashes.
14*3117ece4Schristos  */
15*3117ece4Schristos 
16*3117ece4Schristos #include <stddef.h>
17*3117ece4Schristos #include <stdlib.h>
18*3117ece4Schristos #include <stdio.h>
19*3117ece4Schristos 
20*3117ece4Schristos #define ZSTD_STATIC_LINKING_ONLY
21*3117ece4Schristos 
22*3117ece4Schristos #include "fuzz_helpers.h"
23*3117ece4Schristos #include "zstd.h"
24*3117ece4Schristos #include "fuzz_data_producer.h"
25*3117ece4Schristos 
26*3117ece4Schristos static ZSTD_DCtx *dctx = NULL;
27*3117ece4Schristos 
28*3117ece4Schristos int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
29*3117ece4Schristos {
30*3117ece4Schristos     /* Give a random portion of src data to the producer, to use for
31*3117ece4Schristos     parameter generation. The rest will be used for (de)compression */
32*3117ece4Schristos     FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
33*3117ece4Schristos     size = FUZZ_dataProducer_reserveDataPrefix(producer);
34*3117ece4Schristos 
35*3117ece4Schristos     if (!dctx) {
36*3117ece4Schristos         dctx = ZSTD_createDCtx();
37*3117ece4Schristos         FUZZ_ASSERT(dctx);
38*3117ece4Schristos     }
39*3117ece4Schristos 
40*3117ece4Schristos     {
41*3117ece4Schristos         size_t const bufSize = FUZZ_dataProducer_uint32Range(producer, 0, 10 * size);
42*3117ece4Schristos         void *rBuf = FUZZ_malloc(bufSize);
43*3117ece4Schristos         size_t const dSize = ZSTD_decompressDCtx(dctx, rBuf, bufSize, src, size);
44*3117ece4Schristos         if (!ZSTD_isError(dSize)) {
45*3117ece4Schristos             /* If decompression was successful, the content size from the frame header(s) should be valid. */
46*3117ece4Schristos             unsigned long long const expectedSize = ZSTD_findDecompressedSize(src, size);
47*3117ece4Schristos             FUZZ_ASSERT(expectedSize != ZSTD_CONTENTSIZE_ERROR);
48*3117ece4Schristos             FUZZ_ASSERT(expectedSize == ZSTD_CONTENTSIZE_UNKNOWN || expectedSize == dSize);
49*3117ece4Schristos         }
50*3117ece4Schristos         free(rBuf);
51*3117ece4Schristos     }
52*3117ece4Schristos 
53*3117ece4Schristos     FUZZ_dataProducer_free(producer);
54*3117ece4Schristos 
55*3117ece4Schristos #ifndef STATEFUL_FUZZING
56*3117ece4Schristos     ZSTD_freeDCtx(dctx); dctx = NULL;
57*3117ece4Schristos #endif
58*3117ece4Schristos     return 0;
59*3117ece4Schristos }
60