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