wiggle: add initial support for shared memory (#5225)
This change is the first in a series of changes to support shared memory in Wiggle. Since Wiggle was written under the assumption of single-threaded guest-side access, this change introduces a `shared` field to guest memories in order to flag when this assumption will not be the case. This change always sets `shared` to `false`; once a few more pieces are in place, `shared` will be set dynamically when a shared memory is detected, e.g., in a change like #5054. Using the `shared` field, we can now decide to load Wiggle values differently under the new assumptions. This change makes the guest `T::read` and `T::write` calls into `Relaxed` atomic loads and stores in order to maintain WebAssembly's expected memory consistency guarantees. We choose Rust's `Relaxed` here to match the `Unordered` memory consistency described in the [memory model] section of the ECMA spec. These relaxed accesses are done unconditionally, since we theorize that the performance benefit of an additional branch vs a relaxed load is not much. [memory model]: https://tc39.es/ecma262/multipage/memory-model.html#sec-memory-model Since 128-bit scalar types do not have `Atomic*` equivalents, we remove their `T::read` and `T::write` implementations here. They are unused by any WASI implementations in the project.
This commit is contained in:
@@ -6,10 +6,11 @@ use crate::{BorrowHandle, GuestError, GuestMemory, Region};
|
||||
pub struct WasmtimeGuestMemory<'a> {
|
||||
mem: &'a mut [u8],
|
||||
bc: BorrowChecker,
|
||||
shared: bool,
|
||||
}
|
||||
|
||||
impl<'a> WasmtimeGuestMemory<'a> {
|
||||
pub fn new(mem: &'a mut [u8]) -> Self {
|
||||
pub fn new(mem: &'a mut [u8], shared: bool) -> Self {
|
||||
Self {
|
||||
mem,
|
||||
// Wiggle does not expose any methods for functions to re-enter
|
||||
@@ -22,6 +23,7 @@ impl<'a> WasmtimeGuestMemory<'a> {
|
||||
// integrated fully with wasmtime:
|
||||
// https://github.com/bytecodealliance/wasmtime/issues/1917
|
||||
bc: BorrowChecker::new(),
|
||||
shared,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,4 +53,7 @@ unsafe impl GuestMemory for WasmtimeGuestMemory<'_> {
|
||||
fn mut_unborrow(&self, h: BorrowHandle) {
|
||||
self.bc.mut_unborrow(h)
|
||||
}
|
||||
fn is_shared_memory(&self) -> bool {
|
||||
self.shared
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user