-
Notifications
You must be signed in to change notification settings - Fork 296
/
errors.rs
218 lines (209 loc) · 8.12 KB
/
errors.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use serde::{Deserialize, Serialize};
/// Represents an error that can happen when parsing or encoding a Wasm module
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct WasmError(String);
impl WasmError {
/// Creates a new `WasmError` out of an error message.
pub fn new(error_message: String) -> Self {
Self(error_message)
}
}
impl std::fmt::Display for WasmError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
/// Different errors that be returned by `validate_wasm_binary`
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum WasmValidationError {
/// Failure in deserialization of the wasm module.
WasmDeserializeError(WasmError),
/// wasmtime::Module::validate() failed
WasmtimeValidation(String),
/// Failed to decode the canister module.
DecodingError(String),
/// Module contains an invalid function signature
InvalidFunctionSignature(String),
/// Module contains an invalid import section
InvalidImportSection(String),
/// Module contains an invalid export section
InvalidExportSection(String),
/// Module contains an invalid data section
InvalidDataSection(String),
/// Module contains an invalid custom section
InvalidCustomSection(String),
/// Module contains an invalid global section
InvalidGlobalSection(String),
/// Module contains too many globals.
TooManyGlobals { defined: usize, allowed: usize },
/// Module contains too many functions.
TooManyFunctions { defined: usize, allowed: usize },
/// Module contains too many custom sections.
TooManyCustomSections { defined: usize, allowed: usize },
/// A function was too complex.
FunctionComplexityTooHigh {
index: usize,
complexity: usize,
allowed: usize,
},
/// A function was too large.
FunctionTooLarge {
index: usize,
size: usize,
allowed: usize,
},
/// The code section is too large.
CodeSectionTooLarge { size: u32, allowed: u32 },
}
impl std::fmt::Display for WasmValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::WasmDeserializeError(err) => {
write!(f, "Failed to deserialize wasm module with {}", err)
}
Self::WasmtimeValidation(err) => {
write!(f, "Wasmtime failed to validate wasm module {}", err)
}
Self::DecodingError(err) => {
write!(f, "Failed to decode wasm module: {}", err)
}
Self::InvalidFunctionSignature(err) => {
write!(f, "Wasm module has an invalid function signature. {}", err)
}
Self::InvalidImportSection(err) => {
write!(f, "Wasm module has an invalid import section. {}", err)
}
Self::InvalidExportSection(err) => {
write!(f, "Wasm module has an invalid export section. {}", err)
}
Self::InvalidDataSection(err) => {
write!(f, "Wasm module has an invalid data section. {}", err)
}
Self::InvalidCustomSection(err) => {
write!(f, "Wasm module has an invalid custom section. {}", err)
},
Self::InvalidGlobalSection(err) => {
write!(f, "Wasm module has an invalid global section. {}", err)
}
Self::TooManyGlobals { defined, allowed } => write!(
f,
"Wasm module defined {} globals which exceeds the maximum number allowed {}.",
defined, allowed
),
Self::TooManyFunctions { defined, allowed } => write!(
f,
"Wasm module defined {} functions which exceeds the maximum number allowed {}.",
defined, allowed
),
Self::TooManyCustomSections { defined, allowed } => write!(
f,
"Wasm module defined {} custom sections which exceeds the maximum number allowed {}.",
defined, allowed
),
Self::FunctionComplexityTooHigh{ index, complexity, allowed } => write!(
f,
"Wasm module contains a function at index {} with complexity {} which exceeds the maximum complexity allowed {}",
index, complexity, allowed
),
Self::FunctionTooLarge{index, size, allowed} => write!(
f,
"Wasm module contains a function at index {} of size {} that exceeds the maximum allowed size of {}",
index, size, allowed,
),
Self::CodeSectionTooLarge{size, allowed} => write!(
f,
"Wasm model code section size of {} exceeds the maximum allowed size of {}",
size, allowed,
),
}
}
}
/// Different errors that can be returned by `instrument`
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum WasmInstrumentationError {
/// Failure in deserialization the wasm module
WasmDeserializeError(WasmError),
/// Failure in serialization the wasm module
WasmSerializeError(WasmError),
/// Incorrect number of memory sections
IncorrectNumberMemorySections {
expected: usize,
got: usize,
},
InvalidDataSegment {
offset: usize,
len: usize,
},
InvalidExport(String),
InvalidFunctionType(String),
}
impl std::fmt::Display for WasmInstrumentationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::WasmDeserializeError(err) => {
write!(f, "Failed to deserialize wasm module with {}", err)
}
Self::WasmSerializeError(err) => {
write!(f, "Failed to serialize wasm module with {}", err)
}
Self::IncorrectNumberMemorySections { expected, got } => write!(
f,
"Wasm module has {} memory sections but should have had {}",
got, expected
),
Self::InvalidDataSegment { offset, len } => write!(
f,
"Wasm module has invalid data segment of {} bytes at {}",
len, offset
),
Self::InvalidExport(err) => write!(f, "Failed to export: {}", err),
Self::InvalidFunctionType(err) => write!(f, "Invalid function type: {}", err),
}
}
}
/// Different errors that be returned by the Wasm engine
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum WasmEngineError {
FailedToInitializeEngine,
FailedToInstantiateModule(String),
FailedToSetAsyncStack,
FailedToSetWasmStack,
FailedToSerializeModule(String),
FailedToDeserializeModule(String),
FailedToApplySystemChanges(String),
Other(String),
Unexpected(String),
}
impl std::fmt::Display for WasmEngineError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::FailedToInitializeEngine => {
write!(f, "Failed to initialize engine")
}
Self::FailedToInstantiateModule(s) => {
write!(f, "Failed to instantiate module: {}", s)
}
Self::FailedToSetWasmStack => {
write!(f, "Failed to set Wasm stack")
}
Self::FailedToSetAsyncStack => {
write!(f, "Failed to set async stack")
}
Self::FailedToSerializeModule(s) => {
write!(f, "Failed to serialize module: {}", s)
}
Self::FailedToDeserializeModule(s) => {
write!(f, "Failed to deserialize module: {}", s)
}
Self::FailedToApplySystemChanges(s) => {
write!(f, "Failed to apply system changes: {}", s)
}
Self::Other(s) => {
write!(f, "WasmEngineError: {}", s)
}
Self::Unexpected(s) => {
write!(f, "Unexpected WasmEngineError: {}", s)
}
}
}
}