Skip to content

Commit 7f468eb

Browse files
authored
Second round of cleanups (#83)
* get rid of as many extern / function definition in QEMU codebase. * mostly moved cpu / gdb related code. * move qemu snapshot code in dedicated files.
1 parent 86d38fb commit 7f468eb

File tree

26 files changed

+449
-401
lines changed

26 files changed

+449
-401
lines changed

accel/tcg/tcg-runtime.c

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -39,99 +39,6 @@
3939

4040
#include "libafl/exit.h"
4141

42-
#ifndef CONFIG_USER_ONLY
43-
44-
#include "sysemu/runstate.h"
45-
#include "migration/snapshot.h"
46-
#include "qapi/error.h"
47-
#include "qemu/error-report.h"
48-
#include "qemu/main-loop.h"
49-
#include "hw/core/cpu.h"
50-
#include "sysemu/hw_accel.h"
51-
#include <stdlib.h>
52-
#include <string.h>
53-
54-
void libafl_save_qemu_snapshot(char *name, bool sync);
55-
void libafl_load_qemu_snapshot(char *name, bool sync);
56-
57-
static void save_snapshot_cb(void* opaque)
58-
{
59-
char* name = (char*)opaque;
60-
Error *err = NULL;
61-
if(!save_snapshot(name, true, NULL, false, NULL, &err)) {
62-
error_report_err(err);
63-
error_report("Could not save snapshot");
64-
}
65-
free(opaque);
66-
}
67-
68-
void libafl_save_qemu_snapshot(char *name, bool sync)
69-
{
70-
// use snapshots synchronously, use if main loop is not running
71-
if (sync) {
72-
//TODO: eliminate this code duplication
73-
//by passing a heap-allocated buffer from rust to c,
74-
//which c needs to free
75-
Error *err = NULL;
76-
if(!save_snapshot(name, true, NULL, false, NULL, &err)) {
77-
error_report_err(err);
78-
error_report("Could not save snapshot");
79-
}
80-
return;
81-
}
82-
char* name_buffer = malloc(strlen(name)+1);
83-
strcpy(name_buffer, name);
84-
aio_bh_schedule_oneshot_full(qemu_get_aio_context(), save_snapshot_cb, (void*)name_buffer, "save_snapshot");
85-
}
86-
87-
static void load_snapshot_cb(void* opaque)
88-
{
89-
char* name = (char*)opaque;
90-
Error *err = NULL;
91-
92-
int saved_vm_running = runstate_is_running();
93-
vm_stop(RUN_STATE_RESTORE_VM);
94-
95-
bool loaded = load_snapshot(name, NULL, false, NULL, &err);
96-
97-
if(!loaded) {
98-
error_report_err(err);
99-
error_report("Could not load snapshot");
100-
}
101-
if (loaded && saved_vm_running) {
102-
vm_start();
103-
}
104-
free(opaque);
105-
}
106-
107-
void libafl_load_qemu_snapshot(char *name, bool sync)
108-
{
109-
// use snapshots synchronously, use if main loop is not running
110-
if (sync) {
111-
//TODO: see libafl_save_qemu_snapshot
112-
Error *err = NULL;
113-
114-
int saved_vm_running = runstate_is_running();
115-
vm_stop(RUN_STATE_RESTORE_VM);
116-
117-
bool loaded = load_snapshot(name, NULL, false, NULL, &err);
118-
119-
if(!loaded) {
120-
error_report_err(err);
121-
error_report("Could not load snapshot");
122-
}
123-
if (loaded && saved_vm_running) {
124-
vm_start();
125-
}
126-
return;
127-
}
128-
char* name_buffer = malloc(strlen(name)+1);
129-
strcpy(name_buffer, name);
130-
aio_bh_schedule_oneshot_full(qemu_get_aio_context(), load_snapshot_cb, (void*)name_buffer, "load_snapshot");
131-
}
132-
133-
#endif
134-
13542
void HELPER(libafl_qemu_handle_breakpoint)(CPUArchState *env, uint64_t pc)
13643
{
13744
CPUState* cpu = env_cpu(env);

cpu-target.c

Lines changed: 4 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -47,139 +47,10 @@
4747

4848
//// --- Begin LibAFL code ---
4949

50-
#include "exec/gdbstub.h"
51-
52-
#include "libafl/exit.h"
53-
#include "libafl/hook.h"
54-
55-
int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg);
56-
57-
static __thread GByteArray *libafl_qemu_mem_buf = NULL;
58-
59-
target_ulong libafl_page_from_addr(target_ulong addr);
60-
61-
CPUState* libafl_qemu_get_cpu(int cpu_index);
62-
int libafl_qemu_num_cpus(void);
63-
CPUState* libafl_qemu_current_cpu(void);
64-
int libafl_qemu_cpu_index(CPUState*);
65-
66-
int libafl_qemu_write_reg(CPUState* cpu, int reg, uint8_t* val);
67-
int libafl_qemu_read_reg(CPUState* cpu, int reg, uint8_t* val);
68-
int libafl_qemu_num_regs(CPUState* cpu);
69-
7050
#ifndef CONFIG_USER_ONLY
71-
hwaddr libafl_qemu_current_paging_id(CPUState* cpu);
51+
#include "libafl/syx-snapshot/device-save.h"
7252
#endif
7353

74-
void libafl_flush_jit(void);
75-
76-
extern int libafl_restoring_devices;
77-
78-
/*
79-
void* libafl_qemu_g2h(CPUState *cpu, target_ulong x);
80-
target_ulong libafl_qemu_h2g(CPUState *cpu, void* x);
81-
82-
void* libafl_qemu_g2h(CPUState *cpu, target_ulong x)
83-
{
84-
return g2h(cpu, x);
85-
}
86-
87-
target_ulong libafl_qemu_h2g(CPUState *cpu, void* x)
88-
{
89-
return h2g(cpu, x);
90-
}
91-
*/
92-
93-
target_ulong libafl_page_from_addr(target_ulong addr) {
94-
return addr & TARGET_PAGE_MASK;
95-
}
96-
97-
CPUState* libafl_qemu_get_cpu(int cpu_index)
98-
{
99-
CPUState *cpu;
100-
CPU_FOREACH(cpu) {
101-
if (cpu->cpu_index == cpu_index)
102-
return cpu;
103-
}
104-
return NULL;
105-
}
106-
107-
int libafl_qemu_num_cpus(void)
108-
{
109-
CPUState *cpu;
110-
int num = 0;
111-
CPU_FOREACH(cpu) {
112-
num++;
113-
}
114-
return num;
115-
}
116-
117-
CPUState* libafl_qemu_current_cpu(void)
118-
{
119-
#ifndef CONFIG_USER_ONLY
120-
if (current_cpu == NULL) {
121-
return libafl_last_exit_cpu();
122-
}
123-
#endif
124-
return current_cpu;
125-
}
126-
127-
int libafl_qemu_cpu_index(CPUState* cpu)
128-
{
129-
if (cpu) return cpu->cpu_index;
130-
return -1;
131-
}
132-
133-
int libafl_qemu_write_reg(CPUState* cpu, int reg, uint8_t* val)
134-
{
135-
return gdb_write_register(cpu, val, reg);
136-
}
137-
138-
int libafl_qemu_read_reg(CPUState* cpu, int reg, uint8_t* val)
139-
{
140-
int len;
141-
142-
if (libafl_qemu_mem_buf == NULL) {
143-
libafl_qemu_mem_buf = g_byte_array_sized_new(64);
144-
}
145-
146-
g_byte_array_set_size(libafl_qemu_mem_buf, 0);
147-
148-
len = gdb_read_register(cpu, libafl_qemu_mem_buf, reg);
149-
150-
if (len > 0) {
151-
memcpy(val, libafl_qemu_mem_buf->data, len);
152-
}
153-
154-
return len;
155-
}
156-
157-
int libafl_qemu_num_regs(CPUState* cpu)
158-
{
159-
CPUClass *cc = CPU_GET_CLASS(cpu);
160-
return cc->gdb_num_core_regs;
161-
}
162-
163-
#ifndef CONFIG_USER_ONLY
164-
hwaddr libafl_qemu_current_paging_id(CPUState* cpu)
165-
{
166-
CPUClass* cc = CPU_GET_CLASS(cpu);
167-
if (cc->sysemu_ops && cc->sysemu_ops->get_paging_id) {
168-
return cc->sysemu_ops->get_paging_id(cpu);
169-
} else {
170-
return 0;
171-
}
172-
}
173-
#endif
174-
175-
void libafl_flush_jit(void)
176-
{
177-
CPUState *cpu;
178-
CPU_FOREACH(cpu) {
179-
tb_flush(cpu);
180-
}
181-
}
182-
18354
//// --- End LibAFL code ---
18455

18556
#ifndef CONFIG_USER_ONLY
@@ -203,7 +74,9 @@ static int cpu_common_post_load(void *opaque, int version_id)
20374

20475
// flushing the TBs every restore makes it really slow
20576
// TODO handle writes to X code with specific calls to tb_invalidate_phys_addr
206-
if (!libafl_restoring_devices) tb_flush(cpu);
77+
if (!libafl_devices_is_restoring()) {
78+
tb_flush(cpu);
79+
}
20780

20881
//// --- End LibAFL code ---
20982

@@ -462,23 +335,6 @@ void list_cpus(void)
462335
cpu_list();
463336
}
464337

