Skip to content

Commit 6e7ba73

Browse files
committed
Simplify some functionality and address review feedback.
- don't reject weakref base pointers (allowed in quals now anyway) - fold buildConstantAddress into getConstantSignedPointer (originally to provide a caching layer for global wrappers but not needed anymore) - remove stripPointerCasts from it (originally for typed pointers) - use ConstantInt for the integer/other component of discriminators - remove CodeGen:: helper (originally for swift but not required) - rejigger tests; try for parity in fn body and global init - add a couple missing tests - tweak doc/header comment - adopt 'can be used in cst exprs' phrasing from LangExts doc - various nits, naming, and format
1 parent 88b097d commit 6e7ba73

File tree

8 files changed

+132
-110
lines changed

8 files changed

+132
-110
lines changed

clang/docs/PointerAuthentication.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,14 @@ a non-attackable sequence.
368368

369369
``pointer`` must be a constant expression of pointer type which evaluates to
370370
a non-null pointer. The result will have the same type as ``discriminator``.
371+
``pointer`` must be a constant expression of pointer type which evaluates to
372+
a non-null pointer.
373+
``key`` must be a constant expression of type ``ptrauth_key``.
374+
``discriminator`` must be a constant expression of pointer or integer type;
375+
if an integer, it will be coerced to ``ptrauth_extra_data_t``.
376+
The result will have the same type as ``pointer``.
371377

372-
Calls to this are constant expressions if the discriminator is a null-pointer
373-
constant expression or an integer constant expression. Implementations may
374-
allow other pointer expressions as well.
378+
This can be used in constant expressions.
375379

376380
``ptrauth_sign_unauthenticated``
377381
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/CodeGen/CodeGenABITypes.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ llvm::Type *convertTypeForMemory(CodeGenModule &CGM, QualType T);
104104
unsigned getLLVMFieldNumber(CodeGenModule &CGM,
105105
const RecordDecl *RD, const FieldDecl *FD);
106106

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

clang/lib/CodeGen/CGExprConstant.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,7 +1859,7 @@ class ConstantLValueEmitter : public ConstStmtVisitor<ConstantLValueEmitter,
18591859
ConstantLValue emitPointerAuthSignConstant(const CallExpr *E);
18601860
llvm::Constant *emitPointerAuthPointer(const Expr *E);
18611861
unsigned emitPointerAuthKey(const Expr *E);
1862-
std::pair<llvm::Constant*, llvm::Constant*>
1862+
std::pair<llvm::Constant *, llvm::ConstantInt *>
18631863
emitPointerAuthDiscriminator(const Expr *E);
18641864

