Skip to content

Commit

Permalink
fix(rust): Support new alloc panic handling (#2582)
Browse files Browse the repository at this point in the history
  • Loading branch information
breathx authored Apr 24, 2023
1 parent 4a38918 commit dc86465
Show file tree
Hide file tree
Showing 18 changed files with 279 additions and 294 deletions.
294 changes: 148 additions & 146 deletions Cargo.lock

Large diffs are not rendered by default.

212 changes: 106 additions & 106 deletions Cargo.toml

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion examples/binaries/async-tester/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};

Expand Down
1 change: 0 additions & 1 deletion examples/binaries/exit-handle-sender/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode};
Expand Down
1 change: 0 additions & 1 deletion examples/binaries/exit-handle/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
Expand Down
1 change: 0 additions & 1 deletion examples/binaries/exit-init/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
Expand Down
1 change: 0 additions & 1 deletion examples/binaries/gas-burned/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
Expand Down
1 change: 0 additions & 1 deletion examples/binaries/init-fail-sender/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
Expand Down
1 change: 0 additions & 1 deletion examples/binaries/init-wait-reply-exit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
Expand Down
1 change: 0 additions & 1 deletion examples/binaries/init-wait/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
Expand Down
1 change: 0 additions & 1 deletion examples/binaries/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;
Expand Down
7 changes: 0 additions & 7 deletions examples/ping/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![no_std]
#![feature(alloc_error_handler)]

#[cfg(target_arch = "wasm32")]
extern crate galloc;
Expand All @@ -20,12 +19,6 @@ extern "C" fn handle() {
}
}

#[cfg(target_arch = "wasm32")]
#[alloc_error_handler]
pub fn oom(_: core::alloc::Layout) -> ! {
core::arch::wasm32::unreachable()
}

#[cfg(target_arch = "wasm32")]
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
Expand Down
1 change: 0 additions & 1 deletion gcli/res/messager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), feature(alloc_error_handler))]
#![cfg_attr(not(feature = "std"), feature(const_btree_new))]
#![cfg_attr(not(feature = "std"), no_std)]

Expand Down
7 changes: 0 additions & 7 deletions gcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
//!
//! ```
//! #![no_std]
//! #![feature(alloc_error_handler)]
//!
//! extern crate galloc;
//!
Expand All @@ -49,12 +48,6 @@
//! }
//!
//! # #[cfg(target = "wasm32")]
//! #[alloc_error_handler]
//! pub fn oom(_: core::alloc::Layout) -> ! {
//! core::arch::wasm32::unreachable()
//! }
//!
//! # #[cfg(target = "wasm32")]
//! #[panic_handler]
//! fn panic(_: &core::panic::PanicInfo) -> ! {
//! core::arch::wasm32::unreachable()
Expand Down
8 changes: 1 addition & 7 deletions gcore/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,13 @@ pub mod ext {
///
/// ```rust,ignore
/// #![no_std]
/// #![feature(alloc_error_handler)]
/// #![feature(allocator_api)]
///
/// extern crate alloc;
///
/// use alloc::alloc::{Global, Layout, Allocator};
/// use alloc::alloc::{Global, Allocator};
/// use gcore::ext;
///
/// #[alloc_error_handler]
/// fn oom(_layout: Layout) -> ! {
/// ext::oom_panic()
/// }
///
/// #[no_mangle]
/// extern "C" fn handle() {
/// let layout = Layout::new::<[u8; 64 * 1024]>();
Expand Down
30 changes: 22 additions & 8 deletions gstd/src/common/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@
//! For `debug` mode it provides more extensive logging.
#[cfg(target_arch = "wasm32")]
use {crate::ext, core::alloc::Layout, core::panic::PanicInfo};

#[cfg(target_arch = "wasm32")]
#[alloc_error_handler]
pub fn oom(_: Layout) -> ! {
ext::oom_panic()
}
use {crate::ext, alloc::alloc::AllocErrorPanicPayload, core::panic::PanicInfo};

#[cfg(not(feature = "debug"))]
#[cfg(not(debug_assertions))]
#[cfg(target_arch = "wasm32")]
#[panic_handler]
pub fn panic(_: &PanicInfo) -> ! {
pub fn panic(panic_info: &PanicInfo) -> ! {
// Alloc error handling through panic message.
if panic_info
.payload()
.downcast_ref::<AllocErrorPanicPayload>()
.is_some()
{
ext::oom_panic()
}

// Common panic handling.
ext::panic("no info")
}

Expand All @@ -48,6 +52,16 @@ pub fn panic(_: &PanicInfo) -> ! {
pub fn panic(panic_info: &PanicInfo) -> ! {
use crate::prelude::format;

// Alloc error handling through panic message.
if panic_info
.payload()
.downcast_ref::<AllocErrorPanicPayload>()
.is_some()
{
ext::oom_panic()
}

// Common panic handling.
let msg = match (panic_info.message(), panic_info.location()) {
(Some(msg), Some(loc)) => format!(
"'{:?}', {}:{}:{}",
Expand Down
3 changes: 2 additions & 1 deletion gstd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@
all(target_arch = "wasm32", any(feature = "debug", debug_assertions)),
feature(panic_info_message)
)]
#![cfg_attr(target_arch = "wasm32", feature(alloc_error_handler))]
#![cfg_attr(target_arch = "wasm32", feature(panic_oom_payload))]
#![cfg_attr(feature = "strict", deny(warnings))]
#![doc(html_logo_url = "https://docs.gear.rs/logo.svg")]

extern crate alloc;
#[cfg(target_arch = "wasm32")]
extern crate galloc;

Expand Down
2 changes: 0 additions & 2 deletions gstd/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
//! The `gstd` default prelude. Re-imports default `std` modules and traits.
//! `std` can be safely replaced to `gstd` in the Rust programs.
extern crate alloc;

pub use core::prelude::v1::*;

// Public module re-exports
Expand Down

0 comments on commit dc86465

Please sign in to comment.