Refactor sp-sandbox; make sure both sandbox executors are always tested (#10173)

* sp-sandbox: convert executors into normal `mod`s instead of using `include!`

* sp-sandbox: run `cargo fmt` on `host_executor.rs`

* sp-sandbox: abstract away the executors behind traits

* sp_sandbox: always compile both executors when possible

* sc-executor: make sure all sandbox tests run on both sandbox executors

* sc-executor: fix brainfart: actually call into the sandbox through the trait

* sc-runtime-test: fix cargo fmt

* sc-runtime-test: deduplicate executor-specific sandbox test entrypoints

* sc-executor: test each sandbox executor in a separate test

* cargo fmt (Github's conflict resolving thingy broke indentation)
This commit is contained in:
Koute
2021-11-08 21:52:11 +09:00
committed by GitHub
parent fe36fe85d1
commit a7e3d819f8
12 changed files with 351 additions and 228 deletions
@@ -35,7 +35,10 @@ use pwasm_utils::parity_wasm::{
};
use sp_core::crypto::UncheckedFrom;
use sp_runtime::traits::Hash;
use sp_sandbox::{EnvironmentDefinitionBuilder, Memory};
use sp_sandbox::{
default_executor::{EnvironmentDefinitionBuilder, Memory},
SandboxEnvironmentBuilder, SandboxMemory,
};
use sp_std::{borrow::ToOwned, prelude::*};
/// Pass to `create_code` in order to create a compiled `WasmModule`.
@@ -20,7 +20,10 @@
/// ! environment that provides the seal interface as imported functions.
use super::{code::WasmModule, Config};
use sp_core::crypto::UncheckedFrom;
use sp_sandbox::{EnvironmentDefinitionBuilder, Instance, Memory};
use sp_sandbox::{
default_executor::{EnvironmentDefinitionBuilder, Instance, Memory},
SandboxEnvironmentBuilder, SandboxInstance,
};
/// Minimal execution environment without any exported functions.
pub struct Sandbox {
+5 -4
View File
@@ -36,6 +36,7 @@ use crate::{
use codec::{Decode, Encode};
use frame_support::dispatch::DispatchError;
use sp_core::crypto::UncheckedFrom;
use sp_sandbox::{SandboxEnvironmentBuilder, SandboxInstance, SandboxMemory};
use sp_std::prelude::*;
#[cfg(test)]
pub use tests::MockExt;
@@ -182,8 +183,8 @@ where
function: &ExportedFunction,
input_data: Vec<u8>,
) -> ExecResult {
let memory =
sp_sandbox::Memory::new(self.initial, Some(self.maximum)).unwrap_or_else(|_| {
let memory = sp_sandbox::default_executor::Memory::new(self.initial, Some(self.maximum))
.unwrap_or_else(|_| {
// unlike `.expect`, explicit panic preserves the source location.
// Needed as we can't use `RUST_BACKTRACE` in here.
panic!(
@@ -193,7 +194,7 @@ where
)
});
let mut imports = sp_sandbox::EnvironmentDefinitionBuilder::new();
let mut imports = sp_sandbox::default_executor::EnvironmentDefinitionBuilder::new();
imports.add_memory(self::prepare::IMPORT_MODULE_MEMORY, "memory", memory.clone());
runtime::Env::impls(&mut |module, name, func_ptr| {
imports.add_host_func(module, name, func_ptr);
@@ -209,7 +210,7 @@ where
// Instantiate the instance from the instrumented module code and invoke the contract
// entrypoint.
let result = sp_sandbox::Instance::new(&code, &imports, &mut runtime)
let result = sp_sandbox::default_executor::Instance::new(&code, &imports, &mut runtime)
.and_then(|mut instance| instance.invoke(function.identifier(), &[], &mut runtime));
runtime.to_execution_result(result)
@@ -32,6 +32,7 @@ use pwasm_utils::parity_wasm::elements::ValueType;
use sp_core::{crypto::UncheckedFrom, Bytes};
use sp_io::hashing::{blake2_128, blake2_256, keccak_256, sha2_256};
use sp_runtime::traits::Bounded;
use sp_sandbox::SandboxMemory;
use sp_std::prelude::*;
/// Every error that can be returned to a contract when it calls any of the host functions.
@@ -357,7 +358,7 @@ fn already_charged(_: u32) -> Option<RuntimeCosts> {
pub struct Runtime<'a, E: Ext + 'a> {
ext: &'a mut E,
input_data: Option<Vec<u8>>,
memory: sp_sandbox::Memory,
memory: sp_sandbox::default_executor::Memory,
trap_reason: Option<TrapReason>,
}
@@ -367,7 +368,11 @@ where
<E::T as frame_system::Config>::AccountId:
UncheckedFrom<<E::T as frame_system::Config>::Hash> + AsRef<[u8]>,
{
pub fn new(ext: &'a mut E, input_data: Vec<u8>, memory: sp_sandbox::Memory) -> Self {
pub fn new(
ext: &'a mut E,
input_data: Vec<u8>,
memory: sp_sandbox::default_executor::Memory,
) -> Self {
Runtime { ext, input_data: Some(input_data), memory, trap_reason: None }
}