Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WASI fd_readdir on big-endian #3016

Merged
merged 1 commit into from
Jun 22, 2021

Conversation

uweigand
Copy link
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

Subscribe to Label Action

cc @kubkon

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.

Copy link
Member

@alexcrichton alexcrichton left a comment

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
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
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
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.

None yet

2 participants