From a9d8abbf5357ee0f176af3e1985a6425fb3ffda4 Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Mon, 9 Nov 2020 17:59:30 +0100 Subject: [PATCH] Support big-endian hosts with GuestType (#2383) The GuestType trait is used to access data elements in guest memory. According to the WebAssembly spec, those are always stored in little-endian byte order, even on big-endian hosts. Accessing such elements on big-endian hosts therefore requires byte swapping. Fixed by adding from_le_bytes / to_le_bytes. --- crates/wiggle/src/guest_type.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/wiggle/src/guest_type.rs b/crates/wiggle/src/guest_type.rs index 7eabd0f025..422c3f2394 100644 --- a/crates/wiggle/src/guest_type.rs +++ b/crates/wiggle/src/guest_type.rs @@ -88,7 +88,7 @@ macro_rules! primitives { if ptr.mem().is_borrowed(region) { return Err(GuestError::PtrBorrowed(region)); } - Ok(unsafe { *host_ptr.cast::() }) + Ok(unsafe { <$i>::from_le_bytes(*host_ptr.cast::<[u8; mem::size_of::()]>()) }) } #[inline] @@ -108,7 +108,7 @@ macro_rules! primitives { return Err(GuestError::PtrBorrowed(region)); } unsafe { - *host_ptr.cast::() = val; + *host_ptr.cast::<[u8; mem::size_of::()]>() = <$i>::to_le_bytes(val); } Ok(()) }