tcg/optimize: computes incorrect smask for some shifts
PATCH 1/1 tcg/optimize: computes incorrect smask for some shifts
We were getting test failures in the Zig project test suite that only occurred when running the test binary under qemu-i386 without passing -one-insn-per-tb, suggesting a tcg bug. I reduced the behavioral difference down to:
.globl _start
_start:
movw $0x4000, %ax
addw %ax, %ax
cwtl
shrl %eax
xorl %ebx, %ebx
cmpw $-0x3fff, %ax
setnl %bl
movl $1, %eax
int $0x80which reproduces the bug and is fixed by this patch:
$ as --32 repro.s -o repro.o; ld -m elf_i386 repro.o -o repro
$ ./repro; echo $?
0
$ qemu-i386 -one-insn-per-tb ./repro; echo $?
0
$ qemu-i386 ./repro; echo $?
1
$ ../qemu/build/qemu-i386 ./repro; echo $?
0---
Jacob Young (1):
tcg/optimize: Fix s_mask computation for shifts
tcg/optimize.c | 8 ++++++++ 1 file changed, 8 insertions(+)
---
base-commit: b428fe036233cbd15d37e3c027ab6ca4d3661a80
change-id: 20260803-fix-shift-smask-81e36be921e2
Best regards,
Signature · 1 line
Jacob Young <[email protected]>
PATCH 1/1 tcg/optimize: Fix s_mask computation for shifts
Previously, we would produce an incorrect smask in this case:
t1.smask = 0xffffffff80000000
shr_i32 t0,t1,$0x1
t0.smask = 0xffffffffc0000000because do_constant_folding would produce smask = 0x40000000 which was passed unchanged to fold_masks_zos which would then update with s_mask |= INT32_MIN;, producing the incorrect result above.
Reproducer (i386 assembly):
.text
.globl _start
_start:
movw $0x4000, %ax
addw %ax, %ax
cwtl
shrl %eax
xorl %ebx, %ebx
cmpw $-0x3fff, %ax
setnl %bl
movl $1, %eax
int $0x80--- tcg/optimize.c | 8 ++++++++ 1 file changed, 8 insertions(+)
tcg/optimize.c +8 -0
@@ -2658,6 +2658,14 @@ static bool fold_shift(OptContext *ctx, TCGOp *op) o_mask =do_constant_folding (op->opc, ctx->type, o_mask, sh); s_mask =do_constant_folding (op->opc, ctx->type, s_mask, sh); +/* +* If the bit shifted into the result sign was not in the +* input s_mask, then no bits are known to match it. +*/ +if ((int64_t )s_mask >=0 ) { + s_mask =0 ; + } +return fold_masks_zos (ctx, op, z_mask, o_mask, s_mask); }
Signature · 1 line
2.55.0