diff --git a/bootstrap/src/sun/nio/ch/lincheck/Injections.java b/bootstrap/src/sun/nio/ch/lincheck/Injections.java index 3683d705b..b4fddc0f1 100644 --- a/bootstrap/src/sun/nio/ch/lincheck/Injections.java +++ b/bootstrap/src/sun/nio/ch/lincheck/Injections.java @@ -168,7 +168,17 @@ public static Random deterministicRandom() { * Called from the instrumented code to check whether the object is a [Random] instance. */ public static boolean isRandom(Object any) { - return any instanceof Random; + // Is this a Java random? + if (any instanceof Random) return true; + // Is this a Kotlin random? + try { + Class kotlinRandomClass = any.getClass().getClassLoader().loadClass("kotlin.random.Random"); + return kotlinRandomClass.isInstance(any); + } catch (ClassNotFoundException e) { + // Kotlin is not used in the user project. + } + // No, this is not a random instance. + return false; } /** diff --git a/src/jvm/test/org/jetbrains/kotlinx/lincheck_test/representation/KotlinRandomRepresentationTest.kt b/src/jvm/test/org/jetbrains/kotlinx/lincheck_test/representation/KotlinRandomRepresentationTest.kt new file mode 100644 index 000000000..fa09b1569 --- /dev/null +++ b/src/jvm/test/org/jetbrains/kotlinx/lincheck_test/representation/KotlinRandomRepresentationTest.kt @@ -0,0 +1,11 @@ +package org.jetbrains.kotlinx.lincheck_test.representation + +import kotlin.random.* + +class KotlinRandomRepresentationTest : BaseFailingTest("kotlin_random_representation.txt") { + private var a = 0 + + override fun actionsForTrace() { + a = Random.nextInt() + } +} \ No newline at end of file diff --git a/src/jvm/test/resources/expected_logs/kotlin_random_representation.txt b/src/jvm/test/resources/expected_logs/kotlin_random_representation.txt new file mode 100644 index 000000000..8cb5803bb --- /dev/null +++ b/src/jvm/test/resources/expected_logs/kotlin_random_representation.txt @@ -0,0 +1,38 @@ += Invalid execution results = +| ------------------------------- | +| Thread 1 | Thread 2 | +| ------------------------------- | +| increment(): 0 | increment(): 0 | +| ------------------------------- | + +The following interleaving leads to the error: +| ---------------------------------------------------------------------------------------- | +| Thread 1 | Thread 2 | +| ---------------------------------------------------------------------------------------- | +| | increment(): 0 | +| | counter.READ: 0 at BaseFailingTest.increment(BaseFailingTest.kt:30) | +| | switch | +| increment(): 0 | | +| | counter.WRITE(1) at BaseFailingTest.increment(BaseFailingTest.kt:30) | +| | actionsForTrace() at BaseFailingTest.increment(BaseFailingTest.kt:31) | +| | result: 0 | +| ---------------------------------------------------------------------------------------- | + +Detailed trace: +| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Thread 1 | Thread 2 | +| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| | increment(): 0 | +| | counter.READ: 0 at BaseFailingTest.increment(BaseFailingTest.kt:30) | +| | switch | +| increment(): 0 | | +| counter.READ: 0 at BaseFailingTest.increment(BaseFailingTest.kt:30) | | +| counter.WRITE(1) at BaseFailingTest.increment(BaseFailingTest.kt:30) | | +| actionsForTrace() at BaseFailingTest.increment(BaseFailingTest.kt:31) | | +| a.WRITE(-1147404850) at KotlinRandomRepresentationTest.actionsForTrace(KotlinRandomRepresentationTest.kt:9) | | +| result: 0 | | +| | counter.WRITE(1) at BaseFailingTest.increment(BaseFailingTest.kt:30) | +| | actionsForTrace() at BaseFailingTest.increment(BaseFailingTest.kt:31) | +| | a.WRITE(-1137016629) at KotlinRandomRepresentationTest.actionsForTrace(KotlinRandomRepresentationTest.kt:9) | +| | result: 0 | +| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |