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.
This commit is contained in:
Ulrich Weigand
2020-11-09 17:59:30 +01:00
committed by GitHub
parent 8dd091219a
commit a9d8abbf53

View File

@@ -88,7 +88,7 @@ macro_rules! primitives {
if ptr.mem().is_borrowed(region) { if ptr.mem().is_borrowed(region) {
return Err(GuestError::PtrBorrowed(region)); return Err(GuestError::PtrBorrowed(region));
} }
Ok(unsafe { *host_ptr.cast::<Self>() }) Ok(unsafe { <$i>::from_le_bytes(*host_ptr.cast::<[u8; mem::size_of::<Self>()]>()) })
} }
#[inline] #[inline]
@@ -108,7 +108,7 @@ macro_rules! primitives {
return Err(GuestError::PtrBorrowed(region)); return Err(GuestError::PtrBorrowed(region));
} }
unsafe { unsafe {
*host_ptr.cast::<Self>() = val; *host_ptr.cast::<[u8; mem::size_of::<Self>()]>() = <$i>::to_le_bytes(val);
} }
Ok(()) Ok(())
} }