mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-23 16:41:06 +00:00
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:
@@ -66,3 +66,22 @@ with-tracing = [
|
||||
disable_panic_handler = []
|
||||
disable_oom = []
|
||||
disable_allocator = []
|
||||
|
||||
# This feature flag controls the runtime's behavior when encountering
|
||||
# a panic or when it runs out of memory, improving the diagnostics.
|
||||
#
|
||||
# When enabled the runtime will marshal the relevant error message
|
||||
# to the host through the `PanicHandler::abort_on_panic` runtime interface.
|
||||
# This gives the caller direct programmatic access to the error message.
|
||||
#
|
||||
# When disabled the error message will only be printed out in the
|
||||
# logs, with the caller receving a generic "wasm `unreachable` instruction executed"
|
||||
# error message.
|
||||
#
|
||||
# This has no effect if both `disable_panic_handler` and `disable_oom`
|
||||
# are enabled.
|
||||
#
|
||||
# WARNING: Enabling this feature flag requires the `PanicHandler::abort_on_panic`
|
||||
# host function to be supported by the host. Do *not* enable it for your
|
||||
# runtime without first upgrading your host client!
|
||||
improved_panic_error_reporting = []
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user