1 /*
2  * Copyright (c) 2016-2020, Facebook, Inc.
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 <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define MIN(a, b) ((a) < (b) ? (a) : (b))
31 #define MAX(a, b) ((a) > (b) ? (a) : (b))
32 
33 #define FUZZ_QUOTE_IMPL(str) #str
34 #define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str)
35 
36 /**
37  * Asserts for fuzzing that are always enabled.
38  */
39 #define FUZZ_ASSERT_MSG(cond, msg)                                             \
40   ((cond) ? (void)0                                                            \
41           : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \
42                      __LINE__, FUZZ_QUOTE(cond), (msg)),                       \
43              abort()))
44 #define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), "");
45 #define FUZZ_ZASSERT(code)                                                     \
46   FUZZ_ASSERT_MSG(!ZSTD_isError(code), ZSTD_getErrorName(code))
47 
48 #if defined(__GNUC__)
49 #define FUZZ_STATIC static __inline __attribute__((unused))
50 #elif defined(__cplusplus) ||                                                  \
51     (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
52 #define FUZZ_STATIC static inline
53 #elif defined(_MSC_VER)
54 #define FUZZ_STATIC static __inline
55 #else
56 #define FUZZ_STATIC static
57 #endif
58 
59 /**
60  * malloc except return NULL for zero sized data and FUZZ_ASSERT
61  * that malloc doesn't fail.
62  */
63 void* FUZZ_malloc(size_t size);
64 
65 /**
66  * memcmp but accepts NULL.
67  */
68 int FUZZ_memcmp(void const* lhs, void const* rhs, size_t size);
69 
70 #ifdef __cplusplus
71 }
72 #endif
73 
74 #endif
75