Skip to content

Commit 558a0cc

Browse files
ylnJulian Lettner
andauthored
Add tsan_release edge on task creation (#38074)
* Synchronize both versions of actor_counters.swift test * Synchronize on Job address Make sure to synchronize on Job address (AsyncTasks are Jobs, but not all Jobs are AsyncTasks). * Add fprintf debug output for TSan acquire/release * Add tsan_release edge on task creation without this, we are getting false data races between when a task is created and immediately scheduled on a different thread. False positive for `Sanitizers/tsan/actor_counters.swift` test: ``` WARNING: ThreadSanitizer: data race (pid=81452) Read of size 8 at 0x7b2000000560 by thread T5: #0 Counter.next() <null>:2 (a.out:x86_64+0x1000047f8) #1 (1) suspend resume partial function for worker(identity:counters:numIterations:) <null>:2 (a.out:x86_64+0x100005961) #2 swift::runJobInEstablishedExecutorContext(swift::Job*) <null>:2 (libswift_Concurrency.dylib:x86_64+0x280ef) Previous write of size 8 at 0x7b2000000560 by main thread: #0 Counter.init(maxCount:) <null>:2 (a.out:x86_64+0x1000046af) #1 Counter.__allocating_init(maxCount:) <null>:2 (a.out:x86_64+0x100004619) #2 runTest(numCounters:numWorkers:numIterations:) <null>:2 (a.out:x86_64+0x100006d2e) #3 swift::runJobInEstablishedExecutorContext(swift::Job*) <null>:2 (libswift_Concurrency.dylib:x86_64+0x280ef) #4 main <null>:2 (a.out:x86_64+0x10000a175) ``` New edge with this change: ``` [4357150208] allocate task 0x7b3800000000, parent = 0x0 [4357150208] creating task 0x7b3800000000 with parent 0x0 [4357150208] tsan_release on 0x7b3800000000 <<< new release edge [139088221442048] tsan_acquire on 0x7b3800000000 [139088221442048] trying to switch from executor 0x0 to 0x7ff85e2d9a00 [139088221442048] switch failed, task 0x7b3800000000 enqueued on executor 0x7ff85e2d9a00 [139088221442048] enqueue job 0x7b3800000000 on executor 0x7ff85e2d9a00 [139088221442048] tsan_release on 0x7b3800000000 [139088221442048] tsan_release on 0x7b3800000000 [4357150208] tsan_acquire on 0x7b3800000000 counters: 1, workers: 1, iterations: 1 [4357150208] allocate task 0x7b3c00000000, parent = 0x0 [4357150208] creating task 0x7b3c00000000 with parent 0x0 [4357150208] tsan_release on 0x7b3c00000000 <<< new release edge [139088221442048] tsan_acquire on 0x7b3c00000000 [4357150208] task 0x7b3800000000 waiting on task 0x7b3c00000000, going to sleep [4357150208] tsan_release on 0x7b3800000000 [4357150208] tsan_release on 0x7b3800000000 [139088221442048] getting current executor 0x0 [139088221442048] tsan_release on 0x7b3c00000000 ... ``` rdar://78932849 * Add static_cast<Job *>() * Move TSan release edge to swift_task_enqueueGlobal() Move the TSan release edge from `swift_task_create_commonImpl()` to `swift_task_enqueueGlobalImpl()`. Task creation itself is not an event that needs synchronization, but rather that task creation "happens before" execution of that task on another thread. This edge is usually added when the task is scheduled via `swift_task_enqueue()` (which then usually calls `swift_task_enqueueGlobal()`). However, not all task scheduling goes through the `swift_task_enqueue()` funnel as some places call the more specific `swift_task_enqueueGlobal()` directly. So let's annotate this function (duplicate edges aren't harmful) to ensure we cover all schedule events, including newly-created tasks (our original problem here). rdar://78932849 Co-authored-by: Julian Lettner <[email protected]>
1 parent 6920a47 commit 558a0cc

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

stdlib/public/Concurrency/GlobalExecutor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ static void swift_task_enqueueGlobalImpl(Job *job) {
336336
}
337337

338338
void swift::swift_task_enqueueGlobal(Job *job) {
339+
_swift_tsan_release(job);
340+
339341
if (swift_task_enqueueGlobal_hook)
340342
swift_task_enqueueGlobal_hook(job, swift_task_enqueueGlobalImpl);
341343
else

stdlib/public/Concurrency/Task.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ static void resumeTaskAfterContinuation(AsyncTask *task,
956956

957957
// Make sure TSan knows that the resume call happens-before the task
958958
// restarting.
959-
_swift_tsan_release(task);
959+
_swift_tsan_release(static_cast<Job *>(task));
960960

961961
// The status should be either Pending or Awaited. If it's Awaited,
962962
// which is probably the most likely option, then we should immediately

stdlib/public/Concurrency/ThreadSanitizer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ TSanFunc *tsan_acquire, *tsan_release;
3131
void swift::_swift_tsan_acquire(void *addr) {
3232
if (tsan_acquire) {
3333
tsan_acquire(addr);
34+
#if SWIFT_TASK_PRINTF_DEBUG
35+
fprintf(stderr, "[%lu] tsan_acquire on %p\n", _swift_get_thread_id(), addr);
36+
#endif
3437
}
3538
}
3639

3740
void swift::_swift_tsan_release(void *addr) {
3841
if (tsan_release) {
3942
tsan_release(addr);
43+
#if SWIFT_TASK_PRINTF_DEBUG
44+
fprintf(stderr, "[%lu] tsan_release on %p\n", _swift_get_thread_id(), addr);
45+
#endif
4046
}
4147
}
4248

test/Concurrency/Runtime/actor_counters.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ actor Counter {
2828
value = value + 1
2929
return current
3030
}
31+
32+
deinit {
33+
for i in 0..<value {
34+
assert(scratchBuffer[i] == 1)
35+
}
36+
}
3137
}
3238

3339

test/Sanitizers/tsan/actor_counters.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency %import-libdispatch -parse-as-library -sanitize=thread) | grep -v "^Worker"
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency %import-libdispatch -parse-as-library -sanitize=thread)
22

33
// REQUIRES: executable_test
44
// REQUIRES: concurrency
@@ -8,12 +8,6 @@
88
// UNSUPPORTED: linux
99
// UNSUPPORTED: windows
1010

11-
#if canImport(Darwin)
12-
import Darwin
13-
#elseif canImport(Glibc)
14-
import Glibc
15-
#endif
16-
1711
@available(SwiftStdlib 5.5, *)
1812
actor Counter {
1913
private var value = 0
@@ -66,7 +60,7 @@ func runTest(numCounters: Int, numWorkers: Int, numIterations: Int) async {
6660
for i in 0..<numWorkers {
6761
workers.append(
6862
detach { [counters] in
69-
usleep(UInt32.random(in: 0..<100) * 1000)
63+
await Task.sleep(UInt64.random(in: 0..<100) * 1_000_000)
7064
await worker(identity: i, counters: counters, numIterations: numIterations)
7165
}
7266
)

0 commit comments

Comments
 (0)