@@ -2335,7 +2335,39 @@ 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
+ }
2338
2368
llvm::ConstantInt *CodeGenModule::CreateKCFITypeId (QualType T) {
2369
+ if (getCodeGenOpts ().SanitizeCfiICallGeneralizePointers )
2370
+ T = GeneralizeFunctionType (getContext (), T);
2339
2371
if (auto *FnType = T->getAs <FunctionProtoType>())
2340
2372
T = getContext ().getFunctionType (
2341
2373
FnType->getReturnType (), FnType->getParamTypes (),
@@ -2348,6 +2380,8 @@ llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T) {
2348
2380
2349
2381
if (getCodeGenOpts ().SanitizeCfiICallNormalizeIntegers )
2350
2382
Out << " .normalized" ;
2383
+ if (getCodeGenOpts ().SanitizeCfiICallGeneralizePointers )
2384
+ Out << " .generalized" ;
2351
2385
2352
2386
return llvm::ConstantInt::get (Int32Ty,
2353
2387
static_cast <uint32_t >(llvm::xxHash64 (OutName)));
@@ -7880,38 +7914,6 @@ CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
7880
7914
return CreateMetadataIdentifierImpl (T, VirtualMetadataIdMap, " .virtual" );
7881
7915
}
7882
7916
7883
- // Generalize pointer types to a void pointer with the qualifiers of the
7884
- // originally pointed-to type, e.g. 'const char *' and 'char * const *'
7885
- // generalize to 'const void *' while 'char *' and 'const char **' generalize to
7886
- // 'void *'.
7887
- static QualType GeneralizeType (ASTContext &Ctx, QualType Ty) {
7888
- if (!Ty->isPointerType ())
7889
- return Ty;
7890
-
7891
- return Ctx.getPointerType (
7892
- QualType (Ctx.VoidTy ).withCVRQualifiers (
7893
- Ty->getPointeeType ().getCVRQualifiers ()));
7894
- }
7895
-
7896
- // Apply type generalization to a FunctionType's return and argument types
7897
- static QualType GeneralizeFunctionType (ASTContext &Ctx, QualType Ty) {
7898
- if (auto *FnType = Ty->getAs <FunctionProtoType>()) {
7899
- SmallVector<QualType, 8 > GeneralizedParams;
7900
- for (auto &Param : FnType->param_types ())
7901
- GeneralizedParams.push_back (GeneralizeType (Ctx, Param));
7902
-
7903
- return Ctx.getFunctionType (
7904
- GeneralizeType (Ctx, FnType->getReturnType ()),
7905
- GeneralizedParams, FnType->getExtProtoInfo ());
7906
- }
7907
-
7908
- if (auto *FnType = Ty->getAs <FunctionNoProtoType>())
7909
- return Ctx.getFunctionNoProtoType (
7910
- GeneralizeType (Ctx, FnType->getReturnType ()));
7911
-
7912
- llvm_unreachable (" Encountered unknown FunctionType" );
7913
- }
7914
-
7915
7917
llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized (QualType T) {
7916
7918
return CreateMetadataIdentifierImpl (GeneralizeFunctionType (getContext (), T),
7917
7919
GeneralizedMetadataIdMap, " .generalized" );
0 commit comments