llvm-project
edd22189 - [clang][MS ABI] Fix non-virtual part of vtordispex this adjustment (#248414)

Commit
3 hours ago
[clang][MS ABI] Fix non-virtual part of vtordispex this adjustment (#248414) 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.
Author
Parents
Loading