Skip to content

Commit 33cdfdd

Browse files
committed
Address feedback.
- test elf as well - rename every variable.
1 parent fea3bc2 commit 33cdfdd

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
@@ -938,10 +938,10 @@ class CodeGenModule : public CodeGenTypeCache {
938938
// Return the function body address of the given function.
939939
llvm::Constant *GetFunctionStart(const ValueDecl *Decl);
940940

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

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

clang/lib/Sema/SemaChecking.cpp

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,18 +2033,17 @@ bool Sema::checkConstantPointerAuthKey(Expr *Arg, unsigned &Result) {
20332033
static std::pair<const ValueDecl *, CharUnits>
20342034
findConstantBaseAndOffset(Sema &S, Expr *E) {
20352035
// Must evaluate as a pointer.
2036-
Expr::EvalResult result;
2037-
if (!E->EvaluateAsRValue(result, S.Context) ||
2038-
!result.Val.isLValue())
2036+
Expr::EvalResult Result;
2037+
if (!E->EvaluateAsRValue(Result, S.Context) || !Result.Val.isLValue())
20392038
return std::make_pair(nullptr, CharUnits());
20402039

20412040
// Base must be a declaration and can't be weakly imported.
2042-
auto baseDecl =
2043-
result.Val.getLValueBase().dyn_cast<const ValueDecl *>();
2044-
if (!baseDecl || baseDecl->hasAttr<WeakRefAttr>())
2041+
const auto *BaseDecl =
2042+
Result.Val.getLValueBase().dyn_cast<const ValueDecl *>();
2043+
if (!BaseDecl || BaseDecl->hasAttr<WeakRefAttr>())
20452044
return std::make_pair(nullptr, CharUnits());
20462045

2047-
return std::make_pair(baseDecl, result.Val.getLValueOffset());
2046+
return std::make_pair(BaseDecl, Result.Val.getLValueOffset());
20482047
}
20492048

20502049
static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
@@ -2110,73 +2109,73 @@ static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
21102109
// The main argument.
21112110
if (OpKind == PAO_Sign) {
21122111
// Require the value we're signing to have a special form.
2113-
auto result = findConstantBaseAndOffset(S, Arg);
2114-
bool invalid;
2112+
auto BaseOffsetPair = findConstantBaseAndOffset(S, Arg);
2113+
bool Invalid;
21152114

21162115
// Must be rooted in a declaration reference.
2117-
if (!result.first) {
2118-
invalid = true;
2116+
if (!BaseOffsetPair.first) {
2117+
Invalid = true;
21192118

2120-
// If it's a function declaration, we can't have an offset.
2121-
} else if (isa<FunctionDecl>(result.first)) {
2122-
invalid = !result.second.isZero();
2119+
// If it's a function declaration, we can't have an offset.
2120+
} else if (isa<FunctionDecl>(BaseOffsetPair.first)) {
2121+
Invalid = !BaseOffsetPair.second.isZero();
21232122

2124-
// Otherwise we're fine.
2123+
// Otherwise we're fine.
21252124
} else {
2126-
invalid = false;
2125+
Invalid = false;
21272126
}
21282127

2129-
if (invalid) {
2128+
if (Invalid) {
21302129
S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_pointer);
21312130
}
2132-
return invalid;
2131+
return Invalid;
21332132
}
21342133

21352134
// The discriminator argument.
21362135
assert(OpKind == PAO_Discriminator);
21372136

21382137
// Must be a pointer or integer or blend thereof.
2139-
Expr *pointer = nullptr;
2140-
Expr *integer = nullptr;
2141-
if (auto call = dyn_cast<CallExpr>(Arg->IgnoreParens())) {
2142-
if (call->getBuiltinCallee() ==
2143-
Builtin::BI__builtin_ptrauth_blend_discriminator) {
2144-
pointer = call->getArg(0);
2145-
integer = call->getArg(1);
2138+
Expr *Pointer = nullptr;
2139+
Expr *Integer = nullptr;
2140+
if (auto *Call = dyn_cast<CallExpr>(Arg->IgnoreParens())) {
2141+
if (Call->getBuiltinCallee() ==
2142+
Builtin::BI__builtin_ptrauth_blend_discriminator) {
2143+
Pointer = Call->getArg(0);
2144+
Integer = Call->getArg(1);
21462145
}
21472146
}
2148-
if (!pointer && !integer) {
2147+
if (!Pointer && !Integer) {
21492148
if (Arg->getType()->isPointerType())
2150-
pointer = Arg;
2149+
Pointer = Arg;
21512150
else
2152-
integer = Arg;
2151+
Integer = Arg;
21532152
}
21542153

21552154
// Check the pointer.
2156-
bool invalid = false;
2157-
if (pointer) {
2158-
assert(pointer->getType()->isPointerType());
2155+
bool Invalid = false;
2156+
if (Pointer) {
2157+
assert(Pointer->getType()->isPointerType());
21592158

21602159
// TODO: if we're initializing a global, check that the address is
21612160
// somehow related to what we're initializing. This probably will
21622161
// never really be feasible and we'll have to catch it at link-time.
2163-
auto result = findConstantBaseAndOffset(S, pointer);
2164-
if (!result.first || !isa<VarDecl>(result.first)) {
2165-
invalid = true;
2162+
auto BaseOffsetPair = findConstantBaseAndOffset(S, Pointer);
2163+
if (!BaseOffsetPair.first || !isa<VarDecl>(BaseOffsetPair.first)) {
2164+
Invalid = true;
21662165
}
21672166
}
21682167

21692168
// Check the integer.
2170-
if (integer) {
2171-
assert(integer->getType()->isIntegerType());
2172-
if (!integer->isEvaluatable(S.Context))
2173-
invalid = true;
2169+
if (Integer) {
2170+
assert(Integer->getType()->isIntegerType());
2171+
if (!Integer->isEvaluatable(S.Context))
2172+
Invalid = true;
21742173
}
21752174

2176-
if (invalid) {
2175+
if (Invalid) {
21772176
S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_discriminator);
21782177
}
2179-
return invalid;
2178+
return Invalid;
21802179
}
21812180

21822181
static ExprResult PointerAuthStrip(Sema &S, CallExpr *Call) {
@@ -3029,13 +3028,13 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
30293028
return PointerAuthBlendDiscriminator(*this, TheCall);
30303029
case Builtin::BI__builtin_ptrauth_sign_constant:
30313030
return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign,
3032-
/*constant*/ true);
3031+
/*RequireConstant=*/true);
30333032
case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
30343033
return PointerAuthSignOrAuth(*this, TheCall, PAO_Sign,
3035-
/*constant*/ false);
3034+
/*RequireConstant=*/false);
30363035
case Builtin::BI__builtin_ptrauth_auth:
30373036
return PointerAuthSignOrAuth(*this, TheCall, PAO_Auth,
3038-
/*constant*/ false);
3037+
/*RequireConstant=*/false);
30393038
case Builtin::BI__builtin_ptrauth_sign_generic_data:
30403039
return PointerAuthSignGenericData(*this, TheCall);
30413040
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)