2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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 * You may select, at your option, one of the above-listed licenses.
12 * Helper functions for fuzzing.
15 #ifndef FUZZ_HELPERS_H
16 #define FUZZ_HELPERS_H
22 #include "fuzz_data_producer.h"
31 #define MIN(a, b) ((a) < (b) ? (a) : (b))
32 #define MAX(a, b) ((a) > (b) ? (a) : (b))
34 #define FUZZ_QUOTE_IMPL(str) #str
35 #define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str)
38 * Asserts for fuzzing that are always enabled.
40 #define FUZZ_ASSERT_MSG(cond, msg) \
42 : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \
43 __LINE__, FUZZ_QUOTE(cond), (msg)), \
45 #define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), "");
46 #define FUZZ_ZASSERT(code) \
47 FUZZ_ASSERT_MSG(!ZSTD_isError(code), ZSTD_getErrorName(code))
50 #define FUZZ_STATIC static __inline __attribute__((unused))
51 #elif defined(__cplusplus) || \
52 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
53 #define FUZZ_STATIC static inline
54 #elif defined(_MSC_VER)
55 #define FUZZ_STATIC static __inline
57 #define FUZZ_STATIC static
61 * malloc except return NULL for zero sized data and FUZZ_ASSERT
62 * that malloc doesn't fail.
64 void* FUZZ_malloc(size_t size);
67 * malloc except returns random pointer for zero sized data and FUZZ_ASSERT
68 * that malloc doesn't fail.
70 void* FUZZ_malloc_rand(size_t size, FUZZ_dataProducer_t *producer);
73 * memcmp but accepts NULL.
75 int FUZZ_memcmp(void const* lhs, void const* rhs, size_t size);