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 compress the fuzzed data with the simple
13*01826a49SYabin Cui * compression function with an output buffer that may be too small to
14*01826a49SYabin Cui * ensure that the compressor never crashes.
15*01826a49SYabin Cui */
16*01826a49SYabin Cui
17*01826a49SYabin Cui #include <stddef.h>
18*01826a49SYabin Cui #include <stdlib.h>
19*01826a49SYabin Cui #include <stdio.h>
20*01826a49SYabin Cui #include "fuzz_helpers.h"
21*01826a49SYabin Cui #include "zstd.h"
22*01826a49SYabin Cui #include "zstd_errors.h"
23*01826a49SYabin Cui #include "zstd_helpers.h"
24*01826a49SYabin Cui #include "fuzz_data_producer.h"
25*01826a49SYabin Cui #include "fuzz_third_party_seq_prod.h"
26*01826a49SYabin Cui
27*01826a49SYabin Cui static ZSTD_CCtx *cctx = NULL;
28*01826a49SYabin Cui
LLVMFuzzerTestOneInput(const uint8_t * src,size_t size)29*01826a49SYabin Cui int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
30*01826a49SYabin Cui {
31*01826a49SYabin Cui FUZZ_SEQ_PROD_SETUP();
32*01826a49SYabin Cui
33*01826a49SYabin Cui /* Give a random portion of src data to the producer, to use for
34*01826a49SYabin Cui parameter generation. The rest will be used for (de)compression */
35*01826a49SYabin Cui FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
36*01826a49SYabin Cui size = FUZZ_dataProducer_reserveDataPrefix(producer);
37*01826a49SYabin Cui
38*01826a49SYabin Cui size_t const maxSize = ZSTD_compressBound(size);
39*01826a49SYabin Cui size_t const bufSize = FUZZ_dataProducer_uint32Range(producer, 0, maxSize);
40*01826a49SYabin Cui
41*01826a49SYabin Cui int const cLevel = FUZZ_dataProducer_int32Range(producer, kMinClevel, kMaxClevel);
42*01826a49SYabin Cui
43*01826a49SYabin Cui if (!cctx) {
44*01826a49SYabin Cui cctx = ZSTD_createCCtx();
45*01826a49SYabin Cui FUZZ_ASSERT(cctx);
46*01826a49SYabin Cui }
47*01826a49SYabin Cui
48*01826a49SYabin Cui void *rBuf = FUZZ_malloc(bufSize);
49*01826a49SYabin Cui size_t const ret = ZSTD_compressCCtx(cctx, rBuf, bufSize, src, size, cLevel);
50*01826a49SYabin Cui if (ZSTD_isError(ret)) {
51*01826a49SYabin Cui FUZZ_ASSERT(ZSTD_getErrorCode(ret) == ZSTD_error_dstSize_tooSmall);
52*01826a49SYabin Cui }
53*01826a49SYabin Cui free(rBuf);
54*01826a49SYabin Cui FUZZ_dataProducer_free(producer);
55*01826a49SYabin Cui #ifndef STATEFUL_FUZZING
56*01826a49SYabin Cui ZSTD_freeCCtx(cctx); cctx = NULL;
57*01826a49SYabin Cui #endif
58*01826a49SYabin Cui FUZZ_SEQ_PROD_TEARDOWN();
59*01826a49SYabin Cui return 0;
60*01826a49SYabin Cui }
61