Skip to content

Commit

Permalink
Use debug_assert
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasSte committed Oct 22, 2024
1 parent b7bdc5a commit e997952
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ impl<'a, 'b, C: ContextObject> Interpreter<'a, 'b, C> {
ProgramResult::Err(_err) => return false,
};
} else {
throw_error!(self, EbpfError::UnsupportedInstruction);
debug_assert!(false, "Invalid syscall should have been detected in the verifier.");
}
},
ebpf::RETURN
Expand Down
25 changes: 11 additions & 14 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,14 +715,15 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
// BPF to BPF call
self.emit_internal_call(Value::Constant64(target_pc as i64, true));
} else {
self.emit_throw_unsupported_instruction();
self.emit_ins(X86Instruction::load_immediate(OperandSize::S64, REGISTER_SCRATCH, self.pc as i64));
self.emit_ins(X86Instruction::jump_immediate(self.relative_to_anchor(ANCHOR_CALL_UNSUPPORTED_INSTRUCTION, 5)));
}
},
ebpf::SYSCALL if self.executable.get_sbpf_version().static_syscalls() => {
if let Some((_, function)) = self.executable.get_loader().get_function_registry(self.executable.get_sbpf_version()).lookup_by_key(insn.imm as u32) {
self.emit_syscall_dispatch(function);
} else {
self.emit_throw_unsupported_instruction();
debug_assert!(false, "Invalid syscall should have been detected in the verifier.")
}
},
ebpf::CALL_REG => {
Expand Down Expand Up @@ -1130,12 +1131,6 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
self.emit_undo_profile_instruction_count(0);
}

#[inline]
fn emit_throw_unsupported_instruction(&mut self) {
self.emit_ins(X86Instruction::load_immediate(OperandSize::S64, REGISTER_SCRATCH, self.pc as i64));
self.emit_ins(X86Instruction::jump_immediate(self.relative_to_anchor(ANCHOR_CALL_UNSUPPORTED_INSTRUCTION, 5)));
}

#[inline]
fn emit_address_translation(&mut self, dst: Option<u8>, vm_addr: Value, len: u64, value: Option<Value>) {
debug_assert_ne!(dst.is_some(), value.is_some());
Expand Down Expand Up @@ -1697,7 +1692,7 @@ impl<'a, C: ContextObject> JitCompiler<'a, C> {
mod tests {
use super::*;
use crate::{
program::{BuiltinFunction, BuiltinProgram, FunctionRegistry, SBPFVersion},
program::{BuiltinProgram, FunctionRegistry, SBPFVersion},
syscalls,
vm::TestContextObject,
};
Expand Down Expand Up @@ -1744,12 +1739,10 @@ mod tests {

fn create_mockup_executable(config: Config, program: &[u8]) -> Executable<TestContextObject> {
let sbpf_version = *config.enabled_sbpf_versions.end();
let mut function_registry =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry
.register_function_hashed(*b"gather_bytes", syscalls::SyscallGatherBytes::vm)
let mut loader = BuiltinProgram::new_loader_with_dense_registration(config);
loader
.register_function("gather_bytes", 1, syscalls::SyscallGatherBytes::vm)
.unwrap();
let loader = BuiltinProgram::new_loader(config, function_registry);
let mut function_registry = FunctionRegistry::default();
function_registry
.register_function(8, *b"function_foo", 8)
Expand Down Expand Up @@ -1836,6 +1829,10 @@ mod tests {
opcode = 0x85;
(0x88, 0x91020CDD)
}
0x95 => {
// Put a valid syscall
(0, 1)
}
0xD4 | 0xDC => (0x88, 16),
_ => (0x88, 0xFFFFFFFF),
};
Expand Down
2 changes: 2 additions & 0 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4089,6 +4089,7 @@ fn test_mod() {
}

#[test]
#[should_panic(expected = "Invalid syscall should have been detected in the verifier.")]
fn test_invalid_exit_or_return() {
for sbpf_version in [SBPFVersion::V1, SBPFVersion::V2] {
let inst = if sbpf_version == SBPFVersion::V1 {
Expand Down Expand Up @@ -4128,6 +4129,7 @@ fn test_invalid_exit_or_return() {
}

#[test]
#[should_panic(expected = "Invalid syscall should have been detected in the verifier.")]
fn test_unregistered_syscall() {
let prog = &[
0xbf, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, // mov64 r0, 2
Expand Down

0 comments on commit e997952

Please sign in to comment.