gear_core/code/
errors.rs

1// This file is part of Gear.
2
3// Copyright (C) 2024-2025 Gear Technologies Inc.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Module that describes various code errors.
20
21pub use gear_wasm_instrument::{parity_wasm::SerializationError, InstrumentationError};
22pub use wasmparser::BinaryReaderError;
23
24/// Section name in WASM module.
25#[derive(PartialEq, Eq, Debug, derive_more::Display)]
26pub enum SectionName {
27    /// Type section.
28    #[display(fmt = "Type section")]
29    Type,
30    /// Import section.
31    #[display(fmt = "Import section")]
32    Import,
33    /// Function (Code) section.
34    #[display(fmt = "Function section")]
35    Function,
36    /// Data section.
37    #[display(fmt = "Data section")]
38    Data,
39    /// Global section.
40    #[display(fmt = "Global section")]
41    Global,
42    /// Table section.
43    #[display(fmt = "Table section")]
44    Table,
45    /// Element section.
46    #[display(fmt = "Element section")]
47    Element,
48    /// Export section.
49    #[display(fmt = "Export section")]
50    Export,
51    /// Start section.
52    #[display(fmt = "Start section")]
53    Start,
54}
55
56/// Section error in WASM module.
57#[derive(Debug, derive_more::Display)]
58pub enum SectionError {
59    /// Section not found.
60    #[display(fmt = "{_0} not found")]
61    NotFound(SectionName),
62    /// Section not supported.
63    #[display(fmt = "{_0} not supported")]
64    NotSupported(SectionName),
65}
66
67/// Memory error in WASM module.
68#[derive(Debug, derive_more::Display)]
69pub enum MemoryError {
70    /// Memory entry not found in import section.
71    #[display(fmt = "Memory entry not found")]
72    EntryNotFound,
73    /// The WASM module has invalid count of static memory pages.
74    #[display(fmt = "The WASM module has invalid count of static memory pages")]
75    InvalidStaticPageCount,
76}
77
78/// Stack end error in WASM module.
79#[derive(Debug, derive_more::Display)]
80pub enum StackEndError {
81    /// Unsupported initialization of gear stack end global variable.
82    #[display(fmt = "Unsupported initialization of gear stack end global")]
83    Initialization,
84    /// Gear stack end offset is not aligned to wasm page size.
85    #[display(fmt = "Gear stack end {_0:#x} is not aligned to wasm page size")]
86    NotAligned(u32),
87    /// Gear stack end is out of static memory.
88    #[display(fmt = "Gear stack end {_0:#x} is out of static memory 0x0..{_1:#x}")]
89    OutOfStatic(u32, u64),
90}
91
92/// Data section error in WASM module.
93#[derive(Debug, derive_more::Display)]
94pub enum DataSectionError {
95    /// Unsupported initialization of data segment.
96    #[display(fmt = "Unsupported initialization of data segment")]
97    Initialization,
98    /// Data section overlaps gear stack.
99    #[display(fmt = "Data segment {_0:#x} overlaps gear stack 0x0..{_1:#x}")]
100    GearStackOverlaps(u32, u32),
101    /// Data segment end address is out of possible 32 bits address space.
102    #[display(fmt = "Data segment {_0:#x} ends out of possible 32 bits address space")]
103    EndAddressOverflow(u32),
104    /// Data segment end address is out of static memory.
105    #[display(fmt = "Data segment {_0:#x}..={_1:#x} is out of static memory 0x0..{_2:#x}")]
106    EndAddressOutOfStaticMemory(u32, u32, u64),
107    /// Data segment amount exceeds the limit.
108    #[display(fmt = "Data segment amount limit exceeded: limit={limit}, actual={actual}")]
109    DataSegmentsAmountLimit {
110        /// Limit of data segments.
111        limit: u32,
112        /// Actual amount of data segments.
113        actual: u32,
114    },
115}
116
117/// Table section error in WASM module.
118#[derive(Debug, derive_more::Display)]
119pub enum TableSectionError {
120    /// Number of table exceeds the limit.
121    #[display(fmt = "Number of table limit exceeded: limit={limit}, actual={actual}")]
122    TableNumberLimit {
123        /// Limit on the number of tables.
124        limit: u32,
125        /// Actual number of tables.
126        actual: u32,
127    },
128}
129
130/// Export error in WASM module.
131#[derive(Debug, derive_more::Display)]
132pub enum ExportError {
133    /// Incorrect global export index. Can occur when export refers to not existing global index.
134    #[display(fmt = "Global index `{_0}` in export index `{_1}` is incorrect")]
135    IncorrectGlobalIndex(u32, u32),
136    /// Exporting mutable globals is restricted by the Gear protocol.
137    #[display(fmt = "Global index `{_0}` in export index `{_1}` cannot be mutable")]
138    MutableGlobalExport(u32, u32),
139    /// Export references to an import function, which is not allowed.
140    #[display(fmt = "Export index `{_0}` references to imported function with index `{_1}`")]
141    ExportReferencesToImportFunction(u32, u32),
142    /// Export references to an import global, which is not allowed.
143    #[display(fmt = "Export index `{_0}` references to imported global with index `{_1}`")]
144    ExportReferencesToImportGlobal(u32, u32),
145    /// The signature of an exported function is invalid.
146    #[display(fmt = "Exported function with index `{_0}` must have signature `fn f() {{ ... }}`")]
147    InvalidExportFnSignature(u32),
148    /// The provided code contains excess function export.
149    #[display(fmt = "Excess export with index `{_0}` found")]
150    ExcessExport(u32),
151    /// The provided code doesn't contain the required `init` or `handle` export function.
152    #[display(fmt = "Required export function `init` or `handle` is not found")]
153    RequiredExportNotFound,
154}
155
156/// Import error in WASM module.
157#[derive(Debug, derive_more::Display)]
158pub enum ImportError {
159    /// The imported function is not supported by the Gear protocol.
160    #[display(fmt = "Unknown imported function with index `{_0}`")]
161    UnknownImport(u32),
162    /// The imported function is declared multiple times.
163    #[display(fmt = "Imported function with index `{_0}` is declared multiple times")]
164    DuplicateImport(u32),
165    /// The signature of an imported function is invalid.
166    #[display(fmt = "Invalid function signature for imported function with index `{_0}`")]
167    InvalidImportFnSignature(u32),
168    /// Unexpected import kind.
169    #[display(fmt = "Unexpected import kind `{kind}` with index `{index}`")]
170    UnexpectedImportKind {
171        /// Kind of the import.
172        kind: &'static &'static str,
173        /// Index of the import.
174        index: u32,
175    },
176}
177
178/// Module encode/decode error.
179#[derive(Debug, derive_more::Display)]
180pub enum CodecError {
181    /// The wasm bytecode is failed to be decoded
182    #[display(fmt = "The wasm bytecode is failed to be decoded: {_0}")]
183    Decode(SerializationError),
184    /// Failed to encode instrumented program
185    #[display(fmt = "Failed to encode instrumented program: {_0}")]
186    Encode(SerializationError),
187}
188
189/// Describes why the code is not valid Gear program.
190#[derive(Debug, derive_more::Display, derive_more::From)]
191pub enum CodeError {
192    /// Validation by wasmparser failed.
193    #[display(fmt = "Wasmer validation error: {_0}")]
194    Validation(BinaryReaderError),
195    /// Module encode/decode error.
196    #[display(fmt = "Codec error: {_0}")]
197    Codec(CodecError),
198    /// The provided code contains section error.
199    #[display(fmt = "Section error: {_0}")]
200    Section(SectionError),
201    /// The provided code contains memory error.
202    #[display(fmt = "Memory error: {_0}")]
203    Memory(MemoryError),
204    /// The provided code contains stack end error.
205    #[display(fmt = "Stack end error: {_0}")]
206    StackEnd(StackEndError),
207    /// The provided code contains data section error.
208    #[display(fmt = "Data section error: {_0}")]
209    DataSection(DataSectionError),
210    /// The provided code contains table section error.
211    #[display(fmt = "Table section error: {_0}")]
212    TableSection(TableSectionError),
213    /// The provided code contains export error.
214    #[display(fmt = "Export error: {_0}")]
215    Export(ExportError),
216    /// The provided code contains import error.
217    #[display(fmt = "Import error: {_0}")]
218    Import(ImportError),
219    /// Error occurred during instrumentation WASM module.
220    #[display(fmt = "Instrumentation error: {_0}")]
221    Instrumentation(InstrumentationError),
222}