18651865
bool hasNonZeroOffset() const {
@@ -2075,9 +2075,7 @@ ConstantLValue
20752075
ConstantLValueEmitter::emitPointerAuthSignConstant(const CallExpr *E) {
20762076
llvm::Constant *UnsignedPointer = emitPointerAuthPointer(E->getArg(0));
20772077
unsigned Key = emitPointerAuthKey(E->getArg(1));
2078-
llvm::Constant *StorageAddress;
2079-
llvm::Constant *OtherDiscriminator;
2080-
std::tie(StorageAddress, OtherDiscriminator) =
2078+
auto [StorageAddress, OtherDiscriminator] =
20812079
emitPointerAuthDiscriminator(E->getArg(2));
20822080

20832081
llvm::Constant *SignedPointer = CGM.getConstantSignedPointer(
@@ -2101,26 +2099,25 @@ unsigned ConstantLValueEmitter::emitPointerAuthKey(const Expr *E) {
21012099
return E->EvaluateKnownConstInt(CGM.getContext()).getZExtValue();
21022100
}
21032101

2104-
std::pair<llvm::Constant *, llvm::Constant *>
2102+
std::pair<llvm::Constant *, llvm::ConstantInt *>
21052103
ConstantLValueEmitter::emitPointerAuthDiscriminator(const Expr *E) {
21062104
E = E->IgnoreParens();
21072105

2108-
if (auto *Call = dyn_cast<CallExpr>(E)) {
2106+
if (const auto *Call = dyn_cast<CallExpr>(E)) {
21092107
if (Call->getBuiltinCallee() ==
21102108
Builtin::BI__builtin_ptrauth_blend_discriminator) {
21112109
llvm::Constant *Pointer = ConstantEmitter(CGM).emitAbstract(
21122110
Call->getArg(0), Call->getArg(0)->getType());
2113-
llvm::Constant *Extra = ConstantEmitter(CGM).emitAbstract(
2114-
Call->getArg(1), Call->getArg(1)->getType());
2111+
auto *Extra = cast<llvm::ConstantInt>(ConstantEmitter(CGM).emitAbstract(
2112+
Call->getArg(1), Call->getArg(1)->getType()));
21152113
return {Pointer, Extra};
21162114
}
21172115
}
21182116

21192117
llvm::Constant *Result = ConstantEmitter(CGM).emitAbstract(E, E->getType());
21202118
if (Result->getType()->isPointerTy())
21212119
return {Result, nullptr};
2122-
else
2123-
return {nullptr, Result};
2120+
return {nullptr, cast<llvm::ConstantInt>(Result)};
21242121
}
21252122

21262123
ConstantLValue

clang/lib/CodeGen/CGPointerAuth.cpp

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,27 @@
1717
using namespace clang;
1818
using namespace CodeGen;
1919

20-
/// Build a signed-pointer "ptrauth" constant.
21-
static llvm::ConstantPtrAuth *
22-
buildConstantAddress(CodeGenModule &CGM, llvm::Constant *Pointer, unsigned Key,
23-
llvm::Constant *StorageAddress,
24-
llvm::Constant *OtherDiscriminator) {
25-
llvm::Constant *AddressDiscriminator = nullptr;
20+
llvm::Constant *
21+
CodeGenModule::getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
22+
llvm::Constant *StorageAddress,
23+
llvm::ConstantInt *OtherDiscriminator) {
24+
llvm::Constant *AddressDiscriminator;
2625
if (StorageAddress) {
26+
assert(StorageAddress->getType() == UnqualPtrTy);
2727
AddressDiscriminator = StorageAddress;
28-
assert(StorageAddress->getType() == CGM.UnqualPtrTy);
2928
} else {
30-
AddressDiscriminator = llvm::Constant::getNullValue(CGM.UnqualPtrTy);
29+
AddressDiscriminator = llvm::Constant::getNullValue(UnqualPtrTy);
3130
}
3231

33-
llvm::ConstantInt *IntegerDiscriminator = nullptr;
32+
llvm::ConstantInt *IntegerDiscriminator;
3433
if (OtherDiscriminator) {
35-
assert(OtherDiscriminator->getType() == CGM.Int64Ty);
36-
IntegerDiscriminator = cast<llvm::ConstantInt>(OtherDiscriminator);
34+
assert(OtherDiscriminator->getType() == Int64Ty);
35+
IntegerDiscriminator = OtherDiscriminator;
3736
} else {
38-
IntegerDiscriminator = llvm::ConstantInt::get(CGM.Int64Ty, 0);
37+
IntegerDiscriminator = llvm::ConstantInt::get(Int64Ty, 0);
3938
}
4039

4140
return llvm::ConstantPtrAuth::get(Pointer,
42-
llvm::ConstantInt::get(CGM.Int32Ty, Key),
41+
llvm::ConstantInt::get(Int32Ty, Key),
4342
IntegerDiscriminator, AddressDiscriminator);
4443
}
45-
46-
llvm::Constant *
47-
CodeGenModule::getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
48-
llvm::Constant *StorageAddress,
49-
llvm::Constant *OtherDiscriminator) {
50-
llvm::Constant *Stripped = Pointer->stripPointerCasts();
51-
52-
// Build the constant.
53-
return buildConstantAddress(*this, Stripped, Key, StorageAddress,
54-
OtherDiscriminator);
55-
}
56-
57-
llvm::Constant *
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);
63-
}

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 *
941+
getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
942+
llvm::Constant *StorageAddress,
943+
llvm::ConstantInt *OtherDiscriminator);
944944

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

clang/lib/Headers/ptrauth.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
7878
#define ptrauth_blend_discriminator(__pointer, __integer) \
7979
__builtin_ptrauth_blend_discriminator(__pointer, __integer)
8080

