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
#![deny(
    dead_code,
    nonstandard_style,
    unused_imports,
    unused_mut,
    unused_variables,
    unused_unsafe,
    unreachable_patterns,
    clippy::missing_safety_doc
)]
#![doc(html_favicon_url = "https://wasmer.io/static/icons/favicon.ico")]
#![doc(html_logo_url = "https://avatars3.githubusercontent.com/u/44205449?s=200&v=4")]

//! Wasmer's WASI implementation
//!
//! Use `generate_import_object` to create an [`ImportObject`].  This [`ImportObject`]
//! can be combined with a module to create an `Instance` which can execute WASI
//! Wasm functions.
//!
//! See `state` for the experimental WASI FS API.  Also see the
//! [WASI plugin example](https://github.com/wasmerio/wasmer/blob/master/examples/plugin.rs)
//! for an example of how to extend WASI using the WASI FS API.

#[cfg(target = "windows")]
extern crate winapi;
#[macro_use]
extern crate log;

#[macro_use]
mod macros;
mod ptr;
pub mod state;
mod syscalls;
mod utils;

use self::state::{WasiFs, WasiState};
pub use self::syscalls::types;
use self::syscalls::*;

use std::ffi::c_void;
use std::path::PathBuf;

pub use self::utils::{get_wasi_version, is_wasi_module, WasiVersion};

use wasmer_runtime_core::{func, import::ImportObject, imports};

/// This is returned in `RuntimeError`.
/// Use `downcast` or `downcast_ref` to retrieve the `ExitCode`.
pub struct ExitCode {
    pub code: syscalls::types::__wasi_exitcode_t,
}

/// Creates a Wasi [`ImportObject`] with [`WasiState`] with the latest snapshot
/// of WASI.
pub fn generate_import_object(
    args: Vec<Vec<u8>>,
    envs: Vec<Vec<u8>>,
    preopened_files: Vec<PathBuf>,
    mapped_dirs: Vec<(String, PathBuf)>,
) -> Result<ImportObject, String> {
    // it is just for check that WasiFs could be created
    #[allow(deprecated)]
    let _ = WasiFs::new(&preopened_files, &mapped_dirs)?;

    let state_gen = move || {
        // TODO: look into removing all these unnecessary clones
        fn state_destructor(data: *mut c_void) {
            unsafe {
                drop(Box::from_raw(data as *mut WasiState));
            }
        }
        let preopened_files = preopened_files.clone();
        let mapped_dirs = mapped_dirs.clone();

        // this deprecation warning only applies to external callers
        #[allow(deprecated)]
        let state = Box::new(WasiState {
            fs: WasiFs::new(&preopened_files, &mapped_dirs)
                .expect("WasiFs's been already checked for creation"),
            args: args.clone(),
            envs: envs.clone(),
        });

        (
            Box::into_raw(state) as *mut c_void,
            state_destructor as fn(*mut c_void),
        )
    };

    Ok(generate_import_object_snapshot1_inner(state_gen))
}

/// Create an [`ImportObject`] with an existing [`WasiState`]. [`WasiState`]
/// can be constructed from a [`WasiStateBuilder`](state::WasiStateBuilder).
pub fn generate_import_object_from_state(
    wasi_state: WasiState,
    version: WasiVersion,
) -> ImportObject {
    // HACK(mark): this is really quite nasty and inefficient, a proper fix will
    //             require substantial changes to the internals of the WasiFS
    // copy WasiState by serializing and deserializing
    let wasi_state_bytes = wasi_state.freeze().unwrap();
    let state_gen = move || {
        fn state_destructor(data: *mut c_void) {
            unsafe {
                drop(Box::from_raw(data as *mut WasiState));
            }
        }

        let wasi_state = Box::new(WasiState::unfreeze(&wasi_state_bytes).unwrap());

        (
            Box::into_raw(wasi_state) as *mut c_void,
            state_destructor as fn(*mut c_void),
        )
    };
    match version {
        WasiVersion::Snapshot0 => generate_import_object_snapshot0_inner(state_gen),
        WasiVersion::Snapshot1 | WasiVersion::Latest => {
            generate_import_object_snapshot1_inner(state_gen)
        }
    }
}

