Skip to content

Commit e3a587c

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6
2 parents 0e3c556 + 77240d3 commit e3a587c

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6496,11 +6496,8 @@ RValue CodeGenFunction::EmitCall(QualType CalleeType,
64966496
SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
64976497
EmitSanitizerStatReport(llvm::SanStat_CFI_ICall);
64986498

6499-
llvm::Metadata *MD;
6500-
if (CGM.getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
6501-
MD = CGM.CreateMetadataIdentifierGeneralized(QualType(FnType, 0));
6502-
else
6503-
MD = CGM.CreateMetadataIdentifierForType(QualType(FnType, 0));
6499+
llvm::Metadata *MD =
6500+
CGM.CreateMetadataIdentifierForFnType(QualType(FnType, 0));
65046501

65056502
llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD);
65066503

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,8 +2343,11 @@ llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
23432343
// originally pointed-to type, e.g. 'const char *' and 'char * const *'
23442344
// generalize to 'const void *' while 'char *' and 'const char **' generalize to
23452345
// 'void *'.
2346-
static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) {
2347-
if (!Ty->isPointerType())
2346+
static QualType GeneralizeType(ASTContext &Ctx, QualType Ty,
2347+
bool GeneralizePointers) {
2348+
// TODO: Add other generalizations.
2349+
2350+
if (!GeneralizePointers || !Ty->isPointerType())
23482351
return Ty;
23492352

23502353
return Ctx.getPointerType(
@@ -2353,26 +2356,29 @@ static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) {
23532356
}
23542357

23552358
// 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) {
23572361
if (auto *FnType = Ty->getAs<FunctionProtoType>()) {
23582362
SmallVector<QualType, 8> GeneralizedParams;
23592363
for (auto &Param : FnType->param_types())
2360-
GeneralizedParams.push_back(GeneralizeType(Ctx, Param));
2364+
GeneralizedParams.push_back(
2365+
GeneralizeType(Ctx, Param, GeneralizePointers));
23612366

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());
23642370
}
23652371

23662372
if (auto *FnType = Ty->getAs<FunctionNoProtoType>())
23672373
return Ctx.getFunctionNoProtoType(
2368-
GeneralizeType(Ctx, FnType->getReturnType()));
2374+
GeneralizeType(Ctx, FnType->getReturnType(), GeneralizePointers));
23692375

23702376
llvm_unreachable("Encountered unknown FunctionType");
23712377
}
23722378

23732379
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);
23762382
if (auto *FnType = T->getAs<FunctionProtoType>())
23772383
T = getContext().getFunctionType(
23782384
FnType->getReturnType(), FnType->getParamTypes(),
@@ -3041,9 +3047,13 @@ void CodeGenModule::createFunctionTypeMetadataForIcall(const FunctionDecl *FD,
30413047
if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
30423048
return;
30433049

3044-
llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType());
3050+
QualType FnType = GeneralizeFunctionType(getContext(), FD->getType(),
3051+
/*GeneralizePointers=*/false);
3052+
llvm::Metadata *MD = CreateMetadataIdentifierForType(FnType);
30453053
F->addTypeMetadata(0, MD);
3046-
F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
3054+
FnType = GeneralizeFunctionType(getContext(), FD->getType(),
3055+
/*GeneralizePointers=*/true);
3056+
F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FnType));
30473057

30483058
// Emit a hash-based bit set entry for cross-DSO calls.
30493059
if (CodeGenOpts.SanitizeCfiCrossDso)
@@ -7934,6 +7944,15 @@ CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
79347944
return InternalId;
79357945
}
79367946

7947+
llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForFnType(QualType T) {
7948+
assert(isa<FunctionType>(T));
7949+
T = GeneralizeFunctionType(
7950+
getContext(), T, getCodeGenOpts().SanitizeCfiICallGeneralizePointers);
7951+
if (getCodeGenOpts().SanitizeCfiICallGeneralizePointers)
7952+
return CreateMetadataIdentifierGeneralized(T);
7953+
return CreateMetadataIdentifierForType(T);
7954+
}
7955+
79377956
llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) {
79387957
return CreateMetadataIdentifierImpl(T, MetadataIdMap, "");
79397958
}
@@ -7944,8 +7963,8 @@ CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) {
79447963
}
79457964

79467965
llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) {
7947-
return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T),
7948-
GeneralizedMetadataIdMap, ".generalized");
7966+
return CreateMetadataIdentifierImpl(T, GeneralizedMetadataIdMap,
7967+
".generalized");
79497968
}
79507969

79517970
/// Returns whether this module needs the "all-vtables" type identifier.

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,9 @@ class CodeGenModule : public CodeGenTypeCache {
16231623
/// Generate a KCFI type identifier for T.
16241624
llvm::ConstantInt *CreateKCFITypeId(QualType T, StringRef Salt);
16251625

1626+
/// Create a metadata identifier for the given function type.
1627+
llvm::Metadata *CreateMetadataIdentifierForFnType(QualType T);
1628+
16261629
/// Create a metadata identifier for the given type. This may either be an
16271630
/// MDString (for external identifiers) or a distinct unnamed MDNode (for
16281631
/// internal identifiers).

0 commit comments

Comments
 (0)