Skip to content

Commit 3ebc96e

Browse files
authored
Fixed invalid addresses in read hooks (#70)
* fix read hooks returning potentially wrong address
1 parent 538e6b0 commit 3ebc96e

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

tcg/tcg-op-ldst.c

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@
3333

3434
//// --- Begin LibAFL code ---
3535

36+
/* Copied over from the plugin_maybe_preserve_addr function
37+
* The variable needs to be free'd after use
38+
*
39+
* Only required for loads, where value might overlap addr.
40+
*/
41+
static TCGv_i64 libafl_gen_preserve_addr(TCGTemp *addr)
42+
{
43+
/* Save a copy of the vaddr for use after a load. */
44+
TCGv_i64 temp = tcg_temp_ebb_new_i64();
45+
46+
if (tcg_ctx->addr_type == TCG_TYPE_I32) {
47+
tcg_gen_extu_i32_i64(temp, temp_tcgv_i32(addr));
48+
} else {
49+
tcg_gen_mov_i64(temp, temp_tcgv_i64(addr));
50+
}
51+
52+
return temp;
53+
}
54+
3655
void libafl_gen_read(TCGTemp *addr, MemOpIdx oi);
3756
void libafl_gen_write(TCGTemp *addr, MemOpIdx oi);
3857

@@ -208,12 +227,20 @@ static void tcg_gen_qemu_ld_i32_int(TCGv_i32 val, TCGTemp *addr,
208227
} else {
209228
opc = INDEX_op_qemu_ld_a64_i32;
210229
}
230+
231+
//// --- Begin LibAFL code ---
232+
233+
TCGv_i64 libafl_addr = libafl_gen_preserve_addr(addr);
234+
235+
//// --- End LibAFL code ---
236+
211237
gen_ldst(opc, tcgv_i32_temp(val), NULL, addr, oi);
212238
plugin_gen_mem_callbacks(copy_addr, addr, orig_oi, QEMU_PLUGIN_MEM_R);
213239

214240
//// --- Begin LibAFL code ---
215241

216-
libafl_gen_read(addr, oi);
242+
libafl_gen_read(tcgv_i64_temp(libafl_addr), orig_oi);
243+
tcg_temp_free_i64(libafl_addr);
217244

218245
//// --- End LibAFL code ---
219246

@@ -341,12 +368,20 @@ static void tcg_gen_qemu_ld_i64_int(TCGv_i64 val, TCGTemp *addr,
341368
} else {
342369
opc = INDEX_op_qemu_ld_a64_i64;
343370
}
371+
372+
//// --- Begin LibAFL code ---
373+
374+
TCGv_i64 libafl_addr = libafl_gen_preserve_addr(addr);
375+
376+
//// --- End LibAFL code ---
377+
344378
gen_ldst_i64(opc, val, addr, oi);
345379
plugin_gen_mem_callbacks(copy_addr, addr, orig_oi, QEMU_PLUGIN_MEM_R);
346380

347381
//// --- Begin LibAFL code ---
348382

349-
libafl_gen_read(addr, oi);
383+
libafl_gen_read(tcgv_i64_temp(libafl_addr), orig_oi);
384+
tcg_temp_free_i64(libafl_addr);
350385

351386
//// --- End LibAFL code ---
352387

0 commit comments

Comments
 (0)