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
// This file is part of Gear.

// Copyright (C) 2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Module that describes various code errors.

pub use gear_wasm_instrument::{parity_wasm::SerializationError, InstrumentationError};
pub use wasmparser::BinaryReaderError;

/// Section name in WASM module.
#[derive(PartialEq, Eq, Debug, derive_more::Display)]
pub enum SectionName {
    /// Type section.
    #[display(fmt = "Type section")]
    Type,
    /// Import section.
    #[display(fmt = "Import section")]
    Import,
    /// Function (Code) section.
    #[display(fmt = "Function section")]
    Function,
    /// Data section.
    #[display(fmt = "Data section")]
    Data,
    /// Global section.
    #[display(fmt = "Global section")]
    Global,
    /// Table section.
    #[display(fmt = "Table section")]
    Table,
    /// Element section.
    #[display(fmt = "Element section")]
    Element,
    /// Export section.
    #[display(fmt = "Export section")]
    Export,
    /// Start section.
    #[display(fmt = "Start section")]
    Start,
}

/// Section error in WASM module.
#[derive(Debug, derive_more::Display)]
pub enum SectionError {
    /// Section not found.
    #[display(fmt = "{_0} not found")]
    NotFound(SectionName),
    /// Section not supported.
    #[display(fmt = "{_0} not supported")]
    NotSupported(SectionName),
}

/// Memory error in WASM module.
#[derive(Debug, derive_more::Display)]
pub enum MemoryError {
    /// Memory entry not found in import section.
    #[display(fmt = "Memory entry not found")]
    EntryNotFound,
    /// The WASM module has invalid count of static memory pages.
    #[display(fmt = "The WASM module has invalid count of static memory pages")]
    InvalidStaticPageCount,
}

/// Stack end error in WASM module.
#[derive(Debug, derive_more::Display)]
pub enum StackEndError {
    /// Unsupported initialization of gear stack end global variable.
    #[display(fmt = "Unsupported initialization of gear stack end global")]
    Initialization,
    /// Gear stack end offset is not aligned to wasm page size.
    #[display(fmt = "Gear stack end {_0:#x} is not aligned to wasm page size")]
    NotAligned(u32),
    /// Gear stack end is out of static memory.
    #[display(fmt = "Gear stack end {_0:#x} is out of static memory 0x0..{_1:#x}")]
    OutOfStatic(u32, u64),
}

/// Data section error in WASM module.
#[derive(Debug, derive_more::Display)]
pub enum DataSectionError {
    /// Unsupported initialization of data segment.
    #[display(fmt = "Unsupported initialization of data segment")]
    Initialization,
    /// Data section overlaps gear stack.
    #[display(fmt = "Data segment {_0:#x} overlaps gear stack 0x0..{_1:#x}")]
    GearStackOverlaps(u32, u32),
    /// Data segment end address is out of possible 32 bits address space.
    #[display(fmt = "Data segment {_0:#x} ends out of possible 32 bits address space")]
    EndAddressOverflow(u32),
    /// Data segment end address is out of static memory.
    #[display(fmt = "Data segment {_0:#x}..={_1:#x} is out of static memory 0x0..{_2:#x}")]
    EndAddressOutOfStaticMemory(u32, u32, u64),
    /// Data segment amount exceeds the limit.
    #[display(fmt = "Data segment amount limit exceeded: limit={limit}, actual={actual}")]
    DataSegmentsAmountLimit {
        /// Limit of data segments.
        limit: u32,
        /// Actual amount of data segments.
        actual: u32,
    },
}

/// Table section error in WASM module.
#[derive(Debug, derive_more::Display)]
pub enum TableSectionError {
    /// Number of table exceeds the limit.
    #[display(fmt = "Number of table limit exceeded: limit={limit}, actual={actual}")]
    TableNumberLimit {
        /// Limit on the number of tables.
        limit: u32,
        /// Actual number of tables.
        actual: u32,
    },
}

