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

12
Cargo.lock generated
View File

@@ -1655,9 +1655,6 @@ dependencies = [
"tempfile", "tempfile",
"wasi-common", "wasi-common",
"wasmtime", "wasmtime",
"wasmtime-environ",
"wasmtime-jit",
"wasmtime-runtime",
"wasmtime-wasi", "wasmtime-wasi",
"wat", "wat",
] ]
@@ -2075,9 +2072,7 @@ dependencies = [
"target-lexicon", "target-lexicon",
"wasmparser 0.47.0", "wasmparser 0.47.0",
"wasmtime", "wasmtime",
"wasmtime-environ",
"wasmtime-interface-types", "wasmtime-interface-types",
"wasmtime-runtime",
"wasmtime-wasi", "wasmtime-wasi",
] ]
@@ -2123,15 +2118,10 @@ dependencies = [
name = "wasmtime-wasi" name = "wasmtime-wasi"
version = "0.9.0" version = "0.9.0"
dependencies = [ dependencies = [
"cranelift-codegen", "anyhow",
"cranelift-entity",
"cranelift-wasm",
"log", "log",
"target-lexicon",
"wasi-common", "wasi-common",
"wasmtime", "wasmtime",
"wasmtime-environ",
"wasmtime-jit",
"wasmtime-runtime", "wasmtime-runtime",
"wig", "wig",
] ]

View File

@@ -45,16 +45,19 @@ macro_rules! wrappers {
unsafe extern "C" fn shim<F, $($args,)* R>( unsafe extern "C" fn shim<F, $($args,)* R>(
vmctx: *mut VMContext, vmctx: *mut VMContext,
_caller_vmctx: *mut VMContext, _caller_vmctx: *mut VMContext,
$($args: $args,)* $($args: $args::Abi,)*
) -> R::Abi ) -> R::Abi
where where
F: Fn($($args),*) -> R + 'static, F: Fn($($args),*) -> R + 'static,
$($args: WasmArg,)*
R: WasmRet, R: WasmRet,
{ {
let ret = { let ret = {
let instance = InstanceHandle::from_vmctx(vmctx); let instance = InstanceHandle::from_vmctx(vmctx);
let func = instance.host_state().downcast_ref::<F>().expect("state"); let func = instance.host_state().downcast_ref::<F>().expect("state");
panic::catch_unwind(AssertUnwindSafe(|| func($($args),*))) panic::catch_unwind(AssertUnwindSafe(|| {
func($($args::from_abi(_caller_vmctx, $args)),*)
}))
}; };
match ret { match ret {
Ok(ret) => ret.into_abi(), Ok(ret) => ret.into_abi(),
@@ -166,6 +169,36 @@ impl Func {
/// ///
/// For more information about this function, see [`Func::wrap1`]. /// For more information about this function, see [`Func::wrap1`].
(wrap5, A, B, C, D, E) (wrap5, A, B, C, D, E)
/// Creates a new `Func` from the given Rust closure, which takes 6
/// arguments.
///
/// For more information about this function, see [`Func::wrap1`].
(wrap6, A, B, C, D, E, G)
/// Creates a new `Func` from the given Rust closure, which takes 7
/// arguments.
///
/// For more information about this function, see [`Func::wrap1`].
(wrap7, A, B, C, D, E, G, H)
/// Creates a new `Func` from the given Rust closure, which takes 8
/// arguments.
///
/// For more information about this function, see [`Func::wrap1`].
(wrap8, A, B, C, D, E, G, H, I)
/// Creates a new `Func` from the given Rust closure, which takes 9
/// arguments.
///
/// For more information about this function, see [`Func::wrap1`].
(wrap9, A, B, C, D, E, G, H, I, J)
/// Creates a new `Func` from the given Rust closure, which takes 10
/// arguments.
///
/// For more information about this function, see [`Func::wrap1`].
(wrap10, A, B, C, D, E, G, H, I, J, K)
} }
fn from_wrapped( fn from_wrapped(
@@ -248,36 +281,65 @@ impl fmt::Debug for Func {
/// ///
/// For more information see [`Func::wrap1`] /// For more information see [`Func::wrap1`]
pub trait WasmArg { pub trait WasmArg {
#[doc(hidden)]
type Abi;
#[doc(hidden)] #[doc(hidden)]
fn push(dst: &mut Vec<ValType>); fn push(dst: &mut Vec<ValType>);
#[doc(hidden)]
fn from_abi(vmctx: *mut VMContext, abi: Self::Abi) -> Self;
} }
impl WasmArg for () { impl WasmArg for () {
type Abi = ();
fn push(_dst: &mut Vec<ValType>) {} fn push(_dst: &mut Vec<ValType>) {}
#[inline]
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
abi
}
} }
impl WasmArg for i32 { impl WasmArg for i32 {
type Abi = Self;
fn push(dst: &mut Vec<ValType>) { fn push(dst: &mut Vec<ValType>) {
dst.push(ValType::I32); dst.push(ValType::I32);
} }
#[inline]
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
abi
}
} }
impl WasmArg for i64 { impl WasmArg for i64 {
type Abi = Self;
fn push(dst: &mut Vec<ValType>) { fn push(dst: &mut Vec<ValType>) {
dst.push(ValType::I64); dst.push(ValType::I64);
} }
#[inline]
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
abi
}
} }
impl WasmArg for f32 { impl WasmArg for f32 {
type Abi = Self;
fn push(dst: &mut Vec<ValType>) { fn push(dst: &mut Vec<ValType>) {
dst.push(ValType::F32); dst.push(ValType::F32);
} }
#[inline]
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
abi
}
} }
impl WasmArg for f64 { impl WasmArg for f64 {
type Abi = Self;
fn push(dst: &mut Vec<ValType>) { fn push(dst: &mut Vec<ValType>) {
dst.push(ValType::F64); dst.push(ValType::F64);
} }
#[inline]
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
abi
}
} }
/// A trait implemented for types which can be returned from closures passed to /// A trait implemented for types which can be returned from closures passed to

View File

@@ -2,7 +2,6 @@ use crate::externals::Extern;
use crate::module::Module; use crate::module::Module;
use crate::runtime::Store; use crate::runtime::Store;
use crate::trap::Trap; use crate::trap::Trap;
use crate::types::{ExportType, ExternType};
use anyhow::{Error, Result}; use anyhow::{Error, Result};
use wasmtime_jit::{CompiledModule, Resolver}; use wasmtime_jit::{CompiledModule, Resolver};
use wasmtime_runtime::{Export, InstanceHandle, InstantiationError}; use wasmtime_runtime::{Export, InstanceHandle, InstantiationError};
@@ -173,50 +172,8 @@ impl Instance {
Some(&self.exports()[i]) Some(&self.exports()[i])
} }
#[doc(hidden)]
pub fn from_handle(store: &Store, instance_handle: InstanceHandle) -> Instance {
let mut exports = Vec::new();
let mut exports_types = Vec::new();
for (name, _) in instance_handle.exports() {
let export = instance_handle.lookup(name).expect("export");
if let wasmtime_runtime::Export::Function { signature, .. } = &export {
// HACK ensure all handles, instantiated outside Store, present in
// the store's SignatureRegistry, e.g. WASI instances that are
// imported into this store using the from_handle() method.
store.compiler().signatures().register(signature);
}
// We should support everything supported by wasmtime_runtime, or
// otherwise we've got a bug in this crate, so panic if anything
// fails to convert here.
let extern_type = match ExternType::from_wasmtime_export(&export) {
Some(ty) => ty,
None => panic!("unsupported core wasm external type {:?}", export),
};
exports_types.push(ExportType::new(name, extern_type));
exports.push(Extern::from_wasmtime_export(
store,
instance_handle.clone(),
export.clone(),
));
}
let module = Module::from_exports(store, exports_types.into_boxed_slice());
Instance {
instance_handle,
module,
exports: exports.into_boxed_slice(),
}
}
#[doc(hidden)] #[doc(hidden)]
pub fn handle(&self) -> &InstanceHandle { pub fn handle(&self) -> &InstanceHandle {
&self.instance_handle &self.instance_handle
} }
#[doc(hidden)]
pub fn get_wasmtime_memory(&self) -> Option<wasmtime_runtime::Export> {
self.instance_handle.lookup("memory")
}
} }

