1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
3
4 #include "vmlinux.h"
5 #include <bpf/bpf_helpers.h>
6 #include "bpf_misc.h"
7
8 char _license[] SEC("license") = "GPL";
9
10 struct callback_ctx {
11 int output;
12 };
13
14 struct {
15 __uint(type, BPF_MAP_TYPE_HASH);
16 __uint(max_entries, 32);
17 __type(key, int);
18 __type(value, int);
19 } map1 SEC(".maps");
20
21 /* These should be set by the user program */
22 u32 nested_callback_nr_loops;
23 u32 stop_index = -1;
24 u32 nr_loops;
25 int pid;
26 int callback_selector;
27
28 /* Making these global variables so that the userspace program
29 * can verify the output through the skeleton
30 */
31 int nr_loops_returned;
32 int g_output;
33 int err;
34
callback(__u32 index,void * data)35 static int callback(__u32 index, void *data)
36 {
37 struct callback_ctx *ctx = data;
38
39 if (index >= stop_index)
40 return 1;
41
42 ctx->output += index;
43
44 return 0;
45 }
46
empty_callback(__u32 index,void * data)47 static int empty_callback(__u32 index, void *data)
48 {
49 return 0;
50 }
51
nested_callback2(__u32 index,void * data)52 static int nested_callback2(__u32 index, void *data)
53 {
54 nr_loops_returned += bpf_loop(nested_callback_nr_loops, callback, data, 0);
55
56 return 0;
57 }
58
nested_callback1(__u32 index,void * data)59 static int nested_callback1(__u32 index, void *data)
60 {
61 bpf_loop(nested_callback_nr_loops, nested_callback2, data, 0);
62 return 0;
63 }
64
65 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
test_prog(void * ctx)66 int test_prog(void *ctx)
67 {
68 struct callback_ctx data = {};
69
70 if (bpf_get_current_pid_tgid() >> 32 != pid)
71 return 0;
72
73 nr_loops_returned = bpf_loop(nr_loops, callback, &data, 0);
74
75 if (nr_loops_returned < 0)
76 err = nr_loops_returned;
77 else
78 g_output = data.output;
79
80 return 0;
81 }
82
83 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
prog_null_ctx(void * ctx)84 int prog_null_ctx(void *ctx)
85 {
86 if (bpf_get_current_pid_tgid() >> 32 != pid)
87 return 0;
88
89 nr_loops_returned = bpf_loop(nr_loops, empty_callback, NULL, 0);
90
91 return 0;
92 }
93
94 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
prog_invalid_flags(void * ctx)95 int prog_invalid_flags(void *ctx)
96 {
97 struct callback_ctx data = {};
98
99 if (bpf_get_current_pid_tgid() >> 32 != pid)
100 return 0;
101
102 err = bpf_loop(nr_loops, callback, &data, 1);
103
104 return 0;
105 }
106
107 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
prog_nested_calls(void * ctx)108 int prog_nested_calls(void *ctx)
109 {
110 struct callback_ctx data = {};
111
112 if (bpf_get_current_pid_tgid() >> 32 != pid)
113 return 0;
114
115 nr_loops_returned = 0;
116 bpf_loop(nr_loops, nested_callback1, &data, 0);
117
118 g_output = data.output;
119
120 return 0;
121 }
122
callback_set_f0(int i,void * ctx)123 static int callback_set_f0(int i, void *ctx)
124 {
125 g_output = 0xF0;
126 return 0;
127 }
128
callback_set_0f(int i,void * ctx)129 static int callback_set_0f(int i, void *ctx)
130 {
131 g_output = 0x0F;
132 return 0;
133 }
134
135 /*
136 * non-constant callback is a corner case for bpf_loop inline logic
137 */
138 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
prog_non_constant_callback(void * ctx)139 int prog_non_constant_callback(void *ctx)
140 {
141 struct callback_ctx data = {};
142
143 if (bpf_get_current_pid_tgid() >> 32 != pid)
144 return 0;
145
146 int (*callback)(int i, void *ctx);
147
148 g_output = 0;
149
150 if (callback_selector == 0x0F)
151 callback = callback_set_0f;
152 else
153 callback = callback_set_f0;
154
155 bpf_loop(1, callback, NULL, 0);
156
157 return 0;
158 }
159
stack_check_inner_callback(void * ctx)160 static int stack_check_inner_callback(void *ctx)
161 {
162 return 0;
163 }
164
map1_lookup_elem(int key)165 static int map1_lookup_elem(int key)
166 {
167 int *val = bpf_map_lookup_elem(&map1, &key);
168
169 return val ? *val : -1;
170 }
171
map1_update_elem(int key,int val)172 static void map1_update_elem(int key, int val)
173 {
174 bpf_map_update_elem(&map1, &key, &val, BPF_ANY);
175 }
176
stack_check_outer_callback(void * ctx)177 static int stack_check_outer_callback(void *ctx)
178 {
179 int a = map1_lookup_elem(1);
180 int b = map1_lookup_elem(2);
181 int c = map1_lookup_elem(3);
182 int d = map1_lookup_elem(4);
183 int e = map1_lookup_elem(5);
184 int f = map1_lookup_elem(6);
185
186 bpf_loop(1, stack_check_inner_callback, NULL, 0);
187
188 map1_update_elem(1, a + 1);
189 map1_update_elem(2, b + 1);
190 map1_update_elem(3, c + 1);
191 map1_update_elem(4, d + 1);
192 map1_update_elem(5, e + 1);
193 map1_update_elem(6, f + 1);
194
195 return 0;
196 }
197
198 /* Some of the local variables in stack_check and
199 * stack_check_outer_callback would be allocated on stack by
200 * compiler. This test should verify that stack content for these
201 * variables is preserved between calls to bpf_loop (might be an issue
202 * if loop inlining allocates stack slots incorrectly).
203 */
204 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
stack_check(void * ctx)205 int stack_check(void *ctx)
206 {
207 if (bpf_get_current_pid_tgid() >> 32 != pid)
208 return 0;
209
210 int a = map1_lookup_elem(7);
211 int b = map1_lookup_elem(8);
212 int c = map1_lookup_elem(9);
213 int d = map1_lookup_elem(10);
214 int e = map1_lookup_elem(11);
215 int f = map1_lookup_elem(12);
216
217 bpf_loop(1, stack_check_outer_callback, NULL, 0);
218
219 map1_update_elem(7, a + 1);
220 map1_update_elem(8, b + 1);
221 map1_update_elem(9, c + 1);
222 map1_update_elem(10, d + 1);
223 map1_update_elem(11, e + 1);
224 map1_update_elem(12, f + 1);
225
226 return 0;
227 }
228