wasmedge_types/
error.rs

1//! Defines WasmEdge error types.
2
3use crate::ExternalInstanceType;
4use thiserror::Error;
5
6/// The error types used by both wasmedge-sys and wasmedge crates.
7#[derive(Error, Clone, Debug, PartialEq, Eq)]
8pub enum WasmEdgeError {
9    // For general operation error
10    #[error("{0}")]
11    Operation(String),
12
13    // For user-defined error
14    #[error("{0}")]
15    User(u32),
16
17    /// Errors raised by WasmEdge Core.
18    #[error("{0}")]
19    Core(#[from] CoreError),
20
21    #[error("Fail to create ImportObj module")]
22    ImportObjCreate,
23    #[error("Fail to create Executor context")]
24    ExecutorCreate,
25    #[error("{0}")]
26    Store(#[from] StoreError),
27    #[error("Fail to create Statistics context")]
28    StatisticsCreate,
29    #[error("Fail to create Module")]
30    ModuleCreate,
31    #[error("Fail to create Loader")]
32    LoaderCreate,
33    #[error("Fail to create Config")]
34    ConfigCreate,
35    #[error("Fail to create AOT Compiler")]
36    CompilerCreate,
37    #[error("Fail to create Validator")]
38    ValidatorCreate,
39    #[error("{0}")]
40    Vm(#[from] VmError),
41
42    // instances
43    #[error("{0}")]
44    Func(#[from] FuncError),
45    #[error("Fail to create FuncType")]
46    FuncTypeCreate,
47    #[error("Execution Timed Out")]
48    ExecuteTimeout,
49    #[error("{0}")]
50    Mem(#[from] MemError),
51    #[error("Fail to create MemType")]
52    MemTypeCreate,
53    #[error("{0}")]
54    Global(#[from] GlobalError),
55    #[error("Fail to create GlobalType")]
56    GlobalTypeCreate,
57    #[error("{0}")]
58    Table(#[from] TableError),
59    #[error("Fail to create TableType")]
60    TableTypeCreate,
61    #[error("{0}")]
62    Import(#[from] ImportError),
63    #[error("{0}")]
64    Export(#[from] ExportError),
65    #[error("{0}")]
66    Instance(#[from] InstanceError),
67    #[error("{0}")]
68    Plugin(#[from] PluginError),
69
70    // std
71    #[error("Found an internal 0 byte")]
72    FoundNulByte(#[from] std::ffi::NulError),
73    #[error("Fail to find a nul byte in the expected position")]
74    NotFoundNulByte(#[from] std::ffi::FromBytesWithNulError),
75    #[error("Fail to interpret a sequence of u8 as a string")]
76    Utf8(#[from] std::str::Utf8Error),
77    #[error("Fail to convert a vector of bytes to a string")]
78    FromUtf8(#[from] std::string::FromUtf8Error),
79
80    // Windows platform
81    #[error("Fail to convert path on Windows: {0}")]
82    WindowsPathConversion(String),
83}
84
85/// The error types for WasmEdge Function.
86#[derive(Error, Clone, Debug, PartialEq, Eq)]
87pub enum FuncError {
88    #[error("Fail to create Function instance")]
89    Create,
90    #[error("{0}")]
91    CreateBinding(String),
92    #[error("Fail to get the function type")]
93    Type,
94}
95
96/// The error types for WasmEdge Memory.
97#[derive(Error, Clone, Debug, PartialEq, Eq)]
98pub enum MemError {
99    #[error("Fail to create Memory instance")]
100    Create,
101    #[error("Fail to create shared memory type. The 'max' field must not be None.")]
102    CreateSharedType,
103    #[error("Fail to get the memory type")]
104    Type,
105    #[error("Fail to get the const pointer to data")]
106    ConstPtr,
107    #[error("Fail to get the pointer to data")]
108    MutPtr,
109    #[error("Fail to convert a raw pointer to a reference")]
110    Ptr2Ref,
111}
112
113/// The error types for WasmEdge Global.
114#[derive(Error, Clone, Debug, PartialEq, Eq)]
115pub enum GlobalError {
116    #[error("Fail to create Global instance")]
117    Create,
118    #[error("Fail to get the global type")]
119    Type,
120    #[error("Trying to set value to a const global variable")]
121    ModifyConst,
122    #[error("")]
123    UnmatchedValType,
124}
125
126/// The error types for WasmEdge Table.
127#[derive(Error, Clone, Debug, PartialEq, Eq)]
128pub enum TableError {
129    #[error("Fail to create Table instance")]
130    Create,
131    #[error("Fail to get the table type")]
132    Type,
133}
134
135/// The error types for WasmEdge ImportType.
136#[derive(Error, Clone, Debug, PartialEq, Eq)]
137pub enum ImportError {
138    #[error("The expected Import type is {expected:?}, but found {actual:?}")]
139    Type {
140        expected: ExternalInstanceType,
141        actual: ExternalInstanceType,
142    },
143    #[error("{0}")]
144    FuncType(String),
145    #[error("{0}")]
146    TableType(String),
147    #[error("{0}")]
148    MemType(String),
149    #[error("{0}")]
150    GlobalType(String),
151}
152
153/// The error types for WasmEdge ExportType.
154#[derive(Error, Clone, Debug, PartialEq, Eq)]
155pub enum ExportError {
156    #[error("The expected Export type is {expected:?}, but found {actual:?}")]
157    Type {
158        expected: ExternalInstanceType,
159        actual: ExternalInstanceType,
160    },
161    #[error("{0}")]
162    FuncType(String),
163    #[error("{0}")]
164    TableType(String),
165    #[error("{0}")]
166    MemType(String),
167    #[error("{0}")]
168    GlobalType(String),
169}
170
171/// The error types for WasmEdge Instance.
172#[derive(Error, Clone, Debug, PartialEq, Eq)]
173pub enum InstanceError {
174    #[error("Fail to create Instance context")]
175    Create,
176    #[error("Fail to create WasiModule context")]
177    CreateWasi,
178    #[error("Fail to create WasmEdgeProcessModule context")]
179    CreateWasmEdgeProcess,
180    #[error("Fail to create ImportModule context")]
181    CreateImportModule,
182    #[error("Not found the target function ({0})")]
183    NotFoundFunc(String),
184    #[error("Not found the target table ({0})")]
185    NotFoundTable(String),
186    #[error("Not found the target memory ({0})")]
187    NotFoundMem(String),
188    #[error("Not found the target global ({0})")]
189    NotFoundGlobal(String),
190    #[error("Not found the given mapped Fd/handler")]
191    NotFoundMappedFdHandler,
192}
193
194/// The error types for WasmEdge plugin.
195#[derive(Error, Clone, Debug, PartialEq, Eq)]
196pub enum PluginError {
197    #[error("Failed to create a plugin instance named '{0}'. Make sure that the plugin instance name is correct.")]
198    Create(String),
199    #[error("Not found the plugin named '{0}'. Make sure that the plugin name is correct.")]
200    NotFound(String),
201}
202
203/// The error types for WasmEdge Store.
204#[derive(Error, Clone, Debug, PartialEq, Eq)]
205pub enum StoreError {
206    #[error("Fail to create Store context")]
207    Create,
208    #[error("Fail to find the target function ({0})")]
209    NotFoundFunc(String),
210    #[error("Fail to find the target function ({func_name}) in the module ({mod_name})")]
211    NotFoundFuncRegistered { func_name: String, mod_name: String },
212    #[error("Fail to find the target table ({0})")]
213    NotFoundTable(String),
214    #[error("Fail to find the target table ({table_name}) in the module ({mod_name})")]
215    NotFoundTableRegistered {
216        table_name: String,
217        mod_name: String,
218    },
219    #[error("Fail to find the target memory ({0})")]
220    NotFoundMem(String),
221    #[error("Fail to find the target memory ({mem_name}) in the module ({mod_name})")]
222    NotFoundMemRegistered { mem_name: String, mod_name: String },
223    #[error("Fail to find the target global ({0})")]
224    NotFoundGlobal(String),
225    #[error("Fail to find the target global ({global_name}) in the module ({mod_name})")]
226    NotFoundGlobalRegistered {
227        global_name: String,
228        mod_name: String,
229    },
230    #[error("Not found the target module ({0})")]
231    NotFoundModule(String),
232    #[error("Not found the active module")]
233    NotFoundActiveModule,
234}
235
236/// The error types for WasmEdge Vm.
237#[derive(Error, Clone, Debug, PartialEq, Eq)]
238pub enum VmError {
239    #[error("Fail to create Vm context")]
240    Create,
241    #[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.")]
242    CreateWithWasiContext,
243    #[error("Fail to get the type of the target function ({0})")]
244    NotFoundFuncType(String),
245    #[error("Fail to get module instance ({0})")]
246    NotFoundModule(String),
247    #[error("Fail to get Wasi module instance")]
248    NotFoundWasiModule,
249    #[error("Fail to get WasmEdge_Process module instance")]
250    NotFoundWasmEdgeProcessModule,
251    #[error("Fail to get WasiNn module instance")]
252    NotFoundWasiNnModule,
253    #[error("Fail to get WasiCryptoCommon module instance")]
254    NotFoundWasiCryptoCommonModule,
255    #[error("Fail to get WasiCryptoAsymmetricCommon module instance")]
256    NotFoundWasiCryptoAsymmetricCommonModule,
257    #[error("Fail to get WasiCryptoSymmetric module instance")]
258    NotFoundWasiCryptoSymmetricModule,
259    #[error("Fail to get WasiCryptoKx module instance")]
260    NotFoundWasiCryptoKxModule,
261    #[error("Fail to get WasiCryptoSignatures module instance")]
262    NotFoundWasiCryptoSignaturesModule,
263    #[error("Fail to get Store context")]
264    NotFoundStore,
265    #[error("Fail to get Statistics context")]
266    NotFoundStatistics,
267    #[error("Fail to get the target ImportModule (name: {0})")]
268    NotFoundImportModule(String),
269    #[error(
270        "Fail to register import module named {0}. Another import module with the same name has already existed."
271    )]
272    DuplicateImportModule(String),
273    #[error(
274        "Fail to register module named {0}. Another module instance with the same name has already existed."
275    )]
276    DuplicateModuleInstance(String),
277    #[error("Fail to get Loader context")]
278    NotFoundLoader,
279    #[error("Fail to get Validator context")]
280    NotFoundValidator,
281    #[error("Fail to get Executor context")]
282    NotFoundExecutor,
283    #[error("Try to register an invalid import module")]
284    InvalidImportModule,
285    #[error("Not found active module instance")]
286    NotFoundActiveModule,
287}
288
289/// The error types raised by WasmEdge Core.
290#[derive(Error, Clone, Debug, PartialEq, Eq)]
291pub enum CoreError {
292    #[error("{0}")]
293    Common(CoreCommonError),
294    #[error("{0}")]
295    Load(CoreLoadError),
296    #[error("{0}")]
297    Validation(CoreValidationError),
298    #[error("{0}")]
299    Instantiation(CoreInstantiationError),
300    #[error("{0}")]
301    Execution(CoreExecutionError),
302    #[error("{0}")]
303    Component(CoreComponentError),
304    #[error("unknown error code {0}")]
305    UnknownError(u32),
306}
307
308/// The error type for the common errors from WasmEdge Core.
309#[derive(Error, Clone, Debug, PartialEq, Eq)]
310pub enum CoreCommonError {
311    #[error("process terminated")]
312    Terminated,
313    #[error("generic runtime error")]
314    RuntimeError,
315    #[error("cost limit exceeded")]
316    CostLimitExceeded,
317    #[error("wrong VM workflow")]
318    WrongVMWorkflow,
319    #[error("wasm function not found")]
320    FuncNotFound,
321    #[error("AOT runtime is disabled in this build")]
322    AOTDisabled,
323    #[error("execution interrupted")]
324    Interrupted,
325    #[error("user defined error code")]
326    UserDefError,
327    #[error("wasm module hasn't passed validation yet")]
328    NotValidated,
329    #[error("set null value into non-nullable value type")]
330    NonNullRequired,
331    #[error("set value into const")]
332    SetValueToConst,
333    #[error("set value type mismatch")]
334    SetValueErrorType,
335}
336
337/// The error type for the load phase from WasmEdge Core.
338#[derive(Error, Clone, Debug, PartialEq, Eq)]
339pub enum CoreLoadError {
340    #[error("Invalid file path")]
341    IllegalPath,
342    #[error("Read error")]
343    ReadError,
344    #[error("unexpected end")]
345    UnexpectedEnd,
346    #[error("magic header not detected")]
347    MalformedMagic,
348    #[error("unknown binary version")]
349    MalformedVersion,
350    #[error("malformed section id")]
351    MalformedSection,
352    #[error("section size mismatch")]
353    SectionSizeMismatch,
354    #[error("length out of bounds")]
355    LengthOutOfBounds,
356    #[error("unexpected content after last section")]
357    JunkSection,
358    #[error("function and code section have inconsistent lengths")]
359    IncompatibleFuncCode,
360    #[error("data count and data section have inconsistent lengths")]
361    IncompatibleDataCount,
362    #[error("data count section required")]
363    DataCountRequired,
364    #[error("malformed import kind")]
365    MalformedImportKind,
366    #[error("malformed export kind")]
367    MalformedExportKind,
368    #[error("zero byte expected")]
369    ExpectedZeroByte,
370    #[error("malformed mutability")]
371    InvalidMut,
372    #[error("too many locals")]
373    TooManyLocals,
374    #[error("malformed value type")]
375    MalformedValType,
376    #[error("malformed element type")]
377    MalformedElemType,
378    #[error("malformed reference type")]
379    MalformedRefType,
380    #[error("malformed UTF-8 encoding")]
381    MalformedUTF8,
382    #[error("integer too large")]
383    IntegerTooLarge,
384    #[error("integer representation too long")]
385    IntegerTooLong,
386    #[error("illegal opcode")]
387    IllegalOpCode,
388    #[error("invalid wasm grammar")]
389    IllegalGrammar,
390    #[error("shared memory must have maximum")]
391    SharedMemoryNoMax,
392    #[error("intrinsics table not found")]
393    IntrinsicsTableNotFound,
394    #[error("malformed table")]
395    MalformedTable,
396}
397
398/// The error type for the validation phase from WasmEdge Core.
399#[derive(Error, Clone, Debug, PartialEq, Eq)]
400pub enum CoreValidationError {
401    #[error("alignment must not be larger than natural")]
402    InvalidAlignment,
403    #[error("type mismatch")]
404    TypeCheckFailed,
405    #[error("unknown label")]
406    InvalidLabelIdx,
407    #[error("unknown local")]
408    InvalidLocalIdx,
409    #[error("unknown field")]
410    InvalidFieldIdx,
411    #[error("unknown type")]
412    InvalidFuncTypeIdx,
413    #[error("unknown function")]
414    InvalidFuncIdx,
415    #[error("unknown table")]
416    InvalidTableIdx,
417    #[error("unknown memory")]
418    InvalidMemoryIdx,
419    #[error("unknown global")]
420    InvalidGlobalIdx,
421    #[error("unknown elem segment")]
422    InvalidElemIdx,
423    #[error("unknown data segment")]
424    InvalidDataIdx,
425    #[error("undeclared function reference")]
426    InvalidRefIdx,
427    #[error("constant expression required")]
428    ConstExprRequired,
429    #[error("duplicate export name")]
430    DupExportName,
431    #[error("global is immutable")]
432    ImmutableGlobal,
433    #[error("field is immutable")]
434    ImmutableField,
435    #[error("array is immutable")]
436    ImmutableArray,
437    #[error("invalid result arity")]
438    InvalidResultArity,
439    #[error("multiple tables")]
440    MultiTables,
441    #[error("multiple memories")]
442    MultiMemories,
443    #[error("size minimum must not be greater than maximum")]
444    InvalidLimit,
445    #[error("memory size must be at most 65536 pages (4GiB)")]
446    InvalidMemPages,
447    #[error("start function")]
448    InvalidStartFunc,
449    #[error("invalid lane index")]
450    InvalidLaneIdx,
451    #[error("uninitialized local")]
452    InvalidUninitLocal,
453    #[error("field type is not defaultable")]
454    InvalidNotDefaultableField,
455    #[error("array type is not defaultable")]
456    InvalidNotDefaultableArray,
457    #[error("field is packed")]
458    InvalidPackedField,
459    #[error("array is packed")]
460    InvalidPackedArray,
461    #[error("field is unpacked")]
462    InvalidUnpackedField,
463    #[error("array is unpacked")]
464    InvalidUnpackedArray,
465    #[error("invalid br ref type")]
466    InvalidBrRefType,
467    #[error("array types do not match")]
468    ArrayTypesMismatch,
469    #[error("array type is not numeric or vector")]
470    ArrayTypesNumtypeRequired,
471    #[error("sub type")]
472    InvalidSubType,
473}
474
475/// The error type for the instantiation phase from WasmEdge Core.
476#[derive(Error, Clone, Debug, PartialEq, Eq)]
477pub enum CoreInstantiationError {
478    #[error("module name conflict")]
479    ModuleNameConflict,
480    #[error("incompatible import type")]
481    IncompatibleImportType,
482    #[error("unknown import")]
483    UnknownImport,
484    #[error("data segment does not fit")]
485    DataSegDoesNotFit,
486    #[error("elements segment does not fit")]
487    ElemSegDoesNotFit,
488}
489
490/// The error type for the execution phase from WasmEdge Core.
491#[derive(Error, Clone, Debug, PartialEq, Eq)]
492pub enum CoreExecutionError {
493    #[error("wrong instance address")]
494    WrongInstanceAddress,
495    #[error("wrong instance index")]
496    WrongInstanceIndex,
497    #[error("instruction type mismatch")]
498    InstrTypeMismatch,
499    #[error("function signature mismatch")]
500    FuncSigMismatch,
501    #[error("integer divide by zero")]
502    DivideByZero,
503    #[error("integer overflow")]
504    IntegerOverflow,
505    #[error("invalid conversion to integer")]
506    InvalidConvToInt,
507    #[error("out of bounds table access")]
508    TableOutOfBounds,
509    #[error("out of bounds memory access")]
510    MemoryOutOfBounds,
511    #[error("out of bounds array access")]
512    ArrayOutOfBounds,
513    #[error("unreachable")]
514    Unreachable,
515    #[error("uninitialized element")]
516    UninitializedElement,
517    #[error("undefined element")]
518    UndefinedElement,
519    #[error("indirect call type mismatch")]
520    IndirectCallTypeMismatch,
521    #[error("host function failed")]
522    HostFuncFailed,
523    #[error("reference type mismatch")]
524    RefTypeMismatch,
525    #[error("unaligned atomic")]
526    UnalignedAtomicAccess,
527    #[error("expected shared memory")]
528    ExpectSharedMemory,
529    #[error("null reference")]
530    CastNullToNonNull,
531    #[error("null function reference")]
532    AccessNullFunc,
533    #[error("null structure reference")]
534    AccessNullStruct,
535    #[error("null array reference")]
536    AccessNullArray,
537    #[error("null i31 reference")]
538    AccessNullI31,
539    #[error("cast failure")]
540    CastFailed,
541}
542
543/// The error type for the component model phase from WasmEdge Core.
544#[derive(Error, Clone, Debug, PartialEq, Eq)]
545pub enum CoreComponentError {
546    #[error("malformed sort")]
547    MalformedSort,
548    #[error("malformed alias target")]
549    MalformedAliasTarget,
550    #[error("malformed core instance")]
551    MalformedCoreInstance,
552    #[error("malformed instance")]
553    MalformedInstance,
554    #[error("malformed defined type")]
555    MalformedDefType,
556    #[error("malformed record type")]
557    MalformedRecordType,
558    #[error("malformed variant type")]
559    MalformedVariantType,
560    #[error("malformed tuple type")]
561    MalformedTupleType,
562    #[error("malformed flags type")]
563    MalformedFlagsType,
564    #[error("malformed canonical")]
565    MalformedCanonical,
566    #[error("unknown canonical option")]
567    UnknownCanonicalOption,
568    #[error("malformed name")]
569    MalformedName,
570}
571
572/// The error type for the host function definition.
573#[derive(Error, Clone, Debug, PartialEq, Eq)]
574pub enum HostFuncError {
575    #[error("User error: {0}")]
576    User(u32),
577    #[error("Runtime error: {0}")]
578    Runtime(u32),
579}