Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor - Make declare_builtin_function receive a generic ContextObject #616

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ impl<C: ContextObject> std::fmt::Debug for BuiltinProgram<C> {
/// Generates an adapter for a BuiltinFunction between the Rust and the VM interface
#[macro_export]
macro_rules! declare_builtin_function {
($(#[$attr:meta])* $name:ident $(<$($generic_ident:tt : $generic_type:tt),+>)?, fn rust(
$vm:ident : &mut $ContextObject:ty,
($(#[$attr:meta])* $name:ident $(<$($generic_ident:tt : $generic_type:tt),+>)?, fn rust<$generic_ctx:tt>(
$vm:ident : &mut $generic_ctx_arg:tt,
$arg_a:ident : u64,
$arg_b:ident : u64,
$arg_c:ident : u64,
Expand All @@ -309,8 +309,8 @@ macro_rules! declare_builtin_function {
pub struct $name {}
impl $name {
/// Rust interface
pub fn rust $(<$($generic_ident : $generic_type),+>)? (
$vm: &mut $ContextObject,
pub fn rust<$generic_ctx: $crate::vm::ContextObject, $($($generic_ident : $generic_type),+)?> (
$vm: &mut $generic_ctx,
$arg_a: u64,
$arg_b: u64,
$arg_c: u64,
Expand All @@ -322,17 +322,16 @@ macro_rules! declare_builtin_function {
}
/// VM interface
#[allow(clippy::too_many_arguments)]
pub fn vm $(<$($generic_ident : $generic_type),+>)? (
$vm: *mut $crate::vm::EbpfVm<$ContextObject>,
pub fn vm<$generic_ctx: $crate::vm::ContextObject, $($($generic_ident : $generic_type),+)?> (
$vm: *mut $crate::vm::EbpfVm<$generic_ctx>,
$arg_a: u64,
$arg_b: u64,
$arg_c: u64,
$arg_d: u64,
$arg_e: u64,
) {
use $crate::vm::ContextObject;
let vm = unsafe {
&mut *($vm.cast::<u64>().offset(-($crate::vm::get_runtime_environment_key() as isize)).cast::<$crate::vm::EbpfVm<$ContextObject>>())
&mut *($vm.cast::<u64>().offset(-($crate::vm::get_runtime_environment_key() as isize)).cast::<$crate::vm::EbpfVm<$generic_ctx>>())
};
let config = vm.loader.get_config();
if config.enable_instruction_meter {
Expand Down
25 changes: 12 additions & 13 deletions src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ use crate::{
declare_builtin_function,
error::EbpfError,
memory_region::{AccessType, MemoryMapping},
vm::TestContextObject,
};
use std::{slice::from_raw_parts, str::from_utf8};

declare_builtin_function!(
/// Prints its **last three** arguments to standard output. The **first two** arguments are
/// **unused**. Returns the number of bytes written.
SyscallTracePrintf,
fn rust(
_context_object: &mut TestContextObject,
fn rust<T>(
_context_object: &mut T,
_arg1: u64,
_arg2: u64,
arg3: u64,
Expand All @@ -61,8 +60,8 @@ declare_builtin_function!(
/// The idea is to assemble five bytes into a single `u64`. For compatibility with the syscalls API,
/// each argument must be a `u64`.
SyscallGatherBytes,
fn rust(
_context_object: &mut TestContextObject,
fn rust<T>(
_context_object: &mut T,
arg1: u64,
arg2: u64,
arg3: u64,
Expand All @@ -85,8 +84,8 @@ declare_builtin_function!(
/// The memory is directly modified, and the syscall returns 0 in all
/// cases. Arguments 3 to 5 are unused.
SyscallMemFrob,
fn rust(
_context_object: &mut TestContextObject,
fn rust<T>(
_context_object: &mut T,
vm_addr: u64,
len: u64,
_arg3: u64,
Expand All @@ -110,8 +109,8 @@ declare_builtin_function!(
declare_builtin_function!(
/// C-like `strcmp`, return 0 if the strings are equal, and a non-null value otherwise.
SyscallStrCmp,
fn rust(
_context_object: &mut TestContextObject,
fn rust<T>(
_context_object: &mut T,
arg1: u64,
arg2: u64,
_arg3: u64,
Expand Down Expand Up @@ -148,8 +147,8 @@ declare_builtin_function!(
declare_builtin_function!(
/// Prints a NULL-terminated UTF-8 string.
SyscallString,
fn rust(
_context_object: &mut TestContextObject,
fn rust<T>(
_context_object: &mut T,
vm_addr: u64,
len: u64,
_arg3: u64,
Expand All @@ -173,8 +172,8 @@ declare_builtin_function!(
declare_builtin_function!(
/// Prints the five arguments formated as u64 in decimal.
SyscallU64,
fn rust(
_context_object: &mut TestContextObject,
fn rust<T>(
_context_object: &mut T,
arg1: u64,
arg2: u64,
arg3: u64,
Expand Down
4 changes: 2 additions & 2 deletions tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2658,8 +2658,8 @@ fn test_call_memfrob() {
declare_builtin_function!(
/// For test_nested_vm_syscall()
SyscallNestedVm,
fn rust(
_context_object: &mut TestContextObject,
fn rust<T>(
_context_object: &mut T,
depth: u64,
throw: u64,
_arg3: u64,
Expand Down