1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Red Hat */
3 #include <test_progs.h>
5 #include "bpf/libbpf_internal.h"
6 #include "cgroup_helpers.h"
8 static const char *module_name = "bpf_testmod";
9 static const char *symbol_name = "bpf_fentry_shadow_test";
11 static int get_bpf_testmod_btf_fd(void)
13 struct bpf_btf_info info;
19 err = bpf_btf_get_next_id(id, &id);
21 log_err("failed to iterate BTF objects");
25 fd = bpf_btf_get_fd_by_id(id);
28 continue; /* expected race: BTF was unloaded */
30 log_err("failed to get FD for BTF object #%d", id);
35 memset(&info, 0, sizeof(info));
36 info.name = ptr_to_u64(name);
37 info.name_len = sizeof(name);
39 err = bpf_obj_get_info_by_fd(fd, &info, &len);
42 log_err("failed to get info for BTF object #%d", id);
47 if (strcmp(name, module_name) == 0)
55 void test_module_fentry_shadow(void)
57 struct btf *vmlinux_btf = NULL, *mod_btf = NULL;
64 if (!env.has_testmod) {
69 LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
70 .expected_attach_type = BPF_TRACE_FENTRY,
73 const struct bpf_insn trace_program[] = {
74 BPF_MOV64_IMM(BPF_REG_0, 0),
78 vmlinux_btf = btf__load_vmlinux_btf();
79 if (!ASSERT_OK_PTR(vmlinux_btf, "load_vmlinux_btf"))
82 btf_fd[1] = get_bpf_testmod_btf_fd();
83 if (!ASSERT_GE(btf_fd[1], 0, "get_bpf_testmod_btf_fd"))
86 mod_btf = btf_get_from_fd(btf_fd[1], vmlinux_btf);
87 if (!ASSERT_OK_PTR(mod_btf, "btf_get_from_fd"))
90 btf_id[0] = btf__find_by_name_kind(vmlinux_btf, symbol_name, BTF_KIND_FUNC);
91 if (!ASSERT_GT(btf_id[0], 0, "btf_find_by_name"))
94 btf_id[1] = btf__find_by_name_kind(mod_btf, symbol_name, BTF_KIND_FUNC);
95 if (!ASSERT_GT(btf_id[1], 0, "btf_find_by_name"))
98 for (i = 0; i < 2; i++) {
99 load_opts.attach_btf_id = btf_id[i];
100 load_opts.attach_btf_obj_fd = btf_fd[i];
101 prog_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL",
103 sizeof(trace_program) / sizeof(struct bpf_insn),
105 if (!ASSERT_GE(prog_fd[i], 0, "bpf_prog_load"))
108 /* If the verifier incorrectly resolves addresses of the
109 * shadowed functions and uses the same address for both the
110 * vmlinux and the bpf_testmod functions, this will fail on
111 * attempting to create two trampolines for the same address,
112 * which is forbidden.
114 link_fd[i] = bpf_link_create(prog_fd[i], 0, BPF_TRACE_FENTRY, NULL);
115 if (!ASSERT_GE(link_fd[i], 0, "bpf_link_create"))
119 err = bpf_prog_test_run_opts(prog_fd[0], NULL);
120 ASSERT_OK(err, "running test");
123 btf__free(vmlinux_btf);
125 for (i = 0; i < 2; i++) {