[−][src]Trait wiggle::GuestMemory
A trait which abstracts how to get at the region of host memory taht contains guest memory.
All GuestPtr
types will contain a handle to this trait, signifying where
the pointer is actually pointing into. This type will need to be implemented
for the host's memory storage object.
Safety
Safety around this type is tricky, and the trait is unsafe
since there are
a few contracts you need to uphold to implement this type correctly and have
everything else in this crate work out safely.
The most important method of this trait is the base
method. This returns,
in host memory, a pointer and a length. The pointer should point to valid
memory for the guest to read/write for the length contiguous bytes
afterwards.
The region returned by base
must not only be valid, however, but it must
be valid for "a period of time before the guest is reentered". This isn't
exactly well defined but the general idea is that GuestMemory
is allowed
to change under our feet to accomodate instructions like memory.grow
or
other guest modifications. Memory, however, cannot be changed if the guest
is not reentered or if no explicitly action is taken to modify the guest
memory.
This provides the guarantee that host pointers based on the return value of
base
have a dynamic period for which they are valid. This time duration
must be "somehow nonzero in length" to allow users of GuestMemory
and
GuestPtr
to safely read and write interior data.
Using References
See the safety guarantees of BorrowChecker
, which asserts that exactly
one BorrowChecker
may be constructed for each WebAssembly memory.
The [GuestMemory::as_slice
] or GuestPtr::as_str
will return smart
pointers GuestSlice
and GuestStr
. These types, which implement
std::ops::Deref
and std::ops::DerefMut
, provide mutable references
into the memory region given by a GuestMemory
.
These smart pointers are dynamically borrow-checked by the BorrowChecker
given by GuestMemory::borrow_checker()
. While a GuestSlice
or a GuestStr
are live, the BorrowChecker::has_outstanding_borrows()
method will always return true
. If you need to re-enter the guest or
otherwise read or write to the contents of a WebAssembly memory, all
GuestSlice
s and GuestStr
s for the memory must be dropped, at which
point BorrowChecker::has_outstanding_borrows()
will return false
.
Required methods
fn base(&self) -> (*mut u8, u32)
Returns the base allocation of this guest memory, located in host memory.
A pointer/length pair are returned to signify where the guest memory lives in the host, and how many contiguous bytes the memory is valid for after the returned pointer.
Note that there are safety guarantees about this method that
implementations must uphold, and for more details see the
GuestMemory
documentation.
fn borrow_checker(&self) -> &BorrowChecker
Gives a reference to the BorrowChecker
used to keep track of each
outstanding borrow of the memory region. BorrowChecker::new
safety
rules require that exactly one checker exist for each memory region.
Provided methods
fn validate_size_align(
&self,
offset: u32,
align: usize,
len: u32
) -> Result<*mut u8, GuestError>
&self,
offset: u32,
align: usize,
len: u32
) -> Result<*mut u8, GuestError>
Validates a guest-relative pointer given various attributes, and returns the corresponding host pointer.
offset
- this is the guest-relative pointer, an offset from the base.align
- this is the desired alignment of the guest pointer, and if successful the host pointer will be guaranteed to have this alignment.len
- this is the number of bytes, afteroffset
, that the returned pointer must be valid for.
This function will guarantee that the returned pointer is in-bounds of
base
, at this time, for len
bytes and has alignment align
. If
any guarantees are not upheld then an error will be returned.
Note that the returned pointer is an unsafe pointer. This is not safe to
use in general because guest memory can be relocated. Additionally the
guest may be modifying/reading memory as well. Consult the
GuestMemory
documentation for safety information about using this
returned pointer.
fn ptr<'a, T: ?Sized>(&'a self, offset: T::Pointer) -> GuestPtr<'a, T> where
Self: Sized,
T: Pointee,
Self: Sized,
T: Pointee,
Convenience method for creating a GuestPtr
at a particular offset.
Note that T
can be almost any type, and typically offset
is a u32
.
The exception is slices and strings, in which case offset
is a (u32, u32)
of (offset, length)
.