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    /// Attempt to allocate a buffer that's aligned to an invalid boundary.
18    #[error("Requested allocation alignment is invalid")]
19    InvalidAlignment,
20
21    /// Call to `cabi_realloc` returned a negative value instead of a valid address.
22    #[error("Memory allocation failed")]
23    AllocationFailed,
24
25    /// Attempt to deallocate an address that's after `i32::MAX`.
26    #[error("Attempt to deallocate an invalid address")]
27    DeallocateInvalidAddress,
28
29    /// Attempt to load a function not exported from a module.
30    #[error("Function `{_0}` could not be found in the module's exports")]
31    FunctionNotFound(String),
32
33    /// Attempt to load a function with a name that's used for a different import in the module.
34    #[error("Export `{_0}` is not a function")]
35    NotAFunction(String),
36
37    /// Attempt to load the memory export from a module that doesn't export it.
38    #[error("Failed to load `memory` export")]
39    MissingMemory,
40
41    /// Attempt to load the memory export from a module that exports it as something else.
42    #[error("Unexpected type for `memory` export")]
43    NotMemory,
44
45    /// Attempt to load a string from a sequence of bytes that doesn't contain a UTF-8 string.
46    #[error("Failed to load string from non-UTF-8 bytes: {0}")]
47    InvalidString(#[from] FromUtf8Error),
48
49    /// Attempt to create a `GuestPointer` from an invalid address representation.
50    #[error("Invalid address read: {0}")]
51    InvalidNumber(#[from] TryFromIntError),
52
53    /// Attempt to load an `enum` type but the discriminant doesn't match any of the variants.
54    #[error("Unexpected variant discriminant {discriminant} for `{type_name}`")]
55    InvalidVariant {
56        /// The `enum` type that failed being loaded.
57        type_name: &'static str,
58        /// The invalid discriminant that was received.
59        discriminant: i64,
60    },
61
62    /// A custom error reported by one of the Wasm host's function handlers.
63    #[error("Error reported by host function handler: {_0}")]
64    Custom(#[source] anyhow::Error),
65
66    /// Wasmer runtime error.
67    #[cfg(with_wasmer)]
68    #[error(transparent)]
69    Wasmer(#[from] wasmer::RuntimeError),
70
71    /// Attempt to access an invalid memory address using Wasmer.
72    #[cfg(with_wasmer)]
73    #[error(transparent)]
74    WasmerMemory(#[from] wasmer::MemoryAccessError),
75
76    /// Wasmtime error.
77    #[cfg(with_wasmtime)]
78    #[error(transparent)]
79    Wasmtime(anyhow::Error),
80
81    /// Wasmtime trap during execution.
82    #[cfg(with_wasmtime)]
83    #[error(transparent)]
84    WasmtimeTrap(#[from] wasmtime::Trap),
85}