| Issue |
244525
|
| Summary |
[fuzzer-found][LoongArch] performSETCCCombine drops `zext(trunc)` around an AND compare without checking the truncation width: `(x & y) != x` on the low bytes of two shorts is miscompiled
|
| Labels |
miscompilation,
backend:loongarch,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```cpp
// clang --target=loongarch64-linux-gnu -O2 repro.c (also -O1/-O3; plain LA64, no LSX needed)
// x and y are the low bytes of two shorts. The LoongArch SETCC combine drops the truncate+zero_extend pair
// around the AND because the incoming values carry AssertSext i16, but the truncation is to i8.
int printf(const char *, ...);
__attribute__((noinline)) long f(short a, short b, unsigned char *p, int c, long *o) {
unsigned char x, y;
if (c) { x = a; y = b; } else { x = p[0]; y = p[1]; }
unsigned long X = x, Y = y;
o[0] = X; o[1] = Y;
return (X & Y) != X; /* is some bit of x missing from y? */
}
int main(void) {
unsigned char p[2] = {1, 1};
long o[2];
short as[4] = {0x100, 0x0FF, 0x1FF, -1};
short bs[4] = {0, 0x1FF, 0x0FF, 0x7F00};
long want[4] = {0, 0, 0, 1}; /* x,y = (0,0) (FF,FF) (FF,FF) (FF,0) */
int bad = 0;
for (int i = 0; i < 4; i++) {
short a = as[i], b = bs[i]; int c = 1;
__asm__("" : "+r"(a)); __asm__("" : "+r"(b)); __asm__("" : "+r"(c));
long got = f(a, b, p, c, o);
printf("f(%#x, %#x) = %ld, expected %ld\n", (unsigned short)a, (unsigned short)b, got, want[i]);
bad |= got != want[i];
}
printf(bad ? "FAIL\n" : "PASS\n");
return bad;
}
```
Resulted in this output (`clang --target=loongarch64-linux-gnu -O2`, run in qemu-loongarch64 `-cpu la464` through the freestanding driver `qdrv.c`, which calls the same `f` with the same inputs; -O1/-O3 are the same):
```
f(0x100, 0) = 1, expected 0
f(0xff, 0x1ff) = 0, expected 0
f(0x1ff, 0xff) = 1, expected 0
f(0xffff, 0x7f00) = 1, expected 1
FAIL
```
| a, b (short) | x, y = low bytes | expected `(x & y) != x` | -O2 |
|---|---|---|---|
| 0x100, 0 | 0, 0 | 0 | **1** |
| 0x1ff, 0xff | 0xff, 0xff | 0 | **1** |
Wrong code on the `c != 0` path: `andn $a0, $a0, $a1; sltu $a0, $zero, $a0` on the full 16-bit sign-extended registers, without masking to 8 bits first. The `-O0` build and gcc print PASS.
Trunk: https://godbolt.org/z/6bW7PY4ev
Root cause: `performSETCCCombine` (LoongArchISelLowering.cpp, added in #99272) rewrites `setcc (and (zext (trunc A)), (zext (trunc B))), (zext (trunc B))` (and the `xor -1` form) into `setcc (and A, B), B`. It assumes the truncate/zero-extend pair is redundant. `checkValueWidth` only checks that A and B are i8/i16 loads or carry `AssertSext`/`AssertZext` of i8/i16. It never checks three things:
- that this width equals the truncation width (here AssertSext i16, from the cross-block `short`, truncated to i8);
- that the load is not an any-extending EXTLOAD;
- that the predicate is not a signed relational one when an input is sign-extended (the original compares zero-extended values).
Suggested fix: in `performSETCCCombine`, require that the extension width of both inputs equals the truncation width. Also reject EXTLOAD/NON_EXTLOAD inputs, reject signed predicates when an input is sign-extended, and reject a sign-extended compare operand paired with a zero-extended AND operand. Validated locally: the program prints PASS in qemu, the AGT check no longer fires on the C repro or on the three IR variants in `repro_ir.ll`, and all 270 LoongArch CodeGen tests give the same llc output as before. This includes `andn-icmp.ll`, the combine's own test, whose unsigned-predicate cases stay folded. The only differences are tool artifacts from lit `%t` files.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs