Cranelift riscv64: i8x16.swizzle / i8x16.shuffle assume VLMAX = 16, wrong lanes on VLEN >= 256 hardware (SpacemiT K3)

オープン
#15,509 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust

調査の方向性

Start by locating the Cranelift riscv64 lowering for i8x16.swizzle and i8x16.shuffle, then read the emitted vrgather.vv sequences described in the report. Reproduce with the supplied WAT on VLEN=256 hardware or QEMU using rvv_ta_all_1s=true. Done means both operations produce the expected bytes regardless of VLEN and tail contents.

索引モデルが issue の本文から書いたものです。

説明

cranelift cranelift:area:riscv64

Versions: wasmtime 51.0.0 (32656dc48) and 52.0.0-dev (b738a8928, 2026-11-25), release binaries riscv64gc-linux. Cranelift infers has_v from the host. It infers no has_zvl* flag.

Both ops are lowered to vrgather.vv under vsetivli zero,16,e8,m1,ta,ma:

swizzle:  vrgather.vv v10, v8, v9
shuffle:  vrgather.vv v10, v9, v11        # first operand, original indices
          vadd.vi     v11, v11, -16
          vrgather.vv v9, v8, v11         # second operand, indices - 16
          vor.vv      v8, v10, v9

vrgather.vv writes 0 only for indices >= VLMAX, not >= vl. At VLEN=128, VLMAX(e8,m1) = 16, so wasm's "index >= 16 gives 0" holds and the two-gather shuffle works. At VLEN=256, VLMAX = 32. Indices 16..31 then read elements 16..31 of the source register. Those elements are its tail under vl=16, and RVV 1.0 (3.4.3, tail agnostic) lets them hold anything, including all ones. At VLEN >= 2048 the idx - 16 wrap-around in the shuffle (240..255) also falls below VLMAX.

Reproducer (wasmtime run repro.wat, prints 2 lines):

