Issue 241255
Summary [WholeProgramDevirt] findLowestOffset checks unaligned bytes but stores aligned; slots overlap (miscompile at -O3)
Labels new issue
Assignees
Reporter jeffchen006
    Virtual constant propagation places an `i64` return value at an aligned offset, but the free-byte check used the unaligned offset. The `i64` overwrites an `i32` constant that was placed before, so a virtual call that returns 8 is folded to 0.

**Reproducer** (`repro.cpp`)
```cpp
// Two TUs from this file (-DTU0, -DTU1), full LTO, -fwhole-program-vtables.
// d is a D; D overrides i() to return 8, so pa->i() must return 8.
// Correct output: 8 (clang++ -O0, g++ -DALL). clang++ -O3: 0. The class layout
// only feeds the whole-program virtual-constant-propagation byte allocator.
extern "C" int printf(const char *, ...);

struct Empty {};
struct A {
  virtual ~A();
  virtual unsigned g(unsigned) { return 0; }
  virtual long h() const { return 0; }
 virtual unsigned i() const { return 0; }
};
struct B : A {
  ~B() { ; }
 long h() const { return 0; }
  virtual Empty u() { return Empty(); }
 unsigned i() const { return 0; }
};
struct C : A {
  unsigned g(unsigned) { return 8; }
  virtual int l() const;
};
struct D : virtual B {
 ~D();
  long h() const;
  unsigned i() const;
};
struct E : virtual C {
 E();
  unsigned g(unsigned) { return 3; }
  long h() const;
  int l() const { return 0; }
  unsigned i() const;
};

#if ALL || TU0
A::~A() {}
long D::h() const { return 1; }
unsigned D::i() const { return 8; }
long E::h() const { return 0; }
unsigned E::i() const { return 0; }

int main() {
  D *d = new D;
  B *pb = d;
  pb->h();
  pb->i();
  A *pa = d;
  unsigned r = pa->i();  // D::i: 8
  C *c = new C;
  A *pc = c;
  pc->h();
  c->g(6);
  c->g(5);
  c->l();
  E();
 printf("%u\n", r);
}
#endif

#if ALL || TU1
int C::l() const { return 2; }
D::~D() {}
E::E() : C() {}
#endif
```

**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 -std=c++20 -flto -fwhole-program-vtables -fvisibility=hidden -DTU0 -c repro.cpp -o ./tu0.o
$ clang++ -O0 -std=c++20 -flto -fwhole-program-vtables -fvisibility=hidden -DTU1 -c repro.cpp -o ./tu1.o
$ clang++ -O0 -std=c++20 -flto -fwhole-program-vtables -fvisibility=hidden -fuse-ld=lld ./tu0.o ./tu1.o -o ./m
$ ./m
-O0: 8
$ clang++ -O3 -std=c++20 -flto -fwhole-program-vtables -fvisibility=hidden -DTU0 -c repro.cpp -o ./tu0.o
$ clang++ -O3 -std=c++20 -flto -fwhole-program-vtables -fvisibility=hidden -DTU1 -c repro.cpp -o ./tu1.o
$ clang++ -O3 -std=c++20 -flto -fwhole-program-vtables -fvisibility=hidden -fuse-ld=lld ./tu0.o ./tu1.o -o ./m
$ ./m
-O3: 0
```
Expected (as `-O0`, g++): `8`. `-O3` prints `0`.

**Why the expected output is right**
Only virtual calls on fully constructed objects through valid base pointers; no casts or placement new; both TUs see the same class definitions (no ODR issue). UBSan/ASan/MSan at -O0 (2 TUs + LTO) are clean.

**Potential Root cause**
`llvm/lib/Transforms/IPO/WholeProgramDevirt.cpp`, `wholeprogramdevirt::findLowestOffset`, branch `Size != 1`: it checks that bytes `[I, I + Size/8)` are free, but returns `alignTo((MinByte + I) * 8, Size)`. If the unaligned window is free but the aligned one is not, the new constant overwrites an earlier one. The rounding came with PR #136630; the check did not change. In the output, `m::h` (i64 = 1) is read at +56 and `m::i` (i32 = 8) at +60, inside 56..63.

**Possible Proposed fix**
Check the window that is actually used:
```diff
     for (unsigned I = 0;; ++I) {
+      if (((MinByte + I) * 8) % Size != 0)
+        continue;
 for (auto &&B : Used) {
         ...
       }
-      return alignTo((MinByte + I) * 8, Size);
+      return (MinByte + I) * 8;
```
(Check the "before" direction, `setBeforeReturnValues`, the same way.)

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

Reply via email to