1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2022 Sony Group Corporation */
3 #define _GNU_SOURCE
4 #include <fcntl.h>
5 #include <sys/prctl.h>
6 #include <test_progs.h>
7 #include "bpf_syscall_macro.skel.h"
8
test_bpf_syscall_macro(void)9 void test_bpf_syscall_macro(void)
10 {
11 struct bpf_syscall_macro *skel = NULL;
12 int err;
13 int exp_arg1 = 1001;
14 unsigned long exp_arg2 = 12;
15 unsigned long exp_arg3 = 13;
16 unsigned long exp_arg4 = 14;
17 unsigned long exp_arg5 = 15;
18 loff_t off_in, off_out;
19 ssize_t r;
20
21 /* check whether it can open program */
22 skel = bpf_syscall_macro__open();
23 if (!ASSERT_OK_PTR(skel, "bpf_syscall_macro__open"))
24 return;
25
26 skel->rodata->filter_pid = getpid();
27
28 /* check whether it can load program */
29 err = bpf_syscall_macro__load(skel);
30 if (!ASSERT_OK(err, "bpf_syscall_macro__load"))
31 goto cleanup;
32
33 /* check whether it can attach kprobe */
34 err = bpf_syscall_macro__attach(skel);
35 if (!ASSERT_OK(err, "bpf_syscall_macro__attach"))
36 goto cleanup;
37
38 /* check whether args of syscall are copied correctly */
39 prctl(exp_arg1, exp_arg2, exp_arg3, exp_arg4, exp_arg5);
40
41 #if defined(__aarch64__) || defined(__s390__)
42 ASSERT_NEQ(skel->bss->arg1, exp_arg1, "syscall_arg1");
43 #else
44 ASSERT_EQ(skel->bss->arg1, exp_arg1, "syscall_arg1");
45 #endif
46 ASSERT_EQ(skel->bss->arg2, exp_arg2, "syscall_arg2");
47 ASSERT_EQ(skel->bss->arg3, exp_arg3, "syscall_arg3");
48 /* it cannot copy arg4 when uses PT_REGS_PARM4 on x86_64 */
49 #ifdef __x86_64__
50 ASSERT_NEQ(skel->bss->arg4_cx, exp_arg4, "syscall_arg4_from_cx");
51 #else
52 ASSERT_EQ(skel->bss->arg4_cx, exp_arg4, "syscall_arg4_from_cx");
53 #endif
54 ASSERT_EQ(skel->bss->arg4, exp_arg4, "syscall_arg4");
55 ASSERT_EQ(skel->bss->arg5, exp_arg5, "syscall_arg5");
56
57 /* check whether args of syscall are copied correctly for CORE variants */
58 ASSERT_EQ(skel->bss->arg1_core, exp_arg1, "syscall_arg1_core_variant");
59 ASSERT_EQ(skel->bss->arg2_core, exp_arg2, "syscall_arg2_core_variant");
60 ASSERT_EQ(skel->bss->arg3_core, exp_arg3, "syscall_arg3_core_variant");
61 /* it cannot copy arg4 when uses PT_REGS_PARM4_CORE on x86_64 */
62 #ifdef __x86_64__
63 ASSERT_NEQ(skel->bss->arg4_core_cx, exp_arg4, "syscall_arg4_from_cx_core_variant");
64 #else
65 ASSERT_EQ(skel->bss->arg4_core_cx, exp_arg4, "syscall_arg4_from_cx_core_variant");
66 #endif
67 ASSERT_EQ(skel->bss->arg4_core, exp_arg4, "syscall_arg4_core_variant");
68 ASSERT_EQ(skel->bss->arg5_core, exp_arg5, "syscall_arg5_core_variant");
69
70 ASSERT_EQ(skel->bss->option_syscall, exp_arg1, "BPF_KPROBE_SYSCALL_option");
71 ASSERT_EQ(skel->bss->arg2_syscall, exp_arg2, "BPF_KPROBE_SYSCALL_arg2");
72 ASSERT_EQ(skel->bss->arg3_syscall, exp_arg3, "BPF_KPROBE_SYSCALL_arg3");
73 ASSERT_EQ(skel->bss->arg4_syscall, exp_arg4, "BPF_KPROBE_SYSCALL_arg4");
74 ASSERT_EQ(skel->bss->arg5_syscall, exp_arg5, "BPF_KPROBE_SYSCALL_arg5");
75
76 r = splice(-42, &off_in, 42, &off_out, 0x12340000, SPLICE_F_NONBLOCK);
77 err = -errno;
78 ASSERT_EQ(r, -1, "splice_res");
79 ASSERT_EQ(err, -EBADF, "splice_err");
80
81 ASSERT_EQ(skel->bss->splice_fd_in, -42, "splice_arg1");
82 ASSERT_EQ(skel->bss->splice_off_in, (__u64)&off_in, "splice_arg2");
83 ASSERT_EQ(skel->bss->splice_fd_out, 42, "splice_arg3");
84 ASSERT_EQ(skel->bss->splice_off_out, (__u64)&off_out, "splice_arg4");
85 ASSERT_EQ(skel->bss->splice_len, 0x12340000, "splice_arg5");
86 ASSERT_EQ(skel->bss->splice_flags, SPLICE_F_NONBLOCK, "splice_arg6");
87
88 cleanup:
89 bpf_syscall_macro__destroy(skel);
90 }
91