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
+33 -7
View File
@@ -1290,6 +1290,17 @@ pub trait Allocator {
}
}
/// WASM-only interface which allows for aborting the execution in case
/// of an unrecoverable error.
#[runtime_interface(wasm_only)]
pub trait PanicHandler {
/// Aborts the current execution with the given error message.
#[trap_on_return]
fn abort_on_panic(&mut self, message: &str) {
self.register_panic_error_message(message);
}
}
/// Interface that provides functions for logging from within the runtime.
#[runtime_interface]
pub trait Logging {
@@ -1588,14 +1599,14 @@ pub trait RuntimeTasks {
}
/// Allocator used by Substrate when executing the Wasm runtime.
#[cfg(not(feature = "std"))]
#[cfg(all(target_arch = "wasm32", not(feature = "std")))]
struct WasmAllocator;
#[cfg(all(not(feature = "disable_allocator"), not(feature = "std")))]
#[cfg(all(target_arch = "wasm32", not(feature = "disable_allocator"), not(feature = "std")))]
#[global_allocator]
static ALLOCATOR: WasmAllocator = WasmAllocator;
#[cfg(not(feature = "std"))]
#[cfg(all(target_arch = "wasm32", not(feature = "std")))]
mod allocator_impl {
use super::*;
use core::alloc::{GlobalAlloc, Layout};
@@ -1617,16 +1628,30 @@ mod allocator_impl {
#[no_mangle]
pub fn panic(info: &core::panic::PanicInfo) -> ! {
let message = sp_std::alloc::format!("{}", info);
logging::log(LogLevel::Error, "runtime", message.as_bytes());
core::arch::wasm32::unreachable();
#[cfg(feature = "improved_panic_error_reporting")]
{
panic_handler::abort_on_panic(&message);
}
#[cfg(not(feature = "improved_panic_error_reporting"))]
{
logging::log(LogLevel::Error, "runtime", message.as_bytes());
core::arch::wasm32::unreachable();
}
}
/// A default OOM handler for WASM environment.
#[cfg(all(not(feature = "disable_oom"), not(feature = "std")))]
#[alloc_error_handler]
pub fn oom(_: core::alloc::Layout) -> ! {
logging::log(LogLevel::Error, "runtime", b"Runtime memory exhausted. Aborting");
core::arch::wasm32::unreachable();
#[cfg(feature = "improved_panic_error_reporting")]
{
panic_handler::abort_on_panic("Runtime memory exhausted.");
}
#[cfg(not(feature = "improved_panic_error_reporting"))]
{
logging::log(LogLevel::Error, "runtime", b"Runtime memory exhausted. Aborting");
core::arch::wasm32::unreachable();
}
}
/// Type alias for Externalities implementation used in tests.
@@ -1646,6 +1671,7 @@ pub type SubstrateHostFunctions = (
crypto::HostFunctions,
hashing::HostFunctions,
allocator::HostFunctions,
panic_handler::HostFunctions,
logging::HostFunctions,
sandbox::HostFunctions,
crate::trie::HostFunctions,