465-
//// --- Begin LibAFL code ---
466-
#if defined(CONFIG_USER_ONLY)
467-
void libafl_breakpoint_invalidate(CPUState *cpu, target_ulong pc)
468-
{
469-
mmap_lock();
470-
tb_invalidate_phys_range(pc, pc + 1);
471-
mmap_unlock();
472-
}
473-
#else
474-
void libafl_breakpoint_invalidate(CPUState *cpu, target_ulong pc)
475-
{
476-
// TODO invalidate only the virtual pages related to the TB
477-
tb_flush(cpu);
478-
}
479-
#endif
480-
//// --- End LibAFL code ---
481-
482338
/* enable or disable single step mode. EXCP_DEBUG is returned by the
483339
CPU loop after each instruction */
484340
void cpu_single_step(CPUState *cpu, int enabled)

gdbstub/gdbstub.c

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,30 +1648,6 @@ static void handle_query_thread_extra(GArray *params, void *user_ctx)
16481648
gdb_put_strbuf();
16491649
}
16501650

1651-
//// --- Begin LibAFL code ---
1652-
1653-
struct libafl_custom_gdb_cmd* libafl_qemu_gdb_cmds;
1654-
1655-
void libafl_qemu_add_gdb_cmd(int (*callback)(void*, uint8_t*, size_t), void* data);
1656-
void libafl_qemu_add_gdb_cmd(int (*callback)(void*, uint8_t*, size_t), void* data)
1657-
{
1658-
struct libafl_custom_gdb_cmd* c = malloc(sizeof(struct libafl_custom_gdb_cmd));
1659-
c->callback = callback;
1660-
c->data = data;
1661-
c->next = libafl_qemu_gdb_cmds;
1662-
libafl_qemu_gdb_cmds = c;
1663-
}
1664-
1665-
void libafl_qemu_gdb_reply(const char* buf, size_t len);
1666-
void libafl_qemu_gdb_reply(const char* buf, size_t len)
1667-
{
1668-
g_autoptr(GString) hex_buf = g_string_new("O");
1669-
gdb_memtohex(hex_buf, (const uint8_t *) buf, len);
1670-
gdb_put_packet(hex_buf->str);
1671-
}
1672-
1673-
//// --- End LibAFL code ---
1674-
16751651
static void handle_query_supported(GArray *params, void *user_ctx)
16761652
{
16771653
CPUClass *cc;

gdbstub/internals.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,4 @@ void gdb_breakpoint_remove_all(CPUState *cs);
240240
int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
241241
uint8_t *buf, int len, bool is_write);
242242

