Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Wasmi to v0.32.0-beta.5 #2941

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion substrate/frame/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ serde = { version = "1", optional = true, features = ["derive"] }
smallvec = { version = "1", default-features = false, features = [
"const_generics",
] }
wasmi = { version = "0.31", default-features = false }
wasmi = { version = "0.32.0-beta.5", default-features = false }
impl-trait-for-tuples = "0.2"

# Only used in benchmarking to generate contract code
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/contracts/proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl HostFnReturn {
Self::U64 => quote! { ::core::primitive::u64 },
};
quote! {
::core::result::Result<#ok, ::wasmi::core::Trap>
::core::result::Result<#ok, ::wasmi::Error>
}
}
}
Expand Down Expand Up @@ -660,7 +660,7 @@ fn expand_functions(def: &EnvDef, expand_blocks: bool, host_state: TokenStream2)
let into_host = if expand_blocks {
quote! {
|reason| {
::wasmi::core::Trap::from(reason)
::wasmi::Error::host(reason)
}
}
} else {
Expand Down
7 changes: 3 additions & 4 deletions substrate/frame/contracts/src/wasm/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use sp_runtime::{traits::Hash, DispatchError};
#[cfg(any(test, feature = "runtime-benchmarks"))]
use sp_std::prelude::Vec;
use wasmi::{
core::ValueType as WasmiValueType, Config as WasmiConfig, Engine, ExternType,
FuelConsumptionMode, Module, StackLimits,
core::ValueType as WasmiValueType, Config as WasmiConfig, Engine, ExternType, Module,
StackLimits,
};

/// Imported memory must be located inside this module. The reason for hardcoding is that current
Expand Down Expand Up @@ -71,8 +71,7 @@ impl LoadedModule {
.wasm_extended_const(false)
.wasm_saturating_float_to_int(false)
.floats(matches!(determinism, Determinism::Relaxed))
.consume_fuel(true)
.fuel_consumption_mode(FuelConsumptionMode::Eager);
.consume_fuel(true);

if let Some(stack_limits) = stack_limits {
config.set_stack_limits(stack_limits);
Expand Down
72 changes: 38 additions & 34 deletions substrate/frame/contracts/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,49 +441,53 @@ impl<'a, E: Ext + 'a> Runtime<'a, E> {

/// Converts the sandbox result and the runtime state into the execution outcome.
pub fn to_execution_result(self, sandbox_result: Result<(), wasmi::Error>) -> ExecResult {
use wasmi::core::TrapCode::OutOfFuel;
use wasmi::{
core::TrapCode,
errors::{ErrorKind, FuelError},
};
use TrapReason::*;

match sandbox_result {
let Err(error) = sandbox_result else {
// Contract returned from main function -> no data was returned.
Ok(_) => Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() }),
return Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() })
};
if let ErrorKind::Fuel(FuelError::OutOfFuel) = error.kind() {
// `OutOfGas` when host asks engine to consume more than left in the _store_.
// We should never get this case, as gas meter is being charged (and hence raises error)
// first.
Err(wasmi::Error::Store(_)) => Err(Error::<E::T>::OutOfGas.into()),
// Contract either trapped or some host function aborted the execution.
Err(wasmi::Error::Trap(trap)) => {
if let Some(OutOfFuel) = trap.trap_code() {
// `OutOfGas` during engine execution.
return Err(Error::<E::T>::OutOfGas.into())
}
// If we encoded a reason then it is some abort generated by a host function.
if let Some(reason) = &trap.downcast_ref::<TrapReason>() {
match &reason {
Return(ReturnData { flags, data }) => {
let flags = ReturnFlags::from_bits(*flags)
.ok_or(Error::<E::T>::InvalidCallFlags)?;
return Ok(ExecReturnValue { flags, data: data.to_vec() })
},
Termination =>
return Ok(ExecReturnValue {
flags: ReturnFlags::empty(),
data: Vec::new(),
}),
SupervisorError(error) => return Err((*error).into()),
}
}
return Err(Error::<E::T>::OutOfGas.into())
}
match error.as_trap_code() {
Some(TrapCode::OutOfFuel) => {
// `OutOfGas` during engine execution.
return Err(Error::<E::T>::OutOfGas.into())
},
Some(_trap_code) => {
// Otherwise the trap came from the contract itself.
Err(Error::<E::T>::ContractTrapped.into())
return Err(Error::<E::T>::ContractTrapped.into())
},
// Any other error is returned only if instantiation or linking failed (i.e.
// wasm binary tried to import a function that is not provided by the host).
// This shouldn't happen because validation process ought to reject such binaries.
//
// Because panics are really undesirable in the runtime code, we treat this as
// a trap for now. Eventually, we might want to revisit this.
Err(_) => Err(Error::<E::T>::CodeRejected.into()),
None => {},
}
// If we encoded a reason then it is some abort generated by a host function.
if let Some(reason) = &error.downcast_ref::<TrapReason>() {
match &reason {
Return(ReturnData { flags, data }) => {
let flags =
ReturnFlags::from_bits(*flags).ok_or(Error::<E::T>::InvalidCallFlags)?;
return Ok(ExecReturnValue { flags, data: data.to_vec() })
},
Termination =>
return Ok(ExecReturnValue { flags: ReturnFlags::empty(), data: Vec::new() }),
SupervisorError(error) => return Err((*error).into()),
}
}
// Any other error is returned only if instantiation or linking failed (i.e.
// wasm binary tried to import a function that is not provided by the host).
// This shouldn't happen because validation process ought to reject such binaries.
//
// Because panics are really undesirable in the runtime code, we treat this as
// a trap for now. Eventually, we might want to revisit this.
Err(Error::<E::T>::CodeRejected.into())
}

/// Get a mutable reference to the inner `Ext`.
Expand Down
Loading