| 1 | /* |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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 | * You may select, at your option, one of the above-listed licenses. |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * Helper functions for fuzzing. |
| 13 | */ |
| 14 | |
| 15 | #ifndef FUZZ_HELPERS_H |
| 16 | #define FUZZ_HELPERS_H |
| 17 | |
| 18 | #include "debug.h" |
| 19 | #include "fuzz.h" |
| 20 | #include "xxhash.h" |
| 21 | #include "zstd.h" |
| 22 | #include "fuzz_data_producer.h" |
| 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #ifdef __cplusplus |
| 28 | extern "C" { |
| 29 | #endif |
| 30 | |
| 31 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 32 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 33 | |
| 34 | #define FUZZ_QUOTE_IMPL(str) #str |
| 35 | #define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str) |
| 36 | |
| 37 | /** |
| 38 | * Asserts for fuzzing that are always enabled. |
| 39 | */ |
| 40 | #define FUZZ_ASSERT_MSG(cond, msg) \ |
| 41 | ((cond) ? (void)0 \ |
| 42 | : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \ |
| 43 | __LINE__, FUZZ_QUOTE(cond), (msg)), \ |
| 44 | abort())) |
| 45 | #define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), ""); |
| 46 | #define FUZZ_ZASSERT(code) \ |
| 47 | FUZZ_ASSERT_MSG(!ZSTD_isError(code), ZSTD_getErrorName(code)) |
| 48 | |
| 49 | #if defined(__GNUC__) |
| 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 |
| 56 | #else |
| 57 | #define FUZZ_STATIC static |
| 58 | #endif |
| 59 | |
| 60 | /** |
| 61 | * malloc except return NULL for zero sized data and FUZZ_ASSERT |
| 62 | * that malloc doesn't fail. |
| 63 | */ |
| 64 | void* FUZZ_malloc(size_t size); |
| 65 | |
| 66 | /** |
| 67 | * malloc except returns random pointer for zero sized data and FUZZ_ASSERT |
| 68 | * that malloc doesn't fail. |
| 69 | */ |
| 70 | void* FUZZ_malloc_rand(size_t size, FUZZ_dataProducer_t *producer); |
| 71 | |
| 72 | /** |
| 73 | * memcmp but accepts NULL. |
| 74 | */ |
| 75 | int FUZZ_memcmp(void const* lhs, void const* rhs, size_t size); |
| 76 | |
| 77 | #ifdef __cplusplus |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | #endif |