Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix aarch64 atomic_cas.i32 miscompile without LSE (#14712)

Jim Hall1a7784681ee9
diff --git a/cranelift/codegen/src/isa/aarch64/inst/emit.rs b/cranelift/codegen/src/isa/aarch64/inst/emit.rs
index f96d53f98e..60d2b4eba7 100644
--- a/cranelift/codegen/src/isa/aarch64/inst/emit.rs
+++ b/cranelift/codegen/src/isa/aarch64/inst/emit.rs
@@ -1949,7 +1949,7 @@ impl MachInstEmit for Inst {
/* Emit this:
again:
ldaxr{,b,h} x/w27, [x25]
- cmp x27, x/w26 uxt{b,h}
+ cmp x/w27, x/w26 uxt{b,h}
b.ne out
stlxr{,b,h} w24, x/w28, [x25]
cbnz x24, again
@@ -1980,14 +1980,20 @@ impl MachInstEmit for Inst {
// ldaxr x27, [x25]
sink.put4(enc_ldaxr(ty, x27wr, x25));
- // The top 32-bits are zero-extended by the ldaxr so we don't
- // have to use UXTW, just the x-form of the register.
- let (bit21, extend_op) = match ty {
- I8 => (0b1, 0b000000),
- I16 => (0b1, 0b001000),
- _ => (0b0, 0b000000),
+ // The top 32-bits are zero-extended by the ldaxr so for I64
+ // we don't have to use UXTW, just the x-form of the
+ // register. I8/I16 use UXTB/UXTH to only compare their
+ // respective widths, and I32 uses the w-form of the
+ // register (`sf` cleared) to only compare the low 32 bits,
+ // since the expected value may have garbage in its upper
+ // 32 bits (e.g. left behind by an `ireduce`).
+ let (sf, bit21, extend_op) = match ty {
+ I8 => (0b1, 0b1, 0b000000),
+ I16 => (0b1, 0b1, 0b001000),
+ I32 => (0b0, 0b0, 0b000000),
+ _ => (0b1, 0b0, 0b000000),
};
- let bits_31_21 = 0b111_01011_000 | bit21;
+ let bits_31_21 = (sf << 10) | 0b011_01011_000 | bit21;
// cmp x27, x26 (== subs xzr, x27, x26)
sink.put4(enc_arith_rrr(bits_31_21, extend_op, xzrwr, x27, x26));
diff --git a/cranelift/codegen/src/isa/aarch64/inst/emit_tests.rs b/cranelift/codegen/src/isa/aarch64/inst/emit_tests.rs
index 14ea49686c..5efc306cd0 100644
--- a/cranelift/codegen/src/isa/aarch64/inst/emit_tests.rs
+++ b/cranelift/codegen/src/isa/aarch64/inst/emit_tests.rs
@@ -7897,7 +7897,7 @@ fn test_aarch64_binemit() {
oldval: writable_xreg(27),
scratch: writable_xreg(24),
},
- "3BFF5F887F031AEB610000543CFF188898FFFFB5",
+ "3BFF5F887F031A6B610000543CFF188898FFFFB5",
"atomic_cas_loop_32 addr=x25, expect=x26, replacement=x28, oldval=x27, scratch=x24",
));
diff --git a/cranelift/filetests/filetests/isa/aarch64/atomic-cas.clif b/cranelift/filetests/filetests/isa/aarch64/atomic-cas.clif
index dd0e299456..11b1942104 100644
--- a/cranelift/filetests/filetests/isa/aarch64/atomic-cas.clif
+++ b/cranelift/filetests/filetests/isa/aarch64/atomic-cas.clif
@@ -41,7 +41,7 @@ block0(v0: i64, v1: i32, v2: i32):
; mov x26, x1
; mov x28, x2
; ldaxr w27, [x25] ; trap: heap_oob
-; cmp x27, x26
+; cmp w27, w26
; b.ne #0x34
; stlxr w24, w28, [x25] ; trap: heap_oob
; cbnz x24, #0x20
diff --git a/cranelift/filetests/filetests/isa/aarch64/issue14693.clif b/cranelift/filetests/filetests/isa/aarch64/issue14693.clif
new file mode 100644
index 0000000000..e93820e1c0
--- /dev/null
+++ b/cranelift/filetests/filetests/isa/aarch64/issue14693.clif
@@ -0,0 +1,60 @@
+test compile precise-output
+target aarch64
+
+;; Regression test for https://github.com/bytecodealliance/wasmtime/issues/14693.
+;; Without LSE, the `atomic_cas.i32` loop must only compare the low 32 bits of
+;; the expected value. `ireduce` is a free no-op on aarch64 and does not clear
+;; the upper 32 bits of the register it reuses, so comparing the full 64-bit
+;; register (as opposed to the i8/i16 cases, which already extend correctly)
+;; can spuriously fail the comparison even though the low 32 bits match.
+
+function u0:0(i64, i64, i32) -> i8 system_v {
+block0(v0: i64, v1: i64, v2: i32):
+ v3 = ireduce.i32 v1
+ v6 = atomic_cas.i32 v0, v3, v2
+ v7 = icmp eq v6, v3
+ return v7
+}
+
+; VCode:
+; stp fp, lr, [sp, #-16]!
+; mov fp, sp
+; str x28, [sp, #-16]!
+; stp x26, x27, [sp, #-16]!
+; stp x24, x25, [sp, #-16]!
+; block0:
+; mov x25, x0
+; mov x26, x1
+; mov x28, x2
+; atomic_cas_loop_32 addr=x25, expect=x26, replacement=x28, oldval=x27, scratch=x24
+; subs wzr, w27, w26
+; cset x0, eq
+; ldp x24, x25, [sp], #16
+; ldp x26, x27, [sp], #16
+; ldr x28, [sp], #16
+; ldp fp, lr, [sp], #16
+; ret
+;
+; Disassembled:
+; block0: ; offset 0x0
+; stp x29, x30, [sp, #-0x10]!
+; mov x29, sp
+; str x28, [sp, #-0x10]!
+; stp x26, x27, [sp, #-0x10]!
+; stp x24, x25, [sp, #-0x10]!
+; block1: ; offset 0x14
+; mov x25, x0
+; mov x26, x1
+; mov x28, x2
+; ldaxr w27, [x25] ; trap: heap_oob
+; cmp w27, w26
+; b.ne #0x34
+; stlxr w24, w28, [x25] ; trap: heap_oob
+; cbnz x24, #0x20
+; cmp w27, w26
+; cset x0, eq
+; ldp x24, x25, [sp], #0x10
+; ldp x26, x27, [sp], #0x10
+; ldr x28, [sp], #0x10
+; ldp x29, x30, [sp], #0x10
+; ret
diff --git a/cranelift/filetests/filetests/runtests/issue14693.clif b/cranelift/filetests/filetests/runtests/issue14693.clif
new file mode 100644
index 0000000000..0cf42910eb
--- /dev/null
+++ b/cranelift/filetests/filetests/runtests/issue14693.clif
@@ -0,0 +1,31 @@
+test interpret
+test run
+target aarch64
+target aarch64 has_lse
+target s390x
+target x86_64
+target riscv64 has_a
+target riscv64 has_c has_zcb
+
+; `atomic_cas.i32` must only compare the low 32 bits of the expected value.
+; On aarch64 without LSE, the 32-bit case was comparing the full 64-bit
+; register holding the `ireduce`'d expected value, so garbage in the upper
+; 32 bits (which `ireduce` is allowed to leave behind) made the comparison
+; fail even though the low 32 bits actually matched.
+function %cas_i32_junk_upper_bits(i64) -> i32 {
+ ss0 = explicit_slot 4
+
+block0(v0: i64):
+ v1 = iconst.i32 7
+ v2 = stack_addr.i64 ss0
+ store.i32 v1, v2
+
+ v3 = ireduce.i32 v0
+ v4 = iconst.i32 9
+ v5 = atomic_cas.i32 v2, v3, v4
+
+ v6 = load.i32 v2
+ return v6
+}
+; run: %cas_i32_junk_upper_bits(7) == 9
+; run: %cas_i32_junk_upper_bits(0x1_0000_0007) == 9