1 /* 2 american fuzzy lop++ - type definitions and minor macros 3 -------------------------------------------------------- 4 5 Originally written by Michal Zalewski 6 7 Now maintained by Marc Heuse <mh@mh-sec.de>, 8 Heiko Eißfeldt <heiko.eissfeldt@hexco.de>, 9 Andrea Fioraldi <andreafioraldi@gmail.com>, 10 Dominik Maier <mail@dmnk.co> 11 12 Copyright 2016, 2017 Google Inc. All rights reserved. 13 Copyright 2019-2022 AFLplusplus Project. All rights reserved. 14 15 Licensed under the Apache License, Version 2.0 (the "License"); 16 you may not use this file except in compliance with the License. 17 You may obtain a copy of the License at: 18 19 https://www.apache.org/licenses/LICENSE-2.0 20 21 */ 22 23 #ifndef _HAVE_TYPES_H 24 #define _HAVE_TYPES_H 25 26 #include <stdint.h> 27 #include <stdlib.h> 28 #include "config.h" 29 30 typedef uint8_t u8; 31 typedef uint16_t u16; 32 typedef uint32_t u32; 33 #ifdef WORD_SIZE_64 34 typedef unsigned __int128 uint128_t; 35 typedef uint128_t u128; 36 #endif 37 38 /* Extended forkserver option values */ 39 40 /* Reporting errors */ 41 #define FS_OPT_ERROR 0xf800008f 42 #define FS_OPT_GET_ERROR(x) ((x & 0x00ffff00) >> 8) 43 #define FS_OPT_SET_ERROR(x) ((x & 0x0000ffff) << 8) 44 #define FS_ERROR_MAP_SIZE 1 45 #define FS_ERROR_MAP_ADDR 2 46 #define FS_ERROR_SHM_OPEN 4 47 #define FS_ERROR_SHMAT 8 48 #define FS_ERROR_MMAP 16 49 #define FS_ERROR_OLD_CMPLOG 32 50 #define FS_ERROR_OLD_CMPLOG_QEMU 64 51 52 /* Reporting options */ 53 #define FS_OPT_ENABLED 0x80000001 54 #define FS_OPT_MAPSIZE 0x40000000 55 #define FS_OPT_SNAPSHOT 0x20000000 56 #define FS_OPT_AUTODICT 0x10000000 57 #define FS_OPT_SHDMEM_FUZZ 0x01000000 58 #define FS_OPT_NEWCMPLOG 0x02000000 59 #define FS_OPT_OLD_AFLPP_WORKAROUND 0x0f000000 60 // FS_OPT_MAX_MAPSIZE is 8388608 = 0x800000 = 2^23 = 1 << 22 61 #define FS_OPT_MAX_MAPSIZE ((0x00fffffeU >> 1) + 1) 62 #define FS_OPT_GET_MAPSIZE(x) (((x & 0x00fffffe) >> 1) + 1) 63 #define FS_OPT_SET_MAPSIZE(x) \ 64 (x <= 1 || x > FS_OPT_MAX_MAPSIZE ? 0 : ((x - 1) << 1)) 65 66 typedef unsigned long long u64; 67 68 typedef int8_t s8; 69 typedef int16_t s16; 70 typedef int32_t s32; 71 typedef int64_t s64; 72 #ifdef WORD_SIZE_64 73 typedef __int128 int128_t; 74 typedef int128_t s128; 75 #endif 76 77 #ifndef MIN 78 #define MIN(a, b) \ 79 ({ \ 80 \ 81 __typeof__(a) _a = (a); \ 82 __typeof__(b) _b = (b); \ 83 _a < _b ? _a : _b; \ 84 \ 85 }) 86 87 #define MAX(a, b) \ 88 ({ \ 89 \ 90 __typeof__(a) _a = (a); \ 91 __typeof__(b) _b = (b); \ 92 _a > _b ? _a : _b; \ 93 \ 94 }) 95 96 #endif /* !MIN */ 97 98 #define SWAP16(_x) \ 99 ({ \ 100 \ 101 u16 _ret = (_x); \ 102 (u16)((_ret << 8) | (_ret >> 8)); \ 103 \ 104 }) 105 106 #define SWAP32(_x) \ 107 ({ \ 108 \ 109 u32 _ret = (_x); \ 110 (u32)((_ret << 24) | (_ret >> 24) | ((_ret << 8) & 0x00FF0000) | \ 111 ((_ret >> 8) & 0x0000FF00)); \ 112 \ 113 }) 114 115 #define SWAP64(_x) \ 116 ({ \ 117 \ 118 u64 _ret = (_x); \ 119 _ret = \ 120 (_ret & 0x00000000FFFFFFFF) << 32 | (_ret & 0xFFFFFFFF00000000) >> 32; \ 121 _ret = \ 122 (_ret & 0x0000FFFF0000FFFF) << 16 | (_ret & 0xFFFF0000FFFF0000) >> 16; \ 123 _ret = \ 124 (_ret & 0x00FF00FF00FF00FF) << 8 | (_ret & 0xFF00FF00FF00FF00) >> 8; \ 125 _ret; \ 126 \ 127 }) 128 129 // It is impossible to define 128 bit constants, so ... 130 #ifdef WORD_SIZE_64 131 #define SWAPN(_x, _l) \ 132 ({ \ 133 \ 134 u128 _res = (_x), _ret; \ 135 char *d = (char *)&_ret, *s = (char *)&_res; \ 136 int i; \ 137 for (i = 0; i < 16; i++) \ 138 d[15 - i] = s[i]; \ 139 u32 sr = 128U - ((_l) << 3U); \ 140 (_ret >>= sr); \ 141 (u128) _ret; \ 142 \ 143 }) 144 #endif 145 146 #define SWAPNN(_x, _y, _l) \ 147 ({ \ 148 \ 149 char *d = (char *)(_x), *s = (char *)(_y); \ 150 u32 i, l = (_l)-1; \ 151 for (i = 0; i <= l; i++) \ 152 d[l - i] = s[i]; \ 153 \ 154 }) 155 156 #ifdef AFL_LLVM_PASS 157 #if defined(__linux__) || !defined(__ANDROID__) 158 #define AFL_SR(s) (srandom(s)) 159 #define AFL_R(x) (random() % (x)) 160 #else 161 #define AFL_SR(s) ((void)s) 162 #define AFL_R(x) (arc4random_uniform(x)) 163 #endif 164 #else 165 #if defined(__linux__) || !defined(__ANDROID__) 166 #define SR(s) (srandom(s)) 167 #define R(x) (random() % (x)) 168 #else 169 #define SR(s) ((void)s) 170 #define R(x) (arc4random_uniform(x)) 171 #endif 172 #endif /* ^AFL_LLVM_PASS */ 173 174 #define STRINGIFY_INTERNAL(x) #x 175 #define STRINGIFY(x) STRINGIFY_INTERNAL(x) 176 177 #define MEM_BARRIER() __asm__ volatile("" ::: "memory") 178 179 #if __GNUC__ < 6 180 #ifndef likely 181 #define likely(_x) (_x) 182 #endif 183 #ifndef unlikely 184 #define unlikely(_x) (_x) 185 #endif 186 #else 187 #ifndef likely 188 #define likely(_x) __builtin_expect(!!(_x), 1) 189 #endif 190 #ifndef unlikely 191 #define unlikely(_x) __builtin_expect(!!(_x), 0) 192 #endif 193 #endif 194 195 #endif /* ! _HAVE_TYPES_H */ 196 197