-
Notifications
You must be signed in to change notification settings - Fork 15k
[NFC][CodeGen][CFI] Add GeneralizePointers parameter to GeneralizeFunctionType #158191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NFC][CodeGen][CFI] Add GeneralizePointers parameter to GeneralizeFunctionType #158191
Conversation
Created using spr 1.3.6 [skip ci]
Created using spr 1.3.6
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-codegen Author: Vitaly Buka (vitalybuka) ChangesFull diff: https://github.com/llvm/llvm-project/pull/158191.diff 1 Files Affected:
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);
}
|
Created using spr 1.3.6 [skip ci]
Created using spr 1.3.6 [skip ci]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a GeneralizePointers
parameter to the GeneralizeFunctionType
function to control whether pointer types should be generalized during CFI (Control Flow Integrity) type processing. This change supports issue #158193 by making pointer generalization configurable rather than relying solely on compiler options.
Key changes:
- Added
GeneralizePointers
boolean parameter to bothGeneralizeType
andGeneralizeFunctionType
functions - Updated all call sites to explicitly pass the generalization flag
- Simplified conditional logic in
CreateKCFITypeId
andCreateMetadataIdentifierForFnType
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/190/builds/27353 Here is the relevant piece of the build log for the reference
|
For #158193