xref: /aosp_15_r20/external/zstd/tests/fuzz/generate_sequences.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 #define ZSTD_STATIC_LINKING_ONLY
12*01826a49SYabin Cui 
13*01826a49SYabin Cui #include <stddef.h>
14*01826a49SYabin Cui #include <stdint.h>
15*01826a49SYabin Cui #include <string.h>
16*01826a49SYabin Cui #include <stdlib.h>
17*01826a49SYabin Cui 
18*01826a49SYabin Cui #include "fuzz_data_producer.h"
19*01826a49SYabin Cui #include "fuzz_helpers.h"
20*01826a49SYabin Cui #include "zstd_helpers.h"
21*01826a49SYabin Cui 
22*01826a49SYabin Cui /**
23*01826a49SYabin Cui  * This fuzz target ensures that ZSTD_generateSequences() does not crash and
24*01826a49SYabin Cui  * if it succeeds that ZSTD_compressSequences() round trips.
25*01826a49SYabin Cui  */
26*01826a49SYabin Cui 
testRoundTrip(ZSTD_CCtx * cctx,ZSTD_Sequence const * seqs,size_t nbSeqs,const void * src,size_t srcSize)27*01826a49SYabin Cui static void testRoundTrip(ZSTD_CCtx* cctx, ZSTD_Sequence const* seqs, size_t nbSeqs, const void* src, size_t srcSize) {
28*01826a49SYabin Cui   /* Compress the sequences with block delimiters */
29*01826a49SYabin Cui   const size_t compressBound = ZSTD_compressBound(srcSize);
30*01826a49SYabin Cui   void* dst = FUZZ_malloc(compressBound);
31*01826a49SYabin Cui   FUZZ_ASSERT(dst);
32*01826a49SYabin Cui 
33*01826a49SYabin Cui   size_t compressedSize = ZSTD_compressSequences(cctx, dst, compressBound, seqs, nbSeqs, src, srcSize);
34*01826a49SYabin Cui   FUZZ_ZASSERT(compressedSize);
35*01826a49SYabin Cui 
36*01826a49SYabin Cui   void* decompressed = FUZZ_malloc(srcSize);
37*01826a49SYabin Cui   FUZZ_ASSERT(srcSize == 0 || decompressed);
38*01826a49SYabin Cui   size_t decompressedSize = ZSTD_decompress(decompressed, srcSize, dst, compressedSize);
39*01826a49SYabin Cui   FUZZ_ZASSERT(decompressedSize);
40*01826a49SYabin Cui   FUZZ_ASSERT(decompressedSize == srcSize);
41*01826a49SYabin Cui   if (srcSize != 0) {
42*01826a49SYabin Cui     FUZZ_ASSERT(!memcmp(src, decompressed, srcSize));
43*01826a49SYabin Cui   }
44*01826a49SYabin Cui 
45*01826a49SYabin Cui   free(decompressed);
46*01826a49SYabin Cui   free(dst);
47*01826a49SYabin Cui }
48*01826a49SYabin Cui 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)49*01826a49SYabin Cui int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
50*01826a49SYabin Cui 
51*01826a49SYabin Cui   FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size);
52*01826a49SYabin Cui   size = FUZZ_dataProducer_reserveDataPrefix(producer);
53*01826a49SYabin Cui 
54*01826a49SYabin Cui   ZSTD_CCtx* cctx = ZSTD_createCCtx();
55*01826a49SYabin Cui   FUZZ_ASSERT(cctx);
56*01826a49SYabin Cui 
57*01826a49SYabin Cui   const size_t seqsCapacity = FUZZ_dataProducer_uint32Range(producer, 0, 2 * ZSTD_sequenceBound(size));
58*01826a49SYabin Cui   ZSTD_Sequence* seqs = (ZSTD_Sequence*)FUZZ_malloc(sizeof(ZSTD_Sequence) * seqsCapacity);
59*01826a49SYabin Cui   FUZZ_ASSERT(seqsCapacity == 0 || seqs);
60*01826a49SYabin Cui 
61*01826a49SYabin Cui   FUZZ_setRandomParameters(cctx, size, producer);
62*01826a49SYabin Cui   FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetCBlockSize, 0));
63*01826a49SYabin Cui   FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 0));
64*01826a49SYabin Cui 
65*01826a49SYabin Cui   const size_t nbSeqs = ZSTD_generateSequences(cctx, seqs, seqsCapacity, data, size);
66*01826a49SYabin Cui   if (ZSTD_isError(nbSeqs)) {
67*01826a49SYabin Cui     /* Allowed to error if the destination is too small */
68*01826a49SYabin Cui     if (ZSTD_getErrorCode(nbSeqs) == ZSTD_error_dstSize_tooSmall) {
69*01826a49SYabin Cui         FUZZ_ASSERT(seqsCapacity < ZSTD_sequenceBound(size));
70*01826a49SYabin Cui     }
71*01826a49SYabin Cui   } else {
72*01826a49SYabin Cui     /* Ensure we round trip with and without block delimiters*/
73*01826a49SYabin Cui 
74*01826a49SYabin Cui     FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_blockDelimiters, ZSTD_sf_explicitBlockDelimiters));
75*01826a49SYabin Cui     testRoundTrip(cctx, seqs, nbSeqs, data, size);
76*01826a49SYabin Cui 
77*01826a49SYabin Cui     const size_t nbMergedSeqs = ZSTD_mergeBlockDelimiters(seqs, nbSeqs);
78*01826a49SYabin Cui     FUZZ_ASSERT(nbMergedSeqs <= nbSeqs);
79*01826a49SYabin Cui     FUZZ_ZASSERT(ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only));
80*01826a49SYabin Cui     FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_blockDelimiters, ZSTD_sf_noBlockDelimiters));
81*01826a49SYabin Cui     testRoundTrip(cctx, seqs, nbMergedSeqs, data, size);
82*01826a49SYabin Cui   }
83*01826a49SYabin Cui 
84*01826a49SYabin Cui   free(seqs);
85*01826a49SYabin Cui   ZSTD_freeCCtx(cctx);
86*01826a49SYabin Cui   FUZZ_dataProducer_free(producer);
87*01826a49SYabin Cui   return 0;
88*01826a49SYabin Cui }
89