Skip to content

Commit

Permalink
use qemu primitives to perform gdb rw (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmalmain authored May 1, 2024
1 parent ddbe5be commit 538e6b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
29 changes: 15 additions & 14 deletions cpu-target.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@

//// --- Begin LibAFL code ---

#include "exec/gdbstub.h"

#include "libafl/exit.h"
#include "libafl/hook.h"

int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg);

static __thread GByteArray *libafl_qemu_mem_buf = NULL;

target_ulong libafl_page_from_addr(target_ulong addr);
Expand Down Expand Up @@ -128,29 +132,26 @@ int libafl_qemu_cpu_index(CPUState* cpu)

int libafl_qemu_write_reg(CPUState* cpu, int reg, uint8_t* val)
{
CPUClass *cc = CPU_GET_CLASS(cpu);
if (reg < cc->gdb_num_core_regs) {
return cc->gdb_write_register(cpu, val, reg);
}
return 0;
return gdb_write_register(cpu, val, reg);
}

int libafl_qemu_read_reg(CPUState* cpu, int reg, uint8_t* val)
{
int len;

if (libafl_qemu_mem_buf == NULL) {
libafl_qemu_mem_buf = g_byte_array_sized_new(64);
}

CPUClass *cc = CPU_GET_CLASS(cpu);
if (reg < cc->gdb_num_core_regs) {
g_byte_array_set_size(libafl_qemu_mem_buf, 0);
int len = cc->gdb_read_register(cpu, libafl_qemu_mem_buf, reg);
if (len > 0) {
memcpy(val, libafl_qemu_mem_buf->data, len);
}
return len;
g_byte_array_set_size(libafl_qemu_mem_buf, 0);

len = gdb_read_register(cpu, libafl_qemu_mem_buf, reg);

if (len > 0) {
memcpy(val, libafl_qemu_mem_buf->data, len);
}
return 0;

return len;
}

int libafl_qemu_num_regs(CPUState* cpu)
Expand Down
7 changes: 6 additions & 1 deletion gdbstub/gdbstub.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,12 @@ int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
return 0;
}

static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)

//// --- Begin LibAFL code ---
int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg);
/* static */
//// --- End LibAFL code ---
int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
{
CPUClass *cc = CPU_GET_CLASS(cpu);
GDBRegisterState *r;
Expand Down

0 comments on commit 538e6b0

Please sign in to comment.