tc_executor_common/
error.rs

1// This file is part of Tetcore.
2
3// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
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//! Rust executor possible errors.
20
21use serializer;
22use twasmi;
23
24/// Result type alias.
25pub type Result<T> = std::result::Result<T, Error>;
26
27/// Error type.
28#[derive(Debug, thiserror::Error)]
29#[allow(missing_docs)]
30pub enum Error {
31	#[error("Unserializable data encountered")]
32	InvalidData(#[from] serializer::Error),
33
34	#[error(transparent)]
35	Trap(#[from] twasmi::Trap),
36
37	#[error(transparent)]
38	Wasmi(#[from] twasmi::Error),
39
40	#[error("API Error: {0}")]
41	ApiError(String),
42
43	#[error("Method not found: '{0}'")]
44	MethodNotFound(String),
45
46	#[error("Invalid Code (expected single byte): '{0}'")]
47	InvalidCode(String),
48
49	#[error("On-chain runtime does not specify version")]
50	VersionInvalid,
51
52	#[error("Externalities error")]
53	Externalities,
54
55	#[error("Invalid index provided")]
56	InvalidIndex,
57
58	#[error("Invalid type returned (should be u64)")]
59	InvalidReturn,
60
61	#[error("Runtime error")]
62	Runtime,
63
64	#[error("Runtime panicked: {0}")]
65	RuntimePanicked(String),
66
67	#[error("Invalid memory reference")]
68	InvalidMemoryReference,
69
70	#[error("The runtime doesn't provide a global named `__heap_base` of type `i32`")]
71	HeapBaseNotFoundOrInvalid,
72
73	#[error("The runtime must not have the `start` function defined")]
74	RuntimeHasStartFn,
75
76	#[error("Other: {0}")]
77	Other(String),
78
79	#[error(transparent)]
80	Allocator(#[from] tp_allocator::Error),
81
82	#[error("Host function {0} execution failed with: {1}")]
83	FunctionExecution(String, String),
84
85	#[error("No table exported by wasm blob")]
86	NoTable,
87
88	#[error("No table entry with index {0} in wasm blob exported table")]
89	NoTableEntryWithIndex(u32),
90
91	#[error("Table element with index {0} is not a function in wasm blob exported table")]
92	TableElementIsNotAFunction(u32),
93
94	#[error("Table entry with index {0} in wasm blob is null")]
95	FunctionRefIsNull(u32),
96
97	#[error(transparent)]
98	RuntimeConstruction(#[from] WasmError),
99
100	#[error("Shared memory is not supported")]
101	SharedMemUnsupported,
102
103	#[error("Imported globals are not supported yet")]
104	ImportedGlobalsUnsupported,
105
106	#[error("initializer expression can have only up to 2 expressions in wasm 1.0")]
107	InitializerHasTooManyExpressions,
108
109	#[error("Invalid initializer expression provided {0}")]
110	InvalidInitializerExpression(String),
111}
112
113impl twasmi::HostError for Error {}
114
115impl From<&'static str> for Error {
116	fn from(err: &'static str) -> Error {
117		Error::Other(err.into())
118	}
119}
120
121impl From<String> for Error {
122	fn from(err: String) -> Error {
123		Error::Other(err)
124	}
125}
126
127/// Type for errors occurring during Wasm runtime construction.
128#[derive(Debug, derive_more::Display)]
129pub enum WasmError {
130	/// Code could not be read from the state.
131	CodeNotFound,
132	/// Failure to reinitialize runtime instance from snapshot.
133	ApplySnapshotFailed,
134	/// Failure to erase the wasm memory.
135	///
136	/// Depending on the implementation might mean failure of allocating memory.
137	ErasingFailed(String),
138	/// Wasm code failed validation.
139	InvalidModule,
140	/// Wasm code could not be deserialized.
141	CantDeserializeWasm,
142	/// The module does not export a linear memory named `memory`.
143	InvalidMemory,
144	/// The number of heap pages requested is disallowed by the module.
145	InvalidHeapPages,
146	/// Instantiation error.
147	Instantiation(String),
148	/// Other error happenend.
149	Other(String),
150}
151
152impl std::error::Error for WasmError {}