|
3 | 3 | #include "sysemu/runstate.h" |
4 | 4 | #include "cpu.h" |
5 | 5 |
|
6 | | -// TODO: merge with definition in tcg-runtime.c |
7 | | -#define EXCP_LIBAFL_EXIT 0xf4775747 |
8 | | - |
9 | 6 | #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 |
14 | 8 | #else |
15 | | -static struct libafl_exit_reason last_exit_reason; |
| 9 | +#define THREAD_MODIFIER |
16 | 10 | #endif |
17 | 11 |
|
| 12 | +static THREAD_MODIFIER struct libafl_exit_reason last_exit_reason; |
| 13 | +static THREAD_MODIFIER bool expected_exit = false; |
| 14 | + |
18 | 15 | #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) |
20 | 17 | #else |
21 | | -#define THUMB_MASK(value) value |
| 18 | +#define THUMB_MASK(cpu, value) value |
22 | 19 | #endif |
23 | 20 |
|
24 | | -static bool expected_exit = false; |
25 | | - |
| 21 | +// called before exiting the cpu exec with the custom exception |
26 | 22 | void libafl_sync_exit_cpu(void) |
27 | 23 | { |
28 | 24 | if (last_exit_reason.next_pc) { |
29 | 25 | 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)); |
31 | 27 | } |
32 | 28 | last_exit_reason.next_pc = 0; |
33 | 29 | } |
34 | 30 |
|
35 | | -bool libafl_exit_asap(void){ |
36 | | - return last_exit_reason.exit_asap; |
| 31 | +bool libafl_exit_asap(void) { |
| 32 | + return expected_exit; |
37 | 33 | } |
38 | 34 |
|
39 | | -static void prepare_qemu_exit(CPUState* cpu, ulong next_pc) |
| 35 | +static void prepare_qemu_exit(CPUState* cpu, target_ulong next_pc) |
40 | 36 | { |
41 | 37 | expected_exit = true; |
42 | 38 | last_exit_reason.cpu = cpu; |
43 | 39 | last_exit_reason.next_pc = next_pc; |
44 | 40 |
|
45 | 41 | #ifndef CONFIG_USER_ONLY |
46 | 42 | qemu_system_debug_request(); |
47 | | - cpu->stopped = true; |
| 43 | + cpu->stopped = true; // TODO check if still needed |
48 | 44 | #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 |
49 | 46 | if (cpu->running) { |
50 | 47 | cpu->exception_index = EXCP_LIBAFL_EXIT; |
51 | 48 | cpu_loop_exit(cpu); |
52 | | - } else { |
53 | | - last_exit_reason.exit_asap = 1; |
54 | 49 | } |
55 | 50 | } |
56 | 51 |
|
|
0 commit comments