radix_engine/vm/wasm/
errors.rs

1use crate::errors::{CanBeAbortion, InvokeError, RuntimeError, SelfError, VmError};
2use crate::internal_prelude::*;
3use crate::system::system_modules::costing::FeeReserveError;
4use crate::transaction::AbortReason;
5
6/// Represents an error when validating a WASM file.
7#[derive(Debug, PartialEq, Eq, Clone, Sbor)]
8pub enum PrepareError {
9    /// Failed to deserialize.
10    /// See <https://webassembly.github.io/spec/core/syntax/index.html>
11    DeserializationError,
12    /// Failed to validate
13    /// See <https://webassembly.github.io/spec/core/valid/index.html>
14    ValidationError(String),
15    /// Failed to serialize.
16    SerializationError,
17    /// The wasm module contains a start function.
18    StartFunctionNotAllowed,
19    /// Invalid import section
20    InvalidImport(InvalidImport),
21    /// Invalid memory section
22    InvalidMemory(InvalidMemory),
23    /// Invalid table section
24    InvalidTable(InvalidTable),
25    /// Invalid export symbol name
26    InvalidExportName(String),
27    /// Too many targets in the `br_table` instruction
28    TooManyTargetsInBrTable,
29    /// Too many functions
30    TooManyFunctions,
31    /// Too many function parameters
32    TooManyFunctionParams,
33    /// Too many function local variables
34    TooManyFunctionLocals { max: u32, actual: u32 },
35    /// Too many globals
36    TooManyGlobals { max: u32, current: u32 },
37    /// No export section
38    NoExportSection,
39    /// Missing export
40    MissingExport { export_name: String },
41    /// The wasm module does not have the `scrypto_alloc` export.
42    NoScryptoAllocExport,
43    /// The wasm module does not have the `scrypto_free` export.
44    NoScryptoFreeExport,
45    /// Failed to inject instruction metering
46    RejectedByInstructionMetering { reason: String },
47    /// Failed to inject stack metering
48    RejectedByStackMetering { reason: String },
49    /// Not instantiatable
50    NotInstantiatable { reason: String },
51    /// Not compilable
52    NotCompilable,
53    /// Wrap errors returned by WasmInstrument::ModuleInfoError
54    /// It is wrapped to String, because it's members cannot derive: Sbor, Eq and PartialEq
55    ModuleInfoError(String),
56    /// Wrap errors returned by wasmparser
57    /// It is wrapped to String, because wasmparser error (BinaryReaderError) members cannot derive: Sbor, Eq and PartialEq
58    WasmParserError(String),
59    /// An overflow occurred in some of the internal math
60    Overflow,
61}
62
63#[derive(Debug, PartialEq, Eq, Clone, Sbor)]
64pub enum InvalidImport {
65    /// The import is not allowed
66    ImportNotAllowed(String),
67    /// Scrypto VM version protocol mismatch
68    ProtocolVersionMismatch {
69        name: String,
70        current_version: u64,
71        expected_version: u64,
72    },
73    InvalidFunctionType(String),
74}
75
76#[derive(Debug, PartialEq, Eq, Clone, Sbor)]
77pub enum InvalidMemory {
78    /// The wasm module has no memory section.
79    MissingMemorySection,
80    /// The memory section is empty.
81    NoMemoryDefinition,
82    /// The memory section contains too many memory definitions.
83    TooManyMemoryDefinition,
84    /// The memory size exceeds the limit.
85    MemorySizeLimitExceeded,
86    /// The wasm module does not have the `memory` export.
87    MemoryNotExported,
88}
89
90#[derive(Debug, PartialEq, Eq, Clone, Sbor)]
91pub enum InvalidTable {
92    /// More than one table defined, against WebAssembly MVP spec
93    MoreThanOneTable,
94    /// Initial table size too large
95    InitialTableSizeLimitExceeded,
96}
97
98/// Represents an error when invoking an export of a Scrypto module.
99#[derive(Debug, Clone, PartialEq, Eq, ScryptoSbor)]
100pub enum WasmRuntimeError {
101    /// Host attempted to call unknown WASM function, addressed by name.
102    UnknownExport(String),
103
104    /// Error when reading wasm memory.
105    MemoryAccessError,
106
107    /// WASM function return is not a `u64` fat pointer which points to a valid memory range.
108    InvalidWasmPointer,
109
110    /// WASM execution error, including trap.
111    ExecutionError(String),
112
113    /// Not implemented, no-op wasm runtime
114    NotImplemented,
115
116    /// Buffer not found
117    BufferNotFound(BufferId),
118
119    InvalidAddress(DecodeError),
120
121    /// Invalid method ident
122    InvalidString,
123
124    /// Invalid RE node ID
125    InvalidNodeId,
126
127    InvalidGlobalAddressReservation,
128
129    /// Invalid reference type
130    InvalidReferenceType(u32),
131
132    /// Invalid RE module ID
133    InvalidAttachedModuleId(u32),
134
135    /// Invalid initial app states
136    InvalidObjectStates(DecodeError),
137
138    /// Invalid access rules
139    InvalidAccessRule(DecodeError),
140
141    /// Invalid modules
142    InvalidModules(DecodeError),
143
144    InvalidTemplateArgs(DecodeError),
145
146    InvalidKeyValueStoreSchema(DecodeError),
147
148    /// Invalid component address
149    InvalidLockFlags,
150
151    /// Invalid log level
152    InvalidLogLevel(DecodeError),
153
154    /// Costing error (no-op runtime only!)
155    FeeReserveError(FeeReserveError),
156
157    InvalidEventFlags(u32),
158
159    InvalidPackageAddress,
160
161    TooManyBuffers,
162
163    InvalidBlsPublicKey(DecodeError),
164    InvalidBlsSignature(DecodeError),
165    InvalidBlsPublicKeyOrMessage(DecodeError),
166
167    InputDataEmpty,
168
169    InvalidEd25519PublicKey(ParseEd25519PublicKeyError),
170    InvalidEd25519Signature(ParseEd25519SignatureError),
171
172    InvalidSecp256k1PublicKey(ParseSecp256k1PublicKeyError),
173    InvalidSecp256k1Signature(ParseSecp256k1SignatureError),
174
175    InvalidHash(ParseHashError),
176    Secp256k1KeyRecoveryError,
177}
178
179impl SelfError for WasmRuntimeError {
180    fn into_runtime_error(self) -> RuntimeError {
181        RuntimeError::VmError(VmError::Wasm(self))
182    }
183}
184
185impl CanBeAbortion for WasmRuntimeError {
186    fn abortion(&self) -> Option<&AbortReason> {
187        match self {
188            WasmRuntimeError::FeeReserveError(err) => err.abortion(),
189            _ => None,
190        }
191    }
192}
193
194impl fmt::Display for WasmRuntimeError {
195    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
196        write!(f, "{:?}", self)
197    }
198}
199
200#[cfg(not(feature = "alloc"))]
201impl std::error::Error for WasmRuntimeError {}
202
203impl fmt::Display for InvokeError<WasmRuntimeError> {
204    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
205        write!(f, "{:?}", self)
206    }
207}
208
209#[cfg(not(feature = "alloc"))]
210impl std::error::Error for InvokeError<WasmRuntimeError> {}