linera_wasmer_vm/trap/trap.rs
1use backtrace::Backtrace;
2use std::error::Error;
3use std::fmt;
4use wasmer_types::TrapCode;
5
6/// Stores trace message with backtrace.
7#[derive(Debug)]
8pub enum Trap {
9 /// A user-raised trap through `raise_user_trap`.
10 User(Box<dyn Error + Send + Sync>),
11
12 /// A trap raised from the Wasm generated code
13 ///
14 /// Note: this trap is deterministic (assuming a deterministic host implementation)
15 Wasm {
16 /// The program counter in generated code where this trap happened.
17 pc: usize,
18 /// Native stack backtrace at the time the trap occurred
19 backtrace: Backtrace,
20 /// Optional trapcode associated to the signal that caused the trap
21 signal_trap: Option<TrapCode>,
22 },
23
24 /// A trap raised from a wasm libcall
25 ///
26 /// Note: this trap is deterministic (assuming a deterministic host implementation)
27 Lib {
28 /// Code of the trap.
29 trap_code: TrapCode,
30 /// Native stack backtrace at the time the trap occurred
31 backtrace: Backtrace,
32 },
33
34 /// A trap indicating that the runtime was unable to allocate sufficient memory.
35 ///
36 /// Note: this trap is nondeterministic, since it depends on the host system.
37 OOM {
38 /// Native stack backtrace at the time the OOM occurred
39 backtrace: Backtrace,
40 },
41}
42
43fn _assert_trap_is_sync_and_send(t: &Trap) -> (&dyn Sync, &dyn Send) {
44 (t, t)
45}
46
47impl Trap {
48 /// Construct a new Error with the given a user error.
49 ///
50 /// Internally saves a backtrace when constructed.
51 pub fn user(err: Box<dyn Error + Send + Sync>) -> Self {
52 Self::User(err)
53 }
54
55 /// Construct a new Wasm trap with the given source location and backtrace.
56 ///
57 /// Internally saves a backtrace when constructed.
58 pub fn wasm(pc: usize, backtrace: Backtrace, signal_trap: Option<TrapCode>) -> Self {
59 Self::Wasm {
60 pc,
61 backtrace,
62 signal_trap,
63 }
64 }
65
66 /// Returns trap code, if it's a Trap
67 pub fn to_trap(self) -> Option<TrapCode> {
68 unimplemented!()
69 }
70
71 /// Construct a new Wasm trap with the given trap code.
72 pub fn lib(trap_code: TrapCode) -> Self {
73 let backtrace = Backtrace::from(vec![]);
74 Self::Lib {
75 trap_code,
76 backtrace,
77 }
78 }
79
80 /// Construct a new OOM trap with the given source location and trap code.
81 pub fn oom() -> Self {
82 let backtrace = Backtrace::from(vec![]);
83 Self::OOM { backtrace }
84 }
85
86 /// Attempts to downcast the `Trap` to a concrete type.
87 pub fn downcast<T: Error + 'static>(self) -> Result<T, Self> {
88 match self {
89 // We only try to downcast user errors
90 Self::User(err) if err.is::<T>() => Ok(*err.downcast::<T>().unwrap()),
91 _ => Err(self),
92 }
93 }
94
95 /// Attempts to downcast the `Trap` to a concrete type.
96 pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
97 match &self {
98 // We only try to downcast user errors
99 Self::User(err) if err.is::<T>() => err.downcast_ref::<T>(),
100 _ => None,
101 }
102 }
103
104 /// Returns true if the `Trap` is the same as T
105 pub fn is<T: Error + 'static>(&self) -> bool {
106 match self {
107 Self::User(err) => err.is::<T>(),
108 _ => false,
109 }
110 }
111}
112
113impl std::error::Error for Trap {
114 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
115 match &self {
116 Self::User(err) => Some(&**err),
117 _ => None,
118 }
119 }
120}
121
122impl fmt::Display for Trap {
123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 match self {
125 Self::User(e) => write!(f, "{}", e),
126 Self::Lib { .. } => write!(f, "lib"),
127 Self::Wasm { .. } => write!(f, "wasm"),
128 Self::OOM { .. } => write!(f, "Wasmer VM out of memory"),
129 }
130 }
131}