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
|
// RUN: %target-run-simple-swift(-Xfrontend -disable-availability-checking -parse-as-library)
// REQUIRES: executable_test
// REQUIRES: concurrency
// UNSUPPORTED: freestanding
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
// UNSUPPORTED: OS=wasi
// UNSUPPORTED: back_deploy_concurrency
// This test makes sure that:
//
// 1. Tasks have separate exclusivity sets.
// 2. Exercise the pushing/popping of access sets from tasks.
// NOTE: The cases that we are talking about handling below refer to the cases
// documented in Exclusivity.cpp.
//
// NOTE: We test cases that involve custom executors in
// custom_executors_exclusivity.cpp.
import _Concurrency
import StdlibUnittest
var global1: Int = 5
var global2: Int = 6
var global3: Int = 7
#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
}
@available(SwiftStdlib 5.1, *)
@main
struct Runner {
@MainActor
@inline(never)
static func withExclusiveAccessAsync<T, U>(to x: inout T, f: (inout T) async -> U) async -> U {
await f(&x)
}
@MainActor
@inline(never)
static func withExclusiveAccess<T, U>(to x: inout T, f: (inout T) -> U) -> U {
f(&x)
}
@inline(never)
@MainActor
static func doSomething() async { }
@inline(never)
@Sendable
static func useGlobal(_ x: inout Int) { debugLog("FORCE ACCESS") }
@MainActor static func main() async {
var exclusivityTests = TestSuite("Async Exclusivity")
// First make sure that if we do not introduce a new task, we still get
// our expected conflict.
exclusivityTests.test("testSameTaskBlowsUpSinceSameSet") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
let callee2 = { @MainActor (_ x: inout Int) -> Void in
debugLog("==> Enter callee2")
debugLog("Value: x: \(x)")
debugLog("==> Exit callee2")
}
// We add an inline never here to make sure that we do not eliminate
// the dynamic access after inlining.
@MainActor
@inline(never)
func callee1(_ x: inout Int) async -> () {
debugLog("==> Enter callee1")
// Second access. Same Task so not ok.
await callee2(&global1)
debugLog("==> Exit callee1")
}
debugLog("==> Enter Main")
// First access begins here.
await callee1(&global1)
debugLog("==> Exit Main")
}
// Then do a simple test with a single access to make sure that we do
// not hit any successes b/c we introduced the Task.
exclusivityTests.test("testDifferentTasksHaveDifferentExclusivityAccessSets") { @MainActor in
let callee2 = { @MainActor (_ x: inout Int) -> Void in
debugLog("==> Enter callee2")
debugLog("==> Exit callee2")
}
// We add an inline never here to make sure that we do not eliminate
// the dynamic access after inlining.
@MainActor
@inline(never)
func callee1(_ x: inout Int) async -> () {
debugLog("==> Enter callee1")
// This task is what prevents this example from crashing.
let handle = Task { @MainActor in
debugLog("==> Enter callee1 Closure")
// Second access. Different Task so it is ok.
await callee2(&global1)
debugLog("==> Exit callee1 Closure")
}
await handle.value
debugLog("==> Exit callee1")
}
debugLog("==> Enter Main")
// First access begins here.
await callee1(&global1)
debugLog("==> Exit Main")
}
// Make sure we correctly handle cases where we have multiple accesses
// open at the same time.
exclusivityTests.test("testDifferentTasksWith2Accesses") { @MainActor in
let callee2 = { @MainActor (_ x: inout Int, _ y: inout Int) -> Void in
debugLog("==> Enter callee2")
debugLog("==> Exit callee2")
}
// We add an inline never here to make sure that we do not eliminate
// the dynamic access after inlining.
@MainActor
@inline(never)
func callee1(_ x: inout Int, _ y: inout Int) async -> () {
debugLog("==> Enter callee1")
let handle = Task { @MainActor in
debugLog("==> Enter callee1 Closure")
// Second access. Different Task so it is ok.
await callee2(&global1, &global2)
debugLog("==> Exit callee1 Closure")
}
await handle.value
debugLog("==> Exit callee1")
}
debugLog("==> Enter Main")
// First access begins here.
await callee1(&global1, &global2)
debugLog("==> Exit Main")
}
// Make sure we correctly handle cases where we have multiple accesses
// open at the same time.
exclusivityTests.test("testDifferentTasksWith3Accesses") { @MainActor in
let callee2 = { @MainActor (_ x: inout Int, _ y: inout Int, _ z: inout Int) -> Void in
debugLog("==> Enter callee2")
debugLog("==> Exit callee2")
}
// We add an inline never here to make sure that we do not eliminate
// the dynamic access after inlining.
@MainActor
@inline(never)
func callee1(_ x: inout Int, _ y: inout Int, _ z: inout Int) async -> () {
debugLog("==> Enter callee1")
let handle = Task { @MainActor in
debugLog("==> Enter callee1 Closure")
// Second access. Different Task so it is ok.
await callee2(&global1, &global2, &global3)
debugLog("==> Exit callee1 Closure")
}
await handle.value
debugLog("==> Exit callee1")
}
debugLog("==> Enter Main")
// First access begins here.
await callee1(&global1, &global2, &global3)
debugLog("==> Exit Main")
}
// Now that we have tested our tests with various numbers of accesses,
// lets make specific tests for each case in Exclusivity.cpp.
//
// Case 1: (F, F, F) - No Live Accesses at Task Start, No 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("case1") { @MainActor in
@inline(never)
@Sendable func callee2(_ x: inout Int, _ y: inout Int, _ z: inout Int) -> Void {
debugLog("==> Enter callee2")
debugLog("==> Exit callee2")
}
// We add an inline never here to make sure that we do not eliminate
// the dynamic access after inlining.
@MainActor
@inline(never)
func callee1() async -> () {
debugLog("==> Enter callee1")
let handle = Task { @MainActor in
debugLog("==> Enter callee1 Closure")
// These accesses end before we await in the task.
do {
callee2(&global1, &global2, &global3)
}
await doSomething()
debugLog("==> Exit callee1 Closure")
}
await handle.value
debugLog("==> Exit callee1")
}
await callee1()
}
// In case 2, our task does not start with any live accesses, but it is
// awaited upon after the live access begins. We want to make sure that
// we fail here since we properly restored the callee state.
exclusivityTests.test("case2") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
let callee2 = { @MainActor (_ x: inout Int) -> Void in
debugLog("==> Enter callee2")
debugLog("==> Exit callee2")
}
// We add an inline never here to make sure that we do not eliminate
// the dynamic access after inlining.
@MainActor
@inline(never)
func callee1(_ x: inout Int) async -> () {
debugLog("==> Enter callee1")
// This task is what prevents this example from crashing.
let handle = Task { @MainActor in
debugLog("==> Enter callee1 Closure")
// Second access. Different Task so it is ok.
await callee2(&global1)
debugLog("==> Exit callee1 Closure")
}
await handle.value
useGlobal(&global1) // We should crash here.
debugLog("==> Exit callee1")
}
debugLog("==> Enter Main")
// First access begins here.
await callee1(&global1)
debugLog("==> Exit Main")
}
// In case 5, our task starts with live accesses, but we finish the
// accesses before we return. So we do not crash. The key thing is the
// access lives over a suspension/resume.
exclusivityTests.test("case5") { @MainActor in
let callee2 = { @MainActor (_ x: inout Int) -> Void in
debugLog("==> Enter callee2")
debugLog("==> Exit callee2")
}
// We add an inline never here to make sure that we do not eliminate
// the dynamic access after inlining.
@MainActor
@inline(never)
func callee1(_ x: inout Int) async -> () {
debugLog("==> Enter callee1")
// This task is what prevents this example from crashing.
let handle = Task { @MainActor in
debugLog("==> Enter callee1 Closure")
// Second access. Different Task so it is ok.
await callee2(&global1)
debugLog("==> Exit callee1 Closure")
}
await handle.value
debugLog("==> Exit callee1")
}
@MainActor
@inline(never)
func callCallee1() async {
await callee1(&global1)
}
debugLog("==> Enter Main")
// First access begins here.
await callCallee1()
useGlobal(&global1) // We should not crash here since we cleaned up
// the access in callCallee1 after we returned
// from the await there.
debugLog("==> Exit Main")
}
// In case 6, our task starts with live accesses, and we only finish
// some of the accesses before we return. So we want to validate that by
// running the same code twice, one testing we can access the pointer we
// can fix up and a second that we can not.
exclusivityTests.test("case6") { @MainActor in
let callee2 = { @MainActor (_ x: inout Int) -> Void in
debugLog("==> Enter callee2")
debugLog("==> Exit callee2")
}
// We add an inline never here to make sure that we do not eliminate
// the dynamic access after inlining.
@MainActor
@inline(never)
func callee1(_ x: inout Int) async -> () {
debugLog("==> Enter callee1")
// This task is what prevents this example from crashing.
let handle = Task { @MainActor in
debugLog("==> Enter callee1 Closure")
// Second access. Different Task so it is ok.
await callee2(&global1)
debugLog("==> Exit callee1 Closure")
}
await handle.value
debugLog("==> Exit callee1")
}
@MainActor
@inline(never)
func callCallee1() async {
await callee1(&global1)
}
debugLog("==> Enter Main")
// First access begins here.
await callCallee1()
useGlobal(&global1) // We should not crash here since we cleaned up
// the access in callCallee1 after we returned
// from the await there.
debugLog("==> Exit Main")
}
// These are additional tests that used to be FileChecked but FileCheck
// was too hard to use in a concurrent context.
exclusivityTests.test("case1") { @MainActor in
@inline(never)
@Sendable func callee2(_ x: inout Int, _ y: inout Int, _ z: inout Int) -> Void {
debugLog("==> Enter callee2")
debugLog("==> Exit callee2")
}
// We add an inline never here to make sure that we do not eliminate
// the dynamic access after inlining.
@MainActor
@inline(never)
func callee1() async -> () {
debugLog("==> Enter callee1")
let handle = Task { @MainActor in
debugLog("==> Enter callee1 Closure")
// These accesses end before we await in the task.
do {
callee2(&global1, &global2, &global3)
}
let handle2 = Task { @MainActor in
debugLog("==> Enter handle2!")
debugLog("==> Exit handle2!")
}
await handle2.value
debugLog("==> Exit callee1 Closure")
}
await handle.value
debugLog("==> Exit callee1")
}
debugLog("==> Enter 'testCase1'")
await callee1()
debugLog("==> Exit 'testCase1'")
}
// Case 2: (F, F, T). In case 2, our task does not start with a live access
// and nothing from the outside synchronous context, but does pop with a new
// access.
//
// We use a suspend point and a withExclusiveAccessAsync(to:) to test this.
exclusivityTests.test("case2.filecheck.nocrash") { @MainActor in
debugLog("==> Enter 'testCase2'")
let handle = Task { @MainActor in
debugLog("==> Inner Handle")
await withExclusiveAccessAsync(to: &global1) { @MainActor (x: inout Int) async -> Void in
let innerTaskHandle = Task { @MainActor in
// Different task, shouldn't crash.
withExclusiveAccess(to: &global1) { _ in
debugLog("==> No crash!")
}
debugLog("==> End Inner Task Handle")
}
// This will cause us to serialize the access to global1. If
// we had an access here, we would crash.
await innerTaskHandle.value
debugLog("==> After")
}
// Access is over. We shouldn't crash here.
withExclusiveAccess(to: &global1) { _ in
debugLog("==> No crash!")
}
debugLog("==> Inner Handle: After exclusive access")
}
await handle.value
debugLog("==> After exclusive access")
let handle2 = Task { @MainActor in
debugLog("==> Enter handle2!")
debugLog("==> Exit handle2!")
}
await handle2.value
debugLog("==> Exit 'testCase2'")
}
exclusivityTests.test("case2.filecheck.crash") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
debugLog("==> Enter 'testCase2'")
let handle = Task { @MainActor in
debugLog("==> Inner Handle")
await withExclusiveAccessAsync(to: &global1) { @MainActor (x: inout Int) async -> Void in
let innerTaskHandle = Task { @MainActor in
debugLog("==> End Inner Task Handle")
}
await innerTaskHandle.value
// We will crash here if we properly brought back in the
// access to global1 despite running code on a different
// task.
withExclusiveAccess(to: &global1) { _ in
debugLog("==> Got a crash!")
}
debugLog("==> After")
}
debugLog("==> Inner Handle: After exclusive access")
}
await handle.value
debugLog("==> After exclusive access")
let handle2 = Task { @MainActor in
debugLog("==> Enter handle2!")
debugLog("==> Exit handle2!")
}
await handle2.value
debugLog("==> Exit 'testCase2'")
}
// Case 5: (T,F,F). To test case 5, we use with exclusive access to to
// create an exclusivity scope that goes over a suspension point. We are
// interesting in the case where we return after the suspension point. That
// push/pop is going to have our outer task bring in state and end it.
//
// CHECK-LABEL: ==> Enter 'testCase5'
// CHECK: ==> Task: [[TASK:0x[0-9a-f]+]]
// CHECK: Inserting new access: [[LLNODE:0x[a-z0-9]+]]
// CHECK-NEXT: Tracking!
// CHECK-NEXT: Access. Pointer: [[ACCESS:0x[a-z0-9]+]]
// CHECK: Exiting Thread Local Context. Before Swizzle. Task: [[TASK]]
// CHECK-NEXT: SwiftTaskThreadLocalContext: (FirstAccess,LastAccess): (0x0, 0x0)
// CHECK-NEXT: Access. Pointer: [[ACCESS]]. PC:
// CHECK: Exiting Thread Local Context. After Swizzle. Task: [[TASK]]
// CHECK-NEXT: SwiftTaskThreadLocalContext: (FirstAccess,LastAccess): ([[LLNODE]], [[LLNODE]])
// CHECK-NEXT: No Accesses.
//
// CHECK-NOT: Removing access:
// CHECK: ==> End Inner Task Handle
// CHECK: ==> After
// CHECK: Removing access: [[LLNODE]]
// CHECK: ==> After exclusive access
// CHECK: Exiting Thread Local Context. Before Swizzle. Task: [[TASK]]
// CHECK-NEXT: SwiftTaskThreadLocalContext: (FirstAccess,LastAccess): (0x0, 0x0)
// CHECK-NEXT: No Accesses.
// CHECK: Exiting Thread Local Context. After Swizzle. Task: [[TASK]]
// CHECK-NEXT: SwiftTaskThreadLocalContext: (FirstAccess,LastAccess): (0x0, 0x0)
// CHECK-NEXT: No Accesses.
//
// CHECK: ==> Exit 'testCase5'
exclusivityTests.test("case5.filecheck") { @MainActor in
debugLog("==> Enter 'testCase5'")
let outerHandle = Task { @MainActor in
await withExclusiveAccessAsync(to: &global1) { @MainActor (x: inout Int) async -> Void in
let innerTaskHandle = Task { @MainActor in
debugLog("==> End Inner Task Handle")
}
await innerTaskHandle.value
debugLog("==> After")
}
debugLog("==> After exclusive access")
let handle2 = Task { @MainActor in
debugLog("==> Enter handle2!")
debugLog("==> Exit handle2!")
}
await handle2.value
}
await outerHandle.value
debugLog("==> Exit 'testCase5'")
}
exclusivityTests.test("case5.filecheck.crash") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
debugLog("==> Enter 'testCase5'")
let outerHandle = Task { @MainActor in
await withExclusiveAccessAsync(to: &global1) { @MainActor (x: inout Int) async -> Void in
let innerTaskHandle = Task { @MainActor in
debugLog("==> End Inner Task Handle")
}
await innerTaskHandle.value
debugLog("==> After")
withExclusiveAccess(to: &global1) { _ in
debugLog("==> Crash here")
}
}
debugLog("==> After exclusive access")
let handle2 = Task { @MainActor in
debugLog("==> Enter handle2!")
debugLog("==> Exit handle2!")
}
await handle2.value
}
await outerHandle.value
debugLog("==> Exit 'testCase5'")
}
// Case 6: (T, F, T). In case 6, our task starts with live accesses and is
// popped with live accesses. There are no sync accesses.
//
// We test this by looking at the behavior of the runtime after we
// finish executing handle2. In this case, we first check that things
// just work normally and as a 2nd case perform a conflicting access to
// make sure we crash.
exclusivityTests.test("case6.filecheck") { @MainActor in
let outerHandle = Task { @MainActor in
let callee2 = { @MainActor (_ x: inout Int) -> Void in
debugLog("==> Enter callee2")
debugLog("==> Exit callee2")
}
// We add an inline never here to make sure that we do not eliminate
// the dynamic access after inlining.
@MainActor
@inline(never)
func callee1(_ x: inout Int) async -> () {
debugLog("==> Enter callee1")
// This task is what prevents this example from crashing.
let handle = Task { @MainActor in
debugLog("==> Enter callee1 Closure")
// Second access. Different Task so it is ok.
await withExclusiveAccessAsync(to: &global1) {
await callee2(&$0)
}
debugLog("==> Exit callee1 Closure")
}
await handle.value
debugLog("==> callee1 after first await")
// Force an await here so we can see that we properly swizzle.
let handle2 = Task { @MainActor in
debugLog("==> Enter handle2!")
debugLog("==> Exit handle2!")
}
await handle2.value
debugLog("==> Exit callee1")
}
// First access begins here.
await callee1(&global1)
}
debugLog("==> Enter 'testCase6'")
await outerHandle.value
debugLog("==> Exit 'testCase6'")
}
exclusivityTests.test("case6.filecheck.crash") { @MainActor in
expectCrashLater(withMessage: "Fatal access conflict detected")
let outerHandle = Task { @MainActor in
let callee2 = { @MainActor (_ x: inout Int) -> Void in
debugLog("==> Enter callee2")
debugLog("==> Exit callee2")
}
// We add an inline never here to make sure that we do not eliminate
// the dynamic access after inlining.
@MainActor
@inline(never)
func callee1(_ x: inout Int) async -> () {
debugLog("==> Enter callee1")
// This task is what prevents this example from crashing.
let handle = Task { @MainActor in
debugLog("==> Enter callee1 Closure")
// Second access. Different Task so it is ok.
await withExclusiveAccessAsync(to: &global1) {
await callee2(&$0)
}
debugLog("==> Exit callee1 Closure")
}
await handle.value
debugLog("==> callee1 after first await")
// Force an await here so we can see that we properly swizzle.
let handle2 = Task { @MainActor in
debugLog("==> Enter handle2!")
debugLog("==> Exit handle2!")
}
await handle2.value
// Make sure we brought back in the access to x so we crash
// here.
withExclusiveAccess(to: &global1) { _ in
debugLog("==> Will crash here!")
}
debugLog("==> Exit callee1")
}
// First access begins here.
await callee1(&global1)
}
debugLog("==> Enter 'testCase6'")
await outerHandle.value
debugLog("==> Exit 'testCase6'")
}
await runAllTestsAsync()
}
}
|