From 77240d317127980250d6851cf22f20b021455b51 Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Thu, 11 Sep 2025 21:45:40 -0700 Subject: [PATCH 1/2] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20ch?= =?UTF-8?q?anges=20to=20main=20this=20commit=20is=20based=20on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.6 [skip ci] --- clang/lib/CodeGen/CGExpr.cpp | 7 ++----- clang/lib/CodeGen/CodeGenModule.cpp | 19 +++++++++++++++---- clang/lib/CodeGen/CodeGenModule.h | 3 +++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index e8456a44f8367..e6e4947882544 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -6496,11 +6496,8 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType, SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler); EmitSanitizerStatReport(llvm::SanStat_CFI_ICall); - llvm::Metadata *MD; - if (CGM.getCodeGenOpts().SanitizeCfiICallGeneralizePointers) - MD = CGM.CreateMetadataIdentifierGeneralized(QualType(FnType, 0)); - else - MD = CGM.CreateMetadataIdentifierForType(QualType(FnType, 0)); + llvm::Metadata *MD = + CGM.CreateMetadataIdentifierForFnType(QualType(FnType, 0)); llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD); diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index a16dfb52f4d90..acd77c5aca89c 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -3041,9 +3041,11 @@ void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD, if (isa(FD) && !cast(FD)->isStatic()) return; - llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType()); + QualType FnType = FD->getType(); + llvm::Metadata *MD = CreateMetadataIdentifierForType(FnType); F->addTypeMetadata(0, MD); - F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType())); + FnType = GeneralizeFunctionType(getContext(), FnType); + F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FnType)); // Emit a hash-based bit set entry for cross-DSO calls. if (CodeGenOpts.SanitizeCfiCrossDso) @@ -7934,6 +7936,15 @@ CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map, return InternalId; } +llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForFnType(QualType T) { + assert(isa(T)); + if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers) { + T = GeneralizeFunctionType(getContext(), T); + return CreateMetadataIdentifierGeneralized(T); + } + return CreateMetadataIdentifierForType(T); +} + llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) { return CreateMetadataIdentifierImpl(T, MetadataIdMap, ""); } @@ -7944,8 +7955,8 @@ CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) { } llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) { - return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T), - GeneralizedMetadataIdMap, ".generalized"); + return CreateMetadataIdentifierImpl(T, GeneralizedMetadataIdMap, + ".generalized"); } /// Returns whether this module needs the "all-vtables" type identifier. diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index f62350fd8d378..8b1ac2d976c5e 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -1623,6 +1623,9 @@ class CodeGenModule : public CodeGenTypeCache { /// Generate a KCFI type identifier for T. llvm::ConstantInt *CreateKCFITypeId(QualType T, StringRef Salt); + /// Create a metadata identifier for the given function type. + llvm::Metadata *CreateMetadataIdentifierForFnType(QualType T); + /// Create a metadata identifier for the given type. This may either be an /// MDString (for external identifiers) or a distinct unnamed MDNode (for /// internal identifiers). From cf2ea747a549d25b88fdb87c23f9e276b7a64691 Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Fri, 12 Sep 2025 16:48:47 -0700 Subject: [PATCH 2/2] comment Created using spr 1.3.6 --- clang/lib/CodeGen/CodeGenModule.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 823bdcc309c0f..d25ce3165bd79 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2339,10 +2339,10 @@ llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) { return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString())); } -// Generalize pointer types to a void pointer with the qualifiers of the -// originally pointed-to type, e.g. 'const char *' and 'char * const *' -// generalize to 'const void *' while 'char *' and 'const char **' generalize to -// 'void *'. +// If `GeneralizePointers` is true, generalizes types to a void pointer with the +// qualifiers of the originally pointed-to type, e.g. 'const char *' and 'char * +// const *' generalize to 'const void *' while 'char *' and 'const char **' +// generalize to 'void *'. static QualType GeneralizeType(ASTContext &Ctx, QualType Ty, bool GeneralizePointers) { // TODO: Add other generalizations.