Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert(compiler): Downgrade to actual allocator panic handling approach #37

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions primitives/application-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ full_crypto = [
# 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",
"sp-io/disable_oom",
]
8 changes: 5 additions & 3 deletions primitives/io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ 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]`.
# These two features are used for `no_std` builds for the environments which already provides
# `#[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 @@ -82,7 +83,8 @@ disable_allocator = []
# logs, with the caller receving a generic "wasm `unreachable` instruction executed"
# error message.
#
# This has no effect if `disable_panic_handler` is 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
48 changes: 22 additions & 26 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(
all(not(feature = "disable_panic_handler"), not(feature = "std")),
feature(panic_oom_payload)
)]
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(
feature = "std",
doc = "Substrate runtime standard library as compiled when linked with Rust's standard library."
Expand Down Expand Up @@ -1643,34 +1640,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"), not(feature = "std")))]
#[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
1 change: 1 addition & 0 deletions primitives/state-machine/src/overlayed_changes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl<Transaction, H: Hasher> StorageChanges<Transaction, H> {
/// Storage transactions are calculated as part of the `storage_root`.
/// These transactions can be reused for importing the block into the
/// storage. So, we cache them to not require a recomputation of those transactions.
#[derive(Clone)]
pub struct StorageTransactionCache<Transaction, H: Hasher> {
/// Contains the changes for the main and the child storages as one transaction.
pub(crate) transaction: Option<Transaction>,
Expand Down
Loading