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
8SEC("socket")
9__description("masking, test out of bounds 1")
10__success __success_unpriv __retval(0)
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
28SEC("socket")
29__description("masking, test out of bounds 2")
30__success __success_unpriv __retval(0)
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
48SEC("socket")
49__description("masking, test out of bounds 3")
50__success __success_unpriv __retval(0)
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
68SEC("socket")
69__description("masking, test out of bounds 4")
70__success __success_unpriv __retval(0)
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
88SEC("socket")
89__description("masking, test out of bounds 5")
90__success __success_unpriv __retval(0)
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
108SEC("socket")
109__description("masking, test out of bounds 6")
110__success __success_unpriv __retval(0)
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
128SEC("socket")
129__description("masking, test out of bounds 7")
130__success __success_unpriv __retval(0)
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
148SEC("socket")
149__description("masking, test out of bounds 8")
150__success __success_unpriv __retval(0)
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
168SEC("socket")
169__description("masking, test out of bounds 9")
170__success __success_unpriv __retval(0)
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
188SEC("socket")
189__description("masking, test out of bounds 10")
190__success __success_unpriv __retval(0)
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
208SEC("socket")
209__description("masking, test out of bounds 11")
210__success __success_unpriv __retval(0)
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
228SEC("socket")
229__description("masking, test out of bounds 12")
230__success __success_unpriv __retval(0)
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
248SEC("socket")
249__description("masking, test in bounds 1")
250__success __success_unpriv __retval(4)
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
268SEC("socket")
269__description("masking, test in bounds 2")
270__success __success_unpriv __retval(0)
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
288SEC("socket")
289__description("masking, test in bounds 3")
290__success __success_unpriv __retval(0xfffffffe)
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
308SEC("socket")
309__description("masking, test in bounds 4")
310__success __success_unpriv __retval(0xabcde)
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
328SEC("socket")
329__description("masking, test in bounds 5")
330__success __success_unpriv __retval(0)
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
348SEC("socket")
349__description("masking, test in bounds 6")
350__success __success_unpriv __retval(46)
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
368SEC("socket")
369__description("masking, test in bounds 7")
370__success __success_unpriv __retval(46)
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
389SEC("socket")
390__description("masking, test in bounds 8")
391__success __success_unpriv __retval(0)
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
410char _license[] SEC("license") = "GPL";
411