Switch from pwasm-utils to wasm-instrument (#10680)

* Switch executor to wasm-instrument

* Switch pallet-contracts to wasm-instrument
This commit is contained in:
Alexander Theißen
2022-01-17 17:00:17 +02:00
committed by GitHub
parent d174ff8a61
commit 2a122c44b1
12 changed files with 47 additions and 48 deletions
+11 -13
View File
@@ -5475,7 +5475,6 @@ dependencies = [
"pallet-utility", "pallet-utility",
"parity-scale-codec", "parity-scale-codec",
"pretty_assertions", "pretty_assertions",
"pwasm-utils",
"rand 0.8.4", "rand 0.8.4",
"rand_pcg 0.3.1", "rand_pcg 0.3.1",
"scale-info", "scale-info",
@@ -5486,6 +5485,7 @@ dependencies = [
"sp-runtime", "sp-runtime",
"sp-sandbox", "sp-sandbox",
"sp-std", "sp-std",
"wasm-instrument",
"wasmi-validation", "wasmi-validation",
"wat", "wat",
] ]
@@ -6957,17 +6957,6 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "pwasm-utils"
version = "0.18.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "880b3384fb00b8f6ecccd5d358b93bd2201900ae3daad213791d1864f6441f5c"
dependencies = [
"byteorder",
"log 0.4.14",
"parity-wasm 0.42.2",
]
[[package]] [[package]]
name = "quick-error" name = "quick-error"
version = "1.2.3" version = "1.2.3"
@@ -8094,13 +8083,13 @@ dependencies = [
"derive_more", "derive_more",
"environmental", "environmental",
"parity-scale-codec", "parity-scale-codec",
"pwasm-utils",
"sc-allocator", "sc-allocator",
"sp-core", "sp-core",
"sp-maybe-compressed-blob", "sp-maybe-compressed-blob",
"sp-serializer", "sp-serializer",
"sp-wasm-interface", "sp-wasm-interface",
"thiserror", "thiserror",
"wasm-instrument",
"wasmer", "wasmer",
"wasmer-compiler-singlepass", "wasmer-compiler-singlepass",
"wasmi", "wasmi",
@@ -11306,6 +11295,15 @@ dependencies = [
"rustc-demangle", "rustc-demangle",
] ]
[[package]]
name = "wasm-instrument"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e67369bb53d409b67e57ef31797b1b2d628955fc82f86f2ea78bb403acc7c73"
dependencies = [
"parity-wasm 0.42.2",
]
[[package]] [[package]]
name = "wasm-timer" name = "wasm-timer"
version = "0.2.5" version = "0.2.5"
+1 -1
View File
@@ -15,7 +15,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies] [dependencies]
derive_more = "0.99.16" derive_more = "0.99.16"
pwasm-utils = "0.18.2" wasm-instrument = "0.1"
codec = { package = "parity-scale-codec", version = "2.0.0" } codec = { package = "parity-scale-codec", version = "2.0.0" }
wasmi = "0.9.1" wasmi = "0.9.1"
sp-core = { version = "4.1.0-dev", path = "../../../primitives/core" } sp-core = { version = "4.1.0-dev", path = "../../../primitives/core" }
@@ -18,8 +18,8 @@
use super::RuntimeBlob; use super::RuntimeBlob;
use crate::error::{self, Error}; use crate::error::{self, Error};
use pwasm_utils::parity_wasm::elements::Instruction;
use std::mem; use std::mem;
use wasm_instrument::parity_wasm::elements::Instruction;
/// This is a snapshot of data segments specialzied for a particular instantiation. /// This is a snapshot of data segments specialzied for a particular instantiation.
/// ///
@@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::error::WasmError; use crate::error::WasmError;
use pwasm_utils::{ use wasm_instrument::{
export_mutable_globals, export_mutable_globals,
parity_wasm::elements::{deserialize_buffer, serialize, DataSegment, Internal, Module}, parity_wasm::elements::{deserialize_buffer, serialize, DataSegment, Internal, Module},
}; };
@@ -84,7 +84,7 @@ impl RuntimeBlob {
/// depth of the wasm operand stack. /// depth of the wasm operand stack.
pub fn inject_stack_depth_metering(self, stack_depth_limit: u32) -> Result<Self, WasmError> { pub fn inject_stack_depth_metering(self, stack_depth_limit: u32) -> Result<Self, WasmError> {
let injected_module = let injected_module =
pwasm_utils::stack_height::inject_limiter(self.raw_module, stack_depth_limit).map_err( wasm_instrument::inject_stack_limiter(self.raw_module, stack_depth_limit).map_err(
|e| WasmError::Other(format!("cannot inject the stack limiter: {:?}", e)), |e| WasmError::Other(format!("cannot inject the stack limiter: {:?}", e)),
)?; )?;
+2 -2
View File
@@ -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"] } scale-info = { version = "1.0", default-features = false, features = ["derive"] }
log = { version = "0.4", default-features = false } log = { version = "0.4", default-features = false }
pwasm-utils = { version = "0.18.2", default-features = false } wasm-instrument = { version = "0.1", default-features = false }
serde = { version = "1", optional = true, features = ["derive"] } serde = { version = "1", optional = true, features = ["derive"] }
smallvec = { version = "1", default-features = false, features = [ smallvec = { version = "1", default-features = false, features = [
"const_generics", "const_generics",
@@ -71,7 +71,7 @@ std = [
"frame-benchmarking/std", "frame-benchmarking/std",
"frame-support/std", "frame-support/std",
"frame-system/std", "frame-system/std",
"pwasm-utils/std", "wasm-instrument/std",
"wasmi-validation/std", "wasmi-validation/std",
"pallet-contracts-primitives/std", "pallet-contracts-primitives/std",
"pallet-contracts-proc-macro/full", "pallet-contracts-proc-macro/full",
@@ -26,13 +26,6 @@
use crate::Config; use crate::Config;
use frame_support::traits::Get; 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_core::crypto::UncheckedFrom;
use sp_runtime::traits::Hash; use sp_runtime::traits::Hash;
use sp_sandbox::{ use sp_sandbox::{
@@ -40,6 +33,13 @@ use sp_sandbox::{
SandboxEnvironmentBuilder, SandboxMemory, SandboxEnvironmentBuilder, SandboxMemory,
}; };
use sp_std::{borrow::ToOwned, prelude::*}; 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. /// The location where to put the genrated code.
pub enum Location { pub enum Location {
@@ -562,10 +562,10 @@ where
fn inject_gas_metering<T: Config>(module: Module) -> Module { fn inject_gas_metering<T: Config>(module: Module) -> Module {
let schedule = T::Schedule::get(); let schedule = T::Schedule::get();
let gas_rules = schedule.rules(&module); let gas_rules = schedule.rules(&module);
pwasm_utils::inject_gas_counter(module, &gas_rules, "seal0").unwrap() wasm_instrument::gas_metering::inject(module, &gas_rules, "seal0").unwrap()
} }
fn inject_stack_metering<T: Config>(module: Module) -> Module { fn inject_stack_metering<T: Config>(module: Module) -> Module {
let height = T::Schedule::get().limits.stack_height; let height = T::Schedule::get().limits.stack_height;
pwasm_utils::stack_height::inject_limiter(module, height).unwrap() wasm_instrument::inject_stack_limiter(module, height).unwrap()
} }
@@ -39,12 +39,12 @@ use codec::Encode;
use frame_benchmarking::{account, benchmarks, whitelisted_caller}; use frame_benchmarking::{account, benchmarks, whitelisted_caller};
use frame_support::weights::Weight; use frame_support::weights::Weight;
use frame_system::RawOrigin; use frame_system::RawOrigin;
use pwasm_utils::parity_wasm::elements::{BlockType, BrTableData, Instruction, ValueType};
use sp_runtime::{ use sp_runtime::{
traits::{Bounded, Hash}, traits::{Bounded, Hash},
Perbill, Perbill,
}; };
use sp_std::prelude::*; use sp_std::prelude::*;
use wasm_instrument::parity_wasm::elements::{BlockType, BrTableData, Instruction, ValueType};
/// How many batches we do per API benchmark. /// How many batches we do per API benchmark.
const API_BENCHMARK_BATCHES: u32 = 20; const API_BENCHMARK_BATCHES: u32 = 20;
+5 -5
View File
@@ -23,12 +23,12 @@ use crate::{weights::WeightInfo, Config};
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use frame_support::{weights::Weight, DefaultNoBound}; use frame_support::{weights::Weight, DefaultNoBound};
use pallet_contracts_proc_macro::{ScheduleDebug, WeightDebug}; use pallet_contracts_proc_macro::{ScheduleDebug, WeightDebug};
use pwasm_utils::{parity_wasm::elements, rules};
use scale_info::TypeInfo; use scale_info::TypeInfo;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sp_runtime::RuntimeDebug; use sp_runtime::RuntimeDebug;
use sp_std::{marker::PhantomData, vec::Vec}; 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 /// 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 /// of API calls in batches (per benchmark component increase) is so that the linear regression
@@ -652,7 +652,7 @@ struct ScheduleRules<'a, T: Config> {
} }
impl<T: Config> Schedule<T> { impl<T: Config> Schedule<T> {
pub(crate) fn rules(&self, module: &elements::Module) -> impl rules::Rules + '_ { pub(crate) fn rules(&self, module: &elements::Module) -> impl gas_metering::Rules + '_ {
ScheduleRules { ScheduleRules {
schedule: &self, schedule: &self,
params: module params: module
@@ -668,7 +668,7 @@ impl<T: Config> Schedule<T> {
} }
} }
impl<'a, T: Config> rules::Rules for ScheduleRules<'a, T> { impl<'a, T: Config> gas_metering::Rules for ScheduleRules<'a, T> {
fn instruction_cost(&self, instruction: &elements::Instruction) -> Option<u32> { fn instruction_cost(&self, instruction: &elements::Instruction) -> Option<u32> {
use self::elements::Instruction::*; use self::elements::Instruction::*;
let w = &self.schedule.instruction_weights; let w = &self.schedule.instruction_weights;
@@ -752,10 +752,10 @@ impl<'a, T: Config> rules::Rules for ScheduleRules<'a, T> {
Some(weight) Some(weight)
} }
fn memory_grow_cost(&self) -> Option<rules::MemoryGrowCost> { fn memory_grow_cost(&self) -> gas_metering::MemoryGrowCost {
// We benchmarked the memory.grow instruction with the maximum allowed pages. // We benchmarked the memory.grow instruction with the maximum allowed pages.
// The cost for growing is therefore already included in the instruction cost. // The cost for growing is therefore already included in the instruction cost.
None gas_metering::MemoryGrowCost::Free
} }
} }
@@ -28,7 +28,7 @@ macro_rules! convert_args {
macro_rules! gen_signature { macro_rules! gen_signature {
( ( $( $params: ty ),* ) ) => ( ( ( $( $params: ty ),* ) ) => (
{ {
pwasm_utils::parity_wasm::elements::FunctionType::new( wasm_instrument::parity_wasm::elements::FunctionType::new(
convert_args!($($params),*), vec![], convert_args!($($params),*), vec![],
) )
} }
@@ -36,7 +36,7 @@ macro_rules! gen_signature {
( ( $( $params: ty ),* ) -> $returns: ty ) => ( ( ( $( $params: ty ),* ) -> $returns: ty ) => (
{ {
pwasm_utils::parity_wasm::elements::FunctionType::new( wasm_instrument::parity_wasm::elements::FunctionType::new(
convert_args!($($params),*), convert_args!($($params),*),
vec![{use $crate::wasm::env_def::ConvertibleToWasm; <$returns>::VALUE_TYPE}], vec![{use $crate::wasm::env_def::ConvertibleToWasm; <$returns>::VALUE_TYPE}],
) )
@@ -220,7 +220,7 @@ macro_rules! define_env {
fn can_satisfy( fn can_satisfy(
module: &[u8], module: &[u8],
name: &[u8], name: &[u8],
func_type: &pwasm_utils::parity_wasm::elements::FunctionType, func_type: &wasm_instrument::parity_wasm::elements::FunctionType,
) -> bool ) -> bool
{ {
#[cfg(not(feature = "unstable-interface"))] #[cfg(not(feature = "unstable-interface"))]
@@ -260,9 +260,9 @@ mod tests {
wasm::{runtime::TrapReason, tests::MockExt, Runtime}, wasm::{runtime::TrapReason, tests::MockExt, Runtime},
Weight, Weight,
}; };
use pwasm_utils::parity_wasm::elements::{FunctionType, ValueType};
use sp_runtime::traits::Zero; use sp_runtime::traits::Zero;
use sp_sandbox::{ReturnValue, Value}; use sp_sandbox::{ReturnValue, Value};
use wasm_instrument::parity_wasm::elements::{FunctionType, ValueType};
struct TestRuntime { struct TestRuntime {
value: u32, value: u32,
@@ -18,8 +18,8 @@
use super::Runtime; use super::Runtime;
use crate::exec::Ext; use crate::exec::Ext;
use pwasm_utils::parity_wasm::elements::{FunctionType, ValueType};
use sp_sandbox::Value; use sp_sandbox::Value;
use wasm_instrument::parity_wasm::elements::{FunctionType, ValueType};
#[macro_use] #[macro_use]
pub mod macros; pub mod macros;
@@ -26,9 +26,11 @@ use crate::{
AccountIdOf, Config, Schedule, AccountIdOf, Config, Schedule,
}; };
use codec::{Encode, MaxEncodedLen}; use codec::{Encode, MaxEncodedLen};
use pwasm_utils::parity_wasm::elements::{self, External, Internal, MemoryType, Type, ValueType};
use sp_runtime::traits::Hash; use sp_runtime::traits::Hash;
use sp_std::prelude::*; 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 /// 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. /// compiler toolchains might not support specifying other modules than "env" for memory imports.
@@ -182,17 +184,16 @@ impl<'a, T: Config> ContractModule<'a, T> {
fn inject_gas_metering(self) -> Result<Self, &'static str> { fn inject_gas_metering(self) -> Result<Self, &'static str> {
let gas_rules = self.schedule.rules(&self.module); let gas_rules = self.schedule.rules(&self.module);
let contract_module = pwasm_utils::inject_gas_counter(self.module, &gas_rules, "seal0") let contract_module =
.map_err(|_| "gas instrumentation failed")?; wasm_instrument::gas_metering::inject(self.module, &gas_rules, "seal0")
.map_err(|_| "gas instrumentation failed")?;
Ok(ContractModule { module: contract_module, schedule: self.schedule }) Ok(ContractModule { module: contract_module, schedule: self.schedule })
} }
fn inject_stack_height_metering(self) -> Result<Self, &'static str> { fn inject_stack_height_metering(self) -> Result<Self, &'static str> {
let contract_module = pwasm_utils::stack_height::inject_limiter( let contract_module =
self.module, wasm_instrument::inject_stack_limiter(self.module, self.schedule.limits.stack_height)
self.schedule.limits.stack_height, .map_err(|_| "stack height instrumentation failed")?;
)
.map_err(|_| "stack height instrumentation failed")?;
Ok(ContractModule { module: contract_module, schedule: self.schedule }) Ok(ContractModule { module: contract_module, schedule: self.schedule })
} }
@@ -29,12 +29,12 @@ use bitflags::bitflags;
use codec::{Decode, DecodeAll, Encode, MaxEncodedLen}; use codec::{Decode, DecodeAll, Encode, MaxEncodedLen};
use frame_support::{dispatch::DispatchError, ensure, weights::Weight}; use frame_support::{dispatch::DispatchError, ensure, weights::Weight};
use pallet_contracts_primitives::{ExecReturnValue, ReturnFlags}; use pallet_contracts_primitives::{ExecReturnValue, ReturnFlags};
use pwasm_utils::parity_wasm::elements::ValueType;
use sp_core::{crypto::UncheckedFrom, Bytes}; use sp_core::{crypto::UncheckedFrom, Bytes};
use sp_io::hashing::{blake2_128, blake2_256, keccak_256, sha2_256}; use sp_io::hashing::{blake2_128, blake2_256, keccak_256, sha2_256};
use sp_runtime::traits::{Bounded, Zero}; use sp_runtime::traits::{Bounded, Zero};
use sp_sandbox::SandboxMemory; use sp_sandbox::SandboxMemory;
use sp_std::prelude::*; 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. /// Every error that can be returned to a contract when it calls any of the host functions.
/// ///