From 6be5daefa1a5b0f3016fbfd015c946349de73abb Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 24 Apr 2019 00:18:34 +0100 Subject: [PATCH 1/2] rust: call release_result in tests Also do not double-consume the input pointer. --- bindings/rust/evmc-vm/src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bindings/rust/evmc-vm/src/lib.rs b/bindings/rust/evmc-vm/src/lib.rs index 15c5587..83ae8b1 100644 --- a/bindings/rust/evmc-vm/src/lib.rs +++ b/bindings/rust/evmc-vm/src/lib.rs @@ -115,7 +115,7 @@ impl Into<*const ffi::evmc_result> for ExecutionResult { } } -/// Callback to pass across FFI, de-allocating the struct. +/// Callback to pass across FFI, de-allocating the optional output_data. extern "C" fn release_result(result: *const ffi::evmc_result) { unsafe { let tmp = Box::from_raw(result as *mut ffi::evmc_result); @@ -124,7 +124,6 @@ extern "C" fn release_result(result: *const ffi::evmc_result) { std::alloc::Layout::from_size_align(tmp.output_size, 1).expect("Bad layout"); std::alloc::dealloc(tmp.output_data as *mut u8, buf_layout); } - Box::from_raw(result as *mut ffi::evmc_result); } } @@ -192,6 +191,9 @@ mod tests { == &[0xc0, 0xff, 0xee, 0x71, 0x75] ); assert!((*f).create_address.bytes == [0u8; 20]); + if (*f).release.is_some() { + (*f).release.unwrap()(f); + } } } } From 0d757c4c3e6a6f24c0aa579196dfe489fa7eeae0 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 24 Apr 2019 00:21:36 +0100 Subject: [PATCH 2/2] rust: add test for ExecutionResult without data --- bindings/rust/evmc-vm/src/lib.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/bindings/rust/evmc-vm/src/lib.rs b/bindings/rust/evmc-vm/src/lib.rs index 83ae8b1..87f84c3 100644 --- a/bindings/rust/evmc-vm/src/lib.rs +++ b/bindings/rust/evmc-vm/src/lib.rs @@ -119,7 +119,7 @@ impl Into<*const ffi::evmc_result> for ExecutionResult { extern "C" fn release_result(result: *const ffi::evmc_result) { unsafe { let tmp = Box::from_raw(result as *mut ffi::evmc_result); - if tmp.output_data.is_null() { + if !tmp.output_data.is_null() { let buf_layout = std::alloc::Layout::from_size_align(tmp.output_size, 1).expect("Bad layout"); std::alloc::dealloc(tmp.output_data as *mut u8, buf_layout); @@ -196,4 +196,27 @@ mod tests { } } } + + #[test] + fn into_ffi_empty_data() { + let r = ExecutionResult::new( + ffi::evmc_status_code::EVMC_FAILURE, + 420, + None, + ffi::evmc_address { bytes: [0u8; 20] }, + ); + + let f: *const ffi::evmc_result = r.into(); + assert!(!f.is_null()); + unsafe { + assert!((*f).status_code == ffi::evmc_status_code::EVMC_FAILURE); + assert!((*f).gas_left == 420); + assert!((*f).output_data.is_null()); + assert!((*f).output_size == 0); + assert!((*f).create_address.bytes == [0u8; 20]); + if (*f).release.is_some() { + (*f).release.unwrap()(f); + } + } + } }