-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathGenPointerAuth.h
212 lines (190 loc) · 7.13 KB
/
GenPointerAuth.h
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
//===--- GenPointerAuth.h - IRGen for pointer authentication ----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the basic interface for generating LLVM IR for pointer
// authentication.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_GENPOINTERAUTH_H
#define SWIFT_IRGEN_GENPOINTERAUTH_H
#include "swift/IRGen/ValueWitness.h"
#include "swift/Basic/ExternalUnion.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/ProtocolAssociations.h"
#include "swift/AST/Types.h"
#include "swift/SIL/SILDeclRef.h"
namespace llvm {
class ConstantInt;
class Value;
}
namespace clang {
class PointerAuthSchema;
}
namespace swift {
namespace irgen {
class FunctionPointer;
class IRGenFunction;
class IRGenModule;
class PointerAuthInfo;
/// Additional information about the source of a function pointer.
class PointerAuthEntity {
public:
enum class Special {
BlockCopyHelper,
BlockDisposeHelper,
HeapDestructor,
PartialApplyCapture,
TypeDescriptor,
TypeDescriptorAsArgument,
KeyPathDestroy,
KeyPathCopy,
KeyPathEquals,
KeyPathHash,
KeyPathGetter,
KeyPathNonmutatingSetter,
KeyPathMutatingSetter,
KeyPathGetLayout,
KeyPathInitializer,
KeyPathMetadataAccessor,
DynamicReplacementKey,
ProtocolConformanceDescriptor,
ProtocolConformanceDescriptorAsArgument,
ProtocolDescriptorAsArgument,
OpaqueTypeDescriptorAsArgument,
ContextDescriptorAsArgument,
TypeLayoutString,
};
private:
enum class Kind {
None,
Special,
ValueWitness,
AssociatedType,
AssociatedConformance,
CanSILFunctionType,
CoroutineYieldTypes,
SILDeclRef,
SILFunction,
} StoredKind;
using Members = ExternalUnionMembers<void,
Special,
ValueWitness,
AssociatedType,
AssociatedConformance,
CanSILFunctionType,
SILDeclRef,
SILFunction *>;
static Members::Index getStorageIndexForKind(Kind kind) {
switch (kind) {
case Kind::None:
return Members::indexOf<void>();
case Kind::Special:
return Members::indexOf<Special>();
case Kind::ValueWitness:
return Members::indexOf<ValueWitness>();
case Kind::CoroutineYieldTypes:
case Kind::CanSILFunctionType:
return Members::indexOf<CanSILFunctionType>();
case Kind::SILDeclRef:
return Members::indexOf<SILDeclRef>();
case Kind::AssociatedType:
return Members::indexOf<AssociatedType>();
case Kind::AssociatedConformance:
return Members::indexOf<AssociatedConformance>();
case Kind::SILFunction:
return Members::indexOf<SILFunction *>();
}
llvm_unreachable("bad kind");
}
ExternalUnion<Kind, Members, getStorageIndexForKind> Storage;
static_assert(decltype(Storage)::union_is_trivially_copyable,
"please add copy/move/dtor if you need a non-trivial type");
public:
PointerAuthEntity()
: StoredKind(Kind::None) {
}
PointerAuthEntity(Special specialKind)
: StoredKind(Kind::Special) {
Storage.emplace<Special>(StoredKind, specialKind);
}
PointerAuthEntity(CanSILFunctionType type)
: StoredKind(Kind::CanSILFunctionType) {
Storage.emplace<CanSILFunctionType>(StoredKind, type);
}
PointerAuthEntity(SILDeclRef decl)
: StoredKind(Kind::SILDeclRef) {
Storage.emplace<SILDeclRef>(StoredKind, decl);
}
PointerAuthEntity(ValueWitness witness)
: StoredKind(Kind::ValueWitness) {
assert(isValueWitnessFunction(witness));
Storage.emplace<ValueWitness>(StoredKind, witness);
}
PointerAuthEntity(AssociatedType association)
: StoredKind(Kind::AssociatedType) {
Storage.emplaceAggregate<AssociatedType>(StoredKind, association);
}
PointerAuthEntity(const AssociatedConformance &association)
: StoredKind(Kind::AssociatedConformance) {
Storage.emplaceAggregate<AssociatedConformance>(StoredKind, association);
}
PointerAuthEntity(SILFunction *f)
: StoredKind(Kind::SILFunction) {
Storage.emplace<SILFunction *>(StoredKind, f);
}
static PointerAuthEntity forYieldTypes(CanSILFunctionType fnType) {
assert(fnType->isCoroutine());
PointerAuthEntity result;
result.StoredKind = Kind::CoroutineYieldTypes;
result.Storage.emplace<CanSILFunctionType>(result.StoredKind, fnType);
return result;
}
llvm::ConstantInt *getDeclDiscriminator(IRGenModule &IGM) const;
llvm::ConstantInt *getTypeDiscriminator(IRGenModule &IGM) const;
};
std::pair<clang::PointerAuthSchema, PointerAuthEntity>
getCoroutineResumeFunctionPointerAuth(IRGenModule &IGM,
CanSILFunctionType coroutineFnType);
/// Blend a small integer discriminator with the given address value
/// in a way that is assumed to maximally preserve entropy from both.
llvm::Value *emitPointerAuthBlend(IRGenFunction &IGF,
llvm::Value *address,
llvm::Value *discriminator);
/// Strip the signature of a signed pointer value.
/// The return value has the same type as the input.
llvm::Value *emitPointerAuthStrip(IRGenFunction &IGF, llvm::Value *fnPtr,
unsigned Key);
/// Sign the given pointer value, which is assumed to be unsigned.
/// The return value has the same type as the input. This is a no-op if
/// the target auth info has disabled signing.
llvm::Value *emitPointerAuthSign(IRGenFunction &IGF, llvm::Value *fnPtr,
const PointerAuthInfo &newAuth);
/// Authenticate the given pointer value and return an unsigned value.
llvm::Value *emitPointerAuthAuth(IRGenFunction &IGF, llvm::Value *fnPtr,
const PointerAuthInfo &oldAuth);
/// Resign the given function pointer.
FunctionPointer emitPointerAuthResign(IRGenFunction &IGF,
const FunctionPointer &fn,
const PointerAuthInfo &newAuth);
/// Resign the given pointer value. This function does the right thing
/// for unsigned input and result schemas. The result will have the same
/// type as the input.
llvm::Value *emitPointerAuthResign(IRGenFunction &IGF,
llvm::Value *fnPtr,
const PointerAuthInfo &oldAuth,
const PointerAuthInfo &newAuth);
/// The (non-ABI) discriminator used for non-constant captures of
/// partial apply functions.
const uint16_t PointerAuthDiscriminator_PartialApplyCapture = 7185;
} // end namespace irgen
} // end namespace swift
#endif