Skip to content

Commit 20dea26

Browse files
authored
Merge pull request #40 from rmalmain/internal_exit
Internal exit
2 parents a3b6274 + 4347742 commit 20dea26

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

libafl_extras/exit.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ CPUState* libafl_last_exit_cpu(void)
9797
return NULL;
9898
}
9999

100+
void libafl_exit_request_internal(CPUState* cpu, uint64_t pc, ShutdownCause cause, int signal)
101+
{
102+
last_exit_reason.kind = INTERNAL;
103+
last_exit_reason.data.internal.cause = cause;
104+
last_exit_reason.data.internal.signal = signal;
105+
106+
last_exit_reason.cpu = cpu;
107+
last_exit_reason.next_pc = pc;
108+
expected_exit = true;
109+
}
110+
100111
void libafl_exit_request_sync_backdoor(CPUState* cpu, target_ulong pc)
101112
{
102113
last_exit_reason.kind = SYNC_BACKDOOR;

libafl_extras/exit.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,31 @@ int libafl_qemu_set_breakpoint(target_ulong pc);
1919
int libafl_qemu_remove_breakpoint(target_ulong pc);
2020

2121
enum libafl_exit_reason_kind {
22-
BREAKPOINT = 0,
23-
SYNC_BACKDOOR = 1,
22+
INTERNAL = 0,
23+
BREAKPOINT = 1,
24+
SYNC_BACKDOOR = 2,
2425
};
2526

27+
// A breakpoint has been triggered.
2628
struct libafl_exit_reason_breakpoint {
2729
target_ulong addr;
2830
};
2931

32+
// A synchronous exit has been triggered.
3033
struct libafl_exit_reason_sync_backdoor { };
3134

35+
// QEMU exited on its own for some reason.
36+
struct libafl_exit_reason_internal {
37+
ShutdownCause cause;
38+
int signal; // valid if cause == SHUTDOWN_CAUSE_HOST_SIGNAL
39+
};
40+
3241
struct libafl_exit_reason {
3342
enum libafl_exit_reason_kind kind;
3443
CPUState* cpu; // CPU that triggered an exit.
3544
vaddr next_pc; // The PC that should be stored in the CPU when re-entering.
3645
union {
46+
struct libafl_exit_reason_internal internal;
3747
struct libafl_exit_reason_breakpoint breakpoint; // kind == BREAKPOINT
3848
struct libafl_exit_reason_sync_backdoor backdoor; // kind == SYNC_BACKDOOR
3949
} data;
@@ -47,6 +57,7 @@ void libafl_exit_signal_vm_start(void);
4757
bool libafl_exit_asap(void);
4858
void libafl_sync_exit_cpu(void);
4959

60+
void libafl_exit_request_internal(CPUState* cpu, uint64_t pc, ShutdownCause cause, int signal);
5061
void libafl_exit_request_sync_backdoor(CPUState* cpu, target_ulong pc);
5162
void libafl_exit_request_breakpoint(CPUState* cpu, target_ulong pc);
5263
struct libafl_exit_reason* libafl_get_exit_reason(void);

system/runstate.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ static RunState current_run_state = RUN_STATE_PRELAUNCH;
7070
static RunState vmstop_requested = RUN_STATE__MAX;
7171
static QemuMutex vmstop_lock;
7272

73+
//// --- Begin LibAFL code ---
74+
void libafl_exit_request_internal(CPUState* cpu, uint64_t pc, ShutdownCause cause, int signal);
75+
//// --- End LibAFL code ---
76+
7377
typedef struct {
7478
RunState from;
7579
RunState to;
@@ -582,6 +586,16 @@ void qemu_system_reset_request(ShutdownCause reason)
582586
} else {
583587
reset_requested = reason;
584588
}
589+
590+
//// --- Begin LibAFL code ---
591+
if (current_cpu) {
592+
CPUClass *cc = CPU_GET_CLASS(current_cpu);
593+
libafl_exit_request_internal(current_cpu, cc->get_pc(current_cpu), shutdown_requested, -1);
594+
} else {
595+
libafl_exit_request_internal(NULL, 0, shutdown_requested, -1);
596+
}
597+
//// --- End LibAFL code ---
598+
585599
cpu_stop_current();
586600
qemu_notify_event();
587601
}
@@ -662,6 +676,16 @@ void qemu_system_killed(int signal, pid_t pid)
662676
* we are in a signal handler.
663677
*/
664678
shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
679+
680+
//// --- Begin LibAFL code ---
681+
if (current_cpu) {
682+
CPUClass *cc = CPU_GET_CLASS(current_cpu);
683+
libafl_exit_request_internal(current_cpu, cc->get_pc(current_cpu), shutdown_requested, signal);
684+
} else {
685+
libafl_exit_request_internal(NULL, 0, shutdown_requested, signal);
686+
}
687+
//// --- End LibAFL code ---
688+
665689
qemu_notify_event();
666690
}
667691

@@ -677,6 +701,16 @@ void qemu_system_shutdown_request(ShutdownCause reason)
677701
trace_qemu_system_shutdown_request(reason);
678702
replay_shutdown_request(reason);
679703
shutdown_requested = reason;
704+
705+
//// --- Begin LibAFL code ---
706+
if (current_cpu) {
707+
CPUClass *cc = CPU_GET_CLASS(current_cpu);
708+
libafl_exit_request_internal(current_cpu, cc->get_pc(current_cpu), shutdown_requested, -1);
709+
} else {
710+
libafl_exit_request_internal(NULL, 0, shutdown_requested, -1);
711+
}
712+
//// --- End LibAFL code ---
713+
680714
qemu_notify_event();
681715
}
682716

0 commit comments

Comments
 (0)