1use crate::{Diagnostic, Profile, SourceLocation, SourceSpan};
4use std::{fmt, sync::Arc};
5
6#[derive(Clone)]
11pub struct VmError {
12 pub(crate) kind: VmErrorKind,
13 pub(crate) location: Option<RuntimeLocation>,
14 pub(crate) location_suppressed: bool,
15 pub(crate) diagnostic: Option<Arc<Diagnostic>>,
16 pub(crate) span: Option<(u32, u32)>,
17 core: rustia_vm::Error,
18}
19
20#[derive(Clone, Debug)]
21pub(crate) struct RuntimeLocation {
22 pub(crate) source: Arc<str>,
23 pub(crate) line: u32,
24}
25
26#[derive(Clone, Debug)]
47#[non_exhaustive]
48pub enum VmErrorKind {
49 FuelExhausted {
51 limit: u64,
53 },
54 RecursionLimit {
56 limit: usize,
58 },
59 RegisterOutOfBounds {
61 register: usize,
63 max: usize,
65 },
66 ConstantOutOfBounds {
68 index: usize,
70 len: usize,
72 },
73 TypeError {
75 expected: &'static str,
77 },
78 OutOfMemory,
81 HostDispatcherUnavailable,
83 HostResourceExhausted {
85 resource: &'static str,
87 limit: u64,
89 },
90 HostPanicked {
93 function: String,
95 },
96 VmPoisoned,
99 YieldAcrossBoundary,
101 DecisionResetWhileRunning,
103 HostConfigurationWhileRunning,
105 DecisionEpochExhausted,
107 StaleRetainedValue {
109 retained_epoch: u64,
111 current_epoch: u64,
113 },
114 RetainedValueFromDifferentVm,
116 TableIndexNil,
118 TableIndexNaN,
120 InvalidKeyToNext,
122 AssertionFailed {
124 message: String,
126 },
127 RuntimeError {
129 message: String,
131 },
132 GuestError {
135 message: String,
137 },
138 Unsupported {
140 feature: &'static str,
142 },
143 CapabilityRemoved {
145 feature: &'static str,
147 operation: Option<String>,
149 profile: Profile,
151 },
152 HardenedLeafViolation {
154 feature: &'static str,
156 },
157 GuestEnvironmentSealed,
159 InvalidProgram {
161 reason: &'static str,
163 },
164}
165
166impl VmError {
167 pub fn new(kind: VmErrorKind) -> Self {
173 let mut error = Self::from_core(rustia_vm::Error::new(kind.to_core()));
174 error.kind = kind;
175 error
176 }
177
178 pub fn kind(&self) -> &VmErrorKind {
180 &self.kind
181 }
182
183 pub fn message(&self) -> VmErrorMessage<'_> {
185 VmErrorMessage(self)
186 }
187
188 pub fn source_location(&self) -> Option<SourceLocation<'_>> {
190 if self.location_suppressed {
191 return None;
192 }
193 self.location.as_ref().map(|location| SourceLocation {
194 source: &location.source,
195 line: location.line,
196 })
197 }
198
199 pub fn source_span(&self) -> Option<SourceSpan> {
201 self.span.map(|(start, end)| SourceSpan {
202 start: start as usize,
203 end: end as usize,
204 })
205 }
206
207 pub fn without_source_location(self) -> Self {
211 let mut error = Self::from_core(self.core.without_source_location());
212 error.kind = self.kind;
213 error
214 }
215
216 pub(crate) fn from_core(core: rustia_vm::Error) -> Self {
217 Self {
218 kind: VmErrorKind::from_core(&core.kind),
219 location: core.location.as_ref().map(|location| RuntimeLocation {
220 source: location.source.clone(),
221 line: location.line,
222 }),
223 location_suppressed: core.location_suppressed,
224 diagnostic: core
225 .diagnostic
226 .as_ref()
227 .map(|diagnostic| Arc::new(Diagnostic::from_core(diagnostic))),
228 span: core.span,
229 core,
230 }
231 }
232
233 pub(crate) fn into_core(mut self) -> rustia_vm::Error {
234 if let rustia_vm::ErrorKind::GuestError { message, .. } = &self.core.kind {
235 self.core.kind = rustia_vm::ErrorKind::RuntimeError {
238 message: message.clone(),
239 };
240 }
241 self.core
242 }
243
244 pub(crate) fn is_transaction_abort(&self) -> bool {
245 self.core.is_transaction_abort()
246 }
247}
248
249pub struct VmErrorMessage<'a>(&'a VmError);
251
252impl fmt::Display for VmErrorMessage<'_> {
253 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
254 self.0.core.message().fmt(formatter)
255 }
256}
257
258impl fmt::Debug for VmError {
259 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
260 formatter
261 .debug_struct("VmError")
262 .field("kind", &self.kind)
263 .field("location", &self.source_location())
264 .field("span", &self.source_span())
265 .field("diagnostic", &self.diagnostic)
266 .finish()
267 }
268}
269
270impl fmt::Display for VmError {
271 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
272 self.core.fmt(formatter)
273 }
274}
275
276impl std::error::Error for VmError {
277 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
278 self.diagnostic
279 .as_ref()
280 .and_then(|diagnostic| diagnostic.host_cause.as_deref())
281 .map(|cause| cause as &(dyn std::error::Error + 'static))
282 }
283}
284
285impl VmErrorKind {
286 fn from_core(kind: &rustia_vm::ErrorKind) -> Self {
287 use rustia_vm::ErrorKind as Core;
288 match kind {
289 Core::FuelExhausted { limit } => Self::FuelExhausted { limit: *limit },
290 Core::RecursionLimit { limit } => Self::RecursionLimit { limit: *limit },
291 Core::RegisterOutOfBounds { register, max } => Self::RegisterOutOfBounds {
292 register: *register,
293 max: *max,
294 },
295 Core::ConstantOutOfBounds { index, len } => Self::ConstantOutOfBounds {
296 index: *index,
297 len: *len,
298 },
299 Core::TypeError { expected } => Self::TypeError { expected },
300 Core::OutOfMemory => Self::OutOfMemory,
301 Core::HostDispatcherUnavailable => Self::HostDispatcherUnavailable,
302 Core::HostResourceExhausted { resource, limit } => Self::HostResourceExhausted {
303 resource,
304 limit: *limit,
305 },
306 Core::HostPanicked { function } => Self::HostPanicked {
307 function: function.clone(),
308 },
309 Core::VmPoisoned => Self::VmPoisoned,
310 Core::YieldAcrossBoundary => Self::YieldAcrossBoundary,
311 Core::DecisionResetWhileRunning => Self::DecisionResetWhileRunning,
312 Core::HostConfigurationWhileRunning => Self::HostConfigurationWhileRunning,
313 Core::DecisionEpochExhausted => Self::DecisionEpochExhausted,
314 Core::StaleRetainedValue {
315 retained_epoch,
316 current_epoch,
317 } => Self::StaleRetainedValue {
318 retained_epoch: *retained_epoch,
319 current_epoch: *current_epoch,
320 },
321 Core::RetainedValueFromDifferentVm => Self::RetainedValueFromDifferentVm,
322 Core::TableIndexNil => Self::TableIndexNil,
323 Core::TableIndexNaN => Self::TableIndexNaN,
324 Core::InvalidKeyToNext => Self::InvalidKeyToNext,
325 Core::AssertionFailed { message } => Self::AssertionFailed {
326 message: message.clone(),
327 },
328 Core::RuntimeError { message } => Self::RuntimeError {
329 message: message.clone(),
330 },
331 Core::GuestError { message, .. } => Self::GuestError {
332 message: message.clone(),
333 },
334 Core::Unsupported { feature } => Self::Unsupported { feature },
335 Core::CapabilityRemoved {
336 feature,
337 operation,
338 profile,
339 } => Self::CapabilityRemoved {
340 feature,
341 operation: operation.clone(),
342 profile: Profile::from_core(*profile),
343 },
344 Core::HardenedLeafViolation { feature } => Self::HardenedLeafViolation { feature },
345 Core::GuestEnvironmentSealed => Self::GuestEnvironmentSealed,
346 Core::InvalidProgram { reason } => Self::InvalidProgram { reason },
347 }
348 }
349
350 fn to_core(&self) -> rustia_vm::ErrorKind {
351 use rustia_vm::ErrorKind as Core;
352 match self {
353 Self::FuelExhausted { limit } => Core::FuelExhausted { limit: *limit },
354 Self::RecursionLimit { limit } => Core::RecursionLimit { limit: *limit },
355 Self::RegisterOutOfBounds { register, max } => Core::RegisterOutOfBounds {
356 register: *register,
357 max: *max,
358 },
359 Self::ConstantOutOfBounds { index, len } => Core::ConstantOutOfBounds {
360 index: *index,
361 len: *len,
362 },
363 Self::TypeError { expected } => Core::TypeError { expected },
364 Self::OutOfMemory => Core::OutOfMemory,
365 Self::HostDispatcherUnavailable => Core::HostDispatcherUnavailable,
366 Self::HostResourceExhausted { resource, limit } => Core::HostResourceExhausted {
367 resource,
368 limit: *limit,
369 },
370 Self::HostPanicked { function } => Core::HostPanicked {
371 function: function.clone(),
372 },
373 Self::VmPoisoned => Core::VmPoisoned,
374 Self::YieldAcrossBoundary => Core::YieldAcrossBoundary,
375 Self::DecisionResetWhileRunning => Core::DecisionResetWhileRunning,
376 Self::HostConfigurationWhileRunning => Core::HostConfigurationWhileRunning,
377 Self::DecisionEpochExhausted => Core::DecisionEpochExhausted,
378 Self::StaleRetainedValue {
379 retained_epoch,
380 current_epoch,
381 } => Core::StaleRetainedValue {
382 retained_epoch: *retained_epoch,
383 current_epoch: *current_epoch,
384 },
385 Self::RetainedValueFromDifferentVm => Core::RetainedValueFromDifferentVm,
386 Self::TableIndexNil => Core::TableIndexNil,
387 Self::TableIndexNaN => Core::TableIndexNaN,
388 Self::InvalidKeyToNext => Core::InvalidKeyToNext,
389 Self::AssertionFailed { message } => Core::AssertionFailed {
390 message: message.clone(),
391 },
392 Self::RuntimeError { message } => Core::RuntimeError {
393 message: message.clone(),
394 },
395 Self::GuestError { message } => Core::RuntimeError {
398 message: message.clone(),
399 },
400 Self::Unsupported { feature } => Core::Unsupported { feature },
401 Self::CapabilityRemoved {
402 feature,
403 operation,
404 profile,
405 } => Core::CapabilityRemoved {
406 feature,
407 operation: operation.clone(),
408 profile: profile.to_core(),
409 },
410 Self::HardenedLeafViolation { feature } => Core::HardenedLeafViolation { feature },
411 Self::GuestEnvironmentSealed => Core::GuestEnvironmentSealed,
412 Self::InvalidProgram { reason } => Core::InvalidProgram { reason },
413 }
414 }
415}