-
Notifications
You must be signed in to change notification settings - Fork 824
/
mod.rs
376 lines (332 loc) · 10.4 KB
/
mod.rs
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//! Unofficial API for WASI integrating with the standard Wasm C API.
//!
//! This API will be superseded by a standard WASI API when/if such a standard is created.
mod capture_files;
use super::{
externals::{wasm_extern_t, wasm_extern_vec_t, wasm_func_t, wasm_memory_t},
instance::wasm_instance_t,
module::wasm_module_t,
store::wasm_store_t,
};
// required due to really weird Rust resolution rules for macros
// https://github.com/rust-lang/rust/issues/57966
use crate::error::{update_last_error, CApiError};
use std::convert::TryFrom;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::slice;
use wasmer::{Extern, NamedResolver};
use wasmer_wasi::{
generate_import_object_from_env, get_wasi_version, WasiEnv, WasiFile, WasiState,
WasiStateBuilder, WasiVersion,
};
#[derive(Debug, Default)]
#[allow(non_camel_case_types)]
pub struct wasi_config_t {
inherit_stdout: bool,
inherit_stderr: bool,
inherit_stdin: bool,
/// cbindgen:ignore
state_builder: WasiStateBuilder,
}
#[no_mangle]
pub unsafe extern "C" fn wasi_config_new(
program_name: *const c_char,
) -> Option<Box<wasi_config_t>> {
debug_assert!(!program_name.is_null());
let name_c_str = CStr::from_ptr(program_name);
let prog_name = c_try!(name_c_str.to_str());
Some(Box::new(wasi_config_t {
state_builder: WasiState::new(prog_name),
..wasi_config_t::default()
}))
}
#[no_mangle]
pub unsafe extern "C" fn wasi_config_env(
config: &mut wasi_config_t,
key: *const c_char,
value: *const c_char,
) {
debug_assert!(!key.is_null());
debug_assert!(!value.is_null());
let key_cstr = CStr::from_ptr(key);
let key_bytes = key_cstr.to_bytes();
let value_cstr = CStr::from_ptr(value);
let value_bytes = value_cstr.to_bytes();
config.state_builder.env(key_bytes, value_bytes);
}
#[no_mangle]
pub unsafe extern "C" fn wasi_config_arg(config: &mut wasi_config_t, arg: *const c_char) {
debug_assert!(!arg.is_null());
let arg_cstr = CStr::from_ptr(arg);
let arg_bytes = arg_cstr.to_bytes();
config.state_builder.arg(arg_bytes);
}
#[no_mangle]
pub unsafe extern "C" fn wasi_config_preopen_dir(
config: &mut wasi_config_t,
dir: *const c_char,
) -> bool {
let dir_cstr = CStr::from_ptr(dir);
let dir_bytes = dir_cstr.to_bytes();
let dir_str = match std::str::from_utf8(dir_bytes) {
Ok(dir_str) => dir_str,
Err(e) => {
update_last_error(e);
return false;
}
};
if let Err(e) = config.state_builder.preopen_dir(dir_str) {
update_last_error(e);
return false;
}
true
}
#[no_mangle]
pub unsafe extern "C" fn wasi_config_mapdir(
config: &mut wasi_config_t,
alias: *const c_char,
dir: *const c_char,
) -> bool {
let alias_cstr = CStr::from_ptr(alias);
let alias_bytes = alias_cstr.to_bytes();
let alias_str = match std::str::from_utf8(alias_bytes) {
Ok(alias_str) => alias_str,
Err(e) => {
update_last_error(e);
return false;
}
};
let dir_cstr = CStr::from_ptr(dir);
let dir_bytes = dir_cstr.to_bytes();
let dir_str = match std::str::from_utf8(dir_bytes) {
Ok(dir_str) => dir_str,
Err(e) => {
update_last_error(e);
return false;
}
};
if let Err(e) = config.state_builder.map_dir(alias_str, dir_str) {
update_last_error(e);
return false;
}
true
}
#[no_mangle]
pub extern "C" fn wasi_config_inherit_stdout(config: &mut wasi_config_t) {
config.inherit_stdout = true;
}
#[no_mangle]
pub extern "C" fn wasi_config_inherit_stderr(config: &mut wasi_config_t) {
config.inherit_stderr = true;
}
#[no_mangle]
pub extern "C" fn wasi_config_inherit_stdin(config: &mut wasi_config_t) {
config.inherit_stdin = true;
}
#[allow(non_camel_case_types)]
pub struct wasi_env_t {
/// cbindgen:ignore
inner: WasiEnv,
}
/// Takes ownership over the `wasi_config_t`.
#[no_mangle]
pub extern "C" fn wasi_env_new(mut config: Box<wasi_config_t>) -> Option<Box<wasi_env_t>> {
if config.inherit_stdout {
config
.state_builder
.stdout(Box::new(capture_files::OutputCapturer::new()));
}
if config.inherit_stderr {
config
.state_builder
.stderr(Box::new(capture_files::OutputCapturer::new()));
}
// TODO: impl capturer for stdin
let wasi_state = c_try!(config.state_builder.build());
Some(Box::new(wasi_env_t {
inner: WasiEnv::new(wasi_state),
}))
}
#[no_mangle]
pub extern "C" fn wasi_env_delete(_state: Option<Box<wasi_env_t>>) {}
/// This function is deprecated. You may safely remove all calls to it and everything
/// will continue to work.
// Dead code: deprecate or remove
#[allow(unused_variables)]
#[no_mangle]
pub extern "C" fn wasi_env_set_instance(env: &mut wasi_env_t, instance: &wasm_instance_t) -> bool {
/*
let memory = if let Ok(memory) = instance.inner.exports.get_memory("memory") {
memory
} else {
return false;
};
env.inner.set_memory(memory.clone());
*/
true
}
/// This function is deprecated. You may safely remove all calls to it and everything
/// will continue to work.
// Dead code: deprecate or remove
#[allow(unused_variables)]
#[no_mangle]
pub extern "C" fn wasi_env_set_memory(env: &mut wasi_env_t, memory: &wasm_memory_t) {
//env.inner.set_memory(memory.inner.clone());
}
#[no_mangle]
pub unsafe extern "C" fn wasi_env_read_stdout(
env: &mut wasi_env_t,
buffer: *mut c_char,
buffer_len: usize,
) -> isize {
let inner_buffer = slice::from_raw_parts_mut(buffer as *mut _, buffer_len as usize);
let mut state = env.inner.state();
let stdout = if let Ok(stdout) = state.fs.stdout_mut() {
if let Some(stdout) = stdout.as_mut() {
stdout
} else {
update_last_error(CApiError {
msg: "could not find a file handle for `stdout`".to_string(),
});
return -1;
}
} else {
return -1;
};
read_inner(stdout, inner_buffer)
}
#[no_mangle]
pub unsafe extern "C" fn wasi_env_read_stderr(
env: &mut wasi_env_t,
buffer: *mut c_char,
buffer_len: usize,
) -> isize {
let inner_buffer = slice::from_raw_parts_mut(buffer as *mut _, buffer_len as usize);
let mut state = env.inner.state();
let stderr = if let Ok(stderr) = state.fs.stderr_mut() {
if let Some(stderr) = stderr.as_mut() {
stderr
} else {
update_last_error(CApiError {
msg: "could not find a file handle for `stderr`".to_string(),
});
return -1;
}
} else {
update_last_error(CApiError {
msg: "could not find a file handle for `stderr`".to_string(),
});
return -1;
};
read_inner(stderr, inner_buffer)
}
fn read_inner(wasi_file: &mut Box<dyn WasiFile>, inner_buffer: &mut [u8]) -> isize {
if let Some(oc) = wasi_file.downcast_mut::<capture_files::OutputCapturer>() {
let mut num_bytes_written = 0;
for (address, value) in inner_buffer.iter_mut().zip(oc.buffer.drain(..)) {
*address = value;
num_bytes_written += 1;
}
num_bytes_written
} else {
-1
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
#[allow(non_camel_case_types)]
pub enum wasi_version_t {
Latest = 0,
Snapshot0 = 1,
Snapshot1 = 2,
InvalidVersion = u32::max_value(),
}
impl From<WasiVersion> for wasi_version_t {
fn from(other: WasiVersion) -> Self {
match other {
WasiVersion::Snapshot0 => wasi_version_t::Snapshot0,
WasiVersion::Snapshot1 => wasi_version_t::Snapshot1,
WasiVersion::Latest => wasi_version_t::Latest,
}
}
}
impl TryFrom<wasi_version_t> for WasiVersion {
type Error = &'static str;
fn try_from(other: wasi_version_t) -> Result<Self, Self::Error> {
Ok(match other {
wasi_version_t::Snapshot0 => WasiVersion::Snapshot0,
wasi_version_t::Snapshot1 => WasiVersion::Snapshot1,
wasi_version_t::Latest => WasiVersion::Latest,
wasi_version_t::InvalidVersion => return Err("Invalid WASI version cannot be used"),
})
}
}
#[no_mangle]
pub unsafe extern "C" fn wasi_get_wasi_version(module: &wasm_module_t) -> wasi_version_t {
get_wasi_version(&module.inner, false)
.map(Into::into)
.unwrap_or(wasi_version_t::InvalidVersion)
}
/// Takes ownership of `wasi_env_t`.
#[no_mangle]
pub unsafe extern "C" fn wasi_get_imports(
store: &wasm_store_t,
module: &wasm_module_t,
wasi_env: &wasi_env_t,
imports: &mut wasm_extern_vec_t,
) -> bool {
wasi_get_imports_inner(store, module, wasi_env, imports).is_some()
}
/// Takes ownership of `wasi_env_t`.
unsafe fn wasi_get_imports_inner(
store: &wasm_store_t,
module: &wasm_module_t,
wasi_env: &wasi_env_t,
imports: &mut wasm_extern_vec_t,
) -> Option<()> {
let store = &store.inner;
let version = c_try!(
get_wasi_version(&module.inner, false).ok_or_else(|| CApiError {
msg: "could not detect a WASI version on the given module".to_string(),
})
);
let import_object = generate_import_object_from_env(store, wasi_env.inner.clone(), version);
*imports = module
.inner
.imports()
.map(|import_type| {
let export = c_try!(import_object
.resolve_by_name(import_type.module(), import_type.name())
.ok_or_else(|| CApiError {
msg: format!(
"Failed to resolve import \"{}\" \"{}\"",
import_type.module(),
import_type.name()
),
}));
let inner = Extern::from_vm_export(store, export);
Some(Box::new(wasm_extern_t {
instance: None,
inner,
}))
})
.collect::<Option<Vec<_>>>()?
.into();
Some(())
}
#[no_mangle]
pub unsafe extern "C" fn wasi_get_start_function(
instance: &mut wasm_instance_t,
) -> Option<Box<wasm_func_t>> {
let f = c_try!(instance.inner.exports.get_function("_start"));
Some(Box::new(wasm_func_t {
inner: f.clone(),
instance: Some(instance.inner.clone()),
}))
}
/// Delete a `wasm_extern_t` allocated by the API.
///
/// cbindgen:ignore
#[no_mangle]
pub unsafe extern "C" fn wasm_extern_delete(_item: Option<Box<wasm_extern_t>>) {}