Skip to content

Commit 603dc2e

Browse files
committed
[CoroutineAccessors] Sign de/allocation functions.
Allocator structs are passed in to new ABI yield-once coroutines and contain pointers to functions to de/allocate memory. Here, those pointers are signed.
1 parent 5a46d84 commit 603dc2e

File tree

8 files changed

+121
-8
lines changed

8 files changed

+121
-8
lines changed

include/swift/ABI/MetadataValues.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,10 @@ namespace SpecialPointerAuthDiscriminators {
17931793
/// IsCurrentGlobalActor function used between the Swift runtime and
17941794
/// concurrency runtime.
17951795
const uint16_t IsCurrentGlobalActorFunction = 0xd1b8; // = 53688
1796+
1797+
/// Function pointers stored in the coro allocator struct.
1798+
const uint16_t CoroAllocationFunction = 0x5f95; // = 24469
1799+
const uint16_t CoroDeallocationFunction = 0x9faf; // = 40879
17961800
}
17971801

17981802
/// The number of arguments that will be passed directly to a generic

include/swift/AST/IRGenOptions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ struct PointerAuthOptions : clang::PointerAuthOptions {
251251

252252
/// Like PartialApplyCapture but for use with CoroFunctionPointer values.
253253
PointerAuthSchema CoroPartialApplyCapture;
254+
255+
/// Stored in a coro allocator struct, the function used to allocate memory.
256+
PointerAuthSchema CoroAllocationFunction;
257+
258+
/// Stored in a coro allocator struct, the function used to deallocate memory.
259+
PointerAuthSchema CoroDeallocationFunction;
254260
};
255261

256262
enum class JITDebugArtifact : unsigned {

lib/IRGen/GenCoro.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "ConstantBuilder.h"
2222
#include "Explosion.h"
2323
#include "GenCoro.h"
24+
#include "GenPointerAuth.h"
2425
#include "IRGenFunction.h"
2526
#include "IRGenModule.h"
2627

@@ -669,6 +670,17 @@ struct Allocator {
669670
return IGM.CoroDeallocateFnTy;
670671
}
671672
}
673+
674+
const PointerAuthSchema &getSchema(IRGenModule &IGM) {
675+
switch (kind) {
676+
case Flags:
677+
llvm_unreachable("no schema");
678+
case Field::Allocate:
679+
return IGM.getOptions().PointerAuth.CoroAllocationFunction;
680+
case Field::Deallocate:
681+
return IGM.getOptions().PointerAuth.CoroDeallocationFunction;
682+
}
683+
}
672684
};
673685

