Skip to content

Linear memory, but the host sees it backwards #1478

Closed
@SoniEx2

Description

@SoniEx2

This might be the wrong place for this, but:

Usually a wasm memory read looks like this:

fn read_u32(memory: &[u8], position: u32) -> Option<u32> {
  let b0 = *memory.get(position)?;
  let b1 = *memory.get(position + 1)?;
  let b2 = *memory.get(position + 2)?;
  let b3 = *memory.get(position + 3)?;
  // optimized out on little-endian
  Some(u32::from(b3) << 24 | u32::from(b2) << 16 | u32::from(b1) << 8 | u32::from(b0))
}

But why not do it like so instead:

fn read_u32(memory: &[u8], position: u32) -> Option<u32> {
  let b0 = *memory.get(memory.len() - position - 1)?;
  let b1 = *memory.get(memory.len() - position - 2)?;
  let b2 = *memory.get(memory.len() - position - 3)?;
  let b3 = *memory.get(memory.len() - position - 4)?;
  // optimized out on big-endian
  Some(u32::from(b3) << 24 | u32::from(b2) << 16 | u32::from(b1) << 8 | u32::from(b0))
}

It is fairly easy to prove that, as far as a wasm module is concerned, these are equivalent - a wasm module cannot observe any difference between the two of them.

But the host knows the difference.

While wasm modules don't care, the wasm-c-api currently does not support the latter form of linear memory representation. Would it be acceptable to propose that some wasm-c-api engines may use the latter form on big-endian platforms?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions