Issue 241261
Summary [clang][CodeGen] Relative vtables: ::delete reads offset-to-top with the classic layout (wrong code at -O0)
Labels clang
Assignees
Reporter jeffchen006
    With `-fexperimental-relative-c++-abi-vtables`, `::delete p` on a polymorphic object reads offset-to-top as an `i64` at vtable-16 (classic layout). In the relative layout it is an `i32` at vtable-8. The global `operator delete` gets a wrong pointer at -O0; at -O3 the out-of-bounds load is folded and the output changes again.

**Reproducer** (`repro.cpp`)
```cpp
// ::delete p must pass the address of the complete object (pool) to operator
// delete ([expr.delete]/7). Correct output: ok (g++ -fno-rtti, clang++ -O3).
// clang++ -O0 -fno-rtti -fexperimental-relative-c++-abi-vtables: wrong pointer.
extern "C" int printf(const char *, ...);

alignas(16) unsigned char pool[64];
void *freed;

void *operator new(decltype(sizeof 0)) { return pool; }
void operator delete(void *p, decltype(sizeof 0)) noexcept { freed = p; }

struct B {
  virtual ~B() {}
};

int main() {
  B *p = new B;
  ::delete p;
  if (freed == pool)
    printf("ok\n");
  else
 printf("wrong pointer\n");
}
```

**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 -fno-rtti -fexperimental-relative-c++-abi-vtables repro.cpp -o ./o0
$ ./o0
wrong pointer
$ clang++ -O3 -fno-rtti -fexperimental-relative-c++-abi-vtables repro.cpp -o ./o3
$ ./o3
ok
```
Expected (as the classic ABI and g++ -fno-rtti): `ok`. `-O0` prints `wrong pointer`; `-O3` prints `ok` by chance.

**Why the expected output is right**
The replaced `operator new` returns 16-aligned static storage for one 16-byte object; the sized and unsized `operator delete` are replaced. [expr.delete]/7-9: `::delete p` passes the address of the complete object to the global `operator delete`.

**How the -O0/-O3 difference arises**
Here -O0 is the wrong one: clang -O0 reads 8 bytes before the vtable (ASan reports global-buffer-overflow at that load, the bug itself). At -O1+ the load is out of bounds of the vtable global and is folded, so -O3 prints the right text by chance.

**Potential Root cause**
`clang/lib/CodeGen/ItaniumCXXABI.cpp`, `ItaniumCXXABI::emitVirtualObjectDelete`, global-delete path: `CreateConstInBoundsGEP1_64(CGF.IntPtrTy, VTable, -2)` + `load i64`. This is the classic layout. `emitDynamicCastToVoid` has the `RelativeCXXABIVTables` branch; this function does not.

**Possible Proposed fix**
Reuse the existing correct code:
```diff
   if (UseGlobalDelete) {
-    auto *ClassDecl = ElementType->castAsCXXRecordDecl();
-    llvm::Value *VTable = CGF.GetVTablePtr(Ptr, CGF.DefaultPtrTy, ClassDecl);
-    llvm::Value *OffsetPtr = CGF.Builder.CreateConstInBoundsGEP1_64(
-        CGF.IntPtrTy, VTable, -2, "complete-offset.ptr");
-    llvm::Value *Offset = CGF.Builder.CreateAlignedLoad(CGF.IntPtrTy, OffsetPtr,
- CGF.getPointerAlign());
-    llvm::Value *CompletePtr = Ptr.emitRawPointer(CGF);
-    CompletePtr =
- CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, CompletePtr, Offset);
+ llvm::Value *CompletePtr = emitDynamicCastToVoid(CGF, Ptr, ElementType);
```
For the classic ABI the IR does not change.

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

Reply via email to