-
Notifications
You must be signed in to change notification settings - Fork 346
/
errors.rs
485 lines (432 loc) · 13.7 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use std::fmt::{Debug, Display};
use snafu::Snafu;
#[derive(Debug, Snafu)]
#[non_exhaustive]
pub enum VmError {
#[snafu(display("Cache error: {}", msg))]
CacheErr {
msg: String,
backtrace: snafu::Backtrace,
},
#[snafu(display("Error compiling Wasm: {}", msg))]
CompileErr {
msg: String,
backtrace: snafu::Backtrace,
},
#[snafu(display("Couldn't convert from {} to {}. Input: {}", from_type, to_type, input))]
ConversionErr {
from_type: String,
to_type: String,
input: String,
backtrace: snafu::Backtrace,
},
/// Whenever there is no specific error type available
#[snafu(display("Generic error: {}", msg))]
GenericErr {
msg: String,
backtrace: snafu::Backtrace,
},
#[snafu(display("Error instantiating a Wasm module: {}", msg))]
InstantiationErr {
msg: String,
backtrace: snafu::Backtrace,
},
#[snafu(display("Hash doesn't match stored data"))]
IntegrityErr { backtrace: snafu::Backtrace },
#[snafu(display("Iterator with ID {} does not exist", id))]
IteratorDoesNotExist {
id: u32,
backtrace: snafu::Backtrace,
},
#[snafu(display("Error parsing into type {}: {}", target, msg))]
ParseErr {
/// the target type that was attempted
target: String,
msg: String,
backtrace: snafu::Backtrace,
},
#[snafu(display("Error serializing type {}: {}", source, msg))]
SerializeErr {
/// the source type that was attempted
#[snafu(source(false))]
source: String,
msg: String,
backtrace: snafu::Backtrace,
},
#[snafu(display("Error resolving Wasm function: {}", msg))]
ResolveErr {
msg: String,
backtrace: snafu::Backtrace,
},
#[snafu(display("Region length too big. Got {}, limit {}", length, max_length))]
// Note: this only checks length, not capacity
RegionLengthTooBig {
length: usize,
max_length: usize,
backtrace: snafu::Backtrace,
},
#[snafu(display("Region too small. Got {}, required {}", size, required))]
RegionTooSmall {
size: usize,
required: usize,
backtrace: snafu::Backtrace,
},
#[snafu(display("Error executing Wasm: {}", msg))]
RuntimeErr {
msg: String,
backtrace: snafu::Backtrace,
},
#[snafu(display("Error during static Wasm validation: {}", msg))]
StaticValidationErr {
msg: String,
backtrace: snafu::Backtrace,
},
#[snafu(display("Uninitialized Context Data: {}", kind))]
UninitializedContextData {
kind: String,
backtrace: snafu::Backtrace,
},
#[snafu(display("Calling external function through FFI: {}", source))]
FfiErr {
#[snafu(backtrace)]
source: FfiError,
},
#[snafu(display("Ran out of gas during contract execution"))]
GasDepletion,
}
impl From<wasmer_runtime_core::cache::Error> for VmError {
fn from(original: wasmer_runtime_core::cache::Error) -> Self {
make_cache_err(format!("Wasmer cache error: {:?}", original))
}
}
impl From<wasmer_runtime_core::error::CompileError> for VmError {
fn from(original: wasmer_runtime_core::error::CompileError) -> Self {
make_compile_err(format!("Wasmer compile error: {:?}", original))
}
}
impl From<wasmer_runtime_core::error::ResolveError> for VmError {
fn from(original: wasmer_runtime_core::error::ResolveError) -> Self {
make_resolve_err(format!("Wasmer resolve error: {:?}", original))
}
}
impl From<wasmer_runtime_core::error::RuntimeError> for VmError {
fn from(original: wasmer_runtime_core::error::RuntimeError) -> Self {
use wasmer_runtime_core::error::{InvokeError, RuntimeError};
fn runtime_error(err: RuntimeError) -> VmError {
make_runtime_err(format!("Wasmer runtime error: {:?}", err))
}
match original {
// TODO: fix the issue described below:
// `InvokeError::FailedWithNoError` happens when running out of gas in singlepass v0.17
// but it's supposed to indicate bugs in Wasmer...
// https://github.com/wasmerio/wasmer/issues/1452
RuntimeError::InvokeError(InvokeError::FailedWithNoError) => VmError::GasDepletion,
// This variant contains the error we return from imports.
RuntimeError::User(err) => match err.downcast::<VmError>() {
Ok(err) => *err,
Err(err) => runtime_error(RuntimeError::User(err)),
},
_ => runtime_error(original),
}
}
}
pub type VmResult<T> = core::result::Result<T, VmError>;
pub fn make_cache_err<S: Into<String>>(msg: S) -> VmError {
CacheErr { msg: msg.into() }.build()
}
pub fn make_compile_err<S: Into<String>>(msg: S) -> VmError {
CompileErr { msg: msg.into() }.build()
}
pub fn make_conversion_err<S: Into<String>, T: Into<String>, U: Into<String>>(
from_type: S,
to_type: T,
input: U,
) -> VmError {
ConversionErr {
from_type: from_type.into(),
to_type: to_type.into(),
input: input.into(),
}
.build()
}
pub fn make_generic_err<S: Into<String>>(msg: S) -> VmError {
GenericErr { msg: msg.into() }.build()
}
pub fn make_instantiation_err<S: Into<String>>(msg: S) -> VmError {
InstantiationErr { msg: msg.into() }.build()
}
pub fn make_integrity_err() -> VmError {
IntegrityErr {}.build()
}
#[cfg(feature = "iterator")]
pub fn make_iterator_does_not_exist(iterator_id: u32) -> VmError {
IteratorDoesNotExist { id: iterator_id }.build()
}
pub fn make_parse_err<T: Into<String>, M: Display>(target: T, msg: M) -> VmError {
ParseErr {
target: target.into(),
msg: msg.to_string(),
}
.build()
}
pub fn make_serialize_err<S: Into<String>, M: Display>(source: S, msg: M) -> VmError {
SerializeErr {
source: source.into(),
msg: msg.to_string(),
}
.build()
}
pub fn make_resolve_err<S: Into<String>>(msg: S) -> VmError {
ResolveErr { msg: msg.into() }.build()
}
pub fn make_region_length_too_big(length: usize, max_length: usize) -> VmError {
RegionLengthTooBig { length, max_length }.build()
}
pub fn make_region_too_small(size: usize, required: usize) -> VmError {
RegionTooSmall { size, required }.build()
}
pub fn make_runtime_err<S: Into<String>>(msg: S) -> VmError {
RuntimeErr { msg: msg.into() }.build()
}
pub fn make_static_validation_err<S: Into<String>>(msg: S) -> VmError {
StaticValidationErr { msg: msg.into() }.build()
}
pub fn make_uninitialized_context_data<S: Into<String>>(kind: S) -> VmError {
UninitializedContextData { kind: kind.into() }.build()
}
#[derive(Debug, Snafu)]
pub enum FfiError {
#[snafu(display("Panic in FFI call"))]
ForeignPanic { backtrace: snafu::Backtrace },
#[snafu(display("bad argument passed to FFI"))]
BadArgument { backtrace: snafu::Backtrace },
#[snafu(display("Ran out of gas during FFI call"))]
OutOfGas {},
#[snafu(display("Error during FFI call: {}", error))]
Other {
error: String,
backtrace: snafu::Backtrace,
},
}
impl FfiError {
pub fn foreign_panic() -> Self {
ForeignPanic {}.build()
}
pub fn bad_argument() -> Self {
BadArgument {}.build()
}
pub fn out_of_gas() -> Self {
OutOfGas {}.build()
}
pub fn other<S>(error: S) -> Self
where
S: Into<String>,
{
Other {
error: error.into(),
}
.build()
}
pub fn set_message<S>(&mut self, message: S) -> &mut Self
where
S: Into<String>,
{
if let FfiError::Other { error, .. } = self {
*error = message.into()
}
self
}
}
impl From<FfiError> for VmError {
fn from(ffi_error: FfiError) -> Self {
match ffi_error {
FfiError::OutOfGas {} => VmError::GasDepletion,
_ => VmError::FfiErr { source: ffi_error },
}
}
}
pub type FfiResult<T> = core::result::Result<T, FfiError>;
#[cfg(test)]
mod test {
use super::*;
#[test]
fn make_cache_err_works() {
let err = make_cache_err("something went wrong");
match err {
VmError::CacheErr { msg, .. } => assert_eq!(msg, "something went wrong"),
_ => panic!("Unexpected error"),
}
}
#[test]
fn make_compile_err_works() {
let err = make_compile_err("something went wrong");
match err {
VmError::CompileErr { msg, .. } => assert_eq!(msg, "something went wrong"),
_ => panic!("Unexpected error"),
}
}
#[test]
fn make_conversion_err_works() {
let err = make_conversion_err("i32", "u32", "-9");
match err {
VmError::ConversionErr {
from_type,
to_type,
input,
..
} => {
assert_eq!(from_type, "i32");
assert_eq!(to_type, "u32");
assert_eq!(input, "-9");
}
_ => panic!("Unexpected error"),
}
}
#[test]
fn make_generic_err_works() {
let guess = 7;
let error = make_generic_err(format!("{} is too low", guess));
match error {
VmError::GenericErr { msg, .. } => {
assert_eq!(msg, String::from("7 is too low"));
}
e => panic!("unexpected error, {:?}", e),
}
}
#[test]
fn make_instantiation_err_works() {
let err = make_instantiation_err("something went wrong");
match err {
VmError::InstantiationErr { msg, .. } => assert_eq!(msg, "something went wrong"),
_ => panic!("Unexpected error"),
}
}
#[test]
fn make_integrity_err_works() {
let err = make_integrity_err();
match err {
VmError::IntegrityErr { .. } => {}
_ => panic!("Unexpected error"),
}
}
#[test]
#[cfg(feature = "iterator")]
fn make_iterator_does_not_exist_works() {
let err = make_iterator_does_not_exist(15);
match err {
VmError::IteratorDoesNotExist { id, .. } => assert_eq!(id, 15),
_ => panic!("Unexpected error"),
}
}
#[test]
fn make_parse_err_works() {
let error = make_parse_err("Book", "Missing field: title");
match error {
VmError::ParseErr { target, msg, .. } => {
assert_eq!(target, "Book");
assert_eq!(msg, "Missing field: title");
}
_ => panic!("expect different error"),
}
}
#[test]
fn make_serialize_err_works() {
let error = make_serialize_err("Book", "Content too long");
match error {
VmError::SerializeErr { source, msg, .. } => {
assert_eq!(source, "Book");
assert_eq!(msg, "Content too long");
}
_ => panic!("expect different error"),
}
}
#[test]
fn make_resolve_err_works() {
let error = make_resolve_err("function has different signature");
match error {
VmError::ResolveErr { msg, .. } => assert_eq!(msg, "function has different signature"),
_ => panic!("expect different error"),
}
}
#[test]
fn make_region_length_too_big_works() {
let err = make_region_length_too_big(50, 20);
match err {
VmError::RegionLengthTooBig {
length, max_length, ..
} => {
assert_eq!(length, 50);
assert_eq!(max_length, 20);
}
_ => panic!("Unexpected error"),
}
}
#[test]
fn make_region_too_small_works() {
let err = make_region_too_small(12, 33);
match err {
VmError::RegionTooSmall { size, required, .. } => {
assert_eq!(size, 12);
assert_eq!(required, 33);
}
_ => panic!("Unexpected error"),
}
}
#[test]
fn make_runtime_err_works() {
let err = make_runtime_err("something went wrong");
match err {
VmError::RuntimeErr { msg, .. } => assert_eq!(msg, "something went wrong"),
_ => panic!("Unexpected error"),
}
}
#[test]
fn make_static_validation_err_works() {
let error = make_static_validation_err("export xy missing");
match error {
VmError::StaticValidationErr { msg, .. } => assert_eq!(msg, "export xy missing"),
_ => panic!("expect different error"),
}
}
#[test]
fn make_uninitialized_context_data_works() {
let err = make_uninitialized_context_data("foo");
match err {
VmError::UninitializedContextData { kind, .. } => assert_eq!(kind, "foo"),
_ => panic!("Unexpected error"),
}
}
// FfiError constructors
#[test]
fn ffi_error_foreign_panic() {
let err = FfiError::foreign_panic();
match err {
FfiError::ForeignPanic { .. } => {}
_ => panic!("Unexpected error"),
}
}
#[test]
fn ffi_error_bad_argument() {
let err = FfiError::bad_argument();
match err {
FfiError::BadArgument { .. } => {}
_ => panic!("Unexpected error"),
}
}
#[test]
fn ffi_error_out_of_gas() {
let err = FfiError::out_of_gas();
match err {
FfiError::OutOfGas { .. } => {}
_ => panic!("Unexpected error"),
}
}
#[test]
fn ffi_error_other() {
let err = FfiError::other("broken");
match err {
FfiError::Other { error, .. } => assert_eq!(error, "broken"),
_ => panic!("Unexpected error"),
}
}
}