wasmi/module/instantiate/
error.rs

1use crate::{
2    errors::{MemoryError, TableError},
3    global::GlobalError,
4    Extern,
5    ExternType,
6    FuncType,
7    Table,
8};
9use core::{fmt, fmt::Display};
10
11/// An error that may occur upon instantiation of a Wasm module.
12#[derive(Debug)]
13pub enum InstantiationError {
14    /// Caused when the number of required imports does not match
15    /// the number of given externals upon module instantiation.
16    ImportsExternalsLenMismatch,
17    /// Caused when a given external value does not match the
18    /// type of the required import for module instantiation.
19    ImportsExternalsMismatch {
20        /// The expected external value for the module import.
21        expected: ExternType,
22        /// The actually found external value for the module import.
23        actual: Extern,
24    },
25    /// Caused when a function has a mismatching signature.
26    SignatureMismatch {
27        /// The expected function signature for the function import.
28        expected: FuncType,
29        /// The actual function signature for the function import.
30        actual: FuncType,
31    },
32    /// Occurs when an imported table does not satisfy the required table type.
33    Table(TableError),
34    /// Occurs when an imported memory does not satisfy the required memory type.
35    Memory(MemoryError),
36    /// Occurs when an imported global variable does not satisfy the required global type.
37    Global(GlobalError),
38    /// Caused when an element segment does not fit into the specified table instance.
39    ElementSegmentDoesNotFit {
40        /// The table of the element segment.
41        table: Table,
42        /// The offset to store the `amount` of elements into the table.
43        offset: u32,
44        /// The amount of elements with which the table is initialized at the `offset`.
45        amount: u32,
46    },
47    /// Caused when the `start` function was unexpectedly found in the instantiated module.
48    FoundStartFn {
49        /// The index of the found `start` function.
50        index: u32,
51    },
52    TooManyInstances,
53}
54
55#[cfg(feature = "std")]
56impl std::error::Error for InstantiationError {}
57
58impl Display for InstantiationError {
59    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60        match self {
61            Self::ImportsExternalsLenMismatch => write!(
62                f,
63                "encountered mismatch between number of given externals and module imports",
64            ),
65            Self::ImportsExternalsMismatch { expected, actual } => write!(
66                f,
67                "expected {expected:?} external for import but found {actual:?}",
68            ),
69            Self::SignatureMismatch { expected, actual } => {
70                write!(
71                    f,
72                    "expected {expected:?} function signature but found {actual:?}",
73                )
74            }
75            Self::ElementSegmentDoesNotFit {
76                table,
77                offset,
78                amount,
79            } => write!(
80                f,
81                "out of bounds table access: {table:?} does not fit {amount} elements starting from offset {offset}",
82            ),
83            Self::FoundStartFn { index } => {
84                write!(f, "found an unexpected start function with index {index}")
85            }
86            Self::Table(error) => Display::fmt(error, f),
87            Self::Memory(error) => Display::fmt(error, f),
88            Self::Global(error) => Display::fmt(error, f),
89            Self::TooManyInstances => write!(f, "too many instances")
90        }
91    }
92}
93
94impl From<TableError> for InstantiationError {
95    fn from(error: TableError) -> Self {
96        Self::Table(error)
97    }
98}
99
100impl From<MemoryError> for InstantiationError {
101    fn from(error: MemoryError) -> Self {
102        Self::Memory(error)
103    }
104}
105
106impl From<GlobalError> for InstantiationError {
107    fn from(error: GlobalError) -> Self {
108        Self::Global(error)
109    }
110}