wasmedge_types/
error.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! Defines WasmEdge error types.

use crate::ExternalInstanceType;
use thiserror::Error;

/// The error types used by both wasmedge-sys and wasmedge crates.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum WasmEdgeError {
    // For general operation error
    #[error("{0}")]
    Operation(String),

    // For user-defined error
    #[error("{0}")]
    User(u32),

    /// Errors raised by WasmEdge Core.
    #[error("{0}")]
    Core(#[from] CoreError),

    #[error("Fail to create ImportObj module")]
    ImportObjCreate,
    #[error("Fail to create Executor context")]
    ExecutorCreate,
    #[error("{0}")]
    Store(#[from] StoreError),
    #[error("Fail to create Statistics context")]
    StatisticsCreate,
    #[error("Fail to create Module")]
    ModuleCreate,
    #[error("Fail to create Loader")]
    LoaderCreate,
    #[error("Fail to create Config")]
    ConfigCreate,
    #[error("Fail to create AOT Compiler")]
    CompilerCreate,
    #[error("Fail to create Validator")]
    ValidatorCreate,
    #[error("{0}")]
    Vm(#[from] VmError),

    // instances
    #[error("{0}")]
    Func(#[from] FuncError),
    #[error("Fail to create FuncType")]
    FuncTypeCreate,
    #[error("Execution Timed Out")]
    ExecuteTimeout,
    #[error("{0}")]
    Mem(#[from] MemError),
    #[error("Fail to create MemType")]
    MemTypeCreate,
    #[error("{0}")]
    Global(#[from] GlobalError),
    #[error("Fail to create GlobalType")]
    GlobalTypeCreate,
    #[error("{0}")]
    Table(#[from] TableError),
    #[error("Fail to create TableType")]
    TableTypeCreate,
    #[error("{0}")]
    Import(#[from] ImportError),
    #[error("{0}")]
    Export(#[from] ExportError),
    #[error("{0}")]
    Instance(#[from] InstanceError),
    #[error("{0}")]
    Plugin(#[from] PluginError),

    // std
    #[error("Found an internal 0 byte")]
    FoundNulByte(#[from] std::ffi::NulError),
    #[error("Fail to find a nul byte in the expected position")]
    NotFoundNulByte(#[from] std::ffi::FromBytesWithNulError),
    #[error("Fail to interpret a sequence of u8 as a string")]
    Utf8(#[from] std::str::Utf8Error),
    #[error("Fail to convert a vector of bytes to a string")]
    FromUtf8(#[from] std::string::FromUtf8Error),

    // Windows platform
    #[error("Fail to convert path on Windows: {0}")]
    WindowsPathConversion(String),
}

/// The error types for WasmEdge Function.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum FuncError {
    #[error("Fail to create Function instance")]
    Create,
    #[error("{0}")]
    CreateBinding(String),
    #[error("Fail to get the function type")]
    Type,
}

/// The error types for WasmEdge Memory.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum MemError {
    #[error("Fail to create Memory instance")]
    Create,
    #[error("Fail to create shared memory type. The 'max' field must not be None.")]
    CreateSharedType,
    #[error("Fail to get the memory type")]
    Type,
    #[error("Fail to get the const pointer to data")]
    ConstPtr,
    #[error("Fail to get the pointer to data")]
    MutPtr,
    #[error("Fail to convert a raw pointer to a reference")]
    Ptr2Ref,
}

/// The error types for WasmEdge Global.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum GlobalError {
    #[error("Fail to create Global instance")]
    Create,
    #[error("Fail to get the global type")]
    Type,
    #[error("Trying to set value to a const global variable")]
    ModifyConst,
    #[error("")]
    UnmatchedValType,
}

/// The error types for WasmEdge Table.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum TableError {
    #[error("Fail to create Table instance")]
    Create,
    #[error("Fail to get the table type")]
    Type,
}

/// The error types for WasmEdge ImportType.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum ImportError {
    #[error("The expected Import type is {expected:?}, but found {actual:?}")]
    Type {
        expected: ExternalInstanceType,
        actual: ExternalInstanceType,
    },
    #[error("{0}")]
    FuncType(String),
    #[error("{0}")]
    TableType(String),
    #[error("{0}")]
    MemType(String),
    #[error("{0}")]
    GlobalType(String),
}

/// The error types for WasmEdge ExportType.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum ExportError {
    #[error("The expected Export type is {expected:?}, but found {actual:?}")]
    Type {
        expected: ExternalInstanceType,
        actual: ExternalInstanceType,
    },
    #[error("{0}")]
    FuncType(String),
    #[error("{0}")]
    TableType(String),
    #[error("{0}")]
    MemType(String),
    #[error("{0}")]
    GlobalType(String),
}

/// The error types for WasmEdge Instance.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum InstanceError {
    #[error("Fail to create Instance context")]
    Create,
    #[error("Fail to create WasiModule context")]
    CreateWasi,
    #[error("Fail to create WasmEdgeProcessModule context")]
    CreateWasmEdgeProcess,
    #[error("Fail to create ImportModule context")]
    CreateImportModule,
    #[error("Not found the target function ({0})")]
    NotFoundFunc(String),
    #[error("Not found the target table ({0})")]
    NotFoundTable(String),
    #[error("Not found the target memory ({0})")]
    NotFoundMem(String),
    #[error("Not found the target global ({0})")]
    NotFoundGlobal(String),
    #[error("Not found the given mapped Fd/handler")]
    NotFoundMappedFdHandler,
}

/// The error types for WasmEdge plugin.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum PluginError {
    #[error("Failed to create a plugin instance named '{0}'. Make sure that the plugin instance name is correct.")]
    Create(String),
    #[error("Not found the plugin named '{0}'. Make sure that the plugin name is correct.")]
    NotFound(String),
}

/// The error types for WasmEdge Store.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum StoreError {
    #[error("Fail to create Store context")]
    Create,
    #[error("Fail to find the target function ({0})")]
    NotFoundFunc(String),
    #[error("Fail to find the target function ({func_name}) in the module ({mod_name})")]
    NotFoundFuncRegistered { func_name: String, mod_name: String },
    #[error("Fail to find the target table ({0})")]
    NotFoundTable(String),
    #[error("Fail to find the target table ({table_name}) in the module ({mod_name})")]
    NotFoundTableRegistered {
        table_name: String,
        mod_name: String,
    },
    #[error("Fail to find the target memory ({0})")]
    NotFoundMem(String),
    #[error("Fail to find the target memory ({mem_name}) in the module ({mod_name})")]
    NotFoundMemRegistered { mem_name: String, mod_name: String },
    #[error("Fail to find the target global ({0})")]
    NotFoundGlobal(String),
    #[error("Fail to find the target global ({global_name}) in the module ({mod_name})")]
    NotFoundGlobalRegistered {
        global_name: String,
        mod_name: String,
    },
    #[error("Not found the target module ({0})")]
    NotFoundModule(String),
    #[error("Not found the active module")]
    NotFoundActiveModule,
}

/// The error types for WasmEdge Vm.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum VmError {
    #[error("Fail to create Vm context")]
    Create,
    #[error("Fail to create Vm context. It is required to provide a `WasiContext` instance for creating a Vm when the `wasi` config option is enabled.")]
    CreateWithWasiContext,
    #[error("Fail to get the type of the target function ({0})")]
    NotFoundFuncType(String),
    #[error("Fail to get module instance ({0})")]
    NotFoundModule(String),
    #[error("Fail to get Wasi module instance")]
    NotFoundWasiModule,
    #[error("Fail to get WasmEdge_Process module instance")]
    NotFoundWasmEdgeProcessModule,
    #[error("Fail to get WasiNn module instance")]
    NotFoundWasiNnModule,
    #[error("Fail to get WasiCryptoCommon module instance")]
    NotFoundWasiCryptoCommonModule,
    #[error("Fail to get WasiCryptoAsymmetricCommon module instance")]
    NotFoundWasiCryptoAsymmetricCommonModule,
    #[error("Fail to get WasiCryptoSymmetric module instance")]
    NotFoundWasiCryptoSymmetricModule,
    #[error("Fail to get WasiCryptoKx module instance")]
    NotFoundWasiCryptoKxModule,
    #[error("Fail to get WasiCryptoSignatures module instance")]
    NotFoundWasiCryptoSignaturesModule,
    #[error("Fail to get Store context")]
    NotFoundStore,
    #[error("Fail to get Statistics context")]
    NotFoundStatistics,
    #[error("Fail to get the target ImportModule (name: {0})")]
    NotFoundImportModule(String),
    #[error(
        "Fail to register import module named {0}. Another import module with the same name has already existed."
    )]
    DuplicateImportModule(String),
    #[error(
        "Fail to register module named {0}. Another module instance with the same name has already existed."
    )]
    DuplicateModuleInstance(String),
    #[error("Fail to get Loader context")]
    NotFoundLoader,
    #[error("Fail to get Validator context")]
    NotFoundValidator,
    #[error("Fail to get Executor context")]
    NotFoundExecutor,
    #[error("Try to register an invalid import module")]
    InvalidImportModule,
    #[error("Not found active module instance")]
    NotFoundActiveModule,
}

