Skip to content

Commit

Permalink
Process kotlin.random.Random similarly to java.util.Random (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkoval authored Sep 17, 2024
1 parent dcfdfa7 commit 57942a7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
12 changes: 11 additions & 1 deletion bootstrap/src/sun/nio/ch/lincheck/Injections.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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 |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

0 comments on commit 57942a7

Please sign in to comment.