xref: /aosp_15_r20/external/zstd/tests/fuzz/huf_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 performs a zstd round-trip test (compress & decompress),
13*01826a49SYabin Cui  * compares the result with the original, and calls abort() on corruption.
14*01826a49SYabin Cui  */
15*01826a49SYabin Cui 
16*01826a49SYabin Cui #include <stddef.h>
17*01826a49SYabin Cui #include <stdlib.h>
18*01826a49SYabin Cui #include <stdio.h>
19*01826a49SYabin Cui #include <string.h>
20*01826a49SYabin Cui #include "common/cpu.h"
21*01826a49SYabin Cui #include "common/huf.h"
22*01826a49SYabin Cui #include "fuzz_helpers.h"
23*01826a49SYabin Cui #include "fuzz_data_producer.h"
24*01826a49SYabin Cui 
LLVMFuzzerTestOneInput(const uint8_t * src,size_t size)25*01826a49SYabin Cui int LLVMFuzzerTestOneInput(const uint8_t *src, size_t size)
26*01826a49SYabin Cui {
27*01826a49SYabin Cui     FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(src, size);
28*01826a49SYabin Cui     /* Select random parameters: #streams, X1 or X2 decoding, bmi2 */
29*01826a49SYabin Cui     int const streams = FUZZ_dataProducer_int32Range(producer, 0, 1);
30*01826a49SYabin Cui     int const symbols = FUZZ_dataProducer_int32Range(producer, 0, 1);
31*01826a49SYabin Cui     int const flags = 0
32*01826a49SYabin Cui         | (ZSTD_cpuid_bmi2(ZSTD_cpuid()) && FUZZ_dataProducer_int32Range(producer, 0, 1) ? HUF_flags_bmi2 : 0)
33*01826a49SYabin Cui         | (FUZZ_dataProducer_int32Range(producer, 0, 1) ? HUF_flags_optimalDepth : 0)
34*01826a49SYabin Cui         | (FUZZ_dataProducer_int32Range(producer, 0, 1) ? HUF_flags_preferRepeat : 0)
35*01826a49SYabin Cui         | (FUZZ_dataProducer_int32Range(producer, 0, 1) ? HUF_flags_suspectUncompressible : 0)
36*01826a49SYabin Cui         | (FUZZ_dataProducer_int32Range(producer, 0, 1) ? HUF_flags_disableAsm : 0)
37*01826a49SYabin Cui         | (FUZZ_dataProducer_int32Range(producer, 0, 1) ? HUF_flags_disableFast : 0);
38*01826a49SYabin Cui     /* Select a random cBufSize - it may be too small */
39*01826a49SYabin Cui     size_t const dBufSize = FUZZ_dataProducer_uint32Range(producer, 0, 8 * size + 500);
40*01826a49SYabin Cui     size_t const maxTableLog = FUZZ_dataProducer_uint32Range(producer, 1, HUF_TABLELOG_MAX);
41*01826a49SYabin Cui     HUF_DTable* dt = (HUF_DTable*)FUZZ_malloc(HUF_DTABLE_SIZE(maxTableLog) * sizeof(HUF_DTable));
42*01826a49SYabin Cui     size_t const wkspSize = HUF_WORKSPACE_SIZE;
43*01826a49SYabin Cui     void* wksp = FUZZ_malloc(wkspSize);
44*01826a49SYabin Cui     void* dBuf = FUZZ_malloc(dBufSize);
45*01826a49SYabin Cui     dt[0] = maxTableLog * 0x01000001;
46*01826a49SYabin Cui     size = FUZZ_dataProducer_remainingBytes(producer);
47*01826a49SYabin Cui 
48*01826a49SYabin Cui     if (symbols == 0) {
49*01826a49SYabin Cui         size_t const err = HUF_readDTableX1_wksp(dt, src, size, wksp, wkspSize, flags);
50*01826a49SYabin Cui         if (ZSTD_isError(err))
51*01826a49SYabin Cui             goto _out;
52*01826a49SYabin Cui     } else {
53*01826a49SYabin Cui         size_t const err = HUF_readDTableX2_wksp(dt, src, size, wksp, wkspSize, flags);
54*01826a49SYabin Cui         if (ZSTD_isError(err))
55*01826a49SYabin Cui             goto _out;
56*01826a49SYabin Cui     }
57*01826a49SYabin Cui     if (streams == 0)
58*01826a49SYabin Cui         HUF_decompress1X_usingDTable(dBuf, dBufSize, src, size, dt, flags);
59*01826a49SYabin Cui     else
60*01826a49SYabin Cui         HUF_decompress4X_usingDTable(dBuf, dBufSize, src, size, dt, flags);
61*01826a49SYabin Cui 
62*01826a49SYabin Cui _out:
63*01826a49SYabin Cui     free(dt);
64*01826a49SYabin Cui     free(wksp);
65*01826a49SYabin Cui     free(dBuf);
66*01826a49SYabin Cui     FUZZ_dataProducer_free(producer);
67*01826a49SYabin Cui     return 0;
68*01826a49SYabin Cui }
69