Add a new host function for reporting fatal errors; make WASM backtraces readable when printing out errors (#10741)

* Add a new host function for reporting fatal errors

* Fix one of the wasmtime executor tests

* Have `#[runtime_interface(wasm_only)]` actually mean WASM-only, and not no_std-only

* Print out errors through `Display` instead of `Debug`

* Switch one more trait to require `Error` for its error instead of only `Debug`

* Align to review comments
This commit is contained in:
Koute
2022-02-09 18:12:55 +09:00
committed by GitHub
parent bd261d57c4
commit 9a31b2c341
68 changed files with 554 additions and 249 deletions
+41 -3
View File
@@ -31,9 +31,6 @@ pub enum Error {
#[error("Unserializable data encountered")]
InvalidData(#[from] sp_serializer::Error),
#[error(transparent)]
Trap(#[from] wasmi::Trap),
#[error(transparent)]
Wasmi(#[from] wasmi::Error),
@@ -108,6 +105,12 @@ pub enum Error {
#[error("Invalid initializer expression provided {0}")]
InvalidInitializerExpression(String),
#[error("Execution aborted due to panic: {0}")]
AbortedDueToPanic(MessageWithBacktrace),
#[error("Execution aborted due to trap: {0}")]
AbortedDueToTrap(MessageWithBacktrace),
}
impl wasmi::HostError for Error {}
@@ -160,3 +163,38 @@ pub enum WasmError {
#[error("{0}")]
Other(String),
}
/// An error message with an attached backtrace.
#[derive(Debug)]
pub struct MessageWithBacktrace {
/// The error message.
pub message: String,
/// The backtrace associated with the error message.
pub backtrace: Option<Backtrace>,
}
impl std::fmt::Display for MessageWithBacktrace {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.write_str(&self.message)?;
if let Some(ref backtrace) = self.backtrace {
fmt.write_str("\nWASM backtrace:\n")?;
backtrace.backtrace_string.fmt(fmt)?;
}
Ok(())
}
}
/// A WASM backtrace.
#[derive(Debug)]
pub struct Backtrace {
/// The string containing the backtrace.
pub backtrace_string: String,
}
impl std::fmt::Display for Backtrace {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
fmt.write_str(&self.backtrace_string)
}
}