etc/protocols - sync with NetBSD-8
[minix.git] / tests / net / bpfjit / t_bpfjit.c
blob58db1e06a083efeb1423267be6c40bac3288ae55
1 /* $NetBSD: t_bpfjit.c,v 1.11 2015/02/14 22:34:33 alnsn Exp $ */
3 /*-
4 * Copyright (c) 2011-2012, 2014-2015 Alexander Nasonov.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: t_bpfjit.c,v 1.11 2015/02/14 22:34:33 alnsn Exp $");
32 #include <sys/param.h>
33 #include <sys/mbuf.h>
34 #include <unistd.h>
36 #include <net/bpf.h>
37 #include <net/bpfjit.h>
39 #include <stdint.h>
40 #include <string.h>
42 #include <rump/rump.h>
43 #include <rump/rump_syscalls.h>
45 #include "../../net/bpf/h_bpf.h"
47 /* XXX: atf-c.h has collisions with mbuf */
48 #undef m_type
49 #undef m_data
50 #include <atf-c.h>
52 #include "../../h_macros.h"
55 static uint8_t deadbeef_at_5[16] = {
56 0, 0xf1, 2, 0xf3, 4, 0xde, 0xad, 0xbe, 0xef, 0xff
59 static inline
60 unsigned int jitcall(bpfjit_func_t fn,
61 const uint8_t *pkt, unsigned int wirelen, unsigned int buflen)
63 bpf_args_t args;
65 args.pkt = pkt;
66 args.wirelen = wirelen;
67 args.buflen = buflen;
69 return fn(NULL, &args);
72 ATF_TC(bpfjit_empty);
73 ATF_TC_HEAD(bpfjit_empty, tc)
75 atf_tc_set_md_var(tc, "descr",
76 "Test that JIT compilation of an empty bpf program fails");
79 ATF_TC_BODY(bpfjit_empty, tc)
81 struct bpf_insn dummy;
82 bpfjit_func_t code;
84 RZ(rump_init());
86 ATF_CHECK(!prog_validate(&dummy, 0));
88 rump_schedule();
89 code = rumpns_bpfjit_generate_code(NULL, &dummy, 0);
90 rump_unschedule();
92 ATF_CHECK(code == NULL);
95 ATF_TC(bpfjit_ret_k);
96 ATF_TC_HEAD(bpfjit_ret_k, tc)
98 atf_tc_set_md_var(tc, "descr",
99 "Test JIT compilation of a trivial bpf program");
102 ATF_TC_BODY(bpfjit_ret_k, tc)
104 static struct bpf_insn insns[] = {
105 BPF_STMT(BPF_RET+BPF_K, 17)
108 uint8_t pkt[1]; /* the program doesn't read any data */
110 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
112 RZ(rump_init());
114 ATF_CHECK(prog_validate(insns, insn_count));
115 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 17);
118 ATF_TC(bpfjit_bad_ret_k);
119 ATF_TC_HEAD(bpfjit_bad_ret_k, tc)
121 atf_tc_set_md_var(tc, "descr",
122 "Test that JIT compilation of a program with bad BPF_RET fails");
125 ATF_TC_BODY(bpfjit_bad_ret_k, tc)
127 static struct bpf_insn insns[] = {
128 BPF_STMT(BPF_RET+BPF_K+0x8000, 13)
131 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
133 uint8_t pkt[1]; /* the program doesn't read any data */
136 * The point of this test is checking a bad instruction of
137 * a valid class and with a valid BPF_RVAL data.
139 const uint16_t rcode = insns[0].code;
140 ATF_CHECK(BPF_CLASS(rcode) == BPF_RET &&
141 (BPF_RVAL(rcode) == BPF_K || BPF_RVAL(rcode) == BPF_A));
143 RZ(rump_init());
145 ATF_CHECK(!prog_validate(insns, insn_count));
147 /* Current implementation generates code. */
148 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 13);
151 ATF_TC(bpfjit_alu_add_k);
152 ATF_TC_HEAD(bpfjit_alu_add_k, tc)
154 atf_tc_set_md_var(tc, "descr",
155 "Test JIT compilation of BPF_ALU+BPF_ADD+BPF_K");
158 ATF_TC_BODY(bpfjit_alu_add_k, tc)
160 static struct bpf_insn insns[] = {
161 BPF_STMT(BPF_LD+BPF_IMM, 3),
162 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 2),
163 BPF_STMT(BPF_RET+BPF_A, 0)
166 uint8_t pkt[1]; /* the program doesn't read any data */
168 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
170 RZ(rump_init());
172 ATF_CHECK(prog_validate(insns, insn_count));
173 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 5);
176 ATF_TC(bpfjit_alu_sub_k);
177 ATF_TC_HEAD(bpfjit_alu_sub_k, tc)
179 atf_tc_set_md_var(tc, "descr",
180 "Test JIT compilation of BPF_ALU+BPF_SUB+BPF_K");
183 ATF_TC_BODY(bpfjit_alu_sub_k, tc)
185 static struct bpf_insn insns[] = {
186 BPF_STMT(BPF_LD+BPF_IMM, 1),
187 BPF_STMT(BPF_ALU+BPF_SUB+BPF_K, 2),
188 BPF_STMT(BPF_RET+BPF_A, 0)
191 uint8_t pkt[1]; /* the program doesn't read any data */
193 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
195 RZ(rump_init());
197 ATF_CHECK(prog_validate(insns, insn_count));
198 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == UINT32_MAX);
201 ATF_TC(bpfjit_alu_mul_k);
202 ATF_TC_HEAD(bpfjit_alu_mul_k, tc)
204 atf_tc_set_md_var(tc, "descr",
205 "Test JIT compilation of BPF_ALU+BPF_MUL+BPF_K");
208 ATF_TC_BODY(bpfjit_alu_mul_k, tc)
210 static struct bpf_insn insns[] = {
211 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
212 BPF_STMT(BPF_ALU+BPF_MUL+BPF_K, 3),
213 BPF_STMT(BPF_RET+BPF_A, 0)
216 uint8_t pkt[1]; /* the program doesn't read any data */
218 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
220 RZ(rump_init());
222 ATF_CHECK(prog_validate(insns, insn_count));
223 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xfffffffd);
226 ATF_TC(bpfjit_alu_div0_k);
227 ATF_TC_HEAD(bpfjit_alu_div0_k, tc)
229 atf_tc_set_md_var(tc, "descr",
230 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=0");
233 ATF_TC_BODY(bpfjit_alu_div0_k, tc)
235 static struct bpf_insn insns[] = {
236 BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 0),
237 BPF_STMT(BPF_RET+BPF_A, 0)
240 uint8_t pkt[1]; /* the program doesn't read any data */
242 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
244 RZ(rump_init());
246 //ATF_CHECK(prog_validate(insns, insn_count));
247 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0);
250 ATF_TC(bpfjit_alu_div1_k);
251 ATF_TC_HEAD(bpfjit_alu_div1_k, tc)
253 atf_tc_set_md_var(tc, "descr",
254 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=1");
257 ATF_TC_BODY(bpfjit_alu_div1_k, tc)
259 static struct bpf_insn insns[] = {
260 BPF_STMT(BPF_LD+BPF_IMM, 7),
261 BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 1),
262 BPF_STMT(BPF_RET+BPF_A, 0)
265 uint8_t pkt[1]; /* the program doesn't read any data */
267 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
269 RZ(rump_init());
271 ATF_CHECK(prog_validate(insns, insn_count));
272 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 7);
275 ATF_TC(bpfjit_alu_div2_k);
276 ATF_TC_HEAD(bpfjit_alu_div2_k, tc)
278 atf_tc_set_md_var(tc, "descr",
279 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=2");
282 ATF_TC_BODY(bpfjit_alu_div2_k, tc)
284 static struct bpf_insn insns[] = {
285 BPF_STMT(BPF_LD+BPF_IMM, 7),
286 BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 2),
287 BPF_STMT(BPF_RET+BPF_A, 0)
290 uint8_t pkt[1]; /* the program doesn't read any data */
292 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
294 RZ(rump_init());
296 ATF_CHECK(prog_validate(insns, insn_count));
297 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 3);
300 ATF_TC(bpfjit_alu_div4_k);
301 ATF_TC_HEAD(bpfjit_alu_div4_k, tc)
303 atf_tc_set_md_var(tc, "descr",
304 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=4");
307 ATF_TC_BODY(bpfjit_alu_div4_k, tc)
309 static struct bpf_insn insns[] = {
310 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
311 BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 4),
312 BPF_STMT(BPF_RET+BPF_A, 0)
315 uint8_t pkt[1]; /* the program doesn't read any data */
317 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
319 RZ(rump_init());
321 ATF_CHECK(prog_validate(insns, insn_count));
322 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0x3fffffff);
325 ATF_TC(bpfjit_alu_div10_k);
326 ATF_TC_HEAD(bpfjit_alu_div10_k, tc)
328 atf_tc_set_md_var(tc, "descr",
329 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=10");
332 ATF_TC_BODY(bpfjit_alu_div10_k, tc)
334 static struct bpf_insn insns[] = {
335 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
336 BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 10),
337 BPF_STMT(BPF_RET+BPF_A, 0)
340 uint8_t pkt[1]; /* the program doesn't read any data */
342 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
344 RZ(rump_init());
346 ATF_CHECK(prog_validate(insns, insn_count));
347 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 429484384);
350 ATF_TC(bpfjit_alu_div10000_k);
351 ATF_TC_HEAD(bpfjit_alu_div10000_k, tc)
353 atf_tc_set_md_var(tc, "descr",
354 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=10000");
357 ATF_TC_BODY(bpfjit_alu_div10000_k, tc)
359 static struct bpf_insn insns[] = {
360 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
361 BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 10000),
362 BPF_STMT(BPF_RET+BPF_A, 0)
365 uint8_t pkt[1]; /* the program doesn't read any data */
367 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
369 RZ(rump_init());
371 ATF_CHECK(prog_validate(insns, insn_count));
372 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 429484);
375 ATF_TC(bpfjit_alu_div7609801_k);
376 ATF_TC_HEAD(bpfjit_alu_div7609801_k, tc)
378 atf_tc_set_md_var(tc, "descr",
379 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=7609801");
382 ATF_TC_BODY(bpfjit_alu_div7609801_k, tc)
384 static struct bpf_insn insns[] = {
385 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294967295)),
386 BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, UINT32_C(7609801)),
387 BPF_STMT(BPF_RET+BPF_A, 0)
390 uint8_t pkt[1]; /* the program doesn't read any data */
392 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
394 RZ(rump_init());
396 ATF_CHECK(prog_validate(insns, insn_count));
397 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 564);
400 ATF_TC(bpfjit_alu_div80000000_k);
401 ATF_TC_HEAD(bpfjit_alu_div80000000_k, tc)
403 atf_tc_set_md_var(tc, "descr",
404 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=0x80000000");
407 ATF_TC_BODY(bpfjit_alu_div80000000_k, tc)
409 static struct bpf_insn insns[] = {
410 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffde)),
411 BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, UINT32_C(0x80000000)),
412 BPF_STMT(BPF_RET+BPF_A, 0)
415 uint8_t pkt[1]; /* the program doesn't read any data */
417 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
419 RZ(rump_init());
421 ATF_CHECK(prog_validate(insns, insn_count));
422 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 1);
425 ATF_TC(bpfjit_alu_mod0_k);
426 ATF_TC_HEAD(bpfjit_alu_mod0_k, tc)
428 atf_tc_set_md_var(tc, "descr",
429 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=0");
432 ATF_TC_BODY(bpfjit_alu_mod0_k, tc)
434 static struct bpf_insn insns[] = {
435 BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 0),
436 BPF_STMT(BPF_RET+BPF_A, 0)
439 uint8_t pkt[1]; /* the program doesn't read any data */
441 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
443 RZ(rump_init());
445 //ATF_CHECK(prog_validate(insns, insn_count));
446 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0);
449 ATF_TC(bpfjit_alu_mod1_k);
450 ATF_TC_HEAD(bpfjit_alu_mod1_k, tc)
452 atf_tc_set_md_var(tc, "descr",
453 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=1");
456 ATF_TC_BODY(bpfjit_alu_mod1_k, tc)
458 static struct bpf_insn insns[] = {
459 BPF_STMT(BPF_LD+BPF_IMM, 7),
460 BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 1),
461 BPF_STMT(BPF_RET+BPF_A, 0)
464 uint8_t pkt[1]; /* the program doesn't read any data */
466 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
468 RZ(rump_init());
470 ATF_CHECK(prog_validate(insns, insn_count));
471 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0);
474 ATF_TC(bpfjit_alu_mod2_k);
475 ATF_TC_HEAD(bpfjit_alu_mod2_k, tc)
477 atf_tc_set_md_var(tc, "descr",
478 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=2");
481 ATF_TC_BODY(bpfjit_alu_mod2_k, tc)
483 static struct bpf_insn insns[] = {
484 BPF_STMT(BPF_LD+BPF_IMM, 7),
485 BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 2),
486 BPF_STMT(BPF_RET+BPF_A, 0)
489 uint8_t pkt[1]; /* the program doesn't read any data */
491 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
493 RZ(rump_init());
495 ATF_CHECK(prog_validate(insns, insn_count));
496 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 1);
499 ATF_TC(bpfjit_alu_mod4_k);
500 ATF_TC_HEAD(bpfjit_alu_mod4_k, tc)
502 atf_tc_set_md_var(tc, "descr",
503 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=4");
506 ATF_TC_BODY(bpfjit_alu_mod4_k, tc)
508 static struct bpf_insn insns[] = {
509 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
510 BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 4),
511 BPF_STMT(BPF_RET+BPF_A, 0)
514 uint8_t pkt[1]; /* the program doesn't read any data */
516 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
518 RZ(rump_init());
520 ATF_CHECK(prog_validate(insns, insn_count));
521 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 3);
524 ATF_TC(bpfjit_alu_mod10_k);
525 ATF_TC_HEAD(bpfjit_alu_mod10_k, tc)
527 atf_tc_set_md_var(tc, "descr",
528 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=10");
531 ATF_TC_BODY(bpfjit_alu_mod10_k, tc)
533 static struct bpf_insn insns[] = {
534 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
535 BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 10),
536 BPF_STMT(BPF_RET+BPF_A, 0)
539 uint8_t pkt[1]; /* the program doesn't read any data */
541 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
543 RZ(rump_init());
545 ATF_CHECK(prog_validate(insns, insn_count));
546 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 9);
549 ATF_TC(bpfjit_alu_mod10000_k);
550 ATF_TC_HEAD(bpfjit_alu_mod10000_k, tc)
552 atf_tc_set_md_var(tc, "descr",
553 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=10000");
556 ATF_TC_BODY(bpfjit_alu_mod10000_k, tc)
558 static struct bpf_insn insns[] = {
559 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
560 BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 10000),
561 BPF_STMT(BPF_RET+BPF_A, 0)
564 uint8_t pkt[1]; /* the program doesn't read any data */
566 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
568 RZ(rump_init());
570 ATF_CHECK(prog_validate(insns, insn_count));
571 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 3849);
574 ATF_TC(bpfjit_alu_mod7609801_k);
575 ATF_TC_HEAD(bpfjit_alu_mod7609801_k, tc)
577 atf_tc_set_md_var(tc, "descr",
578 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=7609801");
581 ATF_TC_BODY(bpfjit_alu_mod7609801_k, tc)
583 static struct bpf_insn insns[] = {
584 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294967295)),
585 BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, UINT32_C(7609801)),
586 BPF_STMT(BPF_RET+BPF_A, 0)
589 uint8_t pkt[1]; /* the program doesn't read any data */
591 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
593 RZ(rump_init());
595 ATF_CHECK(prog_validate(insns, insn_count));
596 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 3039531);
599 ATF_TC(bpfjit_alu_mod80000000_k);
600 ATF_TC_HEAD(bpfjit_alu_mod80000000_k, tc)
602 atf_tc_set_md_var(tc, "descr",
603 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=0x80000000");
606 ATF_TC_BODY(bpfjit_alu_mod80000000_k, tc)
608 static struct bpf_insn insns[] = {
609 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffde)),
610 BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, UINT32_C(0x80000000)),
611 BPF_STMT(BPF_RET+BPF_A, 0)
614 uint8_t pkt[1]; /* the program doesn't read any data */
616 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
618 RZ(rump_init());
620 ATF_CHECK(prog_validate(insns, insn_count));
621 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == UINT32_C(0x7fffffde));
624 ATF_TC(bpfjit_alu_and_k);
625 ATF_TC_HEAD(bpfjit_alu_and_k, tc)
627 atf_tc_set_md_var(tc, "descr",
628 "Test JIT compilation of BPF_ALU+BPF_AND+BPF_K");
631 ATF_TC_BODY(bpfjit_alu_and_k, tc)
633 static struct bpf_insn insns[] = {
634 BPF_STMT(BPF_LD+BPF_IMM, 0xdead),
635 BPF_STMT(BPF_ALU+BPF_AND+BPF_K, 0xbeef),
636 BPF_STMT(BPF_RET+BPF_A, 0)
639 uint8_t pkt[1]; /* the program doesn't read any data */
641 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
643 RZ(rump_init());
645 ATF_CHECK(prog_validate(insns, insn_count));
646 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == (0xdead&0xbeef));
649 ATF_TC(bpfjit_alu_or_k);
650 ATF_TC_HEAD(bpfjit_alu_or_k, tc)
652 atf_tc_set_md_var(tc, "descr",
653 "Test JIT compilation of BPF_ALU+BPF_OR+BPF_K");
656 ATF_TC_BODY(bpfjit_alu_or_k, tc)
658 static struct bpf_insn insns[] = {
659 BPF_STMT(BPF_LD+BPF_IMM, 0xdead0000),
660 BPF_STMT(BPF_ALU+BPF_OR+BPF_K, 0x0000beef),
661 BPF_STMT(BPF_RET+BPF_A, 0)
664 uint8_t pkt[1]; /* the program doesn't read any data */
666 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
668 RZ(rump_init());
670 ATF_CHECK(prog_validate(insns, insn_count));
671 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xdeadbeef);
674 ATF_TC(bpfjit_alu_xor_k);
675 ATF_TC_HEAD(bpfjit_alu_xor_k, tc)
677 atf_tc_set_md_var(tc, "descr",
678 "Test JIT compilation of BPF_ALU+BPF_XOR+BPF_K");
681 ATF_TC_BODY(bpfjit_alu_xor_k, tc)
683 static struct bpf_insn insns[] = {
684 BPF_STMT(BPF_LD+BPF_IMM, 0xdead0f0f),
685 BPF_STMT(BPF_ALU+BPF_XOR+BPF_K, 0x0000b1e0),
686 BPF_STMT(BPF_RET+BPF_A, 0)
689 uint8_t pkt[1]; /* the program doesn't read any data */
691 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
693 RZ(rump_init());
695 ATF_CHECK(prog_validate(insns, insn_count));
696 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xdeadbeef);
699 ATF_TC(bpfjit_alu_lsh_k);
700 ATF_TC_HEAD(bpfjit_alu_lsh_k, tc)
702 atf_tc_set_md_var(tc, "descr",
703 "Test JIT compilation of BPF_ALU+BPF_LSH+BPF_K");
706 ATF_TC_BODY(bpfjit_alu_lsh_k, tc)
708 static struct bpf_insn insns[] = {
709 BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
710 BPF_STMT(BPF_ALU+BPF_LSH+BPF_K, 16),
711 BPF_STMT(BPF_RET+BPF_A, 0)
714 uint8_t pkt[1]; /* the program doesn't read any data */
716 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
718 RZ(rump_init());
720 ATF_CHECK(prog_validate(insns, insn_count));
721 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xbeef0000);
724 ATF_TC(bpfjit_alu_lsh0_k);
725 ATF_TC_HEAD(bpfjit_alu_lsh0_k, tc)
727 atf_tc_set_md_var(tc, "descr",
728 "Test JIT compilation of BPF_ALU+BPF_LSH+BPF_K with k=0");
731 ATF_TC_BODY(bpfjit_alu_lsh0_k, tc)
733 static struct bpf_insn insns[] = {
734 BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
735 BPF_STMT(BPF_ALU+BPF_LSH+BPF_K, 0),
736 BPF_STMT(BPF_RET+BPF_A, 0)
739 uint8_t pkt[1]; /* the program doesn't read any data */
741 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
743 RZ(rump_init());
745 ATF_CHECK(prog_validate(insns, insn_count));
746 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xdeadbeef);
749 ATF_TC(bpfjit_alu_rsh_k);
750 ATF_TC_HEAD(bpfjit_alu_rsh_k, tc)
752 atf_tc_set_md_var(tc, "descr",
753 "Test JIT compilation of BPF_ALU+BPF_RSH+BPF_K");
756 ATF_TC_BODY(bpfjit_alu_rsh_k, tc)
758 static struct bpf_insn insns[] = {
759 BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
760 BPF_STMT(BPF_ALU+BPF_RSH+BPF_K, 16),
761 BPF_STMT(BPF_RET+BPF_A, 0)
764 uint8_t pkt[1]; /* the program doesn't read any data */
766 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
768 RZ(rump_init());
770 ATF_CHECK(prog_validate(insns, insn_count));
771 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0x0000dead);
774 ATF_TC(bpfjit_alu_rsh0_k);
775 ATF_TC_HEAD(bpfjit_alu_rsh0_k, tc)
777 atf_tc_set_md_var(tc, "descr",
778 "Test JIT compilation of BPF_ALU+BPF_RSH+BPF_K with k=0");
781 ATF_TC_BODY(bpfjit_alu_rsh0_k, tc)
783 static struct bpf_insn insns[] = {
784 BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
785 BPF_STMT(BPF_ALU+BPF_RSH+BPF_K, 0),
786 BPF_STMT(BPF_RET+BPF_A, 0)
789 uint8_t pkt[1]; /* the program doesn't read any data */
791 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
793 RZ(rump_init());
795 ATF_CHECK(prog_validate(insns, insn_count));
796 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xdeadbeef);
799 ATF_TC(bpfjit_alu_modulo_k);
800 ATF_TC_HEAD(bpfjit_alu_modulo_k, tc)
802 atf_tc_set_md_var(tc, "descr",
803 "Test JIT compilation of modulo logic of BPF_ALU+BPF_K operations");
806 ATF_TC_BODY(bpfjit_alu_modulo_k, tc)
808 static struct bpf_insn insns[] = {
809 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0x7fffff77)),
811 /* (7FFFFF77 * 0FFFFF77) = 07FFFFB2,F0004951 */
812 BPF_STMT(BPF_ALU+BPF_MUL+BPF_K, UINT32_C(0x0fffff77)),
814 /* 07FFFFB2,F0004951 << 1 = 0FFFFF65,E00092A2 */
815 BPF_STMT(BPF_ALU+BPF_LSH+BPF_K, 1),
817 /* 0FFFFF65,E00092A2 + DDDDDDDD = 0FFFFF66,BDDE707F */
818 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, UINT32_C(0xdddddddd)),
820 /* 0FFFFF66,BDDE707F - FFFFFFFF = 0FFFFF65,BDDE7080 */
821 BPF_STMT(BPF_ALU+BPF_SUB+BPF_K, UINT32_C(0xffffffff)),
823 /* 0FFFFF65,BDDE7080 | 0000030C = 0FFFFF65,BDDE738C */
824 BPF_STMT(BPF_ALU+BPF_OR+BPF_K, UINT32_C(0x0000030c)),
826 /* -0FFFFF65,BDDE738C mod(2^64) = F000009A,42218C74 */
827 BPF_STMT(BPF_ALU+BPF_NEG, 0),
829 /* F000009A,42218C74 & FFFFFF0F = F000009A,42218C04 */
830 BPF_STMT(BPF_ALU+BPF_AND+BPF_K, UINT32_C(0xffffff0f)),
832 /* F000009A,42218C74 >> 3 = 1E000013,48443180 */
833 /* 00000000,42218C74 >> 3 = 00000000,08443180 */
834 BPF_STMT(BPF_ALU+BPF_RSH+BPF_K, 3),
836 /* 00000000,08443180 * 7FFFFF77 = 042218BB,93818280 */
837 BPF_STMT(BPF_ALU+BPF_MUL+BPF_K, UINT32_C(0x7fffff77)),
839 /* 042218BB,93818280 / DEAD = 000004C0,71CBBBC3 */
840 /* 00000000,93818280 / DEAD = 00000000,0000A994 */
841 BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, UINT32_C(0xdead)),
843 BPF_STMT(BPF_RET+BPF_A, 0)
846 bpfjit_func_t code;
847 uint8_t pkt[1]; /* the program doesn't read any data */
849 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
851 RZ(rump_init());
853 ATF_CHECK(prog_validate(insns, insn_count));
855 rump_schedule();
856 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
857 rump_unschedule();
858 ATF_REQUIRE(code != NULL);
860 ATF_CHECK(jitcall(code, pkt, 1, 1) != UINT32_C(0x71cbbbc3));
861 ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0x0000a994));
863 rump_schedule();
864 rumpns_bpfjit_free_code(code);
865 rump_unschedule();
868 ATF_TC(bpfjit_alu_add_x);
869 ATF_TC_HEAD(bpfjit_alu_add_x, tc)
871 atf_tc_set_md_var(tc, "descr",
872 "Test JIT compilation of BPF_ALU+BPF_ADD+BPF_X");
875 ATF_TC_BODY(bpfjit_alu_add_x, tc)
877 static struct bpf_insn insns[] = {
878 BPF_STMT(BPF_LD+BPF_IMM, 3),
879 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
880 BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
881 BPF_STMT(BPF_RET+BPF_A, 0)
884 uint8_t pkt[1]; /* the program doesn't read any data */
886 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
888 RZ(rump_init());
890 ATF_CHECK(prog_validate(insns, insn_count));
891 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 5);
894 ATF_TC(bpfjit_alu_sub_x);
895 ATF_TC_HEAD(bpfjit_alu_sub_x, tc)
897 atf_tc_set_md_var(tc, "descr",
898 "Test JIT compilation of BPF_ALU+BPF_SUB+BPF_X");
901 ATF_TC_BODY(bpfjit_alu_sub_x, tc)
903 static struct bpf_insn insns[] = {
904 BPF_STMT(BPF_LD+BPF_IMM, 1),
905 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
906 BPF_STMT(BPF_ALU+BPF_SUB+BPF_X, 0),
907 BPF_STMT(BPF_RET+BPF_A, 0)
910 uint8_t pkt[1]; /* the program doesn't read any data */
912 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
914 RZ(rump_init());
916 ATF_CHECK(prog_validate(insns, insn_count));
917 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == UINT32_MAX);
920 ATF_TC(bpfjit_alu_mul_x);
921 ATF_TC_HEAD(bpfjit_alu_mul_x, tc)
923 atf_tc_set_md_var(tc, "descr",
924 "Test JIT compilation of BPF_ALU+BPF_MUL+BPF_X");
927 ATF_TC_BODY(bpfjit_alu_mul_x, tc)
929 static struct bpf_insn insns[] = {
930 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
931 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
932 BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0),
933 BPF_STMT(BPF_RET+BPF_A, 0)
936 uint8_t pkt[1]; /* the program doesn't read any data */
938 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
940 RZ(rump_init());
942 ATF_CHECK(prog_validate(insns, insn_count));
943 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xfffffffd);
946 ATF_TC(bpfjit_alu_div0_x);
947 ATF_TC_HEAD(bpfjit_alu_div0_x, tc)
949 atf_tc_set_md_var(tc, "descr",
950 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=0");
953 ATF_TC_BODY(bpfjit_alu_div0_x, tc)
955 static struct bpf_insn insns[] = {
956 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
957 BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
958 BPF_STMT(BPF_RET+BPF_A, 0)
961 uint8_t pkt[1]; /* the program doesn't read any data */
963 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
965 RZ(rump_init());
967 ATF_CHECK(prog_validate(insns, insn_count));
968 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0);
971 ATF_TC(bpfjit_alu_div1_x);
972 ATF_TC_HEAD(bpfjit_alu_div1_x, tc)
974 atf_tc_set_md_var(tc, "descr",
975 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=1");
978 ATF_TC_BODY(bpfjit_alu_div1_x, tc)
980 static struct bpf_insn insns[] = {
981 BPF_STMT(BPF_LD+BPF_IMM, 7),
982 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1),
983 BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
984 BPF_STMT(BPF_RET+BPF_A, 0)
987 uint8_t pkt[1]; /* the program doesn't read any data */
989 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
991 RZ(rump_init());
993 ATF_CHECK(prog_validate(insns, insn_count));
994 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 7);
997 ATF_TC(bpfjit_alu_div2_x);
998 ATF_TC_HEAD(bpfjit_alu_div2_x, tc)
1000 atf_tc_set_md_var(tc, "descr",
1001 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=2");
1004 ATF_TC_BODY(bpfjit_alu_div2_x, tc)
1006 static struct bpf_insn insns[] = {
1007 BPF_STMT(BPF_LD+BPF_IMM, 7),
1008 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
1009 BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
1010 BPF_STMT(BPF_RET+BPF_A, 0)
1013 uint8_t pkt[1]; /* the program doesn't read any data */
1015 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1017 RZ(rump_init());
1019 ATF_CHECK(prog_validate(insns, insn_count));
1020 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 3);
1023 ATF_TC(bpfjit_alu_div4_x);
1024 ATF_TC_HEAD(bpfjit_alu_div4_x, tc)
1026 atf_tc_set_md_var(tc, "descr",
1027 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=4");
1030 ATF_TC_BODY(bpfjit_alu_div4_x, tc)
1032 static struct bpf_insn insns[] = {
1033 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
1034 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 4),
1035 BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
1036 BPF_STMT(BPF_RET+BPF_A, 0)
1039 uint8_t pkt[1]; /* the program doesn't read any data */
1041 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1043 RZ(rump_init());
1045 ATF_CHECK(prog_validate(insns, insn_count));
1046 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0x3fffffff);
1049 ATF_TC(bpfjit_alu_div10_x);
1050 ATF_TC_HEAD(bpfjit_alu_div10_x, tc)
1052 atf_tc_set_md_var(tc, "descr",
1053 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=10");
1056 ATF_TC_BODY(bpfjit_alu_div10_x, tc)
1058 static struct bpf_insn insns[] = {
1059 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
1060 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 10),
1061 BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
1062 BPF_STMT(BPF_RET+BPF_A, 0)
1065 uint8_t pkt[1]; /* the program doesn't read any data */
1067 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1069 RZ(rump_init());
1071 ATF_CHECK(prog_validate(insns, insn_count));
1072 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 429484384);
1075 ATF_TC(bpfjit_alu_div10000_x);
1076 ATF_TC_HEAD(bpfjit_alu_div10000_x, tc)
1078 atf_tc_set_md_var(tc, "descr",
1079 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=10000");
1082 ATF_TC_BODY(bpfjit_alu_div10000_x, tc)
1084 static struct bpf_insn insns[] = {
1085 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
1086 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 10000),
1087 BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
1088 BPF_STMT(BPF_RET+BPF_A, 0)
1091 uint8_t pkt[1]; /* the program doesn't read any data */
1093 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1095 RZ(rump_init());
1097 ATF_CHECK(prog_validate(insns, insn_count));
1098 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 429484);
1101 ATF_TC(bpfjit_alu_div7609801_x);
1102 ATF_TC_HEAD(bpfjit_alu_div7609801_x, tc)
1104 atf_tc_set_md_var(tc, "descr",
1105 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=7609801");
1108 ATF_TC_BODY(bpfjit_alu_div7609801_x, tc)
1110 static struct bpf_insn insns[] = {
1111 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294967295)),
1112 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(7609801)),
1113 BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
1114 BPF_STMT(BPF_RET+BPF_A, 0)
1117 uint8_t pkt[1]; /* the program doesn't read any data */
1119 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1121 RZ(rump_init());
1123 ATF_CHECK(prog_validate(insns, insn_count));
1124 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 564);
1127 ATF_TC(bpfjit_alu_div80000000_x);
1128 ATF_TC_HEAD(bpfjit_alu_div80000000_x, tc)
1130 atf_tc_set_md_var(tc, "descr",
1131 "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=0x80000000");
1134 ATF_TC_BODY(bpfjit_alu_div80000000_x, tc)
1136 static struct bpf_insn insns[] = {
1137 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffde)),
1138 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0x80000000)),
1139 BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
1140 BPF_STMT(BPF_RET+BPF_A, 0)
1143 uint8_t pkt[1]; /* the program doesn't read any data */
1145 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1147 RZ(rump_init());
1149 ATF_CHECK(prog_validate(insns, insn_count));
1150 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 1);
1153 ATF_TC(bpfjit_alu_mod0_x);
1154 ATF_TC_HEAD(bpfjit_alu_mod0_x, tc)
1156 atf_tc_set_md_var(tc, "descr",
1157 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=0");
1160 ATF_TC_BODY(bpfjit_alu_mod0_x, tc)
1162 static struct bpf_insn insns[] = {
1163 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
1164 BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1165 BPF_STMT(BPF_RET+BPF_A, 0)
1168 uint8_t pkt[1]; /* the program doesn't read any data */
1170 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1172 RZ(rump_init());
1174 ATF_CHECK(prog_validate(insns, insn_count));
1175 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0);
1178 ATF_TC(bpfjit_alu_mod1_x);
1179 ATF_TC_HEAD(bpfjit_alu_mod1_x, tc)
1181 atf_tc_set_md_var(tc, "descr",
1182 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=1");
1185 ATF_TC_BODY(bpfjit_alu_mod1_x, tc)
1187 static struct bpf_insn insns[] = {
1188 BPF_STMT(BPF_LD+BPF_IMM, 7),
1189 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1),
1190 BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1191 BPF_STMT(BPF_RET+BPF_A, 0)
1194 uint8_t pkt[1]; /* the program doesn't read any data */
1196 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1198 RZ(rump_init());
1200 ATF_CHECK(prog_validate(insns, insn_count));
1201 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0);
1204 ATF_TC(bpfjit_alu_mod2_x);
1205 ATF_TC_HEAD(bpfjit_alu_mod2_x, tc)
1207 atf_tc_set_md_var(tc, "descr",
1208 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=2");
1211 ATF_TC_BODY(bpfjit_alu_mod2_x, tc)
1213 static struct bpf_insn insns[] = {
1214 BPF_STMT(BPF_LD+BPF_IMM, 7),
1215 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
1216 BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1217 BPF_STMT(BPF_RET+BPF_A, 0)
1220 uint8_t pkt[1]; /* the program doesn't read any data */
1222 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1224 RZ(rump_init());
1226 ATF_CHECK(prog_validate(insns, insn_count));
1227 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 1);
1230 ATF_TC(bpfjit_alu_mod4_x);
1231 ATF_TC_HEAD(bpfjit_alu_mod4_x, tc)
1233 atf_tc_set_md_var(tc, "descr",
1234 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=4");
1237 ATF_TC_BODY(bpfjit_alu_mod4_x, tc)
1239 static struct bpf_insn insns[] = {
1240 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
1241 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 4),
1242 BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1243 BPF_STMT(BPF_RET+BPF_A, 0)
1246 uint8_t pkt[1]; /* the program doesn't read any data */
1248 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1250 RZ(rump_init());
1252 ATF_CHECK(prog_validate(insns, insn_count));
1253 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 3);
1256 ATF_TC(bpfjit_alu_mod10_x);
1257 ATF_TC_HEAD(bpfjit_alu_mod10_x, tc)
1259 atf_tc_set_md_var(tc, "descr",
1260 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=10");
1263 ATF_TC_BODY(bpfjit_alu_mod10_x, tc)
1265 static struct bpf_insn insns[] = {
1266 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
1267 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 10),
1268 BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1269 BPF_STMT(BPF_RET+BPF_A, 0)
1272 uint8_t pkt[1]; /* the program doesn't read any data */
1274 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1276 RZ(rump_init());
1278 ATF_CHECK(prog_validate(insns, insn_count));
1279 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 9);
1282 ATF_TC(bpfjit_alu_mod10000_x);
1283 ATF_TC_HEAD(bpfjit_alu_mod10000_x, tc)
1285 atf_tc_set_md_var(tc, "descr",
1286 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=10000");
1289 ATF_TC_BODY(bpfjit_alu_mod10000_x, tc)
1291 static struct bpf_insn insns[] = {
1292 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
1293 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 10000),
1294 BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1295 BPF_STMT(BPF_RET+BPF_A, 0)
1298 uint8_t pkt[1]; /* the program doesn't read any data */
1300 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1302 RZ(rump_init());
1304 ATF_CHECK(prog_validate(insns, insn_count));
1305 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 3849);
1308 ATF_TC(bpfjit_alu_mod7609801_x);
1309 ATF_TC_HEAD(bpfjit_alu_mod7609801_x, tc)
1311 atf_tc_set_md_var(tc, "descr",
1312 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=7609801");
1315 ATF_TC_BODY(bpfjit_alu_mod7609801_x, tc)
1317 static struct bpf_insn insns[] = {
1318 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294967295)),
1319 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(7609801)),
1320 BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1321 BPF_STMT(BPF_RET+BPF_A, 0)
1324 uint8_t pkt[1]; /* the program doesn't read any data */
1326 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1328 RZ(rump_init());
1330 ATF_CHECK(prog_validate(insns, insn_count));
1331 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 3039531);
1334 ATF_TC(bpfjit_alu_mod80000000_x);
1335 ATF_TC_HEAD(bpfjit_alu_mod80000000_x, tc)
1337 atf_tc_set_md_var(tc, "descr",
1338 "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=0x80000000");
1341 ATF_TC_BODY(bpfjit_alu_mod80000000_x, tc)
1343 static struct bpf_insn insns[] = {
1344 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffde)),
1345 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0x80000000)),
1346 BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1347 BPF_STMT(BPF_RET+BPF_A, 0)
1350 uint8_t pkt[1]; /* the program doesn't read any data */
1352 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1354 RZ(rump_init());
1356 ATF_CHECK(prog_validate(insns, insn_count));
1357 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == UINT32_C(0x7fffffde));
1360 ATF_TC(bpfjit_alu_and_x);
1361 ATF_TC_HEAD(bpfjit_alu_and_x, tc)
1363 atf_tc_set_md_var(tc, "descr",
1364 "Test JIT compilation of BPF_ALU+BPF_AND+BPF_X");
1367 ATF_TC_BODY(bpfjit_alu_and_x, tc)
1369 static struct bpf_insn insns[] = {
1370 BPF_STMT(BPF_LD+BPF_IMM, 0xdead),
1371 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0xbeef),
1372 BPF_STMT(BPF_ALU+BPF_AND+BPF_X, 0),
1373 BPF_STMT(BPF_RET+BPF_A, 0)
1376 uint8_t pkt[1]; /* the program doesn't read any data */
1378 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1380 RZ(rump_init());
1382 ATF_CHECK(prog_validate(insns, insn_count));
1383 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == (0xdead&0xbeef));
1386 ATF_TC(bpfjit_alu_or_x);
1387 ATF_TC_HEAD(bpfjit_alu_or_x, tc)
1389 atf_tc_set_md_var(tc, "descr",
1390 "Test JIT compilation of BPF_ALU+BPF_OR+BPF_X");
1393 ATF_TC_BODY(bpfjit_alu_or_x, tc)
1395 static struct bpf_insn insns[] = {
1396 BPF_STMT(BPF_LD+BPF_IMM, 0xdead0000),
1397 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0x0000beef),
1398 BPF_STMT(BPF_ALU+BPF_OR+BPF_X, 0),
1399 BPF_STMT(BPF_RET+BPF_A, 0)
1402 uint8_t pkt[1]; /* the program doesn't read any data */
1404 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1406 RZ(rump_init());
1408 ATF_CHECK(prog_validate(insns, insn_count));
1409 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xdeadbeef);
1412 ATF_TC(bpfjit_alu_xor_x);
1413 ATF_TC_HEAD(bpfjit_alu_xor_x, tc)
1415 atf_tc_set_md_var(tc, "descr",
1416 "Test JIT compilation of BPF_ALU+BPF_XOR+BPF_X");
1419 ATF_TC_BODY(bpfjit_alu_xor_x, tc)
1421 static struct bpf_insn insns[] = {
1422 BPF_STMT(BPF_LD+BPF_IMM, 0xdead0f0f),
1423 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0x0000b1e0),
1424 BPF_STMT(BPF_ALU+BPF_XOR+BPF_X, 0),
1425 BPF_STMT(BPF_RET+BPF_A, 0)
1428 uint8_t pkt[1]; /* the program doesn't read any data */
1430 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1432 RZ(rump_init());
1434 ATF_CHECK(prog_validate(insns, insn_count));
1435 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xdeadbeef);
1438 ATF_TC(bpfjit_alu_lsh_x);
1439 ATF_TC_HEAD(bpfjit_alu_lsh_x, tc)
1441 atf_tc_set_md_var(tc, "descr",
1442 "Test JIT compilation of BPF_ALU+BPF_LSH+BPF_X");
1445 ATF_TC_BODY(bpfjit_alu_lsh_x, tc)
1447 static struct bpf_insn insns[] = {
1448 BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
1449 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 16),
1450 BPF_STMT(BPF_ALU+BPF_LSH+BPF_X, 0),
1451 BPF_STMT(BPF_RET+BPF_A, 0)
1454 uint8_t pkt[1]; /* the program doesn't read any data */
1456 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1458 RZ(rump_init());
1460 ATF_CHECK(prog_validate(insns, insn_count));
1461 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xbeef0000);
1464 ATF_TC(bpfjit_alu_lsh0_x);
1465 ATF_TC_HEAD(bpfjit_alu_lsh0_x, tc)
1467 atf_tc_set_md_var(tc, "descr",
1468 "Test JIT compilation of BPF_ALU+BPF_LSH+BPF_X with k=0");
1471 ATF_TC_BODY(bpfjit_alu_lsh0_x, tc)
1473 static struct bpf_insn insns[] = {
1474 BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
1475 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
1476 BPF_STMT(BPF_ALU+BPF_LSH+BPF_X, 0),
1477 BPF_STMT(BPF_RET+BPF_A, 0)
1480 uint8_t pkt[1]; /* the program doesn't read any data */
1482 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1484 RZ(rump_init());
1486 ATF_CHECK(prog_validate(insns, insn_count));
1487 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xdeadbeef);
1490 ATF_TC(bpfjit_alu_rsh_x);
1491 ATF_TC_HEAD(bpfjit_alu_rsh_x, tc)
1493 atf_tc_set_md_var(tc, "descr",
1494 "Test JIT compilation of BPF_ALU+BPF_RSH+BPF_X");
1497 ATF_TC_BODY(bpfjit_alu_rsh_x, tc)
1499 static struct bpf_insn insns[] = {
1500 BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
1501 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 16),
1502 BPF_STMT(BPF_ALU+BPF_RSH+BPF_X, 0),
1503 BPF_STMT(BPF_RET+BPF_A, 0)
1506 uint8_t pkt[1]; /* the program doesn't read any data */
1508 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1510 RZ(rump_init());
1512 ATF_CHECK(prog_validate(insns, insn_count));
1513 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0x0000dead);
1516 ATF_TC(bpfjit_alu_rsh0_x);
1517 ATF_TC_HEAD(bpfjit_alu_rsh0_x, tc)
1519 atf_tc_set_md_var(tc, "descr",
1520 "Test JIT compilation of BPF_ALU+BPF_RSH+BPF_X with k=0");
1523 ATF_TC_BODY(bpfjit_alu_rsh0_x, tc)
1525 static struct bpf_insn insns[] = {
1526 BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
1527 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
1528 BPF_STMT(BPF_ALU+BPF_RSH+BPF_X, 0),
1529 BPF_STMT(BPF_RET+BPF_A, 0)
1532 uint8_t pkt[1]; /* the program doesn't read any data */
1534 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1536 RZ(rump_init());
1538 ATF_CHECK(prog_validate(insns, insn_count));
1539 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0xdeadbeef);
1542 ATF_TC(bpfjit_alu_modulo_x);
1543 ATF_TC_HEAD(bpfjit_alu_modulo_x, tc)
1545 atf_tc_set_md_var(tc, "descr",
1546 "Test JIT compilation of modulo logic of BPF_ALU+BPF_X operations");
1549 ATF_TC_BODY(bpfjit_alu_modulo_x, tc)
1551 static struct bpf_insn insns[] = {
1552 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0x7fffff77)),
1554 /* (7FFFFF77 * 0FFFFF77) = 07FFFFB2,F0004951 */
1555 BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0x0fffff77)),
1556 BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0),
1558 /* 07FFFFB2,F0004951 << 1 = 0FFFFF65,E00092A2 */
1559 BPF_STMT(BPF_LDX+BPF_W+BPF_K, 1),
1560 BPF_STMT(BPF_ALU+BPF_LSH+BPF_X, 0),
1562 /* 0FFFFF65,E00092A2 + DDDDDDDD = 0FFFFF66,BDDE707F */
1563 BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0xdddddddd)),
1564 BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
1566 /* 0FFFFF66,BDDE707F - FFFFFFFF = 0FFFFF65,BDDE7080 */
1567 BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0xffffffff)),
1568 BPF_STMT(BPF_ALU+BPF_SUB+BPF_X, 0),
1570 /* 0FFFFF65,BDDE7080 | 0000030C = 0FFFFF65,BDDE738C */
1571 BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0x0000030c)),
1572 BPF_STMT(BPF_ALU+BPF_OR+BPF_X, 0),
1574 /* -0FFFFF65,BDDE738C mod(2^64) = F000009A,42218C74 */
1575 BPF_STMT(BPF_ALU+BPF_NEG, 0),
1577 /* F000009A,42218C74 & FFFFFF0F = F000009A,42218C04 */
1578 BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0xffffff0f)),
1579 BPF_STMT(BPF_ALU+BPF_AND+BPF_X, 0),
1581 /* F000009A,42218C74 >> 3 = 1E000013,48443180 */
1582 /* 00000000,42218C74 >> 3 = 00000000,08443180 */
1583 BPF_STMT(BPF_LDX+BPF_W+BPF_K, 3),
1584 BPF_STMT(BPF_ALU+BPF_RSH+BPF_X, 0),
1586 /* 00000000,08443180 * 7FFFFF77 = 042218BB,93818280 */
1587 BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0x7fffff77)),
1588 BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0),
1590 /* 042218BB,93818280 / DEAD = 000004C0,71CBBBC3 */
1591 /* 00000000,93818280 / DEAD = 00000000,0000A994 */
1592 BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0xdead)),
1593 BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
1595 BPF_STMT(BPF_RET+BPF_A, 0)
1598 bpfjit_func_t code;
1599 uint8_t pkt[1]; /* the program doesn't read any data */
1601 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1603 RZ(rump_init());
1605 ATF_CHECK(prog_validate(insns, insn_count));
1607 rump_schedule();
1608 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
1609 rump_unschedule();
1610 ATF_REQUIRE(code != NULL);
1612 ATF_CHECK(jitcall(code, pkt, 1, 1) != UINT32_C(0x71cbbbc3));
1613 ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0x0000a994));
1615 rump_schedule();
1616 rumpns_bpfjit_free_code(code);
1617 rump_unschedule();
1620 ATF_TC(bpfjit_alu_neg);
1621 ATF_TC_HEAD(bpfjit_alu_neg, tc)
1623 atf_tc_set_md_var(tc, "descr",
1624 "Test JIT compilation of BPF_ALU+BPF_NEG");
1627 ATF_TC_BODY(bpfjit_alu_neg, tc)
1629 static struct bpf_insn insns[] = {
1630 BPF_STMT(BPF_LD+BPF_IMM, 777),
1631 BPF_STMT(BPF_ALU+BPF_NEG, 0),
1632 BPF_STMT(BPF_RET+BPF_A, 0)
1635 uint8_t pkt[1]; /* the program doesn't read any data */
1637 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1639 RZ(rump_init());
1641 ATF_CHECK(prog_validate(insns, insn_count));
1642 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0u-777u);
1645 ATF_TC(bpfjit_jmp_ja);
1646 ATF_TC_HEAD(bpfjit_jmp_ja, tc)
1648 atf_tc_set_md_var(tc, "descr",
1649 "Test JIT compilation of BPF_JMP+BPF_JA");
1652 ATF_TC_BODY(bpfjit_jmp_ja, tc)
1654 static struct bpf_insn insns[] = {
1655 BPF_STMT(BPF_JMP+BPF_JA, 1),
1656 BPF_STMT(BPF_RET+BPF_K, 0),
1657 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
1658 BPF_STMT(BPF_RET+BPF_K, 1),
1659 BPF_STMT(BPF_RET+BPF_K, 2),
1660 BPF_STMT(BPF_RET+BPF_K, 3),
1663 uint8_t pkt[1]; /* the program doesn't read any data */
1665 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1667 RZ(rump_init());
1669 ATF_CHECK(prog_validate(insns, insn_count));
1670 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == UINT32_MAX);
1673 ATF_TC(bpfjit_jmp_ja_invalid);
1674 ATF_TC_HEAD(bpfjit_jmp_ja_invalid, tc)
1676 atf_tc_set_md_var(tc, "descr",
1677 "Test BPF_JMP+BPF_JA to invalid destination");
1680 ATF_TC_BODY(bpfjit_jmp_ja_invalid, tc)
1682 static struct bpf_insn insns[] = {
1683 BPF_STMT(BPF_JMP+BPF_JA, 4),
1684 BPF_STMT(BPF_RET+BPF_K, 0),
1685 BPF_STMT(BPF_RET+BPF_K, 1),
1686 BPF_STMT(BPF_RET+BPF_K, 2),
1687 BPF_STMT(BPF_RET+BPF_K, 3),
1690 bpfjit_func_t code;
1691 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1693 RZ(rump_init());
1695 ATF_CHECK(!prog_validate(insns, insn_count));
1697 rump_schedule();
1698 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
1699 rump_unschedule();
1700 ATF_CHECK(code == NULL);
1703 ATF_TC(bpfjit_jmp_ja_overflow);
1704 ATF_TC_HEAD(bpfjit_jmp_ja_overflow, tc)
1706 atf_tc_set_md_var(tc, "descr",
1707 "Test BPF_JMP+BPF_JA with negative offset");
1710 ATF_TC_BODY(bpfjit_jmp_ja_overflow, tc)
1712 static struct bpf_insn insns[] = {
1713 BPF_STMT(BPF_JMP+BPF_JA, 1),
1714 BPF_STMT(BPF_RET+BPF_K, 777),
1715 BPF_STMT(BPF_JMP+BPF_JA, UINT32_MAX - 1), // -2
1716 BPF_STMT(BPF_RET+BPF_K, 0)
1719 bpfjit_func_t code;
1720 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1722 RZ(rump_init());
1724 /* Jumps with negative offsets work only in userspace. */
1725 ATF_CHECK(!prog_validate(insns, insn_count));
1727 rump_schedule();
1728 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
1729 rump_unschedule();
1730 ATF_CHECK(code == NULL);
1733 ATF_TC(bpfjit_jmp_jgt_k);
1734 ATF_TC_HEAD(bpfjit_jmp_jgt_k, tc)
1736 atf_tc_set_md_var(tc, "descr",
1737 "Test JIT compilation of BPF_JMP+BPF_JGT+BPF_K");
1740 ATF_TC_BODY(bpfjit_jmp_jgt_k, tc)
1742 static struct bpf_insn insns[] = {
1743 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
1744 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 7, 0, 1),
1745 BPF_STMT(BPF_RET+BPF_K, 0),
1746 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 2, 2, 0),
1747 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 9, 0, 0),
1748 BPF_STMT(BPF_RET+BPF_K, 1),
1749 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 4, 1, 1),
1750 BPF_STMT(BPF_RET+BPF_K, 2),
1751 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 6, 2, 3),
1752 BPF_STMT(BPF_RET+BPF_K, 3),
1753 BPF_STMT(BPF_RET+BPF_K, 4),
1754 BPF_STMT(BPF_RET+BPF_K, 5),
1755 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 5, 3, 1),
1756 BPF_STMT(BPF_RET+BPF_K, 6),
1757 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 0, 0, 0),
1758 BPF_STMT(BPF_RET+BPF_K, 7),
1759 BPF_STMT(BPF_RET+BPF_K, 8)
1762 bpfjit_func_t code;
1763 uint8_t pkt[8]; /* the program doesn't read any data */
1765 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1767 RZ(rump_init());
1769 ATF_CHECK(prog_validate(insns, insn_count));
1771 rump_schedule();
1772 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
1773 rump_unschedule();
1774 ATF_REQUIRE(code != NULL);
1776 ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
1777 ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
1778 ATF_CHECK(jitcall(code, pkt, 3, 3) == 7);
1779 ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
1780 ATF_CHECK(jitcall(code, pkt, 5, 5) == 7);
1781 ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
1782 ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
1783 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
1785 rump_schedule();
1786 rumpns_bpfjit_free_code(code);
1787 rump_unschedule();
1790 ATF_TC(bpfjit_jmp_jge_k);
1791 ATF_TC_HEAD(bpfjit_jmp_jge_k, tc)
1793 atf_tc_set_md_var(tc, "descr",
1794 "Test JIT compilation of BPF_JMP+BPF_JGE+BPF_K");
1797 ATF_TC_BODY(bpfjit_jmp_jge_k, tc)
1799 static struct bpf_insn insns[] = {
1800 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
1801 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 8, 0, 1),
1802 BPF_STMT(BPF_RET+BPF_K, 0),
1803 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 3, 2, 0),
1804 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 9, 0, 0),
1805 BPF_STMT(BPF_RET+BPF_K, 1),
1806 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 5, 1, 1),
1807 BPF_STMT(BPF_RET+BPF_K, 2),
1808 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 7, 2, 3),
1809 BPF_STMT(BPF_RET+BPF_K, 3),
1810 BPF_STMT(BPF_RET+BPF_K, 4),
1811 BPF_STMT(BPF_RET+BPF_K, 5),
1812 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 6, 3, 1),
1813 BPF_STMT(BPF_RET+BPF_K, 6),
1814 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 1, 0, 0),
1815 BPF_STMT(BPF_RET+BPF_K, 7),
1816 BPF_STMT(BPF_RET+BPF_K, 8)
1819 bpfjit_func_t code;
1820 uint8_t pkt[8]; /* the program doesn't read any data */
1822 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1824 RZ(rump_init());
1826 ATF_CHECK(prog_validate(insns, insn_count));
1828 rump_schedule();
1829 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
1830 rump_unschedule();
1831 ATF_REQUIRE(code != NULL);
1833 ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
1834 ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
1835 ATF_CHECK(jitcall(code, pkt, 3, 3) == 7);
1836 ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
1837 ATF_CHECK(jitcall(code, pkt, 5, 5) == 7);
1838 ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
1839 ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
1840 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
1842 rump_schedule();
1843 rumpns_bpfjit_free_code(code);
1844 rump_unschedule();
1847 ATF_TC(bpfjit_jmp_jeq_k);
1848 ATF_TC_HEAD(bpfjit_jmp_jeq_k, tc)
1850 atf_tc_set_md_var(tc, "descr",
1851 "Test JIT compilation of BPF_JMP+BPF_JEQ+BPF_K");
1854 ATF_TC_BODY(bpfjit_jmp_jeq_k, tc)
1856 static struct bpf_insn insns[] = {
1857 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
1858 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 8, 0, 1),
1859 BPF_STMT(BPF_RET+BPF_K, 0),
1860 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 3, 1, 0),
1861 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 9, 1, 1),
1862 BPF_STMT(BPF_RET+BPF_K, 1),
1863 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 5, 1, 1),
1864 BPF_STMT(BPF_RET+BPF_K, 2),
1865 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 7, 2, 3),
1866 BPF_STMT(BPF_RET+BPF_K, 3),
1867 BPF_STMT(BPF_RET+BPF_K, 4),
1868 BPF_STMT(BPF_RET+BPF_K, 5),
1869 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 6, 3, 1),
1870 BPF_STMT(BPF_RET+BPF_K, 6),
1871 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 1, 0, 0),
1872 BPF_STMT(BPF_RET+BPF_K, 7),
1873 BPF_STMT(BPF_RET+BPF_K, 8)
1876 bpfjit_func_t code;
1877 uint8_t pkt[8]; /* the program doesn't read any data */
1879 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1881 RZ(rump_init());
1883 ATF_CHECK(prog_validate(insns, insn_count));
1885 rump_schedule();
1886 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
1887 rump_unschedule();
1888 ATF_REQUIRE(code != NULL);
1890 ATF_CHECK(jitcall(code, pkt, 1, 1) == 7);
1891 ATF_CHECK(jitcall(code, pkt, 2, 2) == 7);
1892 ATF_CHECK(jitcall(code, pkt, 3, 3) == 1);
1893 ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
1894 ATF_CHECK(jitcall(code, pkt, 5, 5) == 7);
1895 ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
1896 ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
1897 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
1899 rump_schedule();
1900 rumpns_bpfjit_free_code(code);
1901 rump_unschedule();
1904 ATF_TC(bpfjit_jmp_jset_k);
1905 ATF_TC_HEAD(bpfjit_jmp_jset_k, tc)
1907 atf_tc_set_md_var(tc, "descr",
1908 "Test JIT compilation of BPF_JMP+BPF_JSET+BPF_K");
1911 ATF_TC_BODY(bpfjit_jmp_jset_k, tc)
1913 static struct bpf_insn insns[] = {
1914 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
1915 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 8, 0, 1),
1916 BPF_STMT(BPF_RET+BPF_K, 0),
1917 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 4, 2, 0),
1918 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 3, 0, 0),
1919 BPF_STMT(BPF_RET+BPF_K, 1),
1920 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 2, 1, 1),
1921 BPF_STMT(BPF_RET+BPF_K, 2),
1922 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 1, 2, 3),
1923 BPF_STMT(BPF_RET+BPF_K, 3),
1924 BPF_STMT(BPF_RET+BPF_K, 4),
1925 BPF_STMT(BPF_RET+BPF_K, 5),
1926 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 2, 3, 1),
1927 BPF_STMT(BPF_RET+BPF_K, 6),
1928 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 7, 0, 0),
1929 BPF_STMT(BPF_RET+BPF_K, 7),
1930 BPF_STMT(BPF_RET+BPF_K, 8)
1933 bpfjit_func_t code;
1934 uint8_t pkt[8]; /* the program doesn't read any data */
1936 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1938 RZ(rump_init());
1940 ATF_CHECK(prog_validate(insns, insn_count));
1942 rump_schedule();
1943 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
1944 rump_unschedule();
1945 ATF_REQUIRE(code != NULL);
1947 ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
1948 ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
1949 ATF_CHECK(jitcall(code, pkt, 3, 3) == 1);
1950 ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
1951 ATF_CHECK(jitcall(code, pkt, 5, 5) == 5);
1952 ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
1953 ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
1954 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
1956 rump_schedule();
1957 rumpns_bpfjit_free_code(code);
1958 rump_unschedule();
1961 ATF_TC(bpfjit_jmp_modulo_k);
1962 ATF_TC_HEAD(bpfjit_jmp_modulo_k, tc)
1964 atf_tc_set_md_var(tc, "descr",
1965 "Test JIT compilation of modulo logic of BPF_JMP+BPF_K operations");
1968 ATF_TC_BODY(bpfjit_jmp_modulo_k, tc)
1970 static struct bpf_insn insns[] = {
1971 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0x7fffff77)),
1972 BPF_STMT(BPF_ALU+BPF_LSH+BPF_K, 4),
1973 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, UINT32_C(0xfffff770), 1, 0),
1974 BPF_STMT(BPF_RET+BPF_K, 0),
1975 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, UINT32_C(0xfffff770), 0, 1),
1976 BPF_STMT(BPF_RET+BPF_K, 1),
1977 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, UINT32_C(0xfffff771), 0, 1),
1978 BPF_STMT(BPF_RET+BPF_K, 2),
1979 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, UINT32_C(0xfffff770), 0, 3),
1980 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, UINT32_C(0xfffff770), 2, 0),
1981 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, UINT32_C(0xfffff771), 1, 0),
1982 BPF_STMT(BPF_JMP+BPF_JA, 1),
1983 BPF_STMT(BPF_RET+BPF_K, 3),
1985 /* FFFFF770+FFFFF770 = 00000001,FFFFEEE0 */
1986 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, UINT32_C(0xfffff770)),
1988 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, UINT32_C(0xffffeee0), 1, 0),
1989 BPF_STMT(BPF_RET+BPF_K, 4),
1990 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, UINT32_C(0xffffeee0), 0, 1),
1991 BPF_STMT(BPF_RET+BPF_K, 5),
1992 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, UINT32_C(0xffffeee1), 0, 1),
1993 BPF_STMT(BPF_RET+BPF_K, 6),
1994 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, UINT32_C(0xffffeee0), 0, 3),
1995 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, UINT32_C(0xffffeee0), 2, 0),
1996 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, UINT32_C(0xffffeee1), 1, 0),
1997 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
1998 BPF_STMT(BPF_RET+BPF_K, 7)
2001 uint8_t pkt[1]; /* the program doesn't read any data */
2003 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2005 RZ(rump_init());
2007 ATF_CHECK(prog_validate(insns, insn_count));
2008 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == UINT32_MAX);
2011 ATF_TC(bpfjit_jmp_jgt_x);
2012 ATF_TC_HEAD(bpfjit_jmp_jgt_x, tc)
2014 atf_tc_set_md_var(tc, "descr",
2015 "Test JIT compilation of BPF_JMP+BPF_JGT+BPF_X");
2018 ATF_TC_BODY(bpfjit_jmp_jgt_x, tc)
2020 static struct bpf_insn insns[] = {
2021 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
2022 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 7),
2023 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 0, 1),
2024 BPF_STMT(BPF_RET+BPF_K, 0),
2025 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
2026 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 3, 0),
2027 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 9),
2028 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 0, 0),
2029 BPF_STMT(BPF_RET+BPF_K, 1),
2030 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 4),
2031 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 1, 1),
2032 BPF_STMT(BPF_RET+BPF_K, 2),
2033 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 6),
2034 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 2, 3),
2035 BPF_STMT(BPF_RET+BPF_K, 3),
2036 BPF_STMT(BPF_RET+BPF_K, 4),
2037 BPF_STMT(BPF_RET+BPF_K, 5),
2038 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
2039 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 4, 1),
2040 BPF_STMT(BPF_RET+BPF_K, 6),
2041 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
2042 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 0, 0),
2043 BPF_STMT(BPF_RET+BPF_K, 7),
2044 BPF_STMT(BPF_RET+BPF_K, 8)
2047 bpfjit_func_t code;
2048 uint8_t pkt[8]; /* the program doesn't read any data */
2050 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2052 RZ(rump_init());
2054 ATF_CHECK(prog_validate(insns, insn_count));
2056 rump_schedule();
2057 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
2058 rump_unschedule();
2059 ATF_REQUIRE(code != NULL);
2061 ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
2062 ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
2063 ATF_CHECK(jitcall(code, pkt, 3, 3) == 7);
2064 ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
2065 ATF_CHECK(jitcall(code, pkt, 5, 5) == 7);
2066 ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
2067 ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
2068 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
2070 rump_schedule();
2071 rumpns_bpfjit_free_code(code);
2072 rump_unschedule();
2075 ATF_TC(bpfjit_jmp_jge_x);
2076 ATF_TC_HEAD(bpfjit_jmp_jge_x, tc)
2078 atf_tc_set_md_var(tc, "descr",
2079 "Test JIT compilation of BPF_JMP+BPF_JGE+BPF_X");
2082 ATF_TC_BODY(bpfjit_jmp_jge_x, tc)
2084 static struct bpf_insn insns[] = {
2085 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
2086 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 8),
2087 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 0, 1),
2088 BPF_STMT(BPF_RET+BPF_K, 0),
2089 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
2090 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 3, 0),
2091 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 9),
2092 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 0, 0),
2093 BPF_STMT(BPF_RET+BPF_K, 1),
2094 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
2095 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 1, 1),
2096 BPF_STMT(BPF_RET+BPF_K, 2),
2097 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 7),
2098 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 2, 3),
2099 BPF_STMT(BPF_RET+BPF_K, 3),
2100 BPF_STMT(BPF_RET+BPF_K, 4),
2101 BPF_STMT(BPF_RET+BPF_K, 5),
2102 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 6),
2103 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 4, 1),
2104 BPF_STMT(BPF_RET+BPF_K, 6),
2105 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1),
2106 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 0, 0),
2107 BPF_STMT(BPF_RET+BPF_K, 7),
2108 BPF_STMT(BPF_RET+BPF_K, 8)
2111 bpfjit_func_t code;
2112 uint8_t pkt[8]; /* the program doesn't read any data */
2114 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2116 RZ(rump_init());
2118 ATF_CHECK(prog_validate(insns, insn_count));
2120 rump_schedule();
2121 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
2122 rump_unschedule();
2123 ATF_REQUIRE(code != NULL);
2125 ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
2126 ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
2127 ATF_CHECK(jitcall(code, pkt, 3, 3) == 7);
2128 ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
2129 ATF_CHECK(jitcall(code, pkt, 5, 5) == 7);
2130 ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
2131 ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
2132 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
2134 rump_schedule();
2135 rumpns_bpfjit_free_code(code);
2136 rump_unschedule();
2139 ATF_TC(bpfjit_jmp_jeq_x);
2140 ATF_TC_HEAD(bpfjit_jmp_jeq_x, tc)
2142 atf_tc_set_md_var(tc, "descr",
2143 "Test JIT compilation of BPF_JMP+BPF_JEQ+BPF_X");
2146 ATF_TC_BODY(bpfjit_jmp_jeq_x, tc)
2148 static struct bpf_insn insns[] = {
2149 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
2150 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 8),
2151 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1),
2152 BPF_STMT(BPF_RET+BPF_K, 1),
2153 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
2154 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 2, 0),
2155 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 9),
2156 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 1),
2157 BPF_STMT(BPF_RET+BPF_K, 2),
2158 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
2159 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1),
2160 BPF_STMT(BPF_RET+BPF_K, 3),
2161 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 7),
2162 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 2, 3),
2163 BPF_STMT(BPF_RET+BPF_K, 4),
2164 BPF_STMT(BPF_RET+BPF_K, 5),
2165 BPF_STMT(BPF_RET+BPF_K, 6),
2166 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 6),
2167 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 3, 1),
2168 BPF_STMT(BPF_RET+BPF_K, 7),
2169 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 0),
2170 BPF_STMT(BPF_RET+BPF_K, 8),
2171 BPF_STMT(BPF_RET+BPF_K, 9)
2174 bpfjit_func_t code;
2175 uint8_t pkt[8]; /* the program doesn't read any data */
2177 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2179 RZ(rump_init());
2181 ATF_CHECK(prog_validate(insns, insn_count));
2183 rump_schedule();
2184 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
2185 rump_unschedule();
2186 ATF_REQUIRE(code != NULL);
2188 ATF_CHECK(jitcall(code, pkt, 1, 1) == 8);
2189 ATF_CHECK(jitcall(code, pkt, 2, 2) == 8);
2190 ATF_CHECK(jitcall(code, pkt, 3, 3) == 2);
2191 ATF_CHECK(jitcall(code, pkt, 4, 4) == 8);
2192 ATF_CHECK(jitcall(code, pkt, 5, 5) == 3);
2193 ATF_CHECK(jitcall(code, pkt, 6, 6) == 9);
2194 ATF_CHECK(jitcall(code, pkt, 7, 7) == 6);
2195 ATF_CHECK(jitcall(code, pkt, 8, 8) == 1);
2197 rump_schedule();
2198 rumpns_bpfjit_free_code(code);
2199 rump_unschedule();
2202 ATF_TC(bpfjit_jmp_jset_x);
2203 ATF_TC_HEAD(bpfjit_jmp_jset_x, tc)
2205 atf_tc_set_md_var(tc, "descr",
2206 "Test JIT compilation of BPF_JMP+BPF_JSET+BPF_X");
2209 ATF_TC_BODY(bpfjit_jmp_jset_x, tc)
2211 static struct bpf_insn insns[] = {
2212 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
2213 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 8),
2214 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 0, 1),
2215 BPF_STMT(BPF_RET+BPF_K, 0),
2216 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 4),
2217 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 2, 0),
2218 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 3, 0, 0),
2219 BPF_STMT(BPF_RET+BPF_K, 1),
2220 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
2221 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 1, 1),
2222 BPF_STMT(BPF_RET+BPF_K, 2),
2223 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1),
2224 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 2, 3),
2225 BPF_STMT(BPF_RET+BPF_K, 3),
2226 BPF_STMT(BPF_RET+BPF_K, 4),
2227 BPF_STMT(BPF_RET+BPF_K, 5),
2228 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
2229 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 4, 1),
2230 BPF_STMT(BPF_RET+BPF_K, 6),
2231 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 7),
2232 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 0, 0),
2233 BPF_STMT(BPF_RET+BPF_K, 7),
2234 BPF_STMT(BPF_RET+BPF_K, 8)
2237 bpfjit_func_t code;
2238 uint8_t pkt[8]; /* the program doesn't read any data */
2240 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2242 RZ(rump_init());
2244 ATF_CHECK(prog_validate(insns, insn_count));
2246 rump_schedule();
2247 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
2248 rump_unschedule();
2249 ATF_REQUIRE(code != NULL);
2251 ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
2252 ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
2253 ATF_CHECK(jitcall(code, pkt, 3, 3) == 1);
2254 ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
2255 ATF_CHECK(jitcall(code, pkt, 5, 5) == 5);
2256 ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
2257 ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
2258 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
2260 rump_schedule();
2261 rumpns_bpfjit_free_code(code);
2262 rump_unschedule();
2265 ATF_TC(bpfjit_jmp_jeq_x_noinit_ax);
2266 ATF_TC_HEAD(bpfjit_jmp_jeq_x_noinit_ax, tc)
2268 atf_tc_set_md_var(tc, "descr", "Test JIT compilation "
2269 "of BPF_JMP+BPF_EQ+BPF_X with uninitialised A and X");
2272 ATF_TC_BODY(bpfjit_jmp_jeq_x_noinit_ax, tc)
2274 static struct bpf_insn insns[] = {
2275 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1),
2276 BPF_STMT(BPF_RET+BPF_K, 10),
2277 BPF_STMT(BPF_RET+BPF_K, 11)
2280 bpfjit_func_t code;
2281 uint8_t pkt[8]; /* the program doesn't read any data */
2283 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2285 RZ(rump_init());
2287 ATF_CHECK(prog_validate(insns, insn_count));
2289 rump_schedule();
2290 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
2291 rump_unschedule();
2292 ATF_REQUIRE(code != NULL);
2294 ATF_CHECK(jitcall(code, pkt, 1, 1) == 10);
2296 rump_schedule();
2297 rumpns_bpfjit_free_code(code);
2298 rump_unschedule();
2301 ATF_TC(bpfjit_jmp_jeq_x_noinit_a);
2302 ATF_TC_HEAD(bpfjit_jmp_jeq_x_noinit_a, tc)
2304 atf_tc_set_md_var(tc, "descr", "Test JIT compilation "
2305 "of BPF_JMP+BPF_EQ+BPF_X with uninitialised A");
2308 ATF_TC_BODY(bpfjit_jmp_jeq_x_noinit_a, tc)
2310 static struct bpf_insn insns[] = {
2311 BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0), /* X > 0 */
2312 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1),
2313 BPF_STMT(BPF_RET+BPF_K, 10),
2314 BPF_STMT(BPF_RET+BPF_K, 11)
2317 bpfjit_func_t code;
2318 uint8_t pkt[8]; /* the program doesn't read any data */
2320 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2322 RZ(rump_init());
2324 ATF_CHECK(prog_validate(insns, insn_count));
2326 rump_schedule();
2327 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
2328 rump_unschedule();
2329 ATF_REQUIRE(code != NULL);
2331 ATF_CHECK(jitcall(code, pkt, 1, 1) == 11);
2333 rump_schedule();
2334 rumpns_bpfjit_free_code(code);
2335 rump_unschedule();
2338 ATF_TC(bpfjit_jmp_jeq_x_noinit_x);
2339 ATF_TC_HEAD(bpfjit_jmp_jeq_x_noinit_x, tc)
2341 atf_tc_set_md_var(tc, "descr", "Test JIT compilation "
2342 "of BPF_JMP+BPF_EQ+BPF_X with uninitialised X");
2345 ATF_TC_BODY(bpfjit_jmp_jeq_x_noinit_x, tc)
2347 static struct bpf_insn insns[] = {
2348 BPF_STMT(BPF_LD+BPF_LEN, 0), /* A > 0 */
2349 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1),
2350 BPF_STMT(BPF_RET+BPF_K, 10),
2351 BPF_STMT(BPF_RET+BPF_K, 11)
2354 bpfjit_func_t code;
2355 uint8_t pkt[8]; /* the program doesn't read any data */
2357 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2359 RZ(rump_init());
2361 ATF_CHECK(prog_validate(insns, insn_count));
2363 rump_schedule();
2364 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
2365 rump_unschedule();
2366 ATF_REQUIRE(code != NULL);
2368 ATF_CHECK(jitcall(code, pkt, 1, 1) == 11);
2370 rump_schedule();
2371 rumpns_bpfjit_free_code(code);
2372 rump_unschedule();
2375 ATF_TC(bpfjit_jmp_modulo_x);
2376 ATF_TC_HEAD(bpfjit_jmp_modulo_x, tc)
2378 atf_tc_set_md_var(tc, "descr",
2379 "Test JIT compilation of modulo logic of BPF_JMP+BPF_X operations");
2382 ATF_TC_BODY(bpfjit_jmp_modulo_x, tc)
2384 static struct bpf_insn insns[] = {
2385 BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0x7fffff77)),
2386 /* FFFFF770 << 4 = FFFFF770 */
2387 BPF_STMT(BPF_ALU+BPF_LSH+BPF_K, 4),
2389 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xfffff770)),
2390 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 0),
2391 BPF_STMT(BPF_RET+BPF_K, 0),
2392 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 0, 1),
2393 BPF_STMT(BPF_RET+BPF_K, 1),
2394 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xfffff771)),
2395 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 0, 1),
2396 BPF_STMT(BPF_RET+BPF_K, 2),
2397 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xfffff770)),
2398 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 4),
2399 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 3, 0),
2400 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xfffff771)),
2401 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 1, 0),
2402 BPF_STMT(BPF_JMP+BPF_JA, 1),
2403 BPF_STMT(BPF_RET+BPF_K, 3),
2405 /* FFFFF770+FFFFF770 = 00000001,FFFFEEE0 */
2406 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, UINT32_C(0xfffff770)),
2408 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xffffeee0)),
2409 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 0),
2410 BPF_STMT(BPF_RET+BPF_K, 4),
2411 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 0, 1),
2412 BPF_STMT(BPF_RET+BPF_K, 5),
2413 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xffffeee1)),
2414 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 0, 1),
2415 BPF_STMT(BPF_RET+BPF_K, 6),
2416 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xffffeee0)),
2417 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 4),
2418 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 3, 0),
2419 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xffffeee1)),
2420 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 1, 0),
2421 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
2422 BPF_STMT(BPF_RET+BPF_K, 7)
2425 uint8_t pkt[1]; /* the program doesn't read any data */
2427 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2429 RZ(rump_init());
2431 ATF_CHECK(prog_validate(insns, insn_count));
2432 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == UINT32_MAX);
2435 ATF_TC(bpfjit_ld_abs);
2436 ATF_TC_HEAD(bpfjit_ld_abs, tc)
2438 atf_tc_set_md_var(tc, "descr",
2439 "Test JIT compilation of BPF_LD+BPF_ABS");
2442 ATF_TC_BODY(bpfjit_ld_abs, tc)
2444 static struct bpf_insn insns[3][2] = {
2446 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 5),
2447 BPF_STMT(BPF_RET+BPF_A, 0)
2450 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 5),
2451 BPF_STMT(BPF_RET+BPF_A, 0)
2454 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 5),
2455 BPF_STMT(BPF_RET+BPF_A, 0)
2459 static size_t lengths[3] = { 1, 2, 4 };
2460 static unsigned int expected[3] = { 0xde, 0xdead, 0xdeadbeef };
2462 size_t i, l;
2463 uint8_t *pkt = deadbeef_at_5;
2464 size_t pktsize = sizeof(deadbeef_at_5);
2466 size_t insn_count = sizeof(insns[0]) / sizeof(insns[0][0]);
2468 RZ(rump_init());
2470 for (i = 0; i < 3; i++) {
2471 bpfjit_func_t code;
2473 ATF_CHECK(prog_validate(insns[i], insn_count));
2475 rump_schedule();
2476 code = rumpns_bpfjit_generate_code(NULL, insns[i], insn_count);
2477 rump_unschedule();
2478 ATF_REQUIRE(code != NULL);
2480 for (l = 1; l < 5 + lengths[i]; l++) {
2481 ATF_CHECK(jitcall(code, pkt, l, l) == 0);
2482 ATF_CHECK(jitcall(code, pkt, pktsize, l) == 0);
2485 l = 5 + lengths[i];
2486 ATF_CHECK(jitcall(code, pkt, l, l) == expected[i]);
2487 ATF_CHECK(jitcall(code, pkt, pktsize, l) == expected[i]);
2489 l = pktsize;
2490 ATF_CHECK(jitcall(code, pkt, l, l) == expected[i]);
2492 rump_schedule();
2493 rumpns_bpfjit_free_code(code);
2494 rump_unschedule();
2498 ATF_TC(bpfjit_ld_abs_k_overflow);
2499 ATF_TC_HEAD(bpfjit_ld_abs_k_overflow, tc)
2501 atf_tc_set_md_var(tc, "descr",
2502 "Test JIT compilation of BPF_LD+BPF_ABS with overflow in k+4");
2505 ATF_TC_BODY(bpfjit_ld_abs_k_overflow, tc)
2507 static struct bpf_insn insns[12][3] = {
2509 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, UINT32_MAX),
2510 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2511 BPF_STMT(BPF_RET+BPF_K, 1)
2514 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, UINT32_MAX - 1),
2515 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2516 BPF_STMT(BPF_RET+BPF_K, 1)
2519 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX),
2520 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2521 BPF_STMT(BPF_RET+BPF_K, 1)
2524 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 1),
2525 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2526 BPF_STMT(BPF_RET+BPF_K, 1)
2529 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 2),
2530 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2531 BPF_STMT(BPF_RET+BPF_K, 1)
2534 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 3),
2535 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2536 BPF_STMT(BPF_RET+BPF_K, 1)
2539 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2540 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, UINT32_MAX),
2541 BPF_STMT(BPF_RET+BPF_K, 1)
2544 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2545 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, UINT32_MAX - 1),
2546 BPF_STMT(BPF_RET+BPF_K, 1)
2549 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2550 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX),
2551 BPF_STMT(BPF_RET+BPF_K, 1)
2554 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2555 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 1),
2556 BPF_STMT(BPF_RET+BPF_K, 1)
2559 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2560 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 2),
2561 BPF_STMT(BPF_RET+BPF_K, 1)
2564 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
2565 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 3),
2566 BPF_STMT(BPF_RET+BPF_K, 1)
2570 int i;
2571 uint8_t pkt[8] = { 0 };
2573 size_t insn_count = sizeof(insns[0]) / sizeof(insns[0][0]);
2575 RZ(rump_init());
2577 for (i = 0; i < 3; i++) {
2578 ATF_CHECK(prog_validate(insns[i], insn_count));
2579 ATF_CHECK(exec_prog(insns[i], insn_count, pkt, 8) == 0);
2583 ATF_TC(bpfjit_ld_ind);
2584 ATF_TC_HEAD(bpfjit_ld_ind, tc)
2586 atf_tc_set_md_var(tc, "descr",
2587 "Test JIT compilation of BPF_LD+BPF_IND");
2590 ATF_TC_BODY(bpfjit_ld_ind, tc)
2592 static struct bpf_insn insns[6][3] = {
2594 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
2595 BPF_STMT(BPF_LD+BPF_B+BPF_IND, 2),
2596 BPF_STMT(BPF_RET+BPF_A, 0)
2599 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
2600 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 2),
2601 BPF_STMT(BPF_RET+BPF_A, 0)
2604 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
2605 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 2),
2606 BPF_STMT(BPF_RET+BPF_A, 0)
2609 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
2610 BPF_STMT(BPF_LD+BPF_B+BPF_IND, 0),
2611 BPF_STMT(BPF_RET+BPF_A, 0)
2614 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
2615 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 0),
2616 BPF_STMT(BPF_RET+BPF_A, 0)
2619 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
2620 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 0),
2621 BPF_STMT(BPF_RET+BPF_A, 0)
2625 static size_t lengths[6] = { 1, 2, 4, 1, 2, 4 };
2627 static unsigned int expected[6] = {
2628 0xde, 0xdead, 0xdeadbeef,
2629 0xde, 0xdead, 0xdeadbeef
2632 size_t i, l;
2633 uint8_t *pkt = deadbeef_at_5;
2634 size_t pktsize = sizeof(deadbeef_at_5);
2636 size_t insn_count = sizeof(insns[0]) / sizeof(insns[0][0]);
2638 RZ(rump_init());
2640 for (i = 0; i < 3; i++) {
2641 bpfjit_func_t code;
2643 ATF_CHECK(prog_validate(insns[i], insn_count));
2645 rump_schedule();
2646 code = rumpns_bpfjit_generate_code(NULL, insns[i], insn_count);
2647 rump_unschedule();
2648 ATF_REQUIRE(code != NULL);
2650 for (l = 1; l < 5 + lengths[i]; l++) {
2651 ATF_CHECK(jitcall(code, pkt, l, l) == 0);
2652 ATF_CHECK(jitcall(code, pkt, pktsize, l) == 0);
2655 l = 5 + lengths[i];
2656 ATF_CHECK(jitcall(code, pkt, l, l) == expected[i]);
2657 ATF_CHECK(jitcall(code, pkt, pktsize, l) == expected[i]);
2659 l = pktsize;
2660 ATF_CHECK(jitcall(code, pkt, l, l) == expected[i]);
2662 rump_schedule();
2663 rumpns_bpfjit_free_code(code);
2664 rump_unschedule();
2668 ATF_TC(bpfjit_ld_ind_k_overflow);
2669 ATF_TC_HEAD(bpfjit_ld_ind_k_overflow, tc)
2671 atf_tc_set_md_var(tc, "descr",
2672 "Test JIT compilation of BPF_LD+BPF_IND with overflow in k+4");
2675 ATF_TC_BODY(bpfjit_ld_ind_k_overflow, tc)
2677 static struct bpf_insn insns[12][3] = {
2679 BPF_STMT(BPF_LD+BPF_H+BPF_IND, UINT32_MAX),
2680 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2681 BPF_STMT(BPF_RET+BPF_K, 1)
2684 BPF_STMT(BPF_LD+BPF_H+BPF_IND, UINT32_MAX - 1),
2685 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2686 BPF_STMT(BPF_RET+BPF_K, 1)
2689 BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX),
2690 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2691 BPF_STMT(BPF_RET+BPF_K, 1)
2694 BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 1),
2695 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2696 BPF_STMT(BPF_RET+BPF_K, 1)
2699 BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 2),
2700 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2701 BPF_STMT(BPF_RET+BPF_K, 1)
2704 BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 3),
2705 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2706 BPF_STMT(BPF_RET+BPF_K, 1)
2709 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2710 BPF_STMT(BPF_LD+BPF_H+BPF_IND, UINT32_MAX),
2711 BPF_STMT(BPF_RET+BPF_K, 1)
2714 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2715 BPF_STMT(BPF_LD+BPF_H+BPF_IND, UINT32_MAX - 1),
2716 BPF_STMT(BPF_RET+BPF_K, 1)
2719 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2720 BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX),
2721 BPF_STMT(BPF_RET+BPF_K, 1)
2724 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2725 BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 1),
2726 BPF_STMT(BPF_RET+BPF_K, 1)
2729 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2730 BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 2),
2731 BPF_STMT(BPF_RET+BPF_K, 1)
2734 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
2735 BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 3),
2736 BPF_STMT(BPF_RET+BPF_K, 1)
2740 int i;
2741 uint8_t pkt[8] = { 0 };
2743 size_t insn_count = sizeof(insns[0]) / sizeof(insns[0][0]);
2745 RZ(rump_init());
2747 for (i = 0; i < 3; i++) {
2749 ATF_CHECK(prog_validate(insns[i], insn_count));
2750 ATF_CHECK(exec_prog(insns[i], insn_count, pkt, 8) == 0);
2754 ATF_TC(bpfjit_ld_ind_x_overflow1);
2755 ATF_TC_HEAD(bpfjit_ld_ind_x_overflow1, tc)
2757 atf_tc_set_md_var(tc, "descr",
2758 "Test JIT compilation of BPF_LD+BPF_IND with overflow in X+4");
2761 ATF_TC_BODY(bpfjit_ld_ind_x_overflow1, tc)
2763 static struct bpf_insn insns[] = {
2764 BPF_STMT(BPF_LD+BPF_LEN, 0),
2765 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, UINT32_C(0xffffffff)),
2766 BPF_STMT(BPF_MISC+BPF_TAX, 0),
2767 BPF_STMT(BPF_LD+BPF_B+BPF_IND, 0),
2768 BPF_STMT(BPF_RET+BPF_A, 0)
2771 size_t i;
2772 bpfjit_func_t code;
2773 uint8_t pkt[8] = { 10, 20, 30, 40, 50, 60, 70, 80 };
2775 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2777 RZ(rump_init());
2779 ATF_CHECK(prog_validate(insns, insn_count));
2781 rump_schedule();
2782 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
2783 rump_unschedule();
2784 ATF_REQUIRE(code != NULL);
2786 for (i = 1; i <= sizeof(pkt); i++) {
2787 //ATF_CHECK(bpf_filter(insns, pkt, i, i) == 10 * i);
2788 ATF_CHECK(jitcall(code, pkt, i, i) == 10 * i);
2791 rump_schedule();
2792 rumpns_bpfjit_free_code(code);
2793 rump_unschedule();
2796 ATF_TC(bpfjit_ld_ind_x_overflow2);
2797 ATF_TC_HEAD(bpfjit_ld_ind_x_overflow2, tc)
2799 atf_tc_set_md_var(tc, "descr",
2800 "Test JIT compilation of BPF_LD+BPF_IND with overflow in X+4");
2803 ATF_TC_BODY(bpfjit_ld_ind_x_overflow2, tc)
2805 static struct bpf_insn insns[] = {
2806 BPF_STMT(BPF_LD+BPF_LEN, 0),
2807 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, UINT32_C(0xffffffff)),
2808 BPF_STMT(BPF_ST, 3),
2809 BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 3),
2810 BPF_STMT(BPF_LD+BPF_B+BPF_IND, 0),
2811 BPF_STMT(BPF_RET+BPF_A, 0)
2814 size_t i;
2815 bpfjit_func_t code;
2816 uint8_t pkt[8] = { 10, 20, 30, 40, 50, 60, 70, 80 };
2818 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2820 RZ(rump_init());
2822 ATF_CHECK(prog_validate(insns, insn_count));
2824 rump_schedule();
2825 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
2826 rump_unschedule();
2827 ATF_REQUIRE(code != NULL);
2829 for (i = 1; i <= sizeof(pkt); i++) {
2830 //ATF_CHECK(bpf_filter(insns, pkt, i, i) == 10 * i);
2831 ATF_CHECK(jitcall(code, pkt, i, i) == 10 * i);
2834 rump_schedule();
2835 rumpns_bpfjit_free_code(code);
2836 rump_unschedule();
2839 ATF_TC(bpfjit_ld_len);
2840 ATF_TC_HEAD(bpfjit_ld_len, tc)
2842 atf_tc_set_md_var(tc, "descr",
2843 "Test JIT compilation of BPF_LD+BPF_W+BPF_LEN");
2846 ATF_TC_BODY(bpfjit_ld_len, tc)
2848 static struct bpf_insn insns[] = {
2849 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
2850 BPF_STMT(BPF_RET+BPF_A, 0)
2853 size_t i;
2854 bpfjit_func_t code;
2855 uint8_t pkt[32]; /* the program doesn't read any data */
2857 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2859 RZ(rump_init());
2861 ATF_CHECK(prog_validate(insns, insn_count));
2863 rump_schedule();
2864 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
2865 rump_unschedule();
2866 ATF_REQUIRE(code != NULL);
2868 for (i = 0; i < sizeof(pkt); i++)
2869 ATF_CHECK(jitcall(code, pkt, i, 1) == i);
2871 rump_schedule();
2872 rumpns_bpfjit_free_code(code);
2873 rump_unschedule();
2876 ATF_TC(bpfjit_ld_imm);
2877 ATF_TC_HEAD(bpfjit_ld_imm, tc)
2879 atf_tc_set_md_var(tc, "descr",
2880 "Test JIT compilation of BPF_LD+BPF_IMM");
2883 ATF_TC_BODY(bpfjit_ld_imm, tc)
2885 static struct bpf_insn insns[] = {
2886 BPF_STMT(BPF_LD+BPF_IMM, UINT32_MAX),
2887 BPF_STMT(BPF_RET+BPF_A, 0)
2890 uint8_t pkt[1]; /* the program doesn't read any data */
2892 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2894 RZ(rump_init());
2896 ATF_CHECK(prog_validate(insns, insn_count));
2897 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == UINT32_MAX);
2900 ATF_TC(bpfjit_ldx_imm1);
2901 ATF_TC_HEAD(bpfjit_ldx_imm1, tc)
2903 atf_tc_set_md_var(tc, "descr",
2904 "Test JIT compilation of BPF_LDX+BPF_IMM");
2907 ATF_TC_BODY(bpfjit_ldx_imm1, tc)
2909 static struct bpf_insn insns[] = {
2910 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_MAX - 5),
2911 BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
2912 BPF_STMT(BPF_RET+BPF_A, 0)
2915 uint8_t pkt[1]; /* the program doesn't read any data */
2917 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2919 RZ(rump_init());
2921 ATF_CHECK(prog_validate(insns, insn_count));
2922 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == UINT32_MAX - 5);
2925 ATF_TC(bpfjit_ldx_imm2);
2926 ATF_TC_HEAD(bpfjit_ldx_imm2, tc)
2928 atf_tc_set_md_var(tc, "descr",
2929 "Test JIT compilation of BPF_LDX+BPF_IMM");
2932 ATF_TC_BODY(bpfjit_ldx_imm2, tc)
2934 static struct bpf_insn insns[] = {
2935 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
2936 BPF_STMT(BPF_LD+BPF_IMM, 5),
2937 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 0),
2938 BPF_STMT(BPF_RET+BPF_K, 7),
2939 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX)
2942 uint8_t pkt[1]; /* the program doesn't read any data */
2944 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2946 RZ(rump_init());
2948 ATF_CHECK(prog_validate(insns, insn_count));
2949 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == UINT32_MAX);
2952 ATF_TC(bpfjit_ldx_len1);
2953 ATF_TC_HEAD(bpfjit_ldx_len1, tc)
2955 atf_tc_set_md_var(tc, "descr",
2956 "Test JIT compilation of BPF_LDX+BPF_LEN");
2959 ATF_TC_BODY(bpfjit_ldx_len1, tc)
2961 static struct bpf_insn insns[] = {
2962 BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0),
2963 BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
2964 BPF_STMT(BPF_RET+BPF_A, 0)
2967 size_t i;
2968 bpfjit_func_t code;
2969 uint8_t pkt[5]; /* the program doesn't read any data */
2971 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2973 RZ(rump_init());
2975 ATF_CHECK(prog_validate(insns, insn_count));
2977 rump_schedule();
2978 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
2979 rump_unschedule();
2980 ATF_REQUIRE(code != NULL);
2982 for (i = 1; i < sizeof(pkt); i++) {
2983 ATF_CHECK(jitcall(code, pkt, i, 1) == i);
2984 ATF_CHECK(jitcall(code, pkt, i + 1, i) == i + 1);
2987 rump_schedule();
2988 rumpns_bpfjit_free_code(code);
2989 rump_unschedule();
2992 ATF_TC(bpfjit_ldx_len2);
2993 ATF_TC_HEAD(bpfjit_ldx_len2, tc)
2995 atf_tc_set_md_var(tc, "descr",
2996 "Test JIT compilation of BPF_LDX+BPF_LEN");
2999 ATF_TC_BODY(bpfjit_ldx_len2, tc)
3001 static struct bpf_insn insns[] = {
3002 BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0),
3003 BPF_STMT(BPF_LD+BPF_IMM, 5),
3004 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 0),
3005 BPF_STMT(BPF_RET+BPF_K, 7),
3006 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX)
3009 bpfjit_func_t code;
3010 uint8_t pkt[5]; /* the program doesn't read any data */
3012 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3014 RZ(rump_init());
3016 ATF_CHECK(prog_validate(insns, insn_count));
3018 rump_schedule();
3019 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3020 rump_unschedule();
3021 ATF_REQUIRE(code != NULL);
3023 ATF_CHECK(jitcall(code, pkt, 5, 1) == UINT32_MAX);
3024 ATF_CHECK(jitcall(code, pkt, 6, 5) == 7);
3026 rump_schedule();
3027 rumpns_bpfjit_free_code(code);
3028 rump_unschedule();
3031 ATF_TC(bpfjit_ldx_msh);
3032 ATF_TC_HEAD(bpfjit_ldx_msh, tc)
3034 atf_tc_set_md_var(tc, "descr",
3035 "Test JIT compilation of BPF_LDX+BPF_MSH");
3038 ATF_TC_BODY(bpfjit_ldx_msh, tc)
3040 static struct bpf_insn insns[] = {
3041 BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 1),
3042 BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
3043 BPF_STMT(BPF_RET+BPF_A, 0)
3046 uint8_t pkt[2] = { 0, 0x7a };
3048 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3050 RZ(rump_init());
3052 ATF_CHECK(prog_validate(insns, insn_count));
3053 ATF_CHECK(exec_prog(insns, insn_count, pkt, 2) == 40);
3056 ATF_TC(bpfjit_misc_tax);
3057 ATF_TC_HEAD(bpfjit_misc_tax, tc)
3059 atf_tc_set_md_var(tc, "descr",
3060 "Test JIT compilation of BPF_MISC+BPF_TAX");
3063 ATF_TC_BODY(bpfjit_misc_tax, tc)
3065 static struct bpf_insn insns[] = {
3066 BPF_STMT(BPF_LD+BPF_IMM, 3),
3067 BPF_STMT(BPF_MISC+BPF_TAX, 0),
3068 BPF_STMT(BPF_LD+BPF_B+BPF_IND, 2),
3069 BPF_STMT(BPF_RET+BPF_A, 0)
3072 uint8_t pkt[6] = { 0, 11, 22, 33, 44, 55 };
3074 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3076 RZ(rump_init());
3078 ATF_CHECK(prog_validate(insns, insn_count));
3079 ATF_CHECK(exec_prog(insns, insn_count, pkt, 6) == 55);
3082 ATF_TC(bpfjit_misc_txa);
3083 ATF_TC_HEAD(bpfjit_misc_txa, tc)
3085 atf_tc_set_md_var(tc, "descr",
3086 "Test JIT compilation of BPF_MISC+BPF_TXA");
3089 ATF_TC_BODY(bpfjit_misc_txa, tc)
3091 static struct bpf_insn insns[] = {
3092 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 391),
3093 BPF_STMT(BPF_MISC+BPF_TXA, 0),
3094 BPF_STMT(BPF_RET+BPF_A, 0)
3097 uint8_t pkt[1]; /* the program doesn't read any data */
3099 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3101 RZ(rump_init());
3103 ATF_CHECK(prog_validate(insns, insn_count));
3104 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 391);
3107 ATF_TC(bpfjit_st1);
3108 ATF_TC_HEAD(bpfjit_st1, tc)
3110 atf_tc_set_md_var(tc, "descr",
3111 "Test JIT compilation of BPF_ST");
3114 ATF_TC_BODY(bpfjit_st1, tc)
3116 static struct bpf_insn insns[] = {
3117 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
3118 BPF_STMT(BPF_ST, 0),
3119 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1),
3120 BPF_STMT(BPF_LD+BPF_MEM, 0),
3121 BPF_STMT(BPF_RET+BPF_A, 0)
3124 size_t i;
3125 bpfjit_func_t code;
3126 uint8_t pkt[16]; /* the program doesn't read any data */
3128 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3130 RZ(rump_init());
3132 ATF_CHECK(prog_validate(insns, insn_count));
3134 rump_schedule();
3135 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3136 rump_unschedule();
3137 ATF_REQUIRE(code != NULL);
3139 for (i = 1; i <= sizeof(pkt); i++)
3140 ATF_CHECK(jitcall(code, pkt, i, sizeof(pkt)) == i);
3142 rump_schedule();
3143 rumpns_bpfjit_free_code(code);
3144 rump_unschedule();
3147 ATF_TC(bpfjit_st2);
3148 ATF_TC_HEAD(bpfjit_st2, tc)
3150 atf_tc_set_md_var(tc, "descr",
3151 "Test JIT compilation of BPF_ST");
3154 ATF_TC_BODY(bpfjit_st2, tc)
3156 static struct bpf_insn insns[] = {
3157 BPF_STMT(BPF_ST, 0),
3158 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
3159 BPF_STMT(BPF_ST, BPF_MEMWORDS-1),
3160 BPF_STMT(BPF_LD+BPF_MEM, 0),
3161 BPF_STMT(BPF_RET+BPF_A, 0)
3164 uint8_t pkt[1]; /* the program doesn't read any data */
3166 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3168 RZ(rump_init());
3170 ATF_CHECK(prog_validate(insns, insn_count));
3171 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0);
3174 ATF_TC(bpfjit_st3);
3175 ATF_TC_HEAD(bpfjit_st3, tc)
3177 atf_tc_set_md_var(tc, "descr",
3178 "Test JIT compilation of BPF_ST");
3181 ATF_TC_BODY(bpfjit_st3, tc)
3183 static struct bpf_insn insns[] = {
3184 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
3185 BPF_STMT(BPF_ST, 0),
3186 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 100),
3187 BPF_STMT(BPF_ST, BPF_MEMWORDS-1),
3188 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 200),
3189 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 301, 2, 0),
3190 BPF_STMT(BPF_LD+BPF_MEM, BPF_MEMWORDS-1),
3191 BPF_STMT(BPF_RET+BPF_A, 0),
3192 BPF_STMT(BPF_LD+BPF_MEM, 0),
3193 BPF_STMT(BPF_RET+BPF_A, 0)
3196 bpfjit_func_t code;
3197 uint8_t pkt[2]; /* the program doesn't read any data */
3199 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3201 ATF_REQUIRE(BPF_MEMWORDS > 1);
3203 RZ(rump_init());
3205 ATF_CHECK(prog_validate(insns, insn_count));
3207 rump_schedule();
3208 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3209 rump_unschedule();
3210 ATF_REQUIRE(code != NULL);
3212 ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
3213 ATF_CHECK(jitcall(code, pkt, 2, 2) == 102);
3215 rump_schedule();
3216 rumpns_bpfjit_free_code(code);
3217 rump_unschedule();
3220 ATF_TC(bpfjit_st4);
3221 ATF_TC_HEAD(bpfjit_st4, tc)
3223 atf_tc_set_md_var(tc, "descr",
3224 "Test JIT compilation of BPF_ST");
3227 ATF_TC_BODY(bpfjit_st4, tc)
3229 static struct bpf_insn insns[] = {
3230 BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
3231 BPF_STMT(BPF_ST, 5),
3232 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 100),
3233 BPF_STMT(BPF_ST, BPF_MEMWORDS-1),
3234 BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 200),
3235 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 301, 2, 0),
3236 BPF_STMT(BPF_LD+BPF_MEM, BPF_MEMWORDS-1),
3237 BPF_STMT(BPF_RET+BPF_A, 0),
3238 BPF_STMT(BPF_LD+BPF_MEM, 5),
3239 BPF_STMT(BPF_RET+BPF_A, 0)
3242 bpfjit_func_t code;
3243 uint8_t pkt[2]; /* the program doesn't read any data */
3245 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3247 ATF_REQUIRE(BPF_MEMWORDS > 6);
3249 RZ(rump_init());
3251 ATF_CHECK(prog_validate(insns, insn_count));
3253 rump_schedule();
3254 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3255 rump_unschedule();
3256 ATF_REQUIRE(code != NULL);
3258 ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
3259 ATF_CHECK(jitcall(code, pkt, 2, 2) == 102);
3261 rump_schedule();
3262 rumpns_bpfjit_free_code(code);
3263 rump_unschedule();
3266 ATF_TC(bpfjit_st5);
3267 ATF_TC_HEAD(bpfjit_st5, tc)
3269 atf_tc_set_md_var(tc, "descr",
3270 "Test JIT compilation of BPF_ST");
3273 ATF_TC_BODY(bpfjit_st5, tc)
3275 struct bpf_insn insns[5*BPF_MEMWORDS+2];
3276 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3278 size_t k;
3279 bpfjit_func_t code;
3280 uint8_t pkt[BPF_MEMWORDS]; /* the program doesn't read any data */
3282 memset(insns, 0, sizeof(insns));
3284 /* for each k do M[k] = k */
3285 for (k = 0; k < BPF_MEMWORDS; k++) {
3286 insns[2*k].code = BPF_LD+BPF_IMM;
3287 insns[2*k].k = 3*k;
3288 insns[2*k+1].code = BPF_ST;
3289 insns[2*k+1].k = k;
3292 /* load wirelen into A */
3293 insns[2*BPF_MEMWORDS].code = BPF_LD+BPF_W+BPF_LEN;
3295 /* for each k, if (A == k + 1) return M[k] */
3296 for (k = 0; k < BPF_MEMWORDS; k++) {
3297 insns[2*BPF_MEMWORDS+3*k+1].code = BPF_JMP+BPF_JEQ+BPF_K;
3298 insns[2*BPF_MEMWORDS+3*k+1].k = k+1;
3299 insns[2*BPF_MEMWORDS+3*k+1].jt = 0;
3300 insns[2*BPF_MEMWORDS+3*k+1].jf = 2;
3301 insns[2*BPF_MEMWORDS+3*k+2].code = BPF_LD+BPF_MEM;
3302 insns[2*BPF_MEMWORDS+3*k+2].k = k;
3303 insns[2*BPF_MEMWORDS+3*k+3].code = BPF_RET+BPF_A;
3304 insns[2*BPF_MEMWORDS+3*k+3].k = 0;
3307 insns[5*BPF_MEMWORDS+1].code = BPF_RET+BPF_K;
3308 insns[5*BPF_MEMWORDS+1].k = UINT32_MAX;
3310 RZ(rump_init());
3312 ATF_CHECK(prog_validate(insns, insn_count));
3314 rump_schedule();
3315 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3316 rump_unschedule();
3317 ATF_REQUIRE(code != NULL);
3319 for (k = 1; k <= sizeof(pkt); k++)
3320 ATF_CHECK(jitcall(code, pkt, k, k) == 3*(k-1));
3322 rump_schedule();
3323 rumpns_bpfjit_free_code(code);
3324 rump_unschedule();
3327 ATF_TC(bpfjit_stx1);
3328 ATF_TC_HEAD(bpfjit_stx1, tc)
3330 atf_tc_set_md_var(tc, "descr",
3331 "Test JIT compilation of BPF_STX");
3334 ATF_TC_BODY(bpfjit_stx1, tc)
3336 static struct bpf_insn insns[] = {
3337 BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0),
3338 BPF_STMT(BPF_STX, 0),
3339 BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0),
3340 BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
3341 BPF_STMT(BPF_RET+BPF_A, 0)
3344 size_t i;
3345 bpfjit_func_t code;
3346 uint8_t pkt[16]; /* the program doesn't read any data */
3348 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3350 RZ(rump_init());
3352 ATF_CHECK(prog_validate(insns, insn_count));
3354 rump_schedule();
3355 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3356 rump_unschedule();
3357 ATF_REQUIRE(code != NULL);
3359 for (i = 1; i <= sizeof(pkt); i++)
3360 ATF_CHECK(jitcall(code, pkt, i, sizeof(pkt)) == i);
3362 rump_schedule();
3363 rumpns_bpfjit_free_code(code);
3364 rump_unschedule();
3367 ATF_TC(bpfjit_stx2);
3368 ATF_TC_HEAD(bpfjit_stx2, tc)
3370 atf_tc_set_md_var(tc, "descr",
3371 "Test JIT compilation of BPF_STX");
3374 ATF_TC_BODY(bpfjit_stx2, tc)
3376 static struct bpf_insn insns[] = {
3377 BPF_STMT(BPF_ST, 0),
3378 BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0),
3379 BPF_STMT(BPF_STX, BPF_MEMWORDS-1),
3380 BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0),
3381 BPF_STMT(BPF_MISC+BPF_TXA, 0),
3382 BPF_STMT(BPF_RET+BPF_A, 0)
3385 uint8_t pkt[1]; /* the program doesn't read any data */
3387 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3389 RZ(rump_init());
3391 ATF_CHECK(prog_validate(insns, insn_count));
3392 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == 0);
3395 ATF_TC(bpfjit_stx3);
3396 ATF_TC_HEAD(bpfjit_stx3, tc)
3398 atf_tc_set_md_var(tc, "descr",
3399 "Test JIT compilation of BPF_STX");
3402 ATF_TC_BODY(bpfjit_stx3, tc)
3404 static struct bpf_insn insns[] = {
3405 BPF_STMT(BPF_STX, 6),
3406 BPF_STMT(BPF_ST, 1),
3407 BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0),
3408 BPF_STMT(BPF_STX, 5),
3409 BPF_STMT(BPF_STX, 2),
3410 BPF_STMT(BPF_STX, 3),
3411 BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 1),
3412 BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
3413 BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 2),
3414 BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
3415 BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 3),
3416 BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
3417 BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 5),
3418 BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
3419 BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 6),
3420 BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
3421 BPF_STMT(BPF_RET+BPF_A, 0)
3424 size_t i;
3425 bpfjit_func_t code;
3426 uint8_t pkt[16]; /* the program doesn't read any data */
3428 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3430 RZ(rump_init());
3432 ATF_CHECK(prog_validate(insns, insn_count));
3434 rump_schedule();
3435 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3436 rump_unschedule();
3437 ATF_REQUIRE(code != NULL);
3439 for (i = 1; i <= sizeof(pkt); i++)
3440 ATF_CHECK(jitcall(code, pkt, i, sizeof(pkt)) == 3 * i);
3442 rump_schedule();
3443 rumpns_bpfjit_free_code(code);
3444 rump_unschedule();
3447 ATF_TC(bpfjit_stx4);
3448 ATF_TC_HEAD(bpfjit_stx4, tc)
3450 atf_tc_set_md_var(tc, "descr",
3451 "Test JIT compilation of BPF_STX");
3454 ATF_TC_BODY(bpfjit_stx4, tc)
3456 struct bpf_insn insns[5*BPF_MEMWORDS+2];
3457 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3459 size_t k;
3460 bpfjit_func_t code;
3461 uint8_t pkt[BPF_MEMWORDS]; /* the program doesn't read any data */
3463 memset(insns, 0, sizeof(insns));
3465 /* for each k do M[k] = k */
3466 for (k = 0; k < BPF_MEMWORDS; k++) {
3467 insns[2*k].code = BPF_LDX+BPF_W+BPF_IMM;
3468 insns[2*k].k = 3*k;
3469 insns[2*k+1].code = BPF_STX;
3470 insns[2*k+1].k = k;
3473 /* load wirelen into A */
3474 insns[2*BPF_MEMWORDS].code = BPF_LD+BPF_W+BPF_LEN;
3476 /* for each k, if (A == k + 1) return M[k] */
3477 for (k = 0; k < BPF_MEMWORDS; k++) {
3478 insns[2*BPF_MEMWORDS+3*k+1].code = BPF_JMP+BPF_JEQ+BPF_K;
3479 insns[2*BPF_MEMWORDS+3*k+1].k = k+1;
3480 insns[2*BPF_MEMWORDS+3*k+1].jt = 0;
3481 insns[2*BPF_MEMWORDS+3*k+1].jf = 2;
3482 insns[2*BPF_MEMWORDS+3*k+2].code = BPF_LD+BPF_MEM;
3483 insns[2*BPF_MEMWORDS+3*k+2].k = k;
3484 insns[2*BPF_MEMWORDS+3*k+3].code = BPF_RET+BPF_A;
3485 insns[2*BPF_MEMWORDS+3*k+3].k = 0;
3488 insns[5*BPF_MEMWORDS+1].code = BPF_RET+BPF_K;
3489 insns[5*BPF_MEMWORDS+1].k = UINT32_MAX;
3491 RZ(rump_init());
3493 ATF_CHECK(prog_validate(insns, insn_count));
3495 rump_schedule();
3496 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3497 rump_unschedule();
3498 ATF_REQUIRE(code != NULL);
3500 for (k = 1; k <= sizeof(pkt); k++)
3501 ATF_CHECK(jitcall(code, pkt, k, k) == 3*(k-1));
3503 rump_schedule();
3504 rumpns_bpfjit_free_code(code);
3505 rump_unschedule();
3508 ATF_TC(bpfjit_opt_ld_abs_1);
3509 ATF_TC_HEAD(bpfjit_opt_ld_abs_1, tc)
3511 atf_tc_set_md_var(tc, "descr",
3512 "Test JIT compilation with length optimization "
3513 "applied to BPF_LD+BPF_ABS");
3516 ATF_TC_BODY(bpfjit_opt_ld_abs_1, tc)
3518 static struct bpf_insn insns[] = {
3519 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
3520 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 8),
3521 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
3522 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
3523 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
3524 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 4),
3525 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
3526 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
3527 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 1),
3528 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
3529 BPF_STMT(BPF_RET+BPF_K, 0),
3532 size_t i, j;
3533 bpfjit_func_t code;
3534 uint8_t pkt[2][34] = {
3536 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3537 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3538 0x80, 0x03, 0x70, 0x0f,
3539 0x80, 0x03, 0x70, 0x23
3542 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3543 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3544 0x80, 0x03, 0x70, 0x23,
3545 0x80, 0x03, 0x70, 0x0f
3549 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3551 RZ(rump_init());
3553 ATF_CHECK(prog_validate(insns, insn_count));
3555 rump_schedule();
3556 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3557 rump_unschedule();
3558 ATF_REQUIRE(code != NULL);
3560 for (i = 0; i < 2; i++) {
3561 for (j = 1; j < sizeof(pkt[i]); j++)
3562 ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3563 ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
3566 rump_schedule();
3567 rumpns_bpfjit_free_code(code);
3568 rump_unschedule();
3571 ATF_TC(bpfjit_opt_ld_abs_2);
3572 ATF_TC_HEAD(bpfjit_opt_ld_abs_2, tc)
3574 atf_tc_set_md_var(tc, "descr",
3575 "Test JIT compilation with length optimization "
3576 "applied to BPF_LD+BPF_ABS");
3579 ATF_TC_BODY(bpfjit_opt_ld_abs_2, tc)
3581 static struct bpf_insn insns[] = {
3582 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
3583 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
3584 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
3585 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 6),
3586 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 5),
3587 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
3588 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 3),
3589 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
3590 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 1),
3591 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
3592 BPF_STMT(BPF_RET+BPF_K, 0),
3595 size_t i, j;
3596 bpfjit_func_t code;
3597 uint8_t pkt[2][34] = {
3599 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3600 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3601 0x80, 0x03, 0x70, 0x0f,
3602 0x80, 0x03, 0x70, 0x23
3605 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3606 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3607 0x80, 0x03, 0x70, 0x23,
3608 0x80, 0x03, 0x70, 0x0f
3612 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3614 RZ(rump_init());
3616 ATF_CHECK(prog_validate(insns, insn_count));
3618 rump_schedule();
3619 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3620 rump_unschedule();
3621 ATF_REQUIRE(code != NULL);
3623 for (i = 0; i < 2; i++) {
3624 for (j = 1; j < sizeof(pkt[i]); j++)
3625 ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3626 ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
3629 rump_schedule();
3630 rumpns_bpfjit_free_code(code);
3631 rump_unschedule();
3634 ATF_TC(bpfjit_opt_ld_abs_3);
3635 ATF_TC_HEAD(bpfjit_opt_ld_abs_3, tc)
3637 atf_tc_set_md_var(tc, "descr",
3638 "Test JIT compilation with length optimization "
3639 "applied to BPF_LD+BPF_ABS");
3642 ATF_TC_BODY(bpfjit_opt_ld_abs_3, tc)
3644 static struct bpf_insn insns[] = {
3645 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
3646 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 2),
3647 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
3648 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 3, 6),
3649 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 5),
3650 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
3651 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
3652 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
3653 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 1),
3654 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
3655 BPF_STMT(BPF_RET+BPF_K, 0),
3658 size_t i, j;
3659 bpfjit_func_t code;
3660 uint8_t pkt[2][34] = {
3662 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3663 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3664 0x80, 0x03, 0x70, 0x0f,
3665 0x80, 0x03, 0x70, 0x23
3668 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3669 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3670 0x80, 0x03, 0x70, 0x23,
3671 0x80, 0x03, 0x70, 0x0f
3675 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3677 RZ(rump_init());
3679 ATF_CHECK(prog_validate(insns, insn_count));
3681 rump_schedule();
3682 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3683 rump_unschedule();
3684 ATF_REQUIRE(code != NULL);
3686 for (i = 0; i < 2; i++) {
3687 for (j = 1; j < sizeof(pkt[i]); j++)
3688 ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3689 ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
3692 rump_schedule();
3693 rumpns_bpfjit_free_code(code);
3694 rump_unschedule();
3697 ATF_TC(bpfjit_opt_ld_ind_1);
3698 ATF_TC_HEAD(bpfjit_opt_ld_ind_1, tc)
3700 atf_tc_set_md_var(tc, "descr",
3701 "Test JIT compilation with length optimization "
3702 "applied to BPF_LD+BPF_IND");
3705 ATF_TC_BODY(bpfjit_opt_ld_ind_1, tc)
3707 static struct bpf_insn insns[] = {
3708 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 12),
3709 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 0),
3710 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 8),
3711 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 14),
3712 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
3713 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 18),
3714 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 4),
3715 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
3716 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 18),
3717 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 1),
3718 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
3719 BPF_STMT(BPF_RET+BPF_K, 0),
3722 size_t i, j;
3723 bpfjit_func_t code;
3724 uint8_t pkt[2][34] = {
3726 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3727 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3728 0x80, 0x03, 0x70, 0x0f,
3729 0x80, 0x03, 0x70, 0x23
3732 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3733 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3734 0x80, 0x03, 0x70, 0x23,
3735 0x80, 0x03, 0x70, 0x0f
3739 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3741 RZ(rump_init());
3743 ATF_CHECK(prog_validate(insns, insn_count));
3745 rump_schedule();
3746 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3747 rump_unschedule();
3748 ATF_REQUIRE(code != NULL);
3750 for (i = 0; i < 2; i++) {
3751 for (j = 1; j < sizeof(pkt[i]); j++)
3752 ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3753 ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
3756 rump_schedule();
3757 rumpns_bpfjit_free_code(code);
3758 rump_unschedule();
3761 ATF_TC(bpfjit_opt_ld_ind_2);
3762 ATF_TC_HEAD(bpfjit_opt_ld_ind_2, tc)
3764 atf_tc_set_md_var(tc, "descr",
3765 "Test JIT compilation with length optimization "
3766 "applied to BPF_LD+BPF_IND");
3769 ATF_TC_BODY(bpfjit_opt_ld_ind_2, tc)
3771 static struct bpf_insn insns[] = {
3772 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
3773 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 26),
3774 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
3775 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 30),
3776 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 6),
3777 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 5),
3778 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 30),
3779 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 3),
3780 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 12),
3781 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 1),
3782 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
3783 BPF_STMT(BPF_RET+BPF_K, 0),
3786 size_t i, j;
3787 bpfjit_func_t code;
3788 uint8_t pkt[2][34] = {
3790 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3791 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3792 0x80, 0x03, 0x70, 0x0f,
3793 0x80, 0x03, 0x70, 0x23
3796 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3797 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3798 0x80, 0x03, 0x70, 0x23,
3799 0x80, 0x03, 0x70, 0x0f
3803 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3805 RZ(rump_init());
3807 ATF_CHECK(prog_validate(insns, insn_count));
3809 rump_schedule();
3810 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3811 rump_unschedule();
3812 ATF_REQUIRE(code != NULL);
3814 for (i = 0; i < 2; i++) {
3815 for (j = 1; j < sizeof(pkt[i]); j++)
3816 ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3817 ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
3820 rump_schedule();
3821 rumpns_bpfjit_free_code(code);
3822 rump_unschedule();
3825 ATF_TC(bpfjit_opt_ld_ind_3);
3826 ATF_TC_HEAD(bpfjit_opt_ld_ind_3, tc)
3828 atf_tc_set_md_var(tc, "descr",
3829 "Test JIT compilation with length optimization "
3830 "applied to BPF_LD+BPF_IND");
3833 ATF_TC_BODY(bpfjit_opt_ld_ind_3, tc)
3835 static struct bpf_insn insns[] = {
3836 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 15),
3837 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 15),
3838 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 2),
3839 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 11),
3840 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 3, 7),
3841 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 6),
3842 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 11),
3843 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 4),
3844 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
3845 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 12),
3846 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 1),
3847 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
3848 BPF_STMT(BPF_RET+BPF_K, 0),
3851 size_t i, j;
3852 bpfjit_func_t code;
3853 uint8_t pkt[2][34] = {
3855 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3856 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3857 0x80, 0x03, 0x70, 0x0f,
3858 0x80, 0x03, 0x70, 0x23
3861 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3862 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3863 0x80, 0x03, 0x70, 0x23,
3864 0x80, 0x03, 0x70, 0x0f
3868 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3870 RZ(rump_init());
3872 ATF_CHECK(prog_validate(insns, insn_count));
3874 rump_schedule();
3875 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3876 rump_unschedule();
3877 ATF_REQUIRE(code != NULL);
3879 for (i = 0; i < 2; i++) {
3880 for (j = 1; j < sizeof(pkt[i]); j++)
3881 ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3882 ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
3885 rump_schedule();
3886 rumpns_bpfjit_free_code(code);
3887 rump_unschedule();
3890 ATF_TC(bpfjit_opt_ld_ind_4);
3891 ATF_TC_HEAD(bpfjit_opt_ld_ind_4, tc)
3893 atf_tc_set_md_var(tc, "descr",
3894 "Test JIT compilation with length optimization "
3895 "applied to BPF_LD+BPF_IND");
3898 ATF_TC_BODY(bpfjit_opt_ld_ind_4, tc)
3900 static struct bpf_insn insns[] = {
3901 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 11),
3902 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 19),
3903 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 2),
3904 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 15),
3905 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 3, 7),
3906 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 6),
3907 BPF_STMT(BPF_LD+BPF_W+BPF_IND, 15),
3908 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 4),
3909 BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
3910 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 12),
3911 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 1),
3912 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
3913 BPF_STMT(BPF_RET+BPF_K, 0),
3916 size_t i, j;
3917 bpfjit_func_t code;
3918 uint8_t pkt[2][34] = {
3920 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3921 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3922 0x80, 0x03, 0x70, 0x0f,
3923 0x80, 0x03, 0x70, 0x23
3926 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
3927 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
3928 0x80, 0x03, 0x70, 0x23,
3929 0x80, 0x03, 0x70, 0x0f
3933 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3935 RZ(rump_init());
3937 ATF_CHECK(prog_validate(insns, insn_count));
3939 rump_schedule();
3940 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3941 rump_unschedule();
3942 ATF_REQUIRE(code != NULL);
3944 for (i = 0; i < 2; i++) {
3945 for (j = 1; j < sizeof(pkt[i]); j++)
3946 ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3947 ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
3950 rump_schedule();
3951 rumpns_bpfjit_free_code(code);
3952 rump_unschedule();
3955 ATF_TC(bpfjit_abc_ja);
3956 ATF_TC_HEAD(bpfjit_abc_ja, tc)
3958 atf_tc_set_md_var(tc, "descr",
3959 "Test ABC optimization with a single BPF_JMP+BPF_JA");
3962 ATF_TC_BODY(bpfjit_abc_ja, tc)
3964 static struct bpf_insn insns[] = {
3965 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 3), /* min. length 4 */
3966 BPF_STMT(BPF_JMP+BPF_JA, 2),
3967 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, UINT32_MAX - 1),
3968 BPF_STMT(BPF_RET+BPF_K, 0),
3969 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 2), /* min. length 6 */
3970 BPF_STMT(BPF_RET+BPF_A, 0),
3971 BPF_STMT(BPF_RET+BPF_K, 1),
3972 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 6),
3973 BPF_STMT(BPF_RET+BPF_K, 2),
3974 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
3975 BPF_STMT(BPF_RET+BPF_K, 3),
3978 bpfjit_func_t code;
3979 uint8_t pkt[6] = {0, 0, /* UINT32_MAX: */ 255, 255, 255, 255};
3981 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
3983 RZ(rump_init());
3985 ATF_CHECK(prog_validate(insns, insn_count));
3987 rump_schedule();
3988 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
3989 rump_unschedule();
3990 ATF_REQUIRE(code != NULL);
3992 ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
3993 ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
3994 ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
3995 ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
3996 ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
3997 ATF_CHECK(jitcall(code, pkt, 6, 6) == UINT32_MAX);
3999 rump_schedule();
4000 rumpns_bpfjit_free_code(code);
4001 rump_unschedule();
4004 ATF_TC(bpfjit_abc_ja_over);
4005 ATF_TC_HEAD(bpfjit_abc_ja_over, tc)
4007 atf_tc_set_md_var(tc, "descr",
4008 "Test ABC optimization when BPF_JMP+BPF_JA jumps over all loads");
4011 ATF_TC_BODY(bpfjit_abc_ja_over, tc)
4013 static struct bpf_insn insns[] = {
4014 BPF_STMT(BPF_JMP+BPF_JA, 2),
4015 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 3),
4016 BPF_STMT(BPF_RET+BPF_K, 0),
4017 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
4018 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 4),
4019 BPF_STMT(BPF_RET+BPF_K, 1),
4020 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 5),
4021 BPF_STMT(BPF_RET+BPF_K, 2),
4022 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 6),
4023 BPF_STMT(BPF_RET+BPF_K, 3),
4026 uint8_t pkt[1]; /* the program doesn't read any data */
4028 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4030 RZ(rump_init());
4032 ATF_CHECK(prog_validate(insns, insn_count));
4033 ATF_CHECK(exec_prog(insns, insn_count, pkt, 1) == UINT32_MAX);
4036 ATF_TC(bpfjit_abc_ld_chain);
4037 ATF_TC_HEAD(bpfjit_abc_ld_chain, tc)
4039 atf_tc_set_md_var(tc, "descr",
4040 "Test ABC optimization of a chain of BPF_LD instructions "
4041 "with exits leading to a single BPF_RET");
4044 ATF_TC_BODY(bpfjit_abc_ld_chain, tc)
4046 static struct bpf_insn insns[] = {
4047 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 3), /* min. length 4 */
4048 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 8, 0, 4),
4049 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 4), /* min. length 6 */
4050 BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 7, 0, 2),
4051 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 6), /* min. length 10 */
4052 BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 6, 0, 1),
4053 BPF_STMT(BPF_RET+BPF_K, 123456789),
4054 BPF_STMT(BPF_RET+BPF_K, 987654321),
4057 bpfjit_func_t code;
4058 uint8_t pkt[10] = {};
4060 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4062 RZ(rump_init());
4064 ATF_CHECK(prog_validate(insns, insn_count));
4066 rump_schedule();
4067 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
4068 rump_unschedule();
4069 ATF_REQUIRE(code != NULL);
4071 /* Packet is too short. */
4072 ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4073 ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4074 ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4076 /* !(pkt[3] == 8) => return 123456789 */
4077 ATF_CHECK(jitcall(code, pkt, 4, 4) == 123456789);
4078 ATF_CHECK(jitcall(code, pkt, 5, 5) == 123456789);
4079 ATF_CHECK(jitcall(code, pkt, 6, 6) == 123456789);
4080 ATF_CHECK(jitcall(code, pkt, 7, 7) == 123456789);
4081 ATF_CHECK(jitcall(code, pkt, 8, 8) == 123456789);
4082 ATF_CHECK(jitcall(code, pkt, 9, 9) == 123456789);
4084 /* !(pkt[4:2] >= 7) => too short or return 123456789 */
4085 pkt[3] = 8;
4086 ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4087 ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4088 ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4089 ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4090 ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4091 ATF_CHECK(jitcall(code, pkt, 6, 6) == 123456789);
4092 ATF_CHECK(jitcall(code, pkt, 9, 9) == 123456789);
4094 /* !(pkt[6:4] > 6) => too short or return 987654321 */
4095 pkt[4] = pkt[5] = 1;
4096 ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4097 ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4098 ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4099 ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4100 ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4101 ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4102 ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4103 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4104 ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4105 ATF_CHECK(jitcall(code, pkt, 10, 10) == 987654321);
4107 /* (pkt[6:4] > 6) => too short or return 123456789 */
4108 pkt[6] = pkt[7] = pkt[8] = pkt[9] = 1;
4109 ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4110 ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4111 ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4112 ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4113 ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4114 ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4115 ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4116 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4117 ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4118 ATF_CHECK(jitcall(code, pkt, 10, 10) == 123456789);
4120 rump_schedule();
4121 rumpns_bpfjit_free_code(code);
4122 rump_unschedule();
4125 ATF_TC(bpfjit_examples_1);
4126 ATF_TC_HEAD(bpfjit_examples_1, tc)
4128 atf_tc_set_md_var(tc, "descr",
4129 "Test the first example from bpf(4) - "
4130 "accept Reverse ARP requests");
4133 ATF_TC_BODY(bpfjit_examples_1, tc)
4136 * The following filter is taken from the Reverse ARP
4137 * Daemon. It accepts only Reverse ARP requests.
4139 struct bpf_insn insns[] = {
4140 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
4141 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8035, 0, 3),
4142 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
4143 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 3, 0, 1),
4144 BPF_STMT(BPF_RET+BPF_K, 42),
4145 BPF_STMT(BPF_RET+BPF_K, 0),
4148 bpfjit_func_t code;
4149 uint8_t pkt[22] = {};
4151 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4153 RZ(rump_init());
4155 ATF_CHECK(prog_validate(insns, insn_count));
4157 rump_schedule();
4158 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
4159 rump_unschedule();
4160 ATF_REQUIRE(code != NULL);
4162 /* Packet is too short. */
4163 ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4164 ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4165 ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4166 ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4167 ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4168 ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4169 ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4170 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4171 ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4172 ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4173 ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4174 ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4175 ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4176 ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4177 ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4178 ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4179 ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4180 ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4181 ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4182 ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4183 ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4185 /* The packet doesn't match. */
4186 ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4188 /* Still no match after setting the protocol field. */
4189 pkt[12] = 0x80; pkt[13] = 0x35;
4190 ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4192 /* Set RARP message type. */
4193 pkt[21] = 3;
4194 ATF_CHECK(jitcall(code, pkt, 22, 22) == 42);
4196 /* Packet is too short. */
4197 ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4198 ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4199 ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4200 ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4201 ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4202 ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4203 ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4204 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4205 ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4206 ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4207 ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4208 ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4209 ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4210 ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4211 ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4212 ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4213 ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4214 ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4215 ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4216 ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4217 ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4219 /* Change RARP message type. */
4220 pkt[20] = 3;
4221 ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4223 rump_schedule();
4224 rumpns_bpfjit_free_code(code);
4225 rump_unschedule();
4228 ATF_TC(bpfjit_examples_2);
4229 ATF_TC_HEAD(bpfjit_examples_2, tc)
4231 atf_tc_set_md_var(tc, "descr",
4232 "Test the second example from bpf(4) - "
4233 "accept IP packets between two specified hosts");
4236 ATF_TC_BODY(bpfjit_examples_2, tc)
4239 * This filter accepts only IP packets between host 128.3.112.15
4240 * and 128.3.112.35.
4242 static struct bpf_insn insns[] = {
4243 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
4244 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x0800, 0, 8),
4245 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
4246 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
4247 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
4248 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 4),
4249 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
4250 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
4251 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 1),
4252 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
4253 BPF_STMT(BPF_RET+BPF_K, 0),
4256 bpfjit_func_t code;
4257 uint8_t pkt[34] = {};
4259 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4261 RZ(rump_init());
4263 ATF_CHECK(prog_validate(insns, insn_count));
4265 rump_schedule();
4266 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
4267 rump_unschedule();
4268 ATF_REQUIRE(code != NULL);
4270 /* Packet is too short. */
4271 ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4272 ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4273 ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4274 ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4275 ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4276 ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4277 ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4278 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4279 ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4280 ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4281 ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4282 ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4283 ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4284 ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4285 ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4286 ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4287 ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4288 ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4289 ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4290 ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4291 ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4292 ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4293 ATF_CHECK(jitcall(code, pkt, 23, 23) == 0);
4294 ATF_CHECK(jitcall(code, pkt, 24, 24) == 0);
4295 ATF_CHECK(jitcall(code, pkt, 25, 25) == 0);
4296 ATF_CHECK(jitcall(code, pkt, 26, 26) == 0);
4297 ATF_CHECK(jitcall(code, pkt, 27, 27) == 0);
4298 ATF_CHECK(jitcall(code, pkt, 28, 28) == 0);
4299 ATF_CHECK(jitcall(code, pkt, 29, 29) == 0);
4300 ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4301 ATF_CHECK(jitcall(code, pkt, 31, 31) == 0);
4302 ATF_CHECK(jitcall(code, pkt, 32, 32) == 0);
4303 ATF_CHECK(jitcall(code, pkt, 33, 33) == 0);
4305 /* The packet doesn't match. */
4306 ATF_CHECK(jitcall(code, pkt, 34, 34) == 0);
4308 /* Still no match after setting the protocol field. */
4309 pkt[12] = 8;
4310 ATF_CHECK(jitcall(code, pkt, 34, 34) == 0);
4312 pkt[26] = 128; pkt[27] = 3; pkt[28] = 112; pkt[29] = 15;
4313 ATF_CHECK(jitcall(code, pkt, 34, 34) == 0);
4315 pkt[30] = 128; pkt[31] = 3; pkt[32] = 112; pkt[33] = 35;
4316 ATF_CHECK(jitcall(code, pkt, 34, 34) == UINT32_MAX);
4318 /* Swap the ip addresses. */
4319 pkt[26] = 128; pkt[27] = 3; pkt[28] = 112; pkt[29] = 35;
4320 ATF_CHECK(jitcall(code, pkt, 34, 34) == 0);
4322 pkt[30] = 128; pkt[31] = 3; pkt[32] = 112; pkt[33] = 15;
4323 ATF_CHECK(jitcall(code, pkt, 34, 34) == UINT32_MAX);
4325 /* Packet is too short. */
4326 ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4327 ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4328 ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4329 ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4330 ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4331 ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4332 ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4333 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4334 ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4335 ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4336 ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4337 ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4338 ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4339 ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4340 ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4341 ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4342 ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4343 ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4344 ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4345 ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4346 ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4347 ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4348 ATF_CHECK(jitcall(code, pkt, 23, 23) == 0);
4349 ATF_CHECK(jitcall(code, pkt, 24, 24) == 0);
4350 ATF_CHECK(jitcall(code, pkt, 25, 25) == 0);
4351 ATF_CHECK(jitcall(code, pkt, 26, 26) == 0);
4352 ATF_CHECK(jitcall(code, pkt, 27, 27) == 0);
4353 ATF_CHECK(jitcall(code, pkt, 28, 28) == 0);
4354 ATF_CHECK(jitcall(code, pkt, 29, 29) == 0);
4355 ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4356 ATF_CHECK(jitcall(code, pkt, 31, 31) == 0);
4357 ATF_CHECK(jitcall(code, pkt, 32, 32) == 0);
4358 ATF_CHECK(jitcall(code, pkt, 33, 33) == 0);
4360 /* Change the protocol field. */
4361 pkt[13] = 8;
4362 ATF_CHECK(jitcall(code, pkt, 34, 34) == 0);
4364 rump_schedule();
4365 rumpns_bpfjit_free_code(code);
4366 rump_unschedule();
4369 ATF_TC(bpfjit_examples_3);
4370 ATF_TC_HEAD(bpfjit_examples_3, tc)
4372 atf_tc_set_md_var(tc, "descr",
4373 "Test the third example from bpf(4) - "
4374 "accept TCP finger packets");
4377 ATF_TC_BODY(bpfjit_examples_3, tc)
4380 * This filter returns only TCP finger packets.
4382 struct bpf_insn insns[] = {
4383 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
4384 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x0800, 0, 10),
4385 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 23),
4386 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 6, 0, 8),
4387 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
4388 BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 0x1fff, 6, 0),
4389 BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 14),
4390 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 14),
4391 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 2, 0),
4392 BPF_STMT(BPF_LD+BPF_H+BPF_IND, 16),
4393 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 0, 1),
4394 BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
4395 BPF_STMT(BPF_RET+BPF_K, 0),
4398 bpfjit_func_t code;
4399 uint8_t pkt[30] = {};
4401 /* Set IP fragment offset to non-zero. */
4402 pkt[20] = 1; pkt[21] = 1;
4404 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4406 RZ(rump_init());
4408 ATF_CHECK(prog_validate(insns, insn_count));
4410 rump_schedule();
4411 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
4412 rump_unschedule();
4413 ATF_REQUIRE(code != NULL);
4415 /* Packet is too short. */
4416 ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4417 ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4418 ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4419 ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4420 ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4421 ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4422 ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4423 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4424 ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4425 ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4426 ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4427 ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4428 ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4429 ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4430 ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4431 ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4432 ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4433 ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4434 ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4435 ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4436 ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4437 ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4438 ATF_CHECK(jitcall(code, pkt, 23, 23) == 0);
4439 ATF_CHECK(jitcall(code, pkt, 24, 24) == 0);
4440 ATF_CHECK(jitcall(code, pkt, 25, 25) == 0);
4441 ATF_CHECK(jitcall(code, pkt, 26, 26) == 0);
4442 ATF_CHECK(jitcall(code, pkt, 27, 27) == 0);
4443 ATF_CHECK(jitcall(code, pkt, 28, 28) == 0);
4444 ATF_CHECK(jitcall(code, pkt, 29, 29) == 0);
4446 /* The packet doesn't match. */
4447 ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4449 /* Still no match after setting the protocol field. */
4450 pkt[12] = 8;
4451 ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4453 /* Get one step closer to the match. */
4454 pkt[23] = 6;
4455 ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4457 /* Set IP fragment offset to zero. */
4458 pkt[20] = 0x20; pkt[21] = 0;
4459 ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4461 /* Set IP header length to 12. */
4462 pkt[14] = 0xd3;
4463 ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4465 /* Match one branch of the program. */
4466 pkt[27] = 79;
4467 ATF_CHECK(jitcall(code, pkt, 30, 30) == UINT32_MAX);
4469 /* Match the other branch of the program. */
4470 pkt[29] = 79; pkt[27] = 0;
4471 ATF_CHECK(jitcall(code, pkt, 30, 30) == UINT32_MAX);
4473 /* Packet is too short. */
4474 ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4475 ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4476 ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4477 ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4478 ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4479 ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4480 ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4481 ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4482 ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4483 ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4484 ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4485 ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4486 ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4487 ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4488 ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4489 ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4490 ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4491 ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4492 ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4493 ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4494 ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4495 ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4496 ATF_CHECK(jitcall(code, pkt, 23, 23) == 0);
4497 ATF_CHECK(jitcall(code, pkt, 24, 24) == 0);
4498 ATF_CHECK(jitcall(code, pkt, 25, 25) == 0);
4499 ATF_CHECK(jitcall(code, pkt, 26, 26) == 0);
4500 ATF_CHECK(jitcall(code, pkt, 27, 27) == 0);
4501 ATF_CHECK(jitcall(code, pkt, 28, 28) == 0);
4502 ATF_CHECK(jitcall(code, pkt, 29, 29) == 0);
4504 /* Set IP header length to 16. Packet is too short. */
4505 pkt[14] = 4;
4506 ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4508 rump_schedule();
4509 rumpns_bpfjit_free_code(code);
4510 rump_unschedule();
4513 ATF_TC(bpfjit_cop_no_ctx);
4514 ATF_TC_HEAD(bpfjit_cop_no_ctx, tc)
4516 atf_tc_set_md_var(tc, "descr", "Test that BPF_MISC|BPF_COP "
4517 "instruction can't be accepted without a context");
4520 ATF_TC_BODY(bpfjit_cop_no_ctx, tc)
4522 static struct bpf_insn insns[] = {
4523 BPF_STMT(BPF_MISC+BPF_COP, 0),
4524 BPF_STMT(BPF_RET+BPF_K, 7)
4527 bpfjit_func_t code;
4528 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4530 RZ(rump_init());
4532 ATF_CHECK(!prog_validate(insns, insn_count));
4534 rump_schedule();
4535 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
4536 rump_unschedule();
4537 ATF_CHECK(code == NULL);
4540 ATF_TC(bpfjit_copx_no_ctx);
4541 ATF_TC_HEAD(bpfjit_copx_no_ctx, tc)
4543 atf_tc_set_md_var(tc, "descr", "Test that BPF_MISC|BPF_COPX "
4544 "instruction can't be accepted without a context");
4547 ATF_TC_BODY(bpfjit_copx_no_ctx, tc)
4549 static struct bpf_insn insns[] = {
4550 BPF_STMT(BPF_MISC+BPF_COPX, 0),
4551 BPF_STMT(BPF_RET+BPF_K, 7)
4554 bpfjit_func_t code;
4555 size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4557 RZ(rump_init());
4559 ATF_CHECK(!prog_validate(insns, insn_count));
4561 rump_schedule();
4562 code = rumpns_bpfjit_generate_code(NULL, insns, insn_count);
4563 rump_unschedule();
4564 ATF_CHECK(code == NULL);
4567 ATF_TP_ADD_TCS(tp)
4571 * For every new test please also add a similar test
4572 * to ../../lib/libbpfjit/t_bpfjit.c
4574 ATF_TP_ADD_TC(tp, bpfjit_empty);
4575 ATF_TP_ADD_TC(tp, bpfjit_ret_k);
4576 ATF_TP_ADD_TC(tp, bpfjit_bad_ret_k);
4577 ATF_TP_ADD_TC(tp, bpfjit_alu_add_k);
4578 ATF_TP_ADD_TC(tp, bpfjit_alu_sub_k);
4579 ATF_TP_ADD_TC(tp, bpfjit_alu_mul_k);
4580 ATF_TP_ADD_TC(tp, bpfjit_alu_div0_k);
4581 ATF_TP_ADD_TC(tp, bpfjit_alu_div1_k);
4582 ATF_TP_ADD_TC(tp, bpfjit_alu_div2_k);
4583 ATF_TP_ADD_TC(tp, bpfjit_alu_div4_k);
4584 ATF_TP_ADD_TC(tp, bpfjit_alu_div10_k);
4585 ATF_TP_ADD_TC(tp, bpfjit_alu_div10000_k);
4586 ATF_TP_ADD_TC(tp, bpfjit_alu_div7609801_k);
4587 ATF_TP_ADD_TC(tp, bpfjit_alu_div80000000_k);
4588 ATF_TP_ADD_TC(tp, bpfjit_alu_mod0_k);
4589 ATF_TP_ADD_TC(tp, bpfjit_alu_mod1_k);
4590 ATF_TP_ADD_TC(tp, bpfjit_alu_mod2_k);
4591 ATF_TP_ADD_TC(tp, bpfjit_alu_mod4_k);
4592 ATF_TP_ADD_TC(tp, bpfjit_alu_mod10_k);
4593 ATF_TP_ADD_TC(tp, bpfjit_alu_mod10000_k);
4594 ATF_TP_ADD_TC(tp, bpfjit_alu_mod7609801_k);
4595 ATF_TP_ADD_TC(tp, bpfjit_alu_mod80000000_k);
4596 ATF_TP_ADD_TC(tp, bpfjit_alu_and_k);
4597 ATF_TP_ADD_TC(tp, bpfjit_alu_or_k);
4598 ATF_TP_ADD_TC(tp, bpfjit_alu_xor_k);
4599 ATF_TP_ADD_TC(tp, bpfjit_alu_lsh_k);
4600 ATF_TP_ADD_TC(tp, bpfjit_alu_lsh0_k);
4601 ATF_TP_ADD_TC(tp, bpfjit_alu_rsh_k);
4602 ATF_TP_ADD_TC(tp, bpfjit_alu_rsh0_k);
4603 ATF_TP_ADD_TC(tp, bpfjit_alu_modulo_k);
4604 ATF_TP_ADD_TC(tp, bpfjit_alu_add_x);
4605 ATF_TP_ADD_TC(tp, bpfjit_alu_sub_x);
4606 ATF_TP_ADD_TC(tp, bpfjit_alu_mul_x);
4607 ATF_TP_ADD_TC(tp, bpfjit_alu_div0_x);
4608 ATF_TP_ADD_TC(tp, bpfjit_alu_div1_x);
4609 ATF_TP_ADD_TC(tp, bpfjit_alu_div2_x);
4610 ATF_TP_ADD_TC(tp, bpfjit_alu_div4_x);
4611 ATF_TP_ADD_TC(tp, bpfjit_alu_div10_x);
4612 ATF_TP_ADD_TC(tp, bpfjit_alu_div10000_x);
4613 ATF_TP_ADD_TC(tp, bpfjit_alu_div7609801_x);
4614 ATF_TP_ADD_TC(tp, bpfjit_alu_div80000000_x);
4615 ATF_TP_ADD_TC(tp, bpfjit_alu_mod0_x);
4616 ATF_TP_ADD_TC(tp, bpfjit_alu_mod1_x);
4617 ATF_TP_ADD_TC(tp, bpfjit_alu_mod2_x);
4618 ATF_TP_ADD_TC(tp, bpfjit_alu_mod4_x);
4619 ATF_TP_ADD_TC(tp, bpfjit_alu_mod10_x);
4620 ATF_TP_ADD_TC(tp, bpfjit_alu_mod10000_x);
4621 ATF_TP_ADD_TC(tp, bpfjit_alu_mod7609801_x);
4622 ATF_TP_ADD_TC(tp, bpfjit_alu_mod80000000_x);
4623 ATF_TP_ADD_TC(tp, bpfjit_alu_and_x);
4624 ATF_TP_ADD_TC(tp, bpfjit_alu_or_x);
4625 ATF_TP_ADD_TC(tp, bpfjit_alu_xor_x);
4626 ATF_TP_ADD_TC(tp, bpfjit_alu_lsh_x);
4627 ATF_TP_ADD_TC(tp, bpfjit_alu_lsh0_x);
4628 ATF_TP_ADD_TC(tp, bpfjit_alu_rsh_x);
4629 ATF_TP_ADD_TC(tp, bpfjit_alu_rsh0_x);
4630 ATF_TP_ADD_TC(tp, bpfjit_alu_modulo_x);
4631 ATF_TP_ADD_TC(tp, bpfjit_alu_neg);
4632 ATF_TP_ADD_TC(tp, bpfjit_jmp_ja);
4633 ATF_TP_ADD_TC(tp, bpfjit_jmp_ja_invalid);
4634 ATF_TP_ADD_TC(tp, bpfjit_jmp_ja_overflow);
4635 ATF_TP_ADD_TC(tp, bpfjit_jmp_jgt_k);
4636 ATF_TP_ADD_TC(tp, bpfjit_jmp_jge_k);
4637 ATF_TP_ADD_TC(tp, bpfjit_jmp_jeq_k);
4638 ATF_TP_ADD_TC(tp, bpfjit_jmp_jset_k);
4639 ATF_TP_ADD_TC(tp, bpfjit_jmp_modulo_k);
4640 ATF_TP_ADD_TC(tp, bpfjit_jmp_jgt_x);
4641 ATF_TP_ADD_TC(tp, bpfjit_jmp_jge_x);
4642 ATF_TP_ADD_TC(tp, bpfjit_jmp_jeq_x);
4643 ATF_TP_ADD_TC(tp, bpfjit_jmp_jset_x);
4644 ATF_TP_ADD_TC(tp, bpfjit_jmp_jeq_x_noinit_ax);
4645 ATF_TP_ADD_TC(tp, bpfjit_jmp_jeq_x_noinit_a);
4646 ATF_TP_ADD_TC(tp, bpfjit_jmp_jeq_x_noinit_x);
4647 ATF_TP_ADD_TC(tp, bpfjit_jmp_modulo_x);
4648 ATF_TP_ADD_TC(tp, bpfjit_ld_abs);
4649 ATF_TP_ADD_TC(tp, bpfjit_ld_abs_k_overflow);
4650 ATF_TP_ADD_TC(tp, bpfjit_ld_ind);
4651 ATF_TP_ADD_TC(tp, bpfjit_ld_ind_k_overflow);
4652 ATF_TP_ADD_TC(tp, bpfjit_ld_ind_x_overflow1);
4653 ATF_TP_ADD_TC(tp, bpfjit_ld_ind_x_overflow2);
4654 ATF_TP_ADD_TC(tp, bpfjit_ld_len);
4655 ATF_TP_ADD_TC(tp, bpfjit_ld_imm);
4656 ATF_TP_ADD_TC(tp, bpfjit_ldx_imm1);
4657 ATF_TP_ADD_TC(tp, bpfjit_ldx_imm2);
4658 ATF_TP_ADD_TC(tp, bpfjit_ldx_len1);
4659 ATF_TP_ADD_TC(tp, bpfjit_ldx_len2);
4660 ATF_TP_ADD_TC(tp, bpfjit_ldx_msh);
4661 ATF_TP_ADD_TC(tp, bpfjit_misc_tax);
4662 ATF_TP_ADD_TC(tp, bpfjit_misc_txa);
4663 ATF_TP_ADD_TC(tp, bpfjit_st1);
4664 ATF_TP_ADD_TC(tp, bpfjit_st2);
4665 ATF_TP_ADD_TC(tp, bpfjit_st3);
4666 ATF_TP_ADD_TC(tp, bpfjit_st4);
4667 ATF_TP_ADD_TC(tp, bpfjit_st5);
4668 ATF_TP_ADD_TC(tp, bpfjit_stx1);
4669 ATF_TP_ADD_TC(tp, bpfjit_stx2);
4670 ATF_TP_ADD_TC(tp, bpfjit_stx3);
4671 ATF_TP_ADD_TC(tp, bpfjit_stx4);
4672 ATF_TP_ADD_TC(tp, bpfjit_opt_ld_abs_1);
4673 ATF_TP_ADD_TC(tp, bpfjit_opt_ld_abs_2);
4674 ATF_TP_ADD_TC(tp, bpfjit_opt_ld_abs_3);
4675 ATF_TP_ADD_TC(tp, bpfjit_opt_ld_ind_1);
4676 ATF_TP_ADD_TC(tp, bpfjit_opt_ld_ind_2);
4677 ATF_TP_ADD_TC(tp, bpfjit_opt_ld_ind_3);
4678 ATF_TP_ADD_TC(tp, bpfjit_opt_ld_ind_4);
4679 ATF_TP_ADD_TC(tp, bpfjit_abc_ja);
4680 ATF_TP_ADD_TC(tp, bpfjit_abc_ja_over);
4681 ATF_TP_ADD_TC(tp, bpfjit_abc_ld_chain);
4682 ATF_TP_ADD_TC(tp, bpfjit_examples_1);
4683 ATF_TP_ADD_TC(tp, bpfjit_examples_2);
4684 ATF_TP_ADD_TC(tp, bpfjit_examples_3);
4685 ATF_TP_ADD_TC(tp, bpfjit_cop_no_ctx);
4686 ATF_TP_ADD_TC(tp, bpfjit_copx_no_ctx);
4688 return atf_no_error();