• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * meaning 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 "fuzz.h"
19 #include "xxhash.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 LZ4_COMMONDEFS_ONLY
29 #ifndef LZ4_SRC_INCLUDED
30 #include "lz4.c"   /* LZ4_count, constants, mem */
31 #endif
32 
33 #define MIN(a,b)   ( (a) < (b) ? (a) : (b) )
34 #define MAX(a,b)   ( (a) > (b) ? (a) : (b) )
35 
36 #define FUZZ_QUOTE_IMPL(str) #str
37 #define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str)
38 
39 /**
40  * Asserts for fuzzing that are always enabled.
41  */
42 #define FUZZ_ASSERT_MSG(cond, msg)                                             \
43   ((cond) ? (void)0                                                            \
44           : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \
45                      __LINE__, FUZZ_QUOTE(cond), (msg)),                       \
46              abort()))
47 #define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), "");
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  * Deterministically constructs a seed based on the fuzz input.
62  * Consumes up to the first FUZZ_RNG_SEED_SIZE bytes of the input.
63  */
FUZZ_seed(uint8_t const ** src,size_t * size)64 FUZZ_STATIC uint32_t FUZZ_seed(uint8_t const **src, size_t* size) {
65     uint8_t const *data = *src;
66     size_t const toHash = MIN(FUZZ_RNG_SEED_SIZE, *size);
67     *size -= toHash;
68     *src += toHash;
69     return XXH32(data, toHash, 0);
70 }
71 
72 #define FUZZ_rotl32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
73 
FUZZ_rand(uint32_t * state)74 FUZZ_STATIC uint32_t FUZZ_rand(uint32_t *state) {
75     static const uint32_t prime1 = 2654435761U;
76     static const uint32_t prime2 = 2246822519U;
77     uint32_t rand32 = *state;
78     rand32 *= prime1;
79     rand32 += prime2;
80     rand32 = FUZZ_rotl32(rand32, 13);
81     *state = rand32;
82     return rand32 >> 5;
83 }
84 
85 /* Returns a random number in the range [min, max]. */
FUZZ_rand32(uint32_t * state,uint32_t min,uint32_t max)86 FUZZ_STATIC uint32_t FUZZ_rand32(uint32_t *state, uint32_t min, uint32_t max) {
87     uint32_t random = FUZZ_rand(state);
88     return min + (random % (max - min + 1));
89 }
90 
91 #ifdef __cplusplus
92 }
93 #endif
94 
95 #endif
96