Skip to content

Commit

Permalink
tests/ubpf_vm.rs: Add non-regression tests for 32-bit divisions by 0
Browse files Browse the repository at this point in the history
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: #95
Link: #96
Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Richard Smith <ret2happy@126.com>
  • Loading branch information
qmonnet committed Jan 5, 2024
1 parent e866963 commit 9bd61f2
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/ubpf_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("
Expand All @@ -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("
Expand Down

0 comments on commit 9bd61f2

Please sign in to comment.