From 4422db19fbc384a76ed4bc8b557183e6c1365d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Sun, 1 Oct 2023 15:37:47 +0200 Subject: [PATCH] Makes rust interface of syscalls return Box. --- src/program.rs | 6 +++--- src/syscalls.rs | 12 ++++++------ tests/execution.rs | 24 +++++++++++++++--------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/program.rs b/src/program.rs index 55bbb20d..fd74c98c 100644 --- a/src/program.rs +++ b/src/program.rs @@ -296,7 +296,7 @@ macro_rules! declare_builtin_function { $arg_d:ident : u64, $arg_e:ident : u64, $memory_mapping:ident : &mut $MemoryMapping:ty, - ) -> Result $rust:tt) => { + ) -> Result> $rust:tt) => { $(#[$attr])* pub struct $name {} impl $name { @@ -309,7 +309,7 @@ macro_rules! declare_builtin_function { $arg_d: u64, $arg_e: u64, $memory_mapping: &mut $MemoryMapping, - ) -> Result { + ) -> Result> { $rust } /// VM interface @@ -332,7 +332,7 @@ macro_rules! declare_builtin_function { } let converted_result: $crate::error::ProgramResult = Self::rust( vm.context_object_pointer, $arg_a, $arg_b, $arg_c, $arg_d, $arg_e, &mut vm.memory_mapping, - ).into(); + ).map_err(|err| $crate::error::EbpfError::SyscallError(err)).into(); vm.program_result = converted_result; if config.enable_instruction_meter { vm.previous_instruction_meter = vm.context_object_pointer.get_remaining(); diff --git a/src/syscalls.rs b/src/syscalls.rs index 09b9adf7..b21930dc 100644 --- a/src/syscalls.rs +++ b/src/syscalls.rs @@ -41,7 +41,7 @@ declare_builtin_function!( arg4: u64, arg5: u64, _memory_mapping: &mut MemoryMapping, - ) -> Result { + ) -> Result> { println!("bpf_trace_printf: {arg3:#x}, {arg4:#x}, {arg5:#x}"); let size_arg = |x| { if x == 0 { @@ -69,7 +69,7 @@ declare_builtin_function!( arg4: u64, arg5: u64, _memory_mapping: &mut MemoryMapping, - ) -> Result { + ) -> Result> { Ok(arg1.wrapping_shl(32) | arg2.wrapping_shl(24) | arg3.wrapping_shl(16) @@ -91,7 +91,7 @@ declare_builtin_function!( _arg4: u64, _arg5: u64, memory_mapping: &mut MemoryMapping, - ) -> Result { + ) -> Result> { let host_addr: Result = memory_mapping.map(AccessType::Store, vm_addr, len).into(); let host_addr = host_addr?; @@ -116,7 +116,7 @@ declare_builtin_function!( _arg4: u64, _arg5: u64, memory_mapping: &mut MemoryMapping, - ) -> Result { + ) -> Result> { // C-like strcmp, maybe shorter than converting the bytes to string and comparing? if arg1 == 0 || arg2 == 0 { return Ok(u64::MAX); @@ -154,7 +154,7 @@ declare_builtin_function!( _arg4: u64, _arg5: u64, memory_mapping: &mut MemoryMapping, - ) -> Result { + ) -> Result> { let host_addr: Result = memory_mapping.map(AccessType::Load, vm_addr, len).into(); let host_addr = host_addr?; @@ -185,7 +185,7 @@ declare_builtin_function!( arg4: u64, arg5: u64, memory_mapping: &mut MemoryMapping, - ) -> Result { + ) -> Result> { println!( "dump_64: {:#x}, {:#x}, {:#x}, {:#x}, {:#x}, {:?}", arg1, arg2, arg3, arg4, arg5, memory_mapping as *const _ diff --git a/tests/execution.rs b/tests/execution.rs index 769b111f..ddc30ab8 100644 --- a/tests/execution.rs +++ b/tests/execution.rs @@ -2415,7 +2415,7 @@ fn test_err_syscall_string() { "bpf_syscall_string" => syscalls::SyscallString::vm, ), TestContextObject::new(2), - ProgramResult::Err(EbpfError::AccessViolation(AccessType::Load, 0, 0, "unknown")), + ProgramResult::Err(EbpfError::SyscallError(Box::new(EbpfError::AccessViolation(AccessType::Load, 0, 0, "unknown")))), ); } @@ -2510,12 +2510,18 @@ declare_builtin_function!( _arg4: u64, _arg5: u64, _memory_mapping: &mut MemoryMapping, - ) -> Result { - let result = if throw == 0 { - ProgramResult::Ok(42) - } else { - ProgramResult::Err(EbpfError::CallDepthExceeded) - }; + ) -> Result> { + let (result, expected_result): (Result>, ProgramResult) = + if throw == 0 { + (Result::Ok(42), ProgramResult::Ok(42)) + } else { + ( + Result::Err(Box::new(EbpfError::CallDepthExceeded)), + ProgramResult::Err(EbpfError::SyscallError(Box::new( + EbpfError::CallDepthExceeded, + ))), + ) + }; #[allow(unused_mut)] if depth > 0 { let mut function_registry = @@ -2538,10 +2544,10 @@ declare_builtin_function!( executable, mem, TestContextObject::new(if throw == 0 { 4 } else { 3 }), - result, + expected_result, ); } - result.into() + result } );