Skip to content

Commit d499b27

Browse files
Merge pull request #33 from WorksButNotTested/unstable
Add support for tracking unstable edges
2 parents a120c3f + 1ae3096 commit d499b27

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

accel/tcg/tcg-runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ DEF_HELPER_FLAGS_5(gvec_bitsel, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, ptr, i32)
335335
DEF_HELPER_FLAGS_1(afl_entry_routine, TCG_CALL_NO_RWG, void, env)
336336
DEF_HELPER_FLAGS_1(afl_persistent_routine, TCG_CALL_NO_RWG, void, env)
337337
DEF_HELPER_FLAGS_1(afl_maybe_log, TCG_CALL_NO_RWG, void, tl)
338+
DEF_HELPER_FLAGS_1(afl_maybe_log_trace, TCG_CALL_NO_RWG, void, tl)
338339
DEF_HELPER_FLAGS_3(afl_compcov_16, TCG_CALL_NO_RWG, void, tl, tl, tl)
339340
DEF_HELPER_FLAGS_3(afl_compcov_32, TCG_CALL_NO_RWG, void, tl, tl, tl)
340341
DEF_HELPER_FLAGS_3(afl_compcov_64, TCG_CALL_NO_RWG, void, tl, tl, tl)

accel/tcg/translate-all.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,32 @@
7171

7272
__thread int cur_block_is_good;
7373

74-
void HELPER(afl_maybe_log)(target_ulong cur_loc) {
74+
static int afl_track_unstable_log_fd(void) {
75+
static bool initialized = false;
76+
static int track_fd = -1;
77+
if (unlikely(!initialized)) {
78+
char * fname = getenv("AFL_QEMU_TRACK_UNSTABLE");
79+
if (fname != NULL) {
80+
track_fd = open(fname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR);
81+
}
82+
initialized = true;
83+
if (track_fd > 0) dprintf(track_fd, "QEMU UNSTABLE TRACKING ENABLED\n");
84+
}
85+
return track_fd;
86+
}
7587

88+
void HELPER(afl_maybe_log)(target_ulong cur_loc) {
7689
register uintptr_t afl_idx = cur_loc ^ afl_prev_loc;
7790

7891
INC_AFL_AREA(afl_idx);
7992

8093
// afl_prev_loc = ((cur_loc & (MAP_SIZE - 1) >> 1)) |
8194
// ((cur_loc & 1) << ((int)ceil(log2(MAP_SIZE)) -1));
8295
afl_prev_loc = cur_loc >> 1;
96+
}
8397

98+
void HELPER(afl_maybe_log_trace)(target_ulong cur_loc) {
99+
INC_AFL_AREA(cur_loc);
84100
}
85101

86102
static target_ulong pc_hash(target_ulong x) {
@@ -116,7 +132,11 @@ static void afl_gen_trace(target_ulong cur_loc) {
116132
if (cur_loc >= afl_inst_rms) return;
117133

118134
TCGv cur_loc_v = tcg_const_tl(cur_loc);
119-
gen_helper_afl_maybe_log(cur_loc_v);
135+
if (unlikely(afl_track_unstable_log_fd() >= 0)) {
136+
gen_helper_afl_maybe_log_trace(cur_loc_v);
137+
} else {
138+
gen_helper_afl_maybe_log(cur_loc_v);
139+
}
120140
tcg_temp_free(cur_loc_v);
121141

122142
}
@@ -1930,7 +1950,7 @@ TranslationBlock *afl_gen_edge(CPUState *cpu, unsigned long afl_id)
19301950
tcg_func_start(tcg_ctx);
19311951

19321952
tcg_ctx->cpu = env_cpu(env);
1933-
1953+
19341954
target_ulong afl_loc = afl_id & (MAP_SIZE -1);
19351955
//*afl_dynamic_size = MAX(*afl_dynamic_size, afl_loc);
19361956
TCGv tmp0 = tcg_const_tl(afl_loc);
@@ -2075,6 +2095,18 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
20752095

20762096
trace_translate_block(tb, tb->pc, tb->tc.ptr);
20772097

2098+
/* If we are tracking block instability, then since afl-fuzz will log the ids
2099+
of the unstable blocks, in fuzzer_stats, we must log these alongside the
2100+
instruction pointer so that the user can associate these back with the
2101+
actual binary */
2102+
int track_fd = afl_track_unstable_log_fd();
2103+
if (unlikely(track_fd >= 0)) {
2104+
uintptr_t block_id = (uintptr_t)(afl_hash_ip((uint64_t)pc));
2105+
block_id &= (MAP_SIZE - 1);
2106+
dprintf(track_fd, "BLOCK ID: 0x%016" PRIx64 ", PC: 0x%016zx-0x%016zx\n",
2107+
block_id, pc, pc + tb->size);
2108+
}
2109+
20782110
/* generate machine code */
20792111
tb->jmp_reset_offset[0] = TB_JMP_RESET_OFFSET_INVALID;
20802112
tb->jmp_reset_offset[1] = TB_JMP_RESET_OFFSET_INVALID;

0 commit comments

Comments
 (0)