[PATCH bpf-next v3 2/2] selftests/bpf: add .BTF.ext line/func info getter tests

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Mykyta Yatsenko <yatsenko@xxxxxxxx>

Add selftests checking that line and func info retrieved by newly added
libbpf APIs are the same as returned by kernel via bpf_prog_get_info_by_fd.

Signed-off-by: Mykyta Yatsenko <yatsenko@xxxxxxxx>
---
 .../selftests/bpf/prog_tests/test_btf_ext.c   | 111 ++++++++++++++++++
 .../selftests/bpf/progs/test_btf_ext.c        |  21 ++++
 2 files changed, 132 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_btf_ext.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_btf_ext.c

diff --git a/tools/testing/selftests/bpf/prog_tests/test_btf_ext.c b/tools/testing/selftests/bpf/prog_tests/test_btf_ext.c
new file mode 100644
index 000000000000..00aeebc8f863
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/test_btf_ext.c
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
+#include <test_progs.h>
+#include "test_btf_ext.skel.h"
+#include "btf_helpers.h"
+
+static void subtest_line_info(void)
+{
+	struct test_btf_ext *skel;
+	struct bpf_prog_info info;
+	struct bpf_line_info line_info[1024], *libbpf_line_info;
+	__u32 info_len = sizeof(info), libbbpf_line_info_cnt;
+	int err, fd, i;
+
+	skel = test_btf_ext__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	bpf_program__set_autoload(skel->progs.global_func, true);
+
+	err = test_btf_ext__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto out;
+
+	fd = bpf_program__fd(skel->progs.global_func);
+
+	memset(&info, 0, sizeof(info));
+	info.line_info = ptr_to_u64(&line_info);
+	info.nr_line_info = sizeof(line_info);
+	info.line_info_rec_size = sizeof(*line_info);
+	err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
+	if (!ASSERT_OK(err, "prog_info"))
+		goto out;
+
+	libbpf_line_info = bpf_program__line_info(skel->progs.global_func);
+	libbbpf_line_info_cnt = bpf_program__line_info_cnt(skel->progs.global_func);
+
+	if (!ASSERT_OK_PTR(libbpf_line_info, "bpf_program__line_info"))
+		goto out;
+	if (!ASSERT_GT(libbbpf_line_info_cnt, 0, "line_info_cnt>0"))
+		goto out;
+	if (!ASSERT_EQ(libbbpf_line_info_cnt, info.nr_line_info, "line_info_cnt"))
+		goto out;
+
+	for (i = 0; i < libbbpf_line_info_cnt; ++i) {
+		int eq = memcmp(libbpf_line_info + i, line_info + i, sizeof(*line_info));
+
+		if (!ASSERT_EQ(eq, 0, "line_info"))
+			goto out;
+	}
+
+out:
+	test_btf_ext__destroy(skel);
+}
+
+static void subtest_func_info(void)
+{
+	struct test_btf_ext *skel;
+	struct bpf_prog_info info;
+	struct bpf_func_info func_info[1024], *libbpf_func_info;
+	__u32 info_len = sizeof(info), libbbpf_func_info_cnt;
+	int err, fd, i;
+
+	skel = test_btf_ext__open();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	bpf_program__set_autoload(skel->progs.global_func, true);
+
+	err = test_btf_ext__load(skel);
+	if (!ASSERT_OK(err, "skel_load"))
+		goto out;
+
+	fd = bpf_program__fd(skel->progs.global_func);
+
+	memset(&info, 0, sizeof(info));
+	info.func_info = ptr_to_u64(&func_info);
+	info.nr_func_info = sizeof(func_info);
+	info.func_info_rec_size = sizeof(*func_info);
+	err = bpf_prog_get_info_by_fd(fd, &info, &info_len);
+	if (!ASSERT_OK(err, "prog_info"))
+		goto out;
+
+	libbpf_func_info = bpf_program__func_info(skel->progs.global_func);
+	libbbpf_func_info_cnt = bpf_program__func_info_cnt(skel->progs.global_func);
+
+	if (!ASSERT_OK_PTR(libbpf_func_info, "bpf_program__func_info"))
+		goto out;
+	if (!ASSERT_GT(libbbpf_func_info_cnt, 0, "func_info_cnt>0"))
+		goto out;
+	if (!ASSERT_EQ(libbbpf_func_info_cnt, info.nr_func_info, "func_info_cnt"))
+		goto out;
+
+	for (i = 0; i < libbbpf_func_info_cnt; ++i) {
+		int eq = memcmp(libbpf_func_info + i, func_info + i, sizeof(*func_info));
+
+		if (!ASSERT_EQ(eq, 0, "func_info"))
+			goto out;
+	}
+
+out:
+	test_btf_ext__destroy(skel);
+}
+
+void test_btf_ext(void)
+{
+	if (test__start_subtest("func_info"))
+		subtest_func_info();
+	if (test__start_subtest("line_info"))
+		subtest_line_info();
+}
diff --git a/tools/testing/selftests/bpf/progs/test_btf_ext.c b/tools/testing/selftests/bpf/progs/test_btf_ext.c
new file mode 100644
index 000000000000..be92e445a988
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_btf_ext.c
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Meta Platforms Inc. */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+const volatile int val = 3;
+
+static __attribute__ ((noinline))
+int f0(int var)
+{
+	int retval = var + val;
+
+	return retval;
+}
+
+SEC("tc")
+int global_func(struct __sk_buff *skb)
+{
+	return f0(skb->len);
+}
-- 
2.49.0





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux