mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 07:37:57 +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:
@@ -22,6 +22,6 @@
|
||||
#![deny(unused_crate_dependencies)]
|
||||
|
||||
pub mod error;
|
||||
pub mod runtime_blob;
|
||||
pub mod sandbox;
|
||||
pub mod wasm_runtime;
|
||||
pub mod runtime_blob;
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::error::{self, Error};
|
||||
use super::RuntimeBlob;
|
||||
use std::mem;
|
||||
use crate::error::{self, Error};
|
||||
use pwasm_utils::parity_wasm::elements::Instruction;
|
||||
use std::mem;
|
||||
|
||||
/// This is a snapshot of data segments specialzied for a particular instantiation.
|
||||
///
|
||||
@@ -49,7 +49,7 @@ impl DataSegmentsSnapshot {
|
||||
|
||||
// [op, End]
|
||||
if init_expr.len() != 2 {
|
||||
return Err(Error::InitializerHasTooManyExpressions);
|
||||
return Err(Error::InitializerHasTooManyExpressions)
|
||||
}
|
||||
let offset = match &init_expr[0] {
|
||||
Instruction::I32Const(v) => *v as u32,
|
||||
@@ -60,8 +60,8 @@ impl DataSegmentsSnapshot {
|
||||
// At the moment of writing the Substrate Runtime Interface does not provide
|
||||
// any globals. There is nothing that prevents us from supporting this
|
||||
// if/when we gain those.
|
||||
return Err(Error::ImportedGlobalsUnsupported);
|
||||
}
|
||||
return Err(Error::ImportedGlobalsUnsupported)
|
||||
},
|
||||
insn => return Err(Error::InvalidInitializerExpression(format!("{:?}", insn))),
|
||||
};
|
||||
|
||||
|
||||
@@ -50,17 +50,14 @@ pub trait InstanceGlobals {
|
||||
/// a runtime blob that was instrumented by
|
||||
/// [`RuntimeBlob::expose_mutable_globals`](super::RuntimeBlob::expose_mutable_globals`).
|
||||
|
||||
///
|
||||
/// If the code wasn't instrumented then it would be empty and snapshot would do nothing.
|
||||
pub struct ExposedMutableGlobalsSet(Vec<String>);
|
||||
|
||||
impl ExposedMutableGlobalsSet {
|
||||
/// Collect the set from the given runtime blob. See the struct documentation for details.
|
||||
pub fn collect(runtime_blob: &RuntimeBlob) -> Self {
|
||||
let global_names = runtime_blob
|
||||
.exported_internal_global_names()
|
||||
.map(ToOwned::to_owned)
|
||||
.collect();
|
||||
let global_names =
|
||||
runtime_blob.exported_internal_global_names().map(ToOwned::to_owned).collect();
|
||||
Self(global_names)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,5 +53,5 @@ mod globals_snapshot;
|
||||
mod runtime_blob;
|
||||
|
||||
pub use data_segments_snapshot::DataSegmentsSnapshot;
|
||||
pub use globals_snapshot::{GlobalsSnapshot, ExposedMutableGlobalsSet, InstanceGlobals};
|
||||
pub use globals_snapshot::{ExposedMutableGlobalsSet, GlobalsSnapshot, InstanceGlobals};
|
||||
pub use runtime_blob::RuntimeBlob;
|
||||
|
||||
@@ -16,13 +16,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use pwasm_utils::{
|
||||
parity_wasm::elements::{
|
||||
DataSegment, Module, deserialize_buffer, serialize, Internal,
|
||||
},
|
||||
export_mutable_globals,
|
||||
};
|
||||
use crate::error::WasmError;
|
||||
use pwasm_utils::{
|
||||
export_mutable_globals,
|
||||
parity_wasm::elements::{deserialize_buffer, serialize, DataSegment, Internal, Module},
|
||||
};
|
||||
|
||||
/// A bunch of information collected from a WebAssembly module.
|
||||
#[derive(Clone)]
|
||||
@@ -53,11 +51,7 @@ impl RuntimeBlob {
|
||||
|
||||
/// Extract the data segments from the given wasm code.
|
||||
pub(super) fn data_segments(&self) -> Vec<DataSegment> {
|
||||
self.raw_module
|
||||
.data_section()
|
||||
.map(|ds| ds.entries())
|
||||
.unwrap_or(&[])
|
||||
.to_vec()
|
||||
self.raw_module.data_section().map(|ds| ds.entries()).unwrap_or(&[]).to_vec()
|
||||
}
|
||||
|
||||
/// The number of globals defined in locally in this module.
|
||||
@@ -70,10 +64,7 @@ impl RuntimeBlob {
|
||||
|
||||
/// The number of imports of globals.
|
||||
pub fn imported_globals_count(&self) -> u32 {
|
||||
self.raw_module
|
||||
.import_section()
|
||||
.map(|is| is.globals() as u32)
|
||||
.unwrap_or(0)
|
||||
self.raw_module.import_section().map(|is| is.globals() as u32).unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Perform an instrumentation that makes sure that the mutable globals are exported.
|
||||
@@ -95,35 +86,29 @@ impl RuntimeBlob {
|
||||
|e| WasmError::Other(format!("cannot inject the stack limiter: {:?}", e)),
|
||||
)?;
|
||||
|
||||
Ok(Self {
|
||||
raw_module: injected_module,
|
||||
})
|
||||
Ok(Self { raw_module: injected_module })
|
||||
}
|
||||
|
||||
/// Perform an instrumentation that makes sure that a specific function `entry_point` is exported
|
||||
pub fn entry_point_exists(&self, entry_point: &str) -> bool {
|
||||
self.raw_module.export_section().map(|e| {
|
||||
e.entries()
|
||||
.iter()
|
||||
.any(|e| matches!(e.internal(), Internal::Function(_)) && e.field() == entry_point)
|
||||
}).unwrap_or_default()
|
||||
self.raw_module
|
||||
.export_section()
|
||||
.map(|e| {
|
||||
e.entries().iter().any(|e| {
|
||||
matches!(e.internal(), Internal::Function(_)) && e.field() == entry_point
|
||||
})
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Returns an iterator of all globals which were exported by [`expose_mutable_globals`].
|
||||
pub(super) fn exported_internal_global_names<'module>(
|
||||
&'module self,
|
||||
) -> impl Iterator<Item = &'module str> {
|
||||
let exports = self
|
||||
.raw_module
|
||||
.export_section()
|
||||
.map(|es| es.entries())
|
||||
.unwrap_or(&[]);
|
||||
let exports = self.raw_module.export_section().map(|es| es.entries()).unwrap_or(&[]);
|
||||
exports.iter().filter_map(|export| match export.internal() {
|
||||
Internal::Global(_)
|
||||
if export.field().starts_with("exported_internal_global") =>
|
||||
{
|
||||
Some(export.field())
|
||||
}
|
||||
Internal::Global(_) if export.field().starts_with("exported_internal_global") =>
|
||||
Some(export.field()),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
@@ -135,12 +120,11 @@ impl RuntimeBlob {
|
||||
.custom_sections()
|
||||
.find(|cs| cs.name() == section_name)
|
||||
.map(|cs| cs.payload())
|
||||
}
|
||||
}
|
||||
|
||||
/// Consumes this runtime blob and serializes it.
|
||||
pub fn serialize(self) -> Vec<u8> {
|
||||
serialize(self.raw_module)
|
||||
.expect("serializing into a vec should succeed; qed")
|
||||
serialize(self.raw_module).expect("serializing into a vec should succeed; qed")
|
||||
}
|
||||
|
||||
/// Destructure this structure into the underlying parity-wasm Module.
|
||||
|
||||
@@ -21,15 +21,15 @@
|
||||
//! Sandboxing is baked by wasmi at the moment. In future, however, we would like to add/switch to
|
||||
//! a compiled execution engine.
|
||||
|
||||
use crate::error::{Result, Error};
|
||||
use std::{collections::HashMap, rc::Rc};
|
||||
use crate::error::{Error, Result};
|
||||
use codec::{Decode, Encode};
|
||||
use sp_core::sandbox as sandbox_primitives;
|
||||
use wasmi::{
|
||||
Externals, ImportResolver, MemoryInstance, MemoryRef, Module, ModuleInstance,
|
||||
ModuleRef, RuntimeArgs, RuntimeValue, Trap, TrapKind, memory_units::Pages,
|
||||
};
|
||||
use sp_wasm_interface::{FunctionContext, Pointer, WordSize};
|
||||
use std::{collections::HashMap, rc::Rc};
|
||||
use wasmi::{
|
||||
memory_units::Pages, Externals, ImportResolver, MemoryInstance, MemoryRef, Module,
|
||||
ModuleInstance, ModuleRef, RuntimeArgs, RuntimeValue, Trap, TrapKind,
|
||||
};
|
||||
|
||||
/// Index of a function inside the supervisor.
|
||||
///
|
||||
@@ -83,15 +83,9 @@ impl ImportResolver for Imports {
|
||||
field_name: &str,
|
||||
signature: &::wasmi::Signature,
|
||||
) -> std::result::Result<wasmi::FuncRef, wasmi::Error> {
|
||||
let key = (
|
||||
module_name.as_bytes().to_owned(),
|
||||
field_name.as_bytes().to_owned(),
|
||||
);
|
||||
let key = (module_name.as_bytes().to_owned(), field_name.as_bytes().to_owned());
|
||||
let idx = *self.func_map.get(&key).ok_or_else(|| {
|
||||
wasmi::Error::Instantiation(format!(
|
||||
"Export {}:{} not found",
|
||||
module_name, field_name
|
||||
))
|
||||
wasmi::Error::Instantiation(format!("Export {}:{} not found", module_name, field_name))
|
||||
})?;
|
||||
Ok(wasmi::FuncInstance::alloc_host(signature.clone(), idx.0))
|
||||
}
|
||||
@@ -102,11 +96,9 @@ impl ImportResolver for Imports {
|
||||
field_name: &str,
|
||||
_memory_type: &::wasmi::MemoryDescriptor,
|
||||
) -> std::result::Result<MemoryRef, wasmi::Error> {
|
||||
let key = (
|
||||
module_name.as_bytes().to_vec(),
|
||||
field_name.as_bytes().to_vec(),
|
||||
);
|
||||
let mem = self.memories_map
|
||||
let key = (module_name.as_bytes().to_vec(), field_name.as_bytes().to_vec());
|
||||
let mem = self
|
||||
.memories_map
|
||||
.get(&key)
|
||||
.ok_or_else(|| {
|
||||
wasmi::Error::Instantiation(format!(
|
||||
@@ -124,10 +116,7 @@ impl ImportResolver for Imports {
|
||||
field_name: &str,
|
||||
_global_type: &::wasmi::GlobalDescriptor,
|
||||
) -> std::result::Result<wasmi::GlobalRef, wasmi::Error> {
|
||||
Err(wasmi::Error::Instantiation(format!(
|
||||
"Export {}:{} not found",
|
||||
module_name, field_name
|
||||
)))
|
||||
Err(wasmi::Error::Instantiation(format!("Export {}:{} not found", module_name, field_name)))
|
||||
}
|
||||
|
||||
fn resolve_table(
|
||||
@@ -136,10 +125,7 @@ impl ImportResolver for Imports {
|
||||
field_name: &str,
|
||||
_table_type: &::wasmi::TableDescriptor,
|
||||
) -> std::result::Result<wasmi::TableRef, wasmi::Error> {
|
||||
Err(wasmi::Error::Instantiation(format!(
|
||||
"Export {}:{} not found",
|
||||
module_name, field_name
|
||||
)))
|
||||
Err(wasmi::Error::Instantiation(format!("Export {}:{} not found", module_name, field_name)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,7 +173,9 @@ fn trap(msg: &'static str) -> Trap {
|
||||
TrapKind::Host(Box::new(Error::Other(msg.into()))).into()
|
||||
}
|
||||
|
||||
fn deserialize_result(mut 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)
|
||||
@@ -222,7 +210,8 @@ impl<'a, FE: SandboxCapabilities + 'a> Externals for GuestExternals<'a, FE> {
|
||||
);
|
||||
|
||||
// Serialize arguments into a byte vector.
|
||||
let invoke_args_data: Vec<u8> = args.as_ref()
|
||||
let invoke_args_data: Vec<u8> = args
|
||||
.as_ref()
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(sp_wasm_interface::Value::from)
|
||||
@@ -240,10 +229,7 @@ impl<'a, FE: SandboxCapabilities + 'a> Externals for GuestExternals<'a, FE> {
|
||||
.map_err(|_| trap("Can't allocate memory in supervisor for the arguments"))?;
|
||||
|
||||
let deallocate = |this: &mut GuestExternals<FE>, ptr, fail_msg| {
|
||||
this
|
||||
.supervisor_externals
|
||||
.deallocate_memory(ptr)
|
||||
.map_err(|_| trap(fail_msg))
|
||||
this.supervisor_externals.deallocate_memory(ptr).map_err(|_| trap(fail_msg))
|
||||
};
|
||||
|
||||
if self
|
||||
@@ -251,8 +237,12 @@ impl<'a, FE: SandboxCapabilities + 'a> Externals for GuestExternals<'a, FE> {
|
||||
.write_memory(invoke_args_ptr, &invoke_args_data)
|
||||
.is_err()
|
||||
{
|
||||
deallocate(self, invoke_args_ptr, "Failed dealloction after failed write of invoke arguments")?;
|
||||
return Err(trap("Can't write invoke args into memory"));
|
||||
deallocate(
|
||||
self,
|
||||
invoke_args_ptr,
|
||||
"Failed dealloction after failed write of invoke arguments",
|
||||
)?;
|
||||
return Err(trap("Can't write invoke args into memory"))
|
||||
}
|
||||
|
||||
let result = self.supervisor_externals.invoke(
|
||||
@@ -263,7 +253,11 @@ impl<'a, FE: SandboxCapabilities + 'a> Externals for GuestExternals<'a, FE> {
|
||||
func_idx,
|
||||
);
|
||||
|
||||
deallocate(self, invoke_args_ptr, "Can't deallocate memory for dispatch thunk's invoke arguments")?;
|
||||
deallocate(
|
||||
self,
|
||||
invoke_args_ptr,
|
||||
"Can't deallocate memory for dispatch thunk's invoke arguments",
|
||||
)?;
|
||||
let result = result?;
|
||||
|
||||
// dispatch_thunk returns pointer to serialized arguments.
|
||||
@@ -276,13 +270,18 @@ impl<'a, FE: SandboxCapabilities + 'a> Externals for GuestExternals<'a, FE> {
|
||||
(Pointer::new(ptr), len)
|
||||
};
|
||||
|
||||
let serialized_result_val = self.supervisor_externals
|
||||
let serialized_result_val = self
|
||||
.supervisor_externals
|
||||
.read_memory(serialized_result_val_ptr, serialized_result_val_len)
|
||||
.map_err(|_| trap("Can't read the serialized result from dispatch thunk"));
|
||||
|
||||
deallocate(self, serialized_result_val_ptr, "Can't deallocate memory for dispatch thunk's result")
|
||||
.and_then(|_| serialized_result_val)
|
||||
.and_then(|serialized_result_val| deserialize_result(&serialized_result_val))
|
||||
deallocate(
|
||||
self,
|
||||
serialized_result_val_ptr,
|
||||
"Can't deallocate memory for dispatch thunk's result",
|
||||
)
|
||||
.and_then(|_| serialized_result_val)
|
||||
.and_then(|serialized_result_val| deserialize_result(&serialized_result_val))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,11 +295,7 @@ where
|
||||
FE: SandboxCapabilities,
|
||||
F: FnOnce(&mut GuestExternals<FE>) -> R,
|
||||
{
|
||||
let mut guest_externals = GuestExternals {
|
||||
supervisor_externals,
|
||||
sandbox_instance,
|
||||
state,
|
||||
};
|
||||
let mut guest_externals = GuestExternals { supervisor_externals, sandbox_instance, state };
|
||||
f(&mut guest_externals)
|
||||
}
|
||||
|
||||
@@ -332,32 +327,23 @@ impl<FR> SandboxInstance<FR> {
|
||||
///
|
||||
/// The `state` parameter can be used to provide custom data for
|
||||
/// these syscall implementations.
|
||||
pub fn invoke<FE: SandboxCapabilities<SupervisorFuncRef=FR>>(
|
||||
pub fn invoke<FE: SandboxCapabilities<SupervisorFuncRef = FR>>(
|
||||
&self,
|
||||
export_name: &str,
|
||||
args: &[RuntimeValue],
|
||||
supervisor_externals: &mut FE,
|
||||
state: u32,
|
||||
) -> std::result::Result<Option<wasmi::RuntimeValue>, wasmi::Error> {
|
||||
with_guest_externals(
|
||||
supervisor_externals,
|
||||
self,
|
||||
state,
|
||||
|guest_externals| {
|
||||
self.instance
|
||||
.invoke_export(export_name, args, guest_externals)
|
||||
},
|
||||
)
|
||||
with_guest_externals(supervisor_externals, self, state, |guest_externals| {
|
||||
self.instance.invoke_export(export_name, args, guest_externals)
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the value from a global with the given `name`.
|
||||
///
|
||||
/// Returns `Some(_)` if the global could be found.
|
||||
pub fn get_global_val(&self, name: &str) -> Option<sp_wasm_interface::Value> {
|
||||
let global = self.instance
|
||||
.export_by_name(name)?
|
||||
.as_global()?
|
||||
.get();
|
||||
let global = self.instance.export_by_name(name)?.as_global()?.get();
|
||||
|
||||
Some(global.into())
|
||||
}
|
||||
@@ -398,7 +384,7 @@ fn decode_environment_definition(
|
||||
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) => {
|
||||
let memory_ref = memories
|
||||
.get(memory_idx as usize)
|
||||
@@ -406,17 +392,11 @@ fn decode_environment_definition(
|
||||
.ok_or_else(|| InstantiationError::EnvironmentDefinitionCorrupted)?
|
||||
.ok_or_else(|| InstantiationError::EnvironmentDefinitionCorrupted)?;
|
||||
memories_map.insert((module, field), memory_ref);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Ok((
|
||||
Imports {
|
||||
func_map,
|
||||
memories_map,
|
||||
},
|
||||
guest_to_supervisor_mapping,
|
||||
))
|
||||
Ok((Imports { func_map, memories_map }, guest_to_supervisor_mapping))
|
||||
}
|
||||
|
||||
/// An environment in which the guest module is instantiated.
|
||||
@@ -435,10 +415,7 @@ impl GuestEnvironment {
|
||||
) -> std::result::Result<Self, InstantiationError> {
|
||||
let (imports, guest_to_supervisor_mapping) =
|
||||
decode_environment_definition(raw_env_def, &store.memories)?;
|
||||
Ok(Self {
|
||||
imports,
|
||||
guest_to_supervisor_mapping,
|
||||
})
|
||||
Ok(Self { imports, guest_to_supervisor_mapping })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -493,16 +470,11 @@ pub fn instantiate<'a, FE: SandboxCapabilities>(
|
||||
guest_to_supervisor_mapping: host_env.guest_to_supervisor_mapping,
|
||||
});
|
||||
|
||||
with_guest_externals(
|
||||
supervisor_externals,
|
||||
&sandbox_instance,
|
||||
state,
|
||||
|guest_externals| {
|
||||
instance
|
||||
.run_start(guest_externals)
|
||||
.map_err(|_| InstantiationError::StartTrapped)
|
||||
},
|
||||
)?;
|
||||
with_guest_externals(supervisor_externals, &sandbox_instance, state, |guest_externals| {
|
||||
instance
|
||||
.run_start(guest_externals)
|
||||
.map_err(|_| InstantiationError::StartTrapped)
|
||||
})?;
|
||||
|
||||
Ok(UnregisteredInstance { sandbox_instance })
|
||||
}
|
||||
@@ -519,10 +491,7 @@ pub struct Store<FR> {
|
||||
impl<FR> Store<FR> {
|
||||
/// Create a new empty sandbox store.
|
||||
pub fn new() -> Self {
|
||||
Store {
|
||||
instances: Vec::new(),
|
||||
memories: Vec::new(),
|
||||
}
|
||||
Store { instances: Vec::new(), memories: Vec::new() }
|
||||
}
|
||||
|
||||
/// Create a new memory instance and return it's index.
|
||||
@@ -537,11 +506,7 @@ impl<FR> Store<FR> {
|
||||
specified_limit => Some(Pages(specified_limit as usize)),
|
||||
};
|
||||
|
||||
let mem =
|
||||
MemoryInstance::alloc(
|
||||
Pages(initial as usize),
|
||||
maximum,
|
||||
)?;
|
||||
let mem = MemoryInstance::alloc(Pages(initial as usize), maximum)?;
|
||||
|
||||
let mem_idx = self.memories.len();
|
||||
self.memories.push(Some(mem));
|
||||
@@ -589,7 +554,7 @@ impl<FR> Store<FR> {
|
||||
Some(memory) => {
|
||||
*memory = None;
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -606,7 +571,7 @@ impl<FR> Store<FR> {
|
||||
Some(instance) => {
|
||||
*instance = None;
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user