Skip to main content

near_vm_engine/trap/
error.rs

1use super::frame_info::{FRAME_INFO, FrameInfo, GlobalFrameInfo};
2use backtrace::Backtrace;
3use near_vm_vm::{Trap, TrapCode, raise_user_trap};
4use std::error::Error;
5use std::fmt;
6use std::sync::Arc;
7
8/// A struct representing an aborted instruction execution, with a message
9/// indicating the cause.
10#[derive(Clone)]
11pub struct RuntimeError {
12    inner: Arc<RuntimeErrorInner>,
13}
14
15/// The source of the `RuntimeError`.
16#[derive(Debug)]
17enum RuntimeErrorSource {
18    Generic(String),
19    OOM,
20    User(Box<dyn Error + Send + Sync>),
21    Trap(TrapCode),
22}
23
24impl fmt::Display for RuntimeErrorSource {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Self::Generic(s) => write!(f, "{}", s),
28            Self::User(s) => write!(f, "{}", s),
29            Self::OOM => write!(f, "Wasmer VM out of memory"),
30            Self::Trap(s) => write!(f, "{}", s.message()),
31        }
32    }
33}
34
35struct RuntimeErrorInner {
36    /// The source error (this can be a custom user `Error` or a [`TrapCode`])
37    source: RuntimeErrorSource,
38    /// The reconstructed Wasm trace (from the native trace and the `GlobalFrameInfo`).
39    wasm_trace: Vec<FrameInfo>,
40    /// The native backtrace
41    native_trace: Backtrace,
42}
43
44fn _assert_trap_is_sync_and_send(t: &Trap) -> (&dyn Sync, &dyn Send) {
45    (t, t)
46}
47
48impl RuntimeError {
49    /// Creates a new generic `RuntimeError` with the given `message`.
50    ///
51    /// # Example
52    /// ```
53    /// let trap = near_vm_engine::RuntimeError::new("unexpected error");
54    /// assert_eq!("unexpected error", trap.message());
55    /// ```
56    pub fn new<I: Into<String>>(message: I) -> Self {
57        let info = FRAME_INFO.read();
58        let msg = message.into();
59        Self::new_with_trace(
60            &info,
61            None,
62            RuntimeErrorSource::Generic(msg),
63            Backtrace::new_unresolved(),
64        )
65    }
66
67    /// Create a new RuntimeError from a Trap.
68    pub fn from_trap(trap: Trap) -> Self {
69        let info = FRAME_INFO.read();
70        match trap {
71            // A user error
72            Trap::User(error) => {
73                match error.downcast::<Self>() {
74                    // The error is already a RuntimeError, we return it directly
75                    Ok(runtime_error) => *runtime_error,
76                    Err(e) => Self::new_with_trace(
77                        &info,
78                        None,
79                        RuntimeErrorSource::User(e),
80                        Backtrace::new_unresolved(),
81                    ),
82                }
83            }
84            // A trap caused by the VM being Out of Memory
85            Trap::OOM { backtrace } => {
86                Self::new_with_trace(&info, None, RuntimeErrorSource::OOM, backtrace)
87            }
88            // A trap caused by an error on the generated machine code for a Wasm function
89            Trap::Wasm { pc, signal_trap, backtrace } => {
90                let code = info
91                    .lookup_trap_info(pc)
92                    .map_or(signal_trap.unwrap_or(TrapCode::StackOverflow), |info| info.trap_code);
93                Self::new_with_trace(&info, Some(pc), RuntimeErrorSource::Trap(code), backtrace)
94            }
95            // A trap triggered manually from the Wasmer runtime
96            Trap::Lib { trap_code, backtrace } => {
97                Self::new_with_trace(&info, None, RuntimeErrorSource::Trap(trap_code), backtrace)
98            }
99        }
100    }
101
102    /// Raises a custom user Error
103    pub fn raise(error: Box<dyn Error + Send + Sync>) -> ! {
104        unsafe { raise_user_trap(error) }
105    }
106
107    fn new_with_trace(
108        info: &GlobalFrameInfo,
109        trap_pc: Option<usize>,
110        source: RuntimeErrorSource,
111        native_trace: Backtrace,
112    ) -> Self {
113        let wasm_trace = native_trace
114            .frames()
115            .iter()
116            .filter_map(|frame| {
117                let pc = frame.ip() as usize;
118                if pc == 0 {
119                    None
120                } else {
121                    // Note that we need to be careful about the pc we pass in here to
122                    // lookup frame information. This program counter is used to
123                    // translate back to an original source location in the origin wasm
124                    // module. If this pc is the exact pc that the trap happened at,
125                    // then we look up that pc precisely. Otherwise backtrace
126                    // information typically points at the pc *after* the call
127                    // instruction (because otherwise it's likely a call instruction on
128                    // the stack). In that case we want to lookup information for the
129                    // previous instruction (the call instruction) so we subtract one as
130                    // the lookup.
131                    let pc = if Some(pc) == trap_pc { pc } else { pc - 1 };
132                    info.lookup_frame_info(pc)
133                }
134            })
135            .collect();
136
137        Self { inner: Arc::new(RuntimeErrorInner { source, wasm_trace, native_trace }) }
138    }
139
140    /// Returns a reference the `message` stored in `Trap`.
141    pub fn message(&self) -> String {
142        self.inner.source.to_string()
143    }
144
145    /// Returns a list of function frames in WebAssembly code that led to this
146    /// trap happening.
147    pub fn trace(&self) -> &[FrameInfo] {
148        &self.inner.wasm_trace
149    }
150
151    /// Attempts to downcast the `RuntimeError` to a concrete type.
152    pub fn downcast<T: Error + 'static>(self) -> Result<T, Self> {
153        match Arc::try_unwrap(self.inner) {
154            // We only try to downcast user errors
155            Ok(RuntimeErrorInner { source: RuntimeErrorSource::User(err), .. })
156                if err.is::<T>() =>
157            {
158                Ok(*err.downcast::<T>().unwrap())
159            }
160            Ok(inner) => Err(Self { inner: Arc::new(inner) }),
161            Err(inner) => Err(Self { inner }),
162        }
163    }
164
165    /// Returns trap code, if it's a Trap
166    pub fn to_trap(self) -> Option<TrapCode> {
167        if let RuntimeErrorSource::Trap(trap_code) = self.inner.source {
168            Some(trap_code)
169        } else {
170            None
171        }
172    }
173
174    /// Returns true if the `RuntimeError` is the same as T
175    pub fn is<T: Error + 'static>(&self) -> bool {
176        match &self.inner.source {
177            RuntimeErrorSource::User(err) => err.is::<T>(),
178            _ => false,
179        }
180    }
181}
182
183impl fmt::Debug for RuntimeError {
184    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185        f.debug_struct("RuntimeError")
186            .field("source", &self.inner.source)
187            .field("wasm_trace", &self.inner.wasm_trace)
188            .field("native_trace", &self.inner.native_trace)
189            .finish()
190    }
191}
192
193impl fmt::Display for RuntimeError {
194    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
195        write!(f, "RuntimeError: {}", self.message())?;
196        let trace = self.trace();
197        if trace.is_empty() {
198            return Ok(());
199        }
200        for frame in self.trace() {
201            let name = frame.module_name();
202            let func_index = frame.func_index();
203            writeln!(f)?;
204            write!(f, "    at ")?;
205            match frame.function_name() {
206                Some(name) => match rustc_demangle::try_demangle(name) {
207                    Ok(name) => write!(f, "{}", name)?,
208                    Err(_) => write!(f, "{}", name)?,
209                },
210                None => write!(f, "<unnamed>")?,
211            }
212            write!(f, " ({}[{}]:0x{:x})", name, func_index, frame.module_offset())?;
213        }
214        Ok(())
215    }
216}
217
218impl std::error::Error for RuntimeError {
219    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
220        match &self.inner.source {
221            RuntimeErrorSource::User(err) => Some(&**err),
222            RuntimeErrorSource::Trap(err) => Some(err),
223            _ => None,
224        }
225    }
226}
227
228impl From<Trap> for RuntimeError {
229    fn from(trap: Trap) -> Self {
230        Self::from_trap(trap)
231    }
232}