Skip to content

Commit

Permalink
Use afl_hash_ip
Browse files Browse the repository at this point in the history
  • Loading branch information
wtdcode committed Aug 12, 2023
1 parent df1b95f commit 979d944
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 46 deletions.
71 changes: 71 additions & 0 deletions include/afl-hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef _AFL_HASH_H

#define _AFL_HASH_H

/* This is an exerpt of xxhash/XXH3 to prevent colliding with xxhash that is
in QEMU */

// #pragma GCC optimize 3

#include <stdio.h>
#include <limits.h>
#include <stdint.h>

uint32_t afl_hash_ip(uint64_t ip);
uint64_t AFL_readLE64(const void* memPtr);
uint64_t AFL_rrmxmx(uint64_t h64, uint64_t len);

#define AFL_rotl64(x, r) (((x) << (r)) | ((x) >> (64 - (r))))

inline uint64_t AFL_readLE64(const void* memPtr) {

const uint8_t* bytePtr = (const uint8_t*)memPtr;
return bytePtr[0] | ((uint64_t)bytePtr[1] << 8) |
((uint64_t)bytePtr[2] << 16) | ((uint64_t)bytePtr[3] << 24) |
((uint64_t)bytePtr[4] << 32) | ((uint64_t)bytePtr[5] << 40) |
((uint64_t)bytePtr[6] << 48) | ((uint64_t)bytePtr[7] << 56);
}

inline uint64_t AFL_rrmxmx(uint64_t h64, uint64_t len) {

/* this mix is inspired by Pelle Evensen's rrmxmx */
h64 ^= AFL_rotl64(h64, 49) ^ AFL_rotl64(h64, 24);
h64 *= 0x9FB21C651E98DF25ULL;
h64 ^= (h64 >> 35) + len;
h64 *= 0x9FB21C651E98DF25ULL;
return h64 ^ (h64 >> 28);
}

inline uint32_t afl_hash_ip(uint64_t ip) {

const uint8_t secret[] = {

0xb8, 0xfe, 0x6c, 0x39, 0x23, 0xa4, 0x4b, 0xbe, 0x7c, 0x01, 0x81, 0x2c,
0xf7, 0x21, 0xad, 0x1c, 0xde, 0xd4, 0x6d, 0xe9, 0x83, 0x90, 0x97, 0xdb,
0x72, 0x40, 0xa4, 0xa4, 0xb7, 0xb3, 0x67, 0x1f, 0xcb, 0x79, 0xe6, 0x4e,
0xcc, 0xc0, 0xe5, 0x78, 0x82, 0x5a, 0xd0, 0x7d, 0xcc, 0xff, 0x72, 0x21,
0xb8, 0x08, 0x46, 0x74, 0xf7, 0x43, 0x24, 0x8e, 0xe0, 0x35, 0x90, 0xe6,
0x81, 0x3a, 0x26, 0x4c, 0x3c, 0x28, 0x52, 0xbb, 0x91, 0xc3, 0x00, 0xcb,
0x88, 0xd0, 0x65, 0x8b, 0x1b, 0x53, 0x2e, 0xa3, 0x71, 0x64, 0x48, 0x97,
0xa2, 0x0d, 0xf9, 0x4e, 0x38, 0x19, 0xef, 0x46, 0xa9, 0xde, 0xac, 0xd8,
0xa8, 0xfa, 0x76, 0x3f, 0xe3, 0x9c, 0x34, 0x3f, 0xf9, 0xdc, 0xbb, 0xc7,
0xc7, 0x0b, 0x4f, 0x1d, 0x8a, 0x51, 0xe0, 0x4b, 0xcd, 0xb4, 0x59, 0x31,
0xc8, 0x9f, 0x7e, 0xc9, 0xd9, 0x78, 0x73, 0x64, 0xea, 0xc5, 0xac, 0x83,
0x34, 0xd3, 0xeb, 0xc3, 0xc5, 0x81, 0xa0, 0xff, 0xfa, 0x13, 0x63, 0xeb,
0x17, 0x0d, 0xdd, 0x51, 0xb7, 0xf0, 0xda, 0x49, 0xd3, 0x16, 0x55, 0x26,
0x29, 0xd4, 0x68, 0x9e, 0x2b, 0x16, 0xbe, 0x58, 0x7d, 0x47, 0xa1, 0xfc,
0x8f, 0xf8, 0xb8, 0xd1, 0x7a, 0xd0, 0x31, 0xce, 0x45, 0xcb, 0x3a, 0x8f,
0x95, 0x16, 0x04, 0x28, 0xaf, 0xd7, 0xfb, 0xca, 0xbb, 0x4b, 0x40, 0x7e,

};

uint32_t const input1 = (uint32_t)(ip & 0xffffffff);
uint32_t const input2 = (uint32_t)(ip >> 32);
uint64_t const bitflip =
(AFL_readLE64(secret + 8) ^ AFL_readLE64(secret + 16));
uint64_t const input64 = input2 + (((uint64_t)input1) << 32);
uint64_t const keyed = input64 ^ bitflip;
return AFL_rrmxmx(keyed, 8);
}

