From: Eric Long <i@xxxxxxxxxx> Previously when multiple BPF object files referencing the same extern function (usually kfunc) are statically linked using `bpftool gen object`, libbpf tries to get the nonexistent size of BTF_KIND_FUNC_PROTO and fails. This test ensures it is fixed. Signed-off-by: Eric Long <i@xxxxxxxxxx> --- tools/testing/selftests/bpf/Makefile | 3 ++- .../selftests/bpf/prog_tests/dup_extern_funcs.c | 9 +++++++++ .../testing/selftests/bpf/progs/dup_extern_funcs1.c | 20 ++++++++++++++++++++ .../testing/selftests/bpf/progs/dup_extern_funcs2.c | 18 ++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index e295e3df5ec6c3c21abe368038514cfb34b42f69..644c4dd6002c691a9cd94ef26ddf51f6dc84e2cc 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -496,7 +496,7 @@ SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ linked_vars.skel.h linked_maps.skel.h \ test_subskeleton.skel.h test_subskeleton_lib.skel.h \ - test_usdt.skel.h + test_usdt.skel.h dup_extern_funcs.skel.h LSKELS := fentry_test.c fexit_test.c fexit_sleep.c atomics.c \ trace_printk.c trace_vprintk.c map_ptr_kern.c \ @@ -520,6 +520,7 @@ test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o xdp_features.skel.h-deps := xdp_features.bpf.o +dup_extern_funcs.skel.h-deps := dup_extern_funcs1.bpf.o dup_extern_funcs2.bpf.o LINKED_BPF_OBJS := $(foreach skel,$(LINKED_SKELS),$($(skel)-deps)) LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(LINKED_BPF_OBJS)) diff --git a/tools/testing/selftests/bpf/prog_tests/dup_extern_funcs.c b/tools/testing/selftests/bpf/prog_tests/dup_extern_funcs.c new file mode 100644 index 0000000000000000000000000000000000000000..b26f855745b451f7f53e44b27d47a2f659ad1378 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/dup_extern_funcs.c @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "dup_extern_funcs.skel.h" + +void test_dup_extern_funcs(void) +{ + RUN_TESTS(dup_extern_funcs); +} diff --git a/tools/testing/selftests/bpf/progs/dup_extern_funcs1.c b/tools/testing/selftests/bpf/progs/dup_extern_funcs1.c new file mode 100644 index 0000000000000000000000000000000000000000..6850e01d1455c0a2da947ad6d5b1c5dab0187e00 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/dup_extern_funcs1.c @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +void *bpf_cast_to_kern_ctx(void *obj) __ksym; + +SEC("tc") +int handler1(struct __sk_buff *skb) +{ + struct sk_buff *skb_kern = bpf_cast_to_kern_ctx(skb); + + if (!skb_kern) + return -1; + + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/dup_extern_funcs2.c b/tools/testing/selftests/bpf/progs/dup_extern_funcs2.c new file mode 100644 index 0000000000000000000000000000000000000000..66ba3620274497cc40f8dfbb8d98856d4eab707e --- /dev/null +++ b/tools/testing/selftests/bpf/progs/dup_extern_funcs2.c @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +void *bpf_cast_to_kern_ctx(void *obj) __ksym; + +SEC("xdp") +int handler2(struct xdp_md *xdp) +{ + struct xdp_buff *xdp_kern = bpf_cast_to_kern_ctx(xdp); + + if (!xdp_kern) + return -1; + + return 0; +} -- 2.46.2