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
+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)