Skip to content

Commit ee43af7

Browse files
authored
Run thread hooks for KVM mode (#84)
* Run thread hooks for KVM mode * Unify qemu init function symbol for systemmode and usermode * get tid from caller instead of callee
1 parent 7f468eb commit ee43af7

File tree

10 files changed

+43
-7
lines changed

10 files changed

+43
-7
lines changed

accel/kvm/kvm-accel-ops.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
#include <linux/kvm.h>
2727
#include "kvm-cpus.h"
2828

29+
//// --- Begin LibAFL code ---
30+
31+
#include "libafl/hooks/thread.h"
32+
33+
//// --- End LibAFL code ---
34+
2935
static void *kvm_vcpu_thread_fn(void *arg)
3036
{
3137
CPUState *cpu = arg;
@@ -41,6 +47,12 @@ static void *kvm_vcpu_thread_fn(void *arg)
4147
r = kvm_init_vcpu(cpu, &error_fatal);
4248
kvm_init_cpu_signals(cpu);
4349

50+
//// --- Begin LibAFL code ---
51+
52+
libafl_hook_new_thread_run(cpu_env(cpu), cpu->thread_id);
53+
54+
//// --- End LibAFL code ---
55+
4456
/* signal CPU creation */
4557
cpu_thread_signal_created(cpu);
4658
qemu_guest_random_seed_thread_part2(cpu->random_seed);

include/libafl/hooks/thread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ size_t libafl_add_new_thread_hook(bool (*callback)(uint64_t data,
2828
uint64_t data);
2929
int libafl_qemu_remove_new_thread_hook(size_t num);
3030

31-
bool libafl_hook_new_thread_run(CPUArchState* env);
31+
bool libafl_hook_new_thread_run(CPUArchState* env, uint32_t tid);

include/libafl/system.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void libafl_qemu_init(int argc, char** argv);

include/libafl/user.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ struct image_info* libafl_get_image_info(void);
3030

3131
uint64_t libafl_get_brk(void);
3232
uint64_t libafl_set_brk(uint64_t new_brk);
33+
34+
int _libafl_qemu_user_init(int argc, char** argv, char** envp);
35+
36+
#ifdef AS_LIB
37+
void libafl_qemu_init(int argc, char** argv);
38+
#endif

libafl/hooks/thread.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ size_t libafl_add_new_thread_hook(bool (*callback)(uint64_t data,
2424
return hook->num;
2525
}
2626

27-
bool libafl_hook_new_thread_run(CPUArchState* env)
27+
bool libafl_hook_new_thread_run(CPUArchState* env, uint32_t tid)
2828
{
29+
#ifdef CONFIG_USER_ONLY
2930
libafl_set_qemu_env(env);
31+
#endif
3032

3133
if (libafl_new_thread_hooks) {
3234
bool continue_execution = true;
33-
int tid = gettid();
3435

3536
struct libafl_new_thread_hook* h = libafl_new_thread_hooks;
3637
while (h) {

libafl/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ specific_ss.add(files(
1616

1717
# General hooks
1818
'hooks/cpu_run.c',
19+
'hooks/thread.c',
1920
))
2021

2122
specific_ss.add(when : 'CONFIG_SOFTMMU', if_true : [files(
23+
'system.c',
2224
'qemu_snapshot.c',
2325
'syx-snapshot/device-save.c',
2426
'syx-snapshot/syx-snapshot.c',
@@ -29,6 +31,5 @@ specific_ss.add(when : 'CONFIG_SOFTMMU', if_true : [files(
2931
specific_ss.add(when : 'CONFIG_USER_ONLY', if_true : [files(
3032
'user.c',
3133
'hooks/syscall.c',
32-
'hooks/thread.c',
3334
)])
3435

libafl/system.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "qemu/osdep.h"
2+
#include "sysemu/sysemu.h"
3+
4+
#include "libafl/system.h"
5+
6+
void libafl_qemu_init(int argc, char** argv) { qemu_init(argc, argv); }

libafl/user.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ uint64_t libafl_set_brk(uint64_t new_brk)
3535
target_brk = (abi_ulong)new_brk;
3636
return old_brk;
3737
}
38+
39+
#ifdef AS_LIB
40+
void libafl_qemu_init(int argc, char** argv)
41+
{
42+
// main function in usermode has an env parameter but is unused in practice.
43+
_libafl_qemu_user_init(argc, argv, NULL);
44+
}
45+
#endif

linux-user/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,7 @@ static int parse_args(int argc, char **argv)
693693
struct linux_binprm bprm;
694694

695695
#ifdef AS_LIB
696-
int qemu_user_init(int argc, char **argv, char **envp);
697-
int qemu_user_init(int argc, char **argv, char **envp)
696+
int _libafl_qemu_user_init(int argc, char **argv, char **envp)
698697
#else
699698
//// --- End LibAFL code ---
700699
int main(int argc, char **argv, char **envp)

linux-user/syscall.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6556,7 +6556,7 @@ static void *clone_func(void *arg)
65566556

65576557
//// --- Begin LibAFL code ---
65586558

6559-
if (libafl_hook_new_thread_run(env)) {
6559+
if (libafl_hook_new_thread_run(env, info->tid)) {
65606560
cpu_loop(env);
65616561
}
65626562

0 commit comments

Comments
 (0)