bobcat_panic/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2
3#[cfg(feature = "panic-revert")]
4use bobcat_entry::{write_result_slice, U};
5
6#[cfg(feature = "alloc")]
7extern crate alloc;
8
9#[cfg(feature = "panic-revert")]
10use alloc::vec::Vec;
11
12#[cfg(feature = "panic-revert")]
13use core::fmt::{Result as FmtResult, Write};
14
15#[cfg(target_arch = "wasm32")]
16mod wasm {
17    #[link(wasm_import_module = "console")]
18    #[cfg(feature = "console")]
19    unsafe extern "C" {
20        pub(crate) fn log_txt(ptr: *const u8, len: usize);
21    }
22
23    #[link(wasm_import_module = "vm_hooks")]
24    #[allow(unused)]
25    unsafe extern "C" {
26        pub(crate) fn exit_early(code: i32) -> !;
27    }
28}
29
30#[cfg(feature = "panic-revert")]
31const ERROR_PREAMBLE: [u8; 32 + 4] = match const_hex::const_decode_to_array::<{ 32 + 4 }>(
32    b"08c379a00000000000000000000000000000000000000000000000000000000000000020",
33) {
34    Ok(v) => v,
35    Err(_) => panic!(),
36};
37
38#[cfg(feature = "panic-revert")]
39struct VecWriter<'a>(&'a mut Vec<u8>);
40
41#[cfg(feature = "panic-revert")]
42impl<'a> Write for VecWriter<'a> {
43    fn write_str(&mut self, s: &str) -> FmtResult {
44        self.0.extend_from_slice(s.as_bytes());
45        Ok(())
46    }
47}
48
49#[cfg(all(not(feature = "std"), target_arch = "wasm32"))]
50#[panic_handler]
51pub fn panic_handler(_msg: &core::panic::PanicInfo) -> ! {
52    #[cfg(feature = "console")]
53    {
54        let msg = alloc::format!("{_msg}");
55        unsafe { wasm::log_txt(msg.as_ptr(), msg.len()) }
56    }
57    #[cfg(feature = "panic-revert")]
58    {
59        let mut d = ERROR_PREAMBLE.to_vec();
60        let mut b = Vec::new();
61        write!(VecWriter(&mut b), "{_msg}").unwrap();
62        let l = b.len();
63        let p = (32 - (l % 32)) % 32;
64        d.extend_from_slice(&U::from(l).0);
65        d.append(&mut b);
66        d.resize(d.len() + p, 0);
67        write_result_slice(&d);
68        unsafe { wasm::exit_early(1) }
69    }
70    // Prefer the normal behaviour if the user hasn't opted into this
71    // feature. Maybe it's better to wipe out the revertdata if this happens,
72    // the other behaviour is different.
73    #[allow(unreachable_code)]
74    core::arch::wasm32::unreachable()
75}