Skip to content

Fix WASI fd_readdir on big-endian - #3016

Merged
alexcrichton merged 1 commit into
bytecodealliance:mainfrom
uweigand:wasm-readdir-be
Jun 22, 2021
Merged

alexcrichton merged 1 commit into
bytecodealliance:mainfrom
uweigand:wasm-readdir-be

Conversation

@uweigand

Copy link
Copy Markdown
Member

This code assumes that the Dirent structure has the same memory
layout on the host (Rust code) as in wasm code. This is not true
if the host is big-endian, as wasm is always little-endian.

Fixed by always byte-swapping Dirent fields to little-endian
before passing them on to wasm code.

@uweigand uweigand mentioned this pull request Jun 22, 2021
@github-actions github-actions Bot added the wasi Issues pertaining to WASI label Jun 22, 2021
@github-actions

Copy link
Copy Markdown

Subscribe to Label Action

cc @kubkon

Details This issue or pull request has been labeled: "wasi"

Thus the following users have been cc'd because of the following labels:

  • kubkon: wasi

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@alexcrichton alexcrichton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely a preexisting bug in wiggle that it never tried to handle endidanness, and this is something that we'll ideally be fixing at the bindings generation layer since in general host-code shouldn't have to care about endianness. I suspect this isn't the last endian bug in the WASI bindings, but I'm hoping that we can survive long enough to get to the point where we can properly fix this.

FWIW the semi-replacement of wiggle (more like the next stage in evolution) at https://github.com/bytecodealliance/witx-bindgen/ should fix issues like this.

let guest_dirent = types::Dirent {
d_ino: dirent.d_ino.to_le(),
d_namlen: dirent.d_namlen.to_le(),
d_type: dirent.d_type, // endian-invariant

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this assert that the size of this field is 1 so the endianness is known to not matter?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this? Patch updated.

This code assumes that the Dirent structure has the same memory
layout on the host (Rust code) as in wasm code.  This is not true
if the host is big-endian, as wasm is always little-endian.

Fixed by always byte-swapping Dirent fields to little-endian
before passing them on to wasm code.
@alexcrichton

Copy link
Copy Markdown
Member

👍

@alexcrichton
alexcrichton merged commit 1a865fb into bytecodealliance:main Jun 22, 2021
@uweigand
uweigand deleted the wasm-readdir-be branch June 22, 2021 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasi Issues pertaining to WASI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants