Skip to content

Commit 97bef50

Browse files
authored
Update syshook ret definition (#109)
1 parent 689bc5f commit 97bef50

File tree

4 files changed

+41
-42
lines changed

4 files changed

+41
-42
lines changed

Diff for: include/libafl/exit.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct libafl_exit_reason {
5757
struct libafl_exit_reason_breakpoint breakpoint; // kind == BREAKPOINT
5858
struct libafl_exit_reason_custom_insn
5959
custom_insn; // kind == CUSTOM_INSN
60-
struct libafl_exit_reason_crash crash; // kind == CRASH
60+
struct libafl_exit_reason_crash crash; // kind == CRASH
6161
struct libafl_exit_reason_timeout timeout; // kind == TIMEOUT
6262
} data;
6363
};

Diff for: include/libafl/hooks/syscall.h

+29-27
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,34 @@
1010
#include "libafl/exit.h"
1111
#include "libafl/hook.h"
1212

13-
struct syshook_ret {
14-
target_ulong retval;
15-
bool skip_syscall;
13+
enum libafl_syshook_ret_tag {
14+
LIBAFL_SYSHOOK_RUN,
15+
LIBAFL_SYSHOOK_SKIP,
1616
};
1717

18+
// Representation of a pre-syscall hook result.
19+
// It is associated with the LibAFL enum `SyscallHookResult`.
20+
// Any change made here should be also propagated to the Rust enum.
21+
struct libafl_syshook_ret {
22+
enum libafl_syshook_ret_tag tag;
23+
union {
24+
target_ulong syshook_skip_retval;
25+
};
26+
};
27+
28+
typedef struct libafl_syshook_ret (*libafl_pre_syscall_cb)(
29+
uint64_t data, int sys_num, target_ulong arg0, target_ulong arg1,
30+
target_ulong arg2, target_ulong arg3, target_ulong arg4, target_ulong arg5,
31+
target_ulong arg6, target_ulong arg7);
32+
33+
typedef target_ulong (*libafl_post_syscall_cb)(
34+
uint64_t data, target_ulong ret, int sys_num, target_ulong arg0,
35+
target_ulong arg1, target_ulong arg2, target_ulong arg3, target_ulong arg4,
36+
target_ulong arg5, target_ulong arg6, target_ulong arg7);
37+
1838
struct libafl_pre_syscall_hook {
1939
// functions
20-
struct syshook_ret (*callback)(uint64_t data, int sys_num,
21-
target_ulong arg0, target_ulong arg1,
22-
target_ulong arg2, target_ulong arg3,
23-
target_ulong arg4, target_ulong arg5,
24-
target_ulong arg6, target_ulong arg7);
40+
libafl_pre_syscall_cb callback;
2541

2642
// data
2743
uint64_t data;
@@ -33,11 +49,7 @@ struct libafl_pre_syscall_hook {
3349

3450
struct libafl_post_syscall_hook {
3551
// functions
36-
target_ulong (*callback)(uint64_t data, target_ulong ret, int sys_num,
37-
target_ulong arg0, target_ulong arg1,
38-
target_ulong arg2, target_ulong arg3,
39-
target_ulong arg4, target_ulong arg5,
40-
target_ulong arg6, target_ulong arg7);
52+
libafl_post_syscall_cb callback;
4153

4254
// data
4355
uint64_t data;
@@ -47,20 +59,10 @@ struct libafl_post_syscall_hook {
4759
struct libafl_post_syscall_hook* next;
4860
};
4961

50-
size_t libafl_add_pre_syscall_hook(
51-
struct syshook_ret (*callback)(uint64_t data, int sys_num,
52-
target_ulong arg0, target_ulong arg1,
53-
target_ulong arg2, target_ulong arg3,
54-
target_ulong arg4, target_ulong arg5,
55-
target_ulong arg6, target_ulong arg7),
56-
uint64_t data);
57-
size_t libafl_add_post_syscall_hook(
58-
target_ulong (*callback)(uint64_t data, target_ulong ret, int sys_num,
59-
target_ulong arg0, target_ulong arg1,
60-
target_ulong arg2, target_ulong arg3,
61-
target_ulong arg4, target_ulong arg5,
62-
target_ulong arg6, target_ulong arg7),
63-
uint64_t data);
62+
size_t libafl_add_pre_syscall_hook(libafl_pre_syscall_cb callback,
63+
uint64_t data);
64+
size_t libafl_add_post_syscall_hook(libafl_post_syscall_cb callback,
65+
uint64_t data);
6466

6567
int libafl_qemu_remove_pre_syscall_hook(size_t num);
6668
int libafl_qemu_remove_post_syscall_hook(size_t num);

Diff for: libafl/hooks/syscall.c

+8-10
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ size_t libafl_post_syscall_hooks_num = 0;
99
GEN_REMOVE_HOOK1(pre_syscall)
1010
GEN_REMOVE_HOOK1(post_syscall)
1111

12-
size_t libafl_add_pre_syscall_hook(
13-
struct syshook_ret (*callback)(uint64_t data, int sys_num,
14-
target_ulong arg0, target_ulong arg1,
15-
target_ulong arg2, target_ulong arg3,
16-
target_ulong arg4, target_ulong arg5,
17-
target_ulong arg6, target_ulong arg7),
18-
uint64_t data)
12+
size_t libafl_add_pre_syscall_hook(libafl_pre_syscall_cb callback,
13+
uint64_t data)
1914
{
2015
struct libafl_pre_syscall_hook* hook =
2116
calloc(sizeof(struct libafl_pre_syscall_hook), 1);
@@ -57,14 +52,16 @@ bool libafl_hook_syscall_pre_run(CPUArchState* env, int num, abi_long arg1,
5752
struct libafl_pre_syscall_hook* h = libafl_pre_syscall_hooks;
5853
while (h) {
5954
// no null check
60-
struct syshook_ret hook_ret = h->callback(
55+
struct libafl_syshook_ret hook_ret = h->callback(
6156
h->data, num, (target_ulong)arg1, (target_ulong)arg2,
6257
(target_ulong)arg3, (target_ulong)arg4, (target_ulong)arg5,
6358
(target_ulong)arg6, (target_ulong)arg7, (target_ulong)arg8);
64-
if (hook_ret.skip_syscall) {
59+
60+
if (hook_ret.tag == LIBAFL_SYSHOOK_SKIP) {
6561
skip_syscall = true;
66-
*ret = (abi_ulong)hook_ret.retval;
62+
*ret = (abi_ulong)hook_ret.syshook_skip_retval;
6763
}
64+
6865
h = h->next;
6966
}
7067

@@ -77,6 +74,7 @@ void libafl_hook_syscall_post_run(int num, abi_long arg1, abi_long arg2,
7774
abi_long* ret)
7875
{
7976
struct libafl_post_syscall_hook* p = libafl_post_syscall_hooks;
77+
8078
while (p) {
8179
// no null check
8280
*ret = (abi_ulong)p->callback(p->data, (target_ulong)*ret, num,

Diff for: libafl/user.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ uint64_t libafl_set_brk(uint64_t new_brk)
5858
return old_brk;
5959
}
6060

61-
void libafl_set_return_on_crash(bool return_on_crash) {
61+
void libafl_set_return_on_crash(bool return_on_crash)
62+
{
6263
libafl_return_on_crash = return_on_crash;
6364
}
6465

65-
bool libafl_get_return_on_crash(void) {
66-
return libafl_return_on_crash;
67-
}
66+
bool libafl_get_return_on_crash(void) { return libafl_return_on_crash; }
6867

6968
#ifdef AS_LIB
7069
void libafl_qemu_init(int argc, char** argv)

0 commit comments

Comments
 (0)