sp-sandbox: move the sandbox module of sp-core into sp-sandbox (#11027)

* sp-sandbox: move the sandbox module of sp-core into sp-sandbox

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Fix

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Fix

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
This commit is contained in:
Qinxuan Chen
2022-04-26 17:25:41 +08:00
committed by GitHub
parent 94dac682b4
commit e9b69bc1b0
17 changed files with 198 additions and 177 deletions
+16 -15
View File
@@ -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"
+11 -10
View File
@@ -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 = []
+12 -12
View File
@@ -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<Memory>],
) -> 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<DT: Clone> Store<DT> {
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),
};
@@ -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);
@@ -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,
@@ -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",
]
+8 -7
View File
@@ -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" }
+23 -21
View File
@@ -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<RefCell<sandbox::Store<wasmi::FuncRef>>>,
@@ -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)
+12 -11
View File
@@ -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" }
+18 -16
View File
@@ -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<Box<..>>> so that we can temporarily borrow it.
struct SandboxStore(Option<Box<sandbox::Store<Func>>>);
@@ -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> {
}
<HostContext as FunctionContext>::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)