Skip to content

Commit

Permalink
Revert "Remove feature(alloc_error_handler)"
Browse files Browse the repository at this point in the history
This reverts commit 5f725871b67de32235d8ac5140f81c32006af237.
  • Loading branch information
ukint-vs committed Apr 24, 2024
1 parent 13132ab commit 9eae878
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
1 change: 1 addition & 0 deletions substrate/primitives/application-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ serde = [ "dep:serde", "scale-info/serde", "sp-core/serde" ]
# For the regular wasm runtime builds this should not be used.
full_crypto = [
"sp-core/full_crypto",
"sp-io/disable_oom",
# Don't add `panic_handler` and `alloc_error_handler` since they are expected to be provided
# by the user anyway.
"sp-io/disable_panic_handler",
Expand Down
6 changes: 4 additions & 2 deletions substrate/primitives/io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ std = [
with-tracing = [ "sp-tracing/with-tracing" ]

# These two features are used for `no_std` builds for the environments which already provides
# `#[panic_handler]` and `#[global_allocator]`.
# `#[panic_handler]`, `#[alloc_error_handler]` and `#[global_allocator]`.
#
# For the regular wasm runtime builds those are not used.
disable_panic_handler = []
disable_oom = []
disable_allocator = []

# This feature flag controls the runtime's behavior when encountering
Expand All @@ -80,7 +81,8 @@ disable_allocator = []
# logs, with the caller receving a generic "wasm `unreachable` instruction executed"
# error message.
#
# This has no effect if `disable_panic_handler` enabled.
# 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
Expand Down
44 changes: 22 additions & 22 deletions substrate/primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(enable_alloc_error_handler, feature(alloc_error_handler))]

use sp_std::vec::Vec;

Expand Down Expand Up @@ -1765,34 +1766,33 @@ mod allocator_impl {
#[panic_handler]
#[no_mangle]
pub fn panic(info: &core::panic::PanicInfo) -> ! {
use sp_std::alloc::{alloc::AllocErrorPanicPayload, format, string::String};

let improved_panic_error_reporting = match () {
#[cfg(feature = "improved_panic_error_reporting")]
() => true,
#[cfg(not(feature = "improved_panic_error_reporting"))]
() => false,
};

let message = info
.payload()
.downcast_ref::<AllocErrorPanicPayload>()
.map(|_| {
let msg = improved_panic_error_reporting
.then_some("Runtime memory exhausted.")
.unwrap_or("Runtime memory exhausted. Aborting");
String::from(msg)
})
.unwrap_or_else(|| format!("{info}"));

if improved_panic_error_reporting {
let message = sp_std::alloc::format!("{}", info);
#[cfg(feature = "improved_panic_error_reporting")]
{
panic_handler::abort_on_panic(&message);
} else {
}
#[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"), enable_alloc_error_handler))]
#[alloc_error_handler]
pub fn oom(_: core::alloc::Layout) -> ! {
#[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.
#[cfg(feature = "std")]
pub type TestExternalities = sp_state_machine::TestExternalities<sp_core::Blake2Hasher>;
Expand Down

0 comments on commit 9eae878

Please sign in to comment.