1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2023 Yafang Shao <laoar.shao@gmail.com> */
3
4 #include <string.h>
5 #include <linux/bpf.h>
6 #include <linux/limits.h>
7 #include <test_progs.h>
8 #include "trace_helpers.h"
9 #include "test_fill_link_info.skel.h"
10 #include "bpf/libbpf_internal.h"
11
12 #define TP_CAT "sched"
13 #define TP_NAME "sched_switch"
14
15 static const char *kmulti_syms[] = {
16 "bpf_fentry_test2",
17 "bpf_fentry_test1",
18 "bpf_fentry_test3",
19 };
20 #define KMULTI_CNT ARRAY_SIZE(kmulti_syms)
21 static __u64 kmulti_addrs[KMULTI_CNT];
22 static __u64 kmulti_cookies[] = { 3, 1, 2 };
23
24 #define KPROBE_FUNC "bpf_fentry_test1"
25 static __u64 kprobe_addr;
26
27 #define UPROBE_FILE "/proc/self/exe"
28 static ssize_t uprobe_offset;
29 /* uprobe attach point */
uprobe_func(void)30 static noinline void uprobe_func(void)
31 {
32 asm volatile ("");
33 }
34
35 #define PERF_EVENT_COOKIE 0xdeadbeef
36
verify_perf_link_info(int fd,enum bpf_perf_event_type type,long addr,ssize_t offset,ssize_t entry_offset)37 static int verify_perf_link_info(int fd, enum bpf_perf_event_type type, long addr,
38 ssize_t offset, ssize_t entry_offset)
39 {
40 struct bpf_link_info info;
41 __u32 len = sizeof(info);
42 char buf[PATH_MAX];
43 int err;
44
45 memset(&info, 0, sizeof(info));
46 buf[0] = '\0';
47
48 again:
49 err = bpf_link_get_info_by_fd(fd, &info, &len);
50 if (!ASSERT_OK(err, "get_link_info"))
51 return -1;
52
53 if (!ASSERT_EQ(info.type, BPF_LINK_TYPE_PERF_EVENT, "link_type"))
54 return -1;
55 if (!ASSERT_EQ(info.perf_event.type, type, "perf_type_match"))
56 return -1;
57
58 switch (info.perf_event.type) {
59 case BPF_PERF_EVENT_KPROBE:
60 case BPF_PERF_EVENT_KRETPROBE:
61 ASSERT_EQ(info.perf_event.kprobe.offset, offset, "kprobe_offset");
62
63 /* In case kernel.kptr_restrict is not permitted or MAX_SYMS is reached */
64 if (addr)
65 ASSERT_EQ(info.perf_event.kprobe.addr, addr + entry_offset,
66 "kprobe_addr");
67
68 ASSERT_EQ(info.perf_event.kprobe.cookie, PERF_EVENT_COOKIE, "kprobe_cookie");
69
70 ASSERT_EQ(info.perf_event.kprobe.name_len, strlen(KPROBE_FUNC) + 1,
71 "name_len");
72 if (!info.perf_event.kprobe.func_name) {
73 info.perf_event.kprobe.func_name = ptr_to_u64(&buf);
74 info.perf_event.kprobe.name_len = sizeof(buf);
75 goto again;
76 }
77
78 err = strncmp(u64_to_ptr(info.perf_event.kprobe.func_name), KPROBE_FUNC,
79 strlen(KPROBE_FUNC));
80 ASSERT_EQ(err, 0, "cmp_kprobe_func_name");
81 break;
82 case BPF_PERF_EVENT_TRACEPOINT:
83 ASSERT_EQ(info.perf_event.tracepoint.name_len, strlen(TP_NAME) + 1,
84 "name_len");
85 if (!info.perf_event.tracepoint.tp_name) {
86 info.perf_event.tracepoint.tp_name = ptr_to_u64(&buf);
87 info.perf_event.tracepoint.name_len = sizeof(buf);
88 goto again;
89 }
90
91 ASSERT_EQ(info.perf_event.tracepoint.cookie, PERF_EVENT_COOKIE, "tracepoint_cookie");
92
93 err = strncmp(u64_to_ptr(info.perf_event.tracepoint.tp_name), TP_NAME,
94 strlen(TP_NAME));
95 ASSERT_EQ(err, 0, "cmp_tp_name");
96 break;
97 case BPF_PERF_EVENT_UPROBE:
98 case BPF_PERF_EVENT_URETPROBE:
99 ASSERT_EQ(info.perf_event.uprobe.offset, offset, "uprobe_offset");
100
101 ASSERT_EQ(info.perf_event.uprobe.name_len, strlen(UPROBE_FILE) + 1,
102 "name_len");
103 if (!info.perf_event.uprobe.file_name) {
104 info.perf_event.uprobe.file_name = ptr_to_u64(&buf);
105 info.perf_event.uprobe.name_len = sizeof(buf);
106 goto again;
107 }
108
109 ASSERT_EQ(info.perf_event.uprobe.cookie, PERF_EVENT_COOKIE, "uprobe_cookie");
110
111 err = strncmp(u64_to_ptr(info.perf_event.uprobe.file_name), UPROBE_FILE,
112 strlen(UPROBE_FILE));
113 ASSERT_EQ(err, 0, "cmp_file_name");
114 break;
115 case BPF_PERF_EVENT_EVENT:
116 ASSERT_EQ(info.perf_event.event.type, PERF_TYPE_SOFTWARE, "event_type");
117 ASSERT_EQ(info.perf_event.event.config, PERF_COUNT_SW_PAGE_FAULTS, "event_config");
118 ASSERT_EQ(info.perf_event.event.cookie, PERF_EVENT_COOKIE, "event_cookie");
119 break;
120 default:
121 err = -1;
122 break;
123 }
124 return err;
125 }
126
kprobe_fill_invalid_user_buffer(int fd)127 static void kprobe_fill_invalid_user_buffer(int fd)
128 {
129 struct bpf_link_info info;
130 __u32 len = sizeof(info);
131 int err;
132
133 memset(&info, 0, sizeof(info));
134
135 info.perf_event.kprobe.func_name = 0x1; /* invalid address */
136 err = bpf_link_get_info_by_fd(fd, &info, &len);
137 ASSERT_EQ(err, -EINVAL, "invalid_buff_and_len");
138
139 info.perf_event.kprobe.name_len = 64;
140 err = bpf_link_get_info_by_fd(fd, &info, &len);
141 ASSERT_EQ(err, -EFAULT, "invalid_buff");
142
143 info.perf_event.kprobe.func_name = 0;
144 err = bpf_link_get_info_by_fd(fd, &info, &len);
145 ASSERT_EQ(err, -EINVAL, "invalid_len");
146
147 ASSERT_EQ(info.perf_event.kprobe.addr, 0, "func_addr");
148 ASSERT_EQ(info.perf_event.kprobe.offset, 0, "func_offset");
149 ASSERT_EQ(info.perf_event.type, 0, "type");
150 }
151
test_kprobe_fill_link_info(struct test_fill_link_info * skel,enum bpf_perf_event_type type,bool invalid)152 static void test_kprobe_fill_link_info(struct test_fill_link_info *skel,
153 enum bpf_perf_event_type type,
154 bool invalid)
155 {
156 DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts,
157 .attach_mode = PROBE_ATTACH_MODE_LINK,
158 .retprobe = type == BPF_PERF_EVENT_KRETPROBE,
159 .bpf_cookie = PERF_EVENT_COOKIE,
160 );
161 ssize_t entry_offset = 0;
162 struct bpf_link *link;
163 int link_fd, err;
164
165 link = bpf_program__attach_kprobe_opts(skel->progs.kprobe_run, KPROBE_FUNC, &opts);
166 if (!ASSERT_OK_PTR(link, "attach_kprobe"))
167 return;
168
169 link_fd = bpf_link__fd(link);
170 if (!invalid) {
171 /* See also arch_adjust_kprobe_addr(). */
172 if (skel->kconfig->CONFIG_X86_KERNEL_IBT)
173 entry_offset = 4;
174 err = verify_perf_link_info(link_fd, type, kprobe_addr, 0, entry_offset);
175 ASSERT_OK(err, "verify_perf_link_info");
176 } else {
177 kprobe_fill_invalid_user_buffer(link_fd);
178 }
179 bpf_link__destroy(link);
180 }
181
test_tp_fill_link_info(struct test_fill_link_info * skel)182 static void test_tp_fill_link_info(struct test_fill_link_info *skel)
183 {
184 DECLARE_LIBBPF_OPTS(bpf_tracepoint_opts, opts,
185 .bpf_cookie = PERF_EVENT_COOKIE,
186 );
187 struct bpf_link *link;
188 int link_fd, err;
189
190 link = bpf_program__attach_tracepoint_opts(skel->progs.tp_run, TP_CAT, TP_NAME, &opts);
191 if (!ASSERT_OK_PTR(link, "attach_tp"))
192 return;
193
194 link_fd = bpf_link__fd(link);
195 err = verify_perf_link_info(link_fd, BPF_PERF_EVENT_TRACEPOINT, 0, 0, 0);
196 ASSERT_OK(err, "verify_perf_link_info");
197 bpf_link__destroy(link);
198 }
199
test_event_fill_link_info(struct test_fill_link_info * skel)200 static void test_event_fill_link_info(struct test_fill_link_info *skel)
201 {
202 DECLARE_LIBBPF_OPTS(bpf_perf_event_opts, opts,
203 .bpf_cookie = PERF_EVENT_COOKIE,
204 );
205 struct bpf_link *link;
206 int link_fd, err, pfd;
207 struct perf_event_attr attr = {
208 .type = PERF_TYPE_SOFTWARE,
209 .config = PERF_COUNT_SW_PAGE_FAULTS,
210 .freq = 1,
211 .sample_freq = 1,
212 .size = sizeof(struct perf_event_attr),
213 };
214
215 pfd = syscall(__NR_perf_event_open, &attr, -1 /* pid */, 0 /* cpu 0 */,
216 -1 /* group id */, 0 /* flags */);
217 if (!ASSERT_GE(pfd, 0, "perf_event_open"))
218 return;
219
220 link = bpf_program__attach_perf_event_opts(skel->progs.event_run, pfd, &opts);
221 if (!ASSERT_OK_PTR(link, "attach_event"))
222 goto error;
223
224 link_fd = bpf_link__fd(link);
225 err = verify_perf_link_info(link_fd, BPF_PERF_EVENT_EVENT, 0, 0, 0);
226 ASSERT_OK(err, "verify_perf_link_info");
227 bpf_link__destroy(link);
228
229 error:
230 close(pfd);
231 }
232
test_uprobe_fill_link_info(struct test_fill_link_info * skel,enum bpf_perf_event_type type)233 static void test_uprobe_fill_link_info(struct test_fill_link_info *skel,
234 enum bpf_perf_event_type type)
235 {
236 DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts,
237 .retprobe = type == BPF_PERF_EVENT_URETPROBE,
238 .bpf_cookie = PERF_EVENT_COOKIE,
239 );
240 struct bpf_link *link;
241 int link_fd, err;
242
243 link = bpf_program__attach_uprobe_opts(skel->progs.uprobe_run,
244 0, /* self pid */
245 UPROBE_FILE, uprobe_offset,
246 &opts);
247 if (!ASSERT_OK_PTR(link, "attach_uprobe"))
248 return;
249
250 link_fd = bpf_link__fd(link);
251 err = verify_perf_link_info(link_fd, type, 0, uprobe_offset, 0);
252 ASSERT_OK(err, "verify_perf_link_info");
253 bpf_link__destroy(link);
254 }
255
verify_kmulti_link_info(int fd,bool retprobe,bool has_cookies)256 static int verify_kmulti_link_info(int fd, bool retprobe, bool has_cookies)
257 {
258 __u64 addrs[KMULTI_CNT], cookies[KMULTI_CNT];
259 struct bpf_link_info info;
260 __u32 len = sizeof(info);
261 int flags, i, err;
262
263 memset(&info, 0, sizeof(info));
264
265 again:
266 err = bpf_link_get_info_by_fd(fd, &info, &len);
267 if (!ASSERT_OK(err, "get_link_info"))
268 return -1;
269
270 if (!ASSERT_EQ(info.type, BPF_LINK_TYPE_KPROBE_MULTI, "kmulti_type"))
271 return -1;
272
273 ASSERT_EQ(info.kprobe_multi.count, KMULTI_CNT, "func_cnt");
274 flags = info.kprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN;
275 if (!retprobe)
276 ASSERT_EQ(flags, 0, "kmulti_flags");
277 else
278 ASSERT_NEQ(flags, 0, "kretmulti_flags");
279
280 if (!info.kprobe_multi.addrs) {
281 info.kprobe_multi.addrs = ptr_to_u64(addrs);
282 info.kprobe_multi.cookies = ptr_to_u64(cookies);
283 goto again;
284 }
285 for (i = 0; i < KMULTI_CNT; i++) {
286 ASSERT_EQ(addrs[i], kmulti_addrs[i], "kmulti_addrs");
287 ASSERT_EQ(cookies[i], has_cookies ? kmulti_cookies[i] : 0,
288 "kmulti_cookies_value");
289 }
290 return 0;
291 }
292
verify_kmulti_invalid_user_buffer(int fd)293 static void verify_kmulti_invalid_user_buffer(int fd)
294 {
295 __u64 addrs[KMULTI_CNT], cookies[KMULTI_CNT];
296 struct bpf_link_info info;
297 __u32 len = sizeof(info);
298 int err, i;
299
300 memset(&info, 0, sizeof(info));
301
302 info.kprobe_multi.count = KMULTI_CNT;
303 err = bpf_link_get_info_by_fd(fd, &info, &len);
304 ASSERT_EQ(err, -EINVAL, "no_addr");
305
306 info.kprobe_multi.addrs = ptr_to_u64(addrs);
307 info.kprobe_multi.count = 0;
308 err = bpf_link_get_info_by_fd(fd, &info, &len);
309 ASSERT_EQ(err, -EINVAL, "no_cnt");
310
311 for (i = 0; i < KMULTI_CNT; i++)
312 addrs[i] = 0;
313 info.kprobe_multi.count = KMULTI_CNT - 1;
314 err = bpf_link_get_info_by_fd(fd, &info, &len);
315 ASSERT_EQ(err, -ENOSPC, "smaller_cnt");
316 for (i = 0; i < KMULTI_CNT - 1; i++)
317 ASSERT_EQ(addrs[i], kmulti_addrs[i], "kmulti_addrs");
318 ASSERT_EQ(addrs[i], 0, "kmulti_addrs");
319
320 for (i = 0; i < KMULTI_CNT; i++)
321 addrs[i] = 0;
322 info.kprobe_multi.count = KMULTI_CNT + 1;
323 err = bpf_link_get_info_by_fd(fd, &info, &len);
324 ASSERT_EQ(err, 0, "bigger_cnt");
325 for (i = 0; i < KMULTI_CNT; i++)
326 ASSERT_EQ(addrs[i], kmulti_addrs[i], "kmulti_addrs");
327
328 info.kprobe_multi.count = KMULTI_CNT;
329 info.kprobe_multi.addrs = 0x1; /* invalid addr */
330 err = bpf_link_get_info_by_fd(fd, &info, &len);
331 ASSERT_EQ(err, -EFAULT, "invalid_buff_addrs");
332
333 info.kprobe_multi.count = KMULTI_CNT;
334 info.kprobe_multi.addrs = ptr_to_u64(addrs);
335 info.kprobe_multi.cookies = 0x1; /* invalid addr */
336 err = bpf_link_get_info_by_fd(fd, &info, &len);
337 ASSERT_EQ(err, -EFAULT, "invalid_buff_cookies");
338
339 /* cookies && !count */
340 info.kprobe_multi.count = 0;
341 info.kprobe_multi.addrs = ptr_to_u64(NULL);
342 info.kprobe_multi.cookies = ptr_to_u64(cookies);
343 err = bpf_link_get_info_by_fd(fd, &info, &len);
344 ASSERT_EQ(err, -EINVAL, "invalid_cookies_count");
345 }
346
symbols_cmp_r(const void * a,const void * b)347 static int symbols_cmp_r(const void *a, const void *b)
348 {
349 const char **str_a = (const char **) a;
350 const char **str_b = (const char **) b;
351
352 return strcmp(*str_a, *str_b);
353 }
354
test_kprobe_multi_fill_link_info(struct test_fill_link_info * skel,bool retprobe,bool cookies,bool invalid)355 static void test_kprobe_multi_fill_link_info(struct test_fill_link_info *skel,
356 bool retprobe, bool cookies,
357 bool invalid)
358 {
359 LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
360 struct bpf_link *link;
361 int link_fd, err;
362
363 opts.syms = kmulti_syms;
364 opts.cookies = cookies ? kmulti_cookies : NULL;
365 opts.cnt = KMULTI_CNT;
366 opts.retprobe = retprobe;
367 link = bpf_program__attach_kprobe_multi_opts(skel->progs.kmulti_run, NULL, &opts);
368 if (!ASSERT_OK_PTR(link, "attach_kprobe_multi"))
369 return;
370
371 link_fd = bpf_link__fd(link);
372 if (!invalid) {
373 err = verify_kmulti_link_info(link_fd, retprobe, cookies);
374 ASSERT_OK(err, "verify_kmulti_link_info");
375 } else {
376 verify_kmulti_invalid_user_buffer(link_fd);
377 }
378 bpf_link__destroy(link);
379 }
380
381 #define SEC(name) __attribute__((section(name), used))
382
383 static short uprobe_link_info_sema_1 SEC(".probes");
384 static short uprobe_link_info_sema_2 SEC(".probes");
385 static short uprobe_link_info_sema_3 SEC(".probes");
386
uprobe_link_info_func_1(void)387 noinline void uprobe_link_info_func_1(void)
388 {
389 asm volatile ("");
390 uprobe_link_info_sema_1++;
391 }
392
uprobe_link_info_func_2(void)393 noinline void uprobe_link_info_func_2(void)
394 {
395 asm volatile ("");
396 uprobe_link_info_sema_2++;
397 }
398
uprobe_link_info_func_3(void)399 noinline void uprobe_link_info_func_3(void)
400 {
401 asm volatile ("");
402 uprobe_link_info_sema_3++;
403 }
404
405 static int
verify_umulti_link_info(int fd,bool retprobe,__u64 * offsets,__u64 * cookies,__u64 * ref_ctr_offsets)406 verify_umulti_link_info(int fd, bool retprobe, __u64 *offsets,
407 __u64 *cookies, __u64 *ref_ctr_offsets)
408 {
409 char path[PATH_MAX], path_buf[PATH_MAX];
410 struct bpf_link_info info;
411 __u32 len = sizeof(info);
412 __u64 ref_ctr_offsets_buf[3];
413 __u64 offsets_buf[3];
414 __u64 cookies_buf[3];
415 int i, err, bit;
416 __u32 count = 0;
417
418 memset(path, 0, sizeof(path));
419 err = readlink("/proc/self/exe", path, sizeof(path));
420 if (!ASSERT_NEQ(err, -1, "readlink"))
421 return -1;
422
423 memset(&info, 0, sizeof(info));
424 err = bpf_link_get_info_by_fd(fd, &info, &len);
425 if (!ASSERT_OK(err, "bpf_link_get_info_by_fd"))
426 return -1;
427
428 ASSERT_EQ(info.uprobe_multi.count, 3, "info.uprobe_multi.count");
429 ASSERT_EQ(info.uprobe_multi.path_size, strlen(path) + 1,
430 "info.uprobe_multi.path_size");
431
432 for (bit = 0; bit < 8; bit++) {
433 memset(&info, 0, sizeof(info));
434 info.uprobe_multi.path = ptr_to_u64(path_buf);
435 info.uprobe_multi.path_size = sizeof(path_buf);
436 info.uprobe_multi.count = count;
437
438 if (bit & 0x1)
439 info.uprobe_multi.offsets = ptr_to_u64(offsets_buf);
440 if (bit & 0x2)
441 info.uprobe_multi.cookies = ptr_to_u64(cookies_buf);
442 if (bit & 0x4)
443 info.uprobe_multi.ref_ctr_offsets = ptr_to_u64(ref_ctr_offsets_buf);
444
445 err = bpf_link_get_info_by_fd(fd, &info, &len);
446 if (!ASSERT_OK(err, "bpf_link_get_info_by_fd"))
447 return -1;
448
449 if (!ASSERT_EQ(info.type, BPF_LINK_TYPE_UPROBE_MULTI, "info.type"))
450 return -1;
451
452 ASSERT_EQ(info.uprobe_multi.pid, getpid(), "info.uprobe_multi.pid");
453 ASSERT_EQ(info.uprobe_multi.count, 3, "info.uprobe_multi.count");
454 ASSERT_EQ(info.uprobe_multi.flags & BPF_F_KPROBE_MULTI_RETURN,
455 retprobe, "info.uprobe_multi.flags.retprobe");
456 ASSERT_EQ(info.uprobe_multi.path_size, strlen(path) + 1, "info.uprobe_multi.path_size");
457 ASSERT_STREQ(path_buf, path, "info.uprobe_multi.path");
458
459 for (i = 0; i < info.uprobe_multi.count; i++) {
460 if (info.uprobe_multi.offsets)
461 ASSERT_EQ(offsets_buf[i], offsets[i], "info.uprobe_multi.offsets");
462 if (info.uprobe_multi.cookies)
463 ASSERT_EQ(cookies_buf[i], cookies[i], "info.uprobe_multi.cookies");
464 if (info.uprobe_multi.ref_ctr_offsets) {
465 ASSERT_EQ(ref_ctr_offsets_buf[i], ref_ctr_offsets[i],
466 "info.uprobe_multi.ref_ctr_offsets");
467 }
468 }
469 count = count ?: info.uprobe_multi.count;
470 }
471
472 return 0;
473 }
474
verify_umulti_invalid_user_buffer(int fd)475 static void verify_umulti_invalid_user_buffer(int fd)
476 {
477 struct bpf_link_info info;
478 __u32 len = sizeof(info);
479 __u64 buf[3];
480 int err;
481
482 /* upath_size defined, not path */
483 memset(&info, 0, sizeof(info));
484 info.uprobe_multi.path_size = 3;
485 err = bpf_link_get_info_by_fd(fd, &info, &len);
486 ASSERT_EQ(err, -EINVAL, "failed_upath_size");
487
488 /* path defined, but small */
489 memset(&info, 0, sizeof(info));
490 info.uprobe_multi.path = ptr_to_u64(buf);
491 info.uprobe_multi.path_size = 3;
492 err = bpf_link_get_info_by_fd(fd, &info, &len);
493 ASSERT_LT(err, 0, "failed_upath_small");
494
495 /* path has wrong pointer */
496 memset(&info, 0, sizeof(info));
497 info.uprobe_multi.path_size = PATH_MAX;
498 info.uprobe_multi.path = 123;
499 err = bpf_link_get_info_by_fd(fd, &info, &len);
500 ASSERT_EQ(err, -EFAULT, "failed_bad_path_ptr");
501
502 /* count zero, with offsets */
503 memset(&info, 0, sizeof(info));
504 info.uprobe_multi.offsets = ptr_to_u64(buf);
505 err = bpf_link_get_info_by_fd(fd, &info, &len);
506 ASSERT_EQ(err, -EINVAL, "failed_count");
507
508 /* offsets not big enough */
509 memset(&info, 0, sizeof(info));
510 info.uprobe_multi.offsets = ptr_to_u64(buf);
511 info.uprobe_multi.count = 2;
512 err = bpf_link_get_info_by_fd(fd, &info, &len);
513 ASSERT_EQ(err, -ENOSPC, "failed_small_count");
514
515 /* offsets has wrong pointer */
516 memset(&info, 0, sizeof(info));
517 info.uprobe_multi.offsets = 123;
518 info.uprobe_multi.count = 3;
519 err = bpf_link_get_info_by_fd(fd, &info, &len);
520 ASSERT_EQ(err, -EFAULT, "failed_wrong_offsets");
521 }
522
test_uprobe_multi_fill_link_info(struct test_fill_link_info * skel,bool retprobe,bool invalid)523 static void test_uprobe_multi_fill_link_info(struct test_fill_link_info *skel,
524 bool retprobe, bool invalid)
525 {
526 LIBBPF_OPTS(bpf_uprobe_multi_opts, opts,
527 .retprobe = retprobe,
528 );
529 const char *syms[3] = {
530 "uprobe_link_info_func_1",
531 "uprobe_link_info_func_2",
532 "uprobe_link_info_func_3",
533 };
534 __u64 cookies[3] = {
535 0xdead,
536 0xbeef,
537 0xcafe,
538 };
539 const char *sema[3] = {
540 "uprobe_link_info_sema_1",
541 "uprobe_link_info_sema_2",
542 "uprobe_link_info_sema_3",
543 };
544 __u64 *offsets = NULL, *ref_ctr_offsets;
545 struct bpf_link *link;
546 int link_fd, err;
547
548 err = elf_resolve_syms_offsets("/proc/self/exe", 3, sema,
549 (unsigned long **) &ref_ctr_offsets, STT_OBJECT);
550 if (!ASSERT_OK(err, "elf_resolve_syms_offsets_object"))
551 return;
552
553 err = elf_resolve_syms_offsets("/proc/self/exe", 3, syms,
554 (unsigned long **) &offsets, STT_FUNC);
555 if (!ASSERT_OK(err, "elf_resolve_syms_offsets_func"))
556 goto out;
557
558 opts.syms = syms;
559 opts.cookies = &cookies[0];
560 opts.ref_ctr_offsets = (unsigned long *) &ref_ctr_offsets[0];
561 opts.cnt = ARRAY_SIZE(syms);
562
563 link = bpf_program__attach_uprobe_multi(skel->progs.umulti_run, 0,
564 "/proc/self/exe", NULL, &opts);
565 if (!ASSERT_OK_PTR(link, "bpf_program__attach_uprobe_multi"))
566 goto out;
567
568 link_fd = bpf_link__fd(link);
569 if (invalid)
570 verify_umulti_invalid_user_buffer(link_fd);
571 else
572 verify_umulti_link_info(link_fd, retprobe, offsets, cookies, ref_ctr_offsets);
573
574 bpf_link__destroy(link);
575 out:
576 free(ref_ctr_offsets);
577 free(offsets);
578 }
579
test_fill_link_info(void)580 void test_fill_link_info(void)
581 {
582 struct test_fill_link_info *skel;
583 int i;
584
585 skel = test_fill_link_info__open_and_load();
586 if (!ASSERT_OK_PTR(skel, "skel_open"))
587 return;
588
589 /* load kallsyms to compare the addr */
590 if (!ASSERT_OK(load_kallsyms(), "load_kallsyms"))
591 goto cleanup;
592
593 kprobe_addr = ksym_get_addr(KPROBE_FUNC);
594 if (test__start_subtest("kprobe_link_info"))
595 test_kprobe_fill_link_info(skel, BPF_PERF_EVENT_KPROBE, false);
596 if (test__start_subtest("kretprobe_link_info"))
597 test_kprobe_fill_link_info(skel, BPF_PERF_EVENT_KRETPROBE, false);
598 if (test__start_subtest("kprobe_invalid_ubuff"))
599 test_kprobe_fill_link_info(skel, BPF_PERF_EVENT_KPROBE, true);
600 if (test__start_subtest("tracepoint_link_info"))
601 test_tp_fill_link_info(skel);
602 if (test__start_subtest("event_link_info"))
603 test_event_fill_link_info(skel);
604
605 uprobe_offset = get_uprobe_offset(&uprobe_func);
606 if (test__start_subtest("uprobe_link_info"))
607 test_uprobe_fill_link_info(skel, BPF_PERF_EVENT_UPROBE);
608 if (test__start_subtest("uretprobe_link_info"))
609 test_uprobe_fill_link_info(skel, BPF_PERF_EVENT_URETPROBE);
610
611 qsort(kmulti_syms, KMULTI_CNT, sizeof(kmulti_syms[0]), symbols_cmp_r);
612 for (i = 0; i < KMULTI_CNT; i++)
613 kmulti_addrs[i] = ksym_get_addr(kmulti_syms[i]);
614 if (test__start_subtest("kprobe_multi_link_info")) {
615 test_kprobe_multi_fill_link_info(skel, false, false, false);
616 test_kprobe_multi_fill_link_info(skel, false, true, false);
617 }
618 if (test__start_subtest("kretprobe_multi_link_info")) {
619 test_kprobe_multi_fill_link_info(skel, true, false, false);
620 test_kprobe_multi_fill_link_info(skel, true, true, false);
621 }
622 if (test__start_subtest("kprobe_multi_invalid_ubuff"))
623 test_kprobe_multi_fill_link_info(skel, true, true, true);
624
625 if (test__start_subtest("uprobe_multi_link_info"))
626 test_uprobe_multi_fill_link_info(skel, false, false);
627 if (test__start_subtest("uretprobe_multi_link_info"))
628 test_uprobe_multi_fill_link_info(skel, true, false);
629 if (test__start_subtest("uprobe_multi_invalid"))
630 test_uprobe_multi_fill_link_info(skel, false, true);
631
632 cleanup:
633 test_fill_link_info__destroy(skel);
634 }
635