Skip to content

Commit 8ad012a

Browse files
committed
Mark BufferedChannelIterator.continuation @BeningDataRace to address potential UB and OoTA on K/N
Fixes #3834
1 parent aa6b74e commit 8ad012a

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

kotlinx-coroutines-core/common/src/channels/BufferedChannel.kt

+1
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,7 @@ internal open class BufferedChannel<E>(
15831583
* continuation. The [tryResumeHasNext] and [tryResumeHasNextOnClosedChannel]
15841584
* function resume this continuation when the [hasNext] invocation should complete.
15851585
*/
1586+
@BenignDataRace // See its uses for the explanation of the race
15861587
private var continuation: CancellableContinuationImpl<Boolean>? = null
15871588

15881589
// `hasNext()` is just a special receive operation.

kotlinx-coroutines-core/common/src/internal/Concurrent.common.kt

+12
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ internal expect class ReentrantLock() {
1212
internal expect inline fun <T> ReentrantLock.withLock(action: () -> T): T
1313

1414
internal expect fun <E> identitySet(expectedSize: Int): MutableSet<E>
15+
16+
/**
17+
* Annotation indicating that the marked property is the subject of benign data race.
18+
* LLVM does not support this notion, so on K/N platforms we alias it into `@Volatile` to prevent potential OoTA.
19+
*
20+
* The purpose of this annotation is not to save an extra-volatile on JVM platform, but rather to explicitly emphasize
21+
* that data-race is benign.
22+
*/
23+
@OptionalExpectation
24+
@Target(AnnotationTarget.FIELD)
25+
@OptIn(ExperimentalMultiplatform::class)
26+
internal expect annotation class BenignDataRace()

kotlinx-coroutines-core/native/src/internal/Concurrent.kt

+2
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ internal open class SuppressSupportingThrowableImpl : Throwable() {
2929
}
3030
}
3131

32+
@Suppress("ACTUAL_WITHOUT_EXPECT") // This suppress can be removed in 2.0: KT-59355
33+
internal actual typealias BenignDataRace = kotlin.concurrent.Volatile

0 commit comments

Comments
 (0)