Skip to content

Commit 84c518e

Browse files
Merge pull request #85166 from nate-chandler/rdar163330882
[CoroutineAccessors] Ptrauth improvements.
2 parents 4eb0e95 + 042579b commit 84c518e

File tree

3 files changed

+139
-40
lines changed

3 files changed

+139
-40
lines changed

lib/IRGen/GenCoro.cpp

Lines changed: 33 additions & 26 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(
@@ -1083,28 +1091,30 @@ static llvm::Constant *getAddrOfGlobalCoroAllocator(
10831091
return taskAllocator;
10841092
}
10851093

1086-
static llvm::Constant *getAddrOfSwiftCoroMalloc(
1087-
IRGenModule &IGM) {
1094+
static llvm::Constant *getAddrOfGlobalCoroAllocator(
1095+
IRGenModule &IGM, CoroAllocatorKind kind, bool shouldDeallocateImmediately,
1096+
llvm::Constant *allocFn, llvm::Constant *deallocFn) {
1097+
return getAddrOfGlobalCoroAllocator(IGM, kind, shouldDeallocateImmediately,
1098+
allocFn, deallocFn, allocFn, deallocFn);
1099+
}
1100+
1101+
static llvm::Constant *getAddrOfSwiftCoroMalloc(IRGenModule &IGM) {
10881102
return getAddrOfSwiftCoroAllocThunk("_swift_coro_malloc",
10891103
&IRGenModule::getMallocFunctionPointer,
10901104
IGM);
10911105
}
10921106

1093-
static llvm::Constant *getAddrOfSwiftCoroFree(
1094-
IRGenModule &IGM) {
1107+
static llvm::Constant *getAddrOfSwiftCoroFree(IRGenModule &IGM) {
10951108
return getAddrOfSwiftCoroDeallocThunk("_swift_coro_free",
10961109
&IRGenModule::getFreeFunctionPointer,
10971110
IGM);
10981111
}
10991112

11001113
llvm::Constant *IRGenModule::getAddrOfGlobalCoroMallocAllocator() {
1101-
return getAddrOfGlobalCoroAllocator(
1102-
*this, CoroAllocatorKind::Malloc,
1103-
/*shouldDeallocateImmediately=*/true,
1104-
getAddrOfSwiftCoroMalloc(*this),
1105-
getAddrOfSwiftCoroFree(*this),
1106-
getAddrOfSwiftCoroMalloc(*this),
1107-
getAddrOfSwiftCoroFree(*this));
1114+
return getAddrOfGlobalCoroAllocator(*this, CoroAllocatorKind::Malloc,
1115+
/*shouldDeallocateImmediately=*/true,
1116+
getAddrOfSwiftCoroMalloc(*this),
1117+
getAddrOfSwiftCoroFree(*this));
11081118
}
11091119

11101120
static llvm::Constant *
@@ -1122,13 +1132,10 @@ getAddrOfSwiftCoroTaskDealloc(IRGenModule &IGM) {
11221132
}
11231133

11241134
llvm::Constant *IRGenModule::getAddrOfGlobalCoroAsyncTaskAllocator() {
1125-
return getAddrOfGlobalCoroAllocator(
1126-
*this, CoroAllocatorKind::Async,
1127-
/*shouldDeallocateImmediately=*/false,
1128-
getAddrOfSwiftCoroTaskAlloc(*this),
1129-
getAddrOfSwiftCoroTaskDealloc(*this),
1130-
getAddrOfSwiftCoroTaskAlloc(*this),
1131-
getAddrOfSwiftCoroTaskDealloc(*this));
1135+
return getAddrOfGlobalCoroAllocator(*this, CoroAllocatorKind::Async,
1136+
/*shouldDeallocateImmediately=*/false,
1137+
getAddrOfSwiftCoroTaskAlloc(*this),
1138+
getAddrOfSwiftCoroTaskDealloc(*this));
11321139
}
11331140

11341141
llvm::Value *

lib/IRGen/IRGen.cpp

Lines changed: 6 additions & 6 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

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

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

test/IRGen/coroutine_accessors.swift

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,128 @@
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
52+
// CHECK-arm64e-LABEL: _swift_coro_malloc.ptrauth.1 = private constant {
53+
// CHECK-arm64e-SAME: ptr @_swift_coro_malloc,
54+
// CHECK-arm64e-SAME: i32 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: )
62+
// CHECK-arm64e-SAME: i64 53841 }
63+
// CHECK-arm64e-SAME: section "llvm.ptrauth"
64+
// CHECK-arm64e-SAME: align 8
65+
// CHECK-arm64e-LABEL: _swift_coro_free.ptrauth.2 = private constant {
66+
// CHECK-arm64e-SAME: ptr @_swift_coro_free,
67+
// CHECK-arm64e-SAME: i32 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: )
75+
// CHECK-arm64e-SAME: i64 23464 },
76+
// CHECK-arm64e-SAME: section "llvm.ptrauth",
77+
// CHECK-arm64e-SAME: align 8
4078
// CHECK-LABEL: _swift_coro_malloc_allocator = linkonce_odr hidden constant %swift.coro_allocator {
4179
// CHECK-SAME: i32 258,
42-
// CHECK-SAME: malloc
43-
// CHECK-SAME: free
80+
// CHECK-SAME: _swift_coro_malloc
81+
// CHECK-ar64e-SAME: .ptrauth
82+
// CHECK-SAME: _swift_coro_free
83+
// CHECK-ar64e-SAME: .ptrauth
84+
// CHECK-SAME: _swift_coro_malloc
85+
// CHECK-ar64e-SAME: .ptrauth.1
86+
// CHECK-SAME: _swift_coro_free
87+
// CHECK-ar64e-SAME: .ptrauth.2
4488
// CHECK-SAME: }
4589
// CHECK-arm64e-LABEL: _swift_coro_task_alloc.ptrauth = private constant {
4690
// CHECK-arm64e-SAME: ptr @_swift_coro_task_alloc,
4791
// CHECK-arm64e-SAME: i32 0,
48-
// 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: )
4999
// CHECK-arm64e-SAME: i64 24469 }
50100
// CHECK-arm64e-SAME: section "llvm.ptrauth"
51101
// CHECK-arm64e-SAME: align 8
52102
// CHECK-arm64e-LABEL: @_swift_coro_task_dealloc.ptrauth = private constant {
53103
// CHECK-arm64e-SAME: ptr @_swift_coro_task_dealloc,
54104
// CHECK-arm64e-SAME: i32 0,
55-
// 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: )
56112
// CHECK-arm64e-SAME: i64 40879 },
57113
// CHECK-arm64e-SAME: section "llvm.ptrauth",
58114
// CHECK-arm64e-SAME: align 8
115+
// CHECK-arm64e-LABEL: _swift_coro_task_alloc.ptrauth.3 = private constant {
116+
// CHECK-arm64e-SAME: ptr @_swift_coro_task_alloc,
117+
// CHECK-arm64e-SAME: i32 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: )
125+
// CHECK-arm64e-SAME: i64 53841 }
126+
// CHECK-arm64e-SAME: section "llvm.ptrauth"
127+
// CHECK-arm64e-SAME: align 8
128+
// CHECK-arm64e-LABEL: @_swift_coro_task_dealloc.ptrauth.4 = private constant {
129+
// CHECK-arm64e-SAME: ptr @_swift_coro_task_dealloc,
130+
// CHECK-arm64e-SAME: i32 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: )
138+
// CHECK-arm64e-SAME: i64 23464 },
139+
// CHECK-arm64e-SAME: section "llvm.ptrauth",
140+
// CHECK-arm64e-SAME: align 8
59141
// CHECK-LABEL: _swift_coro_async_allocator = linkonce_odr hidden constant %swift.coro_allocator {
60142
// CHECK-SAME: i32 1,
61143
// CHECK-SAME: _swift_coro_task_alloc
144+
// CHECK-ar64e-SAME: .ptrauth
145+
// CHECK-SAME: _swift_coro_task_dealloc
146+
// CHECK-ar64e-SAME: .ptrauth
147+
// CHECK-SAME: _swift_coro_task_alloc
148+
// CHECK-ar64e-SAME: .ptrauth.3
62149
// CHECK-SAME: _swift_coro_task_dealloc
150+
// CHECK-ar64e-SAME: .ptrauth.4
63151
// CHECK-SAME: }
64152

65153
// CHECK-LABEL: @_swift_coro_alloc(
@@ -74,8 +162,10 @@
74162
// CHECK-SAME: i32 0
75163
// CHECK-SAME: i32 1
76164
// 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)
77167
// CHECK-arm64e: [[ALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[ALLOCATE_FN]] to i64
78-
// 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]])
79169
// CHECK-arm64e: [[ALLOCATE_FN:%[^,]+]] = inttoptr i64 [[ALLOCATE_FN_BITS_AUTHED]]
80170
// CHECK: [[ALLOCATION:%[^,]+]] = call swiftcc ptr [[ALLOCATE_FN]](ptr [[FRAME]], ptr swiftcoro [[ALLOCATOR]], [[INT]] [[SIZE]])
81171
// CHECK: ret ptr [[ALLOCATION]]
@@ -106,8 +196,10 @@
106196
// CHECK-SAME: i32 0
107197
// CHECK-SAME: i32 2
108198
// 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)
109201
// CHECK-arm64e: [[DEALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[DEALLOCATE_FN]] to i64
110-
// 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]])
111203
// CHECK-arm64e: [[DEALLOCATE_FN:%[^,]+]] = inttoptr i64 [[DEALLOCATE_FN_BITS_AUTHED]]
112204
// CHECK: call swiftcc void [[DEALLOCATE_FN]](ptr [[FRAME]], ptr swiftcoro [[ALLOCATOR]], ptr [[ADDRESS]])
113205
// CHECK: ret void

0 commit comments

Comments
 (0)