Skip to content

Commit 6d578df

Browse files
ahatanakahmedbougacha
authored andcommitted
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 4cebec2 commit 6d578df

File tree

10 files changed

+52
-64
lines changed

10 files changed

+52
-64
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
@@ -20,7 +20,7 @@ using namespace CodeGen;
2020
/// Return the abstract pointer authentication schema for a pointer to the given
2121
/// function type.
2222
CGPointerAuthInfo CodeGenModule::getFunctionPointerAuthInfo(QualType T) {
23-
auto &Schema = getCodeGenOpts().PointerAuth.FunctionPointers;
23+
const auto &Schema = getCodeGenOpts().PointerAuth.FunctionPointers;
2424
if (!Schema)
2525
return CGPointerAuthInfo();
2626

@@ -62,33 +62,25 @@ CodeGenModule::getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
6262

6363
/// If applicable, sign a given constant function pointer with the ABI rules for
6464
/// functionType.
65-
llvm::Constant *CodeGenModule::getFunctionPointer(llvm::Constant *pointer,
66-
QualType functionType,
65+
llvm::Constant *CodeGenModule::getFunctionPointer(llvm::Constant *Pointer,
66+
QualType FunctionType,
6767
GlobalDecl GD) {
68-
assert(functionType->isFunctionType() ||
69-
functionType->isFunctionReferenceType() ||
70-
functionType->isFunctionPointerType());
68+
assert(FunctionType->isFunctionType() ||
69+
FunctionType->isFunctionReferenceType() ||
70+
FunctionType->isFunctionPointerType());
7171

72-
if (auto pointerAuth = getFunctionPointerAuthInfo(functionType)) {
72+
if (auto PointerAuth = getFunctionPointerAuthInfo(FunctionType)) {
7373
return getConstantSignedPointer(
74-
pointer, pointerAuth.getKey(), nullptr,
75-
cast_or_null<llvm::Constant>(pointerAuth.getDiscriminator()));
74+
Pointer, PointerAuth.getKey(), nullptr,
75+
cast_or_null<llvm::Constant>(PointerAuth.getDiscriminator()));
7676
}
7777

78-
return pointer;
78+
return Pointer;
7979
}
8080

8181
llvm::Constant *CodeGenModule::getFunctionPointer(GlobalDecl GD,
8282
llvm::Type *Ty) {
83-
const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
84-
85-
// Annoyingly, K&R functions have prototypes in the clang AST, but
86-
// expressions referring to them are unprototyped.
83+
const auto *FD = cast<FunctionDecl>(GD.getDecl());
8784
QualType FuncType = FD->getType();
88-
if (!FD->hasPrototype())
89-
if (const auto *Proto = FuncType->getAs<FunctionProtoType>())
90-
FuncType = Context.getFunctionNoProtoType(Proto->getReturnType(),
91-
Proto->getExtInfo());
92-
9385
return getFunctionPointer(getRawFunctionPointer(GD, Ty), FuncType, GD);
9486
}

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
@@ -3049,18 +3049,18 @@ llvm::Value *CodeGenFunction::emitBoolVecConversion(llvm::Value *SrcVec,
30493049
}
30503050

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

3057-
auto key = Builder.getInt32(pointerAuth.getKey());
3057+
auto Key = Builder.getInt32(PointerAuth.getKey());
30583058

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

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

clang/lib/CodeGen/CodeGenFunction.h

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

44184418
bool isPointerKnownNonNull(const Expr *E);
4419-
CGPointerAuthInfo EmitPointerAuthInfo(const PointerAuthSchema &schema,
4420-
llvm::Value *storageAddress,
4421-
GlobalDecl calleeDecl,
4422-
QualType calleeType);
4419+
44234420
void EmitPointerAuthOperandBundle(
4424-
const CGPointerAuthInfo &info,
4425-
SmallVectorImpl<llvm::OperandBundleDef> &bundles);
4421+
const CGPointerAuthInfo &Info,
4422+
SmallVectorImpl<llvm::OperandBundleDef> &Bundles);
44264423

44274424
// Return the copy constructor name with the prefix "__copy_constructor_"
44284425
// removed.

clang/lib/CodeGen/CodeGenModule.h

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

960-
CGPointerAuthInfo getFunctionPointerAuthInfo(QualType functionType);
959+
CGPointerAuthInfo getFunctionPointerAuthInfo(QualType T);
961960

962-
CGPointerAuthInfo getPointerAuthInfoForPointeeType(QualType type);
961+
llvm::Constant *getConstantSignedPointer(llvm::Constant *Pointer,
962+
const PointerAuthSchema &Schema,
963+
llvm::Constant *StorageAddress,
964+
GlobalDecl SchemaDecl,
965+
QualType SchemaType);
963966

964-
CGPointerAuthInfo getPointerAuthInfoForType(QualType type);
965-
966-
llvm::Constant *getConstantSignedPointer(llvm::Constant *pointer,
967-
const PointerAuthSchema &schema,
968-
llvm::Constant *storageAddress,
969-
GlobalDecl schemaDecl,
970-
QualType schemaType);
971967
llvm::Constant *
972968
getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
973969
llvm::Constant *StorageAddress,
974970
llvm::ConstantInt *OtherDiscriminator);
975971

976-
CGPointerAuthInfo EmitPointerAuthInfo(const RecordDecl *RD);
977-
978972
// Return whether RTTI information should be emitted for this target.
979973
bool shouldEmitRTTI(bool ForEH = false) {
980974
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)