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