Skip to content

Commit 8db5524

Browse files
Remove unused and duplicate code
1 parent 4226e16 commit 8db5524

File tree

12 files changed

+65
-57
lines changed

12 files changed

+65
-57
lines changed

accel/tcg/cpu-exec.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -715,16 +715,14 @@ static inline void cpu_handle_debug_exception(CPUState *cpu)
715715

716716
//// --- Begin LibAFL code ---
717717

718-
void libafl_sync_exit_cpu(void);
718+
#include "libafl_extras/exit.h"
719719

720720
//// --- End LibAFL code ---
721721

722722
static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
723723
{
724724
//// --- Begin LibAFL code ---
725725

726-
#define EXCP_LIBAFL_EXIT 0xf4775747
727-
728726
if (cpu->exception_index == EXCP_LIBAFL_EXIT) {
729727
*ret = cpu->exception_index;
730728
cpu->exception_index = -1;
@@ -992,8 +990,6 @@ TranslationBlock *libafl_gen_edge(CPUState *cpu, target_ulong src_block,
992990
target_ulong dst_block, int exit_n, target_ulong cs_base,
993991
uint32_t flags, int cflags);
994992

995-
extern __thread int libafl_valid_current_cpu;
996-
997993
//// --- End LibAFL code ---
998994

999995
/* main execution loop */
@@ -1102,6 +1098,8 @@ cpu_exec_loop(CPUState *cpu, SyncClocks *sc)
11021098
}
11031099

11041100
if (has_libafl_edge) {
1101+
// execute the edge to make sure to log it the first execution
1102+
// the edge will then jump to the translated block
11051103
cpu_loop_exec_tb(cpu, edge, last_tb_pc, &last_tb, &tb_exit, &last_tb_pc);
11061104
} else {
11071105
cpu_loop_exec_tb(cpu, tb, pc, &last_tb, &tb_exit, &last_tb_pc);
@@ -1135,12 +1133,6 @@ int cpu_exec(CPUState *cpu)
11351133
/* replay_interrupt may need current_cpu */
11361134
current_cpu = cpu;
11371135

1138-
//// --- Begin LibAFL code ---
1139-
1140-
libafl_valid_current_cpu = 1;
1141-
1142-
//// --- End LibAFL code ---
1143-
11441136
if (cpu_handle_halt(cpu)) {
11451137
return EXCP_HALTED;
11461138
}

accel/tcg/tcg-runtime.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,6 @@ void libafl_load_qemu_snapshot(char *name, bool sync)
132132

133133
#endif
134134

135-
#define EXCP_LIBAFL_EXIT 0xf4775747
136-
137-
#ifdef CONFIG_USER_ONLY
138-
extern __thread int libafl_qemu_break_asap;
139-
#else
140-
extern int libafl_qemu_break_asap;
141-
#endif
142-
143135
void HELPER(libafl_qemu_handle_breakpoint)(CPUArchState *env, uint64_t pc)
144136
{
145137
CPUState* cpu = env_cpu(env);

cpu-target.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ struct libafl_hook {
7272
struct libafl_hook* libafl_qemu_hooks[LIBAFL_TABLES_SIZE];
7373
size_t libafl_qemu_hooks_num = 0;
7474

75-
__thread int libafl_valid_current_cpu = 0;
76-
7775
static __thread GByteArray *libafl_qemu_mem_buf = NULL;
7876

7977
target_ulong libafl_page_from_addr(target_ulong addr);

libafl_extras/exit.c

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,49 @@
33
#include "sysemu/runstate.h"
44
#include "cpu.h"
55

6-
// TODO: merge with definition in tcg-runtime.c
7-
#define EXCP_LIBAFL_EXIT 0xf4775747
8-
96
#ifdef CONFIG_USER_ONLY
10-
__thread int libafl_qemu_break_asap = 0;
11-
__thread CPUState* libafl_breakpoint_cpu;
12-
__thread vaddr libafl_breakpoint_pc;
13-
static __thread struct libafl_exit_reason last_exit_reason;
7+
#define THREAD_MODIFIER __thread
148
#else
15-
static struct libafl_exit_reason last_exit_reason;
9+
#define THREAD_MODIFIER
1610
#endif
1711

12+
static THREAD_MODIFIER struct libafl_exit_reason last_exit_reason;
13+
static THREAD_MODIFIER bool expected_exit = false;
14+
1815
#if defined(TARGET_ARM) && !defined(TARGET_AARCH64)
19-
#define THUMB_MASK(value) (value | cpu_env(libafl_breakpoint_cpu)->thumb)
16+
#define THUMB_MASK(cpu, value) (value | cpu_env(cpu)->thumb)
2017
#else
21-
#define THUMB_MASK(value) value
18+
#define THUMB_MASK(cpu, value) value
2219
#endif
2320

24-
static bool expected_exit = false;
25-
21+
// called before exiting the cpu exec with the custom exception
2622
void libafl_sync_exit_cpu(void)
2723
{
2824
if (last_exit_reason.next_pc) {
2925
CPUClass* cc = CPU_GET_CLASS(last_exit_reason.cpu);
30-
cc->set_pc(last_exit_reason.cpu, THUMB_MASK(last_exit_reason.next_pc));
26+
cc->set_pc(last_exit_reason.cpu, THUMB_MASK(last_exit_reason.cpu, last_exit_reason.next_pc));
3127
}
3228
last_exit_reason.next_pc = 0;
3329
}
3430

35-
bool libafl_exit_asap(void){
36-
return last_exit_reason.exit_asap;
31+
bool libafl_exit_asap(void) {
32+
return expected_exit;
3733
}
3834

39-
static void prepare_qemu_exit(CPUState* cpu, ulong next_pc)
35+
static void prepare_qemu_exit(CPUState* cpu, target_ulong next_pc)
4036
{
4137
expected_exit = true;
4238
last_exit_reason.cpu = cpu;
4339
last_exit_reason.next_pc = next_pc;
4440

4541
#ifndef CONFIG_USER_ONLY
4642
qemu_system_debug_request();
47-
cpu->stopped = true;
43+
cpu->stopped = true; // TODO check if still needed
4844
#endif
45+
// in usermode, this may be called from the syscall hook, thus already out of the cpu_exec but still in the cpu_loop
4946
if (cpu->running) {
5047
cpu->exception_index = EXCP_LIBAFL_EXIT;
5148
cpu_loop_exit(cpu);
52-
} else {
53-
last_exit_reason.exit_asap = 1;
5449
}
5550
}
5651

libafl_extras/exit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "qemu/osdep.h"
44
#include "exec/cpu-defs.h"
55

6+
#define EXCP_LIBAFL_EXIT 0xf4775747
7+
68
enum libafl_exit_reason_kind {
79
BREAKPOINT = 0,
810
SYNC_BACKDOOR = 1
@@ -18,7 +20,6 @@ struct libafl_exit_reason {
1820
enum libafl_exit_reason_kind kind;
1921
CPUState* cpu; // CPU that triggered an exit.
2022
vaddr next_pc; // The PC that should be stored in the CPU when re-entering.
21-
int exit_asap; // TODO: add a field to CPU
2223
union {
2324
struct libafl_exit_reason_breakpoint breakpoint; // kind == BREAKPOINT
2425
struct libafl_exit_reason_sync_backdoor backdoor; // kind == SYNC_BACKDOOR

linux-user/aarch64/cpu_loop.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,17 @@ void cpu_loop(CPUARMState *env)
8282
int trapnr, ec, fsc, si_code, si_signo;
8383
abi_long ret;
8484

85+
//// --- Begin LibAFL code ---
86+
87+
libafl_exit_signal_vm_start();
88+
89+
//// --- End LibAFL code ---
90+
8591
for (;;) {
8692

8793
//// --- Begin LibAFL code ---
8894

89-
if (libafl_qemu_break_asap) return;
95+
if (libafl_exit_asap()) return;
9096

9197
//// --- End LibAFL code ---
9298

@@ -99,8 +105,6 @@ void cpu_loop(CPUARMState *env)
99105

100106
//// --- Begin LibAFL code ---
101107

102-
#define EXCP_LIBAFL_EXIT 0xf4775747
103-
104108
case EXCP_LIBAFL_EXIT:
105109
return;
106110

linux-user/arm/cpu_loop.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,17 @@ void cpu_loop(CPUARMState *env)
323323
unsigned int n, insn;
324324
abi_ulong ret;
325325

326+
//// --- Begin LibAFL code ---
327+
328+
libafl_exit_signal_vm_start();
329+
330+
//// --- End LibAFL code ---
331+
326332
for(;;) {
327333

328334
//// --- Begin LibAFL code ---
329335

330-
if (libafl_qemu_break_asap) return;
336+
if (libafl_exit_asap()) return;
331337

332338
//// --- End LibAFL code ---
333339

@@ -340,8 +346,6 @@ void cpu_loop(CPUARMState *env)
340346

341347
//// --- Begin LibAFL code ---
342348

343-
#define EXCP_LIBAFL_EXIT 0xf4775747
344-
345349
case EXCP_LIBAFL_EXIT:
346350
return;
347351

linux-user/cpu_loop-common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
//// --- Begin LibAFL code ---
2727

28-
extern __thread int libafl_qemu_break_asap;
28+
#include "libafl_extras/exit.h"
2929

3030
//// --- End LibAFL code ---
3131

linux-user/hexagon/cpu_loop.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@ void cpu_loop(CPUHexagonState *env)
3232
target_ulong syscallnum;
3333
target_ulong ret;
3434

35+
//// --- Begin LibAFL code ---
36+
37+
libafl_exit_signal_vm_start();
38+
39+
//// --- End LibAFL code ---
40+
3541
for (;;) {
3642

3743
//// --- Begin LibAFL code ---
3844

39-
if (libafl_qemu_break_asap) return;
45+
if (libafl_exit_asap()) return;
4046

4147
//// --- End LibAFL code ---
4248

@@ -49,8 +55,6 @@ void cpu_loop(CPUHexagonState *env)
4955

5056
//// --- Begin LibAFL code ---
5157

52-
#define EXCP_LIBAFL_EXIT 0xf4775747
53-
5458
case EXCP_LIBAFL_EXIT:
5559
return;
5660

linux-user/i386/cpu_loop.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ void cpu_loop(CPUX86State *env)
209209
int trapnr;
210210
abi_ulong ret;
211211

212+
//// --- Begin LibAFL code ---
213+
214+
libafl_exit_signal_vm_start();
215+
216+
//// --- End LibAFL code ---
217+
212218
for(;;) {
213219

214220
//// --- Begin LibAFL code ---
@@ -226,8 +232,6 @@ void cpu_loop(CPUX86State *env)
226232

227233
//// --- Begin LibAFL code ---
228234

229-
#define EXCP_LIBAFL_EXIT 0xf4775747
230-
231235
case EXCP_LIBAFL_EXIT:
232236
return;
233237

0 commit comments

Comments
 (0)