瀏覽代碼

tests/ubpf_vm.rs: Add tests for arsh offset masking

Add tests to make sure we handle the masking of the value (in the source
register or as an immediate) telling by how many bits the value in the
destination register should be shifted.

The context is that we used to do no masking on this value, but the BPF
ISA specification does require one, and the kernel implements it when
the value is passed in the source register (and rejects immediates that
it deems invalid).

Signed-off-by: Quentin Monnet <qmo@qmon.net>
Quentin Monnet 1 年之前
父節點
當前提交
a16cc00d8c
共有 1 個文件被更改,包括 46 次插入0 次删除
  1. 46 0
      tests/ubpf_vm.rs

+ 46 - 0
tests/ubpf_vm.rs

@@ -194,6 +194,52 @@ fn test_vm_arsh_reg() {
     assert_eq!(vm.execute_program().unwrap(), 0xffff8000);
 }
 
+#[test]
+fn test_vm_arsh_imm_overflow() {
+    let prog = assemble("
+        mov r0, 1
+        lsh r0, 63
+        arsh r0, 0xff20
+        exit").unwrap();
+    let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
+    assert_eq!(vm.execute_program().unwrap(), 0xffffffff80000000);
+}
+
+#[test]
+fn test_vm_arsh_reg_overflow() {
+    let prog = assemble("
+        mov r0, 1
+        lsh r0, 63
+        mov r1, 0xff04
+        arsh r0, r1
+        exit").unwrap();
+    let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
+    assert_eq!(vm.execute_program().unwrap(), 0xf800000000000000);
+}
+
+#[test]
+fn test_vm_arsh32_imm_overflow() {
+    let prog = assemble("
+        mov32 r0, 1
+        lsh32 r0, 31
+        arsh32 r0, 0xff10
+        exit").unwrap();
+    let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
+    assert_eq!(vm.execute_program().unwrap(), 0xffff8000);
+}
+
+#[test]
+fn test_vm_arsh32_reg_overflow() {
+    let prog = assemble("
+        mov32 r0, 1
+        lsh32 r0, 31
+        mov32 r1, 32
+        arsh32 r0, r1
+        exit").unwrap();
+    let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
+    assert_eq!(vm.execute_program().unwrap(), 0x80000000);
+}
+
 #[test]
 fn test_vm_be16() {
     let prog = assemble("