From 59b3067754e2153b5ea80d505a11bf30cd282a85 Mon Sep 17 00:00:00 2001 From: Reuven Podmazo Date: Wed, 2 Sep 2020 16:56:00 +0300 Subject: [PATCH] final touches --- .../packages/wasmi-runtime/src/exports.rs | 24 ++++++++++++++++--- .../wasmi-runtime/src/recursion_depth.rs | 8 +++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/cosmwasm/packages/wasmi-runtime/src/exports.rs b/cosmwasm/packages/wasmi-runtime/src/exports.rs index c5096a37d..b59e74b3c 100644 --- a/cosmwasm/packages/wasmi-runtime/src/exports.rs +++ b/cosmwasm/packages/wasmi-runtime/src/exports.rs @@ -119,7 +119,13 @@ pub unsafe extern "C" fn ecall_init( let _recursion_guard = match recursion_depth::guard() { Ok(rg) => rg, Err(err) => { - warn!("recursion limit exceeded, can not perform init!"); + // https://github.com/enigmampc/SecretNetwork/pull/517#discussion_r481924571 + // I believe that this error condition is currently unreachable. + // I think we can safely remove it completely right now, and have + // recursion_depth::increment() simply increment the counter with no further checks, + // but i wanted to stay on the safe side here, in case something changes in the + // future, and we can easily spot that we forgot to add a limit somewhere. + error!("recursion limit exceeded, can not perform init!"); return InitResult::Failure { err }; } }; @@ -210,7 +216,13 @@ pub unsafe extern "C" fn ecall_handle( let _recursion_guard = match recursion_depth::guard() { Ok(rg) => rg, Err(err) => { - warn!("recursion limit exceeded, can not perform handle!"); + // https://github.com/enigmampc/SecretNetwork/pull/517#discussion_r481924571 + // I believe that this error condition is currently unreachable. + // I think we can safely remove it completely right now, and have + // recursion_depth::increment() simply increment the counter with no further checks, + // but i wanted to stay on the safe side here, in case something changes in the + // future, and we can easily spot that we forgot to add a limit somewhere. + error!("recursion limit exceeded, can not perform handle!"); return HandleResult::Failure { err }; } }; @@ -297,7 +309,13 @@ pub unsafe extern "C" fn ecall_query( let _recursion_guard = match recursion_depth::guard() { Ok(rg) => rg, Err(err) => { - warn!("recursion limit exceeded, can not perform query!"); + // https://github.com/enigmampc/SecretNetwork/pull/517#discussion_r481924571 + // I believe that this error condition is currently unreachable. + // I think we can safely remove it completely right now, and have + // recursion_depth::increment() simply increment the counter with no further checks, + // but i wanted to stay on the safe side here, in case something changes in the + // future, and we can easily spot that we forgot to add a limit somewhere. + error!("recursion limit exceeded, can not perform query!"); return QueryResult::Failure { err }; } }; diff --git a/cosmwasm/packages/wasmi-runtime/src/recursion_depth.rs b/cosmwasm/packages/wasmi-runtime/src/recursion_depth.rs index 8d846f52a..8d7e201f9 100644 --- a/cosmwasm/packages/wasmi-runtime/src/recursion_depth.rs +++ b/cosmwasm/packages/wasmi-runtime/src/recursion_depth.rs @@ -4,14 +4,14 @@ use lazy_static::lazy_static; use enclave_ffi_types::EnclaveError; -const RECURSION_LIMIT: i32 = 5; +const RECURSION_LIMIT: u8 = 5; lazy_static! { /// This counter tracks the recursion depth of queries, /// and effectively the amount of loaded instances of WASMI. /// /// It is incremented before each computation begins and is decremented after each computation ends. - static ref RECURSION_DEPTH: SgxMutex = SgxMutex::new(0); + static ref RECURSION_DEPTH: SgxMutex = SgxMutex::new(0); } fn increment() -> Result<(), EnclaveError> { @@ -19,13 +19,13 @@ fn increment() -> Result<(), EnclaveError> { if *depth == RECURSION_LIMIT { return Err(EnclaveError::ExceededRecursionLimit); } - *depth += 1; + *depth = depth.saturating_add(1); Ok(()) } fn decrement() { let mut depth = RECURSION_DEPTH.lock().unwrap(); - *depth -= 1; + *depth = depth.saturating_sub(1); } /// Returns whether or not this is the last possible level of recursion