/// The error types raised by WasmEdge Core.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum CoreError {
    #[error("{0}")]
    Common(CoreCommonError),
    #[error("{0}")]
    Load(CoreLoadError),
    #[error("{0}")]
    Validation(CoreValidationError),
    #[error("{0}")]
    Instantiation(CoreInstantiationError),
    #[error("{0}")]
    Execution(CoreExecutionError),
    #[error("{0}")]
    Component(CoreComponentError),
    #[error("unknown error code {0}")]
    UnknownError(u32),
}

/// The error type for the common errors from WasmEdge Core.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum CoreCommonError {
    #[error("process terminated")]
    Terminated,
    #[error("generic runtime error")]
    RuntimeError,
    #[error("cost limit exceeded")]
    CostLimitExceeded,
    #[error("wrong VM workflow")]
    WrongVMWorkflow,
    #[error("wasm function not found")]
    FuncNotFound,
    #[error("AOT runtime is disabled in this build")]
    AOTDisabled,
    #[error("execution interrupted")]
    Interrupted,
    #[error("user defined error code")]
    UserDefError,
    #[error("wasm module hasn't passed validation yet")]
    NotValidated,
    #[error("set null value into non-nullable value type")]
    NonNullRequired,
    #[error("set value into const")]
    SetValueToConst,
    #[error("set value type mismatch")]
    SetValueErrorType,
}

/// The error type for the load phase from WasmEdge Core.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum CoreLoadError {
    #[error("Invalid file path")]
    IllegalPath,
    #[error("Read error")]
    ReadError,
    #[error("unexpected end")]
    UnexpectedEnd,
    #[error("magic header not detected")]
    MalformedMagic,
    #[error("unknown binary version")]
    MalformedVersion,
    #[error("malformed section id")]
    MalformedSection,
    #[error("section size mismatch")]
    SectionSizeMismatch,
    #[error("length out of bounds")]
    LengthOutOfBounds,
    #[error("unexpected content after last section")]
    JunkSection,
    #[error("function and code section have inconsistent lengths")]
    IncompatibleFuncCode,
    #[error("data count and data section have inconsistent lengths")]
    IncompatibleDataCount,
    #[error("data count section required")]
    DataCountRequired,
    #[error("malformed import kind")]
    MalformedImportKind,
    #[error("malformed export kind")]
    MalformedExportKind,
    #[error("zero byte expected")]
    ExpectedZeroByte,
    #[error("malformed mutability")]
    InvalidMut,
    #[error("too many locals")]
    TooManyLocals,
    #[error("malformed value type")]
    MalformedValType,
    #[error("malformed element type")]
    MalformedElemType,
    #[error("malformed reference type")]
    MalformedRefType,
    #[error("malformed UTF-8 encoding")]
    MalformedUTF8,
    #[error("integer too large")]
    IntegerTooLarge,
    #[error("integer representation too long")]
    IntegerTooLong,
    #[error("illegal opcode")]
    IllegalOpCode,
    #[error("invalid wasm grammar")]
    IllegalGrammar,
    #[error("shared memory must have maximum")]
    SharedMemoryNoMax,
    #[error("intrinsics table not found")]
    IntrinsicsTableNotFound,
    #[error("malformed table")]
    MalformedTable,
}

