[clang] [clang][MS ABI] Fix non-virtual part of vtordispex this adjustment (PR #248414)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 3 04:34:39 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: torsten48
<details>
<summary>Changes</summary>
A vtordispex thunk locates the virtual base that contains the final overrider
through the vbtable of the most derived class and then adds a static offset.
`CalculateVtordispAdjustment` computed that offset relative to the overrider's
own class (`Overrider.Offset`) instead of relative to that virtual base
(`Overrider.VirtualBase`). The two only agree when the class sits at offset
zero inside the virtual base. Otherwise, e.g. when a base with a vfptr is laid
out before it, the overrider is called with a `this` that is too low by
exactly that offset. In the project where I ran into this, the callee then
read its pimpl pointer from the wrong place and crashed.
The fix computes the static part relative to `Overrider.VirtualBase`, which is
where the dynamic part of the thunk lands. For the i686 example in #<!-- -->205392 the
thunk now ends at 12 + 16 = 28, the same address MSVC's (plain vtordisp)
thunk produces. The existing vtordispex tests all have the overrider's class
at offset zero inside its virtual base and are unchanged; the new test covers
the non-zero case.
The mangled names of the affected thunks change, since they encode the
adjustment. I did not put this behind `-fclang-abi-compat`, because the old
thunk passes a wrong `this` and nothing can rely on it.
Testing (Windows, X86 target, Release + assertions):
- `clang/test/CodeGenCXX` and `clang/test/Layout` pass. The new test fails
without the fix (`12 non-virtual` instead of `16`).
- The reproducer from #<!-- -->205392 prints OK with the patched clang-cl for i686 and
x86_64, with default flags, `/vd2`, `/Od` and `/vd0`.
- In the original project, all ten vtordispex thunks of the affected class
move by exactly the missing offset (4 bytes on i686, 8 on x86_64 for the
common case).
- I did not run the full `check-clang` locally; I'm relying on CI for that.
Fixes #<!-- -->205392
I used Claude to analyze the bug and prepare this patch; I reviewed the change
and the test results above.
CC @<!-- -->rnk (Microsoft ABI maintainer)
---
Full diff: https://github.com/llvm/llvm-project/pull/248414.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+6)
- (modified) clang/lib/AST/VTableBuilder.cpp (+6-1)
- (modified) clang/test/CodeGenCXX/microsoft-abi-vtables-virtual-inheritance-vtordisps.cpp (+37)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index e87f00c687ab6b..639d57ed5aa67a 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -222,6 +222,12 @@ features cannot lower the translation-unit ABI level;
to generate more efficient code, if compatibility with either the platform
ABI or old Clang-built objects is not necessary. (#GH236450)
+- On Windows targets using the Microsoft C++ ABI, fixed the `this` adjustment
+ of vtordispex thunks when the class of the final overrider is not at offset
+ zero inside the virtual base that contains it. The overrider was called with
+ a wrong `this` pointer. The mangled names of the affected thunks change.
+ (#GH205392)
+
### AST Dumping Potentially Breaking Changes
- The AST printer (`-ast-print`) now prints a floating-point literal as the
diff --git a/clang/lib/AST/VTableBuilder.cpp b/clang/lib/AST/VTableBuilder.cpp
index b512820a72c8ee..5517d21d205625 100644
--- a/clang/lib/AST/VTableBuilder.cpp
+++ b/clang/lib/AST/VTableBuilder.cpp
@@ -2999,7 +2999,12 @@ void VFTableBuilder::CalculateVtordispAdjustment(
Context.getTypeSizeInChars(Context.IntTy).getQuantity() *
VTables.getVBTableIndex(MostDerivedClass, Overrider.VirtualBase);
- TA.NonVirtual = (ThisOffset - Overrider.Offset).getQuantity();
+ // The vbtable lookup yields the virtual base that contains the overrider,
+ // so the static part is relative to that base. The overrider's class need
+ // not be at offset zero inside it.
+ CharUnits OffsetOfOverriderVBase =
+ MostDerivedClassLayout.getVBaseClassOffset(Overrider.VirtualBase);
+ TA.NonVirtual = (ThisOffset - OffsetOfOverriderVBase).getQuantity();
}
static void GroupNewVirtualOverloads(
diff --git a/clang/test/CodeGenCXX/microsoft-abi-vtables-virtual-inheritance-vtordisps.cpp b/clang/test/CodeGenCXX/microsoft-abi-vtables-virtual-inheritance-vtordisps.cpp
index be9f281560dcff..4f8bdb715fe126 100644
--- a/clang/test/CodeGenCXX/microsoft-abi-vtables-virtual-inheritance-vtordisps.cpp
+++ b/clang/test/CodeGenCXX/microsoft-abi-vtables-virtual-inheritance-vtordisps.cpp
@@ -567,3 +567,40 @@ struct X : B, C {
X::X() {}
}
+
+namespace gh205392 {
+// The final overrider A::f is defined in a virtual base B of the most derived
+// class, but A is not at offset zero inside B: P has a vfptr and is laid out
+// first. The vtordispex thunk finds B through the vbtable, so its non-virtual
+// part has to be relative to B, not to A.
+struct X {
+ virtual void f();
+};
+
+struct A : virtual X {
+ A();
+ virtual void f();
+ int a;
+};
+
+struct P {
+ virtual void p();
+};
+
+struct B : P, A {};
+
+struct C : virtual B {
+ // C: vbptr at 0, vtordisp at 4, X at 8, B at 12, P at 12, A at 16.
+ // A::f expects "this" at A + 12 = 28, i.e. B + 16.
+
+ // CHECK-LABEL: VFTable for 'gh205392::X' in 'gh205392::A' in 'gh205392::B' in 'gh205392::C' (1 entry).
+ // CHECK-NEXT: 0 | void gh205392::A::f()
+ // CHECK-NEXT: [this adjustment: vtordisp at -4, vbptr at 8 to the left,
+ // CHECK-NEXT: vboffset at 8 in the vbtable, 16 non-virtual]
+
+ // `vtordispex{8,8,4294967292,16}'
+ // MANGLING-DAG: @"?f at A@gh205392@@$R477PPPPPPPM at BA@AEXXZ"
+};
+
+C c;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/248414
More information about the cfe-commits
mailing list