Changes to explicitly place the previous_pc

This commit is contained in:
jon 2021-12-27 01:45:12 +00:00 committed by jon
parent 54eca027a5
commit 81aae9b54c
9 changed files with 94 additions and 28 deletions

View File

@ -32,7 +32,7 @@ int main (int argc, char** argv, char** envp) {
dl_iterate_phdr(phdr_callback, &base); dl_iterate_phdr(phdr_callback, &base);
printf("0x%016lx\n", base); printf("%p\n", (void *)base);
if (base == 0) { return 1; } if (base == 0) { return 1; }
return 0; return 0;

View File

@ -5,14 +5,13 @@
#include "config.h" #include "config.h"
extern char * instrument_debug_filename; extern char * instrument_debug_filename;
extern char * instrument_coverage_filename; extern char * instrument_coverage_filename;
extern gboolean instrument_tracing; extern gboolean instrument_tracing;
extern gboolean instrument_optimize; extern gboolean instrument_optimize;
extern gboolean instrument_unique; extern gboolean instrument_unique;
extern __thread guint64 instrument_previous_pc; extern guint64 instrument_hash_zero;
extern guint64 instrument_hash_zero; extern char * instrument_coverage_unstable_filename;
extern char * instrument_coverage_unstable_filename;
extern gboolean instrument_use_fixed_seed; extern gboolean instrument_use_fixed_seed;
extern guint64 instrument_fixed_seed; extern guint64 instrument_fixed_seed;
@ -20,6 +19,8 @@ extern guint64 instrument_fixed_seed;
extern uint8_t *__afl_area_ptr; extern uint8_t *__afl_area_ptr;
extern uint32_t __afl_map_size; extern uint32_t __afl_map_size;
extern __thread guint64 *instrument_previous_pc_addr;
void instrument_config(void); void instrument_config(void);
void instrument_init(void); void instrument_init(void);

View File

@ -32,12 +32,13 @@ char * instrument_coverage_unstable_filename = NULL;
static GumStalkerTransformer *transformer = NULL; static GumStalkerTransformer *transformer = NULL;
__attribute__((aligned(0x1000))) __thread guint64 instrument_previous_pc = 0;
static GumAddress previous_rip = 0; static GumAddress previous_rip = 0;
static GumAddress previous_end = 0; static GumAddress previous_end = 0;
static u8 * edges_notified = NULL; static u8 * edges_notified = NULL;
__thread guint64 instrument_previous_pc;
__thread guint64 *instrument_previous_pc_addr = NULL;
typedef struct { typedef struct {
GumAddress address; GumAddress address;
@ -105,8 +106,14 @@ __attribute__((hot)) static void on_basic_block(GumCpuContext *context,
guint16 current_end = ctx->end; guint16 current_end = ctx->end;
guint64 current_pc = instrument_get_offset_hash(current_rip); guint64 current_pc = instrument_get_offset_hash(current_rip);
guint64 edge; guint64 edge;
if (instrument_previous_pc_addr == NULL) {
edge = current_pc ^ instrument_previous_pc; instrument_previous_pc_addr = &instrument_previous_pc;
*instrument_previous_pc_addr = instrument_hash_zero;
}
edge = current_pc ^ *instrument_previous_pc_addr;
instrument_increment_map(edge); instrument_increment_map(edge);
@ -136,7 +143,7 @@ __attribute__((hot)) static void on_basic_block(GumCpuContext *context,
previous_end = current_end; previous_end = current_end;
gsize map_size_pow2 = util_log2(__afl_map_size); gsize map_size_pow2 = util_log2(__afl_map_size);
instrument_previous_pc = util_rotate(current_pc, 1, map_size_pow2); *instrument_previous_pc_addr = util_rotate(current_pc, 1, map_size_pow2);
} }
@ -393,7 +400,11 @@ GumStalkerTransformer *instrument_get_transformer(void) {
void instrument_on_fork() { void instrument_on_fork() {
instrument_previous_pc = instrument_hash_zero; if (instrument_previous_pc_addr != NULL) {
*instrument_previous_pc_addr = instrument_hash_zero;
}
} }

View File

@ -155,10 +155,23 @@ void instrument_coverage_optimize(const cs_insn * instr,
afl_log_code code = {0}; afl_log_code code = {0};
GumArm64Writer *cw = output->writer.arm64; GumArm64Writer *cw = output->writer.arm64;
guint64 area_offset = instrument_get_offset_hash(GUM_ADDRESS(instr->address)); guint64 area_offset = instrument_get_offset_hash(GUM_ADDRESS(instr->address));
gsize map_size_pow2; gsize map_size_pow2;
gsize area_offset_ror; gsize area_offset_ror;
GumAddress code_addr = 0; GumAddress code_addr = 0;
if (instrument_previous_pc_addr == NULL) {
GumAddressSpec spec = {.near_address = cw->code,
.max_distance = 1ULL << 30};
instrument_previous_pc_addr = gum_memory_allocate_near(
&spec, sizeof(guint64), 0x1000, GUM_PAGE_READ | GUM_PAGE_WRITE);
*instrument_previous_pc_addr = instrument_hash_zero;
FVERBOSE("instrument_previous_pc_addr: %p", instrument_previous_pc_addr);
FVERBOSE("code_addr: %p", cw->code);
}
// gum_arm64_writer_put_brk_imm(cw, 0x0); // gum_arm64_writer_put_brk_imm(cw, 0x0);
code_addr = cw->pc; code_addr = cw->pc;
@ -170,13 +183,13 @@ void instrument_coverage_optimize(const cs_insn * instr,
* 64KB in size, then it should also end on a 64 KB boundary. It is followed * 64KB in size, then it should also end on a 64 KB boundary. It is followed
* by our previous_pc, so this too should be 64KB aligned. * by our previous_pc, so this too should be 64KB aligned.
*/ */
g_assert(PAGE_ALIGNED(&instrument_previous_pc)); g_assert(PAGE_ALIGNED(instrument_previous_pc_addr));
g_assert(PAGE_ALIGNED(__afl_area_ptr)); g_assert(PAGE_ALIGNED(__afl_area_ptr));
instrument_patch_ardp( instrument_patch_ardp(
&code.code.adrp_x0_prev_loc1, &code.code.adrp_x0_prev_loc1,
code_addr + offsetof(afl_log_code, code.adrp_x0_prev_loc1), code_addr + offsetof(afl_log_code, code.adrp_x0_prev_loc1),
GUM_ADDRESS(&instrument_previous_pc)); GUM_ADDRESS(instrument_previous_pc_addr));
code.code.mov_x0_curr_loc |= area_offset << 5; code.code.mov_x0_curr_loc |= area_offset << 5;
@ -191,7 +204,7 @@ void instrument_coverage_optimize(const cs_insn * instr,
instrument_patch_ardp( instrument_patch_ardp(
&code.code.adrp_x0_prev_loc2, &code.code.adrp_x0_prev_loc2,
code_addr + offsetof(afl_log_code, code.adrp_x0_prev_loc2), code_addr + offsetof(afl_log_code, code.adrp_x0_prev_loc2),
GUM_ADDRESS(&instrument_previous_pc)); GUM_ADDRESS(instrument_previous_pc_addr));
code.code.mov_x1_curr_loc_shr_1 |= (area_offset_ror << 5); code.code.mov_x1_curr_loc_shr_1 |= (area_offset_ror << 5);
@ -214,7 +227,6 @@ void instrument_coverage_optimize_init(void) {
} }
FVERBOSE("__afl_area_ptr: %p", __afl_area_ptr); FVERBOSE("__afl_area_ptr: %p", __afl_area_ptr);
FVERBOSE("instrument_previous_pc: %p", &instrument_previous_pc);
} }

View File

@ -360,7 +360,6 @@ void instrument_coverage_optimize_init(void) {
} }
FVERBOSE("__afl_area_ptr: %p", __afl_area_ptr); FVERBOSE("__afl_area_ptr: %p", __afl_area_ptr);
FVERBOSE("instrument_previous_pc: %p", &instrument_previous_pc);
} }
@ -439,6 +438,18 @@ void instrument_coverage_optimize(const cs_insn * instr,
gsize map_size_pow2; gsize map_size_pow2;
gsize area_offset_ror; gsize area_offset_ror;
GumAddress code_addr = 0; GumAddress code_addr = 0;
if (instrument_previous_pc_addr == NULL) {
GumAddressSpec spec = {.near_address = cw->code,
.max_distance = 1ULL << 30};
instrument_previous_pc_addr = gum_memory_allocate_near(
&spec, sizeof(guint64), 0x1000, GUM_PAGE_READ | GUM_PAGE_WRITE);
*instrument_previous_pc_addr = instrument_hash_zero;
FVERBOSE("instrument_previous_pc_addr: %p", instrument_previous_pc_addr);
FVERBOSE("code_addr: %p", cw->code);
}
instrument_coverage_suppress_init(); instrument_coverage_suppress_init();
@ -462,7 +473,7 @@ void instrument_coverage_optimize(const cs_insn * instr,
*((guint32 *)&code.bytes[curr_loc_shr_1_offset]) = (guint32)(area_offset_ror); *((guint32 *)&code.bytes[curr_loc_shr_1_offset]) = (guint32)(area_offset_ror);
gssize prev_loc_value = gssize prev_loc_value =
GPOINTER_TO_SIZE(&instrument_previous_pc) - GPOINTER_TO_SIZE(instrument_previous_pc_addr) -
(code_addr + offsetof(afl_log_code, code.mov_prev_loc_curr_loc_shr1) + (code_addr + offsetof(afl_log_code, code.mov_prev_loc_curr_loc_shr1) +
sizeof(code.code.mov_prev_loc_curr_loc_shr1)); sizeof(code.code.mov_prev_loc_curr_loc_shr1));
gssize prev_loc_value_offset = gssize prev_loc_value_offset =
@ -478,7 +489,7 @@ void instrument_coverage_optimize(const cs_insn * instr,
*((gint *)&code.bytes[prev_loc_value_offset]) = (gint)prev_loc_value; *((gint *)&code.bytes[prev_loc_value_offset]) = (gint)prev_loc_value;
gssize prev_loc_value2 = gssize prev_loc_value2 =
GPOINTER_TO_SIZE(&instrument_previous_pc) - GPOINTER_TO_SIZE(instrument_previous_pc_addr) -
(code_addr + offsetof(afl_log_code, code.mov_eax_prev_loc) + (code_addr + offsetof(afl_log_code, code.mov_eax_prev_loc) +
sizeof(code.code.mov_eax_prev_loc)); sizeof(code.code.mov_eax_prev_loc));
gssize prev_loc_value_offset2 = gssize prev_loc_value_offset2 =

View File

@ -153,6 +153,19 @@ void instrument_coverage_optimize(const cs_insn * instr,
gsize map_size_pow2; gsize map_size_pow2;
gsize area_offset_ror; gsize area_offset_ror;
if (instrument_previous_pc_addr == NULL) {
GumAddressSpec spec = {.near_address = cw->code,
.max_distance = 1ULL << 30};
instrument_previous_pc_addr = gum_memory_allocate_near(
&spec, sizeof(guint64), 0x1000, GUM_PAGE_READ | GUM_PAGE_WRITE);
*instrument_previous_pc_addr = instrument_hash_zero;
FVERBOSE("instrument_previous_pc_addr: %p", instrument_previous_pc_addr);
FVERBOSE("code_addr: %p", cw->code);
}
code.code = template; code.code = template;
instrument_coverage_suppress_init(); instrument_coverage_suppress_init();
@ -170,7 +183,7 @@ void instrument_coverage_optimize(const cs_insn * instr,
sizeof(code.code.mov_eax_prev_loc) - sizeof(gint); sizeof(code.code.mov_eax_prev_loc) - sizeof(gint);
*((gint *)&code.bytes[prev_loc_value_offset2]) = *((gint *)&code.bytes[prev_loc_value_offset2]) =
(gint)GPOINTER_TO_SIZE(&instrument_previous_pc); (gint)GPOINTER_TO_SIZE(instrument_previous_pc_addr);
gssize curr_loc_shr_1_offset = gssize curr_loc_shr_1_offset =
offsetof(afl_log_code, code.mov_prev_loc_curr_loc_shr1) + offsetof(afl_log_code, code.mov_prev_loc_curr_loc_shr1) +
@ -187,7 +200,7 @@ void instrument_coverage_optimize(const cs_insn * instr,
sizeof(guint32); sizeof(guint32);
*((gint *)&code.bytes[prev_loc_value_offset]) = *((gint *)&code.bytes[prev_loc_value_offset]) =
(gint)GPOINTER_TO_SIZE(&instrument_previous_pc); (gint)GPOINTER_TO_SIZE(instrument_previous_pc_addr);
gssize xor_curr_loc_offset = offsetof(afl_log_code, code.xor_eax_curr_loc) + gssize xor_curr_loc_offset = offsetof(afl_log_code, code.xor_eax_curr_loc) +
sizeof(code.code.xor_eax_curr_loc) - sizeof(code.code.xor_eax_curr_loc) -

View File

@ -236,7 +236,13 @@ static void instrument_exit(GumArm64Writer *cw) {
static int instrument_afl_persistent_loop_func(void) { static int instrument_afl_persistent_loop_func(void) {
int ret = __afl_persistent_loop(persistent_count); int ret = __afl_persistent_loop(persistent_count);
instrument_previous_pc = instrument_hash_zero; if (instrument_previous_pc_addr == NULL) {
FATAL("instrument_previous_pc_addr uninitialized");
}
*instrument_previous_pc_addr = instrument_hash_zero;
return ret; return ret;
} }

View File

@ -173,7 +173,13 @@ static void instrument_exit(GumX86Writer *cw) {
static int instrument_afl_persistent_loop_func(void) { static int instrument_afl_persistent_loop_func(void) {
int ret = __afl_persistent_loop(persistent_count); int ret = __afl_persistent_loop(persistent_count);
instrument_previous_pc = instrument_hash_zero; if (instrument_previous_pc_addr == NULL) {
FATAL("instrument_previous_pc_addr uninitialized");
}
*instrument_previous_pc_addr = instrument_hash_zero;
return ret; return ret;
} }

View File

@ -130,7 +130,13 @@ static void instrument_exit(GumX86Writer *cw) {
static int instrument_afl_persistent_loop_func(void) { static int instrument_afl_persistent_loop_func(void) {
int ret = __afl_persistent_loop(persistent_count); int ret = __afl_persistent_loop(persistent_count);
instrument_previous_pc = instrument_hash_zero; if (instrument_previous_pc_addr == NULL) {
FATAL("instrument_previous_pc_addr uninitialized");
}
*instrument_previous_pc_addr = instrument_hash_zero;
return ret; return ret;
} }