Skip to content

Wrong encoding WAT format with the multiple memories proposal #1958

Closed

Description

@q82419

Hi,

I found that when I encode the following WAT into WASM by the wat2wasm binary tool in both 1.0.29 or master:
(The WAT is in the test case of the multiple memories proposal)

(module
  (memory 1)
  (memory $m 1)

  (func
    (local $v v128)

    (drop (v128.load8_lane 1 (i32.const 0) (local.get $v)))
    (drop (v128.load8_lane 1 offset=0 1 (i32.const 0) (local.get $v)))
    (drop (v128.load8_lane 1 offset=0 align=1 1 (i32.const 0) (local.get $v)))
    (drop (v128.load8_lane 1 align=1 1 (i32.const 0) (local.get $v)))

    (drop (v128.load8_lane $m 1 (i32.const 0) (local.get $v)))
    (drop (v128.load8_lane $m offset=0 1 (i32.const 0) (local.get $v)))
    (drop (v128.load8_lane $m offset=0 align=1 1 (i32.const 0) (local.get $v)))
    (drop (v128.load8_lane $m align=1 1 (i32.const 0) (local.get $v)))

    (drop (v128.load8_lane 1 1 (i32.const 0) (local.get $v)))
    (drop (v128.load8_lane 1 offset=0 1 (i32.const 0) (local.get $v)))
    (drop (v128.load8_lane 1 offset=0 align=1 1 (i32.const 0) (local.get $v)))
    (drop (v128.load8_lane 1 align=1 1 (i32.const 0) (local.get $v)))

    (v128.store8_lane 1 (i32.const 0) (local.get $v))
    (v128.store8_lane offset=0 1 (i32.const 0) (local.get $v))
    (v128.store8_lane offset=0 align=1 1 (i32.const 0) (local.get $v))
    (v128.store8_lane align=1 1 (i32.const 0) (local.get $v))

    (v128.store8_lane $m 1 (i32.const 0) (local.get $v))
    (v128.store8_lane $m offset=0 1 (i32.const 0) (local.get $v))
    (v128.store8_lane $m offset=0 align=1 1 (i32.const 0) (local.get $v))
    (v128.store8_lane $m align=1 1 (i32.const 0) (local.get $v))

    (v128.store8_lane 1 1 (i32.const 0) (local.get $v))
    (v128.store8_lane 1 offset=0 1 (i32.const 0) (local.get $v))
    (v128.store8_lane 1 offset=0 align=1 1 (i32.const 0) (local.get $v))
    (v128.store8_lane 1 align=1 1 (i32.const 0) (local.get $v))
  )
)

Here, I take the first line of function body (drop (v128.load8_lane 1 (i32.const 0) (local.get $v))) as Line 1, and so on.

The command is:

$ ./wat2wasm --output=test.wasm --enable-multi-memory test.wat

The output test.wasm in binary is as following:
(The tar file here: test.tar.gz)

;; WASM magic and version
00 61 73 6D 01 00 00 00
;; Type section
01 04 01 60 00 00
;; Function section
03 02 01 00
;; Memory section
05 05 02 00 01 00 01
;; Code section
0A EB 01
;; Vector of code segment
01
;; Code segment: size
E8 01
;; Code segment: local (local $v v128)
01 01 7B
;; Expression start
;; Line 1: (drop (v128.load8_lane 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 2: (drop (v128.load8_lane 1 offset=0 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 3: (drop (v128.load8_lane 1 offset=0 align=1 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 4: (drop (v128.load8_lane 1 align=1 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 5: (drop (v128.load8_lane $m 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 6: (drop (v128.load8_lane $m offset=0 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 7: (drop (v128.load8_lane $m offset=0 align=1 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 8: (drop (v128.load8_lane $m align=1 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 9: (drop (v128.load8_lane 1 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 10: (drop (v128.load8_lane 1 offset=0 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 11: (drop (v128.load8_lane 1 offset=0 align=1 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 12: (drop (v128.load8_lane 1 align=1 1 (i32.const 0) (local.get $v)))
41 00 20 00 FD 54 00 00 01 1A
;; Line 13: (v128.store8_lane 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Line 14: (v128.store8_lane offset=0 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Line 15: (v128.store8_lane offset=0 align=1 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Line 16: (v128.store8_lane align=1 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Line 17: (v128.store8_lane $m 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Line 18: (v128.store8_lane $m offset=0 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Line 19: (v128.store8_lane $m offset=0 align=1 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Line 20: (v128.store8_lane $m align=1 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Line 21: (v128.store8_lane 1 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Line 22: (v128.store8_lane 1 offset=0 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Line 23: (v128.store8_lane 1 offset=0 align=1 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Line 24: (v128.store8_lane 1 align=1 1 (i32.const 0) (local.get $v))
41 00 20 00 FD 58 00 00 01
;; Expression end
0B

As above, the instructions of Line 1 to Line 12 are not totally the same, but the encode result binaries are the same.
The same situation occurs at Line 13 to Line 24.

Take the Line 7: (drop (v128.load8_lane $m offset=0 align=1 1 (i32.const 0) (local.get $v))) as example.
The wat2wasm encoding is:

;; local.get $v
41 00
;; i32.const 0
20 00
;; v128.load8_lane $m offset=0 align=1 1
FD 54 00 00 01
;; drop
1A

We can concentrate on the v128.load8_lane instruction encoding: FD 54 00 00 01.

With the binary format in multiple memories proposal, the memory $m will be index 1, so the memarg immdiate of the instruction should not be 00 00.

I think the encoding should be:

;; Instruction v128.load8_lane
FD 54
;; memarg: align (bit 6 of the flags field containing alignment is set)
40
;; memarg: offset
00
;; memarg: memory index
01
;; lane index
01

And so on with the other instructions which contains the memory index > 0 with the multiple memory proposal.

Can you help to check if this is en error?

Thanks.

Activity

  1. q82419 commented on Aug 14, 2022

    @q82419
    Author

    By the way, the multiple memory proposal has a breaking change.
    Please refer to WebAssembly/multi-memory#28 and WebAssembly/multi-memory#29.
    But the binary format of the modified spec is not changed.
    I've created the PR WebAssembly/multi-memory#33 .

  2. keithw commented on Aug 15, 2022

    @keithw
    Member

    Thank you for catching this -- wabt only supported parsing multi-memory SIMD load/stores but not writing them. It would be nice if this were tested by the multi-memory testsuite. (Somebody should really write a proper test of the execution of these instructions...)

    Could you please take a look at the proposed fix in #1962 and see if you think it does the trick?

    For the breaking change to the multi-memory proposal, I don't think it will be a problem to swap the order of the immediates, but there really should be an update to the testsuite that verifies that implementations are correctly parsing the new binary format. Otherwise it seems quite likely that some implementations are going to mix this up.

  3. self-assigned this
    on Aug 15, 2022
  4. q82419 commented on Aug 15, 2022

    @q82419
    Author

    I've tested the WAT above and got the expected result. Thanks a lot.

    For the multi-memory proposal change, wasm-tools did: bytecodealliance/wasm-tools#650
    And I found that the test suite only supply the WAT format. The binary format is not covered. This change only affects the binary format :(

  5. keithw commented on Aug 15, 2022

    @keithw
    Member

    Glad to hear it. If you're up for it, would appreciate a second look after the most recent commit on that branch. There's a bunch of other instructions (other SIMD ops and atomics) that probably need to have memidx parsed whenever the memarg flag is set. I also adjusted the order of the immediates in the binary format to match the new multi-memory proposal.

    I'm guessing that unless somebody contributes a bunch of tests for the binary format to the upstream proposal repo (including for the more obscure operations, e.g. load splat, load zero, atomics), there are going to be a lot of untested and perhaps non-interoperable implementations of this. :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions