1*3117ece4Schristos /* 2*3117ece4Schristos * Copyright (c) Meta Platforms, Inc. and affiliates. 3*3117ece4Schristos * All rights reserved. 4*3117ece4Schristos * 5*3117ece4Schristos * This source code is licensed under both the BSD-style license (found in the 6*3117ece4Schristos * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7*3117ece4Schristos * in the COPYING file in the root directory of this source tree). 8*3117ece4Schristos * You may select, at your option, one of the above-listed licenses. 9*3117ece4Schristos */ 10*3117ece4Schristos 11*3117ece4Schristos /** 12*3117ece4Schristos * Helper functions for fuzzing. 13*3117ece4Schristos */ 14*3117ece4Schristos 15*3117ece4Schristos #ifndef FUZZ_HELPERS_H 16*3117ece4Schristos #define FUZZ_HELPERS_H 17*3117ece4Schristos 18*3117ece4Schristos #include "debug.h" 19*3117ece4Schristos #include "fuzz.h" 20*3117ece4Schristos #include "xxhash.h" 21*3117ece4Schristos #include "zstd.h" 22*3117ece4Schristos #include "fuzz_data_producer.h" 23*3117ece4Schristos #include <stdint.h> 24*3117ece4Schristos #include <stdio.h> 25*3117ece4Schristos #include <stdlib.h> 26*3117ece4Schristos 27*3117ece4Schristos #ifdef __cplusplus 28*3117ece4Schristos extern "C" { 29*3117ece4Schristos #endif 30*3117ece4Schristos 31*3117ece4Schristos #define MIN(a, b) ((a) < (b) ? (a) : (b)) 32*3117ece4Schristos #define MAX(a, b) ((a) > (b) ? (a) : (b)) 33*3117ece4Schristos 34*3117ece4Schristos #define FUZZ_QUOTE_IMPL(str) #str 35*3117ece4Schristos #define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str) 36*3117ece4Schristos 37*3117ece4Schristos /** 38*3117ece4Schristos * Asserts for fuzzing that are always enabled. 39*3117ece4Schristos */ 40*3117ece4Schristos #define FUZZ_ASSERT_MSG(cond, msg) \ 41*3117ece4Schristos ((cond) ? (void)0 \ 42*3117ece4Schristos : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \ 43*3117ece4Schristos __LINE__, FUZZ_QUOTE(cond), (msg)), \ 44*3117ece4Schristos abort())) 45*3117ece4Schristos #define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), ""); 46*3117ece4Schristos #define FUZZ_ZASSERT(code) \ 47*3117ece4Schristos FUZZ_ASSERT_MSG(!ZSTD_isError(code), ZSTD_getErrorName(code)) 48*3117ece4Schristos 49*3117ece4Schristos #if defined(__GNUC__) 50*3117ece4Schristos #define FUZZ_STATIC static __inline __attribute__((unused)) 51*3117ece4Schristos #elif defined(__cplusplus) || \ 52*3117ece4Schristos (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) 53*3117ece4Schristos #define FUZZ_STATIC static inline 54*3117ece4Schristos #elif defined(_MSC_VER) 55*3117ece4Schristos #define FUZZ_STATIC static __inline 56*3117ece4Schristos #else 57*3117ece4Schristos #define FUZZ_STATIC static 58*3117ece4Schristos #endif 59*3117ece4Schristos 60*3117ece4Schristos /** 61*3117ece4Schristos * malloc except return NULL for zero sized data and FUZZ_ASSERT 62*3117ece4Schristos * that malloc doesn't fail. 63*3117ece4Schristos */ 64*3117ece4Schristos void* FUZZ_malloc(size_t size); 65*3117ece4Schristos 66*3117ece4Schristos /** 67*3117ece4Schristos * malloc except returns random pointer for zero sized data and FUZZ_ASSERT 68*3117ece4Schristos * that malloc doesn't fail. 69*3117ece4Schristos */ 70*3117ece4Schristos void* FUZZ_malloc_rand(size_t size, FUZZ_dataProducer_t *producer); 71*3117ece4Schristos 72*3117ece4Schristos /** 73*3117ece4Schristos * memcmp but accepts NULL. 74*3117ece4Schristos */ 75*3117ece4Schristos int FUZZ_memcmp(void const* lhs, void const* rhs, size_t size); 76*3117ece4Schristos 77*3117ece4Schristos #ifdef __cplusplus 78*3117ece4Schristos } 79*3117ece4Schristos #endif 80*3117ece4Schristos 81*3117ece4Schristos #endif 82