From 752a3efec25de949df017588e79d6fd64cc1d2c7 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sun, 27 Dec 2020 13:33:04 +0100 Subject: [PATCH] Handle gas depletion correctly --- contracts/hackatom/tests/integration.rs | 3 +-- packages/vm/src/cache.rs | 1 - packages/vm/src/environment.rs | 14 +++++++++++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/contracts/hackatom/tests/integration.rs b/contracts/hackatom/tests/integration.rs index dff8619cba..fa14ebc843 100644 --- a/contracts/hackatom/tests/integration.rs +++ b/contracts/hackatom/tests/integration.rs @@ -297,8 +297,7 @@ fn handle_panic() { &to_vec(&HandleMsg::Panic {}).unwrap(), ); match handle_res.unwrap_err() { - // TODO: Don't accept GasDepletion here (https://github.com/CosmWasm/cosmwasm/issues/501) - VmError::RuntimeErr { .. } | VmError::GasDepletion { .. } => {} + VmError::RuntimeErr { .. } => {} err => panic!("Unexpected error: {:?}", err), } } diff --git a/packages/vm/src/cache.rs b/packages/vm/src/cache.rs index 72002fb86f..145f6ea9bd 100644 --- a/packages/vm/src/cache.rs +++ b/packages/vm/src/cache.rs @@ -510,7 +510,6 @@ mod tests { } #[test] - #[ignore] // fails due to the inability to handle gas depletion (https://github.com/wasmerio/wasmer/issues/1931) fn recovers_from_out_of_gas() { let mut cache = unsafe { Cache::new(make_testing_options()).unwrap() }; let id = cache.save_wasm(CONTRACT).unwrap(); diff --git a/packages/vm/src/environment.rs b/packages/vm/src/environment.rs index aa19962a02..40aecedbf1 100644 --- a/packages/vm/src/environment.rs +++ b/packages/vm/src/environment.rs @@ -141,7 +141,19 @@ impl Environment { } None => Err(VmError::uninitialized_context_data("wasmer_instance")), })?; - Ok(func.call(args)?) + + func.call(args).map_err(|runtime_err| -> VmError { + self.with_context_data(|context_data| match context_data.wasmer_instance { + Some(instance_ptr) => { + let instance_ref = unsafe { instance_ptr.as_ref() }; + match get_remaining_points(instance_ref) { + MeteringPoints::Remaining(_) => VmError::from(runtime_err), + MeteringPoints::Exhausted => VmError::gas_depletion(), + } + } + None => VmError::uninitialized_context_data("wasmer_instance"), + }) + }) } pub fn with_storage_from_context(&self, callback: C) -> VmResult