13d08b6f2SKP Singh // SPDX-License-Identifier: GPL-2.0
23d08b6f2SKP Singh 
33d08b6f2SKP Singh /*
43d08b6f2SKP Singh  * Copyright 2020 Google LLC.
53d08b6f2SKP Singh  */
63d08b6f2SKP Singh 
73d08b6f2SKP Singh #include <linux/bpf.h>
83d08b6f2SKP Singh #include <bpf/bpf_helpers.h>
93d08b6f2SKP Singh #include <bpf/bpf_tracing.h>
103d08b6f2SKP Singh 
113d08b6f2SKP Singh char _license[] SEC("license") = "GPL";
123d08b6f2SKP Singh 
133d08b6f2SKP Singh static int sequence = 0;
143d08b6f2SKP Singh __s32 input_retval = 0;
153d08b6f2SKP Singh 
163d08b6f2SKP Singh __u64 fentry_result = 0;
173d08b6f2SKP Singh SEC("fentry/bpf_modify_return_test")
BPF_PROG(fentry_test,int a,__u64 b)183d08b6f2SKP Singh int BPF_PROG(fentry_test, int a, __u64 b)
193d08b6f2SKP Singh {
203d08b6f2SKP Singh 	sequence++;
213d08b6f2SKP Singh 	fentry_result = (sequence == 1);
223d08b6f2SKP Singh 	return 0;
233d08b6f2SKP Singh }
243d08b6f2SKP Singh 
253d08b6f2SKP Singh __u64 fmod_ret_result = 0;
263d08b6f2SKP Singh SEC("fmod_ret/bpf_modify_return_test")
BPF_PROG(fmod_ret_test,int a,int * b,int ret)273d08b6f2SKP Singh int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
283d08b6f2SKP Singh {
293d08b6f2SKP Singh 	sequence++;
303d08b6f2SKP Singh 	/* This is the first fmod_ret program, the ret passed should be 0 */
313d08b6f2SKP Singh 	fmod_ret_result = (sequence == 2 && ret == 0);
323d08b6f2SKP Singh 	return input_retval;
333d08b6f2SKP Singh }
343d08b6f2SKP Singh 
353d08b6f2SKP Singh __u64 fexit_result = 0;
363d08b6f2SKP Singh SEC("fexit/bpf_modify_return_test")
BPF_PROG(fexit_test,int a,__u64 b,int ret)373d08b6f2SKP Singh int BPF_PROG(fexit_test, int a, __u64 b, int ret)
383d08b6f2SKP Singh {
393d08b6f2SKP Singh 	sequence++;
403d08b6f2SKP Singh 	/* If the input_reval is non-zero a successful modification should have
413d08b6f2SKP Singh 	 * occurred.
423d08b6f2SKP Singh 	 */
433d08b6f2SKP Singh 	if (input_retval)
443d08b6f2SKP Singh 		fexit_result = (sequence == 3 && ret == input_retval);
453d08b6f2SKP Singh 	else
463d08b6f2SKP Singh 		fexit_result = (sequence == 3 && ret == 4);
473d08b6f2SKP Singh 
483d08b6f2SKP Singh 	return 0;
493d08b6f2SKP Singh }
50*5e9cf77dSMenglong Dong 
51*5e9cf77dSMenglong Dong static int sequence2;
52*5e9cf77dSMenglong Dong 
53*5e9cf77dSMenglong Dong __u64 fentry_result2 = 0;
54*5e9cf77dSMenglong Dong 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*5e9cf77dSMenglong Dong int BPF_PROG(fentry_test2, int a, int *b, short c, int d, void *e, char f,
56*5e9cf77dSMenglong Dong 	     int g)
57*5e9cf77dSMenglong Dong {
58*5e9cf77dSMenglong Dong 	sequence2++;
59*5e9cf77dSMenglong Dong 	fentry_result2 = (sequence2 == 1);
60*5e9cf77dSMenglong Dong 	return 0;
61*5e9cf77dSMenglong Dong }
62*5e9cf77dSMenglong Dong 
63*5e9cf77dSMenglong Dong __u64 fmod_ret_result2 = 0;
64*5e9cf77dSMenglong Dong 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*5e9cf77dSMenglong Dong int BPF_PROG(fmod_ret_test2, int a, int *b, short c, int d, void *e, char f,
66*5e9cf77dSMenglong Dong 	     int g, int ret)
67*5e9cf77dSMenglong Dong {
68*5e9cf77dSMenglong Dong 	sequence2++;
69*5e9cf77dSMenglong Dong 	/* This is the first fmod_ret program, the ret passed should be 0 */
70*5e9cf77dSMenglong Dong 	fmod_ret_result2 = (sequence2 == 2 && ret == 0);
71*5e9cf77dSMenglong Dong 	return input_retval;
72*5e9cf77dSMenglong Dong }
73*5e9cf77dSMenglong Dong 
74*5e9cf77dSMenglong Dong __u64 fexit_result2 = 0;
75*5e9cf77dSMenglong Dong 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*5e9cf77dSMenglong Dong int BPF_PROG(fexit_test2, int a, int *b, short c, int d, void *e, char f,
77*5e9cf77dSMenglong Dong 	     int g, int ret)
78*5e9cf77dSMenglong Dong {
79*5e9cf77dSMenglong Dong 	sequence2++;
80*5e9cf77dSMenglong Dong 	/* If the input_reval is non-zero a successful modification should have
81*5e9cf77dSMenglong Dong 	 * occurred.
82*5e9cf77dSMenglong Dong 	 */
83*5e9cf77dSMenglong Dong 	if (input_retval)
84*5e9cf77dSMenglong Dong 		fexit_result2 = (sequence2 == 3 && ret == input_retval);
85*5e9cf77dSMenglong Dong 	else
86*5e9cf77dSMenglong Dong 		fexit_result2 = (sequence2 == 3 && ret == 29);
87*5e9cf77dSMenglong Dong 
88*5e9cf77dSMenglong Dong 	return 0;
89*5e9cf77dSMenglong Dong }
90