Skip to content

Commit

Permalink
update.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed Jul 15, 2024
1 parent 929dabf commit c8cef43
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 8 deletions.
8 changes: 7 additions & 1 deletion core/src/main/kotlin/cmu/pasta/fray/core/GlobalContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import java.util.concurrent.locks.LockSupport
import java.util.concurrent.locks.ReentrantLock
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock
import kotlin.system.exitProcess

// TODO(aoli): make this a class maybe?
@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN")
Expand Down Expand Up @@ -91,6 +92,10 @@ object GlobalContext {
for (logger in loggers) {
logger.applicationEvent(sw.toString())
}
if (!config!!.exploreMode && config!!.exitWhenBugFound) {
loggers.forEach { it.executionDone(bugFound) }
exitProcess(-1)
}
}
}

Expand Down Expand Up @@ -877,7 +882,8 @@ object GlobalContext {
index,
nextThread.index,
enabledOperations.size,
enabledOperations.map { it.index }))
enabledOperations.map { it.index },
nextThread.pendingOperation.toString()))
}
}
nextThread.state = ThreadState.Running
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/kotlin/cmu/pasta/fray/core/RuntimeDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,16 @@ class RuntimeDelegate : Delegate() {
override fun onNanoTime(): Long {
return GlobalContext.nanoTime()
}

override fun onThreadHashCode(t: Any): Int {
if (t is Thread) {
val context = GlobalContext.registeredThreads[t.id]
if (context != null) {
return 0
} else {
return t.hashCode()
}
}
return t.hashCode()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class MainCommand : CliktCommand() {
"pct" to PCT())
val noFray by option("--no-fray").flag()
val exploreMode by option("--explore").flag()
val exitWhenBugFound by option("--exit-on-bug").flag()
val runConfig by
option()
.groupChoice(
Expand All @@ -176,6 +177,7 @@ class MainCommand : CliktCommand() {
fullSchedule,
logger!!.getLogger(report, fullSchedule),
exploreMode,
exitWhenBugFound,
noFray)
}
}
Expand All @@ -188,5 +190,6 @@ data class Configuration(
val fullSchedule: Boolean,
val logger: LoggerBase,
val exploreMode: Boolean,
val exitWhenBugFound: Boolean,
val noFray: Boolean,
) {}
3 changes: 2 additions & 1 deletion core/src/main/kotlin/cmu/pasta/fray/core/scheduler/Choice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ data class Choice(
val selected: Int,
val threadId: Int,
val enabled: Int,
val enabledIds: List<Int>
val enabledIds: List<Int>,
val operation: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,29 @@ class ReplayScheduler(val schedule: Schedule) : Scheduler {
if (threads.size == 1 && !schedule.fullSchedule) {
return threads[0]
}
if (index == 1710) {
println("???")
}

if (index >= schedule.choices.size) {
return threads[0]
// throw SchedulerInternalException("Require more scheduling choices for replay
// scheduler")
}
val choice = schedule.choices[index]
assert(threads.map { it.index }.toList() == choice.enabledIds)
assert(choice.enabled == threads.size)
if (threads.map { it.index }.toList() != choice.enabledIds) {
// println("?")
}
// assert(threads.map { it.index }.toList() == choice.enabledIds)
// assert(choice.enabled == threads.size)

val selected = threads[choice.selected]
assert(choice.threadId == selected.index)
val selected = threads[choice.selected % threads.size]
// assert(choice.threadId == selected.index)
index += 1
if (selected.pendingOperation.toString().split("@")[0] != choice.operation.split("@")[0]) {
// println("?")
}
// assert(selected.pendingOperation.toString() == choice.operation)
return selected
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ data class Schedule(val choices: MutableList<Choice>, val fullSchedule: Boolean)
}
if (line.strip().isEmpty()) continue
val parts = line.split(",")
val enabledIds = parts.subList(3, parts.size).map { it.toInt() }
choices.add(Choice(parts[0].toInt(), parts[1].toInt(), parts[2].toInt(), enabledIds))
val enabledIds = parts.subList(3, parts.size - 1).map { it.toInt() }
choices.add(
Choice(parts[0].toInt(), parts[1].toInt(), parts[2].toInt(), enabledIds, parts.last()))
}
return Schedule(choices, fullSchedule)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class ApplicationCodeTransformer : ClassFileTransformer {
cv = ClassConstructorInstrumenter(cv)
cv = SleepInstrumenter(cv)
cv = TimeInstrumenter(cv)
cv = ThreadHashCodeInstrumenter(cv)
val classVersionInstrumenter = ClassVersionInstrumenter(cv)
cv = ArrayOperationInstrumenter(classVersionInstrumenter)
classReader.accept(cv, ClassReader.EXPAND_FRAMES)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmu.pasta.fray.instrumentation.visitors

import cmu.pasta.fray.runtime.Runtime
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes.ASM9
import org.objectweb.asm.Type
import org.objectweb.asm.commons.AdviceAdapter

class ThreadHashCodeInstrumenter(cv: ClassVisitor) : ClassVisitor(ASM9, cv) {
override fun visitMethod(
access: Int,
name: String,
descriptor: String,
signature: String?,
exceptions: Array<out String>?
): MethodVisitor {
return object :
AdviceAdapter(
ASM9,
super.visitMethod(access, name, descriptor, signature, exceptions),
access,
name,
descriptor) {
override fun visitMethodInsn(
opcodeAndSource: Int,
owner: String,
name: String,
descriptor: String,
isInterface: Boolean
) {
if (name == "hashCode" && owner == "java/lang/Object") {
invokeStatic(
Type.getObjectType(Runtime::class.java.name.replace(".", "/")),
Utils.kFunctionToASMMethod(Runtime::onThreadHashCode),
)
} else {
super.visitMethodInsn(opcodeAndSource, owner, name, descriptor, isInterface)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public String runTest(Function0<Unit> exec, Scheduler scheduler, int iter) {
true,
new JsonLogger("/tmp/report", false),
false,
false,
false
);
TestRunner runner = new TestRunner(config);
Expand Down
4 changes: 4 additions & 0 deletions runtime/src/main/java/cmu/pasta/fray/runtime/Delegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,9 @@ public boolean onThreadIsInterrupted(boolean result, Thread t) {
public long onNanoTime() {
return System.nanoTime();
}

public int onThreadHashCode(Object t) {
return t.hashCode();
}
}

4 changes: 4 additions & 0 deletions runtime/src/main/java/cmu/pasta/fray/runtime/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,8 @@ public static boolean onLockHasQueuedThread(boolean result, Lock l, Thread t) {
public static long onNanoTime() {
return DELEGATE.onNanoTime();
}

public static int onThreadHashCode(Object t) {
return DELEGATE.onThreadHashCode(t);
}
}

0 comments on commit c8cef43

Please sign in to comment.