Message ID | 20220206134051.721574-3-hengqi.chen@gmail.com (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | BPF |
Headers | show |
Series | libbpf: Add syscall-specific variant of BPF_KPROBE | expand |
On Sun, Feb 6, 2022 at 5:41 AM Hengqi Chen <hengqi.chen@gmail.com> wrote: > > Add tests for the newly added BPF_KPROBE_SYSCALL macro. > > Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com> > --- > .../selftests/bpf/prog_tests/kprobe_syscall.c | 37 +++++++++++++++++++ > .../selftests/bpf/progs/test_kprobe_syscall.c | 34 +++++++++++++++++ > 2 files changed, 71 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c > create mode 100644 tools/testing/selftests/bpf/progs/test_kprobe_syscall.c > > diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c b/tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c > new file mode 100644 > index 000000000000..0ac89c987024 > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c > @@ -0,0 +1,37 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (c) 2022 Hengqi Chen */ > + > +#include <test_progs.h> > +#include <sys/prctl.h> > +#include "test_kprobe_syscall.skel.h" > + > +void test_kprobe_syscall(void) > +{ > + struct test_kprobe_syscall *skel; > + int err; > + > + skel = test_kprobe_syscall__open(); > + if (!ASSERT_OK_PTR(skel, "test_kprobe_syscall__open")) > + return; > + > + skel->rodata->my_pid = getpid(); > + > + err = test_kprobe_syscall__load(skel); > + if (!ASSERT_OK(err, "test_kprobe_syscall__load")) > + goto cleanup; > + > + err = test_kprobe_syscall__attach(skel); > + if (!ASSERT_OK(err, "test_kprobe_syscall__attach")) > + goto cleanup; > + > + prctl(1, 2, 3, 4, 5); > + > + ASSERT_EQ(skel->bss->option, 1, "BPF_KPROBE_SYSCALL failed"); > + ASSERT_EQ(skel->bss->arg2, 2, "BPF_KPROBE_SYSCALL failed"); > + ASSERT_EQ(skel->bss->arg3, 3, "BPF_KPROBE_SYSCALL failed"); > + ASSERT_EQ(skel->bss->arg4, 4, "BPF_KPROBE_SYSCALL failed"); > + ASSERT_EQ(skel->bss->arg5, 5, "BPF_KPROBE_SYSCALL failed"); > + > +cleanup: > + test_kprobe_syscall__destroy(skel); > +} > diff --git a/tools/testing/selftests/bpf/progs/test_kprobe_syscall.c b/tools/testing/selftests/bpf/progs/test_kprobe_syscall.c > new file mode 100644 > index 000000000000..abd59c3d5b59 > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/test_kprobe_syscall.c > @@ -0,0 +1,34 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (c) 2022 Hengqi Chen */ > + > +#include "vmlinux.h" > +#include <bpf/bpf_helpers.h> > +#include <bpf/bpf_tracing.h> > +#include <bpf/bpf_core_read.h> > +#include "bpf_misc.h" > + > +const volatile pid_t my_pid = 0; > +int option = 0; > +unsigned long arg2 = 0; > +unsigned long arg3 = 0; > +unsigned long arg4 = 0; > +unsigned long arg5 = 0; > + > +SEC("kprobe/" SYS_PREFIX "sys_prctl") > +int BPF_KPROBE_SYSCALL(prctl_enter, int opt, unsigned long a2, > + unsigned long a3, unsigned long a4, unsigned long a5) > +{ > + pid_t pid = bpf_get_current_pid_tgid() >> 32; > + > + if (pid != my_pid) > + return 0; > + > + option = opt; > + arg2 = a2; > + arg3 = a3; > + arg4 = a4; > + arg5 = a5; > + return 0; > +} > + Let's add all this into progs/bpf_syscall_macro.c and prog_tests/test_bpf_syscall_macro.c, instead of adding a new selftest. They are closely related anyways. > +char _license[] SEC("license") = "GPL"; > -- > 2.30.2
diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c b/tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c new file mode 100644 index 000000000000..0ac89c987024 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Hengqi Chen */ + +#include <test_progs.h> +#include <sys/prctl.h> +#include "test_kprobe_syscall.skel.h" + +void test_kprobe_syscall(void) +{ + struct test_kprobe_syscall *skel; + int err; + + skel = test_kprobe_syscall__open(); + if (!ASSERT_OK_PTR(skel, "test_kprobe_syscall__open")) + return; + + skel->rodata->my_pid = getpid(); + + err = test_kprobe_syscall__load(skel); + if (!ASSERT_OK(err, "test_kprobe_syscall__load")) + goto cleanup; + + err = test_kprobe_syscall__attach(skel); + if (!ASSERT_OK(err, "test_kprobe_syscall__attach")) + goto cleanup; + + prctl(1, 2, 3, 4, 5); + + ASSERT_EQ(skel->bss->option, 1, "BPF_KPROBE_SYSCALL failed"); + ASSERT_EQ(skel->bss->arg2, 2, "BPF_KPROBE_SYSCALL failed"); + ASSERT_EQ(skel->bss->arg3, 3, "BPF_KPROBE_SYSCALL failed"); + ASSERT_EQ(skel->bss->arg4, 4, "BPF_KPROBE_SYSCALL failed"); + ASSERT_EQ(skel->bss->arg5, 5, "BPF_KPROBE_SYSCALL failed"); + +cleanup: + test_kprobe_syscall__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_kprobe_syscall.c b/tools/testing/selftests/bpf/progs/test_kprobe_syscall.c new file mode 100644 index 000000000000..abd59c3d5b59 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_kprobe_syscall.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Hengqi Chen */ + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_core_read.h> +#include "bpf_misc.h" + +const volatile pid_t my_pid = 0; +int option = 0; +unsigned long arg2 = 0; +unsigned long arg3 = 0; +unsigned long arg4 = 0; +unsigned long arg5 = 0; + +SEC("kprobe/" SYS_PREFIX "sys_prctl") +int BPF_KPROBE_SYSCALL(prctl_enter, int opt, unsigned long a2, + unsigned long a3, unsigned long a4, unsigned long a5) +{ + pid_t pid = bpf_get_current_pid_tgid() >> 32; + + if (pid != my_pid) + return 0; + + option = opt; + arg2 = a2; + arg3 = a3; + arg4 = a4; + arg5 = a5; + return 0; +} + +char _license[] SEC("license") = "GPL";
Add tests for the newly added BPF_KPROBE_SYSCALL macro. Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com> --- .../selftests/bpf/prog_tests/kprobe_syscall.c | 37 +++++++++++++++++++ .../selftests/bpf/progs/test_kprobe_syscall.c | 34 +++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/kprobe_syscall.c create mode 100644 tools/testing/selftests/bpf/progs/test_kprobe_syscall.c -- 2.30.2