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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
|
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -parse-as-library)
// REQUIRES: concurrency
// REQUIRES: executable_test
// rdar://106849189 move-only types should be supported in freestanding mode
// UNSUPPORTED: freestanding
// rdar://76038845
// UNSUPPORTED: back_deployment_runtime
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deploy_concurrency
// Crash expectations can't be implemented on WASI/WebAssembly.
// UNSUPPORTED: OS=wasi
// This test makes sure that we properly save/restore access when we
// synchronously launch a task from a serial executor. The access from the task
// should be merged into the already created access set while it runs and then
// unmerged afterwards.
import _Concurrency
import StdlibUnittest
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Android)
import Android
#elseif canImport(CRT)
import CRT
#endif
@inlinable
public func debugLog(_ s: String) {
// Only enable this when debugging test failures against an asserts
// runtime. Otherwise, the test is noisy and has non-windows
// dependencies. We need stderr to ensure proper log output interleaving
// with the runtime's own stderr emitted output.
#if DEBUG_LOGGING
fputs(s + "\n", stderr)
fflush(stderr)
#endif
}
@inline(never)
public func withExclusiveAccess<T, U>(to x: inout T, f: (inout T) -> U) -> U {
debugLog("==> Enter 'withExclusiveAccess'")
defer { debugLog("==> Exit 'withExclusiveAccess'") }
return f(&x)
}
@available(SwiftStdlib 5.1, *)
@MainActor @inline(never)
func withExclusiveAccessAsync<T, U>(to x: inout T, f: (inout T) async -> U) async -> U {
debugLog("==> Enter 'withExclusiveAccessAsync'")
defer { debugLog("==> Exit 'withExclusiveAccessAsync'") }
return await f(&x)
}
@available(SwiftStdlib 5.1, *)
public final class MySerialExecutor : SerialExecutor {
public init() {
debugLog("==> MySerialExecutor: Creating MySerialExecutor!")
}
public static var sharedSerialExecutor = MySerialExecutor()
public static var sharedUnownedExecutor: UnownedSerialExecutor {
debugLog("==> MySerialExecutor: Getting Shared Unowned Executor!")
return UnownedSerialExecutor(ordinary: sharedSerialExecutor)
}
public func enqueue(_ job: UnownedJob) {
debugLog("==> MySerialExecutor: Got an enqueue!")
// This is the exclusive access that we are going to be swizzling
// in/out.
//
// Make sure we have 2x synchronized to test.
withExclusiveAccess(to: &global2) { _ in
withExclusiveAccess(to: &global3) { _ in
debugLog("==> MySerialExecutor: Inside access!")
job._runSynchronously(on: asUnownedSerialExecutor())
debugLog("==> MySerialExecutor: Inside access after run synchronously!")
}
}
debugLog("==> MySerialExecutor: After access, after run synchronously")
}
public func asUnownedSerialExecutor() -> UnownedSerialExecutor {
debugLog("==> MySerialExecutor: Getting Unowned Executor!")
return UnownedSerialExecutor(ordinary: self)
}
}
/// A singleton actor whose executor is equivalent to the main
/// dispatch queue.
@available(SwiftStdlib 5.1, *)
@globalActor public final actor MyMainActor: Executor {
public static let shared = MyMainActor()
public let executor = MySerialExecutor()
@inlinable
public nonisolated var unownedExecutor: UnownedSerialExecutor {
debugLog("==> MyMainActor: Getting unowned executor!")
return executor.asUnownedSerialExecutor()
}
@inlinable
public static var sharedUnownedExecutor: UnownedSerialExecutor {
debugLog("==> MyMainActor: Getting shared unowned executor!")
return MySerialExecutor.sharedUnownedExecutor
}
@inlinable
public nonisolated func enqueue(_ job: UnownedJob) {
debugLog("==> MyMainActor: enqueuing!")
executor.enqueue(job)
}
}
/// An actor that we use to test that after eliminating the synchronous
/// accesses, we properly deserialize the task access set causing a crash in
/// unownedExecutor.
@available(SwiftStdlib 5.1, *)
@globalActor public final actor MyMainActorWithAccessInUnownedExecAccessor: Executor {
public static let shared = MyMainActorWithAccessInUnownedExecAccessor()
public let executor = MySerialExecutor()
@inlinable
public nonisolated var unownedExecutor: UnownedSerialExecutor {
debugLog("==> MyMainActorWithAccessInUnownedExecAccessor: Getting unowned executor!")
withExclusiveAccess(to: &global) { _ in debugLog("Crash!") }
return executor.asUnownedSerialExecutor()
}
@inlinable
public static var sharedUnownedExecutor: UnownedSerialExecutor {
debugLog("==> MyMainActorWithAccessInUnownedExecAccessor: Getting shared unowned executor!")
return MySerialExecutor.sharedUnownedExecutor
}
@inlinable
public nonisolated func enqueue(_ job: UnownedJob) {
debugLog("==> MyMainActorWithAccessInUnownedExecAccessor: enqueuing!")
executor.enqueue(job)
}
}
@available(SwiftStdlib 5.1, *)
actor Custom {
var count = 0
func report() async {
debugLog("==> Custom: custom.count == \(count)")
count += 1
}
}
@available(SwiftStdlib 5.1, *)
@globalActor
struct CustomActor {
static var shared: Custom {
debugLog("==> CustomActor: Getting custom!")
return Custom()
}
}
public var global: Int = 5
public var global2: Int = 6
public var global3: Int = 7
public var global4: Int = 8
@available(SwiftStdlib 5.1, *)
@main
struct Runner {
@MainActor static func main() async {
var exclusivityTests = TestSuite("Async Exclusivity Custom Executors")
// As a quick soundness test, make sure that the crash doesn't occur if we
// don't have the withExclusiveAccess(to: ) from the case below.
exclusivityTests.test("exclusivityAccessesPropagateFromExecutorIntoTasks NoConflict") {
@MainActor in
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
}
await handle.value
}
// Make sure that we crash here due to the task forming an access to
// memory that the executor has exclusive access to.
exclusivityTests.test("exclusivityAccessesPropagateFromExecutorIntoTasks Crash") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
withExclusiveAccess(to: &global2) { _ in
debugLog("==> Crash!")
}
debugLog("==> Main: All done!")
}
await handle.value
}
// If all of the previous tests passed, then we have basic soundness
// done. Lets now test out our cases that involve a live sync access.
//
// We test cases 3,4,7,8 here. The other cases that do not involve a
// custom executor are tested in Runtime/exclusivity.swift.
// Now that we have tested our tests with various numbers of accesses,
// lets make specific tests for each case in Exclusivity.cpp.
//
// Case 3: (F, T, F) - No Live Accesses at Task Start, Exiting Live Sync
// Accesses When Push, No Live Task Accesses when pop.
//
// This case is the case where we do not have any accesses in our code
// at all or if the task cleans up the tasks before it awaits again. We
// test the task cleanup case.
exclusivityTests.test("case3") { @MainActor in
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
withExclusiveAccess(to: &global) { _ in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
// Different task without sync so no access issue.
withExclusiveAccess(to: &global) { _ in
debugLog("==> Making sure can push/pop access")
}
withExclusiveAccess(to: &global2) { _ in
debugLog("==> Making sure can push/pop access")
}
}
await handle2.value
}
withExclusiveAccess(to: &global2) { _ in
// Sync accesses should have not been injected yet.
debugLog("==> No crash.")
}
withExclusiveAccess(to: &global) { _ in
// Make sure that global is cleaned up appropriately.
debugLog("==> No crash.")
}
await handle.value
withExclusiveAccess(to: &global2) { _ in
// Sync accesses should have been cleaned up by now. So no
// crash.
debugLog("==> No crash.")
}
withExclusiveAccess(to: &global) { _ in
// Make sure that global is cleaned up appropriately.
debugLog("==> No crash.")
}
}
// Make sure that we crash when accessing &global.
exclusivityTests.test("case3.crash") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
withExclusiveAccess(to: &global) { _ in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
// No crash here.
withExclusiveAccess(to: &global) { _ in
debugLog("==> Making sure can push/pop access")
}
withExclusiveAccess(to: &global2) { _ in
debugLog("==> Making sure can push/pop access")
}
}
await handle2.value
// But we should crash here if we restore appropriately.
withExclusiveAccess(to: &global2) { _ in
debugLog("Crash!")
}
}
await handle.value
}
// Case 4: (F, T, T). In case 4, our task does not start with a live
// access but we have accesses from the outside synchronous context, and
// do add new accesses when we pop.
exclusivityTests.test("case4") { @MainActor in
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsync(to: &global) {
@MyMainActor (x: inout Int) async -> Void in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
// Make sure we crash with the sync access despite mixing in the tasks
// accesses.
exclusivityTests.test("case4.execaccess.to_sync") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsync(to: &global) {
@MyMainActor (x: inout Int) async -> Void in
withExclusiveAccess(to: &global2) { _ in debugLog("CRASH!") }
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
// Make sure we do not crash with the sync access despite mixing in the tasks
// accesses.
exclusivityTests.test("case4.no_crash") { @MainActor in
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsync(to: &global) {
@MyMainActor (x: inout Int) async -> Void in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
// This test makes sure that despite us going through case4, that we
// properly deserialize the task's access and hit a crash in the
// UnownedExecAccessor.
exclusivityTests.test("case4.crash_due_to_deserialized_task") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
debugLog("==> Before handle")
let handle = Task { @MyMainActorWithAccessInUnownedExecAccessor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsync(to: &global) {
@MyMainActorWithAccessInUnownedExecAccessor (x: inout Int) async -> Void in
print("do something to avoid optimizing away to executor switch")
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
// CHECK-LABEL: ==> Enter 'testCase4'
// CHECK: ==> MySerialExecutor: Got an enqueue!
// CHECK-NEXT: Inserting new access: [[SYNC_NODE_1:0x[0-9a-f]+]]
// CHECK-NEXT: Tracking!
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_1:0x[0-9a-f]+]]. PC:
// CHECK: ==> Enter 'withExclusiveAccess'
// CHECK-NEXT: Inserting new access: [[SYNC_NODE_2:0x[0-9a-f]+]]
// CHECK-NEXT: Tracking!
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_2]]. PC:
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_1]]. PC:
// CHECK: ==> Enter 'withExclusiveAccess'
// CHECK: ==> MySerialExecutor: Inside access!
// CHECK: ==> MySerialExecutor: Getting Unowned Executor!
// CHECK-NEXT: Entering Thread Local Context. Before Swizzle. Task: [[TASK:0x[0-9a-f]+]]
// CHECK-NEXT: SwiftTaskThreadLocalContext: (FirstAccess,LastAccess): (0x0, 0x0)
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_2]]. PC:
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_1]]. PC:
// CHECK-NEXT: Entering Thread Local Context. After Swizzle. Task: [[TASK]]
// CHECK-NEXT: SwiftTaskThreadLocalContext: (FirstAccess,LastAccess): ([[SYNC_NODE_2]], 0x0)
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_2]]. PC:
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_1]]. PC:
// CHECK: ==> Main: In handle!
// CHECK: ==> No Crash!
// CHECK: ==> Main: All done!
// CHECK: Inserting new access: [[TASK_NODE:0x[0-9a-f]+]]
// CHECK-NEXT: Tracking!
// CHECK-NEXT: Access. Pointer: [[TASK_ACCESS:0x[0-9a-f]+]].
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_2]]. PC:
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_1]]. PC:
// CHECK-NEXT: Exiting Thread Local Context. Before Swizzle. Task: [[TASK]]
// CHECK-NEXT: SwiftTaskThreadLocalContext: (FirstAccess,LastAccess): ([[SYNC_NODE_2]], 0x0)
// CHECK-NEXT: Access. Pointer: [[TASK_ACCESS]]. PC:
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_2]]. PC:
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_1]]. PC:
// CHECK-NEXT: Exiting Thread Local Context. After Swizzle. Task: [[TASK]]
// CHECK-NEXT: SwiftTaskThreadLocalContext: (FirstAccess,LastAccess): ([[TASK_NODE]], [[TASK_NODE]])
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_2]]. PC:
// CHECK-NEXT: Access. Pointer: [[SYNC_ACCESS_1]]. PC:
// CHECK: ==> MySerialExecutor: Inside access after run synchronously!
// CHECK: ==> Exit 'testCase4'
exclusivityTests.test("case4.filecheck") { @MainActor in
debugLog("==> Enter 'testCase4'")
defer { debugLog("==> Exit 'testCase4'") }
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsync(to: &global) {
@MyMainActor (x: inout Int) async -> Void in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
// Case 7. (T, T, F). In case 7, our task starts with live accesses and
// sync accesses, but the live accesses are popped before we return.
@Sendable
@MyMainActor @inline(never)
func withExclusiveAccessAsyncCase7<T, U>(to x: inout T, f: (inout T) async -> U) async -> U {
debugLog("==> Enter 'withExclusiveAccessAsyncCase7'")
defer { debugLog("==> Exit 'withExclusiveAccessAsyncCase7'") }
let t = Task { @MainActor in
debugLog("==> Task to force serialization of MyMainActor by using MainActor")
}
await t.value
return await f(&x)
}
exclusivityTests.test("case7") { @MainActor in
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsyncCase7(to: &global) {
@MyMainActor (x: inout Int) async -> Void in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
@Sendable
@MyMainActor @inline(never)
func withExclusiveAccessAsyncCase7AccessGlobal<T, U>(to x: inout T, f: (inout T) async -> U) async -> U {
debugLog("==> Enter 'withExclusiveAccessAsyncCase7'")
defer { debugLog("==> Exit 'withExclusiveAccessAsyncCase7'") }
let t = Task { @MainActor in
debugLog("==> Task to force serialization of MyMainActor by using MainActor")
}
await t.value
// We should crash here since x should also be global and we
// properly deserialized.
withExclusiveAccess(to: &global) { _ in }
return await f(&x)
}
// Validate case7 by crashing due to a Task access <-> Task access conflict
exclusivityTests.test("case7.crash.taskaccess_taskaccess_conflict") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsyncCase7AccessGlobal(to: &global) {
@MyMainActor (x: inout Int) async -> Void in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
@Sendable
@MyMainActor @inline(never)
func withExclusiveAccessAsyncCase7AccessGlobal2<T, U>(to x: inout T, f: (inout T) async -> U) async -> U {
debugLog("==> Enter 'withExclusiveAccessAsyncCase7'")
defer { debugLog("==> Exit 'withExclusiveAccessAsyncCase7'") }
let t = Task { @MainActor in
debugLog("==> Task to force serialization of MyMainActor by using MainActor")
}
await t.value
// We should crash here since our executor had exclusive access to
// global2.
withExclusiveAccess(to: &global2) { _ in }
return await f(&x)
}
// Validate case7 by crashing due to a Task access <-> Task access conflict
exclusivityTests.test("case7.crash.syncaccess_taskaccess_conflict") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsyncCase7AccessGlobal2(to: &global) {
@MyMainActor (x: inout Int) async -> Void in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
// Case 8. (T, T, T). In case 8, our task starts with live accesses and
// sync accesses, and we have remaining live accesses when we return.
@Sendable
@MyMainActor @inline(never)
func withExclusiveAccessAsyncCase8<T, U>(to x: inout T, f: (inout T) async -> U) async -> U {
debugLog("==> Enter 'withExclusiveAccessAsyncCase8'")
defer { debugLog("==> Exit 'withExclusiveAccessAsyncCase8'") }
let t = Task { @MainActor in
debugLog("==> Task1 to force serialization of MyMainActor by using MainActor")
}
await t.value
await f(&x)
let t2 = Task { @MainActor in
debugLog("==> Task2 to force serialization of MyMainActor by using MainActor")
}
await t2.value
return await f(&x)
}
exclusivityTests.test("case8") { @MainActor in
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsyncCase8(to: &global) {
@MyMainActor (x: inout Int) async -> Void in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
// Case 8. (T, T, T). In case 8, our task starts with live accesses and
// sync accesses, and we have remaining live accesses when we return.
@Sendable
@MyMainActor @inline(never)
func withExclusiveAccessAsyncCase8Access<T, U>(to x: inout T, f: (inout T) async -> U) async -> U {
debugLog("==> Enter 'withExclusiveAccessAsyncCase8'")
defer { debugLog("==> Exit 'withExclusiveAccessAsyncCase8'") }
let t = Task { @MainActor in
debugLog("==> Task1 to force serialization of MyMainActor by using MainActor")
}
await t.value
await f(&x)
let t2 = Task { @MainActor in
debugLog("==> Task2 to force serialization of MyMainActor by using MainActor")
}
await t2.value
// This is the time period we are testing works in the positive case.
return await f(&x)
}
exclusivityTests.test("case8") { @MainActor in
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsyncCase8(to: &global) {
@MyMainActor (x: inout Int) async -> Void in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
@Sendable
@MyMainActor @inline(never)
func withExclusiveAccessAsyncCase8AccessGlobal<T, U>(to x: inout T, f: (inout T) async -> U) async -> U {
debugLog("==> Enter 'withExclusiveAccessAsyncCase8'")
defer { debugLog("==> Exit 'withExclusiveAccessAsyncCase8'") }
let t = Task { @MainActor in
debugLog("==> Task1 to force serialization of MyMainActor by using MainActor")
}
await t.value
await f(&x)
let t2 = Task { @MainActor in
debugLog("==> Task2 to force serialization of MyMainActor by using MainActor")
}
await t2.value
// Make sure we swizzled back in our serialized task state, so we
// crash.
withExclusiveAccess(to: &global) { _ in
debugLog("==> TaskAccess + TaskAccess == Crash!")
}
return await f(&x)
}
exclusivityTests.test("case8.crash.taskaccess_taskaccess_conflict") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsyncCase8AccessGlobal(to: &global) {
@MyMainActor (x: inout Int) async -> Void in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = await Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
@Sendable
@MyMainActor @inline(never)
func withExclusiveAccessAsyncCase8AccessGlobal2<T, U>(to x: inout T, f: (inout T) async -> U) async -> U {
debugLog("==> Enter 'withExclusiveAccessAsyncCase8'")
defer { debugLog("==> Exit 'withExclusiveAccessAsyncCase8'") }
let t = Task { @MainActor in
debugLog("==> Task1 to force serialization of MyMainActor by using MainActor")
}
await t.value
await f(&x)
let t2 = Task { @MainActor in
debugLog("==> Task2 to force serialization of MyMainActor by using MainActor")
}
await t2.value
// Make sure we swizzled back in our serialized task state, so we
// crash.
withExclusiveAccess(to: &global2) { _ in
debugLog("==> SyncAccess + TaskAccess == Crash!")
}
return await f(&x)
}
exclusivityTests.test("case8.crash.syncaccess_taskaccess_conflict") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
debugLog("==> Before handle")
let handle = Task { @MyMainActor in
debugLog("==> Main: In handle!")
debugLog("==> No Crash!")
debugLog("==> Main: All done!")
await withExclusiveAccessAsyncCase8AccessGlobal2(to: &global) {
@MyMainActor (x: inout Int) async -> Void in
debugLog("==> Making sure can push/pop access")
}
// In order to test that we properly hand off the access, we
// need to await here.
let handle2 = Task { @CustomActor in
debugLog("==> In inner handle")
}
await handle2.value
}
await handle.value
}
await runAllTestsAsync()
}
}
|