Skip to content

Commit

Permalink
Update Rust bindings to consider host_context as an opaque pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed Nov 5, 2019
1 parent 71bdfad commit 97fea0b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 37 deletions.
8 changes: 4 additions & 4 deletions bindings/rust/evmc-declare/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,14 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream {
use evmc_vm::EvmcVm;

// TODO: context is optional in case of the "precompiles" capability
if instance.is_null() || host.is_null() || context.is_null() || msg.is_null() || (code.is_null() && code_size != 0) {
if instance.is_null() || host.is_null() || msg.is_null() || (code.is_null() && code_size != 0) {
// These are irrecoverable errors that violate the EVMC spec.
std::process::abort();
}

assert!(!instance.is_null());
// TODO: context is optional in case of the "precompiles" capability
assert!(!context.is_null());
// TODO: host is optional in case of the "precompiles" capability
assert!(!host.is_null());
assert!(!msg.is_null());

let execution_message: ::evmc_vm::ExecutionMessage = unsafe {
Expand All @@ -379,7 +379,7 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream {
let mut execution_context = unsafe {
::evmc_vm::ExecutionContext::new(
host.as_ref().expect("EVMC host is null"),
context.as_mut().expect("EVMC context is null")
context,
)
};
container.execute(revision, code_ref, &execution_message, &mut execution_context)
Expand Down
6 changes: 0 additions & 6 deletions bindings/rust/evmc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ impl Default for evmc_bytes32 {
}
}

impl Default for evmc_host_context {
fn default() -> Self {
evmc_host_context { _unused: [0u8; 0] }
}
}

#[cfg(test)]
mod tests {
use std::mem::size_of;
Expand Down
6 changes: 3 additions & 3 deletions bindings/rust/evmc-vm/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ mod tests {
get_block_hash: None,
emit_log: None,
};
let mut host_context = ::evmc_sys::evmc_host_context::default();
let host_context = std::ptr::null_mut();

let mut context = ExecutionContext::new(&host, &mut host_context);
let mut context = ExecutionContext::new(&host, host_context);
let container = EvmcContainer::<TestVm>::new(instance);
assert_eq!(
container
Expand All @@ -150,7 +150,7 @@ mod tests {

let ptr = unsafe { EvmcContainer::into_ffi_pointer(container) };

let mut context = ExecutionContext::new(&host, &mut host_context);
let mut context = ExecutionContext::new(&host, host_context);
let container = unsafe { EvmcContainer::<TestVm>::from_ffi_pointer(ptr) };
assert_eq!(
container
Expand Down
47 changes: 23 additions & 24 deletions bindings/rust/evmc-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub type ExecutionTxContext = ffi::evmc_tx_context;
/// to the executing VM.
pub struct ExecutionContext<'a> {
host: &'a ffi::evmc_host_interface,
context: &'a mut ffi::evmc_host_context,
context: *mut ffi::evmc_host_context,
tx_context: ExecutionTxContext,
}

Expand Down Expand Up @@ -197,11 +197,11 @@ impl ExecutionMessage {
impl<'a> ExecutionContext<'a> {
pub fn new(
host: &'a ffi::evmc_host_interface,
_context: &'a mut ffi::evmc_host_context,
_context: *mut ffi::evmc_host_context,
) -> Self {
let _tx_context = unsafe {
assert!((*host).get_tx_context.is_some());
(*host).get_tx_context.unwrap()(_context as *mut ffi::evmc_host_context)
(*host).get_tx_context.unwrap()(_context)
};

ExecutionContext {
Expand All @@ -221,7 +221,7 @@ impl<'a> ExecutionContext<'a> {
unsafe {
assert!((*self.host).account_exists.is_some());
(*self.host).account_exists.unwrap()(
self.context as *mut ffi::evmc_host_context,
self.context,
address as *const Address,
)
}
Expand All @@ -232,7 +232,7 @@ impl<'a> ExecutionContext<'a> {
unsafe {
assert!((*self.host).get_storage.is_some());
(*self.host).get_storage.unwrap()(
self.context as *mut ffi::evmc_host_context,
self.context,
address as *const Address,
key as *const Bytes32,
)
Expand All @@ -249,7 +249,7 @@ impl<'a> ExecutionContext<'a> {
unsafe {
assert!((*self.host).set_storage.is_some());
(*self.host).set_storage.unwrap()(
self.context as *mut ffi::evmc_host_context,
self.context,
address as *const Address,
key as *const Bytes32,
value as *const Bytes32,
Expand All @@ -262,7 +262,7 @@ impl<'a> ExecutionContext<'a> {
unsafe {
assert!((*self.host).get_balance.is_some());
(*self.host).get_balance.unwrap()(
self.context as *mut ffi::evmc_host_context,
self.context,
address as *const Address,
)
}
Expand All @@ -273,7 +273,7 @@ impl<'a> ExecutionContext<'a> {
unsafe {
assert!((*self.host).get_code_size.is_some());
(*self.host).get_code_size.unwrap()(
self.context as *mut ffi::evmc_host_context,
self.context,
address as *const Address,
)
}
Expand All @@ -284,7 +284,7 @@ impl<'a> ExecutionContext<'a> {
unsafe {
assert!((*self.host).get_code_size.is_some());
(*self.host).get_code_hash.unwrap()(
self.context as *mut ffi::evmc_host_context,
self.context,
address as *const Address,
)
}
Expand All @@ -295,7 +295,7 @@ impl<'a> ExecutionContext<'a> {
unsafe {
assert!((*self.host).copy_code.is_some());
(*self.host).copy_code.unwrap()(
self.context as *mut ffi::evmc_host_context,
self.context,
address as *const Address,
code_offset,
// FIXME: ensure that alignment of the array elements is OK
Expand All @@ -310,7 +310,7 @@ impl<'a> ExecutionContext<'a> {
unsafe {
assert!((*self.host).selfdestruct.is_some());
(*self.host).selfdestruct.unwrap()(
self.context as *mut ffi::evmc_host_context,
self.context,
address as *const Address,
beneficiary as *const Address,
)
Expand Down Expand Up @@ -349,7 +349,7 @@ impl<'a> ExecutionContext<'a> {
unsafe {
assert!((*self.host).call.is_some());
(*self.host).call.unwrap()(
self.context as *mut ffi::evmc_host_context,
self.context,
&message as *const ffi::evmc_message,
)
.into()
Expand All @@ -360,7 +360,7 @@ impl<'a> ExecutionContext<'a> {
pub fn get_block_hash(&mut self, num: i64) -> Bytes32 {
unsafe {
assert!((*self.host).get_block_hash.is_some());
(*self.host).get_block_hash.unwrap()(self.context as *mut ffi::evmc_host_context, num)
(*self.host).get_block_hash.unwrap()(self.context, num)
}
}

Expand All @@ -369,7 +369,7 @@ impl<'a> ExecutionContext<'a> {
unsafe {
assert!((*self.host).emit_log.is_some());
(*self.host).emit_log.unwrap()(
self.context as *mut ffi::evmc_host_context,
self.context,
address as *const Address,
// FIXME: ensure that alignment of the array elements is OK
data.as_ptr(),
Expand Down Expand Up @@ -821,14 +821,13 @@ mod tests {

#[test]
fn execution_context() {
let mut host_context = ffi::evmc_host_context::default();
let mut host_context_copy = host_context.clone();
let host_context = std::ptr::null_mut();
let host_interface = get_dummy_host_interface();
let exe_context = ExecutionContext::new(&host_interface, &mut host_context);
let exe_context = ExecutionContext::new(&host_interface, host_context);
let a = exe_context.get_tx_context();

let b =
unsafe { get_dummy_tx_context(&mut host_context_copy as *mut ffi::evmc_host_context) };
unsafe { get_dummy_tx_context(host_context) };

assert_eq!(a.block_gas_limit, b.block_gas_limit);
assert_eq!(a.block_timestamp, b.block_timestamp);
Expand All @@ -840,9 +839,9 @@ mod tests {
// This address is useless. Just a dummy parameter for the interface function.
let test_addr = Address { bytes: [0u8; 20] };
let host = get_dummy_host_interface();
let mut host_context = ffi::evmc_host_context::default();
let host_context = std::ptr::null_mut();

let mut exe_context = ExecutionContext::new(&host, &mut host_context);
let mut exe_context = ExecutionContext::new(&host, host_context);

let a: usize = 105023;
let b = exe_context.get_code_size(&test_addr);
Expand All @@ -855,8 +854,8 @@ mod tests {
// This address is useless. Just a dummy parameter for the interface function.
let test_addr = ffi::evmc_address { bytes: [0u8; 20] };
let host = get_dummy_host_interface();
let mut host_context = ffi::evmc_host_context::default();
let mut exe_context = ExecutionContext::new(&host, &mut host_context);
let host_context = std::ptr::null_mut();
let mut exe_context = ExecutionContext::new(&host, host_context);

let message = ExecutionMessage::new(
ffi::evmc_call_kind::EVMC_CALL,
Expand Down Expand Up @@ -884,8 +883,8 @@ mod tests {
// This address is useless. Just a dummy parameter for the interface function.
let test_addr = ffi::evmc_address { bytes: [0u8; 20] };
let host = get_dummy_host_interface();
let mut host_context = ffi::evmc_host_context::default();
let mut exe_context = ExecutionContext::new(&host, &mut host_context);
let host_context = std::ptr::null_mut();
let mut exe_context = ExecutionContext::new(&host, host_context);

let data = vec![0xc0, 0xff, 0xfe];

Expand Down

0 comments on commit 97fea0b

Please sign in to comment.