Skip to content

Commit c834bda

Browse files
committed
breakpoint working with gdb and libafl qemu in parallel
1 parent af27154 commit c834bda

File tree

12 files changed

+66
-37
lines changed

12 files changed

+66
-37
lines changed

accel/tcg/cpu-exec.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -714,17 +714,15 @@ static inline void cpu_handle_debug_exception(CPUState *cpu)
714714

715715
static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
716716
{
717-
//// --- Begin LibAFL code ---
718-
717+
//// --- Begin LibAFL code ---
719718
if (cpu->exception_index == EXCP_LIBAFL_EXIT) {
720719
*ret = cpu->exception_index;
721720
cpu->exception_index = -1;
722721

723722
libafl_sync_exit_cpu();
724723
return true;
725724
}
726-
727-
//// --- End LibAFL code ---
725+
//// --- End LibAFL code ---
728726

729727
if (cpu->exception_index < 0) {
730728
#ifndef CONFIG_USER_ONLY

include/libafl/system.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ int libafl_qemu_set_hw_breakpoint(vaddr addr);
66
int libafl_qemu_remove_hw_breakpoint(vaddr addr);
77

88
void libafl_qemu_init(int argc, char** argv);
9+
int libafl_qemu_run(void);
10+
11+
size_t libafl_target_page_size(void);
12+
int libafl_target_page_mask(void);
13+
int libafl_target_page_offset_mask(void);

include/libafl/user.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#include "exec/cpu-defs.h"
88

99
struct libafl_mapinfo {
10-
target_ulong start;
11-
target_ulong end;
12-
target_ulong offset;
10+
uint64_t start;
11+
uint64_t end;
12+
uint64_t offset;
1313
const char* path;
1414
int flags;
1515
int is_priv;

include/system/runstate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ void qemu_system_shutdown_request(ShutdownCause reason);
9595
void qemu_system_powerdown_request(void);
9696
void qemu_register_powerdown_notifier(Notifier *notifier);
9797
void qemu_register_shutdown_notifier(Notifier *notifier);
98+
void qemu_system_return_request(void);
9899
void qemu_system_debug_request(void);
99100
void qemu_system_vmstop_request(RunState reason);
100101
void qemu_system_vmstop_request_prepare(void);

libafl/exit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static void prepare_qemu_exit(CPUState* cpu, target_ulong next_pc)
8080
last_exit_reason.next_pc = next_pc;
8181

8282
#ifndef CONFIG_USER_ONLY
83-
qemu_system_debug_request();
83+
qemu_system_return_request();
8484
#endif
8585

8686
// in usermode, this may be called from the syscall hook, thus already out
@@ -146,7 +146,7 @@ void libafl_exit_request_timeout(void)
146146
last_exit_reason.kind = TIMEOUT;
147147
last_exit_reason.cpu = current_cpu;
148148

149-
qemu_system_debug_request();
149+
qemu_system_return_request();
150150
}
151151
#endif
152152

libafl/system.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,32 @@
44
#include "system/accel-ops.h"
55
#include "system/cpus.h"
66
#include "gdbstub/enums.h"
7+
#include "exec/target_page.h"
8+
#include "qemu/main-loop.h"
9+
#include "system/replay.h"
10+
#include "system/runstate.h"
711

812
#include "libafl/system.h"
913

1014
int libafl_qemu_toggle_hw_breakpoint(vaddr addr, bool set);
1115

12-
void libafl_qemu_init(int argc, char** argv) { qemu_init(argc, argv); }
16+
void libafl_qemu_init(int argc, char** argv)
17+
{
18+
qemu_init(argc, argv);
19+
}
20+
21+
int libafl_qemu_run(void)
22+
{
23+
if (runstate_check(RUN_STATE_RET)) {
24+
// we are resuming from a return to libafl
25+
// transition to RUN_STATE_RUNNING
26+
vm_start();
27+
}
28+
29+
int status = qemu_main_loop();
30+
31+
return status;
32+
}
1333

1434
int libafl_qemu_set_hw_breakpoint(vaddr addr)
1535
{

linux-user/arm/cpu_loop.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,17 +297,13 @@ void cpu_loop(CPUARMState *env)
297297
abi_ulong ret;
298298

299299
//// --- Begin LibAFL code ---
300-
301300
libafl_exit_signal_vm_start();
302-
303301
//// --- End LibAFL code ---
304302

305303
for(;;) {
306304

307305
//// --- Begin LibAFL code ---
308-
309306
if (libafl_exit_asap()) return;
310-
311307
//// --- End LibAFL code ---
312308

313309
cpu_exec_start(cs);
@@ -318,10 +314,8 @@ void cpu_loop(CPUARMState *env)
318314
switch(trapnr) {
319315

320316
//// --- Begin LibAFL code ---
321-
322317
case EXCP_LIBAFL_EXIT:
323318
return;
324-
325319
//// --- End LibAFL code ---
326320

327321
case EXCP_UDEF:

linux-user/ppc/cpu_loop.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,14 @@ void cpu_loop(CPUPPCState *env)
7878
target_ulong ret;
7979

8080
//// --- Begin LibAFL code ---
81-
8281
libafl_exit_signal_vm_start();
83-
8482
//// --- End LibAFL code ---
8583

8684
for(;;) {
8785
bool arch_interrupt;
8886

8987
//// --- Begin LibAFL code ---
90-
9188
if (libafl_exit_asap()) return;
92-
9389
//// --- End LibAFL code ---
9490

9591
cpu_exec_start(cs);
@@ -101,10 +97,8 @@ void cpu_loop(CPUPPCState *env)
10197
switch (trapnr) {
10298

10399
//// --- Begin LibAFL code ---
104-
105100
case EXCP_LIBAFL_EXIT:
106101
return;
107-
108102
//// --- End LibAFL code ---
109103

110104
case POWERPC_EXCP_NONE:

linux-user/syscall.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13953,9 +13953,9 @@ IntervalTreeNode * libafl_maps_next(IntervalTreeNode *pageflags_maps_node, Inter
1395313953
if (flags & PAGE_EXEC) libafl_flags |= PROT_EXEC;
1395413954

1395513955
ret->is_valid = true;
13956-
ret->start = (target_ulong)h2g_nocheck(min);
13957-
ret->end = (target_ulong)h2g_nocheck(max);
13958-
ret->offset = (target_ulong)e->offset;
13956+
ret->start = (uint64_t) h2g_nocheck(min);
13957+
ret->end = (uint64_t) max;
13958+
ret->offset = (uint64_t) e->offset;
1395913959
ret->path = e->path;
1396013960
ret->flags = libafl_flags;
1396113961
ret->is_priv = e->is_priv;

qapi/run-state.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@
5252
# @colo: guest is paused to save/restore VM state under colo
5353
# checkpoint, VM can not get into this state unless colo
5454
# capability is enabled for migration. (since 2.8)
55+
#
56+
# @ret: guest stopped to return to the library caller.
57+
# this is only valid if QEMU is compiled as a library (with AS_LIB).
5558
##
5659
{ 'enum': 'RunState',
5760
'data': [ 'debug', 'inmigrate', 'internal-error', 'io-error', 'paused',
5861
'postmigrate', 'prelaunch', 'finish-migrate', 'restore-vm',
5962
'running', 'save-vm', 'shutdown', 'suspended', 'watchdog',
60-
'guest-panicked', 'colo' ] }
63+
'guest-panicked', 'colo', 'ret' ] }
6164

6265
##
6366
# @ShutdownCause:

0 commit comments

Comments
 (0)