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"
9 static const char *module_name = "bpf_testmod";
10 static const char *symbol_name = "bpf_fentry_shadow_test";
12 static int get_bpf_testmod_btf_fd(void)
14 struct bpf_btf_info info;
20 err = bpf_btf_get_next_id(id, &id);
22 log_err("failed to iterate BTF objects");
26 fd = bpf_btf_get_fd_by_id(id);
29 continue; /* expected race: BTF was unloaded */
31 log_err("failed to get FD for BTF object #%d", id);
36 memset(&info, 0, sizeof(info));
37 info.name = ptr_to_u64(name);
38 info.name_len = sizeof(name);
40 err = bpf_obj_get_info_by_fd(fd, &info, &len);
43 log_err("failed to get info for BTF object #%d", id);
48 if (strcmp(name, module_name) == 0)
56 void test_module_fentry_shadow(void)
58 struct btf *vmlinux_btf = NULL, *mod_btf = NULL;
65 if (!env.has_testmod) {
70 LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
71 .expected_attach_type = BPF_TRACE_FENTRY,
74 const struct bpf_insn trace_program[] = {
75 BPF_MOV64_IMM(BPF_REG_0, 0),
79 vmlinux_btf = btf__load_vmlinux_btf();
80 if (!ASSERT_OK_PTR(vmlinux_btf, "load_vmlinux_btf"))
83 btf_fd[1] = get_bpf_testmod_btf_fd();
84 if (!ASSERT_GE(btf_fd[1], 0, "get_bpf_testmod_btf_fd"))
87 mod_btf = btf_get_from_fd(btf_fd[1], vmlinux_btf);
88 if (!ASSERT_OK_PTR(mod_btf, "btf_get_from_fd"))
91 btf_id[0] = btf__find_by_name_kind(vmlinux_btf, symbol_name, BTF_KIND_FUNC);
92 if (!ASSERT_GT(btf_id[0], 0, "btf_find_by_name"))
95 btf_id[1] = btf__find_by_name_kind(mod_btf, symbol_name, BTF_KIND_FUNC);
96 if (!ASSERT_GT(btf_id[1], 0, "btf_find_by_name"))
99 for (i = 0; i < 2; i++) {
100 load_opts.attach_btf_id = btf_id[i];
101 load_opts.attach_btf_obj_fd = btf_fd[i];
102 prog_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL",
104 ARRAY_SIZE(trace_program),
106 if (!ASSERT_GE(prog_fd[i], 0, "bpf_prog_load"))
109 /* If the verifier incorrectly resolves addresses of the
110 * shadowed functions and uses the same address for both the
111 * vmlinux and the bpf_testmod functions, this will fail on
112 * attempting to create two trampolines for the same address,
113 * which is forbidden.
115 link_fd[i] = bpf_link_create(prog_fd[i], 0, BPF_TRACE_FENTRY, NULL);
116 if (!ASSERT_GE(link_fd[i], 0, "bpf_link_create"))
120 err = bpf_prog_test_run_opts(prog_fd[0], NULL);
121 ASSERT_OK(err, "running test");
124 btf__free(vmlinux_btf);
126 for (i = 0; i < 2; i++) {