Skip to content

Commit

Permalink
selftests/bpf: make sure linking objects with duplicate extern functi…
Browse files Browse the repository at this point in the history
…ons doesn't fail

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@hack3r.moe>
  • Loading branch information
hack3ric committed Sep 29, 2024
1 parent 934683e commit 0f38bbe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tools/testing/selftests/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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))
Expand Down
9 changes: 9 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/dup_extern_funcs.c
Original file line number Diff line number Diff line change
@@ -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);
}
17 changes: 17 additions & 0 deletions tools/testing/selftests/bpf/progs/dup_extern_funcs1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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;
}
15 changes: 15 additions & 0 deletions tools/testing/selftests/bpf/progs/dup_extern_funcs2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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;
}

0 comments on commit 0f38bbe

Please sign in to comment.