xref: /linux/tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "kprobe_multi.skel.h"
4 #include "trace_helpers.h"
5 #include "kprobe_multi_empty.skel.h"
6 #include "kprobe_multi_override.skel.h"
7 #include "kprobe_multi_session.skel.h"
8 #include "kprobe_multi_session_cookie.skel.h"
9 #include "kprobe_multi_verifier.skel.h"
10 #include "bpf/libbpf_internal.h"
11 #include "bpf/hashmap.h"
12 
13 static void kprobe_multi_test_run(struct kprobe_multi *skel, bool test_return)
14 {
15 	LIBBPF_OPTS(bpf_test_run_opts, topts);
16 	int err, prog_fd;
17 
18 	prog_fd = bpf_program__fd(skel->progs.trigger);
19 	err = bpf_prog_test_run_opts(prog_fd, &topts);
20 	ASSERT_OK(err, "test_run");
21 	ASSERT_EQ(topts.retval, 0, "test_run");
22 
23 	ASSERT_EQ(skel->bss->kprobe_test1_result, 1, "kprobe_test1_result");
24 	ASSERT_EQ(skel->bss->kprobe_test2_result, 1, "kprobe_test2_result");
25 	ASSERT_EQ(skel->bss->kprobe_test3_result, 1, "kprobe_test3_result");
26 	ASSERT_EQ(skel->bss->kprobe_test4_result, 1, "kprobe_test4_result");
27 	ASSERT_EQ(skel->bss->kprobe_test5_result, 1, "kprobe_test5_result");
28 	ASSERT_EQ(skel->bss->kprobe_test6_result, 1, "kprobe_test6_result");
29 	ASSERT_EQ(skel->bss->kprobe_test7_result, 1, "kprobe_test7_result");
30 	ASSERT_EQ(skel->bss->kprobe_test8_result, 1, "kprobe_test8_result");
31 
32 	if (test_return) {
33 		ASSERT_EQ(skel->bss->kretprobe_test1_result, 1, "kretprobe_test1_result");
34 		ASSERT_EQ(skel->bss->kretprobe_test2_result, 1, "kretprobe_test2_result");
35 		ASSERT_EQ(skel->bss->kretprobe_test3_result, 1, "kretprobe_test3_result");
36 		ASSERT_EQ(skel->bss->kretprobe_test4_result, 1, "kretprobe_test4_result");
37 		ASSERT_EQ(skel->bss->kretprobe_test5_result, 1, "kretprobe_test5_result");
38 		ASSERT_EQ(skel->bss->kretprobe_test6_result, 1, "kretprobe_test6_result");
39 		ASSERT_EQ(skel->bss->kretprobe_test7_result, 1, "kretprobe_test7_result");
40 		ASSERT_EQ(skel->bss->kretprobe_test8_result, 1, "kretprobe_test8_result");
41 	}
42 }
43 
44 static void test_skel_api(void)
45 {
46 	struct kprobe_multi *skel = NULL;
47 	int err;
48 
49 	skel = kprobe_multi__open_and_load();
50 	if (!ASSERT_OK_PTR(skel, "kprobe_multi__open_and_load"))
51 		goto cleanup;
52 
53 	skel->bss->pid = getpid();
54 	err = kprobe_multi__attach(skel);
55 	if (!ASSERT_OK(err, "kprobe_multi__attach"))
56 		goto cleanup;
57 
58 	kprobe_multi_test_run(skel, true);
59 
60 cleanup:
61 	kprobe_multi__destroy(skel);
62 }
63 
64 static void test_link_api(struct bpf_link_create_opts *opts)
65 {
66 	int prog_fd, link1_fd = -1, link2_fd = -1;
67 	struct kprobe_multi *skel = NULL;
68 
69 	skel = kprobe_multi__open_and_load();
70 	if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load"))
71 		goto cleanup;
72 
73 	skel->bss->pid = getpid();
74 	prog_fd = bpf_program__fd(skel->progs.test_kprobe);
75 	link1_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, opts);
76 	if (!ASSERT_GE(link1_fd, 0, "link_fd"))
77 		goto cleanup;
78 
79 	opts->kprobe_multi.flags = BPF_F_KPROBE_MULTI_RETURN;
80 	prog_fd = bpf_program__fd(skel->progs.test_kretprobe);
81 	link2_fd = bpf_link_create(prog_fd, 0, BPF_TRACE_KPROBE_MULTI, opts);
82 	if (!ASSERT_GE(link2_fd, 0, "link_fd"))
83 		goto cleanup;
84 
85 	kprobe_multi_test_run(skel, true);
86 
87 cleanup:
88 	if (link1_fd != -1)
89 		close(link1_fd);
90 	if (link2_fd != -1)
91 		close(link2_fd);
92 	kprobe_multi__destroy(skel);
93 }
94 
95 #define GET_ADDR(__sym, __addr) ({					\
96 	__addr = ksym_get_addr(__sym);					\
97 	if (!ASSERT_NEQ(__addr, 0, "kallsyms load failed for " #__sym))	\
98 		return;							\
99 })
100 
101 static void test_link_api_addrs(void)
102 {
103 	LIBBPF_OPTS(bpf_link_create_opts, opts);
104 	unsigned long long addrs[8];
105 
106 	GET_ADDR("bpf_fentry_test1", addrs[0]);
107 	GET_ADDR("bpf_fentry_test2", addrs[1]);
108 	GET_ADDR("bpf_fentry_test3", addrs[2]);
109 	GET_ADDR("bpf_fentry_test4", addrs[3]);
110 	GET_ADDR("bpf_fentry_test5", addrs[4]);
111 	GET_ADDR("bpf_fentry_test6", addrs[5]);
112 	GET_ADDR("bpf_fentry_test7", addrs[6]);
113 	GET_ADDR("bpf_fentry_test8", addrs[7]);
114 
115 	opts.kprobe_multi.addrs = (const unsigned long*) addrs;
116 	opts.kprobe_multi.cnt = ARRAY_SIZE(addrs);
117 	test_link_api(&opts);
118 }
119 
120 static void test_link_api_syms(void)
121 {
122 	LIBBPF_OPTS(bpf_link_create_opts, opts);
123 	const char *syms[8] = {
124 		"bpf_fentry_test1",
125 		"bpf_fentry_test2",
126 		"bpf_fentry_test3",
127 		"bpf_fentry_test4",
128 		"bpf_fentry_test5",
129 		"bpf_fentry_test6",
130 		"bpf_fentry_test7",
131 		"bpf_fentry_test8",
132 	};
133 
134 	opts.kprobe_multi.syms = syms;
135 	opts.kprobe_multi.cnt = ARRAY_SIZE(syms);
136 	test_link_api(&opts);
137 }
138 
139 static void
140 test_attach_api(const char *pattern, struct bpf_kprobe_multi_opts *opts)
141 {
142 	struct bpf_link *link1 = NULL, *link2 = NULL;
143 	struct kprobe_multi *skel = NULL;
144 
145 	skel = kprobe_multi__open_and_load();
146 	if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load"))
147 		goto cleanup;
148 
149 	skel->bss->pid = getpid();
150 	link1 = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
151 						      pattern, opts);
152 	if (!ASSERT_OK_PTR(link1, "bpf_program__attach_kprobe_multi_opts"))
153 		goto cleanup;
154 
155 	if (opts) {
156 		opts->retprobe = true;
157 		link2 = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kretprobe_manual,
158 							      pattern, opts);
159 		if (!ASSERT_OK_PTR(link2, "bpf_program__attach_kprobe_multi_opts"))
160 			goto cleanup;
161 	}
162 
163 	kprobe_multi_test_run(skel, !!opts);
164 
165 cleanup:
166 	bpf_link__destroy(link2);
167 	bpf_link__destroy(link1);
168 	kprobe_multi__destroy(skel);
169 }
170 
171 static void test_attach_api_pattern(void)
172 {
173 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
174 
175 	test_attach_api("bpf_fentry_test*", &opts);
176 	test_attach_api("bpf_fentry_test?", NULL);
177 }
178 
179 static void test_attach_api_addrs(void)
180 {
181 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
182 	unsigned long long addrs[8];
183 
184 	GET_ADDR("bpf_fentry_test1", addrs[0]);
185 	GET_ADDR("bpf_fentry_test2", addrs[1]);
186 	GET_ADDR("bpf_fentry_test3", addrs[2]);
187 	GET_ADDR("bpf_fentry_test4", addrs[3]);
188 	GET_ADDR("bpf_fentry_test5", addrs[4]);
189 	GET_ADDR("bpf_fentry_test6", addrs[5]);
190 	GET_ADDR("bpf_fentry_test7", addrs[6]);
191 	GET_ADDR("bpf_fentry_test8", addrs[7]);
192 
193 	opts.addrs = (const unsigned long *) addrs;
194 	opts.cnt = ARRAY_SIZE(addrs);
195 	test_attach_api(NULL, &opts);
196 }
197 
198 static void test_attach_api_syms(void)
199 {
200 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
201 	const char *syms[8] = {
202 		"bpf_fentry_test1",
203 		"bpf_fentry_test2",
204 		"bpf_fentry_test3",
205 		"bpf_fentry_test4",
206 		"bpf_fentry_test5",
207 		"bpf_fentry_test6",
208 		"bpf_fentry_test7",
209 		"bpf_fentry_test8",
210 	};
211 
212 	opts.syms = syms;
213 	opts.cnt = ARRAY_SIZE(syms);
214 	test_attach_api(NULL, &opts);
215 }
216 
217 static void test_attach_api_fails(void)
218 {
219 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
220 	struct kprobe_multi *skel = NULL;
221 	struct bpf_link *link = NULL;
222 	unsigned long long addrs[2];
223 	const char *syms[2] = {
224 		"bpf_fentry_test1",
225 		"bpf_fentry_test2",
226 	};
227 	__u64 cookies[2];
228 	int saved_error;
229 
230 	addrs[0] = ksym_get_addr("bpf_fentry_test1");
231 	addrs[1] = ksym_get_addr("bpf_fentry_test2");
232 
233 	if (!ASSERT_FALSE(!addrs[0] || !addrs[1], "ksym_get_addr"))
234 		goto cleanup;
235 
236 	skel = kprobe_multi__open_and_load();
237 	if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load"))
238 		goto cleanup;
239 
240 	skel->bss->pid = getpid();
241 
242 	/* fail_1 - pattern and opts NULL */
243 	link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
244 						     NULL, NULL);
245 	saved_error = -errno;
246 	if (!ASSERT_ERR_PTR(link, "fail_1"))
247 		goto cleanup;
248 
249 	if (!ASSERT_EQ(saved_error, -EINVAL, "fail_1_error"))
250 		goto cleanup;
251 
252 	/* fail_2 - both addrs and syms set */
253 	opts.addrs = (const unsigned long *) addrs;
254 	opts.syms = syms;
255 	opts.cnt = ARRAY_SIZE(syms);
256 	opts.cookies = NULL;
257 
258 	link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
259 						     NULL, &opts);
260 	saved_error = -errno;
261 	if (!ASSERT_ERR_PTR(link, "fail_2"))
262 		goto cleanup;
263 
264 	if (!ASSERT_EQ(saved_error, -EINVAL, "fail_2_error"))
265 		goto cleanup;
266 
267 	/* fail_3 - pattern and addrs set */
268 	opts.addrs = (const unsigned long *) addrs;
269 	opts.syms = NULL;
270 	opts.cnt = ARRAY_SIZE(syms);
271 	opts.cookies = NULL;
272 
273 	link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
274 						     "ksys_*", &opts);
275 	saved_error = -errno;
276 	if (!ASSERT_ERR_PTR(link, "fail_3"))
277 		goto cleanup;
278 
279 	if (!ASSERT_EQ(saved_error, -EINVAL, "fail_3_error"))
280 		goto cleanup;
281 
282 	/* fail_4 - pattern and cnt set */
283 	opts.addrs = NULL;
284 	opts.syms = NULL;
285 	opts.cnt = ARRAY_SIZE(syms);
286 	opts.cookies = NULL;
287 
288 	link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
289 						     "ksys_*", &opts);
290 	saved_error = -errno;
291 	if (!ASSERT_ERR_PTR(link, "fail_4"))
292 		goto cleanup;
293 
294 	if (!ASSERT_EQ(saved_error, -EINVAL, "fail_4_error"))
295 		goto cleanup;
296 
297 	/* fail_5 - pattern and cookies */
298 	opts.addrs = NULL;
299 	opts.syms = NULL;
300 	opts.cnt = 0;
301 	opts.cookies = cookies;
302 
303 	link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
304 						     "ksys_*", &opts);
305 	saved_error = -errno;
306 	if (!ASSERT_ERR_PTR(link, "fail_5"))
307 		goto cleanup;
308 
309 	if (!ASSERT_EQ(saved_error, -EINVAL, "fail_5_error"))
310 		goto cleanup;
311 
312 	/* fail_6 - abnormal cnt */
313 	opts.addrs = (const unsigned long *) addrs;
314 	opts.syms = NULL;
315 	opts.cnt = INT_MAX;
316 	opts.cookies = NULL;
317 
318 	link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
319 						     NULL, &opts);
320 	saved_error = -errno;
321 	if (!ASSERT_ERR_PTR(link, "fail_6"))
322 		goto cleanup;
323 
324 	if (!ASSERT_EQ(saved_error, -E2BIG, "fail_6_error"))
325 		goto cleanup;
326 
327 cleanup:
328 	bpf_link__destroy(link);
329 	kprobe_multi__destroy(skel);
330 }
331 
332 static void test_session_skel_api(void)
333 {
334 	struct kprobe_multi_session *skel = NULL;
335 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
336 	LIBBPF_OPTS(bpf_test_run_opts, topts);
337 	struct bpf_link *link = NULL;
338 	int i, err, prog_fd;
339 
340 	skel = kprobe_multi_session__open_and_load();
341 	if (!ASSERT_OK_PTR(skel, "kprobe_multi_session__open_and_load"))
342 		return;
343 
344 	skel->bss->pid = getpid();
345 
346 	err = kprobe_multi_session__attach(skel);
347 	if (!ASSERT_OK(err, " kprobe_multi_session__attach"))
348 		goto cleanup;
349 
350 	prog_fd = bpf_program__fd(skel->progs.trigger);
351 	err = bpf_prog_test_run_opts(prog_fd, &topts);
352 	ASSERT_OK(err, "test_run");
353 	ASSERT_EQ(topts.retval, 0, "test_run");
354 
355 	/* bpf_fentry_test1-4 trigger return probe, result is 2 */
356 	for (i = 0; i < 4; i++)
357 		ASSERT_EQ(skel->bss->kprobe_session_result[i], 2, "kprobe_session_result");
358 
359 	/* bpf_fentry_test5-8 trigger only entry probe, result is 1 */
360 	for (i = 4; i < 8; i++)
361 		ASSERT_EQ(skel->bss->kprobe_session_result[i], 1, "kprobe_session_result");
362 
363 cleanup:
364 	bpf_link__destroy(link);
365 	kprobe_multi_session__destroy(skel);
366 }
367 
368 static void test_session_cookie_skel_api(void)
369 {
370 	struct kprobe_multi_session_cookie *skel = NULL;
371 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
372 	LIBBPF_OPTS(bpf_test_run_opts, topts);
373 	struct bpf_link *link = NULL;
374 	int err, prog_fd;
375 
376 	skel = kprobe_multi_session_cookie__open_and_load();
377 	if (!ASSERT_OK_PTR(skel, "fentry_raw_skel_load"))
378 		return;
379 
380 	skel->bss->pid = getpid();
381 
382 	err = kprobe_multi_session_cookie__attach(skel);
383 	if (!ASSERT_OK(err, " kprobe_multi_wrapper__attach"))
384 		goto cleanup;
385 
386 	prog_fd = bpf_program__fd(skel->progs.trigger);
387 	err = bpf_prog_test_run_opts(prog_fd, &topts);
388 	ASSERT_OK(err, "test_run");
389 	ASSERT_EQ(topts.retval, 0, "test_run");
390 
391 	ASSERT_EQ(skel->bss->test_kprobe_1_result, 1, "test_kprobe_1_result");
392 	ASSERT_EQ(skel->bss->test_kprobe_2_result, 2, "test_kprobe_2_result");
393 	ASSERT_EQ(skel->bss->test_kprobe_3_result, 3, "test_kprobe_3_result");
394 
395 cleanup:
396 	bpf_link__destroy(link);
397 	kprobe_multi_session_cookie__destroy(skel);
398 }
399 
400 static size_t symbol_hash(long key, void *ctx __maybe_unused)
401 {
402 	return str_hash((const char *) key);
403 }
404 
405 static bool symbol_equal(long key1, long key2, void *ctx __maybe_unused)
406 {
407 	return strcmp((const char *) key1, (const char *) key2) == 0;
408 }
409 
410 static bool is_invalid_entry(char *buf, bool kernel)
411 {
412 	if (kernel && strchr(buf, '['))
413 		return true;
414 	if (!kernel && !strchr(buf, '['))
415 		return true;
416 	return false;
417 }
418 
419 static bool skip_entry(char *name)
420 {
421 	/*
422 	 * We attach to almost all kernel functions and some of them
423 	 * will cause 'suspicious RCU usage' when fprobe is attached
424 	 * to them. Filter out the current culprits - arch_cpu_idle
425 	 * default_idle and rcu_* functions.
426 	 */
427 	if (!strcmp(name, "arch_cpu_idle"))
428 		return true;
429 	if (!strcmp(name, "default_idle"))
430 		return true;
431 	if (!strncmp(name, "rcu_", 4))
432 		return true;
433 	if (!strcmp(name, "bpf_dispatcher_xdp_func"))
434 		return true;
435 	if (!strncmp(name, "__ftrace_invalid_address__",
436 		     sizeof("__ftrace_invalid_address__") - 1))
437 		return true;
438 	return false;
439 }
440 
441 /* Do comparision by ignoring '.llvm.<hash>' suffixes. */
442 static int compare_name(const char *name1, const char *name2)
443 {
444 	const char *res1, *res2;
445 	int len1, len2;
446 
447 	res1 = strstr(name1, ".llvm.");
448 	res2 = strstr(name2, ".llvm.");
449 	len1 = res1 ? res1 - name1 : strlen(name1);
450 	len2 = res2 ? res2 - name2 : strlen(name2);
451 
452 	if (len1 == len2)
453 		return strncmp(name1, name2, len1);
454 	if (len1 < len2)
455 		return strncmp(name1, name2, len1) <= 0 ? -1 : 1;
456 	return strncmp(name1, name2, len2) >= 0 ? 1 : -1;
457 }
458 
459 static int load_kallsyms_compare(const void *p1, const void *p2)
460 {
461 	return compare_name(((const struct ksym *)p1)->name, ((const struct ksym *)p2)->name);
462 }
463 
464 static int search_kallsyms_compare(const void *p1, const struct ksym *p2)
465 {
466 	return compare_name(p1, p2->name);
467 }
468 
469 static int get_syms(char ***symsp, size_t *cntp, bool kernel)
470 {
471 	size_t cap = 0, cnt = 0;
472 	char *name = NULL, *ksym_name, **syms = NULL;
473 	struct hashmap *map;
474 	struct ksyms *ksyms;
475 	struct ksym *ks;
476 	char buf[256];
477 	FILE *f;
478 	int err = 0;
479 
480 	ksyms = load_kallsyms_custom_local(load_kallsyms_compare);
481 	if (!ASSERT_OK_PTR(ksyms, "load_kallsyms_custom_local"))
482 		return -EINVAL;
483 
484 	/*
485 	 * The available_filter_functions contains many duplicates,
486 	 * but other than that all symbols are usable in kprobe multi
487 	 * interface.
488 	 * Filtering out duplicates by using hashmap__add, which won't
489 	 * add existing entry.
490 	 */
491 
492 	if (access("/sys/kernel/tracing/trace", F_OK) == 0)
493 		f = fopen("/sys/kernel/tracing/available_filter_functions", "r");
494 	else
495 		f = fopen("/sys/kernel/debug/tracing/available_filter_functions", "r");
496 
497 	if (!f)
498 		return -EINVAL;
499 
500 	map = hashmap__new(symbol_hash, symbol_equal, NULL);
501 	if (IS_ERR(map)) {
502 		err = libbpf_get_error(map);
503 		goto error;
504 	}
505 
506 	while (fgets(buf, sizeof(buf), f)) {
507 		if (is_invalid_entry(buf, kernel))
508 			continue;
509 
510 		free(name);
511 		if (sscanf(buf, "%ms$*[^\n]\n", &name) != 1)
512 			continue;
513 		if (skip_entry(name))
514 			continue;
515 
516 		ks = search_kallsyms_custom_local(ksyms, name, search_kallsyms_compare);
517 		if (!ks) {
518 			err = -EINVAL;
519 			goto error;
520 		}
521 
522 		ksym_name = ks->name;
523 		err = hashmap__add(map, ksym_name, 0);
524 		if (err == -EEXIST) {
525 			err = 0;
526 			continue;
527 		}
528 		if (err)
529 			goto error;
530 
531 		err = libbpf_ensure_mem((void **) &syms, &cap,
532 					sizeof(*syms), cnt + 1);
533 		if (err)
534 			goto error;
535 
536 		syms[cnt++] = ksym_name;
537 	}
538 
539 	*symsp = syms;
540 	*cntp = cnt;
541 
542 error:
543 	free(name);
544 	fclose(f);
545 	hashmap__free(map);
546 	if (err)
547 		free(syms);
548 	return err;
549 }
550 
551 static int get_addrs(unsigned long **addrsp, size_t *cntp, bool kernel)
552 {
553 	unsigned long *addr, *addrs, *tmp_addrs;
554 	int err = 0, max_cnt, inc_cnt;
555 	char *name = NULL;
556 	size_t cnt = 0;
557 	char buf[256];
558 	FILE *f;
559 
560 	if (access("/sys/kernel/tracing/trace", F_OK) == 0)
561 		f = fopen("/sys/kernel/tracing/available_filter_functions_addrs", "r");
562 	else
563 		f = fopen("/sys/kernel/debug/tracing/available_filter_functions_addrs", "r");
564 
565 	if (!f)
566 		return -ENOENT;
567 
568 	/* In my local setup, the number of entries is 50k+ so Let us initially
569 	 * allocate space to hold 64k entries. If 64k is not enough, incrementally
570 	 * increase 1k each time.
571 	 */
572 	max_cnt = 65536;
573 	inc_cnt = 1024;
574 	addrs = malloc(max_cnt * sizeof(long));
575 	if (addrs == NULL) {
576 		err = -ENOMEM;
577 		goto error;
578 	}
579 
580 	while (fgets(buf, sizeof(buf), f)) {
581 		if (is_invalid_entry(buf, kernel))
582 			continue;
583 
584 		free(name);
585 		if (sscanf(buf, "%p %ms$*[^\n]\n", &addr, &name) != 2)
586 			continue;
587 		if (skip_entry(name))
588 			continue;
589 
590 		if (cnt == max_cnt) {
591 			max_cnt += inc_cnt;
592 			tmp_addrs = realloc(addrs, max_cnt);
593 			if (!tmp_addrs) {
594 				err = -ENOMEM;
595 				goto error;
596 			}
597 			addrs = tmp_addrs;
598 		}
599 
600 		addrs[cnt++] = (unsigned long)addr;
601 	}
602 
603 	*addrsp = addrs;
604 	*cntp = cnt;
605 
606 error:
607 	free(name);
608 	fclose(f);
609 	if (err)
610 		free(addrs);
611 	return err;
612 }
613 
614 static void do_bench_test(struct kprobe_multi_empty *skel, struct bpf_kprobe_multi_opts *opts)
615 {
616 	long attach_start_ns, attach_end_ns;
617 	long detach_start_ns, detach_end_ns;
618 	double attach_delta, detach_delta;
619 	struct bpf_link *link = NULL;
620 
621 	attach_start_ns = get_time_ns();
622 	link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_empty,
623 						     NULL, opts);
624 	attach_end_ns = get_time_ns();
625 
626 	if (!ASSERT_OK_PTR(link, "bpf_program__attach_kprobe_multi_opts"))
627 		return;
628 
629 	detach_start_ns = get_time_ns();
630 	bpf_link__destroy(link);
631 	detach_end_ns = get_time_ns();
632 
633 	attach_delta = (attach_end_ns - attach_start_ns) / 1000000000.0;
634 	detach_delta = (detach_end_ns - detach_start_ns) / 1000000000.0;
635 
636 	printf("%s: found %lu functions\n", __func__, opts->cnt);
637 	printf("%s: attached in %7.3lfs\n", __func__, attach_delta);
638 	printf("%s: detached in %7.3lfs\n", __func__, detach_delta);
639 }
640 
641 static void test_kprobe_multi_bench_attach(bool kernel)
642 {
643 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
644 	struct kprobe_multi_empty *skel = NULL;
645 	char **syms = NULL;
646 	size_t cnt = 0;
647 
648 	if (!ASSERT_OK(get_syms(&syms, &cnt, kernel), "get_syms"))
649 		return;
650 
651 	skel = kprobe_multi_empty__open_and_load();
652 	if (!ASSERT_OK_PTR(skel, "kprobe_multi_empty__open_and_load"))
653 		goto cleanup;
654 
655 	opts.syms = (const char **) syms;
656 	opts.cnt = cnt;
657 
658 	do_bench_test(skel, &opts);
659 
660 cleanup:
661 	kprobe_multi_empty__destroy(skel);
662 	if (syms)
663 		free(syms);
664 }
665 
666 static void test_kprobe_multi_bench_attach_addr(bool kernel)
667 {
668 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
669 	struct kprobe_multi_empty *skel = NULL;
670 	unsigned long *addrs = NULL;
671 	size_t cnt = 0;
672 	int err;
673 
674 	err = get_addrs(&addrs, &cnt, kernel);
675 	if (err == -ENOENT) {
676 		test__skip();
677 		return;
678 	}
679 
680 	if (!ASSERT_OK(err, "get_addrs"))
681 		return;
682 
683 	skel = kprobe_multi_empty__open_and_load();
684 	if (!ASSERT_OK_PTR(skel, "kprobe_multi_empty__open_and_load"))
685 		goto cleanup;
686 
687 	opts.addrs = addrs;
688 	opts.cnt = cnt;
689 
690 	do_bench_test(skel, &opts);
691 
692 cleanup:
693 	kprobe_multi_empty__destroy(skel);
694 	free(addrs);
695 }
696 
697 static void test_attach_override(void)
698 {
699 	struct kprobe_multi_override *skel = NULL;
700 	struct bpf_link *link = NULL;
701 
702 	skel = kprobe_multi_override__open_and_load();
703 	if (!ASSERT_OK_PTR(skel, "kprobe_multi_empty__open_and_load"))
704 		goto cleanup;
705 
706 	/* The test_override calls bpf_override_return so it should fail
707 	 * to attach to bpf_fentry_test1 function, which is not on error
708 	 * injection list.
709 	 */
710 	link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_override,
711 						     "bpf_fentry_test1", NULL);
712 	if (!ASSERT_ERR_PTR(link, "override_attached_bpf_fentry_test1")) {
713 		bpf_link__destroy(link);
714 		goto cleanup;
715 	}
716 
717 	/* The should_fail_bio function is on error injection list,
718 	 * attach should succeed.
719 	 */
720 	link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_override,
721 						     "should_fail_bio", NULL);
722 	if (!ASSERT_OK_PTR(link, "override_attached_should_fail_bio"))
723 		goto cleanup;
724 
725 	bpf_link__destroy(link);
726 
727 cleanup:
728 	kprobe_multi_override__destroy(skel);
729 }
730 
731 void serial_test_kprobe_multi_bench_attach(void)
732 {
733 	if (test__start_subtest("kernel"))
734 		test_kprobe_multi_bench_attach(true);
735 	if (test__start_subtest("modules"))
736 		test_kprobe_multi_bench_attach(false);
737 	if (test__start_subtest("kernel"))
738 		test_kprobe_multi_bench_attach_addr(true);
739 	if (test__start_subtest("modules"))
740 		test_kprobe_multi_bench_attach_addr(false);
741 }
742 
743 void test_kprobe_multi_test(void)
744 {
745 	if (!ASSERT_OK(load_kallsyms(), "load_kallsyms"))
746 		return;
747 
748 	if (test__start_subtest("skel_api"))
749 		test_skel_api();
750 	if (test__start_subtest("link_api_addrs"))
751 		test_link_api_syms();
752 	if (test__start_subtest("link_api_syms"))
753 		test_link_api_addrs();
754 	if (test__start_subtest("attach_api_pattern"))
755 		test_attach_api_pattern();
756 	if (test__start_subtest("attach_api_addrs"))
757 		test_attach_api_addrs();
758 	if (test__start_subtest("attach_api_syms"))
759 		test_attach_api_syms();
760 	if (test__start_subtest("attach_api_fails"))
761 		test_attach_api_fails();
762 	if (test__start_subtest("attach_override"))
763 		test_attach_override();
764 	if (test__start_subtest("session"))
765 		test_session_skel_api();
766 	if (test__start_subtest("session_cookie"))
767 		test_session_cookie_skel_api();
768 	RUN_TESTS(kprobe_multi_verifier);
769 }
770