Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5bff94

Browse files
[NFC][CodeGen][CFI] Add GeneralizePointers parameter to GeneralizeFunctionType (llvm#158191)
For llvm#158193 --------- Co-authored-by: Alex Langford <[email protected]>
1 parent 1cbdb73 commit a5bff94

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,12 +2339,15 @@ llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
23392339
return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
23402340
}
23412341

2342-
// Generalize pointer types to a void pointer with the qualifiers of the
2343-
// originally pointed-to type, e.g. 'const char *' and 'char * const *'
2344-
// generalize to 'const void *' while 'char *' and 'const char **' generalize to
2345-
// 'void *'.
2346-
static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) {
2347-
if (!Ty->isPointerType())
2342+
// If `GeneralizePointers` is true, generalizes types to a void pointer with the
2343+
// qualifiers of the originally pointed-to type, e.g. 'const char *' and 'char *
2344+
// const *' generalize to 'const void *' while 'char *' and 'const char **'
2345+
// generalize to 'void *'.
2346+
static QualType GeneralizeType(ASTContext &Ctx, QualType Ty,
2347+
bool GeneralizePointers) {
2348+
// TODO: Add other generalizations.
2349+
2350+
if (!GeneralizePointers || !Ty->isPointerType())
23482351
return Ty;
23492352

23502353
return Ctx.getPointerType(
@@ -2353,26 +2356,29 @@ static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) {
23532356
}
23542357

23552358
// Apply type generalization to a FunctionType's return and argument types
2356-
static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) {
2359+
static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty,
2360+
bool GeneralizePointers) {
23572361
if (auto *FnType = Ty->getAs<FunctionProtoType>()) {
23582362
SmallVector<QualType, 8> GeneralizedParams;
23592363
for (auto &Param : FnType->param_types())
2360-
GeneralizedParams.push_back(GeneralizeType(Ctx, Param));
2364+
GeneralizedParams.push_back(
2365+
GeneralizeType(Ctx, Param, GeneralizePointers));
23612366

2362-
return Ctx.getFunctionType(GeneralizeType(Ctx, FnType->getReturnType()),
2363-
GeneralizedParams, FnType->getExtProtoInfo());
2367+
return Ctx.getFunctionType(
2368+
GeneralizeType(Ctx, FnType->getReturnType(), GeneralizePointers),
2369+
GeneralizedParams, FnType->getExtProtoInfo());
23642370
}
23652371

23662372
if (auto *FnType = Ty->getAs<FunctionNoProtoType>())
23672373
return Ctx.getFunctionNoProtoType(
2368-
GeneralizeType(Ctx, FnType->getReturnType()));
2374+
GeneralizeType(Ctx, FnType->getReturnType(), GeneralizePointers));
23692375

23702376
llvm_unreachable("Encountered unknown FunctionType");
23712377
}
23722378

23732379
llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T, StringRef Salt) {
2374-
if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
2375-
T = GeneralizeFunctionType(getContext(), T);
2380+
T = GeneralizeFunctionType(
2381+
getContext(), T, getCodeGenOpts().SanitizeCfiICallGeneralizePointers);
23762382
if (auto *FnType = T->getAs<FunctionProtoType>())
23772383
T = getContext().getFunctionType(
23782384
FnType->getReturnType(), FnType->getParamTypes(),
@@ -3041,11 +3047,13 @@ void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
30413047
if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
30423048
return;
30433049

3044-
QualType FnType = FD->getType();
3050+
QualType FnType = GeneralizeFunctionType(getContext(), FD->getType(),
3051+
/*GeneralizePointers=*/false);
30453052
llvm::Metadata *MD = CreateMetadataIdentifierForType(FnType);
30463053
F->addTypeMetadata(0, MD);
30473054

3048-
QualType GenPtrFnType = GeneralizeFunctionType(getContext(), FD->getType());
3055+
QualType GenPtrFnType = GeneralizeFunctionType(getContext(), FD->getType(),
3056+
/*GeneralizePointers=*/true);
30493057
F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(GenPtrFnType));
30503058

30513059
// Emit a hash-based bit set entry for cross-DSO calls.
@@ -7939,10 +7947,10 @@ CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
79397947

79407948
llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForFnType(QualType T) {
79417949
assert(isa<FunctionType>(T));
7942-
if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers) {
7943-
T = GeneralizeFunctionType(getContext(), T);
7950+
T = GeneralizeFunctionType(
7951+
getContext(), T, getCodeGenOpts().SanitizeCfiICallGeneralizePointers);
7952+
if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
79447953
return CreateMetadataIdentifierGeneralized(T);
7945-
}
79467954
return CreateMetadataIdentifierForType(T);
79477955
}
79487956

0 commit comments

Comments
 (0)