1 // SPDX-License-Identifier: GPL-2.0
2 /* Converted from tools/testing/selftests/bpf/verifier/masking.c */
3 
4 #include <linux/bpf.h>
5 #include <bpf/bpf_helpers.h>
6 #include "bpf_misc.h"
7 
8 SEC("socket")
9 __description("masking, test out of bounds 1")
10 __success __success_unpriv __retval(0)
test_out_of_bounds_1(void)11 __naked void test_out_of_bounds_1(void)
12 {
13 	asm volatile ("					\
14 	w1 = 5;						\
15 	w2 = %[__imm_0];				\
16 	r2 -= r1;					\
17 	r2 |= r1;					\
18 	r2 = -r2;					\
19 	r2 s>>= 63;					\
20 	r1 &= r2;					\
21 	r0 = r1;					\
22 	exit;						\
23 "	:
24 	: __imm_const(__imm_0, 5 - 1)
25 	: __clobber_all);
26 }
27 
28 SEC("socket")
29 __description("masking, test out of bounds 2")
30 __success __success_unpriv __retval(0)
test_out_of_bounds_2(void)31 __naked void test_out_of_bounds_2(void)
32 {
33 	asm volatile ("					\
34 	w1 = 1;						\
35 	w2 = %[__imm_0];				\
36 	r2 -= r1;					\
37 	r2 |= r1;					\
38 	r2 = -r2;					\
39 	r2 s>>= 63;					\
40 	r1 &= r2;					\
41 	r0 = r1;					\
42 	exit;						\
43 "	:
44 	: __imm_const(__imm_0, 1 - 1)
45 	: __clobber_all);
46 }
47 
48 SEC("socket")
49 __description("masking, test out of bounds 3")
50 __success __success_unpriv __retval(0)
test_out_of_bounds_3(void)51 __naked void test_out_of_bounds_3(void)
52 {
53 	asm volatile ("					\
54 	w1 = 0xffffffff;				\
55 	w2 = %[__imm_0];				\
56 	r2 -= r1;					\
57 	r2 |= r1;					\
58 	r2 = -r2;					\
59 	r2 s>>= 63;					\
60 	r1 &= r2;					\
61 	r0 = r1;					\
62 	exit;						\
63 "	:
64 	: __imm_const(__imm_0, 0xffffffff - 1)
65 	: __clobber_all);
66 }
67 
68 SEC("socket")
69 __description("masking, test out of bounds 4")
70 __success __success_unpriv __retval(0)
test_out_of_bounds_4(void)71 __naked void test_out_of_bounds_4(void)
72 {
73 	asm volatile ("					\
74 	w1 = 0xffffffff;				\
75 	w2 = %[__imm_0];				\
76 	r2 -= r1;					\
77 	r2 |= r1;					\
78 	r2 = -r2;					\
79 	r2 s>>= 63;					\
80 	r1 &= r2;					\
81 	r0 = r1;					\
82 	exit;						\
83 "	:
84 	: __imm_const(__imm_0, 1 - 1)
85 	: __clobber_all);
86 }
87 
88 SEC("socket")
89 __description("masking, test out of bounds 5")
90 __success __success_unpriv __retval(0)
test_out_of_bounds_5(void)91 __naked void test_out_of_bounds_5(void)
92 {
93 	asm volatile ("					\
94 	w1 = -1;					\
95 	w2 = %[__imm_0];				\
96 	r2 -= r1;					\
97 	r2 |= r1;					\
98 	r2 = -r2;					\
99 	r2 s>>= 63;					\
100 	r1 &= r2;					\
101 	r0 = r1;					\
102 	exit;						\
103 "	:
104 	: __imm_const(__imm_0, 1 - 1)
105 	: __clobber_all);
106 }
107 
108 SEC("socket")
109 __description("masking, test out of bounds 6")
110 __success __success_unpriv __retval(0)
test_out_of_bounds_6(void)111 __naked void test_out_of_bounds_6(void)
112 {
113 	asm volatile ("					\
114 	w1 = -1;					\
115 	w2 = %[__imm_0];				\
116 	r2 -= r1;					\
117 	r2 |= r1;					\
118 	r2 = -r2;					\
119 	r2 s>>= 63;					\
120 	r1 &= r2;					\
121 	r0 = r1;					\
122 	exit;						\
123 "	:
124 	: __imm_const(__imm_0, 0xffffffff - 1)
125 	: __clobber_all);
126 }
127 
128 SEC("socket")
129 __description("masking, test out of bounds 7")
130 __success __success_unpriv __retval(0)
test_out_of_bounds_7(void)131 __naked void test_out_of_bounds_7(void)
132 {
133 	asm volatile ("					\
134 	r1 = 5;						\
135 	w2 = %[__imm_0];				\
136 	r2 -= r1;					\
137 	r2 |= r1;					\
138 	r2 = -r2;					\
139 	r2 s>>= 63;					\
140 	r1 &= r2;					\
141 	r0 = r1;					\
142 	exit;						\
143 "	:
144 	: __imm_const(__imm_0, 5 - 1)
145 	: __clobber_all);
146 }
147 
148 SEC("socket")
149 __description("masking, test out of bounds 8")
150 __success __success_unpriv __retval(0)
test_out_of_bounds_8(void)151 __naked void test_out_of_bounds_8(void)
152 {
153 	asm volatile ("					\
154 	r1 = 1;						\
155 	w2 = %[__imm_0];				\
156 	r2 -= r1;					\
157 	r2 |= r1;					\
158 	r2 = -r2;					\
159 	r2 s>>= 63;					\
160 	r1 &= r2;					\
161 	r0 = r1;					\
162 	exit;						\
163 "	:
164 	: __imm_const(__imm_0, 1 - 1)
165 	: __clobber_all);
166 }
167 
168 SEC("socket")
169 __description("masking, test out of bounds 9")
170 __success __success_unpriv __retval(0)
test_out_of_bounds_9(void)171 __naked void test_out_of_bounds_9(void)
172 {
173 	asm volatile ("					\
174 	r1 = 0xffffffff;				\
175 	w2 = %[__imm_0];				\
176 	r2 -= r1;					\
177 	r2 |= r1;					\
178 	r2 = -r2;					\
179 	r2 s>>= 63;					\
180 	r1 &= r2;					\
181 	r0 = r1;					\
182 	exit;						\
183 "	:
184 	: __imm_const(__imm_0, 0xffffffff - 1)
185 	: __clobber_all);
186 }
187 
188 SEC("socket")
189 __description("masking, test out of bounds 10")
190 __success __success_unpriv __retval(0)
test_out_of_bounds_10(void)191 __naked void test_out_of_bounds_10(void)
192 {
193 	asm volatile ("					\
194 	r1 = 0xffffffff;				\
195 	w2 = %[__imm_0];				\
196 	r2 -= r1;					\
197 	r2 |= r1;					\
198 	r2 = -r2;					\
199 	r2 s>>= 63;					\
200 	r1 &= r2;					\
201 	r0 = r1;					\
202 	exit;						\
203 "	:
204 	: __imm_const(__imm_0, 1 - 1)
205 	: __clobber_all);
206 }
207 
208 SEC("socket")
209 __description("masking, test out of bounds 11")
210 __success __success_unpriv __retval(0)
test_out_of_bounds_11(void)211 __naked void test_out_of_bounds_11(void)
212 {
213 	asm volatile ("					\
214 	r1 = -1;					\
215 	w2 = %[__imm_0];				\
216 	r2 -= r1;					\
217 	r2 |= r1;					\
218 	r2 = -r2;					\
219 	r2 s>>= 63;					\
220 	r1 &= r2;					\
221 	r0 = r1;					\
222 	exit;						\
223 "	:
224 	: __imm_const(__imm_0, 1 - 1)
225 	: __clobber_all);
226 }
227 
228 SEC("socket")
229 __description("masking, test out of bounds 12")
230 __success __success_unpriv __retval(0)
test_out_of_bounds_12(void)231 __naked void test_out_of_bounds_12(void)
232 {
233 	asm volatile ("					\
234 	r1 = -1;					\
235 	w2 = %[__imm_0];				\
236 	r2 -= r1;					\
237 	r2 |= r1;					\
238 	r2 = -r2;					\
239 	r2 s>>= 63;					\
240 	r1 &= r2;					\
241 	r0 = r1;					\
242 	exit;						\
243 "	:
244 	: __imm_const(__imm_0, 0xffffffff - 1)
245 	: __clobber_all);
246 }
247 
248 SEC("socket")
249 __description("masking, test in bounds 1")
250 __success __success_unpriv __retval(4)
masking_test_in_bounds_1(void)251 __naked void masking_test_in_bounds_1(void)
252 {
253 	asm volatile ("					\
254 	w1 = 4;						\
255 	w2 = %[__imm_0];				\
256 	r2 -= r1;					\
257 	r2 |= r1;					\
258 	r2 = -r2;					\
259 	r2 s>>= 63;					\
260 	r1 &= r2;					\
261 	r0 = r1;					\
262 	exit;						\
263 "	:
264 	: __imm_const(__imm_0, 5 - 1)
265 	: __clobber_all);
266 }
267 
268 SEC("socket")
269 __description("masking, test in bounds 2")
270 __success __success_unpriv __retval(0)
masking_test_in_bounds_2(void)271 __naked void masking_test_in_bounds_2(void)
272 {
273 	asm volatile ("					\
274 	w1 = 0;						\
275 	w2 = %[__imm_0];				\
276 	r2 -= r1;					\
277 	r2 |= r1;					\
278 	r2 = -r2;					\
279 	r2 s>>= 63;					\
280 	r1 &= r2;					\
281 	r0 = r1;					\
282 	exit;						\
283 "	:
284 	: __imm_const(__imm_0, 0xffffffff - 1)
285 	: __clobber_all);
286 }
287 
288 SEC("socket")
289 __description("masking, test in bounds 3")
290 __success __success_unpriv __retval(0xfffffffe)
masking_test_in_bounds_3(void)291 __naked void masking_test_in_bounds_3(void)
292 {
293 	asm volatile ("					\
294 	w1 = 0xfffffffe;				\
295 	w2 = %[__imm_0];				\
296 	r2 -= r1;					\
297 	r2 |= r1;					\
298 	r2 = -r2;					\
299 	r2 s>>= 63;					\
300 	r1 &= r2;					\
301 	r0 = r1;					\
302 	exit;						\
303 "	:
304 	: __imm_const(__imm_0, 0xffffffff - 1)
305 	: __clobber_all);
306 }
307 
308 SEC("socket")
309 __description("masking, test in bounds 4")
310 __success __success_unpriv __retval(0xabcde)
masking_test_in_bounds_4(void)311 __naked void masking_test_in_bounds_4(void)
312 {
313 	asm volatile ("					\
314 	w1 = 0xabcde;					\
315 	w2 = %[__imm_0];				\
316 	r2 -= r1;					\
317 	r2 |= r1;					\
318 	r2 = -r2;					\
319 	r2 s>>= 63;					\
320 	r1 &= r2;					\
321 	r0 = r1;					\
322 	exit;						\
323 "	:
324 	: __imm_const(__imm_0, 0xabcdef - 1)
325 	: __clobber_all);
326 }
327 
328 SEC("socket")
329 __description("masking, test in bounds 5")
330 __success __success_unpriv __retval(0)
masking_test_in_bounds_5(void)331 __naked void masking_test_in_bounds_5(void)
332 {
333 	asm volatile ("					\
334 	w1 = 0;						\
335 	w2 = %[__imm_0];				\
336 	r2 -= r1;					\
337 	r2 |= r1;					\
338 	r2 = -r2;					\
339 	r2 s>>= 63;					\
340 	r1 &= r2;					\
341 	r0 = r1;					\
342 	exit;						\
343 "	:
344 	: __imm_const(__imm_0, 1 - 1)
345 	: __clobber_all);
346 }
347 
348 SEC("socket")
349 __description("masking, test in bounds 6")
350 __success __success_unpriv __retval(46)
masking_test_in_bounds_6(void)351 __naked void masking_test_in_bounds_6(void)
352 {
353 	asm volatile ("					\
354 	w1 = 46;					\
355 	w2 = %[__imm_0];				\
356 	r2 -= r1;					\
357 	r2 |= r1;					\
358 	r2 = -r2;					\
359 	r2 s>>= 63;					\
360 	r1 &= r2;					\
361 	r0 = r1;					\
362 	exit;						\
363 "	:
364 	: __imm_const(__imm_0, 47 - 1)
365 	: __clobber_all);
366 }
367 
368 SEC("socket")
369 __description("masking, test in bounds 7")
370 __success __success_unpriv __retval(46)
masking_test_in_bounds_7(void)371 __naked void masking_test_in_bounds_7(void)
372 {
373 	asm volatile ("					\
374 	r3 = -46;					\
375 	r3 *= -1;					\
376 	w2 = %[__imm_0];				\
377 	r2 -= r3;					\
378 	r2 |= r3;					\
379 	r2 = -r2;					\
380 	r2 s>>= 63;					\
381 	r3 &= r2;					\
382 	r0 = r3;					\
383 	exit;						\
384 "	:
385 	: __imm_const(__imm_0, 47 - 1)
386 	: __clobber_all);
387 }
388 
389 SEC("socket")
390 __description("masking, test in bounds 8")
391 __success __success_unpriv __retval(0)
masking_test_in_bounds_8(void)392 __naked void masking_test_in_bounds_8(void)
393 {
394 	asm volatile ("					\
395 	r3 = -47;					\
396 	r3 *= -1;					\
397 	w2 = %[__imm_0];				\
398 	r2 -= r3;					\
399 	r2 |= r3;					\
400 	r2 = -r2;					\
401 	r2 s>>= 63;					\
402 	r3 &= r2;					\
403 	r0 = r3;					\
404 	exit;						\
405 "	:
406 	: __imm_const(__imm_0, 47 - 1)
407 	: __clobber_all);
408 }
409 
410 char _license[] SEC("license") = "GPL";
411