Skip to content

Commit

Permalink
Makes rust interface of syscalls return Box<dyn std::error::Error>.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtso committed Oct 1, 2023
1 parent e8d8d17 commit 4422db1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ macro_rules! declare_builtin_function {
$arg_d:ident : u64,
$arg_e:ident : u64,
$memory_mapping:ident : &mut $MemoryMapping:ty,
) -> Result<u64, EbpfError> $rust:tt) => {
) -> Result<u64, Box<dyn std::error::Error>> $rust:tt) => {
$(#[$attr])*
pub struct $name {}
impl $name {
Expand All @@ -309,7 +309,7 @@ macro_rules! declare_builtin_function {
$arg_d: u64,
$arg_e: u64,
$memory_mapping: &mut $MemoryMapping,
) -> Result<u64, EbpfError> {
) -> Result<u64, Box<dyn std::error::Error>> {
$rust
}
/// VM interface
Expand All @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ declare_builtin_function!(
arg4: u64,
arg5: u64,
_memory_mapping: &mut MemoryMapping,
) -> Result<u64, EbpfError> {
) -> Result<u64, Box<dyn std::error::Error>> {
println!("bpf_trace_printf: {arg3:#x}, {arg4:#x}, {arg5:#x}");
let size_arg = |x| {
if x == 0 {
Expand Down Expand Up @@ -69,7 +69,7 @@ declare_builtin_function!(
arg4: u64,
arg5: u64,
_memory_mapping: &mut MemoryMapping,
) -> Result<u64, EbpfError> {
) -> Result<u64, Box<dyn std::error::Error>> {
Ok(arg1.wrapping_shl(32)
| arg2.wrapping_shl(24)
| arg3.wrapping_shl(16)
Expand All @@ -91,7 +91,7 @@ declare_builtin_function!(
_arg4: u64,
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, EbpfError> {
) -> Result<u64, Box<dyn std::error::Error>> {
let host_addr: Result<u64, EbpfError> =
memory_mapping.map(AccessType::Store, vm_addr, len).into();
let host_addr = host_addr?;
Expand All @@ -116,7 +116,7 @@ declare_builtin_function!(
_arg4: u64,
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, EbpfError> {
) -> Result<u64, Box<dyn std::error::Error>> {
// C-like strcmp, maybe shorter than converting the bytes to string and comparing?
if arg1 == 0 || arg2 == 0 {
return Ok(u64::MAX);
Expand Down Expand Up @@ -154,7 +154,7 @@ declare_builtin_function!(
_arg4: u64,
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, EbpfError> {
) -> Result<u64, Box<dyn std::error::Error>> {
let host_addr: Result<u64, EbpfError> =
memory_mapping.map(AccessType::Load, vm_addr, len).into();
let host_addr = host_addr?;
Expand Down Expand Up @@ -185,7 +185,7 @@ declare_builtin_function!(
arg4: u64,
arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, EbpfError> {
) -> Result<u64, Box<dyn std::error::Error>> {
println!(
"dump_64: {:#x}, {:#x}, {:#x}, {:#x}, {:#x}, {:?}",
arg1, arg2, arg3, arg4, arg5, memory_mapping as *const _
Expand Down
24 changes: 15 additions & 9 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))),
);
}

Expand Down Expand Up @@ -2510,12 +2510,18 @@ declare_builtin_function!(
_arg4: u64,
_arg5: u64,
_memory_mapping: &mut MemoryMapping,
) -> Result<u64, EbpfError> {
let result = if throw == 0 {
ProgramResult::Ok(42)
} else {
ProgramResult::Err(EbpfError::CallDepthExceeded)
};
) -> Result<u64, Box<dyn std::error::Error>> {
let (result, expected_result): (Result<u64, Box<dyn std::error::Error>>, 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 =
Expand All @@ -2538,10 +2544,10 @@ declare_builtin_function!(
executable,
mem,
TestContextObject::new(if throw == 0 { 4 } else { 3 }),
result,
expected_result,
);
}
result.into()
result
}
);

Expand Down

0 comments on commit 4422db1

Please sign in to comment.