[llvm-branch-commits] [NFC][CodeGen][CFI] Add GeneralizePointers parameter to GeneralizeFunctionType (PR #158191)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Sep 11 21:46:20 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-codegen
Author: Vitaly Buka (vitalybuka)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/158191.diff
1 Files Affected:
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+22-14)
``````````diff
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index acd77c5aca89c..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<FunctionProtoType>()) {
SmallVector<QualType, 8> 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<FunctionNoProtoType>())
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<FunctionProtoType>())
T = getContext().getFunctionType(
FnType->getReturnType(), FnType->getParamTypes(),
@@ -3041,10 +3047,12 @@ void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
return;
- QualType FnType = FD->getType();
+ QualType FnType = GeneralizeFunctionType(getContext(), FD->getType(),
+ /*GeneralizePointers=*/false);
llvm::Metadata *MD = CreateMetadataIdentifierForType(FnType);
F->addTypeMetadata(0, MD);
- FnType = GeneralizeFunctionType(getContext(), FnType);
+ FnType = GeneralizeFunctionType(getContext(), FD->getType(),
+ /*GeneralizePointers=*/true);
F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FnType));
// Emit a hash-based bit set entry for cross-DSO calls.
@@ -7938,10 +7946,10 @@ CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForFnType(QualType T) {
assert(isa<FunctionType>(T));
- if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers) {
- T = GeneralizeFunctionType(getContext(), T);
+ T = GeneralizeFunctionType(
+ getContext(), T, getCodeGenOpts().SanitizeCfiICallGeneralizePointers);
+ if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
return CreateMetadataIdentifierGeneralized(T);
- }
return CreateMetadataIdentifierForType(T);
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/158191
More information about the llvm-branch-commits
mailing list