|
71 | 71 |
|
72 | 72 | __thread int cur_block_is_good;
|
73 | 73 |
|
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 | +} |
75 | 87 |
|
| 88 | +void HELPER(afl_maybe_log)(target_ulong cur_loc) { |
76 | 89 | register uintptr_t afl_idx = cur_loc ^ afl_prev_loc;
|
77 | 90 |
|
78 | 91 | INC_AFL_AREA(afl_idx);
|
79 | 92 |
|
80 | 93 | // afl_prev_loc = ((cur_loc & (MAP_SIZE - 1) >> 1)) |
|
81 | 94 | // ((cur_loc & 1) << ((int)ceil(log2(MAP_SIZE)) -1));
|
82 | 95 | afl_prev_loc = cur_loc >> 1;
|
| 96 | +} |
83 | 97 |
|
| 98 | +void HELPER(afl_maybe_log_trace)(target_ulong cur_loc) { |
| 99 | + INC_AFL_AREA(cur_loc); |
84 | 100 | }
|
85 | 101 |
|
86 | 102 | static target_ulong pc_hash(target_ulong x) {
|
@@ -116,7 +132,11 @@ static void afl_gen_trace(target_ulong cur_loc) {
|
116 | 132 | if (cur_loc >= afl_inst_rms) return;
|
117 | 133 |
|
118 | 134 | 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 | + } |
120 | 140 | tcg_temp_free(cur_loc_v);
|
121 | 141 |
|
122 | 142 | }
|
@@ -1930,7 +1950,7 @@ TranslationBlock *afl_gen_edge(CPUState *cpu, unsigned long afl_id)
|
1930 | 1950 | tcg_func_start(tcg_ctx);
|
1931 | 1951 |
|
1932 | 1952 | tcg_ctx->cpu = env_cpu(env);
|
1933 |
| - |
| 1953 | + |
1934 | 1954 | target_ulong afl_loc = afl_id & (MAP_SIZE -1);
|
1935 | 1955 | //*afl_dynamic_size = MAX(*afl_dynamic_size, afl_loc);
|
1936 | 1956 | TCGv tmp0 = tcg_const_tl(afl_loc);
|
@@ -2075,6 +2095,18 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
|
2075 | 2095 |
|
2076 | 2096 | trace_translate_block(tb, tb->pc, tb->tc.ptr);
|
2077 | 2097 |
|
| 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 | + |
2078 | 2110 | /* generate machine code */
|
2079 | 2111 | tb->jmp_reset_offset[0] = TB_JMP_RESET_OFFSET_INVALID;
|
2080 | 2112 | tb->jmp_reset_offset[1] = TB_JMP_RESET_OFFSET_INVALID;
|
|
0 commit comments