From f4f07fff17a3a13d78588409ef751c7b3f0ae448 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 4 Jul 2019 21:00:44 +0100 Subject: [PATCH] rust: make ExecutionContext optional in EvmcVm.execute() --- bindings/rust/evmc-declare-tests/src/lib.rs | 2 +- bindings/rust/evmc-declare/src/lib.rs | 18 ++++++++++-------- bindings/rust/evmc-vm/src/container.rs | 6 +++--- bindings/rust/evmc-vm/src/lib.rs | 2 +- examples/example-rust-vm/src/lib.rs | 7 ++++++- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/bindings/rust/evmc-declare-tests/src/lib.rs b/bindings/rust/evmc-declare-tests/src/lib.rs index a2ef12e98..8640d4fcf 100644 --- a/bindings/rust/evmc-declare-tests/src/lib.rs +++ b/bindings/rust/evmc-declare-tests/src/lib.rs @@ -22,7 +22,7 @@ impl EvmcVm for FooVM { _revision: evmc_sys::evmc_revision, _code: &[u8], _message: &ExecutionMessage, - _context: &mut ExecutionContext, + _context: Option<&mut ExecutionContext>, ) -> ExecutionResult { ExecutionResult::success(1337, None) } diff --git a/bindings/rust/evmc-declare/src/lib.rs b/bindings/rust/evmc-declare/src/lib.rs index 4821a5660..53e9dd580 100644 --- a/bindings/rust/evmc-declare/src/lib.rs +++ b/bindings/rust/evmc-declare/src/lib.rs @@ -22,14 +22,14 @@ //! ExampleVM {} //! } //! -//! fn execute(&self, revision: evmc_vm::ffi::evmc_revision, code: &[u8], message: &evmc_vm::ExecutionMessage, context: &mut evmc_vm::ExecutionContext) -> evmc_vm::ExecutionResult { +//! fn execute(&self, revision: evmc_vm::ffi::evmc_revision, code: &[u8], message: &evmc_vm::ExecutionMessage, context: Option<&mut evmc_vm::ExecutionContext>) -> evmc_vm::ExecutionResult { //! evmc_vm::ExecutionResult::success(1337, None) //! } //! } //! ``` // Set a higher recursion limit because parsing certain token trees might fail with the default of 64. -#![recursion_limit = "128"] +#![recursion_limit = "160"] extern crate proc_macro; @@ -338,8 +338,6 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream { use evmc_vm::EvmcVm; assert!(!instance.is_null()); - // TODO: context is optional in case of the "precompiles" capability - assert!(!context.is_null()); assert!(!msg.is_null()); let execution_message: ::evmc_vm::ExecutionMessage = unsafe { @@ -361,10 +359,14 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream { }; let result = ::std::panic::catch_unwind(|| { - let mut execution_context = unsafe { - ::evmc_vm::ExecutionContext::new(context.as_mut().expect("EVMC context is null")) - }; - container.execute(revision, code_ref, &execution_message, &mut execution_context) + if context.is_null() { + container.execute(revision, code_ref, &execution_message, None) + } else { + let mut execution_context = unsafe { + ::evmc_vm::ExecutionContext::new(context.as_mut().expect("EVMC context is null")) + }; + container.execute(revision, code_ref, &execution_message, Some(&mut execution_context)) + } }); let result = if result.is_err() { diff --git a/bindings/rust/evmc-vm/src/container.rs b/bindings/rust/evmc-vm/src/container.rs index 0bc512952..87705eba8 100644 --- a/bindings/rust/evmc-vm/src/container.rs +++ b/bindings/rust/evmc-vm/src/container.rs @@ -68,7 +68,7 @@ mod tests { _revision: evmc_sys::evmc_revision, _code: &[u8], _message: &ExecutionMessage, - _context: &mut ExecutionContext, + _context: Option<&mut ExecutionContext>, ) -> ExecutionResult { ExecutionResult::failure() } @@ -141,7 +141,7 @@ mod tests { evmc_sys::evmc_revision::EVMC_PETERSBURG, &code, &message, - &mut context + Some(&mut context) ) .get_status_code(), ::evmc_sys::evmc_status_code::EVMC_FAILURE @@ -157,7 +157,7 @@ mod tests { evmc_sys::evmc_revision::EVMC_PETERSBURG, &code, &message, - &mut context + Some(&mut context) ) .get_status_code(), ::evmc_sys::evmc_status_code::EVMC_FAILURE diff --git a/bindings/rust/evmc-vm/src/lib.rs b/bindings/rust/evmc-vm/src/lib.rs index d3071ddcd..4e1222d14 100644 --- a/bindings/rust/evmc-vm/src/lib.rs +++ b/bindings/rust/evmc-vm/src/lib.rs @@ -23,7 +23,7 @@ pub trait EvmcVm { revision: ffi::evmc_revision, code: &'a [u8], message: &'a ExecutionMessage, - context: &'a mut ExecutionContext<'a>, + context: Option<&'a mut ExecutionContext<'a>>, ) -> ExecutionResult; } diff --git a/examples/example-rust-vm/src/lib.rs b/examples/example-rust-vm/src/lib.rs index 360460a1f..009240b5d 100644 --- a/examples/example-rust-vm/src/lib.rs +++ b/examples/example-rust-vm/src/lib.rs @@ -19,8 +19,13 @@ impl EvmcVm for ExampleRustVM { _revision: evmc_sys::evmc_revision, _code: &'a [u8], message: &'a ExecutionMessage, - _context: &'a mut ExecutionContext<'a>, + _context: Option<&'a mut ExecutionContext<'a>>, ) -> ExecutionResult { + if _context.is_none() { + return ExecutionResult::failure(); + } + let _context = _context.unwrap(); + if message.kind() != evmc_sys::evmc_call_kind::EVMC_CALL { return ExecutionResult::failure(); }