Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

contracts: switch from parity-wasm-based to wasmi-based module validation #14449

Merged
merged 83 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
eef15f1
upgrade to wasmi 0.29
agryaznov May 3, 2023
dee28ff
prepare cleanup
agryaznov May 3, 2023
efeb0bd
sync ref_time w engine from the stack frame
agryaznov May 3, 2023
4fdd5c8
proc_macro: sync gas in host funcs
agryaznov May 4, 2023
cac61c6
clean benchmarks & schedule: w_base = w_i64const
agryaznov May 5, 2023
be0df3c
scale gas values btw engine and gas meter
agryaznov May 5, 2023
8a32757
(re)instrumentation & code_cache removed
agryaznov May 5, 2023
f8abec4
remove gas() host fn, continue clean-up
agryaznov May 8, 2023
22440ae
address review comments
agryaznov Jun 9, 2023
c94821d
Merge branch 'master' into ag-wasmeter
agryaznov Jun 9, 2023
2010ff7
move from CodeStorage&PrefabWasmModule to PristineCode&WasmBlob
agryaznov Jun 9, 2023
100cdcd
refactor: no reftime_limit&schedule passes, no CodeStorage
agryaznov Jun 9, 2023
85a0340
bugs fixing
agryaznov Jun 10, 2023
505f750
fix tests: expected deposit amount
agryaznov Jun 10, 2023
c25f634
fix prepare::tests
agryaznov Jun 10, 2023
7f46e7e
update tests and fix bugs
agryaznov Jun 10, 2023
7bf4fbe
update docs
agryaznov Jun 11, 2023
cc05e88
bump wasmi 0.30.0
agryaznov Jun 12, 2023
0e2b0ab
benchmarks updated, tests pass
agryaznov Jun 12, 2023
e6e3563
refactoring
agryaznov Jun 12, 2023
f4ffcf2
s/OwnerInfo/CodeInfo/g;
agryaznov Jun 12, 2023
a50b7cf
migration: draft, compiles
agryaznov Jun 12, 2023
c6b9f74
migration: draft, runs
agryaznov Jun 13, 2023
132e9ce
migration: draft, runs (fixing)
agryaznov Jun 13, 2023
9935e92
deposits repaid non pro rata
agryaznov Jun 13, 2023
0b9125a
deposits repaid pro rata
agryaznov Jun 13, 2023
08a15b7
better try-runtime output
agryaznov Jun 13, 2023
5f6c1ac
even better try-runtime output
agryaznov Jun 13, 2023
b1290f0
benchmark migration
agryaznov Jun 14, 2023
9ffca5d
Merge branch 'master' into ag-wasmeter
agryaznov Jun 14, 2023
94f8476
fix merge leftover
agryaznov Jun 14, 2023
ba67751
add forgotten fixtures, fix docs
agryaznov Jun 14, 2023
0ee3293
address review comments
agryaznov Jun 14, 2023
a45c538
ci fixes
agryaznov Jun 14, 2023
f24a267
cleanup
agryaznov Jun 15, 2023
e6c21f3
benchmarks::prepare to return DispatchError
agryaznov Jun 15, 2023
444a97b
Merge branch 'master' into ag-wasmeter
agryaznov Jun 15, 2023
d3f59b2
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jun 15, 2023
f12927d
store memory limits to CodeInfo
agryaznov Jun 16, 2023
0a9b351
ci: roll back weights
agryaznov Jun 16, 2023
cff9da7
Merge branch 'master' of https://github.com/paritytech/substrate into…
Jun 16, 2023
f0e24c5
".git/.scripts/commands/bench-vm/bench-vm.sh" pallet dev pallet_contr…
Jun 16, 2023
2e07393
drive-by: update Readme and pallet rustdoc
agryaznov Jun 16, 2023
f465ecf
Merge branch 'master' into ag-wasmeter
agryaznov Jun 17, 2023
94be691
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jun 17, 2023
721d4b6
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jun 17, 2023
e226c0e
use wasmi 0.29
agryaznov Jun 18, 2023
e57596f
Merge branch 'master' of https://github.com/paritytech/substrate into…
Jun 18, 2023
d687f1c
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jun 18, 2023
76a8c20
use wasmi 0.30 again
agryaznov Jun 20, 2023
fbc43ae
query memory limits from wasmi
agryaznov Jun 20, 2023
cd54ff2
save: scan_exports ported, compiles
agryaznov Jun 20, 2023
b548064
save (wip, not compiles)
agryaznov Jun 20, 2023
0b4bb91
query memory limits from wasmi
agryaznov Jun 20, 2023
b15d5a9
better migration types
agryaznov Jun 20, 2023
8eb8710
ci: pull weights from master
agryaznov Jun 20, 2023
dae3f32
Merge branch 'master' into ag-wasmeter
agryaznov Jun 21, 2023
f42db5d
refactoring
agryaznov Jun 21, 2023
b789e6b
".git/.scripts/commands/bench-vm/bench-vm.sh" pallet dev pallet_contr…
Jun 21, 2023
3cadbb1
Merge branch 'ag-wasmeter' into ag-wasmi-validation
agryaznov Jun 21, 2023
eba148b
scan_imports ported
agryaznov Jun 21, 2023
ca5938d
scan_export ported, other checks removed
agryaznov Jun 22, 2023
c2c3d9b
tests fixed
agryaznov Jun 22, 2023
56dc2df
drop wasmparser and parity-wasm dependencies
agryaznov Jun 23, 2023
086e219
typo fix
agryaznov Jun 27, 2023
2544972
addressing review comments
agryaznov Jun 27, 2023
387bec9
refactor
agryaznov Jun 27, 2023
8be0537
address review comments
agryaznov Jun 29, 2023
ffdb8d1
optimize migration
agryaznov Jun 30, 2023
eaf7a31
Merge branch 'master' into ag-wasmeter
agryaznov Jun 30, 2023
f4dad0f
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jun 30, 2023
b5ad1de
another review round comments addressed
agryaznov Jun 30, 2023
0e0155f
ci fix one
agryaznov Jun 30, 2023
d139757
clippy fix
agryaznov Jun 30, 2023
670ff1a
ci fix two
agryaznov Jun 30, 2023
18236e1
Merge branch 'ag-wasmeter' into ag-wasmi-validation
agryaznov Jul 3, 2023
0f83c77
allow stored modules to have no memory imports
agryaznov Jul 3, 2023
ca111cf
Merge branch 'master' into ag-wasmi-validation
agryaznov Jul 3, 2023
bf52770
rollback: allow stored modules to have no memory imports
agryaznov Jul 3, 2023
824ef6d
Merge branch 'master' of https://github.com/paritytech/substrate into…
Jul 3, 2023
b55dbcb
".git/.scripts/commands/bench/bench.sh" pallet dev pallet_contracts
Jul 3, 2023
39fb730
address review comments
agryaznov Jul 4, 2023
ac9d5c2
Merge branch 'ag-wasmi-validation'
agryaznov Jul 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions frame/contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ codec = { package = "parity-scale-codec", version = "3.6.1", default-features =
] }
scale-info = { version = "2.5.0", default-features = false, features = ["derive"] }
log = { version = "0.4", default-features = false }
wasm-instrument = { version = "0.4", default-features = false }
serde = { version = "1", optional = true, features = ["derive"] }
smallvec = { version = "1", default-features = false, features = [
"const_generics",
] }
wasmi = { version = "0.30", default-features = false }
wasmparser = { package = "wasmparser-nostd", version = "0.100", default-features = false }
impl-trait-for-tuples = "0.2"

