mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 20:57:59 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -18,25 +18,26 @@
|
||||
|
||||
//! This crate provides an implementation of `WasmModule` that is baked by wasmi.
|
||||
|
||||
use std::{str, cell::RefCell, sync::Arc};
|
||||
use wasmi::{
|
||||
Module, ModuleInstance, MemoryInstance, MemoryRef, TableRef, ImportsBuilder, ModuleRef,
|
||||
FuncInstance, memory_units::Pages,
|
||||
RuntimeValue::{I32, I64, self},
|
||||
};
|
||||
use codec::{Encode, Decode};
|
||||
use sp_core::sandbox as sandbox_primitives;
|
||||
use log::{error, trace, debug};
|
||||
use sp_wasm_interface::{
|
||||
FunctionContext, Pointer, WordSize, Sandbox, MemoryId, Result as WResult, Function,
|
||||
};
|
||||
use sp_runtime_interface::unpack_ptr_and_len;
|
||||
use sc_executor_common::wasm_runtime::{WasmModule, WasmInstance, InvokeMethod};
|
||||
use codec::{Decode, Encode};
|
||||
use log::{debug, error, trace};
|
||||
use sc_executor_common::{
|
||||
error::{Error, WasmError},
|
||||
runtime_blob::{DataSegmentsSnapshot, RuntimeBlob},
|
||||
sandbox,
|
||||
wasm_runtime::{InvokeMethod, WasmInstance, WasmModule},
|
||||
};
|
||||
use sp_core::sandbox as sandbox_primitives;
|
||||
use sp_runtime_interface::unpack_ptr_and_len;
|
||||
use sp_wasm_interface::{
|
||||
Function, FunctionContext, MemoryId, Pointer, Result as WResult, Sandbox, WordSize,
|
||||
};
|
||||
use std::{cell::RefCell, str, sync::Arc};
|
||||
use wasmi::{
|
||||
memory_units::Pages,
|
||||
FuncInstance, ImportsBuilder, MemoryInstance, MemoryRef, Module, ModuleInstance, ModuleRef,
|
||||
RuntimeValue::{self, I32, I64},
|
||||
TableRef,
|
||||
};
|
||||
use sc_executor_common::runtime_blob::{RuntimeBlob, DataSegmentsSnapshot};
|
||||
|
||||
struct FunctionExecutor<'a> {
|
||||
sandbox_store: sandbox::Store<wasmi::FuncRef>,
|
||||
@@ -109,16 +110,14 @@ impl<'a> FunctionContext for FunctionExecutor<'a> {
|
||||
|
||||
fn allocate_memory(&mut self, size: WordSize) -> WResult<Pointer<u8>> {
|
||||
let heap = &mut self.heap;
|
||||
self.memory.with_direct_access_mut(|mem| {
|
||||
heap.allocate(mem, size).map_err(|e| e.to_string())
|
||||
})
|
||||
self.memory
|
||||
.with_direct_access_mut(|mem| heap.allocate(mem, size).map_err(|e| e.to_string()))
|
||||
}
|
||||
|
||||
fn deallocate_memory(&mut self, ptr: Pointer<u8>) -> WResult<()> {
|
||||
let heap = &mut self.heap;
|
||||
self.memory.with_direct_access_mut(|mem| {
|
||||
heap.deallocate(mem, ptr).map_err(|e| e.to_string())
|
||||
})
|
||||
self.memory
|
||||
.with_direct_access_mut(|mem| heap.deallocate(mem, ptr).map_err(|e| e.to_string()))
|
||||
}
|
||||
|
||||
fn sandbox(&mut self) -> &mut dyn Sandbox {
|
||||
@@ -173,11 +172,7 @@ impl<'a> Sandbox for FunctionExecutor<'a> {
|
||||
self.sandbox_store.memory_teardown(memory_id).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn memory_new(
|
||||
&mut self,
|
||||
initial: u32,
|
||||
maximum: u32,
|
||||
) -> WResult<MemoryId> {
|
||||
fn memory_new(&mut self, initial: u32, maximum: u32) -> WResult<MemoryId> {
|
||||
self.sandbox_store.new_memory(initial, maximum).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -213,7 +208,7 @@ impl<'a> Sandbox for FunctionExecutor<'a> {
|
||||
self.write_memory(return_val, val).map_err(|_| "Return value buffer is OOB")?;
|
||||
Ok(sandbox_primitives::ERR_OK)
|
||||
})
|
||||
}
|
||||
},
|
||||
Err(_) => Ok(sandbox_primitives::ERR_EXECUTION),
|
||||
}
|
||||
}
|
||||
@@ -231,9 +226,12 @@ impl<'a> Sandbox for FunctionExecutor<'a> {
|
||||
) -> WResult<u32> {
|
||||
// Extract a dispatch thunk from instance's table by the specified index.
|
||||
let dispatch_thunk = {
|
||||
let table = self.table.as_ref()
|
||||
let table = self
|
||||
.table
|
||||
.as_ref()
|
||||
.ok_or_else(|| "Runtime doesn't have a table; sandbox is unavailable")?;
|
||||
table.get(dispatch_thunk_id)
|
||||
table
|
||||
.get(dispatch_thunk_id)
|
||||
.map_err(|_| "dispatch_thunk_idx is out of the table bounds")?
|
||||
.ok_or_else(|| "dispatch_thunk_idx points on an empty table entry")?
|
||||
};
|
||||
@@ -248,8 +246,7 @@ impl<'a> Sandbox for FunctionExecutor<'a> {
|
||||
.map(|i| i.register(&mut self.sandbox_store))
|
||||
{
|
||||
Ok(instance_idx) => instance_idx,
|
||||
Err(sandbox::InstantiationError::StartTrapped) =>
|
||||
sandbox_primitives::ERR_EXECUTION,
|
||||
Err(sandbox::InstantiationError::StartTrapped) => sandbox_primitives::ERR_EXECUTION,
|
||||
Err(_) => sandbox_primitives::ERR_MODULE,
|
||||
};
|
||||
|
||||
@@ -288,7 +285,7 @@ struct Resolver<'a> {
|
||||
|
||||
impl<'a> Resolver<'a> {
|
||||
fn new(
|
||||
host_functions: &'a[&'static dyn Function],
|
||||
host_functions: &'a [&'static dyn Function],
|
||||
allow_missing_func_imports: bool,
|
||||
heap_pages: usize,
|
||||
) -> Resolver<'a> {
|
||||
@@ -303,25 +300,23 @@ impl<'a> Resolver<'a> {
|
||||
}
|
||||
|
||||
impl<'a> wasmi::ModuleImportResolver for Resolver<'a> {
|
||||
fn resolve_func(&self, name: &str, signature: &wasmi::Signature)
|
||||
-> std::result::Result<wasmi::FuncRef, wasmi::Error>
|
||||
{
|
||||
fn resolve_func(
|
||||
&self,
|
||||
name: &str,
|
||||
signature: &wasmi::Signature,
|
||||
) -> std::result::Result<wasmi::FuncRef, wasmi::Error> {
|
||||
let signature = sp_wasm_interface::Signature::from(signature);
|
||||
for (function_index, function) in self.host_functions.iter().enumerate() {
|
||||
if name == function.name() {
|
||||
if signature == function.signature() {
|
||||
return Ok(
|
||||
wasmi::FuncInstance::alloc_host(signature.into(), function_index),
|
||||
)
|
||||
return Ok(wasmi::FuncInstance::alloc_host(signature.into(), function_index))
|
||||
} else {
|
||||
return Err(wasmi::Error::Instantiation(
|
||||
format!(
|
||||
"Invalid signature for function `{}` expected `{:?}`, got `{:?}`",
|
||||
function.name(),
|
||||
signature,
|
||||
function.signature(),
|
||||
),
|
||||
))
|
||||
return Err(wasmi::Error::Instantiation(format!(
|
||||
"Invalid signature for function `{}` expected `{:?}`, got `{:?}`",
|
||||
function.name(),
|
||||
signature,
|
||||
function.signature(),
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -333,9 +328,7 @@ impl<'a> wasmi::ModuleImportResolver for Resolver<'a> {
|
||||
|
||||
Ok(wasmi::FuncInstance::alloc_host(signature.into(), id))
|
||||
} else {
|
||||
Err(wasmi::Error::Instantiation(
|
||||
format!("Export {} not found", name),
|
||||
))
|
||||
Err(wasmi::Error::Instantiation(format!("Export {} not found", name)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,15 +339,14 @@ impl<'a> wasmi::ModuleImportResolver for Resolver<'a> {
|
||||
) -> Result<MemoryRef, wasmi::Error> {
|
||||
if field_name == "memory" {
|
||||
match &mut *self.import_memory.borrow_mut() {
|
||||
Some(_) => Err(wasmi::Error::Instantiation(
|
||||
"Memory can not be imported twice!".into(),
|
||||
)),
|
||||
Some(_) =>
|
||||
Err(wasmi::Error::Instantiation("Memory can not be imported twice!".into())),
|
||||
memory_ref @ None => {
|
||||
if memory_type
|
||||
.maximum()
|
||||
.map(|m| m.saturating_sub(memory_type.initial()))
|
||||
.map(|m| self.heap_pages > m as usize)
|
||||
.unwrap_or(false)
|
||||
.maximum()
|
||||
.map(|m| m.saturating_sub(memory_type.initial()))
|
||||
.map(|m| self.heap_pages > m as usize)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
Err(wasmi::Error::Instantiation(format!(
|
||||
"Heap pages ({}) is greater than imported memory maximum ({}).",
|
||||
@@ -372,35 +364,40 @@ impl<'a> wasmi::ModuleImportResolver for Resolver<'a> {
|
||||
*memory_ref = Some(memory.clone());
|
||||
Ok(memory)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
} else {
|
||||
Err(wasmi::Error::Instantiation(
|
||||
format!("Unknown memory reference with name: {}", field_name),
|
||||
))
|
||||
Err(wasmi::Error::Instantiation(format!(
|
||||
"Unknown memory reference with name: {}",
|
||||
field_name
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> wasmi::Externals for FunctionExecutor<'a> {
|
||||
fn invoke_index(&mut self, index: usize, args: wasmi::RuntimeArgs)
|
||||
-> Result<Option<wasmi::RuntimeValue>, wasmi::Trap>
|
||||
{
|
||||
fn invoke_index(
|
||||
&mut self,
|
||||
index: usize,
|
||||
args: wasmi::RuntimeArgs,
|
||||
) -> Result<Option<wasmi::RuntimeValue>, wasmi::Trap> {
|
||||
let mut args = args.as_ref().iter().copied().map(Into::into);
|
||||
|
||||
if let Some(function) = self.host_functions.get(index) {
|
||||
function.execute(self, &mut args)
|
||||
function
|
||||
.execute(self, &mut args)
|
||||
.map_err(|msg| Error::FunctionExecution(function.name().to_string(), msg))
|
||||
.map_err(wasmi::Trap::from)
|
||||
.map(|v| v.map(Into::into))
|
||||
} else if self.allow_missing_func_imports
|
||||
&& index >= self.host_functions.len()
|
||||
&& index < self.host_functions.len() + self.missing_functions.len()
|
||||
} else if self.allow_missing_func_imports &&
|
||||
index >= self.host_functions.len() &&
|
||||
index < self.host_functions.len() + self.missing_functions.len()
|
||||
{
|
||||
Err(Error::from(format!(
|
||||
"Function `{}` is only a stub. Calling a stub is not allowed.",
|
||||
self.missing_functions[index - self.host_functions.len()],
|
||||
)).into())
|
||||
))
|
||||
.into())
|
||||
} else {
|
||||
Err(Error::from(format!("Could not find host function with index: {}", index)).into())
|
||||
}
|
||||
@@ -462,25 +459,26 @@ fn call_in_wasm_module(
|
||||
function_executor.write_memory(offset, data)?;
|
||||
|
||||
let result = match method {
|
||||
InvokeMethod::Export(method) => {
|
||||
module_instance.invoke_export(
|
||||
method,
|
||||
&[I32(u32::from(offset) as i32), I32(data.len() as i32)],
|
||||
&mut function_executor,
|
||||
)
|
||||
},
|
||||
InvokeMethod::Export(method) => module_instance.invoke_export(
|
||||
method,
|
||||
&[I32(u32::from(offset) as i32), I32(data.len() as i32)],
|
||||
&mut function_executor,
|
||||
),
|
||||
InvokeMethod::Table(func_ref) => {
|
||||
let func = table.ok_or(Error::NoTable)?
|
||||
let func = table
|
||||
.ok_or(Error::NoTable)?
|
||||
.get(func_ref)?
|
||||
.ok_or(Error::NoTableEntryWithIndex(func_ref))?;
|
||||
FuncInstance::invoke(
|
||||
&func,
|
||||
&[I32(u32::from(offset) as i32), I32(data.len() as i32)],
|
||||
&mut function_executor,
|
||||
).map_err(Into::into)
|
||||
)
|
||||
.map_err(Into::into)
|
||||
},
|
||||
InvokeMethod::TableWithWrapper { dispatcher_ref, func } => {
|
||||
let dispatcher = table.ok_or(Error::NoTable)?
|
||||
let dispatcher = table
|
||||
.ok_or(Error::NoTable)?
|
||||
.get(dispatcher_ref)?
|
||||
.ok_or(Error::NoTableEntryWithIndex(dispatcher_ref))?;
|
||||
|
||||
@@ -488,7 +486,8 @@ fn call_in_wasm_module(
|
||||
&dispatcher,
|
||||
&[I32(func as _), I32(u32::from(offset) as i32), I32(data.len() as i32)],
|
||||
&mut function_executor,
|
||||
).map_err(Into::into)
|
||||
)
|
||||
.map_err(Into::into)
|
||||
},
|
||||
};
|
||||
|
||||
@@ -518,15 +517,12 @@ fn instantiate_module(
|
||||
) -> Result<(ModuleRef, Vec<String>, MemoryRef), Error> {
|
||||
let resolver = Resolver::new(host_functions, allow_missing_func_imports, heap_pages);
|
||||
// start module instantiation. Don't run 'start' function yet.
|
||||
let intermediate_instance = ModuleInstance::new(
|
||||
module,
|
||||
&ImportsBuilder::new().with_resolver("env", &resolver),
|
||||
)?;
|
||||
let intermediate_instance =
|
||||
ModuleInstance::new(module, &ImportsBuilder::new().with_resolver("env", &resolver))?;
|
||||
|
||||
// Verify that the module has the heap base global variable.
|
||||
let _ = get_heap_base(intermediate_instance.not_started_instance())?;
|
||||
|
||||
|
||||
// Get the memory reference. Runtimes should import memory, but to be backwards
|
||||
// compatible we also support exported memory.
|
||||
let memory = match resolver.import_memory.into_inner() {
|
||||
@@ -541,7 +537,7 @@ fn instantiate_module(
|
||||
memory.grow(Pages(heap_pages)).map_err(|_| Error::Runtime)?;
|
||||
|
||||
memory
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
if intermediate_instance.has_start() {
|
||||
@@ -592,9 +588,7 @@ impl GlobalValsSnapshot {
|
||||
// the instance should be the same as used for preserving and
|
||||
// we iterate the same way it as we do it for preserving values that means that the
|
||||
// types should be the same and all the values are mutable. So no error is expected/
|
||||
global_ref
|
||||
.set(*global_val)
|
||||
.map_err(|_| WasmError::ApplySnapshotFailed)?;
|
||||
global_ref.set(*global_val).map_err(|_| WasmError::ApplySnapshotFailed)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -624,7 +618,8 @@ impl WasmModule for WasmiRuntime {
|
||||
&self.module,
|
||||
&self.host_functions,
|
||||
self.allow_missing_func_imports,
|
||||
).map_err(|e| WasmError::Instantiation(e.to_string()))?;
|
||||
)
|
||||
.map_err(|e| WasmError::Instantiation(e.to_string()))?;
|
||||
|
||||
Ok(Box::new(WasmiInstance {
|
||||
instance,
|
||||
@@ -646,11 +641,11 @@ pub fn create_runtime(
|
||||
host_functions: Vec<&'static dyn Function>,
|
||||
allow_missing_func_imports: bool,
|
||||
) -> Result<WasmiRuntime, WasmError> {
|
||||
let data_segments_snapshot = DataSegmentsSnapshot::take(&blob)
|
||||
.map_err(|e| WasmError::Other(e.to_string()))?;
|
||||
let data_segments_snapshot =
|
||||
DataSegmentsSnapshot::take(&blob).map_err(|e| WasmError::Other(e.to_string()))?;
|
||||
|
||||
let module = Module::from_parity_wasm_module(blob.into_inner())
|
||||
.map_err(|_| WasmError::InvalidModule)?;
|
||||
let module =
|
||||
Module::from_parity_wasm_module(blob.into_inner()).map_err(|_| WasmError::InvalidModule)?;
|
||||
|
||||
let global_vals_snapshot = {
|
||||
let (instance, _, _) = instantiate_module(
|
||||
@@ -734,7 +729,7 @@ impl WasmInstance for WasmiInstance {
|
||||
.as_global()
|
||||
.ok_or_else(|| format!("`{}` is not a global", name))?
|
||||
.get()
|
||||
.into()
|
||||
.into(),
|
||||
)),
|
||||
None => Ok(None),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user