xref: /aosp_15_r20/external/AFLplusplus/src/afl-performance.c (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1 #include <stdint.h>
2 #include "afl-fuzz.h"
3 #include "types.h"
4 
5 #define XXH_INLINE_ALL
6 #include "xxhash.h"
7 #undef XXH_INLINE_ALL
8 
rand_set_seed(afl_state_t * afl,s64 init_seed)9 void rand_set_seed(afl_state_t *afl, s64 init_seed) {
10 
11   afl->init_seed = init_seed;
12   afl->rand_seed[0] =
13       hash64((u8 *)&afl->init_seed, sizeof(afl->init_seed), HASH_CONST);
14   afl->rand_seed[1] = afl->rand_seed[0] ^ 0x1234567890abcdef;
15   afl->rand_seed[2] = (afl->rand_seed[0] & 0x1234567890abcdef) ^
16                       (afl->rand_seed[1] | 0xfedcba9876543210);
17 
18 }
19 
20 #define ROTL(d, lrot) ((d << (lrot)) | (d >> (8 * sizeof(d) - (lrot))))
21 
22 #ifdef WORD_SIZE_64
23 // romuDuoJr
rand_next(afl_state_t * afl)24 inline AFL_RAND_RETURN rand_next(afl_state_t *afl) {
25 
26   AFL_RAND_RETURN xp = afl->rand_seed[0];
27   afl->rand_seed[0] = 15241094284759029579u * afl->rand_seed[1];
28   afl->rand_seed[1] = afl->rand_seed[1] - xp;
29   afl->rand_seed[1] = ROTL(afl->rand_seed[1], 27);
30   return xp;
31 
32 }
33 
34 #else
35 // RomuTrio32
rand_next(afl_state_t * afl)36 inline AFL_RAND_RETURN rand_next(afl_state_t *afl) {
37 
38   AFL_RAND_RETURN xp = afl->rand_seed[0], yp = afl->rand_seed[1],
39                   zp = afl->rand_seed[2];
40   afl->rand_seed[0] = 3323815723u * zp;
41   afl->rand_seed[1] = yp - xp;
42   afl->rand_seed[1] = ROTL(afl->rand_seed[1], 6);
43   afl->rand_seed[2] = zp - yp;
44   afl->rand_seed[2] = ROTL(afl->rand_seed[2], 22);
45   return xp;
46 
47 }
48 
49 #endif
50 
51 #undef ROTL
52 
53 /* returns a double between 0.000000000 and 1.000000000 */
54 
rand_next_percent(afl_state_t * afl)55 inline double rand_next_percent(afl_state_t *afl) {
56 
57   return (double)(((double)rand_next(afl)) / (double)0xffffffffffffffff);
58 
59 }
60 
61 /* we switch from afl's murmur implementation to xxh3 as it is 30% faster -
62    and get 64 bit hashes instead of just 32 bit. Less collisions! :-) */
63 
64 #ifdef _DEBUG
hash32(u8 * key,u32 len,u32 seed)65 u32 hash32(u8 *key, u32 len, u32 seed) {
66 
67 #else
68 inline u32 hash32(u8 *key, u32 len, u32 seed) {
69 
70 #endif
71 
72   (void)seed;
73   return (u32)XXH3_64bits(key, len);
74 
75 }
76 
77 #ifdef _DEBUG
78 u64 hash64(u8 *key, u32 len, u64 seed) {
79 
80 #else
81 inline u64 hash64(u8 *key, u32 len, u64 seed) {
82 
83 #endif
84 
85   (void)seed;
86   return XXH3_64bits(key, len);
87 
88 }
89 
90