Skip to content

Commit 5903e97

Browse files
committed
Actually make sure the type printer works properly
1 parent 02bc476 commit 5903e97

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

clang/lib/AST/TypePrinter.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2537,7 +2537,39 @@ void PointerAuthQualifier::print(raw_ostream &OS,
25372537
OS << "__ptrauth(";
25382538
OS << getKey();
25392539
OS << "," << unsigned(isAddressDiscriminated()) << ","
2540-
<< getExtraDiscriminator() << ")";
2540+
<< getExtraDiscriminator();
2541+
2542+
bool HasAppendedOption = false;
2543+
auto AppendOption = [&](StringRef Option) {
2544+
OS << ",";
2545+
if (!HasAppendedOption)
2546+
OS << '"';
2547+
HasAppendedOption = true;
2548+
OS << Option;
2549+
};
2550+
switch (getAuthenticationMode()) {
2551+
case PointerAuthenticationMode::None:
2552+
llvm_unreachable("Mode is unauthenticated but claims to be present");
2553+
return;
2554+
case PointerAuthenticationMode::Strip:
2555+
AppendOption(PointerAuthenticationOptionStrip);
2556+
break;
2557+
case PointerAuthenticationMode::SignAndStrip:
2558+
AppendOption(PointerAuthenticationOptionSignAndStrip);
2559+
break;
2560+
case clang::PointerAuthenticationMode::SignAndAuth:
2561+
// Don't emit default auth
2562+
break;
2563+
}
2564+
if (isIsaPointer())
2565+
AppendOption(PointerAuthenticationOptionIsaPointer);
2566+
if (authenticatesNullValues())
2567+
AppendOption(PointerAuthenticationOptionAuthenticatesNullValues);
2568+
2569+
if (HasAppendedOption)
2570+
OS << '"';
2571+
2572+
OS << ")";
25412573
}
25422574

25432575
std::string Qualifiers::getAsString() const {

clang/test/Sema/ptrauth-qualifier-options.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,46 @@ int *__ptrauth(VALID_DATA_KEY, 1, 0, "sign-and-strip"
6363
int *__ptrauth(VALID_DATA_KEY, 1, 0, "sign-and-strip\n,isa-pointer") valid27;
6464
int *__ptrauth(VALID_DATA_KEY, 1, 0, "sign-and-strip\t,isa-pointer") valid28;
6565
int *__ptrauth(VALID_DATA_KEY, 1, 0, "") valid29;
66+
int *__ptrauth(VALID_DATA_KEY, 1, 0, "authenticates-null-values") valid30;
67+
int *__ptrauth(VALID_DATA_KEY, 1, 0, "isa-pointer, authenticates-null-values") valid31;
68+
69+
int *global_ptr;
70+
71+
// Check qualifier option printing
72+
int *__ptrauth(1, 1, 0, "strip") * strip = &global_ptr;
73+
// expected-error@-1 {{int *__ptrauth(1,1,0,"strip") *}}
74+
int *__ptrauth(1, 1, 0, "sign-and-strip") * signAndStrip = &global_ptr;
75+
// expected-error@-1 {{int *__ptrauth(1,1,0,"sign-and-strip") *}}
76+
int *__ptrauth(1, 1, 0, "sign-and-auth") * signAndAuth = &global_ptr;
77+
// expected-error@-1 {{int *__ptrauth(1,1,0) *}}
78+
int *__ptrauth(1, 1, 0, "sign-and-auth, isa-pointer") * signAndAuthIsa = &global_ptr;
79+
// expected-error@-1 {{int *__ptrauth(1,1,0,"isa-pointer") *}}
80+
int *__ptrauth(1, 1, 0, "sign-and-strip, isa-pointer") * signAndStripIsa = &global_ptr;
81+
// expected-error@-1 {{int *__ptrauth(1,1,0,"sign-and-strip,isa-pointer") *'}}
82+
int *__ptrauth(1, 1, 0, "sign-and-strip, authenticates-null-values") * signAndAuthAuthenticatesNullValues = &global_ptr;
83+
// expected-error@-1 {{int *__ptrauth(1,1,0,"sign-and-strip,authenticates-null-values") *}}
84+
int *__ptrauth(1, 1, 0, "sign-and-strip, authenticates-null-values, isa-pointer") * signAndAuthIsaPointerAuthenticatesNullValues = &global_ptr;
85+
// expected-error@-1 {{int *__ptrauth(1,1,0,"sign-and-strip,isa-pointer,authenticates-null-values") *}}
86+
87+
// Check mismatching options are identified
88+
void f() {
89+
// default auth matches explicit use of sign-and-auth
90+
int *__ptrauth(1, 1, 0) * signAndAuthDefault = signAndAuth;
91+
92+
int *__ptrauth(1, 1, 0, "sign-and-strip") * signAndStrip2 = strip;
93+
// expected-error@-1 {{initializing 'int *__ptrauth(1,1,0,"sign-and-strip") *' with an expression of type 'int *__ptrauth(1,1,0,"strip") *' changes pointer authentication of pointee type}}
94+
int *__ptrauth(1, 1, 0) * signAndAuth2 = signAndStrip;
95+
// expected-error@-1 {{'int *__ptrauth(1,1,0) *' with an expression of type 'int *__ptrauth(1,1,0,"sign-and-strip") *' changes pointer authentication of pointee type}}
96+
int *__ptrauth(1, 1, 0, "authenticates-null-values") * signAndAuthAuthenticatesNullValues2 = signAndAuth;
97+
// expected-error@-1 {{initializing 'int *__ptrauth(1,1,0,"authenticates-null-values") *' with an expression of type 'int *__ptrauth(1,1,0) *' changes pointer authentication of pointee type}}
98+
int *__ptrauth(0, 1, 0) * signAndAuth3 = signAndAuth; // different key
99+
// expected-error@-1 {{initializing 'int *__ptrauth(0,1,0) *' with an expression of type 'int *__ptrauth(1,1,0) *' changes pointer authentication of pointee type}}
100+
int *__ptrauth(1, 0, 0) * signAndAuth4 = signAndAuth; // different address discrimination
101+
// expected-error@-1 {{initializing 'int *__ptrauth(1,0,0) *' with an expression of type 'int *__ptrauth(1,1,0) *' changes pointer authentication of pointee type}}
102+
int *__ptrauth(1, 1, 1) * signAndAuth5 = signAndAuth; // different discriminator
103+
// expected-error@-1 {{initializing 'int *__ptrauth(1,1,1) *' with an expression of type 'int *__ptrauth(1,1,0) *' changes pointer authentication of pointee type}}
104+
}
105+
106+
107+
108+

0 commit comments

Comments
 (0)