Skip to content

Commit

Permalink
Fix access to VMMemoryDefinition::current_length on big-endian
Browse files Browse the repository at this point in the history
The current_length member is defined as "usize" in Rust code,
but generated wasm code refers to it as if it were "u32".  Not
sure why this is case, and it happens to work on little-endian
machines (as long as the length is < 4GB), but it will always
fail on big-endian machines.

Add the minimal fixes to make it work on big-endian as well.
(Note: this patch does not attempt to fix the actual underlying
type mismatch ...)
  • Loading branch information
uweigand committed Jun 22, 2021
1 parent 8760bcc commit ffb7f9e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion crates/environ/src/vmoffsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,12 @@ impl VMOffsets {
#[allow(clippy::identity_op)]
#[inline]
pub fn vmmemory_definition_current_length(&self) -> u8 {
1 * self.pointer_size
/* TODO: type mismatch - usize vs. u32 */
if cfg!(target_endian = "little") {
1 * self.pointer_size
} else {
2 * self.pointer_size - 4
}
}

/// The size of the `current_length` field.
Expand Down
2 changes: 1 addition & 1 deletion crates/runtime/src/vmcontext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ mod test_vmmemory_definition {
offset_of!(VMMemoryDefinition, base),
usize::from(offsets.vmmemory_definition_base())
);
/* TODO: Assert that `current_length` matches.
assert_eq!(
offset_of!(VMMemoryDefinition, current_length),
usize::from(offsets.vmmemory_definition_current_length())
);
/* TODO: Assert that the size of `current_length` matches.
assert_eq!(
size_of::<VMMemoryDefinition::current_length>(),
usize::from(offsets.size_of_vmmemory_definition_current_length())
Expand Down

0 comments on commit ffb7f9e

Please sign in to comment.