Coverage Report

Created: 2024-10-16 07:58

/src/wasmer/lib/types/src/error.rs
Line
Count
Source (jump to first uncovered line)
1
//! The WebAssembly possible errors
2
use crate::{ExternType, Pages};
3
use std::io;
4
use thiserror::Error;
5
6
/// The Serialize error can occur when serializing a
7
/// compiled Module into a binary.
8
0
#[derive(Error, Debug)]
Unexecuted instantiation: <wasmer_types::error::SerializeError as core::error::Error>::source
Unexecuted instantiation: <wasmer_types::error::SerializeError as core::fmt::Display>::fmt
Unexecuted instantiation: <wasmer_types::error::SerializeError as core::convert::From<std::io::error::Error>>::from
Unexecuted instantiation: <wasmer_types::error::SerializeError as core::error::Error>::source
Unexecuted instantiation: <wasmer_types::error::SerializeError as core::fmt::Display>::fmt
Unexecuted instantiation: <wasmer_types::error::SerializeError as core::convert::From<std::io::error::Error>>::from
9
pub enum SerializeError {
10
    /// An IO error
11
    #[error(transparent)]
12
    Io(#[from] io::Error),
13
    /// A generic serialization error
14
    #[error("{0}")]
15
    Generic(String),
16
}
17
18
/// The Deserialize error can occur when loading a
19
/// compiled Module from a binary.
20
0
#[derive(Error, Debug)]
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::convert::From<std::io::error::Error>>::from
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::convert::From<wasmer_types::error::CompileError>>::from
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::convert::From<std::io::error::Error>>::from
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::error::Error>::source
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::fmt::Display>::fmt
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::convert::From<std::io::error::Error>>::from
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::convert::From<wasmer_types::error::CompileError>>::from
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::convert::From<std::io::error::Error>>::from
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::convert::From<wasmer_types::error::CompileError>>::from
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::convert::From<std::io::error::Error>>::from
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::error::Error>::source
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::fmt::Display>::fmt
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::convert::From<std::io::error::Error>>::from
Unexecuted instantiation: <wasmer_types::error::DeserializeError as core::convert::From<wasmer_types::error::CompileError>>::from
21
pub enum DeserializeError {
22
    /// An IO error
23
    #[error(transparent)]
24
    Io(#[from] io::Error),
25
    /// A generic deserialization error
26
    #[error("{0}")]
27
    Generic(String),
28
    /// Incompatible serialized binary
29
    #[error("incompatible binary: {0}")]
30
    Incompatible(String),
31
    /// The provided binary is corrupted
32
    #[error("corrupted binary: {0}")]
33
    CorruptedBinary(String),
34
    /// The binary was valid, but we got an error when
35
    /// trying to allocate the required resources.
36
    #[error(transparent)]
37
    Compiler(#[from] CompileError),
38
    /// Input artifact bytes have an invalid length
39
    #[error("invalid input bytes: expected {expected} bytes, got {got}")]
40
    InvalidByteLength {
41
        /// How many bytes were expected
42
        expected: usize,
43
        /// How many bytes the artifact contained
44
        got: usize,
45
    },
46
}
47
48
/// Error type describing things that can go wrong when operating on Wasm Memories.
49
0
#[derive(Error, Debug, Clone, PartialEq, Eq, Hash)]
Unexecuted instantiation: <wasmer_types::error::MemoryError as core::fmt::Display>::fmt
Unexecuted instantiation: <wasmer_types::error::MemoryError as core::fmt::Display>::fmt
50
#[non_exhaustive]
51
pub enum MemoryError {
52
    /// Low level error with mmap.
53
    #[error("Error when allocating memory: {0}")]
54
    Region(String),
55
    /// The operation would cause the size of the memory to exceed the maximum or would cause
56
    /// an overflow leading to unindexable memory.
57
    #[error("The memory could not grow: current size {} pages, requested increase: {} pages", current.0, attempted_delta.0)]
58
    CouldNotGrow {
59
        /// The current size in pages.
60
        current: Pages,
61
        /// The attempted amount to grow by in pages.
62
        attempted_delta: Pages,
63
    },
64
    /// Invalid memory was provided.
65
    #[error("The memory is invalid because {}", reason)]
66
    InvalidMemory {
67
        /// The reason why the provided memory is invalid.
68
        reason: String,
69
    },
70
    /// Caller asked for more minimum memory than we can give them.
71
    #[error("The minimum requested ({} pages) memory is greater than the maximum allowed memory ({} pages)", min_requested.0, max_allowed.0)]
72
    MinimumMemoryTooLarge {
73
        /// The number of pages requested as the minimum amount of memory.
74
        min_requested: Pages,
75
        /// The maximum amount of memory we can allocate.
76
        max_allowed: Pages,
77
    },
78
    /// Caller asked for a maximum memory greater than we can give them.
79
    #[error("The maximum requested memory ({} pages) is greater than the maximum allowed memory ({} pages)", max_requested.0, max_allowed.0)]
80
    MaximumMemoryTooLarge {
81
        /// The number of pages requested as the maximum amount of memory.
82
        max_requested: Pages,
83
        /// The number of pages requested as the maximum amount of memory.
84
        max_allowed: Pages,
85
    },
86
    /// Returned when a shared memory is required, but the given memory is not shared.
87
    #[error("The memory is not shared")]
88
    MemoryNotShared,
89
    /// Returned when trying to call a memory operation that is not supported by
90
    /// the particular memory implementation.
91
    #[error("tried to call an unsupported memory operation: {message}")]
92
    UnsupportedOperation {
93
        /// Message describing the unsupported operation.
94
        message: String,
95
    },
96
    /// The memory does not support atomic operations.
97
    #[error("The memory does not support atomic operations")]
98
    AtomicsNotSupported,
99
    /// A user defined error value, used for error cases not listed above.
100
    #[error("A user-defined error occurred: {0}")]
101
    Generic(String),
102
}
103
104
/// An ImportError.
105
///
106
/// Note: this error is not standard to WebAssembly, but it's
107
/// useful to determine the import issue on the API side.
108
0
#[derive(Error, Debug, Clone)]
Unexecuted instantiation: <wasmer_types::error::ImportError as core::fmt::Display>::fmt
Unexecuted instantiation: <wasmer_types::error::ImportError as core::fmt::Display>::fmt
109
pub enum ImportError {
110
    /// Incompatible Import Type.
111
    /// This error occurs when the import types mismatch.
112
    #[error("incompatible import type. Expected {0:?} but received {1:?}")]
113
    IncompatibleType(ExternType, ExternType),
114
115
    /// Unknown Import.
116
    /// This error occurs when an import was expected but not provided.
117
    #[error("unknown import. Expected {0:?}")]
118
    UnknownImport(ExternType),
119
120
    /// Memory Error
121
    #[error("memory error. {0}")]
122
    MemoryError(String),
123
}
124
125
/// An error while preinstantiating a module.
126
///
127
0
#[derive(Error, Debug)]
Unexecuted instantiation: <wasmer_types::error::PreInstantiationError as core::fmt::Display>::fmt
Unexecuted instantiation: <wasmer_types::error::PreInstantiationError as core::fmt::Display>::fmt
128
pub enum PreInstantiationError {
129
    /// The module was compiled with a CPU feature that is not available on
130
    /// the current host.
131
    #[error("module compiled with CPU feature that is missing from host")]
132
    CpuFeature(String),
133
}
134
135
use crate::lib::std::string::String;
136
137
// Compilation Errors
138
//
139
// If `std` feature is enable, we can't use `thiserror` until
140
// https://github.com/dtolnay/thiserror/pull/64 is merged.
141
142
/// The WebAssembly.CompileError object indicates an error during
143
/// WebAssembly decoding or validation.
144
///
145
/// This is based on the [Wasm Compile Error][compile-error] API.
146
///
147
/// [compiler-error]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/CompileError
148
#[derive(Debug)]
149
462
#[cfg_attr(feature = "std", derive(Error))]
Unexecuted instantiation: <wasmer_types::error::CompileError as core::fmt::Display>::fmt
<wasmer_types::error::CompileError as core::fmt::Display>::fmt
Line
Count
Source
149
462
#[cfg_attr(feature = "std", derive(Error))]
150
pub enum CompileError {
151
    /// A Wasm translation error occured.
152
    #[cfg_attr(feature = "std", error("WebAssembly translation error: {0}"))]
153
    Wasm(WasmError),
154
155
    /// A compilation error occured.
156
    #[cfg_attr(feature = "std", error("Compilation error: {0}"))]
157
    Codegen(String),
158
159
    /// The module did not pass validation.
160
    #[cfg_attr(feature = "std", error("Validation error: {0}"))]
161
    Validate(String),
162
163
    /// The compiler doesn't support a Wasm feature
164
    #[cfg_attr(feature = "std", error("Feature {0} is not yet supported"))]
165
    UnsupportedFeature(String),
166
167
    /// The compiler cannot compile for the given target.
168
    /// This can refer to the OS, the chipset or any other aspect of the target system.
169
    #[cfg_attr(
170
        feature = "std",
171
        error("The target {0} is not yet supported (see https://docs.wasmer.io/runtime/features)")
172
    )]
173
    UnsupportedTarget(String),
174
175
    /// Insufficient resources available for execution.
176
    #[cfg_attr(feature = "std", error("Insufficient resources: {0}"))]
177
    Resource(String),
178
179
    /// Middleware error occurred.
180
    #[cfg_attr(feature = "std", error("Middleware error: {0}"))]
181
    MiddlewareError(String),
182
}
183
184
impl From<WasmError> for CompileError {
185
0
    fn from(original: WasmError) -> Self {
186
0
        Self::Wasm(original)
187
0
    }
Unexecuted instantiation: <wasmer_types::error::CompileError as core::convert::From<wasmer_types::error::WasmError>>::from
Unexecuted instantiation: <wasmer_types::error::CompileError as core::convert::From<wasmer_types::error::WasmError>>::from
Unexecuted instantiation: <wasmer_types::error::CompileError as core::convert::From<wasmer_types::error::WasmError>>::from
Unexecuted instantiation: <wasmer_types::error::CompileError as core::convert::From<wasmer_types::error::WasmError>>::from
Unexecuted instantiation: <wasmer_types::error::CompileError as core::convert::From<wasmer_types::error::WasmError>>::from
Unexecuted instantiation: <wasmer_types::error::CompileError as core::convert::From<wasmer_types::error::WasmError>>::from
Unexecuted instantiation: <wasmer_types::error::CompileError as core::convert::From<wasmer_types::error::WasmError>>::from
188
}
189
190
/// A error in the middleware.
191
#[derive(Debug)]
192
0
#[cfg_attr(feature = "std", derive(Error))]
Unexecuted instantiation: <wasmer_types::error::MiddlewareError as core::fmt::Display>::fmt
Unexecuted instantiation: <wasmer_types::error::MiddlewareError as core::fmt::Display>::fmt
193
#[cfg_attr(feature = "std", error("Error in middleware {name}: {message}"))]
194
pub struct MiddlewareError {
195
    /// The name of the middleware where the error was created
196
    pub name: String,
197
    /// The error message
198
    pub message: String,
199
}
200
201
impl MiddlewareError {
202
    /// Create a new `MiddlewareError`
203
0
    pub fn new<A: Into<String>, B: Into<String>>(name: A, message: B) -> Self {
204
0
        Self {
205
0
            name: name.into(),
206
0
            message: message.into(),
207
0
        }
208
0
    }
Unexecuted instantiation: <wasmer_types::error::MiddlewareError>::new::<_, _>
Unexecuted instantiation: <wasmer_types::error::MiddlewareError>::new::<_, _>
209
}
210
211
/// A WebAssembly translation error.
212
///
213
/// When a WebAssembly function can't be translated, one of these error codes will be returned
214
/// to describe the failure.
215
#[derive(Debug)]
216
0
#[cfg_attr(feature = "std", derive(Error))]
Unexecuted instantiation: <wasmer_types::error::WasmError as core::fmt::Display>::fmt
Unexecuted instantiation: <wasmer_types::error::WasmError as core::fmt::Display>::fmt
217
pub enum WasmError {
218
    /// The input WebAssembly code is invalid.
219
    ///
220
    /// This error code is used by a WebAssembly translator when it encounters invalid WebAssembly
221
    /// code. This should never happen for validated WebAssembly code.
222
    #[cfg_attr(
223
        feature = "std",
224
        error("Invalid input WebAssembly code at offset {offset}: {message}")
225
    )]
