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