Procházet zdrojové kódy

tests/ubpf_vm.rs: Add non-regression tests for 32-bit divisions by 0

We had an issue in the interpreter, because for 32-bit div and mod we
would check that the whole (64-bit long) divisor was not 0, but then
we'd only use the last 32 bits of this divisor to perform the arithmetic
operation. We fixed the bug, but let's add a non-regression test to
catch this in the future.

Link: https://github.com/qmonnet/rbpf/issues/95
Link: https://github.com/qmonnet/rbpf/issues/96
Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Richard Smith <ret2happy@126.com>
Quentin Monnet před 1 rokem
rodič
revize
9bd61f2dd7
1 změnil soubory, kde provedl 22 přidání a 0 odebrání
  1. 22 0
      tests/ubpf_vm.rs

+ 22 - 0
tests/ubpf_vm.rs

@@ -440,6 +440,17 @@ fn test_vm_mod_by_zero_imm() {
     assert_eq!(vm.execute_program().unwrap(), 0x1);
 }
 
+// Make sure we only consider the last 32 bits of the divisor.
+#[test]
+fn test_vm_mod_by_zero_reg_long() {
+    let prog = assemble("
+        lddw r1, 0x100000000
+        mod32 r0, r1
+        exit").unwrap();
+    let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
+    assert_eq!(vm.execute_program().unwrap(), 0x0);
+}
+
 #[test]
 fn test_vm_div64_by_zero_reg() {
     let prog = assemble("
@@ -462,6 +473,17 @@ fn test_vm_div_by_zero_reg() {
     assert_eq!(vm.execute_program().unwrap(), 0x0);
 }
 
+// Make sure we only consider the last 32 bits of the divisor.
+#[test]
+fn test_vm_div_by_zero_reg_long() {
+    let prog = assemble("
+        lddw r1, 0x100000000
+        div32 r0, r1
+        exit").unwrap();
+    let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
+    assert_eq!(vm.execute_program().unwrap(), 0x0);
+}
+
 #[test]
 fn test_vm_mod64_by_zero_reg() {
     let prog = assemble("