Skip to main content

custom_panic_default

Macro custom_panic_default 

Source
macro_rules! custom_panic_default {
    () => { ... };
}
Expand description

Define the default global panic handler.

This must be used if the entrypoint macro is not used, and no other panic handler has been defined; otherwise compilation will fail with a missing custom_panic symbol.

The default global allocator is enabled only if the calling crate has not disabled it using Cargo features as described below. It is only defined for on-chain targets.

§Cargo features

A crate that calls this macro can provide its own custom panic handler, or allow others to provide their own custom panic handler, by adding a custom-panic feature to its Cargo.toml. After enabling the feature, one may define their own panic handler.

A good way to reduce the final size of the program is to provide a custom_panic implementation that does nothing. Doing so will cut ~25kb from a noop program. That number goes down the more the programs pulls in Rust’s standard library for other purposes.

§Defining a panic handler for Solana

The mechanism for defining a Solana panic handler is different from most Rust programs.

To define a panic handler one must define a custom_panic function with the #[no_mangle] attribute, as below:

#[cfg(all(feature = "custom-panic", target_os = "solana"))]
#[no_mangle]
fn custom_panic(info: &core::panic::PanicInfo<'_>) {
    $crate::msg!("{}", info);
}

The above is how Solana defines the default panic handler.