81-
/* Add a signature to the given pointer value using a specific key,
82-
using the given extra data as a salt to the signing process.
81+
/* Return a signed pointer for a constant address in a manner which guarantees
82+
a non-attackable sequence.
8383
84-
The value must be a constant expression of pointer type.
84+
The value must be a constant expression of pointer type which evaluates to
85+
a non-null pointer.
8586
The key must be a constant expression of type ptrauth_key.
8687
The extra data must be a constant expression of pointer or integer type;
8788
if an integer, it will be coerced to ptrauth_extra_data_t.
8889
The result will have the same type as the original value.
8990
90-
This is a constant expression if the extra data is an integer or
91-
null pointer constant. */
91+
This can be used in constant expressions. */
9292
#define ptrauth_sign_constant(__value, __key, __data) \
9393
__builtin_ptrauth_sign_constant(__value, __key, __data)
9494

clang/lib/Sema/SemaChecking.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,19 +2045,17 @@ findConstantBaseAndOffset(Sema &S, Expr *E) {
20452045
// Must evaluate as a pointer.
20462046
Expr::EvalResult Result;
20472047
if (!E->EvaluateAsRValue(Result, S.Context) || !Result.Val.isLValue())
2048-
return std::make_pair(nullptr, CharUnits());
2048+
return {nullptr, CharUnits()};
20492049

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

2056-
return std::make_pair(BaseDecl, Result.Val.getLValueOffset());
2055+
return {BaseDecl, Result.Val.getLValueOffset()};
20572056
}
20582057

2059-
static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
2060-
PointerAuthOpKind OpKind,
2058+
static bool checkPointerAuthValue(Sema &S, Expr *&Arg, PointerAuthOpKind OpKind,
20612059
bool RequireConstant = false) {
20622060
if (Arg->hasPlaceholderType()) {
20632061
ExprResult R = S.CheckPlaceholderExpr(Arg);
@@ -2119,25 +2117,23 @@ static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
21192117
// The main argument.
21202118
if (OpKind == PAO_Sign) {
21212119
// Require the value we're signing to have a special form.
2122-
auto BaseOffsetPair = findConstantBaseAndOffset(S, Arg);
2120+
auto [BaseDecl, Offset] = findConstantBaseAndOffset(S, Arg);
21232121
bool Invalid;
21242122

21252123
// Must be rooted in a declaration reference.
2126-
if (!BaseOffsetPair.first) {
2124+
if (!BaseDecl)
21272125
Invalid = true;
21282126

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();
2127+
// If it's a function declaration, we can't have an offset.
2128+
else if (isa<FunctionDecl>(BaseDecl))
2129+
Invalid = !Offset.isZero();
21322130

2133-
// Otherwise we're fine.
2134-
} else {
2131+
// Otherwise we're fine.
2132+
else
21352133
Invalid = false;
2136-
}
21372134

2138-
if (Invalid) {
2135+
if (Invalid)
21392136
S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_pointer);
2140-
}
21412137
return Invalid;
21422138
}
21432139

@@ -2169,10 +2165,9 @@ static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
21692165
// TODO: if we're initializing a global, check that the address is
21702166
// somehow related to what we're initializing. This probably will
21712167
// never really be feasible and we'll have to catch it at link-time.
2172-
auto BaseOffsetPair = findConstantBaseAndOffset(S, Pointer);
2173-
if (!BaseOffsetPair.first || !isa<VarDecl>(BaseOffsetPair.first)) {
2168+
auto [BaseDecl, Offset] = findConstantBaseAndOffset(S, Pointer);
2169+
if (!BaseDecl || !isa<VarDecl>(BaseDecl))
21742170
Invalid = true;
2175-
}
21762171
}
21772172

21782173
// Check the integer.
@@ -2182,9 +2177,8 @@ static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
21822177
Invalid = true;
21832178
}
21842179

2185-
if (Invalid) {
2180+
if (Invalid)
21862181
S.Diag(Arg->getExprLoc(), diag::err_ptrauth_bad_constant_discriminator);
2187-
}
21882182
return Invalid;
21892183
}
21902184

0 commit comments

Comments
 (0)