contracts: Replace sp-sandbox and wasmi-validation by newest wasmi (#12501)

* Replace sp-sandbox and wasmi-validation by just wasmi

* ".git/.scripts/bench-bot.sh" pallet dev pallet_contracts

* Re-check original code on re-instrumentation

* Fix clippy

* ".git/.scripts/bench-bot.sh" pallet dev pallet_contracts

* Apply suggestions from code review

Co-authored-by: Robin Freyler <robin.freyler@gmail.com>

* Replace wasmi by ::wasmi

* Bump wasmi to 0.20

* Add explanation for `unreachable`

* Change proof

* Fixup master merge

* ".git/.scripts/bench-bot.sh" pallet dev pallet_contracts

* Fixup naming inconsistencies introduced by reentrancy PR

* Fix `scan_imports` docs

* Apply suggestions from code review

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Fixup suggestions

* Remove unnecessary &mut

* Fix test

* ".git/.scripts/bench-bot.sh" pallet dev pallet_contracts

* Fix benchmark merge fail

* ".git/.scripts/bench-bot.sh" pallet dev pallet_contracts

* Fix docs as suggested by code review

* Improve docs for `CodeRejected`

* Apply suggestions from code review

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Fix logic bug when setting `deterministic_only`

* Don't panic when module fails to compile

* Apply suggestions from code review

Co-authored-by: Robin Freyler <robin.freyler@gmail.com>

Co-authored-by: command-bot <>
Co-authored-by: Robin Freyler <robin.freyler@gmail.com>
Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
This commit is contained in:
Alexander Theißen
2022-11-24 23:51:36 +01:00
committed by GitHub
parent e69c3649b5
commit 08657f14b7
23 changed files with 1909 additions and 1634 deletions
@@ -19,22 +19,20 @@
/// ! sandbox to execute the wasm code. This is because we do not need the full
/// ! environment that provides the seal interface as imported functions.
use super::{code::WasmModule, Config};
use crate::wasm::{Environment, PrefabWasmModule};
use sp_core::crypto::UncheckedFrom;
use sp_sandbox::{
default_executor::{EnvironmentDefinitionBuilder, Instance, Memory},
SandboxEnvironmentBuilder, SandboxInstance,
};
use wasmi::{errors::LinkerError, Func, Linker, StackLimits, Store};
/// Minimal execution environment without any exported functions.
/// Minimal execution environment without any imported functions.
pub struct Sandbox {
instance: Instance<()>,
_memory: Option<Memory>,
entry_point: Func,
store: Store<()>,
}
impl Sandbox {
/// Invoke the `call` function of a contract code and panic on any execution error.
pub fn invoke(&mut self) {
self.instance.invoke("call", &[], &mut ()).unwrap();
self.entry_point.call(&mut self.store, &[], &mut []).unwrap();
}
}
@@ -46,10 +44,27 @@ where
/// Creates an instance from the supplied module and supplies as much memory
/// to the instance as the module declares as imported.
fn from(module: &WasmModule<T>) -> Self {
let mut env_builder = EnvironmentDefinitionBuilder::new();
let memory = module.add_memory(&mut env_builder);
let instance = Instance::new(&module.code, &env_builder, &mut ())
.expect("Failed to create benchmarking Sandbox instance");
Self { instance, _memory: memory }
let memory = module
.memory
.as_ref()
.map(|mem| (mem.min_pages, mem.max_pages))
.unwrap_or((0, 0));
let (store, _memory, instance) = PrefabWasmModule::<T>::instantiate::<EmptyEnv, _>(
&module.code,
(),
memory,
StackLimits::default(),
)
.expect("Failed to create benchmarking Sandbox instance");
let entry_point = instance.get_export(&store, "call").unwrap().into_func().unwrap();
Self { entry_point, store }
}
}
struct EmptyEnv;
impl Environment<()> for EmptyEnv {
fn define(_store: &mut Store<()>, _linker: &mut Linker<()>) -> Result<(), LinkerError> {
Ok(())
}
}