Skip to content

Commit

Permalink
Merge pull request #2902 from wasmerio/wasmer3-migrate-wasi
Browse files Browse the repository at this point in the history
Migrated WASI and WAST to Wasmer 3.0 API
  • Loading branch information
ptitSeb authored May 30, 2022
2 parents bb2858b + dbc950b commit 7cb05b7
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 310 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ wasmer-engine = { version = "=2.2.1", path = "lib/engine" }
wasmer-engine-universal = { version = "=2.2.1", path = "lib/engine-universal", optional = true }
wasmer-engine-dylib = { version = "=2.2.1", path = "lib/engine-dylib", optional = true }
wasmer-engine-staticlib = { version = "=2.2.1", path = "lib/engine-staticlib", optional = true }
#wasmer-wasi = { version = "=2.2.1", path = "lib/wasi", optional = true }
wasmer-wasi = { version = "=2.2.1", path = "lib/wasi", optional = true }
wasmer-wast = { version = "=2.2.1", path = "tests/lib/wast", optional = true }
#wasi-test-generator = { version = "=2.2.1", path = "tests/wasi-wast", optional = true }
#wasmer-cache = { version = "=2.2.1", path = "lib/cache", optional = true }
Expand Down Expand Up @@ -94,7 +94,7 @@ default = [
"dylib",
"staticlib",
#"cache",
#"wasi",
"wasi",
#"emscripten",
#"middlewares",
]
Expand All @@ -104,7 +104,7 @@ dylib = ["wasmer-engine-dylib", "engine"]
staticlib = ["wasmer-engine-staticlib", "engine"]
#cache = ["wasmer-cache"]
wast = ["wasmer-wast"]
#wasi = ["wasmer-wasi"]
wasi = ["wasmer-wasi"]
#emscripten = ["wasmer-emscripten"]
wat = ["wasmer/wat"]
compiler = [
Expand Down
274 changes: 116 additions & 158 deletions lib/wasi/src/lib.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/wasi/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ macro_rules! wasi_try_mem {

/// Reads a string from Wasm memory.
macro_rules! get_input_str {
($memory:expr, $data:expr, $len:expr) => {{
wasi_try_mem!($data.read_utf8_string($memory, $len))
($ctx:expr, $memory:expr, $data:expr, $len:expr) => {{
wasi_try_mem!($data.read_utf8_string($ctx, $memory, $len))
}};
}
54 changes: 34 additions & 20 deletions lib/wasi/src/syscalls/legacy/snapshot0.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::syscalls;
use crate::syscalls::types::{self, snapshot0};
use crate::{mem_error_to_wasi, WasiEnv};
use wasmer::WasmPtr;
use wasmer::{AsContextMut, ContextMut, WasmPtr};

/// Wrapper around `syscalls::fd_filestat_get` with extra logic to handle the size
/// difference of `wasi_filestat_t`
Expand All @@ -10,10 +10,11 @@ use wasmer::WasmPtr;
/// Wasm memory. If the memory clobbered by the current syscall is also used by
/// that syscall, then it may break.
pub fn fd_filestat_get(
env: &WasiEnv,
mut ctx: ContextMut<'_, WasiEnv>,
fd: types::__wasi_fd_t,
buf: WasmPtr<snapshot0::__wasi_filestat_t>,
) -> types::__wasi_errno_t {
let env = ctx.data();
let memory = env.memory();

// transmute the WasmPtr<T1> into a WasmPtr<T2> where T2 > T1, this will read extra memory.
Expand All @@ -22,17 +23,18 @@ pub fn fd_filestat_get(
let new_buf: WasmPtr<types::__wasi_filestat_t> = buf.cast();

// Copy the data including the extra data
let new_filestat_setup: types::__wasi_filestat_t = wasi_try_mem!(new_buf.read(memory));
let new_filestat_setup: types::__wasi_filestat_t = wasi_try_mem!(new_buf.read(&ctx, memory));

// Set up complete, make the call with the pointer that will write to the
// struct and some unrelated memory after the struct.
let result = syscalls::fd_filestat_get(env, fd, new_buf);
let result = syscalls::fd_filestat_get(ctx.as_context_mut(), fd, new_buf);

// reborrow memory
let env = ctx.data();
let memory = env.memory();

// get the values written to memory
let new_filestat = wasi_try_mem!(new_buf.deref(memory).read());
let new_filestat = wasi_try_mem!(new_buf.deref(&ctx, memory).read());
// translate the new struct into the old struct in host memory
let old_stat = snapshot0::__wasi_filestat_t {
st_dev: new_filestat.st_dev,
Expand All @@ -47,35 +49,39 @@ pub fn fd_filestat_get(

// write back the original values at the pointer's memory locations
// (including the memory unrelated to the pointer)
wasi_try_mem!(new_buf.deref(memory).write(new_filestat_setup));
wasi_try_mem!(new_buf.deref(&ctx, memory).write(new_filestat_setup));

// Now that this memory is back as it was, write the translated filestat
// into memory leaving it as it should be
wasi_try_mem!(buf.deref(memory).write(old_stat));
wasi_try_mem!(buf.deref(&ctx, memory).write(old_stat));

result
}

/// Wrapper around `syscalls::path_filestat_get` with extra logic to handle the size
/// difference of `wasi_filestat_t`
pub fn path_filestat_get(
env: &WasiEnv,
mut ctx: ContextMut<'_, WasiEnv>,
fd: types::__wasi_fd_t,
flags: types::__wasi_lookupflags_t,
path: WasmPtr<u8>,
path_len: u32,
buf: WasmPtr<snapshot0::__wasi_filestat_t>,
) -> types::__wasi_errno_t {
// see `fd_filestat_get` in this file for an explanation of this strange behavior
let env = ctx.data();
let memory = env.memory();

let new_buf: WasmPtr<types::__wasi_filestat_t> = buf.cast();
let new_filestat_setup: types::__wasi_filestat_t = wasi_try_mem!(new_buf.read(memory));
let new_filestat_setup: types::__wasi_filestat_t = wasi_try_mem!(new_buf.read(&ctx, memory));

let result = syscalls::path_filestat_get(env, fd, flags, path, path_len, new_buf);
let result =
syscalls::path_filestat_get(ctx.as_context_mut(), fd, flags, path, path_len, new_buf);

// need to re-borrow
let env = ctx.data();
let memory = env.memory();
let new_filestat = wasi_try_mem!(new_buf.deref(memory).read());
let new_filestat = wasi_try_mem!(new_buf.deref(&ctx, memory).read());
let old_stat = snapshot0::__wasi_filestat_t {
st_dev: new_filestat.st_dev,
st_ino: new_filestat.st_ino,
Expand All @@ -87,16 +93,16 @@ pub fn path_filestat_get(
st_ctim: new_filestat.st_ctim,
};

wasi_try_mem!(new_buf.deref(memory).write(new_filestat_setup));
wasi_try_mem!(buf.deref(memory).write(old_stat));
wasi_try_mem!(new_buf.deref(&ctx, memory).write(new_filestat_setup));
wasi_try_mem!(buf.deref(&ctx, memory).write(old_stat));

result
}

/// Wrapper around `syscalls::fd_seek` with extra logic to remap the values
/// of `__wasi_whence_t`
pub fn fd_seek(
env: &WasiEnv,
ctx: ContextMut<'_, WasiEnv>,
fd: types::__wasi_fd_t,
offset: types::__wasi_filedelta_t,
whence: snapshot0::__wasi_whence_t,
Expand All @@ -109,13 +115,13 @@ pub fn fd_seek(
// if it's invalid, let the new fd_seek handle it
_ => whence,
};
syscalls::fd_seek(env, fd, offset, new_whence, newoffset)
syscalls::fd_seek(ctx, fd, offset, new_whence, newoffset)
}

/// Wrapper around `syscalls::poll_oneoff` with extra logic to add the removed
/// userdata field back
pub fn poll_oneoff(
env: &WasiEnv,
mut ctx: ContextMut<'_, WasiEnv>,
in_: WasmPtr<snapshot0::__wasi_subscription_t>,
out_: WasmPtr<types::__wasi_event_t>,
nsubscriptions: u32,
Expand All @@ -125,14 +131,15 @@ pub fn poll_oneoff(
// we just need to readjust and copy it

// we start by adjusting `in_` into a format that the new code can understand
let env = ctx.data();
let memory = env.memory();
let in_origs = wasi_try_mem!(in_.slice(memory, nsubscriptions));
let in_origs = wasi_try_mem!(in_.slice(&ctx, memory, nsubscriptions));
let in_origs = wasi_try_mem!(in_origs.read_to_vec());

// get a pointer to the smaller new type
let in_new_type_ptr: WasmPtr<types::__wasi_subscription_t> = in_.cast();

for (in_sub_new, orig) in wasi_try_mem!(in_new_type_ptr.slice(memory, nsubscriptions))
for (in_sub_new, orig) in wasi_try_mem!(in_new_type_ptr.slice(&ctx, memory, nsubscriptions))
.iter()
.zip(in_origs.iter())
{
Expand All @@ -157,12 +164,19 @@ pub fn poll_oneoff(
}

// make the call
let result = syscalls::poll_oneoff(env, in_new_type_ptr, out_, nsubscriptions, nevents);
let result = syscalls::poll_oneoff(
ctx.as_context_mut(),
in_new_type_ptr,
out_,
nsubscriptions,
nevents,
);

// replace the old values of in, in case the calling code reuses the memory
let env = ctx.data();
let memory = env.memory();

for (in_sub, orig) in wasi_try_mem!(in_.slice(memory, nsubscriptions))
for (in_sub, orig) in wasi_try_mem!(in_.slice(&ctx, memory, nsubscriptions))
.iter()
.zip(in_origs.into_iter())
{
Expand Down
Loading

0 comments on commit 7cb05b7

Please sign in to comment.