From 650ff7879c79d6a9f4da5f1017e632deea9eb565 Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Thu, 11 Sep 2025 21:45:44 -0700 Subject: [PATCH 1/3] =?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 | 45 ++++++++++++++++++++--------- clang/lib/CodeGen/CodeGenModule.h | 3 ++ 3 files changed, 37 insertions(+), 18 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..c647003ff389d 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -2343,8 +2343,11 @@ llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) { // 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) { - if (!Ty->isPointerType()) +static QualType GeneralizeType(ASTContext &Ctx, QualType Ty, + bool GeneralizePointers) { + // TODO: Add other generalizations. + + if (!GeneralizePointers || !Ty->isPointerType()) return Ty; return Ctx.getPointerType( @@ -2353,26 +2356,29 @@ static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) { } // Apply type generalization to a FunctionType's return and argument types -static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) { +static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty, + bool GeneralizePointers) { if (auto *FnType = Ty->getAs()) { SmallVector GeneralizedParams; for (auto &Param : FnType->param_types()) - GeneralizedParams.push_back(GeneralizeType(Ctx, Param)); + GeneralizedParams.push_back( + GeneralizeType(Ctx, Param, GeneralizePointers)); - return Ctx.getFunctionType(GeneralizeType(Ctx, FnType->getReturnType()), - GeneralizedParams, FnType->getExtProtoInfo()); + return Ctx.getFunctionType( + GeneralizeType(Ctx, FnType->getReturnType(), GeneralizePointers), + GeneralizedParams, FnType->getExtProtoInfo()); } if (auto *FnType = Ty->getAs()) return Ctx.getFunctionNoProtoType( - GeneralizeType(Ctx, FnType->getReturnType())); + GeneralizeType(Ctx, FnType->getReturnType(), GeneralizePointers)); llvm_unreachable("Encountered unknown FunctionType"); } llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T, StringRef Salt) { - if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers) - T = GeneralizeFunctionType(getContext(), T); + T = GeneralizeFunctionType( + getContext(), T, getCodeGenOpts().SanitizeCfiICallGeneralizePointers); if (auto *FnType = T->getAs()) T = getContext().getFunctionType( FnType->getReturnType(), FnType->getParamTypes(), @@ -3041,9 +3047,13 @@ void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD, if (isa(FD) && !cast(FD)->isStatic()) return; - llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType()); + QualType FnType = GeneralizeFunctionType(getContext(), FD->getType(), + /*GeneralizePointers=*/false); + llvm::Metadata *MD = CreateMetadataIdentifierForType(FnType); F->addTypeMetadata(0, MD); - F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType())); + FnType = GeneralizeFunctionType(getContext(), FD->getType(), + /*GeneralizePointers=*/true); + F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FnType)); // Emit a hash-based bit set entry for cross-DSO calls. if (CodeGenOpts.SanitizeCfiCrossDso) @@ -7934,6 +7944,15 @@ CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map, return InternalId; } +llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForFnType(QualType T) { + assert(isa(T)); + T = GeneralizeFunctionType( + getContext(), T, getCodeGenOpts().SanitizeCfiICallGeneralizePointers); + if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers) + return CreateMetadataIdentifierGeneralized(T); + return CreateMetadataIdentifierForType(T); +} + llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) { return CreateMetadataIdentifierImpl(T, MetadataIdMap, ""); } @@ -7944,8 +7963,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 ad7bdb7658fe6a8b767242fcfa1df15fd17b1ae2 Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Thu, 11 Sep 2025 22:26:58 -0700 Subject: [PATCH 2/3] spaces Created using spr 1.3.6 --- clang/test/CodeGen/cfi-icall-generalize.c | 2 +- clang/test/CodeGen/cfi-icall-normalize2.c | 2 +- clang/test/CodeGen/kcfi-generalize.c | 2 +- clang/test/CodeGen/kcfi-normalize.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/clang/test/CodeGen/cfi-icall-generalize.c b/clang/test/CodeGen/cfi-icall-generalize.c index 3b5f915e33746..46d38511ba6b6 100644 --- a/clang/test/CodeGen/cfi-icall-generalize.c +++ b/clang/test/CodeGen/cfi-icall-generalize.c @@ -17,7 +17,7 @@ void g(int** (*fp)(const char *, const char **)) { union Union { char *c; - long* n; + long *n; } __attribute__((transparent_union)); // CHECK: define{{.*}} void @uni({{.*}} !type [[TYPE2:![0-9]+]] !type [[TYPE2_GENERALIZED:![0-9]+]] diff --git a/clang/test/CodeGen/cfi-icall-normalize2.c b/clang/test/CodeGen/cfi-icall-normalize2.c index 5bda0edc2aaeb..5e457dc97f0a2 100644 --- a/clang/test/CodeGen/cfi-icall-normalize2.c +++ b/clang/test/CodeGen/cfi-icall-normalize2.c @@ -26,7 +26,7 @@ void baz(void (*fn)(int, int, int), int arg1, int arg2, int arg3) { union Union { char *c; - long* n; + long *n; } __attribute__((transparent_union)); void uni(void (*fn)(union Union), union Union arg1) { diff --git a/clang/test/CodeGen/kcfi-generalize.c b/clang/test/CodeGen/kcfi-generalize.c index de9997eab6829..83805ba46d049 100644 --- a/clang/test/CodeGen/kcfi-generalize.c +++ b/clang/test/CodeGen/kcfi-generalize.c @@ -28,7 +28,7 @@ void g(int** (*fp)(const char *, const char **)) { union Union { char *c; - long* n; + long *n; } __attribute__((transparent_union)); // CHECK: define{{.*}} void @uni({{.*}} !kcfi_type [[TYPE2:![0-9]+]] diff --git a/clang/test/CodeGen/kcfi-normalize.c b/clang/test/CodeGen/kcfi-normalize.c index e01680d3645bd..9291ff8529b31 100644 --- a/clang/test/CodeGen/kcfi-normalize.c +++ b/clang/test/CodeGen/kcfi-normalize.c @@ -30,7 +30,7 @@ void baz(void (*fn)(int, int, int), int arg1, int arg2, int arg3) { union Union { char *c; - long* n; + long *n; } __attribute__((transparent_union)); void uni(void (*fn)(union Union), union Union arg1) { From 4c3ee31cbdc9806329b4491bdb3948ef269f878c Mon Sep 17 00:00:00 2001 From: Vitaly Buka Date: Thu, 11 Sep 2025 22:38:10 -0700 Subject: [PATCH 3/3] TYPE4 Created using spr 1.3.6 --- clang/test/CodeGen/kcfi-generalize.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/test/CodeGen/kcfi-generalize.c b/clang/test/CodeGen/kcfi-generalize.c index 83805ba46d049..864cdb8c2e092 100644 --- a/clang/test/CodeGen/kcfi-generalize.c +++ b/clang/test/CodeGen/kcfi-generalize.c @@ -31,7 +31,7 @@ union Union { long *n; } __attribute__((transparent_union)); -// CHECK: define{{.*}} void @uni({{.*}} !kcfi_type [[TYPE2:![0-9]+]] +// CHECK: define{{.*}} void @uni({{.*}} !kcfi_type [[TYPE4:![0-9]+]] void uni(void (*fn)(union Union), union Union arg1) { // UNGENERALIZED: call {{.*}} [ "kcfi"(i32 -1037059548) ] // GENERALIZED: call {{.*}} [ "kcfi"(i32 422130955) ] @@ -44,6 +44,6 @@ void uni(void (*fn)(union Union), union Union arg1) { // UNGENERALIZED: [[TYPE3]] = !{i32 874141567} // GENERALIZED: [[TYPE3]] = !{i32 954385378} -// UNGENERALIZED: [[TYPE2]] = !{i32 981319178} -// GENERALIZED: [[TYPE2]] = !{i32 -1599950473} +// UNGENERALIZED: [[TYPE4]] = !{i32 981319178} +// GENERALIZED: [[TYPE4]] = !{i32 -1599950473}