#endif
85 changes: 42 additions & 43 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
will be kept and written to the crash/ directory as RECORD:... files.
Note that every crash will be written, not only unique ones! */

//#define AFL_PERSISTENT_RECORD
// #define AFL_PERSISTENT_RECORD

/* console output colors: There are three ways to configure its behavior
* 1. default: colored outputs fixed on: defined USE_COLOR && defined
Expand All @@ -99,10 +99,10 @@
#define USE_COLOR

#ifdef USE_COLOR
/* Comment in to always enable terminal colors */
/* Comment out to enable runtime controlled terminal colors via AFL_NO_COLOR
*/
#define ALWAYS_COLORED 1
/* Comment in to always enable terminal colors */
/* Comment out to enable runtime controlled terminal colors via AFL_NO_COLOR
*/
#define ALWAYS_COLORED 1
#endif

/* StatsD config
Expand All @@ -120,8 +120,8 @@

/* Comment out to disable fancy ANSI boxes and use poor man's 7-bit UI: */

#ifndef ANDROID_DISABLE_FANCY // Fancy boxes are ugly from adb
#define FANCY_BOXES
#ifndef ANDROID_DISABLE_FANCY // Fancy boxes are ugly from adb
#define FANCY_BOXES
#endif

/* Default timeout for fuzzed code (milliseconds). This is the upper bound,
Expand All @@ -135,7 +135,7 @@

/* 64bit arch MACRO */
#if (defined(__x86_64__) || defined(__arm64__) || defined(__aarch64__))
#define WORD_SIZE_64 1
#define WORD_SIZE_64 1
#endif

/* Default memory limit for child process (MB) 0 = disabled : */
Expand Down Expand Up @@ -211,9 +211,9 @@
/* Probabilities of skipping non-favored entries in the queue, expressed as
percentages: */

#define SKIP_TO_NEW_PROB 99 /* ...when there are new, pending favorites */
#define SKIP_NFAV_OLD_PROB 95 /* ...no new favs, cur entry already fuzzed */
#define SKIP_NFAV_NEW_PROB 75 /* ...no new favs, cur entry not fuzzed yet */
#define SKIP_TO_NEW_PROB 99 /* ...when there are new, pending favorites */
#define SKIP_NFAV_OLD_PROB 95 /* ...no new favs, cur entry already fuzzed */
#define SKIP_NFAV_NEW_PROB 75 /* ...no new favs, cur entry not fuzzed yet */

/* Splicing cycle count: */

Expand Down Expand Up @@ -318,42 +318,42 @@

/* List of interesting values to use in fuzzing. */

#define INTERESTING_8 \
-128, /* Overflow signed 8-bit when decremented */ \
-1, /* */ \
0, /* */ \
1, /* */ \
16, /* One-off with common buffer size */ \
32, /* One-off with common buffer size */ \
64, /* One-off with common buffer size */ \
100, /* One-off with common buffer size */ \
127 /* Overflow signed 8-bit when incremented */
#define INTERESTING_8 \
-128, /* Overflow signed 8-bit when decremented */ \
-1, /* */ \
0, /* */ \
1, /* */ \
16, /* One-off with common buffer size */ \
32, /* One-off with common buffer size */ \
64, /* One-off with common buffer size */ \
100, /* One-off with common buffer size */ \
127 /* Overflow signed 8-bit when incremented */

#define INTERESTING_8_LEN 9

#define INTERESTING_16 \
-32768, /* Overflow signed 16-bit when decremented */ \
-129, /* Overflow signed 8-bit */ \
128, /* Overflow signed 8-bit */ \
255, /* Overflow unsig 8-bit when incremented */ \
256, /* Overflow unsig 8-bit */ \
512, /* One-off with common buffer size */ \
1000, /* One-off with common buffer size */ \
1024, /* One-off with common buffer size */ \
4096, /* One-off with common buffer size */ \
32767 /* Overflow signed 16-bit when incremented */
#define INTERESTING_16 \
-32768, /* Overflow signed 16-bit when decremented */ \
-129, /* Overflow signed 8-bit */ \
128, /* Overflow signed 8-bit */ \
255, /* Overflow unsig 8-bit when incremented */ \
256, /* Overflow unsig 8-bit */ \
512, /* One-off with common buffer size */ \
1000, /* One-off with common buffer size */ \
1024, /* One-off with common buffer size */ \
4096, /* One-off with common buffer size */ \
32767 /* Overflow signed 16-bit when incremented */

#define INTERESTING_16_LEN 10

#define INTERESTING_32 \
-2147483648LL, /* Overflow signed 32-bit when decremented */ \
-100663046, /* Large negative number (endian-agnostic) */ \
-32769, /* Overflow signed 16-bit */ \
32768, /* Overflow signed 16-bit */ \
65535, /* Overflow unsig 16-bit when incremented */ \
65536, /* Overflow unsig 16 bit */ \
100663045, /* Large positive number (endian-agnostic) */ \
2147483647 /* Overflow signed 32-bit when incremented */
#define INTERESTING_32 \
-2147483648LL, /* Overflow signed 32-bit when decremented */ \
-100663046, /* Large negative number (endian-agnostic) */ \
-32769, /* Overflow signed 16-bit */ \
32768, /* Overflow signed 16-bit */ \
65535, /* Overflow unsig 16-bit when incremented */ \
65536, /* Overflow unsig 16 bit */ \
100663045, /* Large positive number (endian-agnostic) */ \
2147483647 /* Overflow signed 32-bit when incremented */

#define INTERESTING_32_LEN 8

Expand Down Expand Up @@ -507,5 +507,4 @@

#define AFL_TXT_STRING_MAX_MUTATIONS 6

#endif /* ! _HAVE_CONFIG_H */

#endif /* ! _HAVE_CONFIG_H */
8 changes: 5 additions & 3 deletions unicornafl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
#include <errno.h>
#include <chrono>
#include <cstdlib>
#include "afl-hash.h"

static bool afl_debug_enabled = false; // General debug message
static bool afl_debug_unicorn_enabled = false; // Unicorn specific debug messages from child
static bool afl_debug_enabled = false; // General debug message
static bool afl_debug_unicorn_enabled =
false; // Unicorn specific debug messages from child
static std::chrono::time_point<std::chrono::steady_clock> t0;

static void log_init() {
Expand Down Expand Up @@ -331,7 +333,7 @@ class UCAFL {

static void _uc_hook_block(uc_engine* uc, uint64_t address, uint32_t size,
void* user_data) {
uint64_t cur_loc = ((address >> 4) ^ (address << 8)) & (MAP_SIZE - 7);
uint64_t cur_loc = afl_hash_ip(address);
UCAFL* ucafl = (UCAFL*)user_data;

ucafl->afl_area_ptr_[cur_loc ^ ucafl->afl_prev_loc_]++;
Expand Down

0 comments on commit 979d944

Please sign in to comment.