| Issue |
241281
|
| Summary |
[clang] f() & and f() && share one Itanium vcall offset (ref-qualifier ignored) (wrong code at all -O levels)
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
jeffchen006
|
In a virtual base, `V::f() &` and `V::f() &&` get ONE vcall offset. The virtual thunk for `Y::f() &&` reads the slot of `X::f() &` and passes the X subobject as `this`. GCC uses two slots. Wrong at all levels; at -O3 InstCombine folds the compare with the `align 16` of `this`, which hides it.
**Reproducer** (`repro.cpp`)
```cpp
// V has two virtual functions f() & and f() &&. In D, X overrides f() & and
// Y overrides f() &&, so the call std::move(*v).f() must run Y::f() && with
// this == the Y subobject of g (not the X subobject). Correct output: 0
// (clang gives f() & and f() && one shared vcall offset, so the thunk passes X.)
extern "C" int printf(const char *, ...);
struct V { virtual int f() & { return 1; } virtual int f() && { return 2; } };
struct Z { virtual void z() {} };
struct X : virtual V { int f() & override { return 3; } };
struct alignas(16) Y : virtual V { int f() && override; };
struct D : Z, X, Y {}; // Z at 0, X at 8, Y at 16
D g;
int Y::f() && { return (void *)this == (void *)static_cast<X *>(&g); }
int main() {
V *v = &g;
printf("%d\n", static_cast<V &&>(*v).f());
}
```
**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
$ clang++ -std=c++17 -O0 -S -emit-llvm repro.cpp -o - | grep -o '_ZTv0_n[0-9]*_N[RO]1[XY]1fEv' | sort -u
_ZTv0_n24_NO1Y1fEv
_ZTv0_n24_NR1X1fEv
```
Correct: `0`. Clang `-O0` prints `1`; `-O3` prints `0` only because a compare is folded. Both thunks use the same vcall offset (`n24`).
**Why the expected output is right**
`V::f() &&` has final overrider `Y::f() &&` ([class.virtual]), so `this` there is the Y subobject; the `void*` compares are defined. ASan at -O0 is clean.
**How the -O0/-O3 difference arises**
Here -O0 is the wrong one: UBSan at -O0 reports "member call on misaligned address for type Y" because of the wrong `this` from the bug. At -O3 the `align 16` claim on `this` lets InstCombine fold the compare, so -O3 prints the right value by chance.
**Potential Root cause**
`clang/lib/AST/VTableBuilder.cpp`, `HasSameVirtualSignature` (called from `VCallOffsetMap::MethodsCanShareVCallOffset`): it compares cv-qualifiers and parameter types, not the ref-qualifier. Clang emits `_ZTv0_n24_NR1X1fEv` and `_ZTv0_n24_NO1Y1fEv` (same vcall offset -24); g++ emits `_ZTv0_n24_...` and `_ZTv0_n32_...`.
**Possible Proposed fix**
```diff
if (LT->getMethodQuals() != RT->getMethodQuals())
return false;
+ if (LT->getRefQualifier() != RT->getRefQualifier())
+ return false;
return LT->getParamTypes() == RT->getParamTypes();
```
This changes the vtable layout for such classes (to match GCC and the Itanium ABI); it needs a release note.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs