Skip to main content

sc_executor_common/
error.rs

1// This file is part of Substrate.
2
3// Copyright (C) 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
21/// Result type alias.
22pub type Result<T> = std::result::Result<T, Error>;
23
24/// Error type.
25#[derive(Debug, thiserror::Error)]
26#[allow(missing_docs)]
27pub enum Error {
28	#[error("Error calling api function: {0}")]
29	ApiError(Box<dyn std::error::Error + Send + Sync>),
30
31	#[error("Method not found: '{0}'")]
32	MethodNotFound(String),
33
34	#[error("On-chain runtime does not specify version")]
35	VersionInvalid,
36
37	#[error("Externalities error")]
38	Externalities,
39
40	#[error("Invalid index provided")]
41	InvalidIndex,
42
43	#[error("Invalid type returned (should be u64)")]
44	InvalidReturn,
45
46	#[error("Runtime panicked: {0}")]
47	RuntimePanicked(String),
48
49	#[error("Invalid memory reference")]
50	InvalidMemoryReference,
51
52	#[error("The runtime doesn't provide a global named `__heap_base` of type `i32`")]
53	HeapBaseNotFoundOrInvalid,
54
55	#[error("The runtime must not have the `start` function defined")]
56	RuntimeHasStartFn,
57
58	#[error("Other: {0}")]
59	Other(String),
60
61	#[error(transparent)]
62	Allocator(#[from] sc_allocator::Error),
63
64	#[error("Host function {0} execution failed with: {1}")]
65	FunctionExecution(String, String),
66
67	#[error("No table exported by wasm blob")]
68	NoTable,
69
70	#[error("No table entry with index {0} in wasm blob exported table")]
71	NoTableEntryWithIndex(u32),
72
73	#[error("Table element with index {0} is not a function in wasm blob exported table")]
74	TableElementIsNotAFunction(u32),
75
76	#[error("Table entry with index {0} in wasm blob is null")]
77	FunctionRefIsNull(u32),
78
79	#[error(transparent)]
80	RuntimeConstruction(#[from] WasmError),
81
82	#[error("Shared memory is not supported")]
83	SharedMemUnsupported,
84
85	#[error("Imported globals are not supported yet")]
86	ImportedGlobalsUnsupported,
87
88	#[error("initializer expression can have only up to 2 expressions in wasm 1.0")]
89	InitializerHasTooManyExpressions,
90
91	#[error("Invalid initializer expression provided {0}")]
92	InvalidInitializerExpression(String),
93
94	#[error("Execution aborted due to panic: {0}")]
95	AbortedDueToPanic(MessageWithBacktrace),
96
97	#[error("Execution aborted due to trap: {0}")]
98	AbortedDueToTrap(MessageWithBacktrace),
99
100	#[error("Output exceeds bounds of wasm memory")]
101	OutputExceedsBounds,
102}
103
104impl From<&'static str> for Error {
105	fn from(err: &'static str) -> Error {
106		Error::Other(err.into())
107	}
108}
109
110impl From<String> for Error {
111	fn from(err: String) -> Error {
112		Error::Other(err)
113	}
114}
115
116/// Type for errors occurring during Wasm runtime construction.
117#[derive(Debug, thiserror::Error)]
118#[allow(missing_docs)]
119pub enum WasmError {
120	#[error("Code could not be read from the state.")]
121	CodeNotFound,
122
123	#[error("Failure to reinitialize runtime instance from snapshot.")]
124	ApplySnapshotFailed,
125
126	/// Failure to erase the wasm memory.
127	///
128	/// Depending on the implementation might mean failure of allocating memory.
129	#[error("Failure to erase the wasm memory: {0}")]
130	ErasingFailed(String),
131
132	#[error("Code failed validation: {0}")]
133	InvalidModule(String),
134
135	#[error("Wasm code could not be deserialized.")]
136	CantDeserializeWasm,
137
138	#[error("The module does not export a linear memory named `memory`.")]
139	InvalidMemory,
140
141	#[error("The number of heap pages requested is disallowed by the module.")]
142	InvalidHeapPages,
143
144	/// Instantiation error.
145	#[error("{0}")]
146	Instantiation(String),
147
148	/// Other error happened.
149	#[error("Other error happened while constructing the runtime: {0}")]
150	Other(String),
151}
152
153impl From<polkavm::program::ProgramParseError> for WasmError {
154	fn from(error: polkavm::program::ProgramParseError) -> Self {
155		WasmError::Other(error.to_string())
156	}
157}
158
159impl From<polkavm::Error> for WasmError {
160	fn from(error: polkavm::Error) -> Self {
161		WasmError::Other(error.to_string())
162	}
163}
164
165impl From<polkavm::CompileError> for WasmError {
166	fn from(error: polkavm::CompileError) -> Self {
167		match error {
168			polkavm::CompileError::ValidationFailed(msg) => WasmError::InvalidModule(msg),
169			polkavm::CompileError::Error(err) => WasmError::Other(err.to_string()),
170		}
171	}
172}
173
174impl From<polkavm::Error> for Error {
175	fn from(error: polkavm::Error) -> Self {
176		Error::Other(error.to_string())
177	}
178}
179
180/// An error message with an attached backtrace.
181#[derive(Debug)]
182pub struct MessageWithBacktrace {
183	/// The error message.
184	pub message: String,
185
186	/// The backtrace associated with the error message.
187	pub backtrace: Option<Backtrace>,
188}
189
190impl std::fmt::Display for MessageWithBacktrace {
191	fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
192		fmt.write_str(&self.message)?;
193		if let Some(ref backtrace) = self.backtrace {
194			fmt.write_str("\nWASM backtrace:\n")?;
195			backtrace.backtrace_string.fmt(fmt)?;
196		}
197
198		Ok(())
199	}
200}
201
202/// A WASM backtrace.
203#[derive(Debug)]
204pub struct Backtrace {
205	/// The string containing the backtrace.
206	pub backtrace_string: String,
207}
208
209impl std::fmt::Display for Backtrace {
210	fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
211		fmt.write_str(&self.backtrace_string)
212	}
213}