Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ Improvements to Clang's diagnostics
pointers under ``-Wthread-safety-beta`` (still experimental), which reduces
both false positives but also false negatives through more precise analysis.

- A new warning ``-Wptrauth-weak-schema`` has been added to detect variable
declarations of function pointer type and internal linkage that use a weak
pointer authentication schema, either implicitly or by specifying a discriminator
with no diversifiers. This warning applies only to targets supporting pointer
authentication.

Improvements to Clang's time-trace
----------------------------------

Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -3976,6 +3976,10 @@ class Sema final : public SemaBase {
void CheckVariableDeclarationType(VarDecl *NewVD);
void CheckCompleteVariableDeclaration(VarDecl *VD);

// Warn about the use of a weak pointer authentication schema on a variable of
// function pointer type and internal linkage.
void DiagnoseWeakPointerAuthenticationSchema(VarDecl *VD);

NamedDecl *ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
TypeSourceInfo *TInfo,
LookupResult &Previous,
Expand Down
20 changes: 11 additions & 9 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7627,6 +7627,16 @@ static bool isMainVar(DeclarationName Name, VarDecl *VD) {
VD->isExternC());
}

void Sema::DiagnoseWeakPointerAuthenticationSchema(VarDecl *VD) {
if (Context.isPointerAuthenticationAvailable() &&
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'd prefer separating the pointer auth availability check -- we're planning to add more checks later so I think this should be

if (!Context.isPointerAuthenticationAvailable())
  return;

VD->isFunctionPointerType() && !VD->isExternallyVisible()) {
PointerAuthQualifier Q = VD->getType().getQualifiers().getPointerAuth();
if (!Q || (!Q.isAddressDiscriminated() && Q.getExtraDiscriminator() == 0)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: single statement if statements don't get braces, even if they are multiple lines. I don't like it, but those are the rules (it would be nice if clang-format could be configured for this)

Diag(VD->getLocation(), diag::warn_ptrauth_weak_schema) << VD << !Q;
}
}
}

NamedDecl *Sema::ActOnVariableDeclarator(
Scope *S, Declarator &D, DeclContext *DC, TypeSourceInfo *TInfo,
LookupResult &Previous, MultiTemplateParamsArg TemplateParamLists,
Expand Down Expand Up @@ -8357,15 +8367,7 @@ NamedDecl *Sema::ActOnVariableDeclarator(
D.isFunctionDefinition());
}

// Warn about the use of a weak pointer authentication schema on a variable
// with internal linkage.
if (Context.isPointerAuthenticationAvailable() &&
NewVD->isFunctionPointerType() && !NewVD->isExternallyVisible()) {
PointerAuthQualifier Q = NewVD->getType().getQualifiers().getPointerAuth();
if (!Q || (!Q.isAddressDiscriminated() && Q.getExtraDiscriminator() == 0)) {
Diag(NewVD->getLocation(), diag::warn_ptrauth_weak_schema) << NewVD << !Q;
}
}
DiagnoseWeakPointerAuthenticationSchema(NewVD);

if (NewTemplate) {
if (NewVD->isInvalidDecl())
Expand Down