wiggle_runtime/lib.rs
1use std::cell::Cell;
2use std::fmt;
3use std::marker;
4use std::rc::Rc;
5use std::slice;
6use std::str;
7use std::sync::Arc;
8
9#[cfg(feature = "wiggle_metadata")]
10pub use witx;
11
12mod borrow;
13mod error;
14mod guest_type;
15mod region;
16
17pub use borrow::GuestBorrows;
18pub use error::GuestError;
19pub use guest_type::{GuestErrorType, GuestType, GuestTypeTransparent};
20pub use region::Region;
21
22/// A trait which abstracts how to get at the region of host memory taht
23/// contains guest memory.
24///
25/// All `GuestPtr` types will contain a handle to this trait, signifying where
26/// the pointer is actually pointing into. This type will need to be implemented
27/// for the host's memory storage object.
28///
29/// # Safety
30///
31/// Safety around this type is tricky, and the trait is `unsafe` since there are
32/// a few contracts you need to uphold to implement this type correctly and have
33/// everything else in this crate work out safely.
34///
35/// The most important method of this trait is the `base` method. This returns,
36/// in host memory, a pointer and a length. The pointer should point to valid
37/// memory for the guest to read/write for the length contiguous bytes
38/// afterwards.
39///
40/// The region returned by `base` must not only be valid, however, but it must
41/// be valid for "a period of time before the guest is reentered". This isn't
42/// exactly well defined but the general idea is that `GuestMemory` is allowed
43/// to change under our feet to accomodate instructions like `memory.grow` or
44/// other guest modifications. Memory, however, cannot be changed if the guest
45/// is not reentered or if no explicitly action is taken to modify the guest
46/// memory.
47///
48/// This provides the guarantee that host pointers based on the return value of
49/// `base` have a dynamic period for which they are valid. This time duration
50/// must be "somehow nonzero in length" to allow users of `GuestMemory` and
51/// `GuestPtr` to safely read and write interior data.
52///
53/// # Using Raw Pointers
54///
55/// Methods like [`GuestMemory::base`] or [`GuestPtr::as_raw`] will return raw
56/// pointers to use. Returning raw pointers is significant because it shows
57/// there are hazards with using the returned pointers, and they can't blanket
58/// be used in a safe fashion. It is possible to use these pointers safely, but
59/// any usage needs to uphold a few guarantees.
60///
61/// * Whenever a `*mut T` is accessed or modified, it must be guaranteed that
62/// since the pointer was originally obtained the guest memory wasn't
63/// relocated in any way. This means you can't call back into the guest, call
64/// other arbitrary functions which might call into the guest, etc. The
65/// problem here is that the guest could execute instructions like
66/// `memory.grow` which would invalidate the raw pointer. If, however, after
67/// you acquire `*mut T` you only execute your own code and it doesn't touch
68/// the guest, then `*mut T` is still guaranteed to point to valid code.
69///
70/// * Furthermore, Rust's aliasing rules must still be upheld. For example you
71/// can't have two `&mut T` types that point to the area or overlap in any
72/// way. This in particular becomes an issue when you're dealing with multiple
73/// `GuestPtr` types. If you want to simultaneously work with them then you
74/// need to dynamically validate that you're either working with them all in a
75/// shared fashion (e.g. as if they were `&T`) or you must verify that they do
76/// not overlap to work with them as `&mut T`.
77///
78/// Note that safely using the raw pointers is relatively difficult. This crate
79/// strives to provide utilities to safely work with guest pointers so long as
80/// the previous guarantees are all upheld. If advanced operations are done with
81/// guest pointers it's recommended to be extremely cautious and thoroughly
82/// consider possible ramifications with respect to this API before codifying
83/// implementation details.
84pub unsafe trait GuestMemory {
85 /// Returns the base allocation of this guest memory, located in host
86 /// memory.
87 ///
88 /// A pointer/length pair are returned to signify where the guest memory
89 /// lives in the host, and how many contiguous bytes the memory is valid for
90 /// after the returned pointer.
91 ///
92 /// Note that there are safety guarantees about this method that
93 /// implementations must uphold, and for more details see the
94 /// [`GuestMemory`] documentation.
95 fn base(&self) -> (*mut u8, u32);
96
97 /// Validates a guest-relative pointer given various attributes, and returns
98 /// the corresponding host pointer.
99 ///
100 /// * `offset` - this is the guest-relative pointer, an offset from the
101 /// base.
102 /// * `align` - this is the desired alignment of the guest pointer, and if
103 /// successful the host pointer will be guaranteed to have this alignment.
104 /// * `len` - this is the number of bytes, after `offset`, that the returned
105 /// pointer must be valid for.
106 ///
107 /// This function will guarantee that the returned pointer is in-bounds of
108 /// `base`, *at this time*, for `len` bytes and has alignment `align`. If
109 /// any guarantees are not upheld then an error will be returned.
110 ///
111 /// Note that the returned pointer is an unsafe pointer. This is not safe to
112 /// use in general because guest memory can be relocated. Additionally the
113 /// guest may be modifying/reading memory as well. Consult the
114 /// [`GuestMemory`] documentation for safety information about using this
115 /// returned pointer.
116 fn validate_size_align(
117 &self,
118 offset: u32,
119 align: usize,
120 len: u32,
121 ) -> Result<*mut u8, GuestError> {
122 let (base_ptr, base_len) = self.base();
123 let region = Region { start: offset, len };
124
125 // Figure out our pointer to the start of memory
126 let start = match (base_ptr as usize).checked_add(offset as usize) {
127 Some(ptr) => ptr,
128 None => return Err(GuestError::PtrOverflow),
129 };
130 // and use that to figure out the end pointer
131 let end = match start.checked_add(len as usize) {
132 Some(ptr) => ptr,
133 None => return Err(GuestError::PtrOverflow),
134 };
135 // and then verify that our end doesn't reach past the end of our memory
136 if end > (base_ptr as usize) + (base_len as usize) {
137 return Err(GuestError::PtrOutOfBounds(region));
138 }
139 // and finally verify that the alignment is correct
140 if start % align != 0 {
141 return Err(GuestError::PtrNotAligned(region, align as u32));
142 }
143 Ok(start as *mut u8)
144 }
145
146 /// Convenience method for creating a `GuestPtr` at a particular offset.
147 ///
148 /// Note that `T` can be almost any type, and typically `offset` is a `u32`.
149 /// The exception is slices and strings, in which case `offset` is a `(u32,
150 /// u32)` of `(offset, length)`.
151 fn ptr<'a, T>(&'a self, offset: T::Pointer) -> GuestPtr<'a, T>
152 where
153 Self: Sized,
154 T: ?Sized + Pointee,
155 {
156 GuestPtr::new(self, offset)
157 }
158}
159
160// Forwarding trait implementations to the original type
161
162unsafe impl<'a, T: ?Sized + GuestMemory> GuestMemory for &'a T {
163 fn base(&self) -> (*mut u8, u32) {
164 T::base(self)
165 }
166}
167
168unsafe impl<'a, T: ?Sized + GuestMemory> GuestMemory for &'a mut T {
169 fn base(&self) -> (*mut u8, u32) {
170 T::base(self)
171 }
172}
173
174unsafe impl<T: ?Sized + GuestMemory> GuestMemory for Box<T> {
175 fn base(&self) -> (*mut u8, u32) {
176 T::base(self)
177 }
178}
179
180unsafe impl<T: ?Sized + GuestMemory> GuestMemory for Rc<T> {
181 fn base(&self) -> (*mut u8, u32) {
182 T::base(self)
183 }
184}
185
186unsafe impl<T: ?Sized + GuestMemory> GuestMemory for Arc<T> {
187 fn base(&self) -> (*mut u8, u32) {
188 T::base(self)
189 }
190}
191
192/// A *guest* pointer into host memory.
193///
194/// This type represents a pointer from the guest that points into host memory.
195/// Internally a `GuestPtr` contains a handle to its original [`GuestMemory`] as
196/// well as the offset into the memory that the pointer is pointing at.
197///
198/// Presence of a [`GuestPtr`] does not imply any form of validity. Pointers can
199/// be out-of-bounds, misaligned, etc. It is safe to construct a `GuestPtr` with
200/// any offset at any time. Consider a `GuestPtr<T>` roughly equivalent to `*mut
201/// T`, although there are a few more safety guarantees around this type.
202///
203/// ## Slices and Strings
204///
205/// Note that the type parameter does not need to implement the `Sized` trait,
206/// so you can implement types such as this:
207///
208/// * `GuestPtr<'_, str>` - a pointer to a guest string
209/// * `GuestPtr<'_, [T]>` - a pointer to a guest array
210///
211/// Unsized types such as this may have extra methods and won't have methods
212/// like [`GuestPtr::read`] or [`GuestPtr::write`].
213///
214/// ## Type parameter and pointee
215///
216/// The `T` type parameter is largely intended for more static safety in Rust as
217/// well as having a better handle on what we're pointing to. A `GuestPtr<T>`,
218/// however, does not necessarily literally imply a guest pointer pointing to
219/// type `T`. Instead the [`GuestType`] trait is a layer of abstraction where
220/// `GuestPtr<T>` may actually be a pointer to `U` in guest memory, but you can
221/// construct a `T` from a `U`.
222///
223/// For example `GuestPtr<GuestPtr<T>>` is a valid type, but this is actually
224/// more equivalent to `GuestPtr<u32>` because guest pointers are always
225/// 32-bits. That being said you can create a `GuestPtr<T>` from a `u32`.
226///
227/// Additionally `GuestPtr<MyEnum>` will actually delegate, typically, to and
228/// implementation which loads the underlying data as `GuestPtr<u8>` (or
229/// similar) and then the bytes loaded are validated to fit within the
230/// definition of `MyEnum` before `MyEnum` is returned.
231///
232/// For more information see the [`GuestPtr::read`] and [`GuestPtr::write`]
233/// methods. In general though be extremely careful about writing `unsafe` code
234/// when working with a `GuestPtr` if you're not using one of the
235/// already-attached helper methods.
236pub struct GuestPtr<'a, T: ?Sized + Pointee> {
237 mem: &'a (dyn GuestMemory + 'a),
238 pointer: T::Pointer,
239 _marker: marker::PhantomData<&'a Cell<T>>,
240}
241
242impl<'a, T: ?Sized + Pointee> GuestPtr<'a, T> {
243 /// Creates a new `GuestPtr` from the given `mem` and `pointer` values.
244 ///
245 /// Note that for sized types like `u32`, `GuestPtr<T>`, etc, the `pointer`
246 /// vlue is a `u32` offset into guest memory. For slices and strings,
247 /// `pointer` is a `(u32, u32)` offset/length pair.
248 pub fn new(mem: &'a (dyn GuestMemory + 'a), pointer: T::Pointer) -> GuestPtr<'_, T> {
249 GuestPtr {
250 mem,
251 pointer,
252 _marker: marker::PhantomData,
253 }
254 }
255
256 /// Returns the offset of this pointer in guest memory.
257 ///
258 /// Note that for sized types this returns a `u32`, but for slices and
259 /// strings it returns a `(u32, u32)` pointer/length pair.
260 pub fn offset(&self) -> T::Pointer {
261 self.pointer
262 }
263
264 /// Returns the guest memory that this pointer is coming from.
265 pub fn mem(&self) -> &'a (dyn GuestMemory + 'a) {
266 self.mem
267 }
268
269 /// Casts this `GuestPtr` type to a different type.
270 ///
271 /// This is a safe method which is useful for simply reinterpreting the type
272 /// parameter on this `GuestPtr`. Note that this is a safe method, where
273 /// again there's no guarantees about alignment, validity, in-bounds-ness,
274 /// etc of the returned pointer.
275 pub fn cast<U>(&self) -> GuestPtr<'a, U>
276 where
277 T: Pointee<Pointer = u32>,
278 {
279 GuestPtr::new(self.mem, self.pointer)
280 }
281
282 /// Safely read a value from this pointer.
283 ///
284 /// This is a fun method, and is one of the lynchpins of this
285 /// implementation. The highlight here is that this is a *safe* operation,
286 /// not an unsafe one like `*mut T`. This works for a few reasons:
287 ///
288 /// * The `unsafe` contract of the `GuestMemory` trait means that there's
289 /// always at least some backing memory for this `GuestPtr<T>`.
290 ///
291 /// * This does not use Rust-intrinsics to read the type `T`, but rather it
292 /// delegates to `T`'s implementation of [`GuestType`] to actually read
293 /// the underlying data. This again is a safe method, so any unsafety, if
294 /// any, must be internally documented.
295 ///
296 /// * Eventually what typically happens it that this bottoms out in the read
297 /// implementations for primitives types (like `i32`) which can safely be
298 /// read at any time, and then it's up to the runtime to determine what to
299 /// do with the bytes it read in a safe manner.
300 ///
301 /// Naturally lots of things can still go wrong, such as out-of-bounds
302 /// checks, alignment checks, validity checks (e.g. for enums), etc. All of
303 /// these check failures, however, are returned as a [`GuestError`] in the
304 /// `Result` here, and `Ok` is only returned if all the checks passed.
305 pub fn read(&self) -> Result<T, GuestError>
306 where
307 T: GuestType<'a>,
308 {
309 T::read(self)
310 }
311
312 /// Safely write a value to this pointer.
313 ///
314 /// This method, like [`GuestPtr::read`], is pretty crucial for the safe
315 /// operation of this crate. All the same reasons apply though for why this
316 /// method is safe, even eventually bottoming out in primitives like writing
317 /// an `i32` which is safe to write bit patterns into memory at any time due
318 /// to the guarantees of [`GuestMemory`].
319 ///
320 /// Like `read`, `write` can fail due to any manner of pointer checks, but
321 /// any failure is returned as a [`GuestError`].
322 pub fn write(&self, val: T) -> Result<(), GuestError>
323 where
324 T: GuestType<'a>,
325 {
326 T::write(self, val)
327 }
328
329 /// Performs pointer arithmetic on this pointer, moving the pointer forward
330 /// `amt` slots.
331 ///
332 /// This will either return the resulting pointer or `Err` if the pointer
333 /// arithmetic calculation would overflow around the end of the address
334 /// space.
335 pub fn add(&self, amt: u32) -> Result<GuestPtr<'a, T>, GuestError>
336 where
337 T: GuestType<'a> + Pointee<Pointer = u32>,
338 {
339 let offset = amt
340 .checked_mul(T::guest_size())
341 .and_then(|o| self.pointer.checked_add(o));
342 let offset = match offset {
343 Some(o) => o,
344 None => return Err(GuestError::PtrOverflow),
345 };
346 Ok(GuestPtr::new(self.mem, offset))
347 }
348
349 /// Returns a `GuestPtr` for an array of `T`s using this pointer as the
350 /// base.
351 pub fn as_array(&self, elems: u32) -> GuestPtr<'a, [T]>
352 where
353 T: GuestType<'a> + Pointee<Pointer = u32>,
354 {
355 GuestPtr::new(self.mem, (self.pointer, elems))
356 }
357}
358
359impl<'a, T> GuestPtr<'a, [T]> {
360 /// For slices, specifically returns the relative pointer to the base of the
361 /// array.
362 ///
363 /// This is similar to `<[T]>::as_ptr()`
364 pub fn offset_base(&self) -> u32 {
365 self.pointer.0
366 }
367
368 /// For slices, returns the length of the slice, in units.
369 pub fn len(&self) -> u32 {
370 self.pointer.1
371 }
372
373 /// Returns an iterator over interior pointers.
374 ///
375 /// Each item is a `Result` indicating whether it overflowed past the end of
376 /// the address space or not.
377 pub fn iter<'b>(
378 &'b self,
379 ) -> impl ExactSizeIterator<Item = Result<GuestPtr<'a, T>, GuestError>> + 'b
380 where
381 T: GuestType<'a>,
382 {
383 let base = self.as_ptr();
384 (0..self.len()).map(move |i| base.add(i))
385 }
386
387 /// Attempts to read a raw `*mut [T]` pointer from this pointer, performing
388 /// bounds checks and type validation.
389 /// The resulting `*mut [T]` can be used as a `&mut [t]` as long as the
390 /// reference is dropped before any Wasm code is re-entered.
391 ///
392 /// This function will return a raw pointer into host memory if all checks
393 /// succeed (valid utf-8, valid pointers, etc). If any checks fail then
394 /// `GuestError` will be returned.
395 ///
396 /// Note that the `*mut [T]` pointer is still unsafe to use in general, but
397 /// there are specific situations that it is safe to use. For more
398 /// information about using the raw pointer, consult the [`GuestMemory`]
399 /// trait documentation.
400 ///
401 /// For safety against overlapping mutable borrows, the user must use the
402 /// same `GuestBorrows` to create all *mut str or *mut [T] that are alive
403 /// at the same time.
404 pub fn as_raw(&self, bc: &mut GuestBorrows) -> Result<*mut [T], GuestError>
405 where
406 T: GuestTypeTransparent<'a>,
407 {
408 let len = match self.pointer.1.checked_mul(T::guest_size()) {
409 Some(l) => l,
410 None => return Err(GuestError::PtrOverflow),
411 };
412 let ptr =
413 self.mem
414 .validate_size_align(self.pointer.0, T::guest_align(), len)? as *mut T;
415
416 bc.borrow(Region {
417 start: self.pointer.0,
418 len,
419 })?;
420
421 // Validate all elements in slice.
422 // SAFETY: ptr has been validated by self.mem.validate_size_align
423 for offs in 0..self.pointer.1 {
424 T::validate(unsafe { ptr.add(offs as usize) })?;
425 }
426
427 // SAFETY: iff there are no overlapping borrows (all uses of as_raw use this same
428 // GuestBorrows), its valid to construct a *mut [T]
429 unsafe {
430 let s = slice::from_raw_parts_mut(ptr, self.pointer.1 as usize);
431 Ok(s as *mut [T])
432 }
433 }
434
435 /// Copies the data pointed to by `slice` into this guest region.
436 ///
437 /// This method is a *safe* method to copy data from the host to the guest.
438 /// This requires that `self` and `slice` have the same length. The pointee
439 /// type `T` requires the [`GuestTypeTransparent`] trait which is an
440 /// assertion that the representation on the host and on the guest is the
441 /// same.
442 ///
443 /// # Errors
444 ///
445 /// Returns an error if this guest pointer is out of bounds or if the length
446 /// of this guest pointer is not equal to the length of the slice provided.
447 pub fn copy_from_slice(&self, slice: &[T]) -> Result<(), GuestError>
448 where
449 T: GuestTypeTransparent<'a> + Copy,
450 {
451 // bounds check ...
452 let raw = self.as_raw(&mut GuestBorrows::new())?;
453 unsafe {
454 // ... length check ...
455 if (*raw).len() != slice.len() {
456 return Err(GuestError::SliceLengthsDiffer);
457 }
458 // ... and copy!
459 (*raw).copy_from_slice(slice);
460 Ok(())
461 }
462 }
463
464 /// Returns a `GuestPtr` pointing to the base of the array for the interior
465 /// type `T`.
466 pub fn as_ptr(&self) -> GuestPtr<'a, T> {
467 GuestPtr::new(self.mem, self.offset_base())
468 }
469}
470
471impl<'a> GuestPtr<'a, str> {
472 /// For strings, returns the relative pointer to the base of the string
473 /// allocation.
474 pub fn offset_base(&self) -> u32 {
475 self.pointer.0
476 }
477
478 /// Returns the length, in bytes, of th estring.
479 pub fn len(&self) -> u32 {
480 self.pointer.1
481 }
482
483 /// Returns a raw pointer for the underlying slice of bytes that this
484 /// pointer points to.
485 pub fn as_bytes(&self) -> GuestPtr<'a, [u8]> {
486 GuestPtr::new(self.mem, self.pointer)
487 }
488
489 /// Attempts to read a raw `*mut str` pointer from this pointer, performing
490 /// bounds checks and utf-8 checks.
491 /// The resulting `*mut str` can be used as a `&mut str` as long as the
492 /// reference is dropped before any Wasm code is re-entered.
493 ///
494 /// This function will return a raw pointer into host memory if all checks
495 /// succeed (valid utf-8, valid pointers, etc). If any checks fail then
496 /// `GuestError` will be returned.
497 ///
498 /// Note that the `*mut str` pointer is still unsafe to use in general, but
499 /// there are specific situations that it is safe to use. For more
500 /// information about using the raw pointer, consult the [`GuestMemory`]
501 /// trait documentation.
502 ///
503 /// For safety against overlapping mutable borrows, the user must use the
504 /// same `GuestBorrows` to create all *mut str or *mut [T] that are alive
505 /// at the same time.
506 pub fn as_raw(&self, bc: &mut GuestBorrows) -> Result<*mut str, GuestError> {
507 let ptr = self
508 .mem
509 .validate_size_align(self.pointer.0, 1, self.pointer.1)?;
510
511 bc.borrow(Region {
512 start: self.pointer.0,
513 len: self.pointer.1,
514 })?;
515
516 // SAFETY: iff there are no overlapping borrows (all uses of as_raw use this same
517 // GuestBorrows), its valid to construct a *mut str
518 unsafe {
519 let s = slice::from_raw_parts_mut(ptr, self.pointer.1 as usize);
520 match str::from_utf8_mut(s) {
521 Ok(s) => Ok(s),
522 Err(e) => Err(GuestError::InvalidUtf8(e)),
523 }
524 }
525 }
526}
527
528impl<T: ?Sized + Pointee> Clone for GuestPtr<'_, T> {
529 fn clone(&self) -> Self {
530 *self
531 }
532}
533
534impl<T: ?Sized + Pointee> Copy for GuestPtr<'_, T> {}
535
536impl<T: ?Sized + Pointee> fmt::Debug for GuestPtr<'_, T> {
537 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
538 T::debug(self.pointer, f)
539 }
540}
541
542mod private {
543 pub trait Sealed {}
544 impl<T> Sealed for T {}
545 impl<T> Sealed for [T] {}
546 impl Sealed for str {}
547}
548
549/// Types that can be pointed to by `GuestPtr<T>`.
550///
551/// In essence everything can, and the only special-case is unsized types like
552/// `str` and `[T]` which have special implementations.
553pub trait Pointee: private::Sealed {
554 #[doc(hidden)]
555 type Pointer: Copy;
556 #[doc(hidden)]
557 fn debug(pointer: Self::Pointer, f: &mut fmt::Formatter) -> fmt::Result;
558}
559
560impl<T> Pointee for T {
561 type Pointer = u32;
562 fn debug(pointer: Self::Pointer, f: &mut fmt::Formatter) -> fmt::Result {
563 write!(f, "*guest {:#x}", pointer)
564 }
565}
566
567impl<T> Pointee for [T] {
568 type Pointer = (u32, u32);
569 fn debug(pointer: Self::Pointer, f: &mut fmt::Formatter) -> fmt::Result {
570 write!(f, "*guest {:#x}/{}", pointer.0, pointer.1)
571 }
572}
573
574impl Pointee for str {
575 type Pointer = (u32, u32);
576 fn debug(pointer: Self::Pointer, f: &mut fmt::Formatter) -> fmt::Result {
577 <[u8]>::debug(pointer, f)
578 }
579}