On Sun, Nov 27, 2022 at 11:26 PM Viktor Malik <vmalik@xxxxxxxxxx> wrote: > > Adds a new test that tries to attach a program to fentry of two > functions of the same name, one located in vmlinux and the other in > bpf_testmod. > > To avoid conflicts with existing tests, a new function > "bpf_fentry_shadow_test" was created both in vmlinux and in bpf_testmod. > > The previous commit fixed a bug which caused this test to fail. The > verifier would always use the vmlinux function's address as the target > trampoline address, hence trying to attach two programs to the same > trampoline. > > Signed-off-by: Viktor Malik <vmalik@xxxxxxxxxx> > --- <...> > diff --git a/tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c b/tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c > new file mode 100644 > index 000000000000..0c604a0f22ca > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/module_attach_shadow.c > @@ -0,0 +1,120 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (c) 2022 Red Hat */ > +#include <test_progs.h> > +#include <bpf/btf.h> > +#include "bpf/libbpf_internal.h" > +#include "cgroup_helpers.h" > + > +static const char *module_name = "bpf_testmod"; > +static const char *symbol_name = "bpf_fentry_shadow_test"; > + > +int get_bpf_testmod_btf_fd(void) > +{ > + struct bpf_btf_info info; > + char name[64]; > + __u32 id, len; We need to initialize 'id'. > + int err, fd; <...> > +} > + > +void test_module_fentry_shadow(void) > +{ <...> > + > + btf_id[0] = btf__find_by_name(vmlinux_btf, symbol_name); > + if (!ASSERT_GT(btf_id[0], 0, "btf_find_by_name")) > + goto out; > + > + btf_id[1] = btf__find_by_name(mod_btf, symbol_name); btf__find_by_name_kind() may be better. It skips the name comparison if the kind doesn't match. > + if (!ASSERT_GT(btf_id[1], 0, "btf_find_by_name")) > + goto out; > + <...> > + err = bpf_prog_test_run_opts(prog_fd[0], &test_opts); > + ASSERT_OK(err, "running test"); > + > +out: We also need to btf__free vmlinux_btf and mod_btf. > + for (i = 0; i < 2; i++) { > + if (btf_fd[i]) > + close(btf_fd[i]); > + if (prog_fd[i]) > + close(prog_fd[i]); > + if (link_fd[i]) > + close(link_fd[i]); > + } > +} > -- > 2.38.1 >