diff mbox series

[bpf-next,2/2] selftests/bpf: Add PROG_TEST_RUN selftest for BPF_PROG_TYPE_KPROBE

Message ID a8f5faada9b96218d79beb7b7ddebe6a837a5536.1653861287.git.dxu@dxuuu.xyz (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series Add PROG_TEST_RUN support to BPF_PROG_TYPE_KPROBE | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 8 maintainers not CCed: netdev@vger.kernel.org songliubraving@fb.com linux-kselftest@vger.kernel.org yhs@fb.com john.fastabend@gmail.com kafai@fb.com shuah@kernel.org kpsingh@kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning CHECK: Alignment should match open parenthesis WARNING: Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-15
bpf/vmtest-bpf-next-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc

Commit Message

Daniel Xu May 29, 2022, 10:06 p.m. UTC
This commit adds a selftest to test that we can both PROG_TEST_RUN a
kprobe prog and set its context.

Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
---
 .../selftests/bpf/prog_tests/kprobe_ctx.c     | 57 +++++++++++++++++++
 .../testing/selftests/bpf/progs/kprobe_ctx.c  | 33 +++++++++++
 2 files changed, 90 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c
 create mode 100644 tools/testing/selftests/bpf/progs/kprobe_ctx.c

Comments

Song Liu May 31, 2022, 5:11 p.m. UTC | #1
On Sun, May 29, 2022 at 3:06 PM Daniel Xu <dxu@dxuuu.xyz> wrote:
>
> This commit adds a selftest to test that we can both PROG_TEST_RUN a
> kprobe prog and set its context.

nit: per Documentation/process/submitting-patches.rst:

Describe your changes in imperative mood, e.g. "make xyzzy do frotz"
instead of "[This patch] makes xyzzy do frotz" or "[I] changed xyzzy
to do frotz", as if you are giving orders to the codebase to change
its behaviour.

>
> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>

Other than that,

Acked-by: Song Liu <songliubraving@fb.com>


> ---
>  .../selftests/bpf/prog_tests/kprobe_ctx.c     | 57 +++++++++++++++++++
>  .../testing/selftests/bpf/progs/kprobe_ctx.c  | 33 +++++++++++
>  2 files changed, 90 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c
>  create mode 100644 tools/testing/selftests/bpf/progs/kprobe_ctx.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c b/tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c
> new file mode 100644
> index 000000000000..260966fd4506
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c
> @@ -0,0 +1,57 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <test_progs.h>
> +#include <linux/ptrace.h>
> +#include "kprobe_ctx.skel.h"
> +
> +/*
> + * x86_64 happens to be one of the architectures that exports the
> + * kernel `struct pt_regs` to userspace ABI. For the architectures
> + * that don't, users will have to extract `struct pt_regs` from vmlinux
> + * BTF in order to use BPF_PROG_TYPE_KPROBE's BPF_PROG_RUN functionality.
> + *
> + * We choose to only test x86 here to keep the test simple.
> + */
> +void test_kprobe_ctx(void)
> +{
> +#ifdef __x86_64__
> +       struct pt_regs regs = {
> +               .rdi = 1,
> +               .rsi = 2,
> +               .rdx = 3,
> +               .rcx = 4,
> +               .r8 = 5,
> +       };
> +
> +       LIBBPF_OPTS(bpf_test_run_opts, tattr,
> +               .ctx_in = &regs,
> +               .ctx_size_in = sizeof(regs),
> +       );
> +
> +       struct kprobe_ctx *skel = NULL;
> +       int prog_fd;
> +       int err;
> +
> +       skel = kprobe_ctx__open_and_load();
> +       if (!ASSERT_OK_PTR(skel, "skel_open"))
> +               return;
> +
> +       skel->bss->expected_p1 = (void *)1;
> +       skel->bss->expected_p2 = (void *)2;
> +       skel->bss->expected_p3 = (void *)3;
> +       skel->bss->expected_p4 = (void *)4;
> +       skel->bss->expected_p5 = (void *)5;
> +
> +       prog_fd = bpf_program__fd(skel->progs.prog);
> +       err = bpf_prog_test_run_opts(prog_fd, &tattr);
> +       if (!ASSERT_OK(err, "bpf_prog_test_run"))
> +               goto cleanup;
> +
> +       if (!ASSERT_TRUE(skel->bss->ret, "ret"))
> +               goto cleanup;
> +
> +       if (!ASSERT_GT(tattr.duration, 0, "duration"))
> +               goto cleanup;
> +cleanup:
> +       kprobe_ctx__destroy(skel);
> +#endif
> +}
> diff --git a/tools/testing/selftests/bpf/progs/kprobe_ctx.c b/tools/testing/selftests/bpf/progs/kprobe_ctx.c
> new file mode 100644
> index 000000000000..98063c549930
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/kprobe_ctx.c
> @@ -0,0 +1,33 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +volatile void *expected_p1;
> +volatile void *expected_p2;
> +volatile void *expected_p3;
> +volatile void *expected_p4;
> +volatile void *expected_p5;
> +volatile bool ret = false;
> +
> +SEC("kprobe/this_function_does_not_exist")
> +int prog(struct pt_regs *ctx)
> +{
> +       void *p1, *p2, *p3, *p4, *p5;
> +
> +       p1 = (void *)PT_REGS_PARM1(ctx);
> +       p2 = (void *)PT_REGS_PARM2(ctx);
> +       p3 = (void *)PT_REGS_PARM3(ctx);
> +       p4 = (void *)PT_REGS_PARM4(ctx);
> +       p5 = (void *)PT_REGS_PARM5(ctx);
> +
> +       if (p1 != expected_p1 || p2 != expected_p2 || p3 != expected_p3 ||
> +           p4 != expected_p4 || p5 != expected_p5)
> +               return 0;
> +
> +       ret = true;
> +       return 0;
> +}
> +
> +char _license[] SEC("license") = "GPL";
> --
> 2.36.1
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c b/tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c
new file mode 100644
index 000000000000..260966fd4506
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/kprobe_ctx.c
@@ -0,0 +1,57 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <linux/ptrace.h>
+#include "kprobe_ctx.skel.h"
+
+/*
+ * x86_64 happens to be one of the architectures that exports the
+ * kernel `struct pt_regs` to userspace ABI. For the architectures
+ * that don't, users will have to extract `struct pt_regs` from vmlinux
+ * BTF in order to use BPF_PROG_TYPE_KPROBE's BPF_PROG_RUN functionality.
+ *
+ * We choose to only test x86 here to keep the test simple.
+ */
+void test_kprobe_ctx(void)
+{
+#ifdef __x86_64__
+	struct pt_regs regs = {
+		.rdi = 1,
+		.rsi = 2,
+		.rdx = 3,
+		.rcx = 4,
+		.r8 = 5,
+	};
+
+	LIBBPF_OPTS(bpf_test_run_opts, tattr,
+		.ctx_in = &regs,
+		.ctx_size_in = sizeof(regs),
+	);
+
+	struct kprobe_ctx *skel = NULL;
+	int prog_fd;
+	int err;
+
+	skel = kprobe_ctx__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	skel->bss->expected_p1 = (void *)1;
+	skel->bss->expected_p2 = (void *)2;
+	skel->bss->expected_p3 = (void *)3;
+	skel->bss->expected_p4 = (void *)4;
+	skel->bss->expected_p5 = (void *)5;
+
+	prog_fd = bpf_program__fd(skel->progs.prog);
+	err = bpf_prog_test_run_opts(prog_fd, &tattr);
+	if (!ASSERT_OK(err, "bpf_prog_test_run"))
+		goto cleanup;
+
+	if (!ASSERT_TRUE(skel->bss->ret, "ret"))
+		goto cleanup;
+
+	if (!ASSERT_GT(tattr.duration, 0, "duration"))
+		goto cleanup;
+cleanup:
+	kprobe_ctx__destroy(skel);
+#endif
+}
diff --git a/tools/testing/selftests/bpf/progs/kprobe_ctx.c b/tools/testing/selftests/bpf/progs/kprobe_ctx.c
new file mode 100644
index 000000000000..98063c549930
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/kprobe_ctx.c
@@ -0,0 +1,33 @@ 
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+volatile void *expected_p1;
+volatile void *expected_p2;
+volatile void *expected_p3;
+volatile void *expected_p4;
+volatile void *expected_p5;
+volatile bool ret = false;
+
+SEC("kprobe/this_function_does_not_exist")
+int prog(struct pt_regs *ctx)
+{
+	void *p1, *p2, *p3, *p4, *p5;
+
+	p1 = (void *)PT_REGS_PARM1(ctx);
+	p2 = (void *)PT_REGS_PARM2(ctx);
+	p3 = (void *)PT_REGS_PARM3(ctx);
+	p4 = (void *)PT_REGS_PARM4(ctx);
+	p5 = (void *)PT_REGS_PARM5(ctx);
+
+	if (p1 != expected_p1 || p2 != expected_p2 || p3 != expected_p3 ||
+	    p4 != expected_p4 || p5 != expected_p5)
+		return 0;
+
+	ret = true;
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";