xref: /minix3/tests/net/bpfjit/t_extmem.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: t_extmem.c,v 1.1 2014/07/09 13:49:49 alnsn Exp $ */
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2014 Alexander Nasonov.
5*0a6a1f1dSLionel Sambuc  * All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc  * are met:
10*0a6a1f1dSLionel Sambuc  *
11*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
12*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
13*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
14*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
15*0a6a1f1dSLionel Sambuc  *    the documentation and/or other materials provided with the
16*0a6a1f1dSLionel Sambuc  *    distribution.
17*0a6a1f1dSLionel Sambuc  *
18*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*0a6a1f1dSLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*0a6a1f1dSLionel Sambuc  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21*0a6a1f1dSLionel Sambuc  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22*0a6a1f1dSLionel Sambuc  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23*0a6a1f1dSLionel Sambuc  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24*0a6a1f1dSLionel Sambuc  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*0a6a1f1dSLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26*0a6a1f1dSLionel Sambuc  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27*0a6a1f1dSLionel Sambuc  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28*0a6a1f1dSLionel Sambuc  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*0a6a1f1dSLionel Sambuc  * SUCH DAMAGE.
30*0a6a1f1dSLionel Sambuc  */
31*0a6a1f1dSLionel Sambuc 
32*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
33*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_extmem.c,v 1.1 2014/07/09 13:49:49 alnsn Exp $");
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc #include <stdint.h>
36*0a6a1f1dSLionel Sambuc #include <string.h>
37*0a6a1f1dSLionel Sambuc 
38*0a6a1f1dSLionel Sambuc #define __BPF_PRIVATE
39*0a6a1f1dSLionel Sambuc #include <net/bpf.h>
40*0a6a1f1dSLionel Sambuc #include <net/bpfjit.h>
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc #include "../../net/bpf/h_bpf.h"
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc /* XXX: atf-c.h has collisions with mbuf */
45*0a6a1f1dSLionel Sambuc #undef m_type
46*0a6a1f1dSLionel Sambuc #undef m_data
47*0a6a1f1dSLionel Sambuc #include <atf-c.h>
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc #include "../../h_macros.h"
50*0a6a1f1dSLionel Sambuc 
51*0a6a1f1dSLionel Sambuc static uint32_t retM(const bpf_ctx_t *bc, bpf_args_t *args, uint32_t A);
52*0a6a1f1dSLionel Sambuc 
53*0a6a1f1dSLionel Sambuc static const bpf_copfunc_t copfuncs[] = {
54*0a6a1f1dSLionel Sambuc 	&retM
55*0a6a1f1dSLionel Sambuc };
56*0a6a1f1dSLionel Sambuc 
57*0a6a1f1dSLionel Sambuc static const bpf_ctx_t ctx = {
58*0a6a1f1dSLionel Sambuc 	.copfuncs = copfuncs,
59*0a6a1f1dSLionel Sambuc 	.nfuncs = sizeof(copfuncs) / sizeof(copfuncs[0]),
60*0a6a1f1dSLionel Sambuc 	.extwords = 4,
61*0a6a1f1dSLionel Sambuc 	.preinited = BPF_MEMWORD_INIT(0) | BPF_MEMWORD_INIT(3),
62*0a6a1f1dSLionel Sambuc };
63*0a6a1f1dSLionel Sambuc 
64*0a6a1f1dSLionel Sambuc static uint32_t
retM(const bpf_ctx_t * bc,bpf_args_t * args,uint32_t A)65*0a6a1f1dSLionel Sambuc retM(const bpf_ctx_t *bc, bpf_args_t *args, uint32_t A)
66*0a6a1f1dSLionel Sambuc {
67*0a6a1f1dSLionel Sambuc 
68*0a6a1f1dSLionel Sambuc 	return args->mem[(uintptr_t)args->arg];
69*0a6a1f1dSLionel Sambuc }
70*0a6a1f1dSLionel Sambuc 
71*0a6a1f1dSLionel Sambuc 
72*0a6a1f1dSLionel Sambuc ATF_TC(bpfjit_extmem_load_preinited);
ATF_TC_HEAD(bpfjit_extmem_load_preinited,tc)73*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(bpfjit_extmem_load_preinited, tc)
74*0a6a1f1dSLionel Sambuc {
75*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test a load of external "
76*0a6a1f1dSLionel Sambuc 	    "pre-initialized memory");
77*0a6a1f1dSLionel Sambuc }
78*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(bpfjit_extmem_load_preinited,tc)79*0a6a1f1dSLionel Sambuc ATF_TC_BODY(bpfjit_extmem_load_preinited, tc)
80*0a6a1f1dSLionel Sambuc {
81*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
82*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_MEM, 3),
83*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
84*0a6a1f1dSLionel Sambuc 	};
85*0a6a1f1dSLionel Sambuc 
86*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
87*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1] = { 0 };
88*0a6a1f1dSLionel Sambuc 	uint32_t mem[ctx.extwords];
89*0a6a1f1dSLionel Sambuc 
90*0a6a1f1dSLionel Sambuc 	/* Pre-inited words. */
91*0a6a1f1dSLionel Sambuc 	mem[0] = 0;
92*0a6a1f1dSLionel Sambuc 	mem[3] = 3;
93*0a6a1f1dSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc 	bpf_args_t args = {
95*0a6a1f1dSLionel Sambuc 		.pkt = pkt,
96*0a6a1f1dSLionel Sambuc 		.buflen = sizeof(pkt),
97*0a6a1f1dSLionel Sambuc 		.wirelen = sizeof(pkt),
98*0a6a1f1dSLionel Sambuc 		.mem = mem,
99*0a6a1f1dSLionel Sambuc 	};
100*0a6a1f1dSLionel Sambuc 
101*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
102*0a6a1f1dSLionel Sambuc 
103*0a6a1f1dSLionel Sambuc 	RZ(rump_init());
104*0a6a1f1dSLionel Sambuc 
105*0a6a1f1dSLionel Sambuc 	rump_schedule();
106*0a6a1f1dSLionel Sambuc 	code = rumpns_bpfjit_generate_code(&ctx, insns, insn_count);
107*0a6a1f1dSLionel Sambuc 	rump_unschedule();
108*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
109*0a6a1f1dSLionel Sambuc 
110*0a6a1f1dSLionel Sambuc 	ATF_CHECK(code(&ctx, &args) == 3);
111*0a6a1f1dSLionel Sambuc 
112*0a6a1f1dSLionel Sambuc 	rump_schedule();
113*0a6a1f1dSLionel Sambuc 	rumpns_bpfjit_free_code(code);
114*0a6a1f1dSLionel Sambuc 	rump_unschedule();
115*0a6a1f1dSLionel Sambuc }
116*0a6a1f1dSLionel Sambuc 
117*0a6a1f1dSLionel Sambuc ATF_TC(bpfjit_extmem_invalid_load);
ATF_TC_HEAD(bpfjit_extmem_invalid_load,tc)118*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(bpfjit_extmem_invalid_load, tc)
119*0a6a1f1dSLionel Sambuc {
120*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test that out-of-range load "
121*0a6a1f1dSLionel Sambuc 	    "fails validation");
122*0a6a1f1dSLionel Sambuc }
123*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(bpfjit_extmem_invalid_load,tc)124*0a6a1f1dSLionel Sambuc ATF_TC_BODY(bpfjit_extmem_invalid_load, tc)
125*0a6a1f1dSLionel Sambuc {
126*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
127*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_MEM, 4),
128*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
129*0a6a1f1dSLionel Sambuc 	};
130*0a6a1f1dSLionel Sambuc 
131*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
132*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
133*0a6a1f1dSLionel Sambuc 
134*0a6a1f1dSLionel Sambuc 	RZ(rump_init());
135*0a6a1f1dSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc 	rump_schedule();
137*0a6a1f1dSLionel Sambuc 	code = rumpns_bpfjit_generate_code(&ctx, insns, insn_count);
138*0a6a1f1dSLionel Sambuc 	rump_unschedule();
139*0a6a1f1dSLionel Sambuc 	ATF_CHECK(code == NULL);
140*0a6a1f1dSLionel Sambuc }
141*0a6a1f1dSLionel Sambuc 
142*0a6a1f1dSLionel Sambuc ATF_TC(bpfjit_extmem_store);
ATF_TC_HEAD(bpfjit_extmem_store,tc)143*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(bpfjit_extmem_store, tc)
144*0a6a1f1dSLionel Sambuc {
145*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test stores to external memory");
146*0a6a1f1dSLionel Sambuc }
147*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(bpfjit_extmem_store,tc)148*0a6a1f1dSLionel Sambuc ATF_TC_BODY(bpfjit_extmem_store, tc)
149*0a6a1f1dSLionel Sambuc {
150*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
151*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 1),        /* A <- 1     */
152*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2), /* X <- 2     */
153*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 1),                /* M[1] <- A  */
154*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */
155*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_STX, 2),               /* M[2] <- X  */
156*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 3),                /* M[3] <- A  */
157*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)          /* ret A      */
158*0a6a1f1dSLionel Sambuc 	};
159*0a6a1f1dSLionel Sambuc 
160*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
161*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1] = { 0 };
162*0a6a1f1dSLionel Sambuc 	uint32_t mem[ctx.extwords];
163*0a6a1f1dSLionel Sambuc 
164*0a6a1f1dSLionel Sambuc 	/* Pre-inited words. */
165*0a6a1f1dSLionel Sambuc 	mem[0] = 0;
166*0a6a1f1dSLionel Sambuc 	mem[3] = 7;
167*0a6a1f1dSLionel Sambuc 
168*0a6a1f1dSLionel Sambuc 	mem[1] = mem[2] = 0xdeadbeef;
169*0a6a1f1dSLionel Sambuc 
170*0a6a1f1dSLionel Sambuc 	bpf_args_t args = {
171*0a6a1f1dSLionel Sambuc 		.pkt = pkt,
172*0a6a1f1dSLionel Sambuc 		.buflen = sizeof(pkt),
173*0a6a1f1dSLionel Sambuc 		.wirelen = sizeof(pkt),
174*0a6a1f1dSLionel Sambuc 		.mem = mem,
175*0a6a1f1dSLionel Sambuc 	};
176*0a6a1f1dSLionel Sambuc 
177*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
178*0a6a1f1dSLionel Sambuc 
179*0a6a1f1dSLionel Sambuc 	RZ(rump_init());
180*0a6a1f1dSLionel Sambuc 
181*0a6a1f1dSLionel Sambuc 	rump_schedule();
182*0a6a1f1dSLionel Sambuc 	code = rumpns_bpfjit_generate_code(&ctx, insns, insn_count);
183*0a6a1f1dSLionel Sambuc 	rump_unschedule();
184*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
185*0a6a1f1dSLionel Sambuc 
186*0a6a1f1dSLionel Sambuc 	ATF_CHECK(code(&ctx, &args) == 3);
187*0a6a1f1dSLionel Sambuc 
188*0a6a1f1dSLionel Sambuc 	rump_schedule();
189*0a6a1f1dSLionel Sambuc 	rumpns_bpfjit_free_code(code);
190*0a6a1f1dSLionel Sambuc 	rump_unschedule();
191*0a6a1f1dSLionel Sambuc 
192*0a6a1f1dSLionel Sambuc 	ATF_CHECK(mem[0] == 0);
193*0a6a1f1dSLionel Sambuc 	ATF_CHECK(mem[1] == 1);
194*0a6a1f1dSLionel Sambuc 	ATF_CHECK(mem[2] == 2);
195*0a6a1f1dSLionel Sambuc 	ATF_CHECK(mem[3] == 3);
196*0a6a1f1dSLionel Sambuc }
197*0a6a1f1dSLionel Sambuc 
198*0a6a1f1dSLionel Sambuc ATF_TC(bpfjit_extmem_side_effect);
ATF_TC_HEAD(bpfjit_extmem_side_effect,tc)199*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(bpfjit_extmem_side_effect, tc)
200*0a6a1f1dSLionel Sambuc {
201*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test that ABC optimization doesn\'t "
202*0a6a1f1dSLionel Sambuc 	    "skip stores to external memory");
203*0a6a1f1dSLionel Sambuc }
204*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(bpfjit_extmem_side_effect,tc)205*0a6a1f1dSLionel Sambuc ATF_TC_BODY(bpfjit_extmem_side_effect, tc)
206*0a6a1f1dSLionel Sambuc {
207*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
208*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 0),  /* A <- P[0]  */
209*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_W+BPF_IMM, 2), /* X <- 2     */
210*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 1),                /* M[1] <- A  */
211*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- A + X */
212*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_STX, 2),               /* M[2] <- X  */
213*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 3),                /* M[3] <- A  */
214*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 99), /* A <- P[99] */
215*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)          /* ret A      */
216*0a6a1f1dSLionel Sambuc 	};
217*0a6a1f1dSLionel Sambuc 
218*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
219*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1] = { 1 };
220*0a6a1f1dSLionel Sambuc 	uint32_t mem[ctx.extwords];
221*0a6a1f1dSLionel Sambuc 
222*0a6a1f1dSLionel Sambuc 	/* Pre-inited words. */
223*0a6a1f1dSLionel Sambuc 	mem[0] = 0;
224*0a6a1f1dSLionel Sambuc 	mem[3] = 7;
225*0a6a1f1dSLionel Sambuc 
226*0a6a1f1dSLionel Sambuc 	mem[1] = mem[2] = 0xdeadbeef;
227*0a6a1f1dSLionel Sambuc 
228*0a6a1f1dSLionel Sambuc 	bpf_args_t args = {
229*0a6a1f1dSLionel Sambuc 		.pkt = pkt,
230*0a6a1f1dSLionel Sambuc 		.buflen = sizeof(pkt),
231*0a6a1f1dSLionel Sambuc 		.wirelen = sizeof(pkt),
232*0a6a1f1dSLionel Sambuc 		.mem = mem,
233*0a6a1f1dSLionel Sambuc 	};
234*0a6a1f1dSLionel Sambuc 
235*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
236*0a6a1f1dSLionel Sambuc 
237*0a6a1f1dSLionel Sambuc 	RZ(rump_init());
238*0a6a1f1dSLionel Sambuc 
239*0a6a1f1dSLionel Sambuc 	rump_schedule();
240*0a6a1f1dSLionel Sambuc 	code = rumpns_bpfjit_generate_code(&ctx, insns, insn_count);
241*0a6a1f1dSLionel Sambuc 	rump_unschedule();
242*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
243*0a6a1f1dSLionel Sambuc 
244*0a6a1f1dSLionel Sambuc 	ATF_CHECK(code(&ctx, &args) == 0);
245*0a6a1f1dSLionel Sambuc 
246*0a6a1f1dSLionel Sambuc 	rump_schedule();
247*0a6a1f1dSLionel Sambuc 	rumpns_bpfjit_free_code(code);
248*0a6a1f1dSLionel Sambuc 	rump_unschedule();
249*0a6a1f1dSLionel Sambuc 
250*0a6a1f1dSLionel Sambuc 	ATF_CHECK(mem[0] == 0);
251*0a6a1f1dSLionel Sambuc 	ATF_CHECK(mem[1] == 1);
252*0a6a1f1dSLionel Sambuc 	ATF_CHECK(mem[2] == 2);
253*0a6a1f1dSLionel Sambuc 	ATF_CHECK(mem[3] == 3);
254*0a6a1f1dSLionel Sambuc }
255*0a6a1f1dSLionel Sambuc 
256*0a6a1f1dSLionel Sambuc ATF_TC(bpfjit_extmem_invalid_store);
ATF_TC_HEAD(bpfjit_extmem_invalid_store,tc)257*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(bpfjit_extmem_invalid_store, tc)
258*0a6a1f1dSLionel Sambuc {
259*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test that out-of-range store "
260*0a6a1f1dSLionel Sambuc 	    "fails validation");
261*0a6a1f1dSLionel Sambuc }
262*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(bpfjit_extmem_invalid_store,tc)263*0a6a1f1dSLionel Sambuc ATF_TC_BODY(bpfjit_extmem_invalid_store, tc)
264*0a6a1f1dSLionel Sambuc {
265*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
266*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 4),
267*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
268*0a6a1f1dSLionel Sambuc 	};
269*0a6a1f1dSLionel Sambuc 
270*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
271*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
272*0a6a1f1dSLionel Sambuc 
273*0a6a1f1dSLionel Sambuc 	RZ(rump_init());
274*0a6a1f1dSLionel Sambuc 
275*0a6a1f1dSLionel Sambuc 	rump_schedule();
276*0a6a1f1dSLionel Sambuc 	code = rumpns_bpfjit_generate_code(&ctx, insns, insn_count);
277*0a6a1f1dSLionel Sambuc 	rump_unschedule();
278*0a6a1f1dSLionel Sambuc 	ATF_CHECK(code == NULL);
279*0a6a1f1dSLionel Sambuc }
280*0a6a1f1dSLionel Sambuc 
281*0a6a1f1dSLionel Sambuc ATF_TC(bpfjit_cop_ret_mem);
ATF_TC_HEAD(bpfjit_cop_ret_mem,tc)282*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(bpfjit_cop_ret_mem, tc)
283*0a6a1f1dSLionel Sambuc {
284*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test coprocessor function "
285*0a6a1f1dSLionel Sambuc 	    "that returns a content of external memory word");
286*0a6a1f1dSLionel Sambuc }
287*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(bpfjit_cop_ret_mem,tc)288*0a6a1f1dSLionel Sambuc ATF_TC_BODY(bpfjit_cop_ret_mem, tc)
289*0a6a1f1dSLionel Sambuc {
290*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
291*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 13),
292*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 2),
293*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 137),
294*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 1),
295*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_MISC+BPF_COP, 0), // retM
296*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
297*0a6a1f1dSLionel Sambuc 	};
298*0a6a1f1dSLionel Sambuc 
299*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
300*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1] = { 0 };
301*0a6a1f1dSLionel Sambuc 	uint32_t mem[ctx.extwords];
302*0a6a1f1dSLionel Sambuc 	void *arg = (void*)(uintptr_t)2;
303*0a6a1f1dSLionel Sambuc 
304*0a6a1f1dSLionel Sambuc 	/* Pre-inited words. */
305*0a6a1f1dSLionel Sambuc 	mem[0] = 0;
306*0a6a1f1dSLionel Sambuc 	mem[3] = 3;
307*0a6a1f1dSLionel Sambuc 
308*0a6a1f1dSLionel Sambuc 	bpf_args_t args = {
309*0a6a1f1dSLionel Sambuc 		.pkt = pkt,
310*0a6a1f1dSLionel Sambuc 		.buflen = sizeof(pkt),
311*0a6a1f1dSLionel Sambuc 		.wirelen = sizeof(pkt),
312*0a6a1f1dSLionel Sambuc 		.arg = arg,
313*0a6a1f1dSLionel Sambuc 		.mem = mem,
314*0a6a1f1dSLionel Sambuc 	};
315*0a6a1f1dSLionel Sambuc 
316*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
317*0a6a1f1dSLionel Sambuc 
318*0a6a1f1dSLionel Sambuc 	RZ(rump_init());
319*0a6a1f1dSLionel Sambuc 
320*0a6a1f1dSLionel Sambuc 	rump_schedule();
321*0a6a1f1dSLionel Sambuc 	code = rumpns_bpfjit_generate_code(&ctx, insns, insn_count);
322*0a6a1f1dSLionel Sambuc 	rump_unschedule();
323*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
324*0a6a1f1dSLionel Sambuc 
325*0a6a1f1dSLionel Sambuc 	ATF_CHECK(code(&ctx, &args) == 13);
326*0a6a1f1dSLionel Sambuc 
327*0a6a1f1dSLionel Sambuc 	rump_schedule();
328*0a6a1f1dSLionel Sambuc 	rumpns_bpfjit_free_code(code);
329*0a6a1f1dSLionel Sambuc 	rump_unschedule();
330*0a6a1f1dSLionel Sambuc }
331*0a6a1f1dSLionel Sambuc 
332*0a6a1f1dSLionel Sambuc ATF_TC(bpfjit_cop_ret_preinited_mem);
ATF_TC_HEAD(bpfjit_cop_ret_preinited_mem,tc)333*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(bpfjit_cop_ret_preinited_mem, tc)
334*0a6a1f1dSLionel Sambuc {
335*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test coprocessor function that "
336*0a6a1f1dSLionel Sambuc 	    "returns a content of external pre-initialized memory word");
337*0a6a1f1dSLionel Sambuc }
338*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(bpfjit_cop_ret_preinited_mem,tc)339*0a6a1f1dSLionel Sambuc ATF_TC_BODY(bpfjit_cop_ret_preinited_mem, tc)
340*0a6a1f1dSLionel Sambuc {
341*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
342*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 13),
343*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 2),
344*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 137),
345*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 1),
346*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_MISC+BPF_COP, 0), // retM
347*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
348*0a6a1f1dSLionel Sambuc 	};
349*0a6a1f1dSLionel Sambuc 
350*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
351*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1] = { 0 };
352*0a6a1f1dSLionel Sambuc 	uint32_t mem[ctx.extwords];
353*0a6a1f1dSLionel Sambuc 	void *arg = (void*)(uintptr_t)3;
354*0a6a1f1dSLionel Sambuc 
355*0a6a1f1dSLionel Sambuc 	/* Pre-inited words. */
356*0a6a1f1dSLionel Sambuc 	mem[0] = 0;
357*0a6a1f1dSLionel Sambuc 	mem[3] = 3;
358*0a6a1f1dSLionel Sambuc 
359*0a6a1f1dSLionel Sambuc 	bpf_args_t args = {
360*0a6a1f1dSLionel Sambuc 		.pkt = pkt,
361*0a6a1f1dSLionel Sambuc 		.buflen = sizeof(pkt),
362*0a6a1f1dSLionel Sambuc 		.wirelen = sizeof(pkt),
363*0a6a1f1dSLionel Sambuc 		.arg = arg,
364*0a6a1f1dSLionel Sambuc 		.mem = mem,
365*0a6a1f1dSLionel Sambuc 	};
366*0a6a1f1dSLionel Sambuc 
367*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
368*0a6a1f1dSLionel Sambuc 
369*0a6a1f1dSLionel Sambuc 	RZ(rump_init());
370*0a6a1f1dSLionel Sambuc 
371*0a6a1f1dSLionel Sambuc 	rump_schedule();
372*0a6a1f1dSLionel Sambuc 	code = rumpns_bpfjit_generate_code(&ctx, insns, insn_count);
373*0a6a1f1dSLionel Sambuc 	rump_unschedule();
374*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
375*0a6a1f1dSLionel Sambuc 
376*0a6a1f1dSLionel Sambuc 	ATF_CHECK(code(&ctx, &args) == 3);
377*0a6a1f1dSLionel Sambuc 
378*0a6a1f1dSLionel Sambuc 	rump_schedule();
379*0a6a1f1dSLionel Sambuc 	rumpns_bpfjit_free_code(code);
380*0a6a1f1dSLionel Sambuc 	rump_unschedule();
381*0a6a1f1dSLionel Sambuc }
382*0a6a1f1dSLionel Sambuc 
383*0a6a1f1dSLionel Sambuc ATF_TC(bpfjit_copx_ret_mem);
ATF_TC_HEAD(bpfjit_copx_ret_mem,tc)384*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(bpfjit_copx_ret_mem, tc)
385*0a6a1f1dSLionel Sambuc {
386*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test coprocessor function "
387*0a6a1f1dSLionel Sambuc 	    "that returns a content of external memory word");
388*0a6a1f1dSLionel Sambuc }
389*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(bpfjit_copx_ret_mem,tc)390*0a6a1f1dSLionel Sambuc ATF_TC_BODY(bpfjit_copx_ret_mem, tc)
391*0a6a1f1dSLionel Sambuc {
392*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
393*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 13),
394*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 2),
395*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 137),
396*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 1),
397*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_IMM, 0), // retM
398*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_MISC+BPF_COPX, 0),
399*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
400*0a6a1f1dSLionel Sambuc 	};
401*0a6a1f1dSLionel Sambuc 
402*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
403*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1] = { 0 };
404*0a6a1f1dSLionel Sambuc 	uint32_t mem[ctx.extwords];
405*0a6a1f1dSLionel Sambuc 	void *arg = (void*)(uintptr_t)2;
406*0a6a1f1dSLionel Sambuc 
407*0a6a1f1dSLionel Sambuc 	/* Pre-inited words. */
408*0a6a1f1dSLionel Sambuc 	mem[0] = 0;
409*0a6a1f1dSLionel Sambuc 	mem[3] = 3;
410*0a6a1f1dSLionel Sambuc 
411*0a6a1f1dSLionel Sambuc 	bpf_args_t args = {
412*0a6a1f1dSLionel Sambuc 		.pkt = pkt,
413*0a6a1f1dSLionel Sambuc 		.buflen = sizeof(pkt),
414*0a6a1f1dSLionel Sambuc 		.wirelen = sizeof(pkt),
415*0a6a1f1dSLionel Sambuc 		.arg = arg,
416*0a6a1f1dSLionel Sambuc 		.mem = mem,
417*0a6a1f1dSLionel Sambuc 	};
418*0a6a1f1dSLionel Sambuc 
419*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
420*0a6a1f1dSLionel Sambuc 
421*0a6a1f1dSLionel Sambuc 	RZ(rump_init());
422*0a6a1f1dSLionel Sambuc 
423*0a6a1f1dSLionel Sambuc 	rump_schedule();
424*0a6a1f1dSLionel Sambuc 	code = rumpns_bpfjit_generate_code(&ctx, insns, insn_count);
425*0a6a1f1dSLionel Sambuc 	rump_unschedule();
426*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
427*0a6a1f1dSLionel Sambuc 
428*0a6a1f1dSLionel Sambuc 	ATF_CHECK(code(&ctx, &args) == 13);
429*0a6a1f1dSLionel Sambuc 
430*0a6a1f1dSLionel Sambuc 	rump_schedule();
431*0a6a1f1dSLionel Sambuc 	rumpns_bpfjit_free_code(code);
432*0a6a1f1dSLionel Sambuc 	rump_unschedule();
433*0a6a1f1dSLionel Sambuc }
434*0a6a1f1dSLionel Sambuc 
435*0a6a1f1dSLionel Sambuc ATF_TC(bpfjit_copx_ret_preinited_mem);
ATF_TC_HEAD(bpfjit_copx_ret_preinited_mem,tc)436*0a6a1f1dSLionel Sambuc ATF_TC_HEAD(bpfjit_copx_ret_preinited_mem, tc)
437*0a6a1f1dSLionel Sambuc {
438*0a6a1f1dSLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test coprocessor function that "
439*0a6a1f1dSLionel Sambuc 	    "returns a content of external pre-initialized memory word");
440*0a6a1f1dSLionel Sambuc }
441*0a6a1f1dSLionel Sambuc 
ATF_TC_BODY(bpfjit_copx_ret_preinited_mem,tc)442*0a6a1f1dSLionel Sambuc ATF_TC_BODY(bpfjit_copx_ret_preinited_mem, tc)
443*0a6a1f1dSLionel Sambuc {
444*0a6a1f1dSLionel Sambuc 	static struct bpf_insn insns[] = {
445*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 13),
446*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 2),
447*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LD+BPF_IMM, 137),
448*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_ST, 1),
449*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_LDX+BPF_IMM, 0), // retM
450*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_MISC+BPF_COPX, 0),
451*0a6a1f1dSLionel Sambuc 		BPF_STMT(BPF_RET+BPF_A, 0)
452*0a6a1f1dSLionel Sambuc 	};
453*0a6a1f1dSLionel Sambuc 
454*0a6a1f1dSLionel Sambuc 	bpfjit_func_t code;
455*0a6a1f1dSLionel Sambuc 	uint8_t pkt[1] = { 0 };
456*0a6a1f1dSLionel Sambuc 	uint32_t mem[ctx.extwords];
457*0a6a1f1dSLionel Sambuc 	void *arg = (void*)(uintptr_t)3;
458*0a6a1f1dSLionel Sambuc 
459*0a6a1f1dSLionel Sambuc 	/* Pre-inited words. */
460*0a6a1f1dSLionel Sambuc 	mem[0] = 0;
461*0a6a1f1dSLionel Sambuc 	mem[3] = 3;
462*0a6a1f1dSLionel Sambuc 
463*0a6a1f1dSLionel Sambuc 	bpf_args_t args = {
464*0a6a1f1dSLionel Sambuc 		.pkt = pkt,
465*0a6a1f1dSLionel Sambuc 		.buflen = sizeof(pkt),
466*0a6a1f1dSLionel Sambuc 		.wirelen = sizeof(pkt),
467*0a6a1f1dSLionel Sambuc 		.arg = arg,
468*0a6a1f1dSLionel Sambuc 		.mem = mem,
469*0a6a1f1dSLionel Sambuc 	};
470*0a6a1f1dSLionel Sambuc 
471*0a6a1f1dSLionel Sambuc 	size_t insn_count = sizeof(insns) / sizeof(insns[0]);
472*0a6a1f1dSLionel Sambuc 
473*0a6a1f1dSLionel Sambuc 	RZ(rump_init());
474*0a6a1f1dSLionel Sambuc 
475*0a6a1f1dSLionel Sambuc 	rump_schedule();
476*0a6a1f1dSLionel Sambuc 	code = rumpns_bpfjit_generate_code(&ctx, insns, insn_count);
477*0a6a1f1dSLionel Sambuc 	rump_unschedule();
478*0a6a1f1dSLionel Sambuc 	ATF_REQUIRE(code != NULL);
479*0a6a1f1dSLionel Sambuc 
480*0a6a1f1dSLionel Sambuc 	ATF_CHECK(code(&ctx, &args) == 3);
481*0a6a1f1dSLionel Sambuc 
482*0a6a1f1dSLionel Sambuc 	rump_schedule();
483*0a6a1f1dSLionel Sambuc 	rumpns_bpfjit_free_code(code);
484*0a6a1f1dSLionel Sambuc 	rump_unschedule();
485*0a6a1f1dSLionel Sambuc }
486*0a6a1f1dSLionel Sambuc 
ATF_TP_ADD_TCS(tp)487*0a6a1f1dSLionel Sambuc ATF_TP_ADD_TCS(tp)
488*0a6a1f1dSLionel Sambuc {
489*0a6a1f1dSLionel Sambuc 
490*0a6a1f1dSLionel Sambuc 	/*
491*0a6a1f1dSLionel Sambuc 	 * For every new test please also add a similar test
492*0a6a1f1dSLionel Sambuc 	 * to ../../lib/libbpfjit/t_extmem.c
493*0a6a1f1dSLionel Sambuc 	 */
494*0a6a1f1dSLionel Sambuc 	//ATF_TP_ADD_TC(tp, bpfjit_extmem_load_default);
495*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, bpfjit_extmem_load_preinited);
496*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, bpfjit_extmem_invalid_load);
497*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, bpfjit_extmem_store);
498*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, bpfjit_extmem_side_effect);
499*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, bpfjit_extmem_invalid_store);
500*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, bpfjit_cop_ret_mem);
501*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, bpfjit_cop_ret_preinited_mem);
502*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, bpfjit_copx_ret_mem);
503*0a6a1f1dSLionel Sambuc 	ATF_TP_ADD_TC(tp, bpfjit_copx_ret_preinited_mem);
504*0a6a1f1dSLionel Sambuc 
505*0a6a1f1dSLionel Sambuc 	return atf_no_error();
506*0a6a1f1dSLionel Sambuc }
507