rust: properly bind lifetimes to ensure that execute() can propagate references to ExecutionContext

This commit is contained in:
Jake Lang 2019-08-08 17:43:02 -04:00 committed by Alex Beregszaszi
parent 598432e5e5
commit 1c39d46321
3 changed files with 11 additions and 10 deletions

View File

@ -126,8 +126,8 @@ mod tests {
emit_log: None, emit_log: None,
}; };
let mut backing_context = ::evmc_sys::evmc_context { host: &host }; 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::<TestVm>::new(instance); let container = EvmcContainer::<TestVm>::new(instance);
assert_eq!( assert_eq!(
container container
@ -143,6 +143,7 @@ mod tests {
let ptr = unsafe { EvmcContainer::into_ffi_pointer(Box::new(container)) }; let ptr = unsafe { EvmcContainer::into_ffi_pointer(Box::new(container)) };
let mut context = ExecutionContext::new(&mut backing_context);
let container = unsafe { EvmcContainer::<TestVm>::from_ffi_pointer(ptr) }; let container = unsafe { EvmcContainer::<TestVm>::from_ffi_pointer(ptr) };
assert_eq!( assert_eq!(
container container

View File

@ -18,12 +18,12 @@ pub use types::*;
/// Trait EVMC VMs have to implement. /// Trait EVMC VMs have to implement.
pub trait EvmcVm { pub trait EvmcVm {
fn init() -> Self; fn init() -> Self;
fn execute( fn execute<'a>(
&self, &self,
revision: ffi::evmc_revision, revision: ffi::evmc_revision,
code: &[u8], code: &'a [u8],
message: &ExecutionMessage, message: &'a ExecutionMessage,
context: &mut ExecutionContext, context: &'a mut ExecutionContext<'a>,
) -> ExecutionResult; ) -> ExecutionResult;
} }
@ -806,7 +806,7 @@ mod tests {
// sanitizers to complain // sanitizers to complain
let mut context_raw_copy = context_raw.clone(); 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 a = exe_context.get_tx_context();
let b = unsafe { get_dummy_tx_context(&mut context_raw_copy as *mut ffi::evmc_context) }; let b = unsafe { get_dummy_tx_context(&mut context_raw_copy as *mut ffi::evmc_context) };

View File

@ -14,12 +14,12 @@ impl EvmcVm for ExampleRustVM {
ExampleRustVM {} ExampleRustVM {}
} }
fn execute( fn execute<'a>(
&self, &self,
_revision: evmc_sys::evmc_revision, _revision: evmc_sys::evmc_revision,
_code: &[u8], _code: &'a [u8],
message: &ExecutionMessage, message: &'a ExecutionMessage,
_context: &mut ExecutionContext, _context: &'a mut ExecutionContext<'a>,
) -> ExecutionResult { ) -> ExecutionResult {
if message.kind() != evmc_sys::evmc_call_kind::EVMC_CALL { if message.kind() != evmc_sys::evmc_call_kind::EVMC_CALL {
return ExecutionResult::failure(); return ExecutionResult::failure();