/// The error type for the validation phase from WasmEdge Core.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum CoreValidationError {
    #[error("alignment must not be larger than natural")]
    InvalidAlignment,
    #[error("type mismatch")]
    TypeCheckFailed,
    #[error("unknown label")]
    InvalidLabelIdx,
    #[error("unknown local")]
    InvalidLocalIdx,
    #[error("unknown field")]
    InvalidFieldIdx,
    #[error("unknown type")]
    InvalidFuncTypeIdx,
    #[error("unknown function")]
    InvalidFuncIdx,
    #[error("unknown table")]
    InvalidTableIdx,
    #[error("unknown memory")]
    InvalidMemoryIdx,
    #[error("unknown global")]
    InvalidGlobalIdx,
    #[error("unknown elem segment")]
    InvalidElemIdx,
    #[error("unknown data segment")]
    InvalidDataIdx,
    #[error("undeclared function reference")]
    InvalidRefIdx,
    #[error("constant expression required")]
    ConstExprRequired,
    #[error("duplicate export name")]
    DupExportName,
    #[error("global is immutable")]
    ImmutableGlobal,
    #[error("field is immutable")]
    ImmutableField,
    #[error("array is immutable")]
    ImmutableArray,
    #[error("invalid result arity")]
    InvalidResultArity,
    #[error("multiple tables")]
    MultiTables,
    #[error("multiple memories")]
    MultiMemories,
    #[error("size minimum must not be greater than maximum")]
    InvalidLimit,
    #[error("memory size must be at most 65536 pages (4GiB)")]
    InvalidMemPages,
    #[error("start function")]
    InvalidStartFunc,
    #[error("invalid lane index")]
    InvalidLaneIdx,
    #[error("uninitialized local")]
    InvalidUninitLocal,
    #[error("field type is not defaultable")]
    InvalidNotDefaultableField,
    #[error("array type is not defaultable")]
    InvalidNotDefaultableArray,
    #[error("field is packed")]
    InvalidPackedField,
    #[error("array is packed")]
    InvalidPackedArray,
    #[error("field is unpacked")]
    InvalidUnpackedField,
    #[error("array is unpacked")]
    InvalidUnpackedArray,
    #[error("invalid br ref type")]
    InvalidBrRefType,
    #[error("array types do not match")]
    ArrayTypesMismatch,
    #[error("array type is not numeric or vector")]
    ArrayTypesNumtypeRequired,
    #[error("sub type")]
    InvalidSubType,
}

/// The error type for the instantiation phase from WasmEdge Core.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum CoreInstantiationError {
    #[error("module name conflict")]
    ModuleNameConflict,
    #[error("incompatible import type")]
    IncompatibleImportType,
    #[error("unknown import")]
    UnknownImport,
    #[error("data segment does not fit")]
    DataSegDoesNotFit,
    #[error("elements segment does not fit")]
    ElemSegDoesNotFit,
}

/// The error type for the execution phase from WasmEdge Core.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum CoreExecutionError {
    #[error("wrong instance address")]
    WrongInstanceAddress,
    #[error("wrong instance index")]
    WrongInstanceIndex,
    #[error("instruction type mismatch")]
    InstrTypeMismatch,
    #[error("function signature mismatch")]
    FuncSigMismatch,
    #[error("integer divide by zero")]
    DivideByZero,
    #[error("integer overflow")]
    IntegerOverflow,
    #[error("invalid conversion to integer")]
    InvalidConvToInt,
    #[error("out of bounds table access")]
    TableOutOfBounds,
    #[error("out of bounds memory access")]
    MemoryOutOfBounds,
    #[error("out of bounds array access")]
    ArrayOutOfBounds,
    #[error("unreachable")]
    Unreachable,
    #[error("uninitialized element")]
    UninitializedElement,
    #[error("undefined element")]
    UndefinedElement,
    #[error("indirect call type mismatch")]
    IndirectCallTypeMismatch,
    #[error("host function failed")]
    HostFuncFailed,
    #[error("reference type mismatch")]
    RefTypeMismatch,
    #[error("unaligned atomic")]
    UnalignedAtomicAccess,
    #[error("expected shared memory")]
    ExpectSharedMemory,
    #[error("null reference")]
    CastNullToNonNull,
    #[error("null function reference")]
    AccessNullFunc,
    #[error("null structure reference")]
    AccessNullStruct,
    #[error("null array reference")]
    AccessNullArray,
    #[error("null i31 reference")]
    AccessNullI31,
    #[error("cast failure")]
    CastFailed,
}

/// The error type for the component model phase from WasmEdge Core.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum CoreComponentError {
    #[error("malformed sort")]
    MalformedSort,
    #[error("malformed alias target")]
    MalformedAliasTarget,
    #[error("malformed core instance")]
    MalformedCoreInstance,
    #[error("malformed instance")]
    MalformedInstance,
    #[error("malformed defined type")]
    MalformedDefType,
    #[error("malformed record type")]
    MalformedRecordType,
    #[error("malformed variant type")]
    MalformedVariantType,
    #[error("malformed tuple type")]
    MalformedTupleType,
    #[error("malformed flags type")]
    MalformedFlagsType,
    #[error("malformed canonical")]
    MalformedCanonical,
    #[error("unknown canonical option")]
    UnknownCanonicalOption,
    #[error("malformed name")]
    MalformedName,
}

/// The error type for the host function definition.
#[derive(Error, Clone, Debug, PartialEq, Eq)]
pub enum HostFuncError {
    #[error("User error: {0}")]
    User(u32),
    #[error("Runtime error: {0}")]
    Runtime(u32),
}