Skip to content

Commit b4249c9

Browse files
authored
Merge pull request #35755 from nate-chandler/concurrency/irgen/rdar72357371-3
[IRGen] Sign task resume context in swift_suspend_point.
2 parents 06a7065 + 3df7b67 commit b4249c9

14 files changed

+79
-33
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ struct PointerAuthOptions : clang::PointerAuthOptions {
147147
/// The resume function stored in AsyncTask.
148148
PointerAuthSchema TaskResumeFunction;
149149

150+
/// The async context stored in AsyncTask.
151+
PointerAuthSchema TaskResumeContext;
152+
150153
/// The swift async context entry in the extended frame info.
151154
PointerAuthSchema AsyncContextExtendedFrameEntry;
152155
};

lib/IRGen/GenFunc.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,11 @@ llvm::Function *IRGenFunction::createAsyncSuspendFn() {
25592559
auto *resumeAddr = Builder.CreateStructGEP(task, 4);
25602560
Builder.CreateStore(resumeFunction, Address(resumeAddr, ptrAlign));
25612561
auto *contextAddr = Builder.CreateStructGEP(task, 5);
2562+
if (auto schema = IGM.getOptions().PointerAuth.TaskResumeContext) {
2563+
auto authInfo = PointerAuthInfo::emit(suspendIGF, schema, contextAddr,
2564+
PointerAuthEntity());
2565+
context = emitPointerAuthSign(suspendIGF, context, authInfo);
2566+
}
25622567
Builder.CreateStore(context, Address(contextAddr, ptrAlign));
25632568
auto *suspendCall = Builder.CreateCall(
25642569
IGM.getTaskSwitchFuncFn(),

lib/IRGen/IRGen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,10 @@ static void setPointerAuthOptions(PointerAuthOptions &opts,
703703
PointerAuthSchema(codeKey, /*address*/ true, Discrimination::Constant,
704704
SpecialPointerAuthDiscriminators::TaskResumeFunction);
705705

706+
opts.TaskResumeContext =
707+
PointerAuthSchema(dataKey, /*address*/ true, Discrimination::Constant,
708+
SpecialPointerAuthDiscriminators::TaskResumeContext);
709+
706710
opts.AsyncContextExtendedFrameEntry = PointerAuthSchema(
707711
dataKey, /*address*/ true, Discrimination::Constant,
708712
SpecialPointerAuthDiscriminators::SwiftAsyncContextExtendedFrameEntry);

lib/IRGen/IRGenFunction.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "Callee.h"
2727
#include "Explosion.h"
28+
#include "GenPointerAuth.h"
2829
#include "IRGenDebugInfo.h"
2930
#include "IRGenFunction.h"
3031
#include "IRGenModule.h"
@@ -563,7 +564,13 @@ void IRGenFunction::emitGetAsyncContinuation(SILType resumeTy,
563564
// TODO: add lifetime with matching lifetime in await_async_continuation
564565
auto contResumeAddr =
565566
Builder.CreateStructGEP(continuationContext.getAddress(), 0);
566-
Builder.CreateStore(getAsyncContext(),
567+
llvm::Value *asyncContextValue = getAsyncContext();
568+
if (auto schema = IGM.getOptions().PointerAuth.AsyncContextParent) {
569+
auto authInfo = PointerAuthInfo::emit(*this, schema, contResumeAddr,
570+
PointerAuthEntity());
571+
asyncContextValue = emitPointerAuthSign(*this, asyncContextValue, authInfo);
572+
}
573+
Builder.CreateStore(asyncContextValue,
567574
Address(contResumeAddr, pointerAlignment));
568575
auto contErrResultAddr =
569576
Builder.CreateStructGEP(continuationContext.getAddress(), 2);
@@ -607,15 +614,27 @@ void IRGenFunction::emitGetAsyncContinuation(SILType resumeTy,
607614
assert(AsyncCoroutineCurrentResume == nullptr &&
608615
"Don't support nested get_async_continuation");
609616
AsyncCoroutineCurrentResume = coroResume;
610-
Builder.CreateStore(
611-
Builder.CreateBitOrPointerCast(coroResume, IGM.FunctionPtrTy),
612-
Address(currTaskResumeTaskAddr, pointerAlignment));
617+
llvm::Value *coroResumeValue =
618+
Builder.CreateBitOrPointerCast(coroResume, IGM.FunctionPtrTy);
619+
if (auto schema = IGM.getOptions().PointerAuth.TaskResumeFunction) {
620+
auto authInfo = PointerAuthInfo::emit(*this, schema, currTaskResumeTaskAddr,
621+
PointerAuthEntity());
622+
coroResumeValue = emitPointerAuthSign(*this, coroResumeValue, authInfo);
623+
}
624+
Builder.CreateStore(coroResumeValue,
625+
Address(currTaskResumeTaskAddr, pointerAlignment));
613626
// currTask->ResumeContext = &continuation_context;
614627
auto currTaskResumeCtxtAddr = Builder.CreateStructGEP(currTask, 5);
615-
Builder.CreateStore(
616-
Builder.CreateBitOrPointerCast(continuationContext.getAddress(),
617-
IGM.SwiftContextPtrTy),
618-
Address(currTaskResumeCtxtAddr, pointerAlignment));
628+
llvm::Value *continuationContextValue = Builder.CreateBitOrPointerCast(
629+
continuationContext.getAddress(), IGM.SwiftContextPtrTy);
630+
if (auto schema = IGM.getOptions().PointerAuth.TaskResumeContext) {
631+
auto authInfo = PointerAuthInfo::emit(*this, schema, currTaskResumeCtxtAddr,
632+
PointerAuthEntity());
633+
continuationContextValue =
634+
emitPointerAuthSign(*this, continuationContextValue, authInfo);
635+
}
636+
Builder.CreateStore(continuationContextValue,
637+
Address(currTaskResumeCtxtAddr, pointerAlignment));
619638

620639
// Publish all the writes.
621640
// continuation_context.awaitSynchronization =(atomic release) nullptr;

test/Concurrency/Runtime/actor_counters.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
// Remove with rdar://problem/72439642
88
// UNSUPPORTED: asan
9-
// Remove with rdar://problem/72357371
10-
// UNSUPPORTED: CPU=arm64e
119

1210
#if canImport(Darwin)
1311
import Darwin

test/Concurrency/Runtime/async_taskgroup_add_handle_completion.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency -parse-as-library) | %FileCheck %s --dump-input=always
22
// REQUIRES: executable_test
33
// REQUIRES: concurrency
4-
// REQUIRES: OS=macosx
5-
// REQUIRES: CPU=x86_64
4+
// XFAIL: windows
5+
// XFAIL: linux
66

77
import Dispatch
88
import Darwin

test/Concurrency/Runtime/async_taskgroup_is_empty.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency -parse-as-library) | %FileCheck %s --dump-input always
22
// REQUIRES: executable_test
33
// REQUIRES: concurrency
4-
// REQUIRES: OS=macosx
5-
// REQUIRES: CPU=x86_64
4+
// XFAIL: windows
5+
// XFAIL: linux
66

77
import Dispatch
88

test/Concurrency/Runtime/async_taskgroup_next_not_invoked_cancelAll.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency -parse-as-library) | %FileCheck %s --dump-input=always
22
// REQUIRES: executable_test
33
// REQUIRES: concurrency
4-
// REQUIRES: OS=macosx
5-
// REQUIRES: CPU=x86_64
4+
// XFAIL: windows
5+
// XFAIL: linux
66

77
import Dispatch
88

test/Concurrency/Runtime/async_taskgroup_next_not_invoked_without_cancelAll.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency -parse-as-library) | %FileCheck %s --dump-input=always
22
// REQUIRES: executable_test
33
// REQUIRES: concurrency
4-
// REQUIRES: OS=macosx
5-
// REQUIRES: CPU=x86_64
4+
// XFAIL: windows
5+
// XFAIL: linux
66

77
import func Foundation.sleep
88

test/Concurrency/Runtime/async_taskgroup_throw_recover.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency -parse-as-library) | %FileCheck %s --dump-input always
22
// REQUIRES: executable_test
33
// REQUIRES: concurrency
4-
// REQUIRES: OS=macosx
5-
// REQUIRES: CPU=x86_64
4+
// XFAIL: windows
5+
// XFAIL: linux
66

77
import Dispatch
88

test/Concurrency/Runtime/async_taskgroup_throw_rethrow.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency -parse-as-library) | %FileCheck %s --dump-input always
22
// REQUIRES: executable_test
33
// REQUIRES: concurrency
4-
// REQUIRES: OS=macosx
5-
// REQUIRES: CPU=x86_64
64

