1aa3d65deSViktor Malik // SPDX-License-Identifier: GPL-2.0
2aa3d65deSViktor Malik /* Copyright (c) 2022 Red Hat */
3aa3d65deSViktor Malik #include <test_progs.h>
4aa3d65deSViktor Malik #include <bpf/btf.h>
5aa3d65deSViktor Malik #include "bpf/libbpf_internal.h"
6aa3d65deSViktor Malik #include "cgroup_helpers.h"
7*23457b37SFeng Yang #include "bpf_util.h"
8aa3d65deSViktor Malik
9aa3d65deSViktor Malik static const char *module_name = "bpf_testmod";
10aa3d65deSViktor Malik static const char *symbol_name = "bpf_fentry_shadow_test";
11aa3d65deSViktor Malik
get_bpf_testmod_btf_fd(void)12aa3d65deSViktor Malik static int get_bpf_testmod_btf_fd(void)
13aa3d65deSViktor Malik {
14aa3d65deSViktor Malik struct bpf_btf_info info;
15aa3d65deSViktor Malik char name[64];
16aa3d65deSViktor Malik __u32 id = 0, len;
17aa3d65deSViktor Malik int err, fd;
18aa3d65deSViktor Malik
19aa3d65deSViktor Malik while (true) {
20aa3d65deSViktor Malik err = bpf_btf_get_next_id(id, &id);
21aa3d65deSViktor Malik if (err) {
22aa3d65deSViktor Malik log_err("failed to iterate BTF objects");
23aa3d65deSViktor Malik return err;
24aa3d65deSViktor Malik }
25aa3d65deSViktor Malik
26aa3d65deSViktor Malik fd = bpf_btf_get_fd_by_id(id);
27aa3d65deSViktor Malik if (fd < 0) {
28aa3d65deSViktor Malik if (errno == ENOENT)
29aa3d65deSViktor Malik continue; /* expected race: BTF was unloaded */
30aa3d65deSViktor Malik err = -errno;
31aa3d65deSViktor Malik log_err("failed to get FD for BTF object #%d", id);
32aa3d65deSViktor Malik return err;
33aa3d65deSViktor Malik }
34aa3d65deSViktor Malik
35aa3d65deSViktor Malik len = sizeof(info);
36aa3d65deSViktor Malik memset(&info, 0, sizeof(info));
37aa3d65deSViktor Malik info.name = ptr_to_u64(name);
38aa3d65deSViktor Malik info.name_len = sizeof(name);
39aa3d65deSViktor Malik
40aa3d65deSViktor Malik err = bpf_obj_get_info_by_fd(fd, &info, &len);
41aa3d65deSViktor Malik if (err) {
42aa3d65deSViktor Malik err = -errno;
43aa3d65deSViktor Malik log_err("failed to get info for BTF object #%d", id);
44aa3d65deSViktor Malik close(fd);
45aa3d65deSViktor Malik return err;
46aa3d65deSViktor Malik }
47aa3d65deSViktor Malik
48aa3d65deSViktor Malik if (strcmp(name, module_name) == 0)
49aa3d65deSViktor Malik return fd;
50aa3d65deSViktor Malik
51aa3d65deSViktor Malik close(fd);
52aa3d65deSViktor Malik }
53aa3d65deSViktor Malik return -ENOENT;
54aa3d65deSViktor Malik }
55aa3d65deSViktor Malik
test_module_fentry_shadow(void)56aa3d65deSViktor Malik void test_module_fentry_shadow(void)
57aa3d65deSViktor Malik {
58aa3d65deSViktor Malik struct btf *vmlinux_btf = NULL, *mod_btf = NULL;
59aa3d65deSViktor Malik int err, i;
60aa3d65deSViktor Malik int btf_fd[2] = {};
61aa3d65deSViktor Malik int prog_fd[2] = {};
62aa3d65deSViktor Malik int link_fd[2] = {};
63aa3d65deSViktor Malik __s32 btf_id[2] = {};
64aa3d65deSViktor Malik
65971f7c32SArtem Savkov if (!env.has_testmod) {
66971f7c32SArtem Savkov test__skip();
67971f7c32SArtem Savkov return;
68971f7c32SArtem Savkov }
69971f7c32SArtem Savkov
70aa3d65deSViktor Malik LIBBPF_OPTS(bpf_prog_load_opts, load_opts,
71aa3d65deSViktor Malik .expected_attach_type = BPF_TRACE_FENTRY,
72aa3d65deSViktor Malik );
73aa3d65deSViktor Malik
74aa3d65deSViktor Malik const struct bpf_insn trace_program[] = {
75aa3d65deSViktor Malik BPF_MOV64_IMM(BPF_REG_0, 0),
76aa3d65deSViktor Malik BPF_EXIT_INSN(),
77aa3d65deSViktor Malik };
78aa3d65deSViktor Malik
79aa3d65deSViktor Malik vmlinux_btf = btf__load_vmlinux_btf();
80aa3d65deSViktor Malik if (!ASSERT_OK_PTR(vmlinux_btf, "load_vmlinux_btf"))
81aa3d65deSViktor Malik return;
82aa3d65deSViktor Malik
83aa3d65deSViktor Malik btf_fd[1] = get_bpf_testmod_btf_fd();
84aa3d65deSViktor Malik if (!ASSERT_GE(btf_fd[1], 0, "get_bpf_testmod_btf_fd"))
85aa3d65deSViktor Malik goto out;
86aa3d65deSViktor Malik
87aa3d65deSViktor Malik mod_btf = btf_get_from_fd(btf_fd[1], vmlinux_btf);
88aa3d65deSViktor Malik if (!ASSERT_OK_PTR(mod_btf, "btf_get_from_fd"))
89aa3d65deSViktor Malik goto out;
90aa3d65deSViktor Malik
91aa3d65deSViktor Malik btf_id[0] = btf__find_by_name_kind(vmlinux_btf, symbol_name, BTF_KIND_FUNC);
92aa3d65deSViktor Malik if (!ASSERT_GT(btf_id[0], 0, "btf_find_by_name"))
93aa3d65deSViktor Malik goto out;
94aa3d65deSViktor Malik
95aa3d65deSViktor Malik btf_id[1] = btf__find_by_name_kind(mod_btf, symbol_name, BTF_KIND_FUNC);
96aa3d65deSViktor Malik if (!ASSERT_GT(btf_id[1], 0, "btf_find_by_name"))
97aa3d65deSViktor Malik goto out;
98aa3d65deSViktor Malik
99aa3d65deSViktor Malik for (i = 0; i < 2; i++) {
100aa3d65deSViktor Malik load_opts.attach_btf_id = btf_id[i];
101aa3d65deSViktor Malik load_opts.attach_btf_obj_fd = btf_fd[i];
102aa3d65deSViktor Malik prog_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL",
103aa3d65deSViktor Malik trace_program,
104*23457b37SFeng Yang ARRAY_SIZE(trace_program),
105aa3d65deSViktor Malik &load_opts);
106aa3d65deSViktor Malik if (!ASSERT_GE(prog_fd[i], 0, "bpf_prog_load"))
107aa3d65deSViktor Malik goto out;
108aa3d65deSViktor Malik
109aa3d65deSViktor Malik /* If the verifier incorrectly resolves addresses of the
110aa3d65deSViktor Malik * shadowed functions and uses the same address for both the
111aa3d65deSViktor Malik * vmlinux and the bpf_testmod functions, this will fail on
112aa3d65deSViktor Malik * attempting to create two trampolines for the same address,
113aa3d65deSViktor Malik * which is forbidden.
114aa3d65deSViktor Malik */
115aa3d65deSViktor Malik link_fd[i] = bpf_link_create(prog_fd[i], 0, BPF_TRACE_FENTRY, NULL);
116aa3d65deSViktor Malik if (!ASSERT_GE(link_fd[i], 0, "bpf_link_create"))
117aa3d65deSViktor Malik goto out;
118aa3d65deSViktor Malik }
119aa3d65deSViktor Malik
120aa3d65deSViktor Malik err = bpf_prog_test_run_opts(prog_fd[0], NULL);
121aa3d65deSViktor Malik ASSERT_OK(err, "running test");
122aa3d65deSViktor Malik
123aa3d65deSViktor Malik out:
124aa3d65deSViktor Malik btf__free(vmlinux_btf);
125aa3d65deSViktor Malik btf__free(mod_btf);
126aa3d65deSViktor Malik for (i = 0; i < 2; i++) {
127aa3d65deSViktor Malik if (btf_fd[i])
128aa3d65deSViktor Malik close(btf_fd[i]);
129aa3d65deSViktor Malik if (prog_fd[i] > 0)
130aa3d65deSViktor Malik close(prog_fd[i]);
131aa3d65deSViktor Malik if (link_fd[i] > 0)
132aa3d65deSViktor Malik close(link_fd[i]);
133aa3d65deSViktor Malik }
134aa3d65deSViktor Malik }
135