فهرست منبع

Add test cases and fix errors.

Signed-off-by: Godones <chenlinfeng25@outlook.com>
Godones 2 ماه پیش
والد
کامیت
b4bef82024
7فایلهای تغییر یافته به همراه61 افزوده شده و 3 حذف شده
  1. 1 1
      src/interpreter.rs
  2. 1 1
      src/jit.rs
  3. 5 0
      src/lib.rs
  4. 1 1
      src/verifier.rs
  5. 16 0
      tests/ubpf_jit_x86_64.rs
  6. 22 0
      tests/ubpf_verifier.rs
  7. 15 0
      tests/ubpf_vm.rs

+ 1 - 1
src/interpreter.rs

@@ -387,7 +387,7 @@ pub fn execute_program(
                         insn_ptr += insn.imm as usize;
                     }
                     _ => {
-                        Err(Error::new(ErrorKind::Other, format!("Error: invalid call to function #{:?} (insn #{insn_ptr:?})", insn.imm)))?;
+                        Err(Error::new(ErrorKind::Other, format!("Error: unexpected call type #{} (insn #{})", _src, insn_ptr-1)))?;
                     }
                 }
             }

+ 1 - 1
src/jit.rs

@@ -908,7 +908,7 @@ impl JitCompiler {
                             self.emit_local_call(mem, target_pc);
                         }   
                         _ => {
-                            Err(Error::new(ErrorKind::Other, format!("Error: unexpected call type {:#x}",src)))?;
+                            Err(Error::new(ErrorKind::Other, format!("[JIT] Error: unexpected call type #{:?} (insn #{insn_ptr:?})", insn.src)))?;
                         }
                     }
                 },

+ 5 - 0
src/lib.rs

@@ -81,6 +81,11 @@ pub mod lib {
     #[cfg(feature = "std")]
     pub use std::vec::Vec;
 
+    #[cfg(not(feature = "std"))]
+    pub use alloc::boxed::Box;
+    #[cfg(feature = "std")]
+    pub use std::boxed::Box;
+
     #[cfg(not(feature = "std"))]
     pub use alloc::string::{String, ToString};
     #[cfg(feature = "std")]

+ 1 - 1
src/verifier.rs

@@ -260,7 +260,7 @@ pub fn check(prog: &[u8]) -> Result<(), Error> {
                             reject(format!("call out of code to #{dst_insn_ptr:?} (insn #{insn_ptr:?})"))?;
                         }
                     }
-                    _ => { reject(format!("invalid call to function #{:?} (insn #{insn_ptr:?})", insn.imm))?; }
+                    _ => { reject(format!("unexpected call type #{:?} (insn #{insn_ptr:?})", src))?; }
                 }
             },
             ebpf::TAIL_CALL  => { unimplemented!() },

+ 16 - 0
tests/ubpf_jit_x86_64.rs

@@ -2391,3 +2391,19 @@ fn test_bpf_to_bpf_call(){
     let vm_res= unsafe { vm.execute_program_jit().unwrap() };    
     assert_eq!(vm_res, 0x10);
 }
+
+#[test]
+#[should_panic(expected = "[JIT] Error: unexpected call type #2 (insn #0)")]
+fn test_other_type_call(){
+    let prog = &[
+        0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ];
+    let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
+    vm.set_verifier(|_|{
+        Ok(())
+    }).unwrap();
+    vm.set_program(prog).unwrap();
+    vm.jit_compile().unwrap();
+    unsafe { vm.execute_program_jit().unwrap() };
+}

+ 22 - 0
tests/ubpf_verifier.rs

@@ -150,3 +150,25 @@ fn test_verifier_err_write_r10() {
     let vm = rbpf::EbpfVmNoData::new(Some(&prog)).unwrap();
     vm.execute_program().unwrap();
 }
+
+#[test]
+#[should_panic(expected = "[Verifier] Error: call out of code to #2 (insn #0)")]
+fn test_verifier_err_funcall_over_the_end() {
+    let prog = &[
+        0x85, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ];
+    let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
+    vm.execute_program().unwrap();
+}
+
+#[test]
+#[should_panic(expected = "[Verifier] Error: unexpected call type #2 (insn #0)")]
+fn test_verifier_err_other_type_call(){
+    let prog = &[
+        0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ];
+    let vm = rbpf::EbpfVmNoData::new(Some(prog)).unwrap();
+    vm.execute_program().unwrap();
+}

+ 15 - 0
tests/ubpf_vm.rs

@@ -2420,3 +2420,18 @@ fn test_vm_tcp_sack_nomatch() {
     let vm = rbpf::EbpfVmRaw::new(Some(&prog)).unwrap();
     assert_eq!(vm.execute_program(mem.as_mut_slice()).unwrap(), 0x0);
 }
+
+#[test]
+#[should_panic(expected = "Error: unexpected call type #2 (insn #0)")]
+fn test_other_type_call(){
+    let prog = &[
+        0x85, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    ];
+    let mut vm = rbpf::EbpfVmNoData::new(None).unwrap();
+    vm.set_verifier(|_|{
+        Ok(())
+    }).unwrap();
+    vm.set_program(prog).unwrap();
+    vm.execute_program().unwrap();
+}