Skip to main content

soil_client/executor/common/
error.rs

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