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:
@@ -45,16 +45,19 @@ macro_rules! wrappers {
|
||||
unsafe extern "C" fn shim<F, $($args,)* R>(
|
||||
vmctx: *mut VMContext,
|
||||
_caller_vmctx: *mut VMContext,
|
||||
$($args: $args,)*
|
||||
$($args: $args::Abi,)*
|
||||
) -> R::Abi
|
||||
where
|
||||
F: Fn($($args),*) -> R + 'static,
|
||||
$($args: WasmArg,)*
|
||||
R: WasmRet,
|
||||
{
|
||||
let ret = {
|
||||
let instance = InstanceHandle::from_vmctx(vmctx);
|
||||
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 {
|
||||
Ok(ret) => ret.into_abi(),
|
||||
@@ -166,6 +169,36 @@ impl Func {
|
||||
///
|
||||
/// For more information about this function, see [`Func::wrap1`].
|
||||
(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(
|
||||
@@ -248,36 +281,65 @@ impl fmt::Debug for Func {
|
||||
///
|
||||
/// For more information see [`Func::wrap1`]
|
||||
pub trait WasmArg {
|
||||
#[doc(hidden)]
|
||||
type Abi;
|
||||
#[doc(hidden)]
|
||||
fn push(dst: &mut Vec<ValType>);
|
||||
#[doc(hidden)]
|
||||
fn from_abi(vmctx: *mut VMContext, abi: Self::Abi) -> Self;
|
||||
}
|
||||
|
||||
impl WasmArg for () {
|
||||
type Abi = ();
|
||||
fn push(_dst: &mut Vec<ValType>) {}
|
||||
#[inline]
|
||||
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
|
||||
abi
|
||||
}
|
||||
}
|
||||
|
||||
impl WasmArg for i32 {
|
||||
type Abi = Self;
|
||||
fn push(dst: &mut Vec<ValType>) {
|
||||
dst.push(ValType::I32);
|
||||
}
|
||||
#[inline]
|
||||
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
|
||||
abi
|
||||
}
|
||||
}
|
||||
|
||||
impl WasmArg for i64 {
|
||||
type Abi = Self;
|
||||
fn push(dst: &mut Vec<ValType>) {
|
||||
dst.push(ValType::I64);
|
||||
}
|
||||
#[inline]
|
||||
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
|
||||
abi
|
||||
}
|
||||
}
|
||||
|
||||
impl WasmArg for f32 {
|
||||
type Abi = Self;
|
||||
fn push(dst: &mut Vec<ValType>) {
|
||||
dst.push(ValType::F32);
|
||||
}
|
||||
#[inline]
|
||||
fn from_abi(_vmctx: *mut VMContext, abi: Self::Abi) -> Self {
|
||||
abi
|
||||
}
|
||||
}
|
||||
|
||||
impl WasmArg for f64 {
|
||||
type Abi = Self;
|
||||
fn push(dst: &mut Vec<ValType>) {
|
||||
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
|
||||
|
||||
@@ -2,7 +2,6 @@ use crate::externals::Extern;
|
||||
use crate::module::Module;
|
||||
use crate::runtime::Store;
|
||||
use crate::trap::Trap;
|
||||
use crate::types::{ExportType, ExternType};
|
||||
use anyhow::{Error, Result};
|
||||
use wasmtime_jit::{CompiledModule, Resolver};
|
||||
use wasmtime_runtime::{Export, InstanceHandle, InstantiationError};
|
||||
@@ -173,50 +172,8 @@ impl Instance {
|
||||
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)]
|
||||
pub fn handle(&self) -> &InstanceHandle {
|
||||
&self.instance_handle
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub fn get_wasmtime_memory(&self) -> Option<wasmtime_runtime::Export> {
|
||||
self.instance_handle.lookup("memory")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,13 +272,6 @@ impl Module {
|
||||
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> {
|
||||
let compiled = CompiledModule::new(
|
||||
&mut store.compiler_mut(),
|
||||
|
||||
@@ -157,25 +157,6 @@ impl ExternType {
|
||||
(Table(TableType) table unwrap_table)
|
||||
(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
|
||||
|
||||
@@ -18,9 +18,7 @@ doc = false
|
||||
|
||||
[dependencies]
|
||||
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-runtime = { path = "../../runtime", version = "0.9.0" }
|
||||
wasmtime-wasi = { path = "../../wasi", version = "0.9.0" }
|
||||
target-lexicon = { version = "0.10.0", default-features = false }
|
||||
anyhow = "1.0.19"
|
||||
|
||||
@@ -89,9 +89,11 @@ pub fn instantiate(
|
||||
// If this module expects to be able to use wasi then go ahead and hook
|
||||
// that up into the imported crates.
|
||||
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()))?;
|
||||
Some((module_name, instance))
|
||||
let wasi = wasmtime_wasi::Wasi::new(&store, cx);
|
||||
Some((module_name, wasi))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
@@ -111,7 +113,7 @@ pub fn instantiate(
|
||||
.ok_or_else(|| {
|
||||
PyErr::new::<Exception, _>(format!("wasi export {} is not found", i.name(),))
|
||||
})?;
|
||||
imports.push(e.clone());
|
||||
imports.push(e.clone().into());
|
||||
} else {
|
||||
return Err(PyErr::new::<Exception, _>(format!(
|
||||
"imported module {} is not found",
|
||||
|
||||
@@ -62,14 +62,14 @@ fn generate_load(item: &syn::ItemTrait) -> syn::Result<TokenStream> {
|
||||
|
||||
let mut imports: Vec<Extern> = Vec::new();
|
||||
if let Some(module_name) = data.find_wasi_module_name() {
|
||||
let wasi_instance = #root::wasmtime_wasi::create_wasi_instance(&store, &[], &[], &[])
|
||||
.map_err(|e| format_err!("wasm instantiation error: {:?}", e))?;
|
||||
let wasi_cx = #root::wasmtime_wasi::WasiCtxBuilder::new().build()?;
|
||||
let wasi = #root::wasmtime_wasi::Wasi::new(&store, wasi_cx);
|
||||
for i in module.imports().iter() {
|
||||
if i.module() != module_name {
|
||||
bail!("unknown import module {}", i.module());
|
||||
}
|
||||
if let Some(export) = wasi_instance.get_export(i.name()) {
|
||||
imports.push(export.clone());
|
||||
if let Some(export) = wasi.get_export(i.name()) {
|
||||
imports.push(export.clone().into());
|
||||
} else {
|
||||
bail!("unknown import {}:{}", i.module(), i.name())
|
||||
}
|
||||
|
||||
@@ -11,9 +11,6 @@ cfg-if = "0.1.9"
|
||||
|
||||
[dev-dependencies]
|
||||
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 = { path = "../api", version = "0.9.0" }
|
||||
target-lexicon = "0.10.0"
|
||||
|
||||
@@ -33,14 +33,7 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
|
||||
// stdin is closed which causes tests to fail.
|
||||
let (reader, _writer) = os_pipe::pipe()?;
|
||||
builder = builder.stdin(reader_to_file(reader));
|
||||
let snapshot1 = Instance::from_handle(
|
||||
&store,
|
||||
wasmtime_wasi::instantiate_wasi_with_context(
|
||||
builder.build().context("failed to build wasi context")?,
|
||||
)
|
||||
.context("failed to instantiate wasi")?,
|
||||
);
|
||||
|
||||
let snapshot1 = wasmtime_wasi::Wasi::new(&store, builder.build()?);
|
||||
let module = Module::new(&store, &data).context("failed to create wasm module")?;
|
||||
let imports = module
|
||||
.imports()
|
||||
@@ -48,7 +41,7 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
|
||||
.map(|i| {
|
||||
let field_name = i.name();
|
||||
if let Some(export) = snapshot1.get_export(field_name) {
|
||||
Ok(export.clone())
|
||||
Ok(export.clone().into())
|
||||
} else {
|
||||
bail!(
|
||||
"import {} was not found in module {}",
|
||||
|
||||
@@ -34,8 +34,8 @@ pub fn witx_wasi32_types(args: TokenStream) -> TokenStream {
|
||||
|
||||
/// A single-use macro in the `wasmtime-wasi` crate.
|
||||
#[proc_macro]
|
||||
pub fn define_add_wrappers_to_module(args: TokenStream) -> TokenStream {
|
||||
wasi::add_wrappers_to_module(args.into()).into()
|
||||
pub fn define_wasi_struct(args: TokenStream) -> TokenStream {
|
||||
wasi::define_struct(args.into()).into()
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
|
||||
@@ -11,18 +11,16 @@ enum Abi {
|
||||
|
||||
/// 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
|
||||
/// use the input arguments to register all wasi functions inside of a `Module`
|
||||
/// instance. This will automatically assign the wasm underlying types and
|
||||
/// perform conversions from the wasm type to the underlying wasi type (often
|
||||
/// unsigned or of a smaller width).
|
||||
/// This macro will generate a structure, `Wasi`, which will create all the
|
||||
/// functions necessary to bind wasi and hook everything up via the `wasmtime`
|
||||
/// crate.
|
||||
///
|
||||
/// The generated shim functions here will also `trace!` their arguments for
|
||||
/// logging purposes. Otherwise this is hopefully somewhat straightforward!
|
||||
///
|
||||
/// I'd recommend using `cargo +nightly expand` to explore the output of this
|
||||
/// 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 doc = match witx::load(&[&path]) {
|
||||
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 func in module.funcs() {
|
||||
let name = func.name.as_str();
|
||||
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 params = Vec::new();
|
||||
@@ -178,56 +182,71 @@ pub fn add_wrappers_to_module(args: TokenStream) -> TokenStream {
|
||||
}
|
||||
|
||||
let format_str = format!("{}({})", name, formats.join(", "));
|
||||
add.push(quote! {
|
||||
let sig = module.signatures.push(translate_signature(
|
||||
ir::Signature {
|
||||
params: vec![#(cranelift_codegen::ir::AbiParam::new(#params)),*],
|
||||
returns: vec![#(cranelift_codegen::ir::AbiParam::new(#returns)),*],
|
||||
call_conv,
|
||||
},
|
||||
pointer_type,
|
||||
));
|
||||
let func = module.functions.push(sig);
|
||||
module
|
||||
.exports
|
||||
.insert(#name.to_owned(), Export::Function(func));
|
||||
|
||||
unsafe extern "C" fn #name_ident(
|
||||
ctx: *mut wasmtime_runtime::VMContext,
|
||||
caller_ctx: *mut wasmtime_runtime::VMContext,
|
||||
#(#shim_arg_decls),*
|
||||
) -> #ret_ty {
|
||||
log::trace!(
|
||||
#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 _);
|
||||
let wrap = format_ident!("wrap{}", shim_arg_decls.len() + 1);
|
||||
ctor_externs.push(quote! {
|
||||
let my_cx = cx.clone();
|
||||
let #name_ident = wasmtime::Func::#wrap(
|
||||
store,
|
||||
move |mem: crate::WasiCallerMemory #(,#shim_arg_decls)*| -> #ret_ty {
|
||||
log::trace!(
|
||||
#format_str,
|
||||
#(#format_args),*
|
||||
);
|
||||
unsafe {
|
||||
let memory = match mem.get() {
|
||||
Ok(e) => e,
|
||||
Err(e) => #handle_early_error,
|
||||
};
|
||||
hostcalls::#name_ident(
|
||||
&mut my_cx.borrow_mut(),
|
||||
memory,
|
||||
#(#hostcall_args),*
|
||||
) #cvt_ret
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
quote! {
|
||||
pub fn add_wrappers_to_module(
|
||||
module: &mut Module,
|
||||
finished_functions: &mut PrimaryMap<DefinedFuncIndex, *const wasmtime_runtime::VMFunctionBody>,
|
||||
call_conv: isa::CallConv,
|
||||
pointer_type: types::Type,
|
||||
) {
|
||||
#(#add)*
|
||||
/// An instantiated instance of the wasi exports.
|
||||
///
|
||||
/// This represents a wasi module which can be used to instantiate other
|
||||
/// wasm modules. This structure exports all that various fields of the
|
||||
/// wasi instance as fields which can be used to implement your own
|
||||
/// instantiation logic, if necessary. Additionally [`Wasi::get_export`]
|
||||
/// 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,16 +11,11 @@ readme = "README.md"
|
||||
edition = "2018"
|
||||
|
||||
[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-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" }
|
||||
|
||||
[badges]
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
15
crates/wasi/src/old/snapshot_0.rs
Normal file
15
crates/wasi/src/old/snapshot_0.rs
Normal 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")
|
||||
}
|
||||
@@ -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"
|
||||
);
|
||||
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user