;; i8x16.swizzle / i8x16.shuffle on riscv64 Cranelift: indices 16..31 must give 0
;; (swizzle) or pick from the second operand (shuffle). Cranelift lowers both to
;; vrgather.vv with vl=16, which returns 0 only for indices >= VLMAX. At VLEN=128,
;; VLMAX(e8,m1) = 16 and the result is right; at VLEN >= 256 the indices 16..VLMAX-1
;; read the tail of the source register instead.
;;
;; Expected output (x86-64 wasmtime, riscv64 at VLEN=128):
;;   00000200040006000809000b0c0d0e0f
;;   011103130515071709190b1b0d1d0f1f
;; riscv64 wasmtime 51.0.0 and 52.0.0-dev under qemu -cpu rv64,v=true,vlen=256,rvv_ta_all_1s=true:
;;   00ff02ff04ff06000809ff0b0c0d0e0f
;;   01ff03ff05ff07ff09ff0bff0dff0fff
(module
  (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
  (memory (export "memory") 1)
  (data (i32.const 0) "\00\01\02\03\04\05\06\07\08\09\0a\0b\0c\0d\0e\0f\10\11\12\13\14\15\16\17\18\19\1a\1b\1c\1d\1e\1f")
  (data (i32.const 1024) "0123456789abcdef")
  (func $hex (param $src i32) (param $dst i32)
    (local $i i32) (local $b i32)
    (block $done (loop $l
      (br_if $done (i32.ge_u (local.get $i) (i32.const 16)))
      (local.set $b (i32.load8_u (i32.add (local.get $src) (local.get $i))))
      (i32.store8 (i32.add (local.get $dst) (i32.shl (local.get $i) (i32.const 1)))
        (i32.load8_u offset=1024 (i32.shr_u (local.get $b) (i32.const 4))))
      (i32.store8 offset=1 (i32.add (local.get $dst) (i32.shl (local.get $i) (i32.const 1)))
        (i32.load8_u offset=1024 (i32.and (local.get $b) (i32.const 15))))
      (local.set $i (i32.add (local.get $i) (i32.const 1)))
      (br $l)))
    (i32.store8 offset=32 (local.get $dst) (i32.const 10)))
  (func (export "_start")
    ;; swizzle: indices 17, 31, 16, 20 are out of range -> 0; 255 too
    (v128.store (i32.const 256)
      (i8x16.swizzle (v128.load (i32.const 0)) (v128.const i8x16 0 17 2 31 4 16 6 255 8 9 20 11 12 13 14 15)))
    ;; shuffle: odd lanes come from the second operand (indices 16..31)
    (v128.store (i32.const 272)
      (i8x16.shuffle 1 17 3 19 5 21 7 23 9 25 11 27 13 29 15 31 (v128.load (i32.const 0)) (v128.load (i32.const 16))))
    (call $hex (i32.const 256) (i32.const 2048))
    (call $hex (i32.const 272) (i32.const 2081))
    (i32.store (i32.const 3000) (i32.const 2048))
    (i32.store (i32.const 3004) (i32.const 66))
    (drop (call $fd_write (i32.const 1) (i32.const 3000) (i32.const 1) (i32.const 3008))))
)
platform (qemu -cpu rv64,v=true,vlen=N,...) swizzle line shuffle line
x86-64 wasmtime, riscv64 VLEN=128 (all knobs), riscv64 VLEN=256/512/1024 default 00000200040006000809000b0c0d0e0f 011103130515071709190b1b0d1d0f1f
riscv64 VLEN=256/512/1024 with rvv_ta_all_1s=true 00ff02ff04ff06000809ff0b0c0d0e0f 01ff03ff05ff07ff09ff0bff0dff0fff

QEMU's default tail-agnostic behaviour keeps old values, and fresh registers are zero, which is why the default VLEN=256 run happens to print the right answer. The same holds on a SpacemiT K1 board (VLEN=256, real hardware): there both versions print the right bytes. So the wrong result needs a core whose tail-agnostic writes leave other values, or wider vector code earlier in the same thread. The lowering depends on tail contents either way. Random programs (wasmgen) hit this in 5 of 6 programs that use either op. With both ops excluded, 300 further programs x 22 platforms showed no other divergence.

The wrong lanes also mean wasm code can observe bytes of a vector register outside the v128 it operates on (old contents, on cores that keep tails).

Found with rvvprobe, our RVV test tool (wasm_fuzz / wasm_diff): random wasmgen programs, x86-64 wasmtime as the reference, cross-checked with Pulley, Winch and x86-64 V8, run under QEMU 11.1.3 at VLEN 128/256/512/1024 with the spec's implementation choices, and on SpacemiT K1 and K3 boards.

Possible fixes: mask indices >= 16 explicitly (vmsgtu.vi v0, idx, 15, then a masked gather into a zeroed destination or vmerge), or gather under vl = VLMAX after zeroing the source tail. Either way, wasm semantics must not depend on VLEN or on tail contents. Wasmtime's own riscv64 CI presumably runs QEMU at VLEN=128 with the default tail policy, where this cannot show.

Real hardware. On a SpacemiT K3 board (X100 cores, VLEN=256, Bianbu 4.0.5, kernel 6.18.3), both wasmtime 51.0.0 and 52.0.0-dev print the wrong bytes with no QEMU involved:

00ff02ff04ff06000809ff0b0c0d0e0f
01ff03ff05ff07ff09ff0bff0dff0fff

The K1 board (X60) still prints the correct bytes. So the result depends on how the core implements tail-agnostic writes, as described above.


Reported by 杜浩 (Hao Du, @snozzz, [email protected]) and 刘通 (Tong Liu, @I3eg1nner, [email protected])

主要言語
Rust
スター
18.9k
フォーク
2k
平均マージ
1日 6時間
マージ済み PR(30日)
238

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

bytecodealliance/wasmtime のほかの issue

bytecodealliance/wasmtime の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。