Explorar o código

tests/misc.rs: add unit tests for `LD_ABS_*` and `LD_IND_*` operations

Quentin Monnet %!s(int64=8) %!d(string=hai) anos
pai
achega
bc693be7fe
Modificáronse 1 ficheiros con 218 adicións e 0 borrados
  1. 218 0
      tests/misc.rs

+ 218 - 0
tests/misc.rs

@@ -307,3 +307,221 @@ fn test_jit_mbuff() {
         assert_eq!(vm.prog_exec_jit(mem, &mut mbuff), 0x2211);
     }
 }
+
+#[test]
+fn test_vm_jit_ldabsb() {
+    let prog = &[
+        0x30, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let mem = &mut [
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    ];
+    let mut vm = rbpf::EbpfVmRaw::new(prog);
+    assert_eq!(vm.prog_exec(mem), 0x33);
+
+    vm.jit_compile();
+    unsafe {
+        assert_eq!(vm.prog_exec_jit(mem), 0x33);
+    };
+}
+
+#[test]
+fn test_vm_jit_ldabsh() {
+    let prog = &[
+        0x28, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let mem = &mut [
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    ];
+    let mut vm = rbpf::EbpfVmRaw::new(prog);
+    assert_eq!(vm.prog_exec(mem), 0x4433);
+
+    vm.jit_compile();
+    unsafe {
+        assert_eq!(vm.prog_exec_jit(mem), 0x4433);
+    };
+}
+
+#[test]
+fn test_vm_jit_ldabsw() {
+    let prog = &[
+        0x20, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let mem = &mut [
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    ];
+    let mut vm = rbpf::EbpfVmRaw::new(prog);
+    assert_eq!(vm.prog_exec(mem), 0x66554433);
+    vm.jit_compile();
+
+    unsafe {
+        assert_eq!(vm.prog_exec_jit(mem), 0x66554433);
+    };
+}
+
+#[test]
+fn test_vm_jit_ldabsdw() {
+    let prog = &[
+        0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let mem = &mut [
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    ];
+    let mut vm = rbpf::EbpfVmRaw::new(prog);
+    assert_eq!(vm.prog_exec(mem), 0xaa99887766554433);
+    vm.jit_compile();
+
+    unsafe {
+        assert_eq!(vm.prog_exec_jit(mem), 0xaa99887766554433);
+    };
+}
+
+#[test]
+#[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
+fn test_vm_err_ldabsb_oob() {
+    let prog = &[
+        0x38, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let mem = &mut [
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    ];
+    let vm = rbpf::EbpfVmRaw::new(prog);
+    vm.prog_exec(mem);
+
+    // Memory check not implemented for JIT yet.
+}
+
+#[test]
+#[should_panic(expected = "Error: out of bounds memory load (insn #1),")]
+fn test_vm_err_ldabsb_nomem() {
+    let prog = &[
+        0x38, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let vm = rbpf::EbpfVmNoData::new(prog);
+    vm.prog_exec();
+
+    // Memory check not implemented for JIT yet.
+}
+
+#[test]
+fn test_vm_jit_ldindb() {
+    let prog = &[
+        0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
+        0x50, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let mem = &mut [
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    ];
+    let mut vm = rbpf::EbpfVmRaw::new(prog);
+    assert_eq!(vm.prog_exec(mem), 0x88);
+
+    vm.jit_compile();
+    unsafe {
+        assert_eq!(vm.prog_exec_jit(mem), 0x88);
+    };
+}
+
+#[test]
+fn test_vm_jit_ldindh() {
+    let prog = &[
+        0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
+        0x48, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let mem = &mut [
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    ];
+    let mut vm = rbpf::EbpfVmRaw::new(prog);
+    assert_eq!(vm.prog_exec(mem), 0x9988);
+
+    vm.jit_compile();
+    unsafe {
+        assert_eq!(vm.prog_exec_jit(mem), 0x9988);
+    };
+}
+
+#[test]
+fn test_vm_jit_ldindw() {
+    let prog = &[
+        0xb7, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
+        0x40, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let mem = &mut [
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    ];
+    let mut vm = rbpf::EbpfVmRaw::new(prog);
+    assert_eq!(vm.prog_exec(mem), 0x88776655);
+    vm.jit_compile();
+
+    unsafe {
+        assert_eq!(vm.prog_exec_jit(mem), 0x88776655);
+    };
+}
+
+#[test]
+fn test_vm_jit_ldinddw() {
+    let prog = &[
+        0xb7, 0x01, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+        0x58, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let mem = &mut [
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    ];
+    let mut vm = rbpf::EbpfVmRaw::new(prog);
+    assert_eq!(vm.prog_exec(mem), 0xccbbaa9988776655);
+    vm.jit_compile();
+
+    unsafe {
+        assert_eq!(vm.prog_exec_jit(mem), 0xccbbaa9988776655);
+    };
+}
+
+#[test]
+#[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
+fn test_vm_err_ldindb_oob() {
+    let prog = &[
+        0xb7, 0x01, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
+        0x38, 0x10, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let mem = &mut [
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    ];
+    let vm = rbpf::EbpfVmRaw::new(prog);
+    vm.prog_exec(mem);
+
+    // Memory check not implemented for JIT yet.
+}
+
+#[test]
+#[should_panic(expected = "Error: out of bounds memory load (insn #2),")]
+fn test_vm_err_ldindb_nomem() {
+    let prog = &[
+        0xb7, 0x01, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+        0x38, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    ];
+    let vm = rbpf::EbpfVmNoData::new(prog);
+    vm.prog_exec();
+
+    // Memory check not implemented for JIT yet.
+}