Skip to content

Commit

Permalink
Adds macro declare_builtin_function. (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso authored Sep 29, 2023
1 parent b3a3495 commit 8f8568a
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 332 deletions.
2 changes: 1 addition & 1 deletion benches/elf_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use test::Bencher;
fn loader() -> Arc<BuiltinProgram<TestContextObject>> {
let mut function_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry
.register_function_hashed(*b"log", syscalls::bpf_syscall_string)
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
.unwrap();
Arc::new(BuiltinProgram::new_loader(
Config::default(),
Expand Down
4 changes: 2 additions & 2 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,10 +1203,10 @@ mod test {
let mut function_registry =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry
.register_function_hashed(*b"log", syscalls::bpf_syscall_string)
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
.unwrap();
function_registry
.register_function_hashed(*b"log_64", syscalls::bpf_syscall_u64)
.register_function_hashed(*b"log_64", syscalls::SyscallU64::vm)
.unwrap();
Arc::new(BuiltinProgram::new_loader(
Config::default(),
Expand Down
2 changes: 1 addition & 1 deletion src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,7 @@ mod tests {
let mut function_registry =
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
function_registry
.register_function_hashed(*b"gather_bytes", syscalls::bpf_gather_bytes)
.register_function_hashed(*b"gather_bytes", syscalls::SyscallGatherBytes::vm)
.unwrap();
let loader = BuiltinProgram::new_loader(
Config {
Expand Down
30 changes: 30 additions & 0 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,33 @@ impl<C: ContextObject> std::fmt::Debug for BuiltinProgram<C> {
Ok(())
}
}

/// Generates an adapter for a BuiltinFunction between the Rust and the VM interface
#[macro_export]
macro_rules! declare_builtin_function {
($(#[$attr:meta])* $name:ident, $rust:item) => {
$(#[$attr])*
pub struct $name {}
impl $name {
/// Rust interface
$rust
/// VM interface
#[allow(clippy::too_many_arguments)]
pub fn vm(
context_object: &mut TestContextObject,
arg_a: u64,
arg_b: u64,
arg_c: u64,
arg_d: u64,
arg_e: u64,
memory_mapping: &mut $crate::memory_region::MemoryMapping,
program_result: &mut $crate::vm::ProgramResult,
) {
let converted_result: $crate::vm::ProgramResult = Self::rust(
context_object, arg_a, arg_b, arg_c, arg_d, arg_e, memory_mapping,
).into();
*program_result = converted_result;
}
}
};
}
Loading

0 comments on commit 8f8568a

Please sign in to comment.