xref: /linux/tools/testing/selftests/bpf/progs/uprobe_multi_session.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <vmlinux.h>
3 #include <bpf/bpf_helpers.h>
4 #include <bpf/bpf_tracing.h>
5 #include <stdbool.h>
6 #include "bpf_misc.h"
7 
8 char _license[] SEC("license") = "GPL";
9 
10 __u64 uprobe_multi_func_1_addr = 0;
11 __u64 uprobe_multi_func_2_addr = 0;
12 __u64 uprobe_multi_func_3_addr = 0;
13 
14 __u64 uprobe_session_result[3] = {};
15 __u64 uprobe_multi_sleep_result = 0;
16 
17 void *user_ptr = 0;
18 int pid = 0;
19 
20 static int uprobe_multi_check(void *ctx, bool is_return)
21 {
22 	const __u64 funcs[] = {
23 		uprobe_multi_func_1_addr,
24 		uprobe_multi_func_2_addr,
25 		uprobe_multi_func_3_addr,
26 	};
27 	unsigned int i;
28 	__u64 addr;
29 
30 	if (bpf_get_current_pid_tgid() >> 32 != pid)
31 		return 1;
32 
33 	addr = bpf_get_func_ip(ctx);
34 
35 	for (i = 0; i < ARRAY_SIZE(funcs); i++) {
36 		if (funcs[i] == addr) {
37 			uprobe_session_result[i]++;
38 			break;
39 		}
40 	}
41 
42 	/* only uprobe_multi_func_2 executes return probe */
43 	if ((addr == uprobe_multi_func_1_addr) ||
44 	    (addr == uprobe_multi_func_3_addr))
45 		return 1;
46 
47 	return 0;
48 }
49 
50 SEC("uprobe.session//proc/self/exe:uprobe_multi_func_*")
51 int uprobe(struct pt_regs *ctx)
52 {
53 	return uprobe_multi_check(ctx, bpf_session_is_return(ctx));
54 }
55 
56 static __always_inline bool verify_sleepable_user_copy(void)
57 {
58 	char data[9];
59 
60 	bpf_copy_from_user(data, sizeof(data), user_ptr);
61 	return bpf_strncmp(data, sizeof(data), "test_data") == 0;
62 }
63 
64 SEC("uprobe.session.s//proc/self/exe:uprobe_multi_func_*")
65 int uprobe_sleepable(struct pt_regs *ctx)
66 {
67 	if (verify_sleepable_user_copy())
68 		uprobe_multi_sleep_result++;
69 	return uprobe_multi_check(ctx, bpf_session_is_return(ctx));
70 }
71