Issue 234272
Summary [clang] Big-endian "CRC" whose check uses the CRC's *low* byte is recognized with the data treated as dead: the loop becomes a table lookup that ignores the data (miscompile from C, `-O1`+, x86-64)
Labels backend:X86, miscompilation, generated by fuzzer
Assignees
Reporter YuanchengJiang
    ## C reproducer — `repro.c`
```c
#include <stdio.h>
#include <stdint.h>

/* bitwise CRC-16 step over one byte, where the data byte is compared against the
   LOW byte of the CRC (a non-standard alignment) */
__attribute__((noinline)) uint16_t crc_step(uint8_t data, uint16_t crc) {
  for (int i = 0; i < 8; i++) {
    uint8_t x = data ^ (uint8_t)crc;
    data <<= 1;
    uint16_t sh = crc << 1;
    crc = (int8_t)x >= 0 ? sh : sh ^ 0x1021;
  }
  return crc;
}

int main(void) {
  volatile uint8_t d = 0xff;
  volatile uint16_t c = 0;
  printf("%d\n", crc_step(d, c));   /* want 11395 */
  return 0;
}
```

| compiler (x86-64, native) | output |
|---|---|
| `clang -O0` | `11395` (correct; a Python model of the loop agrees) |
| `clang -O1` / `-O2` / `-O3` / `-O2 -march=x86-64-v3` | **`0` (WRONG)** |
| `-O2` with the fix below | `11395` |

Trunk: https://godbolt.org/z/rTjzdPKzj

`crc_step` at `-O2`: the `data` argument is never read.
```
crc_step:  movl %esi,%eax ; shll $8,%eax ; shrl $8,%esi
           leaq .L.crctable(%rip),%rcx ; xorw (%rcx,%rsi,2),%ax ; retq
```

## Root cause
- **What HashRecognize accepts:** it treats the loop as a big-endian CRC-16 (shift left, polynomial 0x1021) with
  auxiliary data `data`. The checked bit is bit 7 of `data ^ trunc(crc)`: an **i8** XOR, so it is bit 7 of the CRC's
  low byte, not the CRC's top bit 15.
- **The bad rule:** `recognizeCRC` then applies a rule added by **PR #207231** ("Make `LHSAux` null if it is dead"):
  ```cpp
  // In the big-endian case where LHSAux is narrower than LHS, the most
  // significant bit check will never be influenced by LHSAux. ...
  if (*IsBigEndian && LHSAux && bw(LHSAux) < bw(LHS))
    LHSAux = nullptr;
  ```
- **Where the rule holds and where it fails:** it's right when the XOR is done in the CRC's width (`zext(data) ^ crc`,
  the `crc16.be.tc8.zext.data` test): the zero-extended data never reaches bit 15. When the CRC is truncated to the
  data's width instead (`data ^ trunc(crc)`, the `misalign` test), `data` feeds every checked bit, and the checked
  CRC bit isn't the top bit, so the loop isn't a CRC of the recognized form at all.
- **Effect of #207231:** it turned that case from emitting poison into silently dropping the data.
- **Result:** `optimizeCRCLoop` builds a Sarwate table for a big-endian CRC-16 with no data:
  `crc' = (crc << 8) ^ T[crc >> 8]`.

## Fix (validated)
Only treat `LHSAux` as dead when the XOR feeding the check is as wide as the CRC. Otherwise reject the loop.
`isConditionalOnXorOfPHIs` now also returns the matched XOR:
```cpp
    if (DataXor && DataXor->getType()->getIntegerBitWidth() <
                       LHS->getType()->getIntegerBitWidth())
      return "Big-endian CRC check misaligned with the CRC's top bit";
    LHSAux = nullptr;
```
Results:
- `repro.c` prints `11395` at `-O2`.
- TV is silent on the LoopIdiom test.
- In `Analysis/HashRecognize/cyclic-redundancy-check.ll` only `crc16.be.tc8.misalign` changes, to "Did not find a hash
  algorithm".
- The other CRC loops in the LoopIdiom tests are still transformed; only this function changes.

The CHECK lines for `crc16.be.tc8.misalign` must be updated in `Transforms/LoopIdiom/RISCV/`, `Transforms/LoopIdiom/AArch64/`
(the same TV fire with `-mtriple=aarch64`) and `Analysis/HashRecognize/` `cyclic-redundancy-check.ll`.

In the AGT tree this is v225 (`AGT_NOFIX=1` disables it). The positive suite entry is S28.

## Sibling case: data wider than the CRC (found by the CRC-loop generator, `scripts/crcgen.py`)
In `wide-data.ll`, `zext(crc) ^ data` is done at the **data's** width (i16 for a CRC-8) and its sign bit is tested.
The CRC's bits never reach that bit, yet HashRecognize reports a "big-endian CRC-8" and LoopIdiom builds a table:
TV-NEQ, e.g. `107` becomes `0` under `-passes=loop-idiom` on x86-64-v3 and AArch64+aes. It's the same missing
check. The final fix rejects every big-endian loop whose deciding XOR is not at the CRC's own width:
```cpp
  if (*IsBigEndian && DataXor && bw(DataXor) != bw(LHS))
    return "Big-endian CRC check misaligned with the CRC's top bit";
```
This shape is IR-only here: the C version is reshaped by earlier passes and isn't recognized at `-O2`. In
`Analysis/HashRecognize/cyclic-redundancy-check.ll`, only the misaligned function changes outcome.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to