1f7a11eecSJiri Olsa // SPDX-License-Identifier: GPL-2.0
2f7a11eecSJiri Olsa #include <linux/bpf.h>
3f7a11eecSJiri Olsa #include <bpf/bpf_helpers.h>
4f7a11eecSJiri Olsa #include <bpf/bpf_tracing.h>
5f7a11eecSJiri Olsa #include <stdbool.h>
6f7a11eecSJiri Olsa 
7f7a11eecSJiri Olsa char _license[] SEC("license") = "GPL";
8f7a11eecSJiri Olsa 
9f7a11eecSJiri Olsa extern const void bpf_fentry_test1 __ksym;
10f7a11eecSJiri Olsa extern const void bpf_fentry_test2 __ksym;
11f7a11eecSJiri Olsa extern const void bpf_fentry_test3 __ksym;
12f7a11eecSJiri Olsa extern const void bpf_fentry_test4 __ksym;
13f7a11eecSJiri Olsa extern const void bpf_fentry_test5 __ksym;
14f7a11eecSJiri Olsa extern const void bpf_fentry_test6 __ksym;
15f7a11eecSJiri Olsa extern const void bpf_fentry_test7 __ksym;
16f7a11eecSJiri Olsa extern const void bpf_fentry_test8 __ksym;
17f7a11eecSJiri Olsa 
18f7a11eecSJiri Olsa int pid = 0;
192c6401c9SJiri Olsa bool test_cookie = false;
20f7a11eecSJiri Olsa 
21f7a11eecSJiri Olsa __u64 kprobe_test1_result = 0;
22f7a11eecSJiri Olsa __u64 kprobe_test2_result = 0;
23f7a11eecSJiri Olsa __u64 kprobe_test3_result = 0;
24f7a11eecSJiri Olsa __u64 kprobe_test4_result = 0;
25f7a11eecSJiri Olsa __u64 kprobe_test5_result = 0;
26f7a11eecSJiri Olsa __u64 kprobe_test6_result = 0;
27f7a11eecSJiri Olsa __u64 kprobe_test7_result = 0;
28f7a11eecSJiri Olsa __u64 kprobe_test8_result = 0;
29f7a11eecSJiri Olsa 
30f7a11eecSJiri Olsa __u64 kretprobe_test1_result = 0;
31f7a11eecSJiri Olsa __u64 kretprobe_test2_result = 0;
32f7a11eecSJiri Olsa __u64 kretprobe_test3_result = 0;
33f7a11eecSJiri Olsa __u64 kretprobe_test4_result = 0;
34f7a11eecSJiri Olsa __u64 kretprobe_test5_result = 0;
35f7a11eecSJiri Olsa __u64 kretprobe_test6_result = 0;
36f7a11eecSJiri Olsa __u64 kretprobe_test7_result = 0;
37f7a11eecSJiri Olsa __u64 kretprobe_test8_result = 0;
38f7a11eecSJiri Olsa 
kprobe_multi_check(void * ctx,bool is_return)39f7a11eecSJiri Olsa static void kprobe_multi_check(void *ctx, bool is_return)
40f7a11eecSJiri Olsa {
41f7a11eecSJiri Olsa 	if (bpf_get_current_pid_tgid() >> 32 != pid)
42f7a11eecSJiri Olsa 		return;
43f7a11eecSJiri Olsa 
442c6401c9SJiri Olsa 	__u64 cookie = test_cookie ? bpf_get_attach_cookie(ctx) : 0;
45c09eb2e5SJiri Olsa 	__u64 addr = bpf_get_func_ip(ctx);
46f7a11eecSJiri Olsa 
472c6401c9SJiri Olsa #define SET(__var, __addr, __cookie) ({			\
482c6401c9SJiri Olsa 	if (((const void *) addr == __addr) &&		\
492c6401c9SJiri Olsa 	     (!test_cookie || (cookie == __cookie)))	\
50f7a11eecSJiri Olsa 		__var = 1;				\
51f7a11eecSJiri Olsa })
52f7a11eecSJiri Olsa 
53f7a11eecSJiri Olsa 	if (is_return) {
542c6401c9SJiri Olsa 		SET(kretprobe_test1_result, &bpf_fentry_test1, 8);
55ad884853SJiri Olsa 		SET(kretprobe_test2_result, &bpf_fentry_test2, 2);
56ad884853SJiri Olsa 		SET(kretprobe_test3_result, &bpf_fentry_test3, 7);
57ad884853SJiri Olsa 		SET(kretprobe_test4_result, &bpf_fentry_test4, 6);
58ad884853SJiri Olsa 		SET(kretprobe_test5_result, &bpf_fentry_test5, 5);
59ad884853SJiri Olsa 		SET(kretprobe_test6_result, &bpf_fentry_test6, 4);
60ad884853SJiri Olsa 		SET(kretprobe_test7_result, &bpf_fentry_test7, 3);
612c6401c9SJiri Olsa 		SET(kretprobe_test8_result, &bpf_fentry_test8, 1);
62f7a11eecSJiri Olsa 	} else {
632c6401c9SJiri Olsa 		SET(kprobe_test1_result, &bpf_fentry_test1, 1);
64ad884853SJiri Olsa 		SET(kprobe_test2_result, &bpf_fentry_test2, 7);
65ad884853SJiri Olsa 		SET(kprobe_test3_result, &bpf_fentry_test3, 2);
66ad884853SJiri Olsa 		SET(kprobe_test4_result, &bpf_fentry_test4, 3);
67ad884853SJiri Olsa 		SET(kprobe_test5_result, &bpf_fentry_test5, 4);
68ad884853SJiri Olsa 		SET(kprobe_test6_result, &bpf_fentry_test6, 5);
69ad884853SJiri Olsa 		SET(kprobe_test7_result, &bpf_fentry_test7, 6);
702c6401c9SJiri Olsa 		SET(kprobe_test8_result, &bpf_fentry_test8, 8);
71f7a11eecSJiri Olsa 	}
72f7a11eecSJiri Olsa 
73f7a11eecSJiri Olsa #undef SET
74f7a11eecSJiri Olsa }
75f7a11eecSJiri Olsa 
76f7a11eecSJiri Olsa /*
77f7a11eecSJiri Olsa  * No tests in here, just to trigger 'bpf_fentry_test*'
78f7a11eecSJiri Olsa  * through tracing test_run
79f7a11eecSJiri Olsa  */
80f7a11eecSJiri Olsa SEC("fentry/bpf_modify_return_test")
BPF_PROG(trigger)81f7a11eecSJiri Olsa int BPF_PROG(trigger)
82f7a11eecSJiri Olsa {
83f7a11eecSJiri Olsa 	return 0;
84f7a11eecSJiri Olsa }
85f7a11eecSJiri Olsa 
86f7a11eecSJiri Olsa SEC("kprobe.multi/bpf_fentry_tes??")
test_kprobe(struct pt_regs * ctx)87f7a11eecSJiri Olsa int test_kprobe(struct pt_regs *ctx)
88f7a11eecSJiri Olsa {
89f7a11eecSJiri Olsa 	kprobe_multi_check(ctx, false);
90f7a11eecSJiri Olsa 	return 0;
91f7a11eecSJiri Olsa }
92f7a11eecSJiri Olsa 
93f7a11eecSJiri Olsa SEC("kretprobe.multi/bpf_fentry_test*")
test_kretprobe(struct pt_regs * ctx)94f7a11eecSJiri Olsa int test_kretprobe(struct pt_regs *ctx)
95f7a11eecSJiri Olsa {
96f7a11eecSJiri Olsa 	kprobe_multi_check(ctx, true);
97f7a11eecSJiri Olsa 	return 0;
98f7a11eecSJiri Olsa }
9932c03c49SAndrii Nakryiko 
10032c03c49SAndrii Nakryiko SEC("kprobe.multi")
test_kprobe_manual(struct pt_regs * ctx)10132c03c49SAndrii Nakryiko int test_kprobe_manual(struct pt_regs *ctx)
10232c03c49SAndrii Nakryiko {
10332c03c49SAndrii Nakryiko 	kprobe_multi_check(ctx, false);
10432c03c49SAndrii Nakryiko 	return 0;
10532c03c49SAndrii Nakryiko }
10632c03c49SAndrii Nakryiko 
10732c03c49SAndrii Nakryiko SEC("kretprobe.multi")
test_kretprobe_manual(struct pt_regs * ctx)10832c03c49SAndrii Nakryiko int test_kretprobe_manual(struct pt_regs *ctx)
10932c03c49SAndrii Nakryiko {
11032c03c49SAndrii Nakryiko 	kprobe_multi_check(ctx, true);
11132c03c49SAndrii Nakryiko 	return 0;
11232c03c49SAndrii Nakryiko }
113*b2440443SJiri Olsa 
114*b2440443SJiri Olsa extern const void bpf_testmod_fentry_test1 __ksym;
115*b2440443SJiri Olsa extern const void bpf_testmod_fentry_test2 __ksym;
116*b2440443SJiri Olsa extern const void bpf_testmod_fentry_test3 __ksym;
117*b2440443SJiri Olsa 
118*b2440443SJiri Olsa __u64 kprobe_testmod_test1_result = 0;
119*b2440443SJiri Olsa __u64 kprobe_testmod_test2_result = 0;
120*b2440443SJiri Olsa __u64 kprobe_testmod_test3_result = 0;
121*b2440443SJiri Olsa 
122*b2440443SJiri Olsa __u64 kretprobe_testmod_test1_result = 0;
123*b2440443SJiri Olsa __u64 kretprobe_testmod_test2_result = 0;
124*b2440443SJiri Olsa __u64 kretprobe_testmod_test3_result = 0;
125*b2440443SJiri Olsa 
kprobe_multi_testmod_check(void * ctx,bool is_return)126*b2440443SJiri Olsa static void kprobe_multi_testmod_check(void *ctx, bool is_return)
127*b2440443SJiri Olsa {
128*b2440443SJiri Olsa 	if (bpf_get_current_pid_tgid() >> 32 != pid)
129*b2440443SJiri Olsa 		return;
130*b2440443SJiri Olsa 
131*b2440443SJiri Olsa 	__u64 addr = bpf_get_func_ip(ctx);
132*b2440443SJiri Olsa 
133*b2440443SJiri Olsa 	if (is_return) {
134*b2440443SJiri Olsa 		if ((const void *) addr == &bpf_testmod_fentry_test1)
135*b2440443SJiri Olsa 			kretprobe_testmod_test1_result = 1;
136*b2440443SJiri Olsa 		if ((const void *) addr == &bpf_testmod_fentry_test2)
137*b2440443SJiri Olsa 			kretprobe_testmod_test2_result = 1;
138*b2440443SJiri Olsa 		if ((const void *) addr == &bpf_testmod_fentry_test3)
139*b2440443SJiri Olsa 			kretprobe_testmod_test3_result = 1;
140*b2440443SJiri Olsa 	} else {
141*b2440443SJiri Olsa 		if ((const void *) addr == &bpf_testmod_fentry_test1)
142*b2440443SJiri Olsa 			kprobe_testmod_test1_result = 1;
143*b2440443SJiri Olsa 		if ((const void *) addr == &bpf_testmod_fentry_test2)
144*b2440443SJiri Olsa 			kprobe_testmod_test2_result = 1;
145*b2440443SJiri Olsa 		if ((const void *) addr == &bpf_testmod_fentry_test3)
146*b2440443SJiri Olsa 			kprobe_testmod_test3_result = 1;
147*b2440443SJiri Olsa 	}
148*b2440443SJiri Olsa }
149*b2440443SJiri Olsa 
150*b2440443SJiri Olsa SEC("kprobe.multi")
test_kprobe_testmod(struct pt_regs * ctx)151*b2440443SJiri Olsa int test_kprobe_testmod(struct pt_regs *ctx)
152*b2440443SJiri Olsa {
153*b2440443SJiri Olsa 	kprobe_multi_testmod_check(ctx, false);
154*b2440443SJiri Olsa 	return 0;
155*b2440443SJiri Olsa }
156*b2440443SJiri Olsa 
157*b2440443SJiri Olsa SEC("kretprobe.multi")
test_kretprobe_testmod(struct pt_regs * ctx)158*b2440443SJiri Olsa int test_kretprobe_testmod(struct pt_regs *ctx)
159*b2440443SJiri Olsa {
160*b2440443SJiri Olsa 	kprobe_multi_testmod_check(ctx, true);
161*b2440443SJiri Olsa 	return 0;
162*b2440443SJiri Olsa }
163