1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * Copyright 2020 Google LLC.
5  */
6 
7 #include <linux/bpf.h>
8 #include <bpf/bpf_helpers.h>
9 #include <bpf/bpf_tracing.h>
10 
11 char _license[] SEC("license") = "GPL";
12 
13 static int sequence = 0;
14 __s32 input_retval = 0;
15 
16 __u64 fentry_result = 0;
17 SEC("fentry/bpf_modify_return_test")
BPF_PROG(fentry_test,int a,__u64 b)18 int BPF_PROG(fentry_test, int a, __u64 b)
19 {
20 	sequence++;
21 	fentry_result = (sequence == 1);
22 	return 0;
23 }
24 
25 __u64 fmod_ret_result = 0;
26 SEC("fmod_ret/bpf_modify_return_test")
BPF_PROG(fmod_ret_test,int a,int * b,int ret)27 int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
28 {
29 	sequence++;
30 	/* This is the first fmod_ret program, the ret passed should be 0 */
31 	fmod_ret_result = (sequence == 2 && ret == 0);
32 	return input_retval;
33 }
34 
35 __u64 fexit_result = 0;
36 SEC("fexit/bpf_modify_return_test")
BPF_PROG(fexit_test,int a,__u64 b,int ret)37 int BPF_PROG(fexit_test, int a, __u64 b, int ret)
38 {
39 	sequence++;
40 	/* If the input_reval is non-zero a successful modification should have
41 	 * occurred.
42 	 */
43 	if (input_retval)
44 		fexit_result = (sequence == 3 && ret == input_retval);
45 	else
46 		fexit_result = (sequence == 3 && ret == 4);
47 
48 	return 0;
49 }
50 
51 static int sequence2;
52 
53 __u64 fentry_result2 = 0;
54 SEC("fentry/bpf_modify_return_test2")
BPF_PROG(fentry_test2,int a,int * b,short c,int d,void * e,char f,int g)55 int BPF_PROG(fentry_test2, int a, int *b, short c, int d, void *e, char f,
56 	     int g)
57 {
58 	sequence2++;
59 	fentry_result2 = (sequence2 == 1);
60 	return 0;
61 }
62 
63 __u64 fmod_ret_result2 = 0;
64 SEC("fmod_ret/bpf_modify_return_test2")
BPF_PROG(fmod_ret_test2,int a,int * b,short c,int d,void * e,char f,int g,int ret)65 int BPF_PROG(fmod_ret_test2, int a, int *b, short c, int d, void *e, char f,
66 	     int g, int ret)
67 {
68 	sequence2++;
69 	/* This is the first fmod_ret program, the ret passed should be 0 */
70 	fmod_ret_result2 = (sequence2 == 2 && ret == 0);
71 	return input_retval;
72 }
73 
74 __u64 fexit_result2 = 0;
75 SEC("fexit/bpf_modify_return_test2")
BPF_PROG(fexit_test2,int a,int * b,short c,int d,void * e,char f,int g,int ret)76 int BPF_PROG(fexit_test2, int a, int *b, short c, int d, void *e, char f,
77 	     int g, int ret)
78 {
79 	sequence2++;
80 	/* If the input_reval is non-zero a successful modification should have
81 	 * occurred.
82 	 */
83 	if (input_retval)
84 		fexit_result2 = (sequence2 == 3 && ret == input_retval);
85 	else
86 		fexit_result2 = (sequence2 == 3 && ret == 29);
87 
88 	return 0;
89 }
90