# Only used in benchmarking to generate random contract code
# Only used in benchmarking to generate contract code
wasm-instrument = { version = "0.4", optional = true, default-features = false }
agryaznov marked this conversation as resolved.
Show resolved Hide resolved
rand = { version = "0.8", optional = true, default-features = false }
rand_pcg = { version = "0.3", optional = true }

Expand Down Expand Up @@ -81,12 +80,12 @@ std = [
"pallet-contracts-proc-macro/full",
"log/std",
"rand/std",
"wasmparser/std",
"environmental/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"rand",
"rand_pcg",
"wasm-instrument",
]
try-runtime = ["frame-support/try-runtime"]
1 change: 1 addition & 0 deletions frame/contracts/fixtures/dummy.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
;; A valid contract which does nothing at all
(module
(import "env" "memory" (memory 1 1))
(func (export "deploy"))
(func (export "call"))
)
1 change: 1 addition & 0 deletions frame/contracts/fixtures/float_instruction.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
;; Module that contains a float instruction which is illegal in deterministic mode
(module
(import "env" "memory" (memory 1 1))
athei marked this conversation as resolved.
Show resolved Hide resolved
(func (export "call")
f32.const 1
drop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
;; Valid module but missing the call function
(module
(import "env" "memory" (memory 1 1))
(func (export "deploy"))
)
5 changes: 5 additions & 0 deletions frame/contracts/fixtures/invalid_contract_no_memory.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
;; A valid contract which does nothing at all
(module
(func (export "deploy"))
(func (export "call"))
)
1 change: 1 addition & 0 deletions frame/contracts/fixtures/run_out_of_gas.wat
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(module
(import "env" "memory" (memory 1 1))
(func (export "call")
(loop $inf (br $inf)) ;; just run out of gas
(unreachable)
Expand Down
21 changes: 11 additions & 10 deletions frame/contracts/src/benchmarking/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl<T: Config> From<ModuleDefinition> for WasmModule<T> {
// internal functions start at that offset.
let func_offset = u32::try_from(def.imported_functions.len()).unwrap();

// Every contract must export "deploy" and "call" functions
// Every contract must export "deploy" and "call" functions.
let mut contract = builder::module()
// deploy function (first internal function)
.function()
Expand Down Expand Up @@ -163,15 +163,16 @@ impl<T: Config> From<ModuleDefinition> for WasmModule<T> {
}

// Grant access to linear memory.
if let Some(memory) = &def.memory {
contract = contract
.import()
.module("env")
.field("memory")
.external()
.memory(memory.min_pages, Some(memory.max_pages))
.build();
}
// Every contract module is required to have an imported memory.
// If no memory is specified in the passed ModuleDefenition, then
// default to (1, 1).
let (init, max) = if let Some(memory) = &def.memory {
(memory.min_pages, Some(memory.max_pages))
} else {
(1, Some(1))
};

contract = contract.import().path("env", "memory").external().memory(init, max).build();

// Import supervisor functions. They start with idx 0.
for func in def.imported_functions {
Expand Down
5 changes: 4 additions & 1 deletion frame/contracts/src/benchmarking/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
/// ! sandbox to execute the Wasm code. This is because we do not need the full
/// ! environment that provides the seal interface as imported functions.
use super::{code::WasmModule, Config};
use crate::wasm::{AllowDeprecatedInterface, AllowUnstableInterface, Environment, WasmBlob};
use crate::wasm::{
AllowDeprecatedInterface, AllowUnstableInterface, Determinism, Environment, WasmBlob,
};
use sp_core::Get;
use wasmi::{errors::LinkerError, Func, Linker, StackLimits, Store};

Expand All @@ -44,6 +46,7 @@ impl<T: Config> From<&WasmModule<T>> for Sandbox {
&module.code,
(),
&<T>::Schedule::get(),
Determinism::Relaxed,
StackLimits::default(),
// We are testing with an empty environment anyways
AllowDeprecatedInterface::No,
Expand Down
2 changes: 2 additions & 0 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,8 @@ pub mod pallet {
CodeTooLarge,
/// No code could be found at the supplied code hash.
CodeNotFound,
/// No code info could be found at the supplied code hash.
CodeInfoNotFound,
/// A buffer outside of sandbox memory was passed to a contract API function.
OutOfBounds,
/// Input passed to a contract API function failed to decode as expected type.
Expand Down
35 changes: 32 additions & 3 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2055,7 +2055,7 @@ fn disabled_chain_extension_errors_on_call() {
TestExtension::disable();
assert_err_ignore_postinfo!(
Contracts::call(RuntimeOrigin::signed(ALICE), addr.clone(), 0, GAS_LIMIT, None, vec![],),
Error::<Test>::NoChainExtension,
Error::<Test>::CodeRejected,
);
});
}
Expand Down Expand Up @@ -4419,10 +4419,10 @@ fn code_rejected_error_works() {
assert_err!(result.result, <Error<Test>>::CodeRejected);
assert_eq!(
std::str::from_utf8(&result.debug_message).unwrap(),
"Validation of new code failed!"
"Can't load the module into wasmi!"
);

let (wasm, _) = compile_module::<Test>("invalid_contract").unwrap();
let (wasm, _) = compile_module::<Test>("invalid_contract_no_call").unwrap();
assert_noop!(
Contracts::upload_code(
RuntimeOrigin::signed(ALICE),
Expand All @@ -4449,6 +4449,34 @@ fn code_rejected_error_works() {
std::str::from_utf8(&result.debug_message).unwrap(),
"call function isn't exported"
);

let (wasm, _) = compile_module::<Test>("invalid_contract_no_memory").unwrap();
assert_noop!(
Contracts::upload_code(
RuntimeOrigin::signed(ALICE),
wasm.clone(),
None,
Determinism::Enforced
),
<Error<Test>>::CodeRejected,
);

let result = Contracts::bare_instantiate(
ALICE,
0,
GAS_LIMIT,
None,
Code::Upload(wasm),
vec![],
vec![],
DebugInfo::UnsafeDebug,
CollectEvents::Skip,
);
assert_err!(result.result, <Error<Test>>::CodeRejected);
assert_eq!(
std::str::from_utf8(&result.debug_message).unwrap(),
"No memory import found in the module"
);
});
}

Expand Down Expand Up @@ -5117,6 +5145,7 @@ fn cannot_instantiate_indeterministic_code() {
None,
Determinism::Relaxed,
));

assert_err_ignore_postinfo!(
Contracts::instantiate(
RuntimeOrigin::signed(ALICE),
Expand Down
89 changes: 24 additions & 65 deletions frame/contracts/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use crate::wasm::{
use crate::{
exec::{ExecResult, Executable, ExportedFunction, Ext},
gas::{GasMeter, Token},
wasm::prepare::IMPORT_MODULE_MEMORY,
wasm::prepare::LoadedModule,
weights::WeightInfo,
AccountIdOf, BadOrigin, BalanceOf, CodeHash, CodeInfoOf, CodeVec, Config, Error, Event, Pallet,
PristineCode, Schedule, Weight, LOG_TARGET,
Expand All @@ -52,10 +52,8 @@ use frame_support::{
use sp_core::Get;
use sp_runtime::RuntimeDebug;
use sp_std::prelude::*;
use wasmi::{
Config as WasmiConfig, Engine, ExternType, FuelConsumptionMode, Instance, Linker, Memory,
MemoryType, Module, StackLimits, Store,
};
use wasmi::{Instance, Linker, Memory, MemoryType, StackLimits, Store};

const BYTES_PER_PAGE: usize = 64 * 1024;

/// Validated Wasm module ready for execution.
Expand Down Expand Up @@ -204,26 +202,16 @@ impl<T: Config> WasmBlob<T> {
code: &[u8],
host_state: H,
schedule: &Schedule<T>,
determinism: Determinism,
stack_limits: StackLimits,
allow_deprecated: AllowDeprecatedInterface,
) -> Result<(Store<H>, Memory, Instance), &'static str>
where
E: Environment<H>,
{
let mut config = WasmiConfig::default();
config
.set_stack_limits(stack_limits)
.wasm_multi_value(false)
.wasm_mutable_global(false)
.wasm_sign_extension(false)
.wasm_saturating_float_to_int(false)
.consume_fuel(true)
.fuel_consumption_mode(FuelConsumptionMode::Eager);

let engine = Engine::new(&config);
let module = Module::new(&engine, code.clone()).map_err(|_| "can't decode Wasm module")?;
let mut store = Store::new(&engine, host_state);
let mut linker = Linker::new(&engine);
let contract = LoadedModule::new::<T>(&code, determinism, Some(stack_limits))?;
let mut store = Store::new(&contract.engine, host_state);
let mut linker = Linker::new(&contract.engine);
E::define(
&mut store,
&mut linker,
Expand All @@ -235,8 +223,9 @@ impl<T: Config> WasmBlob<T> {
allow_deprecated,
)
.map_err(|_| "can't define host functions to Linker")?;

// Query wasmi for memory limits specified in the module's import entry.
let memory_limits = Self::get_memory_limits(module.imports(), schedule)?;
let memory_limits = contract.scan_imports::<T>(schedule)?;
// Here we allocate this memory in the _store_. It allocates _inital_ value, but allows it
// to grow up to maximum number of memory pages, if neccesary.
let qed = "We checked the limits versus our Schedule,
Expand All @@ -247,63 +236,20 @@ impl<T: Config> WasmBlob<T> {
MemoryType::new(memory_limits.0, Some(memory_limits.1)).expect(qed),
)
.expect(qed);

linker
.define("env", "memory", memory)
.expect("We just created the Linker. It has no definitions with this name; qed");

let instance = linker
.instantiate(&mut store, &module)
.instantiate(&mut store, &contract.module)
.map_err(|_| "can't instantiate module with provided definitions")?
.ensure_no_start(&mut store)
.map_err(|_| "start function is forbidden but found in the module")?;

Ok((store, memory, instance))
}

/// Query wasmi for memory limits specified for the import in Wasm module.
fn get_memory_limits(
imports: wasmi::ModuleImportsIter,
schedule: &Schedule<T>,
) -> Result<(u32, u32), &'static str> {
let mut mem_type = None;
for import in imports {
match *import.ty() {
ExternType::Memory(mt) => {
if import.module() != IMPORT_MODULE_MEMORY {
return Err("Invalid module for imported memory")
}
if import.name() != "memory" {
return Err("Memory import must have the field name 'memory'")
}
mem_type = Some(mt);
break
},
_ => continue,
}
}
// We don't need to check here if module memory limits satisfy the schedule,
// as this was already done during the code uploading.
// If none memory imported then set its limits to (0,0).
// Any access to it will then lead to out of bounds trap.
let (initial, maximum) = mem_type.map_or(Default::default(), |mt| {
(
mt.initial_pages().to_bytes().unwrap_or(0).saturating_div(BYTES_PER_PAGE) as u32,
mt.maximum_pages().map_or(schedule.limits.memory_pages, |p| {
p.to_bytes().unwrap_or(0).saturating_div(BYTES_PER_PAGE) as u32
}),
)
});
if initial > maximum {
return Err(
"Requested initial number of memory pages should not exceed the requested maximum",
)
}
if maximum > schedule.limits.memory_pages {
return Err("Maximum number of memory pages should not exceed the maximum configured in the Schedule.")
}
Ok((initial, maximum))
}

/// Getter method for the code_info.
pub fn code_info(&self) -> &CodeInfo<T> {
&self.code_info
Expand Down Expand Up @@ -469,6 +415,7 @@ impl<T: Config> Executable<T> for WasmBlob<T> {
code,
runtime,
&schedule,
self.code_info.determinism,
StackLimits::default(),
match function {
ExportedFunction::Call => AllowDeprecatedInterface::Yes,
Expand Down Expand Up @@ -3314,6 +3261,8 @@ mod tests {
const CODE: &str = r#"
(module
(import "seal0" "instantiation_nonce" (func $nonce (result i64)))
(import "env" "memory" (memory 1 1))

(func $assert (param i32)
(block $ok
(br_if $ok
Expand Down Expand Up @@ -3344,6 +3293,8 @@ mod tests {
const CANNOT_DEPLOY_UNSTABLE: &str = r#"
(module
(import "seal0" "reentrance_count" (func $reentrance_count (result i32)))
(import "env" "memory" (memory 1 1))

(func (export "call"))
(func (export "deploy"))
)
Expand All @@ -3364,27 +3315,35 @@ mod tests {
const CODE_RANDOM_0: &str = r#"
(module
(import "seal0" "seal_random" (func $seal_random (param i32 i32 i32 i32)))
(import "env" "memory" (memory 1 1))

(func (export "call"))
(func (export "deploy"))
)
"#;
const CODE_RANDOM_1: &str = r#"
(module
(import "seal1" "seal_random" (func $seal_random (param i32 i32 i32 i32)))
(import "env" "memory" (memory 1 1))

(func (export "call"))
(func (export "deploy"))
)
"#;
const CODE_RANDOM_2: &str = r#"
(module
(import "seal0" "random" (func $seal_random (param i32 i32 i32 i32)))
(import "env" "memory" (memory 1 1))

(func (export "call"))
(func (export "deploy"))
)
"#;
const CODE_RANDOM_3: &str = r#"
(module
(import "seal1" "random" (func $seal_random (param i32 i32 i32 i32)))
(import "env" "memory" (memory 1 1))

(func (export "call"))
(func (export "deploy"))
)
Expand Down
Loading