[clang] [NFC][Clang][CFI] Extract EmitVTableCheck helper (PR #241636)

Vitaly Buka via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 19:57:01 PST 2026


https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/241636

>From 8cffc2fc4b43b859be1fac8692f16ce73274cfc3 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Wed, 11 Nov 2026 19:04:57 -0800
Subject: [PATCH] [spr] update

Created using spr 2.0.0-beta.2
---
 clang/lib/CodeGen/CGClass.cpp       | 26 +++++++++++++++++---------
 clang/lib/CodeGen/CodeGenFunction.h |  5 +++++
 2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp
index 514bed6dddaaa..95e050fc9b618 100644
--- a/clang/lib/CodeGen/CGClass.cpp
+++ b/clang/lib/CodeGen/CGClass.cpp
@@ -2922,30 +2922,38 @@ void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD,
       !CGM.HasHiddenLTOVisibility(RD))
     return;
 
-  auto [M, SSK] = SanitizerInfoFromCFICheckKind(TCK);
-
-  std::string TypeName = RD->getQualifiedNameAsString();
+  auto [M, _] = SanitizerInfoFromCFICheckKind(TCK);
   if (getContext().getNoSanitizeList().containsType(
-          SanitizerMask::bitPosToMask(M), TypeName))
+          SanitizerMask::bitPosToMask(M), RD->getQualifiedNameAsString()))
     return;
 
-  EmitSanitizerStatReport(SSK);
-
   CanQualType T = CGM.getContext().getCanonicalTagType(RD);
   llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(T);
   llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD);
-
   llvm::Value *TypeTest = Builder.CreateCall(
       CGM.getIntrinsic(llvm::Intrinsic::type_test), {VTable, TypeId});
 
+  EmitVTableCheck(TCK, T, MD, VTable, TypeTest, Loc,
+                  CGM.getCodeGenOpts().SanitizeCfiCrossDso);
+}
+
+void CodeGenFunction::EmitVTableCheck(CFITypeCheckKind TCK, CanQualType T,
+                                      llvm::Metadata *MD, llvm::Value *VTable,
+                                      llvm::Value *TypeTest, SourceLocation Loc,
+                                      bool UseCrossDsoSlowPath) {
+  assert(IsSanitizerScope);
+  auto [M, SSK] = SanitizerInfoFromCFICheckKind(TCK);
+  EmitSanitizerStatReport(SSK);
+
   llvm::Constant *StaticData[] = {
       llvm::ConstantInt::get(Int8Ty, TCK),
       EmitCheckSourceLocation(Loc),
       EmitCheckTypeDescriptor(T),
   };
 
-  auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD);
-  if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) {
+  llvm::ConstantInt *CrossDsoTypeId =
+      UseCrossDsoSlowPath ? CGM.CreateCrossDsoCfiTypeId(MD) : nullptr;
+  if (CrossDsoTypeId) {
     EmitCfiSlowPathCheck(M, TypeTest, CrossDsoTypeId, VTable, StaticData);
     return;
   }
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index 2c7820ec57225..79ddc79ff6c34 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -2640,6 +2640,11 @@ class CodeGenFunction : public CodeGenTypeCache {
   void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable,
                           CFITypeCheckKind TCK, SourceLocation Loc);
 
+  /// Emit a CFI check failure handler or trap for a vtable type test result.
+  void EmitVTableCheck(CFITypeCheckKind TCK, CanQualType T, llvm::Metadata *MD,
+                       llvm::Value *VTable, llvm::Value *TypeTest,
+                       SourceLocation Loc, bool UseCrossDsoSlowPath);
+
   /// If whole-program virtual table optimization is enabled, emit an assumption
   /// that VTable is a member of RD's type identifier. Or, if vptr CFI is
   /// enabled, emit a check that VTable is a member of RD's type identifier.



More information about the cfe-commits mailing list