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
7 char _license[] SEC("license") = "GPL";
8
9 extern const void bpf_fentry_test1 __ksym;
10 extern const void bpf_fentry_test2 __ksym;
11 extern const void bpf_fentry_test3 __ksym;
12 extern const void bpf_fentry_test4 __ksym;
13 extern const void bpf_fentry_test5 __ksym;
14 extern const void bpf_fentry_test6 __ksym;
15 extern const void bpf_fentry_test7 __ksym;
16 extern const void bpf_fentry_test8 __ksym;
17
18 int pid = 0;
19 bool test_cookie = false;
20
21 __u64 kprobe_test1_result = 0;
22 __u64 kprobe_test2_result = 0;
23 __u64 kprobe_test3_result = 0;
24 __u64 kprobe_test4_result = 0;
25 __u64 kprobe_test5_result = 0;
26 __u64 kprobe_test6_result = 0;
27 __u64 kprobe_test7_result = 0;
28 __u64 kprobe_test8_result = 0;
29
30 __u64 kretprobe_test1_result = 0;
31 __u64 kretprobe_test2_result = 0;
32 __u64 kretprobe_test3_result = 0;
33 __u64 kretprobe_test4_result = 0;
34 __u64 kretprobe_test5_result = 0;
35 __u64 kretprobe_test6_result = 0;
36 __u64 kretprobe_test7_result = 0;
37 __u64 kretprobe_test8_result = 0;
38
kprobe_multi_check(void * ctx,bool is_return)39 static void kprobe_multi_check(void *ctx, bool is_return)
40 {
41 if (bpf_get_current_pid_tgid() >> 32 != pid)
42 return;
43
44 __u64 cookie = test_cookie ? bpf_get_attach_cookie(ctx) : 0;
45 __u64 addr = bpf_get_func_ip(ctx);
46
47 #define SET(__var, __addr, __cookie) ({ \
48 if (((const void *) addr == __addr) && \
49 (!test_cookie || (cookie == __cookie))) \
50 __var = 1; \
51 })
52
53 if (is_return) {
54 SET(kretprobe_test1_result, &bpf_fentry_test1, 8);
55 SET(kretprobe_test2_result, &bpf_fentry_test2, 2);
56 SET(kretprobe_test3_result, &bpf_fentry_test3, 7);
57 SET(kretprobe_test4_result, &bpf_fentry_test4, 6);
58 SET(kretprobe_test5_result, &bpf_fentry_test5, 5);
59 SET(kretprobe_test6_result, &bpf_fentry_test6, 4);
60 SET(kretprobe_test7_result, &bpf_fentry_test7, 3);
61 SET(kretprobe_test8_result, &bpf_fentry_test8, 1);
62 } else {
63 SET(kprobe_test1_result, &bpf_fentry_test1, 1);
64 SET(kprobe_test2_result, &bpf_fentry_test2, 7);
65 SET(kprobe_test3_result, &bpf_fentry_test3, 2);
66 SET(kprobe_test4_result, &bpf_fentry_test4, 3);
67 SET(kprobe_test5_result, &bpf_fentry_test5, 4);
68 SET(kprobe_test6_result, &bpf_fentry_test6, 5);
69 SET(kprobe_test7_result, &bpf_fentry_test7, 6);
70 SET(kprobe_test8_result, &bpf_fentry_test8, 8);
71 }
72
73 #undef SET
74 }
75
76 /*
77 * No tests in here, just to trigger 'bpf_fentry_test*'
78 * through tracing test_run
79 */
80 SEC("fentry/bpf_modify_return_test")
BPF_PROG(trigger)81 int BPF_PROG(trigger)
82 {
83 return 0;
84 }
85
86 SEC("kprobe.multi/bpf_fentry_tes??")
test_kprobe(struct pt_regs * ctx)87 int test_kprobe(struct pt_regs *ctx)
88 {
89 kprobe_multi_check(ctx, false);
90 return 0;
91 }
92
93 SEC("kretprobe.multi/bpf_fentry_test*")
test_kretprobe(struct pt_regs * ctx)94 int test_kretprobe(struct pt_regs *ctx)
95 {
96 kprobe_multi_check(ctx, true);
97 return 0;
98 }
99
100 SEC("kprobe.multi")
test_kprobe_manual(struct pt_regs * ctx)101 int test_kprobe_manual(struct pt_regs *ctx)
102 {
103 kprobe_multi_check(ctx, false);
104 return 0;
105 }
106
107 SEC("kretprobe.multi")
test_kretprobe_manual(struct pt_regs * ctx)108 int test_kretprobe_manual(struct pt_regs *ctx)
109 {
110 kprobe_multi_check(ctx, true);
111 return 0;
112 }
113
114 extern const void bpf_testmod_fentry_test1 __ksym;
115 extern const void bpf_testmod_fentry_test2 __ksym;
116 extern const void bpf_testmod_fentry_test3 __ksym;
117
118 __u64 kprobe_testmod_test1_result = 0;
119 __u64 kprobe_testmod_test2_result = 0;
120 __u64 kprobe_testmod_test3_result = 0;
121
122 __u64 kretprobe_testmod_test1_result = 0;
123 __u64 kretprobe_testmod_test2_result = 0;
124 __u64 kretprobe_testmod_test3_result = 0;
125
kprobe_multi_testmod_check(void * ctx,bool is_return)126 static void kprobe_multi_testmod_check(void *ctx, bool is_return)
127 {
128 if (bpf_get_current_pid_tgid() >> 32 != pid)
129 return;
130
131 __u64 addr = bpf_get_func_ip(ctx);
132
133 if (is_return) {
134 if ((const void *) addr == &bpf_testmod_fentry_test1)
135 kretprobe_testmod_test1_result = 1;
136 if ((const void *) addr == &bpf_testmod_fentry_test2)
137 kretprobe_testmod_test2_result = 1;
138 if ((const void *) addr == &bpf_testmod_fentry_test3)
139 kretprobe_testmod_test3_result = 1;
140 } else {
141 if ((const void *) addr == &bpf_testmod_fentry_test1)
142 kprobe_testmod_test1_result = 1;
143 if ((const void *) addr == &bpf_testmod_fentry_test2)
144 kprobe_testmod_test2_result = 1;
145 if ((const void *) addr == &bpf_testmod_fentry_test3)
146 kprobe_testmod_test3_result = 1;
147 }
148 }
149
150 SEC("kprobe.multi")
test_kprobe_testmod(struct pt_regs * ctx)151 int test_kprobe_testmod(struct pt_regs *ctx)
152 {
153 kprobe_multi_testmod_check(ctx, false);
154 return 0;
155 }
156
157 SEC("kretprobe.multi")
test_kretprobe_testmod(struct pt_regs * ctx)158 int test_kretprobe_testmod(struct pt_regs *ctx)
159 {
160 kprobe_multi_testmod_check(ctx, true);
161 return 0;
162 }
163