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    /// Encountered when trying to instantiate a Wasm module with
15    /// a non-matching number of external imports.
16    InvalidNumberOfImports {
17        /// The number of imports required by the Wasm module definition.
18        required: usize,
19        /// The number of imports given by the faulty Wasm module instantiation.
20        given: usize,
21    },
22    /// Caused when a given external value does not match the
23    /// type of the required import for module instantiation.
24    ImportsExternalsMismatch {
25        /// The expected external value for the module import.
26        expected: ExternType,
27        /// The actually found external value for the module import.
28        actual: Extern,
29    },
30    /// Caused when a function has a mismatching signature.
31    SignatureMismatch {
32        /// The expected function signature for the function import.
33        expected: FuncType,
34        /// The actual function signature for the function import.
35        actual: FuncType,
36    },
37    /// Occurs when an imported table does not satisfy the required table type.
38    Table(TableError),
39    /// Occurs when an imported memory does not satisfy the required memory type.
40    Memory(MemoryError),
41    /// Occurs when an imported global variable does not satisfy the required global type.
42    Global(GlobalError),
43    /// Caused when an element segment does not fit into the specified table instance.
44    ElementSegmentDoesNotFit {
45        /// The table of the element segment.
46        table: Table,
47        /// The offset to store the `amount` of elements into the table.
48        table_index: u64,
49        /// The amount of elements with which the table is initialized at the `offset`.
50        len: u32,
51    },
52    /// Caused when the `start` function was unexpectedly found in the instantiated module.
53    FoundStartFn {
54        /// The index of the found `start` function.
55        index: u32,
56    },
57    TooManyInstances,
58}
59
60#[cfg(feature = "std")]
61impl std::error::Error for InstantiationError {}
62
63impl Display for InstantiationError {
64    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65        match self {
66            Self::InvalidNumberOfImports { required, given } => write!(
67                f,
68                "invalid number of imports: required = {required}, given = {given}",
69            ),
70            Self::ImportsExternalsMismatch { expected, actual } => write!(
71                f,
72                "expected {expected:?} external for import but found {actual:?}",
73            ),
74            Self::SignatureMismatch { expected, actual } => {
75                write!(
76                    f,
77                    "expected {expected:?} function signature but found {actual:?}",
78                )
79            }
80            Self::ElementSegmentDoesNotFit {
81                table,
82                table_index: offset,
83                len: amount,
84            } => write!(
85                f,
86                "out of bounds table access: {table:?} does not fit {amount} elements starting from offset {offset}",
87            ),
88            Self::FoundStartFn { index } => {
89                write!(f, "found an unexpected start function with index {index}")
90            }
91            Self::Table(error) => Display::fmt(error, f),
92            Self::Memory(error) => Display::fmt(error, f),
93            Self::Global(error) => Display::fmt(error, f),
94            Self::TooManyInstances => write!(f, "too many instances")
95        }
96    }
97}
98
99impl From<TableError> for InstantiationError {
100    fn from(error: TableError) -> Self {
101        Self::Table(error)
102    }
103}
104
105impl From<MemoryError> for InstantiationError {
106    fn from(error: MemoryError) -> Self {
107        Self::Memory(error)
108    }
109}
110
111impl From<GlobalError> for InstantiationError {
112    fn from(error: GlobalError) -> Self {
113        Self::Global(error)
114    }
115}