virtual_machine/
error.rs

1//! Error types and result handling for the Virtual Machine module.
2//!
3//! This module defines comprehensive error types that can occur during WASM module
4//! loading, compilation, instantiation, and execution.
5
6use core::fmt::Display;
7
8use alloc::string::String;
9use wamr_rust_sdk::RuntimeError;
10
11/// Result type alias for Virtual Machine operations
12pub type Result<T> = core::result::Result<T, Error>;
13
14/// Comprehensive error types for Virtual Machine operations
15///
16/// This enum covers all possible error conditions that can occur during
17/// WASM module lifecycle operations, from loading to execution.
18#[derive(Debug)]
19#[repr(C)]
20pub enum Error {
21    /// Invalid pointer provided to a function
22    InvalidPointer,
23
24    /// String contains invalid UTF-8 sequences
25    InvalidUtf8String,
26
27    /// Failed to convert between slice types
28    SliceConversionFailed(shared::Error),
29
30    /// Requested functionality is not yet implemented
31    NotImplemented,
32
33    /// WASM runtime initialization failed
34    InitializationFailure,
35
36    /// WASM module compilation failed with detailed error message
37    CompilationError(String),
38
39    /// WASM module instantiation failed with detailed error message
40    InstantiationFailure(String),
41
42    /// WASM function execution failed with detailed error message
43    ExecutionError(String),
44
45    /// Requested function was not found in the module
46    FunctionNotFound,
47
48    /// Memory allocation failed
49    AllocationFailure,
50
51    /// Failed to retrieve task information
52    FailedToGetTaskInformations(task::Error),
53
54    /// Mutex or lock was poisoned
55    PoisonedLock,
56
57    /// Invalid WASM module format or structure
58    InvalidModule,
59
60    /// Internal runtime error
61    InternalError,
62
63    /// Invalid thread identifier provided
64    InvalidThreadIdentifier,
65
66    /// Time-related operation failed
67    Time(time::Error),
68
69    /// Failed to transfert file identifiers
70    FailedToRegisterFileContext,
71}
72
73impl Display for Error {
74    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
75        write!(formatter, "{:?}", self)
76    }
77}
78
79impl core::error::Error for Error {}
80
81impl From<RuntimeError> for Error {
82    fn from(error: RuntimeError) -> Self {
83        match error {
84            RuntimeError::NotImplemented => Error::NotImplemented,
85            RuntimeError::InitializationFailure => Error::InitializationFailure,
86            RuntimeError::WasmFileFSError(_) => Error::InitializationFailure,
87            RuntimeError::CompilationError(e) => Error::CompilationError(e),
88            RuntimeError::InstantiationFailure(e) => Error::InstantiationFailure(e),
89            RuntimeError::ExecutionError(e) => Error::ExecutionError(e.message),
90            RuntimeError::FunctionNotFound => Error::FunctionNotFound,
91        }
92    }
93}
94impl From<task::Error> for Error {
95    fn from(error: task::Error) -> Self {
96        Error::FailedToGetTaskInformations(error)
97    }
98}