Skip to main content

wasmi/module/instantiate/
error.rs

1use crate::{
2    errors::{MemoryError, TableError},
3    Extern,
4    ExternType,
5    FuncType,
6    GlobalType,
7    MemoryType,
8    Table,
9    TableType,
10};
11use core::{
12    error::Error,
13    fmt::{self, Display},
14};
15
16/// An error that may occur upon instantiation of a Wasm module.
17#[derive(Debug)]
18pub enum InstantiationError {
19    /// Encountered when trying to instantiate a Wasm module with
20    /// a non-matching number of external imports.
21    InvalidNumberOfImports {
22        /// The number of imports required by the Wasm module definition.
23        required: usize,
24        /// The number of imports given by the faulty Wasm module instantiation.
25        given: usize,
26    },
27    /// Caused when a given external value does not match the
28    /// type of the required import for module instantiation.
29    ImportsExternalsMismatch {
30        /// The expected external value for the module import.
31        expected: ExternType,
32        /// The actually found external value for the module import.
33        actual: Extern,
34    },
35    /// Returned when a global has a mismatching type.
36    GlobalTypeMismatch {
37        /// The expected global type of the global import.
38        expected: GlobalType,
39        /// The actual global type of the global import.
40        actual: GlobalType,
41    },
42    /// Returned when a function has a mismatching type.
43    FuncTypeMismatch {
44        /// The expected function type of the function import.
45        expected: FuncType,
46        /// The actual function type of the function import.
47        actual: FuncType,
48    },
49    /// Returned when a table has a mismatching type.
50    TableTypeMismatch {
51        /// The expected table type of the table import.
52        expected: TableType,
53        /// The actual table type of the table import.
54        actual: TableType,
55    },
56    /// Returned when a linear memory has a mismatching type.
57    MemoryTypeMismatch {
58        /// The expected memory type of the memory import.
59        expected: MemoryType,
60        /// The actual memory type of the memory import.
61        actual: MemoryType,
62    },
63    /// Caused when an element segment does not fit into the specified table instance.
64    ElementSegmentDoesNotFit {
65        /// The table of the element segment.
66        table: Table,
67        /// The offset to store the `amount` of elements into the table.
68        table_index: u64,
69        /// The amount of elements with which the table is initialized at the `offset`.
70        len: u32,
71    },
72    /// Caused when the `start` function was unexpectedly found in the instantiated module.
73    UnexpectedStartFn {
74        /// The index of the found `start` function.
75        index: u32,
76    },
77    /// When trying to instantiate more instances than supported by Wasmi.
78    TooManyInstances,
79    /// When trying to instantiate more tables than supported by Wasmi.
80    TooManyTables,
81    /// When trying to instantiate more linear memories than supported by Wasmi.
82    TooManyMemories,
83    /// Encountered when failing to instantiate a linear memory.
84    FailedToInstantiateMemory(MemoryError),
85    /// Encountered when failing to instantiate a table.
86    FailedToInstantiateTable(TableError),
87}
88
89impl Error for InstantiationError {}
90
91impl Display for InstantiationError {
92    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93        match self {
94            Self::InvalidNumberOfImports { required, given } => write!(
95                f,
96                "invalid number of imports: required = {required}, given = {given}",
97            ),
98            Self::ImportsExternalsMismatch { expected, actual } => write!(
99                f,
100                "expected {expected:?} external for import but found {actual:?}",
101            ),
102            Self::GlobalTypeMismatch { expected, actual } => write!(f, "imported global type mismatch. expected {expected:?} but found {actual:?}"),
103            Self::FuncTypeMismatch { expected, actual } => write!(f, "imported function type mismatch. expected {expected:?} but found {actual:?}"),
104            Self::TableTypeMismatch { expected, actual } => write!(f, "imported table type mismatch. expected {expected:?} but found {actual:?}"),
105            Self::MemoryTypeMismatch { expected, actual } => write!(f, "imported memory type mismatch. expected {expected:?} but found {actual:?}"),
106            Self::ElementSegmentDoesNotFit {
107                table,
108                table_index: offset,
109                len: amount,
110            } => write!(
111                f,
112                "out of bounds table access: {table:?} does not fit {amount} elements starting from offset {offset}",
113            ),
114            Self::UnexpectedStartFn { index } => {
115                write!(f, "found an unexpected start function with index {index}")
116            }
117            Self::TooManyInstances => write!(f, "tried to instantiate too many instances"),
118            Self::TooManyTables => write!(f, "tried to instantiate too many tables"),
119            Self::TooManyMemories => write!(f, "tried to instantiate too many linear memories"),
120            Self::FailedToInstantiateMemory(error) => write!(f, "failed to instantiate memory: {error}"),
121            Self::FailedToInstantiateTable(error) => write!(f, "failed to instantiate table: {error}"),
122        }
123    }
124}