1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/// Module for validating WASM modules against the expected WASI ABI.
pub mod is_valid {
/// Validates if unresolved WASI imports remain, ensuring no un-plugged custom WASIP1 imports are dropped silently.
pub fn validate_unresolved_imports(
unresolved_imports: &[&str],
wasm_names: &[impl AsRef<str>],
) -> eyre::Result<()> {
let mut err_wasm_names = HashMap::<_, Vec<_>>::new();
for i_name in unresolved_imports {
let name_str = i_name.strip_prefix("__wasip1_vfs_");
if let Some(name) = name_str {
if let Some(func_name) = name.strip_prefix("__self_") {
if let Ok(_func) = func_name.parse::<super::Wasip1ABIFunc>() {
// self is ignored if not plugged
continue;
}
}
if name == "wasi_thread_start_entry" {
continue;
}
if let Some((wasm_name, plugger, func_name)) = wasm_names.iter().find_map(|n| {
let func_name = name.strip_prefix(n.as_ref())?.strip_prefix("_")?;
if func_name == "thread_spawn" || func_name == "wasi_thread_start" {
return Some((
n.as_ref().to_string(),
Wasip1ABIPlugger::PlugThread,
func_name.to_string(),
));
}
let func: super::Wasip1ABIFunc = func_name.parse().ok()?;
Some((
n.as_ref().to_string(),
Wasip1ABIPlugger::from_variant(&func).unwrap(),
func.to_string(),
))
}) {
err_wasm_names
.entry((wasm_name, plugger))
.or_default()
.push(func_name);
} else {
return Err(eyre::eyre!(
"Invalid import: Failed to parse wasm target and WASI function name. \
This import is not a valid custom import or the function name is malformed: {}",
i_name
));
}
} else {
if let Ok(_func) = i_name.parse::<super::Wasip1ABIFunc>() {
// self is ignored if not plugged
continue;
} else {
return Err(eyre::eyre!(
"This import is not a valid this library custom import: {}",
i_name
));
}
}
}
let err_wasm_names = err_wasm_names
.into_iter()
.map(|((wasm_name, plugger), variants)| {
log::error!(
"Extra imports remain for `{wasm_name}`. You must use the `{plugger}!` macro plugger to export these functions: {}{}",
variants.iter().map(|v| v.to_string()).collect::<Vec<_>>().join(", "),
format!("\n Extra message: {}", plugger.get_message().unwrap_or(""))
);
wasm_name
})
.collect::<std::collections::HashSet<_>>();
if !err_wasm_names.is_empty() {
let mut names = err_wasm_names.into_iter().collect::<Vec<_>>();
names.sort();
Err(eyre::eyre!(
"Extra imports remain for `{names}`. This is not allowed in a component",
names = names.join(", ")
))
} else {
Ok(())
}
}
#[derive(
strum::EnumString,
strum::EnumIter,
Clone,
Copy,
Debug,
PartialEq,
Eq,
strum::Display,
Hash,
strum::EnumMessage,
)]
#[strum(serialize_all = "snake_case")]
/// Identifies specific WASI feature sets that need to be plugged during virtualization.
pub enum Wasip1ABIPlugger {
/// Plugins for command-line arguments.
PlugArgs,
/// Plugins for environment variables.
PlugEnv,
#[strum(
message = "Plug Fs is complex and difficult so you should see the documentation for more details."
)]
/// Plugins for file system operations.
PlugFs,
#[strum(message = "Plug Socks but this is not implemented")]
/// Plugins for socket operations (currently unimplemented).
PlugSocks,
/// Plugins for clock and timing operations.
PlugClock,
/// Plugins for random number generation.
PlugRandom,
#[strum(message = "Plug Process is default so this message should not be shown")]
/// Plugins for process-related operations.
PlugProcess,
/// Plugins for scheduler operations.
PlugSched,
/// Plugins for polling operations.
PlugPoll,
/// Plugins for thread operations.
PlugThread,
}
use std::collections::HashMap;
use strum::EnumMessage as _;
use super::{Wasip1ABIFunc, Wasip1ABIFunc::*};
impl Wasip1ABIPlugger {
const PLUG_ENV: &'static [Wasip1ABIFunc] = &[EnvironSizesGet, EnvironGet];
const PLUG_FS: &'static [Wasip1ABIFunc] = &[
FdAdvise,
FdAllocate,
FdDatasync,
FdFdstatSetFlags,
FdFdstatSetRights,
FdWrite,
FdPwrite,
FdReaddir,
FdClose,
FdPrestatGet,
FdPrestatDirName,
FdFilestatGet,
FdRead,
FdFdstatGet,
FdPread,
FdFilestatSetSize,
FdFilestatSetTimes,
FdRenumber,
FdSeek,
FdSync,
FdTell,
PathCreateDirectory,
PathFilestatGet,
PathFilestatSetTimes,
PathLink,
PathReadlink,
PathRemoveDirectory,
PathRename,
PathOpen,
PathSymlink,
PathUnlinkFile,
];
const PLUG_ARGS: &'static [Wasip1ABIFunc] = &[ArgsGet, ArgsSizesGet];
const PLUG_SOCKS: &'static [Wasip1ABIFunc] =
&[SockAccept, SockRecv, SockSend, SockShutdown];
const PLUG_CLOCK: &'static [Wasip1ABIFunc] = &[ClockTimeGet, ClockResGet];
const PLUG_RANDOM: &'static [Wasip1ABIFunc] = &[RandomGet];
const PLUG_PROCESS: &'static [Wasip1ABIFunc] = &[ProcExit];
const PLUG_SCHED: &'static [Wasip1ABIFunc] = &[SchedYield];
const PLUG_POLL: &'static [Wasip1ABIFunc] = &[PollOneoff];
/// Returns the list of `Wasip1ABIFunc` variants associated with this plugger.
pub const fn variants(self) -> &'static [Wasip1ABIFunc] {
match self {
Wasip1ABIPlugger::PlugEnv => Self::PLUG_ENV,
Wasip1ABIPlugger::PlugFs => Self::PLUG_FS,
Wasip1ABIPlugger::PlugArgs => Self::PLUG_ARGS,
Wasip1ABIPlugger::PlugSocks => Self::PLUG_SOCKS,
Wasip1ABIPlugger::PlugClock => Self::PLUG_CLOCK,
Wasip1ABIPlugger::PlugRandom => Self::PLUG_RANDOM,
Wasip1ABIPlugger::PlugProcess => Self::PLUG_PROCESS,
Wasip1ABIPlugger::PlugSched => Self::PLUG_SCHED,
Wasip1ABIPlugger::PlugPoll => Self::PLUG_POLL,
Wasip1ABIPlugger::PlugThread => &[],
}
}
/// Identifies the appropriate `Wasip1ABIPlugger` for a specific WASI function.
pub fn from_variant(func: &Wasip1ABIFunc) -> Option<Wasip1ABIPlugger> {
use strum::IntoEnumIterator;
for plugger in Self::iter() {
if plugger.variants().contains(func) {
return Some(plugger);
}
}
None
}
}
}
#[derive(
strum::EnumString, strum::VariantArray, strum::VariantNames, PartialEq, strum::Display,
)]
#[strum(serialize_all = "snake_case")]
/// Enumeration of all supported WASI preview1 functions.
pub enum Wasip1ABIFunc {
/// environ_sizes_get
EnvironSizesGet,
/// environ_get
EnvironGet,
/// proc_exit
ProcExit,
/// random_get
RandomGet,
/// sched_yield
SchedYield,
/// clock_time_get
ClockTimeGet,
/// clock_res_get
ClockResGet,
/// fd_advise
FdAdvise,
/// fd_allocate
FdAllocate,
/// fd_datasync
FdDatasync,
/// fd_fdstat_set_flags
FdFdstatSetFlags,
/// fd_fdstat_set_rights
FdFdstatSetRights,
/// fd_fdstat_get
FdFdstatGet,
/// fd_write
FdWrite,
/// fd_pwrite
FdPwrite,
/// fd_readdir
FdReaddir,
/// fd_close
FdClose,
/// fd_prestat_get
FdPrestatGet,
/// fd_prestat_dir_name
FdPrestatDirName,
/// fd_filestat_get
FdFilestatGet,
/// fd_read
FdRead,
/// fd_pread
FdPread,
/// fd_filestat_set_size
FdFilestatSetSize,
/// fd_filestat_set_times
FdFilestatSetTimes,
/// fd_renumber
FdRenumber,
/// fd_seek
FdSeek,
/// fd_sync
FdSync,
/// fd_tell
FdTell,
/// path_create_directory
PathCreateDirectory,
/// path_filestat_get
PathFilestatGet,
/// path_filestat_set_times
PathFilestatSetTimes,
/// path_link
PathLink,
/// path_readlink
PathReadlink,
/// path_remove_directory
PathRemoveDirectory,
/// path_rename
PathRename,
/// path_open
PathOpen,
/// path_symlink
PathSymlink,
/// path_unlink_file
PathUnlinkFile,
/// poll_oneoff
PollOneoff,
/// args_get
ArgsGet,
/// args_sizes_get
ArgsSizesGet,
/// sock_accept
SockAccept,
/// sock_recv
SockRecv,
/// sock_send
SockSend,
/// sock_shutdown
SockShutdown,
}
#[derive(
strum::EnumString, strum::VariantArray, strum::VariantNames, PartialEq, strum::Display,
)]
#[strum(serialize_all = "kebab_case")]
/// Enumeration of WASI threads functions.
pub enum Wasip1ThreadsABIFunc {
/// thread_spawn
ThreadSpawn,
}
#[derive(
strum::EnumString, strum::VariantArray, strum::VariantNames, PartialEq, strum::Display,
)]
#[strum(serialize_all = "snake_case")]
/// Enumeration of internal exports required by WASI threads.
pub enum Wasip1ThreadsABIExportFunc {
/// wasi_thread_start
WasiThreadStart,
/// wasi_thread_start_entry
WasiThreadStartEntry,
}