Skip to content

[TSan] Do not report benign race on _swiftEmptyArrayStorage #28529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ extension Array: RangeReplaceableCollection {
"newElements.underestimatedCount was an overestimate")
// can't check for overflow as sequences can underestimate

// This check prevents a data race writting to _swiftEmptyArrayStorage
// This check prevents a data race writing to _swiftEmptyArrayStorage
if writtenCount > 0 {
_buffer.count += writtenCount
}
Expand Down
5 changes: 4 additions & 1 deletion stdlib/public/core/ArraySlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,10 @@ extension ArraySlice: RangeReplaceableCollection {
"newElements.underestimatedCount was an overestimate")
// can't check for overflow as sequences can underestimate

_buffer.count += writtenCount
// This check prevents a data race writing to _swiftEmptyArrayStorage
if writtenCount > 0 {
_buffer.count += writtenCount
}

if writtenUpTo == buf.endIndex {
// there may be elements that didn't fit in the existing buffer,
Expand Down
5 changes: 4 additions & 1 deletion stdlib/public/core/ContiguousArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,10 @@ extension ContiguousArray: RangeReplaceableCollection {
"newElements.underestimatedCount was an overestimate")
// can't check for overflow as sequences can underestimate

_buffer.count += writtenCount
// This check prevents a data race writing to _swiftEmptyArrayStorage
if writtenCount > 0 {
_buffer.count += writtenCount
}

if writtenUpTo == buf.endIndex {
// there may be elements that didn't fit in the existing buffer,
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/ContiguousArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ internal struct _UnsafePartiallyInitializedContiguousArrayBuffer<Element> {
p = newResult.firstElementAddress + result.capacity
remainingCapacity = newResult.capacity - result.capacity
if !result.isEmpty {
// This check prevents a data race writting to _swiftEmptyArrayStorage
// This check prevents a data race writing to _swiftEmptyArrayStorage
// Since count is always 0 there, this code does nothing anyway
newResult.firstElementAddress.moveInitialize(
from: result.firstElementAddress, count: result.capacity)
Expand Down
51 changes: 35 additions & 16 deletions test/Sanitizers/tsan-emptyarraystorage.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %target-swiftc_driver %s -target %sanitizers-target-triple -g -sanitize=thread -o %t_tsan-binary
// RUN: %target-codesign %t_tsan-binary
// RUN: %target-run %t_tsan-binary 2>&1 | %FileCheck %s
// RUN: %target-run %t_tsan-binary 2>&1 | %FileCheck %s --implicit-check-not='ThreadSanitizer'
// REQUIRES: executable_test
// REQUIRES: tsan_runtime
// UNSUPPORTED: OS=tvos
Expand All @@ -13,28 +13,47 @@ import Foundation

let sem = DispatchSemaphore(value: 0)

class T1: Thread {
class T: Thread {
let closure: () -> Void
init(closure: @escaping () -> Void) {
self.closure = closure
}
override func main() {
var oneEmptyArray: [[String:String]] = []
oneEmptyArray.append(contentsOf: [])
closure()
sem.signal()
}
}
let t1 = T1()
t1.start()

class T2: Thread {
override func main() {
var aCompletelyUnrelatedOtherEmptyArray: [[Double:Double]] = []
aCompletelyUnrelatedOtherEmptyArray.append(contentsOf: [])
sem.signal()
}
func runOnThread(_ closure: @escaping () -> Void) {
let t = T(closure: closure)
t.start()
}

runOnThread {
var oneEmptyArray: [[String:String]] = []
oneEmptyArray.append(contentsOf: [])
}
runOnThread {
var aCompletelyUnrelatedOtherEmptyArray: [[Double:Double]] = []
aCompletelyUnrelatedOtherEmptyArray.append(contentsOf: [])
}
runOnThread {
var array = Array<Int>()
array.append(contentsOf: [])
}
runOnThread {
var arraySlice = ArraySlice<Int>()
arraySlice.append(contentsOf: [])
}
runOnThread {
var contiguousArray = ContiguousArray<Int>()
contiguousArray.append(contentsOf: [])
}

for _ in 1...5 {
sem.wait()
}
let t2 = T2()
t2.start()

sem.wait()
sem.wait()
print("Done!")

// CHECK: Done!