Update to Zstandard 1.4.4
[freebsd.git] / sys / contrib / zstd / tests / fuzz / simple_round_trip.c
1 /*
2  * Copyright (c) 2016-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  */
9
10 /**
11  * This fuzz target performs a zstd round-trip test (compress & decompress),
12  * compares the result with the original, and calls abort() on corruption.
13  */
14
15 #define ZSTD_STATIC_LINKING_ONLY
16
17 #include <stddef.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include "fuzz_helpers.h"
22 #include "zstd_helpers.h"
23 #include "fuzz_data_producer.h"
24
25 static ZSTD_CCtx *cctx = NULL;
26 static ZSTD_DCtx *dctx = NULL;
27
28 static size_t roundTripTest(void *result, size_t resultCapacity,
29                             void *compressed, size_t compressedCapacity,
30                             const void *src, size_t srcSize,
31                             FUZZ_dataProducer_t *producer)
32 {
33     size_t cSize;
34     if (FUZZ_dataProducer_uint32Range(producer, 0, 1)) {
35         FUZZ_setRandomParameters(cctx, srcSize, producer);
36         cSize = ZSTD_compress2(cctx, compressed, compressedCapacity, src, srcSize);
37     } else {
38       int const cLevel = FUZZ_dataProducer_int32Range(producer, kMinClevel, kMaxClevel);
39
40         cSize = ZSTD_compressCCtx(
41             cctx, compressed, compressedCapacity, src, srcSize, cLevel);
42     }
43     FUZZ_ZASSERT(cSize);
44     return ZSTD_decompressDCtx(dctx, result, resultCapacity, compressed, cSize);
45 }
46
47 int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
48 {
49     size_t const rBufSize = size;
50     void* rBuf = malloc(rBufSize);
51     size_t cBufSize = ZSTD_compressBound(size);
52     void* cBuf;
53
54     /* Give a random portion of src data to the producer, to use for
55     parameter generation. The rest will be used for (de)compression */
56     FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
57     size = FUZZ_dataProducer_reserveDataPrefix(producer);
58
59     /* Half of the time fuzz with a 1 byte smaller output size.
60      * This will still succeed because we don't use a dictionary, so the dictID
61      * field is empty, giving us 4 bytes of overhead.
62      */
63     cBufSize -= FUZZ_dataProducer_uint32Range(producer, 0, 1);
64
65     cBuf = malloc(cBufSize);
66
67     FUZZ_ASSERT(cBuf && rBuf);
68
69     if (!cctx) {
70         cctx = ZSTD_createCCtx();
71         FUZZ_ASSERT(cctx);
72     }
73     if (!dctx) {
74         dctx = ZSTD_createDCtx();
75         FUZZ_ASSERT(dctx);
76     }
77
78     {
79         size_t const result =
80             roundTripTest(rBuf, rBufSize, cBuf, cBufSize, src, size, producer);
81         FUZZ_ZASSERT(result);
82         FUZZ_ASSERT_MSG(result == size, "Incorrect regenerated size");
83         FUZZ_ASSERT_MSG(!memcmp(src, rBuf, size), "Corruption!");
84     }
85     free(rBuf);
86     free(cBuf);
87     FUZZ_dataProducer_free(producer);
88 #ifndef STATEFUL_FUZZING
89     ZSTD_freeCCtx(cctx); cctx = NULL;
90     ZSTD_freeDCtx(dctx); dctx = NULL;
91 #endif
92     return 0;
93 }