Skip to content

Commit 042579b

Browse files
committed
[IRGen] Address-discriminate these ptrauth ents.
They are stored at an address so that address should be used to strengthen auth.
1 parent 5ca6910 commit 042579b

File tree

3 files changed

+82
-22
lines changed

3 files changed

+82
-22
lines changed

lib/IRGen/GenCoro.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -801,17 +801,24 @@ struct Allocator {
801801
Allocator(llvm::Value *address, IRGenFunction &IGF)
802802
: address(address), IGF(IGF) {}
803803

804-
llvm::Value *getField(Field field) {
804+
struct FieldLoad {
805+
llvm::Value *address;
806+
llvm::Value *value;
807+
};
808+
809+
FieldLoad loadField(Field field) {
805810
auto *fieldAddress = IGF.Builder.CreateInBoundsGEP(
806811
IGF.IGM.CoroAllocatorTy, address,
807812
{llvm::ConstantInt::get(IGF.IGM.Int32Ty, 0),
808813
llvm::ConstantInt::get(IGF.IGM.Int32Ty, field.kind)});
809-
return IGF.Builder.CreateLoad(Address(fieldAddress, field.getType(IGF.IGM),
810-
field.getAlignment(IGF.IGM)),
811-
field.getName());
814+
auto *value =
815+
IGF.Builder.CreateLoad(Address(fieldAddress, field.getType(IGF.IGM),
816+
field.getAlignment(IGF.IGM)),
817+
field.getName());
818+
return {fieldAddress, value};
812819
}
813820

814-
llvm::Value *getFlags() { return getField(Field::Flags); }
821+
llvm::Value *getFlags() { return loadField(Field::Flags).value; }
815822

816823
FunctionPointer getAllocate(AllocationKind kind) {
817824
switch (kind) {
@@ -862,10 +869,11 @@ struct Allocator {
862869
}
863870

864871
FunctionPointer getFunctionPointer(Field field) {
865-
llvm::Value *callee = getField(field);
872+
auto fieldValues = loadField(field);
873+
auto *callee = fieldValues.value;
866874
if (auto &schema = field.getSchema(IGF.IGM)) {
867-
auto info =
868-
PointerAuthInfo::emit(IGF, schema, nullptr, PointerAuthEntity());
875+
auto info = PointerAuthInfo::emit(IGF, schema, fieldValues.address,
876+
field.getEntity(IGF.IGM));
869877
callee = emitPointerAuthAuth(IGF, callee, info);
870878
}
871879
return FunctionPointer::createUnsigned(

lib/IRGen/IRGen.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,19 +1087,19 @@ static void setPointerAuthOptions(PointerAuthOptions &opts,
10871087
PointerAuthSchema(nonABIDataKey, /*address*/ true, Discrimination::Decl);
10881088

10891089
opts.CoroAllocationFunction = PointerAuthSchema(
1090-
codeKey, /*address*/ false, Discrimination::Constant,
1090+
codeKey, /*address*/ true, Discrimination::Constant,
10911091
SpecialPointerAuthDiscriminators::CoroAllocationFunction);
10921092

10931093
opts.CoroDeallocationFunction = PointerAuthSchema(
1094-
codeKey, /*address*/ false, Discrimination::Constant,
1094+
codeKey, /*address*/ true, Discrimination::Constant,
10951095
SpecialPointerAuthDiscriminators::CoroDeallocationFunction);
10961096

10971097
opts.CoroFrameAllocationFunction = PointerAuthSchema(
1098-
codeKey, /*address*/ false, Discrimination::Constant,
1098+
codeKey, /*address*/ true, Discrimination::Constant,
10991099
SpecialPointerAuthDiscriminators::CoroFrameAllocationFunction);
11001100

11011101
opts.CoroFrameDeallocationFunction = PointerAuthSchema(
1102-
codeKey, /*address*/ false, Discrimination::Constant,
1102+
codeKey, /*address*/ true, Discrimination::Constant,
11031103
SpecialPointerAuthDiscriminators::CoroFrameDeallocationFunction);
11041104
}
11051105

test/IRGen/coroutine_accessors.swift

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,52 @@
2626
// CHECK-arm64e-LABEL: _swift_coro_malloc.ptrauth = private constant {
2727
// CHECK-arm64e-SAME: ptr @_swift_coro_malloc,
2828
// CHECK-arm64e-SAME: i32 0,
29-
// CHECK-arm64e-SAME: i64 0,
29+
// CHECK-arm64e-SAME: i64 ptrtoint (
30+
// CHECK-arm64e-SAME: ptr getelementptr inbounds (
31+
// CHECK-arm64e-SAME: ptr @_swift_coro_malloc_allocator,
32+
// CHECK-arm64e-SAME: i32 0,
33+
// CHECK-arm64e-SAME: i32 1
34+
// CHECK-arm64e-SAME: )
35+
// CHECK-arm64e-SAME: )
3036
// CHECK-arm64e-SAME: i64 24469 }
3137
// CHECK-arm64e-SAME: section "llvm.ptrauth"
3238
// CHECK-arm64e-SAME: align 8
3339
// CHECK-arm64e-LABEL: _swift_coro_free.ptrauth = private constant {
3440
// CHECK-arm64e-SAME: ptr @_swift_coro_free,
3541
// CHECK-arm64e-SAME: i32 0,
36-
// CHECK-arm64e-SAME: i64 0,
42+
// CHECK-arm64e-SAME: i64 ptrtoint (
43+
// CHECK-arm64e-SAME: ptr getelementptr inbounds (
44+
// CHECK-arm64e-SAME: ptr @_swift_coro_malloc_allocator,
45+
// CHECK-arm64e-SAME: i32 0,
46+
// CHECK-arm64e-SAME: i32 2
47+
// CHECK-arm64e-SAME: )
48+
// CHECK-arm64e-SAME: )
3749
// CHECK-arm64e-SAME: i64 40879 },
3850
// CHECK-arm64e-SAME: section "llvm.ptrauth",
3951
// CHECK-arm64e-SAME: align 8
4052
// CHECK-arm64e-LABEL: _swift_coro_malloc.ptrauth.1 = private constant {
4153
// CHECK-arm64e-SAME: ptr @_swift_coro_malloc,
4254
// CHECK-arm64e-SAME: i32 0,
43-
// CHECK-arm64e-SAME: i64 0,
55+
// CHECK-arm64e-SAME: i64 ptrtoint (
56+
// CHECK-arm64e-SAME: ptr getelementptr inbounds (
57+
// CHECK-arm64e-SAME: ptr @_swift_coro_malloc_allocator,
58+
// CHECK-arm64e-SAME: i32 0,
59+
// CHECK-arm64e-SAME: i32 3
60+
// CHECK-arm64e-SAME: )
61+
// CHECK-arm64e-SAME: )
4462
// CHECK-arm64e-SAME: i64 53841 }
4563
// CHECK-arm64e-SAME: section "llvm.ptrauth"
4664
// CHECK-arm64e-SAME: align 8
4765
// CHECK-arm64e-LABEL: _swift_coro_free.ptrauth.2 = private constant {
4866
// CHECK-arm64e-SAME: ptr @_swift_coro_free,
4967
// CHECK-arm64e-SAME: i32 0,
50-
// CHECK-arm64e-SAME: i64 0,
68+
// CHECK-arm64e-SAME: i64 ptrtoint (
69+
// CHECK-arm64e-SAME: ptr getelementptr inbounds (
70+
// CHECK-arm64e-SAME: ptr @_swift_coro_malloc_allocator,
71+
// CHECK-arm64e-SAME: i32 0,
72+
// CHECK-arm64e-SAME: i32 4
73+
// CHECK-arm64e-SAME: )
74+
// CHECK-arm64e-SAME: )
5175
// CHECK-arm64e-SAME: i64 23464 },
5276
// CHECK-arm64e-SAME: section "llvm.ptrauth",
5377
// CHECK-arm64e-SAME: align 8
@@ -65,28 +89,52 @@
6589
// CHECK-arm64e-LABEL: _swift_coro_task_alloc.ptrauth = private constant {
6690
// CHECK-arm64e-SAME: ptr @_swift_coro_task_alloc,
6791
// CHECK-arm64e-SAME: i32 0,
68-
// CHECK-arm64e-SAME: i64 0,
92+
// CHECK-arm64e-SAME: i64 ptrtoint (
93+
// CHECK-arm64e-SAME: ptr getelementptr inbounds (
94+
// CHECK-arm64e-SAME: ptr @_swift_coro_async_allocator,
95+
// CHECK-arm64e-SAME: i32 0,
96+
// CHECK-arm64e-SAME: i32 1
97+
// CHECK-arm64e-SAME: )
98+
// CHECK-arm64e-SAME: )
6999
// CHECK-arm64e-SAME: i64 24469 }
70100
// CHECK-arm64e-SAME: section "llvm.ptrauth"
71101
// CHECK-arm64e-SAME: align 8
72102
// CHECK-arm64e-LABEL: @_swift_coro_task_dealloc.ptrauth = private constant {
73103
// CHECK-arm64e-SAME: ptr @_swift_coro_task_dealloc,
74104
// CHECK-arm64e-SAME: i32 0,
75-
// CHECK-arm64e-SAME: i64 0,
105+
// CHECK-arm64e-SAME: i64 ptrtoint (
106+
// CHECK-arm64e-SAME: ptr getelementptr inbounds (
107+
// CHECK-arm64e-SAME: ptr @_swift_coro_async_allocator,
108+
// CHECK-arm64e-SAME: i32 0,
109+
// CHECK-arm64e-SAME: i32 2
110+
// CHECK-arm64e-SAME: )
111+
// CHECK-arm64e-SAME: )
76112
// CHECK-arm64e-SAME: i64 40879 },
77113
// CHECK-arm64e-SAME: section "llvm.ptrauth",
78114
// CHECK-arm64e-SAME: align 8
79115
// CHECK-arm64e-LABEL: _swift_coro_task_alloc.ptrauth.3 = private constant {
80116
// CHECK-arm64e-SAME: ptr @_swift_coro_task_alloc,
81117
// CHECK-arm64e-SAME: i32 0,
82-
// CHECK-arm64e-SAME: i64 0,
118+
// CHECK-arm64e-SAME: i64 ptrtoint (
119+
// CHECK-arm64e-SAME: ptr getelementptr inbounds (
120+
// CHECK-arm64e-SAME: ptr @_swift_coro_async_allocator,
121+
// CHECK-arm64e-SAME: i32 0,
122+
// CHECK-arm64e-SAME: i32 3
123+
// CHECK-arm64e-SAME: )
124+
// CHECK-arm64e-SAME: )
83125
// CHECK-arm64e-SAME: i64 53841 }
84126
// CHECK-arm64e-SAME: section "llvm.ptrauth"
85127
// CHECK-arm64e-SAME: align 8
86128
// CHECK-arm64e-LABEL: @_swift_coro_task_dealloc.ptrauth.4 = private constant {
87129
// CHECK-arm64e-SAME: ptr @_swift_coro_task_dealloc,
88130
// CHECK-arm64e-SAME: i32 0,
89-
// CHECK-arm64e-SAME: i64 0,
131+
// CHECK-arm64e-SAME: i64 ptrtoint (
132+
// CHECK-arm64e-SAME: ptr getelementptr inbounds (
133+
// CHECK-arm64e-SAME: ptr @_swift_coro_async_allocator,
134+
// CHECK-arm64e-SAME: i32 0,
135+
// CHECK-arm64e-SAME: i32 4
136+
// CHECK-arm64e-SAME: )
137+
// CHECK-arm64e-SAME: )
90138
// CHECK-arm64e-SAME: i64 23464 },
91139
// CHECK-arm64e-SAME: section "llvm.ptrauth",
92140
// CHECK-arm64e-SAME: align 8
@@ -114,8 +162,10 @@
114162
// CHECK-SAME: i32 0
115163
// CHECK-SAME: i32 1
116164
// CHECK: [[ALLOCATE_FN:%[^,]+]] = load ptr, ptr [[ALLOCATE_FN_PTR]]
165+
// CHECK-arm64e: [[ALLOCATE_FN_PTR_BITS:%[^,]+]] = ptrtoint ptr [[ALLOCATE_FN_PTR]] to i64
166+
// CHECK-arm64e: [[ALLOCATE_FN_DISCRIMINATOR:%[^,]+]] = call i64 @llvm.ptrauth.blend(i64 [[ALLOCATE_FN_PTR_BITS]], i64 24469)
117167
// CHECK-arm64e: [[ALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[ALLOCATE_FN]] to i64
118-
// CHECK-arm64e: [[ALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[ALLOCATE_FN_BITS]], i32 0, i64 24469)
168+
// CHECK-arm64e: [[ALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[ALLOCATE_FN_BITS]], i32 0, i64 [[ALLOCATE_FN_DISCRIMINATOR]])
119169
// CHECK-arm64e: [[ALLOCATE_FN:%[^,]+]] = inttoptr i64 [[ALLOCATE_FN_BITS_AUTHED]]
120170
// CHECK: [[ALLOCATION:%[^,]+]] = call swiftcc ptr [[ALLOCATE_FN]](ptr [[FRAME]], ptr swiftcoro [[ALLOCATOR]], [[INT]] [[SIZE]])
121171
// CHECK: ret ptr [[ALLOCATION]]
@@ -146,8 +196,10 @@
146196
// CHECK-SAME: i32 0
147197
// CHECK-SAME: i32 2
148198
// CHECK: [[DEALLOCATE_FN:%[^,]+]] = load ptr, ptr [[DEALLOCATE_FN_PTR]]
199+
// CHECK-arm64e: [[DEALLOCATE_FN_PTR_BITS:%[^,]+]] = ptrtoint ptr [[DEALLOCATE_FN_PTR]] to i64
200+
// CHECK-arm64e: [[DEALLOCATE_FN_DISCRIMINATOR:%[^,]+]] = call i64 @llvm.ptrauth.blend(i64 [[DEALLOCATE_FN_PTR_BITS]], i64 40879)
149201
// CHECK-arm64e: [[DEALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[DEALLOCATE_FN]] to i64
150-
// CHECK-arm64e: [[DEALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[DEALLOCATE_FN_BITS]], i32 0, i64 40879)
202+
// CHECK-arm64e: [[DEALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[DEALLOCATE_FN_BITS]], i32 0, i64 [[DEALLOCATE_FN_DISCRIMINATOR]])
151203
// CHECK-arm64e: [[DEALLOCATE_FN:%[^,]+]] = inttoptr i64 [[DEALLOCATE_FN_BITS_AUTHED]]
152204
// CHECK: call swiftcc void [[DEALLOCATE_FN]](ptr [[FRAME]], ptr swiftcoro [[ALLOCATOR]], ptr [[ADDRESS]])
153205
// CHECK: ret void

0 commit comments

Comments
 (0)