Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
8char _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
17void *user_ptr = 0;
18int pid = 0;
19
20static 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
50SEC("uprobe.session//proc/self/exe:uprobe_multi_func_*")
51int uprobe(struct pt_regs *ctx)
52{
53 return uprobe_multi_check(ctx, bpf_session_is_return(ctx));
54}
55
56static __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
64SEC("uprobe.session.s//proc/self/exe:uprobe_multi_func_*")
65int 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}