xref: /linux/tools/testing/selftests/bpf/progs/get_func_ip_test.c (revision edac6f6332d96aab59af5f27a195f55cd080f034)
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 
6 char _license[] SEC("license") = "GPL";
7 
8 extern const void bpf_fentry_test1 __ksym;
9 extern const void bpf_fentry_test2 __ksym;
10 extern const void bpf_fentry_test3 __ksym;
11 extern const void bpf_fentry_test4 __ksym;
12 extern const void bpf_modify_return_test __ksym;
13 extern const void bpf_fentry_test6 __ksym;
14 
15 __u64 test1_result = 0;
16 SEC("fentry/bpf_fentry_test1")
17 int BPF_PROG(test1, int a)
18 {
19 	__u64 addr = bpf_get_func_ip(ctx);
20 
21 	test1_result = (const void *) addr == &bpf_fentry_test1;
22 	return 0;
23 }
24 
25 __u64 test2_result = 0;
26 SEC("fexit/bpf_fentry_test2")
27 int BPF_PROG(test2, int a)
28 {
29 	__u64 addr = bpf_get_func_ip(ctx);
30 
31 	test2_result = (const void *) addr == &bpf_fentry_test2;
32 	return 0;
33 }
34 
35 __u64 test3_result = 0;
36 SEC("kprobe/bpf_fentry_test3")
37 int test3(struct pt_regs *ctx)
38 {
39 	__u64 addr = bpf_get_func_ip(ctx);
40 
41 	test3_result = (const void *) addr == &bpf_fentry_test3;
42 	return 0;
43 }
44 
45 __u64 test4_result = 0;
46 SEC("kretprobe/bpf_fentry_test4")
47 int BPF_KRETPROBE(test4)
48 {
49 	__u64 addr = bpf_get_func_ip(ctx);
50 
51 	test4_result = (const void *) addr == &bpf_fentry_test4;
52 	return 0;
53 }
54 
55 __u64 test5_result = 0;
56 SEC("fmod_ret/bpf_modify_return_test")
57 int BPF_PROG(test5, int a, int *b, int ret)
58 {
59 	__u64 addr = bpf_get_func_ip(ctx);
60 
61 	test5_result = (const void *) addr == &bpf_modify_return_test;
62 	return ret;
63 }
64 
65 __u64 test6_result = 0;
66 SEC("kprobe/bpf_fentry_test6+0x5")
67 int test6(struct pt_regs *ctx)
68 {
69 	__u64 addr = bpf_get_func_ip(ctx);
70 
71 	test6_result = (const void *) addr == &bpf_fentry_test6 + 5;
72 	return 0;
73 }
74