Merge pull request #1336 from bytecodealliance/pch/wiggle_tweaks_for_lucet

[wiggle] Tweaks from lucet integration
This commit is contained in:
Pat Hickey
2020-03-20 14:48:10 -07:00
committed by GitHub
9 changed files with 70 additions and 40 deletions

View File

@@ -14,11 +14,14 @@ use wiggle_runtime::{GuestBorrows, GuestPtr};
impl<'a> WasiSnapshotPreview1 for WasiCtx { impl<'a> WasiSnapshotPreview1 for WasiCtx {
fn args_get<'b>( fn args_get<'b>(
&self, &self,
mut argv: GuestPtr<'b, GuestPtr<'b, u8>>, argv: &GuestPtr<'b, GuestPtr<'b, u8>>,
mut argv_buf: GuestPtr<'b, u8>, argv_buf: &GuestPtr<'b, u8>,
) -> Result<()> { ) -> Result<()> {
trace!("args_get(argv_ptr={:?}, argv_buf={:?})", argv, argv_buf); trace!("args_get(argv_ptr={:?}, argv_buf={:?})", argv, argv_buf);
let mut argv = argv.clone();
let mut argv_buf = argv_buf.clone();
for arg in &self.args { for arg in &self.args {
let arg_bytes = arg.as_bytes_with_nul(); let arg_bytes = arg.as_bytes_with_nul();
let elems = arg_bytes.len().try_into()?; let elems = arg_bytes.len().try_into()?;
@@ -49,8 +52,8 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
fn environ_get<'b>( fn environ_get<'b>(
&self, &self,
mut environ: GuestPtr<'b, GuestPtr<'b, u8>>, environ: &GuestPtr<'b, GuestPtr<'b, u8>>,
mut environ_buf: GuestPtr<'b, u8>, environ_buf: &GuestPtr<'b, u8>,
) -> Result<()> { ) -> Result<()> {
trace!( trace!(
"environ_get(environ={:?}, environ_buf={:?})", "environ_get(environ={:?}, environ_buf={:?})",
@@ -58,6 +61,9 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
environ_buf environ_buf
); );
let mut environ = environ.clone();
let mut environ_buf = environ_buf.clone();
for e in &self.env { for e in &self.env {
let environ_bytes = e.as_bytes_with_nul(); let environ_bytes = e.as_bytes_with_nul();
let elems = environ_bytes.len().try_into()?; let elems = environ_bytes.len().try_into()?;
@@ -409,7 +415,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
fn fd_prestat_dir_name( fn fd_prestat_dir_name(
&self, &self,
fd: types::Fd, fd: types::Fd,
path: GuestPtr<u8>, path: &GuestPtr<u8>,
path_len: types::Size, path_len: types::Size,
) -> Result<()> { ) -> Result<()> {
trace!( trace!(
@@ -536,7 +542,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
fn fd_readdir( fn fd_readdir(
&self, &self,
fd: types::Fd, fd: types::Fd,
buf: GuestPtr<u8>, buf: &GuestPtr<u8>,
buf_len: types::Size, buf_len: types::Size,
cookie: types::Dircookie, cookie: types::Dircookie,
) -> Result<types::Size> { ) -> Result<types::Size> {
@@ -555,10 +561,11 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
fn copy_entities<T: Iterator<Item = Result<(types::Dirent, String)>>>( fn copy_entities<T: Iterator<Item = Result<(types::Dirent, String)>>>(
iter: T, iter: T,
mut buf: GuestPtr<u8>, buf: &GuestPtr<u8>,
buf_len: types::Size, buf_len: types::Size,
) -> Result<types::Size> { ) -> Result<types::Size> {
let mut bufused = 0; let mut bufused = 0;
let mut buf = buf.clone();
for pair in iter { for pair in iter {
let (dirent, name) = pair?; let (dirent, name) = pair?;
let dirent_raw = dirent.as_bytes()?; let dirent_raw = dirent.as_bytes()?;
@@ -971,7 +978,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
&self, &self,
dirfd: types::Fd, dirfd: types::Fd,
path: &GuestPtr<'_, str>, path: &GuestPtr<'_, str>,
buf: GuestPtr<u8>, buf: &GuestPtr<u8>,
buf_len: types::Size, buf_len: types::Size,
) -> Result<types::Size> { ) -> Result<types::Size> {
trace!( trace!(
@@ -1140,8 +1147,8 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
fn poll_oneoff( fn poll_oneoff(
&self, &self,
in_: GuestPtr<types::Subscription>, in_: &GuestPtr<types::Subscription>,
out: GuestPtr<types::Event>, out: &GuestPtr<types::Event>,
nsubscriptions: types::Size, nsubscriptions: types::Size,
) -> Result<types::Size> { ) -> Result<types::Size> {
trace!( trace!(
@@ -1294,7 +1301,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
Ok(()) Ok(())
} }
fn random_get(&self, buf: GuestPtr<u8>, buf_len: types::Size) -> Result<()> { fn random_get(&self, buf: &GuestPtr<u8>, buf_len: types::Size) -> Result<()> {
trace!("random_get(buf={:?}, buf_len={:?})", buf, buf_len); trace!("random_get(buf={:?}, buf_len={:?})", buf, buf_len);
let slice = unsafe { let slice = unsafe {

View File

@@ -2,6 +2,7 @@ use proc_macro2::TokenStream;
use quote::quote; use quote::quote;
use crate::lifetimes::anon_lifetime; use crate::lifetimes::anon_lifetime;
use crate::module_trait::passed_by_reference;
use crate::names::Names; use crate::names::Names;
pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream { pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream {
@@ -67,10 +68,10 @@ pub fn define_func(names: &Names, func: &witx::InterfaceFunc) -> TokenStream {
.map(|p| marshal_arg(names, p, error_handling(p.name.as_str()))); .map(|p| marshal_arg(names, p, error_handling(p.name.as_str())));
let trait_args = func.params.iter().map(|param| { let trait_args = func.params.iter().map(|param| {
let name = names.func_param(&param.name); let name = names.func_param(&param.name);
match param.tref.type_().passed_by() { if passed_by_reference(&*param.tref.type_()) {
witx::TypePassedBy::Value { .. } => quote!(#name), quote!(&#name)
witx::TypePassedBy::Pointer { .. } => quote!(&#name), } else {
witx::TypePassedBy::PointerLengthPair { .. } => quote!(&#name), quote!(#name)
} }
}); });

View File

@@ -5,6 +5,21 @@ use crate::lifetimes::{anon_lifetime, LifetimeExt};
use crate::names::Names; use crate::names::Names;
use witx::Module; use witx::Module;
pub fn passed_by_reference(ty: &witx::Type) -> bool {
let passed_by = match ty.passed_by() {
witx::TypePassedBy::Value { .. } => false,
witx::TypePassedBy::Pointer { .. } | witx::TypePassedBy::PointerLengthPair { .. } => true,
};
match ty {
witx::Type::Builtin(b) => match &*b {
witx::BuiltinType::String => true,
_ => passed_by,
},
witx::Type::Pointer(_) | witx::Type::ConstPointer(_) | witx::Type::Array(_) => true,
_ => passed_by,
}
}
pub fn define_module_trait(names: &Names, m: &Module) -> TokenStream { pub fn define_module_trait(names: &Names, m: &Module) -> TokenStream {
let traitname = names.trait_name(&m.name); let traitname = names.trait_name(&m.name);
let traitmethods = m.funcs().map(|f| { let traitmethods = m.funcs().map(|f| {
@@ -25,10 +40,10 @@ pub fn define_module_trait(names: &Names, m: &Module) -> TokenStream {
let args = f.params.iter().map(|arg| { let args = f.params.iter().map(|arg| {
let arg_name = names.func_param(&arg.name); let arg_name = names.func_param(&arg.name);
let arg_typename = names.type_ref(&arg.tref, lifetime.clone()); let arg_typename = names.type_ref(&arg.tref, lifetime.clone());
let arg_type = match arg.tref.type_().passed_by() { let arg_type = if passed_by_reference(&*arg.tref.type_()) {
witx::TypePassedBy::Value { .. } => quote!(#arg_typename), quote!(&#arg_typename)
witx::TypePassedBy::Pointer { .. } => quote!(&#arg_typename), } else {
witx::TypePassedBy::PointerLengthPair { .. } => quote!(&#arg_typename), quote!(#arg_typename)
}; };
quote!(#arg_name: #arg_type) quote!(#arg_name: #arg_type)
}); });

View File

@@ -38,7 +38,7 @@ impl Names {
BuiltinType::F32 => quote!(f32), BuiltinType::F32 => quote!(f32),
BuiltinType::F64 => quote!(f64), BuiltinType::F64 => quote!(f64),
BuiltinType::Char8 => quote!(u8), BuiltinType::Char8 => quote!(u8),
BuiltinType::USize => quote!(usize), BuiltinType::USize => quote!(u32),
} }
} }
pub fn atom_type(&self, atom: AtomType) -> TokenStream { pub fn atom_type(&self, atom: AtomType) -> TokenStream {
@@ -66,7 +66,11 @@ impl Names {
let pointee_type = self.type_ref(&pointee, lifetime.clone()); let pointee_type = self.type_ref(&pointee, lifetime.clone());
quote!(wiggle_runtime::GuestPtr<#lifetime, #pointee_type>) quote!(wiggle_runtime::GuestPtr<#lifetime, #pointee_type>)
} }
_ => unimplemented!("anonymous type ref"), witx::Type::Array(pointee) => {
let pointee_type = self.type_ref(&pointee, lifetime.clone());
quote!(wiggle_runtime::GuestPtr<#lifetime, [#pointee_type]>)
}
_ => unimplemented!("anonymous type ref {:?}", tref),
}, },
} }
} }

View File

@@ -108,9 +108,9 @@ macro_rules! primitives {
primitives! { primitives! {
// signed // signed
i8 i16 i32 i64 i128 isize i8 i16 i32 i64 i128
// unsigned // unsigned
u8 u16 u32 u64 u128 usize u8 u16 u32 u64 u128
// floats // floats
f32 f64 f32 f64
} }

View File

@@ -14,7 +14,7 @@ impl<'a> flags::Flags for WasiCtx<'a> {
fn configure_car( fn configure_car(
&self, &self,
old_config: types::CarConfig, old_config: types::CarConfig,
other_config_ptr: GuestPtr<types::CarConfig>, other_config_ptr: &GuestPtr<types::CarConfig>,
) -> Result<types::CarConfig, types::Errno> { ) -> Result<types::CarConfig, types::Errno> {
let other_config = other_config_ptr.read().map_err(|e| { let other_config = other_config_ptr.read().map_err(|e| {
eprintln!("old_config_ptr error: {}", e); eprintln!("old_config_ptr error: {}", e);

View File

@@ -13,9 +13,9 @@ impl<'a> pointers::Pointers for WasiCtx<'a> {
fn pointers_and_enums<'b>( fn pointers_and_enums<'b>(
&self, &self,
input1: types::Excuse, input1: types::Excuse,
input2_ptr: GuestPtr<'b, types::Excuse>, input2_ptr: &GuestPtr<'b, types::Excuse>,
input3_ptr: GuestPtr<'b, types::Excuse>, input3_ptr: &GuestPtr<'b, types::Excuse>,
input4_ptr_ptr: GuestPtr<'b, GuestPtr<'b, types::Excuse>>, input4_ptr_ptr: &GuestPtr<'b, GuestPtr<'b, types::Excuse>>,
) -> Result<(), types::Errno> { ) -> Result<(), types::Errno> {
println!("BAZ input1 {:?}", input1); println!("BAZ input1 {:?}", input1);
let input2: types::Excuse = input2_ptr.read().map_err(|e| { let input2: types::Excuse = input2_ptr.read().map_err(|e| {
@@ -52,7 +52,7 @@ impl<'a> pointers::Pointers for WasiCtx<'a> {
println!("input4 {:?}", input4); println!("input4 {:?}", input4);
// Write ptr value to mutable ptr: // Write ptr value to mutable ptr:
input4_ptr_ptr.write(input2_ptr).map_err(|e| { input4_ptr_ptr.write(*input2_ptr).map_err(|e| {
eprintln!("input4_ptr_ptr error: {}", e); eprintln!("input4_ptr_ptr error: {}", e);
types::Errno::InvalidArg types::Errno::InvalidArg
})?; })?;

View File

@@ -44,10 +44,13 @@ impl<'a> structs::Structs for WasiCtx<'a> {
fn return_pair_of_ptrs<'b>( fn return_pair_of_ptrs<'b>(
&self, &self,
first: GuestPtr<'b, i32>, first: &GuestPtr<'b, i32>,
second: GuestPtr<'b, i32>, second: &GuestPtr<'b, i32>,
) -> Result<types::PairIntPtrs<'b>, types::Errno> { ) -> Result<types::PairIntPtrs<'b>, types::Errno> {
Ok(types::PairIntPtrs { first, second }) Ok(types::PairIntPtrs {
first: *first,
second: *second,
})
} }
} }

View File

@@ -23,7 +23,7 @@ impl<'a> GuestErrorType<'a> for types::Errno {
} }
impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> { impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> {
fn args_get(&self, _argv: GuestPtr<GuestPtr<u8>>, _argv_buf: GuestPtr<u8>) -> Result<()> { fn args_get(&self, _argv: &GuestPtr<GuestPtr<u8>>, _argv_buf: &GuestPtr<u8>) -> Result<()> {
unimplemented!("args_get") unimplemented!("args_get")
} }
@@ -33,8 +33,8 @@ impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> {
fn environ_get( fn environ_get(
&self, &self,
_environ: GuestPtr<GuestPtr<u8>>, _environ: &GuestPtr<GuestPtr<u8>>,
_environ_buf: GuestPtr<u8>, _environ_buf: &GuestPtr<u8>,
) -> Result<()> { ) -> Result<()> {
unimplemented!("environ_get") unimplemented!("environ_get")
} }
@@ -153,7 +153,7 @@ impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> {
fn fd_prestat_dir_name( fn fd_prestat_dir_name(
&self, &self,
_fd: types::Fd, _fd: types::Fd,
_path: GuestPtr<u8>, _path: &GuestPtr<u8>,
_path_len: types::Size, _path_len: types::Size,
) -> Result<()> { ) -> Result<()> {
unimplemented!("fd_prestat_dir_name") unimplemented!("fd_prestat_dir_name")
@@ -175,7 +175,7 @@ impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> {
fn fd_readdir( fn fd_readdir(
&self, &self,
_fd: types::Fd, _fd: types::Fd,
_buf: GuestPtr<u8>, _buf: &GuestPtr<u8>,
_buf_len: types::Size, _buf_len: types::Size,
_cookie: types::Dircookie, _cookie: types::Dircookie,
) -> Result<types::Size> { ) -> Result<types::Size> {
@@ -260,7 +260,7 @@ impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> {
&self, &self,
_fd: types::Fd, _fd: types::Fd,
_path: &GuestPtr<'_, str>, _path: &GuestPtr<'_, str>,
_buf: GuestPtr<u8>, _buf: &GuestPtr<u8>,
_buf_len: types::Size, _buf_len: types::Size,
) -> Result<types::Size> { ) -> Result<types::Size> {
unimplemented!("path_readlink") unimplemented!("path_readlink")
@@ -295,8 +295,8 @@ impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> {
fn poll_oneoff( fn poll_oneoff(
&self, &self,
_in_: GuestPtr<types::Subscription>, _in_: &GuestPtr<types::Subscription>,
_out: GuestPtr<types::Event>, _out: &GuestPtr<types::Event>,
_nsubscriptions: types::Size, _nsubscriptions: types::Size,
) -> Result<types::Size> { ) -> Result<types::Size> {
unimplemented!("poll_oneoff") unimplemented!("poll_oneoff")
@@ -314,7 +314,7 @@ impl<'a> crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx<'a> {
unimplemented!("sched_yield") unimplemented!("sched_yield")
} }
fn random_get(&self, _buf: GuestPtr<u8>, _buf_len: types::Size) -> Result<()> { fn random_get(&self, _buf: &GuestPtr<u8>, _buf_len: types::Size) -> Result<()> {
unimplemented!("random_get") unimplemented!("random_get")
} }