Issue 241288
Summary [clang][TBAA] "vtable pointer" is not under "omnipotent char"; char reads miss vptr stores (miscompile at -O3)
Labels clang
Assignees
Reporter jeffchen006
    The TBAA node for vptr accesses is a child of the root, not of "omnipotent char". So TBAA says that a vptr store in a constructor does not alias `unsigned char` reads of the same bytes, and EarlyCSE reuses bytes read before the derived constructor changed the vptr.

**Reproducer** (`repro.cpp`)
```cpp
// B() saves the bytes of the object's vptr (now B's vtable). D() then sets the
// vptr to D's vtable, so the same bytes read again in D() differ.  Correct output: 1
// hide() returns its argument; the volatile pointer only hides that p == this.
extern "C" int printf(const char *, ...);
unsigned char *id(void *p) { return (unsigned char *)p; }
unsigned char *(*volatile hide)(void *) = id;
unsigned char *p;
struct B {
  B(unsigned char *old) {
    p = hide(this);
    for (int i = 0; i < 8; i++) old[i] = p[i];
  }
  virtual void f() {}
};
struct D : B {
 D(unsigned char *old, int *changed) : B(old) {
    *changed = 0;
    for (int i = 0; i < 8; i++) *changed |= p[i] != old[i];
  }
};
int main() {
 unsigned char old[8];
  int changed;
  D d(old, &changed);
 printf("%d\n", changed);
}
```

**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 -O0 repro.cpp -o o0
$ ./o0
1
$ clang++ -std=c++17 -O3 repro.cpp -o o3
$ ./o3
0
```
Expected (as `-O0`): `1`. `-O3` prints `0`.

**Why the expected output is right**
All reads are `unsigned char` glvalues inside `d` ([basic.lval]/11.3); `rep` comes from `this`, so [class.cdtor]/2 does not apply. The output (vptr bytes changed or not) is fixed by the Itanium ABI. UBSan/ASan at -O0 are clean, MSan with libc++ too. (P1839 notes that object-representation access is underspecified in C++17; it is intended to be valid.)

**Potential Root cause**
`clang/lib/CodeGen/CodeGenTBAA.cpp`, `CodeGenTBAA::getVTablePtrAccessInfo`: `createScalarTypeNode("vtable pointer", getRoot(), Size)`. The parent is the root, a sibling of "omnipotent char" (the comment at `getChar()` says char powers do not cover vtables). After both constructors are inlined, EarlyCSE reuses the bytes loaded in B's constructor.

**Possible Proposed fix**
```diff
-  return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getRoot(), Size),
+ return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getChar(), Size),
                         Size);
```
vptr accesses still do not alias int/float/pointer accesses, but they alias char accesses. Some vptr load CSE may get weaker.

Related: the placement of "vtable pointer" outside "omnipotent char" is a deliberate design (141e46faf0d7, 2012), and open PR #209053 changes vptr TBAA in another way without discussing char. No report so far shows a valid program that this design miscompiles; this one does.

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

Reply via email to