Skip to content

Commit

Permalink
Update preview1 to trap on misaligned pointers (#6776)
Browse files Browse the repository at this point in the history
* Update preview1 to trap on misaligned pointers

Previously Wasmtime would return `EINVAL` to a guest but the upstream
documentation indicates that misaligned pointers should trap, so this
commit updates the alignment error to get converted into a trap rather
than than being returned to the guest.

* Change OOB errors to traps too

* Update preview2's implementation of preview1

* Handle some more errors
  • Loading branch information
alexcrichton committed Aug 2, 2023
1 parent 5200159 commit 615d697
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 103 deletions.
16 changes: 13 additions & 3 deletions crates/wasi-common/src/snapshots/preview_1/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,19 @@ impl From<wiggle::GuestError> for Error {
match err {
InvalidFlagValue { .. } => Errno::Inval.into(),
InvalidEnumValue { .. } => Errno::Inval.into(),
PtrOverflow { .. } => Errno::Fault.into(),
PtrOutOfBounds { .. } => Errno::Fault.into(),
PtrNotAligned { .. } => Errno::Inval.into(),
// As per
// https://github.com/WebAssembly/wasi/blob/main/legacy/tools/witx-docs.md#pointers
//
// > If a misaligned pointer is passed to a function, the function
// > shall trap.
// >
// > If an out-of-bounds pointer is passed to a function and the
// > function needs to dereference it, the function shall trap.
//
// so this turns OOB and misalignment errors into traps.
PtrOverflow { .. } | PtrOutOfBounds { .. } | PtrNotAligned { .. } => {
Error::trap(err.into())
}
PtrBorrowed { .. } => Errno::Fault.into(),
InvalidUtf8 { .. } => Errno::Ilseq.into(),
TryFromIntError { .. } => Errno::Overflow.into(),
Expand Down
Loading

0 comments on commit 615d697

Please sign in to comment.