Skip to main content

gear_core/code/
errors.rs

1// Copyright (C) Gear Technologies Inc.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4//! Module that describes various code errors.
5
6pub use gear_wasm_instrument::{InstrumentationError, ModuleError};
7pub use wasmparser::BinaryReaderError;
8
9/// Section name in WASM module.
10#[derive(PartialEq, Eq, Debug, derive_more::Display)]
11pub enum SectionName {
12    /// Type section.
13    #[display("Type section")]
14    Type,
15    /// Import section.
16    #[display("Import section")]
17    Import,
18    /// Function (Code) section.
19    #[display("Function section")]
20    Function,
21    /// Data section.
22    #[display("Data section")]
23    Data,
24    /// Global section.
25    #[display("Global section")]
26    Global,
27    /// Table section.
28    #[display("Table section")]
29    Table,
30    /// Element section.
31    #[display("Element section")]
32    Element,
33    /// Export section.
34    #[display("Export section")]
35    Export,
36    /// Start section.
37    #[display("Start section")]
38    Start,
39}
40
41/// Section error in WASM module.
42#[derive(Debug, derive_more::Display)]
43pub enum SectionError {
44    /// Section not found.
45    #[display("{_0} not found")]
46    NotFound(SectionName),
47    /// Section not supported.
48    #[display("{_0} not supported")]
49    NotSupported(SectionName),
50}
51
52/// Memory error in WASM module.
53#[derive(Debug, derive_more::Display)]
54pub enum MemoryError {
55    /// Memory entry not found in import section.
56    #[display("Memory entry not found")]
57    EntryNotFound,
58    /// The WASM module has invalid count of static memory pages.
59    #[display("The WASM module has invalid count of static memory pages")]
60    InvalidStaticPageCount,
61}
62
63/// Stack end error in WASM module.
64#[derive(Debug, derive_more::Display)]
65pub enum StackEndError {
66    /// Unsupported initialization of gear stack end global variable.
67    #[display("Unsupported initialization of gear stack end global")]
68    Initialization,
69    /// Gear stack end offset is not aligned to wasm page size.
70    #[display("Gear stack end {_0:#x} is not aligned to wasm page size")]
71    NotAligned(u32),
72    /// Gear stack end is out of static memory.
73    #[display("Gear stack end {_0:#x} is out of static memory 0x0..{_1:#x}")]
74    OutOfStatic(u32, u64),
75}
76
77/// Data section error in WASM module.
78#[derive(Debug, derive_more::Display)]
79pub enum DataSectionError {
80    /// Unsupported initialization of data segment.
81    #[display("Unsupported initialization of data segment")]
82    Initialization,
83    /// Data section overlaps gear stack.
84    #[display("Data segment {_0:#x} overlaps gear stack 0x0..{_1:#x}")]
85    GearStackOverlaps(u32, u32),
86    /// Data segment end address is out of possible 32 bits address space.
87    #[display("Data segment {_0:#x} ends out of possible 32 bits address space")]
88    EndAddressOverflow(u32),
89    /// Data segment end address is out of static memory.
90    #[display("Data segment {_0:#x}..={_1:#x} is out of static memory 0x0..{_2:#x}")]
91    EndAddressOutOfStaticMemory(u32, u32, u64),
92    /// Data segment amount exceeds the limit.
93    #[display("Data segment amount limit exceeded: limit={limit}, actual={actual}")]
94    DataSegmentsAmountLimit {
95        /// Limit of data segments.
96        limit: u32,
97        /// Actual amount of data segments.
98        actual: u32,
99    },
100}
101
102/// Type section error in WASM module.
103#[derive(Debug, derive_more::Display)]
104pub enum TypeSectionError {
105    /// Type section length exceeds the limit.
106    #[display("Type section length limit exceeded: limit={limit}, actual={actual}")]
107    LengthLimitExceeded {
108        /// Max length of type section.
109        limit: u32,
110        /// Actual length of type section.
111        actual: u32,
112    },
113    /// Type section number of parameters per type exceeds the limit.
114    #[display("Type section parameters per type limit exceeded: limit={limit}, actual={actual}")]
115    ParametersPerTypeLimitExceeded {
116        /// Max number of parameters per type.
117        limit: u32,
118        /// Actual number of parameters per type.
119        actual: u32,
120    },
121}
122
123/// Export error in WASM module.
124#[derive(Debug, derive_more::Display)]
125pub enum ExportError {
126    /// Incorrect global export index. Can occur when export refers to not existing global index.
127    #[display("Global index `{_0}` in export index `{_1}` is incorrect")]
128    IncorrectGlobalIndex(u32, u32),
129    /// Exporting mutable globals is restricted by the Gear protocol.
130    #[display("Global index `{_0}` in export index `{_1}` cannot be mutable")]
131    MutableGlobalExport(u32, u32),
132    /// Export references to an import function, which is not allowed.
133    #[display("Export index `{_0}` references to imported function with index `{_1}`")]
134    ExportReferencesToImportFunction(u32, u32),
135    /// Export references to an import global, which is not allowed.
136    #[display("Export index `{_0}` references to imported global with index `{_1}`")]
137    ExportReferencesToImportGlobal(u32, u32),
138    /// The signature of an exported function is invalid.
139    #[display("Exported function with index `{_0}` must have signature `fn f() {{ ... }}`")]
140    InvalidExportFnSignature(u32),
141    /// The provided code contains excess function export.
142    #[display("Excess export with index `{_0}` found")]
143    ExcessExport(u32),
144    /// The provided code doesn't contain the required `init` or `handle` export function.
145    #[display("Required export function `init` or `handle` is not found")]
146    RequiredExportNotFound,
147}
148
149/// Import error in WASM module.
150#[derive(Debug, derive_more::Display)]
151pub enum ImportError {
152    /// The imported function is not supported by the Gear protocol.
153    #[display("Unknown imported function with index `{_0}`")]
154    UnknownImport(u32),
155    /// The imported function is declared multiple times.
156    #[display("Imported function with index `{_0}` is declared multiple times")]
157    DuplicateImport(u32),
158    /// The signature of an imported function is invalid.
159    #[display("Invalid function signature for imported function with index `{_0}`")]
160    InvalidImportFnSignature(u32),
161    /// Unexpected import kind.
162    #[display("Unexpected import kind `{kind}` with index `{index}`")]
163    UnexpectedImportKind {
164        /// Kind of the import.
165        kind: &'static &'static str,
166        /// Index of the import.
167        index: u32,
168    },
169}
170
171/// Describes why the code is not valid Gear program.
172#[derive(Debug, derive_more::Display, derive_more::From)]
173pub enum CodeError {
174    /// Validation by wasmparser failed.
175    #[display("wasmparser validation error: {_0}")]
176    Validation(BinaryReaderError),
177    /// Module encode/decode error.
178    #[display("Codec error: {_0}")]
179    Module(ModuleError),
180    /// The provided code contains section error.
181    #[display("Section error: {_0}")]
182    Section(SectionError),
183    /// The provided code contains memory error.
184    #[display("Memory error: {_0}")]
185    Memory(MemoryError),
186    /// The provided code contains stack end error.
187    #[display("Stack end error: {_0}")]
188    StackEnd(StackEndError),
189    /// The provided code contains data section error.
190    #[display("Data section error: {_0}")]
191    DataSection(DataSectionError),
192    /// The provided code contains type section error.
193    #[display("Type section error: {_0}")]
194    TypeSection(TypeSectionError),
195    /// The provided code contains export error.
196    #[display("Export error: {_0}")]
197    Export(ExportError),
198    /// The provided code contains import error.
199    #[display("Import error: {_0}")]
200    Import(ImportError),
201    /// Error occurred during instrumentation WASM module.
202    #[display("Instrumentation error: {_0}")]
203    Instrumentation(InstrumentationError),
204}
205
206impl core::error::Error for CodeError {}