Subject: [PATCH bpf-next 23/29] selftests/bpf: Add bpf_arg/bpf_ret_value test

From: Jiri Olsa <[email-masked]>
Date: Thu, 18 Nov 2021 12:24:49 +0100

Adding tests for bpf_arg/bpf_ret_value helpers on
both fentry and fexit programs.

Signed-off-by: Jiri Olsa <[email-masked]>
---
 .../selftests/bpf/prog_tests/args_test.c      | 34 +++++++++++++++++++
 tools/testing/selftests/bpf/progs/args_test.c | 30 ++++++++++++++++
 2 files changed, 64 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/args_test.c
 create mode 100644 tools/testing/selftests/bpf/progs/args_test.c

diff --git a/tools/testing/selftests/bpf/prog_tests/args_test.c b/tools/testing/selftests/bpf/prog_tests/args_test.c
new file mode 100644
index 000000000000..1938f2616ee9
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/args_test.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include "args_test.skel.h"
+
+void test_args_test(void)
+{
+	struct args_test *skel = NULL;
+	__u32 duration = 0, retval;
+	int err, prog_fd;
+
+	skel = args_test__open();
+	if (!ASSERT_OK_PTR(skel, "args_test__open"))
+		return;
+
+	err = args_test__load(skel);
+	if (!ASSERT_OK(err, "args_test__load"))
+		goto cleanup;
+
+	err = args_test__attach(skel);
+	if (!ASSERT_OK(err, "args_test__attach"))
+		goto cleanup;
+
+	prog_fd = bpf_program__fd(skel->progs.test1);
+	err = bpf_prog_test_run(prog_fd, 1, NULL, 0,
+				NULL, NULL, &retval, &duration);
+	ASSERT_OK(err, "test_run");
+	ASSERT_EQ(retval, 0, "test_run");
+
+	ASSERT_EQ(skel->bss->test1_result, 1, "test1_result");
+	ASSERT_EQ(skel->bss->test2_result, 1, "test2_result");
+
+cleanup:
+	args_test__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/args_test.c b/tools/testing/selftests/bpf/progs/args_test.c
new file mode 100644
index 000000000000..7fc8e9fb41bd
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/args_test.c
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+__u64 test1_result = 0;
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(test1)
+{
+	__u64 a = bpf_arg(ctx, 0);
+	__u64 x = bpf_arg(ctx, 1);
+
+	test1_result = (int) a == 1 && x == 0;
+	return 0;
+}
+
+__u64 test2_result = 0;
+SEC("fexit/bpf_fentry_test2")
+int BPF_PROG(test2)
+{
+	__u64 ret = bpf_ret_value(ctx);
+	__u64 a = bpf_arg(ctx, 0);
+	__u64 b = bpf_arg(ctx, 1);
+	__u64 x = bpf_arg(ctx, 2);
+
+	test2_result = (int) a == 2 && b == 3 && ret == 5 && x == 0;
+	return 0;
+}
-- 
2.31.1