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 
10 /**
11  * Helper functions for fuzzing.
12  */
13 
14 #ifndef FUZZ_HELPERS_H
15 #define FUZZ_HELPERS_H
16 
17 #include "fuzz.h"
18 #include "xxhash.h"
19 #include "zstd.h"
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define MIN(a, b) ((a) < (b) ? (a) : (b))
29 #define MAX(a, b) ((a) > (b) ? (a) : (b))
30 
31 #define FUZZ_QUOTE_IMPL(str) #str
32 #define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str)
33 
34 /**
35  * Asserts for fuzzing that are always enabled.
36  */
37 #define FUZZ_ASSERT_MSG(cond, msg)                                             \
38   ((cond) ? (void)0                                                            \
39           : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \
40                      __LINE__, FUZZ_QUOTE(cond), (msg)),                       \
41              abort()))
42 #define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), "");
43 #define FUZZ_ZASSERT(code)                                                     \
44   FUZZ_ASSERT_MSG(!ZSTD_isError(code), ZSTD_getErrorName(code))
45 
46 #if defined(__GNUC__)
47 #define FUZZ_STATIC static __inline __attribute__((unused))
48 #elif defined(__cplusplus) ||                                                  \
49     (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
50 #define FUZZ_STATIC static inline
51 #elif defined(_MSC_VER)
52 #define FUZZ_STATIC static __inline
53 #else
54 #define FUZZ_STATIC static
55 #endif
56 
57 /**
58  * Deterministically constructs a seed based on the fuzz input.
59  * Consumes up to the first FUZZ_RNG_SEED_SIZE bytes of the input.
60  */
FUZZ_seed(uint8_t const ** src,size_t * size)61 FUZZ_STATIC uint32_t FUZZ_seed(uint8_t const **src, size_t* size) {
62     uint8_t const *data = *src;
63     size_t const toHash = MIN(FUZZ_RNG_SEED_SIZE, *size);
64     *size -= toHash;
65     *src += toHash;
66     return XXH32(data, toHash, 0);
67 }
68 
69 #define FUZZ_rotl32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
70 
FUZZ_rand(uint32_t * state)71 FUZZ_STATIC uint32_t FUZZ_rand(uint32_t *state) {
72     static const uint32_t prime1 = 2654435761U;
73     static const uint32_t prime2 = 2246822519U;
74     uint32_t rand32 = *state;
75     rand32 *= prime1;
76     rand32 += prime2;
77     rand32 = FUZZ_rotl32(rand32, 13);
78     *state = rand32;
79     return rand32 >> 5;
80 }
81 
82 /* Returns a random numer in the range [min, max]. */
FUZZ_rand32(uint32_t * state,uint32_t min,uint32_t max)83 FUZZ_STATIC uint32_t FUZZ_rand32(uint32_t *state, uint32_t min, uint32_t max) {
84     uint32_t random = FUZZ_rand(state);
85     return min + (random % (max - min + 1));
86 }
87 
88 #ifdef __cplusplus
89 }
90 #endif
91 
92 #endif
93