]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
selftests/bpf: Add BTF.ext line/func info getter tests
authorMykyta Yatsenko <yatsenko@meta.com>
Tue, 8 Apr 2025 23:44:17 +0000 (00:44 +0100)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 9 Apr 2025 23:16:56 +0000 (16:16 -0700)
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@meta.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250408234417.452565-3-mykyta.yatsenko5@gmail.com
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/test_btf_ext.c [new file with mode: 0644]

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 (file)
index 0000000..7d1b478
--- /dev/null
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms Inc. */
+#include <test_progs.h>
+#include "test_btf_ext.skel.h"
+#include "btf_helpers.h"
+
+static void subtest_line_func_info(void)
+{
+       struct test_btf_ext *skel;
+       struct bpf_prog_info info;
+       struct bpf_line_info line_info[128], *libbpf_line_info;
+       struct bpf_func_info func_info[128], *libbpf_func_info;
+       __u32 info_len = sizeof(info), libbbpf_line_info_cnt, libbbpf_func_info_cnt;
+       int err, fd;
+
+       skel = test_btf_ext__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+               return;
+
+       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_line_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);
+
+       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_func_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_line_info, "bpf_program__line_info"))
+               goto out;
+       if (!ASSERT_EQ(libbbpf_line_info_cnt, info.nr_line_info, "line_info_cnt"))
+               goto out;
+       if (!ASSERT_OK_PTR(libbpf_func_info, "bpf_program__func_info"))
+               goto out;
+       if (!ASSERT_EQ(libbbpf_func_info_cnt, info.nr_func_info, "func_info_cnt"))
+               goto out;
+       ASSERT_MEMEQ(libbpf_line_info, line_info, libbbpf_line_info_cnt * sizeof(*line_info),
+                    "line_info");
+       ASSERT_MEMEQ(libbpf_func_info, func_info, libbbpf_func_info_cnt * sizeof(*func_info),
+                    "func_info");
+out:
+       test_btf_ext__destroy(skel);
+}
+
+void test_btf_ext(void)
+{
+       if (test__start_subtest("line_func_info"))
+               subtest_line_func_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 (file)
index 0000000..cdf2033
--- /dev/null
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2025 Meta Platforms Inc. */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+__noinline static void f0(void)
+{
+       __u64 a = 1;
+
+       __sink(a);
+}
+
+SEC("xdp")
+__u64 global_func(struct xdp_md *xdp)
+{
+       f0();
+       return XDP_DROP;
+}