diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index 58cb4ed0e9..d58c660968 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -8618,8 +8618,8 @@ dependencies = [ "environmental", "parity-scale-codec", "sc-allocator", - "sp-core", "sp-maybe-compressed-blob", + "sp-sandbox", "sp-serializer", "sp-wasm-interface", "thiserror", @@ -8637,8 +8637,8 @@ dependencies = [ "sc-allocator", "sc-executor-common", "scoped-tls", - "sp-core", "sp-runtime-interface", + "sp-sandbox", "sp-wasm-interface", "wasmi", ] @@ -8655,9 +8655,9 @@ dependencies = [ "sc-allocator", "sc-executor-common", "sc-runtime-test", - "sp-core", "sp-io", "sp-runtime-interface", + "sp-sandbox", "sp-wasm-interface", "wasmtime", "wat", diff --git a/substrate/client/executor/Cargo.toml b/substrate/client/executor/Cargo.toml index 8e10de1b04..ad2288b927 100644 --- a/substrate/client/executor/Cargo.toml +++ b/substrate/client/executor/Cargo.toml @@ -14,26 +14,27 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -codec = { package = "parity-scale-codec", version = "3.0.0" } -sp-io = { version = "6.0.0", path = "../../primitives/io" } -sp-core = { version = "6.0.0", path = "../../primitives/core" } -sp-tasks = { version = "4.0.0-dev", path = "../../primitives/tasks" } -sp-trie = { version = "6.0.0", path = "../../primitives/trie" } -sp-version = { version = "5.0.0", path = "../../primitives/version" } -sp-panic-handler = { version = "4.0.0", path = "../../primitives/panic-handler" } -wasmi = "0.9.1" lazy_static = "1.4.0" -sp-api = { version = "4.0.0-dev", path = "../../primitives/api" } -sp-wasm-interface = { version = "6.0.0", path = "../../primitives/wasm-interface" } -sp-runtime-interface = { version = "6.0.0", path = "../../primitives/runtime-interface" } -sp-externalities = { version = "0.12.0", path = "../../primitives/externalities" } +lru = "0.7.5" +parking_lot = "0.12.0" +tracing = "0.1.29" +wasmi = "0.9.1" + +codec = { package = "parity-scale-codec", version = "3.0.0" } sc-executor-common = { version = "0.10.0-dev", path = "common" } sc-executor-wasmi = { version = "0.10.0-dev", path = "wasmi" } sc-executor-wasmtime = { version = "0.10.0-dev", path = "wasmtime", optional = true } -parking_lot = "0.12.0" +sp-api = { version = "4.0.0-dev", path = "../../primitives/api" } +sp-core = { version = "6.0.0", path = "../../primitives/core" } sp-core-hashing-proc-macro = { version = "5.0.0", path = "../../primitives/core/hashing/proc-macro" } -lru = "0.7.5" -tracing = "0.1.29" +sp-externalities = { version = "0.12.0", path = "../../primitives/externalities" } +sp-io = { version = "6.0.0", path = "../../primitives/io" } +sp-panic-handler = { version = "4.0.0", path = "../../primitives/panic-handler" } +sp-runtime-interface = { version = "6.0.0", path = "../../primitives/runtime-interface" } +sp-tasks = { version = "4.0.0-dev", path = "../../primitives/tasks" } +sp-trie = { version = "6.0.0", path = "../../primitives/trie" } +sp-version = { version = "5.0.0", path = "../../primitives/version" } +sp-wasm-interface = { version = "6.0.0", path = "../../primitives/wasm-interface" } [dev-dependencies] wat = "1.0" diff --git a/substrate/client/executor/common/Cargo.toml b/substrate/client/executor/common/Cargo.toml index 149d9fdc23..677148f709 100644 --- a/substrate/client/executor/common/Cargo.toml +++ b/substrate/client/executor/common/Cargo.toml @@ -14,17 +14,18 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -wasm-instrument = "0.1" -codec = { package = "parity-scale-codec", version = "3.0.0" } -wasmi = "0.9.1" -sp-core = { version = "6.0.0", path = "../../../primitives/core" } -sc-allocator = { version = "4.1.0-dev", path = "../../allocator" } -sp-wasm-interface = { version = "6.0.0", path = "../../../primitives/wasm-interface" } -sp-maybe-compressed-blob = { version = "4.1.0-dev", path = "../../../primitives/maybe-compressed-blob" } -sp-serializer = { version = "4.0.0-dev", path = "../../../primitives/serializer" } -thiserror = "1.0.30" environmental = "1.1.3" -wasmer = { version = "2.2", optional = true, features = ["singlepass"] } +thiserror = "1.0.30" +wasm-instrument = "0.1" +wasmi = "0.9.1" +wasmer = { version = "2.2", features = ["singlepass"], optional = true } + +codec = { package = "parity-scale-codec", version = "3.0.0" } +sc-allocator = { version = "4.1.0-dev", path = "../../allocator" } +sp-maybe-compressed-blob = { version = "4.1.0-dev", path = "../../../primitives/maybe-compressed-blob" } +sp-sandbox = { version = "0.10.0-dev", path = "../../../primitives/sandbox" } +sp-serializer = { version = "4.0.0-dev", path = "../../../primitives/serializer" } +sp-wasm-interface = { version = "6.0.0", path = "../../../primitives/wasm-interface" } [features] default = [] diff --git a/substrate/client/executor/common/src/sandbox.rs b/substrate/client/executor/common/src/sandbox.rs index 98eafaf32b..fe14c0865c 100644 --- a/substrate/client/executor/common/src/sandbox.rs +++ b/substrate/client/executor/common/src/sandbox.rs @@ -22,26 +22,26 @@ #[cfg(feature = "wasmer-sandbox")] mod wasmer_backend; - mod wasmi_backend; +use std::{collections::HashMap, rc::Rc}; + +use codec::Decode; +use sp_sandbox::env as sandbox_env; +use sp_wasm_interface::{FunctionContext, Pointer, WordSize}; + use crate::{ error::{self, Result}, util, }; -use codec::Decode; -use sp_core::sandbox as sandbox_primitives; -use sp_wasm_interface::{FunctionContext, Pointer, WordSize}; -use std::{collections::HashMap, rc::Rc}; #[cfg(feature = "wasmer-sandbox")] -use wasmer_backend::{ +use self::wasmer_backend::{ get_global as wasmer_get_global, instantiate as wasmer_instantiate, invoke as wasmer_invoke, new_memory as wasmer_new_memory, Backend as WasmerBackend, MemoryWrapper as WasmerMemoryWrapper, }; - -use wasmi_backend::{ +use self::wasmi_backend::{ get_global as wasmi_get_global, instantiate as wasmi_instantiate, invoke as wasmi_invoke, new_memory as wasmi_new_memory, MemoryWrapper as WasmiMemoryWrapper, }; @@ -243,7 +243,7 @@ fn decode_environment_definition( mut raw_env_def: &[u8], memories: &[Option], ) -> std::result::Result<(Imports, GuestToSupervisorFunctionMapping), InstantiationError> { - let env_def = sandbox_primitives::EnvironmentDefinition::decode(&mut raw_env_def) + let env_def = sandbox_env::EnvironmentDefinition::decode(&mut raw_env_def) .map_err(|_| InstantiationError::EnvironmentDefinitionCorrupted)?; let mut func_map = HashMap::new(); @@ -255,12 +255,12 @@ fn decode_environment_definition( let field = entry.field_name.clone(); match entry.entity { - sandbox_primitives::ExternEntity::Function(func_idx) => { + sandbox_env::ExternEntity::Function(func_idx) => { let externals_idx = guest_to_supervisor_mapping.define(SupervisorFuncIndex(func_idx as usize)); func_map.insert((module, field), externals_idx); }, - sandbox_primitives::ExternEntity::Memory(memory_idx) => { + sandbox_env::ExternEntity::Memory(memory_idx) => { let memory_ref = memories .get(memory_idx as usize) .cloned() @@ -446,7 +446,7 @@ impl Store
{ let backend_context = &self.backend_context; let maximum = match maximum { - sandbox_primitives::MEM_UNLIMITED => None, + sandbox_env::MEM_UNLIMITED => None, specified_limit => Some(specified_limit), }; diff --git a/substrate/client/executor/common/src/sandbox/wasmer_backend.rs b/substrate/client/executor/common/src/sandbox/wasmer_backend.rs index fd2f317457..ab585c7d15 100644 --- a/substrate/client/executor/common/src/sandbox/wasmer_backend.rs +++ b/substrate/client/executor/common/src/sandbox/wasmer_backend.rs @@ -18,20 +18,21 @@ //! Wasmer specific impls for sandbox -use crate::{ - error::{Error, Result}, - sandbox::Memory, - util::{checked_range, MemoryTransfer}, -}; -use codec::{Decode, Encode}; -use sp_core::sandbox::HostError; -use sp_wasm_interface::{FunctionContext, Pointer, ReturnValue, Value, WordSize}; use std::{cell::RefCell, collections::HashMap, rc::Rc}; + use wasmer::RuntimeError; -use crate::sandbox::{ - BackendInstance, GuestEnvironment, InstantiationError, SandboxContext, SandboxInstance, - SupervisorFuncIndex, +use codec::{Decode, Encode}; +use sp_sandbox::HostError; +use sp_wasm_interface::{FunctionContext, Pointer, ReturnValue, Value, WordSize}; + +use crate::{ + error::{Error, Result}, + sandbox::{ + BackendInstance, GuestEnvironment, InstantiationError, Memory, SandboxContext, + SandboxInstance, SupervisorFuncIndex, + }, + util::{checked_range, MemoryTransfer}, }; environmental::environmental!(SandboxContextStore: trait SandboxContext); diff --git a/substrate/client/executor/common/src/sandbox/wasmi_backend.rs b/substrate/client/executor/common/src/sandbox/wasmi_backend.rs index 0954287f52..9c7c154b5b 100644 --- a/substrate/client/executor/common/src/sandbox/wasmi_backend.rs +++ b/substrate/client/executor/common/src/sandbox/wasmi_backend.rs @@ -18,11 +18,11 @@ //! Wasmi specific impls for sandbox -use codec::{Decode, Encode}; -use sp_core::sandbox::HostError; -use sp_wasm_interface::{FunctionContext, Pointer, ReturnValue, Value, WordSize}; use std::rc::Rc; +use codec::{Decode, Encode}; +use sp_sandbox::HostError; +use sp_wasm_interface::{FunctionContext, Pointer, ReturnValue, Value, WordSize}; use wasmi::{ memory_units::Pages, ImportResolver, MemoryInstance, Module, ModuleInstance, RuntimeArgs, RuntimeValue, Trap, TrapKind, diff --git a/substrate/client/executor/runtime-test/Cargo.toml b/substrate/client/executor/runtime-test/Cargo.toml index 352ffdf7a6..507b35114d 100644 --- a/substrate/client/executor/runtime-test/Cargo.toml +++ b/substrate/client/executor/runtime-test/Cargo.toml @@ -13,24 +13,25 @@ repository = "https://github.com/paritytech/substrate/" targets = ["x86_64-unknown-linux-gnu"] [dependencies] +paste = "1.0.6" + sp-core = { version = "6.0.0", default-features = false, path = "../../../primitives/core" } sp-io = { version = "6.0.0", default-features = false, path = "../../../primitives/io", features = ["improved_panic_error_reporting"] } sp-runtime = { version = "6.0.0", default-features = false, path = "../../../primitives/runtime" } sp-sandbox = { version = "0.10.0-dev", default-features = false, path = "../../../primitives/sandbox" } sp-std = { version = "4.0.0", default-features = false, path = "../../../primitives/std" } sp-tasks = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/tasks" } -paste = "1.0.6" [build-dependencies] substrate-wasm-builder = { version = "5.0.0-dev", path = "../../../utils/wasm-builder" } [features] -default = [ "std" ] +default = ["std"] std = [ "sp-core/std", "sp-io/std", "sp-runtime/std", "sp-sandbox/std", "sp-std/std", - "sp-tasks/std", + "sp-tasks/std", ] diff --git a/substrate/client/executor/wasmi/Cargo.toml b/substrate/client/executor/wasmi/Cargo.toml index 99b8010c07..ed653dca0a 100644 --- a/substrate/client/executor/wasmi/Cargo.toml +++ b/substrate/client/executor/wasmi/Cargo.toml @@ -15,11 +15,12 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] log = "0.4.16" -wasmi = "0.9.1" -codec = { package = "parity-scale-codec", version = "3.0.0" } -sc-executor-common = { version = "0.10.0-dev", path = "../common" } -sc-allocator = { version = "4.1.0-dev", path = "../../allocator" } -sp-wasm-interface = { version = "6.0.0", path = "../../../primitives/wasm-interface" } -sp-runtime-interface = { version = "6.0.0", path = "../../../primitives/runtime-interface" } -sp-core = { version = "6.0.0", path = "../../../primitives/core" } scoped-tls = "1.0" +wasmi = "0.9.1" + +codec = { package = "parity-scale-codec", version = "3.0.0" } +sc-allocator = { version = "4.1.0-dev", path = "../../allocator" } +sc-executor-common = { version = "0.10.0-dev", path = "../common" } +sp-sandbox = { version = "0.10.0-dev", path = "../../../primitives/sandbox" } +sp-runtime-interface = { version = "6.0.0", path = "../../../primitives/runtime-interface" } +sp-wasm-interface = { version = "6.0.0", path = "../../../primitives/wasm-interface" } diff --git a/substrate/client/executor/wasmi/src/lib.rs b/substrate/client/executor/wasmi/src/lib.rs index 97c73c3454..ec98dc0df3 100644 --- a/substrate/client/executor/wasmi/src/lib.rs +++ b/substrate/client/executor/wasmi/src/lib.rs @@ -18,8 +18,17 @@ //! This crate provides an implementation of `WasmModule` that is baked by wasmi. -use codec::{Decode, Encode}; +use std::{cell::RefCell, rc::Rc, str, sync::Arc}; + use log::{debug, error, trace}; +use wasmi::{ + memory_units::Pages, + FuncInstance, ImportsBuilder, MemoryInstance, MemoryRef, Module, ModuleInstance, ModuleRef, + RuntimeValue::{self, I32, I64}, + TableRef, +}; + +use codec::{Decode, Encode}; use sc_executor_common::{ error::{Error, MessageWithBacktrace, WasmError}, runtime_blob::{DataSegmentsSnapshot, RuntimeBlob}, @@ -27,18 +36,11 @@ use sc_executor_common::{ util::MemoryTransfer, wasm_runtime::{InvokeMethod, WasmInstance, WasmModule}, }; -use sp_core::sandbox as sandbox_primitives; use sp_runtime_interface::unpack_ptr_and_len; +use sp_sandbox::env as sandbox_env; use sp_wasm_interface::{ Function, FunctionContext, MemoryId, Pointer, Result as WResult, Sandbox, WordSize, }; -use std::{cell::RefCell, rc::Rc, str, sync::Arc}; -use wasmi::{ - memory_units::Pages, - FuncInstance, ImportsBuilder, MemoryInstance, MemoryRef, Module, ModuleInstance, ModuleRef, - RuntimeValue::{self, I32, I64}, - TableRef, -}; struct FunctionExecutor { sandbox_store: Rc>>, @@ -155,15 +157,15 @@ impl Sandbox for FunctionExecutor { let len = buf_len as usize; let buffer = match sandboxed_memory.read(Pointer::new(offset as u32), len) { - Err(_) => return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS), + Err(_) => return Ok(sandbox_env::ERR_OUT_OF_BOUNDS), Ok(buffer) => buffer, }; if let Err(_) = self.memory.set(buf_ptr.into(), &buffer) { - return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS) + return Ok(sandbox_env::ERR_OUT_OF_BOUNDS) } - Ok(sandbox_primitives::ERR_OK) + Ok(sandbox_env::ERR_OK) } fn memory_set( @@ -179,15 +181,15 @@ impl Sandbox for FunctionExecutor { let len = val_len as usize; let buffer = match self.memory.get(val_ptr.into(), len) { - Err(_) => return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS), + Err(_) => return Ok(sandbox_env::ERR_OUT_OF_BOUNDS), Ok(buffer) => buffer, }; if let Err(_) = sandboxed_memory.write_from(Pointer::new(offset as u32), &buffer) { - return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS) + return Ok(sandbox_env::ERR_OUT_OF_BOUNDS) } - Ok(sandbox_primitives::ERR_OK) + Ok(sandbox_env::ERR_OK) } fn memory_teardown(&mut self, memory_id: MemoryId) -> WResult<()> { @@ -236,7 +238,7 @@ impl Sandbox for FunctionExecutor { state, &mut SandboxContext { dispatch_thunk, executor: self }, ) { - Ok(None) => Ok(sandbox_primitives::ERR_OK), + Ok(None) => Ok(sandbox_env::ERR_OK), Ok(Some(val)) => { // Serialize return value and write it back into the memory. sp_wasm_interface::ReturnValue::Value(val.into()).using_encoded(|val| { @@ -244,10 +246,10 @@ impl Sandbox for FunctionExecutor { Err("Return value buffer is too small")?; } self.write_memory(return_val, val).map_err(|_| "Return value buffer is OOB")?; - Ok(sandbox_primitives::ERR_OK) + Ok(sandbox_env::ERR_OK) }) }, - Err(_) => Ok(sandbox_primitives::ERR_EXECUTION), + Err(_) => Ok(sandbox_env::ERR_EXECUTION), } } @@ -280,7 +282,7 @@ impl Sandbox for FunctionExecutor { let guest_env = match sandbox::GuestEnvironment::decode(&*self.sandbox_store.borrow(), raw_env_def) { Ok(guest_env) => guest_env, - Err(_) => return Ok(sandbox_primitives::ERR_MODULE as u32), + Err(_) => return Ok(sandbox_env::ERR_MODULE as u32), }; let store = self.sandbox_store.clone(); @@ -294,8 +296,8 @@ impl Sandbox for FunctionExecutor { let instance_idx_or_err_code = match result.map(|i| i.register(&mut store.borrow_mut(), dispatch_thunk)) { Ok(instance_idx) => instance_idx, - Err(sandbox::InstantiationError::StartTrapped) => sandbox_primitives::ERR_EXECUTION, - Err(_) => sandbox_primitives::ERR_MODULE, + Err(sandbox::InstantiationError::StartTrapped) => sandbox_env::ERR_EXECUTION, + Err(_) => sandbox_env::ERR_MODULE, }; Ok(instance_idx_or_err_code) diff --git a/substrate/client/executor/wasmtime/Cargo.toml b/substrate/client/executor/wasmtime/Cargo.toml index 15f3a40cd4..5871629f47 100644 --- a/substrate/client/executor/wasmtime/Cargo.toml +++ b/substrate/client/executor/wasmtime/Cargo.toml @@ -13,23 +13,24 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -libc = "0.2.121" cfg-if = "1.0" +libc = "0.2.121" log = "0.4.16" parity-wasm = "0.42.0" -codec = { package = "parity-scale-codec", version = "3.0.0" } -sc-executor-common = { version = "0.10.0-dev", path = "../common" } -sp-wasm-interface = { version = "6.0.0", path = "../../../primitives/wasm-interface", features = ["wasmtime"] } -sp-runtime-interface = { version = "6.0.0", path = "../../../primitives/runtime-interface" } -sp-core = { version = "6.0.0", path = "../../../primitives/core" } -sc-allocator = { version = "4.1.0-dev", path = "../../allocator" } wasmtime = { version = "0.35.3", default-features = false, features = [ - "cache", - "cranelift", - "jitdump", - "parallel-compilation", + "cache", + "cranelift", + "jitdump", + "parallel-compilation", ] } +codec = { package = "parity-scale-codec", version = "3.0.0" } +sc-allocator = { version = "4.1.0-dev", path = "../../allocator" } +sc-executor-common = { version = "0.10.0-dev", path = "../common" } +sp-runtime-interface = { version = "6.0.0", path = "../../../primitives/runtime-interface" } +sp-sandbox = { version = "0.10.0-dev", path = "../../../primitives/sandbox" } +sp-wasm-interface = { version = "6.0.0", path = "../../../primitives/wasm-interface", features = ["wasmtime"] } + [dev-dependencies] sc-runtime-test = { version = "2.0.0", path = "../runtime-test" } sp-io = { version = "6.0.0", path = "../../../primitives/io" } diff --git a/substrate/client/executor/wasmtime/src/host.rs b/substrate/client/executor/wasmtime/src/host.rs index 23deacbf93..376eba8682 100644 --- a/substrate/client/executor/wasmtime/src/host.rs +++ b/substrate/client/executor/wasmtime/src/host.rs @@ -19,18 +19,20 @@ //! This module defines `HostState` and `HostContext` structs which provide logic and state //! required for execution of host. -use crate::{runtime::StoreData, util}; -use codec::{Decode, Encode}; use log::trace; +use wasmtime::{Caller, Func, Val}; + +use codec::{Decode, Encode}; use sc_allocator::FreeingBumpHeapAllocator; use sc_executor_common::{ error::Result, sandbox::{self, SupervisorFuncIndex}, util::MemoryTransfer, }; -use sp_core::sandbox as sandbox_primitives; +use sp_sandbox::env as sandbox_env; use sp_wasm_interface::{FunctionContext, MemoryId, Pointer, Sandbox, WordSize}; -use wasmtime::{Caller, Func, Val}; + +use crate::{runtime::StoreData, util}; // The sandbox store is inside of a Option>> so that we can temporarily borrow it. struct SandboxStore(Option>>); @@ -164,15 +166,15 @@ impl<'a> Sandbox for HostContext<'a> { let len = buf_len as usize; let buffer = match sandboxed_memory.read(Pointer::new(offset as u32), len) { - Err(_) => return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS), + Err(_) => return Ok(sandbox_env::ERR_OUT_OF_BOUNDS), Ok(buffer) => buffer, }; if util::write_memory_from(&mut self.caller, buf_ptr, &buffer).is_err() { - return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS) + return Ok(sandbox_env::ERR_OUT_OF_BOUNDS) } - Ok(sandbox_primitives::ERR_OK) + Ok(sandbox_env::ERR_OK) } fn memory_set( @@ -187,15 +189,15 @@ impl<'a> Sandbox for HostContext<'a> { let len = val_len as usize; let buffer = match util::read_memory(&self.caller, val_ptr, len) { - Err(_) => return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS), + Err(_) => return Ok(sandbox_env::ERR_OUT_OF_BOUNDS), Ok(buffer) => buffer, }; if sandboxed_memory.write_from(Pointer::new(offset as u32), &buffer).is_err() { - return Ok(sandbox_primitives::ERR_OUT_OF_BOUNDS) + return Ok(sandbox_env::ERR_OUT_OF_BOUNDS) } - Ok(sandbox_primitives::ERR_OK) + Ok(sandbox_env::ERR_OK) } fn memory_teardown(&mut self, memory_id: MemoryId) -> sp_wasm_interface::Result<()> { @@ -236,7 +238,7 @@ impl<'a> Sandbox for HostContext<'a> { ); match result { - Ok(None) => Ok(sandbox_primitives::ERR_OK), + Ok(None) => Ok(sandbox_env::ERR_OK), Ok(Some(val)) => { // Serialize return value and write it back into the memory. sp_wasm_interface::ReturnValue::Value(val.into()).using_encoded(|val| { @@ -245,10 +247,10 @@ impl<'a> Sandbox for HostContext<'a> { } ::write_memory(self, return_val, val) .map_err(|_| "can't write return value")?; - Ok(sandbox_primitives::ERR_OK) + Ok(sandbox_env::ERR_OK) }) }, - Err(_) => Ok(sandbox_primitives::ERR_EXECUTION), + Err(_) => Ok(sandbox_env::ERR_EXECUTION), } } @@ -285,7 +287,7 @@ impl<'a> Sandbox for HostContext<'a> { let guest_env = match sandbox::GuestEnvironment::decode(&self.sandbox_store(), raw_env_def) { Ok(guest_env) => guest_env, - Err(_) => return Ok(sandbox_primitives::ERR_MODULE as u32), + Err(_) => return Ok(sandbox_env::ERR_MODULE as u32), }; let mut store = self @@ -315,8 +317,8 @@ impl<'a> Sandbox for HostContext<'a> { let instance_idx_or_err_code = match result { Ok(instance) => instance.register(&mut self.sandbox_store_mut(), dispatch_thunk), - Err(sandbox::InstantiationError::StartTrapped) => sandbox_primitives::ERR_EXECUTION, - Err(_) => sandbox_primitives::ERR_MODULE, + Err(sandbox::InstantiationError::StartTrapped) => sandbox_env::ERR_EXECUTION, + Err(_) => sandbox_env::ERR_MODULE, }; Ok(instance_idx_or_err_code as u32) diff --git a/substrate/primitives/core/src/lib.rs b/substrate/primitives/core/src/lib.rs index b7c8b69e8a..0709b615cf 100644 --- a/substrate/primitives/core/src/lib.rs +++ b/substrate/primitives/core/src/lib.rs @@ -62,7 +62,6 @@ pub mod hash; #[cfg(feature = "std")] mod hasher; pub mod offchain; -pub mod sandbox; pub mod sr25519; pub mod testing; #[cfg(feature = "std")] diff --git a/substrate/primitives/sandbox/Cargo.toml b/substrate/primitives/sandbox/Cargo.toml index 22d295f313..db9301a530 100644 --- a/substrate/primitives/sandbox/Cargo.toml +++ b/substrate/primitives/sandbox/Cargo.toml @@ -19,28 +19,29 @@ wasmi = { version = "0.9.1", default-features = false, features = ["core"] } wasmi = "0.9.0" [dependencies] -wasmi = { version = "0.9.0", optional = true } -sp-core = { version = "6.0.0", default-features = false, path = "../core" } -sp-std = { version = "4.0.0", default-features = false, path = "../std" } -sp-io = { version = "6.0.0", default-features = false, path = "../io" } -sp-wasm-interface = { version = "6.0.0", default-features = false, path = "../wasm-interface" } -codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false } log = { version = "0.4", default-features = false } +wasmi = { version = "0.9.0", optional = true } + +codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false } +sp-core = { version = "6.0.0", default-features = false, path = "../core" } +sp-io = { version = "6.0.0", default-features = false, path = "../io" } +sp-std = { version = "4.0.0", default-features = false, path = "../std" } +sp-wasm-interface = { version = "6.0.0", default-features = false, path = "../wasm-interface" } [dev-dependencies] -wat = "1.0" assert_matches = "1.3.0" +wat = "1.0" [features] default = ["std"] std = [ - "wasmi", - "sp-core/std", - "sp-std/std", - "codec/std", - "sp-io/std", - "sp-wasm-interface/std", "log/std", + "wasmi", + "codec/std", + "sp-core/std", + "sp-io/std", + "sp-std/std", + "sp-wasm-interface/std", ] strict = [] wasmer-sandbox = [] diff --git a/substrate/primitives/sandbox/src/embedded_executor.rs b/substrate/primitives/sandbox/src/embedded_executor.rs old mode 100755 new mode 100644 index 43967a0a38..8a20cc1b39 --- a/substrate/primitives/sandbox/src/embedded_executor.rs +++ b/substrate/primitives/sandbox/src/embedded_executor.rs @@ -17,18 +17,20 @@ //! An embedded WASM executor utilizing `wasmi`. -use super::{Error, HostError, HostFuncType, ReturnValue, Value, TARGET}; use alloc::string::String; -use log::debug; -use sp_std::{ - borrow::ToOwned, collections::btree_map::BTreeMap, fmt, marker::PhantomData, prelude::*, -}; + use wasmi::{ memory_units::Pages, Externals, FuncInstance, FuncRef, GlobalDescriptor, GlobalRef, ImportResolver, MemoryDescriptor, MemoryInstance, MemoryRef, Module, ModuleInstance, ModuleRef, RuntimeArgs, RuntimeValue, Signature, TableDescriptor, TableRef, Trap, TrapKind, }; +use sp_std::{ + borrow::ToOwned, collections::btree_map::BTreeMap, fmt, marker::PhantomData, prelude::*, +}; + +use crate::{Error, HostError, HostFuncType, ReturnValue, Value, TARGET}; + /// The linear memory used by the sandbox. #[derive(Clone)] pub struct Memory { @@ -162,13 +164,18 @@ impl ImportResolver for EnvironmentDefinitionBuilder { ) -> Result { let key = (module_name.as_bytes().to_owned(), field_name.as_bytes().to_owned()); let externval = self.map.get(&key).ok_or_else(|| { - debug!(target: TARGET, "Export {}:{} not found", module_name, field_name); + log::debug!(target: TARGET, "Export {}:{} not found", module_name, field_name); wasmi::Error::Instantiation(String::new()) })?; let host_func_idx = match *externval { ExternVal::HostFunc(ref idx) => idx, _ => { - debug!(target: TARGET, "Export {}:{} is not a host func", module_name, field_name); + log::debug!( + target: TARGET, + "Export {}:{} is not a host func", + module_name, + field_name, + ); return Err(wasmi::Error::Instantiation(String::new())) }, }; @@ -181,7 +188,7 @@ impl ImportResolver for EnvironmentDefinitionBuilder { _field_name: &str, _global_type: &GlobalDescriptor, ) -> Result { - debug!(target: TARGET, "Importing globals is not supported yet"); + log::debug!(target: TARGET, "Importing globals is not supported yet"); Err(wasmi::Error::Instantiation(String::new())) } @@ -193,13 +200,18 @@ impl ImportResolver for EnvironmentDefinitionBuilder { ) -> Result { let key = (module_name.as_bytes().to_owned(), field_name.as_bytes().to_owned()); let externval = self.map.get(&key).ok_or_else(|| { - debug!(target: TARGET, "Export {}:{} not found", module_name, field_name); + log::debug!(target: TARGET, "Export {}:{} not found", module_name, field_name); wasmi::Error::Instantiation(String::new()) })?; let memory = match *externval { ExternVal::Memory(ref m) => m, _ => { - debug!(target: TARGET, "Export {}:{} is not a memory", module_name, field_name); + log::debug!( + target: TARGET, + "Export {}:{} is not a memory", + module_name, + field_name, + ); return Err(wasmi::Error::Instantiation(String::new())) }, }; @@ -212,7 +224,7 @@ impl ImportResolver for EnvironmentDefinitionBuilder { _field_name: &str, _table_type: &TableDescriptor, ) -> Result { - debug!("Importing tables is not supported yet"); + log::debug!("Importing tables is not supported yet"); Err(wasmi::Error::Instantiation(String::new())) } } diff --git a/substrate/primitives/core/src/sandbox.rs b/substrate/primitives/sandbox/src/env.rs similarity index 92% rename from substrate/primitives/core/src/sandbox.rs rename to substrate/primitives/sandbox/src/env.rs index 1f408a3b8c..94b1c5e467 100644 --- a/substrate/primitives/core/src/sandbox.rs +++ b/substrate/primitives/sandbox/src/env.rs @@ -18,14 +18,16 @@ //! Definition of a sandbox environment. use codec::{Decode, Encode}; + +use sp_core::RuntimeDebug; use sp_std::vec::Vec; /// Error error that can be returned from host function. -#[derive(Encode, Decode, crate::RuntimeDebug)] +#[derive(Encode, Decode, RuntimeDebug)] pub struct HostError; /// Describes an entity to define or import into the environment. -#[derive(Clone, PartialEq, Eq, Encode, Decode, crate::RuntimeDebug)] +#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)] pub enum ExternEntity { /// Function that is specified by an index in a default table of /// a module that creates the sandbox. @@ -42,7 +44,7 @@ pub enum ExternEntity { /// /// Each entry has a two-level name and description of an entity /// being defined. -#[derive(Clone, PartialEq, Eq, Encode, Decode, crate::RuntimeDebug)] +#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)] pub struct Entry { /// Module name of which corresponding entity being defined. pub module_name: Vec, @@ -53,7 +55,7 @@ pub struct Entry { } /// Definition of runtime that could be used by sandboxed code. -#[derive(Clone, PartialEq, Eq, Encode, Decode, crate::RuntimeDebug)] +#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)] pub struct EnvironmentDefinition { /// Vector of all entries in the environment definition. pub entries: Vec, diff --git a/substrate/primitives/sandbox/src/host_executor.rs b/substrate/primitives/sandbox/src/host_executor.rs old mode 100755 new mode 100644 index 83721f40e3..e62c051262 --- a/substrate/primitives/sandbox/src/host_executor.rs +++ b/substrate/primitives/sandbox/src/host_executor.rs @@ -17,12 +17,13 @@ //! A WASM executor utilizing the sandbox runtime interface of the host. -use super::{Error, HostFuncType, ReturnValue, Value}; use codec::{Decode, Encode}; -use sp_core::sandbox as sandbox_primitives; + use sp_io::sandbox; use sp_std::{marker, mem, prelude::*, rc::Rc, slice, vec}; +use crate::{env, Error, HostFuncType, ReturnValue, Value}; + mod ffi { use super::HostFuncType; use sp_std::mem; @@ -68,11 +69,10 @@ pub struct Memory { impl super::SandboxMemory for Memory { fn new(initial: u32, maximum: Option) -> Result { - let maximum = - if let Some(maximum) = maximum { maximum } else { sandbox_primitives::MEM_UNLIMITED }; + let maximum = if let Some(maximum) = maximum { maximum } else { env::MEM_UNLIMITED }; match sandbox::memory_new(initial, maximum) { - sandbox_primitives::ERR_MODULE => Err(Error::Module), + env::ERR_MODULE => Err(Error::Module), memory_idx => Ok(Memory { handle: Rc::new(MemoryHandle { memory_idx }) }), } } @@ -81,8 +81,8 @@ impl super::SandboxMemory for Memory { let result = sandbox::memory_get(self.handle.memory_idx, offset, buf.as_mut_ptr(), buf.len() as u32); match result { - sandbox_primitives::ERR_OK => Ok(()), - sandbox_primitives::ERR_OUT_OF_BOUNDS => Err(Error::OutOfBounds), + env::ERR_OK => Ok(()), + env::ERR_OUT_OF_BOUNDS => Err(Error::OutOfBounds), _ => unreachable!(), } } @@ -95,8 +95,8 @@ impl super::SandboxMemory for Memory { val.len() as u32, ); match result { - sandbox_primitives::ERR_OK => Ok(()), - sandbox_primitives::ERR_OUT_OF_BOUNDS => Err(Error::OutOfBounds), + env::ERR_OK => Ok(()), + env::ERR_OUT_OF_BOUNDS => Err(Error::OutOfBounds), _ => unreachable!(), } } @@ -104,22 +104,18 @@ impl super::SandboxMemory for Memory { /// A builder for the environment of the sandboxed WASM module. pub struct EnvironmentDefinitionBuilder { - env_def: sandbox_primitives::EnvironmentDefinition, + env_def: env::EnvironmentDefinition, retained_memories: Vec, _marker: marker::PhantomData, } impl EnvironmentDefinitionBuilder { - fn add_entry( - &mut self, - module: N1, - field: N2, - extern_entity: sandbox_primitives::ExternEntity, - ) where + fn add_entry(&mut self, module: N1, field: N2, extern_entity: env::ExternEntity) + where N1: Into>, N2: Into>, { - let entry = sandbox_primitives::Entry { + let entry = env::Entry { module_name: module.into(), field_name: field.into(), entity: extern_entity, @@ -131,7 +127,7 @@ impl EnvironmentDefinitionBuilder { impl super::SandboxEnvironmentBuilder for EnvironmentDefinitionBuilder { fn new() -> EnvironmentDefinitionBuilder { EnvironmentDefinitionBuilder { - env_def: sandbox_primitives::EnvironmentDefinition { entries: Vec::new() }, + env_def: env::EnvironmentDefinition { entries: Vec::new() }, retained_memories: Vec::new(), _marker: marker::PhantomData::, } @@ -142,7 +138,7 @@ impl super::SandboxEnvironmentBuilder for EnvironmentDefinitionBui N1: Into>, N2: Into>, { - let f = sandbox_primitives::ExternEntity::Function(f as u32); + let f = env::ExternEntity::Function(f as u32); self.add_entry(module, field, f); } @@ -154,7 +150,7 @@ impl super::SandboxEnvironmentBuilder for EnvironmentDefinitionBui // We need to retain memory to keep it alive while the EnvironmentDefinitionBuilder alive. self.retained_memories.push(mem.clone()); - let mem = sandbox_primitives::ExternEntity::Memory(mem.handle.memory_idx as u32); + let mem = env::ExternEntity::Memory(mem.handle.memory_idx as u32); self.add_entry(module, field, mem); } } @@ -228,8 +224,8 @@ impl super::SandboxInstance for Instance { ); let instance_idx = match result { - sandbox_primitives::ERR_MODULE => return Err(Error::Module), - sandbox_primitives::ERR_EXECUTION => return Err(Error::Execution), + env::ERR_MODULE => return Err(Error::Module), + env::ERR_EXECUTION => return Err(Error::Execution), instance_idx => instance_idx, }; @@ -256,12 +252,12 @@ impl super::SandboxInstance for Instance { ); match result { - sandbox_primitives::ERR_OK => { + env::ERR_OK => { let return_val = ReturnValue::decode(&mut &return_val[..]).map_err(|_| Error::Execution)?; Ok(return_val) }, - sandbox_primitives::ERR_EXECUTION => Err(Error::Execution), + env::ERR_EXECUTION => Err(Error::Execution), _ => unreachable!(), } } diff --git a/substrate/primitives/sandbox/src/lib.rs b/substrate/primitives/sandbox/src/lib.rs old mode 100755 new mode 100644 index 537c7cbb31..b6b4a5a97d --- a/substrate/primitives/sandbox/src/lib.rs +++ b/substrate/primitives/sandbox/src/lib.rs @@ -40,26 +40,27 @@ extern crate alloc; +pub mod embedded_executor; +pub mod env; +#[cfg(not(feature = "std"))] +pub mod host_executor; + +use sp_core::RuntimeDebug; use sp_std::prelude::*; -pub use sp_core::sandbox::HostError; pub use sp_wasm_interface::{ReturnValue, Value}; +#[cfg(not(all(feature = "wasmer-sandbox", not(feature = "std"))))] +pub use self::embedded_executor as default_executor; +pub use self::env::HostError; +#[cfg(all(feature = "wasmer-sandbox", not(feature = "std")))] +pub use self::host_executor as default_executor; + /// The target used for logging. const TARGET: &str = "runtime::sandbox"; -pub mod embedded_executor; -#[cfg(not(feature = "std"))] -pub mod host_executor; - -#[cfg(all(feature = "wasmer-sandbox", not(feature = "std")))] -pub use host_executor as default_executor; - -#[cfg(not(all(feature = "wasmer-sandbox", not(feature = "std"))))] -pub use embedded_executor as default_executor; - /// Error that can occur while using this crate. -#[derive(sp_core::RuntimeDebug)] +#[derive(RuntimeDebug)] pub enum Error { /// Module is not valid, couldn't be instantiated. Module,