Merge branch 'i2c/for-3.19' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[platform/kernel/linux-exynos.git] / lib / test_bpf.c
1 /*
2  * Testsuite for BPF interpreter and BPF JIT compiler
3  *
4  * Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  */
15
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/filter.h>
21 #include <linux/skbuff.h>
22 #include <linux/netdevice.h>
23 #include <linux/if_vlan.h>
24
25 /* General test specific settings */
26 #define MAX_SUBTESTS    3
27 #define MAX_TESTRUNS    10000
28 #define MAX_DATA        128
29 #define MAX_INSNS       512
30 #define MAX_K           0xffffFFFF
31
32 /* Few constants used to init test 'skb' */
33 #define SKB_TYPE        3
34 #define SKB_MARK        0x1234aaaa
35 #define SKB_HASH        0x1234aaab
36 #define SKB_QUEUE_MAP   123
37 #define SKB_VLAN_TCI    0xffff
38 #define SKB_DEV_IFINDEX 577
39 #define SKB_DEV_TYPE    588
40
41 /* Redefine REGs to make tests less verbose */
42 #define R0              BPF_REG_0
43 #define R1              BPF_REG_1
44 #define R2              BPF_REG_2
45 #define R3              BPF_REG_3
46 #define R4              BPF_REG_4
47 #define R5              BPF_REG_5
48 #define R6              BPF_REG_6
49 #define R7              BPF_REG_7
50 #define R8              BPF_REG_8
51 #define R9              BPF_REG_9
52 #define R10             BPF_REG_10
53
54 /* Flags that can be passed to test cases */
55 #define FLAG_NO_DATA            BIT(0)
56 #define FLAG_EXPECTED_FAIL      BIT(1)
57
58 enum {
59         CLASSIC  = BIT(6),      /* Old BPF instructions only. */
60         INTERNAL = BIT(7),      /* Extended instruction set.  */
61 };
62
63 #define TEST_TYPE_MASK          (CLASSIC | INTERNAL)
64
65 struct bpf_test {
66         const char *descr;
67         union {
68                 struct sock_filter insns[MAX_INSNS];
69                 struct bpf_insn insns_int[MAX_INSNS];
70         } u;
71         __u8 aux;
72         __u8 data[MAX_DATA];
73         struct {
74                 int data_size;
75                 __u32 result;
76         } test[MAX_SUBTESTS];
77 };
78
79 static struct bpf_test tests[] = {
80         {
81                 "TAX",
82                 .u.insns = {
83                         BPF_STMT(BPF_LD | BPF_IMM, 1),
84                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
85                         BPF_STMT(BPF_LD | BPF_IMM, 2),
86                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
87                         BPF_STMT(BPF_ALU | BPF_NEG, 0), /* A == -3 */
88                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
89                         BPF_STMT(BPF_LD | BPF_LEN, 0),
90                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
91                         BPF_STMT(BPF_MISC | BPF_TAX, 0), /* X == len - 3 */
92                         BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
93                         BPF_STMT(BPF_RET | BPF_A, 0)
94                 },
95                 CLASSIC,
96                 { 10, 20, 30, 40, 50 },
97                 { { 2, 10 }, { 3, 20 }, { 4, 30 } },
98         },
99         {
100                 "TXA",
101                 .u.insns = {
102                         BPF_STMT(BPF_LDX | BPF_LEN, 0),
103                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
104                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
105                         BPF_STMT(BPF_RET | BPF_A, 0) /* A == len * 2 */
106                 },
107                 CLASSIC,
108                 { 10, 20, 30, 40, 50 },
109                 { { 1, 2 }, { 3, 6 }, { 4, 8 } },
110         },
111         {
112                 "ADD_SUB_MUL_K",
113                 .u.insns = {
114                         BPF_STMT(BPF_LD | BPF_IMM, 1),
115                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 2),
116                         BPF_STMT(BPF_LDX | BPF_IMM, 3),
117                         BPF_STMT(BPF_ALU | BPF_SUB | BPF_X, 0),
118                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 0xffffffff),
119                         BPF_STMT(BPF_ALU | BPF_MUL | BPF_K, 3),
120                         BPF_STMT(BPF_RET | BPF_A, 0)
121                 },
122                 CLASSIC | FLAG_NO_DATA,
123                 { },
124                 { { 0, 0xfffffffd } }
125         },
126         {
127                 "DIV_MOD_KX",
128                 .u.insns = {
129                         BPF_STMT(BPF_LD | BPF_IMM, 8),
130                         BPF_STMT(BPF_ALU | BPF_DIV | BPF_K, 2),
131                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
132                         BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff),
133                         BPF_STMT(BPF_ALU | BPF_DIV | BPF_X, 0),
134                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
135                         BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff),
136                         BPF_STMT(BPF_ALU | BPF_DIV | BPF_K, 0x70000000),
137                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
138                         BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff),
139                         BPF_STMT(BPF_ALU | BPF_MOD | BPF_X, 0),
140                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
141                         BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff),
142                         BPF_STMT(BPF_ALU | BPF_MOD | BPF_K, 0x70000000),
143                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
144                         BPF_STMT(BPF_RET | BPF_A, 0)
145                 },
146                 CLASSIC | FLAG_NO_DATA,
147                 { },
148                 { { 0, 0x20000000 } }
149         },
150         {
151                 "AND_OR_LSH_K",
152                 .u.insns = {
153                         BPF_STMT(BPF_LD | BPF_IMM, 0xff),
154                         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xf0),
155                         BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 27),
156                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
157                         BPF_STMT(BPF_LD | BPF_IMM, 0xf),
158                         BPF_STMT(BPF_ALU | BPF_OR | BPF_K, 0xf0),
159                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
160                         BPF_STMT(BPF_RET | BPF_A, 0)
161                 },
162                 CLASSIC | FLAG_NO_DATA,
163                 { },
164                 { { 0, 0x800000ff }, { 1, 0x800000ff } },
165         },
166         {
167                 "LD_IMM_0",
168                 .u.insns = {
169                         BPF_STMT(BPF_LD | BPF_IMM, 0), /* ld #0 */
170                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0),
171                         BPF_STMT(BPF_RET | BPF_K, 0),
172                         BPF_STMT(BPF_RET | BPF_K, 1),
173                 },
174                 CLASSIC,
175                 { },
176                 { { 1, 1 } },
177         },
178         {
179                 "LD_IND",
180                 .u.insns = {
181                         BPF_STMT(BPF_LDX | BPF_LEN, 0),
182                         BPF_STMT(BPF_LD | BPF_H | BPF_IND, MAX_K),
183                         BPF_STMT(BPF_RET | BPF_K, 1)
184                 },
185                 CLASSIC,
186                 { },
187                 { { 1, 0 }, { 10, 0 }, { 60, 0 } },
188         },
189         {
190                 "LD_ABS",
191                 .u.insns = {
192                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 1000),
193                         BPF_STMT(BPF_RET | BPF_K, 1)
194                 },
195                 CLASSIC,
196                 { },
197                 { { 1, 0 }, { 10, 0 }, { 60, 0 } },
198         },
199         {
200                 "LD_ABS_LL",
201                 .u.insns = {
202                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, SKF_LL_OFF),
203                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
204                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, SKF_LL_OFF + 1),
205                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
206                         BPF_STMT(BPF_RET | BPF_A, 0)
207                 },
208                 CLASSIC,
209                 { 1, 2, 3 },
210                 { { 1, 0 }, { 2, 3 } },
211         },
212         {
213                 "LD_IND_LL",
214                 .u.insns = {
215                         BPF_STMT(BPF_LD | BPF_IMM, SKF_LL_OFF - 1),
216                         BPF_STMT(BPF_LDX | BPF_LEN, 0),
217                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
218                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
219                         BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
220                         BPF_STMT(BPF_RET | BPF_A, 0)
221                 },
222                 CLASSIC,
223                 { 1, 2, 3, 0xff },
224                 { { 1, 1 }, { 3, 3 }, { 4, 0xff } },
225         },
226         {
227                 "LD_ABS_NET",
228                 .u.insns = {
229                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, SKF_NET_OFF),
230                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
231                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, SKF_NET_OFF + 1),
232                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
233                         BPF_STMT(BPF_RET | BPF_A, 0)
234                 },
235                 CLASSIC,
236                 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 },
237                 { { 15, 0 }, { 16, 3 } },
238         },
239         {
240                 "LD_IND_NET",
241                 .u.insns = {
242                         BPF_STMT(BPF_LD | BPF_IMM, SKF_NET_OFF - 15),
243                         BPF_STMT(BPF_LDX | BPF_LEN, 0),
244                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
245                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
246                         BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
247                         BPF_STMT(BPF_RET | BPF_A, 0)
248                 },
249                 CLASSIC,
250                 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 },
251                 { { 14, 0 }, { 15, 1 }, { 17, 3 } },
252         },
253         {
254                 "LD_PKTTYPE",
255                 .u.insns = {
256                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
257                                  SKF_AD_OFF + SKF_AD_PKTTYPE),
258                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SKB_TYPE, 1, 0),
259                         BPF_STMT(BPF_RET | BPF_K, 1),
260                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
261                                  SKF_AD_OFF + SKF_AD_PKTTYPE),
262                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SKB_TYPE, 1, 0),
263                         BPF_STMT(BPF_RET | BPF_K, 1),
264                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
265                                  SKF_AD_OFF + SKF_AD_PKTTYPE),
266                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SKB_TYPE, 1, 0),
267                         BPF_STMT(BPF_RET | BPF_K, 1),
268                         BPF_STMT(BPF_RET | BPF_A, 0)
269                 },
270                 CLASSIC,
271                 { },
272                 { { 1, 3 }, { 10, 3 } },
273         },
274         {
275                 "LD_MARK",
276                 .u.insns = {
277                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
278                                  SKF_AD_OFF + SKF_AD_MARK),
279                         BPF_STMT(BPF_RET | BPF_A, 0)
280                 },
281                 CLASSIC,
282                 { },
283                 { { 1, SKB_MARK}, { 10, SKB_MARK} },
284         },
285         {
286                 "LD_RXHASH",
287                 .u.insns = {
288                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
289                                  SKF_AD_OFF + SKF_AD_RXHASH),
290                         BPF_STMT(BPF_RET | BPF_A, 0)
291                 },
292                 CLASSIC,
293                 { },
294                 { { 1, SKB_HASH}, { 10, SKB_HASH} },
295         },
296         {
297                 "LD_QUEUE",
298                 .u.insns = {
299                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
300                                  SKF_AD_OFF + SKF_AD_QUEUE),
301                         BPF_STMT(BPF_RET | BPF_A, 0)
302                 },
303                 CLASSIC,
304                 { },
305                 { { 1, SKB_QUEUE_MAP }, { 10, SKB_QUEUE_MAP } },
306         },
307         {
308                 "LD_PROTOCOL",
309                 .u.insns = {
310                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 1),
311                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 20, 1, 0),
312                         BPF_STMT(BPF_RET | BPF_K, 0),
313                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
314                                  SKF_AD_OFF + SKF_AD_PROTOCOL),
315                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
316                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
317                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 30, 1, 0),
318                         BPF_STMT(BPF_RET | BPF_K, 0),
319                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
320                         BPF_STMT(BPF_RET | BPF_A, 0)
321                 },
322                 CLASSIC,
323                 { 10, 20, 30 },
324                 { { 10, ETH_P_IP }, { 100, ETH_P_IP } },
325         },
326         {
327                 "LD_VLAN_TAG",
328                 .u.insns = {
329                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
330                                  SKF_AD_OFF + SKF_AD_VLAN_TAG),
331                         BPF_STMT(BPF_RET | BPF_A, 0)
332                 },
333                 CLASSIC,
334                 { },
335                 {
336                         { 1, SKB_VLAN_TCI & ~VLAN_TAG_PRESENT },
337                         { 10, SKB_VLAN_TCI & ~VLAN_TAG_PRESENT }
338                 },
339         },
340         {
341                 "LD_VLAN_TAG_PRESENT",
342                 .u.insns = {
343                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
344                                  SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT),
345                         BPF_STMT(BPF_RET | BPF_A, 0)
346                 },
347                 CLASSIC,
348                 { },
349                 {
350                         { 1, !!(SKB_VLAN_TCI & VLAN_TAG_PRESENT) },
351                         { 10, !!(SKB_VLAN_TCI & VLAN_TAG_PRESENT) }
352                 },
353         },
354         {
355                 "LD_IFINDEX",
356                 .u.insns = {
357                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
358                                  SKF_AD_OFF + SKF_AD_IFINDEX),
359                         BPF_STMT(BPF_RET | BPF_A, 0)
360                 },
361                 CLASSIC,
362                 { },
363                 { { 1, SKB_DEV_IFINDEX }, { 10, SKB_DEV_IFINDEX } },
364         },
365         {
366                 "LD_HATYPE",
367                 .u.insns = {
368                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
369                                  SKF_AD_OFF + SKF_AD_HATYPE),
370                         BPF_STMT(BPF_RET | BPF_A, 0)
371                 },
372                 CLASSIC,
373                 { },
374                 { { 1, SKB_DEV_TYPE }, { 10, SKB_DEV_TYPE } },
375         },
376         {
377                 "LD_CPU",
378                 .u.insns = {
379                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
380                                  SKF_AD_OFF + SKF_AD_CPU),
381                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
382                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
383                                  SKF_AD_OFF + SKF_AD_CPU),
384                         BPF_STMT(BPF_ALU | BPF_SUB | BPF_X, 0),
385                         BPF_STMT(BPF_RET | BPF_A, 0)
386                 },
387                 CLASSIC,
388                 { },
389                 { { 1, 0 }, { 10, 0 } },
390         },
391         {
392                 "LD_NLATTR",
393                 .u.insns = {
394                         BPF_STMT(BPF_LDX | BPF_IMM, 2),
395                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
396                         BPF_STMT(BPF_LDX | BPF_IMM, 3),
397                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
398                                  SKF_AD_OFF + SKF_AD_NLATTR),
399                         BPF_STMT(BPF_RET | BPF_A, 0)
400                 },
401                 CLASSIC,
402 #ifdef __BIG_ENDIAN
403                 { 0xff, 0xff, 0, 4, 0, 2, 0, 4, 0, 3 },
404 #else
405                 { 0xff, 0xff, 4, 0, 2, 0, 4, 0, 3, 0 },
406 #endif
407                 { { 4, 0 }, { 20, 6 } },
408         },
409         {
410                 "LD_NLATTR_NEST",
411                 .u.insns = {
412                         BPF_STMT(BPF_LD | BPF_IMM, 2),
413                         BPF_STMT(BPF_LDX | BPF_IMM, 3),
414                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
415                                  SKF_AD_OFF + SKF_AD_NLATTR_NEST),
416                         BPF_STMT(BPF_LD | BPF_IMM, 2),
417                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
418                                  SKF_AD_OFF + SKF_AD_NLATTR_NEST),
419                         BPF_STMT(BPF_LD | BPF_IMM, 2),
420                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
421                                  SKF_AD_OFF + SKF_AD_NLATTR_NEST),
422                         BPF_STMT(BPF_LD | BPF_IMM, 2),
423                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
424                                  SKF_AD_OFF + SKF_AD_NLATTR_NEST),
425                         BPF_STMT(BPF_LD | BPF_IMM, 2),
426                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
427                                  SKF_AD_OFF + SKF_AD_NLATTR_NEST),
428                         BPF_STMT(BPF_LD | BPF_IMM, 2),
429                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
430                                  SKF_AD_OFF + SKF_AD_NLATTR_NEST),
431                         BPF_STMT(BPF_LD | BPF_IMM, 2),
432                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
433                                  SKF_AD_OFF + SKF_AD_NLATTR_NEST),
434                         BPF_STMT(BPF_LD | BPF_IMM, 2),
435                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
436                                  SKF_AD_OFF + SKF_AD_NLATTR_NEST),
437                         BPF_STMT(BPF_RET | BPF_A, 0)
438                 },
439                 CLASSIC,
440 #ifdef __BIG_ENDIAN
441                 { 0xff, 0xff, 0, 12, 0, 1, 0, 4, 0, 2, 0, 4, 0, 3 },
442 #else
443                 { 0xff, 0xff, 12, 0, 1, 0, 4, 0, 2, 0, 4, 0, 3, 0 },
444 #endif
445                 { { 4, 0 }, { 20, 10 } },
446         },
447         {
448                 "LD_PAYLOAD_OFF",
449                 .u.insns = {
450                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
451                                  SKF_AD_OFF + SKF_AD_PAY_OFFSET),
452                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
453                                  SKF_AD_OFF + SKF_AD_PAY_OFFSET),
454                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
455                                  SKF_AD_OFF + SKF_AD_PAY_OFFSET),
456                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
457                                  SKF_AD_OFF + SKF_AD_PAY_OFFSET),
458                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
459                                  SKF_AD_OFF + SKF_AD_PAY_OFFSET),
460                         BPF_STMT(BPF_RET | BPF_A, 0)
461                 },
462                 CLASSIC,
463                 /* 00:00:00:00:00:00 > 00:00:00:00:00:00, ethtype IPv4 (0x0800),
464                  * length 98: 127.0.0.1 > 127.0.0.1: ICMP echo request,
465                  * id 9737, seq 1, length 64
466                  */
467                 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
468                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
469                   0x08, 0x00,
470                   0x45, 0x00, 0x00, 0x54, 0xac, 0x8b, 0x40, 0x00, 0x40,
471                   0x01, 0x90, 0x1b, 0x7f, 0x00, 0x00, 0x01 },
472                 { { 30, 0 }, { 100, 42 } },
473         },
474         {
475                 "LD_ANC_XOR",
476                 .u.insns = {
477                         BPF_STMT(BPF_LD | BPF_IMM, 10),
478                         BPF_STMT(BPF_LDX | BPF_IMM, 300),
479                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
480                                  SKF_AD_OFF + SKF_AD_ALU_XOR_X),
481                         BPF_STMT(BPF_RET | BPF_A, 0)
482                 },
483                 CLASSIC,
484                 { },
485                 { { 4, 10 ^ 300 }, { 20, 10 ^ 300 } },
486         },
487         {
488                 "SPILL_FILL",
489                 .u.insns = {
490                         BPF_STMT(BPF_LDX | BPF_LEN, 0),
491                         BPF_STMT(BPF_LD | BPF_IMM, 2),
492                         BPF_STMT(BPF_ALU | BPF_RSH, 1),
493                         BPF_STMT(BPF_ALU | BPF_XOR | BPF_X, 0),
494                         BPF_STMT(BPF_ST, 1), /* M1 = 1 ^ len */
495                         BPF_STMT(BPF_ALU | BPF_XOR | BPF_K, 0x80000000),
496                         BPF_STMT(BPF_ST, 2), /* M2 = 1 ^ len ^ 0x80000000 */
497                         BPF_STMT(BPF_STX, 15), /* M3 = len */
498                         BPF_STMT(BPF_LDX | BPF_MEM, 1),
499                         BPF_STMT(BPF_LD | BPF_MEM, 2),
500                         BPF_STMT(BPF_ALU | BPF_XOR | BPF_X, 0),
501                         BPF_STMT(BPF_LDX | BPF_MEM, 15),
502                         BPF_STMT(BPF_ALU | BPF_XOR | BPF_X, 0),
503                         BPF_STMT(BPF_RET | BPF_A, 0)
504                 },
505                 CLASSIC,
506                 { },
507                 { { 1, 0x80000001 }, { 2, 0x80000002 }, { 60, 0x80000000 ^ 60 } }
508         },
509         {
510                 "JEQ",
511                 .u.insns = {
512                         BPF_STMT(BPF_LDX | BPF_LEN, 0),
513                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
514                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 1),
515                         BPF_STMT(BPF_RET | BPF_K, 1),
516                         BPF_STMT(BPF_RET | BPF_K, MAX_K)
517                 },
518                 CLASSIC,
519                 { 3, 3, 3, 3, 3 },
520                 { { 1, 0 }, { 3, 1 }, { 4, MAX_K } },
521         },
522         {
523                 "JGT",
524                 .u.insns = {
525                         BPF_STMT(BPF_LDX | BPF_LEN, 0),
526                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
527                         BPF_JUMP(BPF_JMP | BPF_JGT | BPF_X, 0, 0, 1),
528                         BPF_STMT(BPF_RET | BPF_K, 1),
529                         BPF_STMT(BPF_RET | BPF_K, MAX_K)
530                 },
531                 CLASSIC,
532                 { 4, 4, 4, 3, 3 },
533                 { { 2, 0 }, { 3, 1 }, { 4, MAX_K } },
534         },
535         {
536                 "JGE",
537                 .u.insns = {
538                         BPF_STMT(BPF_LDX | BPF_LEN, 0),
539                         BPF_STMT(BPF_LD | BPF_B | BPF_IND, MAX_K),
540                         BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 1, 1, 0),
541                         BPF_STMT(BPF_RET | BPF_K, 10),
542                         BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 2, 1, 0),
543                         BPF_STMT(BPF_RET | BPF_K, 20),
544                         BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 3, 1, 0),
545                         BPF_STMT(BPF_RET | BPF_K, 30),
546                         BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 4, 1, 0),
547                         BPF_STMT(BPF_RET | BPF_K, 40),
548                         BPF_STMT(BPF_RET | BPF_K, MAX_K)
549                 },
550                 CLASSIC,
551                 { 1, 2, 3, 4, 5 },
552                 { { 1, 20 }, { 3, 40 }, { 5, MAX_K } },
553         },
554         {
555                 "JSET",
556                 .u.insns = {
557                         BPF_JUMP(BPF_JMP | BPF_JA, 0, 0, 0),
558                         BPF_JUMP(BPF_JMP | BPF_JA, 1, 1, 1),
559                         BPF_JUMP(BPF_JMP | BPF_JA, 0, 0, 0),
560                         BPF_JUMP(BPF_JMP | BPF_JA, 0, 0, 0),
561                         BPF_STMT(BPF_LDX | BPF_LEN, 0),
562                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
563                         BPF_STMT(BPF_ALU | BPF_SUB | BPF_K, 4),
564                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
565                         BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
566                         BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 1, 0, 1),
567                         BPF_STMT(BPF_RET | BPF_K, 10),
568                         BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0x80000000, 0, 1),
569                         BPF_STMT(BPF_RET | BPF_K, 20),
570                         BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0xffffff, 1, 0),
571                         BPF_STMT(BPF_RET | BPF_K, 30),
572                         BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0xffffff, 1, 0),
573                         BPF_STMT(BPF_RET | BPF_K, 30),
574                         BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0xffffff, 1, 0),
575                         BPF_STMT(BPF_RET | BPF_K, 30),
576                         BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0xffffff, 1, 0),
577                         BPF_STMT(BPF_RET | BPF_K, 30),
578                         BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0xffffff, 1, 0),
579                         BPF_STMT(BPF_RET | BPF_K, 30),
580                         BPF_STMT(BPF_RET | BPF_K, MAX_K)
581                 },
582                 CLASSIC,
583                 { 0, 0xAA, 0x55, 1 },
584                 { { 4, 10 }, { 5, 20 }, { 6, MAX_K } },
585         },
586         {
587                 "tcpdump port 22",
588                 .u.insns = {
589                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
590                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x86dd, 0, 8), /* IPv6 */
591                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 20),
592                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x84, 2, 0),
593                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x6, 1, 0),
594                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x11, 0, 17),
595                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 54),
596                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 14, 0),
597                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 56),
598                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 12, 13),
599                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0800, 0, 12), /* IPv4 */
600                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 23),
601                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x84, 2, 0),
602                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x6, 1, 0),
603                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x11, 0, 8),
604                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
605                         BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0x1fff, 6, 0),
606                         BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 14),
607                         BPF_STMT(BPF_LD | BPF_H | BPF_IND, 14),
608                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 2, 0),
609                         BPF_STMT(BPF_LD | BPF_H | BPF_IND, 16),
610                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 0, 1),
611                         BPF_STMT(BPF_RET | BPF_K, 0xffff),
612                         BPF_STMT(BPF_RET | BPF_K, 0),
613                 },
614                 CLASSIC,
615                 /* 3c:07:54:43:e5:76 > 10:bf:48:d6:43:d6, ethertype IPv4(0x0800)
616                  * length 114: 10.1.1.149.49700 > 10.1.2.10.22: Flags [P.],
617                  * seq 1305692979:1305693027, ack 3650467037, win 65535,
618                  * options [nop,nop,TS val 2502645400 ecr 3971138], length 48
619                  */
620                 { 0x10, 0xbf, 0x48, 0xd6, 0x43, 0xd6,
621                   0x3c, 0x07, 0x54, 0x43, 0xe5, 0x76,
622                   0x08, 0x00,
623                   0x45, 0x10, 0x00, 0x64, 0x75, 0xb5,
624                   0x40, 0x00, 0x40, 0x06, 0xad, 0x2e, /* IP header */
625                   0x0a, 0x01, 0x01, 0x95, /* ip src */
626                   0x0a, 0x01, 0x02, 0x0a, /* ip dst */
627                   0xc2, 0x24,
628                   0x00, 0x16 /* dst port */ },
629                 { { 10, 0 }, { 30, 0 }, { 100, 65535 } },
630         },
631         {
632                 "tcpdump complex",
633                 .u.insns = {
634                         /* tcpdump -nei eth0 'tcp port 22 and (((ip[2:2] -
635                          * ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0) and
636                          * (len > 115 or len < 30000000000)' -d
637                          */
638                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
639                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x86dd, 30, 0),
640                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x800, 0, 29),
641                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 23),
642                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x6, 0, 27),
643                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
644                         BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0x1fff, 25, 0),
645                         BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 14),
646                         BPF_STMT(BPF_LD | BPF_H | BPF_IND, 14),
647                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 2, 0),
648                         BPF_STMT(BPF_LD | BPF_H | BPF_IND, 16),
649                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 0, 20),
650                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 16),
651                         BPF_STMT(BPF_ST, 1),
652                         BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 14),
653                         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xf),
654                         BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 2),
655                         BPF_STMT(BPF_MISC | BPF_TAX, 0x5), /* libpcap emits K on TAX */
656                         BPF_STMT(BPF_LD | BPF_MEM, 1),
657                         BPF_STMT(BPF_ALU | BPF_SUB | BPF_X, 0),
658                         BPF_STMT(BPF_ST, 5),
659                         BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 14),
660                         BPF_STMT(BPF_LD | BPF_B | BPF_IND, 26),
661                         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xf0),
662                         BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 2),
663                         BPF_STMT(BPF_MISC | BPF_TAX, 0x9), /* libpcap emits K on TAX */
664                         BPF_STMT(BPF_LD | BPF_MEM, 5),
665                         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, 4, 0),
666                         BPF_STMT(BPF_LD | BPF_LEN, 0),
667                         BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, 0x73, 1, 0),
668                         BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 0xfc23ac00, 1, 0),
669                         BPF_STMT(BPF_RET | BPF_K, 0xffff),
670                         BPF_STMT(BPF_RET | BPF_K, 0),
671                 },
672                 CLASSIC,
673                 { 0x10, 0xbf, 0x48, 0xd6, 0x43, 0xd6,
674                   0x3c, 0x07, 0x54, 0x43, 0xe5, 0x76,
675                   0x08, 0x00,
676                   0x45, 0x10, 0x00, 0x64, 0x75, 0xb5,
677                   0x40, 0x00, 0x40, 0x06, 0xad, 0x2e, /* IP header */
678                   0x0a, 0x01, 0x01, 0x95, /* ip src */
679                   0x0a, 0x01, 0x02, 0x0a, /* ip dst */
680                   0xc2, 0x24,
681                   0x00, 0x16 /* dst port */ },
682                 { { 10, 0 }, { 30, 0 }, { 100, 65535 } },
683         },
684         {
685                 "RET_A",
686                 .u.insns = {
687                         /* check that unitialized X and A contain zeros */
688                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
689                         BPF_STMT(BPF_RET | BPF_A, 0)
690                 },
691                 CLASSIC,
692                 { },
693                 { {1, 0}, {2, 0} },
694         },
695         {
696                 "INT: ADD trivial",
697                 .u.insns_int = {
698                         BPF_ALU64_IMM(BPF_MOV, R1, 1),
699                         BPF_ALU64_IMM(BPF_ADD, R1, 2),
700                         BPF_ALU64_IMM(BPF_MOV, R2, 3),
701                         BPF_ALU64_REG(BPF_SUB, R1, R2),
702                         BPF_ALU64_IMM(BPF_ADD, R1, -1),
703                         BPF_ALU64_IMM(BPF_MUL, R1, 3),
704                         BPF_ALU64_REG(BPF_MOV, R0, R1),
705                         BPF_EXIT_INSN(),
706                 },
707                 INTERNAL,
708                 { },
709                 { { 0, 0xfffffffd } }
710         },
711         {
712                 "INT: MUL_X",
713                 .u.insns_int = {
714                         BPF_ALU64_IMM(BPF_MOV, R0, -1),
715                         BPF_ALU64_IMM(BPF_MOV, R1, -1),
716                         BPF_ALU64_IMM(BPF_MOV, R2, 3),
717                         BPF_ALU64_REG(BPF_MUL, R1, R2),
718                         BPF_JMP_IMM(BPF_JEQ, R1, 0xfffffffd, 1),
719                         BPF_EXIT_INSN(),
720                         BPF_ALU64_IMM(BPF_MOV, R0, 1),
721                         BPF_EXIT_INSN(),
722                 },
723                 INTERNAL,
724                 { },
725                 { { 0, 1 } }
726         },
727         {
728                 "INT: MUL_X2",
729                 .u.insns_int = {
730                         BPF_ALU32_IMM(BPF_MOV, R0, -1),
731                         BPF_ALU32_IMM(BPF_MOV, R1, -1),
732                         BPF_ALU32_IMM(BPF_MOV, R2, 3),
733                         BPF_ALU64_REG(BPF_MUL, R1, R2),
734                         BPF_ALU64_IMM(BPF_RSH, R1, 8),
735                         BPF_JMP_IMM(BPF_JEQ, R1, 0x2ffffff, 1),
736                         BPF_EXIT_INSN(),
737                         BPF_ALU32_IMM(BPF_MOV, R0, 1),
738                         BPF_EXIT_INSN(),
739                 },
740                 INTERNAL,
741                 { },
742                 { { 0, 1 } }
743         },
744         {
745                 "INT: MUL32_X",
746                 .u.insns_int = {
747                         BPF_ALU32_IMM(BPF_MOV, R0, -1),
748                         BPF_ALU64_IMM(BPF_MOV, R1, -1),
749                         BPF_ALU32_IMM(BPF_MOV, R2, 3),
750                         BPF_ALU32_REG(BPF_MUL, R1, R2),
751                         BPF_ALU64_IMM(BPF_RSH, R1, 8),
752                         BPF_JMP_IMM(BPF_JEQ, R1, 0xffffff, 1),
753                         BPF_EXIT_INSN(),
754                         BPF_ALU32_IMM(BPF_MOV, R0, 1),
755                         BPF_EXIT_INSN(),
756                 },
757                 INTERNAL,
758                 { },
759                 { { 0, 1 } }
760         },
761         {
762                 /* Have to test all register combinations, since
763                  * JITing of different registers will produce
764                  * different asm code.
765                  */
766                 "INT: ADD 64-bit",
767                 .u.insns_int = {
768                         BPF_ALU64_IMM(BPF_MOV, R0, 0),
769                         BPF_ALU64_IMM(BPF_MOV, R1, 1),
770                         BPF_ALU64_IMM(BPF_MOV, R2, 2),
771                         BPF_ALU64_IMM(BPF_MOV, R3, 3),
772                         BPF_ALU64_IMM(BPF_MOV, R4, 4),
773                         BPF_ALU64_IMM(BPF_MOV, R5, 5),
774                         BPF_ALU64_IMM(BPF_MOV, R6, 6),
775                         BPF_ALU64_IMM(BPF_MOV, R7, 7),
776                         BPF_ALU64_IMM(BPF_MOV, R8, 8),
777                         BPF_ALU64_IMM(BPF_MOV, R9, 9),
778                         BPF_ALU64_IMM(BPF_ADD, R0, 20),
779                         BPF_ALU64_IMM(BPF_ADD, R1, 20),
780                         BPF_ALU64_IMM(BPF_ADD, R2, 20),
781                         BPF_ALU64_IMM(BPF_ADD, R3, 20),
782                         BPF_ALU64_IMM(BPF_ADD, R4, 20),
783                         BPF_ALU64_IMM(BPF_ADD, R5, 20),
784                         BPF_ALU64_IMM(BPF_ADD, R6, 20),
785                         BPF_ALU64_IMM(BPF_ADD, R7, 20),
786                         BPF_ALU64_IMM(BPF_ADD, R8, 20),
787                         BPF_ALU64_IMM(BPF_ADD, R9, 20),
788                         BPF_ALU64_IMM(BPF_SUB, R0, 10),
789                         BPF_ALU64_IMM(BPF_SUB, R1, 10),
790                         BPF_ALU64_IMM(BPF_SUB, R2, 10),
791                         BPF_ALU64_IMM(BPF_SUB, R3, 10),
792                         BPF_ALU64_IMM(BPF_SUB, R4, 10),
793                         BPF_ALU64_IMM(BPF_SUB, R5, 10),
794                         BPF_ALU64_IMM(BPF_SUB, R6, 10),
795                         BPF_ALU64_IMM(BPF_SUB, R7, 10),
796                         BPF_ALU64_IMM(BPF_SUB, R8, 10),
797                         BPF_ALU64_IMM(BPF_SUB, R9, 10),
798                         BPF_ALU64_REG(BPF_ADD, R0, R0),
799                         BPF_ALU64_REG(BPF_ADD, R0, R1),
800                         BPF_ALU64_REG(BPF_ADD, R0, R2),
801                         BPF_ALU64_REG(BPF_ADD, R0, R3),
802                         BPF_ALU64_REG(BPF_ADD, R0, R4),
803                         BPF_ALU64_REG(BPF_ADD, R0, R5),
804                         BPF_ALU64_REG(BPF_ADD, R0, R6),
805                         BPF_ALU64_REG(BPF_ADD, R0, R7),
806                         BPF_ALU64_REG(BPF_ADD, R0, R8),
807                         BPF_ALU64_REG(BPF_ADD, R0, R9), /* R0 == 155 */
808                         BPF_JMP_IMM(BPF_JEQ, R0, 155, 1),
809                         BPF_EXIT_INSN(),
810                         BPF_ALU64_REG(BPF_ADD, R1, R0),
811                         BPF_ALU64_REG(BPF_ADD, R1, R1),
812                         BPF_ALU64_REG(BPF_ADD, R1, R2),
813                         BPF_ALU64_REG(BPF_ADD, R1, R3),
814                         BPF_ALU64_REG(BPF_ADD, R1, R4),
815                         BPF_ALU64_REG(BPF_ADD, R1, R5),
816                         BPF_ALU64_REG(BPF_ADD, R1, R6),
817                         BPF_ALU64_REG(BPF_ADD, R1, R7),
818                         BPF_ALU64_REG(BPF_ADD, R1, R8),
819                         BPF_ALU64_REG(BPF_ADD, R1, R9), /* R1 == 456 */
820                         BPF_JMP_IMM(BPF_JEQ, R1, 456, 1),
821                         BPF_EXIT_INSN(),
822                         BPF_ALU64_REG(BPF_ADD, R2, R0),
823                         BPF_ALU64_REG(BPF_ADD, R2, R1),
824                         BPF_ALU64_REG(BPF_ADD, R2, R2),
825                         BPF_ALU64_REG(BPF_ADD, R2, R3),
826                         BPF_ALU64_REG(BPF_ADD, R2, R4),
827                         BPF_ALU64_REG(BPF_ADD, R2, R5),
828                         BPF_ALU64_REG(BPF_ADD, R2, R6),
829                         BPF_ALU64_REG(BPF_ADD, R2, R7),
830                         BPF_ALU64_REG(BPF_ADD, R2, R8),
831                         BPF_ALU64_REG(BPF_ADD, R2, R9), /* R2 == 1358 */
832                         BPF_JMP_IMM(BPF_JEQ, R2, 1358, 1),
833                         BPF_EXIT_INSN(),
834                         BPF_ALU64_REG(BPF_ADD, R3, R0),
835                         BPF_ALU64_REG(BPF_ADD, R3, R1),
836                         BPF_ALU64_REG(BPF_ADD, R3, R2),
837                         BPF_ALU64_REG(BPF_ADD, R3, R3),
838                         BPF_ALU64_REG(BPF_ADD, R3, R4),
839                         BPF_ALU64_REG(BPF_ADD, R3, R5),
840                         BPF_ALU64_REG(BPF_ADD, R3, R6),
841                         BPF_ALU64_REG(BPF_ADD, R3, R7),
842                         BPF_ALU64_REG(BPF_ADD, R3, R8),
843                         BPF_ALU64_REG(BPF_ADD, R3, R9), /* R3 == 4063 */
844                         BPF_JMP_IMM(BPF_JEQ, R3, 4063, 1),
845                         BPF_EXIT_INSN(),
846                         BPF_ALU64_REG(BPF_ADD, R4, R0),
847                         BPF_ALU64_REG(BPF_ADD, R4, R1),
848                         BPF_ALU64_REG(BPF_ADD, R4, R2),
849                         BPF_ALU64_REG(BPF_ADD, R4, R3),
850                         BPF_ALU64_REG(BPF_ADD, R4, R4),
851                         BPF_ALU64_REG(BPF_ADD, R4, R5),
852                         BPF_ALU64_REG(BPF_ADD, R4, R6),
853                         BPF_ALU64_REG(BPF_ADD, R4, R7),
854                         BPF_ALU64_REG(BPF_ADD, R4, R8),
855                         BPF_ALU64_REG(BPF_ADD, R4, R9), /* R4 == 12177 */
856                         BPF_JMP_IMM(BPF_JEQ, R4, 12177, 1),
857                         BPF_EXIT_INSN(),
858                         BPF_ALU64_REG(BPF_ADD, R5, R0),
859                         BPF_ALU64_REG(BPF_ADD, R5, R1),
860                         BPF_ALU64_REG(BPF_ADD, R5, R2),
861                         BPF_ALU64_REG(BPF_ADD, R5, R3),
862                         BPF_ALU64_REG(BPF_ADD, R5, R4),
863                         BPF_ALU64_REG(BPF_ADD, R5, R5),
864                         BPF_ALU64_REG(BPF_ADD, R5, R6),
865                         BPF_ALU64_REG(BPF_ADD, R5, R7),
866                         BPF_ALU64_REG(BPF_ADD, R5, R8),
867                         BPF_ALU64_REG(BPF_ADD, R5, R9), /* R5 == 36518 */
868                         BPF_JMP_IMM(BPF_JEQ, R5, 36518, 1),
869                         BPF_EXIT_INSN(),
870                         BPF_ALU64_REG(BPF_ADD, R6, R0),
871                         BPF_ALU64_REG(BPF_ADD, R6, R1),
872                         BPF_ALU64_REG(BPF_ADD, R6, R2),
873                         BPF_ALU64_REG(BPF_ADD, R6, R3),
874                         BPF_ALU64_REG(BPF_ADD, R6, R4),
875                         BPF_ALU64_REG(BPF_ADD, R6, R5),
876                         BPF_ALU64_REG(BPF_ADD, R6, R6),
877                         BPF_ALU64_REG(BPF_ADD, R6, R7),
878                         BPF_ALU64_REG(BPF_ADD, R6, R8),
879                         BPF_ALU64_REG(BPF_ADD, R6, R9), /* R6 == 109540 */
880                         BPF_JMP_IMM(BPF_JEQ, R6, 109540, 1),
881                         BPF_EXIT_INSN(),
882                         BPF_ALU64_REG(BPF_ADD, R7, R0),
883                         BPF_ALU64_REG(BPF_ADD, R7, R1),
884                         BPF_ALU64_REG(BPF_ADD, R7, R2),
885                         BPF_ALU64_REG(BPF_ADD, R7, R3),
886                         BPF_ALU64_REG(BPF_ADD, R7, R4),
887                         BPF_ALU64_REG(BPF_ADD, R7, R5),
888                         BPF_ALU64_REG(BPF_ADD, R7, R6),
889                         BPF_ALU64_REG(BPF_ADD, R7, R7),
890                         BPF_ALU64_REG(BPF_ADD, R7, R8),
891                         BPF_ALU64_REG(BPF_ADD, R7, R9), /* R7 == 328605 */
892                         BPF_JMP_IMM(BPF_JEQ, R7, 328605, 1),
893                         BPF_EXIT_INSN(),
894                         BPF_ALU64_REG(BPF_ADD, R8, R0),
895                         BPF_ALU64_REG(BPF_ADD, R8, R1),
896                         BPF_ALU64_REG(BPF_ADD, R8, R2),
897                         BPF_ALU64_REG(BPF_ADD, R8, R3),
898                         BPF_ALU64_REG(BPF_ADD, R8, R4),
899                         BPF_ALU64_REG(BPF_ADD, R8, R5),
900                         BPF_ALU64_REG(BPF_ADD, R8, R6),
901                         BPF_ALU64_REG(BPF_ADD, R8, R7),
902                         BPF_ALU64_REG(BPF_ADD, R8, R8),
903                         BPF_ALU64_REG(BPF_ADD, R8, R9), /* R8 == 985799 */
904                         BPF_JMP_IMM(BPF_JEQ, R8, 985799, 1),
905                         BPF_EXIT_INSN(),
906                         BPF_ALU64_REG(BPF_ADD, R9, R0),
907                         BPF_ALU64_REG(BPF_ADD, R9, R1),
908                         BPF_ALU64_REG(BPF_ADD, R9, R2),
909                         BPF_ALU64_REG(BPF_ADD, R9, R3),
910                         BPF_ALU64_REG(BPF_ADD, R9, R4),
911                         BPF_ALU64_REG(BPF_ADD, R9, R5),
912                         BPF_ALU64_REG(BPF_ADD, R9, R6),
913                         BPF_ALU64_REG(BPF_ADD, R9, R7),
914                         BPF_ALU64_REG(BPF_ADD, R9, R8),
915                         BPF_ALU64_REG(BPF_ADD, R9, R9), /* R9 == 2957380 */
916                         BPF_ALU64_REG(BPF_MOV, R0, R9),
917                         BPF_EXIT_INSN(),
918                 },
919                 INTERNAL,
920                 { },
921                 { { 0, 2957380 } }
922         },
923         {
924                 "INT: ADD 32-bit",
925                 .u.insns_int = {
926                         BPF_ALU32_IMM(BPF_MOV, R0, 20),
927                         BPF_ALU32_IMM(BPF_MOV, R1, 1),
928                         BPF_ALU32_IMM(BPF_MOV, R2, 2),
929                         BPF_ALU32_IMM(BPF_MOV, R3, 3),
930                         BPF_ALU32_IMM(BPF_MOV, R4, 4),
931                         BPF_ALU32_IMM(BPF_MOV, R5, 5),
932                         BPF_ALU32_IMM(BPF_MOV, R6, 6),
933                         BPF_ALU32_IMM(BPF_MOV, R7, 7),
934                         BPF_ALU32_IMM(BPF_MOV, R8, 8),
935                         BPF_ALU32_IMM(BPF_MOV, R9, 9),
936                         BPF_ALU64_IMM(BPF_ADD, R1, 10),
937                         BPF_ALU64_IMM(BPF_ADD, R2, 10),
938                         BPF_ALU64_IMM(BPF_ADD, R3, 10),
939                         BPF_ALU64_IMM(BPF_ADD, R4, 10),
940                         BPF_ALU64_IMM(BPF_ADD, R5, 10),
941                         BPF_ALU64_IMM(BPF_ADD, R6, 10),
942                         BPF_ALU64_IMM(BPF_ADD, R7, 10),
943                         BPF_ALU64_IMM(BPF_ADD, R8, 10),
944                         BPF_ALU64_IMM(BPF_ADD, R9, 10),
945                         BPF_ALU32_REG(BPF_ADD, R0, R1),
946                         BPF_ALU32_REG(BPF_ADD, R0, R2),
947                         BPF_ALU32_REG(BPF_ADD, R0, R3),
948                         BPF_ALU32_REG(BPF_ADD, R0, R4),
949                         BPF_ALU32_REG(BPF_ADD, R0, R5),
950                         BPF_ALU32_REG(BPF_ADD, R0, R6),
951                         BPF_ALU32_REG(BPF_ADD, R0, R7),
952                         BPF_ALU32_REG(BPF_ADD, R0, R8),
953                         BPF_ALU32_REG(BPF_ADD, R0, R9), /* R0 == 155 */
954                         BPF_JMP_IMM(BPF_JEQ, R0, 155, 1),
955                         BPF_EXIT_INSN(),
956                         BPF_ALU32_REG(BPF_ADD, R1, R0),
957                         BPF_ALU32_REG(BPF_ADD, R1, R1),
958                         BPF_ALU32_REG(BPF_ADD, R1, R2),
959                         BPF_ALU32_REG(BPF_ADD, R1, R3),
960                         BPF_ALU32_REG(BPF_ADD, R1, R4),
961                         BPF_ALU32_REG(BPF_ADD, R1, R5),
962                         BPF_ALU32_REG(BPF_ADD, R1, R6),
963                         BPF_ALU32_REG(BPF_ADD, R1, R7),
964                         BPF_ALU32_REG(BPF_ADD, R1, R8),
965                         BPF_ALU32_REG(BPF_ADD, R1, R9), /* R1 == 456 */
966                         BPF_JMP_IMM(BPF_JEQ, R1, 456, 1),
967                         BPF_EXIT_INSN(),
968                         BPF_ALU32_REG(BPF_ADD, R2, R0),
969                         BPF_ALU32_REG(BPF_ADD, R2, R1),
970                         BPF_ALU32_REG(BPF_ADD, R2, R2),
971                         BPF_ALU32_REG(BPF_ADD, R2, R3),
972                         BPF_ALU32_REG(BPF_ADD, R2, R4),
973                         BPF_ALU32_REG(BPF_ADD, R2, R5),
974                         BPF_ALU32_REG(BPF_ADD, R2, R6),
975                         BPF_ALU32_REG(BPF_ADD, R2, R7),
976                         BPF_ALU32_REG(BPF_ADD, R2, R8),
977                         BPF_ALU32_REG(BPF_ADD, R2, R9), /* R2 == 1358 */
978                         BPF_JMP_IMM(BPF_JEQ, R2, 1358, 1),
979                         BPF_EXIT_INSN(),
980                         BPF_ALU32_REG(BPF_ADD, R3, R0),
981                         BPF_ALU32_REG(BPF_ADD, R3, R1),
982                         BPF_ALU32_REG(BPF_ADD, R3, R2),
983                         BPF_ALU32_REG(BPF_ADD, R3, R3),
984                         BPF_ALU32_REG(BPF_ADD, R3, R4),
985                         BPF_ALU32_REG(BPF_ADD, R3, R5),
986                         BPF_ALU32_REG(BPF_ADD, R3, R6),
987                         BPF_ALU32_REG(BPF_ADD, R3, R7),
988                         BPF_ALU32_REG(BPF_ADD, R3, R8),
989                         BPF_ALU32_REG(BPF_ADD, R3, R9), /* R3 == 4063 */
990                         BPF_JMP_IMM(BPF_JEQ, R3, 4063, 1),
991                         BPF_EXIT_INSN(),
992                         BPF_ALU32_REG(BPF_ADD, R4, R0),
993                         BPF_ALU32_REG(BPF_ADD, R4, R1),
994                         BPF_ALU32_REG(BPF_ADD, R4, R2),
995                         BPF_ALU32_REG(BPF_ADD, R4, R3),
996                         BPF_ALU32_REG(BPF_ADD, R4, R4),
997                         BPF_ALU32_REG(BPF_ADD, R4, R5),
998                         BPF_ALU32_REG(BPF_ADD, R4, R6),
999                         BPF_ALU32_REG(BPF_ADD, R4, R7),
1000                         BPF_ALU32_REG(BPF_ADD, R4, R8),
1001                         BPF_ALU32_REG(BPF_ADD, R4, R9), /* R4 == 12177 */
1002                         BPF_JMP_IMM(BPF_JEQ, R4, 12177, 1),
1003                         BPF_EXIT_INSN(),
1004                         BPF_ALU32_REG(BPF_ADD, R5, R0),
1005                         BPF_ALU32_REG(BPF_ADD, R5, R1),
1006                         BPF_ALU32_REG(BPF_ADD, R5, R2),
1007                         BPF_ALU32_REG(BPF_ADD, R5, R3),
1008                         BPF_ALU32_REG(BPF_ADD, R5, R4),
1009                         BPF_ALU32_REG(BPF_ADD, R5, R5),
1010                         BPF_ALU32_REG(BPF_ADD, R5, R6),
1011                         BPF_ALU32_REG(BPF_ADD, R5, R7),
1012                         BPF_ALU32_REG(BPF_ADD, R5, R8),
1013                         BPF_ALU32_REG(BPF_ADD, R5, R9), /* R5 == 36518 */
1014                         BPF_JMP_IMM(BPF_JEQ, R5, 36518, 1),
1015                         BPF_EXIT_INSN(),
1016                         BPF_ALU32_REG(BPF_ADD, R6, R0),
1017                         BPF_ALU32_REG(BPF_ADD, R6, R1),
1018                         BPF_ALU32_REG(BPF_ADD, R6, R2),
1019                         BPF_ALU32_REG(BPF_ADD, R6, R3),
1020                         BPF_ALU32_REG(BPF_ADD, R6, R4),
1021                         BPF_ALU32_REG(BPF_ADD, R6, R5),
1022                         BPF_ALU32_REG(BPF_ADD, R6, R6),
1023                         BPF_ALU32_REG(BPF_ADD, R6, R7),
1024                         BPF_ALU32_REG(BPF_ADD, R6, R8),
1025                         BPF_ALU32_REG(BPF_ADD, R6, R9), /* R6 == 109540 */
1026                         BPF_JMP_IMM(BPF_JEQ, R6, 109540, 1),
1027                         BPF_EXIT_INSN(),
1028                         BPF_ALU32_REG(BPF_ADD, R7, R0),
1029                         BPF_ALU32_REG(BPF_ADD, R7, R1),
1030                         BPF_ALU32_REG(BPF_ADD, R7, R2),
1031                         BPF_ALU32_REG(BPF_ADD, R7, R3),
1032                         BPF_ALU32_REG(BPF_ADD, R7, R4),
1033                         BPF_ALU32_REG(BPF_ADD, R7, R5),
1034                         BPF_ALU32_REG(BPF_ADD, R7, R6),
1035                         BPF_ALU32_REG(BPF_ADD, R7, R7),
1036                         BPF_ALU32_REG(BPF_ADD, R7, R8),
1037                         BPF_ALU32_REG(BPF_ADD, R7, R9), /* R7 == 328605 */
1038                         BPF_JMP_IMM(BPF_JEQ, R7, 328605, 1),
1039                         BPF_EXIT_INSN(),
1040                         BPF_ALU32_REG(BPF_ADD, R8, R0),
1041                         BPF_ALU32_REG(BPF_ADD, R8, R1),
1042                         BPF_ALU32_REG(BPF_ADD, R8, R2),
1043                         BPF_ALU32_REG(BPF_ADD, R8, R3),
1044                         BPF_ALU32_REG(BPF_ADD, R8, R4),
1045                         BPF_ALU32_REG(BPF_ADD, R8, R5),
1046                         BPF_ALU32_REG(BPF_ADD, R8, R6),
1047                         BPF_ALU32_REG(BPF_ADD, R8, R7),
1048                         BPF_ALU32_REG(BPF_ADD, R8, R8),
1049                         BPF_ALU32_REG(BPF_ADD, R8, R9), /* R8 == 985799 */
1050                         BPF_JMP_IMM(BPF_JEQ, R8, 985799, 1),
1051                         BPF_EXIT_INSN(),
1052                         BPF_ALU32_REG(BPF_ADD, R9, R0),
1053                         BPF_ALU32_REG(BPF_ADD, R9, R1),
1054                         BPF_ALU32_REG(BPF_ADD, R9, R2),
1055                         BPF_ALU32_REG(BPF_ADD, R9, R3),
1056                         BPF_ALU32_REG(BPF_ADD, R9, R4),
1057                         BPF_ALU32_REG(BPF_ADD, R9, R5),
1058                         BPF_ALU32_REG(BPF_ADD, R9, R6),
1059                         BPF_ALU32_REG(BPF_ADD, R9, R7),
1060                         BPF_ALU32_REG(BPF_ADD, R9, R8),
1061                         BPF_ALU32_REG(BPF_ADD, R9, R9), /* R9 == 2957380 */
1062                         BPF_ALU32_REG(BPF_MOV, R0, R9),
1063                         BPF_EXIT_INSN(),
1064                 },
1065                 INTERNAL,
1066                 { },
1067                 { { 0, 2957380 } }
1068         },
1069         {       /* Mainly checking JIT here. */
1070                 "INT: SUB",
1071                 .u.insns_int = {
1072                         BPF_ALU64_IMM(BPF_MOV, R0, 0),
1073                         BPF_ALU64_IMM(BPF_MOV, R1, 1),
1074                         BPF_ALU64_IMM(BPF_MOV, R2, 2),
1075                         BPF_ALU64_IMM(BPF_MOV, R3, 3),
1076                         BPF_ALU64_IMM(BPF_MOV, R4, 4),
1077                         BPF_ALU64_IMM(BPF_MOV, R5, 5),
1078                         BPF_ALU64_IMM(BPF_MOV, R6, 6),
1079                         BPF_ALU64_IMM(BPF_MOV, R7, 7),
1080                         BPF_ALU64_IMM(BPF_MOV, R8, 8),
1081                         BPF_ALU64_IMM(BPF_MOV, R9, 9),
1082                         BPF_ALU64_REG(BPF_SUB, R0, R0),
1083                         BPF_ALU64_REG(BPF_SUB, R0, R1),
1084                         BPF_ALU64_REG(BPF_SUB, R0, R2),
1085                         BPF_ALU64_REG(BPF_SUB, R0, R3),
1086                         BPF_ALU64_REG(BPF_SUB, R0, R4),
1087                         BPF_ALU64_REG(BPF_SUB, R0, R5),
1088                         BPF_ALU64_REG(BPF_SUB, R0, R6),
1089                         BPF_ALU64_REG(BPF_SUB, R0, R7),
1090                         BPF_ALU64_REG(BPF_SUB, R0, R8),
1091                         BPF_ALU64_REG(BPF_SUB, R0, R9),
1092                         BPF_ALU64_IMM(BPF_SUB, R0, 10),
1093                         BPF_JMP_IMM(BPF_JEQ, R0, -55, 1),
1094                         BPF_EXIT_INSN(),
1095                         BPF_ALU64_REG(BPF_SUB, R1, R0),
1096                         BPF_ALU64_REG(BPF_SUB, R1, R2),
1097                         BPF_ALU64_REG(BPF_SUB, R1, R3),
1098                         BPF_ALU64_REG(BPF_SUB, R1, R4),
1099                         BPF_ALU64_REG(BPF_SUB, R1, R5),
1100                         BPF_ALU64_REG(BPF_SUB, R1, R6),
1101                         BPF_ALU64_REG(BPF_SUB, R1, R7),
1102                         BPF_ALU64_REG(BPF_SUB, R1, R8),
1103                         BPF_ALU64_REG(BPF_SUB, R1, R9),
1104                         BPF_ALU64_IMM(BPF_SUB, R1, 10),
1105                         BPF_ALU64_REG(BPF_SUB, R2, R0),
1106                         BPF_ALU64_REG(BPF_SUB, R2, R1),
1107                         BPF_ALU64_REG(BPF_SUB, R2, R3),
1108                         BPF_ALU64_REG(BPF_SUB, R2, R4),
1109                         BPF_ALU64_REG(BPF_SUB, R2, R5),
1110                         BPF_ALU64_REG(BPF_SUB, R2, R6),
1111                         BPF_ALU64_REG(BPF_SUB, R2, R7),
1112                         BPF_ALU64_REG(BPF_SUB, R2, R8),
1113                         BPF_ALU64_REG(BPF_SUB, R2, R9),
1114                         BPF_ALU64_IMM(BPF_SUB, R2, 10),
1115                         BPF_ALU64_REG(BPF_SUB, R3, R0),
1116                         BPF_ALU64_REG(BPF_SUB, R3, R1),
1117                         BPF_ALU64_REG(BPF_SUB, R3, R2),
1118                         BPF_ALU64_REG(BPF_SUB, R3, R4),
1119                         BPF_ALU64_REG(BPF_SUB, R3, R5),
1120                         BPF_ALU64_REG(BPF_SUB, R3, R6),
1121                         BPF_ALU64_REG(BPF_SUB, R3, R7),
1122                         BPF_ALU64_REG(BPF_SUB, R3, R8),
1123                         BPF_ALU64_REG(BPF_SUB, R3, R9),
1124                         BPF_ALU64_IMM(BPF_SUB, R3, 10),
1125                         BPF_ALU64_REG(BPF_SUB, R4, R0),
1126                         BPF_ALU64_REG(BPF_SUB, R4, R1),
1127                         BPF_ALU64_REG(BPF_SUB, R4, R2),
1128                         BPF_ALU64_REG(BPF_SUB, R4, R3),
1129                         BPF_ALU64_REG(BPF_SUB, R4, R5),
1130                         BPF_ALU64_REG(BPF_SUB, R4, R6),
1131                         BPF_ALU64_REG(BPF_SUB, R4, R7),
1132                         BPF_ALU64_REG(BPF_SUB, R4, R8),
1133                         BPF_ALU64_REG(BPF_SUB, R4, R9),
1134                         BPF_ALU64_IMM(BPF_SUB, R4, 10),
1135                         BPF_ALU64_REG(BPF_SUB, R5, R0),
1136                         BPF_ALU64_REG(BPF_SUB, R5, R1),
1137                         BPF_ALU64_REG(BPF_SUB, R5, R2),
1138                         BPF_ALU64_REG(BPF_SUB, R5, R3),
1139                         BPF_ALU64_REG(BPF_SUB, R5, R4),
1140                         BPF_ALU64_REG(BPF_SUB, R5, R6),
1141                         BPF_ALU64_REG(BPF_SUB, R5, R7),
1142                         BPF_ALU64_REG(BPF_SUB, R5, R8),
1143                         BPF_ALU64_REG(BPF_SUB, R5, R9),
1144                         BPF_ALU64_IMM(BPF_SUB, R5, 10),
1145                         BPF_ALU64_REG(BPF_SUB, R6, R0),
1146                         BPF_ALU64_REG(BPF_SUB, R6, R1),
1147                         BPF_ALU64_REG(BPF_SUB, R6, R2),
1148                         BPF_ALU64_REG(BPF_SUB, R6, R3),
1149                         BPF_ALU64_REG(BPF_SUB, R6, R4),
1150                         BPF_ALU64_REG(BPF_SUB, R6, R5),
1151                         BPF_ALU64_REG(BPF_SUB, R6, R7),
1152                         BPF_ALU64_REG(BPF_SUB, R6, R8),
1153                         BPF_ALU64_REG(BPF_SUB, R6, R9),
1154                         BPF_ALU64_IMM(BPF_SUB, R6, 10),
1155                         BPF_ALU64_REG(BPF_SUB, R7, R0),
1156                         BPF_ALU64_REG(BPF_SUB, R7, R1),
1157                         BPF_ALU64_REG(BPF_SUB, R7, R2),
1158                         BPF_ALU64_REG(BPF_SUB, R7, R3),
1159                         BPF_ALU64_REG(BPF_SUB, R7, R4),
1160                         BPF_ALU64_REG(BPF_SUB, R7, R5),
1161                         BPF_ALU64_REG(BPF_SUB, R7, R6),
1162                         BPF_ALU64_REG(BPF_SUB, R7, R8),
1163                         BPF_ALU64_REG(BPF_SUB, R7, R9),
1164                         BPF_ALU64_IMM(BPF_SUB, R7, 10),
1165                         BPF_ALU64_REG(BPF_SUB, R8, R0),
1166                         BPF_ALU64_REG(BPF_SUB, R8, R1),
1167                         BPF_ALU64_REG(BPF_SUB, R8, R2),
1168                         BPF_ALU64_REG(BPF_SUB, R8, R3),
1169                         BPF_ALU64_REG(BPF_SUB, R8, R4),
1170                         BPF_ALU64_REG(BPF_SUB, R8, R5),
1171                         BPF_ALU64_REG(BPF_SUB, R8, R6),
1172                         BPF_ALU64_REG(BPF_SUB, R8, R7),
1173                         BPF_ALU64_REG(BPF_SUB, R8, R9),
1174                         BPF_ALU64_IMM(BPF_SUB, R8, 10),
1175                         BPF_ALU64_REG(BPF_SUB, R9, R0),
1176                         BPF_ALU64_REG(BPF_SUB, R9, R1),
1177                         BPF_ALU64_REG(BPF_SUB, R9, R2),
1178                         BPF_ALU64_REG(BPF_SUB, R9, R3),
1179                         BPF_ALU64_REG(BPF_SUB, R9, R4),
1180                         BPF_ALU64_REG(BPF_SUB, R9, R5),
1181                         BPF_ALU64_REG(BPF_SUB, R9, R6),
1182                         BPF_ALU64_REG(BPF_SUB, R9, R7),
1183                         BPF_ALU64_REG(BPF_SUB, R9, R8),
1184                         BPF_ALU64_IMM(BPF_SUB, R9, 10),
1185                         BPF_ALU64_IMM(BPF_SUB, R0, 10),
1186                         BPF_ALU64_IMM(BPF_NEG, R0, 0),
1187                         BPF_ALU64_REG(BPF_SUB, R0, R1),
1188                         BPF_ALU64_REG(BPF_SUB, R0, R2),
1189                         BPF_ALU64_REG(BPF_SUB, R0, R3),
1190                         BPF_ALU64_REG(BPF_SUB, R0, R4),
1191                         BPF_ALU64_REG(BPF_SUB, R0, R5),
1192                         BPF_ALU64_REG(BPF_SUB, R0, R6),
1193                         BPF_ALU64_REG(BPF_SUB, R0, R7),
1194                         BPF_ALU64_REG(BPF_SUB, R0, R8),
1195                         BPF_ALU64_REG(BPF_SUB, R0, R9),
1196                         BPF_EXIT_INSN(),
1197                 },
1198                 INTERNAL,
1199                 { },
1200                 { { 0, 11 } }
1201         },
1202         {       /* Mainly checking JIT here. */
1203                 "INT: XOR",
1204                 .u.insns_int = {
1205                         BPF_ALU64_REG(BPF_SUB, R0, R0),
1206                         BPF_ALU64_REG(BPF_XOR, R1, R1),
1207                         BPF_JMP_REG(BPF_JEQ, R0, R1, 1),
1208                         BPF_EXIT_INSN(),
1209                         BPF_ALU64_IMM(BPF_MOV, R0, 10),
1210                         BPF_ALU64_IMM(BPF_MOV, R1, -1),
1211                         BPF_ALU64_REG(BPF_SUB, R1, R1),
1212                         BPF_ALU64_REG(BPF_XOR, R2, R2),
1213                         BPF_JMP_REG(BPF_JEQ, R1, R2, 1),
1214                         BPF_EXIT_INSN(),
1215                         BPF_ALU64_REG(BPF_SUB, R2, R2),
1216                         BPF_ALU64_REG(BPF_XOR, R3, R3),
1217                         BPF_ALU64_IMM(BPF_MOV, R0, 10),
1218                         BPF_ALU64_IMM(BPF_MOV, R1, -1),
1219                         BPF_JMP_REG(BPF_JEQ, R2, R3, 1),
1220                         BPF_EXIT_INSN(),
1221                         BPF_ALU64_REG(BPF_SUB, R3, R3),
1222                         BPF_ALU64_REG(BPF_XOR, R4, R4),
1223                         BPF_ALU64_IMM(BPF_MOV, R2, 1),
1224                         BPF_ALU64_IMM(BPF_MOV, R5, -1),
1225                         BPF_JMP_REG(BPF_JEQ, R3, R4, 1),
1226                         BPF_EXIT_INSN(),
1227                         BPF_ALU64_REG(BPF_SUB, R4, R4),
1228                         BPF_ALU64_REG(BPF_XOR, R5, R5),
1229                         BPF_ALU64_IMM(BPF_MOV, R3, 1),
1230                         BPF_ALU64_IMM(BPF_MOV, R7, -1),
1231                         BPF_JMP_REG(BPF_JEQ, R5, R4, 1),
1232                         BPF_EXIT_INSN(),
1233                         BPF_ALU64_IMM(BPF_MOV, R5, 1),
1234                         BPF_ALU64_REG(BPF_SUB, R5, R5),
1235                         BPF_ALU64_REG(BPF_XOR, R6, R6),
1236                         BPF_ALU64_IMM(BPF_MOV, R1, 1),
1237                         BPF_ALU64_IMM(BPF_MOV, R8, -1),
1238                         BPF_JMP_REG(BPF_JEQ, R5, R6, 1),
1239                         BPF_EXIT_INSN(),
1240                         BPF_ALU64_REG(BPF_SUB, R6, R6),
1241                         BPF_ALU64_REG(BPF_XOR, R7, R7),
1242                         BPF_JMP_REG(BPF_JEQ, R7, R6, 1),
1243                         BPF_EXIT_INSN(),
1244                         BPF_ALU64_REG(BPF_SUB, R7, R7),
1245                         BPF_ALU64_REG(BPF_XOR, R8, R8),
1246                         BPF_JMP_REG(BPF_JEQ, R7, R8, 1),
1247                         BPF_EXIT_INSN(),
1248                         BPF_ALU64_REG(BPF_SUB, R8, R8),
1249                         BPF_ALU64_REG(BPF_XOR, R9, R9),
1250                         BPF_JMP_REG(BPF_JEQ, R9, R8, 1),
1251                         BPF_EXIT_INSN(),
1252                         BPF_ALU64_REG(BPF_SUB, R9, R9),
1253                         BPF_ALU64_REG(BPF_XOR, R0, R0),
1254                         BPF_JMP_REG(BPF_JEQ, R9, R0, 1),
1255                         BPF_EXIT_INSN(),
1256                         BPF_ALU64_REG(BPF_SUB, R1, R1),
1257                         BPF_ALU64_REG(BPF_XOR, R0, R0),
1258                         BPF_JMP_REG(BPF_JEQ, R9, R0, 2),
1259                         BPF_ALU64_IMM(BPF_MOV, R0, 0),
1260                         BPF_EXIT_INSN(),
1261                         BPF_ALU64_IMM(BPF_MOV, R0, 1),
1262                         BPF_EXIT_INSN(),
1263                 },
1264                 INTERNAL,
1265                 { },
1266                 { { 0, 1 } }
1267         },
1268         {       /* Mainly checking JIT here. */
1269                 "INT: MUL",
1270                 .u.insns_int = {
1271                         BPF_ALU64_IMM(BPF_MOV, R0, 11),
1272                         BPF_ALU64_IMM(BPF_MOV, R1, 1),
1273                         BPF_ALU64_IMM(BPF_MOV, R2, 2),
1274                         BPF_ALU64_IMM(BPF_MOV, R3, 3),
1275                         BPF_ALU64_IMM(BPF_MOV, R4, 4),
1276                         BPF_ALU64_IMM(BPF_MOV, R5, 5),
1277                         BPF_ALU64_IMM(BPF_MOV, R6, 6),
1278                         BPF_ALU64_IMM(BPF_MOV, R7, 7),
1279                         BPF_ALU64_IMM(BPF_MOV, R8, 8),
1280                         BPF_ALU64_IMM(BPF_MOV, R9, 9),
1281                         BPF_ALU64_REG(BPF_MUL, R0, R0),
1282                         BPF_ALU64_REG(BPF_MUL, R0, R1),
1283                         BPF_ALU64_REG(BPF_MUL, R0, R2),
1284                         BPF_ALU64_REG(BPF_MUL, R0, R3),
1285                         BPF_ALU64_REG(BPF_MUL, R0, R4),
1286                         BPF_ALU64_REG(BPF_MUL, R0, R5),
1287                         BPF_ALU64_REG(BPF_MUL, R0, R6),
1288                         BPF_ALU64_REG(BPF_MUL, R0, R7),
1289                         BPF_ALU64_REG(BPF_MUL, R0, R8),
1290                         BPF_ALU64_REG(BPF_MUL, R0, R9),
1291                         BPF_ALU64_IMM(BPF_MUL, R0, 10),
1292                         BPF_JMP_IMM(BPF_JEQ, R0, 439084800, 1),
1293                         BPF_EXIT_INSN(),
1294                         BPF_ALU64_REG(BPF_MUL, R1, R0),
1295                         BPF_ALU64_REG(BPF_MUL, R1, R2),
1296                         BPF_ALU64_REG(BPF_MUL, R1, R3),
1297                         BPF_ALU64_REG(BPF_MUL, R1, R4),
1298                         BPF_ALU64_REG(BPF_MUL, R1, R5),
1299                         BPF_ALU64_REG(BPF_MUL, R1, R6),
1300                         BPF_ALU64_REG(BPF_MUL, R1, R7),
1301                         BPF_ALU64_REG(BPF_MUL, R1, R8),
1302                         BPF_ALU64_REG(BPF_MUL, R1, R9),
1303                         BPF_ALU64_IMM(BPF_MUL, R1, 10),
1304                         BPF_ALU64_REG(BPF_MOV, R2, R1),
1305                         BPF_ALU64_IMM(BPF_RSH, R2, 32),
1306                         BPF_JMP_IMM(BPF_JEQ, R2, 0x5a924, 1),
1307                         BPF_EXIT_INSN(),
1308                         BPF_ALU64_IMM(BPF_LSH, R1, 32),
1309                         BPF_ALU64_IMM(BPF_ARSH, R1, 32),
1310                         BPF_JMP_IMM(BPF_JEQ, R1, 0xebb90000, 1),
1311                         BPF_EXIT_INSN(),
1312                         BPF_ALU64_REG(BPF_MUL, R2, R0),
1313                         BPF_ALU64_REG(BPF_MUL, R2, R1),
1314                         BPF_ALU64_REG(BPF_MUL, R2, R3),
1315                         BPF_ALU64_REG(BPF_MUL, R2, R4),
1316                         BPF_ALU64_REG(BPF_MUL, R2, R5),
1317                         BPF_ALU64_REG(BPF_MUL, R2, R6),
1318                         BPF_ALU64_REG(BPF_MUL, R2, R7),
1319                         BPF_ALU64_REG(BPF_MUL, R2, R8),
1320                         BPF_ALU64_REG(BPF_MUL, R2, R9),
1321                         BPF_ALU64_IMM(BPF_MUL, R2, 10),
1322                         BPF_ALU64_IMM(BPF_RSH, R2, 32),
1323                         BPF_ALU64_REG(BPF_MOV, R0, R2),
1324                         BPF_EXIT_INSN(),
1325                 },
1326                 INTERNAL,
1327                 { },
1328                 { { 0, 0x35d97ef2 } }
1329         },
1330         {
1331                 "INT: ALU MIX",
1332                 .u.insns_int = {
1333                         BPF_ALU64_IMM(BPF_MOV, R0, 11),
1334                         BPF_ALU64_IMM(BPF_ADD, R0, -1),
1335                         BPF_ALU64_IMM(BPF_MOV, R2, 2),
1336                         BPF_ALU64_IMM(BPF_XOR, R2, 3),
1337                         BPF_ALU64_REG(BPF_DIV, R0, R2),
1338                         BPF_JMP_IMM(BPF_JEQ, R0, 10, 1),
1339                         BPF_EXIT_INSN(),
1340                         BPF_ALU64_IMM(BPF_MOD, R0, 3),
1341                         BPF_JMP_IMM(BPF_JEQ, R0, 1, 1),
1342                         BPF_EXIT_INSN(),
1343                         BPF_ALU64_IMM(BPF_MOV, R0, -1),
1344                         BPF_EXIT_INSN(),
1345                 },
1346                 INTERNAL,
1347                 { },
1348                 { { 0, -1 } }
1349         },
1350         {
1351                 "INT: shifts by register",
1352                 .u.insns_int = {
1353                         BPF_MOV64_IMM(R0, -1234),
1354                         BPF_MOV64_IMM(R1, 1),
1355                         BPF_ALU32_REG(BPF_RSH, R0, R1),
1356                         BPF_JMP_IMM(BPF_JEQ, R0, 0x7ffffd97, 1),
1357                         BPF_EXIT_INSN(),
1358                         BPF_MOV64_IMM(R2, 1),
1359                         BPF_ALU64_REG(BPF_LSH, R0, R2),
1360                         BPF_MOV32_IMM(R4, -1234),
1361                         BPF_JMP_REG(BPF_JEQ, R0, R4, 1),
1362                         BPF_EXIT_INSN(),
1363                         BPF_ALU64_IMM(BPF_AND, R4, 63),
1364                         BPF_ALU64_REG(BPF_LSH, R0, R4), /* R0 <= 46 */
1365                         BPF_MOV64_IMM(R3, 47),
1366                         BPF_ALU64_REG(BPF_ARSH, R0, R3),
1367                         BPF_JMP_IMM(BPF_JEQ, R0, -617, 1),
1368                         BPF_EXIT_INSN(),
1369                         BPF_MOV64_IMM(R2, 1),
1370                         BPF_ALU64_REG(BPF_LSH, R4, R2), /* R4 = 46 << 1 */
1371                         BPF_JMP_IMM(BPF_JEQ, R4, 92, 1),
1372                         BPF_EXIT_INSN(),
1373                         BPF_MOV64_IMM(R4, 4),
1374                         BPF_ALU64_REG(BPF_LSH, R4, R4), /* R4 = 4 << 4 */
1375                         BPF_JMP_IMM(BPF_JEQ, R4, 64, 1),
1376                         BPF_EXIT_INSN(),
1377                         BPF_MOV64_IMM(R4, 5),
1378                         BPF_ALU32_REG(BPF_LSH, R4, R4), /* R4 = 5 << 5 */
1379                         BPF_JMP_IMM(BPF_JEQ, R4, 160, 1),
1380                         BPF_EXIT_INSN(),
1381                         BPF_MOV64_IMM(R0, -1),
1382                         BPF_EXIT_INSN(),
1383                 },
1384                 INTERNAL,
1385                 { },
1386                 { { 0, -1 } }
1387         },
1388         {
1389                 "INT: DIV + ABS",
1390                 .u.insns_int = {
1391                         BPF_ALU64_REG(BPF_MOV, R6, R1),
1392                         BPF_LD_ABS(BPF_B, 3),
1393                         BPF_ALU64_IMM(BPF_MOV, R2, 2),
1394                         BPF_ALU32_REG(BPF_DIV, R0, R2),
1395                         BPF_ALU64_REG(BPF_MOV, R8, R0),
1396                         BPF_LD_ABS(BPF_B, 4),
1397                         BPF_ALU64_REG(BPF_ADD, R8, R0),
1398                         BPF_LD_IND(BPF_B, R8, -70),
1399                         BPF_EXIT_INSN(),
1400                 },
1401                 INTERNAL,
1402                 { 10, 20, 30, 40, 50 },
1403                 { { 4, 0 }, { 5, 10 } }
1404         },
1405         {
1406                 "INT: DIV by zero",
1407                 .u.insns_int = {
1408                         BPF_ALU64_REG(BPF_MOV, R6, R1),
1409                         BPF_ALU64_IMM(BPF_MOV, R7, 0),
1410                         BPF_LD_ABS(BPF_B, 3),
1411                         BPF_ALU32_REG(BPF_DIV, R0, R7),
1412                         BPF_EXIT_INSN(),
1413                 },
1414                 INTERNAL,
1415                 { 10, 20, 30, 40, 50 },
1416                 { { 3, 0 }, { 4, 0 } }
1417         },
1418         {
1419                 "check: missing ret",
1420                 .u.insns = {
1421                         BPF_STMT(BPF_LD | BPF_IMM, 1),
1422                 },
1423                 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1424                 { },
1425                 { }
1426         },
1427         {
1428                 "check: div_k_0",
1429                 .u.insns = {
1430                         BPF_STMT(BPF_ALU | BPF_DIV | BPF_K, 0),
1431                         BPF_STMT(BPF_RET | BPF_K, 0)
1432                 },
1433                 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1434                 { },
1435                 { }
1436         },
1437         {
1438                 "check: unknown insn",
1439                 .u.insns = {
1440                         /* seccomp insn, rejected in socket filter */
1441                         BPF_STMT(BPF_LDX | BPF_W | BPF_ABS, 0),
1442                         BPF_STMT(BPF_RET | BPF_K, 0)
1443                 },
1444                 CLASSIC | FLAG_EXPECTED_FAIL,
1445                 { },
1446                 { }
1447         },
1448         {
1449                 "check: out of range spill/fill",
1450                 .u.insns = {
1451                         BPF_STMT(BPF_STX, 16),
1452                         BPF_STMT(BPF_RET | BPF_K, 0)
1453                 },
1454                 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1455                 { },
1456                 { }
1457         },
1458         {
1459                 "JUMPS + HOLES",
1460                 .u.insns = {
1461                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1462                         BPF_JUMP(BPF_JMP | BPF_JGE, 0, 13, 15),
1463                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1464                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1465                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1466                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1467                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1468                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1469                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1470                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1471                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1472                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1473                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1474                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1475                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1476                         BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90c2894d, 3, 4),
1477                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1478                         BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90c2894d, 1, 2),
1479                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1480                         BPF_JUMP(BPF_JMP | BPF_JGE, 0, 14, 15),
1481                         BPF_JUMP(BPF_JMP | BPF_JGE, 0, 13, 14),
1482                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1483                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1484                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1485                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1486                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1487                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1488                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1489                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1490                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1491                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1492                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1493                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1494                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1495                         BPF_JUMP(BPF_JMP | BPF_JEQ, 0x2ac28349, 2, 3),
1496                         BPF_JUMP(BPF_JMP | BPF_JEQ, 0x2ac28349, 1, 2),
1497                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1498                         BPF_JUMP(BPF_JMP | BPF_JGE, 0, 14, 15),
1499                         BPF_JUMP(BPF_JMP | BPF_JGE, 0, 13, 14),
1500                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1501                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1502                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1503                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1504                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1505                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1506                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1507                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1508                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1509                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1510                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1511                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1512                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1513                         BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90d2ff41, 2, 3),
1514                         BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90d2ff41, 1, 2),
1515                         BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1516                         BPF_STMT(BPF_RET | BPF_A, 0),
1517                         BPF_STMT(BPF_RET | BPF_A, 0),
1518                 },
1519                 CLASSIC,
1520                 { 0x00, 0x1b, 0x21, 0x3c, 0x9d, 0xf8,
1521                   0x90, 0xe2, 0xba, 0x0a, 0x56, 0xb4,
1522                   0x08, 0x00,
1523                   0x45, 0x00, 0x00, 0x28, 0x00, 0x00,
1524                   0x20, 0x00, 0x40, 0x11, 0x00, 0x00, /* IP header */
1525                   0xc0, 0xa8, 0x33, 0x01,
1526                   0xc0, 0xa8, 0x33, 0x02,
1527                   0xbb, 0xb6,
1528                   0xa9, 0xfa,
1529                   0x00, 0x14, 0x00, 0x00,
1530                   0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1531                   0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1532                   0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1533                   0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1534                   0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1535                   0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1536                   0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1537                   0xcc, 0xcc, 0xcc, 0xcc },
1538                 { { 88, 0x001b } }
1539         },
1540         {
1541                 "check: RET X",
1542                 .u.insns = {
1543                         BPF_STMT(BPF_RET | BPF_X, 0),
1544                 },
1545                 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1546                 { },
1547                 { },
1548         },
1549         {
1550                 "check: LDX + RET X",
1551                 .u.insns = {
1552                         BPF_STMT(BPF_LDX | BPF_IMM, 42),
1553                         BPF_STMT(BPF_RET | BPF_X, 0),
1554                 },
1555                 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1556                 { },
1557                 { },
1558         },
1559         {       /* Mainly checking JIT here. */
1560                 "M[]: alt STX + LDX",
1561                 .u.insns = {
1562                         BPF_STMT(BPF_LDX | BPF_IMM, 100),
1563                         BPF_STMT(BPF_STX, 0),
1564                         BPF_STMT(BPF_LDX | BPF_MEM, 0),
1565                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1566                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1567                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1568                         BPF_STMT(BPF_STX, 1),
1569                         BPF_STMT(BPF_LDX | BPF_MEM, 1),
1570                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1571                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1572                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1573                         BPF_STMT(BPF_STX, 2),
1574                         BPF_STMT(BPF_LDX | BPF_MEM, 2),
1575                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1576                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1577                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1578                         BPF_STMT(BPF_STX, 3),
1579                         BPF_STMT(BPF_LDX | BPF_MEM, 3),
1580                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1581                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1582                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1583                         BPF_STMT(BPF_STX, 4),
1584                         BPF_STMT(BPF_LDX | BPF_MEM, 4),
1585                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1586                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1587                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1588                         BPF_STMT(BPF_STX, 5),
1589                         BPF_STMT(BPF_LDX | BPF_MEM, 5),
1590                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1591                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1592                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1593                         BPF_STMT(BPF_STX, 6),
1594                         BPF_STMT(BPF_LDX | BPF_MEM, 6),
1595                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1596                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1597                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1598                         BPF_STMT(BPF_STX, 7),
1599                         BPF_STMT(BPF_LDX | BPF_MEM, 7),
1600                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1601                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1602                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1603                         BPF_STMT(BPF_STX, 8),
1604                         BPF_STMT(BPF_LDX | BPF_MEM, 8),
1605                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1606                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1607                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1608                         BPF_STMT(BPF_STX, 9),
1609                         BPF_STMT(BPF_LDX | BPF_MEM, 9),
1610                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1611                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1612                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1613                         BPF_STMT(BPF_STX, 10),
1614                         BPF_STMT(BPF_LDX | BPF_MEM, 10),
1615                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1616                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1617                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1618                         BPF_STMT(BPF_STX, 11),
1619                         BPF_STMT(BPF_LDX | BPF_MEM, 11),
1620                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1621                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1622                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1623                         BPF_STMT(BPF_STX, 12),
1624                         BPF_STMT(BPF_LDX | BPF_MEM, 12),
1625                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1626                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1627                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1628                         BPF_STMT(BPF_STX, 13),
1629                         BPF_STMT(BPF_LDX | BPF_MEM, 13),
1630                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1631                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1632                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1633                         BPF_STMT(BPF_STX, 14),
1634                         BPF_STMT(BPF_LDX | BPF_MEM, 14),
1635                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1636                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1637                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1638                         BPF_STMT(BPF_STX, 15),
1639                         BPF_STMT(BPF_LDX | BPF_MEM, 15),
1640                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1641                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1642                         BPF_STMT(BPF_MISC | BPF_TAX, 0),
1643                         BPF_STMT(BPF_RET | BPF_A, 0),
1644                 },
1645                 CLASSIC | FLAG_NO_DATA,
1646                 { },
1647                 { { 0, 116 } },
1648         },
1649         {       /* Mainly checking JIT here. */
1650                 "M[]: full STX + full LDX",
1651                 .u.insns = {
1652                         BPF_STMT(BPF_LDX | BPF_IMM, 0xbadfeedb),
1653                         BPF_STMT(BPF_STX, 0),
1654                         BPF_STMT(BPF_LDX | BPF_IMM, 0xecabedae),
1655                         BPF_STMT(BPF_STX, 1),
1656                         BPF_STMT(BPF_LDX | BPF_IMM, 0xafccfeaf),
1657                         BPF_STMT(BPF_STX, 2),
1658                         BPF_STMT(BPF_LDX | BPF_IMM, 0xbffdcedc),
1659                         BPF_STMT(BPF_STX, 3),
1660                         BPF_STMT(BPF_LDX | BPF_IMM, 0xfbbbdccb),
1661                         BPF_STMT(BPF_STX, 4),
1662                         BPF_STMT(BPF_LDX | BPF_IMM, 0xfbabcbda),
1663                         BPF_STMT(BPF_STX, 5),
1664                         BPF_STMT(BPF_LDX | BPF_IMM, 0xaedecbdb),
1665                         BPF_STMT(BPF_STX, 6),
1666                         BPF_STMT(BPF_LDX | BPF_IMM, 0xadebbade),
1667                         BPF_STMT(BPF_STX, 7),
1668                         BPF_STMT(BPF_LDX | BPF_IMM, 0xfcfcfaec),
1669                         BPF_STMT(BPF_STX, 8),
1670                         BPF_STMT(BPF_LDX | BPF_IMM, 0xbcdddbdc),
1671                         BPF_STMT(BPF_STX, 9),
1672                         BPF_STMT(BPF_LDX | BPF_IMM, 0xfeefdfac),
1673                         BPF_STMT(BPF_STX, 10),
1674                         BPF_STMT(BPF_LDX | BPF_IMM, 0xcddcdeea),
1675                         BPF_STMT(BPF_STX, 11),
1676                         BPF_STMT(BPF_LDX | BPF_IMM, 0xaccfaebb),
1677                         BPF_STMT(BPF_STX, 12),
1678                         BPF_STMT(BPF_LDX | BPF_IMM, 0xbdcccdcf),
1679                         BPF_STMT(BPF_STX, 13),
1680                         BPF_STMT(BPF_LDX | BPF_IMM, 0xaaedecde),
1681                         BPF_STMT(BPF_STX, 14),
1682                         BPF_STMT(BPF_LDX | BPF_IMM, 0xfaeacdad),
1683                         BPF_STMT(BPF_STX, 15),
1684                         BPF_STMT(BPF_LDX | BPF_MEM, 0),
1685                         BPF_STMT(BPF_MISC | BPF_TXA, 0),
1686                         BPF_STMT(BPF_LDX | BPF_MEM, 1),
1687                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1688                         BPF_STMT(BPF_LDX | BPF_MEM, 2),
1689                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1690                         BPF_STMT(BPF_LDX | BPF_MEM, 3),
1691                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1692                         BPF_STMT(BPF_LDX | BPF_MEM, 4),
1693                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1694                         BPF_STMT(BPF_LDX | BPF_MEM, 5),
1695                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1696                         BPF_STMT(BPF_LDX | BPF_MEM, 6),
1697                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1698                         BPF_STMT(BPF_LDX | BPF_MEM, 7),
1699                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1700                         BPF_STMT(BPF_LDX | BPF_MEM, 8),
1701                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1702                         BPF_STMT(BPF_LDX | BPF_MEM, 9),
1703                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1704                         BPF_STMT(BPF_LDX | BPF_MEM, 10),
1705                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1706                         BPF_STMT(BPF_LDX | BPF_MEM, 11),
1707                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1708                         BPF_STMT(BPF_LDX | BPF_MEM, 12),
1709                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1710                         BPF_STMT(BPF_LDX | BPF_MEM, 13),
1711                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1712                         BPF_STMT(BPF_LDX | BPF_MEM, 14),
1713                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1714                         BPF_STMT(BPF_LDX | BPF_MEM, 15),
1715                         BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1716                         BPF_STMT(BPF_RET | BPF_A, 0),
1717                 },
1718                 CLASSIC | FLAG_NO_DATA,
1719                 { },
1720                 { { 0, 0x2a5a5e5 } },
1721         },
1722         {
1723                 "check: SKF_AD_MAX",
1724                 .u.insns = {
1725                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1726                                  SKF_AD_OFF + SKF_AD_MAX),
1727                         BPF_STMT(BPF_RET | BPF_A, 0),
1728                 },
1729                 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1730                 { },
1731                 { },
1732         },
1733         {       /* Passes checker but fails during runtime. */
1734                 "LD [SKF_AD_OFF-1]",
1735                 .u.insns = {
1736                         BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1737                                  SKF_AD_OFF - 1),
1738                         BPF_STMT(BPF_RET | BPF_K, 1),
1739                 },
1740                 CLASSIC,
1741                 { },
1742                 { { 1, 0 } },
1743         },
1744         {
1745                 "load 64-bit immediate",
1746                 .u.insns_int = {
1747                         BPF_LD_IMM64(R1, 0x567800001234LL),
1748                         BPF_MOV64_REG(R2, R1),
1749                         BPF_MOV64_REG(R3, R2),
1750                         BPF_ALU64_IMM(BPF_RSH, R2, 32),
1751                         BPF_ALU64_IMM(BPF_LSH, R3, 32),
1752                         BPF_ALU64_IMM(BPF_RSH, R3, 32),
1753                         BPF_ALU64_IMM(BPF_MOV, R0, 0),
1754                         BPF_JMP_IMM(BPF_JEQ, R2, 0x5678, 1),
1755                         BPF_EXIT_INSN(),
1756                         BPF_JMP_IMM(BPF_JEQ, R3, 0x1234, 1),
1757                         BPF_EXIT_INSN(),
1758                         BPF_ALU64_IMM(BPF_MOV, R0, 1),
1759                         BPF_EXIT_INSN(),
1760                 },
1761                 INTERNAL,
1762                 { },
1763                 { { 0, 1 } }
1764         },
1765         {
1766                 "nmap reduced",
1767                 .u.insns_int = {
1768                         BPF_MOV64_REG(R6, R1),
1769                         BPF_LD_ABS(BPF_H, 12),
1770                         BPF_JMP_IMM(BPF_JNE, R0, 0x806, 28),
1771                         BPF_LD_ABS(BPF_H, 12),
1772                         BPF_JMP_IMM(BPF_JNE, R0, 0x806, 26),
1773                         BPF_MOV32_IMM(R0, 18),
1774                         BPF_STX_MEM(BPF_W, R10, R0, -64),
1775                         BPF_LDX_MEM(BPF_W, R7, R10, -64),
1776                         BPF_LD_IND(BPF_W, R7, 14),
1777                         BPF_STX_MEM(BPF_W, R10, R0, -60),
1778                         BPF_MOV32_IMM(R0, 280971478),
1779                         BPF_STX_MEM(BPF_W, R10, R0, -56),
1780                         BPF_LDX_MEM(BPF_W, R7, R10, -56),
1781                         BPF_LDX_MEM(BPF_W, R0, R10, -60),
1782                         BPF_ALU32_REG(BPF_SUB, R0, R7),
1783                         BPF_JMP_IMM(BPF_JNE, R0, 0, 15),
1784                         BPF_LD_ABS(BPF_H, 12),
1785                         BPF_JMP_IMM(BPF_JNE, R0, 0x806, 13),
1786                         BPF_MOV32_IMM(R0, 22),
1787                         BPF_STX_MEM(BPF_W, R10, R0, -56),
1788                         BPF_LDX_MEM(BPF_W, R7, R10, -56),
1789                         BPF_LD_IND(BPF_H, R7, 14),
1790                         BPF_STX_MEM(BPF_W, R10, R0, -52),
1791                         BPF_MOV32_IMM(R0, 17366),
1792                         BPF_STX_MEM(BPF_W, R10, R0, -48),
1793                         BPF_LDX_MEM(BPF_W, R7, R10, -48),
1794                         BPF_LDX_MEM(BPF_W, R0, R10, -52),
1795                         BPF_ALU32_REG(BPF_SUB, R0, R7),
1796                         BPF_JMP_IMM(BPF_JNE, R0, 0, 2),
1797                         BPF_MOV32_IMM(R0, 256),
1798                         BPF_EXIT_INSN(),
1799                         BPF_MOV32_IMM(R0, 0),
1800                         BPF_EXIT_INSN(),
1801                 },
1802                 INTERNAL,
1803                 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x08, 0x06, 0, 0,
1804                   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1805                   0x10, 0xbf, 0x48, 0xd6, 0x43, 0xd6},
1806                 { { 38, 256 } }
1807         },
1808 };
1809
1810 static struct net_device dev;
1811
1812 static struct sk_buff *populate_skb(char *buf, int size)
1813 {
1814         struct sk_buff *skb;
1815
1816         if (size >= MAX_DATA)
1817                 return NULL;
1818
1819         skb = alloc_skb(MAX_DATA, GFP_KERNEL);
1820         if (!skb)
1821                 return NULL;
1822
1823         memcpy(__skb_put(skb, size), buf, size);
1824
1825         /* Initialize a fake skb with test pattern. */
1826         skb_reset_mac_header(skb);
1827         skb->protocol = htons(ETH_P_IP);
1828         skb->pkt_type = SKB_TYPE;
1829         skb->mark = SKB_MARK;
1830         skb->hash = SKB_HASH;
1831         skb->queue_mapping = SKB_QUEUE_MAP;
1832         skb->vlan_tci = SKB_VLAN_TCI;
1833         skb->dev = &dev;
1834         skb->dev->ifindex = SKB_DEV_IFINDEX;
1835         skb->dev->type = SKB_DEV_TYPE;
1836         skb_set_network_header(skb, min(size, ETH_HLEN));
1837
1838         return skb;
1839 }
1840
1841 static void *generate_test_data(struct bpf_test *test, int sub)
1842 {
1843         if (test->aux & FLAG_NO_DATA)
1844                 return NULL;
1845
1846         /* Test case expects an skb, so populate one. Various
1847          * subtests generate skbs of different sizes based on
1848          * the same data.
1849          */
1850         return populate_skb(test->data, test->test[sub].data_size);
1851 }
1852
1853 static void release_test_data(const struct bpf_test *test, void *data)
1854 {
1855         if (test->aux & FLAG_NO_DATA)
1856                 return;
1857
1858         kfree_skb(data);
1859 }
1860
1861 static int probe_filter_length(struct sock_filter *fp)
1862 {
1863         int len = 0;
1864
1865         for (len = MAX_INSNS - 1; len > 0; --len)
1866                 if (fp[len].code != 0 || fp[len].k != 0)
1867                         break;
1868
1869         return len + 1;
1870 }
1871
1872 static struct bpf_prog *generate_filter(int which, int *err)
1873 {
1874         struct bpf_prog *fp;
1875         struct sock_fprog_kern fprog;
1876         unsigned int flen = probe_filter_length(tests[which].u.insns);
1877         __u8 test_type = tests[which].aux & TEST_TYPE_MASK;
1878
1879         switch (test_type) {
1880         case CLASSIC:
1881                 fprog.filter = tests[which].u.insns;
1882                 fprog.len = flen;
1883
1884                 *err = bpf_prog_create(&fp, &fprog);
1885                 if (tests[which].aux & FLAG_EXPECTED_FAIL) {
1886                         if (*err == -EINVAL) {
1887                                 pr_cont("PASS\n");
1888                                 /* Verifier rejected filter as expected. */
1889                                 *err = 0;
1890                                 return NULL;
1891                         } else {
1892                                 pr_cont("UNEXPECTED_PASS\n");
1893                                 /* Verifier didn't reject the test that's
1894                                  * bad enough, just return!
1895                                  */
1896                                 *err = -EINVAL;
1897                                 return NULL;
1898                         }
1899                 }
1900                 /* We don't expect to fail. */
1901                 if (*err) {
1902                         pr_cont("FAIL to attach err=%d len=%d\n",
1903                                 *err, fprog.len);
1904                         return NULL;
1905                 }
1906                 break;
1907
1908         case INTERNAL:
1909                 fp = bpf_prog_alloc(bpf_prog_size(flen), 0);
1910                 if (fp == NULL) {
1911                         pr_cont("UNEXPECTED_FAIL no memory left\n");
1912                         *err = -ENOMEM;
1913                         return NULL;
1914                 }
1915
1916                 fp->len = flen;
1917                 memcpy(fp->insnsi, tests[which].u.insns_int,
1918                        fp->len * sizeof(struct bpf_insn));
1919
1920                 bpf_prog_select_runtime(fp);
1921                 break;
1922         }
1923
1924         *err = 0;
1925         return fp;
1926 }
1927
1928 static void release_filter(struct bpf_prog *fp, int which)
1929 {
1930         __u8 test_type = tests[which].aux & TEST_TYPE_MASK;
1931
1932         switch (test_type) {
1933         case CLASSIC:
1934                 bpf_prog_destroy(fp);
1935                 break;
1936         case INTERNAL:
1937                 bpf_prog_free(fp);
1938                 break;
1939         }
1940 }
1941
1942 static int __run_one(const struct bpf_prog *fp, const void *data,
1943                      int runs, u64 *duration)
1944 {
1945         u64 start, finish;
1946         int ret = 0, i;
1947
1948         start = ktime_to_us(ktime_get());
1949
1950         for (i = 0; i < runs; i++)
1951                 ret = BPF_PROG_RUN(fp, data);
1952
1953         finish = ktime_to_us(ktime_get());
1954
1955         *duration = (finish - start) * 1000ULL;
1956         do_div(*duration, runs);
1957
1958         return ret;
1959 }
1960
1961 static int run_one(const struct bpf_prog *fp, struct bpf_test *test)
1962 {
1963         int err_cnt = 0, i, runs = MAX_TESTRUNS;
1964
1965         for (i = 0; i < MAX_SUBTESTS; i++) {
1966                 void *data;
1967                 u64 duration;
1968                 u32 ret;
1969
1970                 if (test->test[i].data_size == 0 &&
1971                     test->test[i].result == 0)
1972                         break;
1973
1974                 data = generate_test_data(test, i);
1975                 ret = __run_one(fp, data, runs, &duration);
1976                 release_test_data(test, data);
1977
1978                 if (ret == test->test[i].result) {
1979                         pr_cont("%lld ", duration);
1980                 } else {
1981                         pr_cont("ret %d != %d ", ret,
1982                                 test->test[i].result);
1983                         err_cnt++;
1984                 }
1985         }
1986
1987         return err_cnt;
1988 }
1989
1990 static __init int test_bpf(void)
1991 {
1992         int i, err_cnt = 0, pass_cnt = 0;
1993
1994         for (i = 0; i < ARRAY_SIZE(tests); i++) {
1995                 struct bpf_prog *fp;
1996                 int err;
1997
1998                 pr_info("#%d %s ", i, tests[i].descr);
1999
2000                 fp = generate_filter(i, &err);
2001                 if (fp == NULL) {
2002                         if (err == 0) {
2003                                 pass_cnt++;
2004                                 continue;
2005                         }
2006
2007                         return err;
2008                 }
2009                 err = run_one(fp, &tests[i]);
2010                 release_filter(fp, i);
2011
2012                 if (err) {
2013                         pr_cont("FAIL (%d times)\n", err);
2014                         err_cnt++;
2015                 } else {
2016                         pr_cont("PASS\n");
2017                         pass_cnt++;
2018                 }
2019         }
2020
2021         pr_info("Summary: %d PASSED, %d FAILED\n", pass_cnt, err_cnt);
2022         return err_cnt ? -EINVAL : 0;
2023 }
2024
2025 static int __init test_bpf_init(void)
2026 {
2027         return test_bpf();
2028 }
2029
2030 static void __exit test_bpf_exit(void)
2031 {
2032 }
2033
2034 module_init(test_bpf_init);
2035 module_exit(test_bpf_exit);
2036
2037 MODULE_LICENSE("GPL");