xref: /aosp_15_r20/external/zstd/tests/fuzz/fuzz_helpers.h (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  * Helper functions for fuzzing.
13*01826a49SYabin Cui  */
14*01826a49SYabin Cui 
15*01826a49SYabin Cui #ifndef FUZZ_HELPERS_H
16*01826a49SYabin Cui #define FUZZ_HELPERS_H
17*01826a49SYabin Cui 
18*01826a49SYabin Cui #include "debug.h"
19*01826a49SYabin Cui #include "fuzz.h"
20*01826a49SYabin Cui #include "xxhash.h"
21*01826a49SYabin Cui #include "zstd.h"
22*01826a49SYabin Cui #include "fuzz_data_producer.h"
23*01826a49SYabin Cui #include <stdint.h>
24*01826a49SYabin Cui #include <stdio.h>
25*01826a49SYabin Cui #include <stdlib.h>
26*01826a49SYabin Cui 
27*01826a49SYabin Cui #ifdef __cplusplus
28*01826a49SYabin Cui extern "C" {
29*01826a49SYabin Cui #endif
30*01826a49SYabin Cui 
31*01826a49SYabin Cui #define MIN(a, b) ((a) < (b) ? (a) : (b))
32*01826a49SYabin Cui #define MAX(a, b) ((a) > (b) ? (a) : (b))
33*01826a49SYabin Cui 
34*01826a49SYabin Cui #define FUZZ_QUOTE_IMPL(str) #str
35*01826a49SYabin Cui #define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str)
36*01826a49SYabin Cui 
37*01826a49SYabin Cui /**
38*01826a49SYabin Cui  * Asserts for fuzzing that are always enabled.
39*01826a49SYabin Cui  */
40*01826a49SYabin Cui #define FUZZ_ASSERT_MSG(cond, msg)                                             \
41*01826a49SYabin Cui   ((cond) ? (void)0                                                            \
42*01826a49SYabin Cui           : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \
43*01826a49SYabin Cui                      __LINE__, FUZZ_QUOTE(cond), (msg)),                       \
44*01826a49SYabin Cui              abort()))
45*01826a49SYabin Cui #define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), "");
46*01826a49SYabin Cui #define FUZZ_ZASSERT(code)                                                     \
47*01826a49SYabin Cui   FUZZ_ASSERT_MSG(!ZSTD_isError(code), ZSTD_getErrorName(code))
48*01826a49SYabin Cui 
49*01826a49SYabin Cui #if defined(__GNUC__)
50*01826a49SYabin Cui #define FUZZ_STATIC static __inline __attribute__((unused))
51*01826a49SYabin Cui #elif defined(__cplusplus) ||                                                  \
52*01826a49SYabin Cui     (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
53*01826a49SYabin Cui #define FUZZ_STATIC static inline
54*01826a49SYabin Cui #elif defined(_MSC_VER)
55*01826a49SYabin Cui #define FUZZ_STATIC static __inline
56*01826a49SYabin Cui #else
57*01826a49SYabin Cui #define FUZZ_STATIC static
58*01826a49SYabin Cui #endif
59*01826a49SYabin Cui 
60*01826a49SYabin Cui /**
61*01826a49SYabin Cui  * malloc except return NULL for zero sized data and FUZZ_ASSERT
62*01826a49SYabin Cui  * that malloc doesn't fail.
63*01826a49SYabin Cui  */
64*01826a49SYabin Cui void* FUZZ_malloc(size_t size);
65*01826a49SYabin Cui 
66*01826a49SYabin Cui /**
67*01826a49SYabin Cui  * malloc except returns random pointer for zero sized data and FUZZ_ASSERT
68*01826a49SYabin Cui  * that malloc doesn't fail.
69*01826a49SYabin Cui  */
70*01826a49SYabin Cui void* FUZZ_malloc_rand(size_t size,  FUZZ_dataProducer_t *producer);
71*01826a49SYabin Cui 
72*01826a49SYabin Cui /**
73*01826a49SYabin Cui  * memcmp but accepts NULL.
74*01826a49SYabin Cui  */
75*01826a49SYabin Cui int FUZZ_memcmp(void const* lhs, void const* rhs, size_t size);
76*01826a49SYabin Cui 
77*01826a49SYabin Cui #ifdef __cplusplus
78*01826a49SYabin Cui }
79*01826a49SYabin Cui #endif
80*01826a49SYabin Cui 
81*01826a49SYabin Cui #endif
82