Skip to content

Commit 88b097d

Browse files
committed
Address feedback.
- test elf as well - rename every variable.
1 parent f5a87e5 commit 88b097d

File tree

6 files changed

+107
-121
lines changed

6 files changed

+107
-121
lines changed

clang/include/clang/CodeGen/CodeGenABITypes.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ unsigned getLLVMFieldNumber(CodeGenModule &CGM,
106106

107107
/// Return a signed constant pointer.
108108
llvm::Constant *getConstantSignedPointer(CodeGenModule &CGM,
109-
llvm::Constant *pointer,
110-
unsigned key,
111-
llvm::Constant *storageAddress,
112-
llvm::Constant *otherDiscriminator);
109+
llvm::Constant *Pointer, unsigned Key,
110+
llvm::Constant *StorageAddress,
111+
llvm::Constant *OtherDiscriminator);
112+
113113
/// Given the language and code-generation options that Clang was configured
114114
/// with, set the default LLVM IR attributes for a function definition.
115115
/// The attributes set here are mostly global target-configuration and

clang/lib/CodeGen/CGExprConstant.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,54 +2073,54 @@ ConstantLValueEmitter::VisitCallExpr(const CallExpr *E) {
20732073

20742074
ConstantLValue
20752075
ConstantLValueEmitter::emitPointerAuthSignConstant(const CallExpr *E) {
2076-
auto unsignedPointer = emitPointerAuthPointer(E->getArg(0));
2077-
auto key = emitPointerAuthKey(E->getArg(1));
2078-
llvm::Constant *storageAddress;
2079-
llvm::Constant *otherDiscriminator;
2080-
std::tie(storageAddress, otherDiscriminator) =
2081-
emitPointerAuthDiscriminator(E->getArg(2));
2076+
llvm::Constant *UnsignedPointer = emitPointerAuthPointer(E->getArg(0));
2077+
unsigned Key = emitPointerAuthKey(E->getArg(1));
2078+
llvm::Constant *StorageAddress;
2079+
llvm::Constant *OtherDiscriminator;
2080+
std::tie(StorageAddress, OtherDiscriminator) =
2081+
emitPointerAuthDiscriminator(E->getArg(2));
20822082

2083-
auto signedPointer =
2084-
CGM.getConstantSignedPointer(unsignedPointer, key, storageAddress,
2085-
otherDiscriminator);
2086-
return signedPointer;
2083+
llvm::Constant *SignedPointer = CGM.getConstantSignedPointer(
2084+
UnsignedPointer, Key, StorageAddress, OtherDiscriminator);
2085+
return SignedPointer;
20872086
}
20882087

20892088
llvm::Constant *ConstantLValueEmitter::emitPointerAuthPointer(const Expr *E) {
2090-
Expr::EvalResult result;
2091-
bool succeeded = E->EvaluateAsRValue(result, CGM.getContext());
2092-
assert(succeeded); (void) succeeded;
2089+
Expr::EvalResult Result;
2090+
bool Succeeded = E->EvaluateAsRValue(Result, CGM.getContext());
2091+
assert(Succeeded);
2092+
(void)Succeeded;
20932093

20942094
// The assertions here are all checked by Sema.
2095-
assert(result.Val.isLValue());
2095+
assert(Result.Val.isLValue());
20962096
return ConstantEmitter(CGM, Emitter.CGF)
2097-
.emitAbstract(E->getExprLoc(), result.Val, E->getType());
2097+
.emitAbstract(E->getExprLoc(), Result.Val, E->getType());
20982098
}
20992099

21002100
unsigned ConstantLValueEmitter::emitPointerAuthKey(const Expr *E) {
21012101
return E->EvaluateKnownConstInt(CGM.getContext()).getZExtValue();
21022102
}
21032103

2104-
std::pair<llvm::Constant*, llvm::Constant*>
2104+
std::pair<llvm::Constant *, llvm::Constant *>
21052105
ConstantLValueEmitter::emitPointerAuthDiscriminator(const Expr *E) {
21062106
E = E->IgnoreParens();
21072107

2108-
if (auto call = dyn_cast<CallExpr>(E)) {
2109-
if (call->getBuiltinCallee() ==
2110-
Builtin::BI__builtin_ptrauth_blend_discriminator) {
2111-
auto pointer = ConstantEmitter(CGM).emitAbstract(call->getArg(0),
2112-
call->getArg(0)->getType());
2113-
auto extra = ConstantEmitter(CGM).emitAbstract(call->getArg(1),
2114-
call->getArg(1)->getType());
2115-
return { pointer, extra };
2108+
if (auto *Call = dyn_cast<CallExpr>(E)) {
2109+
if (Call->getBuiltinCallee() ==
2110+
Builtin::BI__builtin_ptrauth_blend_discriminator) {
2111+
llvm::Constant *Pointer = ConstantEmitter(CGM).emitAbstract(
2112+
Call->getArg(0), Call->getArg(0)->getType());
2113+
llvm::Constant *Extra = ConstantEmitter(CGM).emitAbstract(
2114+
Call->getArg(1), Call->getArg(1)->getType());
2115+
return {Pointer, Extra};
21162116
}
21172117
}
21182118

2119-
auto result = ConstantEmitter(CGM).emitAbstract(E, E->getType());
2120-
if (result->getType()->isPointerTy())
2121-
return { result, nullptr };
2119+
llvm::Constant *Result = ConstantEmitter(CGM).emitAbstract(E, E->getType());
2120+
if (Result->getType()->isPointerTy())
2121+
return {Result, nullptr};
21222122
else
2123-
return { nullptr, result };
2123+
return {nullptr, Result};
21242124
}
21252125

21262126
ConstantLValue

clang/lib/CodeGen/CGPointerAuth.cpp

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,53 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "CGCXXABI.h"
15-
#include "CGCall.h"
16-
#include "CodeGenFunction.h"
1714
#include "CodeGenModule.h"
18-
#include "clang/AST/Attr.h"
19-
#include "clang/Basic/PointerAuthOptions.h"
2015
#include "clang/CodeGen/CodeGenABITypes.h"
21-
#include "clang/CodeGen/ConstantInitBuilder.h"
22-
23-
#include "llvm/ADT/DenseMap.h"
24-
#include "llvm/IR/ValueMap.h"
25-
#include "llvm/Analysis/ValueTracking.h"
26-
#include <vector>
2716

2817
using namespace clang;
2918
using namespace CodeGen;
3019

3120
/// Build a signed-pointer "ptrauth" constant.
3221
static llvm::ConstantPtrAuth *
33-
buildConstantAddress(CodeGenModule &CGM, llvm::Constant *pointer, unsigned key,
34-
llvm::Constant *storageAddress,
35-
llvm::Constant *otherDiscriminator) {
36-
llvm::Constant *addressDiscriminator = nullptr;
37-
if (storageAddress) {
38-
addressDiscriminator = storageAddress;
39-
assert(storageAddress->getType() == CGM.UnqualPtrTy);
22+
buildConstantAddress(CodeGenModule &CGM, llvm::Constant *Pointer, unsigned Key,
23+
llvm::Constant *StorageAddress,
24+
llvm::Constant *OtherDiscriminator) {
25+
llvm::Constant *AddressDiscriminator = nullptr;
26+
if (StorageAddress) {
27+
AddressDiscriminator = StorageAddress;
28+
assert(StorageAddress->getType() == CGM.UnqualPtrTy);
4029
} else {
41-
addressDiscriminator = llvm::Constant::getNullValue(CGM.UnqualPtrTy);
30+
AddressDiscriminator = llvm::Constant::getNullValue(CGM.UnqualPtrTy);
4231
}
4332

44-
llvm::ConstantInt *integerDiscriminator = nullptr;
45-
if (otherDiscriminator) {
46-
assert(otherDiscriminator->getType() == CGM.Int64Ty);
47-
integerDiscriminator = cast<llvm::ConstantInt>(otherDiscriminator);
33+
llvm::ConstantInt *IntegerDiscriminator = nullptr;
34+
if (OtherDiscriminator) {
35+
assert(OtherDiscriminator->getType() == CGM.Int64Ty);
36+
IntegerDiscriminator = cast<llvm::ConstantInt>(OtherDiscriminator);
4837
} else {
49-
integerDiscriminator = llvm::ConstantInt::get(CGM.Int64Ty, 0);
38+
IntegerDiscriminator = llvm::ConstantInt::get(CGM.Int64Ty, 0);
5039
}
5140

52-
return llvm::ConstantPtrAuth::get(
53-
pointer, llvm::ConstantInt::get(CGM.Int32Ty, key), integerDiscriminator,
54-
addressDiscriminator);
41+
return llvm::ConstantPtrAuth::get(Pointer,
42+
llvm::ConstantInt::get(CGM.Int32Ty, Key),
43+
IntegerDiscriminator, AddressDiscriminator);
5544
}
5645

5746
llvm::Constant *
58-
CodeGenModule::getConstantSignedPointer(llvm::Constant *pointer,
59-
unsigned key,
60-
llvm::Constant *storageAddress,
61-
llvm::Constant *otherDiscriminator) {
62-
// Unique based on the underlying value, not a signing of it.
63-
auto stripped = pointer->stripPointerCasts();
47+
CodeGenModule::getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
48+
llvm::Constant *StorageAddress,
49+
llvm::Constant *OtherDiscriminator) {
50+
llvm::Constant *Stripped = Pointer->stripPointerCasts();
6451

6552
// Build the constant.
66-
return buildConstantAddress(*this, stripped, key, storageAddress,
67-
otherDiscriminator);
53+
return buildConstantAddress(*this, Stripped, Key, StorageAddress,
54+
OtherDiscriminator);
6855
}
6956

7057
llvm::Constant *
71-
CodeGen::getConstantSignedPointer(CodeGenModule &CGM,
72-
llvm::Constant *pointer, unsigned key,
73-
llvm::Constant *storageAddress,
74-
llvm::Constant *otherDiscriminator) {
75-
return CGM.getConstantSignedPointer(pointer, key, storageAddress,
76-
otherDiscriminator);
58+
CodeGen::getConstantSignedPointer(CodeGenModule &CGM, llvm::Constant *Pointer,
59+
unsigned Key, llvm::Constant *StorageAddress,
60+
llvm::Constant *OtherDiscriminator) {
61+
return CGM.getConstantSignedPointer(Pointer, Key, StorageAddress,
62+
OtherDiscriminator);
7763
}

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -937,10 +937,10 @@ class CodeGenModule : public CodeGenTypeCache {
937937
// Return the function body address of the given function.
938938
llvm::Constant *GetFunctionStart(const ValueDecl *Decl);
939939

940-
llvm::Constant *getConstantSignedPointer(llvm::Constant *pointer,
941-
unsigned key,
942-
llvm::Constant *storageAddress,
943-
llvm::Constant *extraDiscrim);
940+
llvm::Constant *getConstantSignedPointer(llvm::Constant *Pointer,
941+
unsigned Key,
942+
llvm::Constant *StorageAddress,
943+
llvm::Constant *ExtraDiscrim);
944944

945945
// Return whether RTTI information should be emitted for this target.
946946
bool shouldEmitRTTI(bool ForEH = false) {

clang/lib/Sema/SemaChecking.cpp

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,18 +2043,17 @@ bool Sema::checkConstantPointerAuthKey(Expr *Arg, unsigned &Result) {
20432043
static std::pair<const ValueDecl *, CharUnits>
20442044
findConstantBaseAndOffset(Sema &S, Expr *E) {
20452045
// Must evaluate as a pointer.
2046-
Expr::EvalResult result;
2047-
if (!E->EvaluateAsRValue(result, S.Context) ||
2048-
!result.Val.isLValue())
2046+
Expr::EvalResult Result;
2047+
if (!E->EvaluateAsRValue(Result, S.Context) || !Result.Val.isLValue())
20492048
return std::make_pair(nullptr, CharUnits());
20502049

20512050
// Base must be a declaration and can't be weakly imported.
2052-
auto baseDecl =
2053-
result.Val.getLValueBase().dyn_cast<const ValueDecl *>();
2054-
if (!baseDecl || baseDecl->hasAttr<WeakRefAttr>())
2051+
const auto *BaseDecl =
2052+
Result.Val.getLValueBase().dyn_cast<const ValueDecl *>();
2053+
if (!BaseDecl || BaseDecl->hasAttr<WeakRefAttr>())
20552054
return std::make_pair(nullptr, CharUnits());
20562055

2057-
return std::make_pair(baseDecl, result.Val.getLValueOffset());
2056+
return std::make_pair(BaseDecl, Result.Val.getLValueOffset());
20582057
}
20592058

20602059
static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
@@ -2120,73 +2119,73 @@ static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
21202119
// The main argument.
21212120
if (OpKind == PAO_Sign) {
21222121
// Require the value we're signing to have a special form.
2123-
auto result = findConstantBaseAndOffset(S, Arg);
2124-
bool invalid;
2122+
auto BaseOffsetPair = findConstantBaseAndOffset(S, Arg);
2123+
bool Invalid;
21252124

21262125
// Must be rooted in a declaration reference.
2127-
if (!result.first) {
2128-
invalid = true;
2126+
if (!BaseOffsetPair.first) {
2127+
Invalid = true;
21292128

2130-
// If it's a function declaration, we can't have an offset.
2131-
} else if (isa<FunctionDecl>(result.first)) {
2132-
invalid = !result.second.isZero();
2129+
// If it's a function declaration, we can't have an offset.
2130+
} else if (isa<FunctionDecl>(BaseOffsetPair.first)) {
2131+
Invalid = !BaseOffsetPair.second.isZero();
21332132

2134-
// Otherwise we're fine.
2133+
// Otherwise we're fine.
21352134
} else {
2136-
invalid = false;
2135+
Invalid = false;
21372136
}
21382137

2139-
if (invalid) {
2138+
if (Invalid) {
21402139
S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_pointer);
21412140
}
2142-
return invalid;
2141+
return Invalid;
21432142
}
21442143

21452144
// The discriminator argument.
21462145
assert(OpKind == PAO_Discriminator);
21472146

21482147
// Must be a pointer or integer or blend thereof.
2149-
Expr *pointer = nullptr;
2150-
Expr *integer = nullptr;
2151-
if (auto call = dyn_cast<CallExpr>(Arg->IgnoreParens())) {
2152-
if (call->getBuiltinCallee() ==
2153-
Builtin::BI__builtin_ptrauth_blend_discriminator) {
2154-
pointer = call->getArg(0);
2155-
integer = call->getArg(1);
2148+
Expr *Pointer = nullptr;
2149+
Expr *Integer = nullptr;
2150+
if (auto *Call = dyn_cast<CallExpr>(Arg->IgnoreParens())) {
2151+
if (Call->getBuiltinCallee() ==
2152+
Builtin::BI__builtin_ptrauth_blend_discriminator) {
2153+
Pointer = Call->getArg(0);
2154+
Integer = Call->getArg(1);
21562155
}
21572156
}
2158-
if (!pointer && !integer) {
2157+
if (!Pointer && !Integer) {
21592158
if (Arg->getType()->isPointerType())
2160-
pointer = Arg;
2159+
Pointer = Arg;
21612160
else
2162-
integer = Arg;
2161+
Integer = Arg;
21632162
}
21642163

21652164
// Check the pointer.
2166-
bool invalid = false;
2167-
if (pointer) {
2168-
assert(pointer->getType()->isPointerType());
2165+
bool Invalid = false;
2166+
if (Pointer) {
2167+
assert(Pointer->getType()->isPointerType());
21692168

21702169
// TODO: if we're initializing a global, check that the address is
21712170
// somehow related to what we're initializing. This probably will
21722171
// never really be feasible and we'll have to catch it at link-time.
2173-
auto result = findConstantBaseAndOffset(S, pointer);
2174-
if (!result.first || !isa<VarDecl>(result.first)) {
2175-
invalid = true;
2172+
auto BaseOffsetPair = findConstantBaseAndOffset(S, Pointer);
2173+
if (!BaseOffsetPair.first || !isa<VarDecl>(BaseOffsetPair.first)) {
2174+
Invalid = true;
21762175
}
21772176
}
21782177

21792178
// Check the integer.
2180-
if (integer) {
2181-
assert(integer->getType()->isIntegerType());
2182-
if (!integer->isEvaluatable(S.Context))
2183-
invalid = true;
2179+
if (Integer) {
2180+
assert(Integer->getType()->isIntegerType());
2181+
if (!Integer->isEvaluatable(S.Context))
2182+
Invalid = true;
21842183
}
21852184

2186-
if (invalid) {
2185+
if (Invalid) {
21872186
S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_discriminator);
21882187
}
2189-
return invalid;
2188+
return Invalid;
21902189
}
21912190

21922191
static ExprResult PointerAuthStrip(Sema &S, CallExpr *Call) {
@@ -3040,13 +3039,13 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
30403039
return PointerAuthBlendDiscriminator(*this, TheCall);
30413040
case Builtin::BI__builtin_ptrauth_sign_constant:
30423041
return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign,
3043-
/*constant*/ true);
3042+
/*RequireConstant=*/true);
30443043
case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
30453044
return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign,
3046-
/*constant*/ false);
3045+
/*RequireConstant=*/false);
30473046
case Builtin::BI__builtin_ptrauth_auth:
30483047
return PointerAuthSignOrAuth(*this, TheCall, PAO_Auth,
3049-
/*constant*/ false);
3048+
/*RequireConstant=*/false);
30503049
case Builtin::BI__builtin_ptrauth_sign_generic_data:
30513050
return PointerAuthSignGenericData(*this, TheCall);
30523051
case Builtin::BI__builtin_ptrauth_auth_and_resign:

clang/test/CodeGen/ptrauth-intrinsic-sign-constant.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -triple arm64-apple-ios -fptrauth-intrinsics -emit-llvm %s -o - | FileCheck %s
2+
// RUN: %clang_cc1 -triple aarch64-elf -fptrauth-intrinsics -emit-llvm %s -o - | FileCheck %s
23

34
extern int external;
45

@@ -12,7 +13,7 @@ void *ptr2 = __builtin_ptrauth_sign_constant(&external, 2, __builtin_ptrauth_ble
1213
void *ptr3;
1314

1415
void test_sign_constant_code() {
15-
// CHECK-LABEL: define void @test_sign_constant_code()
16+
// CHECK-LABEL: define {{.*}}void @test_sign_constant_code()
1617
// CHECK-NEXT: entry:
1718
// CHECK-NEXT: store ptr ptrauth (ptr @external, i32 2, i64 1234), ptr @ptr3, align 8
1819
// CHECK-NEXT: ret void

0 commit comments

Comments
 (0)