Skip to content

Commit 4fde673

Browse files
committed
Address review comments
- Remove unneeded declarations. - Fix includes. - Use `llvm::to_underlying`. - Fix variable names. - Fix size and type of bit-fields. - Add test case for `err_drv_ptrauth_not_supported`.
1 parent d1f35a5 commit 4fde673

File tree

10 files changed

+52
-65
lines changed

10 files changed

+52
-65
lines changed

clang/include/clang/Basic/LangOptions.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,6 @@ class LangOptionsBase {
346346
BKey
347347
};
348348

349-
using PointerAuthenticationMode = ::clang::PointerAuthenticationMode;
350-
351349
enum class ThreadModelKind {
352350
/// POSIX Threads.
353351
POSIX,

clang/include/clang/Basic/PointerAuthOptions.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616

1717
#include "clang/Basic/LLVM.h"
1818
#include "clang/Basic/LangOptions.h"
19+
#include "llvm/ADT/STLForwardCompat.h"
1920
#include "llvm/Support/ErrorHandling.h"
2021
#include "llvm/Target/TargetOptions.h"
21-
#include <map>
22-
#include <memory>
23-
#include <string>
24-
#include <vector>
22+
#include <optional>
2523

2624
namespace clang {
2725

@@ -76,7 +74,7 @@ class PointerAuthSchema {
7674
IsIsaPointer(IsIsaPointer),
7775
AuthenticatesNullValues(AuthenticatesNullValues),
7876
SelectedAuthenticationMode(AuthenticationMode),
79-
DiscriminationKind(OtherDiscrimination), Key(unsigned(Key)) {
77+
DiscriminationKind(OtherDiscrimination), Key(llvm::to_underlying(Key)) {
8078
assert((getOtherDiscrimination() != Discrimination::Constant ||
8179
ConstantDiscriminatorOrNone) &&
8280
"constant discrimination requires a constant!");
@@ -126,15 +124,15 @@ class PointerAuthSchema {
126124

127125
uint16_t getConstantDiscrimination() const {
128126
assert(getOtherDiscrimination() == Discrimination::Constant);
129-
return (uint16_t)ConstantDiscriminator;
127+
return ConstantDiscriminator;
130128
}
131129

132130
unsigned getKey() const {
133131
switch (getKind()) {
134132
case Kind::None:
135133
llvm_unreachable("calling getKey() on disabled schema");
136134
case Kind::ARM8_3:
137-
return unsigned(getARM8_3Key());
135+
return llvm::to_underlying(getARM8_3Key());
138136
}
139137
llvm_unreachable("bad key kind");
140138
}

clang/lib/CodeGen/CGCall.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ class CGCallee {
196196
KindOrFunctionPointer =
197197
SpecialKind(reinterpret_cast<uintptr_t>(functionPtr));
198198
}
199-
void setPointerAuthInfo(CGPointerAuthInfo pointerAuth) {
199+
void setPointerAuthInfo(CGPointerAuthInfo PointerAuth) {
200200
assert(isOrdinary());
201-
OrdinaryInfo.PointerAuthInfo = pointerAuth;
201+
OrdinaryInfo.PointerAuthInfo = PointerAuth;
202202
}
203203

204204
bool isVirtual() const {

clang/lib/CodeGen/CGPointerAuth.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using namespace CodeGen;
3131
/// Return the abstract pointer authentication schema for a pointer to the given
3232
/// function type.
3333
CGPointerAuthInfo CodeGenModule::getFunctionPointerAuthInfo(QualType T) {
34-
auto &Schema = getCodeGenOpts().PointerAuth.FunctionPointers;
34+
const auto &Schema = getCodeGenOpts().PointerAuth.FunctionPointers;
3535
if (!Schema)
3636
return CGPointerAuthInfo();
3737

@@ -96,33 +96,25 @@ CodeGen::getConstantSignedPointer(CodeGenModule &CGM,
9696

9797
/// If applicable, sign a given constant function pointer with the ABI rules for
9898
/// functionType.
99-
llvm::Constant *CodeGenModule::getFunctionPointer(llvm::Constant *pointer,
100-
QualType functionType,
99+
llvm::Constant *CodeGenModule::getFunctionPointer(llvm::Constant *Pointer,
100+
QualType FunctionType,
101101
GlobalDecl GD) {
102-
assert(functionType->isFunctionType() ||
103-
functionType->isFunctionReferenceType() ||
104-
functionType->isFunctionPointerType());
102+
assert(FunctionType->isFunctionType() ||
103+
FunctionType->isFunctionReferenceType() ||
104+
FunctionType->isFunctionPointerType());
105105

106-
if (auto pointerAuth = getFunctionPointerAuthInfo(functionType)) {
106+
if (auto PointerAuth = getFunctionPointerAuthInfo(FunctionType)) {
107107
return getConstantSignedPointer(
108-
pointer, pointerAuth.getKey(), nullptr,
109-
cast_or_null<llvm::Constant>(pointerAuth.getDiscriminator()));
108+
Pointer, PointerAuth.getKey(), nullptr,
109+
cast_or_null<llvm::Constant>(PointerAuth.getDiscriminator()));
110110
}
111111

112-
return pointer;
112+
return Pointer;
113113
}
114114

115115
llvm::Constant *CodeGenModule::getFunctionPointer(GlobalDecl GD,
116116
llvm::Type *Ty) {
117-
const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
118-
119-
// Annoyingly, K&R functions have prototypes in the clang AST, but
120-
// expressions referring to them are unprototyped.
117+
const auto *FD = cast<FunctionDecl>(GD.getDecl());
121118
QualType FuncType = FD->getType();
122-
if (!FD->hasPrototype())
123-
if (const auto *Proto = FuncType->getAs<FunctionProtoType>())
124-
FuncType = Context.getFunctionNoProtoType(Proto->getReturnType(),
125-
Proto->getExtInfo());
126-
127119
return getFunctionPointer(getRawFunctionPointer(GD, Ty), FuncType, GD);
128120
}

clang/lib/CodeGen/CGPointerAuthInfo.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_CLANG_LIB_CODEGEN_CGPOINTERAUTHINFO_H
1515

1616
#include "clang/AST/Type.h"
17+
#include "clang/Basic/LangOptions.h"
1718
#include "llvm/IR/Type.h"
1819
#include "llvm/IR/Value.h"
1920

@@ -23,9 +24,9 @@ namespace CodeGen {
2324
class CGPointerAuthInfo {
2425
private:
2526
PointerAuthenticationMode AuthenticationMode : 2;
26-
bool IsIsaPointer : 1;
27-
bool AuthenticatesNullValues : 1;
28-
unsigned Key : 28;
27+
unsigned IsIsaPointer : 1;
28+
unsigned AuthenticatesNullValues : 1;
29+
unsigned Key : 4;
2930
llvm::Value *Discriminator;
3031

3132
public:

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,18 +3048,18 @@ llvm::Value *CodeGenFunction::emitBoolVecConversion(llvm::Value *SrcVec,
30483048
}
30493049

30503050
void CodeGenFunction::EmitPointerAuthOperandBundle(
3051-
const CGPointerAuthInfo &pointerAuth,
3052-
SmallVectorImpl<llvm::OperandBundleDef> &bundles) {
3053-
if (!pointerAuth.isSigned())
3051+
const CGPointerAuthInfo &PointerAuth,
3052+
SmallVectorImpl<llvm::OperandBundleDef> &Bundles) {
3053+
if (!PointerAuth.isSigned())
30543054
return;
30553055

3056-
auto key = Builder.getInt32(pointerAuth.getKey());
3056+
auto Key = Builder.getInt32(PointerAuth.getKey());
30573057

3058-
llvm::Value *discriminator = pointerAuth.getDiscriminator();
3059-
if (!discriminator) {
3060-
discriminator = Builder.getSize(0);
3058+
llvm::Value *Discriminator = PointerAuth.getDiscriminator();
3059+
if (!Discriminator) {
3060+
Discriminator = Builder.getSize(0);
30613061
}
30623062

3063-
llvm::Value *args[] = {key, discriminator};
3064-
bundles.emplace_back("ptrauth", args);
3063+
llvm::Value *Args[] = {Key, Discriminator};
3064+
Bundles.emplace_back("ptrauth", Args);
30653065
}

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4406,13 +4406,10 @@ class CodeGenFunction : public CodeGenTypeCache {
44064406
}
44074407

44084408
bool isPointerKnownNonNull(const Expr *E);
4409-
CGPointerAuthInfo EmitPointerAuthInfo(const PointerAuthSchema &schema,
4410-
llvm::Value *storageAddress,
4411-
GlobalDecl calleeDecl,
4412-
QualType calleeType);
4409+
44134410
void EmitPointerAuthOperandBundle(
4414-
const CGPointerAuthInfo &info,
4415-
SmallVectorImpl<llvm::OperandBundleDef> &bundles);
4411+
const CGPointerAuthInfo &Info,
4412+
SmallVectorImpl<llvm::OperandBundleDef> &Bundles);
44164413

44174414
// Return the copy constructor name with the prefix "__copy_constructor_"
44184415
// removed.

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "clang/Basic/LangOptions.h"
2727
#include "clang/Basic/Module.h"
2828
#include "clang/Basic/NoSanitizeList.h"
29-
#include "clang/Basic/PointerAuthOptions.h"
3029
#include "clang/Basic/ProfileList.h"
3130
#include "clang/Basic/TargetInfo.h"
3231
#include "clang/Basic/XRayLists.h"
@@ -954,29 +953,23 @@ class CodeGenModule : public CodeGenTypeCache {
954953
/// Return the ABI-correct function pointer value for a reference
955954
/// to the given function. This will apply a pointer signature if
956955
/// necessary, but will only cache the result if \p FD is passed.
957-
llvm::Constant *getFunctionPointer(llvm::Constant *pointer,
958-
QualType functionType,
956+
llvm::Constant *getFunctionPointer(llvm::Constant *Pointer,
957+
QualType FunctionType,
959958
GlobalDecl GD = GlobalDecl());
960959

961-
CGPointerAuthInfo getFunctionPointerAuthInfo(QualType functionType);
960+
CGPointerAuthInfo getFunctionPointerAuthInfo(QualType T);
962961

963-
CGPointerAuthInfo getPointerAuthInfoForPointeeType(QualType type);
964-
965-
CGPointerAuthInfo getPointerAuthInfoForType(QualType type);
966-
967-
llvm::Constant *getConstantSignedPointer(llvm::Constant *pointer,
968-
const PointerAuthSchema &schema,
969-
llvm::Constant *storageAddress,
970-
GlobalDecl schemaDecl,
971-
QualType schemaType);
962+
llvm::Constant *getConstantSignedPointer(llvm::Constant *Pointer,
963+
const PointerAuthSchema &Schema,
964+
llvm::Constant *StorageAddress,
965+
GlobalDecl SchemaDecl,
966+
QualType SchemaType);
972967

973968
llvm::Constant *getConstantSignedPointer(llvm::Constant *pointer,
974969
unsigned key,
975970
llvm::Constant *storageAddress,
976971
llvm::Constant *extraDiscrim);
977972

978-
CGPointerAuthInfo EmitPointerAuthInfo(const RecordDecl *RD);
979-
980973
// Return whether RTTI information should be emitted for this target.
981974
bool shouldEmitRTTI(bool ForEH = false) {
982975
return (ForEH || getLangOpts().RTTI) && !getLangOpts().CUDAIsDevice &&

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,6 @@ bool CompilerInvocation::setDefaultPointerAuthOptions(
14761476
}
14771477

14781478
static bool parsePointerAuthOptions(PointerAuthOptions &Opts,
1479-
ArgList &Args,
14801479
const LangOptions &LangOpts,
14811480
const llvm::Triple &Triple,
14821481
DiagnosticsEngine &Diags) {
@@ -2187,7 +2186,7 @@ bool CompilerInvocation::ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args,
21872186
Opts.EmitVersionIdentMetadata = Args.hasFlag(OPT_Qy, OPT_Qn, true);
21882187

21892188
if (!LangOpts->CUDAIsDevice)
2190-
parsePointerAuthOptions(Opts.PointerAuth, Args, *LangOpts, T, Diags);
2189+
parsePointerAuthOptions(Opts.PointerAuth, *LangOpts, T, Diags);
21912190

21922191
if (Args.hasArg(options::OPT_ffinite_loops))
21932192
Opts.FiniteLoops = CodeGenOptions::FiniteLoopsKind::Always;

clang/test/Driver/ptrauth.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %clang -target arm64-apple-macosx -fptrauth-calls -c %s -### 2>&1 | FileCheck %s --check-prefix PTRAUTH_CALLS
2+
// RUN: %clang -target aarch64-linux-gnu -fptrauth-calls -c %s -### 2>&1 | FileCheck %s --check-prefix PTRAUTH_CALLS
3+
// RUN: %clang -target aarch64-windows-msvc -fptrauth-calls -c %s -### 2>&1 | FileCheck %s --check-prefix PTRAUTH_CALLS
4+
// RUN: not %clang -target x86_64-apple-macosx -fptrauth-calls -c %s -### 2>&1 | FileCheck %s --check-prefix INVALID
5+
// RUN: not %clang -target x86_64-linux-gnu -fptrauth-calls -c %s -### 2>&1 | FileCheck %s --check-prefix INVALID
6+
// RUN: not %clang -target x86_64-windows-msvc -fptrauth-calls -c %s -### 2>&1 | FileCheck %s --check-prefix INVALID
7+
8+
// PTRAUTH_CALLS: "-fptrauth-calls"
9+
// INVALID: unsupported option '-fptrauth-calls'

0 commit comments

Comments
 (0)