瀏覽代碼

src/verifier.rs: Remove checks for divisions by zero

We have a defined behaviour for divisions by zero in the interpreter and
the JIT compiler, we no longer need to check for null immediates in the
verifier. Let's remove these checks.

Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Quentin Monnet 2 年之前
父節點
當前提交
bc1f051e7b
共有 2 個文件被更改,包括 4 次插入23 次删除
  1. 4 12
      src/verifier.rs
  2. 0 11
      tests/ubpf_verifier.rs

+ 4 - 12
src/verifier.rs

@@ -48,14 +48,6 @@ fn check_prog_len(prog: &[u8]) -> Result<(), Error> {
     Ok(())
 }
 
-fn check_imm_nonzero(insn: &ebpf::Insn, insn_ptr: usize) -> Result<(), Error> {
-    if insn.imm == 0 {
-        reject(format!("division by 0 (insn #{insn_ptr:?})"))?;
-    }
-
-    Ok(())
-}
-
 fn check_imm_endian(insn: &ebpf::Insn, insn_ptr: usize) -> Result<(), Error> {
     match insn.imm {
         16 | 32 | 64 => Ok(()),
@@ -158,7 +150,7 @@ pub fn check(prog: &[u8]) -> Result<(), Error> {
             ebpf::SUB32_REG  => {},
             ebpf::MUL32_IMM  => {},
             ebpf::MUL32_REG  => {},
-            ebpf::DIV32_IMM  => { check_imm_nonzero(&insn, insn_ptr)?; },
+            ebpf::DIV32_IMM  => {},
             ebpf::DIV32_REG  => {},
             ebpf::OR32_IMM   => {},
             ebpf::OR32_REG   => {},
@@ -169,7 +161,7 @@ pub fn check(prog: &[u8]) -> Result<(), Error> {
             ebpf::RSH32_IMM  => {},
             ebpf::RSH32_REG  => {},
             ebpf::NEG32      => {},
-            ebpf::MOD32_IMM  => { check_imm_nonzero(&insn, insn_ptr)?; },
+            ebpf::MOD32_IMM  => {},
             ebpf::MOD32_REG  => {},
             ebpf::XOR32_IMM  => {},
             ebpf::XOR32_REG  => {},
@@ -187,7 +179,7 @@ pub fn check(prog: &[u8]) -> Result<(), Error> {
             ebpf::SUB64_REG  => {},
             ebpf::MUL64_IMM  => {},
             ebpf::MUL64_REG  => {},
-            ebpf::DIV64_IMM  => { check_imm_nonzero(&insn, insn_ptr)?; },
+            ebpf::DIV64_IMM  => {},
             ebpf::DIV64_REG  => {},
             ebpf::OR64_IMM   => {},
             ebpf::OR64_REG   => {},
@@ -198,7 +190,7 @@ pub fn check(prog: &[u8]) -> Result<(), Error> {
             ebpf::RSH64_IMM  => {},
             ebpf::RSH64_REG  => {},
             ebpf::NEG64      => {},
-            ebpf::MOD64_IMM  => { check_imm_nonzero(&insn, insn_ptr)?; },
+            ebpf::MOD64_IMM  => {},
             ebpf::MOD64_REG  => {},
             ebpf::XOR64_IMM  => {},
             ebpf::XOR64_REG  => {},

+ 0 - 11
tests/ubpf_verifier.rs

@@ -21,17 +21,6 @@ extern crate rbpf;
 use rbpf::assembler::assemble;
 use rbpf::ebpf;
 
-#[test]
-#[should_panic(expected = "[Verifier] Error: division by 0 (insn #1)")]
-fn test_verifier_err_div_by_zero_imm() {
-    let prog = assemble("
-        mov32 r0, 1
-        div32 r0, 0
-        exit").unwrap();
-    let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
-    vm.execute_program().unwrap();
-}
-
 #[test]
 #[should_panic(expected = "[Verifier] Error: unsupported argument for LE/BE (insn #0)")]
 fn test_verifier_err_endian_size() {