https://github.com/zygoloid created https://github.com/llvm/llvm-project/pull/242501
In a few different places we were incorrectly computing the alignment of `this` when emitting thunks for virtual function calls, generally by picking up information from the derived type's this pointer, which in general might be more aligned than the this pointer that a thunk actually sees. Assisted-by: Antigravity with Gemini and Claude >From a9108e1ea7c7f131a0d654170056de6cf6607986 Mon Sep 17 00:00:00 2001 From: Richard Smith <[email protected]> Date: Wed, 11 Nov 2026 01:21:03 +0000 Subject: [PATCH] Fix incorrect `this` alignment in virtual function thunks. In a few different places we were incorrectly computing the alignment of `this` when emitting thunks for virtual function calls, generally by picking up information from the derived type's this pointer, which in general might be more aligned than the this pointer that a thunk actually sees. --- clang/lib/AST/VTableBuilder.cpp | 7 +++- clang/lib/CodeGen/CGVTables.cpp | 14 ++++++- clang/test/CodeGenCXX/thunks.cpp | 63 ++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/clang/lib/AST/VTableBuilder.cpp b/clang/lib/AST/VTableBuilder.cpp index 3c05d4b22b03e..b512820a72c8e 100644 --- a/clang/lib/AST/VTableBuilder.cpp +++ b/clang/lib/AST/VTableBuilder.cpp @@ -1575,10 +1575,13 @@ void ItaniumVTableBuilder::AddMethods( ReturnAdjustment ReturnAdjustment = ComputeReturnAdjustment(ReturnAdjustmentOffset); + const CXXMethodDecl *OrigMD = + VTables.findOriginalMethodInMap(OverriddenMD); + // This is a virtual thunk for the most derived class, add it. AddThunk(Overrider.Method, ThunkInfo(ThisAdjustment, ReturnAdjustment, - OverriddenMD->getThisType().getTypePtr())); + OrigMD->getThisType().getTypePtr(), OrigMD)); } } @@ -2378,7 +2381,7 @@ ItaniumVTableContext::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, } GlobalDecl ItaniumVTableContext::findOriginalMethod(GlobalDecl GD) { - const auto *MD = cast<CXXMethodDecl>(GD.getDecl()); + const auto *MD = cast<CXXMethodDecl>(GD.getDecl()->getCanonicalDecl()); computeVTableRelatedInformation(MD->getParent()); const CXXMethodDecl *OriginalMD = findOriginalMethodInMap(MD); diff --git a/clang/lib/CodeGen/CGVTables.cpp b/clang/lib/CodeGen/CGVTables.cpp index c5e16e8fe03fd..14609eade00a2 100644 --- a/clang/lib/CodeGen/CGVTables.cpp +++ b/clang/lib/CodeGen/CGVTables.cpp @@ -213,6 +213,18 @@ CodeGenFunction::GenerateVarArgsThunk(llvm::Function *Fn, // Ensure that the value mapper does not encounter any of them. resolveTopLevelMetadata(BaseFn, VMap); llvm::Function *NewFn = llvm::CloneFunction(BaseFn, VMap); + + // The clone inherited the callee's attributes. Keep its function attributes, + // which describe the cloned body, but take the return and parameter + // attributes from the thunk declaration. + llvm::AttributeList ThunkAttrs = Fn->getAttributes(); + SmallVector<llvm::AttributeSet, 8> ParamAttrs; + for (unsigned I = 0, N = NewFn->arg_size(); I != N; ++I) + ParamAttrs.push_back(ThunkAttrs.getParamAttrs(I)); + NewFn->setAttributes(llvm::AttributeList::get( + getLLVMContext(), NewFn->getAttributes().getFnAttrs(), + ThunkAttrs.getRetAttrs(), ParamAttrs)); + Fn->replaceAllUsesWith(NewFn); NewFn->takeName(Fn); Fn->eraseFromParent(); @@ -584,7 +596,7 @@ llvm::Constant *CodeGenVTables::maybeEmitThunk(GlobalDecl GD, OldThunkFn->setName(StringRef()); ThunkFn = llvm::Function::Create(ThunkFnTy, llvm::Function::ExternalLinkage, Name.str(), &CGM.getModule()); - CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn, /*IsThunk=*/false); + CGM.SetLLVMFunctionAttributes(MD, FnInfo, ThunkFn, /*IsThunk=*/true); if (!OldThunkFn->use_empty()) { OldThunkFn->replaceAllUsesWith(ThunkFn); diff --git a/clang/test/CodeGenCXX/thunks.cpp b/clang/test/CodeGenCXX/thunks.cpp index 3ab3476ccfd02..c85200df93515 100644 --- a/clang/test/CodeGenCXX/thunks.cpp +++ b/clang/test/CodeGenCXX/thunks.cpp @@ -464,6 +464,69 @@ void Derived::g(int x, ...) {} // CHECK-TAIL-NEXT: load ptr, ptr %[[THIS]], align 8 } +// Like Test18, but the thunk is created while building the vtable for an +// overaligned base B whose primary base is A. The vptr load should use the +// alignment of the class that introduced the vtable slot (A), not of the class +// whose vtable entry it overrode (B). StealA takes A as its primary base, so A +// is not B's primary base in D. +namespace Test19 { +struct A { virtual int f(); }; +struct alignas(32) B : virtual A { int f() override; }; +struct C : virtual B { int f() override; }; +struct StealA : virtual A {}; +struct D : virtual StealA, C { int f() override; }; +int D::f() { return 0; } +// CHECK-LABEL: define{{.*}} @_ZTv0_n24_N6Test191D1fEv( +// CHECK: %[[THIS:.*]] = load ptr, ptr %{{.*}}, align 8 +// CHECK-NEXT: load ptr, ptr %[[THIS]], align 8 +// CHECK: ret i32 +} // namespace Test19 + +// Same, for a variadic thunk. +namespace Test20 { +struct A { virtual void f(const char *, ...); }; +struct alignas(32) B : virtual A { void f(const char *, ...) override; }; +struct C : virtual B { void f(const char *, ...) override; }; +struct StealA : virtual A {}; +struct D : virtual StealA, C { void f(const char *, ...) override; }; +void D::f(const char *, ...) {} +// CHECK-CLONE-LABEL: define{{.*}} @_ZTv0_n24_N6Test201D1fEPKcz(ptr noundef %this, +// CHECK-CLONE: load ptr, ptr %this, align 8 +// CHECK-CLONE: ret void +// CHECK-TAIL-LABEL: define{{.*}} @_ZTv0_n24_N6Test201D1fEPKcz( +// CHECK-TAIL: %[[THIS:.*]] = load ptr, ptr %{{.*}}, align 8 +// CHECK-TAIL-NEXT: load ptr, ptr %[[THIS]], align 8 +// CHECK-TAIL: musttail call +} // namespace Test20 + +// A variadic thunk with a return adjustment is always emitted by cloning. The +// clone should not inherit the callee's attributes on 'this' or on the +// returned reference, which describe a Derived rather than a Base. +namespace Test22 { +struct V { virtual void v(); }; +struct Base { virtual Base &f(const char *, ...); }; +struct alignas(32) Derived : V, Base { Derived &f(const char *, ...) override; }; +Derived &Derived::f(const char *, ...) { return *this; } +// CHECK-LABEL: define{{.*}} noundef nonnull align 32 dereferenceable(16) ptr @_ZN6Test227Derived1fEPKcz(ptr noundef nonnull align 32 dereferenceable(16) %this, +// CHECK-LABEL: define{{.*}} noundef ptr @_ZTchn8_h8_N6Test227Derived1fEPKcz(ptr noundef %this, +// WIN64-LABEL: define{{.*}} noundef ptr @"?f@Derived@Test22@@QEAAAEAUBase@2@PEBDZZ"(ptr noundef %this, +} // namespace Test22 + +// If a declaration of the thunk with the wrong type already exists, we replace +// it. The replacement should not get 'this' or reference-return attributes +// that only hold for the target method. +namespace Test21 { +struct A { virtual int &f(); }; +struct alignas(32) B : virtual A { int &f() override; }; +void fake() __asm__("_ZTv0_n24_N6Test211B1fEv"); +void use() { fake(); } +int &B::f() { static int n; return n; } +// CHECK-LABEL: define{{.*}} noundef ptr @_ZTv0_n24_N6Test211B1fEv(ptr noundef %this) +// CHECK: %[[THIS:.*]] = load ptr, ptr %{{.*}}, align 8 +// CHECK-NEXT: load ptr, ptr %[[THIS]], align 8 +// CHECK: ret ptr +} // namespace Test21 + // Varargs non-covariant thunk test. // PR18098 namespace Test15 { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
