Add basic GuestString support to wiggle (#13)
* Add basic GuestString support to wiggle This commit adds basic `GuestString` support to `wiggle`. `GuestString` is a wrapper around `GuestArray<'_, u8>` array type which itself can be made into either an owned (cloned) Rust `String` or borrowed as a reference `&str`. In both cases, `GuestString` ensures that the underlying bytes are valid Unicode code units, throwing a `InvalidUtf8` error if not. This commit adds support *only* for passing in strings as arguments in WASI. Marshalling of the return arg has not yet been implemented. I'm not even sure it's possible without multi-value return args feature of Wasm. It's not a major setback especially since the WASI spec (and this includes even the `ephemeral` snapshot) doesn't return strings anywhere. They are only ever passed in as arguments to interface functions. It should be noted that error returned in case of invalid UTF-8 requires a lot more love as it doesn't include anything besides flagging an event that the string contained an invalid Unicode code unit. * Borrow all of string's memory including nul-byte Borrow all of string's underlying memory including the nul-byte. This perhaps might not have a tremendous impact on anything, but since the nul-byte is technically part of the WASI string, we should include it in the borrow as well. * Fill in wiggle-generate blanks for strings * Print to screen passed string in proptest * Strings are PointerLengthPairs! * Fix generation of strings in compound types * Update test with simple string strategy * Generate better test strings * Finalise proptest for strings * Fix formatting * Update crates/runtime/src/memory/string.rs Removes unnecessary comment in code * Apply Pat's suggestion to wrap Utf8Error as error
This commit is contained in:
@@ -2,7 +2,7 @@ use proptest::prelude::*;
|
||||
use std::convert::TryFrom;
|
||||
use wiggle_runtime::{
|
||||
GuestArray, GuestError, GuestErrorType, GuestMemory, GuestPtr, GuestPtrMut, GuestRef,
|
||||
GuestRefMut,
|
||||
GuestRefMut, GuestString,
|
||||
};
|
||||
|
||||
wiggle_generate::from_witx!({
|
||||
@@ -136,6 +136,13 @@ impl foo::Foo for WasiCtx {
|
||||
})?;
|
||||
Ok(old_config ^ other_config)
|
||||
}
|
||||
|
||||
fn hello_string(&mut self, a_string: &GuestString<'_>) -> Result<u32, types::Errno> {
|
||||
let as_ref = a_string.as_ref().expect("deref ptr should succeed");
|
||||
let as_str = as_ref.as_str().expect("valid UTF-8 string");
|
||||
println!("a_string='{}'", as_str);
|
||||
Ok(as_str.len() as u32)
|
||||
}
|
||||
}
|
||||
// Errno is used as a first return value in the functions above, therefore
|
||||
// it must implement GuestErrorType with type Context = WasiCtx.
|
||||
@@ -867,3 +874,87 @@ proptest! {
|
||||
e.test()
|
||||
}
|
||||
}
|
||||
|
||||
fn test_string_strategy() -> impl Strategy<Value = String> {
|
||||
"\\p{Greek}{1,256}"
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct HelloStringExercise {
|
||||
test_word: String,
|
||||
string_ptr_loc: MemArea,
|
||||
string_len_loc: MemArea,
|
||||
return_ptr_loc: MemArea,
|
||||
}
|
||||
|
||||
impl HelloStringExercise {
|
||||
pub fn strat() -> BoxedStrategy<Self> {
|
||||
(test_string_strategy(),)
|
||||
.prop_flat_map(|(test_word,)| {
|
||||
(
|
||||
Just(test_word.clone()),
|
||||
HostMemory::mem_area_strat(test_word.len() as u32),
|
||||
HostMemory::mem_area_strat(4),
|
||||
HostMemory::mem_area_strat(4),
|
||||
)
|
||||
})
|
||||
.prop_map(
|
||||
|(test_word, string_ptr_loc, string_len_loc, return_ptr_loc)| Self {
|
||||
test_word,
|
||||
string_ptr_loc,
|
||||
string_len_loc,
|
||||
return_ptr_loc,
|
||||
},
|
||||
)
|
||||
.prop_filter("non-overlapping pointers", |e| {
|
||||
non_overlapping_set(&[&e.string_ptr_loc, &e.string_len_loc, &e.return_ptr_loc])
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
|
||||
pub fn test(&self) {
|
||||
let mut ctx = WasiCtx::new();
|
||||
let mut host_memory = HostMemory::new();
|
||||
let mut guest_memory = GuestMemory::new(host_memory.as_mut_ptr(), host_memory.len() as u32);
|
||||
|
||||
// Populate string length
|
||||
*guest_memory
|
||||
.ptr_mut(self.string_len_loc.ptr)
|
||||
.expect("ptr mut to string len")
|
||||
.as_ref_mut()
|
||||
.expect("deref ptr mut to string len") = self.test_word.len() as u32;
|
||||
|
||||
// Populate string in guest's memory
|
||||
{
|
||||
let mut next: GuestPtrMut<'_, u8> = guest_memory
|
||||
.ptr_mut(self.string_ptr_loc.ptr)
|
||||
.expect("ptr mut to the first byte of string");
|
||||
for byte in self.test_word.as_bytes() {
|
||||
*next.as_ref_mut().expect("deref mut") = *byte;
|
||||
next = next.elem(1).expect("increment ptr by 1");
|
||||
}
|
||||
}
|
||||
|
||||
let res = foo::hello_string(
|
||||
&mut ctx,
|
||||
&mut guest_memory,
|
||||
self.string_ptr_loc.ptr as i32,
|
||||
self.string_len_loc.ptr as i32,
|
||||
self.return_ptr_loc.ptr as i32,
|
||||
);
|
||||
assert_eq!(res, types::Errno::Ok.into(), "hello string errno");
|
||||
|
||||
let given = *guest_memory
|
||||
.ptr::<u32>(self.return_ptr_loc.ptr)
|
||||
.expect("ptr to return value")
|
||||
.as_ref()
|
||||
.expect("deref ptr to return value");
|
||||
assert_eq!(self.test_word.len() as u32, given);
|
||||
}
|
||||
}
|
||||
proptest! {
|
||||
#[test]
|
||||
fn hello_string(e in HelloStringExercise::strat()) {
|
||||
e.test()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,4 +72,9 @@
|
||||
(result $error $errno)
|
||||
(result $new_config $car_config)
|
||||
)
|
||||
(@interface func (export "hello_string")
|
||||
(param $a_string string)
|
||||
(result $error $errno)
|
||||
(result $total_bytes u32)
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user