@@ -2339,12 +2339,15 @@ llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
2339
2339
return llvm::ConstantInt::get (Int64Ty, llvm::MD5Hash (MDS->getString ()));
2340
2340
}
2341
2341
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 ())
2348
2351
return Ty;
2349
2352
2350
2353
return Ctx.getPointerType (
@@ -2353,26 +2356,29 @@ static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) {
2353
2356
}
2354
2357
2355
2358
// 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) {
2357
2361
if (auto *FnType = Ty->getAs <FunctionProtoType>()) {
2358
2362
SmallVector<QualType, 8 > GeneralizedParams;
2359
2363
for (auto &Param : FnType->param_types ())
2360
- GeneralizedParams.push_back (GeneralizeType (Ctx, Param));
2364
+ GeneralizedParams.push_back (
2365
+ GeneralizeType (Ctx, Param, GeneralizePointers));
2361
2366
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 ());
2364
2370
}
2365
2371
2366
2372
if (auto *FnType = Ty->getAs <FunctionNoProtoType>())
2367
2373
return Ctx.getFunctionNoProtoType (
2368
- GeneralizeType (Ctx, FnType->getReturnType ()));
2374
+ GeneralizeType (Ctx, FnType->getReturnType (), GeneralizePointers ));
2369
2375
2370
2376
llvm_unreachable (" Encountered unknown FunctionType" );
2371
2377
}
2372
2378
2373
2379
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 );
2376
2382
if (auto *FnType = T->getAs <FunctionProtoType>())
2377
2383
T = getContext ().getFunctionType (
2378
2384
FnType->getReturnType (), FnType->getParamTypes (),
@@ -3041,11 +3047,13 @@ void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
3041
3047
if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic ())
3042
3048
return ;
3043
3049
3044
- QualType FnType = FD->getType ();
3050
+ QualType FnType = GeneralizeFunctionType (getContext (), FD->getType (),
3051
+ /* GeneralizePointers=*/ false );
3045
3052
llvm::Metadata *MD = CreateMetadataIdentifierForType (FnType);
3046
3053
F->addTypeMetadata (0 , MD);
3047
3054
3048
- QualType GenPtrFnType = GeneralizeFunctionType (getContext (), FD->getType ());
3055
+ QualType GenPtrFnType = GeneralizeFunctionType (getContext (), FD->getType (),
3056
+ /* GeneralizePointers=*/ true );
3049
3057
F->addTypeMetadata (0 , CreateMetadataIdentifierGeneralized (GenPtrFnType));
3050
3058
3051
3059
// Emit a hash-based bit set entry for cross-DSO calls.
@@ -7939,10 +7947,10 @@ CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
7939
7947
7940
7948
llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForFnType (QualType T) {
7941
7949
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 )
7944
7953
return CreateMetadataIdentifierGeneralized (T);
7945
- }
7946
7954
return CreateMetadataIdentifierForType (T);
7947
7955
}
7948
7956
0 commit comments