linera_witty/runtime/error.rs
1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Common error type for usage of different Wasm runtimes.
5
6use std::{num::TryFromIntError, string::FromUtf8Error};
7
8use thiserror::Error;
9
10/// Errors that can occur when using a Wasm runtime.
11#[derive(Debug, Error)]
12pub enum RuntimeError {
13 /// Attempt to allocate a buffer larger than `i32::MAX`.
14 #[error("Requested allocation size is too large")]
15 AllocationTooLarge,
16
17 /// Call to `cabi_realloc` returned a negative value instead of a valid address.
18 #[error("Memory allocation failed")]
19 AllocationFailed,
20
21 /// Attempt to deallocate an address that's after `i32::MAX`.
22 #[error("Attempt to deallocate an invalid address")]
23 DeallocateInvalidAddress,
24
25 /// Attempt to load a function not exported from a module.
26 #[error("Function `{_0}` could not be found in the module's exports")]
27 FunctionNotFound(String),
28
29 /// Attempt to load a function with a name that's used for a different import in the module.
30 #[error("Export `{_0}` is not a function")]
31 NotAFunction(String),
32
33 /// Attempt to load the memory export from a module that doesn't export it.
34 #[error("Failed to load `memory` export")]
35 MissingMemory,
36
37 /// Attempt to load the memory export from a module that exports it as something else.
38 #[error("Unexpected type for `memory` export")]
39 NotMemory,
40
41 /// Attempt to load a string from a sequence of bytes that doesn't contain a UTF-8 string.
42 #[error("Failed to load string from non-UTF-8 bytes")]
43 InvalidString(#[from] FromUtf8Error),
44
45 /// Attempt to create a `GuestPointer` from an invalid address representation.
46 #[error("Invalid address read")]
47 InvalidNumber(#[from] TryFromIntError),
48
49 /// Attempt to load an `enum` type but the discriminant doesn't match any of the variants.
50 #[error("Unexpected variant discriminant {discriminant} for `{type_name}`")]
51 InvalidVariant {
52 /// The `enum` type that failed being loaded.
53 type_name: &'static str,
54 /// The invalid discriminant that was received.
55 discriminant: i64,
56 },
57
58 /// A custom error reported by one of the Wasm host's function handlers.
59 #[error("Error reported by host function handler: {_0}")]
60 Custom(#[source] anyhow::Error),
61
62 /// Wasmer runtime error.
63 #[cfg(with_wasmer)]
64 #[error(transparent)]
65 Wasmer(#[from] wasmer::RuntimeError),
66
67 /// Attempt to access an invalid memory address using Wasmer.
68 #[cfg(with_wasmer)]
69 #[error(transparent)]
70 WasmerMemory(#[from] wasmer::MemoryAccessError),
71
72 /// Wasmtime error.
73 #[cfg(with_wasmtime)]
74 #[error(transparent)]
75 Wasmtime(anyhow::Error),
76
77 /// Wasmtime trap during execution.
78 #[cfg(with_wasmtime)]
79 #[error(transparent)]
80 WasmtimeTrap(#[from] wasmtime::Trap),
81}