Skip to content

Commit 27f0e6e

Browse files
ojhunttru
authored andcommitted
[clang][PAC] Fix builtins that claim address discriminated types are bitwise compatible (#154490)
A number of builtins report some variation of "this type is compatibile with some bitwise equivalent operation", but this is not true for address discriminated values. We had address a number of cases, but not all of them. This PR corrects the remaining builtins. Fixes #154394
1 parent e35cb1a commit 27f0e6e

File tree

9 files changed

+440
-9
lines changed

9 files changed

+440
-9
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
648648
bool containsNonRelocatablePointerAuth(QualType T) {
649649
if (!isPointerAuthenticationAvailable())
650650
return false;
651-
return findPointerAuthContent(T) ==
652-
PointerAuthContent::AddressDiscriminatedData;
651+
return findPointerAuthContent(T) != PointerAuthContent::None;
653652
}
654653

655654
private:

clang/lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3032,7 +3032,7 @@ bool ASTContext::hasUniqueObjectRepresentations(
30323032
return true;
30333033
}
30343034

3035-
// All other pointers (except __ptrauth pointers) are unique.
3035+
// All other pointers are unique.
30363036
if (Ty->isPointerType())
30373037
return !Ty.hasAddressDiscriminatedPointerAuth();
30383038

clang/lib/AST/DeclCXX.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,13 @@ void CXXRecordDecl::addedMember(Decl *D) {
14481448
data().StructuralIfLiteral = false;
14491449
}
14501450

1451+
// If this type contains any address discriminated values we should
1452+
// have already indicated that the only special member functions that
1453+
// can possibly be trivial are the default constructor and destructor.
1454+
if (T.hasAddressDiscriminatedPointerAuth())
1455+
data().HasTrivialSpecialMembers &=
1456+
SMF_DefaultConstructor | SMF_Destructor;
1457+
14511458
// C++14 [meta.unary.prop]p4:
14521459
// T is a class type [...] with [...] no non-static data members other
14531460
// than subobjects of zero size

clang/lib/AST/Type.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,6 +2715,11 @@ bool QualType::isCXX98PODType(const ASTContext &Context) const {
27152715
return false;
27162716

27172717
QualType CanonicalType = getTypePtr()->CanonicalType;
2718+
2719+
// Any type that is, or contains, address discriminated data is never POD.
2720+
if (const_cast<ASTContext&>(Context).containsAddressDiscriminatedPointerAuth(CanonicalType))
2721+
return false;
2722+
27182723
switch (CanonicalType->getTypeClass()) {
27192724
// Everything not explicitly mentioned is not POD.
27202725
default:
@@ -2773,6 +2778,11 @@ bool QualType::isTrivialType(const ASTContext &Context) const {
27732778
if (CanonicalType->isDependentType())
27742779
return false;
27752780

2781+
// Any type that is, or contains, address discriminated data is never a
2782+
// trivial type.
2783+
if (const_cast<ASTContext&>(Context).containsAddressDiscriminatedPointerAuth(CanonicalType))
2784+
return false;
2785+
27762786
// C++0x [basic.types]p9:
27772787
// Scalar types, trivial class types, arrays of such types, and
27782788
// cv-qualified versions of these types are collectively called trivial
@@ -2870,6 +2880,12 @@ bool QualType::isBitwiseCloneableType(const ASTContext &Context) const {
28702880

28712881
if (CanonicalType->isIncompleteType())
28722882
return false;
2883+
2884+
// Any type that is, or contains, address discriminated data is never
2885+
// bitwise clonable.
2886+
if (const_cast<ASTContext&>(Context).containsAddressDiscriminatedPointerAuth(CanonicalType))
2887+
return false;
2888+
28732889
const auto *RD = CanonicalType->getAsRecordDecl(); // struct/union/class
28742890
if (!RD)
28752891
return true;
@@ -3115,6 +3131,10 @@ bool QualType::isCXX11PODType(const ASTContext &Context) const {
31153131
if (BaseTy->isIncompleteType())
31163132
return false;
31173133

3134+
// Any type that is, or contains, address discriminated data is non-POD.
3135+
if (const_cast<ASTContext&>(Context).containsAddressDiscriminatedPointerAuth(*this))
3136+
return false;
3137+
31183138
// As an extension, Clang treats vector types as Scalar types.
31193139
if (BaseTy->isScalarType() || BaseTy->isVectorType())
31203140
return true;

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19651,6 +19651,7 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl,
1965119651
Q && Q.isAddressDiscriminated()) {
1965219652
Record->setArgPassingRestrictions(
1965319653
RecordArgPassingKind::CanNeverPassInRegs);
19654+
Record->setNonTrivialToPrimitiveCopy(true);
1965419655
}
1965519656
}
1965619657

clang/lib/Sema/SemaTypeTraits.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1768,7 +1768,10 @@ static bool EvaluateBinaryTypeTrait(Sema &Self, TypeTrait BTT,
17681768
// Objective-C lifetime, this is a non-trivial assignment.
17691769
if (LhsT.getNonReferenceType().hasNonTrivialObjCLifetime())
17701770
return false;
1771-
1771+
ASTContext &Context = Self.getASTContext();
1772+
if (Context.containsAddressDiscriminatedPointerAuth(LhsT) ||
1773+
Context.containsAddressDiscriminatedPointerAuth(RhsT))
1774+
return false;
17721775
return !Result.get()->hasNonTrivialCall(Self.Context);
17731776
}
17741777

clang/test/SemaCXX/ptrauth-triviality.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static_assert(__is_trivially_destructible(S3));
7474
static_assert(!__is_trivially_copyable(S3));
7575
static_assert(!__is_trivially_relocatable(S3)); // expected-warning{{deprecated}}
7676
//FIXME
77-
static_assert(__builtin_is_cpp_trivially_relocatable(S3));
77+
static_assert(!__builtin_is_cpp_trivially_relocatable(S3));
7878
static_assert(!__is_trivially_equality_comparable(S3));
7979

8080

@@ -84,7 +84,7 @@ static_assert(!__is_trivially_assignable(Holder<S3>, const Holder<S3>&));
8484
static_assert(__is_trivially_destructible(Holder<S3>));
8585
static_assert(!__is_trivially_copyable(Holder<S3>));
8686
static_assert(!__is_trivially_relocatable(Holder<S3>)); // expected-warning{{deprecated}}
87-
static_assert(__builtin_is_cpp_trivially_relocatable(Holder<S3>));
87+
static_assert(!__builtin_is_cpp_trivially_relocatable(Holder<S3>));
8888
static_assert(!__is_trivially_equality_comparable(Holder<S3>));
8989

9090
struct IA S4 {
@@ -207,7 +207,7 @@ template <class T> struct UnionWrapper trivially_relocatable_if_eligible {
207207
} u;
208208
};
209209

210-
static_assert(test_is_trivially_relocatable_v<AddressDiscriminatedPolymorphicBase>);
210+
static_assert(!test_is_trivially_relocatable_v<AddressDiscriminatedPolymorphicBase>);
211211
static_assert(test_is_trivially_relocatable_v<NoAddressDiscriminatedPolymorphicBase>);
212212
static_assert(inheritance_relocatability_matches_bases_v<AddressDiscriminatedPolymorphicBase, NoAddressDiscriminatedPolymorphicBase>);
213213
static_assert(inheritance_relocatability_matches_bases_v<NoAddressDiscriminatedPolymorphicBase, AddressDiscriminatedPolymorphicBase>);

0 commit comments

Comments
 (0)