Skip to content

Commit

Permalink
final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
reuvenpo committed Sep 2, 2020
1 parent f3833c2 commit 59b3067
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
24 changes: 21 additions & 3 deletions cosmwasm/packages/wasmi-runtime/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
};
Expand Down Expand Up @@ -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 };
}
};
Expand Down Expand Up @@ -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 };
}
};
Expand Down
8 changes: 4 additions & 4 deletions cosmwasm/packages/wasmi-runtime/src/recursion_depth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ 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<i32> = SgxMutex::new(0);
static ref RECURSION_DEPTH: SgxMutex<u8> = SgxMutex::new(0);
}

fn increment() -> Result<(), EnclaveError> {
let mut depth = RECURSION_DEPTH.lock().unwrap();
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
Expand Down

0 comments on commit 59b3067

Please sign in to comment.