Skip to content

Commit f0a065d

Browse files
committed
Merge remote-tracking branch 'origin/users/ahmedbougacha/ptrauth-sign-constant' into users/ahmedbougacha/ptrauth-function-pointers
2 parents c260f8d + 33cdfdd commit f0a065d

File tree

10 files changed

+171
-140
lines changed

10 files changed

+171
-140
lines changed

clang/docs/PointerAuthentication.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,21 @@ be done in a single instruction with an immediate integer.
328328
``pointer`` must have pointer type, and ``integer`` must have integer type. The
329329
result has type ``ptrauth_extra_data_t``.
330330

331+
``ptrauth_string_discriminator``
332+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
333+
334+
.. code-block:: c
335+
336+
ptrauth_string_discriminator(string)
337+
338+
Produce a discriminator value for the given string. ``string`` must be
339+
a string literal of ``char`` character type. The result has type
340+
``ptrauth_extra_data_t``.
341+
342+
The result is always a constant expression. The result value is never zero and
343+
always within range for both the ``__ptrauth`` qualifier and
344+
``ptrauth_blend_discriminator``.
345+
331346
``ptrauth_strip``
332347
^^^^^^^^^^^^^^^^^
333348

@@ -339,6 +354,23 @@ Given that ``signedPointer`` matches the layout for signed pointers signed with
339354
the given key, extract the raw pointer from it. This operation does not trap
340355
and cannot fail, even if the pointer is not validly signed.
341356

357+
``ptrauth_sign_constant``
358+
^^^^^^^^^^^^^^^^^^^^^^^^^
359+
360+
.. code-block:: c
361+
362+
ptrauth_sign_constant(pointer, key, discriminator)
363+
364+
Return a signed pointer for a constant address in a manner which guarantees
365+
a non-attackable sequence.
366+
367+
``pointer`` must be a constant expression of pointer type which evaluates to
368+
a non-null pointer. The result will have the same type as ``discriminator``.
369+
370+
Calls to this are constant expressions if the discriminator is a null-pointer
371+
constant expression or an integer constant expression. Implementations may
372+
allow other pointer expressions as well.
373+
342374
``ptrauth_sign_unauthenticated``
343375
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
344376

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/AST/ExprConstant.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12586,9 +12586,10 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1258612586
return Visit(E->getArg(0));
1258712587

1258812588
case Builtin::BI__builtin_ptrauth_string_discriminator: {
12589-
auto literal = cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts());
12590-
auto result = getPointerAuthStableSipHash16(literal->getString());
12591-
return Success(result, E);
12589+
const auto *Literal =
12590+
cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts());
12591+
uint64_t Result = getPointerAuthStableSipHash16(Literal->getString());
12592+
return Success(Result, E);
1259212593
}
1259312594

1259412595
case Builtin::BI__builtin_ffs:

clang/lib/CodeGen/CGExprConstant.cpp

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

20912091
ConstantLValue
20922092
ConstantLValueEmitter::emitPointerAuthSignConstant(const CallExpr *E) {
2093-
auto unsignedPointer = emitPointerAuthPointer(E->getArg(0));
2094-
auto key = emitPointerAuthKey(E->getArg(1));
2095-
llvm::Constant *storageAddress;
2096-
llvm::Constant *otherDiscriminator;
2097-
std::tie(storageAddress, otherDiscriminator) =
2098-
emitPointerAuthDiscriminator(E->getArg(2));
2093+
llvm::Constant *UnsignedPointer = emitPointerAuthPointer(E->getArg(0));
2094+
unsigned Key = emitPointerAuthKey(E->getArg(1));
2095+
llvm::Constant *StorageAddress;
2096+
llvm::Constant *OtherDiscriminator;
2097+
std::tie(StorageAddress, OtherDiscriminator) =
2098+
emitPointerAuthDiscriminator(E->getArg(2));
20992099

2100-
auto signedPointer =
2101-
CGM.getConstantSignedPointer(unsignedPointer, key, storageAddress,
2102-
otherDiscriminator);
2103-
return signedPointer;
2100+
llvm::Constant *SignedPointer = CGM.getConstantSignedPointer(
2101+
UnsignedPointer, Key, StorageAddress, OtherDiscriminator);
2102+
return SignedPointer;
21042103
}
21052104