243-
//// --- Begin LibAFL code ---
244-
245-
struct libafl_custom_gdb_cmd {
246-
int (*callback)(void*, uint8_t*, size_t);
247-
void* data;
248-
struct libafl_custom_gdb_cmd* next;
249-
};
250-
251-
extern struct libafl_custom_gdb_cmd* libafl_qemu_gdb_cmds;
252-
253-
//// --- End LibAFL code ---
254-
255243
#endif /* GDBSTUB_INTERNALS_H */

gdbstub/system.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
#include "trace.h"
3131
#include "internals.h"
3232

33+
//// --- Begin LibAFL code ---
34+
#include "libafl/gdb.h"
35+
//// --- End LibAFL code ---
36+
3337
/* System emulation specific state */
3438
typedef struct {
3539
CharBackend chr;
@@ -531,14 +535,7 @@ void gdb_handle_query_rcmd(GArray *params, void *ctx)
531535

532536
//// --- Begin LibAFL code ---
533537

534-
struct libafl_custom_gdb_cmd** c = &libafl_qemu_gdb_cmds;
535-
int recognized = 0;
536-
while (*c) {
537-
recognized |= (*c)->callback((*c)->data, gdbserver_state.mem_buf->data, gdbserver_state.mem_buf->len);
538-
c = &(*c)->next;
539-
}
540-
541-
if (recognized) {
538+
if (libafl_qemu_gdb_exec()) {
542539
gdb_put_packet("OK");
543540
return;
544541
}

gdbstub/user-target.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#ifdef CONFIG_LINUX
1515
#include "linux-user/loader.h"
1616
#include "linux-user/qemu.h"
17+
18+
//// --- Begin LibAFL code ---
19+
#include "libafl/gdb.h"
20+
//// --- End LibAFL code ---
21+
1722
#endif
1823

1924
/*
@@ -305,27 +310,17 @@ void gdb_handle_query_rcmd(GArray *params, void *user_ctx)
305310
g_assert(gdbserver_state.mem_buf->len == 0);
306311
len = len / 2;
307312
gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
308-
309-
//// --- Begin LibAFL code ---
310-
311-
struct libafl_custom_gdb_cmd** c = &libafl_qemu_gdb_cmds;
312-
int recognized = 0;
313-
while (*c) {
314-
recognized |= (*c)->callback((*c)->data, gdbserver_state.mem_buf->data, gdbserver_state.mem_buf->len);
315-
c = &(*c)->next;
316-
}
317313

318-
if (recognized) {
314+
if (libafl_qemu_gdb_exec()) {
319315
gdb_put_packet("OK");
320316
} else {
321317
gdb_put_packet("");
322318
}
323319
}
320+
#endif
324321

325322
//// --- End LibAFL code ---
326323

327-
#endif
328-
329324
static const char *get_filename_param(GArray *params, int i)
330325
{
331326
const char *hex_filename = get_param(params, i)->data;

0 commit comments

Comments
 (0)