[llvm-branch-commits] [clang] [Clang][CFI] Check ObjectWithVPtr in ShouldEmitVTableTypeCheckedLoad (PR #241639)
Vitaly Buka via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 11 23:24:01 PST 2026
https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/241639
>From a2459c3791516cfa6571d8e8c2c00206b6199a40 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Wed, 11 Nov 2026 19:05:18 -0800
Subject: [PATCH] [spr] update
Created using spr 2.0.0-beta.2
---
clang/lib/CodeGen/MicrosoftCXXABI.cpp | 55 +++++++++----------
.../CodeGenCXX/cfi-ms-secondary-vfptr.cpp | 32 +++++------
2 files changed, 40 insertions(+), 47 deletions(-)
diff --git a/clang/lib/CodeGen/MicrosoftCXXABI.cpp b/clang/lib/CodeGen/MicrosoftCXXABI.cpp
index 7a95ca2f50fdd..e7aac1530637c 100644
--- a/clang/lib/CodeGen/MicrosoftCXXABI.cpp
+++ b/clang/lib/CodeGen/MicrosoftCXXABI.cpp
@@ -2031,37 +2031,36 @@ CGCallee MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF,
MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext();
MethodVFTableLocation ML = VFTContext.getMethodVFTableLocation(GD);
- // Compute the identity of the most derived class whose virtual table is
- // located at the MethodVFTableLocation ML.
- auto getObjectWithVPtr = [&] {
- return llvm::find_if(VFTContext.getVFPtrOffsets(
- ML.VBase ? ML.VBase : MethodDecl->getParent()),
- [&](const std::unique_ptr<VPtrInfo> &Info) {
- return Info->FullOffsetInMDC == ML.VFPtrOffset;
- })
- ->get()
- ->ObjectWithVPtr;
- };
-
- llvm::Value *VFunc;
- if (CGF.ShouldEmitVTableTypeCheckedLoad(MethodDecl->getParent())) {
- VFunc = CGF.EmitVTableTypeCheckedLoad(
- getObjectWithVPtr(), VTable, Ty,
- ML.Index *
- CGM.getContext().getTargetInfo().getPointerWidth(LangAS::Default) /
- 8,
- Loc);
- } else {
- if (CGM.getCodeGenOpts().PrepareForLTO)
- CGF.EmitTypeMetadataCodeForVCall(getObjectWithVPtr(), VTable, Loc);
+ if (CGM.getCodeGenOpts().PrepareForLTO) {
+ // Compute the identity of the most derived class whose virtual table is
+ // located at the MethodVFTableLocation ML.
+ const CXXRecordDecl *ObjectWithVPtr =
+ llvm::find_if(VFTContext.getVFPtrOffsets(
+ ML.VBase ? ML.VBase : MethodDecl->getParent()),
+ [&](const std::unique_ptr<VPtrInfo> &Info) {
+ return Info->FullOffsetInMDC == ML.VFPtrOffset;
+ })
+ ->get()
+ ->ObjectWithVPtr;
+ if (CGF.ShouldEmitVTableTypeCheckedLoad(ObjectWithVPtr)) {
+ llvm::Value *VFunc = CGF.EmitVTableTypeCheckedLoad(
+ ObjectWithVPtr, VTable, Ty,
+ ML.Index *
+ CGM.getContext().getTargetInfo().getPointerWidth(
+ LangAS::Default) /
+ 8,
+ Loc);
+ return CGCallee(GD, VFunc);
+ }
- llvm::Value *VFuncPtr =
- Builder.CreateConstInBoundsGEP1_64(Ty, VTable, ML.Index, "vfn");
- VFunc = Builder.CreateAlignedLoad(Ty, VFuncPtr, CGF.getPointerAlign());
+ CGF.EmitTypeMetadataCodeForVCall(ObjectWithVPtr, VTable, Loc);
}
- CGCallee Callee(GD, VFunc);
- return Callee;
+ llvm::Value *VFuncPtr =
+ Builder.CreateConstInBoundsGEP1_64(Ty, VTable, ML.Index, "vfn");
+ llvm::Value *VFunc =
+ Builder.CreateAlignedLoad(Ty, VFuncPtr, CGF.getPointerAlign());
+ return CGCallee(GD, VFunc);
}
llvm::Value *MicrosoftCXXABI::EmitVirtualDestructorCall(
diff --git a/clang/test/CodeGenCXX/cfi-ms-secondary-vfptr.cpp b/clang/test/CodeGenCXX/cfi-ms-secondary-vfptr.cpp
index f0f8864c0302e..b28ceaf896b09 100644
--- a/clang/test/CodeGenCXX/cfi-ms-secondary-vfptr.cpp
+++ b/clang/test/CodeGenCXX/cfi-ms-secondary-vfptr.cpp
@@ -30,15 +30,9 @@ struct DerivedFromHidden : Primary, HiddenSec {
// CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[TMP0]], i64 8
// CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i8, ptr [[TMP0]], i64 8
// CHECK-NEXT: [[VTABLE:%.*]] = load ptr, ptr [[TMP2]], align 8
-// CHECK-NEXT: [[TMP3:%.*]] = call { ptr, i1 } @llvm.type.checked.load(ptr [[VTABLE]], i32 0, metadata !"?AUPublic@@"), !nosanitize [[META9:![0-9]+]]
-// CHECK-NEXT: [[TMP4:%.*]] = extractvalue { ptr, i1 } [[TMP3]], 1, !nosanitize [[META9]]
-// CHECK-NEXT: br i1 [[TMP4]], label %[[CONT:.*]], label %[[TRAP:.*]], !prof [[PROF10:![0-9]+]], !nosanitize [[META9]]
-// CHECK: [[TRAP]]:
-// CHECK-NEXT: call void @llvm.ubsantrap(i8 2) #[[ATTR3:[0-9]+]], !nosanitize [[META9]]
-// CHECK-NEXT: unreachable, !nosanitize [[META9]]
-// CHECK: [[CONT]]:
-// CHECK-NEXT: [[TMP5:%.*]] = extractvalue { ptr, i1 } [[TMP3]], 0, !nosanitize [[META9]]
-// CHECK-NEXT: call void [[TMP5]](ptr noundef [[TMP1]])
+// CHECK-NEXT: [[VFN:%.*]] = getelementptr inbounds ptr, ptr [[VTABLE]], i64 0
+// CHECK-NEXT: [[TMP3:%.*]] = load ptr, ptr [[VFN]], align 8
+// CHECK-NEXT: call void [[TMP3]](ptr noundef [[TMP1]])
// CHECK-NEXT: ret void
//
extern "C" void call_public_sec(DerivedFromPublic *p) {
@@ -46,7 +40,7 @@ extern "C" void call_public_sec(DerivedFromPublic *p) {
}
// CHECK-LABEL: define dso_local void @call_hidden_sec(
-// CHECK-SAME: ptr noundef [[P:%.*]]) #[[ATTR0]] !guid [[META11:![0-9]+]] {
+// CHECK-SAME: ptr noundef [[P:%.*]]) #[[ATTR0]] !guid [[META9:![0-9]+]] {
// CHECK-NEXT: [[ENTRY:.*:]]
// CHECK-NEXT: [[P_ADDR:%.*]] = alloca ptr, align 8
// CHECK-NEXT: store ptr [[P]], ptr [[P_ADDR]], align 8
@@ -54,14 +48,14 @@ extern "C" void call_public_sec(DerivedFromPublic *p) {
// CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[TMP0]], i64 8
// CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i8, ptr [[TMP0]], i64 8
// CHECK-NEXT: [[VTABLE:%.*]] = load ptr, ptr [[TMP2]], align 8
-// CHECK-NEXT: [[TMP3:%.*]] = call { ptr, i1 } @llvm.type.checked.load(ptr [[VTABLE]], i32 0, metadata !"?AUHiddenSec@@"), !nosanitize [[META9]]
-// CHECK-NEXT: [[TMP4:%.*]] = extractvalue { ptr, i1 } [[TMP3]], 1, !nosanitize [[META9]]
-// CHECK-NEXT: br i1 [[TMP4]], label %[[CONT:.*]], label %[[TRAP:.*]], !prof [[PROF10]], !nosanitize [[META9]]
+// CHECK-NEXT: [[TMP3:%.*]] = call { ptr, i1 } @llvm.type.checked.load(ptr [[VTABLE]], i32 0, metadata !"?AUHiddenSec@@"), !nosanitize [[META10:![0-9]+]]
+// CHECK-NEXT: [[TMP4:%.*]] = extractvalue { ptr, i1 } [[TMP3]], 1, !nosanitize [[META10]]
+// CHECK-NEXT: br i1 [[TMP4]], label %[[CONT:.*]], label %[[TRAP:.*]], !prof [[PROF11:![0-9]+]], !nosanitize [[META10]]
// CHECK: [[TRAP]]:
-// CHECK-NEXT: call void @llvm.ubsantrap(i8 2) #[[ATTR3]], !nosanitize [[META9]]
-// CHECK-NEXT: unreachable, !nosanitize [[META9]]
+// CHECK-NEXT: call void @llvm.ubsantrap(i8 2) #[[ATTR3:[0-9]+]], !nosanitize [[META10]]
+// CHECK-NEXT: unreachable, !nosanitize [[META10]]
// CHECK: [[CONT]]:
-// CHECK-NEXT: [[TMP5:%.*]] = extractvalue { ptr, i1 } [[TMP3]], 0, !nosanitize [[META9]]
+// CHECK-NEXT: [[TMP5:%.*]] = extractvalue { ptr, i1 } [[TMP3]], 0, !nosanitize [[META10]]
// CHECK-NEXT: call void [[TMP5]](ptr noundef [[TMP1]])
// CHECK-NEXT: ret void
//
@@ -70,7 +64,7 @@ extern "C" void call_hidden_sec(DerivedFromHidden *p) {
}
//.
// CHECK: [[META8]] = !{i64 -5210807322846835646}
-// CHECK: [[META9]] = !{}
-// CHECK: [[PROF10]] = !{!"branch_weights", i32 1048575, i32 1}
-// CHECK: [[META11]] = !{i64 702925180092474428}
+// CHECK: [[META9]] = !{i64 702925180092474428}
+// CHECK: [[META10]] = !{}
+// CHECK: [[PROF11]] = !{!"branch_weights", i32 1048575, i32 1}
//.
More information about the llvm-branch-commits
mailing list