View File

@@ -272,13 +272,6 @@ impl Module {
validate(binary, Some(config)).map_err(Error::new) validate(binary, Some(config)).map_err(Error::new)
} }
#[doc(hidden)]
pub fn from_exports(store: &Store, exports: Box<[ExportType]>) -> Self {
let mut ret = unsafe { Module::compile(store, b"\0asm\x01\0\0\0").unwrap() };
Rc::get_mut(&mut ret.inner).unwrap().exports = exports;
return ret;
}
unsafe fn compile(store: &Store, binary: &[u8]) -> Result<Self> { unsafe fn compile(store: &Store, binary: &[u8]) -> Result<Self> {
let compiled = CompiledModule::new( let compiled = CompiledModule::new(
&mut store.compiler_mut(), &mut store.compiler_mut(),

View File

@@ -157,25 +157,6 @@ impl ExternType {
(Table(TableType) table unwrap_table) (Table(TableType) table unwrap_table)
(Memory(MemoryType) memory unwrap_memory) (Memory(MemoryType) memory unwrap_memory)
} }
/// Returns `None` if the sub-type fails to get converted, see documentation
/// for sub-types about what may fail.
pub(crate) fn from_wasmtime_export(export: &wasmtime_runtime::Export) -> Option<Self> {
Some(match export {
wasmtime_runtime::Export::Function { signature, .. } => {
ExternType::Func(FuncType::from_wasmtime_signature(signature.clone())?)
}
wasmtime_runtime::Export::Memory { memory, .. } => {
ExternType::Memory(MemoryType::from_wasmtime_memory(&memory.memory))
}
wasmtime_runtime::Export::Global { global, .. } => {
ExternType::Global(GlobalType::from_wasmtime_global(&global)?)
}
wasmtime_runtime::Export::Table { table, .. } => {
ExternType::Table(TableType::from_wasmtime_table(&table.table))
}
})
}
} }
// Function Types // Function Types

View File

@@ -18,9 +18,7 @@ doc = false
[dependencies] [dependencies]
wasmtime = { path = "../../api", version = "0.9.0" } wasmtime = { path = "../../api", version = "0.9.0" }
wasmtime-environ = { path = "../../environ", version = "0.9.0" }
wasmtime-interface-types = { path = "../../interface-types", version = "0.9.0" } wasmtime-interface-types = { path = "../../interface-types", version = "0.9.0" }
wasmtime-runtime = { path = "../../runtime", version = "0.9.0" }
wasmtime-wasi = { path = "../../wasi", version = "0.9.0" } wasmtime-wasi = { path = "../../wasi", version = "0.9.0" }
target-lexicon = { version = "0.10.0", default-features = false } target-lexicon = { version = "0.10.0", default-features = false }
anyhow = "1.0.19" anyhow = "1.0.19"

View File

@@ -89,9 +89,11 @@ pub fn instantiate(
// If this module expects to be able to use wasi then go ahead and hook // If this module expects to be able to use wasi then go ahead and hook
// that up into the imported crates. // that up into the imported crates.
let wasi = if let Some(module_name) = data.find_wasi_module_name() { let wasi = if let Some(module_name) = data.find_wasi_module_name() {
let instance = wasmtime_wasi::create_wasi_instance(&store, &[], &[], &[]) let cx = wasmtime_wasi::WasiCtxBuilder::new()
.build()
.map_err(|e| err2py(e.into()))?; .map_err(|e| err2py(e.into()))?;
Some((module_name, instance)) let wasi = wasmtime_wasi::Wasi::new(&store, cx);
Some((module_name, wasi))
} else { } else {
None None
}; };
@@ -111,7 +113,7 @@ pub fn instantiate(
.ok_or_else(|| { .ok_or_else(|| {
PyErr::new::<Exception, _>(format!("wasi export {} is not found", i.name(),)) PyErr::new::<Exception, _>(format!("wasi export {} is not found", i.name(),))
})?; })?;
imports.push(e.clone()); imports.push(e.clone().into());
} else { } else {
return Err(PyErr::new::<Exception, _>(format!( return Err(PyErr::new::<Exception, _>(format!(
"imported module {} is not found", "imported module {} is not found",

View File

@@ -62,14 +62,14 @@ fn generate_load(item: &syn::ItemTrait) -> syn::Result<TokenStream> {
let mut imports: Vec<Extern> = Vec::new(); let mut imports: Vec<Extern> = Vec::new();
if let Some(module_name) = data.find_wasi_module_name() { if let Some(module_name) = data.find_wasi_module_name() {
let wasi_instance = #root::wasmtime_wasi::create_wasi_instance(&store, &[], &[], &[]) let wasi_cx = #root::wasmtime_wasi::WasiCtxBuilder::new().build()?;
.map_err(|e| format_err!("wasm instantiation error: {:?}", e))?; let wasi = #root::wasmtime_wasi::Wasi::new(&store, wasi_cx);
for i in module.imports().iter() { for i in module.imports().iter() {
if i.module() != module_name { if i.module() != module_name {
bail!("unknown import module {}", i.module()); bail!("unknown import module {}", i.module());
} }
if let Some(export) = wasi_instance.get_export(i.name()) { if let Some(export) = wasi.get_export(i.name()) {
imports.push(export.clone()); imports.push(export.clone().into());
} else { } else {
bail!("unknown import {}:{}", i.module(), i.name()) bail!("unknown import {}:{}", i.module(), i.name())
} }

View File

@@ -11,9 +11,6 @@ cfg-if = "0.1.9"
[dev-dependencies] [dev-dependencies]
wasi-common = { path = "../wasi-common", version = "0.9.0" } wasi-common = { path = "../wasi-common", version = "0.9.0" }
wasmtime-runtime = { path = "../runtime", version = "0.9.0" }
wasmtime-environ = { path = "../environ", version = "0.9.0" }
wasmtime-jit = { path = "../jit", version = "0.9.0" }
wasmtime-wasi = { path = "../wasi", version = "0.9.0" } wasmtime-wasi = { path = "../wasi", version = "0.9.0" }
wasmtime = { path = "../api", version = "0.9.0" } wasmtime = { path = "../api", version = "0.9.0" }
target-lexicon = "0.10.0" target-lexicon = "0.10.0"

View File

@@ -33,14 +33,7 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
// stdin is closed which causes tests to fail. // stdin is closed which causes tests to fail.
let (reader, _writer) = os_pipe::pipe()?; let (reader, _writer) = os_pipe::pipe()?;
builder = builder.stdin(reader_to_file(reader)); builder = builder.stdin(reader_to_file(reader));
let snapshot1 = Instance::from_handle( let snapshot1 = wasmtime_wasi::Wasi::new(&store, builder.build()?);
&store,
wasmtime_wasi::instantiate_wasi_with_context(
builder.build().context("failed to build wasi context")?,
)
.context("failed to instantiate wasi")?,
);
let module = Module::new(&store, &data).context("failed to create wasm module")?; let module = Module::new(&store, &data).context("failed to create wasm module")?;
let imports = module let imports = module
.imports() .imports()
@@ -48,7 +41,7 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
.map(|i| { .map(|i| {
let field_name = i.name(); let field_name = i.name();
if let Some(export) = snapshot1.get_export(field_name) { if let Some(export) = snapshot1.get_export(field_name) {
Ok(export.clone()) Ok(export.clone().into())
} else { } else {
bail!( bail!(
"import {} was not found in module {}", "import {} was not found in module {}",

View File

@@ -34,8 +34,8 @@ pub fn witx_wasi32_types(args: TokenStream) -> TokenStream {
/// A single-use macro in the `wasmtime-wasi` crate. /// A single-use macro in the `wasmtime-wasi` crate.
#[proc_macro] #[proc_macro]
pub fn define_add_wrappers_to_module(args: TokenStream) -> TokenStream { pub fn define_wasi_struct(args: TokenStream) -> TokenStream {
wasi::add_wrappers_to_module(args.into()).into() wasi::define_struct(args.into()).into()
} }
#[proc_macro] #[proc_macro]

View File

@@ -11,18 +11,16 @@ enum Abi {
/// This is a single-use macro intended to be used in the `wasmtime-wasi` crate. /// This is a single-use macro intended to be used in the `wasmtime-wasi` crate.
/// ///
/// This macro will generate a function, `add_wrappers_to_module`, which will /// This macro will generate a structure, `Wasi`, which will create all the
/// use the input arguments to register all wasi functions inside of a `Module` /// functions necessary to bind wasi and hook everything up via the `wasmtime`
/// instance. This will automatically assign the wasm underlying types and /// crate.
/// perform conversions from the wasm type to the underlying wasi type (often
/// unsigned or of a smaller width).
/// ///
/// The generated shim functions here will also `trace!` their arguments for /// The generated shim functions here will also `trace!` their arguments for
/// logging purposes. Otherwise this is hopefully somewhat straightforward! /// logging purposes. Otherwise this is hopefully somewhat straightforward!
/// ///
/// I'd recommend using `cargo +nightly expand` to explore the output of this /// I'd recommend using `cargo +nightly expand` to explore the output of this
/// macro some more. /// macro some more.
pub fn add_wrappers_to_module(args: TokenStream) -> TokenStream { pub fn define_struct(args: TokenStream) -> TokenStream {
let (path, _phase) = utils::witx_path_from_args(args); let (path, _phase) = utils::witx_path_from_args(args);
let doc = match witx::load(&[&path]) { let doc = match witx::load(&[&path]) {
Ok(doc) => doc, Ok(doc) => doc,
@@ -31,12 +29,18 @@ pub fn add_wrappers_to_module(args: TokenStream) -> TokenStream {
} }
}; };
let mut add = Vec::new(); let mut fields = Vec::new();
let mut get_exports = Vec::new();
let mut ctor_externs = Vec::new();
let mut ctor_fields = Vec::new();
for module in doc.modules() { for module in doc.modules() {
for func in module.funcs() { for func in module.funcs() {
let name = func.name.as_str(); let name = func.name.as_str();
let name_ident = Ident::new(func.name.as_str(), Span::call_site()); let name_ident = Ident::new(func.name.as_str(), Span::call_site());
fields.push(quote! { pub #name_ident: wasmtime::Func });
get_exports.push(quote! { #name => Some(&self.#name_ident) });
ctor_fields.push(name_ident.clone());
let mut shim_arg_decls = Vec::new(); let mut shim_arg_decls = Vec::new();
let mut params = Vec::new(); let mut params = Vec::new();
@@ -178,56 +182,71 @@ pub fn add_wrappers_to_module(args: TokenStream) -> TokenStream {
} }
let format_str = format!("{}({})", name, formats.join(", ")); let format_str = format!("{}({})", name, formats.join(", "));
add.push(quote! { let wrap = format_ident!("wrap{}", shim_arg_decls.len() + 1);
let sig = module.signatures.push(translate_signature( ctor_externs.push(quote! {
ir::Signature { let my_cx = cx.clone();
params: vec![#(cranelift_codegen::ir::AbiParam::new(#params)),*], let #name_ident = wasmtime::Func::#wrap(
returns: vec![#(cranelift_codegen::ir::AbiParam::new(#returns)),*], store,
call_conv, move |mem: crate::WasiCallerMemory #(,#shim_arg_decls)*| -> #ret_ty {
}, log::trace!(
pointer_type, #format_str,
)); #(#format_args),*
let func = module.functions.push(sig); );
module unsafe {
.exports let memory = match mem.get() {
.insert(#name.to_owned(), Export::Function(func)); Ok(e) => e,
Err(e) => #handle_early_error,
unsafe extern "C" fn #name_ident( };
ctx: *mut wasmtime_runtime::VMContext, hostcalls::#name_ident(
caller_ctx: *mut wasmtime_runtime::VMContext, &mut my_cx.borrow_mut(),
#(#shim_arg_decls),* memory,
) -> #ret_ty { #(#hostcall_args),*
log::trace!( ) #cvt_ret
#format_str, }
#(#format_args),* }
); );
let mut wasi_ctx = match get_wasi_ctx(&mut *ctx) {
Ok(e) => e.borrow_mut(),
Err(e) => #handle_early_error,
};
let memory = match get_memory(&mut *caller_ctx) {
Ok(e) => e,
Err(e) => #handle_early_error,
};
hostcalls::#name_ident(
&mut *wasi_ctx,
memory,
#(#hostcall_args),*
) #cvt_ret
}
finished_functions.push(#name_ident as *const _);
}); });
} }
} }
quote! { quote! {
pub fn add_wrappers_to_module( /// An instantiated instance of the wasi exports.
module: &mut Module, ///
finished_functions: &mut PrimaryMap<DefinedFuncIndex, *const wasmtime_runtime::VMFunctionBody>, /// This represents a wasi module which can be used to instantiate other
call_conv: isa::CallConv, /// wasm modules. This structure exports all that various fields of the
pointer_type: types::Type, /// wasi instance as fields which can be used to implement your own
) { /// instantiation logic, if necessary. Additionally [`Wasi::get_export`]
#(#add)* /// can be used to do name-based resolution.
pub struct Wasi {
#(#fields,)*
}
impl Wasi {
/// Creates a new [`Wasi`] instance.
///
/// External values are allocated into the `store` provided and
/// configuration of the wasi instance itself should be all
/// contained in the `cx` parameter.
pub fn new(store: &wasmtime::Store, cx: WasiCtx) -> Wasi {
let cx = std::rc::Rc::new(std::cell::RefCell::new(cx));
#(#ctor_externs)*
Wasi {
#(#ctor_fields,)*
}
}
/// Looks up a field called `name` in this structure, returning it
/// if found.
///
/// This is often useful when instantiating a `wasmtime` instance
/// where name resolution often happens with strings.
pub fn get_export(&self, name: &str) -> Option<&wasmtime::Func> {
match name {
#(#get_exports,)*
_ => None,
}
}
} }
} }
} }

View File

@@ -11,16 +11,11 @@ readme = "README.md"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
anyhow = "1.0"
log = { version = "0.4.8", default-features = false }
wasi-common = { path = "../wasi-common", version = "0.9.0" }
wasmtime = { path = "../api", version = "0.9.0" } wasmtime = { path = "../api", version = "0.9.0" }
wasmtime-runtime = { path = "../runtime", version = "0.9.0" } wasmtime-runtime = { path = "../runtime", version = "0.9.0" }
wasmtime-environ = { path = "../environ", version = "0.9.0" }
wasmtime-jit = { path = "../jit", version = "0.9.0" }
wasi-common = { path = "../wasi-common", version = "0.9.0" }
cranelift-codegen = { version = "0.56", features = ["enable-serde"] }
cranelift-entity = { version = "0.56", features = ["enable-serde"] }
cranelift-wasm = { version = "0.56", features = ["enable-serde"] }
target-lexicon = "0.10.0"
log = { version = "0.4.8", default-features = false }
wig = { path = "../wasi-common/wig", version = "0.9.2" } wig = { path = "../wasi-common/wig", version = "0.9.2" }
[badges] [badges]

View File

@@ -1,131 +0,0 @@
use cranelift_codegen::ir::types;
use cranelift_codegen::{ir, isa};
use cranelift_entity::PrimaryMap;
use cranelift_wasm::DefinedFuncIndex;
use std::cell::RefCell;
use std::fs::File;
use std::sync::Arc;
use target_lexicon::HOST;
use wasi_common::hostcalls;
use wasi_common::wasi;
use wasi_common::{WasiCtx, WasiCtxBuilder};
use wasmtime_environ::{translate_signature, Export, Module};
use wasmtime_runtime::{Imports, InstanceHandle, InstantiationError, VMContext};
/// Creates `wasmtime::Instance` object implementing the "wasi" interface.
pub fn create_wasi_instance(
store: &wasmtime::Store,
preopened_dirs: &[(String, File)],
argv: &[String],
environ: &[(String, String)],
) -> Result<wasmtime::Instance, InstantiationError> {
let wasi = instantiate_wasi(preopened_dirs, argv, environ)?;
let instance = wasmtime::Instance::from_handle(&store, wasi);
Ok(instance)
}
/// Return an instance implementing the "wasi" interface.
pub fn instantiate_wasi(
preopened_dirs: &[(String, File)],
argv: &[String],
environ: &[(String, String)],
) -> Result<InstanceHandle, InstantiationError> {
let mut wasi_ctx_builder = WasiCtxBuilder::new()
.inherit_stdio()
.args(argv)
.envs(environ);
for (dir, f) in preopened_dirs {
wasi_ctx_builder = wasi_ctx_builder.preopened_dir(
f.try_clone().map_err(|err| {
InstantiationError::Resource(format!(
"couldn't clone an instance handle to pre-opened dir: {}",
err
))
})?,
dir,
);
}
let wasi_ctx = wasi_ctx_builder.build().map_err(|err| {
InstantiationError::Resource(format!("couldn't assemble WASI context object: {}", err))
})?;
instantiate_wasi_with_context(wasi_ctx)
}
/// Return an instance implementing the "wasi" interface.
///
/// The wasi context is configured by
pub fn instantiate_wasi_with_context(
wasi_ctx: WasiCtx,
) -> Result<InstanceHandle, InstantiationError> {
let pointer_type = types::Type::triple_pointer_type(&HOST);
let mut module = Module::new();
let mut finished_functions = PrimaryMap::new();
let call_conv = isa::CallConv::triple_default(&HOST);
// This function is defined in the macro invocation of
// `define_add_wrappers_to_module` below. For more information about how
// this works it'd recommended to read the source in
// `crates/wasi-common/wig/src/wasi.rs`.
add_wrappers_to_module(
&mut module,
&mut finished_functions,
call_conv,
pointer_type,
);
let imports = Imports::none();
let data_initializers = Vec::new();
let signatures = PrimaryMap::new();
unsafe {
InstanceHandle::new(
Arc::new(module),
finished_functions.into_boxed_slice(),
imports,
&data_initializers,
signatures.into_boxed_slice(),
None,
Box::new(RefCell::new(wasi_ctx)),
)
}
}
wig::define_add_wrappers_to_module!(
"snapshot" "wasi_snapshot_preview1"
);
// Used by `add_wrappers_to_module` defined in the macro above
fn get_wasi_ctx(vmctx: &mut VMContext) -> Result<&RefCell<WasiCtx>, wasi::__wasi_errno_t> {
unsafe {
vmctx
.host_state()
.downcast_ref()
.ok_or_else(|| panic!("no host state named WasiCtx available"))
}
}
// Used by `add_wrappers_to_module` defined in the macro above
fn get_memory(caller_vmctx: &mut VMContext) -> Result<&mut [u8], wasi::__wasi_errno_t> {
match unsafe { InstanceHandle::from_vmctx(caller_vmctx) }.lookup("memory") {
Some(wasmtime_runtime::Export::Memory {
definition,
vmctx: _,
memory: _,
}) => unsafe {
let definition = &*definition;
let ptr = definition.base;
let len = definition.current_length;
Ok(std::slice::from_raw_parts_mut(ptr, len))
},
Some(export) => {
log::error!("export named \"memory\" isn't a memory: {:?}", export);
Err(wasi::__WASI_ERRNO_INVAL)
}
None => {
log::error!("no export named \"memory\" available from caller");
Err(wasi::__WASI_ERRNO_INVAL)
}
}
}

View File

@@ -1,7 +1,14 @@
mod instantiate;
pub mod old; 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 { pub fn is_wasi_module(name: &str) -> bool {
// FIXME: this should be more conservative, but while WASI is in flux and // 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. // trick.
name.starts_with("wasi") 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))
}
}
}

View File

@@ -0,0 +1,15 @@
use wasi_common::old::snapshot_0::hostcalls;
use wasi_common::old::snapshot_0::WasiCtx;
// Defines a `struct Wasi` with member fields and appropriate APIs for dealing
// with all the various WASI exports.
wig::define_wasi_struct!(
"old/snapshot_0" "wasi_unstable"
);
pub fn is_wasi_module(name: &str) -> bool {
// FIXME: this should be more conservative, but while WASI is in flux and
// we're figuring out how to support multiple revisions, this should do the
// trick.
name.starts_with("wasi")
}

View File

@@ -1,131 +0,0 @@
use cranelift_codegen::ir::types;
use cranelift_codegen::{ir, isa};
use cranelift_entity::PrimaryMap;
use cranelift_wasm::DefinedFuncIndex;
use std::cell::RefCell;
use std::fs::File;
use std::sync::Arc;
use target_lexicon::HOST;
use wasi_common::old::snapshot_0::hostcalls;
use wasi_common::old::snapshot_0::wasi;
use wasi_common::old::snapshot_0::{WasiCtx, WasiCtxBuilder};
use wasmtime_environ::{translate_signature, Export, Module};
use wasmtime_runtime::{Imports, InstanceHandle, InstantiationError, VMContext};
/// Creates `wasmtime::Instance` object implementing the "wasi" interface.
pub fn create_wasi_instance(
store: &wasmtime::Store,
preopened_dirs: &[(String, File)],
argv: &[String],
environ: &[(String, String)],
) -> Result<wasmtime::Instance, InstantiationError> {
let wasi = instantiate_wasi(preopened_dirs, argv, environ)?;
let instance = wasmtime::Instance::from_handle(&store, wasi);
Ok(instance)
}
/// Return an instance implementing the "wasi" interface.
pub fn instantiate_wasi(
preopened_dirs: &[(String, File)],
argv: &[String],
environ: &[(String, String)],
) -> Result<InstanceHandle, InstantiationError> {
let mut wasi_ctx_builder = WasiCtxBuilder::new()
.inherit_stdio()
.args(argv)
.envs(environ);
for (dir, f) in preopened_dirs {
wasi_ctx_builder = wasi_ctx_builder.preopened_dir(
f.try_clone().map_err(|err| {
InstantiationError::Resource(format!(
"couldn't clone an instance handle to pre-opened dir: {}",
err
))
})?,
dir,
);
}
let wasi_ctx = wasi_ctx_builder.build().map_err(|err| {
InstantiationError::Resource(format!("couldn't assemble WASI context object: {}", err))
})?;
instantiate_wasi_with_context(wasi_ctx)
}
/// Return an instance implementing the "wasi" interface.
///
/// The wasi context is configured by
pub fn instantiate_wasi_with_context(
wasi_ctx: WasiCtx,
) -> Result<InstanceHandle, InstantiationError> {
let pointer_type = types::Type::triple_pointer_type(&HOST);
let mut module = Module::new();
let mut finished_functions = PrimaryMap::new();
let call_conv = isa::CallConv::triple_default(&HOST);
// This function is defined in the macro invocation of
// `define_add_wrappers_to_module` below. For more information about how
// this works it'd recommended to read the source in
// `crates/wasi-common/wig/src/wasi.rs`.
add_wrappers_to_module(
&mut module,
&mut finished_functions,
call_conv,
pointer_type,
);
let imports = Imports::none();
let data_initializers = Vec::new();
let signatures = PrimaryMap::new();
unsafe {
InstanceHandle::new(
Arc::new(module),
finished_functions.into_boxed_slice(),
imports,
&data_initializers,
signatures.into_boxed_slice(),
None,
Box::new(RefCell::new(wasi_ctx)),
)
}
}
// Used by `add_wrappers_to_module` defined in the macro above
fn get_wasi_ctx(vmctx: &mut VMContext) -> Result<&RefCell<WasiCtx>, wasi::__wasi_errno_t> {
unsafe {
vmctx
.host_state()
.downcast_ref()
.ok_or_else(|| panic!("no host state named WasiCtx available"))
}
}
// Used by `add_wrappers_to_module` defined in the macro above
fn get_memory(caller_vmctx: &mut VMContext) -> Result<&mut [u8], wasi::__wasi_errno_t> {
match unsafe { InstanceHandle::from_vmctx(caller_vmctx) }.lookup("memory") {
Some(wasmtime_runtime::Export::Memory {
definition,
vmctx: _,
memory: _,
}) => unsafe {
let definition = &*definition;
let ptr = definition.base;
let len = definition.current_length;
Ok(std::slice::from_raw_parts_mut(ptr, len))
},
Some(export) => {
log::error!("export named \"memory\" isn't a memory: {:?}", export);
Err(wasi::__WASI_ERRNO_INVAL)
}
None => {
log::error!("no export named \"memory\" available from caller");
Err(wasi::__WASI_ERRNO_INVAL)
}
}
}
wig::define_add_wrappers_to_module!(
"old/snapshot_0" "wasi_unstable"
);

View File

@@ -1,12 +0,0 @@
extern crate alloc;
mod instantiate;
pub use instantiate::{create_wasi_instance, instantiate_wasi, instantiate_wasi_with_context};
pub fn is_wasi_module(name: &str) -> bool {
// FIXME: this should be more conservative, but while WASI is in flux and
// we're figuring out how to support multiple revisions, this should do the
// trick.
name.starts_with("wasi")
}

View File

@@ -3,7 +3,6 @@
use crate::{init_file_per_thread_logger, pick_compilation_strategy, CommonOptions}; use crate::{init_file_per_thread_logger, pick_compilation_strategy, CommonOptions};
use anyhow::{bail, Context as _, Result}; use anyhow::{bail, Context as _, Result};
use std::{ use std::{
collections::HashMap,
ffi::{OsStr, OsString}, ffi::{OsStr, OsString},
fmt::Write, fmt::Write,
fs::File, fs::File,
@@ -14,9 +13,7 @@ use wasi_common::preopen_dir;
use wasmtime::{Config, Engine, Instance, Module, Store}; use wasmtime::{Config, Engine, Instance, Module, Store};
use wasmtime_environ::cache_init; use wasmtime_environ::cache_init;
use wasmtime_interface_types::ModuleData; use wasmtime_interface_types::ModuleData;
use wasmtime_wasi::{ use wasmtime_wasi::{old::snapshot_0::Wasi as WasiSnapshot0, Wasi};
create_wasi_instance, old::snapshot_0::create_wasi_instance as create_wasi_instance_snapshot_0,
};
fn parse_module(s: &OsStr) -> Result<PathBuf, OsString> { fn parse_module(s: &OsStr) -> Result<PathBuf, OsString> {
// Do not accept wasmtime subcommand names as the module name // Do not accept wasmtime subcommand names as the module name
@@ -134,20 +131,12 @@ impl RunCommand {
let engine = Engine::new(&config); let engine = Engine::new(&config);
let store = Store::new(&engine); let store = Store::new(&engine);
let mut module_registry = HashMap::new();
// Make wasi available by default. // Make wasi available by default.
let preopen_dirs = self.compute_preopen_dirs()?; let preopen_dirs = self.compute_preopen_dirs()?;
let argv = self.compute_argv(); let argv = self.compute_argv();
let wasi_unstable = let module_registry = ModuleRegistry::new(&store, &preopen_dirs, &argv, &self.vars)?;
create_wasi_instance_snapshot_0(&store, &preopen_dirs, &argv, &self.vars)?;
let wasi_snapshot_preview1 =
create_wasi_instance(&store, &preopen_dirs, &argv, &self.vars)?;
module_registry.insert("wasi_unstable".to_owned(), wasi_unstable);
module_registry.insert("wasi_snapshot_preview1".to_owned(), wasi_snapshot_preview1);
// Load the preload wasm modules. // Load the preload wasm modules.
for preload in self.preloads.iter() { for preload in self.preloads.iter() {
@@ -208,7 +197,7 @@ impl RunCommand {
fn instantiate_module( fn instantiate_module(
store: &Store, store: &Store,
module_registry: &HashMap<String, Instance>, module_registry: &ModuleRegistry,
path: &Path, path: &Path,
) -> Result<(Instance, Module, Vec<u8>)> { ) -> Result<(Instance, Module, Vec<u8>)> {
// Read the wasm module binary either as `*.wat` or a raw binary // Read the wasm module binary either as `*.wat` or a raw binary
@@ -221,20 +210,20 @@ impl RunCommand {
.imports() .imports()
.iter() .iter()
.map(|i| { .map(|i| {
let module_name = i.module(); let export = match i.module() {
if let Some(instance) = module_registry.get(module_name) { "wasi_snapshot_preview1" => {
let field_name = i.name(); module_registry.wasi_snapshot_preview1.get_export(i.name())
if let Some(export) = instance.get_export(field_name) {
Ok(export.clone())
} else {
bail!(
"Import {} was not found in module {}",
field_name,
module_name
)
} }
} else { "wasi_unstable" => module_registry.wasi_unstable.get_export(i.name()),
bail!("Import module {} was not found", module_name) other => bail!("import module `{}` was not found", other),
};
match export {
Some(export) => Ok(export.clone().into()),
None => bail!(
"import `{}` was not found in module `{}`",
i.name(),
i.module()
),
} }
}) })
.collect::<Result<Vec<_>, _>>()?; .collect::<Result<Vec<_>, _>>()?;
@@ -245,11 +234,7 @@ impl RunCommand {
Ok((instance, module, data)) Ok((instance, module, data))
} }
fn handle_module( fn handle_module(&self, store: &Store, module_registry: &ModuleRegistry) -> Result<()> {
&self,
store: &Store,
module_registry: &HashMap<String, Instance>,
) -> Result<()> {
let (instance, module, data) = let (instance, module, data) =
Self::instantiate_module(store, module_registry, &self.module)?; Self::instantiate_module(store, module_registry, &self.module)?;
@@ -337,3 +322,40 @@ impl RunCommand {
Ok(()) Ok(())
} }
} }
struct ModuleRegistry {
wasi_snapshot_preview1: Wasi,
wasi_unstable: WasiSnapshot0,
}
impl ModuleRegistry {
fn new(
store: &Store,
preopen_dirs: &[(String, File)],
argv: &[String],
vars: &[(String, String)],
) -> Result<ModuleRegistry> {
let mut cx1 = wasi_common::WasiCtxBuilder::new()
.inherit_stdio()
.args(argv)
.envs(vars);
for (name, file) in preopen_dirs {
cx1 = cx1.preopened_dir(file.try_clone()?, name);
}
let cx1 = cx1.build()?;
let mut cx2 = wasi_common::old::snapshot_0::WasiCtxBuilder::new()
.inherit_stdio()
.args(argv)
.envs(vars);
for (name, file) in preopen_dirs {
cx2 = cx2.preopened_dir(file.try_clone()?, name);
}
let cx2 = cx2.build()?;
Ok(ModuleRegistry {
wasi_snapshot_preview1: Wasi::new(store, cx1),
wasi_unstable: WasiSnapshot0::new(store, cx2),
})
}
}

View File

@@ -49,21 +49,9 @@ mod tests {
// Locate "memory" export, get base address and size and set memory protection to PROT_NONE // Locate "memory" export, get base address and size and set memory protection to PROT_NONE
fn set_up_memory(instance: &Instance) -> (*mut u8, usize) { fn set_up_memory(instance: &Instance) -> (*mut u8, usize) {
let mem_export = instance.get_wasmtime_memory().expect("memory"); let mem_export = instance.get_export("memory").unwrap().memory().unwrap();
let base = mem_export.data_ptr();
let (base, length) = if let wasmtime_runtime::Export::Memory { let length = mem_export.data_size();
definition,
vmctx: _,
memory: _,
} = mem_export
{
unsafe {
let definition = std::ptr::read(definition);
(definition.base, definition.current_length)
}
} else {
panic!("expected memory");
};
// So we can later trigger SIGSEGV by performing a read // So we can later trigger SIGSEGV by performing a read
unsafe { unsafe {