wasmer_wasix/state/linker/
error.rs

1use std::path::PathBuf;
2
3use crate::SpawnError;
4use virtual_fs::FsError;
5use wasmer::{ExportError, ExternType, InstantiationError, MemoryError, RuntimeError};
6
7use super::ModuleHandle;
8
9#[derive(thiserror::Error, Debug)]
10pub enum LinkError {
11    #[error("Cannot access linker through a dead instance group")]
12    InstanceGroupIsDead,
13
14    #[error("Main module is missing a required import: {0}")]
15    MissingMainModuleImport(String),
16
17    #[error("Failed to spawn module: {0}")]
18    SpawnError(#[from] SpawnError),
19
20    #[error("Failed to instantiate module: {0}")]
21    InstantiationError(#[from] InstantiationError),
22
23    #[error("Memory allocation error: {0}")]
24    MemoryAllocationError(#[from] MemoryError),
25
26    #[error("Failed to allocate function table indices: {0}")]
27    TableAllocationError(RuntimeError),
28
29    #[error("Failed to find shared library {0}: {1}")]
30    SharedLibraryMissing(String, LocateModuleError),
31
32    #[error("Module is not a dynamic library")]
33    NotDynamicLibrary,
34
35    #[error("Module's memory is not shared")]
36    MemoryNotShared,
37
38    #[error("Failed to parse dylink.0 section: {0}")]
39    Dylink0SectionParseError(#[from] wasmparser::BinaryReaderError),
40
41    #[error("Unresolved global '{0}'.{1} due to: {2}")]
42    UnresolvedGlobal(String, String, Box<ResolveError>),
43
44    #[error("Failed to update global {0} due to: {1}")]
45    GlobalUpdateFailed(String, RuntimeError),
46
47    #[error("Expected global to be of type I32 or I64: '{0}'.{1}")]
48    NonIntegerGlobal(String, String),
49
50    #[error("Bad known import: '{0}'.{1} of type {2:?}")]
51    BadImport(String, String, ExternType),
52
53    #[error(
54        "Import could not be satisfied because of type mismatch: '{0}'.{1}, expected {2:?}, found {3:?}"
55    )]
56    ImportTypeMismatch(String, String, ExternType, ExternType),
57
58    #[error("Expected import to be a function: '{0}'.{1}")]
59    ImportMustBeFunction(&'static str, String),
60
61    #[error("Expected export {0} to be a function, found: {1:?}")]
62    ExportMustBeFunction(String, ExternType),
63
64    #[error("Failed to initialize instance: {0}")]
65    InitializationError(anyhow::Error),
66
67    #[error("Runtime hook failed: {0}")]
68    RuntimeHookError(anyhow::Error),
69
70    #[error("Initialization function has invalid signature: {0}")]
71    InitFuncWithInvalidSignature(String),
72
73    #[error("Initialization function {0} failed to run: {1}")]
74    InitFunctionFailed(String, RuntimeError),
75
76    #[error("Failed to initialize WASI(X) module handles: {0}")]
77    MainModuleHandleInitFailed(ExportError),
78
79    #[error("Bad __tls_base export, expected a global of type I32 or I64")]
80    BadTlsBaseExport,
81
82    #[error(
83        "TLS symbol {0} cannot be resolved from module {1} because it does not export its __tls_base"
84    )]
85    MissingTlsBaseExport(String, ModuleHandle),
86}
87
88#[derive(Debug)]
89pub enum LocateModuleError {
90    Single(FsError),
91    Multiple(Vec<(PathBuf, FsError)>),
92}
93
94impl std::fmt::Display for LocateModuleError {
95    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96        match self {
97            LocateModuleError::Single(e) => std::fmt::Display::fmt(&e, f),
98            LocateModuleError::Multiple(errors) => {
99                for (path, error) in errors {
100                    write!(f, "\n    {}: {}", path.display(), error)?;
101                }
102                Ok(())
103            }
104        }
105    }
106}
107
108#[derive(thiserror::Error, Debug)]
109pub enum ResolveError {
110    #[error("Linker not initialized")]
111    NotInitialized,
112
113    #[error("Invalid module handle")]
114    InvalidModuleHandle,
115
116    #[error("Missing export")]
117    MissingExport,
118
119    #[error("Invalid export type: {0:?}")]
120    InvalidExportType(ExternType),
121
122    #[error("Failed to allocate function table indices: {0}")]
123    TableAllocationError(RuntimeError),
124
125    #[error("Cannot access linker through a dead instance group")]
126    InstanceGroupIsDead,
127
128    #[error("Failed to perform pending DL operation: {0}")]
129    PendingDlOperationFailed(#[from] LinkError),
130
131    #[error("Module must export its __tls_base for exported TLS symbols to be resolved correctly")]
132    NoTlsBaseGlobalExport,
133}