Less slices (#9176)

* Less slices

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Squirrel
2021-06-23 13:33:48 +01:00
committed by GitHub
parent 7b50bbb9bf
commit 07449840bd
11 changed files with 41 additions and 46 deletions
@@ -187,10 +187,10 @@ fn trap(msg: &'static str) -> Trap {
TrapKind::Host(Box::new(Error::Other(msg.into()))).into()
}
fn deserialize_result(serialized_result: &[u8]) -> std::result::Result<Option<RuntimeValue>, Trap> {
fn deserialize_result(mut serialized_result: &[u8]) -> std::result::Result<Option<RuntimeValue>, Trap> {
use self::sandbox_primitives::HostError;
use sp_wasm_interface::ReturnValue;
let result_val = std::result::Result::<ReturnValue, HostError>::decode(&mut &serialized_result[..])
let result_val = std::result::Result::<ReturnValue, HostError>::decode(&mut serialized_result)
.map_err(|_| trap("Decoding Result<ReturnValue, HostError> failed!"))?;
match result_val {
@@ -379,10 +379,10 @@ pub enum InstantiationError {
}
fn decode_environment_definition(
raw_env_def: &[u8],
mut raw_env_def: &[u8],
memories: &[Option<MemoryRef>],
) -> std::result::Result<(Imports, GuestToSupervisorFunctionMapping), InstantiationError> {
let env_def = sandbox_primitives::EnvironmentDefinition::decode(&mut &raw_env_def[..])
let env_def = sandbox_primitives::EnvironmentDefinition::decode(&mut raw_env_def)
.map_err(|_| InstantiationError::EnvironmentDefinitionCorrupted)?;
let mut func_map = HashMap::new();
+9 -11
View File
@@ -177,7 +177,7 @@ impl RuntimeCache {
/// Prepares a WASM module instance and executes given function for it.
///
/// This uses internal cache to find avaiable instance or create a new one.
/// This uses internal cache to find available instance or create a new one.
/// # Parameters
///
/// `code` - Provides external code or tells the executor to fetch it from storage.
@@ -196,7 +196,7 @@ impl RuntimeCache {
///
/// `f` - Function to execute.
///
/// # Returns result of `f` wrapped in an additonal result.
/// # Returns result of `f` wrapped in an additional result.
/// In case of failure one of two errors can be returned:
///
/// `Err::InvalidCode` is returned for runtime code issues.
@@ -337,7 +337,7 @@ pub fn create_wasm_runtime_with_code(
}
}
fn decode_version(version: &[u8]) -> Result<RuntimeVersion, WasmError> {
fn decode_version(mut version: &[u8]) -> Result<RuntimeVersion, WasmError> {
let v: RuntimeVersion = sp_api::OldRuntimeVersion::decode(&mut &version[..])
.map_err(|_|
WasmError::Instantiation(
@@ -347,7 +347,7 @@ fn decode_version(version: &[u8]) -> Result<RuntimeVersion, WasmError> {
let core_api_id = sp_core::hashing::blake2_64(b"Core");
if v.has_api_with(&core_api_id, |v| v >= 3) {
sp_api::RuntimeVersion::decode(&mut &version[..])
sp_api::RuntimeVersion::decode(&mut version)
.map_err(|_|
WasmError::Instantiation("failed to decode \"Core_version\" result".into())
)
@@ -367,9 +367,7 @@ fn decode_runtime_apis(apis: &[u8]) -> Result<Vec<([u8; 8], u32)>, WasmError> {
<[u8; RUNTIME_API_INFO_SIZE]>::try_from(chunk)
.map(sp_api::deserialize_runtime_api_info)
.map_err(|_| {
WasmError::Other(format!(
"a clipped runtime api info declaration"
))
WasmError::Other("a clipped runtime api info declaration".to_owned())
})
})
.collect::<Result<Vec<_>, WasmError>>()
@@ -383,15 +381,15 @@ fn decode_runtime_apis(apis: &[u8]) -> Result<Vec<([u8; 8], u32)>, WasmError> {
pub fn read_embedded_version(
blob: &RuntimeBlob,
) -> Result<Option<RuntimeVersion>, WasmError> {
if let Some(version_section) = blob.custom_section_contents("runtime_version") {
if let Some(mut version_section) = blob.custom_section_contents("runtime_version") {
// We do not use `decode_version` here because the runtime_version section is not supposed
// to ever contain a legacy version. Apart from that `decode_version` relies on presence
// of a special API in the `apis` field to treat the input as a non-legacy version. However
// the structure found in the `runtime_version` always contain an empty `apis` field. Therefore
// the version read will be mistakingly treated as an legacy one.
let mut decoded_version = sp_api::RuntimeVersion::decode(&mut &version_section[..])
// the version read will be mistakenly treated as an legacy one.
let mut decoded_version = sp_api::RuntimeVersion::decode(&mut version_section)
.map_err(|_|
WasmError::Instantiation("failed to decode verison section".into())
WasmError::Instantiation("failed to decode version section".into())
)?;
// Don't stop on this and check if there is a special section that encodes all runtime APIs.
+2 -2
View File
@@ -185,7 +185,7 @@ impl<'a> Sandbox for FunctionExecutor<'a> {
&mut self,
instance_id: u32,
export_name: &str,
args: &[u8],
mut args: &[u8],
return_val: Pointer<u8>,
return_val_len: WordSize,
state: u32,
@@ -193,7 +193,7 @@ impl<'a> Sandbox for FunctionExecutor<'a> {
trace!(target: "sp-sandbox", "invoke, instance_idx={}", instance_id);
// Deserialize arguments and convert them into wasmi types.
let args = Vec::<sp_wasm_interface::Value>::decode(&mut &args[..])
let args = Vec::<sp_wasm_interface::Value>::decode(&mut args)
.map_err(|_| "Can't decode serialized arguments for the invocation")?
.into_iter()
.map(Into::into)