1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
//===--- CGPointerAuth.cpp - IR generation for pointer authentication -----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains common routines relating to the emission of
// pointer authentication operations.
//
//===----------------------------------------------------------------------===//
#include "CodeGenFunction.h"
#include "CodeGenModule.h"
#include "clang/CodeGen/CodeGenABITypes.h"
#include "clang/CodeGen/ConstantInitBuilder.h"
#include "llvm/Support/SipHash.h"
using namespace clang;
using namespace CodeGen;
/// Given a pointer-authentication schema, return a concrete "other"
/// discriminator for it.
llvm::ConstantInt *CodeGenModule::getPointerAuthOtherDiscriminator(
const PointerAuthSchema &Schema, GlobalDecl Decl, QualType Type) {
switch (Schema.getOtherDiscrimination()) {
case PointerAuthSchema::Discrimination::None:
return nullptr;
case PointerAuthSchema::Discrimination::Type:
assert(!Type.isNull() && "type not provided for type-discriminated schema");
return llvm::ConstantInt::get(
IntPtrTy, getContext().getPointerAuthTypeDiscriminator(Type));
case PointerAuthSchema::Discrimination::Decl:
assert(Decl.getDecl() &&
"declaration not provided for decl-discriminated schema");
return llvm::ConstantInt::get(IntPtrTy,
getPointerAuthDeclDiscriminator(Decl));
case PointerAuthSchema::Discrimination::Constant:
return llvm::ConstantInt::get(IntPtrTy, Schema.getConstantDiscrimination());
}
llvm_unreachable("bad discrimination kind");
}
uint16_t CodeGen::getPointerAuthTypeDiscriminator(CodeGenModule &CGM,
QualType FunctionType) {
return CGM.getContext().getPointerAuthTypeDiscriminator(FunctionType);
}
uint16_t CodeGen::getPointerAuthDeclDiscriminator(CodeGenModule &CGM,
GlobalDecl Declaration) {
return CGM.getPointerAuthDeclDiscriminator(Declaration);
}
/// Return the "other" decl-specific discriminator for the given decl.
uint16_t
CodeGenModule::getPointerAuthDeclDiscriminator(GlobalDecl Declaration) {
uint16_t &EntityHash = PtrAuthDiscriminatorHashes[Declaration];
if (EntityHash == 0) {
StringRef Name = getMangledName(Declaration);
EntityHash = llvm::getPointerAuthStableSipHash(Name);
}
return EntityHash;
}
/// Return the abstract pointer authentication schema for a pointer to the given
/// function type.
CGPointerAuthInfo CodeGenModule::getFunctionPointerAuthInfo(QualType T) {
const auto &Schema = getCodeGenOpts().PointerAuth.FunctionPointers;
if (!Schema)
return CGPointerAuthInfo();
assert(!Schema.isAddressDiscriminated() &&
"function pointers cannot use address-specific discrimination");
llvm::Constant *Discriminator = nullptr;
if (T->isFunctionPointerType() || T->isFunctionReferenceType())
T = T->getPointeeType();
if (T->isFunctionType())
Discriminator = getPointerAuthOtherDiscriminator(Schema, GlobalDecl(), T);
return CGPointerAuthInfo(Schema.getKey(), Schema.getAuthenticationMode(),
/*IsaPointer=*/false, /*AuthenticatesNull=*/false,
Discriminator);
}
llvm::Value *
CodeGenFunction::EmitPointerAuthBlendDiscriminator(llvm::Value *StorageAddress,
llvm::Value *Discriminator) {
StorageAddress = Builder.CreatePtrToInt(StorageAddress, IntPtrTy);
auto Intrinsic = CGM.getIntrinsic(llvm::Intrinsic::ptrauth_blend);
return Builder.CreateCall(Intrinsic, {StorageAddress, Discriminator});
}
/// Emit the concrete pointer authentication informaton for the
/// given authentication schema.
CGPointerAuthInfo CodeGenFunction::EmitPointerAuthInfo(
const PointerAuthSchema &Schema, llvm::Value *StorageAddress,
GlobalDecl SchemaDecl, QualType SchemaType) {
if (!Schema)
return CGPointerAuthInfo();
llvm::Value *Discriminator =
CGM.getPointerAuthOtherDiscriminator(Schema, SchemaDecl, SchemaType);
if (Schema.isAddressDiscriminated()) {
assert(StorageAddress &&
"address not provided for address-discriminated schema");
if (Discriminator)
Discriminator =
EmitPointerAuthBlendDiscriminator(StorageAddress, Discriminator);
else
Discriminator = Builder.CreatePtrToInt(StorageAddress, IntPtrTy);
}
return CGPointerAuthInfo(Schema.getKey(), Schema.getAuthenticationMode(),
Schema.isIsaPointer(),
Schema.authenticatesNullValues(), Discriminator);
}
/// Return the natural pointer authentication for values of the given
/// pointee type.
static CGPointerAuthInfo
getPointerAuthInfoForPointeeType(CodeGenModule &CGM, QualType PointeeType) {
if (PointeeType.isNull())
return CGPointerAuthInfo();
// Function pointers use the function-pointer schema by default.
if (PointeeType->isFunctionType())
return CGM.getFunctionPointerAuthInfo(PointeeType);
// Normal data pointers never use direct pointer authentication by default.
return CGPointerAuthInfo();
}
CGPointerAuthInfo CodeGenModule::getPointerAuthInfoForPointeeType(QualType T) {
return ::getPointerAuthInfoForPointeeType(*this, T);
}
/// Return the natural pointer authentication for values of the given
/// pointer type.
static CGPointerAuthInfo getPointerAuthInfoForType(CodeGenModule &CGM,
QualType PointerType) {
assert(PointerType->isSignableType());
// Block pointers are currently not signed.
if (PointerType->isBlockPointerType())
return CGPointerAuthInfo();
auto PointeeType = PointerType->getPointeeType();
if (PointeeType.isNull())
return CGPointerAuthInfo();
return ::getPointerAuthInfoForPointeeType(CGM, PointeeType);
}
CGPointerAuthInfo CodeGenModule::getPointerAuthInfoForType(QualType T) {
return ::getPointerAuthInfoForType(*this, T);
}
llvm::Constant *
CodeGenModule::getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
llvm::Constant *StorageAddress,
llvm::ConstantInt *OtherDiscriminator) {
llvm::Constant *AddressDiscriminator;
if (StorageAddress) {
assert(StorageAddress->getType() == UnqualPtrTy);
AddressDiscriminator = StorageAddress;
} else {
AddressDiscriminator = llvm::Constant::getNullValue(UnqualPtrTy);
}
llvm::ConstantInt *IntegerDiscriminator;
if (OtherDiscriminator) {
assert(OtherDiscriminator->getType() == Int64Ty);
IntegerDiscriminator = OtherDiscriminator;
} else {
IntegerDiscriminator = llvm::ConstantInt::get(Int64Ty, 0);
}
return llvm::ConstantPtrAuth::get(Pointer,
llvm::ConstantInt::get(Int32Ty, Key),
IntegerDiscriminator, AddressDiscriminator);
}
/// Does a given PointerAuthScheme require us to sign a value
bool CodeGenModule::shouldSignPointer(const PointerAuthSchema &Schema) {
auto AuthenticationMode = Schema.getAuthenticationMode();
return AuthenticationMode == PointerAuthenticationMode::SignAndStrip ||
AuthenticationMode == PointerAuthenticationMode::SignAndAuth;
}
/// Sign a constant pointer using the given scheme, producing a constant
/// with the same IR type.
llvm::Constant *CodeGenModule::getConstantSignedPointer(
llvm::Constant *Pointer, const PointerAuthSchema &Schema,
llvm::Constant *StorageAddress, GlobalDecl SchemaDecl,
QualType SchemaType) {
assert(shouldSignPointer(Schema));
llvm::ConstantInt *OtherDiscriminator =
getPointerAuthOtherDiscriminator(Schema, SchemaDecl, SchemaType);
return getConstantSignedPointer(Pointer, Schema.getKey(), StorageAddress,
OtherDiscriminator);
}
/// If applicable, sign a given constant function pointer with the ABI rules for
/// functionType.
llvm::Constant *CodeGenModule::getFunctionPointer(llvm::Constant *Pointer,
QualType FunctionType) {
assert(FunctionType->isFunctionType() ||
FunctionType->isFunctionReferenceType() ||
FunctionType->isFunctionPointerType());
if (auto PointerAuth = getFunctionPointerAuthInfo(FunctionType))
return getConstantSignedPointer(
Pointer, PointerAuth.getKey(), /*StorageAddress=*/nullptr,
cast_or_null<llvm::ConstantInt>(PointerAuth.getDiscriminator()));
return Pointer;
}
llvm::Constant *CodeGenModule::getFunctionPointer(GlobalDecl GD,
llvm::Type *Ty) {
const auto *FD = cast<FunctionDecl>(GD.getDecl());
QualType FuncType = FD->getType();
// Annoyingly, K&R functions have prototypes in the clang AST, but
// expressions referring to them are unprototyped.
if (!FD->hasPrototype())
if (const auto *Proto = FuncType->getAs<FunctionProtoType>())
FuncType = Context.getFunctionNoProtoType(Proto->getReturnType(),
Proto->getExtInfo());
return getFunctionPointer(getRawFunctionPointer(GD, Ty), FuncType);
}
std::optional<PointerAuthQualifier>
CodeGenModule::computeVTPointerAuthentication(const CXXRecordDecl *ThisClass) {
auto DefaultAuthentication = getCodeGenOpts().PointerAuth.CXXVTablePointers;
if (!DefaultAuthentication)
return std::nullopt;
const CXXRecordDecl *PrimaryBase =
Context.baseForVTableAuthentication(ThisClass);
unsigned Key = DefaultAuthentication.getKey();
bool AddressDiscriminated = DefaultAuthentication.isAddressDiscriminated();
auto DefaultDiscrimination = DefaultAuthentication.getOtherDiscrimination();
unsigned TypeBasedDiscriminator =
Context.getPointerAuthVTablePointerDiscriminator(PrimaryBase);
unsigned Discriminator;
if (DefaultDiscrimination == PointerAuthSchema::Discrimination::Type) {
Discriminator = TypeBasedDiscriminator;
} else if (DefaultDiscrimination ==
PointerAuthSchema::Discrimination::Constant) {
Discriminator = DefaultAuthentication.getConstantDiscrimination();
} else {
assert(DefaultDiscrimination == PointerAuthSchema::Discrimination::None);
Discriminator = 0;
}
if (auto ExplicitAuthentication =
PrimaryBase->getAttr<VTablePointerAuthenticationAttr>()) {
auto ExplicitAddressDiscrimination =
ExplicitAuthentication->getAddressDiscrimination();
auto ExplicitDiscriminator =
ExplicitAuthentication->getExtraDiscrimination();
unsigned ExplicitKey = ExplicitAuthentication->getKey();
if (ExplicitKey == VTablePointerAuthenticationAttr::NoKey)
return std::nullopt;
if (ExplicitKey != VTablePointerAuthenticationAttr::DefaultKey) {
if (ExplicitKey == VTablePointerAuthenticationAttr::ProcessIndependent)
Key = (unsigned)PointerAuthSchema::ARM8_3Key::ASDA;
else {
assert(ExplicitKey ==
VTablePointerAuthenticationAttr::ProcessDependent);
Key = (unsigned)PointerAuthSchema::ARM8_3Key::ASDB;
}
}
if (ExplicitAddressDiscrimination !=
VTablePointerAuthenticationAttr::DefaultAddressDiscrimination)
AddressDiscriminated =
ExplicitAddressDiscrimination ==
VTablePointerAuthenticationAttr::AddressDiscrimination;
if (ExplicitDiscriminator ==
VTablePointerAuthenticationAttr::TypeDiscrimination)
Discriminator = TypeBasedDiscriminator;
else if (ExplicitDiscriminator ==
VTablePointerAuthenticationAttr::CustomDiscrimination)
Discriminator = ExplicitAuthentication->getCustomDiscriminationValue();
else if (ExplicitDiscriminator ==
VTablePointerAuthenticationAttr::NoExtraDiscrimination)
Discriminator = 0;
}
return PointerAuthQualifier::Create(Key, AddressDiscriminated, Discriminator,
PointerAuthenticationMode::SignAndAuth,
/* IsIsaPointer */ false,
/* AuthenticatesNullValues */ false);
}
std::optional<PointerAuthQualifier>
CodeGenModule::getVTablePointerAuthentication(const CXXRecordDecl *Record) {
if (!Record->getDefinition() || !Record->isPolymorphic())
return std::nullopt;
auto Existing = VTablePtrAuthInfos.find(Record);
std::optional<PointerAuthQualifier> Authentication;
if (Existing != VTablePtrAuthInfos.end()) {
Authentication = Existing->getSecond();
} else {
Authentication = computeVTPointerAuthentication(Record);
VTablePtrAuthInfos.insert(std::make_pair(Record, Authentication));
}
return Authentication;
}
std::optional<CGPointerAuthInfo>
CodeGenModule::getVTablePointerAuthInfo(CodeGenFunction *CGF,
const CXXRecordDecl *Record,
llvm::Value *StorageAddress) {
auto Authentication = getVTablePointerAuthentication(Record);
if (!Authentication)
return std::nullopt;
llvm::Value *Discriminator = nullptr;
if (auto ExtraDiscriminator = Authentication->getExtraDiscriminator())
Discriminator = llvm::ConstantInt::get(IntPtrTy, ExtraDiscriminator);
if (Authentication->isAddressDiscriminated()) {
assert(StorageAddress &&
"address not provided for address-discriminated schema");
if (Discriminator)
Discriminator =
CGF->EmitPointerAuthBlendDiscriminator(StorageAddress, Discriminator);
else
Discriminator = CGF->Builder.CreatePtrToInt(StorageAddress, IntPtrTy);
}
return CGPointerAuthInfo(Authentication->getKey(),
PointerAuthenticationMode::SignAndAuth,
/* IsIsaPointer */ false,
/* AuthenticatesNullValues */ false, Discriminator);
}
|