xref: /aosp_15_r20/external/AFLplusplus/utils/qemu_persistent_hook/read_into_rdi.c (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1 #include "../../qemu_mode/qemuafl/qemuafl/api.h"
2 
3 #include <stdio.h>
4 #include <string.h>
5 
6 #define g2h(x) ((void *)((unsigned long)(x) + guest_base))
7 #define h2g(x) ((uint64_t)(x)-guest_base)
8 
afl_persistent_hook(struct x86_64_regs * regs,uint64_t guest_base,uint8_t * input_buf,uint32_t input_buf_len)9 void afl_persistent_hook(struct x86_64_regs *regs, uint64_t guest_base,
10                          uint8_t *input_buf, uint32_t input_buf_len) {
11 
12   // In this example the register RDI is pointing to the memory location
13   // of the target buffer, and the length of the input is in RSI.
14   // This can be seen with a debugger, e.g. gdb (and "disass main")
15 
16   printf("Placing input into 0x%lx\n", regs->rdi);
17 
18   if (input_buf_len > 1024) input_buf_len = 1024;
19   memcpy(g2h(regs->rdi), input_buf, input_buf_len);
20   regs->rsi = input_buf_len;
21 
22 }
23 
24 #undef g2h
25 #undef h2g
26 
afl_persistent_hook_init(void)27 int afl_persistent_hook_init(void) {
28 
29   // 1 for shared memory input (faster), 0 for normal input (you have to use
30   // read(), input_buf will be NULL)
31   return 1;
32 
33 }
34 
35