staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / testing / selftests / bpf / prog_tests / attach_probe.c
blob5ecc267d98b06b5289d39d01b85630a7145347b1
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
4 ssize_t get_base_addr() {
5 size_t start;
6 char buf[256];
7 FILE *f;
9 f = fopen("/proc/self/maps", "r");
10 if (!f)
11 return -errno;
13 while (fscanf(f, "%zx-%*x %s %*s\n", &start, buf) == 2) {
14 if (strcmp(buf, "r-xp") == 0) {
15 fclose(f);
16 return start;
20 fclose(f);
21 return -EINVAL;
24 void test_attach_probe(void)
26 const char *kprobe_name = "kprobe/sys_nanosleep";
27 const char *kretprobe_name = "kretprobe/sys_nanosleep";
28 const char *uprobe_name = "uprobe/trigger_func";
29 const char *uretprobe_name = "uretprobe/trigger_func";
30 const int kprobe_idx = 0, kretprobe_idx = 1;
31 const int uprobe_idx = 2, uretprobe_idx = 3;
32 const char *file = "./test_attach_probe.o";
33 struct bpf_program *kprobe_prog, *kretprobe_prog;
34 struct bpf_program *uprobe_prog, *uretprobe_prog;
35 struct bpf_object *obj;
36 int err, prog_fd, duration = 0, res;
37 struct bpf_link *kprobe_link = NULL;
38 struct bpf_link *kretprobe_link = NULL;
39 struct bpf_link *uprobe_link = NULL;
40 struct bpf_link *uretprobe_link = NULL;
41 int results_map_fd;
42 size_t uprobe_offset;
43 ssize_t base_addr;
45 base_addr = get_base_addr();
46 if (CHECK(base_addr < 0, "get_base_addr",
47 "failed to find base addr: %zd", base_addr))
48 return;
49 uprobe_offset = (size_t)&get_base_addr - base_addr;
51 /* load programs */
52 err = bpf_prog_load(file, BPF_PROG_TYPE_KPROBE, &obj, &prog_fd);
53 if (CHECK(err, "obj_load", "err %d errno %d\n", err, errno))
54 return;
56 kprobe_prog = bpf_object__find_program_by_title(obj, kprobe_name);
57 if (CHECK(!kprobe_prog, "find_probe",
58 "prog '%s' not found\n", kprobe_name))
59 goto cleanup;
60 kretprobe_prog = bpf_object__find_program_by_title(obj, kretprobe_name);
61 if (CHECK(!kretprobe_prog, "find_probe",
62 "prog '%s' not found\n", kretprobe_name))
63 goto cleanup;
64 uprobe_prog = bpf_object__find_program_by_title(obj, uprobe_name);
65 if (CHECK(!uprobe_prog, "find_probe",
66 "prog '%s' not found\n", uprobe_name))
67 goto cleanup;
68 uretprobe_prog = bpf_object__find_program_by_title(obj, uretprobe_name);
69 if (CHECK(!uretprobe_prog, "find_probe",
70 "prog '%s' not found\n", uretprobe_name))
71 goto cleanup;
73 /* load maps */
74 results_map_fd = bpf_find_map(__func__, obj, "results_map");
75 if (CHECK(results_map_fd < 0, "find_results_map",
76 "err %d\n", results_map_fd))
77 goto cleanup;
79 kprobe_link = bpf_program__attach_kprobe(kprobe_prog,
80 false /* retprobe */,
81 SYS_NANOSLEEP_KPROBE_NAME);
82 if (CHECK(IS_ERR(kprobe_link), "attach_kprobe",
83 "err %ld\n", PTR_ERR(kprobe_link))) {
84 kprobe_link = NULL;
85 goto cleanup;
87 kretprobe_link = bpf_program__attach_kprobe(kretprobe_prog,
88 true /* retprobe */,
89 SYS_NANOSLEEP_KPROBE_NAME);
90 if (CHECK(IS_ERR(kretprobe_link), "attach_kretprobe",
91 "err %ld\n", PTR_ERR(kretprobe_link))) {
92 kretprobe_link = NULL;
93 goto cleanup;
95 uprobe_link = bpf_program__attach_uprobe(uprobe_prog,
96 false /* retprobe */,
97 0 /* self pid */,
98 "/proc/self/exe",
99 uprobe_offset);
100 if (CHECK(IS_ERR(uprobe_link), "attach_uprobe",
101 "err %ld\n", PTR_ERR(uprobe_link))) {
102 uprobe_link = NULL;
103 goto cleanup;
105 uretprobe_link = bpf_program__attach_uprobe(uretprobe_prog,
106 true /* retprobe */,
107 -1 /* any pid */,
108 "/proc/self/exe",
109 uprobe_offset);
110 if (CHECK(IS_ERR(uretprobe_link), "attach_uretprobe",
111 "err %ld\n", PTR_ERR(uretprobe_link))) {
112 uretprobe_link = NULL;
113 goto cleanup;
116 /* trigger & validate kprobe && kretprobe */
117 usleep(1);
119 err = bpf_map_lookup_elem(results_map_fd, &kprobe_idx, &res);
120 if (CHECK(err, "get_kprobe_res",
121 "failed to get kprobe res: %d\n", err))
122 goto cleanup;
123 if (CHECK(res != kprobe_idx + 1, "check_kprobe_res",
124 "wrong kprobe res: %d\n", res))
125 goto cleanup;
127 err = bpf_map_lookup_elem(results_map_fd, &kretprobe_idx, &res);
128 if (CHECK(err, "get_kretprobe_res",
129 "failed to get kretprobe res: %d\n", err))
130 goto cleanup;
131 if (CHECK(res != kretprobe_idx + 1, "check_kretprobe_res",
132 "wrong kretprobe res: %d\n", res))
133 goto cleanup;
135 /* trigger & validate uprobe & uretprobe */
136 get_base_addr();
138 err = bpf_map_lookup_elem(results_map_fd, &uprobe_idx, &res);
139 if (CHECK(err, "get_uprobe_res",
140 "failed to get uprobe res: %d\n", err))
141 goto cleanup;
142 if (CHECK(res != uprobe_idx + 1, "check_uprobe_res",
143 "wrong uprobe res: %d\n", res))
144 goto cleanup;
146 err = bpf_map_lookup_elem(results_map_fd, &uretprobe_idx, &res);
147 if (CHECK(err, "get_uretprobe_res",
148 "failed to get uretprobe res: %d\n", err))
149 goto cleanup;
150 if (CHECK(res != uretprobe_idx + 1, "check_uretprobe_res",
151 "wrong uretprobe res: %d\n", res))
152 goto cleanup;
154 cleanup:
155 bpf_link__destroy(kprobe_link);
156 bpf_link__destroy(kretprobe_link);
157 bpf_link__destroy(uprobe_link);
158 bpf_link__destroy(uretprobe_link);
159 bpf_object__close(obj);