/// Creates a Wasi [`ImportObject`] with [`WasiState`] for the given [`WasiVersion`].
pub fn generate_import_object_for_version(
    version: WasiVersion,
    args: Vec<Vec<u8>>,
    envs: Vec<Vec<u8>>,
    preopened_files: Vec<PathBuf>,
    mapped_dirs: Vec<(String, PathBuf)>,
) -> Result<ImportObject, String> {
    match version {
        WasiVersion::Snapshot0 => {
            generate_import_object_snapshot0(args, envs, preopened_files, mapped_dirs)
        }
        WasiVersion::Snapshot1 | WasiVersion::Latest => {
            generate_import_object(args, envs, preopened_files, mapped_dirs)
        }
    }
}

/// Creates a legacy Wasi [`ImportObject`] with [`WasiState`].
fn generate_import_object_snapshot0(
    args: Vec<Vec<u8>>,
    envs: Vec<Vec<u8>>,
    preopened_files: Vec<PathBuf>,
    mapped_dirs: Vec<(String, PathBuf)>,
) -> Result<ImportObject, String> {
    // it is just for check that WasiFs could be created
    #[allow(deprecated)]
    let _ = WasiFs::new(&preopened_files, &mapped_dirs)?;

    let state_gen = move || {
        // TODO: look into removing all these unnecessary clones
        fn state_destructor(data: *mut c_void) {
            unsafe {
                drop(Box::from_raw(data as *mut WasiState));
            }
        }
        // let preopened_files = preopened_files.clone();
        // let mapped_dirs = mapped_dirs.clone();
        //let wasi_builder = create_wasi_instance();

        // this deprecation warning only applies to external callers
        #[allow(deprecated)]
        let state = Box::new(WasiState {
            fs: WasiFs::new(&preopened_files, &mapped_dirs)
                .expect("it's been already checked that WasiFs can be created"),
            args: args.clone(),
            envs: envs.clone(),
        });

        (
            Box::into_raw(state) as *mut c_void,
            state_destructor as fn(*mut c_void),
        )
    };
    Ok(generate_import_object_snapshot0_inner(state_gen))
}

/// Combines a state generating function with the import list for legacy WASI
fn generate_import_object_snapshot0_inner<F>(state_gen: F) -> ImportObject
where
    F: Fn() -> (*mut c_void, fn(*mut c_void)) + Send + Sync + 'static,
{
    imports! {
        state_gen,
        "wasi_unstable" => {
            "args_get" => func!(args_get),
            "args_sizes_get" => func!(args_sizes_get),
            "clock_res_get" => func!(clock_res_get),
            "clock_time_get" => func!(clock_time_get),
            "environ_get" => func!(environ_get),
            "environ_sizes_get" => func!(environ_sizes_get),
            "fd_advise" => func!(fd_advise),
            "fd_allocate" => func!(fd_allocate),
            "fd_close" => func!(fd_close),
            "fd_datasync" => func!(fd_datasync),
            "fd_fdstat_get" => func!(fd_fdstat_get),
            "fd_fdstat_set_flags" => func!(fd_fdstat_set_flags),
            "fd_fdstat_set_rights" => func!(fd_fdstat_set_rights),
            "fd_filestat_get" => func!(legacy::snapshot0::fd_filestat_get),
            "fd_filestat_set_size" => func!(fd_filestat_set_size),
            "fd_filestat_set_times" => func!(fd_filestat_set_times),
            "fd_pread" => func!(fd_pread),
            "fd_prestat_get" => func!(fd_prestat_get),
            "fd_prestat_dir_name" => func!(fd_prestat_dir_name),
            "fd_pwrite" => func!(fd_pwrite),
            "fd_read" => func!(fd_read),
            "fd_readdir" => func!(fd_readdir),
            "fd_renumber" => func!(fd_renumber),
            "fd_seek" => func!(legacy::snapshot0::fd_seek),
            "fd_sync" => func!(fd_sync),
            "fd_tell" => func!(fd_tell),
            "fd_write" => func!(fd_write),
            "path_create_directory" => func!(path_create_directory),
            "path_filestat_get" => func!(legacy::snapshot0::path_filestat_get),
            "path_filestat_set_times" => func!(path_filestat_set_times),
            "path_link" => func!(path_link),
            "path_open" => func!(path_open),
            "path_readlink" => func!(path_readlink),
            "path_remove_directory" => func!(path_remove_directory),
            "path_rename" => func!(path_rename),
            "path_symlink" => func!(path_symlink),
            "path_unlink_file" => func!(path_unlink_file),
            "poll_oneoff" => func!(legacy::snapshot0::poll_oneoff),
            "proc_exit" => func!(proc_exit),
            "proc_raise" => func!(proc_raise),
            "random_get" => func!(random_get),
            "sched_yield" => func!(sched_yield),
            "sock_recv" => func!(sock_recv),
            "sock_send" => func!(sock_send),
            "sock_shutdown" => func!(sock_shutdown),
        },
    }
}

