diff --git a/Cargo.lock b/Cargo.lock index fb84a29f0329b..71ed4fbd188aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5142,6 +5142,7 @@ dependencies = [ "pallet-utility", "parity-scale-codec", "pretty_assertions", + "pwasm-utils", "rand 0.8.4", "rand_pcg 0.3.1", "scale-info", @@ -5152,7 +5153,6 @@ dependencies = [ "sp-runtime", "sp-sandbox", "sp-std", - "wasm-instrument", "wasmi-validation", "wat", ] @@ -6547,6 +6547,17 @@ dependencies = [ "cc", ] +[[package]] +name = "pwasm-utils" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "880b3384fb00b8f6ecccd5d358b93bd2201900ae3daad213791d1864f6441f5c" +dependencies = [ + "byteorder", + "log", + "parity-wasm 0.42.2", +] + [[package]] name = "quick-error" version = "1.2.3" @@ -7575,13 +7586,13 @@ version = "0.10.0-dev" dependencies = [ "environmental", "parity-scale-codec", + "pwasm-utils", "sc-allocator", "sp-core", "sp-maybe-compressed-blob", "sp-serializer", "sp-wasm-interface", "thiserror", - "wasm-instrument", "wasmer", "wasmer-compiler-singlepass", "wasmi", @@ -10620,15 +10631,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "wasm-instrument" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "962e5b0401bbb6c887f54e69b8c496ea36f704df65db73e81fd5ff8dc3e63a9f" -dependencies = [ - "parity-wasm 0.42.2", -] - [[package]] name = "wasm-timer" version = "0.2.5" diff --git a/client/executor/common/Cargo.toml b/client/executor/common/Cargo.toml index 104d24876d9a5..0bea6ecb4d7ba 100644 --- a/client/executor/common/Cargo.toml +++ b/client/executor/common/Cargo.toml @@ -14,7 +14,12 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -wasm-instrument = "0.1" +# <<<<<<< HEAD +# wasm-instrument = "0.1" +# ======= +# derive_more = "0.99.16" +pwasm-utils = "0.18.2" +# >>>>>>> parent of e2d1f18237 (Switch from `pwasm-utils` to `wasm-instrument` (#10680)) codec = { package = "parity-scale-codec", version = "2.0.0" } wasmi = "0.9.1" sp-core = { version = "4.1.0-dev", path = "../../../primitives/core" } diff --git a/client/executor/common/src/runtime_blob/data_segments_snapshot.rs b/client/executor/common/src/runtime_blob/data_segments_snapshot.rs index b44370e681b13..76f2ad2c345ee 100644 --- a/client/executor/common/src/runtime_blob/data_segments_snapshot.rs +++ b/client/executor/common/src/runtime_blob/data_segments_snapshot.rs @@ -18,8 +18,8 @@ use super::RuntimeBlob; use crate::error::{self, Error}; +use pwasm_utils::parity_wasm::elements::Instruction; use std::mem; -use wasm_instrument::parity_wasm::elements::Instruction; /// This is a snapshot of data segments specialzied for a particular instantiation. /// diff --git a/client/executor/common/src/runtime_blob/runtime_blob.rs b/client/executor/common/src/runtime_blob/runtime_blob.rs index d95dcda1a8779..8318cce7a9bad 100644 --- a/client/executor/common/src/runtime_blob/runtime_blob.rs +++ b/client/executor/common/src/runtime_blob/runtime_blob.rs @@ -17,7 +17,7 @@ // along with this program. If not, see . use crate::error::WasmError; -use wasm_instrument::{ +use pwasm_utils::{ export_mutable_globals, parity_wasm::elements::{deserialize_buffer, serialize, DataSegment, Internal, Module}, }; @@ -84,7 +84,7 @@ impl RuntimeBlob { /// depth of the wasm operand stack. pub fn inject_stack_depth_metering(self, stack_depth_limit: u32) -> Result { let injected_module = - wasm_instrument::inject_stack_limiter(self.raw_module, stack_depth_limit).map_err( + pwasm_utils::stack_height::inject_limiter(self.raw_module, stack_depth_limit).map_err( |e| WasmError::Other(format!("cannot inject the stack limiter: {:?}", e)), )?; diff --git a/frame/contracts/Cargo.toml b/frame/contracts/Cargo.toml index f0d2833c61fd2..85ead939fca82 100644 --- a/frame/contracts/Cargo.toml +++ b/frame/contracts/Cargo.toml @@ -20,7 +20,7 @@ codec = { package = "parity-scale-codec", version = "2.2.0", default-features = ] } scale-info = { version = "1.0", default-features = false, features = ["derive"] } log = { version = "0.4", default-features = false } -wasm-instrument = { version = "0.1", default-features = false } +pwasm-utils = { version = "0.18.2", default-features = false } serde = { version = "1", optional = true, features = ["derive"] } smallvec = { version = "1", default-features = false, features = [ "const_generics", @@ -71,7 +71,7 @@ std = [ "frame-benchmarking/std", "frame-support/std", "frame-system/std", - "wasm-instrument/std", + "pwasm-utils/std", "wasmi-validation/std", "pallet-contracts-primitives/std", "pallet-contracts-proc-macro/full", diff --git a/frame/contracts/src/benchmarking/code.rs b/frame/contracts/src/benchmarking/code.rs index 4d42349f82a1b..d4828871dbbe5 100644 --- a/frame/contracts/src/benchmarking/code.rs +++ b/frame/contracts/src/benchmarking/code.rs @@ -26,6 +26,13 @@ use crate::Config; use frame_support::traits::Get; +use pwasm_utils::parity_wasm::{ + builder, + elements::{ + self, BlockType, CustomSection, External, FuncBody, Instruction, Instructions, Module, + Section, ValueType, + }, +}; use sp_core::crypto::UncheckedFrom; use sp_runtime::traits::Hash; use sp_sandbox::{ @@ -33,13 +40,6 @@ use sp_sandbox::{ SandboxEnvironmentBuilder, SandboxMemory, }; use sp_std::{borrow::ToOwned, prelude::*}; -use wasm_instrument::parity_wasm::{ - builder, - elements::{ - self, BlockType, CustomSection, External, FuncBody, Instruction, Instructions, Module, - Section, ValueType, - }, -}; /// The location where to put the genrated code. pub enum Location { @@ -562,10 +562,10 @@ where fn inject_gas_metering(module: Module) -> Module { let schedule = T::Schedule::get(); let gas_rules = schedule.rules(&module); - wasm_instrument::gas_metering::inject(module, &gas_rules, "seal0").unwrap() + pwasm_utils::inject_gas_counter(module, &gas_rules, "seal0").unwrap() } fn inject_stack_metering(module: Module) -> Module { let height = T::Schedule::get().limits.stack_height; - wasm_instrument::inject_stack_limiter(module, height).unwrap() + pwasm_utils::stack_height::inject_limiter(module, height).unwrap() } diff --git a/frame/contracts/src/benchmarking/mod.rs b/frame/contracts/src/benchmarking/mod.rs index 3537af3d0cf78..f83d15ae4752e 100644 --- a/frame/contracts/src/benchmarking/mod.rs +++ b/frame/contracts/src/benchmarking/mod.rs @@ -39,12 +39,12 @@ use codec::{Encode, MaxEncodedLen}; use frame_benchmarking::{account, benchmarks, whitelisted_caller}; use frame_support::weights::Weight; use frame_system::RawOrigin; +use pwasm_utils::parity_wasm::elements::{BlockType, BrTableData, Instruction, ValueType}; use sp_runtime::{ traits::{Bounded, Hash}, Perbill, }; use sp_std::prelude::*; -use wasm_instrument::parity_wasm::elements::{BlockType, BrTableData, Instruction, ValueType}; /// How many batches we do per API benchmark. const API_BENCHMARK_BATCHES: u32 = 20; diff --git a/frame/contracts/src/schedule.rs b/frame/contracts/src/schedule.rs index f8ca182aea586..a92c0b48424d9 100644 --- a/frame/contracts/src/schedule.rs +++ b/frame/contracts/src/schedule.rs @@ -23,12 +23,12 @@ use crate::{weights::WeightInfo, Config}; use codec::{Decode, Encode}; use frame_support::{weights::Weight, DefaultNoBound}; use pallet_contracts_proc_macro::{ScheduleDebug, WeightDebug}; +use pwasm_utils::{parity_wasm::elements, rules}; use scale_info::TypeInfo; #[cfg(feature = "std")] use serde::{Deserialize, Serialize}; use sp_runtime::RuntimeDebug; use sp_std::{marker::PhantomData, vec::Vec}; -use wasm_instrument::{gas_metering, parity_wasm::elements}; /// How many API calls are executed in a single batch. The reason for increasing the amount /// of API calls in batches (per benchmark component increase) is so that the linear regression @@ -664,7 +664,7 @@ struct ScheduleRules<'a, T: Config> { } impl Schedule { - pub(crate) fn rules(&self, module: &elements::Module) -> impl gas_metering::Rules + '_ { + pub(crate) fn rules(&self, module: &elements::Module) -> impl rules::Rules + '_ { ScheduleRules { schedule: &self, params: module @@ -680,7 +680,7 @@ impl Schedule { } } -impl<'a, T: Config> gas_metering::Rules for ScheduleRules<'a, T> { +impl<'a, T: Config> rules::Rules for ScheduleRules<'a, T> { fn instruction_cost(&self, instruction: &elements::Instruction) -> Option { use self::elements::Instruction::*; let w = &self.schedule.instruction_weights; @@ -764,10 +764,10 @@ impl<'a, T: Config> gas_metering::Rules for ScheduleRules<'a, T> { Some(weight) } - fn memory_grow_cost(&self) -> gas_metering::MemoryGrowCost { + fn memory_grow_cost(&self) -> Option { // We benchmarked the memory.grow instruction with the maximum allowed pages. // The cost for growing is therefore already included in the instruction cost. - gas_metering::MemoryGrowCost::Free + None } } diff --git a/frame/contracts/src/wasm/env_def/macros.rs b/frame/contracts/src/wasm/env_def/macros.rs index aa5a1626681f4..a50290085df8b 100644 --- a/frame/contracts/src/wasm/env_def/macros.rs +++ b/frame/contracts/src/wasm/env_def/macros.rs @@ -28,7 +28,7 @@ macro_rules! convert_args { macro_rules! gen_signature { ( ( $( $params: ty ),* ) ) => ( { - wasm_instrument::parity_wasm::elements::FunctionType::new( + pwasm_utils::parity_wasm::elements::FunctionType::new( convert_args!($($params),*), vec![], ) } @@ -36,7 +36,7 @@ macro_rules! gen_signature { ( ( $( $params: ty ),* ) -> $returns: ty ) => ( { - wasm_instrument::parity_wasm::elements::FunctionType::new( + pwasm_utils::parity_wasm::elements::FunctionType::new( convert_args!($($params),*), vec![{use $crate::wasm::env_def::ConvertibleToWasm; <$returns>::VALUE_TYPE}], ) @@ -220,7 +220,7 @@ macro_rules! define_env { fn can_satisfy( module: &[u8], name: &[u8], - func_type: &wasm_instrument::parity_wasm::elements::FunctionType, + func_type: &pwasm_utils::parity_wasm::elements::FunctionType, ) -> bool { #[cfg(not(feature = "unstable-interface"))] @@ -260,9 +260,9 @@ mod tests { wasm::{runtime::TrapReason, tests::MockExt, Runtime}, Weight, }; + use pwasm_utils::parity_wasm::elements::{FunctionType, ValueType}; use sp_runtime::traits::Zero; use sp_sandbox::{ReturnValue, Value}; - use wasm_instrument::parity_wasm::elements::{FunctionType, ValueType}; struct TestRuntime { value: u32, diff --git a/frame/contracts/src/wasm/env_def/mod.rs b/frame/contracts/src/wasm/env_def/mod.rs index b4c5ffe81e7c1..c74af80d3e4ef 100644 --- a/frame/contracts/src/wasm/env_def/mod.rs +++ b/frame/contracts/src/wasm/env_def/mod.rs @@ -18,8 +18,8 @@ use super::Runtime; use crate::exec::Ext; +use pwasm_utils::parity_wasm::elements::{FunctionType, ValueType}; use sp_sandbox::Value; -use wasm_instrument::parity_wasm::elements::{FunctionType, ValueType}; #[macro_use] pub mod macros; diff --git a/frame/contracts/src/wasm/prepare.rs b/frame/contracts/src/wasm/prepare.rs index 70c15cb8c4e6e..a57e9aabf8bfb 100644 --- a/frame/contracts/src/wasm/prepare.rs +++ b/frame/contracts/src/wasm/prepare.rs @@ -26,11 +26,9 @@ use crate::{ AccountIdOf, Config, Schedule, }; use codec::{Encode, MaxEncodedLen}; +use pwasm_utils::parity_wasm::elements::{self, External, Internal, MemoryType, Type, ValueType}; use sp_runtime::traits::Hash; use sp_std::prelude::*; -use wasm_instrument::parity_wasm::elements::{ - self, External, Internal, MemoryType, Type, ValueType, -}; /// Imported memory must be located inside this module. The reason for hardcoding is that current /// compiler toolchains might not support specifying other modules than "env" for memory imports. @@ -184,16 +182,17 @@ impl<'a, T: Config> ContractModule<'a, T> { fn inject_gas_metering(self) -> Result { let gas_rules = self.schedule.rules(&self.module); - let contract_module = - wasm_instrument::gas_metering::inject(self.module, &gas_rules, "seal0") - .map_err(|_| "gas instrumentation failed")?; + let contract_module = pwasm_utils::inject_gas_counter(self.module, &gas_rules, "seal0") + .map_err(|_| "gas instrumentation failed")?; Ok(ContractModule { module: contract_module, schedule: self.schedule }) } fn inject_stack_height_metering(self) -> Result { - let contract_module = - wasm_instrument::inject_stack_limiter(self.module, self.schedule.limits.stack_height) - .map_err(|_| "stack height instrumentation failed")?; + let contract_module = pwasm_utils::stack_height::inject_limiter( + self.module, + self.schedule.limits.stack_height, + ) + .map_err(|_| "stack height instrumentation failed")?; Ok(ContractModule { module: contract_module, schedule: self.schedule }) } diff --git a/frame/contracts/src/wasm/runtime.rs b/frame/contracts/src/wasm/runtime.rs index 13aa934306978..e9a09ce6b35af 100644 --- a/frame/contracts/src/wasm/runtime.rs +++ b/frame/contracts/src/wasm/runtime.rs @@ -28,12 +28,12 @@ use bitflags::bitflags; use codec::{Decode, DecodeAll, Encode, MaxEncodedLen}; use frame_support::{dispatch::DispatchError, ensure, weights::Weight}; use pallet_contracts_primitives::{ExecReturnValue, ReturnFlags}; +use pwasm_utils::parity_wasm::elements::ValueType; use sp_core::{crypto::UncheckedFrom, Bytes}; use sp_io::hashing::{blake2_128, blake2_256, keccak_256, sha2_256}; use sp_runtime::traits::{Bounded, Zero}; use sp_sandbox::SandboxMemory; use sp_std::prelude::*; -use wasm_instrument::parity_wasm::elements::ValueType; /// Every error that can be returned to a contract when it calls any of the host functions. ///