From 1c39d463214010bdddd5a03bfca79765e486f6f4 Mon Sep 17 00:00:00 2001 From: Jake Lang Date: Thu, 8 Aug 2019 17:43:02 -0400 Subject: [PATCH 1/2] rust: properly bind lifetimes to ensure that execute() can propagate references to ExecutionContext --- bindings/rust/evmc-vm/src/container.rs | 3 ++- bindings/rust/evmc-vm/src/lib.rs | 10 +++++----- examples/example-rust-vm/src/lib.rs | 8 ++++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/bindings/rust/evmc-vm/src/container.rs b/bindings/rust/evmc-vm/src/container.rs index 5bac7ee..5701269 100644 --- a/bindings/rust/evmc-vm/src/container.rs +++ b/bindings/rust/evmc-vm/src/container.rs @@ -126,8 +126,8 @@ mod tests { emit_log: None, }; let mut backing_context = ::evmc_sys::evmc_context { host: &host }; - let mut context = ExecutionContext::new(&mut backing_context); + let mut context = ExecutionContext::new(&mut backing_context); let container = EvmcContainer::::new(instance); assert_eq!( container @@ -143,6 +143,7 @@ mod tests { let ptr = unsafe { EvmcContainer::into_ffi_pointer(Box::new(container)) }; + let mut context = ExecutionContext::new(&mut backing_context); let container = unsafe { EvmcContainer::::from_ffi_pointer(ptr) }; assert_eq!( container diff --git a/bindings/rust/evmc-vm/src/lib.rs b/bindings/rust/evmc-vm/src/lib.rs index 52a27c4..d3071dd 100644 --- a/bindings/rust/evmc-vm/src/lib.rs +++ b/bindings/rust/evmc-vm/src/lib.rs @@ -18,12 +18,12 @@ pub use types::*; /// Trait EVMC VMs have to implement. pub trait EvmcVm { fn init() -> Self; - fn execute( + fn execute<'a>( &self, revision: ffi::evmc_revision, - code: &[u8], - message: &ExecutionMessage, - context: &mut ExecutionContext, + code: &'a [u8], + message: &'a ExecutionMessage, + context: &'a mut ExecutionContext<'a>, ) -> ExecutionResult; } @@ -806,7 +806,7 @@ mod tests { // sanitizers to complain let mut context_raw_copy = context_raw.clone(); - let mut exe_context = ExecutionContext::new(&mut context_raw); + let exe_context = ExecutionContext::new(&mut context_raw); let a = exe_context.get_tx_context(); let b = unsafe { get_dummy_tx_context(&mut context_raw_copy as *mut ffi::evmc_context) }; diff --git a/examples/example-rust-vm/src/lib.rs b/examples/example-rust-vm/src/lib.rs index bedc6fc..360460a 100644 --- a/examples/example-rust-vm/src/lib.rs +++ b/examples/example-rust-vm/src/lib.rs @@ -14,12 +14,12 @@ impl EvmcVm for ExampleRustVM { ExampleRustVM {} } - fn execute( + fn execute<'a>( &self, _revision: evmc_sys::evmc_revision, - _code: &[u8], - message: &ExecutionMessage, - _context: &mut ExecutionContext, + _code: &'a [u8], + message: &'a ExecutionMessage, + _context: &'a mut ExecutionContext<'a>, ) -> ExecutionResult { if message.kind() != evmc_sys::evmc_call_kind::EVMC_CALL { return ExecutionResult::failure(); From f9c05b565cbc2526db890bf11ceb2ec365e7b161 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 9 Aug 2019 10:38:48 +0200 Subject: [PATCH 2/2] rust: use the where syntax consistenly in EvmcContainer --- bindings/rust/evmc-vm/src/container.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bindings/rust/evmc-vm/src/container.rs b/bindings/rust/evmc-vm/src/container.rs index 5701269..0bc5129 100644 --- a/bindings/rust/evmc-vm/src/container.rs +++ b/bindings/rust/evmc-vm/src/container.rs @@ -8,13 +8,19 @@ use crate::EvmcVm; use std::ops::Deref; /// Container struct for EVMC instances and user-defined data. -pub struct EvmcContainer { +pub struct EvmcContainer +where + T: EvmcVm + Sized, +{ #[allow(dead_code)] instance: ::evmc_sys::evmc_instance, vm: T, } -impl EvmcContainer { +impl EvmcContainer +where + T: EvmcVm + Sized, +{ /// Basic constructor. pub fn new(_instance: ::evmc_sys::evmc_instance) -> Self { Self {