/// Combines a state generating function with the import list for snapshot 1
fn generate_import_object_snapshot1_inner<F>(state_gen: F) -> ImportObject
where
    F: Fn() -> (*mut c_void, fn(*mut c_void)) + Send + Sync + 'static,
{
    imports! {
            state_gen,
            "wasi_snapshot_preview1" => {
                "args_get" => func!(args_get),
                "args_sizes_get" => func!(args_sizes_get),
                "clock_res_get" => func!(clock_res_get),
                "clock_time_get" => func!(clock_time_get),
                "environ_get" => func!(environ_get),
                "environ_sizes_get" => func!(environ_sizes_get),
                "fd_advise" => func!(fd_advise),
                "fd_allocate" => func!(fd_allocate),
                "fd_close" => func!(fd_close),
                "fd_datasync" => func!(fd_datasync),
                "fd_fdstat_get" => func!(fd_fdstat_get),
                "fd_fdstat_set_flags" => func!(fd_fdstat_set_flags),
                "fd_fdstat_set_rights" => func!(fd_fdstat_set_rights),
                "fd_filestat_get" => func!(fd_filestat_get),
                "fd_filestat_set_size" => func!(fd_filestat_set_size),
                "fd_filestat_set_times" => func!(fd_filestat_set_times),
                "fd_pread" => func!(fd_pread),
                "fd_prestat_get" => func!(fd_prestat_get),
                "fd_prestat_dir_name" => func!(fd_prestat_dir_name),
                "fd_pwrite" => func!(fd_pwrite),
                "fd_read" => func!(fd_read),
                "fd_readdir" => func!(fd_readdir),
                "fd_renumber" => func!(fd_renumber),
                "fd_seek" => func!(fd_seek),
                "fd_sync" => func!(fd_sync),
                "fd_tell" => func!(fd_tell),
                "fd_write" => func!(fd_write),
                "path_create_directory" => func!(path_create_directory),
                "path_filestat_get" => func!(path_filestat_get),
                "path_filestat_set_times" => func!(path_filestat_set_times),
                "path_link" => func!(path_link),
                "path_open" => func!(path_open),
                "path_readlink" => func!(path_readlink),
                "path_remove_directory" => func!(path_remove_directory),
                "path_rename" => func!(path_rename),
                "path_symlink" => func!(path_symlink),
                "path_unlink_file" => func!(path_unlink_file),
                "poll_oneoff" => func!(poll_oneoff),
                "proc_exit" => func!(proc_exit),
                "proc_raise" => func!(proc_raise),
                "random_get" => func!(random_get),
                "sched_yield" => func!(sched_yield),
                "sock_recv" => func!(sock_recv),
                "sock_send" => func!(sock_send),
                "sock_shutdown" => func!(sock_shutdown),
            },
    }
}