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

Spec interpreter invalid binary format float const reading #543

Closed
kripken opened this issue Aug 4, 2017 · 3 comments
Closed

Spec interpreter invalid binary format float const reading #543

kripken opened this issue Aug 4, 2017 · 3 comments

Comments

@kripken
Copy link
Member

kripken commented Aug 4, 2017

Consider

(module
 (export "calc" (func $calc))
 (func $calc (result f64)
  (f64.convert_u/i32
   (i32.const 4294967249)
  )
 )
)

and

(module
 (export "calc" (func $calc))
 (func $calc (result f64)
  (f64.const 4294967249)
 )
)

The only difference is that in the first we have the const as an int, then convert to a float, and in the second the conversion has been done already. But running these files leads to different results:

$ wasm a.wasm -e '(invoke "calc")' && wasm b.wasm -e '(invoke "calc")'
4294967249. : f64
4294965201. : f64

This is the same no matter who translates the text to binary (binaryen, wabt, the spec interpreter itself), so it looks like the bug is in the reading of the float const in the spec interpreter.

@sunfishcode
Copy link
Member

This is likely due to #421.

@binji
Copy link
Member

binji commented Aug 4, 2017

Hm, interesting, doesn't occur when parsing via text, only reading binary.

$ cat test.wast
(module
 (func (export "calc1") (result f64)
  (f64.convert_u/i32 (i32.const 4294967249))))
(invoke "calc1")

(module
 (func (export "calc2") (result f64)
  (f64.const 4294967249)))
(invoke "calc2")

$ ./wasm test.wast
4294967249. : f64
4294967249. : f64

$ ./wast2wasm --spec test.wast -o foo.json
$ ./wasm foo.1.wasm -e '(invoke "calc2")'
4294965201. : f64

@binji
Copy link
Member

binji commented Aug 9, 2017

Looks like it was a bug when decoding f64 values -- it stores the representation as an OCaml int64, but reads it in two int32 pieces. The conversion from int32 -> int64 was sign-extending, which ended up flipping a bit in the result.

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

3 participants