Rizin
unix-like reverse engineering framework and cli tools
fuzz_helpers.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-present, 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  */
9 
14 #ifndef FUZZ_HELPERS_H
15 #define FUZZ_HELPERS_H
16 
17 #include "fuzz.h"
18 #include "xxhash.h"
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define LZ4_COMMONDEFS_ONLY
28 #ifndef LZ4_SRC_INCLUDED
29 #include "lz4.c" /* LZ4_count, constants, mem */
30 #endif
31 
32 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
33 #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
34 
35 #define FUZZ_QUOTE_IMPL(str) #str
36 #define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str)
37 
41 #define FUZZ_ASSERT_MSG(cond, msg) \
42  ((cond) ? (void)0 \
43  : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \
44  __LINE__, FUZZ_QUOTE(cond), (msg)), \
45  abort()))
46 #define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), "");
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 
64  uint8_t const *data = *src;
65  size_t const toHash = MIN(FUZZ_RNG_SEED_SIZE, *size);
66  *size -= toHash;
67  *src += toHash;
68  return XXH32(data, toHash, 0);
69 }
70 
71 #define FUZZ_rotl32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
72 
74  static const uint32_t prime1 = 2654435761U;
75  static const uint32_t prime2 = 2246822519U;
76  uint32_t rand32 = *state;
77  rand32 *= prime1;
78  rand32 += prime2;
79  rand32 = FUZZ_rotl32(rand32, 13);
80  *state = rand32;
81  return rand32 >> 5;
82 }
83 
84 /* Returns a random numer in the range [min, max]. */
86  uint32_t random = FUZZ_rand(state);
87  return min + (random % (max - min + 1));
88 }
89 
90 #ifdef __cplusplus
91 }
92 #endif
93 
94 #endif
lzma_index * src
Definition: index.h:567
int max
Definition: enough.c:225
static const U32 prime2
Definition: frametest.c:77
static const U32 prime1
Definition: frametest.c:76
#define FUZZ_RNG_SEED_SIZE
Definition: fuzz.h:32
FUZZ_STATIC uint32_t FUZZ_seed(uint8_t const **src, size_t *size)
Definition: fuzz_helpers.h:63
#define MIN(a, b)
Definition: fuzz_helpers.h:32
FUZZ_STATIC uint32_t FUZZ_rand(uint32_t *state)
Definition: fuzz_helpers.h:73
#define FUZZ_rotl32(x, r)
Definition: fuzz_helpers.h:71
FUZZ_STATIC uint32_t FUZZ_rand32(uint32_t *state, uint32_t min, uint32_t max)
Definition: fuzz_helpers.h:85
#define FUZZ_STATIC
Definition: fuzz_helpers.h:56
voidpf void uLong size
Definition: ioapi.h:138
XXH_PUBLIC_API unsigned int XXH32(const void *input, size_t len, unsigned int seed)
Definition: xxhash.c:392
#define min(a, b)
Definition: qsort.h:83
unsigned int uint32_t
Definition: sftypes.h:29
unsigned char uint8_t
Definition: sftypes.h:31
Definition: dis.h:43