Skip to content

Commit c66b915

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
1 parent 1a0121c commit c66b915

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,7 +2335,39 @@ 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+
}
23382368
llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T) {
2369+
if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
2370+
T = GeneralizeFunctionType(getContext(), T);
23392371
if (auto *FnType = T->getAs<FunctionProtoType>())
23402372
T = getContext().getFunctionType(
23412373
FnType->getReturnType(), FnType->getParamTypes(),
@@ -2348,6 +2380,8 @@ llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T) {
23482380

23492381
if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers)
23502382
Out << ".normalized";
2383+
if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
2384+
Out << ".generalized";
23512385

23522386
return llvm::ConstantInt::get(Int32Ty,
23532387
static_cast<uint32_t>(llvm::xxHash64(OutName)));
@@ -7880,38 +7914,6 @@ CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
78807914
return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual");
78817915
}
78827916

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-
79157917
llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) {
79167918
return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T),
79177919
GeneralizedMetadataIdMap, ".generalized");

0 commit comments

Comments
 (0)