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

wasm2wat normalizes element segments when printing #1447

Open
alexcrichton opened this issue May 29, 2020 · 1 comment
Open

wasm2wat normalizes element segments when printing #1447

alexcrichton opened this issue May 29, 2020 · 1 comment

Comments

@alexcrichton
Copy link
Contributor

This is unfortunately a bit of a difficult issue to reproduce, but the gist of the issue is that wasm2wat will normalize an elem segment when printing, so the text format isn't necessarily faithful to how the binary format was written down.

The general idea is with an element segment that looks like (elem (;0;) funcref (ref.func 0)). This is normalized to (elem (;0;) func 0) when printing. An easy test case for this isn't available because wat2wasm will also do this normalization on encoding.

This issue cropped up with one of the upstream spec tests, looking like this:

(module binary
  "\00asm" "\01\00\00\00"

  "\01\04\01\60\00\00"       ;; Type section

  "\03\02\01\00"             ;; Function section

  "\04\04\01"                ;; Table section with 1 entry
  "\70\00\00"                ;; no max, minimum 0, funcref

  "\05\03\01\00\00"          ;; Memory section

  "\09\07\01"                ;; Element section with one segment
  "\05\70"                   ;; Passive, funcref
  "\01"                      ;; 1 element
  "\d2\00\0b"                ;; ref.func, index 0, end

  "\0a\04\01"                ;; Code section

  ;; function 0
  "\02\00"
  "\0b")                     ;; end

The text parser that I'm working with doesn't attempt to interpret the bytes and serializes them out, but wat2wasm in wabt will parse these bytes and, even with this module, encode a binary module that uses the (elem (;0;) func 0) encoding. A binary version of this module that reproduces the issue is here:
foo.wasm.gz

I'm not really sure whether this is an issue per-se. It's a harmless difference but it does seem a bit odd that when printing the segments are normalized. I think it makes sense when encoding to be MVP-friendly where possible, though! If y'all think this isn't a major issue feel free to close!

@binji
Copy link
Member

binji commented May 29, 2020

This is a good point. Currently the wabt IR doesn't distinguish between element expressions and element indexes internally, so it can't know what the original encoding was. But you're right, it probably should when reading a binary file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants