xref: /DragonOS/kernel/crates/rbpf/tests/ubpf_jit_x86_64.rs (revision fae6e9ade46a52976ad5d099643d51cc20876448)
1*fae6e9adSlinfeng // SPDX-License-Identifier: (Apache-2.0 OR MIT)
2*fae6e9adSlinfeng // Converted from the tests for uBPF <https://github.com/iovisor/ubpf>
3*fae6e9adSlinfeng // Copyright 2015 Big Switch Networks, Inc
4*fae6e9adSlinfeng // Copyright 2016 6WIND S.A. <quentin.monnet@6wind.com>
5*fae6e9adSlinfeng 
6*fae6e9adSlinfeng // The tests contained in this file are extracted from the unit tests of uBPF software. Each test
7*fae6e9adSlinfeng // in this file has a name in the form `test_jit_<name>`, and corresponds to the (human-readable)
8*fae6e9adSlinfeng // code in `ubpf/tree/master/tests/<name>`, available at
9*fae6e9adSlinfeng // <https://github.com/iovisor/ubpf/tree/master/tests> (hyphen had to be replaced with underscores
10*fae6e9adSlinfeng // as Rust will not accept them in function names). It is strongly advised to refer to the uBPF
11*fae6e9adSlinfeng // version to understand what these program do.
12*fae6e9adSlinfeng //
13*fae6e9adSlinfeng // Each program was assembled from the uBPF version with the assembler provided by uBPF itself, and
14*fae6e9adSlinfeng // available at <https://github.com/iovisor/ubpf/tree/master/ubpf>.
15*fae6e9adSlinfeng // The very few modifications that have been realized should be indicated.
16*fae6e9adSlinfeng 
17*fae6e9adSlinfeng // These are unit tests for the eBPF JIT compiler.
18*fae6e9adSlinfeng 
19*fae6e9adSlinfeng #![allow(clippy::unreadable_literal)]
20*fae6e9adSlinfeng #![cfg(all(not(windows), feature = "std"))]
21*fae6e9adSlinfeng 
22*fae6e9adSlinfeng extern crate rbpf;
23*fae6e9adSlinfeng mod common;
24*fae6e9adSlinfeng 
25*fae6e9adSlinfeng use common::{TCP_SACK_ASM, TCP_SACK_MATCH, TCP_SACK_NOMATCH};
26*fae6e9adSlinfeng use rbpf::{assembler::assemble, helpers};
27*fae6e9adSlinfeng 
28*fae6e9adSlinfeng #[test]
test_jit_add()29*fae6e9adSlinfeng fn test_jit_add() {
30*fae6e9adSlinfeng     let prog = assemble(
31*fae6e9adSlinfeng         "
32*fae6e9adSlinfeng         mov32 r0, 0
33*fae6e9adSlinfeng         mov32 r1, 2
34*fae6e9adSlinfeng         add32 r0, 1
35*fae6e9adSlinfeng         add32 r0, r1
36*fae6e9adSlinfeng         exit",
37*fae6e9adSlinfeng     )
38*fae6e9adSlinfeng     .unwrap();
39*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
40*fae6e9adSlinfeng     vm.jit_compile().unwrap();
41*fae6e9adSlinfeng     unsafe {
42*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
43*fae6e9adSlinfeng     }
44*fae6e9adSlinfeng }
45*fae6e9adSlinfeng 
46*fae6e9adSlinfeng #[test]
test_jit_alu64_arith()47*fae6e9adSlinfeng fn test_jit_alu64_arith() {
48*fae6e9adSlinfeng     let prog = assemble(
49*fae6e9adSlinfeng         "
50*fae6e9adSlinfeng         mov r0, 0
51*fae6e9adSlinfeng         mov r1, 1
52*fae6e9adSlinfeng         mov r2, 2
53*fae6e9adSlinfeng         mov r3, 3
54*fae6e9adSlinfeng         mov r4, 4
55*fae6e9adSlinfeng         mov r5, 5
56*fae6e9adSlinfeng         mov r6, 6
57*fae6e9adSlinfeng         mov r7, 7
58*fae6e9adSlinfeng         mov r8, 8
59*fae6e9adSlinfeng         mov r9, 9
60*fae6e9adSlinfeng         add r0, 23
61*fae6e9adSlinfeng         add r0, r7
62*fae6e9adSlinfeng         sub r0, 13
63*fae6e9adSlinfeng         sub r0, r1
64*fae6e9adSlinfeng         mul r0, 7
65*fae6e9adSlinfeng         mul r0, r3
66*fae6e9adSlinfeng         div r0, 2
67*fae6e9adSlinfeng         div r0, r4
68*fae6e9adSlinfeng         exit",
69*fae6e9adSlinfeng     )
70*fae6e9adSlinfeng     .unwrap();
71*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
72*fae6e9adSlinfeng     vm.jit_compile().unwrap();
73*fae6e9adSlinfeng     unsafe {
74*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x2a);
75*fae6e9adSlinfeng     }
76*fae6e9adSlinfeng }
77*fae6e9adSlinfeng 
78*fae6e9adSlinfeng #[test]
test_jit_alu64_bit()79*fae6e9adSlinfeng fn test_jit_alu64_bit() {
80*fae6e9adSlinfeng     let prog = assemble(
81*fae6e9adSlinfeng         "
82*fae6e9adSlinfeng         mov r0, 0
83*fae6e9adSlinfeng         mov r1, 1
84*fae6e9adSlinfeng         mov r2, 2
85*fae6e9adSlinfeng         mov r3, 3
86*fae6e9adSlinfeng         mov r4, 4
87*fae6e9adSlinfeng         mov r5, 5
88*fae6e9adSlinfeng         mov r6, 6
89*fae6e9adSlinfeng         mov r7, 7
90*fae6e9adSlinfeng         mov r8, 8
91*fae6e9adSlinfeng         or r0, r5
92*fae6e9adSlinfeng         or r0, 0xa0
93*fae6e9adSlinfeng         and r0, 0xa3
94*fae6e9adSlinfeng         mov r9, 0x91
95*fae6e9adSlinfeng         and r0, r9
96*fae6e9adSlinfeng         lsh r0, 32
97*fae6e9adSlinfeng         lsh r0, 22
98*fae6e9adSlinfeng         lsh r0, r8
99*fae6e9adSlinfeng         rsh r0, 32
100*fae6e9adSlinfeng         rsh r0, 19
101*fae6e9adSlinfeng         rsh r0, r7
102*fae6e9adSlinfeng         xor r0, 0x03
103*fae6e9adSlinfeng         xor r0, r2
104*fae6e9adSlinfeng         exit",
105*fae6e9adSlinfeng     )
106*fae6e9adSlinfeng     .unwrap();
107*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
108*fae6e9adSlinfeng     vm.jit_compile().unwrap();
109*fae6e9adSlinfeng     unsafe {
110*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x11);
111*fae6e9adSlinfeng     }
112*fae6e9adSlinfeng }
113*fae6e9adSlinfeng 
114*fae6e9adSlinfeng #[test]
test_jit_alu_arith()115*fae6e9adSlinfeng fn test_jit_alu_arith() {
116*fae6e9adSlinfeng     let prog = assemble(
117*fae6e9adSlinfeng         "
118*fae6e9adSlinfeng         mov32 r0, 0
119*fae6e9adSlinfeng         mov32 r1, 1
120*fae6e9adSlinfeng         mov32 r2, 2
121*fae6e9adSlinfeng         mov32 r3, 3
122*fae6e9adSlinfeng         mov32 r4, 4
123*fae6e9adSlinfeng         mov32 r5, 5
124*fae6e9adSlinfeng         mov32 r6, 6
125*fae6e9adSlinfeng         mov32 r7, 7
126*fae6e9adSlinfeng         mov32 r8, 8
127*fae6e9adSlinfeng         mov32 r9, 9
128*fae6e9adSlinfeng         add32 r0, 23
129*fae6e9adSlinfeng         add32 r0, r7
130*fae6e9adSlinfeng         sub32 r0, 13
131*fae6e9adSlinfeng         sub32 r0, r1
132*fae6e9adSlinfeng         mul32 r0, 7
133*fae6e9adSlinfeng         mul32 r0, r3
134*fae6e9adSlinfeng         div32 r0, 2
135*fae6e9adSlinfeng         div32 r0, r4
136*fae6e9adSlinfeng         exit",
137*fae6e9adSlinfeng     )
138*fae6e9adSlinfeng     .unwrap();
139*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
140*fae6e9adSlinfeng     vm.jit_compile().unwrap();
141*fae6e9adSlinfeng     unsafe {
142*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x2a);
143*fae6e9adSlinfeng     }
144*fae6e9adSlinfeng }
145*fae6e9adSlinfeng 
146*fae6e9adSlinfeng #[test]
test_jit_alu_bit()147*fae6e9adSlinfeng fn test_jit_alu_bit() {
148*fae6e9adSlinfeng     let prog = assemble(
149*fae6e9adSlinfeng         "
150*fae6e9adSlinfeng         mov32 r0, 0
151*fae6e9adSlinfeng         mov32 r1, 1
152*fae6e9adSlinfeng         mov32 r2, 2
153*fae6e9adSlinfeng         mov32 r3, 3
154*fae6e9adSlinfeng         mov32 r4, 4
155*fae6e9adSlinfeng         mov32 r5, 5
156*fae6e9adSlinfeng         mov32 r6, 6
157*fae6e9adSlinfeng         mov32 r7, 7
158*fae6e9adSlinfeng         mov32 r8, 8
159*fae6e9adSlinfeng         or32 r0, r5
160*fae6e9adSlinfeng         or32 r0, 0xa0
161*fae6e9adSlinfeng         and32 r0, 0xa3
162*fae6e9adSlinfeng         mov32 r9, 0x91
163*fae6e9adSlinfeng         and32 r0, r9
164*fae6e9adSlinfeng         lsh32 r0, 22
165*fae6e9adSlinfeng         lsh32 r0, r8
166*fae6e9adSlinfeng         rsh32 r0, 19
167*fae6e9adSlinfeng         rsh32 r0, r7
168*fae6e9adSlinfeng         xor32 r0, 0x03
169*fae6e9adSlinfeng         xor32 r0, r2
170*fae6e9adSlinfeng         exit",
171*fae6e9adSlinfeng     )
172*fae6e9adSlinfeng     .unwrap();
173*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
174*fae6e9adSlinfeng     vm.jit_compile().unwrap();
175*fae6e9adSlinfeng     unsafe {
176*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x11);
177*fae6e9adSlinfeng     }
178*fae6e9adSlinfeng }
179*fae6e9adSlinfeng 
180*fae6e9adSlinfeng #[test]
test_jit_arsh32_high_shift()181*fae6e9adSlinfeng fn test_jit_arsh32_high_shift() {
182*fae6e9adSlinfeng     let prog = assemble(
183*fae6e9adSlinfeng         "
184*fae6e9adSlinfeng         mov r0, 8
185*fae6e9adSlinfeng         lddw r1, 0x100000001
186*fae6e9adSlinfeng         arsh32 r0, r1
187*fae6e9adSlinfeng         exit",
188*fae6e9adSlinfeng     )
189*fae6e9adSlinfeng     .unwrap();
190*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
191*fae6e9adSlinfeng     vm.jit_compile().unwrap();
192*fae6e9adSlinfeng     unsafe {
193*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x4);
194*fae6e9adSlinfeng     }
195*fae6e9adSlinfeng }
196*fae6e9adSlinfeng 
197*fae6e9adSlinfeng #[test]
test_jit_arsh()198*fae6e9adSlinfeng fn test_jit_arsh() {
199*fae6e9adSlinfeng     let prog = assemble(
200*fae6e9adSlinfeng         "
201*fae6e9adSlinfeng         mov32 r0, 0xf8
202*fae6e9adSlinfeng         lsh32 r0, 28
203*fae6e9adSlinfeng         arsh32 r0, 16
204*fae6e9adSlinfeng         exit",
205*fae6e9adSlinfeng     )
206*fae6e9adSlinfeng     .unwrap();
207*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
208*fae6e9adSlinfeng     vm.jit_compile().unwrap();
209*fae6e9adSlinfeng     unsafe {
210*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0xffff8000);
211*fae6e9adSlinfeng     }
212*fae6e9adSlinfeng }
213*fae6e9adSlinfeng 
214*fae6e9adSlinfeng #[test]
test_jit_arsh64()215*fae6e9adSlinfeng fn test_jit_arsh64() {
216*fae6e9adSlinfeng     let prog = assemble(
217*fae6e9adSlinfeng         "
218*fae6e9adSlinfeng         mov32 r0, 1
219*fae6e9adSlinfeng         lsh r0, 63
220*fae6e9adSlinfeng         arsh r0, 55
221*fae6e9adSlinfeng         mov32 r1, 5
222*fae6e9adSlinfeng         arsh r0, r1
223*fae6e9adSlinfeng         exit",
224*fae6e9adSlinfeng     )
225*fae6e9adSlinfeng     .unwrap();
226*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
227*fae6e9adSlinfeng     vm.jit_compile().unwrap();
228*fae6e9adSlinfeng     unsafe {
229*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0xfffffffffffffff8);
230*fae6e9adSlinfeng     }
231*fae6e9adSlinfeng }
232*fae6e9adSlinfeng 
233*fae6e9adSlinfeng #[test]
test_jit_arsh_reg()234*fae6e9adSlinfeng fn test_jit_arsh_reg() {
235*fae6e9adSlinfeng     let prog = assemble(
236*fae6e9adSlinfeng         "
237*fae6e9adSlinfeng         mov32 r0, 0xf8
238*fae6e9adSlinfeng         mov32 r1, 16
239*fae6e9adSlinfeng         lsh32 r0, 28
240*fae6e9adSlinfeng         arsh32 r0, r1
241*fae6e9adSlinfeng         exit",
242*fae6e9adSlinfeng     )
243*fae6e9adSlinfeng     .unwrap();
244*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
245*fae6e9adSlinfeng     vm.jit_compile().unwrap();
246*fae6e9adSlinfeng     unsafe {
247*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0xffff8000);
248*fae6e9adSlinfeng     }
249*fae6e9adSlinfeng }
250*fae6e9adSlinfeng 
251*fae6e9adSlinfeng #[test]
test_jit_be16()252*fae6e9adSlinfeng fn test_jit_be16() {
253*fae6e9adSlinfeng     let prog = assemble(
254*fae6e9adSlinfeng         "
255*fae6e9adSlinfeng         ldxh r0, [r1]
256*fae6e9adSlinfeng         be16 r0
257*fae6e9adSlinfeng         exit",
258*fae6e9adSlinfeng     )
259*fae6e9adSlinfeng     .unwrap();
260*fae6e9adSlinfeng     let mem = &mut [0x11, 0x22];
261*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
262*fae6e9adSlinfeng     vm.jit_compile().unwrap();
263*fae6e9adSlinfeng     unsafe {
264*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122);
265*fae6e9adSlinfeng     }
266*fae6e9adSlinfeng }
267*fae6e9adSlinfeng 
268*fae6e9adSlinfeng #[test]
test_jit_be16_high()269*fae6e9adSlinfeng fn test_jit_be16_high() {
270*fae6e9adSlinfeng     let prog = assemble(
271*fae6e9adSlinfeng         "
272*fae6e9adSlinfeng         ldxdw r0, [r1]
273*fae6e9adSlinfeng         be16 r0
274*fae6e9adSlinfeng         exit",
275*fae6e9adSlinfeng     )
276*fae6e9adSlinfeng     .unwrap();
277*fae6e9adSlinfeng     let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
278*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
279*fae6e9adSlinfeng     vm.jit_compile().unwrap();
280*fae6e9adSlinfeng     unsafe {
281*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122);
282*fae6e9adSlinfeng     }
283*fae6e9adSlinfeng }
284*fae6e9adSlinfeng 
285*fae6e9adSlinfeng #[test]
test_jit_be32()286*fae6e9adSlinfeng fn test_jit_be32() {
287*fae6e9adSlinfeng     let prog = assemble(
288*fae6e9adSlinfeng         "
289*fae6e9adSlinfeng         ldxw r0, [r1]
290*fae6e9adSlinfeng         be32 r0
291*fae6e9adSlinfeng         exit",
292*fae6e9adSlinfeng     )
293*fae6e9adSlinfeng     .unwrap();
294*fae6e9adSlinfeng     let mem = &mut [0x11, 0x22, 0x33, 0x44];
295*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
296*fae6e9adSlinfeng     vm.jit_compile().unwrap();
297*fae6e9adSlinfeng     unsafe {
298*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11223344);
299*fae6e9adSlinfeng     }
300*fae6e9adSlinfeng }
301*fae6e9adSlinfeng 
302*fae6e9adSlinfeng #[test]
test_jit_be32_high()303*fae6e9adSlinfeng fn test_jit_be32_high() {
304*fae6e9adSlinfeng     let prog = assemble(
305*fae6e9adSlinfeng         "
306*fae6e9adSlinfeng         ldxdw r0, [r1]
307*fae6e9adSlinfeng         be32 r0
308*fae6e9adSlinfeng         exit",
309*fae6e9adSlinfeng     )
310*fae6e9adSlinfeng     .unwrap();
311*fae6e9adSlinfeng     let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
312*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
313*fae6e9adSlinfeng     vm.jit_compile().unwrap();
314*fae6e9adSlinfeng     unsafe {
315*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11223344);
316*fae6e9adSlinfeng     }
317*fae6e9adSlinfeng }
318*fae6e9adSlinfeng 
319*fae6e9adSlinfeng #[test]
test_jit_be64()320*fae6e9adSlinfeng fn test_jit_be64() {
321*fae6e9adSlinfeng     let prog = assemble(
322*fae6e9adSlinfeng         "
323*fae6e9adSlinfeng         ldxdw r0, [r1]
324*fae6e9adSlinfeng         be64 r0
325*fae6e9adSlinfeng         exit",
326*fae6e9adSlinfeng     )
327*fae6e9adSlinfeng     .unwrap();
328*fae6e9adSlinfeng     let mem = &mut [0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88];
329*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
330*fae6e9adSlinfeng     vm.jit_compile().unwrap();
331*fae6e9adSlinfeng     unsafe {
332*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122334455667788);
333*fae6e9adSlinfeng     }
334*fae6e9adSlinfeng }
335*fae6e9adSlinfeng 
336*fae6e9adSlinfeng #[test]
test_jit_call()337*fae6e9adSlinfeng fn test_jit_call() {
338*fae6e9adSlinfeng     let prog = assemble(
339*fae6e9adSlinfeng         "
340*fae6e9adSlinfeng         mov r1, 1
341*fae6e9adSlinfeng         mov r2, 2
342*fae6e9adSlinfeng         mov r3, 3
343*fae6e9adSlinfeng         mov r4, 4
344*fae6e9adSlinfeng         mov r5, 5
345*fae6e9adSlinfeng         call 0
346*fae6e9adSlinfeng         exit",
347*fae6e9adSlinfeng     )
348*fae6e9adSlinfeng     .unwrap();
349*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
350*fae6e9adSlinfeng     vm.register_helper(0, helpers::gather_bytes).unwrap();
351*fae6e9adSlinfeng     vm.jit_compile().unwrap();
352*fae6e9adSlinfeng     unsafe {
353*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x0102030405);
354*fae6e9adSlinfeng     }
355*fae6e9adSlinfeng }
356*fae6e9adSlinfeng 
357*fae6e9adSlinfeng #[test]
test_jit_call_memfrob()358*fae6e9adSlinfeng fn test_jit_call_memfrob() {
359*fae6e9adSlinfeng     let prog = assemble(
360*fae6e9adSlinfeng         "
361*fae6e9adSlinfeng         mov r6, r1
362*fae6e9adSlinfeng         add r1, 2
363*fae6e9adSlinfeng         mov r2, 4
364*fae6e9adSlinfeng         call 1
365*fae6e9adSlinfeng         ldxdw r0, [r6]
366*fae6e9adSlinfeng         be64 r0
367*fae6e9adSlinfeng         exit",
368*fae6e9adSlinfeng     )
369*fae6e9adSlinfeng     .unwrap();
370*fae6e9adSlinfeng     let mem = &mut [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08];
371*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
372*fae6e9adSlinfeng     vm.register_helper(1, helpers::memfrob).unwrap();
373*fae6e9adSlinfeng     vm.jit_compile().unwrap();
374*fae6e9adSlinfeng     unsafe {
375*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x102292e2f2c0708);
376*fae6e9adSlinfeng     }
377*fae6e9adSlinfeng }
378*fae6e9adSlinfeng 
379*fae6e9adSlinfeng // TODO: helpers::trash_registers needs asm!().
380*fae6e9adSlinfeng // Try this again once asm!() is available in stable.
381*fae6e9adSlinfeng //#[test]
382*fae6e9adSlinfeng //fn test_jit_call_save() {
383*fae6e9adSlinfeng //let prog = &[
384*fae6e9adSlinfeng //0xb7, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
385*fae6e9adSlinfeng //0xb7, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
386*fae6e9adSlinfeng //0xb7, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
387*fae6e9adSlinfeng //0xb7, 0x09, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
388*fae6e9adSlinfeng //0x85, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
389*fae6e9adSlinfeng //0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
390*fae6e9adSlinfeng //0x4f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
391*fae6e9adSlinfeng //0x4f, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
392*fae6e9adSlinfeng //0x4f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
393*fae6e9adSlinfeng //0x4f, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
394*fae6e9adSlinfeng //0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
395*fae6e9adSlinfeng //];
396*fae6e9adSlinfeng //let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
397*fae6e9adSlinfeng //vm.register_helper(2, helpers::trash_registers);
398*fae6e9adSlinfeng //vm.jit_compile().unwrap();
399*fae6e9adSlinfeng //unsafe { assert_eq!(vm.execute_program_jit().unwrap(), 0x4321); }
400*fae6e9adSlinfeng //}
401*fae6e9adSlinfeng 
402*fae6e9adSlinfeng #[test]
test_jit_div32_high_divisor()403*fae6e9adSlinfeng fn test_jit_div32_high_divisor() {
404*fae6e9adSlinfeng     let prog = assemble(
405*fae6e9adSlinfeng         "
406*fae6e9adSlinfeng         mov r0, 12
407*fae6e9adSlinfeng         lddw r1, 0x100000004
408*fae6e9adSlinfeng         div32 r0, r1
409*fae6e9adSlinfeng         exit",
410*fae6e9adSlinfeng     )
411*fae6e9adSlinfeng     .unwrap();
412*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
413*fae6e9adSlinfeng     vm.jit_compile().unwrap();
414*fae6e9adSlinfeng     unsafe {
415*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
416*fae6e9adSlinfeng     }
417*fae6e9adSlinfeng }
418*fae6e9adSlinfeng 
419*fae6e9adSlinfeng #[test]
test_jit_div32_imm()420*fae6e9adSlinfeng fn test_jit_div32_imm() {
421*fae6e9adSlinfeng     let prog = assemble(
422*fae6e9adSlinfeng         "
423*fae6e9adSlinfeng         lddw r0, 0x10000000c
424*fae6e9adSlinfeng         div32 r0, 4
425*fae6e9adSlinfeng         exit",
426*fae6e9adSlinfeng     )
427*fae6e9adSlinfeng     .unwrap();
428*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
429*fae6e9adSlinfeng     vm.jit_compile().unwrap();
430*fae6e9adSlinfeng     unsafe {
431*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
432*fae6e9adSlinfeng     }
433*fae6e9adSlinfeng }
434*fae6e9adSlinfeng 
435*fae6e9adSlinfeng #[test]
test_jit_div32_reg()436*fae6e9adSlinfeng fn test_jit_div32_reg() {
437*fae6e9adSlinfeng     let prog = assemble(
438*fae6e9adSlinfeng         "
439*fae6e9adSlinfeng         lddw r0, 0x10000000c
440*fae6e9adSlinfeng         mov r1, 4
441*fae6e9adSlinfeng         div32 r0, r1
442*fae6e9adSlinfeng         exit",
443*fae6e9adSlinfeng     )
444*fae6e9adSlinfeng     .unwrap();
445*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
446*fae6e9adSlinfeng     vm.jit_compile().unwrap();
447*fae6e9adSlinfeng     unsafe {
448*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
449*fae6e9adSlinfeng     }
450*fae6e9adSlinfeng }
451*fae6e9adSlinfeng 
452*fae6e9adSlinfeng #[test]
test_jit_div64_imm()453*fae6e9adSlinfeng fn test_jit_div64_imm() {
454*fae6e9adSlinfeng     let prog = assemble(
455*fae6e9adSlinfeng         "
456*fae6e9adSlinfeng         mov r0, 0xc
457*fae6e9adSlinfeng         lsh r0, 32
458*fae6e9adSlinfeng         div r0, 4
459*fae6e9adSlinfeng         exit",
460*fae6e9adSlinfeng     )
461*fae6e9adSlinfeng     .unwrap();
462*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
463*fae6e9adSlinfeng     vm.jit_compile().unwrap();
464*fae6e9adSlinfeng     unsafe {
465*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x300000000);
466*fae6e9adSlinfeng     }
467*fae6e9adSlinfeng }
468*fae6e9adSlinfeng 
469*fae6e9adSlinfeng #[test]
test_jit_div64_reg()470*fae6e9adSlinfeng fn test_jit_div64_reg() {
471*fae6e9adSlinfeng     let prog = assemble(
472*fae6e9adSlinfeng         "
473*fae6e9adSlinfeng         mov r0, 0xc
474*fae6e9adSlinfeng         lsh r0, 32
475*fae6e9adSlinfeng         mov r1, 4
476*fae6e9adSlinfeng         div r0, r1
477*fae6e9adSlinfeng         exit",
478*fae6e9adSlinfeng     )
479*fae6e9adSlinfeng     .unwrap();
480*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
481*fae6e9adSlinfeng     vm.jit_compile().unwrap();
482*fae6e9adSlinfeng     unsafe {
483*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x300000000);
484*fae6e9adSlinfeng     }
485*fae6e9adSlinfeng }
486*fae6e9adSlinfeng 
487*fae6e9adSlinfeng // For some register numbers, we don't emit the same instructions for handling divisions by zero,
488*fae6e9adSlinfeng // which means we don't use the same offset to skip these instructions when the divisor is not
489*fae6e9adSlinfeng // zero. We've had a regression because of this before, make sure we test it.
490*fae6e9adSlinfeng #[test]
test_jit_div32_highreg()491*fae6e9adSlinfeng fn test_jit_div32_highreg() {
492*fae6e9adSlinfeng     let prog = assemble(
493*fae6e9adSlinfeng         "
494*fae6e9adSlinfeng         mov r0, 2
495*fae6e9adSlinfeng         mov r7, 4
496*fae6e9adSlinfeng         div32 r7, r0
497*fae6e9adSlinfeng         exit",
498*fae6e9adSlinfeng     )
499*fae6e9adSlinfeng     .unwrap();
500*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
501*fae6e9adSlinfeng     vm.jit_compile().unwrap();
502*fae6e9adSlinfeng     unsafe {
503*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x2);
504*fae6e9adSlinfeng     }
505*fae6e9adSlinfeng }
506*fae6e9adSlinfeng 
507*fae6e9adSlinfeng #[test]
test_jit_div64_highreg()508*fae6e9adSlinfeng fn test_jit_div64_highreg() {
509*fae6e9adSlinfeng     let prog = assemble(
510*fae6e9adSlinfeng         "
511*fae6e9adSlinfeng         mov r0, 2
512*fae6e9adSlinfeng         mov r7, 4
513*fae6e9adSlinfeng         div r7, r0
514*fae6e9adSlinfeng         exit",
515*fae6e9adSlinfeng     )
516*fae6e9adSlinfeng     .unwrap();
517*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
518*fae6e9adSlinfeng     vm.jit_compile().unwrap();
519*fae6e9adSlinfeng     unsafe {
520*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x2);
521*fae6e9adSlinfeng     }
522*fae6e9adSlinfeng }
523*fae6e9adSlinfeng 
524*fae6e9adSlinfeng #[test]
test_jit_early_exit()525*fae6e9adSlinfeng fn test_jit_early_exit() {
526*fae6e9adSlinfeng     let prog = assemble(
527*fae6e9adSlinfeng         "
528*fae6e9adSlinfeng         mov r0, 3
529*fae6e9adSlinfeng         exit
530*fae6e9adSlinfeng         mov r0, 4
531*fae6e9adSlinfeng         exit",
532*fae6e9adSlinfeng     )
533*fae6e9adSlinfeng     .unwrap();
534*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
535*fae6e9adSlinfeng     vm.jit_compile().unwrap();
536*fae6e9adSlinfeng     unsafe {
537*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x3);
538*fae6e9adSlinfeng     }
539*fae6e9adSlinfeng }
540*fae6e9adSlinfeng 
541*fae6e9adSlinfeng // uBPF limits the number of user functions at 64. We don't.
542*fae6e9adSlinfeng //#[test]
543*fae6e9adSlinfeng //fn test_jit_err_call_bad_imm() {
544*fae6e9adSlinfeng //}
545*fae6e9adSlinfeng 
546*fae6e9adSlinfeng #[test]
547*fae6e9adSlinfeng #[should_panic(expected = "[JIT] Error: unknown helper function (id: 0x3f)")]
test_jit_err_call_unreg()548*fae6e9adSlinfeng fn test_jit_err_call_unreg() {
549*fae6e9adSlinfeng     let prog = assemble(
550*fae6e9adSlinfeng         "
551*fae6e9adSlinfeng         mov r1, 1
552*fae6e9adSlinfeng         mov r2, 2
553*fae6e9adSlinfeng         mov r3, 3
554*fae6e9adSlinfeng         mov r4, 4
555*fae6e9adSlinfeng         mov r5, 5
556*fae6e9adSlinfeng         call 63
557*fae6e9adSlinfeng         exit",
558*fae6e9adSlinfeng     )
559*fae6e9adSlinfeng     .unwrap();
560*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
561*fae6e9adSlinfeng     vm.jit_compile().unwrap();
562*fae6e9adSlinfeng     unsafe {
563*fae6e9adSlinfeng         vm.execute_program_jit().unwrap();
564*fae6e9adSlinfeng     }
565*fae6e9adSlinfeng }
566*fae6e9adSlinfeng 
567*fae6e9adSlinfeng #[test]
test_jit_div64_by_zero_imm()568*fae6e9adSlinfeng fn test_jit_div64_by_zero_imm() {
569*fae6e9adSlinfeng     let prog = assemble(
570*fae6e9adSlinfeng         "
571*fae6e9adSlinfeng         mov32 r0, 1
572*fae6e9adSlinfeng         div r0, 0
573*fae6e9adSlinfeng         exit",
574*fae6e9adSlinfeng     )
575*fae6e9adSlinfeng     .unwrap();
576*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
577*fae6e9adSlinfeng     vm.jit_compile().unwrap();
578*fae6e9adSlinfeng     unsafe {
579*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
580*fae6e9adSlinfeng     }
581*fae6e9adSlinfeng }
582*fae6e9adSlinfeng 
583*fae6e9adSlinfeng #[test]
test_jit_div_by_zero_imm()584*fae6e9adSlinfeng fn test_jit_div_by_zero_imm() {
585*fae6e9adSlinfeng     let prog = assemble(
586*fae6e9adSlinfeng         "
587*fae6e9adSlinfeng         mov32 r0, 1
588*fae6e9adSlinfeng         div32 r0, 0
589*fae6e9adSlinfeng         exit",
590*fae6e9adSlinfeng     )
591*fae6e9adSlinfeng     .unwrap();
592*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
593*fae6e9adSlinfeng     vm.jit_compile().unwrap();
594*fae6e9adSlinfeng     unsafe {
595*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
596*fae6e9adSlinfeng     }
597*fae6e9adSlinfeng }
598*fae6e9adSlinfeng 
599*fae6e9adSlinfeng #[test]
test_jit_mod64_by_zero_imm()600*fae6e9adSlinfeng fn test_jit_mod64_by_zero_imm() {
601*fae6e9adSlinfeng     let prog = assemble(
602*fae6e9adSlinfeng         "
603*fae6e9adSlinfeng         mov32 r0, 1
604*fae6e9adSlinfeng         mod r0, 0
605*fae6e9adSlinfeng         exit",
606*fae6e9adSlinfeng     )
607*fae6e9adSlinfeng     .unwrap();
608*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
609*fae6e9adSlinfeng     vm.jit_compile().unwrap();
610*fae6e9adSlinfeng     unsafe {
611*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
612*fae6e9adSlinfeng     }
613*fae6e9adSlinfeng }
614*fae6e9adSlinfeng 
615*fae6e9adSlinfeng #[test]
test_jit_mod_by_zero_imm()616*fae6e9adSlinfeng fn test_jit_mod_by_zero_imm() {
617*fae6e9adSlinfeng     let prog = assemble(
618*fae6e9adSlinfeng         "
619*fae6e9adSlinfeng         mov32 r0, 1
620*fae6e9adSlinfeng         mod32 r0, 0
621*fae6e9adSlinfeng         exit",
622*fae6e9adSlinfeng     )
623*fae6e9adSlinfeng     .unwrap();
624*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
625*fae6e9adSlinfeng     vm.jit_compile().unwrap();
626*fae6e9adSlinfeng     unsafe {
627*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
628*fae6e9adSlinfeng     }
629*fae6e9adSlinfeng }
630*fae6e9adSlinfeng 
631*fae6e9adSlinfeng #[test]
test_jit_div64_by_zero_reg()632*fae6e9adSlinfeng fn test_jit_div64_by_zero_reg() {
633*fae6e9adSlinfeng     let prog = assemble(
634*fae6e9adSlinfeng         "
635*fae6e9adSlinfeng         mov32 r0, 1
636*fae6e9adSlinfeng         mov32 r1, 0
637*fae6e9adSlinfeng         div r0, r1
638*fae6e9adSlinfeng         exit",
639*fae6e9adSlinfeng     )
640*fae6e9adSlinfeng     .unwrap();
641*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
642*fae6e9adSlinfeng     vm.jit_compile().unwrap();
643*fae6e9adSlinfeng     unsafe {
644*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
645*fae6e9adSlinfeng     }
646*fae6e9adSlinfeng }
647*fae6e9adSlinfeng 
648*fae6e9adSlinfeng #[test]
test_jit_div_by_zero_reg()649*fae6e9adSlinfeng fn test_jit_div_by_zero_reg() {
650*fae6e9adSlinfeng     let prog = assemble(
651*fae6e9adSlinfeng         "
652*fae6e9adSlinfeng         mov32 r0, 1
653*fae6e9adSlinfeng         mov32 r1, 0
654*fae6e9adSlinfeng         div32 r0, r1
655*fae6e9adSlinfeng         exit",
656*fae6e9adSlinfeng     )
657*fae6e9adSlinfeng     .unwrap();
658*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
659*fae6e9adSlinfeng     vm.jit_compile().unwrap();
660*fae6e9adSlinfeng     unsafe {
661*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
662*fae6e9adSlinfeng     }
663*fae6e9adSlinfeng }
664*fae6e9adSlinfeng 
665*fae6e9adSlinfeng #[test]
test_jit_mod64_by_zero_reg()666*fae6e9adSlinfeng fn test_jit_mod64_by_zero_reg() {
667*fae6e9adSlinfeng     let prog = assemble(
668*fae6e9adSlinfeng         "
669*fae6e9adSlinfeng         mov32 r0, 1
670*fae6e9adSlinfeng         mov32 r1, 0
671*fae6e9adSlinfeng         mod r0, r1
672*fae6e9adSlinfeng         exit",
673*fae6e9adSlinfeng     )
674*fae6e9adSlinfeng     .unwrap();
675*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
676*fae6e9adSlinfeng     vm.jit_compile().unwrap();
677*fae6e9adSlinfeng     unsafe {
678*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
679*fae6e9adSlinfeng     }
680*fae6e9adSlinfeng }
681*fae6e9adSlinfeng 
682*fae6e9adSlinfeng #[test]
test_jit_mod_by_zero_reg()683*fae6e9adSlinfeng fn test_jit_mod_by_zero_reg() {
684*fae6e9adSlinfeng     let prog = assemble(
685*fae6e9adSlinfeng         "
686*fae6e9adSlinfeng         mov32 r0, 1
687*fae6e9adSlinfeng         mov32 r1, 0
688*fae6e9adSlinfeng         mod32 r0, r1
689*fae6e9adSlinfeng         exit",
690*fae6e9adSlinfeng     )
691*fae6e9adSlinfeng     .unwrap();
692*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
693*fae6e9adSlinfeng     vm.jit_compile().unwrap();
694*fae6e9adSlinfeng     unsafe {
695*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
696*fae6e9adSlinfeng     }
697*fae6e9adSlinfeng }
698*fae6e9adSlinfeng 
699*fae6e9adSlinfeng // TODO SKIP: JIT disabled for this testcase (stack oob check not implemented)
700*fae6e9adSlinfeng // #[test]
701*fae6e9adSlinfeng // #[should_panic(expected = "Error: out of bounds memory store (insn #1)")]
702*fae6e9adSlinfeng // fn test_jit_err_stack_out_of_bound() {
703*fae6e9adSlinfeng //     let prog = &[
704*fae6e9adSlinfeng //         0x72, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
705*fae6e9adSlinfeng //         0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
706*fae6e9adSlinfeng //     ];
707*fae6e9adSlinfeng //     let mut vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
708*fae6e9adSlinfeng //     vm.jit_compile().unwrap();
709*fae6e9adSlinfeng //     unsafe { vm.execute_program_jit().unwrap(); }
710*fae6e9adSlinfeng // }
711*fae6e9adSlinfeng 
712*fae6e9adSlinfeng #[test]
test_jit_exit()713*fae6e9adSlinfeng fn test_jit_exit() {
714*fae6e9adSlinfeng     let prog = assemble(
715*fae6e9adSlinfeng         "
716*fae6e9adSlinfeng         mov r0, 0
717*fae6e9adSlinfeng         exit",
718*fae6e9adSlinfeng     )
719*fae6e9adSlinfeng     .unwrap();
720*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
721*fae6e9adSlinfeng     vm.jit_compile().unwrap();
722*fae6e9adSlinfeng     unsafe {
723*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
724*fae6e9adSlinfeng     }
725*fae6e9adSlinfeng }
726*fae6e9adSlinfeng 
727*fae6e9adSlinfeng #[test]
test_jit_ja()728*fae6e9adSlinfeng fn test_jit_ja() {
729*fae6e9adSlinfeng     let prog = assemble(
730*fae6e9adSlinfeng         "
731*fae6e9adSlinfeng         mov r0, 1
732*fae6e9adSlinfeng         ja +1
733*fae6e9adSlinfeng         mov r0, 2
734*fae6e9adSlinfeng         exit",
735*fae6e9adSlinfeng     )
736*fae6e9adSlinfeng     .unwrap();
737*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
738*fae6e9adSlinfeng     vm.jit_compile().unwrap();
739*fae6e9adSlinfeng     unsafe {
740*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
741*fae6e9adSlinfeng     }
742*fae6e9adSlinfeng }
743*fae6e9adSlinfeng 
744*fae6e9adSlinfeng #[test]
test_jit_jeq_imm()745*fae6e9adSlinfeng fn test_jit_jeq_imm() {
746*fae6e9adSlinfeng     let prog = assemble(
747*fae6e9adSlinfeng         "
748*fae6e9adSlinfeng         mov32 r0, 0
749*fae6e9adSlinfeng         mov32 r1, 0xa
750*fae6e9adSlinfeng         jeq r1, 0xb, +4
751*fae6e9adSlinfeng         mov32 r0, 1
752*fae6e9adSlinfeng         mov32 r1, 0xb
753*fae6e9adSlinfeng         jeq r1, 0xb, +1
754*fae6e9adSlinfeng         mov32 r0, 2
755*fae6e9adSlinfeng         exit",
756*fae6e9adSlinfeng     )
757*fae6e9adSlinfeng     .unwrap();
758*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
759*fae6e9adSlinfeng     vm.jit_compile().unwrap();
760*fae6e9adSlinfeng     unsafe {
761*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
762*fae6e9adSlinfeng     }
763*fae6e9adSlinfeng }
764*fae6e9adSlinfeng 
765*fae6e9adSlinfeng #[test]
test_jit_jeq_reg()766*fae6e9adSlinfeng fn test_jit_jeq_reg() {
767*fae6e9adSlinfeng     let prog = assemble(
768*fae6e9adSlinfeng         "
769*fae6e9adSlinfeng         mov32 r0, 0
770*fae6e9adSlinfeng         mov32 r1, 0xa
771*fae6e9adSlinfeng         mov32 r2, 0xb
772*fae6e9adSlinfeng         jeq r1, r2, +4
773*fae6e9adSlinfeng         mov32 r0, 1
774*fae6e9adSlinfeng         mov32 r1, 0xb
775*fae6e9adSlinfeng         jeq r1, r2, +1
776*fae6e9adSlinfeng         mov32 r0, 2
777*fae6e9adSlinfeng         exit",
778*fae6e9adSlinfeng     )
779*fae6e9adSlinfeng     .unwrap();
780*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
781*fae6e9adSlinfeng     vm.jit_compile().unwrap();
782*fae6e9adSlinfeng     unsafe {
783*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
784*fae6e9adSlinfeng     }
785*fae6e9adSlinfeng }
786*fae6e9adSlinfeng 
787*fae6e9adSlinfeng #[test]
test_jit_jge_imm()788*fae6e9adSlinfeng fn test_jit_jge_imm() {
789*fae6e9adSlinfeng     let prog = assemble(
790*fae6e9adSlinfeng         "
791*fae6e9adSlinfeng         mov32 r0, 0
792*fae6e9adSlinfeng         mov32 r1, 0xa
793*fae6e9adSlinfeng         jge r1, 0xb, +4
794*fae6e9adSlinfeng         mov32 r0, 1
795*fae6e9adSlinfeng         mov32 r1, 0xc
796*fae6e9adSlinfeng         jge r1, 0xb, +1
797*fae6e9adSlinfeng         mov32 r0, 2
798*fae6e9adSlinfeng         exit",
799*fae6e9adSlinfeng     )
800*fae6e9adSlinfeng     .unwrap();
801*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
802*fae6e9adSlinfeng     vm.jit_compile().unwrap();
803*fae6e9adSlinfeng     unsafe {
804*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
805*fae6e9adSlinfeng     }
806*fae6e9adSlinfeng }
807*fae6e9adSlinfeng 
808*fae6e9adSlinfeng #[test]
test_jit_jle_imm()809*fae6e9adSlinfeng fn test_jit_jle_imm() {
810*fae6e9adSlinfeng     let prog = assemble(
811*fae6e9adSlinfeng         "
812*fae6e9adSlinfeng         mov32 r0, 0
813*fae6e9adSlinfeng         mov32 r1, 5
814*fae6e9adSlinfeng         jle r1, 4, +1
815*fae6e9adSlinfeng         jle r1, 6, +1
816*fae6e9adSlinfeng         exit
817*fae6e9adSlinfeng         jle r1, 5, +1
818*fae6e9adSlinfeng         exit
819*fae6e9adSlinfeng         mov32 r0, 1
820*fae6e9adSlinfeng         exit",
821*fae6e9adSlinfeng     )
822*fae6e9adSlinfeng     .unwrap();
823*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
824*fae6e9adSlinfeng     vm.jit_compile().unwrap();
825*fae6e9adSlinfeng     unsafe {
826*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
827*fae6e9adSlinfeng     }
828*fae6e9adSlinfeng }
829*fae6e9adSlinfeng 
830*fae6e9adSlinfeng #[test]
test_jit_jle_reg()831*fae6e9adSlinfeng fn test_jit_jle_reg() {
832*fae6e9adSlinfeng     let prog = assemble(
833*fae6e9adSlinfeng         "
834*fae6e9adSlinfeng         mov r0, 0
835*fae6e9adSlinfeng         mov r1, 5
836*fae6e9adSlinfeng         mov r2, 4
837*fae6e9adSlinfeng         mov r3, 6
838*fae6e9adSlinfeng         jle r1, r2, +2
839*fae6e9adSlinfeng         jle r1, r1, +1
840*fae6e9adSlinfeng         exit
841*fae6e9adSlinfeng         jle r1, r3, +1
842*fae6e9adSlinfeng         exit
843*fae6e9adSlinfeng         mov r0, 1
844*fae6e9adSlinfeng         exit",
845*fae6e9adSlinfeng     )
846*fae6e9adSlinfeng     .unwrap();
847*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
848*fae6e9adSlinfeng     vm.jit_compile().unwrap();
849*fae6e9adSlinfeng     unsafe {
850*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
851*fae6e9adSlinfeng     }
852*fae6e9adSlinfeng }
853*fae6e9adSlinfeng 
854*fae6e9adSlinfeng #[test]
test_jit_jgt_imm()855*fae6e9adSlinfeng fn test_jit_jgt_imm() {
856*fae6e9adSlinfeng     let prog = assemble(
857*fae6e9adSlinfeng         "
858*fae6e9adSlinfeng         mov32 r0, 0
859*fae6e9adSlinfeng         mov32 r1, 5
860*fae6e9adSlinfeng         jgt r1, 6, +2
861*fae6e9adSlinfeng         jgt r1, 5, +1
862*fae6e9adSlinfeng         jgt r1, 4, +1
863*fae6e9adSlinfeng         exit
864*fae6e9adSlinfeng         mov32 r0, 1
865*fae6e9adSlinfeng         exit",
866*fae6e9adSlinfeng     )
867*fae6e9adSlinfeng     .unwrap();
868*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
869*fae6e9adSlinfeng     vm.jit_compile().unwrap();
870*fae6e9adSlinfeng     unsafe {
871*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
872*fae6e9adSlinfeng     }
873*fae6e9adSlinfeng }
874*fae6e9adSlinfeng 
875*fae6e9adSlinfeng #[test]
test_jit_jgt_reg()876*fae6e9adSlinfeng fn test_jit_jgt_reg() {
877*fae6e9adSlinfeng     let prog = assemble(
878*fae6e9adSlinfeng         "
879*fae6e9adSlinfeng         mov r0, 0
880*fae6e9adSlinfeng         mov r1, 5
881*fae6e9adSlinfeng         mov r2, 6
882*fae6e9adSlinfeng         mov r3, 4
883*fae6e9adSlinfeng         jgt r1, r2, +2
884*fae6e9adSlinfeng         jgt r1, r1, +1
885*fae6e9adSlinfeng         jgt r1, r3, +1
886*fae6e9adSlinfeng         exit
887*fae6e9adSlinfeng         mov r0, 1
888*fae6e9adSlinfeng         exit",
889*fae6e9adSlinfeng     )
890*fae6e9adSlinfeng     .unwrap();
891*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
892*fae6e9adSlinfeng     vm.jit_compile().unwrap();
893*fae6e9adSlinfeng     unsafe {
894*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
895*fae6e9adSlinfeng     }
896*fae6e9adSlinfeng }
897*fae6e9adSlinfeng 
898*fae6e9adSlinfeng #[test]
test_jit_jlt_imm()899*fae6e9adSlinfeng fn test_jit_jlt_imm() {
900*fae6e9adSlinfeng     let prog = assemble(
901*fae6e9adSlinfeng         "
902*fae6e9adSlinfeng         mov32 r0, 0
903*fae6e9adSlinfeng         mov32 r1, 5
904*fae6e9adSlinfeng         jlt r1, 4, +2
905*fae6e9adSlinfeng         jlt r1, 5, +1
906*fae6e9adSlinfeng         jlt r1, 6, +1
907*fae6e9adSlinfeng         exit
908*fae6e9adSlinfeng         mov32 r0, 1
909*fae6e9adSlinfeng         exit",
910*fae6e9adSlinfeng     )
911*fae6e9adSlinfeng     .unwrap();
912*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
913*fae6e9adSlinfeng     vm.jit_compile().unwrap();
914*fae6e9adSlinfeng     unsafe {
915*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
916*fae6e9adSlinfeng     }
917*fae6e9adSlinfeng }
918*fae6e9adSlinfeng 
919*fae6e9adSlinfeng #[test]
test_jit_jlt_reg()920*fae6e9adSlinfeng fn test_jit_jlt_reg() {
921*fae6e9adSlinfeng     let prog = assemble(
922*fae6e9adSlinfeng         "
923*fae6e9adSlinfeng         mov r0, 0
924*fae6e9adSlinfeng         mov r1, 5
925*fae6e9adSlinfeng         mov r2, 4
926*fae6e9adSlinfeng         mov r3, 6
927*fae6e9adSlinfeng         jlt r1, r2, +2
928*fae6e9adSlinfeng         jlt r1, r1, +1
929*fae6e9adSlinfeng         jlt r1, r3, +1
930*fae6e9adSlinfeng         exit
931*fae6e9adSlinfeng         mov r0, 1
932*fae6e9adSlinfeng         exit",
933*fae6e9adSlinfeng     )
934*fae6e9adSlinfeng     .unwrap();
935*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
936*fae6e9adSlinfeng     vm.jit_compile().unwrap();
937*fae6e9adSlinfeng     unsafe {
938*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
939*fae6e9adSlinfeng     }
940*fae6e9adSlinfeng }
941*fae6e9adSlinfeng 
942*fae6e9adSlinfeng #[test]
test_jit_jit_bounce()943*fae6e9adSlinfeng fn test_jit_jit_bounce() {
944*fae6e9adSlinfeng     let prog = assemble(
945*fae6e9adSlinfeng         "
946*fae6e9adSlinfeng         mov r0, 1
947*fae6e9adSlinfeng         mov r6, r0
948*fae6e9adSlinfeng         mov r7, r6
949*fae6e9adSlinfeng         mov r8, r7
950*fae6e9adSlinfeng         mov r9, r8
951*fae6e9adSlinfeng         mov r0, r9
952*fae6e9adSlinfeng         exit",
953*fae6e9adSlinfeng     )
954*fae6e9adSlinfeng     .unwrap();
955*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
956*fae6e9adSlinfeng     vm.jit_compile().unwrap();
957*fae6e9adSlinfeng     unsafe {
958*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
959*fae6e9adSlinfeng     }
960*fae6e9adSlinfeng }
961*fae6e9adSlinfeng 
962*fae6e9adSlinfeng #[test]
test_jit_jne_reg()963*fae6e9adSlinfeng fn test_jit_jne_reg() {
964*fae6e9adSlinfeng     let prog = assemble(
965*fae6e9adSlinfeng         "
966*fae6e9adSlinfeng         mov32 r0, 0
967*fae6e9adSlinfeng         mov32 r1, 0xb
968*fae6e9adSlinfeng         mov32 r2, 0xb
969*fae6e9adSlinfeng         jne r1, r2, +4
970*fae6e9adSlinfeng         mov32 r0, 1
971*fae6e9adSlinfeng         mov32 r1, 0xa
972*fae6e9adSlinfeng         jne r1, r2, +1
973*fae6e9adSlinfeng         mov32 r0, 2
974*fae6e9adSlinfeng         exit",
975*fae6e9adSlinfeng     )
976*fae6e9adSlinfeng     .unwrap();
977*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
978*fae6e9adSlinfeng     vm.jit_compile().unwrap();
979*fae6e9adSlinfeng     unsafe {
980*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
981*fae6e9adSlinfeng     }
982*fae6e9adSlinfeng }
983*fae6e9adSlinfeng 
984*fae6e9adSlinfeng #[test]
test_jit_jset_imm()985*fae6e9adSlinfeng fn test_jit_jset_imm() {
986*fae6e9adSlinfeng     let prog = assemble(
987*fae6e9adSlinfeng         "
988*fae6e9adSlinfeng         mov32 r0, 0
989*fae6e9adSlinfeng         mov32 r1, 0x7
990*fae6e9adSlinfeng         jset r1, 0x8, +4
991*fae6e9adSlinfeng         mov32 r0, 1
992*fae6e9adSlinfeng         mov32 r1, 0x9
993*fae6e9adSlinfeng         jset r1, 0x8, +1
994*fae6e9adSlinfeng         mov32 r0, 2
995*fae6e9adSlinfeng         exit",
996*fae6e9adSlinfeng     )
997*fae6e9adSlinfeng     .unwrap();
998*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
999*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1000*fae6e9adSlinfeng     unsafe {
1001*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1002*fae6e9adSlinfeng     }
1003*fae6e9adSlinfeng }
1004*fae6e9adSlinfeng 
1005*fae6e9adSlinfeng #[test]
test_jit_jset_reg()1006*fae6e9adSlinfeng fn test_jit_jset_reg() {
1007*fae6e9adSlinfeng     let prog = assemble(
1008*fae6e9adSlinfeng         "
1009*fae6e9adSlinfeng         mov32 r0, 0
1010*fae6e9adSlinfeng         mov32 r1, 0x7
1011*fae6e9adSlinfeng         mov32 r2, 0x8
1012*fae6e9adSlinfeng         jset r1, r2, +4
1013*fae6e9adSlinfeng         mov32 r0, 1
1014*fae6e9adSlinfeng         mov32 r1, 0x9
1015*fae6e9adSlinfeng         jset r1, r2, +1
1016*fae6e9adSlinfeng         mov32 r0, 2
1017*fae6e9adSlinfeng         exit",
1018*fae6e9adSlinfeng     )
1019*fae6e9adSlinfeng     .unwrap();
1020*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1021*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1022*fae6e9adSlinfeng     unsafe {
1023*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1024*fae6e9adSlinfeng     }
1025*fae6e9adSlinfeng }
1026*fae6e9adSlinfeng 
1027*fae6e9adSlinfeng #[test]
test_jit_jsge_imm()1028*fae6e9adSlinfeng fn test_jit_jsge_imm() {
1029*fae6e9adSlinfeng     let prog = assemble(
1030*fae6e9adSlinfeng         "
1031*fae6e9adSlinfeng         mov32 r0, 0
1032*fae6e9adSlinfeng         mov r1, -2
1033*fae6e9adSlinfeng         jsge r1, -1, +5
1034*fae6e9adSlinfeng         jsge r1, 0, +4
1035*fae6e9adSlinfeng         mov32 r0, 1
1036*fae6e9adSlinfeng         mov r1, -1
1037*fae6e9adSlinfeng         jsge r1, -1, +1
1038*fae6e9adSlinfeng         mov32 r0, 2
1039*fae6e9adSlinfeng         exit",
1040*fae6e9adSlinfeng     )
1041*fae6e9adSlinfeng     .unwrap();
1042*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1043*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1044*fae6e9adSlinfeng     unsafe {
1045*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1046*fae6e9adSlinfeng     }
1047*fae6e9adSlinfeng }
1048*fae6e9adSlinfeng 
1049*fae6e9adSlinfeng #[test]
test_jit_jsge_reg()1050*fae6e9adSlinfeng fn test_jit_jsge_reg() {
1051*fae6e9adSlinfeng     let prog = assemble(
1052*fae6e9adSlinfeng         "
1053*fae6e9adSlinfeng         mov32 r0, 0
1054*fae6e9adSlinfeng         mov r1, -2
1055*fae6e9adSlinfeng         mov r2, -1
1056*fae6e9adSlinfeng         mov32 r3, 0
1057*fae6e9adSlinfeng         jsge r1, r2, +5
1058*fae6e9adSlinfeng         jsge r1, r3, +4
1059*fae6e9adSlinfeng         mov32 r0, 1
1060*fae6e9adSlinfeng         mov r1, r2
1061*fae6e9adSlinfeng         jsge r1, r2, +1
1062*fae6e9adSlinfeng         mov32 r0, 2
1063*fae6e9adSlinfeng         exit",
1064*fae6e9adSlinfeng     )
1065*fae6e9adSlinfeng     .unwrap();
1066*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1067*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1068*fae6e9adSlinfeng     unsafe {
1069*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1070*fae6e9adSlinfeng     }
1071*fae6e9adSlinfeng }
1072*fae6e9adSlinfeng 
1073*fae6e9adSlinfeng #[test]
test_jit_jsle_imm()1074*fae6e9adSlinfeng fn test_jit_jsle_imm() {
1075*fae6e9adSlinfeng     let prog = assemble(
1076*fae6e9adSlinfeng         "
1077*fae6e9adSlinfeng         mov32 r0, 0
1078*fae6e9adSlinfeng         mov r1, -2
1079*fae6e9adSlinfeng         jsle r1, -3, +1
1080*fae6e9adSlinfeng         jsle r1, -1, +1
1081*fae6e9adSlinfeng         exit
1082*fae6e9adSlinfeng         mov32 r0, 1
1083*fae6e9adSlinfeng         jsle r1, -2, +1
1084*fae6e9adSlinfeng         mov32 r0, 2
1085*fae6e9adSlinfeng         exit",
1086*fae6e9adSlinfeng     )
1087*fae6e9adSlinfeng     .unwrap();
1088*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1089*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1090*fae6e9adSlinfeng     unsafe {
1091*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1092*fae6e9adSlinfeng     }
1093*fae6e9adSlinfeng }
1094*fae6e9adSlinfeng 
1095*fae6e9adSlinfeng #[test]
test_jit_jsle_reg()1096*fae6e9adSlinfeng fn test_jit_jsle_reg() {
1097*fae6e9adSlinfeng     let prog = assemble(
1098*fae6e9adSlinfeng         "
1099*fae6e9adSlinfeng         mov32 r0, 0
1100*fae6e9adSlinfeng         mov r1, -1
1101*fae6e9adSlinfeng         mov r2, -2
1102*fae6e9adSlinfeng         mov32 r3, 0
1103*fae6e9adSlinfeng         jsle r1, r2, +1
1104*fae6e9adSlinfeng         jsle r1, r3, +1
1105*fae6e9adSlinfeng         exit
1106*fae6e9adSlinfeng         mov32 r0, 1
1107*fae6e9adSlinfeng         mov r1, r2
1108*fae6e9adSlinfeng         jsle r1, r2, +1
1109*fae6e9adSlinfeng         mov32 r0, 2
1110*fae6e9adSlinfeng         exit",
1111*fae6e9adSlinfeng     )
1112*fae6e9adSlinfeng     .unwrap();
1113*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1114*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1115*fae6e9adSlinfeng     unsafe {
1116*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1117*fae6e9adSlinfeng     }
1118*fae6e9adSlinfeng }
1119*fae6e9adSlinfeng 
1120*fae6e9adSlinfeng #[test]
test_jit_jsgt_imm()1121*fae6e9adSlinfeng fn test_jit_jsgt_imm() {
1122*fae6e9adSlinfeng     let prog = assemble(
1123*fae6e9adSlinfeng         "
1124*fae6e9adSlinfeng         mov32 r0, 0
1125*fae6e9adSlinfeng         mov r1, -2
1126*fae6e9adSlinfeng         jsgt r1, -1, +4
1127*fae6e9adSlinfeng         mov32 r0, 1
1128*fae6e9adSlinfeng         mov32 r1, 0
1129*fae6e9adSlinfeng         jsgt r1, -1, +1
1130*fae6e9adSlinfeng         mov32 r0, 2
1131*fae6e9adSlinfeng         exit",
1132*fae6e9adSlinfeng     )
1133*fae6e9adSlinfeng     .unwrap();
1134*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1135*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1136*fae6e9adSlinfeng     unsafe {
1137*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1138*fae6e9adSlinfeng     }
1139*fae6e9adSlinfeng }
1140*fae6e9adSlinfeng 
1141*fae6e9adSlinfeng #[test]
test_jit_jsgt_reg()1142*fae6e9adSlinfeng fn test_jit_jsgt_reg() {
1143*fae6e9adSlinfeng     let prog = assemble(
1144*fae6e9adSlinfeng         "
1145*fae6e9adSlinfeng         mov32 r0, 0
1146*fae6e9adSlinfeng         mov r1, -2
1147*fae6e9adSlinfeng         mov r2, -1
1148*fae6e9adSlinfeng         jsgt r1, r2, +4
1149*fae6e9adSlinfeng         mov32 r0, 1
1150*fae6e9adSlinfeng         mov32 r1, 0
1151*fae6e9adSlinfeng         jsgt r1, r2, +1
1152*fae6e9adSlinfeng         mov32 r0, 2
1153*fae6e9adSlinfeng         exit",
1154*fae6e9adSlinfeng     )
1155*fae6e9adSlinfeng     .unwrap();
1156*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1157*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1158*fae6e9adSlinfeng     unsafe {
1159*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1160*fae6e9adSlinfeng     }
1161*fae6e9adSlinfeng }
1162*fae6e9adSlinfeng 
1163*fae6e9adSlinfeng #[test]
test_jit_jslt_imm()1164*fae6e9adSlinfeng fn test_jit_jslt_imm() {
1165*fae6e9adSlinfeng     let prog = assemble(
1166*fae6e9adSlinfeng         "
1167*fae6e9adSlinfeng         mov32 r0, 0
1168*fae6e9adSlinfeng         mov r1, -2
1169*fae6e9adSlinfeng         jslt r1, -3, +2
1170*fae6e9adSlinfeng         jslt r1, -2, +1
1171*fae6e9adSlinfeng         jslt r1, -1, +1
1172*fae6e9adSlinfeng         exit
1173*fae6e9adSlinfeng         mov32 r0, 1
1174*fae6e9adSlinfeng         exit",
1175*fae6e9adSlinfeng     )
1176*fae6e9adSlinfeng     .unwrap();
1177*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1178*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1179*fae6e9adSlinfeng     unsafe {
1180*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1181*fae6e9adSlinfeng     }
1182*fae6e9adSlinfeng }
1183*fae6e9adSlinfeng 
1184*fae6e9adSlinfeng #[test]
test_jit_jslt_reg()1185*fae6e9adSlinfeng fn test_jit_jslt_reg() {
1186*fae6e9adSlinfeng     let prog = assemble(
1187*fae6e9adSlinfeng         "
1188*fae6e9adSlinfeng         mov32 r0, 0
1189*fae6e9adSlinfeng         mov r1, -2
1190*fae6e9adSlinfeng         mov r2, -3
1191*fae6e9adSlinfeng         mov r3, -1
1192*fae6e9adSlinfeng         jslt r1, r1, +2
1193*fae6e9adSlinfeng         jslt r1, r2, +1
1194*fae6e9adSlinfeng         jslt r1, r3, +1
1195*fae6e9adSlinfeng         exit
1196*fae6e9adSlinfeng         mov32 r0, 1
1197*fae6e9adSlinfeng         exit",
1198*fae6e9adSlinfeng     )
1199*fae6e9adSlinfeng     .unwrap();
1200*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1201*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1202*fae6e9adSlinfeng     unsafe {
1203*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1204*fae6e9adSlinfeng     }
1205*fae6e9adSlinfeng }
1206*fae6e9adSlinfeng 
1207*fae6e9adSlinfeng #[test]
test_jit_jeq32_imm()1208*fae6e9adSlinfeng fn test_jit_jeq32_imm() {
1209*fae6e9adSlinfeng     let prog = assemble(
1210*fae6e9adSlinfeng         "
1211*fae6e9adSlinfeng         mov r9, 1
1212*fae6e9adSlinfeng         lsh r9, 32
1213*fae6e9adSlinfeng         mov32 r0, 0x0
1214*fae6e9adSlinfeng         mov32 r1, 0xa
1215*fae6e9adSlinfeng         jeq32 r1, 0xb, +5
1216*fae6e9adSlinfeng         mov32 r0, 1
1217*fae6e9adSlinfeng         mov r1, 0xb
1218*fae6e9adSlinfeng         or r1, r9
1219*fae6e9adSlinfeng         jeq32 r1, 0xb, +1
1220*fae6e9adSlinfeng         mov32 r0, 2
1221*fae6e9adSlinfeng         exit",
1222*fae6e9adSlinfeng     )
1223*fae6e9adSlinfeng     .unwrap();
1224*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1225*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1226*fae6e9adSlinfeng     unsafe {
1227*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1228*fae6e9adSlinfeng     }
1229*fae6e9adSlinfeng }
1230*fae6e9adSlinfeng 
1231*fae6e9adSlinfeng #[test]
test_jit_jeq32_reg()1232*fae6e9adSlinfeng fn test_jit_jeq32_reg() {
1233*fae6e9adSlinfeng     let prog = assemble(
1234*fae6e9adSlinfeng         "
1235*fae6e9adSlinfeng         mov r9, 1
1236*fae6e9adSlinfeng         lsh r9, 32
1237*fae6e9adSlinfeng         mov32 r0, 0
1238*fae6e9adSlinfeng         mov32 r1, 0xa
1239*fae6e9adSlinfeng         mov32 r2, 0xb
1240*fae6e9adSlinfeng         jeq32 r1, r2, +5
1241*fae6e9adSlinfeng         mov32 r0, 1
1242*fae6e9adSlinfeng         mov32 r1, 0xb
1243*fae6e9adSlinfeng         or r1, r9
1244*fae6e9adSlinfeng         jeq32 r1, r2, +1
1245*fae6e9adSlinfeng         mov32 r0, 2
1246*fae6e9adSlinfeng         exit",
1247*fae6e9adSlinfeng     )
1248*fae6e9adSlinfeng     .unwrap();
1249*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1250*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1251*fae6e9adSlinfeng     unsafe {
1252*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1253*fae6e9adSlinfeng     }
1254*fae6e9adSlinfeng }
1255*fae6e9adSlinfeng 
1256*fae6e9adSlinfeng #[test]
test_jit_jge32_imm()1257*fae6e9adSlinfeng fn test_jit_jge32_imm() {
1258*fae6e9adSlinfeng     let prog = assemble(
1259*fae6e9adSlinfeng         "
1260*fae6e9adSlinfeng         mov r9, 1
1261*fae6e9adSlinfeng         lsh r9, 32
1262*fae6e9adSlinfeng         mov32 r0, 0
1263*fae6e9adSlinfeng         mov32 r1, 0xa
1264*fae6e9adSlinfeng         jge32 r1, 0xb, +5
1265*fae6e9adSlinfeng         mov32 r0, 1
1266*fae6e9adSlinfeng         or r1, r9
1267*fae6e9adSlinfeng         mov32 r1, 0xc
1268*fae6e9adSlinfeng         jge32 r1, 0xb, +1
1269*fae6e9adSlinfeng         mov32 r0, 2
1270*fae6e9adSlinfeng         exit",
1271*fae6e9adSlinfeng     )
1272*fae6e9adSlinfeng     .unwrap();
1273*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1274*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1275*fae6e9adSlinfeng     unsafe {
1276*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1277*fae6e9adSlinfeng     }
1278*fae6e9adSlinfeng }
1279*fae6e9adSlinfeng 
1280*fae6e9adSlinfeng #[test]
test_jit_jge32_reg()1281*fae6e9adSlinfeng fn test_jit_jge32_reg() {
1282*fae6e9adSlinfeng     let prog = assemble(
1283*fae6e9adSlinfeng         "
1284*fae6e9adSlinfeng         mov r9, 1
1285*fae6e9adSlinfeng         lsh r9, 32
1286*fae6e9adSlinfeng         mov32 r0, 0
1287*fae6e9adSlinfeng         mov32 r1, 0xa
1288*fae6e9adSlinfeng         mov32 r2, 0xb
1289*fae6e9adSlinfeng         jge32 r1, r2, +5
1290*fae6e9adSlinfeng         mov32 r0, 1
1291*fae6e9adSlinfeng         or r1, r9
1292*fae6e9adSlinfeng         mov32 r1, 0xc
1293*fae6e9adSlinfeng         jge32 r1, r2, +1
1294*fae6e9adSlinfeng         mov32 r0, 2
1295*fae6e9adSlinfeng         exit",
1296*fae6e9adSlinfeng     )
1297*fae6e9adSlinfeng     .unwrap();
1298*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1299*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1300*fae6e9adSlinfeng     unsafe {
1301*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1302*fae6e9adSlinfeng     }
1303*fae6e9adSlinfeng }
1304*fae6e9adSlinfeng 
1305*fae6e9adSlinfeng #[test]
test_jit_jgt32_imm()1306*fae6e9adSlinfeng fn test_jit_jgt32_imm() {
1307*fae6e9adSlinfeng     let prog = assemble(
1308*fae6e9adSlinfeng         "
1309*fae6e9adSlinfeng         mov r9, 1
1310*fae6e9adSlinfeng         lsh r9, 32
1311*fae6e9adSlinfeng         mov32 r0, 0
1312*fae6e9adSlinfeng         mov32 r1, 5
1313*fae6e9adSlinfeng         or r1, r9
1314*fae6e9adSlinfeng         jgt32 r1, 6, +4
1315*fae6e9adSlinfeng         jgt32 r1, 5, +3
1316*fae6e9adSlinfeng         jgt32 r1, 4, +1
1317*fae6e9adSlinfeng         exit
1318*fae6e9adSlinfeng         mov32 r0, 1
1319*fae6e9adSlinfeng         exit",
1320*fae6e9adSlinfeng     )
1321*fae6e9adSlinfeng     .unwrap();
1322*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1323*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1324*fae6e9adSlinfeng     unsafe {
1325*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1326*fae6e9adSlinfeng     }
1327*fae6e9adSlinfeng }
1328*fae6e9adSlinfeng 
1329*fae6e9adSlinfeng #[test]
test_jit_jgt32_reg()1330*fae6e9adSlinfeng fn test_jit_jgt32_reg() {
1331*fae6e9adSlinfeng     let prog = assemble(
1332*fae6e9adSlinfeng         "
1333*fae6e9adSlinfeng         mov r9, 1
1334*fae6e9adSlinfeng         lsh r9, 32
1335*fae6e9adSlinfeng         mov r0, 0
1336*fae6e9adSlinfeng         mov r1, 5
1337*fae6e9adSlinfeng         mov32 r1, 5
1338*fae6e9adSlinfeng         or r1, r9
1339*fae6e9adSlinfeng         mov r2, 6
1340*fae6e9adSlinfeng         mov r3, 4
1341*fae6e9adSlinfeng         jgt32 r1, r2, +4
1342*fae6e9adSlinfeng         jgt32 r1, r1, +3
1343*fae6e9adSlinfeng         jgt32 r1, r3, +1
1344*fae6e9adSlinfeng         exit
1345*fae6e9adSlinfeng         mov r0, 1
1346*fae6e9adSlinfeng         exit",
1347*fae6e9adSlinfeng     )
1348*fae6e9adSlinfeng     .unwrap();
1349*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1350*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1351*fae6e9adSlinfeng     unsafe {
1352*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1353*fae6e9adSlinfeng     }
1354*fae6e9adSlinfeng }
1355*fae6e9adSlinfeng 
1356*fae6e9adSlinfeng #[test]
test_jit_jle32_imm()1357*fae6e9adSlinfeng fn test_jit_jle32_imm() {
1358*fae6e9adSlinfeng     let prog = assemble(
1359*fae6e9adSlinfeng         "
1360*fae6e9adSlinfeng         mov r9, 1
1361*fae6e9adSlinfeng         lsh r9, 32
1362*fae6e9adSlinfeng         mov32 r0, 0
1363*fae6e9adSlinfeng         mov32 r1, 5
1364*fae6e9adSlinfeng         or r1, r9
1365*fae6e9adSlinfeng         jle32 r1, 4, +5
1366*fae6e9adSlinfeng         jle32 r1, 6, +1
1367*fae6e9adSlinfeng         exit
1368*fae6e9adSlinfeng         jle32 r1, 5, +1
1369*fae6e9adSlinfeng         exit
1370*fae6e9adSlinfeng         mov32 r0, 1
1371*fae6e9adSlinfeng         exit",
1372*fae6e9adSlinfeng     )
1373*fae6e9adSlinfeng     .unwrap();
1374*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1375*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1376*fae6e9adSlinfeng     unsafe {
1377*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1378*fae6e9adSlinfeng     }
1379*fae6e9adSlinfeng }
1380*fae6e9adSlinfeng 
1381*fae6e9adSlinfeng #[test]
test_jit_jle32_reg()1382*fae6e9adSlinfeng fn test_jit_jle32_reg() {
1383*fae6e9adSlinfeng     let prog = assemble(
1384*fae6e9adSlinfeng         "
1385*fae6e9adSlinfeng         mov r9, 1
1386*fae6e9adSlinfeng         lsh r9, 32
1387*fae6e9adSlinfeng         mov r0, 0
1388*fae6e9adSlinfeng         mov r1, 5
1389*fae6e9adSlinfeng         mov r2, 4
1390*fae6e9adSlinfeng         mov r3, 6
1391*fae6e9adSlinfeng         or r1, r9
1392*fae6e9adSlinfeng         jle32 r1, r2, +5
1393*fae6e9adSlinfeng         jle32 r1, r1, +1
1394*fae6e9adSlinfeng         exit
1395*fae6e9adSlinfeng         jle32 r1, r3, +1
1396*fae6e9adSlinfeng         exit
1397*fae6e9adSlinfeng         mov r0, 1
1398*fae6e9adSlinfeng         exit",
1399*fae6e9adSlinfeng     )
1400*fae6e9adSlinfeng     .unwrap();
1401*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1402*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1403*fae6e9adSlinfeng     unsafe {
1404*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1405*fae6e9adSlinfeng     }
1406*fae6e9adSlinfeng }
1407*fae6e9adSlinfeng 
1408*fae6e9adSlinfeng #[test]
test_jit_jlt32_imm()1409*fae6e9adSlinfeng fn test_jit_jlt32_imm() {
1410*fae6e9adSlinfeng     let prog = assemble(
1411*fae6e9adSlinfeng         "
1412*fae6e9adSlinfeng         mov r9, 1
1413*fae6e9adSlinfeng         lsh r9, 32
1414*fae6e9adSlinfeng         mov32 r0, 0
1415*fae6e9adSlinfeng         mov32 r1, 5
1416*fae6e9adSlinfeng         or r1, r9
1417*fae6e9adSlinfeng         jlt32 r1, 4, +4
1418*fae6e9adSlinfeng         jlt32 r1, 5, +3
1419*fae6e9adSlinfeng         jlt32 r1, 6, +1
1420*fae6e9adSlinfeng         exit
1421*fae6e9adSlinfeng         mov32 r0, 1
1422*fae6e9adSlinfeng         exit",
1423*fae6e9adSlinfeng     )
1424*fae6e9adSlinfeng     .unwrap();
1425*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1426*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1427*fae6e9adSlinfeng     unsafe {
1428*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1429*fae6e9adSlinfeng     }
1430*fae6e9adSlinfeng }
1431*fae6e9adSlinfeng 
1432*fae6e9adSlinfeng #[test]
test_jit_jlt32_reg()1433*fae6e9adSlinfeng fn test_jit_jlt32_reg() {
1434*fae6e9adSlinfeng     let prog = assemble(
1435*fae6e9adSlinfeng         "
1436*fae6e9adSlinfeng         mov r9, 1
1437*fae6e9adSlinfeng         lsh r9, 32
1438*fae6e9adSlinfeng         mov r0, 0
1439*fae6e9adSlinfeng         mov r1, 5
1440*fae6e9adSlinfeng         mov r2, 4
1441*fae6e9adSlinfeng         mov r3, 6
1442*fae6e9adSlinfeng         or r1, r9
1443*fae6e9adSlinfeng         jlt32 r1, r2, +4
1444*fae6e9adSlinfeng         jlt32 r1, r1, +3
1445*fae6e9adSlinfeng         jlt32 r1, r3, +1
1446*fae6e9adSlinfeng         exit
1447*fae6e9adSlinfeng         mov r0, 1
1448*fae6e9adSlinfeng         exit",
1449*fae6e9adSlinfeng     )
1450*fae6e9adSlinfeng     .unwrap();
1451*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1452*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1453*fae6e9adSlinfeng     unsafe {
1454*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1455*fae6e9adSlinfeng     }
1456*fae6e9adSlinfeng }
1457*fae6e9adSlinfeng 
1458*fae6e9adSlinfeng #[test]
test_jit_jne32_imm()1459*fae6e9adSlinfeng fn test_jit_jne32_imm() {
1460*fae6e9adSlinfeng     let prog = assemble(
1461*fae6e9adSlinfeng         "
1462*fae6e9adSlinfeng         mov r9, 1
1463*fae6e9adSlinfeng         lsh r9, 32
1464*fae6e9adSlinfeng         mov32 r0, 0
1465*fae6e9adSlinfeng         mov32 r1, 0xb
1466*fae6e9adSlinfeng         or r1, r9
1467*fae6e9adSlinfeng         jne32 r1, 0xb, +4
1468*fae6e9adSlinfeng         mov32 r0, 1
1469*fae6e9adSlinfeng         mov32 r1, 0xa
1470*fae6e9adSlinfeng         or r1, r9
1471*fae6e9adSlinfeng         jne32 r1, 0xb, +1
1472*fae6e9adSlinfeng         mov32 r0, 2
1473*fae6e9adSlinfeng         exit",
1474*fae6e9adSlinfeng     )
1475*fae6e9adSlinfeng     .unwrap();
1476*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1477*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1478*fae6e9adSlinfeng     unsafe {
1479*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1480*fae6e9adSlinfeng     }
1481*fae6e9adSlinfeng }
1482*fae6e9adSlinfeng 
1483*fae6e9adSlinfeng #[test]
test_jit_jne32_reg()1484*fae6e9adSlinfeng fn test_jit_jne32_reg() {
1485*fae6e9adSlinfeng     let prog = assemble(
1486*fae6e9adSlinfeng         "
1487*fae6e9adSlinfeng         mov r9, 1
1488*fae6e9adSlinfeng         lsh r9, 32
1489*fae6e9adSlinfeng         mov32 r0, 0
1490*fae6e9adSlinfeng         mov32 r1, 0xb
1491*fae6e9adSlinfeng         or r1, r9
1492*fae6e9adSlinfeng         mov32 r2, 0xb
1493*fae6e9adSlinfeng         jne32 r1, r2, +4
1494*fae6e9adSlinfeng         mov32 r0, 1
1495*fae6e9adSlinfeng         mov32 r1, 0xa
1496*fae6e9adSlinfeng         or r1, r9
1497*fae6e9adSlinfeng         jne32 r1, r2, +1
1498*fae6e9adSlinfeng         mov32 r0, 2
1499*fae6e9adSlinfeng         exit",
1500*fae6e9adSlinfeng     )
1501*fae6e9adSlinfeng     .unwrap();
1502*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1503*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1504*fae6e9adSlinfeng     unsafe {
1505*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1506*fae6e9adSlinfeng     }
1507*fae6e9adSlinfeng }
1508*fae6e9adSlinfeng 
1509*fae6e9adSlinfeng #[test]
test_jit_jset32_imm()1510*fae6e9adSlinfeng fn test_jit_jset32_imm() {
1511*fae6e9adSlinfeng     let prog = assemble(
1512*fae6e9adSlinfeng         "
1513*fae6e9adSlinfeng         mov r9, 1
1514*fae6e9adSlinfeng         lsh r9, 32
1515*fae6e9adSlinfeng         mov32 r0, 0
1516*fae6e9adSlinfeng         mov32 r1, 0x7
1517*fae6e9adSlinfeng         or r1, r9
1518*fae6e9adSlinfeng         jset32 r1, 0x8, +4
1519*fae6e9adSlinfeng         mov32 r0, 1
1520*fae6e9adSlinfeng         mov32 r1, 0x9
1521*fae6e9adSlinfeng         jset32 r1, 0x8, +1
1522*fae6e9adSlinfeng         mov32 r0, 2
1523*fae6e9adSlinfeng         exit",
1524*fae6e9adSlinfeng     )
1525*fae6e9adSlinfeng     .unwrap();
1526*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1527*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1528*fae6e9adSlinfeng     unsafe {
1529*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1530*fae6e9adSlinfeng     }
1531*fae6e9adSlinfeng }
1532*fae6e9adSlinfeng 
1533*fae6e9adSlinfeng #[test]
test_jit_jset32_reg()1534*fae6e9adSlinfeng fn test_jit_jset32_reg() {
1535*fae6e9adSlinfeng     let prog = assemble(
1536*fae6e9adSlinfeng         "
1537*fae6e9adSlinfeng         mov r9, 1
1538*fae6e9adSlinfeng         lsh r9, 32
1539*fae6e9adSlinfeng         mov32 r0, 0
1540*fae6e9adSlinfeng         mov32 r1, 0x7
1541*fae6e9adSlinfeng         or r1, r9
1542*fae6e9adSlinfeng         mov32 r2, 0x8
1543*fae6e9adSlinfeng         jset32 r1, r2, +4
1544*fae6e9adSlinfeng         mov32 r0, 1
1545*fae6e9adSlinfeng         mov32 r1, 0x9
1546*fae6e9adSlinfeng         jset32 r1, r2, +1
1547*fae6e9adSlinfeng         mov32 r0, 2
1548*fae6e9adSlinfeng         exit",
1549*fae6e9adSlinfeng     )
1550*fae6e9adSlinfeng     .unwrap();
1551*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1552*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1553*fae6e9adSlinfeng     unsafe {
1554*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1555*fae6e9adSlinfeng     }
1556*fae6e9adSlinfeng }
1557*fae6e9adSlinfeng 
1558*fae6e9adSlinfeng #[test]
test_jit_jsge32_imm()1559*fae6e9adSlinfeng fn test_jit_jsge32_imm() {
1560*fae6e9adSlinfeng     let prog = assemble(
1561*fae6e9adSlinfeng         "
1562*fae6e9adSlinfeng         mov r9, 1
1563*fae6e9adSlinfeng         lsh r9, 32
1564*fae6e9adSlinfeng         mov32 r0, 0
1565*fae6e9adSlinfeng         mov32 r1, -2
1566*fae6e9adSlinfeng         or r1, r9
1567*fae6e9adSlinfeng         jsge32 r1, -1, +5
1568*fae6e9adSlinfeng         jsge32 r1, 0, +4
1569*fae6e9adSlinfeng         mov32 r0, 1
1570*fae6e9adSlinfeng         mov r1, -1
1571*fae6e9adSlinfeng         jsge32 r1, -1, +1
1572*fae6e9adSlinfeng         mov32 r0, 2
1573*fae6e9adSlinfeng         exit",
1574*fae6e9adSlinfeng     )
1575*fae6e9adSlinfeng     .unwrap();
1576*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1577*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1578*fae6e9adSlinfeng     unsafe {
1579*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1580*fae6e9adSlinfeng     }
1581*fae6e9adSlinfeng }
1582*fae6e9adSlinfeng 
1583*fae6e9adSlinfeng #[test]
test_jit_jsge32_reg()1584*fae6e9adSlinfeng fn test_jit_jsge32_reg() {
1585*fae6e9adSlinfeng     let prog = assemble(
1586*fae6e9adSlinfeng         "
1587*fae6e9adSlinfeng         mov r9, 1
1588*fae6e9adSlinfeng         lsh r9, 32
1589*fae6e9adSlinfeng         mov32 r0, 0
1590*fae6e9adSlinfeng         mov32 r1, -2
1591*fae6e9adSlinfeng         or r1, r9
1592*fae6e9adSlinfeng         mov r2, -1
1593*fae6e9adSlinfeng         mov32 r3, 0
1594*fae6e9adSlinfeng         jsge32 r1, r2, +5
1595*fae6e9adSlinfeng         jsge32 r1, r3, +4
1596*fae6e9adSlinfeng         mov32 r0, 1
1597*fae6e9adSlinfeng         mov r1, r2
1598*fae6e9adSlinfeng         jsge32 r1, r2, +1
1599*fae6e9adSlinfeng         mov32 r0, 2
1600*fae6e9adSlinfeng         exit",
1601*fae6e9adSlinfeng     )
1602*fae6e9adSlinfeng     .unwrap();
1603*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1604*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1605*fae6e9adSlinfeng     unsafe {
1606*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1607*fae6e9adSlinfeng     }
1608*fae6e9adSlinfeng }
1609*fae6e9adSlinfeng 
1610*fae6e9adSlinfeng #[test]
test_jit_jsgt32_imm()1611*fae6e9adSlinfeng fn test_jit_jsgt32_imm() {
1612*fae6e9adSlinfeng     let prog = assemble(
1613*fae6e9adSlinfeng         "
1614*fae6e9adSlinfeng         mov r9, 1
1615*fae6e9adSlinfeng         lsh r9, 32
1616*fae6e9adSlinfeng         mov32 r0, 0
1617*fae6e9adSlinfeng         mov32 r1, -2
1618*fae6e9adSlinfeng         or r1, r9
1619*fae6e9adSlinfeng         jsgt32 r1, -1, +4
1620*fae6e9adSlinfeng         mov32 r0, 1
1621*fae6e9adSlinfeng         mov32 r1, 0
1622*fae6e9adSlinfeng         jsgt32 r1, -1, +1
1623*fae6e9adSlinfeng         mov32 r0, 2
1624*fae6e9adSlinfeng         exit",
1625*fae6e9adSlinfeng     )
1626*fae6e9adSlinfeng     .unwrap();
1627*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1628*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1629*fae6e9adSlinfeng     unsafe {
1630*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1631*fae6e9adSlinfeng     }
1632*fae6e9adSlinfeng }
1633*fae6e9adSlinfeng 
1634*fae6e9adSlinfeng #[test]
test_jit_jsgt32_reg()1635*fae6e9adSlinfeng fn test_jit_jsgt32_reg() {
1636*fae6e9adSlinfeng     let prog = assemble(
1637*fae6e9adSlinfeng         "
1638*fae6e9adSlinfeng         mov r9, 1
1639*fae6e9adSlinfeng         lsh r9, 32
1640*fae6e9adSlinfeng         mov32 r0, 0
1641*fae6e9adSlinfeng         mov32 r1, -2
1642*fae6e9adSlinfeng         or r1, r9
1643*fae6e9adSlinfeng         mov r2, -1
1644*fae6e9adSlinfeng         jsgt32 r1, r2, +4
1645*fae6e9adSlinfeng         mov32 r0, 1
1646*fae6e9adSlinfeng         mov32 r1, 0
1647*fae6e9adSlinfeng         jsgt32 r1, r2, +1
1648*fae6e9adSlinfeng         mov32 r0, 2
1649*fae6e9adSlinfeng         exit",
1650*fae6e9adSlinfeng     )
1651*fae6e9adSlinfeng     .unwrap();
1652*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1653*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1654*fae6e9adSlinfeng     unsafe {
1655*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1656*fae6e9adSlinfeng     }
1657*fae6e9adSlinfeng }
1658*fae6e9adSlinfeng 
1659*fae6e9adSlinfeng #[test]
test_jit_jsle32_imm()1660*fae6e9adSlinfeng fn test_jit_jsle32_imm() {
1661*fae6e9adSlinfeng     let prog = assemble(
1662*fae6e9adSlinfeng         "
1663*fae6e9adSlinfeng         mov r9, 1
1664*fae6e9adSlinfeng         lsh r9, 32
1665*fae6e9adSlinfeng         mov32 r0, 0
1666*fae6e9adSlinfeng         mov32 r1, -2
1667*fae6e9adSlinfeng         or r1, r9
1668*fae6e9adSlinfeng         jsle32 r1, -3, +5
1669*fae6e9adSlinfeng         jsle32 r1, -1, +1
1670*fae6e9adSlinfeng         exit
1671*fae6e9adSlinfeng         mov32 r0, 1
1672*fae6e9adSlinfeng         jsle32 r1, -2, +1
1673*fae6e9adSlinfeng         mov32 r0, 2
1674*fae6e9adSlinfeng         exit",
1675*fae6e9adSlinfeng     )
1676*fae6e9adSlinfeng     .unwrap();
1677*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1678*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1679*fae6e9adSlinfeng     unsafe {
1680*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1681*fae6e9adSlinfeng     }
1682*fae6e9adSlinfeng }
1683*fae6e9adSlinfeng 
1684*fae6e9adSlinfeng #[test]
test_jit_jsle32_reg()1685*fae6e9adSlinfeng fn test_jit_jsle32_reg() {
1686*fae6e9adSlinfeng     let prog = assemble(
1687*fae6e9adSlinfeng         "
1688*fae6e9adSlinfeng         mov r9, 1
1689*fae6e9adSlinfeng         lsh r9, 32
1690*fae6e9adSlinfeng         mov32 r0, 0
1691*fae6e9adSlinfeng         mov32 r1, -2
1692*fae6e9adSlinfeng         or r1, r9
1693*fae6e9adSlinfeng         mov r2, -3
1694*fae6e9adSlinfeng         mov32 r3, 0
1695*fae6e9adSlinfeng         jsle32 r1, r2, +6
1696*fae6e9adSlinfeng         jsle32 r1, r3, +1
1697*fae6e9adSlinfeng         exit
1698*fae6e9adSlinfeng         mov32 r0, 1
1699*fae6e9adSlinfeng         mov r1, r2
1700*fae6e9adSlinfeng         jsle32 r1, r2, +1
1701*fae6e9adSlinfeng         mov32 r0, 2
1702*fae6e9adSlinfeng         exit",
1703*fae6e9adSlinfeng     )
1704*fae6e9adSlinfeng     .unwrap();
1705*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1706*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1707*fae6e9adSlinfeng     unsafe {
1708*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1709*fae6e9adSlinfeng     }
1710*fae6e9adSlinfeng }
1711*fae6e9adSlinfeng 
1712*fae6e9adSlinfeng #[test]
test_jit_jslt32_imm()1713*fae6e9adSlinfeng fn test_jit_jslt32_imm() {
1714*fae6e9adSlinfeng     let prog = assemble(
1715*fae6e9adSlinfeng         "
1716*fae6e9adSlinfeng         mov r9, 1
1717*fae6e9adSlinfeng         lsh r9, 32
1718*fae6e9adSlinfeng         mov32 r0, 0
1719*fae6e9adSlinfeng         mov32 r1, -2
1720*fae6e9adSlinfeng         or r1, r9
1721*fae6e9adSlinfeng         jslt32 r1, -3, +4
1722*fae6e9adSlinfeng         jslt32 r1, -2, +3
1723*fae6e9adSlinfeng         jslt32 r1, -1, +1
1724*fae6e9adSlinfeng         exit
1725*fae6e9adSlinfeng         mov32 r0, 1
1726*fae6e9adSlinfeng         exit",
1727*fae6e9adSlinfeng     )
1728*fae6e9adSlinfeng     .unwrap();
1729*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1730*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1731*fae6e9adSlinfeng     unsafe {
1732*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1733*fae6e9adSlinfeng     }
1734*fae6e9adSlinfeng }
1735*fae6e9adSlinfeng 
1736*fae6e9adSlinfeng #[test]
test_jit_jslt32_reg()1737*fae6e9adSlinfeng fn test_jit_jslt32_reg() {
1738*fae6e9adSlinfeng     let prog = assemble(
1739*fae6e9adSlinfeng         "
1740*fae6e9adSlinfeng         mov r9, 1
1741*fae6e9adSlinfeng         lsh r9, 32
1742*fae6e9adSlinfeng         mov32 r0, 0
1743*fae6e9adSlinfeng         mov32 r1, -2
1744*fae6e9adSlinfeng         or r1, r9
1745*fae6e9adSlinfeng         mov r2, -3
1746*fae6e9adSlinfeng         mov r3, -1
1747*fae6e9adSlinfeng         jslt32 r1, r1, +4
1748*fae6e9adSlinfeng         jslt32 r1, r2, +3
1749*fae6e9adSlinfeng         jslt32 r1, r3, +1
1750*fae6e9adSlinfeng         exit
1751*fae6e9adSlinfeng         mov32 r0, 1
1752*fae6e9adSlinfeng         exit",
1753*fae6e9adSlinfeng     )
1754*fae6e9adSlinfeng     .unwrap();
1755*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1756*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1757*fae6e9adSlinfeng     unsafe {
1758*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
1759*fae6e9adSlinfeng     }
1760*fae6e9adSlinfeng }
1761*fae6e9adSlinfeng 
1762*fae6e9adSlinfeng #[test]
test_jit_lddw()1763*fae6e9adSlinfeng fn test_jit_lddw() {
1764*fae6e9adSlinfeng     let prog = assemble(
1765*fae6e9adSlinfeng         "
1766*fae6e9adSlinfeng         lddw r0, 0x1122334455667788
1767*fae6e9adSlinfeng         exit",
1768*fae6e9adSlinfeng     )
1769*fae6e9adSlinfeng     .unwrap();
1770*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1771*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1772*fae6e9adSlinfeng     unsafe {
1773*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1122334455667788);
1774*fae6e9adSlinfeng     }
1775*fae6e9adSlinfeng }
1776*fae6e9adSlinfeng 
1777*fae6e9adSlinfeng #[test]
test_jit_lddw2()1778*fae6e9adSlinfeng fn test_jit_lddw2() {
1779*fae6e9adSlinfeng     let prog = assemble(
1780*fae6e9adSlinfeng         "
1781*fae6e9adSlinfeng         lddw r0, 0x0000000080000000
1782*fae6e9adSlinfeng         exit",
1783*fae6e9adSlinfeng     )
1784*fae6e9adSlinfeng     .unwrap();
1785*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
1786*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1787*fae6e9adSlinfeng     unsafe {
1788*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x80000000);
1789*fae6e9adSlinfeng     }
1790*fae6e9adSlinfeng }
1791*fae6e9adSlinfeng 
1792*fae6e9adSlinfeng #[test]
test_jit_ldxb_all()1793*fae6e9adSlinfeng fn test_jit_ldxb_all() {
1794*fae6e9adSlinfeng     let prog = assemble(
1795*fae6e9adSlinfeng         "
1796*fae6e9adSlinfeng         mov r0, r1
1797*fae6e9adSlinfeng         ldxb r9, [r0+0]
1798*fae6e9adSlinfeng         lsh r9, 0
1799*fae6e9adSlinfeng         ldxb r8, [r0+1]
1800*fae6e9adSlinfeng         lsh r8, 4
1801*fae6e9adSlinfeng         ldxb r7, [r0+2]
1802*fae6e9adSlinfeng         lsh r7, 8
1803*fae6e9adSlinfeng         ldxb r6, [r0+3]
1804*fae6e9adSlinfeng         lsh r6, 12
1805*fae6e9adSlinfeng         ldxb r5, [r0+4]
1806*fae6e9adSlinfeng         lsh r5, 16
1807*fae6e9adSlinfeng         ldxb r4, [r0+5]
1808*fae6e9adSlinfeng         lsh r4, 20
1809*fae6e9adSlinfeng         ldxb r3, [r0+6]
1810*fae6e9adSlinfeng         lsh r3, 24
1811*fae6e9adSlinfeng         ldxb r2, [r0+7]
1812*fae6e9adSlinfeng         lsh r2, 28
1813*fae6e9adSlinfeng         ldxb r1, [r0+8]
1814*fae6e9adSlinfeng         lsh r1, 32
1815*fae6e9adSlinfeng         ldxb r0, [r0+9]
1816*fae6e9adSlinfeng         lsh r0, 36
1817*fae6e9adSlinfeng         or r0, r1
1818*fae6e9adSlinfeng         or r0, r2
1819*fae6e9adSlinfeng         or r0, r3
1820*fae6e9adSlinfeng         or r0, r4
1821*fae6e9adSlinfeng         or r0, r5
1822*fae6e9adSlinfeng         or r0, r6
1823*fae6e9adSlinfeng         or r0, r7
1824*fae6e9adSlinfeng         or r0, r8
1825*fae6e9adSlinfeng         or r0, r9
1826*fae6e9adSlinfeng         exit",
1827*fae6e9adSlinfeng     )
1828*fae6e9adSlinfeng     .unwrap();
1829*fae6e9adSlinfeng     let mem = &mut [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09];
1830*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
1831*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1832*fae6e9adSlinfeng     unsafe {
1833*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9876543210);
1834*fae6e9adSlinfeng     }
1835*fae6e9adSlinfeng }
1836*fae6e9adSlinfeng 
1837*fae6e9adSlinfeng #[test]
test_jit_ldxb()1838*fae6e9adSlinfeng fn test_jit_ldxb() {
1839*fae6e9adSlinfeng     let prog = assemble(
1840*fae6e9adSlinfeng         "
1841*fae6e9adSlinfeng         ldxb r0, [r1+2]
1842*fae6e9adSlinfeng         exit",
1843*fae6e9adSlinfeng     )
1844*fae6e9adSlinfeng     .unwrap();
1845*fae6e9adSlinfeng     let mem = &mut [0xaa, 0xbb, 0x11, 0xcc, 0xdd];
1846*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
1847*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1848*fae6e9adSlinfeng     unsafe {
1849*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11);
1850*fae6e9adSlinfeng     }
1851*fae6e9adSlinfeng }
1852*fae6e9adSlinfeng 
1853*fae6e9adSlinfeng #[test]
test_jit_ldxdw()1854*fae6e9adSlinfeng fn test_jit_ldxdw() {
1855*fae6e9adSlinfeng     let prog = assemble(
1856*fae6e9adSlinfeng         "
1857*fae6e9adSlinfeng         ldxdw r0, [r1+2]
1858*fae6e9adSlinfeng         exit",
1859*fae6e9adSlinfeng     )
1860*fae6e9adSlinfeng     .unwrap();
1861*fae6e9adSlinfeng     let mem = &mut [
1862*fae6e9adSlinfeng         0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0xcc, 0xdd,
1863*fae6e9adSlinfeng     ];
1864*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
1865*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1866*fae6e9adSlinfeng     unsafe {
1867*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x8877665544332211);
1868*fae6e9adSlinfeng     }
1869*fae6e9adSlinfeng }
1870*fae6e9adSlinfeng 
1871*fae6e9adSlinfeng #[test]
test_jit_ldxh_all()1872*fae6e9adSlinfeng fn test_jit_ldxh_all() {
1873*fae6e9adSlinfeng     let prog = assemble(
1874*fae6e9adSlinfeng         "
1875*fae6e9adSlinfeng         mov r0, r1
1876*fae6e9adSlinfeng         ldxh r9, [r0+0]
1877*fae6e9adSlinfeng         be16 r9
1878*fae6e9adSlinfeng         lsh r9, 0
1879*fae6e9adSlinfeng         ldxh r8, [r0+2]
1880*fae6e9adSlinfeng         be16 r8
1881*fae6e9adSlinfeng         lsh r8, 4
1882*fae6e9adSlinfeng         ldxh r7, [r0+4]
1883*fae6e9adSlinfeng         be16 r7
1884*fae6e9adSlinfeng         lsh r7, 8
1885*fae6e9adSlinfeng         ldxh r6, [r0+6]
1886*fae6e9adSlinfeng         be16 r6
1887*fae6e9adSlinfeng         lsh r6, 12
1888*fae6e9adSlinfeng         ldxh r5, [r0+8]
1889*fae6e9adSlinfeng         be16 r5
1890*fae6e9adSlinfeng         lsh r5, 16
1891*fae6e9adSlinfeng         ldxh r4, [r0+10]
1892*fae6e9adSlinfeng         be16 r4
1893*fae6e9adSlinfeng         lsh r4, 20
1894*fae6e9adSlinfeng         ldxh r3, [r0+12]
1895*fae6e9adSlinfeng         be16 r3
1896*fae6e9adSlinfeng         lsh r3, 24
1897*fae6e9adSlinfeng         ldxh r2, [r0+14]
1898*fae6e9adSlinfeng         be16 r2
1899*fae6e9adSlinfeng         lsh r2, 28
1900*fae6e9adSlinfeng         ldxh r1, [r0+16]
1901*fae6e9adSlinfeng         be16 r1
1902*fae6e9adSlinfeng         lsh r1, 32
1903*fae6e9adSlinfeng         ldxh r0, [r0+18]
1904*fae6e9adSlinfeng         be16 r0
1905*fae6e9adSlinfeng         lsh r0, 36
1906*fae6e9adSlinfeng         or r0, r1
1907*fae6e9adSlinfeng         or r0, r2
1908*fae6e9adSlinfeng         or r0, r3
1909*fae6e9adSlinfeng         or r0, r4
1910*fae6e9adSlinfeng         or r0, r5
1911*fae6e9adSlinfeng         or r0, r6
1912*fae6e9adSlinfeng         or r0, r7
1913*fae6e9adSlinfeng         or r0, r8
1914*fae6e9adSlinfeng         or r0, r9
1915*fae6e9adSlinfeng         exit",
1916*fae6e9adSlinfeng     )
1917*fae6e9adSlinfeng     .unwrap();
1918*fae6e9adSlinfeng     let mem = &mut [
1919*fae6e9adSlinfeng         0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00,
1920*fae6e9adSlinfeng         0x07, 0x00, 0x08, 0x00, 0x09,
1921*fae6e9adSlinfeng     ];
1922*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
1923*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1924*fae6e9adSlinfeng     unsafe {
1925*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x9876543210);
1926*fae6e9adSlinfeng     }
1927*fae6e9adSlinfeng }
1928*fae6e9adSlinfeng 
1929*fae6e9adSlinfeng #[test]
test_jit_ldxh_all2()1930*fae6e9adSlinfeng fn test_jit_ldxh_all2() {
1931*fae6e9adSlinfeng     let prog = assemble(
1932*fae6e9adSlinfeng         "
1933*fae6e9adSlinfeng         mov r0, r1
1934*fae6e9adSlinfeng         ldxh r9, [r0+0]
1935*fae6e9adSlinfeng         be16 r9
1936*fae6e9adSlinfeng         ldxh r8, [r0+2]
1937*fae6e9adSlinfeng         be16 r8
1938*fae6e9adSlinfeng         ldxh r7, [r0+4]
1939*fae6e9adSlinfeng         be16 r7
1940*fae6e9adSlinfeng         ldxh r6, [r0+6]
1941*fae6e9adSlinfeng         be16 r6
1942*fae6e9adSlinfeng         ldxh r5, [r0+8]
1943*fae6e9adSlinfeng         be16 r5
1944*fae6e9adSlinfeng         ldxh r4, [r0+10]
1945*fae6e9adSlinfeng         be16 r4
1946*fae6e9adSlinfeng         ldxh r3, [r0+12]
1947*fae6e9adSlinfeng         be16 r3
1948*fae6e9adSlinfeng         ldxh r2, [r0+14]
1949*fae6e9adSlinfeng         be16 r2
1950*fae6e9adSlinfeng         ldxh r1, [r0+16]
1951*fae6e9adSlinfeng         be16 r1
1952*fae6e9adSlinfeng         ldxh r0, [r0+18]
1953*fae6e9adSlinfeng         be16 r0
1954*fae6e9adSlinfeng         or r0, r1
1955*fae6e9adSlinfeng         or r0, r2
1956*fae6e9adSlinfeng         or r0, r3
1957*fae6e9adSlinfeng         or r0, r4
1958*fae6e9adSlinfeng         or r0, r5
1959*fae6e9adSlinfeng         or r0, r6
1960*fae6e9adSlinfeng         or r0, r7
1961*fae6e9adSlinfeng         or r0, r8
1962*fae6e9adSlinfeng         or r0, r9
1963*fae6e9adSlinfeng         exit",
1964*fae6e9adSlinfeng     )
1965*fae6e9adSlinfeng     .unwrap();
1966*fae6e9adSlinfeng     let mem = &mut [
1967*fae6e9adSlinfeng         0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00,
1968*fae6e9adSlinfeng         0x80, 0x01, 0x00, 0x02, 0x00,
1969*fae6e9adSlinfeng     ];
1970*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
1971*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1972*fae6e9adSlinfeng     unsafe {
1973*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x3ff);
1974*fae6e9adSlinfeng     }
1975*fae6e9adSlinfeng }
1976*fae6e9adSlinfeng 
1977*fae6e9adSlinfeng #[test]
test_jit_ldxh()1978*fae6e9adSlinfeng fn test_jit_ldxh() {
1979*fae6e9adSlinfeng     let prog = assemble(
1980*fae6e9adSlinfeng         "
1981*fae6e9adSlinfeng         ldxh r0, [r1+2]
1982*fae6e9adSlinfeng         exit",
1983*fae6e9adSlinfeng     )
1984*fae6e9adSlinfeng     .unwrap();
1985*fae6e9adSlinfeng     let mem = &mut [0xaa, 0xbb, 0x11, 0x22, 0xcc, 0xdd];
1986*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
1987*fae6e9adSlinfeng     vm.jit_compile().unwrap();
1988*fae6e9adSlinfeng     unsafe {
1989*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2211);
1990*fae6e9adSlinfeng     }
1991*fae6e9adSlinfeng }
1992*fae6e9adSlinfeng 
1993*fae6e9adSlinfeng #[test]
test_jit_ldxh_same_reg()1994*fae6e9adSlinfeng fn test_jit_ldxh_same_reg() {
1995*fae6e9adSlinfeng     let prog = assemble(
1996*fae6e9adSlinfeng         "
1997*fae6e9adSlinfeng         mov r0, r1
1998*fae6e9adSlinfeng         sth [r0], 0x1234
1999*fae6e9adSlinfeng         ldxh r0, [r0]
2000*fae6e9adSlinfeng         exit",
2001*fae6e9adSlinfeng     )
2002*fae6e9adSlinfeng     .unwrap();
2003*fae6e9adSlinfeng     let mem = &mut [0xff, 0xff];
2004*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2005*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2006*fae6e9adSlinfeng     unsafe {
2007*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1234);
2008*fae6e9adSlinfeng     }
2009*fae6e9adSlinfeng }
2010*fae6e9adSlinfeng 
2011*fae6e9adSlinfeng #[test]
test_jit_ldxw_all()2012*fae6e9adSlinfeng fn test_jit_ldxw_all() {
2013*fae6e9adSlinfeng     let prog = assemble(
2014*fae6e9adSlinfeng         "
2015*fae6e9adSlinfeng         mov r0, r1
2016*fae6e9adSlinfeng         ldxw r9, [r0+0]
2017*fae6e9adSlinfeng         be32 r9
2018*fae6e9adSlinfeng         ldxw r8, [r0+4]
2019*fae6e9adSlinfeng         be32 r8
2020*fae6e9adSlinfeng         ldxw r7, [r0+8]
2021*fae6e9adSlinfeng         be32 r7
2022*fae6e9adSlinfeng         ldxw r6, [r0+12]
2023*fae6e9adSlinfeng         be32 r6
2024*fae6e9adSlinfeng         ldxw r5, [r0+16]
2025*fae6e9adSlinfeng         be32 r5
2026*fae6e9adSlinfeng         ldxw r4, [r0+20]
2027*fae6e9adSlinfeng         be32 r4
2028*fae6e9adSlinfeng         ldxw r3, [r0+24]
2029*fae6e9adSlinfeng         be32 r3
2030*fae6e9adSlinfeng         ldxw r2, [r0+28]
2031*fae6e9adSlinfeng         be32 r2
2032*fae6e9adSlinfeng         ldxw r1, [r0+32]
2033*fae6e9adSlinfeng         be32 r1
2034*fae6e9adSlinfeng         ldxw r0, [r0+36]
2035*fae6e9adSlinfeng         be32 r0
2036*fae6e9adSlinfeng         or r0, r1
2037*fae6e9adSlinfeng         or r0, r2
2038*fae6e9adSlinfeng         or r0, r3
2039*fae6e9adSlinfeng         or r0, r4
2040*fae6e9adSlinfeng         or r0, r5
2041*fae6e9adSlinfeng         or r0, r6
2042*fae6e9adSlinfeng         or r0, r7
2043*fae6e9adSlinfeng         or r0, r8
2044*fae6e9adSlinfeng         or r0, r9
2045*fae6e9adSlinfeng         exit",
2046*fae6e9adSlinfeng     )
2047*fae6e9adSlinfeng     .unwrap();
2048*fae6e9adSlinfeng     let mem = &mut [
2049*fae6e9adSlinfeng         0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
2050*fae6e9adSlinfeng         0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
2051*fae6e9adSlinfeng         0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
2052*fae6e9adSlinfeng     ];
2053*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2054*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2055*fae6e9adSlinfeng     unsafe {
2056*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x030f0f);
2057*fae6e9adSlinfeng     }
2058*fae6e9adSlinfeng }
2059*fae6e9adSlinfeng 
2060*fae6e9adSlinfeng #[test]
test_jit_ldxw()2061*fae6e9adSlinfeng fn test_jit_ldxw() {
2062*fae6e9adSlinfeng     let prog = assemble(
2063*fae6e9adSlinfeng         "
2064*fae6e9adSlinfeng         ldxw r0, [r1+2]
2065*fae6e9adSlinfeng         exit",
2066*fae6e9adSlinfeng     )
2067*fae6e9adSlinfeng     .unwrap();
2068*fae6e9adSlinfeng     let mem = &mut [0xaa, 0xbb, 0x11, 0x22, 0x33, 0x44, 0xcc, 0xdd];
2069*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2070*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2071*fae6e9adSlinfeng     unsafe {
2072*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
2073*fae6e9adSlinfeng     }
2074*fae6e9adSlinfeng }
2075*fae6e9adSlinfeng 
2076*fae6e9adSlinfeng #[test]
test_jit_le16()2077*fae6e9adSlinfeng fn test_jit_le16() {
2078*fae6e9adSlinfeng     let prog = assemble(
2079*fae6e9adSlinfeng         "
2080*fae6e9adSlinfeng         ldxh r0, [r1]
2081*fae6e9adSlinfeng         le16 r0
2082*fae6e9adSlinfeng         exit",
2083*fae6e9adSlinfeng     )
2084*fae6e9adSlinfeng     .unwrap();
2085*fae6e9adSlinfeng     let mem = &mut [0x22, 0x11];
2086*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2087*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2088*fae6e9adSlinfeng     unsafe {
2089*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122);
2090*fae6e9adSlinfeng     }
2091*fae6e9adSlinfeng }
2092*fae6e9adSlinfeng 
2093*fae6e9adSlinfeng #[test]
test_jit_le32()2094*fae6e9adSlinfeng fn test_jit_le32() {
2095*fae6e9adSlinfeng     let prog = assemble(
2096*fae6e9adSlinfeng         "
2097*fae6e9adSlinfeng         ldxw r0, [r1]
2098*fae6e9adSlinfeng         le32 r0
2099*fae6e9adSlinfeng         exit",
2100*fae6e9adSlinfeng     )
2101*fae6e9adSlinfeng     .unwrap();
2102*fae6e9adSlinfeng     let mem = &mut [0x44, 0x33, 0x22, 0x11];
2103*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2104*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2105*fae6e9adSlinfeng     unsafe {
2106*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11223344);
2107*fae6e9adSlinfeng     }
2108*fae6e9adSlinfeng }
2109*fae6e9adSlinfeng 
2110*fae6e9adSlinfeng #[test]
test_jit_le64()2111*fae6e9adSlinfeng fn test_jit_le64() {
2112*fae6e9adSlinfeng     let prog = assemble(
2113*fae6e9adSlinfeng         "
2114*fae6e9adSlinfeng         ldxdw r0, [r1]
2115*fae6e9adSlinfeng         le64 r0
2116*fae6e9adSlinfeng         exit",
2117*fae6e9adSlinfeng     )
2118*fae6e9adSlinfeng     .unwrap();
2119*fae6e9adSlinfeng     let mem = &mut [0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11];
2120*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2121*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2122*fae6e9adSlinfeng     unsafe {
2123*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1122334455667788);
2124*fae6e9adSlinfeng     }
2125*fae6e9adSlinfeng }
2126*fae6e9adSlinfeng 
2127*fae6e9adSlinfeng #[test]
test_jit_lsh_reg()2128*fae6e9adSlinfeng fn test_jit_lsh_reg() {
2129*fae6e9adSlinfeng     let prog = assemble(
2130*fae6e9adSlinfeng         "
2131*fae6e9adSlinfeng         mov r0, 0x1
2132*fae6e9adSlinfeng         mov r7, 4
2133*fae6e9adSlinfeng         lsh r0, r7
2134*fae6e9adSlinfeng         exit",
2135*fae6e9adSlinfeng     )
2136*fae6e9adSlinfeng     .unwrap();
2137*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2138*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2139*fae6e9adSlinfeng     unsafe {
2140*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x10);
2141*fae6e9adSlinfeng     }
2142*fae6e9adSlinfeng }
2143*fae6e9adSlinfeng 
2144*fae6e9adSlinfeng #[test]
test_jit_mod()2145*fae6e9adSlinfeng fn test_jit_mod() {
2146*fae6e9adSlinfeng     let prog = assemble(
2147*fae6e9adSlinfeng         "
2148*fae6e9adSlinfeng         mov32 r0, 5748
2149*fae6e9adSlinfeng         mod32 r0, 92
2150*fae6e9adSlinfeng         mov32 r1, 13
2151*fae6e9adSlinfeng         mod32 r0, r1
2152*fae6e9adSlinfeng         exit",
2153*fae6e9adSlinfeng     )
2154*fae6e9adSlinfeng     .unwrap();
2155*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2156*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2157*fae6e9adSlinfeng     unsafe {
2158*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x5);
2159*fae6e9adSlinfeng     }
2160*fae6e9adSlinfeng }
2161*fae6e9adSlinfeng 
2162*fae6e9adSlinfeng #[test]
test_jit_mod32()2163*fae6e9adSlinfeng fn test_jit_mod32() {
2164*fae6e9adSlinfeng     let prog = assemble(
2165*fae6e9adSlinfeng         "
2166*fae6e9adSlinfeng         lddw r0, 0x100000003
2167*fae6e9adSlinfeng         mod32 r0, 3
2168*fae6e9adSlinfeng         exit",
2169*fae6e9adSlinfeng     )
2170*fae6e9adSlinfeng     .unwrap();
2171*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2172*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2173*fae6e9adSlinfeng     unsafe {
2174*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
2175*fae6e9adSlinfeng     }
2176*fae6e9adSlinfeng }
2177*fae6e9adSlinfeng 
2178*fae6e9adSlinfeng #[test]
test_jit_mod64()2179*fae6e9adSlinfeng fn test_jit_mod64() {
2180*fae6e9adSlinfeng     let prog = assemble(
2181*fae6e9adSlinfeng         "
2182*fae6e9adSlinfeng         mov32 r0, -1316649930
2183*fae6e9adSlinfeng         lsh r0, 32
2184*fae6e9adSlinfeng         or r0, 0x100dc5c8
2185*fae6e9adSlinfeng         mov32 r1, 0xdde263e
2186*fae6e9adSlinfeng         lsh r1, 32
2187*fae6e9adSlinfeng         or r1, 0x3cbef7f3
2188*fae6e9adSlinfeng         mod r0, r1
2189*fae6e9adSlinfeng         mod r0, 0x658f1778
2190*fae6e9adSlinfeng         exit",
2191*fae6e9adSlinfeng     )
2192*fae6e9adSlinfeng     .unwrap();
2193*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2194*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2195*fae6e9adSlinfeng     unsafe {
2196*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x30ba5a04);
2197*fae6e9adSlinfeng     }
2198*fae6e9adSlinfeng }
2199*fae6e9adSlinfeng 
2200*fae6e9adSlinfeng #[test]
test_jit_mov()2201*fae6e9adSlinfeng fn test_jit_mov() {
2202*fae6e9adSlinfeng     let prog = assemble(
2203*fae6e9adSlinfeng         "
2204*fae6e9adSlinfeng         mov32 r1, 1
2205*fae6e9adSlinfeng         mov32 r0, r1
2206*fae6e9adSlinfeng         exit",
2207*fae6e9adSlinfeng     )
2208*fae6e9adSlinfeng     .unwrap();
2209*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2210*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2211*fae6e9adSlinfeng     unsafe {
2212*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
2213*fae6e9adSlinfeng     }
2214*fae6e9adSlinfeng }
2215*fae6e9adSlinfeng 
2216*fae6e9adSlinfeng #[test]
test_jit_mul32_imm()2217*fae6e9adSlinfeng fn test_jit_mul32_imm() {
2218*fae6e9adSlinfeng     let prog = assemble(
2219*fae6e9adSlinfeng         "
2220*fae6e9adSlinfeng         mov r0, 3
2221*fae6e9adSlinfeng         mul32 r0, 4
2222*fae6e9adSlinfeng         exit",
2223*fae6e9adSlinfeng     )
2224*fae6e9adSlinfeng     .unwrap();
2225*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2226*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2227*fae6e9adSlinfeng     unsafe {
2228*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0xc);
2229*fae6e9adSlinfeng     }
2230*fae6e9adSlinfeng }
2231*fae6e9adSlinfeng 
2232*fae6e9adSlinfeng #[test]
test_jit_mul32_reg()2233*fae6e9adSlinfeng fn test_jit_mul32_reg() {
2234*fae6e9adSlinfeng     let prog = assemble(
2235*fae6e9adSlinfeng         "
2236*fae6e9adSlinfeng         mov r0, 3
2237*fae6e9adSlinfeng         mov r1, 4
2238*fae6e9adSlinfeng         mul32 r0, r1
2239*fae6e9adSlinfeng         exit",
2240*fae6e9adSlinfeng     )
2241*fae6e9adSlinfeng     .unwrap();
2242*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2243*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2244*fae6e9adSlinfeng     unsafe {
2245*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0xc);
2246*fae6e9adSlinfeng     }
2247*fae6e9adSlinfeng }
2248*fae6e9adSlinfeng 
2249*fae6e9adSlinfeng #[test]
test_jit_mul32_reg_overflow()2250*fae6e9adSlinfeng fn test_jit_mul32_reg_overflow() {
2251*fae6e9adSlinfeng     let prog = assemble(
2252*fae6e9adSlinfeng         "
2253*fae6e9adSlinfeng         mov r0, 0x40000001
2254*fae6e9adSlinfeng         mov r1, 4
2255*fae6e9adSlinfeng         mul32 r0, r1
2256*fae6e9adSlinfeng         exit",
2257*fae6e9adSlinfeng     )
2258*fae6e9adSlinfeng     .unwrap();
2259*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2260*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2261*fae6e9adSlinfeng     unsafe {
2262*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x4);
2263*fae6e9adSlinfeng     }
2264*fae6e9adSlinfeng }
2265*fae6e9adSlinfeng 
2266*fae6e9adSlinfeng #[test]
test_jit_mul64_imm()2267*fae6e9adSlinfeng fn test_jit_mul64_imm() {
2268*fae6e9adSlinfeng     let prog = assemble(
2269*fae6e9adSlinfeng         "
2270*fae6e9adSlinfeng         mov r0, 0x40000001
2271*fae6e9adSlinfeng         mul r0, 4
2272*fae6e9adSlinfeng         exit",
2273*fae6e9adSlinfeng     )
2274*fae6e9adSlinfeng     .unwrap();
2275*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2276*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2277*fae6e9adSlinfeng     unsafe {
2278*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x100000004);
2279*fae6e9adSlinfeng     }
2280*fae6e9adSlinfeng }
2281*fae6e9adSlinfeng 
2282*fae6e9adSlinfeng #[test]
test_jit_mul64_reg()2283*fae6e9adSlinfeng fn test_jit_mul64_reg() {
2284*fae6e9adSlinfeng     let prog = assemble(
2285*fae6e9adSlinfeng         "
2286*fae6e9adSlinfeng         mov r0, 0x40000001
2287*fae6e9adSlinfeng         mov r1, 4
2288*fae6e9adSlinfeng         mul r0, r1
2289*fae6e9adSlinfeng         exit",
2290*fae6e9adSlinfeng     )
2291*fae6e9adSlinfeng     .unwrap();
2292*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2293*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2294*fae6e9adSlinfeng     unsafe {
2295*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x100000004);
2296*fae6e9adSlinfeng     }
2297*fae6e9adSlinfeng }
2298*fae6e9adSlinfeng 
2299*fae6e9adSlinfeng #[test]
test_jit_mul_loop()2300*fae6e9adSlinfeng fn test_jit_mul_loop() {
2301*fae6e9adSlinfeng     let prog = assemble(
2302*fae6e9adSlinfeng         "
2303*fae6e9adSlinfeng         mov r0, 0x7
2304*fae6e9adSlinfeng         add r1, 0xa
2305*fae6e9adSlinfeng         lsh r1, 0x20
2306*fae6e9adSlinfeng         rsh r1, 0x20
2307*fae6e9adSlinfeng         jeq r1, 0x0, +4
2308*fae6e9adSlinfeng         mov r0, 0x7
2309*fae6e9adSlinfeng         mul r0, 0x7
2310*fae6e9adSlinfeng         add r1, -1
2311*fae6e9adSlinfeng         jne r1, 0x0, -3
2312*fae6e9adSlinfeng         exit",
2313*fae6e9adSlinfeng     )
2314*fae6e9adSlinfeng     .unwrap();
2315*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2316*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2317*fae6e9adSlinfeng     unsafe {
2318*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x75db9c97);
2319*fae6e9adSlinfeng     }
2320*fae6e9adSlinfeng }
2321*fae6e9adSlinfeng 
2322*fae6e9adSlinfeng #[test]
test_jit_neg64()2323*fae6e9adSlinfeng fn test_jit_neg64() {
2324*fae6e9adSlinfeng     let prog = assemble(
2325*fae6e9adSlinfeng         "
2326*fae6e9adSlinfeng         mov32 r0, 2
2327*fae6e9adSlinfeng         neg r0
2328*fae6e9adSlinfeng         exit",
2329*fae6e9adSlinfeng     )
2330*fae6e9adSlinfeng     .unwrap();
2331*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2332*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2333*fae6e9adSlinfeng     unsafe {
2334*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0xfffffffffffffffe);
2335*fae6e9adSlinfeng     }
2336*fae6e9adSlinfeng }
2337*fae6e9adSlinfeng 
2338*fae6e9adSlinfeng #[test]
test_jit_neg()2339*fae6e9adSlinfeng fn test_jit_neg() {
2340*fae6e9adSlinfeng     let prog = assemble(
2341*fae6e9adSlinfeng         "
2342*fae6e9adSlinfeng         mov32 r0, 2
2343*fae6e9adSlinfeng         neg32 r0
2344*fae6e9adSlinfeng         exit",
2345*fae6e9adSlinfeng     )
2346*fae6e9adSlinfeng     .unwrap();
2347*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2348*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2349*fae6e9adSlinfeng     unsafe {
2350*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0xfffffffe);
2351*fae6e9adSlinfeng     }
2352*fae6e9adSlinfeng }
2353*fae6e9adSlinfeng 
2354*fae6e9adSlinfeng #[test]
test_jit_prime()2355*fae6e9adSlinfeng fn test_jit_prime() {
2356*fae6e9adSlinfeng     let prog = assemble(
2357*fae6e9adSlinfeng         "
2358*fae6e9adSlinfeng         mov r1, 67
2359*fae6e9adSlinfeng         mov r0, 0x1
2360*fae6e9adSlinfeng         mov r2, 0x2
2361*fae6e9adSlinfeng         jgt r1, 0x2, +4
2362*fae6e9adSlinfeng         ja +10
2363*fae6e9adSlinfeng         add r2, 0x1
2364*fae6e9adSlinfeng         mov r0, 0x1
2365*fae6e9adSlinfeng         jge r2, r1, +7
2366*fae6e9adSlinfeng         mov r3, r1
2367*fae6e9adSlinfeng         div r3, r2
2368*fae6e9adSlinfeng         mul r3, r2
2369*fae6e9adSlinfeng         mov r4, r1
2370*fae6e9adSlinfeng         sub r4, r3
2371*fae6e9adSlinfeng         mov r0, 0x0
2372*fae6e9adSlinfeng         jne r4, 0x0, -10
2373*fae6e9adSlinfeng         exit",
2374*fae6e9adSlinfeng     )
2375*fae6e9adSlinfeng     .unwrap();
2376*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2377*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2378*fae6e9adSlinfeng     unsafe {
2379*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
2380*fae6e9adSlinfeng     }
2381*fae6e9adSlinfeng }
2382*fae6e9adSlinfeng 
2383*fae6e9adSlinfeng #[test]
test_jit_rhs32()2384*fae6e9adSlinfeng fn test_jit_rhs32() {
2385*fae6e9adSlinfeng     let prog = assemble(
2386*fae6e9adSlinfeng         "
2387*fae6e9adSlinfeng         xor r0, r0
2388*fae6e9adSlinfeng         sub r0, 1
2389*fae6e9adSlinfeng         rsh32 r0, 8
2390*fae6e9adSlinfeng         exit",
2391*fae6e9adSlinfeng     )
2392*fae6e9adSlinfeng     .unwrap();
2393*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2394*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2395*fae6e9adSlinfeng     unsafe {
2396*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x00ffffff);
2397*fae6e9adSlinfeng     }
2398*fae6e9adSlinfeng }
2399*fae6e9adSlinfeng 
2400*fae6e9adSlinfeng #[test]
test_jit_rsh_reg()2401*fae6e9adSlinfeng fn test_jit_rsh_reg() {
2402*fae6e9adSlinfeng     let prog = assemble(
2403*fae6e9adSlinfeng         "
2404*fae6e9adSlinfeng         mov r0, 0x10
2405*fae6e9adSlinfeng         mov r7, 4
2406*fae6e9adSlinfeng         rsh r0, r7
2407*fae6e9adSlinfeng         exit",
2408*fae6e9adSlinfeng     )
2409*fae6e9adSlinfeng     .unwrap();
2410*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2411*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2412*fae6e9adSlinfeng     unsafe {
2413*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x1);
2414*fae6e9adSlinfeng     }
2415*fae6e9adSlinfeng }
2416*fae6e9adSlinfeng 
2417*fae6e9adSlinfeng #[test]
test_jit_stack()2418*fae6e9adSlinfeng fn test_jit_stack() {
2419*fae6e9adSlinfeng     let prog = assemble(
2420*fae6e9adSlinfeng         "
2421*fae6e9adSlinfeng         mov r1, 51
2422*fae6e9adSlinfeng         stdw [r10-16], 0xab
2423*fae6e9adSlinfeng         stdw [r10-8], 0xcd
2424*fae6e9adSlinfeng         and r1, 1
2425*fae6e9adSlinfeng         lsh r1, 3
2426*fae6e9adSlinfeng         mov r2, r10
2427*fae6e9adSlinfeng         add r2, r1
2428*fae6e9adSlinfeng         ldxdw r0, [r2-16]
2429*fae6e9adSlinfeng         exit",
2430*fae6e9adSlinfeng     )
2431*fae6e9adSlinfeng     .unwrap();
2432*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2433*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2434*fae6e9adSlinfeng     unsafe {
2435*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0xcd);
2436*fae6e9adSlinfeng     }
2437*fae6e9adSlinfeng }
2438*fae6e9adSlinfeng 
2439*fae6e9adSlinfeng #[test]
test_jit_stack2()2440*fae6e9adSlinfeng fn test_jit_stack2() {
2441*fae6e9adSlinfeng     let prog = assemble(
2442*fae6e9adSlinfeng         "
2443*fae6e9adSlinfeng         stb [r10-4], 0x01
2444*fae6e9adSlinfeng         stb [r10-3], 0x02
2445*fae6e9adSlinfeng         stb [r10-2], 0x03
2446*fae6e9adSlinfeng         stb [r10-1], 0x04
2447*fae6e9adSlinfeng         mov r1, r10
2448*fae6e9adSlinfeng         mov r2, 0x4
2449*fae6e9adSlinfeng         sub r1, r2
2450*fae6e9adSlinfeng         call 1
2451*fae6e9adSlinfeng         mov r1, 0
2452*fae6e9adSlinfeng         ldxb r2, [r10-4]
2453*fae6e9adSlinfeng         ldxb r3, [r10-3]
2454*fae6e9adSlinfeng         ldxb r4, [r10-2]
2455*fae6e9adSlinfeng         ldxb r5, [r10-1]
2456*fae6e9adSlinfeng         call 0
2457*fae6e9adSlinfeng         xor r0, 0x2a2a2a2a
2458*fae6e9adSlinfeng         exit",
2459*fae6e9adSlinfeng     )
2460*fae6e9adSlinfeng     .unwrap();
2461*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2462*fae6e9adSlinfeng     vm.register_helper(0, helpers::gather_bytes).unwrap();
2463*fae6e9adSlinfeng     vm.register_helper(1, helpers::memfrob).unwrap();
2464*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2465*fae6e9adSlinfeng     unsafe {
2466*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x01020304);
2467*fae6e9adSlinfeng     }
2468*fae6e9adSlinfeng }
2469*fae6e9adSlinfeng 
2470*fae6e9adSlinfeng #[test]
test_jit_stb()2471*fae6e9adSlinfeng fn test_jit_stb() {
2472*fae6e9adSlinfeng     let prog = assemble(
2473*fae6e9adSlinfeng         "
2474*fae6e9adSlinfeng         stb [r1+2], 0x11
2475*fae6e9adSlinfeng         ldxb r0, [r1+2]
2476*fae6e9adSlinfeng         exit",
2477*fae6e9adSlinfeng     )
2478*fae6e9adSlinfeng     .unwrap();
2479*fae6e9adSlinfeng     let mem = &mut [0xaa, 0xbb, 0xff, 0xcc, 0xdd];
2480*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2481*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2482*fae6e9adSlinfeng     unsafe {
2483*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11);
2484*fae6e9adSlinfeng     }
2485*fae6e9adSlinfeng }
2486*fae6e9adSlinfeng 
2487*fae6e9adSlinfeng #[test]
test_jit_stdw()2488*fae6e9adSlinfeng fn test_jit_stdw() {
2489*fae6e9adSlinfeng     let prog = assemble(
2490*fae6e9adSlinfeng         "
2491*fae6e9adSlinfeng         stdw [r1+2], 0x44332211
2492*fae6e9adSlinfeng         ldxdw r0, [r1+2]
2493*fae6e9adSlinfeng         exit",
2494*fae6e9adSlinfeng     )
2495*fae6e9adSlinfeng     .unwrap();
2496*fae6e9adSlinfeng     let mem = &mut [
2497*fae6e9adSlinfeng         0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd,
2498*fae6e9adSlinfeng     ];
2499*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2500*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2501*fae6e9adSlinfeng     unsafe {
2502*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
2503*fae6e9adSlinfeng     }
2504*fae6e9adSlinfeng }
2505*fae6e9adSlinfeng 
2506*fae6e9adSlinfeng #[test]
test_jit_sth()2507*fae6e9adSlinfeng fn test_jit_sth() {
2508*fae6e9adSlinfeng     let prog = assemble(
2509*fae6e9adSlinfeng         "
2510*fae6e9adSlinfeng         sth [r1+2], 0x2211
2511*fae6e9adSlinfeng         ldxh r0, [r1+2]
2512*fae6e9adSlinfeng         exit",
2513*fae6e9adSlinfeng     )
2514*fae6e9adSlinfeng     .unwrap();
2515*fae6e9adSlinfeng     let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd];
2516*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2517*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2518*fae6e9adSlinfeng     unsafe {
2519*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2211);
2520*fae6e9adSlinfeng     }
2521*fae6e9adSlinfeng }
2522*fae6e9adSlinfeng 
2523*fae6e9adSlinfeng #[test]
test_jit_string_stack()2524*fae6e9adSlinfeng fn test_jit_string_stack() {
2525*fae6e9adSlinfeng     let prog = assemble(
2526*fae6e9adSlinfeng         "
2527*fae6e9adSlinfeng         mov r1, 0x78636261
2528*fae6e9adSlinfeng         stxw [r10-8], r1
2529*fae6e9adSlinfeng         mov r6, 0x0
2530*fae6e9adSlinfeng         stxb [r10-4], r6
2531*fae6e9adSlinfeng         stxb [r10-12], r6
2532*fae6e9adSlinfeng         mov r1, 0x79636261
2533*fae6e9adSlinfeng         stxw [r10-16], r1
2534*fae6e9adSlinfeng         mov r1, r10
2535*fae6e9adSlinfeng         add r1, -8
2536*fae6e9adSlinfeng         mov r2, r1
2537*fae6e9adSlinfeng         call 0x4
2538*fae6e9adSlinfeng         mov r1, r0
2539*fae6e9adSlinfeng         mov r0, 0x1
2540*fae6e9adSlinfeng         lsh r1, 0x20
2541*fae6e9adSlinfeng         rsh r1, 0x20
2542*fae6e9adSlinfeng         jne r1, 0x0, +11
2543*fae6e9adSlinfeng         mov r1, r10
2544*fae6e9adSlinfeng         add r1, -8
2545*fae6e9adSlinfeng         mov r2, r10
2546*fae6e9adSlinfeng         add r2, -16
2547*fae6e9adSlinfeng         call 0x4
2548*fae6e9adSlinfeng         mov r1, r0
2549*fae6e9adSlinfeng         lsh r1, 0x20
2550*fae6e9adSlinfeng         rsh r1, 0x20
2551*fae6e9adSlinfeng         mov r0, 0x1
2552*fae6e9adSlinfeng         jeq r1, r6, +1
2553*fae6e9adSlinfeng         mov r0, 0x0
2554*fae6e9adSlinfeng         exit",
2555*fae6e9adSlinfeng     )
2556*fae6e9adSlinfeng     .unwrap();
2557*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
2558*fae6e9adSlinfeng     vm.register_helper(4, helpers::strcmp).unwrap();
2559*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2560*fae6e9adSlinfeng     unsafe {
2561*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit().unwrap(), 0x0);
2562*fae6e9adSlinfeng     }
2563*fae6e9adSlinfeng }
2564*fae6e9adSlinfeng 
2565*fae6e9adSlinfeng #[test]
test_jit_stw()2566*fae6e9adSlinfeng fn test_jit_stw() {
2567*fae6e9adSlinfeng     let prog = assemble(
2568*fae6e9adSlinfeng         "
2569*fae6e9adSlinfeng         stw [r1+2], 0x44332211
2570*fae6e9adSlinfeng         ldxw r0, [r1+2]
2571*fae6e9adSlinfeng         exit",
2572*fae6e9adSlinfeng     )
2573*fae6e9adSlinfeng     .unwrap();
2574*fae6e9adSlinfeng     let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd];
2575*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2576*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2577*fae6e9adSlinfeng     unsafe {
2578*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
2579*fae6e9adSlinfeng     }
2580*fae6e9adSlinfeng }
2581*fae6e9adSlinfeng 
2582*fae6e9adSlinfeng #[test]
test_jit_stxb()2583*fae6e9adSlinfeng fn test_jit_stxb() {
2584*fae6e9adSlinfeng     let prog = assemble(
2585*fae6e9adSlinfeng         "
2586*fae6e9adSlinfeng         mov32 r2, 0x11
2587*fae6e9adSlinfeng         stxb [r1+2], r2
2588*fae6e9adSlinfeng         ldxb r0, [r1+2]
2589*fae6e9adSlinfeng         exit",
2590*fae6e9adSlinfeng     )
2591*fae6e9adSlinfeng     .unwrap();
2592*fae6e9adSlinfeng     let mem = &mut [0xaa, 0xbb, 0xff, 0xcc, 0xdd];
2593*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2594*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2595*fae6e9adSlinfeng     unsafe {
2596*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x11);
2597*fae6e9adSlinfeng     }
2598*fae6e9adSlinfeng }
2599*fae6e9adSlinfeng 
2600*fae6e9adSlinfeng #[test]
test_jit_stxb_all()2601*fae6e9adSlinfeng fn test_jit_stxb_all() {
2602*fae6e9adSlinfeng     let prog = assemble(
2603*fae6e9adSlinfeng         "
2604*fae6e9adSlinfeng         mov r0, 0xf0
2605*fae6e9adSlinfeng         mov r2, 0xf2
2606*fae6e9adSlinfeng         mov r3, 0xf3
2607*fae6e9adSlinfeng         mov r4, 0xf4
2608*fae6e9adSlinfeng         mov r5, 0xf5
2609*fae6e9adSlinfeng         mov r6, 0xf6
2610*fae6e9adSlinfeng         mov r7, 0xf7
2611*fae6e9adSlinfeng         mov r8, 0xf8
2612*fae6e9adSlinfeng         stxb [r1], r0
2613*fae6e9adSlinfeng         stxb [r1+1], r2
2614*fae6e9adSlinfeng         stxb [r1+2], r3
2615*fae6e9adSlinfeng         stxb [r1+3], r4
2616*fae6e9adSlinfeng         stxb [r1+4], r5
2617*fae6e9adSlinfeng         stxb [r1+5], r6
2618*fae6e9adSlinfeng         stxb [r1+6], r7
2619*fae6e9adSlinfeng         stxb [r1+7], r8
2620*fae6e9adSlinfeng         ldxdw r0, [r1]
2621*fae6e9adSlinfeng         be64 r0
2622*fae6e9adSlinfeng         exit",
2623*fae6e9adSlinfeng     )
2624*fae6e9adSlinfeng     .unwrap();
2625*fae6e9adSlinfeng     let mem = &mut [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
2626*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2627*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2628*fae6e9adSlinfeng     unsafe {
2629*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xf0f2f3f4f5f6f7f8);
2630*fae6e9adSlinfeng     }
2631*fae6e9adSlinfeng }
2632*fae6e9adSlinfeng 
2633*fae6e9adSlinfeng #[test]
test_jit_stxb_all2()2634*fae6e9adSlinfeng fn test_jit_stxb_all2() {
2635*fae6e9adSlinfeng     let prog = assemble(
2636*fae6e9adSlinfeng         "
2637*fae6e9adSlinfeng         mov r0, r1
2638*fae6e9adSlinfeng         mov r1, 0xf1
2639*fae6e9adSlinfeng         mov r9, 0xf9
2640*fae6e9adSlinfeng         stxb [r0], r1
2641*fae6e9adSlinfeng         stxb [r0+1], r9
2642*fae6e9adSlinfeng         ldxh r0, [r0]
2643*fae6e9adSlinfeng         be16 r0
2644*fae6e9adSlinfeng         exit",
2645*fae6e9adSlinfeng     )
2646*fae6e9adSlinfeng     .unwrap();
2647*fae6e9adSlinfeng     let mem = &mut [0xff, 0xff];
2648*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2649*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2650*fae6e9adSlinfeng     unsafe {
2651*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0xf1f9);
2652*fae6e9adSlinfeng     }
2653*fae6e9adSlinfeng }
2654*fae6e9adSlinfeng 
2655*fae6e9adSlinfeng #[test]
test_jit_stxb_chain()2656*fae6e9adSlinfeng fn test_jit_stxb_chain() {
2657*fae6e9adSlinfeng     let prog = assemble(
2658*fae6e9adSlinfeng         "
2659*fae6e9adSlinfeng         mov r0, r1
2660*fae6e9adSlinfeng         ldxb r9, [r0+0]
2661*fae6e9adSlinfeng         stxb [r0+1], r9
2662*fae6e9adSlinfeng         ldxb r8, [r0+1]
2663*fae6e9adSlinfeng         stxb [r0+2], r8
2664*fae6e9adSlinfeng         ldxb r7, [r0+2]
2665*fae6e9adSlinfeng         stxb [r0+3], r7
2666*fae6e9adSlinfeng         ldxb r6, [r0+3]
2667*fae6e9adSlinfeng         stxb [r0+4], r6
2668*fae6e9adSlinfeng         ldxb r5, [r0+4]
2669*fae6e9adSlinfeng         stxb [r0+5], r5
2670*fae6e9adSlinfeng         ldxb r4, [r0+5]
2671*fae6e9adSlinfeng         stxb [r0+6], r4
2672*fae6e9adSlinfeng         ldxb r3, [r0+6]
2673*fae6e9adSlinfeng         stxb [r0+7], r3
2674*fae6e9adSlinfeng         ldxb r2, [r0+7]
2675*fae6e9adSlinfeng         stxb [r0+8], r2
2676*fae6e9adSlinfeng         ldxb r1, [r0+8]
2677*fae6e9adSlinfeng         stxb [r0+9], r1
2678*fae6e9adSlinfeng         ldxb r0, [r0+9]
2679*fae6e9adSlinfeng         exit",
2680*fae6e9adSlinfeng     )
2681*fae6e9adSlinfeng     .unwrap();
2682*fae6e9adSlinfeng     let mem = &mut [0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
2683*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2684*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2685*fae6e9adSlinfeng     unsafe {
2686*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2a);
2687*fae6e9adSlinfeng     }
2688*fae6e9adSlinfeng }
2689*fae6e9adSlinfeng 
2690*fae6e9adSlinfeng #[test]
test_jit_stxdw()2691*fae6e9adSlinfeng fn test_jit_stxdw() {
2692*fae6e9adSlinfeng     let prog = assemble(
2693*fae6e9adSlinfeng         "
2694*fae6e9adSlinfeng         mov r2, -2005440939
2695*fae6e9adSlinfeng         lsh r2, 32
2696*fae6e9adSlinfeng         or r2, 0x44332211
2697*fae6e9adSlinfeng         stxdw [r1+2], r2
2698*fae6e9adSlinfeng         ldxdw r0, [r1+2]
2699*fae6e9adSlinfeng         exit",
2700*fae6e9adSlinfeng     )
2701*fae6e9adSlinfeng     .unwrap();
2702*fae6e9adSlinfeng     let mem = &mut [
2703*fae6e9adSlinfeng         0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd,
2704*fae6e9adSlinfeng     ];
2705*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2706*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2707*fae6e9adSlinfeng     unsafe {
2708*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x8877665544332211);
2709*fae6e9adSlinfeng     }
2710*fae6e9adSlinfeng }
2711*fae6e9adSlinfeng 
2712*fae6e9adSlinfeng #[test]
test_jit_stxh()2713*fae6e9adSlinfeng fn test_jit_stxh() {
2714*fae6e9adSlinfeng     let prog = assemble(
2715*fae6e9adSlinfeng         "
2716*fae6e9adSlinfeng         mov32 r2, 0x2211
2717*fae6e9adSlinfeng         stxh [r1+2], r2
2718*fae6e9adSlinfeng         ldxh r0, [r1+2]
2719*fae6e9adSlinfeng         exit",
2720*fae6e9adSlinfeng     )
2721*fae6e9adSlinfeng     .unwrap();
2722*fae6e9adSlinfeng     let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xcc, 0xdd];
2723*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2724*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2725*fae6e9adSlinfeng     unsafe {
2726*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x2211);
2727*fae6e9adSlinfeng     }
2728*fae6e9adSlinfeng }
2729*fae6e9adSlinfeng 
2730*fae6e9adSlinfeng #[test]
test_jit_stxw()2731*fae6e9adSlinfeng fn test_jit_stxw() {
2732*fae6e9adSlinfeng     let prog = assemble(
2733*fae6e9adSlinfeng         "
2734*fae6e9adSlinfeng         mov32 r2, 0x44332211
2735*fae6e9adSlinfeng         stxw [r1+2], r2
2736*fae6e9adSlinfeng         ldxw r0, [r1+2]
2737*fae6e9adSlinfeng         exit",
2738*fae6e9adSlinfeng     )
2739*fae6e9adSlinfeng     .unwrap();
2740*fae6e9adSlinfeng     let mem = &mut [0xaa, 0xbb, 0xff, 0xff, 0xff, 0xff, 0xcc, 0xdd];
2741*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2742*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2743*fae6e9adSlinfeng     unsafe {
2744*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x44332211);
2745*fae6e9adSlinfeng     }
2746*fae6e9adSlinfeng }
2747*fae6e9adSlinfeng 
2748*fae6e9adSlinfeng #[test]
test_jit_subnet()2749*fae6e9adSlinfeng fn test_jit_subnet() {
2750*fae6e9adSlinfeng     let prog = assemble(
2751*fae6e9adSlinfeng         "
2752*fae6e9adSlinfeng         mov r2, 0xe
2753*fae6e9adSlinfeng         ldxh r3, [r1+12]
2754*fae6e9adSlinfeng         jne r3, 0x81, +2
2755*fae6e9adSlinfeng         mov r2, 0x12
2756*fae6e9adSlinfeng         ldxh r3, [r1+16]
2757*fae6e9adSlinfeng         and r3, 0xffff
2758*fae6e9adSlinfeng         jne r3, 0x8, +5
2759*fae6e9adSlinfeng         add r1, r2
2760*fae6e9adSlinfeng         mov r0, 0x1
2761*fae6e9adSlinfeng         ldxw r1, [r1+16]
2762*fae6e9adSlinfeng         and r1, 0xffffff
2763*fae6e9adSlinfeng         jeq r1, 0x1a8c0, +1
2764*fae6e9adSlinfeng         mov r0, 0x0
2765*fae6e9adSlinfeng         exit",
2766*fae6e9adSlinfeng     )
2767*fae6e9adSlinfeng     .unwrap();
2768*fae6e9adSlinfeng     let mem = &mut [
2769*fae6e9adSlinfeng         0x00, 0x00, 0xc0, 0x9f, 0xa0, 0x97, 0x00, 0xa0, 0xcc, 0x3b, 0xbf, 0xfa, 0x08, 0x00, 0x45,
2770*fae6e9adSlinfeng         0x10, 0x00, 0x3c, 0x46, 0x3c, 0x40, 0x00, 0x40, 0x06, 0x73, 0x1c, 0xc0, 0xa8, 0x01, 0x02,
2771*fae6e9adSlinfeng         0xc0, 0xa8, 0x01, 0x01, 0x06, 0x0e, 0x00, 0x17, 0x99, 0xc5, 0xa0, 0xec, 0x00, 0x00, 0x00,
2772*fae6e9adSlinfeng         0x00, 0xa0, 0x02, 0x7d, 0x78, 0xe0, 0xa3, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4, 0x04, 0x02,
2773*fae6e9adSlinfeng         0x08, 0x0a, 0x00, 0x9c, 0x27, 0x24, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x00,
2774*fae6e9adSlinfeng     ];
2775*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2776*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2777*fae6e9adSlinfeng     unsafe {
2778*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1);
2779*fae6e9adSlinfeng     }
2780*fae6e9adSlinfeng }
2781*fae6e9adSlinfeng 
2782*fae6e9adSlinfeng const PROG_TCP_PORT_80: [u8; 152] = [
2783*fae6e9adSlinfeng     0x71, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0x13, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
2784*fae6e9adSlinfeng     0x67, 0x03, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x4f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2785*fae6e9adSlinfeng     0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x03, 0x0c, 0x00, 0x08, 0x00, 0x00, 0x00,
2786*fae6e9adSlinfeng     0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x02, 0x0a, 0x00, 0x06, 0x00, 0x00, 0x00,
2787*fae6e9adSlinfeng     0x71, 0x12, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
2788*fae6e9adSlinfeng     0x57, 0x02, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x67, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
2789*fae6e9adSlinfeng     0x0f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
2790*fae6e9adSlinfeng     0x15, 0x02, 0x02, 0x00, 0x00, 0x50, 0x00, 0x00, 0x69, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2791*fae6e9adSlinfeng     0x55, 0x01, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00, 0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
2792*fae6e9adSlinfeng     0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2793*fae6e9adSlinfeng ];
2794*fae6e9adSlinfeng 
2795*fae6e9adSlinfeng #[test]
test_jit_tcp_port80_match()2796*fae6e9adSlinfeng fn test_jit_tcp_port80_match() {
2797*fae6e9adSlinfeng     let mem = &mut [
2798*fae6e9adSlinfeng         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
2799*fae6e9adSlinfeng         0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
2800*fae6e9adSlinfeng         0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2801*fae6e9adSlinfeng         0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2802*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2803*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2804*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2805*fae6e9adSlinfeng     ];
2806*fae6e9adSlinfeng     let prog = &PROG_TCP_PORT_80;
2807*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
2808*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2809*fae6e9adSlinfeng     unsafe {
2810*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x1);
2811*fae6e9adSlinfeng     }
2812*fae6e9adSlinfeng }
2813*fae6e9adSlinfeng 
2814*fae6e9adSlinfeng #[test]
test_jit_tcp_port80_nomatch()2815*fae6e9adSlinfeng fn test_jit_tcp_port80_nomatch() {
2816*fae6e9adSlinfeng     let mem = &mut [
2817*fae6e9adSlinfeng         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
2818*fae6e9adSlinfeng         0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
2819*fae6e9adSlinfeng         0xc0, 0xa8, 0x00, 0x02, 0x00, 0x16, 0x27, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2820*fae6e9adSlinfeng         0x00, 0x51, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2821*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2822*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2823*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2824*fae6e9adSlinfeng     ];
2825*fae6e9adSlinfeng     let prog = &PROG_TCP_PORT_80;
2826*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
2827*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2828*fae6e9adSlinfeng     unsafe {
2829*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x0);
2830*fae6e9adSlinfeng     }
2831*fae6e9adSlinfeng }
2832*fae6e9adSlinfeng 
2833*fae6e9adSlinfeng #[test]
test_jit_tcp_port80_nomatch_ethertype()2834*fae6e9adSlinfeng fn test_jit_tcp_port80_nomatch_ethertype() {
2835*fae6e9adSlinfeng     let mem = &mut [
2836*fae6e9adSlinfeng         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x01, 0x45,
2837*fae6e9adSlinfeng         0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x06, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
2838*fae6e9adSlinfeng         0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2839*fae6e9adSlinfeng         0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2840*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2841*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2842*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2843*fae6e9adSlinfeng     ];
2844*fae6e9adSlinfeng     let prog = &PROG_TCP_PORT_80;
2845*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
2846*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2847*fae6e9adSlinfeng     unsafe {
2848*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x0);
2849*fae6e9adSlinfeng     }
2850*fae6e9adSlinfeng }
2851*fae6e9adSlinfeng 
2852*fae6e9adSlinfeng #[test]
test_jit_tcp_port80_nomatch_proto()2853*fae6e9adSlinfeng fn test_jit_tcp_port80_nomatch_proto() {
2854*fae6e9adSlinfeng     let mem = &mut [
2855*fae6e9adSlinfeng         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x08, 0x00, 0x45,
2856*fae6e9adSlinfeng         0x00, 0x00, 0x56, 0x00, 0x01, 0x00, 0x00, 0x40, 0x11, 0xf9, 0x4d, 0xc0, 0xa8, 0x00, 0x01,
2857*fae6e9adSlinfeng         0xc0, 0xa8, 0x00, 0x02, 0x27, 0x10, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2858*fae6e9adSlinfeng         0x00, 0x50, 0x02, 0x20, 0x00, 0xc5, 0x18, 0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2859*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2860*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2861*fae6e9adSlinfeng         0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
2862*fae6e9adSlinfeng     ];
2863*fae6e9adSlinfeng     let prog = &PROG_TCP_PORT_80;
2864*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(prog)).unwrap();
2865*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2866*fae6e9adSlinfeng     unsafe {
2867*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem).unwrap(), 0x0);
2868*fae6e9adSlinfeng     }
2869*fae6e9adSlinfeng }
2870*fae6e9adSlinfeng 
2871*fae6e9adSlinfeng #[test]
test_jit_tcp_sack_match()2872*fae6e9adSlinfeng fn test_jit_tcp_sack_match() {
2873*fae6e9adSlinfeng     let mut mem = TCP_SACK_MATCH.to_vec();
2874*fae6e9adSlinfeng     let prog = assemble(TCP_SACK_ASM).unwrap();
2875*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2876*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2877*fae6e9adSlinfeng     unsafe {
2878*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem.as_mut_slice()).unwrap(), 0x1);
2879*fae6e9adSlinfeng     }
2880*fae6e9adSlinfeng }
2881*fae6e9adSlinfeng 
2882*fae6e9adSlinfeng #[test]
test_jit_tcp_sack_nomatch()2883*fae6e9adSlinfeng fn test_jit_tcp_sack_nomatch() {
2884*fae6e9adSlinfeng     let mut mem = TCP_SACK_NOMATCH.to_vec();
2885*fae6e9adSlinfeng     let prog = assemble(TCP_SACK_ASM).unwrap();
2886*fae6e9adSlinfeng     let mut vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
2887*fae6e9adSlinfeng     vm.jit_compile().unwrap();
2888*fae6e9adSlinfeng     unsafe {
2889*fae6e9adSlinfeng         assert_eq!(vm.execute_program_jit(mem.as_mut_slice()).unwrap(), 0x0);
2890*fae6e9adSlinfeng     }
2891*fae6e9adSlinfeng }
2892