/// Export error in WASM module.
#[derive(Debug, derive_more::Display)]
pub enum ExportError {
    /// Incorrect global export index. Can occur when export refers to not existing global index.
    #[display(fmt = "Global index `{_0}` in export index `{_1}` is incorrect")]
    IncorrectGlobalIndex(u32, u32),
    /// Exporting mutable globals is restricted by the Gear protocol.
    #[display(fmt = "Global index `{_0}` in export index `{_1}` cannot be mutable")]
    MutableGlobalExport(u32, u32),
    /// Export references to an import function, which is not allowed.
    #[display(fmt = "Export index `{_0}` references to imported function with index `{_1}`")]
    ExportReferencesToImportFunction(u32, u32),
    /// Export references to an import global, which is not allowed.
    #[display(fmt = "Export index `{_0}` references to imported global with index `{_1}`")]
    ExportReferencesToImportGlobal(u32, u32),
    /// The signature of an exported function is invalid.
    #[display(fmt = "Exported function with index `{_0}` must have signature `fn f() {{ ... }}`")]
    InvalidExportFnSignature(u32),
    /// The provided code contains excess function export.
    #[display(fmt = "Excess export with index `{_0}` found")]
    ExcessExport(u32),
    /// The provided code doesn't contain the required `init` or `handle` export function.
    #[display(fmt = "Required export function `init` or `handle` is not found")]
    RequiredExportNotFound,
}

/// Import error in WASM module.
#[derive(Debug, derive_more::Display)]
pub enum ImportError {
    /// The imported function is not supported by the Gear protocol.
    #[display(fmt = "Unknown imported function with index `{_0}`")]
    UnknownImport(u32),
    /// The imported function is declared multiple times.
    #[display(fmt = "Imported function with index `{_0}` is declared multiple times")]
    DuplicateImport(u32),
    /// The signature of an imported function is invalid.
    #[display(fmt = "Invalid function signature for imported function with index `{_0}`")]
    InvalidImportFnSignature(u32),
    /// Unexpected import kind.
    #[display(fmt = "Unexpected import kind `{kind}` with index `{index}`")]
    UnexpectedImportKind {
        /// Kind of the import.
        kind: &'static &'static str,
        /// Index of the import.
        index: u32,
    },
}

/// Module encode/decode error.
#[derive(Debug, derive_more::Display)]
pub enum CodecError {
    /// The wasm bytecode is failed to be decoded
    #[display(fmt = "The wasm bytecode is failed to be decoded: {_0}")]
    Decode(SerializationError),
    /// Failed to encode instrumented program
    #[display(fmt = "Failed to encode instrumented program: {_0}")]
    Encode(SerializationError),
}

/// Describes why the code is not valid Gear program.
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum CodeError {
    /// Validation by wasmparser failed.
    #[display(fmt = "Wasmer validation error: {_0}")]
    Validation(BinaryReaderError),
    /// Module encode/decode error.
    #[display(fmt = "Codec error: {_0}")]
    Codec(CodecError),
    /// The provided code contains section error.
    #[display(fmt = "Section error: {_0}")]
    Section(SectionError),
    /// The provided code contains memory error.
    #[display(fmt = "Memory error: {_0}")]
    Memory(MemoryError),
    /// The provided code contains stack end error.
    #[display(fmt = "Stack end error: {_0}")]
    StackEnd(StackEndError),
    /// The provided code contains data section error.
    #[display(fmt = "Data section error: {_0}")]
    DataSection(DataSectionError),
    /// The provided code contains table section error.
    #[display(fmt = "Table section error: {_0}")]
    TableSection(TableSectionError),
    /// The provided code contains export error.
    #[display(fmt = "Export error: {_0}")]
    Export(ExportError),
    /// The provided code contains import error.
    #[display(fmt = "Import error: {_0}")]
    Import(ImportError),
    /// Error occurred during instrumentation WASM module.
    #[display(fmt = "Instrumentation error: {_0}")]
    Instrumentation(InstrumentationError),
}