lucet_runtime_internals/
error.rs1use crate::instance::{FaultDetails, TerminationDetails};
2use thiserror::Error;
3
4#[derive(Debug, Error)]
6pub enum Error {
7 #[error("Invalid argument: {0}")]
8 InvalidArgument(&'static str),
9
10 #[error("Region capacity reached: {0} instances")]
12 RegionFull(usize),
13
14 #[error("Module error: {0}")]
16 ModuleError(ModuleError),
17
18 #[error("Instance limits exceeded: {0}")]
21 LimitsExceeded(String),
22
23 #[error("No linear memory available: {0}")]
26 NoLinearMemory(String),
27
28 #[error("Symbol not found: {0}")]
30 SymbolNotFound(String),
31
32 #[error("Function not found: (table {0}, func {1}")]
34 FuncNotFound(u32, u32),
35
36 #[error("Runtime fault: {0:?}")]
38 RuntimeFault(FaultDetails),
39
40 #[error("Runtime terminated")]
46 RuntimeTerminated(TerminationDetails),
47
48 #[error("Dynamic loading error: {0}")]
50 DlError(#[from] std::io::Error),
51
52 #[error("Instance not returned")]
53 InstanceNotReturned,
54
55 #[error("Instance not yielded")]
56 InstanceNotYielded,
57
58 #[error("Start function yielded")]
59 StartYielded,
60
61 #[error("Internal error")]
66 InternalError(#[source] anyhow::Error),
67
68 #[error("Unsupported feature: {0}")]
70 Unsupported(String),
71}
72
73impl From<crate::context::Error> for Error {
74 fn from(e: crate::context::Error) -> Error {
75 Error::InternalError(e.into())
76 }
77}
78
79impl From<nix::Error> for Error {
80 fn from(e: nix::Error) -> Error {
81 Error::InternalError(e.into())
82 }
83}
84
85impl From<std::ffi::IntoStringError> for Error {
86 fn from(e: std::ffi::IntoStringError) -> Error {
87 Error::InternalError(e.into())
88 }
89}
90
91impl From<lucet_module::Error> for Error {
92 fn from(e: lucet_module::Error) -> Error {
93 Error::ModuleError(ModuleError::ModuleDataError(e))
94 }
95}
96
97#[derive(Debug, Error)]
99pub enum ModuleError {
100 #[error("Incorrect module definition: {0}")]
102 IncorrectModule(String),
103
104 #[error("Module data error: {0}")]
106 ModuleDataError(#[from] lucet_module::Error),
107}
108
109#[macro_export]
110macro_rules! lucet_bail {
111 ($e:expr) => {
112 return Err(lucet_format_err!($e));
113 };
114 ($fmt:expr, $($arg:tt)*) => {
115 return Err(lucet_format_err!($fmt, $($arg)*));
116 };
117}
118
119#[macro_export(local_inner_macros)]
120macro_rules! lucet_ensure {
121 ($cond:expr, $e:expr) => {
122 if !($cond) {
123 lucet_bail!($e);
124 }
125 };
126 ($cond:expr, $fmt:expr, $($arg:tt)*) => {
127 if !($cond) {
128 lucet_bail!($fmt, $($arg)*);
129 }
130 };
131}
132
133#[macro_export]
134macro_rules! lucet_format_err {
135 ($($arg:tt)*) => { $crate::error::Error::InternalError(anyhow::format_err!($($arg)*)) }
136}
137
138#[macro_export]
139macro_rules! lucet_incorrect_module {
140 ($($arg:tt)*) => {
141 $crate::error::Error::ModuleError(
142 $crate::error::ModuleError::IncorrectModule(format!($($arg)*))
143 )
144 }
145}
146
147#[macro_export]
148macro_rules! bail_limits_exceeded {
149 ($($arg:tt)*) => { return Err($crate::error::Error::LimitsExceeded(format!($($arg)*))); }
150}