Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for the WASI ABI. #299

Merged
merged 92 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
be08154
Add wasi crate
lachlansneff Mar 28, 2019
0787d00
Add data support to import macro
lachlansneff Mar 28, 2019
e3a6b7c
Add skeleton for wasi abi
lachlansneff Mar 28, 2019
b7254ce
add stubs and dispatch
Mar 28, 2019
7b0992e
Instance now pulls state from the ImportObject
lachlansneff Mar 28, 2019
c045da3
Merge branch 'feature/wasi' of github.com:wasmerio/wasmer into featur…
lachlansneff Mar 28, 2019
94674e9
Add wasi state
lachlansneff Mar 28, 2019
bc863fc
hook up wasi to wasmer
Mar 28, 2019
72dd995
fix typo
Mar 28, 2019
dbc4176
Add env and args syscalls
lachlansneff Mar 28, 2019
256253a
Fix clippy lint
lachlansneff Mar 28, 2019
3c01c11
pass args and env vars to wasi
Mar 28, 2019
a69fdfe
implement wasi check
Mar 28, 2019
5c12fd0
fix test
Mar 28, 2019
e227b89
Merge #301
bors[bot] Mar 28, 2019
21304cb
Merge branch 'feature/wasi' into feature/hook-up-wasi-to-wasmer
MarkMcCaskey Mar 28, 2019
ce22818
add feature gate on import
Mar 28, 2019
3323c3c
Merge #300
bors[bot] Mar 28, 2019
bde6bdf
Add all wasi types
lachlansneff Mar 28, 2019
79133e5
Merge #303
bors[bot] Mar 28, 2019
aed9d3b
remove __wasi_ prefix
Mar 28, 2019
2982e6e
Merge #304
bors[bot] Mar 28, 2019
46f90d3
fix conditional compliation
Mar 28, 2019
9478ba7
actually fix it
Mar 28, 2019
72ec4ab
Merge #305
bors[bot] Mar 28, 2019
c45de22
Add helper types and half-ish of the wasi signatures
lachlansneff Mar 29, 2019
bd09343
add structure for cross-platform wasi syscall implementations
Mar 29, 2019
90db12e
Finish up signatures and converting function types
lachlansneff Mar 29, 2019
d9b89b4
Fix bug in deref of WasmPtr<T, Array>
lachlansneff Mar 29, 2019
39ccf40
Merge #306
bors[bot] Mar 29, 2019
ea27eff
keeep top level wasi calls that call out to platform-specific impls
Mar 29, 2019
d10d028
Merge branch 'feature/wasi' into feature/wasi-cross-platform-skeleton
MarkMcCaskey Mar 29, 2019
514432c
call wasi files correctly
Mar 29, 2019
b1030d3
Add prestat_t
lachlansneff Mar 29, 2019
4df5f02
Merge branch 'feature/wasi' of github.com:wasmerio/wasmer into featur…
lachlansneff Mar 29, 2019
23c09ac
add imports
Mar 29, 2019
48b5918
Merge branch 'master' into feature/wasi-fs
lachlansneff Mar 29, 2019
48d34d9
improve calling of platform-specific code and impl linux clock calls
Mar 29, 2019
de241a0
fix linux impl bugs
Mar 29, 2019
1f8b90b
probably actually fix linux for real though
Mar 29, 2019
28d9d1f
move linux impl to unix (it works on osx too!)
Mar 29, 2019
88212d3
implement random_get()
Mar 29, 2019
35fbf57
add pread on linux
Mar 29, 2019
5dcb95d
fix basic errors in linux impl
Mar 29, 2019
e7a5c01
fix backward enumerate
Mar 29, 2019
147d71a
implement ValueType for prestat_t
Mar 29, 2019
42e8523
impl ValueType for fdstat_t
Mar 29, 2019
5cee576
add some syscall skeletons; context switching
Mar 30, 2019
e156ea2
comment out write logic in linux fd_pread until design discussion
Mar 30, 2019
4108c8f
Merge pull request #307 from wasmerio/feature/wasi-cross-platform-ske…
MarkMcCaskey Apr 1, 2019
7addd92
add more stubs for fs calls
Apr 1, 2019
5b6856d
add lots of doc comments
Apr 1, 2019
68f1123
Add start of wasi fs
lachlansneff Apr 1, 2019
c12c7d5
Merge branch 'feature/wasi-fs' into feature/wasi
lachlansneff Apr 1, 2019
61dd2e1
add more doc comments
Apr 1, 2019
ce35e57
Change ValueType trait and add basic fs
lachlansneff Apr 1, 2019
23b1d1d
Merge branch 'feature/wasi' of github.com:wasmerio/wasmer into featur…
lachlansneff Apr 1, 2019
92ec719
Add wasi_try macro
lachlansneff Apr 1, 2019
2dd7ec8
fix it up
Apr 1, 2019
84dc20a
Merge branch 'feature/wasi' of github.com:wasmerio/wasmer into featur…
Apr 1, 2019
d164c7a
update wasi Cargo.toml
Apr 2, 2019
8bab9f1
init zbox first
lachlansneff Apr 2, 2019
287c81d
Misc fixes
lachlansneff Apr 2, 2019
6cec356
add debug lines to all wasi syscalls
Apr 2, 2019
4356293
get debug statements working; add some extra info
Apr 2, 2019
d421e91
implement some of fd_prestat_get
Apr 2, 2019
242f9f6
add hacked together impl of write for stdout and stderr
Apr 2, 2019
3a6e2c9
Change tagged and untagged methods
lachlansneff Apr 2, 2019
6278ced
implement fd_write for files
Apr 2, 2019
04a8073
Merge branch 'feature/wasi' of github.com:wasmerio/wasmer into featur…
Apr 2, 2019
a4547e3
update cursor in file when writing to it
Apr 2, 2019
0b9fc5a
add null termination to args for wasi
Apr 2, 2019
e61c03a
impl fd_read (untested)
Apr 2, 2019
2de5a5d
implement datasync
Apr 2, 2019
e180fd1
add set_rights syscall
Apr 2, 2019
fe4195f
impl set_flags on fd
Apr 2, 2019
697bdc7
add rights checking fn for future-proofing reasons
Apr 2, 2019
37371eb
implement most of fd_filestat_set_times
Apr 2, 2019
7d07b6f
impl fd_seek
Apr 2, 2019
dd7cfac
implement fd_tell
Apr 2, 2019
ce4676d
implement fd_renumber
Apr 2, 2019
f70b75e
kind of implement fd_pwrite
Apr 2, 2019
b80dd07
implement happy path of fd_open
Apr 3, 2019
7d728fc
implement path_filestat_get
Apr 3, 2019
d04d1bf
improve debug statements for arg syscalls
Apr 3, 2019
10696c4
clean up platform-specific syscall code
Apr 3, 2019
e9e7a33
fix warnings and let it build on windows
xmclark Apr 3, 2019
3b34ea6
Merge remote-tracking branch 'origin/feature/wasi' into feature/wasi
xmclark Apr 3, 2019
c1c99db
rename fs and fix (probable) bug in renumber syscall
Apr 4, 2019
207bd01
rename to destructor
xmclark Apr 4, 2019
2686322
Merge remote-tracking branch 'origin/feature/wasi' into feature/wasi
xmclark Apr 4, 2019
bbf663a
Merge branch 'master' into feature/wasi
xmclark Apr 4, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ wasmer-runtime-core = { path = "lib/runtime-core" }
wasmer-emscripten = { path = "lib/emscripten" }
wasmer-llvm-backend = { path = "lib/llvm-backend", optional = true }
wasmer-dynasm-backend = { path = "lib/dynasm-backend", optional = true }
wasmer-wasi = { path = "lib/wasi", optional = true }

[workspace]
members = ["lib/clif-backend", "lib/dynasm-backend", "lib/runtime", "lib/runtime-core", "lib/emscripten", "lib/spectests", "lib/win-exception-handler", "lib/runtime-c-api", "lib/llvm-backend"]
members = ["lib/clif-backend", "lib/dynasm-backend", "lib/runtime", "lib/runtime-core", "lib/emscripten", "lib/spectests", "lib/win-exception-handler", "lib/runtime-c-api", "lib/llvm-backend", "lib/wasi"]

[build-dependencies]
wabt = "0.7.2"
Expand All @@ -42,4 +43,5 @@ default = ["fast-tests"]
# This feature will allow cargo test to run much faster
fast-tests = []
llvm = ["wasmer-llvm-backend"]
dynasm = ["wasmer-dynasm-backend"]
dynasm = ["wasmer-dynasm-backend"]
wasi = ["wasmer-wasi"]
18 changes: 18 additions & 0 deletions lib/runtime-core/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use hashbrown::{hash_map::Entry, HashMap};
use std::collections::VecDeque;
use std::{
cell::{Ref, RefCell},
ffi::c_void,
rc::Rc,
};

Expand Down Expand Up @@ -45,16 +46,32 @@ impl IsExport for Export {
/// ```
pub struct ImportObject {
map: Rc<RefCell<HashMap<String, Box<dyn LikeNamespace>>>>,
state_creator: Option<Rc<Fn() -> (*mut c_void, fn(*mut c_void))>>,
}

impl ImportObject {
/// Create a new `ImportObject`.
pub fn new() -> Self {
Self {
map: Rc::new(RefCell::new(HashMap::new())),
state_creator: None,
}
}

pub fn new_with_data<F>(state_creator: F) -> Self
where
F: Fn() -> (*mut c_void, fn(*mut c_void)) + 'static,
{
Self {
map: Rc::new(RefCell::new(HashMap::new())),
state_creator: Some(Rc::new(state_creator)),
}
}

pub(crate) fn call_state_creator(&self) -> Option<(*mut c_void, fn(*mut c_void))> {
self.state_creator.as_ref().map(|state_gen| state_gen())
}

/// Register anything that implements `LikeNamespace` as a namespace.
///
/// # Usage:
Expand Down Expand Up @@ -98,6 +115,7 @@ impl ImportObject {
pub fn clone_ref(&self) -> Self {
Self {
map: Rc::clone(&self.map),
state_creator: self.state_creator.clone(),
}
}

Expand Down
11 changes: 10 additions & 1 deletion lib/runtime-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ impl Instance {
// Initialize the vm::Ctx in-place after the backing
// has been boxed.
unsafe {
*inner.vmctx = vm::Ctx::new(&mut inner.backing, &mut inner.import_backing, &module)
*inner.vmctx = match imports.call_state_creator() {
Some((data, dtor)) => vm::Ctx::new_with_data(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dtor? maybe destructor or finalizer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, either of those is fine. dtor was just short and it's common in the c++ world.

&mut inner.backing,
&mut inner.import_backing,
&module,
data,
dtor,
),
None => vm::Ctx::new(&mut inner.backing, &mut inner.import_backing, &module),
};
};

let instance = Instance {
Expand Down
22 changes: 22 additions & 0 deletions lib/runtime-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ macro_rules! func {
/// },
/// };
///
/// let imports_with_state = imports! {
/// || (0 as _, |_a| {}),
/// "env" => {
/// "foo" => func!(foo),
/// },
/// };
///
/// fn foo(_: &mut Ctx, n: i32) -> i32 {
/// n
/// }
Expand All @@ -57,6 +64,21 @@ macro_rules! imports {
import_object.register($ns_name, ns);
})*

import_object
}};
($state_gen:expr, $( $ns_name:expr => $ns:tt, )* ) => {{
use $crate::{
import::{ImportObject, Namespace},
};

let mut import_object = ImportObject::new_with_data($state_gen);

$({
let ns = $crate::__imports_internal!($ns);

import_object.register($ns_name, ns);
})*

import_object
}};
}
Expand Down
6 changes: 3 additions & 3 deletions lib/runtime-core/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Ctx {
module: *const ModuleInner,

pub data: *mut c_void,
pub data_finalizer: Option<extern "C" fn(data: *mut c_void)>,
pub data_finalizer: Option<fn(data: *mut c_void)>,
}

/// The internal context of the currently running WebAssembly instance.
Expand Down Expand Up @@ -100,7 +100,7 @@ impl Ctx {
import_backing: &mut ImportBacking,
module: &ModuleInner,
data: *mut c_void,
data_finalizer: extern "C" fn(*mut c_void),
data_finalizer: fn(*mut c_void),
) -> Self {
Self {
internal: InternalCtx {
Expand Down Expand Up @@ -481,7 +481,7 @@ mod vm_ctx_tests {
str: String,
}

extern "C" fn test_data_finalizer(data: *mut c_void) {
fn test_data_finalizer(data: *mut c_void) {
let test_data: &mut TestData = unsafe { &mut *(data as *mut TestData) };
assert_eq!(test_data.x, 10);
assert_eq!(test_data.y, true);
Expand Down
8 changes: 8 additions & 0 deletions lib/wasi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "wasmer-wasi"
version = "0.1.0"
authors = ["The Wasmer Engineering Team <engineering@wasmer.io>"]
edition = "2018"

[dependencies]
wasmer-runtime-core = { path = "../runtime-core", version = "0.2.1" }
83 changes: 83 additions & 0 deletions lib/wasi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
mod state;
mod syscalls;
mod utils;

use self::state::WasiState;
use self::syscalls::*;

use std::ffi::c_void;

pub use self::utils::is_wasi_module;

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

pub fn generate_import_object(args: Vec<Vec<u8>>, envs: Vec<Vec<u8>>) -> ImportObject {
let state_gen = move || {
fn state_dtor(data: *mut c_void) {
unsafe {
drop(Box::from_raw(data as *mut WasiState));
}
}

let state = Box::new(WasiState {
args: &args[..],
envs: &envs[..],
});

(
Box::leak(state) as *mut WasiState as *mut c_void,
state_dtor as fn(*mut c_void),
)
};
imports! {
// This generates the wasi state.
state_gen,
"wasi_unstable" => {
"__wasi_args_get" => func!(__wasi_args_get),
"__wasi_args_sizes_get" => func!(__wasi_args_sizes_get),
"__wasi_clock_res_get" => func!(__wasi_clock_res_get),
"__wasi_clock_time_get" => func!(__wasi_clock_time_get),
"__wasi_environ_get" => func!(__wasi_environ_get),
"__wasi_environ_sizes_get" => func!(__wasi_environ_sizes_get),
"__wasi_fd_advise" => func!(__wasi_fd_advise),
"__wasi_fd_allocate" => func!(__wasi_fd_allocate),
"__wasi_fd_close" => func!(__wasi_fd_close),
"__wasi_fd_datasync" => func!(__wasi_fd_datasync),
"__wasi_fd_fdstat_get" => func!(__wasi_fd_fdstat_get),
"__wasi_fd_fdstat_set_flags" => func!(__wasi_fd_fdstat_set_flags),
"__wasi_fd_fdstat_set_rights" => func!(__wasi_fd_fdstat_set_rights),
"__wasi_fd_filestat_get" => func!(__wasi_fd_filestat_get),
"__wasi_fd_filestat_set_size" => func!(__wasi_fd_filestat_set_size),
"__wasi_fd_filestat_set_times" => func!(__wasi_fd_filestat_set_times),
"__wasi_fd_pread" => func!(__wasi_fd_pread),
"__wasi_fd_prestat_get" => func!(__wasi_fd_prestat_get),
"__wasi_fd_prestat_dir_name" => func!(__wasi_fd_prestat_dir_name),
"__wasi_fd_pwrite" => func!(__wasi_fd_pwrite),
"__wasi_fd_read" => func!(__wasi_fd_read),
"__wasi_fd_readdir" => func!(__wasi_fd_readdir),
"__wasi_fd_renumber" => func!(__wasi_fd_renumber),
"__wasi_fd_seek" => func!(__wasi_fd_seek),
"__wasi_fd_sync" => func!(__wasi_fd_sync),
"__wasi_fd_tell" => func!(__wasi_fd_tell),
"__wasi_fd_write" => func!(__wasi_fd_write),
"__wasi_path_create_directory" => func!(__wasi_path_create_directory),
"__wasi_path_filestat_get" => func!(__wasi_path_filestat_get),
"__wasi_path_filestat_set_times" => func!(__wasi_path_filestat_set_times),
"__wasi_path_link" => func!(__wasi_path_link),
"__wasi_path_open" => func!(__wasi_path_open),
"__wasi_path_readlink" => func!(__wasi_path_readlink),
"__wasi_path_remove_directory" => func!(__wasi_path_remove_directory),
"__wasi_path_rename" => func!(__wasi_path_rename),
"__wasi_path_symlink" => func!(__wasi_path_symlink),
"__wasi_path_unlink_file" => func!(__wasi_path_unlink_file),
"__wasi_poll_oneoff" => func!(__wasi_poll_oneoff),
"__wasi_proc_exit" => func!(__wasi_proc_exit),
"__wasi_proc_raise" => func!(__wasi_proc_raise),
"__wasi_random_get" => func!(__wasi_random_get),
"__wasi_sched_yield" => func!(__wasi_sched_yield),
"__wasi_sock_recv" => func!(__wasi_sock_recv),
"__wasi_sock_send" => func!(__wasi_sock_send),
"__wasi_sock_shutdown" => func!(__wasi_sock_shutdown),
},
}
}
5 changes: 5 additions & 0 deletions lib/wasi/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub struct WasiState<'a> {
// vfs: Vfs,
pub args: &'a [Vec<u8>],
pub envs: &'a [Vec<u8>],
}
Loading