• 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  */
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 <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 
38 /**
39  * Asserts for fuzzing that are always enabled.
40  */
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 
59 /**
60  * Deterministically constructs a seed based on the fuzz input.
61  * Consumes up to the first FUZZ_RNG_SEED_SIZE bytes of the input.
62  */
FUZZ_seed(uint8_t const ** src,size_t * size)63 FUZZ_STATIC uint32_t FUZZ_seed(uint8_t const **src, size_t* size) {
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 
FUZZ_rand(uint32_t * state)73 FUZZ_STATIC uint32_t FUZZ_rand(uint32_t *state) {
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]. */
FUZZ_rand32(uint32_t * state,uint32_t min,uint32_t max)85 FUZZ_STATIC uint32_t FUZZ_rand32(uint32_t *state, uint32_t min, uint32_t 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
95