Skip to content

Commit 0b7b1f1

Browse files
Revert "Revert "Implement user-space QEMU ASAN (#45)" (#56)" (#59)
This reverts commit 821ad47.
1 parent ce4dbbc commit 0b7b1f1

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

include/tcg/tcg-op.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,19 @@ typedef TCGv_i32 TCGv;
5353
#define tcg_temp_new() tcg_temp_new_i32()
5454
#define tcg_global_mem_new tcg_global_mem_new_i32
5555
#define tcgv_tl_temp tcgv_i32_temp
56+
#define temp_tcgv_tl temp_tcgv_i32
5657
#define tcg_gen_qemu_ld_tl tcg_gen_qemu_ld_i32
5758
#define tcg_gen_qemu_st_tl tcg_gen_qemu_st_i32
59+
#define tcg_gen_tl_ptr tcg_gen_ext_i32_ptr
5860
#elif TARGET_LONG_BITS == 64
5961
typedef TCGv_i64 TCGv;
6062
#define tcg_temp_new() tcg_temp_new_i64()
6163
#define tcg_global_mem_new tcg_global_mem_new_i64
6264
#define tcgv_tl_temp tcgv_i64_temp
65+
#define temp_tcgv_tl temp_tcgv_i64
6366
#define tcg_gen_qemu_ld_tl tcg_gen_qemu_ld_i64
6467
#define tcg_gen_qemu_st_tl tcg_gen_qemu_st_i64
68+
#define tcg_gen_tl_ptr tcg_gen_trunc_i64_ptr
6569
#else
6670
#error Unhandled TARGET_LONG_BITS value
6771
#endif

libafl_extras/hook.c

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ static TCGHelperInfo libafl_exec_write_hookN_info = {
340340
struct libafl_rw_hook* libafl_read_hooks;
341341
size_t libafl_read_hooks_num = 0;
342342

343-
size_t libafl_add_read_hook(uint64_t (*gen)(uint64_t data, target_ulong pc, MemOpIdx oi),
343+
size_t libafl_add_read_hook(uint64_t (*gen)(uint64_t data, target_ulong pc, TCGTemp *addr, MemOpIdx oi),
344344
void (*exec1)(uint64_t data, uint64_t id, target_ulong addr),
345345
void (*exec2)(uint64_t data, uint64_t id, target_ulong addr),
346346
void (*exec4)(uint64_t data, uint64_t id, target_ulong addr),
@@ -394,7 +394,7 @@ GEN_REMOVE_HOOK(read)
394394
struct libafl_rw_hook* libafl_write_hooks;
395395
size_t libafl_write_hooks_num = 0;
396396

397-
size_t libafl_add_write_hook(uint64_t (*gen)(uint64_t data, target_ulong pc, MemOpIdx oi),
397+
size_t libafl_add_write_hook(uint64_t (*gen)(uint64_t data, target_ulong pc, TCGTemp *addr, MemOpIdx oi),
398398
void (*exec1)(uint64_t data, uint64_t id, target_ulong addr),
399399
void (*exec2)(uint64_t data, uint64_t id, target_ulong addr),
400400
void (*exec4)(uint64_t data, uint64_t id, target_ulong addr),
@@ -452,7 +452,7 @@ static void libafl_gen_rw(TCGTemp *addr, MemOpIdx oi, struct libafl_rw_hook* hoo
452452
while (hook) {
453453
uint64_t cur_id = 0;
454454
if (hook->gen)
455-
cur_id = hook->gen(hook->data, libafl_gen_cur_pc, oi);
455+
cur_id = hook->gen(hook->data, libafl_gen_cur_pc, addr, oi);
456456
TCGHelperInfo* info = NULL;
457457
if (size == 1 && hook->helper_info1.func) info = &hook->helper_info1;
458458
else if (size == 2 && hook->helper_info2.func) info = &hook->helper_info2;
@@ -685,3 +685,59 @@ size_t libafl_add_new_thread_hook(bool (*callback)(uint64_t data, uint32_t tid),
685685
}
686686

687687
GEN_REMOVE_HOOK1(new_thread)
688+
689+
#if TARGET_LONG_BITS == 32
690+
#define SHADOW_BASE (0x20000000)
691+
#elif TARGET_LONG_BITS == 64
692+
#define SHADOW_BASE (0x7fff8000)
693+
#else
694+
#error Unhandled TARGET_LONG_BITS value
695+
#endif
696+
697+
void libafl_tcg_gen_asan(TCGTemp * addr, size_t size)
698+
{
699+
if (size == 0)
700+
return;
701+
702+
TCGv addr_val = temp_tcgv_tl(addr);
703+
TCGv k = tcg_temp_new();
704+
TCGv shadow_addr = tcg_temp_new();
705+
TCGv_ptr shadow_ptr = tcg_temp_new_ptr();
706+
TCGv shadow_val = tcg_temp_new();
707+
TCGv test_addr = tcg_temp_new();
708+
TCGv_ptr test_ptr = tcg_temp_new_ptr();
709+
710+
tcg_gen_andi_tl(k, addr_val, 7);
711+
tcg_gen_addi_tl(k, k, size - 1);
712+
713+
tcg_gen_shri_tl(shadow_addr, addr_val, 3);
714+
tcg_gen_addi_tl(shadow_addr, shadow_addr, SHADOW_BASE);
715+
tcg_gen_tl_ptr(shadow_ptr, shadow_addr);
716+
tcg_gen_ld8s_tl(shadow_val, shadow_ptr, 0);
717+
718+
/*
719+
* Making conditional branches here appears to cause QEMU issues with dead
720+
* temporaries so we will instead avoid branches. We will cause the guest
721+
* to perform a NULL dereference in the event of an ASAN fault. Note that
722+
* we will do this by using a store rather than a load, since the TCG may
723+
* otherwise determine that the result of the load is unused and simply
724+
* discard the operation. In the event that the shadow memory doesn't
725+
* detect a fault, we will simply write the value read from the shadow
726+
* memory back to it's original location. If, however, the shadow memory
727+
* detects an invalid access, we will instead attempt to write the value
728+
* at 0x0.
729+
*/
730+
tcg_gen_movcond_tl(TCG_COND_EQ, test_addr,
731+
shadow_val, tcg_constant_tl(0),
732+
shadow_addr, tcg_constant_tl(0));
733+
734+
if (size < 8)
735+
{
736+
tcg_gen_movcond_tl(TCG_COND_GE, test_addr,
737+
k, shadow_val,
738+
test_addr, shadow_addr);
739+
}
740+
741+
tcg_gen_tl_ptr(test_ptr, test_addr);
742+
tcg_gen_st8_tl(shadow_val, test_ptr, 0);
743+
}

libafl_extras/hook.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int libafl_qemu_remove_block_hook(size_t num, int invalidate);
9393
bool libafl_qemu_block_hook_set_jit(size_t num, size_t (*jit)(uint64_t, uint64_t)); // no param names to avoid to be marked as safe
9494

9595
struct libafl_rw_hook {
96-
uint64_t (*gen)(uint64_t data, target_ulong pc, MemOpIdx oi);
96+
uint64_t (*gen)(uint64_t data, target_ulong pc, TCGTemp* addr, MemOpIdx oi);
9797
/*void (*exec1)(uint64_t data, uint64_t id, target_ulong addr);
9898
void (*exec2)(uint64_t data, uint64_t id, target_ulong addr);
9999
void (*exec4)(uint64_t data, uint64_t id, target_ulong addr);
@@ -116,14 +116,14 @@ struct libafl_rw_hook {
116116
extern struct libafl_rw_hook* libafl_read_hooks;
117117
extern struct libafl_rw_hook* libafl_write_hooks;
118118

119-
size_t libafl_add_read_hook(uint64_t (*gen)(uint64_t data, target_ulong pc, MemOpIdx oi),
119+
size_t libafl_add_read_hook(uint64_t (*gen)(uint64_t data, target_ulong pc, TCGTemp *addr, MemOpIdx oi),
120120
void (*exec1)(uint64_t data, uint64_t id, target_ulong addr),
121121
void (*exec2)(uint64_t data, uint64_t id, target_ulong addr),
122122
void (*exec4)(uint64_t data, uint64_t id, target_ulong addr),
123123
void (*exec8)(uint64_t data, uint64_t id, target_ulong addr),
124124
void (*execN)(uint64_t data, uint64_t id, target_ulong addr, size_t size),
125125
uint64_t data);
126-
size_t libafl_add_write_hook(uint64_t (*gen)(uint64_t data, target_ulong pc, MemOpIdx oi),
126+
size_t libafl_add_write_hook(uint64_t (*gen)(uint64_t data, target_ulong pc, TCGTemp *addr, MemOpIdx oi),
127127
void (*exec1)(uint64_t data, uint64_t id, target_ulong addr),
128128
void (*exec2)(uint64_t data, uint64_t id, target_ulong addr),
129129
void (*exec4)(uint64_t data, uint64_t id, target_ulong addr),
@@ -222,3 +222,5 @@ extern struct libafl_new_thread_hook* libafl_new_thread_hooks;
222222
size_t libafl_add_new_thread_hook(bool (*callback)(uint64_t data, uint32_t tid),
223223
uint64_t data);
224224
int libafl_qemu_remove_new_thread_hook(size_t num);
225+
226+
void libafl_tcg_gen_asan(TCGTemp * addr, size_t size);

0 commit comments

Comments
 (0)