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

Commit

Permalink
Additional wasm diagnostics (#4097)
Browse files Browse the repository at this point in the history
* Wasm diagnostics

* Pass the error

* Make errno optional

* Cargo.lock

* Log the error
  • Loading branch information
pepyakin authored and bkchr committed Nov 14, 2019
1 parent 4da0414 commit 25ee6eb
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 35 deletions.
32 changes: 24 additions & 8 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion core/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trie = { package = "substrate-trie", path = "../trie" }
serializer = { package = "substrate-serializer", path = "../serializer" }
runtime_version = { package = "sr-version", path = "../sr-version" }
panic-handler = { package = "substrate-panic-handler", path = "../panic-handler" }
wasmi = "0.5.1"
wasmi = "0.6.2"
parity-wasm = "0.40.3"
lazy_static = "1.4.0"
wasm-interface = { package = "substrate-wasm-interface", path = "../wasm-interface" }
Expand Down Expand Up @@ -55,3 +55,6 @@ wasmtime = [
"wasmtime-jit",
"wasmtime-runtime",
]
wasmi-errno = [
"wasmi/errno"
]
4 changes: 4 additions & 0 deletions core/executor/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub enum WasmError {
CodeNotFound,
/// Failure to reinitialize runtime instance from snapshot.
ApplySnapshotFailed,
/// Failure to erase the wasm memory.
///
/// Depending on the implementation might mean failure of allocating memory.
ErasingFailed(String),
/// Wasm code failed validation.
InvalidModule,
/// Wasm code could not be deserialized.
Expand Down
33 changes: 11 additions & 22 deletions core/executor/src/wasmi_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::sandbox;
use crate::allocator;
use crate::wasm_utils::interpret_runtime_api_result;
use crate::wasm_runtime::WasmRuntime;
use log::trace;
use log::{error, trace};
use parity_wasm::elements::{deserialize_buffer, DataSegment, Instruction, Module as RawModule};
use wasm_interface::{
FunctionContext, Pointer, WordSize, Sandbox, MemoryId, Result as WResult, Function,
Expand Down Expand Up @@ -500,7 +500,7 @@ impl StateSnapshot {
// First, erase the memory and copy the data segments into it.
memory
.erase()
.map_err(|_| WasmError::ApplySnapshotFailed)?;
.map_err(|e| WasmError::ErasingFailed(e.to_string()))?;
for (offset, contents) in &self.data_segments {
memory
.set(*offset, contents)
Expand Down Expand Up @@ -536,23 +536,6 @@ pub struct WasmiRuntime {
host_functions: Vec<&'static dyn Function>,
}

impl WasmiRuntime {
/// Perform an operation with the clean version of the runtime wasm instance.
fn with<R, F>(&self, f: F) -> R
where
F: FnOnce(&ModuleRef) -> R,
{
self.state_snapshot.apply(&self.instance).expect(
"applying the snapshot can only fail if the passed instance is different
from the one that was used for creation of the snapshot;
we use the snapshot that is directly associated with the instance;
thus the snapshot was created using the instance;
qed",
);
f(&self.instance)
}
}

impl WasmRuntime for WasmiRuntime {
fn update_heap_pages(&mut self, heap_pages: u64) -> bool {
self.state_snapshot.heap_pages == heap_pages
Expand All @@ -568,9 +551,15 @@ impl WasmRuntime for WasmiRuntime {
method: &str,
data: &[u8],
) -> Result<Vec<u8>, Error> {
self.with(|module| {
call_in_wasm_module(ext, module, method, data, &self.host_functions)
})
self.state_snapshot.apply(&self.instance)
.map_err(|e| {
// Snapshot restoration failed. This is pretty unexpected since this can happen
// if some invariant is broken or if the system is under extreme memory pressure
// (so erasing fails).
error!(target: "wasm-executor", "snapshot restoration failed: {}", e);
e
})?;
call_in_wasm_module(ext, &self.instance, method, data, &self.host_functions)
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ twox-hash = { version = "1.5.0", default-features = false, optional = true }
byteorder = { version = "1.3.2", default-features = false }
primitive-types = { version = "0.6", default-features = false, features = ["codec"] }
impl-serde = { version = "0.2.3", optional = true }
wasmi = { version = "0.5.1", optional = true }
wasmi = { version = "0.6.2", optional = true }
hash-db = { version = "0.15.2", default-features = false }
hash256-std-hasher = { version = "0.15.2", default-features = false }
ed25519-dalek = { version = "0.9.1", default-features = false, features = ["u64_backend"], optional = true }
Expand Down
2 changes: 1 addition & 1 deletion core/sr-sandbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

[dependencies]
wasmi = { version = "0.5.1", optional = true }
wasmi = { version = "0.6.2", optional = true }
primitives = { package = "substrate-primitives", path = "../primitives", default-features = false }
rstd = { package = "sr-std", path = "../sr-std", default-features = false }
runtime-io = { package = "sr-io", path = "../sr-io", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion core/wasm-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ authors = ["Parity Technologies <[email protected]>"]
edition = "2018"

[dependencies]
wasmi = "0.5.1"
wasmi = "0.6.2"
impl-trait-for-tuples = "0.1.2"
3 changes: 2 additions & 1 deletion node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ cli = [
"tokio",
"exit-future",
"ctrlc",
"substrate-service/rocksdb"
"substrate-service/rocksdb",
"node-executor/wasmi-errno",
]
wasmtime = [
"cli",
Expand Down
3 changes: 3 additions & 0 deletions node/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ criterion = "0.3.0"
wasmtime = [
"substrate-executor/wasmtime",
]
wasmi-errno = [
"substrate-executor/wasmi-errno",
]
stress-test = []

[[bench]]
Expand Down

0 comments on commit 25ee6eb

Please sign in to comment.