674686
llvm::Value *address;
@@ -713,6 +725,11 @@ struct Allocator {
713725
private:
714726
FunctionPointer getFunctionPointer(Field field) {
715727
llvm::Value *callee = getField(field);
728+
if (auto &schema = field.getSchema(IGF.IGM)) {
729+
auto info =
730+
PointerAuthInfo::emit(IGF, schema, nullptr, PointerAuthEntity());
731+
callee = emitPointerAuthAuth(IGF, callee, info);
732+
}
716733
return FunctionPointer::createUnsigned(
717734
FunctionPointer::Kind::Function, callee,
718735
Signature(field.getFunctionType(IGF.IGM), {}, IGF.IGM.SwiftCC));
@@ -767,7 +784,7 @@ llvm::Constant *swift::irgen::getCoroDeallocFn(IRGenModule &IGM) {
767784
{IGM.CoroAllocatorPtrTy, IGM.Int8PtrTy},
768785
[isSwiftCoroCCAvailable](IRGenFunction &IGF) {
769786
auto parameters = IGF.collectParameters();
770-
auto allocator = ::Allocator(parameters.claimNext(), IGF);
787+
auto allocator = Allocator(parameters.claimNext(), IGF);
771788
auto *ptr = parameters.claimNext();
772789
if (isSwiftCoroCCAvailable) {
773790
// swiftcorocc is available, so if there's no allocator pointer,
@@ -825,8 +842,12 @@ static llvm::Constant *getAddrOfGlobalCoroAllocator(
825842
auto flags = CoroAllocatorFlags(kind);
826843
flags.setShouldDeallocateImmediately(shouldDeallocateImmediately);
827844
allocator.addInt32(flags.getOpaqueValue());
828-
allocator.add(allocFn);
829-
allocator.add(deallocFn);
845+
allocator.addSignedPointer(
846+
allocFn, IGM.getOptions().PointerAuth.CoroAllocationFunction,
847+
PointerAuthEntity::Special::CoroAllocationFunction);
848+
allocator.addSignedPointer(
849+
deallocFn, IGM.getOptions().PointerAuth.CoroDeallocationFunction,
850+
PointerAuthEntity::Special::CoroDeallocationFunction);
830851
return allocator.finishAndCreateFuture();
831852
},
832853
[&](llvm::GlobalVariable *var) { var->setConstant(true); });

lib/IRGen/GenPointerAuth.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ PointerAuthEntity::getDeclDiscriminator(IRGenModule &IGM) const {
400400
case Special::BlockCopyHelper:
401401
case Special::BlockDisposeHelper:
402402
llvm_unreachable("no known discriminator for these foreign entities");
403+
case Special::CoroAllocationFunction:
404+
return SpecialPointerAuthDiscriminators::CoroAllocationFunction;
405+
case Special::CoroDeallocationFunction:
406+
return SpecialPointerAuthDiscriminators::CoroDeallocationFunction;
403407
}
404408
llvm_unreachable("bad kind");
405409
};

lib/IRGen/GenPointerAuth.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class PointerAuthEntity {
6868
OpaqueTypeDescriptorAsArgument,
6969
ContextDescriptorAsArgument,
7070
TypeLayoutString,
71+
CoroAllocationFunction,
72+
CoroDeallocationFunction,
7173
};
7274

7375
private:

lib/IRGen/IRGen.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,14 @@ static void setPointerAuthOptions(PointerAuthOptions &opts,
990990

991991
opts.CoroPartialApplyCapture =
992992
PointerAuthSchema(nonABIDataKey, /*address*/ true, Discrimination::Decl);
993+
994+
opts.CoroAllocationFunction = PointerAuthSchema(
995+
codeKey, /*address*/ false, Discrimination::Constant,
996+
SpecialPointerAuthDiscriminators::CoroAllocationFunction);
997+
998+
opts.CoroDeallocationFunction = PointerAuthSchema(
999+
codeKey, /*address*/ false, Discrimination::Constant,
1000+
SpecialPointerAuthDiscriminators::CoroDeallocationFunction);
9931001
}
9941002

9951003
std::unique_ptr<llvm::TargetMachine>

test/IRGen/coroutine_accessors.swift

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,42 @@
2323
// CHECK-SAME: i32 0
2424
// CHECK-SAME: }>
2525

26+
// CHECK-arm64e-LABEL: _swift_malloc.ptrauth = private constant {
27+
// CHECK-arm64e-SAME: ptr @_swift_malloc,
28+
// CHECK-arm64e-SAME: i32 0,
29+
// CHECK-arm64e-SAME: i64 0,
30+
// CHECK-arm64e-SAME: i64 24469 }
31+
// CHECK-arm64e-SAME: section "llvm.ptrauth"
32+
// CHECK-arm64e-SAME: align 8
33+
// CHECK-arm64e-LABEL: _swift_free.ptrauth = private constant {
34+
// CHECK-arm64e-SAME: ptr @_swift_free,
35+
// CHECK-arm64e-SAME: i32 0,
36+
// CHECK-arm64e-SAME: i64 0,
37+
// CHECK-arm64e-SAME: i64 40879 },
38+
// CHECK-arm64e-SAME: section "llvm.ptrauth",
39+
// CHECK-arm64e-SAME: align 8
2640
// CHECK-LABEL: _swift_coro_malloc_allocator = linkonce_odr hidden constant %swift.coro_allocator {
2741
// CHECK-SAME: i32 258,
28-
// CHECK-SAME: malloc,
42+
// CHECK-SAME: malloc
2943
// CHECK-SAME: free
3044
// CHECK-SAME: }
45+
// CHECK-arm64e-LABEL: swift_task_alloc.ptrauth = private constant {
46+
// CHECK-arm64e-SAME: ptr @swift_task_alloc,
47+
// CHECK-arm64e-SAME: i32 0,
48+
// CHECK-arm64e-SAME: i64 0,
49+
// CHECK-arm64e-SAME: i64 24469 }
50+
// CHECK-arm64e-SAME: section "llvm.ptrauth"
51+
// CHECK-arm64e-SAME: align 8
52+
// CHECK-arm64e-LABEL: @swift_task_dealloc.ptrauth = private constant {
53+
// CHECK-arm64e-SAME: ptr @swift_task_dealloc,
54+
// CHECK-arm64e-SAME: i32 0,
55+
// CHECK-arm64e-SAME: i64 0,
56+
// CHECK-arm64e-SAME: i64 40879 },
57+
// CHECK-arm64e-SAME: section "llvm.ptrauth",
58+
// CHECK-arm64e-SAME: align 8
3159
// CHECK-LABEL: _swift_coro_async_allocator = linkonce_odr hidden constant %swift.coro_allocator {
3260
// CHECK-SAME: i32 1,
33-
// CHECK-SAME: swift_task_alloc,
61+
// CHECK-SAME: swift_task_alloc
3462
// CHECK-SAME: swift_task_dealloc
3563
// CHECK-SAME: }
3664

@@ -45,6 +73,9 @@
4573
// CHECK-SAME: i32 0
4674
// CHECK-SAME: i32 1
4775
// CHECK: [[ALLOCATE_FN:%[^,]+]] = load ptr, ptr [[ALLOCATE_FN_PTR]]
76+
// CHECK-arm64e: [[ALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[ALLOCATE_FN]] to i64
77+
// CHECK-arm64e: [[ALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[ALLOCATE_FN_BITS]], i32 0, i64 24469)
78+
// CHECK-arm64e: [[ALLOCATE_FN:%[^,]+]] = inttoptr i64 [[ALLOCATE_FN_BITS_AUTHED]]
4879
// CHECK: [[ALLOCATION:%[^,]+]] = call swiftcc ptr [[ALLOCATE_FN]]([[INT]] [[SIZE]])
4980
// CHECK: ret ptr [[ALLOCATION]]
5081
// CHECK: }
@@ -73,6 +104,9 @@
73104
// CHECK-SAME: i32 0
74105
// CHECK-SAME: i32 2
75106
// CHECK: [[DEALLOCATE_FN:%[^,]+]] = load ptr, ptr [[DEALLOCATE_FN_PTR]]
107+
// CHECK-arm64e: [[DEALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[DEALLOCATE_FN]] to i64
108+
// CHECK-arm64e: [[DEALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[DEALLOCATE_FN_BITS]], i32 0, i64 40879)
109+
// CHECK-arm64e: [[DEALLOCATE_FN:%[^,]+]] = inttoptr i64 [[DEALLOCATE_FN_BITS_AUTHED]]
76110
// CHECK: call swiftcc void [[DEALLOCATE_FN]](ptr [[ADDRESS]])
77111
// CHECK: ret void
78112
// CHECK: }

test/IRGen/coroutine_accessors_popless.swift

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,42 @@
2525
// CHECK-SAME: i32 0
2626
// CHECK-SAME: }>
2727

28+
// CHECK-arm64e-LABEL: swift_task_alloc.ptrauth = private constant {
29+
// CHECK-arm64e-SAME: ptr @swift_task_alloc,
30+
// CHECK-arm64e-SAME: i32 0,
31+
// CHECK-arm64e-SAME: i64 0,
32+
// CHECK-arm64e-SAME: i64 24469 }
33+
// CHECK-arm64e-SAME: section "llvm.ptrauth"
34+
// CHECK-arm64e-SAME: align 8
35+
// CHECK-arm64e-LABEL: @swift_task_dealloc.ptrauth = private constant {
36+
// CHECK-arm64e-SAME: ptr @swift_task_dealloc,
37+
// CHECK-arm64e-SAME: i32 0,
38+
// CHECK-arm64e-SAME: i64 0,
39+
// CHECK-arm64e-SAME: i64 40879 },
40+
// CHECK-arm64e-SAME: section "llvm.ptrauth",
41+
// CHECK-arm64e-SAME: align 8
2842
// CHECK-LABEL: _swift_coro_async_allocator = linkonce_odr hidden constant %swift.coro_allocator {
2943
// CHECK-SAME: i32 1,
30-
// CHECK-SAME: swift_task_alloc,
44+
// CHECK-SAME: swift_task_alloc
3145
// CHECK-SAME: swift_task_dealloc
3246
// CHECK-SAME: }
47+
// CHECK-arm64e-LABEL: _swift_malloc.ptrauth = private constant {
48+
// CHECK-arm64e-SAME: ptr @_swift_malloc,
49+
// CHECK-arm64e-SAME: i32 0,
50+
// CHECK-arm64e-SAME: i64 0,
51+
// CHECK-arm64e-SAME: i64 24469 }
52+
// CHECK-arm64e-SAME: section "llvm.ptrauth"
53+
// CHECK-arm64e-SAME: align 8
54+
// CHECK-arm64e-LABEL: _swift_free.ptrauth = private constant {
55+
// CHECK-arm64e-SAME: ptr @_swift_free,
56+
// CHECK-arm64e-SAME: i32 0,
57+
// CHECK-arm64e-SAME: i64 0,
58+
// CHECK-arm64e-SAME: i64 40879 },
59+
// CHECK-arm64e-SAME: section "llvm.ptrauth",
60+
// CHECK-arm64e-SAME: align 8
3361
// CHECK-LABEL: _swift_coro_malloc_allocator = linkonce_odr hidden constant %swift.coro_allocator {
3462
// CHECK-SAME: i32 258,
35-
// CHECK-SAME: malloc,
63+
// CHECK-SAME: malloc
3664
// CHECK-SAME: free
3765
// CHECK-SAME: }
3866

@@ -56,7 +84,10 @@
5684
// CHECK-SAME: i32 0
5785
// CHECK-SAME: i32 1
5886
// CHECK: [[ALLOCATE_FN:%[^,]+]] = load ptr, ptr [[ALLOCATE_FN_PTR]]
59-
// CHECK: [[ALLOCATION:%[^,]+]] = call swiftcc ptr [[ALLOCATE_FN]](i64 [[SIZE]])
87+
// CHECK-arm64e: [[ALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[ALLOCATE_FN]] to i64
88+
// CHECK-arm64e: [[ALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[ALLOCATE_FN_BITS]], i32 0, i64 24469)
89+
// CHECK-arm64e: [[ALLOCATE_FN:%[^,]+]] = inttoptr i64 [[ALLOCATE_FN_BITS_AUTHED]]
90+
// CHECK: [[ALLOCATION:%[^,]+]] = call swiftcc ptr [[ALLOCATE_FN]]([[INT]] [[SIZE]])
6091
// CHECK: ret ptr [[ALLOCATION]]
6192
// CHECK: }
6293

@@ -91,6 +122,9 @@
91122
// CHECK-SAME: i32 0
92123
// CHECK-SAME: i32 2
93124
// CHECK: [[DEALLOCATE_FN:%[^,]+]] = load ptr, ptr [[DEALLOCATE_FN_PTR]]
125+
// CHECK-arm64e: [[DEALLOCATE_FN_BITS:%[^,]+]] = ptrtoint ptr [[DEALLOCATE_FN]] to i64
126+
// CHECK-arm64e: [[DEALLOCATE_FN_BITS_AUTHED:%[^,]+]] = call i64 @llvm.ptrauth.auth(i64 [[DEALLOCATE_FN_BITS]], i32 0, i64 40879)
127+
// CHECK-arm64e: [[DEALLOCATE_FN:%[^,]+]] = inttoptr i64 [[DEALLOCATE_FN_BITS_AUTHED]]
94128
// CHECK: call swiftcc void [[DEALLOCATE_FN]](ptr [[ADDRESS]])
95129
// CHECK: ret void
96130
// CHECK: }

0 commit comments

Comments
 (0)