@@ -2335,7 +2335,40 @@ llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
23352335 return llvm::ConstantInt::get (Int64Ty, llvm::MD5Hash (MDS->getString ()));
23362336}
23372337
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+
23382369llvm::ConstantInt *CodeGenModule::CreateKCFITypeId (QualType T) {
2370+ if (getCodeGenOpts ().SanitizeCfiICallGeneralizePointers )
2371+ T = GeneralizeFunctionType (getContext (), T);
23392372 if (auto *FnType = T->getAs <FunctionProtoType>())
23402373 T = getContext ().getFunctionType (
23412374 FnType->getReturnType (), FnType->getParamTypes (),
@@ -2348,6 +2381,8 @@ llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T) {
23482381
23492382 if (getCodeGenOpts ().SanitizeCfiICallNormalizeIntegers )
23502383 Out << " .normalized" ;
2384+ if (getCodeGenOpts ().SanitizeCfiICallGeneralizePointers )
2385+ Out << " .generalized" ;
23512386
23522387 return llvm::ConstantInt::get (Int32Ty,
23532388 static_cast <uint32_t >(llvm::xxHash64 (OutName)));
@@ -7886,38 +7921,6 @@ CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
78867921 return CreateMetadataIdentifierImpl (T, VirtualMetadataIdMap, " .virtual" );
78877922}
78887923
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-
79217924llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized (QualType T) {
79227925 return CreateMetadataIdentifierImpl (GeneralizeFunctionType (getContext (), T),
79237926 GeneralizedMetadataIdMap, " .generalized" );
0 commit comments