tezos_smart_rollup_panic_hook/
lib.rs

1// SPDX-FileCopyrightText: 2022-2023 TriliTech <contact@trili.tech>
2// SPDX-FileCopyrightText: 2023 Marigold <contact@marigold.dev>
3// SPDX-FileCopyrightText: 2023 Functori <contact@functori.com>
4//
5// SPDX-License-Identifier: MIT
6
7#![deny(missing_docs)]
8#![deny(rustdoc::broken_intra_doc_links)]
9
10//! Hook to capture kernel panics, and write them to the debug log.
11//!
12//! The hook will abort execution once the panic has been printed to the
13//! debug log.
14
15// Don't depend on standard library by default.
16#![no_std]
17
18// If 'std' is on, pull in the standard library.
19#[cfg(feature = "std")]
20extern crate std;
21
22extern crate alloc;
23
24use core::panic::PanicInfo;
25
26/// Prints the panic info to the host's *debug log*, and then aborts.
27///
28/// When targeting WASM, this will be the *global* panic handler.
29#[allow(unused)]
30pub fn panic_handler(info: &PanicInfo) {
31    #[cfg(feature = "debug")]
32    {
33        let message = if let Some(message) =
34            info.payload().downcast_ref::<alloc::string::String>()
35        {
36            alloc::format!("Kernel panic {:?} at {:?}", message, info.location())
37        } else {
38            let message = info.payload().downcast_ref::<&str>();
39            alloc::format!("Kernel panic {:?} at {:?}", message, info.location())
40        };
41
42        #[cfg(any(target_arch = "wasm32", target_arch = "riscv64"))]
43        unsafe {
44            tezos_smart_rollup_core::smart_rollup_core::write_debug(
45                message.as_ptr(),
46                message.len(),
47            );
48        }
49
50        #[cfg(all(feature = "std", not(target_arch = "wasm32")))]
51        std::eprintln!("{}", message);
52    }
53
54    // If we're testing, we want to be able to see the panic trace
55    #[cfg(all(feature = "abort", target_arch = "wasm32"))]
56    std::process::abort()
57}