@@ -2335,7 +2335,40 @@ llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
2335
2335
return llvm::ConstantInt::get (Int64Ty, llvm::MD5Hash (MDS->getString ()));
2336
2336
}
2337
2337
2338
+ // Generalize pointer types to a void pointer with the qualifiers of the
2339
+ // originally pointed-to type, e.g. 'const char *' and 'char * const *'
2340
+ // generalize to 'const void *' while 'char *' and 'const char **' generalize to
2341
+ // 'void *'.
2342
+ static QualType GeneralizeType (ASTContext &Ctx, QualType Ty) {
2343
+ if (!Ty->isPointerType ())
2344
+ return Ty;
2345
+
2346
+ return Ctx.getPointerType (
2347
+ QualType (Ctx.VoidTy )
2348
+ .withCVRQualifiers (Ty->getPointeeType ().getCVRQualifiers ()));
2349
+ }
2350
+
2351
+ // Apply type generalization to a FunctionType's return and argument types
2352
+ static QualType GeneralizeFunctionType (ASTContext &Ctx, QualType Ty) {
2353
+ if (auto *FnType = Ty->getAs <FunctionProtoType>()) {
2354
+ SmallVector<QualType, 8 > GeneralizedParams;
2355
+ for (auto &Param : FnType->param_types ())
2356
+ GeneralizedParams.push_back (GeneralizeType (Ctx, Param));
2357
+
2358
+ return Ctx.getFunctionType (GeneralizeType (Ctx, FnType->getReturnType ()),
2359
+ GeneralizedParams, FnType->getExtProtoInfo ());
2360
+ }
2361
+
2362
+ if (auto *FnType = Ty->getAs <FunctionNoProtoType>())
2363
+ return Ctx.getFunctionNoProtoType (
2364
+ GeneralizeType (Ctx, FnType->getReturnType ()));
2365
+
2366
+ llvm_unreachable (" Encountered unknown FunctionType" );
2367
+ }
2368
+
2338
2369
llvm::ConstantInt *CodeGenModule::CreateKCFITypeId (QualType T) {
2370
+ if (getCodeGenOpts ().SanitizeCfiICallGeneralizePointers )
2371
+ T = GeneralizeFunctionType (getContext (), T);
2339
2372
if (auto *FnType = T->getAs <FunctionProtoType>())
2340
2373
T = getContext ().getFunctionType (
2341
2374
FnType->getReturnType (), FnType->getParamTypes (),
@@ -2348,6 +2381,8 @@ llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T) {
2348
2381
2349
2382
if (getCodeGenOpts ().SanitizeCfiICallNormalizeIntegers )
2350
2383
Out << " .normalized" ;
2384
+ if (getCodeGenOpts ().SanitizeCfiICallGeneralizePointers )
2385
+ Out << " .generalized" ;
2351
2386
2352
2387
return llvm::ConstantInt::get (Int32Ty,
2353
2388
static_cast <uint32_t >(llvm::xxHash64 (OutName)));
@@ -7886,38 +7921,6 @@ CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
7886
7921
return CreateMetadataIdentifierImpl (T, VirtualMetadataIdMap, " .virtual" );
7887
7922
}
7888
7923
7889
- // Generalize pointer types to a void pointer with the qualifiers of the
7890
- // originally pointed-to type, e.g. 'const char *' and 'char * const *'
7891
- // generalize to 'const void *' while 'char *' and 'const char **' generalize to
7892
- // 'void *'.
7893
- static QualType GeneralizeType (ASTContext &Ctx, QualType Ty) {
7894
- if (!Ty->isPointerType ())
7895
- return Ty;
7896
-
7897
- return Ctx.getPointerType (
7898
- QualType (Ctx.VoidTy ).withCVRQualifiers (
7899
- Ty->getPointeeType ().getCVRQualifiers ()));
7900
- }
7901
-
7902
- // Apply type generalization to a FunctionType's return and argument types
7903
- static QualType GeneralizeFunctionType (ASTContext &Ctx, QualType Ty) {
7904
- if (auto *FnType = Ty->getAs <FunctionProtoType>()) {
7905
- SmallVector<QualType, 8 > GeneralizedParams;
7906
- for (auto &Param : FnType->param_types ())
7907
- GeneralizedParams.push_back (GeneralizeType (Ctx, Param));
7908
-
7909
- return Ctx.getFunctionType (
7910
- GeneralizeType (Ctx, FnType->getReturnType ()),
7911
- GeneralizedParams, FnType->getExtProtoInfo ());
7912
- }
7913
-
7914
- if (auto *FnType = Ty->getAs <FunctionNoProtoType>())
7915
- return Ctx.getFunctionNoProtoType (
7916
- GeneralizeType (Ctx, FnType->getReturnType ()));
7917
-
7918
- llvm_unreachable (" Encountered unknown FunctionType" );
7919
- }
7920
-
7921
7924
llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized (QualType T) {
7922
7925
return CreateMetadataIdentifierImpl (GeneralizeFunctionType (getContext (), T),
7923
7926
GeneralizedMetadataIdMap, " .generalized" );
0 commit comments