Merge remote-tracking branch 'origin/main' into pch/wasi_common_cap_std

This commit is contained in:
Pat Hickey
2021-02-01 13:21:25 -08:00
12 changed files with 178 additions and 59 deletions

View File

@@ -59,6 +59,7 @@ unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> (Vec<DirEntr
let bufused =
wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir");
assert!(bufused <= BUF_LEN);
let sl = slice::from_raw_parts(buf.as_ptr(), bufused);
let dirs: Vec<_> = ReadDir::from_slice(sl).collect();
let eof = bufused < BUF_LEN;

View File

@@ -156,7 +156,7 @@ impl TryFrom<std::io::Error> for types::Errno {
winerror::ERROR_TOO_MANY_OPEN_FILES => Some(types::Errno::Nfile),
winerror::ERROR_ACCESS_DENIED => Some(types::Errno::Acces),
winerror::ERROR_SHARING_VIOLATION => Some(types::Errno::Acces),
winerror::ERROR_PRIVILEGE_NOT_HELD => Some(types::Errno::Notcapable),
winerror::ERROR_PRIVILEGE_NOT_HELD => Some(types::Errno::Perm),
winerror::ERROR_INVALID_HANDLE => Some(types::Errno::Badf),
winerror::ERROR_INVALID_NAME => Some(types::Errno::Noent),
winerror::ERROR_NOT_ENOUGH_MEMORY => Some(types::Errno::Nomem),

View File

@@ -160,6 +160,17 @@ impl Extern {
Extern::Module(_) => "module",
}
}
pub(crate) fn wasmtime_export(&self) -> wasmtime_runtime::Export {
match self {
Extern::Func(f) => f.wasmtime_export().clone().into(),
Extern::Global(f) => f.wasmtime_export().clone().into(),
Extern::Table(f) => f.wasmtime_export().clone().into(),
Extern::Memory(f) => f.wasmtime_export().clone().into(),
Extern::Instance(f) => wasmtime_runtime::Export::Instance(f.wasmtime_export().clone()),
Extern::Module(f) => wasmtime_runtime::Export::Module(Box::new(f.clone())),
}
}
}
impl From<Func> for Extern {

View File

@@ -121,6 +121,10 @@ impl Instance {
ty
}
pub(crate) fn wasmtime_export(&self) -> &RuntimeInstance {
&self.items
}
/// Returns the associated [`Store`] that this `Instance` is compiled into.
///
/// This is the [`Store`] that generally serves as a sort of global cache

View File

@@ -402,34 +402,53 @@ impl Linker {
fn command(&mut self, module_name: &str, module: &Module) -> Result<&mut Self> {
for export in module.exports() {
if let Some(func_ty) = export.ty().func() {
let imports = self.compute_imports(module)?;
let store = self.store.clone();
let imports = self
.compute_imports(module)?
.into_iter()
.map(|e| e.wasmtime_export())
.collect::<Vec<_>>();
let module = module.clone();
let export_name = export.name().to_owned();
let func = Func::new(&self.store, func_ty.clone(), move |_, params, results| {
// Create a new instance for this command execution.
let instance = Instance::new(&store, &module, &imports)?;
let func = Func::new(
&self.store,
func_ty.clone(),
move |caller, params, results| {
let store = caller.store();
// `unwrap()` everything here because we know the instance contains a
// function export with the given name and signature because we're
// iterating over the module it was instantiated from.
let command_results = instance
.get_export(&export_name)
.unwrap()
.into_func()
.unwrap()
.call(params)
.map_err(|error| error.downcast::<Trap>().unwrap())?;
// Note that the unsafety here is due to the validity of
// `i` and the validity of `i` within `store`. For our
// case though these items all come from `imports` above
// so they're all valid. They're also all kept alive by
// the store itself used here so this should be safe.
let imports = imports
.iter()
.map(|i| unsafe { Extern::from_wasmtime_export(&i, &store) })
.collect::<Vec<_>>();
// Copy the return values into the output slice.
for (result, command_result) in
results.iter_mut().zip(command_results.into_vec())
{
*result = command_result;
}
// Create a new instance for this command execution.
let instance = Instance::new(&store, &module, &imports)?;
Ok(())
});
// `unwrap()` everything here because we know the instance contains a
// function export with the given name and signature because we're
// iterating over the module it was instantiated from.
let command_results = instance
.get_export(&export_name)
.unwrap()
.into_func()
.unwrap()
.call(params)
.map_err(|error| error.downcast::<Trap>().unwrap())?;
// Copy the return values into the output slice.
for (result, command_result) in
results.iter_mut().zip(command_results.into_vec())
{
*result = command_result;
}
Ok(())
},
);
self.insert(module_name, export.name(), func.into())?;
} else if export.name() == "memory" && export.ty().memory().is_some() {
// Allow an exported "memory" memory for now.