75
// REQUIRES: rdar73154198
86

test/Concurrency/Runtime/mainactor.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
// REQUIRES: OS=macosx || OS=ios
77
// FIXME: should not require Darwin to run this test once we have async main!
8-
// Remove with rdar://problem/72357371
9-
// UNSUPPORTED: CPU=arm64e
108

119
// for exit(:Int)
1210
#if canImport(Darwin)

test/Concurrency/Runtime/objc_async.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
// REQUIRES: concurrency
88
// REQUIRES: objc_interop
99

10-
// Remove with rdar://problem/72357371
11-
// UNSUPPORTED: CPU=arm64e
12-
1310

1411
@main struct Main {
1512
static func main() async {

test/IRGen/async/get_async_continuation.sil

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -enable-experimental-concurrency -enable-objc-interop -primary-file %s -emit-ir -sil-verify-all -disable-llvm-optzns -disable-swift-specific-llvm-optzns | %FileCheck %s
1+
// RUN: %target-swift-frontend -enable-experimental-concurrency -enable-objc-interop -primary-file %s -emit-ir -sil-verify-all -disable-llvm-optzns -disable-swift-specific-llvm-optzns | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%target-cpu
22
// RUN: %target-swift-frontend -enable-experimental-concurrency -enable-objc-interop -primary-file %s -emit-ir -sil-verify-all
33

44
// REQUIRES: concurrency
@@ -27,7 +27,15 @@ bb0:
2727
// Initialize the async continuation context.
2828
// CHECK: [[context_addr:%.*]] = getelementptr inbounds %swift.async_continuation_context, %swift.async_continuation_context* [[cont_context]], i32 0, i32 0
2929
// CHECK: [[ctxt:%.*]] = load %swift.context*, %swift.context** [[ctxt_addr]]
30-
// CHECK: store %swift.context* [[ctxt]], %swift.context** [[context_addr]]
30+
31+
// CHECK-arm64e: [[ctxt_addr_int:%[0-9]+]] = ptrtoint %swift.context** [[context_addr]] to i64
32+
// CHECK-arm64e: [[ptrauth_blend:%[0-9]+]] = call i64 @llvm.ptrauth.blend.i64(i64 [[ctxt_addr_int]], i64 48546)
33+
// CHECK-arm64e: [[ctxt_int:%[0-9]+]] = ptrtoint %swift.context* [[ctxt]] to i64
34+
// CHECK-arm64e: [[signed_int:%[0-9]+]] = call i64 @llvm.ptrauth.sign.i64(i64 [[ctxt_int]], i32 2, i64 [[ptrauth_blend]])
35+
// CHECK-arm64e: [[signed_ctxt:%[0-9]+]] = inttoptr i64 [[signed_int]] to %swift.context*
36+
// CHECK-arm64e: store %swift.context* [[signed_ctxt]], %swift.context** [[context_addr]]
37+
// CHECK-x86_64: store %swift.context* [[ctxt]], %swift.context** [[context_addr]]
38+
3139
// CHECK: [[error_addr:%.*]] = getelementptr inbounds %swift.async_continuation_context, %swift.async_continuation_context* [[cont_context]], i32 0, i32 2
3240
// CHECK: store %swift.error* null, %swift.error** [[error_addr]]
3341
// CHECK: [[result_addr:%.*]] = getelementptr inbounds %swift.async_continuation_context, %swift.async_continuation_context* [[cont_context]], i32 0, i32 3
@@ -37,12 +45,28 @@ bb0:
3745
// CHECK: [[exe:%.*]] = load %swift.executor*, %swift.executor** [[exe_addr]]
3846
// CHECK: store %swift.executor* [[exe]], %swift.executor** [[exectuor_addr]]
3947
// Initialize the async task with the continuation function and async continuation context.
40-
// CHECK: [[task_continuation_fn_addr:%.*]] = getelementptr inbounds %swift.task, %swift.task* [[tsk]], i32 0, i32 4
48+
// CHECK: [[continuation_fn_addr:%.*]] = getelementptr inbounds %swift.task, %swift.task* [[tsk]], i32 0, i32 4
4149
// CHECK: [[continuation_fn:%.*]] = call i8* @llvm.coro.async.resume()
42-
// CHECK: store i8* [[continuation_fn]], i8** [[task_continuation_fn_addr]]
50+
51+
// CHECK-arm64e: [[continuation_fn_addr_int:%[0-9]+]] = ptrtoint i8** [[continuation_fn_addr]] to i64
52+
// CHECK-arm64e: [[ptrauth_blend:%[0-9]+]] = call i64 @llvm.ptrauth.blend.i64(i64 [[continuation_fn_addr_int]], i64 11330)
53+
// CHECK-arm64e: [[continuation_fn_int:%[0-9]+]] = ptrtoint i8* [[continuation_fn]] to i64
54+
// CHECK-arm64e: [[signed_int:%[0-9]+]] = call i64 @llvm.ptrauth.sign.i64(i64 [[continuation_fn_int]], i32 0, i64 [[ptrauth_blend]])
55+
// CHECK-arm64e: [[signed_continuation_fn:%[0-9]+]] = inttoptr i64 [[signed_int]] to i8*
56+
// CHECK-arm64e: store i8* [[signed_continuation_fn]], i8** [[continuation_fn_addr]]
57+
// CHECK-x86_64: store i8* [[continuation_fn]], i8** [[continuation_fn_addr]]
58+
4359
// CHECK: [[task_resume_context_addr:%.*]] = getelementptr inbounds %swift.task, %swift.task* [[tsk]], i32 0, i32 5
44-
// CHECK: [[cont_context2:%.*]] = bitcast %swift.async_continuation_context* [[cont_context]] to %swift.context*
45-
// CHECK: store %swift.context* [[cont_context2]], %swift.context** [[task_resume_context_addr]]
60+
// CHECK: [[task_resume_context:%.*]] = bitcast %swift.async_continuation_context* [[cont_context]] to %swift.context*
61+
62+
// CHECK-arm64e: [[task_resume_context_addr_int:%[0-9]+]] = ptrtoint %swift.context** [[task_resume_context_addr]] to i64
63+
// CHECK-arm64e: [[ptrauth_blend:%[0-9]+]] = call i64 @llvm.ptrauth.blend.i64(i64 [[task_resume_context_addr_int]], i64 30010)
64+
// CHECK-arm64e: [[task_resume_context_int:%[0-9]+]] = ptrtoint %swift.context* [[task_resume_context]] to i64
65+
// CHECK-arm64e: [[signed_int:%[0-9]+]] = call i64 @llvm.ptrauth.sign.i64(i64 [[task_resume_context_int]], i32 2, i64 [[ptrauth_blend]])
66+
// CHECK-arm64e: [[signed_task_resume_context:%[0-9]+]] = inttoptr i64 [[signed_int]] to %swift.context*
67+
// CHECK-arm64e: store %swift.context* [[signed_task_resume_context]], %swift.context** [[task_resume_context_addr]]
68+
// CHECK-x86_64: store %swift.context* [[task_resume_context]], %swift.context** [[task_resume_context_addr]]
69+
4670
// Initialize the synchronization variable.
4771
// CHECK: [[synchronization_addr:%.*]] = getelementptr inbounds %swift.async_continuation_context, %swift.async_continuation_context* [[cont_context]], i32 0, i32 1
4872
// CHECK: store atomic {{(i64|i32)}} 0, {{(i64|i32)}}* [[synchronization_addr]] release

0 commit comments

Comments
 (0)