Issue 241275
Summary [GVN][MemDep] invariant.group Def in the load block cached as non-local; GVN makes poison (miscompile at -O3)
Labels new issue
Assignees
Reporter jeffchen006
    With `-fstrict-vtable-pointers`, MemDep returns a dominating `!invariant.group` load in the SAME block as a non-local Def. GVN `constructSSAForLoadSet` then asks SSAUpdater for a value in the middle of that block and gets poison. MemorySSA-based GVN is correct.

**Reproducer** (`repro.cpp`)
```cpp
// b->f(41) calls A::f through the virtual-base chain C -> B -> A. Correct output: 42.
// clang++ -O3 -fstrict-vtable-pointers: SIGSEGV. GVN (MemDep invariant.group path) turns
// the vptr load after the invoke of C::C into poison. The local "A a;" is needed to show it.
extern "C" int printf(const char *, ...);
volatile int sink;
struct A { A(); virtual unsigned f(unsigned x); };
struct B : virtual A { B(); };
struct C : virtual B { C(); };
unsigned A::f(unsigned x) { return x + 1; }
A::A() {}
B::B() {}
#define S8 sink = 0; sink = 0; sink = 0; sink = 0; sink = 0; sink = 0; sink = 0; sink = 0;
C::C() {  // big body: not inlined; may throw, so "new C" is an invoke
  S8 S8 S8 S8 S8
  if (sink) throw 1;
}
int main() {
  C *c = new C;
  A a;
  B *b = c;
  printf("%u\n", b->f(41));
}
```

**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang++ -std=c++17 -fstrict-vtable-pointers -O0 repro.cpp -o ./o0
$ ./o0
42
O0 rc=0
$ clang++ -std=c++17 -fstrict-vtable-pointers -O3 repro.cpp -o ./o3
$ ./o3
Segmentation fault
O3 rc=139
```
Expected (as `-O0`): `42`. `-O3` crashes with SIGSEGV.

**Why the expected output is right**
No fields, no placement new, no storage reuse, so the `-fstrict-vtable-pointers` rules hold; only implicit derived-to-base conversions of a fully constructed object. UBSan/ASan/MSan at -O0 are clean.

**Potential Root cause**
`llvm/lib/Analysis/MemoryDependenceAnalysis.cpp`, `MemoryDependenceResults::getInvariantGroupPointerDependency` is called for a block `BB` that is not the load's block (predecessor walk). It finds `%v = load %o, !invariant.group` in the load's own block; `%v->getParent() != BB`, so it caches `%v` as a non-local Def and returns NonLocal. `GVNPassImpl::processNonLocalLoad` -> `constructSSAForLoadSet`: the value is in the load's block, `DT.properlyDominates(ok, ok)` is false, SSAUpdater finds no value in the predecessor, and returns poison.

**Possible Proposed fix**
```diff
   if (ClosestDependency->getParent() == BB)
     return MemDepResult::getDef(ClosestDependency);
+  if (!DT.properlyDominates(ClosestDependency->getParent(), LI->getParent()))
+ return MemDepResult::getUnknown();
```

**Reduced IR**
```llvm
@vt = constant [4 x ptr] zeroinitializer
declare ptr @alloc()
declare void @ctor(ptr)
declare void @llvm.assume(i1)
define ptr @f() personality ptr null {
  %o = call ptr @alloc()
  invoke void @ctor(ptr %o) to label %ok unwind label %lp
ok:
  %v = load ptr, ptr %o, align 8, !invariant.group !0
  %c = icmp eq ptr %v, getelementptr inbounds (i8, ptr @vt, i64 8)
 call void @llvm.assume(i1 %c)
  %v1 = load ptr, ptr %o, align 8
  %off = load i64, ptr %v1, align 8
  %p = getelementptr i8, ptr %o, i64 %off
  %r = load ptr, ptr %p, align 8, !invariant.group !0
  ret ptr %r
lp:
  %l = landingpad { ptr, i32 } cleanup
  resume { ptr, i32 } %l
}
!0 = !{}
```
`opt -passes=gvn gvn_min.ll` gives `ret ptr poison`; with `-enable-gvn-memoryssa` it gives `ret ptr getelementptr (i8, ptr @vt, i64 8)` (right).

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

Reply via email to