#7031·wasmer

[BUG]: singlepass: narrow atomic RMW with an immediate operand wider than the access returns a wrong value (i32) or fails to compile (i64)

Author: khagankhanCreated Sep 25, 2026Updated Sep 29, 2026

Describe the bug

In the singlepass backend, the sub-word atomic read-modify-write instructions (i32.atomic.rmw8.*_u, i32.atomic.rmw16.*_u and their i64 counterparts) mishandle an operand that is a constant whose value does not fit the access width:

  • the i32 forms return a wrong "old value": the bits of the immediate above the access width leak into the result. With the memory at the address equal to 0, i32.atomic.rmw8.add_u with operand 300 returns 256 instead of 0, i32.atomic.rmw8.xchg_u with 0x133 returns 256, and i32.atomic.rmw16.add_u with 70000 returns 65536. The value stored to memory is correct.
  • the i64 forms fail to compile: Codegen("singlepass can't emit MOVZX S8 Imm64(300) S64 GPR(RAX)").

The same instructions with the operand in a local, or with an immediate that fits the access width, are correct. The cranelift and LLVM backends are correct in every case, as are wasmtime and WasmEdge on the same modules.

bash
wasmer 7.4.2
binary: wasmer-cli
commit-hash: ca719eb81fa450a06bcd6f0dd46e0b7cca15d7a9
commit-date: 2026-09-25
host: x86_64-unknown-linux-gnu
CPU flags: sse2 sse3 ssse3 sse4.1 sse4.2 popcnt avx bmi bmi2 avx2 lzcnt fma
runtimes: Singlepass, Cranelift, LLVM
features: wasix
rustc 1.98.1 (48a229cea 2026-09-01)
binary: rustc
commit-hash: 48a229ceaefd4985c50990b14116b6d856af0985
commit-date: 2026-09-01
host: x86_64-unknown-linux-gnu
release: 1.98.1

Built from main with cargo build --release --manifest-path lib/cli/Cargo.toml --features "cranelift,singlepass,llvm" (LLVM 22), Ubuntu 24.04, x86_64.

Steps to reproduce

  1. Save rmw8_add_imm.wat:
wat
(module
  (memory 1 1 shared)
  (func (export "f") (result i32)
    i32.const 64
    i32.const 300
    i32.atomic.rmw8.add_u))
  1. Assemble it: wat2wasm --enable-all rmw8_add_imm.wat -o rmw8_add_imm.wasm
  2. Run it on each backend:
bash
wasmer run --enable-threads --singlepass --invoke=f rmw8_add_imm.wasm
wasmer run --enable-threads --cranelift  --invoke=f rmw8_add_imm.wasm
wasmer run --enable-threads --llvm       --invoke=f rmw8_add_imm.wasm
  1. For the compile failure, save rmw8_add_imm_i64.wat and run it the same way:
wat
(module
  (memory 1 1 shared)
  (func (export "f") (result i32)
    i32.const 64
    i64.const 300
    i64.atomic.rmw8.add_u
    i32.wrap_i64))
  1. Control, rmw8_add_local.wat, the same operation with the operand in a local, which singlepass gets right:
wat
(module
  (memory 1 1 shared)
  (func (export "f") (result i32) (local $x i32)
    i32.const 300
    local.set $x
    i32.const 64
    local.get $x
    i32.atomic.rmw8.add_u))

Expected behavior

i32.atomic.rmw8.add_u returns the old value at address 64, which is 0, and stores (0 + 300) & 0xff = 44. All modules below should print 0 on every backend, and rmw8_add_imm_i64 should compile.

module cranelift, llvm, wasmtime, WasmEdge
rmw8_add_imm (300, rmw8.add_u) 0
rmw8_xchg_imm (0x133, rmw8.xchg_u) 0
rmw16_add_imm (70000, rmw16.add_u) 0
rmw8_add_imm_i64 (i64.const 300, i64.atomic.rmw8.add_u) 0
rmw8_add_local 0

Actual behavior

singlepass:

module result
rmw8_add_imm 256
rmw8_xchg_imm 256
rmw16_add_imm 65536
rmw8_add_imm_i64 error: Unable to compile ... compile error: Codegen("singlepass can't emit MOVZX S8 Imm64(300) S64 GPR(RAX)")
rmw8_add_local 0 (correct)

Additional context

Will be updated