Issue 241286
Summary [X86] X86CallFrameOptimization folds a VLA load into a push after stackrestore (miscompile at -O3)
Labels new issue
Assignees
Reporter jeffchen006
    A load from a VLA is folded into `pushq mem` of an outgoing call argument. The fold moves the load past the stackrestore that frees the VLA and past an earlier push, which writes exactly that VLA element.

**Reproducer** (`repro.c`)
```c
/* f adds k[h % (i + 1)] (k[l] = l) for h < 7, i < 3: 3 + 6 = 9, so the program
   must print 9. Clang -O3 prints 6. take returns d + e (q is never 1001..1044);
   its body is long only so that the inliner keeps the call. On x86-64 the
   arguments d and e go on the stack. */
int printf(const char *, ...);
long take(long p, long q, long r, long s, long u, long w, long d, long e) {
  long t = d + e;
  if (q == 1001) t = t * 1 + s;
  if (q == 1002) t = t * 2 + s;
  if (q == 1003) t = t * 3 + s;
  if (q == 1004) t = t * 4 + s;
  if (q == 1005) t = t * 5 + s;
  if (q == 1006) t = t * 6 + s;
  if (q == 1007) t = t * 7 + s;
  if (q == 1008) t = t * 8 + s;
  if (q == 1009) t = t * 9 + s;
  if (q == 1010) t = t * 10 + s;
  if (q == 1011) t = t * 11 + s;
  if (q == 1012) t = t * 12 + s;
  if (q == 1013) t = t * 13 + s;
  if (q == 1014) t = t * 14 + s;
 if (q == 1015) t = t * 15 + s;
  if (q == 1016) t = t * 16 + s;
  if (q == 1017) t = t * 17 + s;
  if (q == 1018) t = t * 18 + s;
  if (q == 1019) t = t * 19 + s;
  if (q == 1020) t = t * 20 + s;
  if (q == 1021) t = t * 21 + s;
  if (q == 1022) t = t * 22 + s;
  if (q == 1023) t = t * 23 + s;
  if (q == 1024) t = t * 24 + s;
  if (q == 1025) t = t * 25 + s;
  if (q == 1026) t = t * 26 + s;
  if (q == 1027) t = t * 27 + s;
  if (q == 1028) t = t * 28 + s;
  if (q == 1029) t = t * 29 + s;
  if (q == 1030) t = t * 30 + s;
  if (q == 1031) t = t * 31 + s;
  if (q == 1032) t = t * 32 + s;
  if (q == 1033) t = t * 33 + s;
  if (q == 1034) t = t * 34 + s;
  if (q == 1035) t = t * 35 + s;
  if (q == 1036) t = t * 36 + s;
  if (q == 1037) t = t * 37 + s;
  if (q == 1038) t = t * 38 + s;
  if (q == 1039) t = t * 39 + s;
  if (q == 1040) t = t * 40 + s;
  if (q == 1041) t = t * 41 + s;
  if (q == 1042) t = t * 42 + s;
  if (q == 1043) t = t * 43 + s;
  if (q == 1044) t = t * 44 + s;
  return t;
}
long f(int g) {
  long acc = 0;
 for (int h = 0; h < 7; h++)
    for (int i = 0; i < 3; i++) {
      short j;                /* unused; changes the stack layout */
      if (i >> g) /* never true (g = 9) */
        return acc;
      long k[i + 1]; /* VLA: stack save/restore in each iteration */
      for (int l = 0; l < i + 1; l++)
        k[l] = l;
      acc += take(0, acc, 0, acc, acc, acc, k[h % (i + 1)], 0);
    }
  return acc;
}
int main(void) {
 printf("%ld\n", f(9));
  return 0;
}
```

**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang -O0 repro.c -o o0
$ ./o0
9
$ clang -O3 repro.c -o o3
$ ./o3
6
```
Expected (as `-O0`): `9`. `-O3` prints `6`.

**Why the expected output is right**
VLA sizes 1..3; every element is written before the read; the index `h % (i+1)` is in range; the read is a call argument while the VLA is alive. UBSan/ASan/MSan at -O0 are clean.

**Potential Root cause**
`llvm/lib/Target/X86/X86CallFrameOptimization.cpp`: the stores of call arguments become pushes, and `canFoldIntoRegPush` folds `%32 = MOV64rm` (VLA load) into `pushq -16(%rdx,%rdi,8)`. In MIR the load is before `$rsp = COPY %27` (stackrestore) and `ADJCALLSTACKDOWN64`. The legality check scans only from the load to FrameSetup for `isLoadFoldBarrier()` (mayStore / isCall / unmodeled side effects); the SP copy is none of these, and the pushes between FrameSetup and the insertion point are not scanned. After the restore, `pushq $0` writes `k[1]`, and the folded load reads 0.

**Possible Proposed fix**
```diff
   for (MachineBasicBlock::iterator I = *DefMI; I != FrameSetup; ++I)
-    if (I->isLoadFoldBarrier())
+    if (I->isLoadFoldBarrier() ||
+ I->modifiesRegister(TRI->getStackRegister(), TRI))
       return nullptr;
```
Without an SP change the loaded object is above SP and the pushes write below it, so they cannot overlap.

Related: #47943 and #39465 (same hazard, an access to a dead dynamic alloca moved past stackrestore, in MemCpyOpt) and #39711. Nothing is reported for X86CallFrameOptimization; this case needs a separate fix there.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to