226
    InvalidWebAssembly {
227
        /// A string describing the validation error.
228
        message: String,
229
        /// The bytecode offset where the error occurred.
230
        offset: usize,
231
    },
232
233
    /// A feature used by the WebAssembly code is not supported by the embedding environment.
234
    ///
235
    /// Embedding environments may have their own limitations and feature restrictions.
236
    #[cfg_attr(feature = "std", error("Unsupported feature: {0}"))]
237
    Unsupported(String),
238
239
    /// An implementation limit was exceeded.
240
    #[cfg_attr(feature = "std", error("Implementation limit exceeded"))]
241
    ImplLimitExceeded,
242
243
    /// An error from the middleware error.
244
    #[cfg_attr(feature = "std", error("{0}"))]
245
    Middleware(MiddlewareError),
246
247
    /// A generic error.
248
    #[cfg_attr(feature = "std", error("{0}"))]
249
    Generic(String),
250
}
251
252
impl From<MiddlewareError> for WasmError {
253
0
    fn from(original: MiddlewareError) -> Self {
254
0
        Self::Middleware(original)
255
0
    }
Unexecuted instantiation: <wasmer_types::error::WasmError as core::convert::From<wasmer_types::error::MiddlewareError>>::from
Unexecuted instantiation: <wasmer_types::error::WasmError as core::convert::From<wasmer_types::error::MiddlewareError>>::from
Unexecuted instantiation: <wasmer_types::error::WasmError as core::convert::From<wasmer_types::error::MiddlewareError>>::from
Unexecuted instantiation: <wasmer_types::error::WasmError as core::convert::From<wasmer_types::error::MiddlewareError>>::from
256
}
257
258
/// The error that can happen while parsing a `str`
259
/// to retrieve a [`CpuFeature`](crate::CpuFeature).
260
#[derive(Debug)]
261
0
#[cfg_attr(feature = "std", derive(Error))]
Unexecuted instantiation: <wasmer_types::error::ParseCpuFeatureError as core::fmt::Display>::fmt
Unexecuted instantiation: <wasmer_types::error::ParseCpuFeatureError as core::fmt::Display>::fmt
262
pub enum ParseCpuFeatureError {
263
    /// The provided string feature doesn't exist
264
    #[cfg_attr(feature = "std", error("CpuFeature {0} not recognized"))]
265
    Missing(String),
266
}
267
268
/// A convenient alias for a `Result` that uses `WasmError` as the error type.
269
pub type WasmResult<T> = Result<T, WasmError>;
270
271
#[cfg(test)]
272
mod tests {
273
    use super::*;
274
275
    #[test]
276
    fn middleware_error_can_be_created() {
277
        let msg = String::from("Something went wrong");
278
        let error = MiddlewareError::new("manipulator3000", msg);
279
        assert_eq!(error.name, "manipulator3000");
280
        assert_eq!(error.message, "Something went wrong");
281
    }
282
283
    #[test]
284
    fn middleware_error_be_converted_to_wasm_error() {
285
        let error = WasmError::from(MiddlewareError::new("manipulator3000", "foo"));
286
        match error {
287
            WasmError::Middleware(MiddlewareError { name, message }) => {
288
                assert_eq!(name, "manipulator3000");
289
                assert_eq!(message, "foo");
290
            }
291
            err => panic!("Unexpected error: {:?}", err),
292
        }
293
    }
294
}