@@ -2343,8 +2343,11 @@ llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
23432343// originally pointed-to type, e.g. 'const char *' and 'char * const *'
23442344// generalize to 'const void *' while 'char *' and 'const char **' generalize to
23452345// 'void *'.
2346- static QualType GeneralizeType (ASTContext &Ctx, QualType Ty) {
2347- if (!Ty->isPointerType ())
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
23732379llvm::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,9 +3047,13 @@ void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
30413047 if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic ())
30423048 return ;
30433049
3044- llvm::Metadata *MD = CreateMetadataIdentifierForType (FD->getType ());
3050+ QualType FnType = GeneralizeFunctionType (getContext (), FD->getType (),
3051+ /* GeneralizePointers=*/ false );
3052+ llvm::Metadata *MD = CreateMetadataIdentifierForType (FnType);
30453053 F->addTypeMetadata (0 , MD);
3046- F->addTypeMetadata (0 , CreateMetadataIdentifierGeneralized (FD->getType ()));
3054+ FnType = GeneralizeFunctionType (getContext (), FD->getType (),
3055+ /* GeneralizePointers=*/ true );
3056+ F->addTypeMetadata (0 , CreateMetadataIdentifierGeneralized (FnType));
30473057
30483058 // Emit a hash-based bit set entry for cross-DSO calls.
30493059 if (CodeGenOpts.SanitizeCfiCrossDso )
@@ -7934,6 +7944,15 @@ CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
79347944 return InternalId;
79357945}
79367946
7947+ llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForFnType (QualType T) {
7948+ assert (isa<FunctionType>(T));
7949+ T = GeneralizeFunctionType (
7950+ getContext (), T, getCodeGenOpts ().SanitizeCfiICallGeneralizePointers );
7951+ if (getCodeGenOpts ().SanitizeCfiICallGeneralizePointers )
7952+ return CreateMetadataIdentifierGeneralized (T);
7953+ return CreateMetadataIdentifierForType (T);
7954+ }
7955+
79377956llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType (QualType T) {
79387957 return CreateMetadataIdentifierImpl (T, MetadataIdMap, " " );
79397958}
@@ -7944,8 +7963,8 @@ CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
79447963}
79457964
79467965llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized (QualType T) {
7947- return CreateMetadataIdentifierImpl (GeneralizeFunctionType ( getContext (), T) ,
7948- GeneralizedMetadataIdMap, " .generalized" );
7966+ return CreateMetadataIdentifierImpl (T, GeneralizedMetadataIdMap ,
7967+ " .generalized" );
79497968}
79507969
79517970// / Returns whether this module needs the "all-vtables" type identifier.
0 commit comments