Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2352,6 +2352,9 @@ static QualType GeneralizeTransparentUnion(QualType Ty) {
return Ty;
}

static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty,
bool GeneralizePointers);

// Generalize pointer types to a void pointer with the qualifiers of the
// originally pointed-to type, e.g. 'const char *' and 'char * const *'
// generalize to 'const void *' while 'char *' and 'const char **' generalize to
Expand All @@ -2360,12 +2363,19 @@ static QualType GeneralizeType(ASTContext &Ctx, QualType Ty,
bool GeneralizePointers) {
Ty = GeneralizeTransparentUnion(Ty);

if (!GeneralizePointers || !Ty->isPointerType())
if (!Ty->isPointerType())
return Ty;

QualType PTy = Ty->getPointeeType();
if (PTy->getAs<FunctionProtoType>() || PTy->getAs<FunctionNoProtoType>())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is not the only way you can end up with a function type. E.g. pointer to pointer to function, reference to pointer to function, etc.

What do you think about doing this in the mangler? E.g. add a call to a hook near the top of void CXXNameMangler::mangleType(QualType T) and supply a hook that does the transparent_union generalization from here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mangler is too late, if we have pointer generalization, it should apply to pointers form transparent union

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hook could also do the pointer generalization, no?

Ty = Ctx.getPointerType(
GeneralizeFunctionType(Ctx, PTy, GeneralizePointers));

if (!GeneralizePointers)
return Ty;

return Ctx.getPointerType(
QualType(Ctx.VoidTy)
.withCVRQualifiers(Ty->getPointeeType().getCVRQualifiers()));
QualType(Ctx.VoidTy).withCVRQualifiers(PTy.getCVRQualifiers()));
}

// Apply type generalization to a FunctionType's return and argument types
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CodeGen/cfi-icall-generalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ void uni(void (*fn)(union Union), union Union arg1) {
// CHECK: [[TYPE]] = !{i64 0, !"_ZTSFPPiPKcPS2_E"}
// CHECK: [[TYPE_GENERALIZED]] = !{i64 0, !"_ZTSFPvPKvS_E.generalized"}

// CHECK: [[TYPE2]] = !{i64 0, !"_ZTSFvPFv5UnionEPcE"}
// CHECK: [[TYPE2]] = !{i64 0, !"_ZTSFvPFvPcES_E"}
// CHECK: [[TYPE2_GENERALIZED]] = !{i64 0, !"_ZTSFvPvS_E.generalized"}

2 changes: 1 addition & 1 deletion clang/test/CodeGen/cfi-icall-normalize2.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ void uni(void (*fn)(union Union), union Union arg1) {
// CHECK: ![[TYPE1]] = !{i64 0, !"_ZTSFvPFvu3i32ES_E.normalized"}
// CHECK: ![[TYPE2]] = !{i64 0, !"_ZTSFvPFvu3i32S_ES_S_E.normalized"}
// CHECK: ![[TYPE3]] = !{i64 0, !"_ZTSFvPFvu3i32S_S_ES_S_S_E.normalized"}
// CHECK: ![[TYPE4]] = !{i64 0, !"_ZTSFvPFv5UnionEPu2i8E.normalized"}
// CHECK: ![[TYPE4]] = !{i64 0, !"_ZTSFvPFvPu2i8ES0_E.normalized"}

2 changes: 1 addition & 1 deletion clang/test/CodeGen/kcfi-generalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ void uni(void (*fn)(union Union), union Union arg1) {
// UNGENERALIZED: [[TYPE3]] = !{i32 874141567}
// GENERALIZED: [[TYPE3]] = !{i32 954385378}

// UNGENERALIZED: [[TYPE4]] = !{i32 -1619636625}
// UNGENERALIZED: [[TYPE4]] = !{i32 -1954865805}
// GENERALIZED: [[TYPE4]] = !{i32 -125078496}
2 changes: 1 addition & 1 deletion clang/test/CodeGen/kcfi-normalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ void uni(void (*fn)(union Union), union Union arg1) {
// CHECK: ![[TYPE1]] = !{i32 -1143117868}
// CHECK: ![[TYPE2]] = !{i32 -460921415}
// CHECK: ![[TYPE3]] = !{i32 -333839615}
// C: ![[TYPE4]] = !{i32 -650530463}
// C: ![[TYPE4]] = !{i32 1186327125}
// CPP: ![[TYPE4]] = !{i32 1766237188}
Loading