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

Wasmi returns unexpected NaN bit pattern #569

Open
erxiaozhou opened this issue Nov 19, 2022 · 4 comments
Open

Wasmi returns unexpected NaN bit pattern #569

erxiaozhou opened this issue Nov 19, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@erxiaozhou
Copy link

Description

Current State

0x0 0x0 0x0 0x2f 0x0 0x0 0xf0 0xff

Expected

........ 0xf8 0xff (The most significant bit of payload is 1)

Environment

  • Code Version: git commit id fdc136f
  • Hardware Architecture: x86_64
  • Operating system: Ubuntu 20.04

Steps to Reproduce

wasmi.zip

  1. build the wasmi
  2. run the test case by wasmi <test_case_name> to_test
@Robbepop
Copy link
Member

wasmtime run wasmi_nan.wasm --invoke to_test prints NaN and wasmi_cli wasmi_nan.wasm to_test prints NaN as well. What else do you expect exactly? There is no differentiation between +NaN and -NaN in case you mean that.

@erxiaozhou
Copy link
Author

wasmtime run wasmi_nan.wasm --invoke to_test prints NaN and wasmi_cli wasmi_nan.wasm to_test prints NaN as well. What else do you expect exactly? There is no differentiation between +NaN and -NaN in case you mean that.

Thank you for your reply. The output is NaN is as expeced. However, according to the specification https://webassembly.github.io/spec/core/exec/numerics.html#floating-point-operations, in this example, for the value on the stack, the most significant bit of payload is supposed to be 1. It means the highest 13 bits are 0111 1111 1111 1 or 1111 1111 1111 1 . In this example. The highest 13 bit of the value on the stack is 1111 1111 1111 1.

image

Thank you!

@Robbepop Robbepop changed the title Un expected value on the stack Unexpected NaN value on the stack (wrong NaN sign) Nov 20, 2022
@Robbepop Robbepop changed the title Unexpected NaN value on the stack (wrong NaN sign) Unexpected NaN sign value on the stack Nov 20, 2022
@Robbepop
Copy link
Member

With respect to Wasm spec testsuite compliance for NaN values in wasmi I found that wasmi's current spec testsuite is not 100% correct on the semantics of NaN value checking compared to what Wasmtime does here: https://github.com/bytecodealliance/wasmtime/blob/main/crates/wast/src/core.rs#L106

In contrast, wasmi simply evaluates .is_nan(). I am sure we can find and fix those NaN sign bit errors by improving the Wasm spec testsuite in this regard.

@Robbepop Robbepop added the bug Something isn't working label Feb 13, 2023
@Robbepop
Copy link
Member

Robbepop commented Oct 6, 2024

@erxiaozhou I just re-ran the above test with Wasmi v0.38.0 and got the following result:

Original output of old Wasmi version from bug report: 00 00 00 2F 00 00 F0 FF
Expected output: 00 00 00 2F 00 00 F8 FF

% wasmi_cli 04-original.wasm
nan:0x7FF8000000000039

I guess the bits have to be swapped: 39 00 00 00 00 00 F8 7F
So while the end now seems to be correct we have the 39 byte at the start and are still missing the 2F byte in the middle ...

Wasmtime funnily just returns a NaN without further description:

% wasmtime 04-original.wasm
NaN

@Robbepop Robbepop changed the title Unexpected NaN sign value on the stack Wasmi returns unexpected NaN bit pattern Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants