Reimplement wasmtime-wasi on top of wasmtime (#899)

* Reimplement `wasmtime-wasi` on top of `wasmtime`

This commit reimplements the `wasmtime-wasi` crate on top of the
`wasmtime` API crate, instead of being placed on top of the `wasmtime-*`
family of internal crates. The purpose here is to continue to exercise
the API as well as avoid usage of internals wherever possible and
instead use the safe API as much as possible.

The `wasmtime-wasi` crate's API has been updated as part of this PR as
well. The general outline of it is now:

* Each module snapshot has a `WasiCtxBuilder`, `WasiCtx`, and `Wasi`
  type.
  * The `WasiCtx*` types are reexported from `wasi-common`.
  * The `Wasi` type is synthesized by the `wig` crate's procedural macro
* The `Wasi` type exposes one constructor which takes a `Store` and a
  `WasiCtx`, and produces a `Wasi`
* Each `Wasi` struct fields for all the exported functions in that wasi
  module. They're all public an they all have type `wasmtime::Func`
* The `Wasi` type has a `get_export` method to fetch an struct field by
  name.

The intention here is that we can continue to make progress on #727 by
integrating WASI construction into the `Instance::new` experience, but
it requires everything to be part of the same system!

The main oddity required by the `wasmtime-wasi` crate is that it needs
access to the caller's `memory` export, if any. This is currently done
with a bit of a hack and is expected to go away once interface types are
more fully baked in.

* Remove now no-longer-necessary APIs from `wasmtime`

* rustfmt

* Rename to from_abi
This commit is contained in:
Alex Crichton
2020-02-06 09:23:06 -06:00
committed by GitHub
parent c9dce98ba2
commit 3dd5a3cb3f
20 changed files with 284 additions and 488 deletions

View File

@@ -1,7 +1,14 @@
mod instantiate;
pub mod old;
pub use instantiate::{create_wasi_instance, instantiate_wasi, instantiate_wasi_with_context};
use wasi_common::hostcalls;
pub use wasi_common::{WasiCtx, WasiCtxBuilder};
// Defines a `struct Wasi` with member fields and appropriate APIs for dealing
// with all the various WASI exports.
wig::define_wasi_struct!(
"snapshot" "wasi_snapshot_preview1"
);
pub fn is_wasi_module(name: &str) -> bool {
// FIXME: this should be more conservative, but while WASI is in flux and
@@ -9,3 +16,54 @@ pub fn is_wasi_module(name: &str) -> bool {
// trick.
name.starts_with("wasi")
}
/// This is an internal structure used to acquire a handle on the caller's
/// wasm memory buffer.
///
/// This exploits how we can implement `WasmArg` for ourselves locally even
/// though crates in general should not be doing that. This is a crate in
/// the wasmtime project, however, so we should be able to keep up with our own
/// changes.
///
/// In general this type is wildly unsafe. We need to update the wasi crates to
/// probably work with more `wasmtime`-like APIs to grip with the unsafety
/// around dealing with caller memory.
struct WasiCallerMemory {
base: *mut u8,
len: usize,
}
impl wasmtime::WasmArg for WasiCallerMemory {
type Abi = ();
fn push(_dst: &mut Vec<wasmtime::ValType>) {}
fn from_abi(vmctx: *mut wasmtime_runtime::VMContext, _abi: ()) -> Self {
unsafe {
match wasmtime_runtime::InstanceHandle::from_vmctx(vmctx).lookup("memory") {
Some(wasmtime_runtime::Export::Memory {
definition,
vmctx: _,
memory: _,
}) => WasiCallerMemory {
base: (*definition).base,
len: (*definition).current_length,
},
_ => WasiCallerMemory {
base: std::ptr::null_mut(),
len: 0,
},
}
}
}
}
impl WasiCallerMemory {
unsafe fn get(&self) -> Result<&mut [u8], wasi_common::wasi::__wasi_errno_t> {
if self.base.is_null() {
Err(wasi_common::wasi::__WASI_ERRNO_INVAL)
} else {
Ok(std::slice::from_raw_parts_mut(self.base, self.len))
}
}
}