Skip to content

Commit

Permalink
Removes the optional hash parameter from bind_syscall_context_objects…
Browse files Browse the repository at this point in the history
…(). (#309)
  • Loading branch information
Lichtso committed Apr 27, 2022
1 parent 3c58be5 commit 6abf946
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 33 deletions.
5 changes: 1 addition & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,7 @@ fn main() {
_ => {}
}

for (hash, name) in executable.get_syscall_symbols() {
vm.bind_syscall_context_objects(Box::new(MockSyscall { name: name.clone() }), Some(*hash))
.unwrap();
}
vm.bind_syscall_context_objects(0).unwrap();
let result = if matches.value_of("use").unwrap() == "interpreter" {
vm.execute_program_interpreted(&mut instruction_meter)
} else {
Expand Down
40 changes: 15 additions & 25 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> {
&self.tracer
}

/// Bind a context objects instance to a previously registered syscalls
/// Initializes and binds the context object instances for all previously registered syscalls
///
/// # Examples
///
Expand Down Expand Up @@ -611,44 +611,34 @@ impl<'a, E: UserDefinedError, I: InstructionMeter> EbpfVm<'a, E, I> {
/// let mut executable = Executable::<UserError, TestInstructionMeter>::from_text_bytes(prog, None, config, syscall_registry, bpf_functions).unwrap();
/// let mut vm = EbpfVm::<UserError, TestInstructionMeter>::new(&executable, &mut [], Vec::new()).unwrap();
/// // Bind a context object instance to the previously registered syscall
/// vm.bind_syscall_context_objects(0, None);
/// vm.bind_syscall_context_objects(0);
/// ```
pub fn bind_syscall_context_objects<C: Clone>(
&mut self,
syscall_context: C,
hash: Option<u32>,
) -> Result<(), EbpfError<E>> {
let syscall_registry = self.executable.get_syscall_registry();

for (_hash, syscall) in syscall_registry.entries.iter() {
for syscall in syscall_registry.entries.values() {
let syscall_object_init_fn: SyscallInit<C, E> =
unsafe { std::mem::transmute(syscall.init) };
let syscall_context_object: Box<dyn SyscallObject<E> + 'a> =
syscall_object_init_fn(syscall_context.clone());
let fat_ptr: DynTraitFatPointer =
unsafe { std::mem::transmute(&*syscall_context_object) };
let slot = syscall_registry
.lookup_context_object_slot(fat_ptr.vtable.methods[0] as u64)
.ok_or(EbpfError::SyscallNotRegistered(
fat_ptr.vtable.methods[0] as usize,
))?;

let slot = match hash {
Some(hash) => {
syscall_registry
.lookup_syscall(hash)
.ok_or(EbpfError::SyscallNotRegistered(hash as usize))?
.context_object_slot
}
None => syscall_registry
.lookup_context_object_slot(fat_ptr.vtable.methods[0] as u64)
.ok_or(EbpfError::SyscallNotRegistered(
fat_ptr.vtable.methods[0] as usize,
))?,
};
if !self.syscall_context_objects[SYSCALL_CONTEXT_OBJECTS_OFFSET + slot].is_null() {
return Err(EbpfError::SyscallAlreadyBound(slot));
} else {
self.syscall_context_objects[SYSCALL_CONTEXT_OBJECTS_OFFSET + slot] = fat_ptr.data;
// Keep the dyn trait objects so that they can be dropped properly later
self.syscall_context_object_pool
.push(syscall_context_object);
}
debug_assert!(
self.syscall_context_objects[SYSCALL_CONTEXT_OBJECTS_OFFSET + slot].is_null()
);
self.syscall_context_objects[SYSCALL_CONTEXT_OBJECTS_OFFSET + slot] = fat_ptr.data;
// Keep the dyn trait objects so that they can be dropped properly later
self.syscall_context_object_pool
.push(syscall_context_object);
}

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ fn test_fuzz_execute() {
Vec::new(),
)
.unwrap();
vm.bind_syscall_context_objects(0, None).unwrap();
vm.bind_syscall_context_objects(0, None).unwrap();
vm.bind_syscall_context_objects(0).unwrap();
vm.bind_syscall_context_objects(0).unwrap();
let _ = vm.execute_program_interpreted(&mut TestInstructionMeter {
remaining: 1_000_000,
});
Expand Down
3 changes: 1 addition & 2 deletions tests/ubpf_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ macro_rules! test_interpreter_and_jit {
.unwrap();
};
(bind, $vm:expr, $syscall_context:expr) => {
$vm.bind_syscall_context_objects($syscall_context, None)
.unwrap();
$vm.bind_syscall_context_objects($syscall_context).unwrap();
};
($executable:expr, $mem:tt, $syscall_context:expr, $check:block, $expected_instruction_count:expr) => {
#[allow(unused_mut)]
Expand Down

0 comments on commit 6abf946

Please sign in to comment.