wiggle: adapt Wiggle strings for shared use (#5264)
* wiggle: adapt Wiggle strings for shared use This is an extension of #5229 for the `&str` and `&mut str` types. As documented there, we are attempting to maintain Rust guarantees for slices that Wiggle hands out in the presence of WebAssembly shared memory, in which case multiple threads could be modifying the underlying data of the slice. This change changes the API of `GuestPtr` to return an `Option` which is `None` when attempting to view the WebAssembly data as a string and the underlying WebAssembly memory is shared. This reuses the `UnsafeGuestSlice` structure from #5229 to do so and appropriately marks the region as borrowed in Wiggle's manual borrow checker. Each original call site in this project's WASI implementations is fixed up to `expect` that a non-shared memory is used. (Note that I can find no uses of `GuestStrMut` in the WASI implementations). * wiggle: make `GuestStr*` containers wrappers of `GuestSlice*` This change makes it possible to reuse the underlying logic in `UnsafeGuestSlice` and the `GuestSlice*` implementations to continue to expose the `GuestStr` and `GuestStrMut` types. These types now are simple wrappers of their `GuestSlice*` variant. The UTF-8 validation that distinguished `GuestStr*` now lives in the `TryFrom` implementations for each type.
This commit is contained in:
@@ -10,7 +10,10 @@ impl_errno!(types::Errno);
|
||||
|
||||
impl<'a> strings::Strings for WasiCtx<'a> {
|
||||
fn hello_string(&mut self, a_string: &GuestPtr<str>) -> Result<u32, types::Errno> {
|
||||
let s = a_string.as_str().expect("should be valid string");
|
||||
let s = a_string
|
||||
.as_str()
|
||||
.expect("should be valid string")
|
||||
.expect("expected non-shared memory");
|
||||
println!("a_string='{}'", &*s);
|
||||
Ok(s.len() as u32)
|
||||
}
|
||||
@@ -21,9 +24,18 @@ impl<'a> strings::Strings for WasiCtx<'a> {
|
||||
b: &GuestPtr<str>,
|
||||
c: &GuestPtr<str>,
|
||||
) -> Result<u32, types::Errno> {
|
||||
let sa = a.as_str().expect("A should be valid string");
|
||||
let sb = b.as_str().expect("B should be valid string");
|
||||
let sc = c.as_str().expect("C should be valid string");
|
||||
let sa = a
|
||||
.as_str()
|
||||
.expect("A should be valid string")
|
||||
.expect("expected non-shared memory");
|
||||
let sb = b
|
||||
.as_str()
|
||||
.expect("B should be valid string")
|
||||
.expect("expected non-shared memory");
|
||||
let sc = c
|
||||
.as_str()
|
||||
.expect("C should be valid string")
|
||||
.expect("expected non-shared memory");
|
||||
let total_len = sa.len() + sb.len() + sc.len();
|
||||
println!(
|
||||
"len={}, a='{}', b='{}', c='{}'",
|
||||
|
||||
Reference in New Issue
Block a user