Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 4de194b

Browse files
committed
Adds macro declare_builtin_function.
1 parent b3a3495 commit 4de194b

File tree

7 files changed

+250
-332
lines changed

7 files changed

+250
-332
lines changed

benches/elf_loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use test::Bencher;
2222
fn loader() -> Arc<BuiltinProgram<TestContextObject>> {
2323
let mut function_registry = FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
2424
function_registry
25-
.register_function_hashed(*b"log", syscalls::bpf_syscall_string)
25+
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
2626
.unwrap();
2727
Arc::new(BuiltinProgram::new_loader(
2828
Config::default(),

src/elf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,10 +1203,10 @@ mod test {
12031203
let mut function_registry =
12041204
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
12051205
function_registry
1206-
.register_function_hashed(*b"log", syscalls::bpf_syscall_string)
1206+
.register_function_hashed(*b"log", syscalls::SyscallString::vm)
12071207
.unwrap();
12081208
function_registry
1209-
.register_function_hashed(*b"log_64", syscalls::bpf_syscall_u64)
1209+
.register_function_hashed(*b"log_64", syscalls::SyscallU64::vm)
12101210
.unwrap();
12111211
Arc::new(BuiltinProgram::new_loader(
12121212
Config::default(),

src/jit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ mod tests {
16601660
let mut function_registry =
16611661
FunctionRegistry::<BuiltinFunction<TestContextObject>>::default();
16621662
function_registry
1663-
.register_function_hashed(*b"gather_bytes", syscalls::bpf_gather_bytes)
1663+
.register_function_hashed(*b"gather_bytes", syscalls::SyscallGatherBytes::vm)
16641664
.unwrap();
16651665
let loader = BuiltinProgram::new_loader(
16661666
Config {

src/program.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,33 @@ impl<C: ContextObject> std::fmt::Debug for BuiltinProgram<C> {
287287
Ok(())
288288
}
289289
}
290+
291+
/// Generates an adapter for a BuiltinFunction between the Rust and the VM interface
292+
#[macro_export]
293+
macro_rules! declare_builtin_function {
294+
($(#[$attr:meta])* $name:ident, $rust:item) => {
295+
$(#[$attr])*
296+
pub struct $name {}
297+
impl $name {
298+
/// Rust interface
299+
$rust
300+
/// VM interface
301+
#[allow(clippy::too_many_arguments)]
302+
pub fn vm(
303+
context_object: &mut TestContextObject,
304+
arg_a: u64,
305+
arg_b: u64,
306+
arg_c: u64,
307+
arg_d: u64,
308+
arg_e: u64,
309+
memory_mapping: &mut $crate::memory_region::MemoryMapping,
310+
program_result: &mut $crate::vm::ProgramResult,
311+
) {
312+
let converted_result: $crate::vm::ProgramResult = Self::rust(
313+
context_object, arg_a, arg_b, arg_c, arg_d, arg_e, memory_mapping,
314+
).into();
315+
*program_result = converted_result;
316+
}
317+
}
318+
};
319+
}

0 commit comments

Comments
 (0)