xref: /minix3/tests/lib/libbpfjit/t_bpfjit.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: t_bpfjit.c,v 1.14 2015/02/14 22:40:18 alnsn Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2011-2012, 2014-2015 Alexander Nasonov.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
811be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
911be35a1SLionel Sambuc  * are met:
1011be35a1SLionel Sambuc  *
1111be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1211be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1311be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
1511be35a1SLionel Sambuc  *    the documentation and/or other materials provided with the
1611be35a1SLionel Sambuc  *    distribution.
1711be35a1SLionel Sambuc  *
1811be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1911be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2011be35a1SLionel Sambuc  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2111be35a1SLionel Sambuc  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
2211be35a1SLionel Sambuc  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2311be35a1SLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2411be35a1SLionel Sambuc  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2511be35a1SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2611be35a1SLionel Sambuc  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2711be35a1SLionel Sambuc  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2811be35a1SLionel Sambuc  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2911be35a1SLionel Sambuc  * SUCH DAMAGE.
3011be35a1SLionel Sambuc  */
3111be35a1SLionel Sambuc 
3211be35a1SLionel Sambuc #include <sys/cdefs.h>
33*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_bpfjit.c,v 1.14 2015/02/14 22:40:18 alnsn Exp $");
3411be35a1SLionel Sambuc 
3511be35a1SLionel Sambuc #include <atf-c.h>
3611be35a1SLionel Sambuc #include <stdint.h>
3711be35a1SLionel Sambuc #include <string.h>
3811be35a1SLionel Sambuc 
3984d9c625SLionel Sambuc #include <net/bpf.h>
4084d9c625SLionel Sambuc #include <net/bpfjit.h>
4184d9c625SLionel Sambuc 
4211be35a1SLionel Sambuc static uint8_t deadbeef_at_5[16] = {
4311be35a1SLionel Sambuc 	0, 0xf1, 2, 0xf3, 4, 0xde, 0xad, 0xbe, 0xef, 0xff
4411be35a1SLionel Sambuc };
4511be35a1SLionel Sambuc 
46*0a6a1f1dSLionel Sambuc static inline
jitcall(bpfjit_func_t fn,const uint8_t * pkt,unsigned int wirelen,unsigned int buflen)47*0a6a1f1dSLionel Sambuc unsigned int jitcall(bpfjit_func_t fn,
48*0a6a1f1dSLionel Sambuc     const uint8_t *pkt, unsigned int wirelen, unsigned int buflen)
4911be35a1SLionel Sambuc {
50*0a6a1f1dSLionel Sambuc 	bpf_args_t args;
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc 	args.pkt = pkt;
53*0a6a1f1dSLionel Sambuc 	args.wirelen = wirelen;
54*0a6a1f1dSLionel Sambuc 	args.buflen = buflen;
55*0a6a1f1dSLionel Sambuc 
56*0a6a1f1dSLionel Sambuc 	return fn(NULL, &args);
5711be35a1SLionel Sambuc }
5811be35a1SLionel Sambuc 
59*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_empty);
ATF_TC_HEAD(libbpfjit_empty,tc)60*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_empty, tc)
61*0a6a1f1dSLionel Sambuc {
62*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
63*0a6a1f1dSLionel Sambuc 	    "Test that JIT compilation of an empty bpf program fails");
64*0a6a1f1dSLionel Sambuc }
65*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_empty,tc)66*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_empty, tc)
6711be35a1SLionel Sambuc {
6811be35a1SLionel Sambuc 	struct bpf_insn dummy;
6911be35a1SLionel Sambuc 
70*0a6a1f1dSLionel Sambuc 	ATF_CHECK(!bpf_validate(&dummy, 0));
71*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpfjit_generate_code(NULL, &dummy, 0) == NULL);
7211be35a1SLionel Sambuc }
7311be35a1SLionel Sambuc 
74*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ret_k);
ATF_TC_HEAD(libbpfjit_ret_k,tc)75*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ret_k, tc)
76*0a6a1f1dSLionel Sambuc {
77*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
78*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of a trivial bpf program");
79*0a6a1f1dSLionel Sambuc }
80*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_ret_k,tc)81*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ret_k, tc)
82*0a6a1f1dSLionel Sambuc {
83*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
84*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 17)
85*0a6a1f1dSLionel Sambuc 	};
86*0a6a1f1dSLionel Sambuc 
87*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
88*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
89*0a6a1f1dSLionel Sambuc 
90*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
93*0a6a1f1dSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
95*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
96*0a6a1f1dSLionel Sambuc 
97*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 17);
98*0a6a1f1dSLionel Sambuc 
99*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
100*0a6a1f1dSLionel Sambuc }
101*0a6a1f1dSLionel Sambuc 
102*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_bad_ret_k);
ATF_TC_HEAD(libbpfjit_bad_ret_k,tc)103*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_bad_ret_k, tc)
104*0a6a1f1dSLionel Sambuc {
105*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
106*0a6a1f1dSLionel Sambuc 	    "Test that JIT compilation of a program with bad BPF_RET fails");
107*0a6a1f1dSLionel Sambuc }
108*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_bad_ret_k,tc)109*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_bad_ret_k, tc)
110*0a6a1f1dSLionel Sambuc {
111*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
112*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K+0x8000, 13)
113*0a6a1f1dSLionel Sambuc 	};
114*0a6a1f1dSLionel Sambuc 
115*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
116*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
119*0a6a1f1dSLionel Sambuc 
120*0a6a1f1dSLionel Sambuc 	/*
121*0a6a1f1dSLionel Sambuc 	 * The point of this test is checking a bad instruction of
122*0a6a1f1dSLionel Sambuc 	 * a valid class and with a valid BPF_RVAL data.
123*0a6a1f1dSLionel Sambuc 	 */
124*0a6a1f1dSLionel Sambuc 	const uint16_t rcode = insns[0].code;
125*0a6a1f1dSLionel Sambuc 	ATF_CHECK(BPF_CLASS(rcode) == BPF_RET &&
126*0a6a1f1dSLionel Sambuc 	    (BPF_RVAL(rcode) == BPF_K || BPF_RVAL(rcode) == BPF_A));
127*0a6a1f1dSLionel Sambuc 
128*0a6a1f1dSLionel Sambuc 	ATF_CHECK(!bpf_validate(insns, insn_count));
129*0a6a1f1dSLionel Sambuc 
130*0a6a1f1dSLionel Sambuc 	/* Current implementation generates code. */
131*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
132*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
133*0a6a1f1dSLionel Sambuc 
134*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 13);
135*0a6a1f1dSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
137*0a6a1f1dSLionel Sambuc }
138*0a6a1f1dSLionel Sambuc 
139*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_add_k);
ATF_TC_HEAD(libbpfjit_alu_add_k,tc)140*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_add_k, tc)
14111be35a1SLionel Sambuc {
14211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
14311be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_ADD+BPF_K");
14411be35a1SLionel Sambuc }
14511be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_add_k,tc)146*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_add_k, tc)
14711be35a1SLionel Sambuc {
14811be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
14911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 3),
15011be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 2),
15111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
15211be35a1SLionel Sambuc 	};
15311be35a1SLionel Sambuc 
15484d9c625SLionel Sambuc 	bpfjit_func_t code;
15511be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
15611be35a1SLionel Sambuc 
15711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
15811be35a1SLionel Sambuc 
15911be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
16011be35a1SLionel Sambuc 
161*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
16211be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
16311be35a1SLionel Sambuc 
164*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 5);
16511be35a1SLionel Sambuc 
16611be35a1SLionel Sambuc 	bpfjit_free_code(code);
16711be35a1SLionel Sambuc }
16811be35a1SLionel Sambuc 
169*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_sub_k);
ATF_TC_HEAD(libbpfjit_alu_sub_k,tc)170*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_sub_k, tc)
17111be35a1SLionel Sambuc {
17211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
17311be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_SUB+BPF_K");
17411be35a1SLionel Sambuc }
17511be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_sub_k,tc)176*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_sub_k, tc)
17711be35a1SLionel Sambuc {
17811be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
17911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 1),
18011be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_SUB+BPF_K, 2),
18111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
18211be35a1SLionel Sambuc 	};
18311be35a1SLionel Sambuc 
18484d9c625SLionel Sambuc 	bpfjit_func_t code;
18511be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
18611be35a1SLionel Sambuc 
18711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
18811be35a1SLionel Sambuc 
18911be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
19011be35a1SLionel Sambuc 
191*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
19211be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
19311be35a1SLionel Sambuc 
194*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_MAX);
19511be35a1SLionel Sambuc 
19611be35a1SLionel Sambuc 	bpfjit_free_code(code);
19711be35a1SLionel Sambuc }
19811be35a1SLionel Sambuc 
199*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mul_k);
ATF_TC_HEAD(libbpfjit_alu_mul_k,tc)200*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mul_k, tc)
20111be35a1SLionel Sambuc {
20211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
20311be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MUL+BPF_K");
20411be35a1SLionel Sambuc }
20511be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mul_k,tc)206*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mul_k, tc)
20711be35a1SLionel Sambuc {
20811be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
20911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
21011be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MUL+BPF_K, 3),
21111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
21211be35a1SLionel Sambuc 	};
21311be35a1SLionel Sambuc 
21484d9c625SLionel Sambuc 	bpfjit_func_t code;
21511be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
21611be35a1SLionel Sambuc 
21711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
21811be35a1SLionel Sambuc 
21911be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
22011be35a1SLionel Sambuc 
221*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
22211be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
22311be35a1SLionel Sambuc 
224*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0xfffffffd));
22511be35a1SLionel Sambuc 
22611be35a1SLionel Sambuc 	bpfjit_free_code(code);
22711be35a1SLionel Sambuc }
22811be35a1SLionel Sambuc 
229*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div0_k);
ATF_TC_HEAD(libbpfjit_alu_div0_k,tc)230*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div0_k, tc)
23111be35a1SLionel Sambuc {
23211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
23311be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=0");
23411be35a1SLionel Sambuc }
23511be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div0_k,tc)236*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div0_k, tc)
23711be35a1SLionel Sambuc {
23811be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
23911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 0),
24011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
24111be35a1SLionel Sambuc 	};
24211be35a1SLionel Sambuc 
24384d9c625SLionel Sambuc 	bpfjit_func_t code;
24411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
24511be35a1SLionel Sambuc 
24611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
24711be35a1SLionel Sambuc 
24811be35a1SLionel Sambuc 	//ATF_CHECK(bpf_validate(insns, insn_count));
24911be35a1SLionel Sambuc 
250*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
25111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
25211be35a1SLionel Sambuc 
253*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
25411be35a1SLionel Sambuc 
25511be35a1SLionel Sambuc 	bpfjit_free_code(code);
25611be35a1SLionel Sambuc }
25711be35a1SLionel Sambuc 
258*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div1_k);
ATF_TC_HEAD(libbpfjit_alu_div1_k,tc)259*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div1_k, tc)
26011be35a1SLionel Sambuc {
26111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
26211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=1");
26311be35a1SLionel Sambuc }
26411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div1_k,tc)265*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div1_k, tc)
26611be35a1SLionel Sambuc {
26711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
26811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 7),
26911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 1),
27011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
27111be35a1SLionel Sambuc 	};
27211be35a1SLionel Sambuc 
27384d9c625SLionel Sambuc 	bpfjit_func_t code;
27411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
27511be35a1SLionel Sambuc 
27611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
27711be35a1SLionel Sambuc 
27811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
27911be35a1SLionel Sambuc 
280*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
28111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
28211be35a1SLionel Sambuc 
283*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 7);
28411be35a1SLionel Sambuc 
28511be35a1SLionel Sambuc 	bpfjit_free_code(code);
28611be35a1SLionel Sambuc }
28711be35a1SLionel Sambuc 
288*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div2_k);
ATF_TC_HEAD(libbpfjit_alu_div2_k,tc)289*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div2_k, tc)
29011be35a1SLionel Sambuc {
29111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
29211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=2");
29311be35a1SLionel Sambuc }
29411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div2_k,tc)295*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div2_k, tc)
29611be35a1SLionel Sambuc {
29711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
29811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 7),
29911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 2),
30011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
30111be35a1SLionel Sambuc 	};
30211be35a1SLionel Sambuc 
30384d9c625SLionel Sambuc 	bpfjit_func_t code;
30411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
30511be35a1SLionel Sambuc 
30611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
30711be35a1SLionel Sambuc 
30811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
30911be35a1SLionel Sambuc 
310*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
31111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
31211be35a1SLionel Sambuc 
313*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 3);
31411be35a1SLionel Sambuc 
31511be35a1SLionel Sambuc 	bpfjit_free_code(code);
31611be35a1SLionel Sambuc }
31711be35a1SLionel Sambuc 
318*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div4_k);
ATF_TC_HEAD(libbpfjit_alu_div4_k,tc)319*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div4_k, tc)
32011be35a1SLionel Sambuc {
32111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
32211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=4");
32311be35a1SLionel Sambuc }
32411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div4_k,tc)325*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div4_k, tc)
32611be35a1SLionel Sambuc {
32711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
32811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
32911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 4),
33011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
33111be35a1SLionel Sambuc 	};
33211be35a1SLionel Sambuc 
33384d9c625SLionel Sambuc 	bpfjit_func_t code;
33411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
33511be35a1SLionel Sambuc 
33611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
33711be35a1SLionel Sambuc 
33811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
33911be35a1SLionel Sambuc 
340*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
34111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
34211be35a1SLionel Sambuc 
343*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0x3fffffff));
34411be35a1SLionel Sambuc 
34511be35a1SLionel Sambuc 	bpfjit_free_code(code);
34611be35a1SLionel Sambuc }
34711be35a1SLionel Sambuc 
348*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div10_k);
ATF_TC_HEAD(libbpfjit_alu_div10_k,tc)349*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div10_k, tc)
35011be35a1SLionel Sambuc {
35111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
35211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=10");
35311be35a1SLionel Sambuc }
35411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div10_k,tc)355*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div10_k, tc)
35611be35a1SLionel Sambuc {
35711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
35811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
35911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 10),
36011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
36111be35a1SLionel Sambuc 	};
36211be35a1SLionel Sambuc 
36384d9c625SLionel Sambuc 	bpfjit_func_t code;
36411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
36511be35a1SLionel Sambuc 
36611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
36711be35a1SLionel Sambuc 
36811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
36911be35a1SLionel Sambuc 
370*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
37111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
37211be35a1SLionel Sambuc 
373*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(429484384));
37411be35a1SLionel Sambuc 
37511be35a1SLionel Sambuc 	bpfjit_free_code(code);
37611be35a1SLionel Sambuc }
37711be35a1SLionel Sambuc 
378*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div10000_k);
ATF_TC_HEAD(libbpfjit_alu_div10000_k,tc)379*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div10000_k, tc)
38011be35a1SLionel Sambuc {
38111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
38211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=10000");
38311be35a1SLionel Sambuc }
38411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div10000_k,tc)385*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div10000_k, tc)
38611be35a1SLionel Sambuc {
38711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
38811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
38911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, 10000),
39011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
39111be35a1SLionel Sambuc 	};
39211be35a1SLionel Sambuc 
39384d9c625SLionel Sambuc 	bpfjit_func_t code;
39411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
39511be35a1SLionel Sambuc 
39611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
39711be35a1SLionel Sambuc 
39811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
39911be35a1SLionel Sambuc 
400*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
40111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
40211be35a1SLionel Sambuc 
403*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(429484));
40411be35a1SLionel Sambuc 
40511be35a1SLionel Sambuc 	bpfjit_free_code(code);
40611be35a1SLionel Sambuc }
40711be35a1SLionel Sambuc 
408*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div7609801_k);
ATF_TC_HEAD(libbpfjit_alu_div7609801_k,tc)409*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div7609801_k, tc)
41011be35a1SLionel Sambuc {
41111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
41211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=7609801");
41311be35a1SLionel Sambuc }
41411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div7609801_k,tc)415*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div7609801_k, tc)
41611be35a1SLionel Sambuc {
41711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
41811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294967295)),
41911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, UINT32_C(7609801)),
42011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
42111be35a1SLionel Sambuc 	};
42211be35a1SLionel Sambuc 
42384d9c625SLionel Sambuc 	bpfjit_func_t code;
42411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
42511be35a1SLionel Sambuc 
42611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
42711be35a1SLionel Sambuc 
42811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
42911be35a1SLionel Sambuc 
430*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
43111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
43211be35a1SLionel Sambuc 
433*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 564);
43411be35a1SLionel Sambuc 
43511be35a1SLionel Sambuc 	bpfjit_free_code(code);
43611be35a1SLionel Sambuc }
43711be35a1SLionel Sambuc 
438*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div80000000_k);
ATF_TC_HEAD(libbpfjit_alu_div80000000_k,tc)439*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div80000000_k, tc)
44011be35a1SLionel Sambuc {
44111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
44211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_K with k=0x80000000");
44311be35a1SLionel Sambuc }
44411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div80000000_k,tc)445*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div80000000_k, tc)
44611be35a1SLionel Sambuc {
44711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
44811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffde)),
44911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, UINT32_C(0x80000000)),
45011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
45111be35a1SLionel Sambuc 	};
45211be35a1SLionel Sambuc 
45384d9c625SLionel Sambuc 	bpfjit_func_t code;
45411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
45511be35a1SLionel Sambuc 
45611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
45711be35a1SLionel Sambuc 
45811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
45911be35a1SLionel Sambuc 
460*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
46111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
46211be35a1SLionel Sambuc 
463*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
46411be35a1SLionel Sambuc 
46511be35a1SLionel Sambuc 	bpfjit_free_code(code);
46611be35a1SLionel Sambuc }
46711be35a1SLionel Sambuc 
468*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod0_k);
ATF_TC_HEAD(libbpfjit_alu_mod0_k,tc)469*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod0_k, tc)
470*0a6a1f1dSLionel Sambuc {
471*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
472*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=0");
473*0a6a1f1dSLionel Sambuc }
474*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod0_k,tc)475*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod0_k, tc)
476*0a6a1f1dSLionel Sambuc {
477*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
478*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 0),
479*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
480*0a6a1f1dSLionel Sambuc 	};
481*0a6a1f1dSLionel Sambuc 
482*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
483*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
484*0a6a1f1dSLionel Sambuc 
485*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
486*0a6a1f1dSLionel Sambuc 
487*0a6a1f1dSLionel Sambuc 	//ATF_CHECK(bpf_validate(insns, insn_count));
488*0a6a1f1dSLionel Sambuc 
489*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
490*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
491*0a6a1f1dSLionel Sambuc 
492*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
493*0a6a1f1dSLionel Sambuc 
494*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
495*0a6a1f1dSLionel Sambuc }
496*0a6a1f1dSLionel Sambuc 
497*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod1_k);
ATF_TC_HEAD(libbpfjit_alu_mod1_k,tc)498*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod1_k, tc)
499*0a6a1f1dSLionel Sambuc {
500*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
501*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=1");
502*0a6a1f1dSLionel Sambuc }
503*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod1_k,tc)504*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod1_k, tc)
505*0a6a1f1dSLionel Sambuc {
506*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
507*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 7),
508*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 1),
509*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
510*0a6a1f1dSLionel Sambuc 	};
511*0a6a1f1dSLionel Sambuc 
512*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
513*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
514*0a6a1f1dSLionel Sambuc 
515*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
516*0a6a1f1dSLionel Sambuc 
517*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
518*0a6a1f1dSLionel Sambuc 
519*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
520*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
521*0a6a1f1dSLionel Sambuc 
522*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
523*0a6a1f1dSLionel Sambuc 
524*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
525*0a6a1f1dSLionel Sambuc }
526*0a6a1f1dSLionel Sambuc 
527*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod2_k);
ATF_TC_HEAD(libbpfjit_alu_mod2_k,tc)528*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod2_k, tc)
529*0a6a1f1dSLionel Sambuc {
530*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
531*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=2");
532*0a6a1f1dSLionel Sambuc }
533*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod2_k,tc)534*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod2_k, tc)
535*0a6a1f1dSLionel Sambuc {
536*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
537*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 7),
538*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 2),
539*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
540*0a6a1f1dSLionel Sambuc 	};
541*0a6a1f1dSLionel Sambuc 
542*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
543*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
544*0a6a1f1dSLionel Sambuc 
545*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
546*0a6a1f1dSLionel Sambuc 
547*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
548*0a6a1f1dSLionel Sambuc 
549*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
550*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
551*0a6a1f1dSLionel Sambuc 
552*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
553*0a6a1f1dSLionel Sambuc 
554*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
555*0a6a1f1dSLionel Sambuc }
556*0a6a1f1dSLionel Sambuc 
557*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod4_k);
ATF_TC_HEAD(libbpfjit_alu_mod4_k,tc)558*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod4_k, tc)
559*0a6a1f1dSLionel Sambuc {
560*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
561*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=4");
562*0a6a1f1dSLionel Sambuc }
563*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod4_k,tc)564*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod4_k, tc)
565*0a6a1f1dSLionel Sambuc {
566*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
567*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
568*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 4),
569*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
570*0a6a1f1dSLionel Sambuc 	};
571*0a6a1f1dSLionel Sambuc 
572*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
573*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
574*0a6a1f1dSLionel Sambuc 
575*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
576*0a6a1f1dSLionel Sambuc 
577*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
578*0a6a1f1dSLionel Sambuc 
579*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
580*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
581*0a6a1f1dSLionel Sambuc 
582*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 3);
583*0a6a1f1dSLionel Sambuc 
584*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
585*0a6a1f1dSLionel Sambuc }
586*0a6a1f1dSLionel Sambuc 
587*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod10_k);
ATF_TC_HEAD(libbpfjit_alu_mod10_k,tc)588*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod10_k, tc)
589*0a6a1f1dSLionel Sambuc {
590*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
591*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=10");
592*0a6a1f1dSLionel Sambuc }
593*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod10_k,tc)594*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod10_k, tc)
595*0a6a1f1dSLionel Sambuc {
596*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
597*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
598*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 10),
599*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
600*0a6a1f1dSLionel Sambuc 	};
601*0a6a1f1dSLionel Sambuc 
602*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
603*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
604*0a6a1f1dSLionel Sambuc 
605*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
606*0a6a1f1dSLionel Sambuc 
607*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
608*0a6a1f1dSLionel Sambuc 
609*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
610*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
611*0a6a1f1dSLionel Sambuc 
612*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 9);
613*0a6a1f1dSLionel Sambuc 
614*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
615*0a6a1f1dSLionel Sambuc }
616*0a6a1f1dSLionel Sambuc 
617*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod10000_k);
ATF_TC_HEAD(libbpfjit_alu_mod10000_k,tc)618*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod10000_k, tc)
619*0a6a1f1dSLionel Sambuc {
620*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
621*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=10000");
622*0a6a1f1dSLionel Sambuc }
623*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod10000_k,tc)624*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod10000_k, tc)
625*0a6a1f1dSLionel Sambuc {
626*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
627*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
628*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, 10000),
629*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
630*0a6a1f1dSLionel Sambuc 	};
631*0a6a1f1dSLionel Sambuc 
632*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
633*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
634*0a6a1f1dSLionel Sambuc 
635*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
636*0a6a1f1dSLionel Sambuc 
637*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
638*0a6a1f1dSLionel Sambuc 
639*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
640*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
641*0a6a1f1dSLionel Sambuc 
642*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 3849);
643*0a6a1f1dSLionel Sambuc 
644*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
645*0a6a1f1dSLionel Sambuc }
646*0a6a1f1dSLionel Sambuc 
647*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod7609801_k);
ATF_TC_HEAD(libbpfjit_alu_mod7609801_k,tc)648*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod7609801_k, tc)
649*0a6a1f1dSLionel Sambuc {
650*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
651*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_mod+BPF_K with k=7609801");
652*0a6a1f1dSLionel Sambuc }
653*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod7609801_k,tc)654*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod7609801_k, tc)
655*0a6a1f1dSLionel Sambuc {
656*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
657*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294967295)),
658*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, UINT32_C(7609801)),
659*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
660*0a6a1f1dSLionel Sambuc 	};
661*0a6a1f1dSLionel Sambuc 
662*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
663*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
664*0a6a1f1dSLionel Sambuc 
665*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
666*0a6a1f1dSLionel Sambuc 
667*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
668*0a6a1f1dSLionel Sambuc 
669*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
670*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
671*0a6a1f1dSLionel Sambuc 
672*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(3039531));
673*0a6a1f1dSLionel Sambuc 
674*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
675*0a6a1f1dSLionel Sambuc }
676*0a6a1f1dSLionel Sambuc 
677*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod80000000_k);
ATF_TC_HEAD(libbpfjit_alu_mod80000000_k,tc)678*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod80000000_k, tc)
679*0a6a1f1dSLionel Sambuc {
680*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
681*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_K with k=0x80000000");
682*0a6a1f1dSLionel Sambuc }
683*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod80000000_k,tc)684*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod80000000_k, tc)
685*0a6a1f1dSLionel Sambuc {
686*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
687*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffde)),
688*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_K, UINT32_C(0x80000000)),
689*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
690*0a6a1f1dSLionel Sambuc 	};
691*0a6a1f1dSLionel Sambuc 
692*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
693*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
694*0a6a1f1dSLionel Sambuc 
695*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
696*0a6a1f1dSLionel Sambuc 
697*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
698*0a6a1f1dSLionel Sambuc 
699*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
700*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
701*0a6a1f1dSLionel Sambuc 
702*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0x7fffffde));
703*0a6a1f1dSLionel Sambuc 
704*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
705*0a6a1f1dSLionel Sambuc }
706*0a6a1f1dSLionel Sambuc 
707*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_and_k);
ATF_TC_HEAD(libbpfjit_alu_and_k,tc)708*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_and_k, tc)
70911be35a1SLionel Sambuc {
71011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
71111be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_AND+BPF_K");
71211be35a1SLionel Sambuc }
71311be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_and_k,tc)714*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_and_k, tc)
71511be35a1SLionel Sambuc {
71611be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
71711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdead),
71811be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_AND+BPF_K, 0xbeef),
71911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
72011be35a1SLionel Sambuc 	};
72111be35a1SLionel Sambuc 
72284d9c625SLionel Sambuc 	bpfjit_func_t code;
72311be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
72411be35a1SLionel Sambuc 
72511be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
72611be35a1SLionel Sambuc 
72711be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
72811be35a1SLionel Sambuc 
729*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
73011be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
73111be35a1SLionel Sambuc 
732*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == (0xdead&0xbeef));
73311be35a1SLionel Sambuc 
73411be35a1SLionel Sambuc 	bpfjit_free_code(code);
73511be35a1SLionel Sambuc }
73611be35a1SLionel Sambuc 
737*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_or_k);
ATF_TC_HEAD(libbpfjit_alu_or_k,tc)738*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_or_k, tc)
73911be35a1SLionel Sambuc {
74011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
74111be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_OR+BPF_K");
74211be35a1SLionel Sambuc }
74311be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_or_k,tc)744*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_or_k, tc)
74511be35a1SLionel Sambuc {
74611be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
74711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdead0000),
74811be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_OR+BPF_K, 0x0000beef),
74911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
75011be35a1SLionel Sambuc 	};
75111be35a1SLionel Sambuc 
75284d9c625SLionel Sambuc 	bpfjit_func_t code;
75311be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
75411be35a1SLionel Sambuc 
75511be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
75611be35a1SLionel Sambuc 
75711be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
75811be35a1SLionel Sambuc 
759*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
76011be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
76111be35a1SLionel Sambuc 
762*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xdeadbeef);
76311be35a1SLionel Sambuc 
76411be35a1SLionel Sambuc 	bpfjit_free_code(code);
76511be35a1SLionel Sambuc }
76611be35a1SLionel Sambuc 
767*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_xor_k);
ATF_TC_HEAD(libbpfjit_alu_xor_k,tc)768*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_xor_k, tc)
769*0a6a1f1dSLionel Sambuc {
770*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
771*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_XOR+BPF_K");
772*0a6a1f1dSLionel Sambuc }
773*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_xor_k,tc)774*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_xor_k, tc)
775*0a6a1f1dSLionel Sambuc {
776*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
777*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdead0f0f),
778*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_XOR+BPF_K, 0x0000b1e0),
779*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
780*0a6a1f1dSLionel Sambuc 	};
781*0a6a1f1dSLionel Sambuc 
782*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
783*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
784*0a6a1f1dSLionel Sambuc 
785*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
786*0a6a1f1dSLionel Sambuc 
787*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
788*0a6a1f1dSLionel Sambuc 
789*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
790*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
791*0a6a1f1dSLionel Sambuc 
792*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xdeadbeef);
793*0a6a1f1dSLionel Sambuc 
794*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
795*0a6a1f1dSLionel Sambuc }
796*0a6a1f1dSLionel Sambuc 
797*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_lsh_k);
ATF_TC_HEAD(libbpfjit_alu_lsh_k,tc)798*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_lsh_k, tc)
79911be35a1SLionel Sambuc {
80011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
80111be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_LSH+BPF_K");
80211be35a1SLionel Sambuc }
80311be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_lsh_k,tc)804*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_lsh_k, tc)
80511be35a1SLionel Sambuc {
80611be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
80711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
80811be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_LSH+BPF_K, 16),
80911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
81011be35a1SLionel Sambuc 	};
81111be35a1SLionel Sambuc 
81284d9c625SLionel Sambuc 	bpfjit_func_t code;
81311be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
81411be35a1SLionel Sambuc 
81511be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
81611be35a1SLionel Sambuc 
81711be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
81811be35a1SLionel Sambuc 
819*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
82011be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
82111be35a1SLionel Sambuc 
822*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xbeef0000);
82311be35a1SLionel Sambuc 
82411be35a1SLionel Sambuc 	bpfjit_free_code(code);
82511be35a1SLionel Sambuc }
82611be35a1SLionel Sambuc 
827*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_lsh0_k);
ATF_TC_HEAD(libbpfjit_alu_lsh0_k,tc)828*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_lsh0_k, tc)
82911be35a1SLionel Sambuc {
83011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
83111be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_LSH+BPF_K with k=0");
83211be35a1SLionel Sambuc }
83311be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_lsh0_k,tc)834*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_lsh0_k, tc)
83511be35a1SLionel Sambuc {
83611be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
83711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
83811be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_LSH+BPF_K, 0),
83911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
84011be35a1SLionel Sambuc 	};
84111be35a1SLionel Sambuc 
84284d9c625SLionel Sambuc 	bpfjit_func_t code;
84311be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
84411be35a1SLionel Sambuc 
84511be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
84611be35a1SLionel Sambuc 
84711be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
84811be35a1SLionel Sambuc 
849*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
85011be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
85111be35a1SLionel Sambuc 
852*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xdeadbeef);
85311be35a1SLionel Sambuc 
85411be35a1SLionel Sambuc 	bpfjit_free_code(code);
85511be35a1SLionel Sambuc }
85611be35a1SLionel Sambuc 
857*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_rsh_k);
ATF_TC_HEAD(libbpfjit_alu_rsh_k,tc)858*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_rsh_k, tc)
85911be35a1SLionel Sambuc {
86011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
86111be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_RSH+BPF_K");
86211be35a1SLionel Sambuc }
86311be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_rsh_k,tc)864*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_rsh_k, tc)
86511be35a1SLionel Sambuc {
86611be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
86711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
86811be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_RSH+BPF_K, 16),
86911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
87011be35a1SLionel Sambuc 	};
87111be35a1SLionel Sambuc 
87284d9c625SLionel Sambuc 	bpfjit_func_t code;
87311be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
87411be35a1SLionel Sambuc 
87511be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
87611be35a1SLionel Sambuc 
87711be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
87811be35a1SLionel Sambuc 
879*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
88011be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
88111be35a1SLionel Sambuc 
882*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0x0000dead);
88311be35a1SLionel Sambuc 
88411be35a1SLionel Sambuc 	bpfjit_free_code(code);
88511be35a1SLionel Sambuc }
88611be35a1SLionel Sambuc 
887*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_rsh0_k);
ATF_TC_HEAD(libbpfjit_alu_rsh0_k,tc)888*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_rsh0_k, tc)
88911be35a1SLionel Sambuc {
89011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
89111be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_RSH+BPF_K with k=0");
89211be35a1SLionel Sambuc }
89311be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_rsh0_k,tc)894*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_rsh0_k, tc)
89511be35a1SLionel Sambuc {
89611be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
89711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
89811be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_RSH+BPF_K, 0),
89911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
90011be35a1SLionel Sambuc 	};
90111be35a1SLionel Sambuc 
90284d9c625SLionel Sambuc 	bpfjit_func_t code;
90311be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
90411be35a1SLionel Sambuc 
90511be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
90611be35a1SLionel Sambuc 
90711be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
90811be35a1SLionel Sambuc 
909*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
91011be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
91111be35a1SLionel Sambuc 
912*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xdeadbeef);
91311be35a1SLionel Sambuc 
91411be35a1SLionel Sambuc 	bpfjit_free_code(code);
91511be35a1SLionel Sambuc }
91611be35a1SLionel Sambuc 
917*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_modulo_k);
ATF_TC_HEAD(libbpfjit_alu_modulo_k,tc)918*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_modulo_k, tc)
91911be35a1SLionel Sambuc {
92011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
92111be35a1SLionel Sambuc 	    "Test JIT compilation of modulo logic of BPF_ALU+BPF_K operations");
92211be35a1SLionel Sambuc }
92311be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_modulo_k,tc)924*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_modulo_k, tc)
92511be35a1SLionel Sambuc {
92611be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
92711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0x7fffff77)),
92811be35a1SLionel Sambuc 
92911be35a1SLionel Sambuc 		/* (7FFFFF77 * 0FFFFF77) = 07FFFFB2,F0004951 */
93011be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MUL+BPF_K, UINT32_C(0x0fffff77)),
93111be35a1SLionel Sambuc 
93211be35a1SLionel Sambuc 		/* 07FFFFB2,F0004951 << 1 = 0FFFFF65,E00092A2 */
93311be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_LSH+BPF_K, 1),
93411be35a1SLionel Sambuc 
93511be35a1SLionel Sambuc 		/* 0FFFFF65,E00092A2 + DDDDDDDD = 0FFFFF66,BDDE707F */
93611be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, UINT32_C(0xdddddddd)),
93711be35a1SLionel Sambuc 
93811be35a1SLionel Sambuc 		/* 0FFFFF66,BDDE707F - FFFFFFFF = 0FFFFF65,BDDE7080 */
93911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_SUB+BPF_K, UINT32_C(0xffffffff)),
94011be35a1SLionel Sambuc 
94111be35a1SLionel Sambuc 		/* 0FFFFF65,BDDE7080 | 0000030C = 0FFFFF65,BDDE738C */
94211be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_OR+BPF_K, UINT32_C(0x0000030c)),
94311be35a1SLionel Sambuc 
94411be35a1SLionel Sambuc 		/* -0FFFFF65,BDDE738C mod(2^64) = F000009A,42218C74 */
94511be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_NEG, 0),
94611be35a1SLionel Sambuc 
94711be35a1SLionel Sambuc 		/* F000009A,42218C74 & FFFFFF0F = F000009A,42218C04 */
94811be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_AND+BPF_K, UINT32_C(0xffffff0f)),
94911be35a1SLionel Sambuc 
95011be35a1SLionel Sambuc 		/* F000009A,42218C74 >> 3 = 1E000013,48443180 */
95111be35a1SLionel Sambuc 		/* 00000000,42218C74 >> 3 = 00000000,08443180 */
95211be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_RSH+BPF_K, 3),
95311be35a1SLionel Sambuc 
95411be35a1SLionel Sambuc 		/* 00000000,08443180 * 7FFFFF77 = 042218BB,93818280 */
95511be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MUL+BPF_K, UINT32_C(0x7fffff77)),
95611be35a1SLionel Sambuc 
95711be35a1SLionel Sambuc 		/* 042218BB,93818280 / DEAD = 000004C0,71CBBBC3 */
95811be35a1SLionel Sambuc 		/* 00000000,93818280 / DEAD = 00000000,0000A994 */
95911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_K, UINT32_C(0xdead)),
96011be35a1SLionel Sambuc 
96111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
96211be35a1SLionel Sambuc 	};
96311be35a1SLionel Sambuc 
96484d9c625SLionel Sambuc 	bpfjit_func_t code;
96511be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
96611be35a1SLionel Sambuc 
96711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
96811be35a1SLionel Sambuc 
96911be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
97011be35a1SLionel Sambuc 
971*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
97211be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
97311be35a1SLionel Sambuc 
974*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) != UINT32_C(0x71cbbbc3));
975*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0x0000a994));
97611be35a1SLionel Sambuc 
97711be35a1SLionel Sambuc 
97811be35a1SLionel Sambuc 	bpfjit_free_code(code);
97911be35a1SLionel Sambuc }
98011be35a1SLionel Sambuc 
981*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_add_x);
ATF_TC_HEAD(libbpfjit_alu_add_x,tc)982*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_add_x, tc)
98311be35a1SLionel Sambuc {
98411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
98511be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_ADD+BPF_X");
98611be35a1SLionel Sambuc }
98711be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_add_x,tc)988*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_add_x, tc)
98911be35a1SLionel Sambuc {
99011be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
99111be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 3),
99211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
99311be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
99411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
99511be35a1SLionel Sambuc 	};
99611be35a1SLionel Sambuc 
99784d9c625SLionel Sambuc 	bpfjit_func_t code;
99811be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
99911be35a1SLionel Sambuc 
100011be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
100111be35a1SLionel Sambuc 
100211be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
100311be35a1SLionel Sambuc 
1004*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
100511be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
100611be35a1SLionel Sambuc 
1007*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 5);
100811be35a1SLionel Sambuc 
100911be35a1SLionel Sambuc 	bpfjit_free_code(code);
101011be35a1SLionel Sambuc }
101111be35a1SLionel Sambuc 
1012*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_sub_x);
ATF_TC_HEAD(libbpfjit_alu_sub_x,tc)1013*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_sub_x, tc)
101411be35a1SLionel Sambuc {
101511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
101611be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_SUB+BPF_X");
101711be35a1SLionel Sambuc }
101811be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_sub_x,tc)1019*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_sub_x, tc)
102011be35a1SLionel Sambuc {
102111be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
102211be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 1),
102311be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
102411be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_SUB+BPF_X, 0),
102511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
102611be35a1SLionel Sambuc 	};
102711be35a1SLionel Sambuc 
102884d9c625SLionel Sambuc 	bpfjit_func_t code;
102911be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
103011be35a1SLionel Sambuc 
103111be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
103211be35a1SLionel Sambuc 
103311be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
103411be35a1SLionel Sambuc 
1035*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
103611be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
103711be35a1SLionel Sambuc 
1038*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_MAX);
103911be35a1SLionel Sambuc 
104011be35a1SLionel Sambuc 	bpfjit_free_code(code);
104111be35a1SLionel Sambuc }
104211be35a1SLionel Sambuc 
1043*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mul_x);
ATF_TC_HEAD(libbpfjit_alu_mul_x,tc)1044*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mul_x, tc)
104511be35a1SLionel Sambuc {
104611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
104711be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MUL+BPF_X");
104811be35a1SLionel Sambuc }
104911be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mul_x,tc)1050*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mul_x, tc)
105111be35a1SLionel Sambuc {
105211be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
105311be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
105411be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
105511be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0),
105611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
105711be35a1SLionel Sambuc 	};
105811be35a1SLionel Sambuc 
105984d9c625SLionel Sambuc 	bpfjit_func_t code;
106011be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
106111be35a1SLionel Sambuc 
106211be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
106311be35a1SLionel Sambuc 
106411be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
106511be35a1SLionel Sambuc 
1066*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
106711be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
106811be35a1SLionel Sambuc 
1069*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0xfffffffd));
107011be35a1SLionel Sambuc 
107111be35a1SLionel Sambuc 	bpfjit_free_code(code);
107211be35a1SLionel Sambuc }
107311be35a1SLionel Sambuc 
1074*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div0_x);
ATF_TC_HEAD(libbpfjit_alu_div0_x,tc)1075*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div0_x, tc)
107611be35a1SLionel Sambuc {
107711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
107811be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=0");
107911be35a1SLionel Sambuc }
108011be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div0_x,tc)1081*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div0_x, tc)
108211be35a1SLionel Sambuc {
108311be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
108411be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
108511be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
108611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
108711be35a1SLionel Sambuc 	};
108811be35a1SLionel Sambuc 
108984d9c625SLionel Sambuc 	bpfjit_func_t code;
109011be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
109111be35a1SLionel Sambuc 
109211be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
109311be35a1SLionel Sambuc 
109411be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
109511be35a1SLionel Sambuc 
1096*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
109711be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
109811be35a1SLionel Sambuc 
1099*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
110011be35a1SLionel Sambuc 
110111be35a1SLionel Sambuc 	bpfjit_free_code(code);
110211be35a1SLionel Sambuc }
110311be35a1SLionel Sambuc 
1104*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div1_x);
ATF_TC_HEAD(libbpfjit_alu_div1_x,tc)1105*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div1_x, tc)
110611be35a1SLionel Sambuc {
110711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
110811be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=1");
110911be35a1SLionel Sambuc }
111011be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div1_x,tc)1111*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div1_x, tc)
111211be35a1SLionel Sambuc {
111311be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
111411be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 7),
111511be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1),
111611be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
111711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
111811be35a1SLionel Sambuc 	};
111911be35a1SLionel Sambuc 
112084d9c625SLionel Sambuc 	bpfjit_func_t code;
112111be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
112211be35a1SLionel Sambuc 
112311be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
112411be35a1SLionel Sambuc 
112511be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
112611be35a1SLionel Sambuc 
1127*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
112811be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
112911be35a1SLionel Sambuc 
1130*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 7);
113111be35a1SLionel Sambuc 
113211be35a1SLionel Sambuc 	bpfjit_free_code(code);
113311be35a1SLionel Sambuc }
113411be35a1SLionel Sambuc 
1135*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div2_x);
ATF_TC_HEAD(libbpfjit_alu_div2_x,tc)1136*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div2_x, tc)
113711be35a1SLionel Sambuc {
113811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
113911be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=2");
114011be35a1SLionel Sambuc }
114111be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div2_x,tc)1142*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div2_x, tc)
114311be35a1SLionel Sambuc {
114411be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
114511be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 7),
114611be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
114711be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
114811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
114911be35a1SLionel Sambuc 	};
115011be35a1SLionel Sambuc 
115184d9c625SLionel Sambuc 	bpfjit_func_t code;
115211be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
115311be35a1SLionel Sambuc 
115411be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
115511be35a1SLionel Sambuc 
115611be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
115711be35a1SLionel Sambuc 
1158*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
115911be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
116011be35a1SLionel Sambuc 
1161*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 3);
116211be35a1SLionel Sambuc 
116311be35a1SLionel Sambuc 	bpfjit_free_code(code);
116411be35a1SLionel Sambuc }
116511be35a1SLionel Sambuc 
1166*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div4_x);
ATF_TC_HEAD(libbpfjit_alu_div4_x,tc)1167*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div4_x, tc)
116811be35a1SLionel Sambuc {
116911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
117011be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=4");
117111be35a1SLionel Sambuc }
117211be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div4_x,tc)1173*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div4_x, tc)
117411be35a1SLionel Sambuc {
117511be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
117611be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
117711be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 4),
117811be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
117911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
118011be35a1SLionel Sambuc 	};
118111be35a1SLionel Sambuc 
118284d9c625SLionel Sambuc 	bpfjit_func_t code;
118311be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
118411be35a1SLionel Sambuc 
118511be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
118611be35a1SLionel Sambuc 
118711be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
118811be35a1SLionel Sambuc 
1189*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
119011be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
119111be35a1SLionel Sambuc 
1192*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0x3fffffff));
119311be35a1SLionel Sambuc 
119411be35a1SLionel Sambuc 	bpfjit_free_code(code);
119511be35a1SLionel Sambuc }
119611be35a1SLionel Sambuc 
1197*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div10_x);
ATF_TC_HEAD(libbpfjit_alu_div10_x,tc)1198*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div10_x, tc)
119911be35a1SLionel Sambuc {
120011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
120111be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=10");
120211be35a1SLionel Sambuc }
120311be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div10_x,tc)1204*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div10_x, tc)
120511be35a1SLionel Sambuc {
120611be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
120711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
120811be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 10),
120911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
121011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
121111be35a1SLionel Sambuc 	};
121211be35a1SLionel Sambuc 
121384d9c625SLionel Sambuc 	bpfjit_func_t code;
121411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
121511be35a1SLionel Sambuc 
121611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
121711be35a1SLionel Sambuc 
121811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
121911be35a1SLionel Sambuc 
1220*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
122111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
122211be35a1SLionel Sambuc 
1223*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(429484384));
122411be35a1SLionel Sambuc 
122511be35a1SLionel Sambuc 	bpfjit_free_code(code);
122611be35a1SLionel Sambuc }
122711be35a1SLionel Sambuc 
1228*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div10000_x);
ATF_TC_HEAD(libbpfjit_alu_div10000_x,tc)1229*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div10000_x, tc)
123011be35a1SLionel Sambuc {
123111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
123211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=10000");
123311be35a1SLionel Sambuc }
123411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div10000_x,tc)1235*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div10000_x, tc)
123611be35a1SLionel Sambuc {
123711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
123811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
123911be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 10000),
124011be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
124111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
124211be35a1SLionel Sambuc 	};
124311be35a1SLionel Sambuc 
124484d9c625SLionel Sambuc 	bpfjit_func_t code;
124511be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
124611be35a1SLionel Sambuc 
124711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
124811be35a1SLionel Sambuc 
124911be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
125011be35a1SLionel Sambuc 
1251*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
125211be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
125311be35a1SLionel Sambuc 
1254*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(429484));
125511be35a1SLionel Sambuc 
125611be35a1SLionel Sambuc 	bpfjit_free_code(code);
125711be35a1SLionel Sambuc }
125811be35a1SLionel Sambuc 
1259*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div7609801_x);
ATF_TC_HEAD(libbpfjit_alu_div7609801_x,tc)1260*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div7609801_x, tc)
126111be35a1SLionel Sambuc {
126211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
126311be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=7609801");
126411be35a1SLionel Sambuc }
126511be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div7609801_x,tc)1266*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div7609801_x, tc)
126711be35a1SLionel Sambuc {
126811be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
126911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294967295)),
127011be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(7609801)),
127111be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
127211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
127311be35a1SLionel Sambuc 	};
127411be35a1SLionel Sambuc 
127584d9c625SLionel Sambuc 	bpfjit_func_t code;
127611be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
127711be35a1SLionel Sambuc 
127811be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
127911be35a1SLionel Sambuc 
128011be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
128111be35a1SLionel Sambuc 
1282*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
128311be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
128411be35a1SLionel Sambuc 
1285*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 564);
128611be35a1SLionel Sambuc 
128711be35a1SLionel Sambuc 	bpfjit_free_code(code);
128811be35a1SLionel Sambuc }
128911be35a1SLionel Sambuc 
1290*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_div80000000_x);
ATF_TC_HEAD(libbpfjit_alu_div80000000_x,tc)1291*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_div80000000_x, tc)
129211be35a1SLionel Sambuc {
129311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
129411be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_DIV+BPF_X with X=0x80000000");
129511be35a1SLionel Sambuc }
129611be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_div80000000_x,tc)1297*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_div80000000_x, tc)
129811be35a1SLionel Sambuc {
129911be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
1300*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffde)),
130111be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0x80000000)),
130211be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
130311be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
130411be35a1SLionel Sambuc 	};
130511be35a1SLionel Sambuc 
130684d9c625SLionel Sambuc 	bpfjit_func_t code;
130711be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
130811be35a1SLionel Sambuc 
130911be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
131011be35a1SLionel Sambuc 
131111be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
131211be35a1SLionel Sambuc 
1313*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
131411be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
131511be35a1SLionel Sambuc 
1316*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
131711be35a1SLionel Sambuc 
131811be35a1SLionel Sambuc 	bpfjit_free_code(code);
131911be35a1SLionel Sambuc }
132011be35a1SLionel Sambuc 
1321*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod0_x);
ATF_TC_HEAD(libbpfjit_alu_mod0_x,tc)1322*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod0_x, tc)
1323*0a6a1f1dSLionel Sambuc {
1324*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
1325*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=0");
1326*0a6a1f1dSLionel Sambuc }
1327*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod0_x,tc)1328*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod0_x, tc)
1329*0a6a1f1dSLionel Sambuc {
1330*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
1331*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
1332*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1333*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
1334*0a6a1f1dSLionel Sambuc 	};
1335*0a6a1f1dSLionel Sambuc 
1336*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
1337*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
1338*0a6a1f1dSLionel Sambuc 
1339*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1340*0a6a1f1dSLionel Sambuc 
1341*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
1342*0a6a1f1dSLionel Sambuc 
1343*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
1344*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
1345*0a6a1f1dSLionel Sambuc 
1346*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
1347*0a6a1f1dSLionel Sambuc 
1348*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
1349*0a6a1f1dSLionel Sambuc }
1350*0a6a1f1dSLionel Sambuc 
1351*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod1_x);
ATF_TC_HEAD(libbpfjit_alu_mod1_x,tc)1352*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod1_x, tc)
1353*0a6a1f1dSLionel Sambuc {
1354*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
1355*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=1");
1356*0a6a1f1dSLionel Sambuc }
1357*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod1_x,tc)1358*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod1_x, tc)
1359*0a6a1f1dSLionel Sambuc {
1360*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
1361*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 7),
1362*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1),
1363*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1364*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
1365*0a6a1f1dSLionel Sambuc 	};
1366*0a6a1f1dSLionel Sambuc 
1367*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
1368*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
1369*0a6a1f1dSLionel Sambuc 
1370*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1371*0a6a1f1dSLionel Sambuc 
1372*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
1373*0a6a1f1dSLionel Sambuc 
1374*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
1375*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
1376*0a6a1f1dSLionel Sambuc 
1377*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
1378*0a6a1f1dSLionel Sambuc 
1379*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
1380*0a6a1f1dSLionel Sambuc }
1381*0a6a1f1dSLionel Sambuc 
1382*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod2_x);
ATF_TC_HEAD(libbpfjit_alu_mod2_x,tc)1383*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod2_x, tc)
1384*0a6a1f1dSLionel Sambuc {
1385*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
1386*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=2");
1387*0a6a1f1dSLionel Sambuc }
1388*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod2_x,tc)1389*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod2_x, tc)
1390*0a6a1f1dSLionel Sambuc {
1391*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
1392*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 7),
1393*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
1394*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1395*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
1396*0a6a1f1dSLionel Sambuc 	};
1397*0a6a1f1dSLionel Sambuc 
1398*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
1399*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
1400*0a6a1f1dSLionel Sambuc 
1401*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1402*0a6a1f1dSLionel Sambuc 
1403*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
1404*0a6a1f1dSLionel Sambuc 
1405*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
1406*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
1407*0a6a1f1dSLionel Sambuc 
1408*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
1409*0a6a1f1dSLionel Sambuc 
1410*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
1411*0a6a1f1dSLionel Sambuc }
1412*0a6a1f1dSLionel Sambuc 
1413*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod4_x);
ATF_TC_HEAD(libbpfjit_alu_mod4_x,tc)1414*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod4_x, tc)
1415*0a6a1f1dSLionel Sambuc {
1416*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
1417*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=4");
1418*0a6a1f1dSLionel Sambuc }
1419*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod4_x,tc)1420*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod4_x, tc)
1421*0a6a1f1dSLionel Sambuc {
1422*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
1423*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffff)),
1424*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 4),
1425*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1426*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
1427*0a6a1f1dSLionel Sambuc 	};
1428*0a6a1f1dSLionel Sambuc 
1429*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
1430*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
1431*0a6a1f1dSLionel Sambuc 
1432*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1433*0a6a1f1dSLionel Sambuc 
1434*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
1435*0a6a1f1dSLionel Sambuc 
1436*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
1437*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
1438*0a6a1f1dSLionel Sambuc 
1439*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 3);
1440*0a6a1f1dSLionel Sambuc 
1441*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
1442*0a6a1f1dSLionel Sambuc }
1443*0a6a1f1dSLionel Sambuc 
1444*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod10_x);
ATF_TC_HEAD(libbpfjit_alu_mod10_x,tc)1445*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod10_x, tc)
1446*0a6a1f1dSLionel Sambuc {
1447*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
1448*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=10");
1449*0a6a1f1dSLionel Sambuc }
1450*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod10_x,tc)1451*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod10_x, tc)
1452*0a6a1f1dSLionel Sambuc {
1453*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
1454*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
1455*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 10),
1456*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1457*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
1458*0a6a1f1dSLionel Sambuc 	};
1459*0a6a1f1dSLionel Sambuc 
1460*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
1461*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
1462*0a6a1f1dSLionel Sambuc 
1463*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1464*0a6a1f1dSLionel Sambuc 
1465*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
1466*0a6a1f1dSLionel Sambuc 
1467*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
1468*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
1469*0a6a1f1dSLionel Sambuc 
1470*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 9);
1471*0a6a1f1dSLionel Sambuc 
1472*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
1473*0a6a1f1dSLionel Sambuc }
1474*0a6a1f1dSLionel Sambuc 
1475*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod10000_x);
ATF_TC_HEAD(libbpfjit_alu_mod10000_x,tc)1476*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod10000_x, tc)
1477*0a6a1f1dSLionel Sambuc {
1478*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
1479*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=10000");
1480*0a6a1f1dSLionel Sambuc }
1481*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod10000_x,tc)1482*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod10000_x, tc)
1483*0a6a1f1dSLionel Sambuc {
1484*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
1485*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294843849)),
1486*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 10000),
1487*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1488*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
1489*0a6a1f1dSLionel Sambuc 	};
1490*0a6a1f1dSLionel Sambuc 
1491*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
1492*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
1493*0a6a1f1dSLionel Sambuc 
1494*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1495*0a6a1f1dSLionel Sambuc 
1496*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
1497*0a6a1f1dSLionel Sambuc 
1498*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
1499*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
1500*0a6a1f1dSLionel Sambuc 
1501*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 3849);
1502*0a6a1f1dSLionel Sambuc 
1503*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
1504*0a6a1f1dSLionel Sambuc }
1505*0a6a1f1dSLionel Sambuc 
1506*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod7609801_x);
ATF_TC_HEAD(libbpfjit_alu_mod7609801_x,tc)1507*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod7609801_x, tc)
1508*0a6a1f1dSLionel Sambuc {
1509*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
1510*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=7609801");
1511*0a6a1f1dSLionel Sambuc }
1512*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod7609801_x,tc)1513*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod7609801_x, tc)
1514*0a6a1f1dSLionel Sambuc {
1515*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
1516*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(4294967295)),
1517*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(7609801)),
1518*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1519*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
1520*0a6a1f1dSLionel Sambuc 	};
1521*0a6a1f1dSLionel Sambuc 
1522*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
1523*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
1524*0a6a1f1dSLionel Sambuc 
1525*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1526*0a6a1f1dSLionel Sambuc 
1527*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
1528*0a6a1f1dSLionel Sambuc 
1529*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
1530*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
1531*0a6a1f1dSLionel Sambuc 
1532*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(3039531));
1533*0a6a1f1dSLionel Sambuc 
1534*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
1535*0a6a1f1dSLionel Sambuc }
1536*0a6a1f1dSLionel Sambuc 
1537*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_mod80000000_x);
ATF_TC_HEAD(libbpfjit_alu_mod80000000_x,tc)1538*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_mod80000000_x, tc)
1539*0a6a1f1dSLionel Sambuc {
1540*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
1541*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_MOD+BPF_X with X=0x80000000");
1542*0a6a1f1dSLionel Sambuc }
1543*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_mod80000000_x,tc)1544*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_mod80000000_x, tc)
1545*0a6a1f1dSLionel Sambuc {
1546*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
1547*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0xffffffde)),
1548*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0x80000000)),
1549*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MOD+BPF_X, 0),
1550*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
1551*0a6a1f1dSLionel Sambuc 	};
1552*0a6a1f1dSLionel Sambuc 
1553*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
1554*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
1555*0a6a1f1dSLionel Sambuc 
1556*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1557*0a6a1f1dSLionel Sambuc 
1558*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
1559*0a6a1f1dSLionel Sambuc 
1560*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
1561*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
1562*0a6a1f1dSLionel Sambuc 
1563*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0x7fffffde));
1564*0a6a1f1dSLionel Sambuc 
1565*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
1566*0a6a1f1dSLionel Sambuc }
1567*0a6a1f1dSLionel Sambuc 
1568*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_and_x);
ATF_TC_HEAD(libbpfjit_alu_and_x,tc)1569*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_and_x, tc)
157011be35a1SLionel Sambuc {
157111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
157211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_AND+BPF_X");
157311be35a1SLionel Sambuc }
157411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_and_x,tc)1575*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_and_x, tc)
157611be35a1SLionel Sambuc {
157711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
157811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdead),
157911be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0xbeef),
158011be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_AND+BPF_X, 0),
158111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
158211be35a1SLionel Sambuc 	};
158311be35a1SLionel Sambuc 
158484d9c625SLionel Sambuc 	bpfjit_func_t code;
158511be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
158611be35a1SLionel Sambuc 
158711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
158811be35a1SLionel Sambuc 
158911be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
159011be35a1SLionel Sambuc 
1591*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
159211be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
159311be35a1SLionel Sambuc 
1594*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == (0xdead&0xbeef));
159511be35a1SLionel Sambuc 
159611be35a1SLionel Sambuc 	bpfjit_free_code(code);
159711be35a1SLionel Sambuc }
159811be35a1SLionel Sambuc 
1599*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_or_x);
ATF_TC_HEAD(libbpfjit_alu_or_x,tc)1600*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_or_x, tc)
160111be35a1SLionel Sambuc {
160211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
160311be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_OR+BPF_X");
160411be35a1SLionel Sambuc }
160511be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_or_x,tc)1606*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_or_x, tc)
160711be35a1SLionel Sambuc {
160811be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
160911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdead0000),
161011be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0x0000beef),
161111be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_OR+BPF_X, 0),
161211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
161311be35a1SLionel Sambuc 	};
161411be35a1SLionel Sambuc 
161584d9c625SLionel Sambuc 	bpfjit_func_t code;
161611be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
161711be35a1SLionel Sambuc 
161811be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
161911be35a1SLionel Sambuc 
162011be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
162111be35a1SLionel Sambuc 
1622*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
162311be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
162411be35a1SLionel Sambuc 
1625*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xdeadbeef);
162611be35a1SLionel Sambuc 
162711be35a1SLionel Sambuc 	bpfjit_free_code(code);
162811be35a1SLionel Sambuc }
162911be35a1SLionel Sambuc 
1630*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_xor_x);
ATF_TC_HEAD(libbpfjit_alu_xor_x,tc)1631*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_xor_x, tc)
1632*0a6a1f1dSLionel Sambuc {
1633*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
1634*0a6a1f1dSLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_XOR+BPF_X");
1635*0a6a1f1dSLionel Sambuc }
1636*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_xor_x,tc)1637*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_xor_x, tc)
1638*0a6a1f1dSLionel Sambuc {
1639*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
1640*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdead0f0f),
1641*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0x0000b1e0),
1642*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_XOR+BPF_X, 0),
1643*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
1644*0a6a1f1dSLionel Sambuc 	};
1645*0a6a1f1dSLionel Sambuc 
1646*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
1647*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
1648*0a6a1f1dSLionel Sambuc 
1649*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1650*0a6a1f1dSLionel Sambuc 
1651*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
1652*0a6a1f1dSLionel Sambuc 
1653*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
1654*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
1655*0a6a1f1dSLionel Sambuc 
1656*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xdeadbeef);
1657*0a6a1f1dSLionel Sambuc 
1658*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
1659*0a6a1f1dSLionel Sambuc }
1660*0a6a1f1dSLionel Sambuc 
1661*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_lsh_x);
ATF_TC_HEAD(libbpfjit_alu_lsh_x,tc)1662*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_lsh_x, tc)
166311be35a1SLionel Sambuc {
166411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
166511be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_LSH+BPF_X");
166611be35a1SLionel Sambuc }
166711be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_lsh_x,tc)1668*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_lsh_x, tc)
166911be35a1SLionel Sambuc {
167011be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
167111be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
167211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 16),
167311be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_LSH+BPF_X, 0),
167411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
167511be35a1SLionel Sambuc 	};
167611be35a1SLionel Sambuc 
167784d9c625SLionel Sambuc 	bpfjit_func_t code;
167811be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
167911be35a1SLionel Sambuc 
168011be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
168111be35a1SLionel Sambuc 
168211be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
168311be35a1SLionel Sambuc 
1684*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
168511be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
168611be35a1SLionel Sambuc 
1687*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xbeef0000);
168811be35a1SLionel Sambuc 
168911be35a1SLionel Sambuc 	bpfjit_free_code(code);
169011be35a1SLionel Sambuc }
169111be35a1SLionel Sambuc 
1692*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_lsh0_x);
ATF_TC_HEAD(libbpfjit_alu_lsh0_x,tc)1693*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_lsh0_x, tc)
169411be35a1SLionel Sambuc {
169511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
169611be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_LSH+BPF_X with k=0");
169711be35a1SLionel Sambuc }
169811be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_lsh0_x,tc)1699*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_lsh0_x, tc)
170011be35a1SLionel Sambuc {
170111be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
170211be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
170311be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
170411be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_LSH+BPF_X, 0),
170511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
170611be35a1SLionel Sambuc 	};
170711be35a1SLionel Sambuc 
170884d9c625SLionel Sambuc 	bpfjit_func_t code;
170911be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
171011be35a1SLionel Sambuc 
171111be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
171211be35a1SLionel Sambuc 
171311be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
171411be35a1SLionel Sambuc 
1715*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
171611be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
171711be35a1SLionel Sambuc 
1718*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xdeadbeef);
171911be35a1SLionel Sambuc 
172011be35a1SLionel Sambuc 	bpfjit_free_code(code);
172111be35a1SLionel Sambuc }
172211be35a1SLionel Sambuc 
1723*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_rsh_x);
ATF_TC_HEAD(libbpfjit_alu_rsh_x,tc)1724*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_rsh_x, tc)
172511be35a1SLionel Sambuc {
172611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
172711be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_RSH+BPF_X");
172811be35a1SLionel Sambuc }
172911be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_rsh_x,tc)1730*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_rsh_x, tc)
173111be35a1SLionel Sambuc {
173211be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
173311be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
173411be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 16),
173511be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_RSH+BPF_X, 0),
173611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
173711be35a1SLionel Sambuc 	};
173811be35a1SLionel Sambuc 
173984d9c625SLionel Sambuc 	bpfjit_func_t code;
174011be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
174111be35a1SLionel Sambuc 
174211be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
174311be35a1SLionel Sambuc 
174411be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
174511be35a1SLionel Sambuc 
1746*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
174711be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
174811be35a1SLionel Sambuc 
1749*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0x0000dead);
175011be35a1SLionel Sambuc 
175111be35a1SLionel Sambuc 	bpfjit_free_code(code);
175211be35a1SLionel Sambuc }
175311be35a1SLionel Sambuc 
1754*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_rsh0_x);
ATF_TC_HEAD(libbpfjit_alu_rsh0_x,tc)1755*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_rsh0_x, tc)
175611be35a1SLionel Sambuc {
175711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
175811be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_RSH+BPF_X with k=0");
175911be35a1SLionel Sambuc }
176011be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_rsh0_x,tc)1761*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_rsh0_x, tc)
176211be35a1SLionel Sambuc {
176311be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
176411be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 0xdeadbeef),
176511be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
176611be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_RSH+BPF_X, 0),
176711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
176811be35a1SLionel Sambuc 	};
176911be35a1SLionel Sambuc 
177084d9c625SLionel Sambuc 	bpfjit_func_t code;
177111be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
177211be35a1SLionel Sambuc 
177311be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
177411be35a1SLionel Sambuc 
177511be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
177611be35a1SLionel Sambuc 
1777*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
177811be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
177911be35a1SLionel Sambuc 
1780*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0xdeadbeef);
178111be35a1SLionel Sambuc 
178211be35a1SLionel Sambuc 	bpfjit_free_code(code);
178311be35a1SLionel Sambuc }
178411be35a1SLionel Sambuc 
1785*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_modulo_x);
ATF_TC_HEAD(libbpfjit_alu_modulo_x,tc)1786*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_modulo_x, tc)
178711be35a1SLionel Sambuc {
178811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
178911be35a1SLionel Sambuc 	    "Test JIT compilation of modulo logic of BPF_ALU+BPF_X operations");
179011be35a1SLionel Sambuc }
179111be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_modulo_x,tc)1792*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_modulo_x, tc)
179311be35a1SLionel Sambuc {
179411be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
179511be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0x7fffff77)),
179611be35a1SLionel Sambuc 
179711be35a1SLionel Sambuc 		/* (7FFFFF77 * 0FFFFF77) = 07FFFFB2,F0004951 */
179811be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0x0fffff77)),
179911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0),
180011be35a1SLionel Sambuc 
180111be35a1SLionel Sambuc 		/* 07FFFFB2,F0004951 << 1 = 0FFFFF65,E00092A2 */
180211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_K, 1),
180311be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_LSH+BPF_X, 0),
180411be35a1SLionel Sambuc 
180511be35a1SLionel Sambuc 		/* 0FFFFF65,E00092A2 + DDDDDDDD = 0FFFFF66,BDDE707F */
180611be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0xdddddddd)),
180711be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
180811be35a1SLionel Sambuc 
180911be35a1SLionel Sambuc 		/* 0FFFFF66,BDDE707F - FFFFFFFF = 0FFFFF65,BDDE7080 */
181011be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0xffffffff)),
181111be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_SUB+BPF_X, 0),
181211be35a1SLionel Sambuc 
181311be35a1SLionel Sambuc 		/* 0FFFFF65,BDDE7080 | 0000030C = 0FFFFF65,BDDE738C */
181411be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0x0000030c)),
181511be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_OR+BPF_X, 0),
181611be35a1SLionel Sambuc 
181711be35a1SLionel Sambuc 		/* -0FFFFF65,BDDE738C mod(2^64) = F000009A,42218C74 */
181811be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_NEG, 0),
181911be35a1SLionel Sambuc 
182011be35a1SLionel Sambuc 		/* F000009A,42218C74 & FFFFFF0F = F000009A,42218C04 */
182111be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0xffffff0f)),
182211be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_AND+BPF_X, 0),
182311be35a1SLionel Sambuc 
182411be35a1SLionel Sambuc 		/* F000009A,42218C74 >> 3 = 1E000013,48443180 */
182511be35a1SLionel Sambuc 		/* 00000000,42218C74 >> 3 = 00000000,08443180 */
182611be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_K, 3),
182711be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_RSH+BPF_X, 0),
182811be35a1SLionel Sambuc 
182911be35a1SLionel Sambuc 		/* 00000000,08443180 * 7FFFFF77 = 042218BB,93818280 */
183011be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0x7fffff77)),
183111be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_MUL+BPF_X, 0),
183211be35a1SLionel Sambuc 
183311be35a1SLionel Sambuc 		/* 042218BB,93818280 / DEAD = 000004C0,71CBBBC3 */
183411be35a1SLionel Sambuc 		/* 00000000,93818280 / DEAD = 00000000,0000A994 */
183511be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_K, UINT32_C(0xdead)),
183611be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_DIV+BPF_X, 0),
183711be35a1SLionel Sambuc 
183811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
183911be35a1SLionel Sambuc 	};
184011be35a1SLionel Sambuc 
184184d9c625SLionel Sambuc 	bpfjit_func_t code;
184211be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
184311be35a1SLionel Sambuc 
184411be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
184511be35a1SLionel Sambuc 
184611be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
184711be35a1SLionel Sambuc 
1848*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
184911be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
185011be35a1SLionel Sambuc 
1851*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) != UINT32_C(0x71cbbbc3));
1852*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_C(0x0000a994));
185311be35a1SLionel Sambuc 
185411be35a1SLionel Sambuc 
185511be35a1SLionel Sambuc 	bpfjit_free_code(code);
185611be35a1SLionel Sambuc }
185711be35a1SLionel Sambuc 
1858*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_alu_neg);
ATF_TC_HEAD(libbpfjit_alu_neg,tc)1859*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_alu_neg, tc)
186011be35a1SLionel Sambuc {
186111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
186211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ALU+BPF_NEG");
186311be35a1SLionel Sambuc }
186411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_alu_neg,tc)1865*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_alu_neg, tc)
186611be35a1SLionel Sambuc {
186711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
186811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 777),
186911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_NEG, 0),
187011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
187111be35a1SLionel Sambuc 	};
187211be35a1SLionel Sambuc 
187384d9c625SLionel Sambuc 	bpfjit_func_t code;
187411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
187511be35a1SLionel Sambuc 
187611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
187711be35a1SLionel Sambuc 
187811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
187911be35a1SLionel Sambuc 
1880*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
188111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
188211be35a1SLionel Sambuc 
1883*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0u-777u);
188411be35a1SLionel Sambuc 
188511be35a1SLionel Sambuc 	bpfjit_free_code(code);
188611be35a1SLionel Sambuc }
188711be35a1SLionel Sambuc 
1888*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_ja);
ATF_TC_HEAD(libbpfjit_jmp_ja,tc)1889*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_ja, tc)
189011be35a1SLionel Sambuc {
189111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
189211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_JMP+BPF_JA");
189311be35a1SLionel Sambuc }
189411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_ja,tc)1895*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_ja, tc)
189611be35a1SLionel Sambuc {
189711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
189811be35a1SLionel Sambuc 		BPF_STMT(BPF_JMP+BPF_JA, 1),
189911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
190011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
190111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
190211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
190311be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
190411be35a1SLionel Sambuc 	};
190511be35a1SLionel Sambuc 
190684d9c625SLionel Sambuc 	bpfjit_func_t code;
190711be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
190811be35a1SLionel Sambuc 
190911be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
191011be35a1SLionel Sambuc 
191111be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
191211be35a1SLionel Sambuc 
1913*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
191411be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
191511be35a1SLionel Sambuc 
1916*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_MAX);
191711be35a1SLionel Sambuc 
191811be35a1SLionel Sambuc 	bpfjit_free_code(code);
191911be35a1SLionel Sambuc }
192011be35a1SLionel Sambuc 
1921*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_ja_invalid);
ATF_TC_HEAD(libbpfjit_jmp_ja_invalid,tc)1922*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_ja_invalid, tc)
1923*0a6a1f1dSLionel Sambuc {
1924*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
1925*0a6a1f1dSLionel Sambuc 	    "Test BPF_JMP+BPF_JA to invalid destination");
1926*0a6a1f1dSLionel Sambuc }
1927*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_ja_invalid,tc)1928*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_ja_invalid, tc)
1929*0a6a1f1dSLionel Sambuc {
1930*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
1931*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_JMP+BPF_JA, 4),
1932*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
1933*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
1934*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
1935*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
1936*0a6a1f1dSLionel Sambuc 	};
1937*0a6a1f1dSLionel Sambuc 
1938*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1939*0a6a1f1dSLionel Sambuc 
1940*0a6a1f1dSLionel Sambuc 	ATF_CHECK(!bpf_validate(insns, insn_count));
1941*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpfjit_generate_code(NULL, insns, insn_count) == NULL);
1942*0a6a1f1dSLionel Sambuc }
1943*0a6a1f1dSLionel Sambuc 
1944*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_ja_overflow);
ATF_TC_HEAD(libbpfjit_jmp_ja_overflow,tc)1945*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_ja_overflow, tc)
1946*0a6a1f1dSLionel Sambuc {
1947*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
1948*0a6a1f1dSLionel Sambuc 	    "Test BPF_JMP+BPF_JA with negative offset");
1949*0a6a1f1dSLionel Sambuc }
1950*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_ja_overflow,tc)1951*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_ja_overflow, tc)
1952*0a6a1f1dSLionel Sambuc {
1953*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
1954*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_JMP+BPF_JA, 1),
1955*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 777),
1956*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_JMP+BPF_JA, UINT32_MAX - 1), // -2
1957*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0)
1958*0a6a1f1dSLionel Sambuc 	};
1959*0a6a1f1dSLionel Sambuc 
1960*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
1961*0a6a1f1dSLionel Sambuc 
1962*0a6a1f1dSLionel Sambuc 	/* Jumps with negative offsets work in userspace ... */
1963*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
1964*0a6a1f1dSLionel Sambuc 
1965*0a6a1f1dSLionel Sambuc 	/* .. but not for bpfjit. */
1966*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpfjit_generate_code(NULL, insns, insn_count) == NULL);
1967*0a6a1f1dSLionel Sambuc }
1968*0a6a1f1dSLionel Sambuc 
1969*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_jgt_k);
ATF_TC_HEAD(libbpfjit_jmp_jgt_k,tc)1970*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_jgt_k, tc)
197111be35a1SLionel Sambuc {
197211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
197311be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_JMP+BPF_JGT+BPF_K");
197411be35a1SLionel Sambuc }
197511be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_jgt_k,tc)1976*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_jgt_k, tc)
197711be35a1SLionel Sambuc {
197811be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
197911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
198011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 7, 0, 1),
198111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
198211be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 2, 2, 0),
198311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 9, 0, 0),
198411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
198511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 4, 1, 1),
198611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
198711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 6, 2, 3),
198811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
198911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 4),
199011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 5),
199111be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 5, 3, 1),
199211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 6),
199311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 0, 0, 0),
199411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7),
199511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 8)
199611be35a1SLionel Sambuc 	};
199711be35a1SLionel Sambuc 
199884d9c625SLionel Sambuc 	bpfjit_func_t code;
199911be35a1SLionel Sambuc 	uint8_t pkt[8]; /* the program doesn't read any data */
200011be35a1SLionel Sambuc 
200111be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
200211be35a1SLionel Sambuc 
200311be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
200411be35a1SLionel Sambuc 
2005*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
200611be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
200711be35a1SLionel Sambuc 
2008*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
2009*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
2010*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 7);
2011*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
2012*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 7);
2013*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
2014*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
2015*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
201611be35a1SLionel Sambuc 
201711be35a1SLionel Sambuc 	bpfjit_free_code(code);
201811be35a1SLionel Sambuc }
201911be35a1SLionel Sambuc 
2020*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_jge_k);
ATF_TC_HEAD(libbpfjit_jmp_jge_k,tc)2021*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_jge_k, tc)
202211be35a1SLionel Sambuc {
202311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
202411be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_JMP+BPF_JGE+BPF_K");
202511be35a1SLionel Sambuc }
202611be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_jge_k,tc)2027*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_jge_k, tc)
202811be35a1SLionel Sambuc {
202911be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
203011be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
203111be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 8, 0, 1),
203211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
203311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 3, 2, 0),
203411be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 9, 0, 0),
203511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
203611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 5, 1, 1),
203711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
203811be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 7, 2, 3),
203911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
204011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 4),
204111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 5),
204211be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 6, 3, 1),
204311be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 6),
204411be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 1, 0, 0),
204511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7),
204611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 8)
204711be35a1SLionel Sambuc 	};
204811be35a1SLionel Sambuc 
204984d9c625SLionel Sambuc 	bpfjit_func_t code;
205011be35a1SLionel Sambuc 	uint8_t pkt[8]; /* the program doesn't read any data */
205111be35a1SLionel Sambuc 
205211be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
205311be35a1SLionel Sambuc 
205411be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
205511be35a1SLionel Sambuc 
2056*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
205711be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
205811be35a1SLionel Sambuc 
2059*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
2060*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
2061*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 7);
2062*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
2063*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 7);
2064*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
2065*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
2066*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
206711be35a1SLionel Sambuc 
206811be35a1SLionel Sambuc 	bpfjit_free_code(code);
206911be35a1SLionel Sambuc }
207011be35a1SLionel Sambuc 
2071*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_jeq_k);
ATF_TC_HEAD(libbpfjit_jmp_jeq_k,tc)2072*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_jeq_k, tc)
207311be35a1SLionel Sambuc {
207411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
207511be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_JMP+BPF_JEQ+BPF_K");
207611be35a1SLionel Sambuc }
207711be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_jeq_k,tc)2078*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_jeq_k, tc)
207911be35a1SLionel Sambuc {
208011be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
208111be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
208211be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 8, 0, 1),
208311be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
208411be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 3, 1, 0),
208511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 9, 1, 1),
208611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
208711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 5, 1, 1),
208811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
208911be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 7, 2, 3),
209011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
209111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 4),
209211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 5),
209311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 6, 3, 1),
209411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 6),
209511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 1, 0, 0),
209611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7),
209711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 8)
209811be35a1SLionel Sambuc 	};
209911be35a1SLionel Sambuc 
210084d9c625SLionel Sambuc 	bpfjit_func_t code;
210111be35a1SLionel Sambuc 	uint8_t pkt[8]; /* the program doesn't read any data */
210211be35a1SLionel Sambuc 
210311be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
210411be35a1SLionel Sambuc 
210511be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
210611be35a1SLionel Sambuc 
2107*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
210811be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
210911be35a1SLionel Sambuc 
2110*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 7);
2111*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 7);
2112*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 1);
2113*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
2114*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 7);
2115*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
2116*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
2117*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
211811be35a1SLionel Sambuc 
211911be35a1SLionel Sambuc 	bpfjit_free_code(code);
212011be35a1SLionel Sambuc }
212111be35a1SLionel Sambuc 
2122*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_jset_k);
ATF_TC_HEAD(libbpfjit_jmp_jset_k,tc)2123*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_jset_k, tc)
212411be35a1SLionel Sambuc {
212511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
212611be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_JMP+BPF_JSET+BPF_K");
212711be35a1SLionel Sambuc }
212811be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_jset_k,tc)2129*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_jset_k, tc)
213011be35a1SLionel Sambuc {
213111be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
213211be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
213311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 8, 0, 1),
213411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
213511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 4, 2, 0),
213611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 3, 0, 0),
213711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
213811be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 2, 1, 1),
213911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
214011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 1, 2, 3),
214111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
214211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 4),
214311be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 5),
214411be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 2, 3, 1),
214511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 6),
214611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 7, 0, 0),
214711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7),
214811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 8)
214911be35a1SLionel Sambuc 	};
215011be35a1SLionel Sambuc 
215184d9c625SLionel Sambuc 	bpfjit_func_t code;
215211be35a1SLionel Sambuc 	uint8_t pkt[8]; /* the program doesn't read any data */
215311be35a1SLionel Sambuc 
215411be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
215511be35a1SLionel Sambuc 
215611be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
215711be35a1SLionel Sambuc 
2158*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
215911be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
216011be35a1SLionel Sambuc 
2161*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
2162*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
2163*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 1);
2164*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
2165*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 5);
2166*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
2167*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
2168*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
216911be35a1SLionel Sambuc 
217011be35a1SLionel Sambuc 	bpfjit_free_code(code);
217111be35a1SLionel Sambuc }
217211be35a1SLionel Sambuc 
2173*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_modulo_k);
ATF_TC_HEAD(libbpfjit_jmp_modulo_k,tc)2174*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_modulo_k, tc)
217511be35a1SLionel Sambuc {
217611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
217711be35a1SLionel Sambuc 	    "Test JIT compilation of modulo logic of BPF_JMP+BPF_K operations");
217811be35a1SLionel Sambuc }
217911be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_modulo_k,tc)2180*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_modulo_k, tc)
218111be35a1SLionel Sambuc {
218211be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
218311be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0x7fffff77)),
218411be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_LSH+BPF_K, 4),
218511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, UINT32_C(0xfffff770), 1, 0),
218611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
218711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, UINT32_C(0xfffff770), 0, 1),
218811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
218911be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, UINT32_C(0xfffff771), 0, 1),
219011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
219111be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, UINT32_C(0xfffff770), 0, 3),
219211be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, UINT32_C(0xfffff770), 2, 0),
219311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, UINT32_C(0xfffff771), 1, 0),
219411be35a1SLionel Sambuc 		BPF_STMT(BPF_JMP+BPF_JA, 1),
219511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
219611be35a1SLionel Sambuc 
219711be35a1SLionel Sambuc 		/* FFFFF770+FFFFF770 = 00000001,FFFFEEE0 */
219811be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, UINT32_C(0xfffff770)),
219911be35a1SLionel Sambuc 
220011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, UINT32_C(0xffffeee0), 1, 0),
220111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 4),
220211be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, UINT32_C(0xffffeee0), 0, 1),
220311be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 5),
220411be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, UINT32_C(0xffffeee1), 0, 1),
220511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 6),
220611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, UINT32_C(0xffffeee0), 0, 3),
220711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, UINT32_C(0xffffeee0), 2, 0),
220811be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, UINT32_C(0xffffeee1), 1, 0),
220911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
221011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7)
221111be35a1SLionel Sambuc 	};
221211be35a1SLionel Sambuc 
221384d9c625SLionel Sambuc 	bpfjit_func_t code;
221411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
221511be35a1SLionel Sambuc 
221611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
221711be35a1SLionel Sambuc 
221811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
221911be35a1SLionel Sambuc 
2220*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
222111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
222211be35a1SLionel Sambuc 
2223*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_MAX);
222411be35a1SLionel Sambuc 
222511be35a1SLionel Sambuc 	bpfjit_free_code(code);
222611be35a1SLionel Sambuc }
222711be35a1SLionel Sambuc 
2228*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_jgt_x);
ATF_TC_HEAD(libbpfjit_jmp_jgt_x,tc)2229*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_jgt_x, tc)
223011be35a1SLionel Sambuc {
223111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
223211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_JMP+BPF_JGT+BPF_X");
223311be35a1SLionel Sambuc }
223411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_jgt_x,tc)2235*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_jgt_x, tc)
223611be35a1SLionel Sambuc {
223711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
223811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
223911be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 7),
224011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 0, 1),
224111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
224211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
224311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 3, 0),
224411be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 9),
224511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 0, 0),
224611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
224711be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 4),
224811be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 1, 1),
224911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
225011be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 6),
225111be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 2, 3),
225211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
225311be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 4),
225411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 5),
225511be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
225611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 4, 1),
225711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 6),
225811be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
225911be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 0, 0),
226011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7),
226111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 8)
226211be35a1SLionel Sambuc 	};
226311be35a1SLionel Sambuc 
226484d9c625SLionel Sambuc 	bpfjit_func_t code;
226511be35a1SLionel Sambuc 	uint8_t pkt[8]; /* the program doesn't read any data */
226611be35a1SLionel Sambuc 
226711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
226811be35a1SLionel Sambuc 
226911be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
227011be35a1SLionel Sambuc 
2271*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
227211be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
227311be35a1SLionel Sambuc 
2274*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
2275*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
2276*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 7);
2277*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
2278*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 7);
2279*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
2280*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
2281*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
228211be35a1SLionel Sambuc 
228311be35a1SLionel Sambuc 	bpfjit_free_code(code);
228411be35a1SLionel Sambuc }
228511be35a1SLionel Sambuc 
2286*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_jge_x);
ATF_TC_HEAD(libbpfjit_jmp_jge_x,tc)2287*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_jge_x, tc)
228811be35a1SLionel Sambuc {
228911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
229011be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_JMP+BPF_JGE+BPF_X");
229111be35a1SLionel Sambuc }
229211be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_jge_x,tc)2293*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_jge_x, tc)
229411be35a1SLionel Sambuc {
229511be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
229611be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
229711be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 8),
229811be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 0, 1),
229911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
230011be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
230111be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 3, 0),
230211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 9),
230311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 0, 0),
230411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
230511be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
230611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 1, 1),
230711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
230811be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 7),
230911be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 2, 3),
231011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
231111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 4),
231211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 5),
231311be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 6),
231411be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 4, 1),
231511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 6),
231611be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1),
231711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 0, 0),
231811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7),
231911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 8)
232011be35a1SLionel Sambuc 	};
232111be35a1SLionel Sambuc 
232284d9c625SLionel Sambuc 	bpfjit_func_t code;
232311be35a1SLionel Sambuc 	uint8_t pkt[8]; /* the program doesn't read any data */
232411be35a1SLionel Sambuc 
232511be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
232611be35a1SLionel Sambuc 
232711be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
232811be35a1SLionel Sambuc 
2329*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
233011be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
233111be35a1SLionel Sambuc 
2332*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
2333*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
2334*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 7);
2335*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
2336*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 7);
2337*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
2338*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
2339*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
234011be35a1SLionel Sambuc 
234111be35a1SLionel Sambuc 	bpfjit_free_code(code);
234211be35a1SLionel Sambuc }
234311be35a1SLionel Sambuc 
2344*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_jeq_x);
ATF_TC_HEAD(libbpfjit_jmp_jeq_x,tc)2345*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_jeq_x, tc)
234611be35a1SLionel Sambuc {
234711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
234811be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_JMP+BPF_JEQ+BPF_X");
234911be35a1SLionel Sambuc }
235011be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_jeq_x,tc)2351*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_jeq_x, tc)
235211be35a1SLionel Sambuc {
235311be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
235411be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
235511be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 8),
235611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1),
2357*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
235811be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
235911be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 2, 0),
236011be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 9),
236111be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 1),
236211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
2363*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
2364*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1),
2365*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
236611be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 7),
236711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 2, 3),
236811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 4),
236911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 5),
2370*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 6),
237111be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 6),
237211be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 3, 1),
237311be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7),
2374*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 0),
2375*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 8),
2376*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 9)
237711be35a1SLionel Sambuc 	};
237811be35a1SLionel Sambuc 
237984d9c625SLionel Sambuc 	bpfjit_func_t code;
238011be35a1SLionel Sambuc 	uint8_t pkt[8]; /* the program doesn't read any data */
238111be35a1SLionel Sambuc 
238211be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
238311be35a1SLionel Sambuc 
238411be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
238511be35a1SLionel Sambuc 
2386*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
238711be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
238811be35a1SLionel Sambuc 
2389*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 8);
2390*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 8);
2391*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 2);
2392*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 8);
2393*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 3);
2394*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 9);
2395*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 6);
2396*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 1);
239711be35a1SLionel Sambuc 
239811be35a1SLionel Sambuc 	bpfjit_free_code(code);
239911be35a1SLionel Sambuc }
240011be35a1SLionel Sambuc 
2401*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_jset_x);
ATF_TC_HEAD(libbpfjit_jmp_jset_x,tc)2402*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_jset_x, tc)
240311be35a1SLionel Sambuc {
240411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
240511be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_JMP+BPF_JSET+BPF_X");
240611be35a1SLionel Sambuc }
240711be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_jset_x,tc)2408*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_jset_x, tc)
240911be35a1SLionel Sambuc {
241011be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
241111be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
241211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 8),
241311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 0, 1),
241411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
241511be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 4),
241611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 2, 0),
241711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 3, 0, 0),
241811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
241911be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
242011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 1, 1),
242111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
242211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 1),
242311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 2, 3),
242411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
242511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 4),
242611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 5),
242711be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2),
242811be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 4, 1),
242911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 6),
243011be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 7),
243111be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_X, 0, 0, 0),
243211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7),
243311be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 8)
243411be35a1SLionel Sambuc 	};
243511be35a1SLionel Sambuc 
243684d9c625SLionel Sambuc 	bpfjit_func_t code;
243711be35a1SLionel Sambuc 	uint8_t pkt[8]; /* the program doesn't read any data */
243811be35a1SLionel Sambuc 
243911be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
244011be35a1SLionel Sambuc 
244111be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
244211be35a1SLionel Sambuc 
2443*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
244411be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
244511be35a1SLionel Sambuc 
2446*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
2447*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 1);
2448*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 1);
2449*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 7);
2450*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 5);
2451*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 8);
2452*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 5);
2453*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
245411be35a1SLionel Sambuc 
245511be35a1SLionel Sambuc 	bpfjit_free_code(code);
245611be35a1SLionel Sambuc }
245711be35a1SLionel Sambuc 
2458*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_jeq_x_noinit_ax);
ATF_TC_HEAD(libbpfjit_jmp_jeq_x_noinit_ax,tc)2459*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_jeq_x_noinit_ax, tc)
2460*0a6a1f1dSLionel Sambuc {
2461*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test JIT compilation "
2462*0a6a1f1dSLionel Sambuc 	    "of BPF_JMP+BPF_EQ+BPF_X with uninitialised A and X");
2463*0a6a1f1dSLionel Sambuc }
2464*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_jeq_x_noinit_ax,tc)2465*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_jeq_x_noinit_ax, tc)
2466*0a6a1f1dSLionel Sambuc {
2467*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
2468*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1),
2469*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 10),
2470*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 11)
2471*0a6a1f1dSLionel Sambuc 	};
2472*0a6a1f1dSLionel Sambuc 
2473*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
2474*0a6a1f1dSLionel Sambuc 	uint8_t pkt[8]; /* the program doesn't read any data */
2475*0a6a1f1dSLionel Sambuc 
2476*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2477*0a6a1f1dSLionel Sambuc 
2478*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
2479*0a6a1f1dSLionel Sambuc 
2480*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
2481*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
2482*0a6a1f1dSLionel Sambuc 
2483*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 10);
2484*0a6a1f1dSLionel Sambuc 
2485*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
2486*0a6a1f1dSLionel Sambuc }
2487*0a6a1f1dSLionel Sambuc 
2488*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_jeq_x_noinit_a);
ATF_TC_HEAD(libbpfjit_jmp_jeq_x_noinit_a,tc)2489*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_jeq_x_noinit_a, tc)
2490*0a6a1f1dSLionel Sambuc {
2491*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test JIT compilation "
2492*0a6a1f1dSLionel Sambuc 	    "of BPF_JMP+BPF_EQ+BPF_X with uninitialised A");
2493*0a6a1f1dSLionel Sambuc }
2494*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_jeq_x_noinit_a,tc)2495*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_jeq_x_noinit_a, tc)
2496*0a6a1f1dSLionel Sambuc {
2497*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
2498*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0), /* X > 0 */
2499*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1),
2500*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 10),
2501*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 11)
2502*0a6a1f1dSLionel Sambuc 	};
2503*0a6a1f1dSLionel Sambuc 
2504*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
2505*0a6a1f1dSLionel Sambuc 	uint8_t pkt[8]; /* the program doesn't read any data */
2506*0a6a1f1dSLionel Sambuc 
2507*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2508*0a6a1f1dSLionel Sambuc 
2509*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
2510*0a6a1f1dSLionel Sambuc 
2511*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
2512*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
2513*0a6a1f1dSLionel Sambuc 
2514*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 11);
2515*0a6a1f1dSLionel Sambuc 
2516*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
2517*0a6a1f1dSLionel Sambuc }
2518*0a6a1f1dSLionel Sambuc 
2519*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_jeq_x_noinit_x);
ATF_TC_HEAD(libbpfjit_jmp_jeq_x_noinit_x,tc)2520*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_jeq_x_noinit_x, tc)
2521*0a6a1f1dSLionel Sambuc {
2522*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test JIT compilation "
2523*0a6a1f1dSLionel Sambuc 	    "of BPF_JMP+BPF_EQ+BPF_X with uninitialised X");
2524*0a6a1f1dSLionel Sambuc }
2525*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_jeq_x_noinit_x,tc)2526*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_jeq_x_noinit_x, tc)
2527*0a6a1f1dSLionel Sambuc {
2528*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
2529*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_LEN, 0), /* A > 0 */
2530*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 1),
2531*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 10),
2532*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 11)
2533*0a6a1f1dSLionel Sambuc 	};
2534*0a6a1f1dSLionel Sambuc 
2535*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
2536*0a6a1f1dSLionel Sambuc 	uint8_t pkt[8]; /* the program doesn't read any data */
2537*0a6a1f1dSLionel Sambuc 
2538*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
2539*0a6a1f1dSLionel Sambuc 
2540*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
2541*0a6a1f1dSLionel Sambuc 
2542*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
2543*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
2544*0a6a1f1dSLionel Sambuc 
2545*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 11);
2546*0a6a1f1dSLionel Sambuc 
2547*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
2548*0a6a1f1dSLionel Sambuc }
2549*0a6a1f1dSLionel Sambuc 
2550*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_jmp_modulo_x);
ATF_TC_HEAD(libbpfjit_jmp_modulo_x,tc)2551*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_jmp_modulo_x, tc)
255211be35a1SLionel Sambuc {
255311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
255411be35a1SLionel Sambuc 	    "Test JIT compilation of modulo logic of BPF_JMP+BPF_X operations");
255511be35a1SLionel Sambuc }
255611be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_jmp_modulo_x,tc)2557*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_jmp_modulo_x, tc)
255811be35a1SLionel Sambuc {
255911be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
256011be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_C(0x7fffff77)),
256111be35a1SLionel Sambuc 		/* FFFFF770 << 4 = FFFFF770 */
256211be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_LSH+BPF_K, 4),
256311be35a1SLionel Sambuc 
256411be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xfffff770)),
256511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 0),
256611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
256711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 0, 1),
256811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
256911be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xfffff771)),
257011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 0, 1),
257111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
257211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xfffff770)),
257311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 4),
257411be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 3, 0),
257511be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xfffff771)),
257611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 1, 0),
257711be35a1SLionel Sambuc 		BPF_STMT(BPF_JMP+BPF_JA, 1),
257811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
257911be35a1SLionel Sambuc 
258011be35a1SLionel Sambuc 		/* FFFFF770+FFFFF770 = 00000001,FFFFEEE0 */
258111be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, UINT32_C(0xfffff770)),
258211be35a1SLionel Sambuc 
258311be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xffffeee0)),
258411be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 0),
258511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 4),
258611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 0, 1),
258711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 5),
258811be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xffffeee1)),
258911be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 0, 1),
259011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 6),
259111be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xffffeee0)),
259211be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 0, 4),
259311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_X, 0, 3, 0),
259411be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_C(0xffffeee1)),
259511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_X, 0, 1, 0),
259611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
259711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7)
259811be35a1SLionel Sambuc 	};
259911be35a1SLionel Sambuc 
260084d9c625SLionel Sambuc 	bpfjit_func_t code;
260111be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
260211be35a1SLionel Sambuc 
260311be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
260411be35a1SLionel Sambuc 
260511be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
260611be35a1SLionel Sambuc 
2607*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
260811be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
260911be35a1SLionel Sambuc 
2610*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_MAX);
261111be35a1SLionel Sambuc 
261211be35a1SLionel Sambuc 	bpfjit_free_code(code);
261311be35a1SLionel Sambuc }
261411be35a1SLionel Sambuc 
2615*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ld_abs);
ATF_TC_HEAD(libbpfjit_ld_abs,tc)2616*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ld_abs, tc)
261711be35a1SLionel Sambuc {
261811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
261911be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LD+BPF_ABS");
262011be35a1SLionel Sambuc }
262111be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ld_abs,tc)2622*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ld_abs, tc)
262311be35a1SLionel Sambuc {
262411be35a1SLionel Sambuc 	static struct bpf_insn insns[3][2] = {
262511be35a1SLionel Sambuc 		{
262611be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 5),
262711be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_A, 0)
262811be35a1SLionel Sambuc 		},
262911be35a1SLionel Sambuc 		{
263011be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 5),
263111be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_A, 0)
263211be35a1SLionel Sambuc 		},
263311be35a1SLionel Sambuc 		{
263411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 5),
263511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_A, 0)
263611be35a1SLionel Sambuc 		}
263711be35a1SLionel Sambuc 	};
263811be35a1SLionel Sambuc 
263911be35a1SLionel Sambuc 	static size_t lengths[3] = { 1, 2, 4 };
264011be35a1SLionel Sambuc 	static unsigned int expected[3] = { 0xde, 0xdead, 0xdeadbeef };
264111be35a1SLionel Sambuc 
264211be35a1SLionel Sambuc 	size_t i, l;
264311be35a1SLionel Sambuc 	uint8_t *pkt = deadbeef_at_5;
264411be35a1SLionel Sambuc 	size_t pktsize = sizeof(deadbeef_at_5);
264511be35a1SLionel Sambuc 
264611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns[0]) / sizeof(insns[0][0]);
264711be35a1SLionel Sambuc 
264811be35a1SLionel Sambuc 	for (i = 0; i < 3; i++) {
264984d9c625SLionel Sambuc 		bpfjit_func_t code;
265011be35a1SLionel Sambuc 
265111be35a1SLionel Sambuc 		ATF_CHECK(bpf_validate(insns[i], insn_count));
265211be35a1SLionel Sambuc 
2653*0a6a1f1dSLionel Sambuc 		code = bpfjit_generate_code(NULL, insns[i], insn_count);
265411be35a1SLionel Sambuc 		ATF_REQUIRE(code != NULL);
265511be35a1SLionel Sambuc 
2656*0a6a1f1dSLionel Sambuc 		for (l = 1; l < 5 + lengths[i]; l++) {
2657*0a6a1f1dSLionel Sambuc 			ATF_CHECK(jitcall(code, pkt, l, l) == 0);
2658*0a6a1f1dSLionel Sambuc 			ATF_CHECK(jitcall(code, pkt, pktsize, l) == 0);
265911be35a1SLionel Sambuc 		}
266011be35a1SLionel Sambuc 
266111be35a1SLionel Sambuc 		l = 5 + lengths[i];
2662*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, l, l) == expected[i]);
2663*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, pktsize, l) == expected[i]);
266411be35a1SLionel Sambuc 
266511be35a1SLionel Sambuc 		l = pktsize;
2666*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, l, l) == expected[i]);
266711be35a1SLionel Sambuc 
266811be35a1SLionel Sambuc 		bpfjit_free_code(code);
266911be35a1SLionel Sambuc 	}
267011be35a1SLionel Sambuc }
267111be35a1SLionel Sambuc 
2672*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ld_abs_k_overflow);
ATF_TC_HEAD(libbpfjit_ld_abs_k_overflow,tc)2673*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ld_abs_k_overflow, tc)
267411be35a1SLionel Sambuc {
267511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
267611be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LD+BPF_ABS with overflow in k+4");
267711be35a1SLionel Sambuc }
267811be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ld_abs_k_overflow,tc)2679*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ld_abs_k_overflow, tc)
268011be35a1SLionel Sambuc {
268111be35a1SLionel Sambuc 	static struct bpf_insn insns[12][3] = {
268211be35a1SLionel Sambuc 		{
268311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_ABS, UINT32_MAX),
268411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
268511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
268611be35a1SLionel Sambuc 		},
268711be35a1SLionel Sambuc 		{
268811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_ABS, UINT32_MAX - 1),
268911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
269011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
269111be35a1SLionel Sambuc 		},
269211be35a1SLionel Sambuc 		{
269311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX),
269411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
269511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
269611be35a1SLionel Sambuc 		},
269711be35a1SLionel Sambuc 		{
269811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 1),
269911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
270011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
270111be35a1SLionel Sambuc 		},
270211be35a1SLionel Sambuc 		{
270311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 2),
270411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
270511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
270611be35a1SLionel Sambuc 		},
270711be35a1SLionel Sambuc 		{
270811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 3),
270911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
271011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
271111be35a1SLionel Sambuc 		},
271211be35a1SLionel Sambuc 		{
271311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
271411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_ABS, UINT32_MAX),
271511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
271611be35a1SLionel Sambuc 		},
271711be35a1SLionel Sambuc 		{
271811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
271911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_ABS, UINT32_MAX - 1),
272011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
272111be35a1SLionel Sambuc 		},
272211be35a1SLionel Sambuc 		{
272311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
272411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX),
272511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
272611be35a1SLionel Sambuc 		},
272711be35a1SLionel Sambuc 		{
272811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
272911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 1),
273011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
273111be35a1SLionel Sambuc 		},
273211be35a1SLionel Sambuc 		{
273311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
273411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 2),
273511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
273611be35a1SLionel Sambuc 		},
273711be35a1SLionel Sambuc 		{
273811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
273911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_ABS, UINT32_MAX - 3),
274011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
274111be35a1SLionel Sambuc 		}
274211be35a1SLionel Sambuc 	};
274311be35a1SLionel Sambuc 
274411be35a1SLionel Sambuc 	int i;
274511be35a1SLionel Sambuc 	uint8_t pkt[8] = { 0 };
274611be35a1SLionel Sambuc 
274711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns[0]) / sizeof(insns[0][0]);
274811be35a1SLionel Sambuc 
274911be35a1SLionel Sambuc 	for (i = 0; i < 3; i++) {
275084d9c625SLionel Sambuc 		bpfjit_func_t code;
275111be35a1SLionel Sambuc 
275211be35a1SLionel Sambuc 		ATF_CHECK(bpf_validate(insns[i], insn_count));
275311be35a1SLionel Sambuc 
2754*0a6a1f1dSLionel Sambuc 		code = bpfjit_generate_code(NULL, insns[i], insn_count);
275511be35a1SLionel Sambuc 		ATF_REQUIRE(code != NULL);
275611be35a1SLionel Sambuc 
2757*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
275811be35a1SLionel Sambuc 
275911be35a1SLionel Sambuc 		bpfjit_free_code(code);
276011be35a1SLionel Sambuc 	}
276111be35a1SLionel Sambuc }
276211be35a1SLionel Sambuc 
2763*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ld_ind);
ATF_TC_HEAD(libbpfjit_ld_ind,tc)2764*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ld_ind, tc)
276511be35a1SLionel Sambuc {
276611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
276711be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LD+BPF_IND");
276811be35a1SLionel Sambuc }
276911be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ld_ind,tc)2770*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ld_ind, tc)
277111be35a1SLionel Sambuc {
277211be35a1SLionel Sambuc 	static struct bpf_insn insns[6][3] = {
277311be35a1SLionel Sambuc 		{
277411be35a1SLionel Sambuc 			BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
277511be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_IND, 2),
277611be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_A, 0)
277711be35a1SLionel Sambuc 		},
277811be35a1SLionel Sambuc 		{
277911be35a1SLionel Sambuc 			BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
278011be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 2),
278111be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_A, 0)
278211be35a1SLionel Sambuc 		},
278311be35a1SLionel Sambuc 		{
278411be35a1SLionel Sambuc 			BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 3),
278511be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_IND, 2),
278611be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_A, 0)
278711be35a1SLionel Sambuc 		},
278811be35a1SLionel Sambuc 		{
278911be35a1SLionel Sambuc 			BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
279011be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_B+BPF_IND, 0),
279111be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_A, 0)
279211be35a1SLionel Sambuc 		},
279311be35a1SLionel Sambuc 		{
279411be35a1SLionel Sambuc 			BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
279511be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 0),
279611be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_A, 0)
279711be35a1SLionel Sambuc 		},
279811be35a1SLionel Sambuc 		{
279911be35a1SLionel Sambuc 			BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
280011be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_IND, 0),
280111be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_A, 0)
280211be35a1SLionel Sambuc 		}
280311be35a1SLionel Sambuc 	};
280411be35a1SLionel Sambuc 
280511be35a1SLionel Sambuc 	static size_t lengths[6] = { 1, 2, 4, 1, 2, 4 };
280611be35a1SLionel Sambuc 
280711be35a1SLionel Sambuc 	static unsigned int expected[6] = {
280811be35a1SLionel Sambuc 		0xde, 0xdead, 0xdeadbeef,
280911be35a1SLionel Sambuc 		0xde, 0xdead, 0xdeadbeef
281011be35a1SLionel Sambuc 	};
281111be35a1SLionel Sambuc 
281211be35a1SLionel Sambuc 	size_t i, l;
281311be35a1SLionel Sambuc 	uint8_t *pkt = deadbeef_at_5;
281411be35a1SLionel Sambuc 	size_t pktsize = sizeof(deadbeef_at_5);
281511be35a1SLionel Sambuc 
281611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns[0]) / sizeof(insns[0][0]);
281711be35a1SLionel Sambuc 
281811be35a1SLionel Sambuc 	for (i = 0; i < 3; i++) {
281984d9c625SLionel Sambuc 		bpfjit_func_t code;
282011be35a1SLionel Sambuc 
282111be35a1SLionel Sambuc 		ATF_CHECK(bpf_validate(insns[i], insn_count));
282211be35a1SLionel Sambuc 
2823*0a6a1f1dSLionel Sambuc 		code = bpfjit_generate_code(NULL, insns[i], insn_count);
282411be35a1SLionel Sambuc 		ATF_REQUIRE(code != NULL);
282511be35a1SLionel Sambuc 
2826*0a6a1f1dSLionel Sambuc 		for (l = 1; l < 5 + lengths[i]; l++) {
2827*0a6a1f1dSLionel Sambuc 			ATF_CHECK(jitcall(code, pkt, l, l) == 0);
2828*0a6a1f1dSLionel Sambuc 			ATF_CHECK(jitcall(code, pkt, pktsize, l) == 0);
282911be35a1SLionel Sambuc 		}
283011be35a1SLionel Sambuc 
283111be35a1SLionel Sambuc 		l = 5 + lengths[i];
2832*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, l, l) == expected[i]);
2833*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, pktsize, l) == expected[i]);
283411be35a1SLionel Sambuc 
283511be35a1SLionel Sambuc 		l = pktsize;
2836*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, l, l) == expected[i]);
283711be35a1SLionel Sambuc 
283811be35a1SLionel Sambuc 		bpfjit_free_code(code);
283911be35a1SLionel Sambuc 	}
284011be35a1SLionel Sambuc }
284111be35a1SLionel Sambuc 
2842*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ld_ind_k_overflow);
ATF_TC_HEAD(libbpfjit_ld_ind_k_overflow,tc)2843*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ld_ind_k_overflow, tc)
284411be35a1SLionel Sambuc {
284511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
284611be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LD+BPF_IND with overflow in k+4");
284711be35a1SLionel Sambuc }
284811be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ld_ind_k_overflow,tc)2849*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ld_ind_k_overflow, tc)
285011be35a1SLionel Sambuc {
285111be35a1SLionel Sambuc 	static struct bpf_insn insns[12][3] = {
285211be35a1SLionel Sambuc 		{
285311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, UINT32_MAX),
285411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
285511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
285611be35a1SLionel Sambuc 		},
285711be35a1SLionel Sambuc 		{
285811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, UINT32_MAX - 1),
285911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
286011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
286111be35a1SLionel Sambuc 		},
286211be35a1SLionel Sambuc 		{
286311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX),
286411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
286511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
286611be35a1SLionel Sambuc 		},
286711be35a1SLionel Sambuc 		{
286811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 1),
286911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
287011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
287111be35a1SLionel Sambuc 		},
287211be35a1SLionel Sambuc 		{
287311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 2),
287411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
287511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
287611be35a1SLionel Sambuc 		},
287711be35a1SLionel Sambuc 		{
287811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 3),
287911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
288011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
288111be35a1SLionel Sambuc 		},
288211be35a1SLionel Sambuc 		{
288311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
288411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, UINT32_MAX),
288511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
288611be35a1SLionel Sambuc 		},
288711be35a1SLionel Sambuc 		{
288811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
288911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, UINT32_MAX - 1),
289011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
289111be35a1SLionel Sambuc 		},
289211be35a1SLionel Sambuc 		{
289311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
289411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX),
289511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
289611be35a1SLionel Sambuc 		},
289711be35a1SLionel Sambuc 		{
289811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
289911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 1),
290011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
290111be35a1SLionel Sambuc 		},
290211be35a1SLionel Sambuc 		{
290311be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
290411be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 2),
290511be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
290611be35a1SLionel Sambuc 		},
290711be35a1SLionel Sambuc 		{
290811be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_H+BPF_IND, 7),
290911be35a1SLionel Sambuc 			BPF_STMT(BPF_LD+BPF_W+BPF_IND, UINT32_MAX - 3),
291011be35a1SLionel Sambuc 			BPF_STMT(BPF_RET+BPF_K, 1)
291111be35a1SLionel Sambuc 		}
291211be35a1SLionel Sambuc 	};
291311be35a1SLionel Sambuc 
291411be35a1SLionel Sambuc 	int i;
291511be35a1SLionel Sambuc 	uint8_t pkt[8] = { 0 };
291611be35a1SLionel Sambuc 
291711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns[0]) / sizeof(insns[0][0]);
291811be35a1SLionel Sambuc 
291911be35a1SLionel Sambuc 	for (i = 0; i < 3; i++) {
292084d9c625SLionel Sambuc 		bpfjit_func_t code;
292111be35a1SLionel Sambuc 
292211be35a1SLionel Sambuc 		ATF_CHECK(bpf_validate(insns[i], insn_count));
292311be35a1SLionel Sambuc 
2924*0a6a1f1dSLionel Sambuc 		code = bpfjit_generate_code(NULL, insns[i], insn_count);
292511be35a1SLionel Sambuc 		ATF_REQUIRE(code != NULL);
292611be35a1SLionel Sambuc 
2927*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
292811be35a1SLionel Sambuc 
292911be35a1SLionel Sambuc 		bpfjit_free_code(code);
293011be35a1SLionel Sambuc 	}
293111be35a1SLionel Sambuc }
293211be35a1SLionel Sambuc 
2933*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ld_ind_x_overflow1);
ATF_TC_HEAD(libbpfjit_ld_ind_x_overflow1,tc)2934*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ld_ind_x_overflow1, tc)
293511be35a1SLionel Sambuc {
293611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
293711be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LD+BPF_IND with overflow in X+4");
293811be35a1SLionel Sambuc }
293911be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ld_ind_x_overflow1,tc)2940*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ld_ind_x_overflow1, tc)
294111be35a1SLionel Sambuc {
294211be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
294311be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_LEN, 0),
294411be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, UINT32_C(0xffffffff)),
294511be35a1SLionel Sambuc 		BPF_STMT(BPF_MISC+BPF_TAX, 0),
294611be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_IND, 0),
294711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
294811be35a1SLionel Sambuc 	};
294911be35a1SLionel Sambuc 
295011be35a1SLionel Sambuc 	size_t i;
295184d9c625SLionel Sambuc 	bpfjit_func_t code;
295211be35a1SLionel Sambuc 	uint8_t pkt[8] = { 10, 20, 30, 40, 50, 60, 70, 80 };
295311be35a1SLionel Sambuc 
295411be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
295511be35a1SLionel Sambuc 
295611be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
295711be35a1SLionel Sambuc 
2958*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
295911be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
296011be35a1SLionel Sambuc 
296111be35a1SLionel Sambuc 	for (i = 1; i <= sizeof(pkt); i++) {
296211be35a1SLionel Sambuc 		ATF_CHECK(bpf_filter(insns, pkt, i, i) == 10 * i);
2963*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, i, i) == 10 * i);
296411be35a1SLionel Sambuc 	}
296511be35a1SLionel Sambuc 
296611be35a1SLionel Sambuc 	bpfjit_free_code(code);
296711be35a1SLionel Sambuc }
296811be35a1SLionel Sambuc 
2969*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ld_ind_x_overflow2);
ATF_TC_HEAD(libbpfjit_ld_ind_x_overflow2,tc)2970*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ld_ind_x_overflow2, tc)
297111be35a1SLionel Sambuc {
297211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
297311be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LD+BPF_IND with overflow in X+4");
297411be35a1SLionel Sambuc }
297511be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ld_ind_x_overflow2,tc)2976*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ld_ind_x_overflow2, tc)
297711be35a1SLionel Sambuc {
297811be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
297911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_LEN, 0),
298011be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, UINT32_C(0xffffffff)),
298111be35a1SLionel Sambuc 		BPF_STMT(BPF_ST, 3),
298211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 3),
298311be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_IND, 0),
298411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
298511be35a1SLionel Sambuc 	};
298611be35a1SLionel Sambuc 
298711be35a1SLionel Sambuc 	size_t i;
298884d9c625SLionel Sambuc 	bpfjit_func_t code;
298911be35a1SLionel Sambuc 	uint8_t pkt[8] = { 10, 20, 30, 40, 50, 60, 70, 80 };
299011be35a1SLionel Sambuc 
299111be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
299211be35a1SLionel Sambuc 
299311be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
299411be35a1SLionel Sambuc 
2995*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
299611be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
299711be35a1SLionel Sambuc 
299811be35a1SLionel Sambuc 	for (i = 1; i <= sizeof(pkt); i++) {
299911be35a1SLionel Sambuc 		ATF_CHECK(bpf_filter(insns, pkt, i, i) == 10 * i);
3000*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, i, i) == 10 * i);
300111be35a1SLionel Sambuc 	}
300211be35a1SLionel Sambuc 
300311be35a1SLionel Sambuc 	bpfjit_free_code(code);
300411be35a1SLionel Sambuc }
300511be35a1SLionel Sambuc 
3006*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ld_len);
ATF_TC_HEAD(libbpfjit_ld_len,tc)3007*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ld_len, tc)
300811be35a1SLionel Sambuc {
300911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
301011be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LD+BPF_W+BPF_LEN");
301111be35a1SLionel Sambuc }
301211be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ld_len,tc)3013*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ld_len, tc)
301411be35a1SLionel Sambuc {
301511be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
301611be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
301711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
301811be35a1SLionel Sambuc 	};
301911be35a1SLionel Sambuc 
302011be35a1SLionel Sambuc 	size_t i;
302184d9c625SLionel Sambuc 	bpfjit_func_t code;
302211be35a1SLionel Sambuc 	uint8_t pkt[32]; /* the program doesn't read any data */
302311be35a1SLionel Sambuc 
302411be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
302511be35a1SLionel Sambuc 
302611be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
302711be35a1SLionel Sambuc 
3028*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
302911be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
303011be35a1SLionel Sambuc 
303111be35a1SLionel Sambuc 	for (i = 0; i < sizeof(pkt); i++)
3032*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, i, 1) == i);
303311be35a1SLionel Sambuc 
303411be35a1SLionel Sambuc 	bpfjit_free_code(code);
303511be35a1SLionel Sambuc }
303611be35a1SLionel Sambuc 
3037*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ld_imm);
ATF_TC_HEAD(libbpfjit_ld_imm,tc)3038*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ld_imm, tc)
303911be35a1SLionel Sambuc {
304011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
304111be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LD+BPF_IMM");
304211be35a1SLionel Sambuc }
304311be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ld_imm,tc)3044*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ld_imm, tc)
304511be35a1SLionel Sambuc {
304611be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
304711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, UINT32_MAX),
304811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
304911be35a1SLionel Sambuc 	};
305011be35a1SLionel Sambuc 
305184d9c625SLionel Sambuc 	bpfjit_func_t code;
305211be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
305311be35a1SLionel Sambuc 
305411be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
305511be35a1SLionel Sambuc 
305611be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
305711be35a1SLionel Sambuc 
3058*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
305911be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
306011be35a1SLionel Sambuc 
3061*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_MAX);
306211be35a1SLionel Sambuc 
306311be35a1SLionel Sambuc 	bpfjit_free_code(code);
306411be35a1SLionel Sambuc }
306511be35a1SLionel Sambuc 
3066*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ldx_imm1);
ATF_TC_HEAD(libbpfjit_ldx_imm1,tc)3067*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ldx_imm1, tc)
306811be35a1SLionel Sambuc {
306911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
307011be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LDX+BPF_IMM");
307111be35a1SLionel Sambuc }
307211be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ldx_imm1,tc)3073*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ldx_imm1, tc)
307411be35a1SLionel Sambuc {
307511be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
307611be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, UINT32_MAX - 5),
307711be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
307811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
307911be35a1SLionel Sambuc 	};
308011be35a1SLionel Sambuc 
308184d9c625SLionel Sambuc 	bpfjit_func_t code;
308211be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
308311be35a1SLionel Sambuc 
308411be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
308511be35a1SLionel Sambuc 
308611be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
308711be35a1SLionel Sambuc 
3088*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
308911be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
309011be35a1SLionel Sambuc 
3091*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_MAX - 5);
309211be35a1SLionel Sambuc 
309311be35a1SLionel Sambuc 	bpfjit_free_code(code);
309411be35a1SLionel Sambuc }
309511be35a1SLionel Sambuc 
3096*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ldx_imm2);
ATF_TC_HEAD(libbpfjit_ldx_imm2,tc)3097*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ldx_imm2, tc)
309811be35a1SLionel Sambuc {
309911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
310011be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LDX+BPF_IMM");
310111be35a1SLionel Sambuc }
310211be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ldx_imm2,tc)3103*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ldx_imm2, tc)
310411be35a1SLionel Sambuc {
310511be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
310611be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 5),
310711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 5),
310811be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 0),
310911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7),
311011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX)
311111be35a1SLionel Sambuc 	};
311211be35a1SLionel Sambuc 
311384d9c625SLionel Sambuc 	bpfjit_func_t code;
311411be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
311511be35a1SLionel Sambuc 
311611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
311711be35a1SLionel Sambuc 
311811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
311911be35a1SLionel Sambuc 
3120*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
312111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
312211be35a1SLionel Sambuc 
3123*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_MAX);
312411be35a1SLionel Sambuc 
312511be35a1SLionel Sambuc 	bpfjit_free_code(code);
312611be35a1SLionel Sambuc }
312711be35a1SLionel Sambuc 
3128*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ldx_len1);
ATF_TC_HEAD(libbpfjit_ldx_len1,tc)3129*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ldx_len1, tc)
313011be35a1SLionel Sambuc {
313111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
313211be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LDX+BPF_LEN");
313311be35a1SLionel Sambuc }
313411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ldx_len1,tc)3135*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ldx_len1, tc)
313611be35a1SLionel Sambuc {
313711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
313811be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0),
313911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
314011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
314111be35a1SLionel Sambuc 	};
314211be35a1SLionel Sambuc 
314311be35a1SLionel Sambuc 	size_t i;
314484d9c625SLionel Sambuc 	bpfjit_func_t code;
314511be35a1SLionel Sambuc 	uint8_t pkt[5]; /* the program doesn't read any data */
314611be35a1SLionel Sambuc 
314711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
314811be35a1SLionel Sambuc 
314911be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
315011be35a1SLionel Sambuc 
3151*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
315211be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
315311be35a1SLionel Sambuc 
315411be35a1SLionel Sambuc 	for (i = 1; i < sizeof(pkt); i++) {
3155*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, i, 1) == i);
3156*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, i + 1, i) == i + 1);
315711be35a1SLionel Sambuc 	}
315811be35a1SLionel Sambuc 
315911be35a1SLionel Sambuc 	bpfjit_free_code(code);
316011be35a1SLionel Sambuc }
316111be35a1SLionel Sambuc 
3162*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ldx_len2);
ATF_TC_HEAD(libbpfjit_ldx_len2,tc)3163*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ldx_len2, tc)
316411be35a1SLionel Sambuc {
316511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
316611be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LDX+BPF_LEN");
316711be35a1SLionel Sambuc }
316811be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ldx_len2,tc)3169*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ldx_len2, tc)
317011be35a1SLionel Sambuc {
317111be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
317211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0),
317311be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 5),
317411be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_X, 0, 1, 0),
317511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7),
317611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX)
317711be35a1SLionel Sambuc 	};
317811be35a1SLionel Sambuc 
317984d9c625SLionel Sambuc 	bpfjit_func_t code;
318011be35a1SLionel Sambuc 	uint8_t pkt[5]; /* the program doesn't read any data */
318111be35a1SLionel Sambuc 
318211be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
318311be35a1SLionel Sambuc 
318411be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
318511be35a1SLionel Sambuc 
3186*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
318711be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
318811be35a1SLionel Sambuc 
3189*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 1) == UINT32_MAX);
3190*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 5) == 7);
319111be35a1SLionel Sambuc 
319211be35a1SLionel Sambuc 	bpfjit_free_code(code);
319311be35a1SLionel Sambuc }
319411be35a1SLionel Sambuc 
3195*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_ldx_msh);
ATF_TC_HEAD(libbpfjit_ldx_msh,tc)3196*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_ldx_msh, tc)
319711be35a1SLionel Sambuc {
319811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
319911be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_LDX+BPF_MSH");
320011be35a1SLionel Sambuc }
320111be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_ldx_msh,tc)3202*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_ldx_msh, tc)
320311be35a1SLionel Sambuc {
320411be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
320511be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 1),
320611be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
320711be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
320811be35a1SLionel Sambuc 	};
320911be35a1SLionel Sambuc 
321084d9c625SLionel Sambuc 	bpfjit_func_t code;
321111be35a1SLionel Sambuc 	uint8_t pkt[2] = { 0, 0x7a };
321211be35a1SLionel Sambuc 
321311be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
321411be35a1SLionel Sambuc 
321511be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
321611be35a1SLionel Sambuc 
3217*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
321811be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
321911be35a1SLionel Sambuc 
3220*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 40);
322111be35a1SLionel Sambuc 
322211be35a1SLionel Sambuc 	bpfjit_free_code(code);
322311be35a1SLionel Sambuc }
322411be35a1SLionel Sambuc 
3225*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_misc_tax);
ATF_TC_HEAD(libbpfjit_misc_tax,tc)3226*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_misc_tax, tc)
322711be35a1SLionel Sambuc {
322811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
322911be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_MISC+BPF_TAX");
323011be35a1SLionel Sambuc }
323111be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_misc_tax,tc)3232*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_misc_tax, tc)
323311be35a1SLionel Sambuc {
323411be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
323511be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 3),
323611be35a1SLionel Sambuc 		BPF_STMT(BPF_MISC+BPF_TAX, 0),
323711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_IND, 2),
323811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
323911be35a1SLionel Sambuc 	};
324011be35a1SLionel Sambuc 
324184d9c625SLionel Sambuc 	bpfjit_func_t code;
324211be35a1SLionel Sambuc 	uint8_t pkt[] = { 0, 11, 22, 33, 44, 55 };
324311be35a1SLionel Sambuc 
324411be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
324511be35a1SLionel Sambuc 
324611be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
324711be35a1SLionel Sambuc 
3248*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
324911be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
325011be35a1SLionel Sambuc 
3251*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, sizeof(pkt), sizeof(pkt)) == 55);
325211be35a1SLionel Sambuc 
325311be35a1SLionel Sambuc 	bpfjit_free_code(code);
325411be35a1SLionel Sambuc }
325511be35a1SLionel Sambuc 
3256*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_misc_txa);
ATF_TC_HEAD(libbpfjit_misc_txa,tc)3257*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_misc_txa, tc)
325811be35a1SLionel Sambuc {
325911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
326011be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_MISC+BPF_TXA");
326111be35a1SLionel Sambuc }
326211be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_misc_txa,tc)3263*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_misc_txa, tc)
326411be35a1SLionel Sambuc {
326511be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
326611be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 391),
326711be35a1SLionel Sambuc 		BPF_STMT(BPF_MISC+BPF_TXA, 0),
326811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
326911be35a1SLionel Sambuc 	};
327011be35a1SLionel Sambuc 
327184d9c625SLionel Sambuc 	bpfjit_func_t code;
327211be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
327311be35a1SLionel Sambuc 
327411be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
327511be35a1SLionel Sambuc 
327611be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
327711be35a1SLionel Sambuc 
3278*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
327911be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
328011be35a1SLionel Sambuc 
3281*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 391);
328211be35a1SLionel Sambuc 
328311be35a1SLionel Sambuc 	bpfjit_free_code(code);
328411be35a1SLionel Sambuc }
328511be35a1SLionel Sambuc 
3286*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_st1);
ATF_TC_HEAD(libbpfjit_st1,tc)3287*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_st1, tc)
328811be35a1SLionel Sambuc {
328911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
329011be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ST");
329111be35a1SLionel Sambuc }
329211be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_st1,tc)3293*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_st1, tc)
329411be35a1SLionel Sambuc {
329511be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
329611be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
329711be35a1SLionel Sambuc 		BPF_STMT(BPF_ST, 0),
3298*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 1),
329911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_MEM, 0),
330011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
330111be35a1SLionel Sambuc 	};
330211be35a1SLionel Sambuc 
330311be35a1SLionel Sambuc 	size_t i;
330484d9c625SLionel Sambuc 	bpfjit_func_t code;
330511be35a1SLionel Sambuc 	uint8_t pkt[16]; /* the program doesn't read any data */
330611be35a1SLionel Sambuc 
330711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
330811be35a1SLionel Sambuc 
330911be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
331011be35a1SLionel Sambuc 
3311*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
331211be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
331311be35a1SLionel Sambuc 
331411be35a1SLionel Sambuc 	for (i = 1; i <= sizeof(pkt); i++)
3315*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, i, sizeof(pkt)) == i);
331611be35a1SLionel Sambuc 
331711be35a1SLionel Sambuc 	bpfjit_free_code(code);
331811be35a1SLionel Sambuc }
331911be35a1SLionel Sambuc 
3320*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_st2);
ATF_TC_HEAD(libbpfjit_st2,tc)3321*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_st2, tc)
332211be35a1SLionel Sambuc {
332311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
332411be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ST");
332511be35a1SLionel Sambuc }
332611be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_st2,tc)3327*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_st2, tc)
332811be35a1SLionel Sambuc {
332911be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
333011be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
333111be35a1SLionel Sambuc 		BPF_STMT(BPF_ST, BPF_MEMWORDS-1),
333211be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_MEM, 0),
333311be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
333411be35a1SLionel Sambuc 	};
333511be35a1SLionel Sambuc 
333684d9c625SLionel Sambuc 	bpfjit_func_t code;
333711be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
333811be35a1SLionel Sambuc 
333911be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
334011be35a1SLionel Sambuc 
334111be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
334211be35a1SLionel Sambuc 
3343*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
334411be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
334511be35a1SLionel Sambuc 
3346*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
334711be35a1SLionel Sambuc 
334811be35a1SLionel Sambuc 	bpfjit_free_code(code);
334911be35a1SLionel Sambuc }
335011be35a1SLionel Sambuc 
3351*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_st3);
ATF_TC_HEAD(libbpfjit_st3,tc)3352*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_st3, tc)
335311be35a1SLionel Sambuc {
335411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
335511be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ST");
335611be35a1SLionel Sambuc }
335711be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_st3,tc)3358*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_st3, tc)
335911be35a1SLionel Sambuc {
336011be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
336111be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
336211be35a1SLionel Sambuc 		BPF_STMT(BPF_ST, 0),
336311be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 100),
336411be35a1SLionel Sambuc 		BPF_STMT(BPF_ST, BPF_MEMWORDS-1),
336511be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 200),
336611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 301, 2, 0),
336711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_MEM, BPF_MEMWORDS-1),
336811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0),
336911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_MEM, 0),
337011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
337111be35a1SLionel Sambuc 	};
337211be35a1SLionel Sambuc 
337384d9c625SLionel Sambuc 	bpfjit_func_t code;
337411be35a1SLionel Sambuc 	uint8_t pkt[2]; /* the program doesn't read any data */
337511be35a1SLionel Sambuc 
337611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
337711be35a1SLionel Sambuc 
337811be35a1SLionel Sambuc 	ATF_REQUIRE(BPF_MEMWORDS > 1);
337911be35a1SLionel Sambuc 
338011be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
338111be35a1SLionel Sambuc 
3382*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
338311be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
338411be35a1SLionel Sambuc 
3385*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
3386*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 102);
338711be35a1SLionel Sambuc 
338811be35a1SLionel Sambuc 	bpfjit_free_code(code);
338911be35a1SLionel Sambuc }
339011be35a1SLionel Sambuc 
3391*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_st4);
ATF_TC_HEAD(libbpfjit_st4,tc)3392*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_st4, tc)
339311be35a1SLionel Sambuc {
339411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
339511be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ST");
339611be35a1SLionel Sambuc }
339711be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_st4,tc)3398*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_st4, tc)
339911be35a1SLionel Sambuc {
340011be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
340111be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_LEN, 0),
340211be35a1SLionel Sambuc 		BPF_STMT(BPF_ST, 5),
340311be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 100),
340411be35a1SLionel Sambuc 		BPF_STMT(BPF_ST, BPF_MEMWORDS-1),
340511be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_K, 200),
340611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 301, 2, 0),
340711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_MEM, BPF_MEMWORDS-1),
340811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0),
340911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_MEM, 5),
341011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
341111be35a1SLionel Sambuc 	};
341211be35a1SLionel Sambuc 
341384d9c625SLionel Sambuc 	bpfjit_func_t code;
341411be35a1SLionel Sambuc 	uint8_t pkt[2]; /* the program doesn't read any data */
341511be35a1SLionel Sambuc 
341611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
341711be35a1SLionel Sambuc 
341811be35a1SLionel Sambuc 	ATF_REQUIRE(BPF_MEMWORDS > 6);
341911be35a1SLionel Sambuc 
342011be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
342111be35a1SLionel Sambuc 
3422*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
342311be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
342411be35a1SLionel Sambuc 
3425*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 1);
3426*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 102);
342711be35a1SLionel Sambuc 
342811be35a1SLionel Sambuc 	bpfjit_free_code(code);
342911be35a1SLionel Sambuc }
343011be35a1SLionel Sambuc 
3431*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_st5);
ATF_TC_HEAD(libbpfjit_st5,tc)3432*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_st5, tc)
343311be35a1SLionel Sambuc {
343411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
343511be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_ST");
343611be35a1SLionel Sambuc }
343711be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_st5,tc)3438*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_st5, tc)
343911be35a1SLionel Sambuc {
344011be35a1SLionel Sambuc 	struct bpf_insn insns[5*BPF_MEMWORDS+2];
344111be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
344211be35a1SLionel Sambuc 
344311be35a1SLionel Sambuc 	size_t k;
344484d9c625SLionel Sambuc 	bpfjit_func_t code;
344511be35a1SLionel Sambuc 	uint8_t pkt[BPF_MEMWORDS]; /* the program doesn't read any data */
344611be35a1SLionel Sambuc 
344711be35a1SLionel Sambuc 	memset(insns, 0, sizeof(insns));
344811be35a1SLionel Sambuc 
344911be35a1SLionel Sambuc 	/* for each k do M[k] = k */
345011be35a1SLionel Sambuc 	for (k = 0; k < BPF_MEMWORDS; k++) {
345111be35a1SLionel Sambuc 		insns[2*k].code   = BPF_LD+BPF_IMM;
345211be35a1SLionel Sambuc 		insns[2*k].k      = 3*k;
345311be35a1SLionel Sambuc 		insns[2*k+1].code = BPF_ST;
345411be35a1SLionel Sambuc 		insns[2*k+1].k    = k;
345511be35a1SLionel Sambuc 	}
345611be35a1SLionel Sambuc 
345711be35a1SLionel Sambuc 	/* load wirelen into A */
345811be35a1SLionel Sambuc 	insns[2*BPF_MEMWORDS].code = BPF_LD+BPF_W+BPF_LEN;
345911be35a1SLionel Sambuc 
346011be35a1SLionel Sambuc 	/* for each k, if (A == k + 1) return M[k] */
346111be35a1SLionel Sambuc 	for (k = 0; k < BPF_MEMWORDS; k++) {
346211be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+1].code = BPF_JMP+BPF_JEQ+BPF_K;
346311be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+1].k    = k+1;
346411be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+1].jt   = 0;
346511be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+1].jf   = 2;
346611be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+2].code = BPF_LD+BPF_MEM;
346711be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+2].k    = k;
346811be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+3].code = BPF_RET+BPF_A;
346911be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+3].k    = 0;
347011be35a1SLionel Sambuc 	}
347111be35a1SLionel Sambuc 
347211be35a1SLionel Sambuc 	insns[5*BPF_MEMWORDS+1].code = BPF_RET+BPF_K;
347311be35a1SLionel Sambuc 	insns[5*BPF_MEMWORDS+1].k    = UINT32_MAX;
347411be35a1SLionel Sambuc 
347511be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
347611be35a1SLionel Sambuc 
3477*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
347811be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
347911be35a1SLionel Sambuc 
348011be35a1SLionel Sambuc 	for (k = 1; k <= sizeof(pkt); k++)
3481*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, k, k) == 3*(k-1));
348211be35a1SLionel Sambuc 
348311be35a1SLionel Sambuc 	bpfjit_free_code(code);
348411be35a1SLionel Sambuc }
348511be35a1SLionel Sambuc 
3486*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_stx1);
ATF_TC_HEAD(libbpfjit_stx1,tc)3487*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_stx1, tc)
348811be35a1SLionel Sambuc {
348911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
349011be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_STX");
349111be35a1SLionel Sambuc }
349211be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_stx1,tc)3493*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_stx1, tc)
349411be35a1SLionel Sambuc {
349511be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
349611be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0),
349711be35a1SLionel Sambuc 		BPF_STMT(BPF_STX, 0),
349811be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0),
349911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
350011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
350111be35a1SLionel Sambuc 	};
350211be35a1SLionel Sambuc 
350311be35a1SLionel Sambuc 	size_t i;
350484d9c625SLionel Sambuc 	bpfjit_func_t code;
350511be35a1SLionel Sambuc 	uint8_t pkt[16]; /* the program doesn't read any data */
350611be35a1SLionel Sambuc 
350711be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
350811be35a1SLionel Sambuc 
350911be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
351011be35a1SLionel Sambuc 
3511*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
351211be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
351311be35a1SLionel Sambuc 
351411be35a1SLionel Sambuc 	for (i = 1; i <= sizeof(pkt); i++)
3515*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, i, sizeof(pkt)) == i);
351611be35a1SLionel Sambuc 
351711be35a1SLionel Sambuc 	bpfjit_free_code(code);
351811be35a1SLionel Sambuc }
351911be35a1SLionel Sambuc 
3520*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_stx2);
ATF_TC_HEAD(libbpfjit_stx2,tc)3521*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_stx2, tc)
352211be35a1SLionel Sambuc {
352311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
352411be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_STX");
352511be35a1SLionel Sambuc }
352611be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_stx2,tc)3527*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_stx2, tc)
352811be35a1SLionel Sambuc {
352911be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
353011be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0),
353111be35a1SLionel Sambuc 		BPF_STMT(BPF_STX, BPF_MEMWORDS-1),
353211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 0),
353311be35a1SLionel Sambuc 		BPF_STMT(BPF_MISC+BPF_TXA, 0),
353411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
353511be35a1SLionel Sambuc 	};
353611be35a1SLionel Sambuc 
353784d9c625SLionel Sambuc 	bpfjit_func_t code;
353811be35a1SLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
353911be35a1SLionel Sambuc 
354011be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
354111be35a1SLionel Sambuc 
354211be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
354311be35a1SLionel Sambuc 
3544*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
354511be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
354611be35a1SLionel Sambuc 
3547*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
354811be35a1SLionel Sambuc 
354911be35a1SLionel Sambuc 	bpfjit_free_code(code);
355011be35a1SLionel Sambuc }
355111be35a1SLionel Sambuc 
3552*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_stx3);
ATF_TC_HEAD(libbpfjit_stx3,tc)3553*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_stx3, tc)
355411be35a1SLionel Sambuc {
355511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
355611be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_STX");
355711be35a1SLionel Sambuc }
355811be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_stx3,tc)3559*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_stx3, tc)
356011be35a1SLionel Sambuc {
356111be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
356211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_LEN, 0),
356311be35a1SLionel Sambuc 		BPF_STMT(BPF_STX, 5),
356411be35a1SLionel Sambuc 		BPF_STMT(BPF_STX, 2),
356511be35a1SLionel Sambuc 		BPF_STMT(BPF_STX, 3),
356611be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 1),
356711be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
356811be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 2),
356911be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
357011be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 3),
357111be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
357211be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 5),
357311be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
357411be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_MEM, 6),
357511be35a1SLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0),
357611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
357711be35a1SLionel Sambuc 	};
357811be35a1SLionel Sambuc 
357911be35a1SLionel Sambuc 	size_t i;
358084d9c625SLionel Sambuc 	bpfjit_func_t code;
358111be35a1SLionel Sambuc 	uint8_t pkt[16]; /* the program doesn't read any data */
358211be35a1SLionel Sambuc 
358311be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
358411be35a1SLionel Sambuc 
358511be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
358611be35a1SLionel Sambuc 
3587*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
358811be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
358911be35a1SLionel Sambuc 
359011be35a1SLionel Sambuc 	for (i = 1; i <= sizeof(pkt); i++)
3591*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, i, sizeof(pkt)) == 3 * i);
359211be35a1SLionel Sambuc 
359311be35a1SLionel Sambuc 	bpfjit_free_code(code);
359411be35a1SLionel Sambuc }
359511be35a1SLionel Sambuc 
3596*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_stx4);
ATF_TC_HEAD(libbpfjit_stx4,tc)3597*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_stx4, tc)
359811be35a1SLionel Sambuc {
359911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
360011be35a1SLionel Sambuc 	    "Test JIT compilation of BPF_STX");
360111be35a1SLionel Sambuc }
360211be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_stx4,tc)3603*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_stx4, tc)
360411be35a1SLionel Sambuc {
360511be35a1SLionel Sambuc 	struct bpf_insn insns[5*BPF_MEMWORDS+2];
360611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
360711be35a1SLionel Sambuc 
360811be35a1SLionel Sambuc 	size_t k;
360984d9c625SLionel Sambuc 	bpfjit_func_t code;
361011be35a1SLionel Sambuc 	uint8_t pkt[BPF_MEMWORDS]; /* the program doesn't read any data */
361111be35a1SLionel Sambuc 
361211be35a1SLionel Sambuc 	memset(insns, 0, sizeof(insns));
361311be35a1SLionel Sambuc 
361411be35a1SLionel Sambuc 	/* for each k do M[k] = k */
361511be35a1SLionel Sambuc 	for (k = 0; k < BPF_MEMWORDS; k++) {
361611be35a1SLionel Sambuc 		insns[2*k].code   = BPF_LDX+BPF_W+BPF_IMM;
361711be35a1SLionel Sambuc 		insns[2*k].k      = 3*k;
361811be35a1SLionel Sambuc 		insns[2*k+1].code = BPF_STX;
361911be35a1SLionel Sambuc 		insns[2*k+1].k    = k;
362011be35a1SLionel Sambuc 	}
362111be35a1SLionel Sambuc 
362211be35a1SLionel Sambuc 	/* load wirelen into A */
362311be35a1SLionel Sambuc 	insns[2*BPF_MEMWORDS].code = BPF_LD+BPF_W+BPF_LEN;
362411be35a1SLionel Sambuc 
362511be35a1SLionel Sambuc 	/* for each k, if (A == k + 1) return M[k] */
362611be35a1SLionel Sambuc 	for (k = 0; k < BPF_MEMWORDS; k++) {
362711be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+1].code = BPF_JMP+BPF_JEQ+BPF_K;
362811be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+1].k    = k+1;
362911be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+1].jt   = 0;
363011be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+1].jf   = 2;
363111be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+2].code = BPF_LD+BPF_MEM;
363211be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+2].k    = k;
363311be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+3].code = BPF_RET+BPF_A;
363411be35a1SLionel Sambuc 		insns[2*BPF_MEMWORDS+3*k+3].k    = 0;
363511be35a1SLionel Sambuc 	}
363611be35a1SLionel Sambuc 
363711be35a1SLionel Sambuc 	insns[5*BPF_MEMWORDS+1].code = BPF_RET+BPF_K;
363811be35a1SLionel Sambuc 	insns[5*BPF_MEMWORDS+1].k    = UINT32_MAX;
363911be35a1SLionel Sambuc 
364011be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
364111be35a1SLionel Sambuc 
3642*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
364311be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
364411be35a1SLionel Sambuc 
364511be35a1SLionel Sambuc 	for (k = 1; k <= sizeof(pkt); k++)
3646*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt, k, k) == 3*(k-1));
364711be35a1SLionel Sambuc 
364811be35a1SLionel Sambuc 	bpfjit_free_code(code);
364911be35a1SLionel Sambuc }
365011be35a1SLionel Sambuc 
3651*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_opt_ld_abs_1);
ATF_TC_HEAD(libbpfjit_opt_ld_abs_1,tc)3652*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_opt_ld_abs_1, tc)
365311be35a1SLionel Sambuc {
365411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
365511be35a1SLionel Sambuc 	    "Test JIT compilation with length optimization "
365611be35a1SLionel Sambuc 	    "applied to BPF_LD+BPF_ABS");
365711be35a1SLionel Sambuc }
365811be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_opt_ld_abs_1,tc)3659*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_opt_ld_abs_1, tc)
366011be35a1SLionel Sambuc {
366111be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
366211be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
366311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 8),
366411be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
366511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
366611be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
366711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 4),
366811be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
366911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
367011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 1),
367111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
367211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
367311be35a1SLionel Sambuc 	};
367411be35a1SLionel Sambuc 
367511be35a1SLionel Sambuc 	size_t i, j;
367684d9c625SLionel Sambuc 	bpfjit_func_t code;
367711be35a1SLionel Sambuc 	uint8_t pkt[2][34] = {
367811be35a1SLionel Sambuc 		{
367911be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
368011be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
368111be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f,
368211be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23
368311be35a1SLionel Sambuc 		},
368411be35a1SLionel Sambuc 		{
368511be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
368611be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
368711be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23,
368811be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f
368911be35a1SLionel Sambuc 		}
369011be35a1SLionel Sambuc 	};
369111be35a1SLionel Sambuc 
369211be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
369311be35a1SLionel Sambuc 
369411be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
369511be35a1SLionel Sambuc 
3696*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
369711be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
369811be35a1SLionel Sambuc 
369911be35a1SLionel Sambuc 	for (i = 0; i < 2; i++) {
370011be35a1SLionel Sambuc 		for (j = 1; j < sizeof(pkt[i]); j++)
3701*0a6a1f1dSLionel Sambuc 			ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3702*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
370311be35a1SLionel Sambuc 	}
370411be35a1SLionel Sambuc 
370511be35a1SLionel Sambuc 	bpfjit_free_code(code);
370611be35a1SLionel Sambuc }
370711be35a1SLionel Sambuc 
3708*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_opt_ld_abs_2);
ATF_TC_HEAD(libbpfjit_opt_ld_abs_2,tc)3709*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_opt_ld_abs_2, tc)
371011be35a1SLionel Sambuc {
371111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
371211be35a1SLionel Sambuc 	    "Test JIT compilation with length optimization "
371311be35a1SLionel Sambuc 	    "applied to BPF_LD+BPF_ABS");
371411be35a1SLionel Sambuc }
371511be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_opt_ld_abs_2,tc)3716*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_opt_ld_abs_2, tc)
371711be35a1SLionel Sambuc {
371811be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
371911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
372011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
372111be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
372211be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 6),
372311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 5),
372411be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
372511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 3),
372611be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
372711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 1),
372811be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
372911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
373011be35a1SLionel Sambuc 	};
373111be35a1SLionel Sambuc 
373211be35a1SLionel Sambuc 	size_t i, j;
373384d9c625SLionel Sambuc 	bpfjit_func_t code;
373411be35a1SLionel Sambuc 	uint8_t pkt[2][34] = {
373511be35a1SLionel Sambuc 		{
373611be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
373711be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
373811be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f,
373911be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23
374011be35a1SLionel Sambuc 		},
374111be35a1SLionel Sambuc 		{
374211be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
374311be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
374411be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23,
374511be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f
374611be35a1SLionel Sambuc 		}
374711be35a1SLionel Sambuc 	};
374811be35a1SLionel Sambuc 
374911be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
375011be35a1SLionel Sambuc 
375111be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
375211be35a1SLionel Sambuc 
3753*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
375411be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
375511be35a1SLionel Sambuc 
375611be35a1SLionel Sambuc 	for (i = 0; i < 2; i++) {
375711be35a1SLionel Sambuc 		for (j = 1; j < sizeof(pkt[i]); j++)
3758*0a6a1f1dSLionel Sambuc 			ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3759*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
376011be35a1SLionel Sambuc 	}
376111be35a1SLionel Sambuc 
376211be35a1SLionel Sambuc 	bpfjit_free_code(code);
376311be35a1SLionel Sambuc }
376411be35a1SLionel Sambuc 
3765*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_opt_ld_abs_3);
ATF_TC_HEAD(libbpfjit_opt_ld_abs_3,tc)3766*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_opt_ld_abs_3, tc)
376711be35a1SLionel Sambuc {
376811be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
376911be35a1SLionel Sambuc 	    "Test JIT compilation with length optimization "
377011be35a1SLionel Sambuc 	    "applied to BPF_LD+BPF_ABS");
377111be35a1SLionel Sambuc }
377211be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_opt_ld_abs_3,tc)3773*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_opt_ld_abs_3, tc)
377411be35a1SLionel Sambuc {
377511be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
377611be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
377711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 2),
377811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
377911be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 3, 6),
378011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 5),
378111be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
378211be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
378311be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
378411be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 1),
378511be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
378611be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
378711be35a1SLionel Sambuc 	};
378811be35a1SLionel Sambuc 
378911be35a1SLionel Sambuc 	size_t i, j;
379084d9c625SLionel Sambuc 	bpfjit_func_t code;
379111be35a1SLionel Sambuc 	uint8_t pkt[2][34] = {
379211be35a1SLionel Sambuc 		{
379311be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
379411be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
379511be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f,
379611be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23
379711be35a1SLionel Sambuc 		},
379811be35a1SLionel Sambuc 		{
379911be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
380011be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
380111be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23,
380211be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f
380311be35a1SLionel Sambuc 		}
380411be35a1SLionel Sambuc 	};
380511be35a1SLionel Sambuc 
380611be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
380711be35a1SLionel Sambuc 
380811be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
380911be35a1SLionel Sambuc 
3810*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
381111be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
381211be35a1SLionel Sambuc 
381311be35a1SLionel Sambuc 	for (i = 0; i < 2; i++) {
381411be35a1SLionel Sambuc 		for (j = 1; j < sizeof(pkt[i]); j++)
3815*0a6a1f1dSLionel Sambuc 			ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3816*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
381711be35a1SLionel Sambuc 	}
381811be35a1SLionel Sambuc 
381911be35a1SLionel Sambuc 	bpfjit_free_code(code);
382011be35a1SLionel Sambuc }
382111be35a1SLionel Sambuc 
3822*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_opt_ld_ind_1);
ATF_TC_HEAD(libbpfjit_opt_ld_ind_1,tc)3823*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_opt_ld_ind_1, tc)
382411be35a1SLionel Sambuc {
382511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
382611be35a1SLionel Sambuc 	    "Test JIT compilation with length optimization "
382711be35a1SLionel Sambuc 	    "applied to BPF_LD+BPF_IND");
382811be35a1SLionel Sambuc }
382911be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_opt_ld_ind_1,tc)3830*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_opt_ld_ind_1, tc)
383111be35a1SLionel Sambuc {
383211be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
383311be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 12),
383411be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_IND, 0),
383511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 8),
383611be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 14),
383711be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
383811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 18),
383911be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 4),
384011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
384111be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 18),
384211be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 1),
384311be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
384411be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
384511be35a1SLionel Sambuc 	};
384611be35a1SLionel Sambuc 
384711be35a1SLionel Sambuc 	size_t i, j;
384884d9c625SLionel Sambuc 	bpfjit_func_t code;
384911be35a1SLionel Sambuc 	uint8_t pkt[2][34] = {
385011be35a1SLionel Sambuc 		{
385111be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
385211be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
385311be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f,
385411be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23
385511be35a1SLionel Sambuc 		},
385611be35a1SLionel Sambuc 		{
385711be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
385811be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
385911be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23,
386011be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f
386111be35a1SLionel Sambuc 		}
386211be35a1SLionel Sambuc 	};
386311be35a1SLionel Sambuc 
386411be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
386511be35a1SLionel Sambuc 
386611be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
386711be35a1SLionel Sambuc 
3868*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
386911be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
387011be35a1SLionel Sambuc 
387111be35a1SLionel Sambuc 	for (i = 0; i < 2; i++) {
387211be35a1SLionel Sambuc 		for (j = 1; j < sizeof(pkt[i]); j++)
3873*0a6a1f1dSLionel Sambuc 			ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3874*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
387511be35a1SLionel Sambuc 	}
387611be35a1SLionel Sambuc 
387711be35a1SLionel Sambuc 	bpfjit_free_code(code);
387811be35a1SLionel Sambuc }
387911be35a1SLionel Sambuc 
3880*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_opt_ld_ind_2);
ATF_TC_HEAD(libbpfjit_opt_ld_ind_2,tc)3881*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_opt_ld_ind_2, tc)
388211be35a1SLionel Sambuc {
388311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
388411be35a1SLionel Sambuc 	    "Test JIT compilation with length optimization "
388511be35a1SLionel Sambuc 	    "applied to BPF_LD+BPF_IND");
388611be35a1SLionel Sambuc }
388711be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_opt_ld_ind_2,tc)3888*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_opt_ld_ind_2, tc)
388911be35a1SLionel Sambuc {
389011be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
389111be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
389211be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 26),
389311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
389411be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 30),
389511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 6),
389611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 5),
389711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 30),
389811be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 3),
389911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_IND, 12),
390011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 1),
390111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
390211be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
390311be35a1SLionel Sambuc 	};
390411be35a1SLionel Sambuc 
390511be35a1SLionel Sambuc 	size_t i, j;
390684d9c625SLionel Sambuc 	bpfjit_func_t code;
390711be35a1SLionel Sambuc 	uint8_t pkt[2][34] = {
390811be35a1SLionel Sambuc 		{
390911be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
391011be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
391111be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f,
391211be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23
391311be35a1SLionel Sambuc 		},
391411be35a1SLionel Sambuc 		{
391511be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
391611be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
391711be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23,
391811be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f
391911be35a1SLionel Sambuc 		}
392011be35a1SLionel Sambuc 	};
392111be35a1SLionel Sambuc 
392211be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
392311be35a1SLionel Sambuc 
392411be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
392511be35a1SLionel Sambuc 
3926*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
392711be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
392811be35a1SLionel Sambuc 
392911be35a1SLionel Sambuc 	for (i = 0; i < 2; i++) {
393011be35a1SLionel Sambuc 		for (j = 1; j < sizeof(pkt[i]); j++)
3931*0a6a1f1dSLionel Sambuc 			ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3932*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
393311be35a1SLionel Sambuc 	}
393411be35a1SLionel Sambuc 
393511be35a1SLionel Sambuc 	bpfjit_free_code(code);
393611be35a1SLionel Sambuc }
393711be35a1SLionel Sambuc 
3938*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_opt_ld_ind_3);
ATF_TC_HEAD(libbpfjit_opt_ld_ind_3,tc)3939*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_opt_ld_ind_3, tc)
394011be35a1SLionel Sambuc {
394111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
394211be35a1SLionel Sambuc 	    "Test JIT compilation with length optimization "
394311be35a1SLionel Sambuc 	    "applied to BPF_LD+BPF_IND");
394411be35a1SLionel Sambuc }
394511be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_opt_ld_ind_3,tc)3946*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_opt_ld_ind_3, tc)
394711be35a1SLionel Sambuc {
394811be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
394911be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 15),
395011be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 15),
395111be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 2),
395211be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 11),
395311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 3, 7),
395411be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 6),
395511be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 11),
395611be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 4),
395711be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
395811be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_IND, 12),
395911be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 1),
396011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
396111be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
396211be35a1SLionel Sambuc 	};
396311be35a1SLionel Sambuc 
396411be35a1SLionel Sambuc 	size_t i, j;
396584d9c625SLionel Sambuc 	bpfjit_func_t code;
396611be35a1SLionel Sambuc 	uint8_t pkt[2][34] = {
396711be35a1SLionel Sambuc 		{
396811be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
396911be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
397011be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f,
397111be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23
397211be35a1SLionel Sambuc 		},
397311be35a1SLionel Sambuc 		{
397411be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
397511be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
397611be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23,
397711be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f
397811be35a1SLionel Sambuc 		}
397911be35a1SLionel Sambuc 	};
398011be35a1SLionel Sambuc 
398111be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
398211be35a1SLionel Sambuc 
398311be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
398411be35a1SLionel Sambuc 
3985*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
398611be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
398711be35a1SLionel Sambuc 
398811be35a1SLionel Sambuc 	for (i = 0; i < 2; i++) {
398911be35a1SLionel Sambuc 		for (j = 1; j < sizeof(pkt[i]); j++)
3990*0a6a1f1dSLionel Sambuc 			ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
3991*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
399211be35a1SLionel Sambuc 	}
399311be35a1SLionel Sambuc 
399411be35a1SLionel Sambuc 	bpfjit_free_code(code);
399511be35a1SLionel Sambuc }
399611be35a1SLionel Sambuc 
3997*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_opt_ld_ind_4);
ATF_TC_HEAD(libbpfjit_opt_ld_ind_4,tc)3998*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_opt_ld_ind_4, tc)
399911be35a1SLionel Sambuc {
400011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
400111be35a1SLionel Sambuc 	    "Test JIT compilation with length optimization "
400211be35a1SLionel Sambuc 	    "applied to BPF_LD+BPF_IND");
400311be35a1SLionel Sambuc }
400411be35a1SLionel Sambuc 
ATF_TC_BODY(libbpfjit_opt_ld_ind_4,tc)4005*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_opt_ld_ind_4, tc)
400611be35a1SLionel Sambuc {
400711be35a1SLionel Sambuc 	static struct bpf_insn insns[] = {
400811be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 11),
400911be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 19),
401011be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 2),
401111be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 15),
401211be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 3, 7),
401311be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 6),
401411be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_IND, 15),
401511be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 4),
401611be35a1SLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 0),
401711be35a1SLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_IND, 12),
401811be35a1SLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x800, 0, 1),
401911be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
402011be35a1SLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
402111be35a1SLionel Sambuc 	};
402211be35a1SLionel Sambuc 
402311be35a1SLionel Sambuc 	size_t i, j;
402484d9c625SLionel Sambuc 	bpfjit_func_t code;
402511be35a1SLionel Sambuc 	uint8_t pkt[2][34] = {
402611be35a1SLionel Sambuc 		{
402711be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
402811be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
402911be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f,
403011be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23
403111be35a1SLionel Sambuc 		},
403211be35a1SLionel Sambuc 		{
403311be35a1SLionel Sambuc 			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0x08, 0x00,
403411be35a1SLionel Sambuc 			14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
403511be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x23,
403611be35a1SLionel Sambuc 			0x80, 0x03, 0x70, 0x0f
403711be35a1SLionel Sambuc 		}
403811be35a1SLionel Sambuc 	};
403911be35a1SLionel Sambuc 
404011be35a1SLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
404111be35a1SLionel Sambuc 
404211be35a1SLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
404311be35a1SLionel Sambuc 
4044*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
404511be35a1SLionel Sambuc 	ATF_REQUIRE(code != NULL);
404611be35a1SLionel Sambuc 
404711be35a1SLionel Sambuc 	for (i = 0; i < 2; i++) {
404811be35a1SLionel Sambuc 		for (j = 1; j < sizeof(pkt[i]); j++)
4049*0a6a1f1dSLionel Sambuc 			ATF_CHECK(jitcall(code, pkt[i], j, j) == 0);
4050*0a6a1f1dSLionel Sambuc 		ATF_CHECK(jitcall(code, pkt[i], j, j) == UINT32_MAX);
405111be35a1SLionel Sambuc 	}
405211be35a1SLionel Sambuc 
405311be35a1SLionel Sambuc 	bpfjit_free_code(code);
405411be35a1SLionel Sambuc }
405511be35a1SLionel Sambuc 
4056*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_abc_ja);
ATF_TC_HEAD(libbpfjit_abc_ja,tc)4057*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_abc_ja, tc)
4058*0a6a1f1dSLionel Sambuc {
4059*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
4060*0a6a1f1dSLionel Sambuc 	    "Test ABC optimization with a single BPF_JMP+BPF_JA");
4061*0a6a1f1dSLionel Sambuc }
4062*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_abc_ja,tc)4063*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_abc_ja, tc)
4064*0a6a1f1dSLionel Sambuc {
4065*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
4066*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 3), /* min. length 4 */
4067*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_JMP+BPF_JA, 2),
4068*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, UINT32_MAX - 1),
4069*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
4070*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 2), /* min. length 6 */
4071*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0),
4072*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
4073*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 6),
4074*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
4075*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 7),
4076*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
4077*0a6a1f1dSLionel Sambuc 	};
4078*0a6a1f1dSLionel Sambuc 
4079*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
4080*0a6a1f1dSLionel Sambuc 	uint8_t pkt[6] = {0, 0, /* UINT32_MAX: */ 255, 255, 255, 255};
4081*0a6a1f1dSLionel Sambuc 
4082*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4083*0a6a1f1dSLionel Sambuc 
4084*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
4085*0a6a1f1dSLionel Sambuc 
4086*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
4087*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
4088*0a6a1f1dSLionel Sambuc 
4089*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4090*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4091*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4092*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4093*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4094*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == UINT32_MAX);
4095*0a6a1f1dSLionel Sambuc 
4096*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
4097*0a6a1f1dSLionel Sambuc }
4098*0a6a1f1dSLionel Sambuc 
4099*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_abc_ja_over);
ATF_TC_HEAD(libbpfjit_abc_ja_over,tc)4100*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_abc_ja_over, tc)
4101*0a6a1f1dSLionel Sambuc {
4102*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
4103*0a6a1f1dSLionel Sambuc 	    "Test ABC optimization when BPF_JMP+BPF_JA jumps over all loads");
4104*0a6a1f1dSLionel Sambuc }
4105*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_abc_ja_over,tc)4106*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_abc_ja_over, tc)
4107*0a6a1f1dSLionel Sambuc {
4108*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
4109*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_JMP+BPF_JA, 2),
4110*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 3),
4111*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
4112*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
4113*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 4),
4114*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 1),
4115*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 5),
4116*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 2),
4117*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 6),
4118*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 3),
4119*0a6a1f1dSLionel Sambuc 	};
4120*0a6a1f1dSLionel Sambuc 
4121*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
4122*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1]; /* the program doesn't read any data */
4123*0a6a1f1dSLionel Sambuc 
4124*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4125*0a6a1f1dSLionel Sambuc 
4126*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
4127*0a6a1f1dSLionel Sambuc 
4128*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
4129*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
4130*0a6a1f1dSLionel Sambuc 
4131*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == UINT32_MAX);
4132*0a6a1f1dSLionel Sambuc 
4133*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
4134*0a6a1f1dSLionel Sambuc }
4135*0a6a1f1dSLionel Sambuc 
4136*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_abc_ld_chain);
ATF_TC_HEAD(libbpfjit_abc_ld_chain,tc)4137*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_abc_ld_chain, tc)
4138*0a6a1f1dSLionel Sambuc {
4139*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
4140*0a6a1f1dSLionel Sambuc 	    "Test ABC optimization of a chain of BPF_LD instructions "
4141*0a6a1f1dSLionel Sambuc 	    "with exits leading to a single BPF_RET");
4142*0a6a1f1dSLionel Sambuc }
4143*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_abc_ld_chain,tc)4144*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_abc_ld_chain, tc)
4145*0a6a1f1dSLionel Sambuc {
4146*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
4147*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 3), /* min. length 4 */
4148*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 8, 0, 4),
4149*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 4), /* min. length 6 */
4150*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGE+BPF_K, 7, 0, 2),
4151*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 6), /* min. length 10 */
4152*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JGT+BPF_K, 6, 0, 1),
4153*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 123456789),
4154*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 987654321),
4155*0a6a1f1dSLionel Sambuc 	};
4156*0a6a1f1dSLionel Sambuc 
4157*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
4158*0a6a1f1dSLionel Sambuc 	uint8_t pkt[10] = {};
4159*0a6a1f1dSLionel Sambuc 
4160*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4161*0a6a1f1dSLionel Sambuc 
4162*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
4163*0a6a1f1dSLionel Sambuc 
4164*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
4165*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
4166*0a6a1f1dSLionel Sambuc 
4167*0a6a1f1dSLionel Sambuc 	/* Packet is too short. */
4168*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4169*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4170*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4171*0a6a1f1dSLionel Sambuc 
4172*0a6a1f1dSLionel Sambuc 	/* !(pkt[3] == 8) => return 123456789 */
4173*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 123456789);
4174*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 123456789);
4175*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 123456789);
4176*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 123456789);
4177*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 123456789);
4178*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 9, 9) == 123456789);
4179*0a6a1f1dSLionel Sambuc 
4180*0a6a1f1dSLionel Sambuc 	/* !(pkt[4:2] >= 7) => too short or return 123456789 */
4181*0a6a1f1dSLionel Sambuc 	pkt[3] = 8;
4182*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4183*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4184*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4185*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4186*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4187*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 123456789);
4188*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 9, 9) == 123456789);
4189*0a6a1f1dSLionel Sambuc 
4190*0a6a1f1dSLionel Sambuc 	/* !(pkt[6:4] > 6) => too short or return 987654321 */
4191*0a6a1f1dSLionel Sambuc 	pkt[4] = pkt[5] = 1;
4192*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4193*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4194*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4195*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4196*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4197*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4198*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4199*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4200*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4201*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 10, 10) == 987654321);
4202*0a6a1f1dSLionel Sambuc 
4203*0a6a1f1dSLionel Sambuc 	/* (pkt[6:4] > 6) => too short or return 123456789 */
4204*0a6a1f1dSLionel Sambuc 	pkt[6] = pkt[7] = pkt[8] = pkt[9] = 1;
4205*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4206*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4207*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4208*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4209*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4210*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4211*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4212*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4213*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4214*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 10, 10) == 123456789);
4215*0a6a1f1dSLionel Sambuc 
4216*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
4217*0a6a1f1dSLionel Sambuc }
4218*0a6a1f1dSLionel Sambuc 
4219*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_examples_1);
ATF_TC_HEAD(libbpfjit_examples_1,tc)4220*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_examples_1, tc)
4221*0a6a1f1dSLionel Sambuc {
4222*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
4223*0a6a1f1dSLionel Sambuc 	    "Test the first example from bpf(4) - "
4224*0a6a1f1dSLionel Sambuc 	    "accept Reverse ARP requests");
4225*0a6a1f1dSLionel Sambuc }
4226*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_examples_1,tc)4227*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_examples_1, tc)
4228*0a6a1f1dSLionel Sambuc {
4229*0a6a1f1dSLionel Sambuc 	/*
4230*0a6a1f1dSLionel Sambuc 	 * The following filter is taken from the Reverse ARP
4231*0a6a1f1dSLionel Sambuc 	 * Daemon. It accepts only Reverse ARP requests.
4232*0a6a1f1dSLionel Sambuc 	 */
4233*0a6a1f1dSLionel Sambuc 	struct bpf_insn insns[] = {
4234*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
4235*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8035, 0, 3),
4236*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
4237*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 3, 0, 1),
4238*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 42),
4239*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
4240*0a6a1f1dSLionel Sambuc 	};
4241*0a6a1f1dSLionel Sambuc 
4242*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
4243*0a6a1f1dSLionel Sambuc 	uint8_t pkt[22] = {};
4244*0a6a1f1dSLionel Sambuc 
4245*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4246*0a6a1f1dSLionel Sambuc 
4247*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
4248*0a6a1f1dSLionel Sambuc 
4249*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
4250*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
4251*0a6a1f1dSLionel Sambuc 
4252*0a6a1f1dSLionel Sambuc 	/* Packet is too short. */
4253*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4254*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4255*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4256*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4257*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4258*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4259*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4260*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4261*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4262*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4263*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4264*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4265*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4266*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4267*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4268*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4269*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4270*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4271*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4272*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4273*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4274*0a6a1f1dSLionel Sambuc 
4275*0a6a1f1dSLionel Sambuc 	/* The packet doesn't match. */
4276*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4277*0a6a1f1dSLionel Sambuc 
4278*0a6a1f1dSLionel Sambuc 	/* Still no match after setting the protocol field. */
4279*0a6a1f1dSLionel Sambuc 	pkt[12] = 0x80; pkt[13] = 0x35;
4280*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4281*0a6a1f1dSLionel Sambuc 
4282*0a6a1f1dSLionel Sambuc 	/* Set RARP message type. */
4283*0a6a1f1dSLionel Sambuc 	pkt[21] = 3;
4284*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 22, 22) == 42);
4285*0a6a1f1dSLionel Sambuc 
4286*0a6a1f1dSLionel Sambuc 	/* Packet is too short. */
4287*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4288*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4289*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4290*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4291*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4292*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4293*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4294*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4295*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4296*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4297*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4298*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4299*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4300*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4301*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4302*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4303*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4304*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4305*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4306*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4307*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4308*0a6a1f1dSLionel Sambuc 
4309*0a6a1f1dSLionel Sambuc 	/* Change RARP message type. */
4310*0a6a1f1dSLionel Sambuc 	pkt[20] = 3;
4311*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4312*0a6a1f1dSLionel Sambuc 
4313*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
4314*0a6a1f1dSLionel Sambuc }
4315*0a6a1f1dSLionel Sambuc 
4316*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_examples_2);
ATF_TC_HEAD(libbpfjit_examples_2,tc)4317*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_examples_2, tc)
4318*0a6a1f1dSLionel Sambuc {
4319*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
4320*0a6a1f1dSLionel Sambuc 	    "Test the second example from bpf(4) - "
4321*0a6a1f1dSLionel Sambuc 	    "accept IP packets between two specified hosts");
4322*0a6a1f1dSLionel Sambuc }
4323*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_examples_2,tc)4324*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_examples_2, tc)
4325*0a6a1f1dSLionel Sambuc {
4326*0a6a1f1dSLionel Sambuc 	/*
4327*0a6a1f1dSLionel Sambuc 	 * This filter accepts only IP packets between host 128.3.112.15
4328*0a6a1f1dSLionel Sambuc 	 * and 128.3.112.35.
4329*0a6a1f1dSLionel Sambuc 	 */
4330*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
4331*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
4332*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x0800, 0, 8),
4333*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 26),
4334*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 2),
4335*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
4336*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 3, 4),
4337*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x80037023, 0, 3),
4338*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_W+BPF_ABS, 30),
4339*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x8003700f, 0, 1),
4340*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
4341*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
4342*0a6a1f1dSLionel Sambuc 	};
4343*0a6a1f1dSLionel Sambuc 
4344*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
4345*0a6a1f1dSLionel Sambuc 	uint8_t pkt[34] = {};
4346*0a6a1f1dSLionel Sambuc 
4347*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4348*0a6a1f1dSLionel Sambuc 
4349*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
4350*0a6a1f1dSLionel Sambuc 
4351*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
4352*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
4353*0a6a1f1dSLionel Sambuc 
4354*0a6a1f1dSLionel Sambuc 	/* Packet is too short. */
4355*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4356*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4357*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4358*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4359*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4360*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4361*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4362*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4363*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4364*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4365*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4366*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4367*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4368*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4369*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4370*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4371*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4372*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4373*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4374*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4375*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4376*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4377*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 23, 23) == 0);
4378*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 24, 24) == 0);
4379*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 25, 25) == 0);
4380*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 26, 26) == 0);
4381*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 27, 27) == 0);
4382*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 28, 28) == 0);
4383*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 29, 29) == 0);
4384*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4385*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 31, 31) == 0);
4386*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 32, 32) == 0);
4387*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 33, 33) == 0);
4388*0a6a1f1dSLionel Sambuc 
4389*0a6a1f1dSLionel Sambuc 	/* The packet doesn't match. */
4390*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 34, 34) == 0);
4391*0a6a1f1dSLionel Sambuc 
4392*0a6a1f1dSLionel Sambuc 	/* Still no match after setting the protocol field. */
4393*0a6a1f1dSLionel Sambuc 	pkt[12] = 8;
4394*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 34, 34) == 0);
4395*0a6a1f1dSLionel Sambuc 
4396*0a6a1f1dSLionel Sambuc 	pkt[26] = 128; pkt[27] = 3; pkt[28] = 112; pkt[29] = 15;
4397*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 34, 34) == 0);
4398*0a6a1f1dSLionel Sambuc 
4399*0a6a1f1dSLionel Sambuc 	pkt[30] = 128; pkt[31] = 3; pkt[32] = 112; pkt[33] = 35;
4400*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 34, 34) == UINT32_MAX);
4401*0a6a1f1dSLionel Sambuc 
4402*0a6a1f1dSLionel Sambuc 	/* Swap the ip addresses. */
4403*0a6a1f1dSLionel Sambuc 	pkt[26] = 128; pkt[27] = 3; pkt[28] = 112; pkt[29] = 35;
4404*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 34, 34) == 0);
4405*0a6a1f1dSLionel Sambuc 
4406*0a6a1f1dSLionel Sambuc 	pkt[30] = 128; pkt[31] = 3; pkt[32] = 112; pkt[33] = 15;
4407*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 34, 34) == UINT32_MAX);
4408*0a6a1f1dSLionel Sambuc 
4409*0a6a1f1dSLionel Sambuc 	/* Packet is too short. */
4410*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4411*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4412*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4413*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4414*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4415*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4416*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4417*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4418*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4419*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4420*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4421*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4422*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4423*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4424*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4425*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4426*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4427*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4428*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4429*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4430*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4431*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4432*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 23, 23) == 0);
4433*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 24, 24) == 0);
4434*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 25, 25) == 0);
4435*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 26, 26) == 0);
4436*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 27, 27) == 0);
4437*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 28, 28) == 0);
4438*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 29, 29) == 0);
4439*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4440*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 31, 31) == 0);
4441*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 32, 32) == 0);
4442*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 33, 33) == 0);
4443*0a6a1f1dSLionel Sambuc 
4444*0a6a1f1dSLionel Sambuc 	/* Change the protocol field. */
4445*0a6a1f1dSLionel Sambuc 	pkt[13] = 8;
4446*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 34, 34) == 0);
4447*0a6a1f1dSLionel Sambuc 
4448*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
4449*0a6a1f1dSLionel Sambuc }
4450*0a6a1f1dSLionel Sambuc 
4451*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_examples_3);
ATF_TC_HEAD(libbpfjit_examples_3,tc)4452*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_examples_3, tc)
4453*0a6a1f1dSLionel Sambuc {
4454*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr",
4455*0a6a1f1dSLionel Sambuc 	    "Test the third example from bpf(4) - "
4456*0a6a1f1dSLionel Sambuc 	    "accept TCP finger packets");
4457*0a6a1f1dSLionel Sambuc }
4458*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_examples_3,tc)4459*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_examples_3, tc)
4460*0a6a1f1dSLionel Sambuc {
4461*0a6a1f1dSLionel Sambuc 	/*
4462*0a6a1f1dSLionel Sambuc 	 * This filter returns only TCP finger packets.
4463*0a6a1f1dSLionel Sambuc 	 */
4464*0a6a1f1dSLionel Sambuc 	struct bpf_insn insns[] = {
4465*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
4466*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x0800, 0, 10),
4467*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 23),
4468*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 6, 0, 8),
4469*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 20),
4470*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JSET+BPF_K, 0x1fff, 6, 0),
4471*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_B+BPF_MSH, 14),
4472*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_IND, 14),
4473*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 2, 0),
4474*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_H+BPF_IND, 16),
4475*0a6a1f1dSLionel Sambuc 		BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 79, 0, 1),
4476*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, UINT32_MAX),
4477*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 0),
4478*0a6a1f1dSLionel Sambuc 	};
4479*0a6a1f1dSLionel Sambuc 
4480*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
4481*0a6a1f1dSLionel Sambuc 	uint8_t pkt[30] = {};
4482*0a6a1f1dSLionel Sambuc 
4483*0a6a1f1dSLionel Sambuc 	/* Set IP fragment offset to non-zero. */
4484*0a6a1f1dSLionel Sambuc 	pkt[20] = 1; pkt[21] = 1;
4485*0a6a1f1dSLionel Sambuc 
4486*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4487*0a6a1f1dSLionel Sambuc 
4488*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpf_validate(insns, insn_count));
4489*0a6a1f1dSLionel Sambuc 
4490*0a6a1f1dSLionel Sambuc 	code = bpfjit_generate_code(NULL, insns, insn_count);
4491*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
4492*0a6a1f1dSLionel Sambuc 
4493*0a6a1f1dSLionel Sambuc 	/* Packet is too short. */
4494*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4495*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4496*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4497*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4498*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4499*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4500*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4501*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4502*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4503*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4504*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4505*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4506*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4507*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4508*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4509*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4510*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4511*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4512*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4513*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4514*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4515*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4516*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 23, 23) == 0);
4517*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 24, 24) == 0);
4518*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 25, 25) == 0);
4519*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 26, 26) == 0);
4520*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 27, 27) == 0);
4521*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 28, 28) == 0);
4522*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 29, 29) == 0);
4523*0a6a1f1dSLionel Sambuc 
4524*0a6a1f1dSLionel Sambuc 	/* The packet doesn't match. */
4525*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4526*0a6a1f1dSLionel Sambuc 
4527*0a6a1f1dSLionel Sambuc 	/* Still no match after setting the protocol field. */
4528*0a6a1f1dSLionel Sambuc 	pkt[12] = 8;
4529*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4530*0a6a1f1dSLionel Sambuc 
4531*0a6a1f1dSLionel Sambuc 	/* Get one step closer to the match. */
4532*0a6a1f1dSLionel Sambuc 	pkt[23] = 6;
4533*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4534*0a6a1f1dSLionel Sambuc 
4535*0a6a1f1dSLionel Sambuc 	/* Set IP fragment offset to zero. */
4536*0a6a1f1dSLionel Sambuc 	pkt[20] = 0x20; pkt[21] = 0;
4537*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4538*0a6a1f1dSLionel Sambuc 
4539*0a6a1f1dSLionel Sambuc 	/* Set IP header length to 12. */
4540*0a6a1f1dSLionel Sambuc 	pkt[14] = 0xd3;
4541*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4542*0a6a1f1dSLionel Sambuc 
4543*0a6a1f1dSLionel Sambuc 	/* Match one branch of the program. */
4544*0a6a1f1dSLionel Sambuc 	pkt[27] = 79;
4545*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 30, 30) == UINT32_MAX);
4546*0a6a1f1dSLionel Sambuc 
4547*0a6a1f1dSLionel Sambuc 	/* Match the other branch of the program. */
4548*0a6a1f1dSLionel Sambuc 	pkt[29] = 79; pkt[27] = 0;
4549*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 30, 30) == UINT32_MAX);
4550*0a6a1f1dSLionel Sambuc 
4551*0a6a1f1dSLionel Sambuc 	/* Packet is too short. */
4552*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 1, 1) == 0);
4553*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 2, 2) == 0);
4554*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 3, 3) == 0);
4555*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 4, 4) == 0);
4556*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 5, 5) == 0);
4557*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 6, 6) == 0);
4558*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 7, 7) == 0);
4559*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 8, 8) == 0);
4560*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 9, 9) == 0);
4561*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 10, 10) == 0);
4562*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 11, 11) == 0);
4563*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 12, 12) == 0);
4564*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 13, 13) == 0);
4565*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 14, 14) == 0);
4566*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 15, 15) == 0);
4567*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 16, 16) == 0);
4568*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 17, 17) == 0);
4569*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 18, 18) == 0);
4570*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 19, 19) == 0);
4571*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 20, 20) == 0);
4572*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 21, 21) == 0);
4573*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 22, 22) == 0);
4574*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 23, 23) == 0);
4575*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 24, 24) == 0);
4576*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 25, 25) == 0);
4577*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 26, 26) == 0);
4578*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 27, 27) == 0);
4579*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 28, 28) == 0);
4580*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 29, 29) == 0);
4581*0a6a1f1dSLionel Sambuc 
4582*0a6a1f1dSLionel Sambuc 	/* Set IP header length to 16. Packet is too short. */
4583*0a6a1f1dSLionel Sambuc 	pkt[14] = 4;
4584*0a6a1f1dSLionel Sambuc 	ATF_CHECK(jitcall(code, pkt, 30, 30) == 0);
4585*0a6a1f1dSLionel Sambuc 
4586*0a6a1f1dSLionel Sambuc 	bpfjit_free_code(code);
4587*0a6a1f1dSLionel Sambuc }
4588*0a6a1f1dSLionel Sambuc 
4589*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_cop_no_ctx);
ATF_TC_HEAD(libbpfjit_cop_no_ctx,tc)4590*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_cop_no_ctx, tc)
4591*0a6a1f1dSLionel Sambuc {
4592*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test that BPF_MISC|BPF_COP "
4593*0a6a1f1dSLionel Sambuc 	    "instruction can't be accepted without a context");
4594*0a6a1f1dSLionel Sambuc }
4595*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_cop_no_ctx,tc)4596*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_cop_no_ctx, tc)
4597*0a6a1f1dSLionel Sambuc {
4598*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
4599*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_MISC+BPF_COP, 0),
4600*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7)
4601*0a6a1f1dSLionel Sambuc 	};
4602*0a6a1f1dSLionel Sambuc 
4603*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4604*0a6a1f1dSLionel Sambuc 
4605*0a6a1f1dSLionel Sambuc 	ATF_CHECK(!bpf_validate(insns, insn_count));
4606*0a6a1f1dSLionel Sambuc 
4607*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpfjit_generate_code(NULL, insns, insn_count) == NULL);
4608*0a6a1f1dSLionel Sambuc }
4609*0a6a1f1dSLionel Sambuc 
4610*0a6a1f1dSLionel Sambuc ATF_TC(libbpfjit_copx_no_ctx);
ATF_TC_HEAD(libbpfjit_copx_no_ctx,tc)4611*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(libbpfjit_copx_no_ctx, tc)
4612*0a6a1f1dSLionel Sambuc {
4613*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test that BPF_MISC|BPF_COPX "
4614*0a6a1f1dSLionel Sambuc 	    "instruction can't be accepted without a context");
4615*0a6a1f1dSLionel Sambuc }
4616*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(libbpfjit_copx_no_ctx,tc)4617*0a6a1f1dSLionel Sambuc ATF_TC_BODY(libbpfjit_copx_no_ctx, tc)
4618*0a6a1f1dSLionel Sambuc {
4619*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
4620*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_MISC+BPF_COPX, 0),
4621*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_K, 7)
4622*0a6a1f1dSLionel Sambuc 	};
4623*0a6a1f1dSLionel Sambuc 
4624*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
4625*0a6a1f1dSLionel Sambuc 
4626*0a6a1f1dSLionel Sambuc 	ATF_CHECK(!bpf_validate(insns, insn_count));
4627*0a6a1f1dSLionel Sambuc 
4628*0a6a1f1dSLionel Sambuc 	ATF_CHECK(bpfjit_generate_code(NULL, insns, insn_count) == NULL);
4629*0a6a1f1dSLionel Sambuc }
4630*0a6a1f1dSLionel Sambuc 
ATF_TP_ADD_TCS(tp)463111be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
463211be35a1SLionel Sambuc {
463311be35a1SLionel Sambuc 
4634*0a6a1f1dSLionel Sambuc 	/*
4635*0a6a1f1dSLionel Sambuc 	 * For every new test please also add a similar test
4636*0a6a1f1dSLionel Sambuc 	 * to ../../net/bpfjit/t_bpfjit.c
4637*0a6a1f1dSLionel Sambuc 	 */
4638*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_empty);
4639*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ret_k);
4640*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_bad_ret_k);
4641*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_add_k);
4642*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_sub_k);
4643*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mul_k);
4644*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div0_k);
4645*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div1_k);
4646*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div2_k);
4647*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div4_k);
4648*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div10_k);
4649*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div10000_k);
4650*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div7609801_k);
4651*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div80000000_k);
4652*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod0_k);
4653*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod1_k);
4654*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod2_k);
4655*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod4_k);
4656*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod10_k);
4657*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod10000_k);
4658*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod7609801_k);
4659*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod80000000_k);
4660*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_and_k);
4661*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_or_k);
4662*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_xor_k);
4663*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_lsh_k);
4664*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_lsh0_k);
4665*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_rsh_k);
4666*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_rsh0_k);
4667*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_modulo_k);
4668*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_add_x);
4669*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_sub_x);
4670*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mul_x);
4671*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div0_x);
4672*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div1_x);
4673*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div2_x);
4674*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div4_x);
4675*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div10_x);
4676*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div10000_x);
4677*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div7609801_x);
4678*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_div80000000_x);
4679*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod0_x);
4680*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod1_x);
4681*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod2_x);
4682*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod4_x);
4683*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod10_x);
4684*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod10000_x);
4685*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod7609801_x);
4686*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_mod80000000_x);
4687*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_and_x);
4688*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_or_x);
4689*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_xor_x);
4690*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_lsh_x);
4691*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_lsh0_x);
4692*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_rsh_x);
4693*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_rsh0_x);
4694*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_modulo_x);
4695*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_alu_neg);
4696*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_ja);
4697*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_ja_invalid);
4698*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_ja_overflow);
4699*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_jgt_k);
4700*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_jge_k);
4701*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_jeq_k);
4702*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_jset_k);
4703*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_modulo_k);
4704*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_jgt_x);
4705*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_jge_x);
4706*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_jeq_x);
4707*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_jset_x);
4708*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_jeq_x_noinit_ax);
4709*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_jeq_x_noinit_a);
4710*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_jeq_x_noinit_x);
4711*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_jmp_modulo_x);
4712*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ld_abs);
4713*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ld_abs_k_overflow);
4714*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ld_ind);
4715*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ld_ind_k_overflow);
4716*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ld_ind_x_overflow1);
4717*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ld_ind_x_overflow2);
4718*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ld_len);
4719*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ld_imm);
4720*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ldx_imm1);
4721*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ldx_imm2);
4722*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ldx_len1);
4723*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ldx_len2);
4724*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_ldx_msh);
4725*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_misc_tax);
4726*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_misc_txa);
4727*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_st1);
4728*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_st2);
4729*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_st3);
4730*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_st4);
4731*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_st5);
4732*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_stx1);
4733*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_stx2);
4734*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_stx3);
4735*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_stx4);
4736*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_opt_ld_abs_1);
4737*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_opt_ld_abs_2);
4738*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_opt_ld_abs_3);
4739*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_opt_ld_ind_1);
4740*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_opt_ld_ind_2);
4741*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_opt_ld_ind_3);
4742*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_opt_ld_ind_4);
4743*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_abc_ja);
4744*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_abc_ja_over);
4745*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_abc_ld_chain);
4746*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_examples_1);
4747*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_examples_2);
4748*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_examples_3);
4749*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_cop_no_ctx);
4750*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, libbpfjit_copx_no_ctx);
475111be35a1SLionel Sambuc 
475211be35a1SLionel Sambuc 	return atf_no_error();
475311be35a1SLionel Sambuc }
4754