21062105
llvm::Constant *ConstantLValueEmitter::emitPointerAuthPointer(const Expr *E) {
2107-
Expr::EvalResult result;
2108-
bool succeeded = E->EvaluateAsRValue(result, CGM.getContext());
2109-
assert(succeeded); (void) succeeded;
2106+
Expr::EvalResult Result;
2107+
bool Succeeded = E->EvaluateAsRValue(Result, CGM.getContext());
2108+
assert(Succeeded);
2109+
(void)Succeeded;
21102110

21112111
// The assertions here are all checked by Sema.
2112-
assert(result.Val.isLValue());
2112+
assert(Result.Val.isLValue());
21132113
return ConstantEmitter(CGM, Emitter.CGF)
2114-
.emitAbstract(E->getExprLoc(), result.Val, E->getType());
2114+
.emitAbstract(E->getExprLoc(), Result.Val, E->getType());
21152115
}
21162116

21172117
unsigned ConstantLValueEmitter::emitPointerAuthKey(const Expr *E) {
21182118
return E->EvaluateKnownConstInt(CGM.getContext()).getZExtValue();
21192119
}
21202120

2121-
std::pair<llvm::Constant*, llvm::Constant*>
2121+
std::pair<llvm::Constant *, llvm::Constant *>
21222122
ConstantLValueEmitter::emitPointerAuthDiscriminator(const Expr *E) {
21232123
E = E->IgnoreParens();
21242124

2125-
if (auto call = dyn_cast<CallExpr>(E)) {
2126-
if (call->getBuiltinCallee() ==
2127-
Builtin::BI__builtin_ptrauth_blend_discriminator) {
2128-
auto pointer = ConstantEmitter(CGM).emitAbstract(call->getArg(0),
2129-
call->getArg(0)->getType());
2130-
auto extra = ConstantEmitter(CGM).emitAbstract(call->getArg(1),
2131-
call->getArg(1)->getType());
2132-
return { pointer, extra };
2125+
if (auto *Call = dyn_cast<CallExpr>(E)) {
2126+
if (Call->getBuiltinCallee() ==
2127+
Builtin::BI__builtin_ptrauth_blend_discriminator) {
2128+
llvm::Constant *Pointer = ConstantEmitter(CGM).emitAbstract(
2129+
Call->getArg(0), Call->getArg(0)->getType());
2130+
llvm::Constant *Extra = ConstantEmitter(CGM).emitAbstract(
2131+
Call->getArg(1), Call->getArg(1)->getType());
2132+
return {Pointer, Extra};
21332133
}
21342134
}
21352135

2136-
auto result = ConstantEmitter(CGM).emitAbstract(E, E->getType());
2137-
if (result->getType()->isPointerTy())
2138-
return { result, nullptr };
2136+
llvm::Constant *Result = ConstantEmitter(CGM).emitAbstract(E, E->getType());
2137+
if (Result->getType()->isPointerTy())
2138+
return {Result, nullptr};
21392139
else
2140-
return { nullptr, result };
2140+
return {nullptr, Result};
21412141
}
21422142

21432143
ConstantLValue

clang/lib/CodeGen/CGPointerAuth.cpp

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,8 @@
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;
@@ -48,50 +37,47 @@ CGPointerAuthInfo CodeGenModule::getFunctionPointerAuthInfo(QualType T) {
4837

4938
/// Build a signed-pointer "ptrauth" constant.
5039
static llvm::ConstantPtrAuth *
51-
buildConstantAddress(CodeGenModule &CGM, llvm::Constant *pointer, unsigned key,
52-
llvm::Constant *storageAddress,
53-
llvm::Constant *otherDiscriminator) {
54-
llvm::Constant *addressDiscriminator = nullptr;
55-
if (storageAddress) {
56-
addressDiscriminator = storageAddress;
57-
assert(storageAddress->getType() == CGM.UnqualPtrTy);
40+
buildConstantAddress(CodeGenModule &CGM, llvm::Constant *Pointer, unsigned Key,
41+
llvm::Constant *StorageAddress,
42+
llvm::Constant *OtherDiscriminator) {
43+
llvm::Constant *AddressDiscriminator = nullptr;
44+
if (StorageAddress) {
45+
AddressDiscriminator = StorageAddress;
46+
assert(StorageAddress->getType() == CGM.UnqualPtrTy);
5847
} else {
59-
addressDiscriminator = llvm::Constant::getNullValue(CGM.UnqualPtrTy);
48+
AddressDiscriminator = llvm::Constant::getNullValue(CGM.UnqualPtrTy);
6049
}
6150

62-
llvm::ConstantInt *integerDiscriminator = nullptr;
63-
if (otherDiscriminator) {
64-
assert(otherDiscriminator->getType() == CGM.Int64Ty);
65-
integerDiscriminator = cast<llvm::ConstantInt>(otherDiscriminator);
51+
llvm::ConstantInt *IntegerDiscriminator = nullptr;
52+
if (OtherDiscriminator) {
53+
assert(OtherDiscriminator->getType() == CGM.Int64Ty);
54+
IntegerDiscriminator = cast<llvm::ConstantInt>(OtherDiscriminator);
6655
} else {
67-
integerDiscriminator = llvm::ConstantInt::get(CGM.Int64Ty, 0);
56+
IntegerDiscriminator = llvm::ConstantInt::get(CGM.Int64Ty, 0);
6857
}
6958

70-
return llvm::ConstantPtrAuth::get(
71-
pointer, llvm::ConstantInt::get(CGM.Int32Ty, key), integerDiscriminator,
72-
addressDiscriminator);
59+
return llvm::ConstantPtrAuth::get(Pointer,
60+
llvm::ConstantInt::get(CGM.Int32Ty, Key),
61+
IntegerDiscriminator, AddressDiscriminator);
7362
}
7463

7564
llvm::Constant *
76-
CodeGenModule::getConstantSignedPointer(llvm::Constant *pointer,
77-
unsigned key,
78-
llvm::Constant *storageAddress,
79-
llvm::Constant *otherDiscriminator) {
80-
// Unique based on the underlying value, not a signing of it.
81-
auto stripped = pointer->stripPointerCasts();
65+
CodeGenModule::getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
66+
llvm::Constant *StorageAddress,
67+
llvm::Constant *OtherDiscriminator) {
68+
llvm::Constant *Stripped = Pointer->stripPointerCasts();
8269

8370
// Build the constant.
84-
return buildConstantAddress(*this, stripped, key, storageAddress,
85-
otherDiscriminator);
71+
return buildConstantAddress(*this, Stripped, Key, StorageAddress,
72+
OtherDiscriminator);
8673
}
8774

8875
llvm::Constant *
89-
CodeGen::getConstantSignedPointer(CodeGenModule &CGM,
90-
llvm::Constant *pointer, unsigned key,
91-
llvm::Constant *storageAddress,
92-
llvm::Constant *otherDiscriminator) {
93-
return CGM.getConstantSignedPointer(pointer, key, storageAddress,
94-
otherDiscriminator);
76+
CodeGen::getConstantSignedPointer(CodeGenModule &CGM, llvm::Constant *Pointer,
77+
unsigned Key, llvm::Constant *StorageAddress,
78+
llvm::Constant *OtherDiscriminator) {
79+
return CGM.getConstantSignedPointer(Pointer, Key, StorageAddress,
80+
OtherDiscriminator);
9581
}
9682

9783
/// If applicable, sign a given constant function pointer with the ABI rules for

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,10 +965,10 @@ class CodeGenModule : public CodeGenTypeCache {
965965
GlobalDecl SchemaDecl,
966966
QualType SchemaType);
967967

968-
llvm::Constant *getConstantSignedPointer(llvm::Constant *pointer,
969-
unsigned key,
970-
llvm::Constant *storageAddress,
971-
llvm::Constant *extraDiscrim);
968+
llvm::Constant *getConstantSignedPointer(llvm::Constant *Pointer,
969+
unsigned Key,
970+
llvm::Constant *StorageAddress,
971+
llvm::Constant *ExtraDiscrim);
972972

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

0 commit comments

Comments
 (0)