Correctly unpack &mut [T] into *mut T and usize

This commit is contained in:
Jakub Konka
2019-05-14 16:52:01 +02:00
committed by Dan Gohman
parent 006198eb92
commit 52eda1498a
7 changed files with 52 additions and 11 deletions

View File

@@ -45,7 +45,7 @@ cargo build
# Run the tests. We run these in debug mode so that assertions are enabled.
banner "Rust unit tests"
RUST_BACKTRACE=1 cargo test
RUST_BACKTRACE=1 cargo test --all
# Make sure the documentation builds.
banner "Rust documentation: $topdir/target/doc/wasi-common/index.html"

View File

@@ -2,7 +2,7 @@ extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{ArgCaptured, FnArg, Pat, PatIdent, Type, TypeReference};
use syn::{ArgCaptured, FnArg, Pat, PatIdent, Type, TypeReference, TypeSlice};
#[proc_macro_attribute]
pub fn wasi_common_cbindgen(attr: TokenStream, function: TokenStream) -> TokenStream {
@@ -36,11 +36,31 @@ pub fn wasi_common_cbindgen(attr: TokenStream, function: TokenStream) -> TokenSt
if let Type::Reference(ty @ TypeReference { .. }) = &ty {
// if we're here, then we found a &-ref
// so substitute it for *mut since we're exporting to C
let elem = &ty.elem;
arg_type.push(quote!(*mut #elem));
// we need to properly dereference the substituted raw
// pointer if we are to properly call the hostcall fn
call_arg_ident.push(quote!(&mut *#ident));
let elem = &*ty.elem;
if let Type::Slice(elem @ TypeSlice { .. }) = &elem {
// slice: &[type] or &mut [type]
// in C it requires a signature *mut type
let elem = &elem.elem;
arg_type.push(quote!(*mut #elem));
// since it's a slice, we'll need to do more work here
// simple dereferencing is not enough
// firstly, we need to add a len arg to C fn
// secondly, we need to invoke std::slice::from_raw_parts_mut(..)
let concatenated = format!("{}_len", ident);
let len_ident = syn::Ident::new(&concatenated, ident.span());
call_arg_ident.push(quote! {
std::slice::from_raw_parts_mut(#ident, #len_ident)
});
arg_ident.push(quote!(#len_ident));
arg_type.push(quote!(usize));
} else {
// & or &mut type
// so simply substitute with *mut type
arg_type.push(quote!(*mut #elem));
// we need to properly dereference the substituted raw
// pointer if we are to properly call the hostcall fn
call_arg_ident.push(quote!(&mut *#ident));
}
} else {
arg_type.push(quote!(#ty));
// non-&-ref type, so preserve whatever the arg was

View File

@@ -0,0 +1,20 @@
extern crate wasi_common_cbindgen;
pub use wasi_common_cbindgen::wasi_common_cbindgen;
#[wasi_common_cbindgen]
fn array_args(a: &mut [u8]) {
a[0] = 1;
}
fn main() {
let mut expected: &mut [u8] = &mut [0, 0];
array_args(&mut expected);
let given: &mut [u8] = &mut [0, 0];
unsafe {
__wasi_array_args(given.as_mut_ptr(), given.len());
}
assert_eq!(given, expected);
}

View File

@@ -17,4 +17,4 @@ fn main() {
Box::from_raw(raw)
};
assert_eq!(*given, *expected);
}
}

View File

@@ -17,4 +17,4 @@ fn main() {
res
};
assert_eq!(given, expected);
}
}

View File

@@ -5,4 +5,5 @@ fn tests() {
t.pass("tests/val_args.rs");
t.pass("tests/ref_args.rs");
t.pass("tests/mut_args.rs");
}
t.pass("tests/array_args.rs");
}

View File

@@ -9,4 +9,4 @@ fn val_args(a: usize, b: usize) -> usize {
fn main() {
assert_eq!(unsafe { __wasi_val_args(1, 2) }, val_args(1, 2));
}
}