Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add BTF.ext line/func info getter tests

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

authored by

Mykyta Yatsenko and committed by
Andrii Nakryiko
b8390dd1 243d720e

+86
+64
tools/testing/selftests/bpf/prog_tests/test_btf_ext.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2025 Meta Platforms Inc. */ 3 + #include <test_progs.h> 4 + #include "test_btf_ext.skel.h" 5 + #include "btf_helpers.h" 6 + 7 + static void subtest_line_func_info(void) 8 + { 9 + struct test_btf_ext *skel; 10 + struct bpf_prog_info info; 11 + struct bpf_line_info line_info[128], *libbpf_line_info; 12 + struct bpf_func_info func_info[128], *libbpf_func_info; 13 + __u32 info_len = sizeof(info), libbbpf_line_info_cnt, libbbpf_func_info_cnt; 14 + int err, fd; 15 + 16 + skel = test_btf_ext__open_and_load(); 17 + if (!ASSERT_OK_PTR(skel, "skel_open_and_load")) 18 + return; 19 + 20 + fd = bpf_program__fd(skel->progs.global_func); 21 + 22 + memset(&info, 0, sizeof(info)); 23 + info.line_info = ptr_to_u64(&line_info); 24 + info.nr_line_info = sizeof(line_info); 25 + info.line_info_rec_size = sizeof(*line_info); 26 + err = bpf_prog_get_info_by_fd(fd, &info, &info_len); 27 + if (!ASSERT_OK(err, "prog_line_info")) 28 + goto out; 29 + 30 + libbpf_line_info = bpf_program__line_info(skel->progs.global_func); 31 + libbbpf_line_info_cnt = bpf_program__line_info_cnt(skel->progs.global_func); 32 + 33 + memset(&info, 0, sizeof(info)); 34 + info.func_info = ptr_to_u64(&func_info); 35 + info.nr_func_info = sizeof(func_info); 36 + info.func_info_rec_size = sizeof(*func_info); 37 + err = bpf_prog_get_info_by_fd(fd, &info, &info_len); 38 + if (!ASSERT_OK(err, "prog_func_info")) 39 + goto out; 40 + 41 + libbpf_func_info = bpf_program__func_info(skel->progs.global_func); 42 + libbbpf_func_info_cnt = bpf_program__func_info_cnt(skel->progs.global_func); 43 + 44 + if (!ASSERT_OK_PTR(libbpf_line_info, "bpf_program__line_info")) 45 + goto out; 46 + if (!ASSERT_EQ(libbbpf_line_info_cnt, info.nr_line_info, "line_info_cnt")) 47 + goto out; 48 + if (!ASSERT_OK_PTR(libbpf_func_info, "bpf_program__func_info")) 49 + goto out; 50 + if (!ASSERT_EQ(libbbpf_func_info_cnt, info.nr_func_info, "func_info_cnt")) 51 + goto out; 52 + ASSERT_MEMEQ(libbpf_line_info, line_info, libbbpf_line_info_cnt * sizeof(*line_info), 53 + "line_info"); 54 + ASSERT_MEMEQ(libbpf_func_info, func_info, libbbpf_func_info_cnt * sizeof(*func_info), 55 + "func_info"); 56 + out: 57 + test_btf_ext__destroy(skel); 58 + } 59 + 60 + void test_btf_ext(void) 61 + { 62 + if (test__start_subtest("line_func_info")) 63 + subtest_line_func_info(); 64 + }
+22
tools/testing/selftests/bpf/progs/test_btf_ext.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* Copyright (c) 2025 Meta Platforms Inc. */ 3 + 4 + #include <linux/bpf.h> 5 + #include <bpf/bpf_helpers.h> 6 + #include "bpf_misc.h" 7 + 8 + char _license[] SEC("license") = "GPL"; 9 + 10 + __noinline static void f0(void) 11 + { 12 + __u64 a = 1; 13 + 14 + __sink(a); 15 + } 16 + 17 + SEC("xdp") 18 + __u64 global_func(struct xdp_md *xdp) 19 + { 20 + f0(); 21 + return XDP_DROP; 22 + }