Skip to content

Commit

Permalink
Make sure SFuzz stops gracefully when failure occurs.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed Mar 28, 2024
1 parent c722175 commit 059f6c9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 79 deletions.
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
kotlin("jvm")
kotlin("plugin.serialization") version "1.9.22"
id("com.github.johnrengelman.shadow") version "8.1.1"
id("maven-publish")
}

repositories {
Expand Down
47 changes: 41 additions & 6 deletions core/src/main/kotlin/cmu/pasta/sfuzz/core/GlobalContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,21 @@ object GlobalContext {
val t = Thread.currentThread()
val context = registeredThreads[t.id]!!
context.state = ThreadState.Completed
scheduleNextOperation(true)
while (registeredThreads.any { it.value.state != ThreadState.Completed }) {
try {
scheduleNextOperation(true)
} catch (e: TargetTerminateException) {
// If deadlock detected let's try to unblock one thread and continue.
if (e.status == -1) {
for (thread in registeredThreads.values) {
if (thread.state == ThreadState.Paused) {
thread.state = ThreadState.Enabled
break
}
}
}
}
}
}

fun start() {
Expand Down Expand Up @@ -172,7 +186,9 @@ object GlobalContext {
}

fun threadCompleted(t: Thread) {
monitorEnter(t)
if (!errorFound) {
monitorEnter(t)
}
objectNotifyAll(t)
registeredThreads[t.id]?.state = ThreadState.Completed
// We do not want to send notify all because
Expand All @@ -191,9 +207,24 @@ object GlobalContext {
Thread.yield()
}
lockUnlockDone(t)
unlockImpl(t, t.id, false, false, true)
syncManager.synchronizationPoints.remove(System.identityHashCode(t))
scheduleNextOperation(false)
if (!errorFound) {
unlockImpl(t, t.id, false, false, true)
syncManager.synchronizationPoints.remove(System.identityHashCode(t))
}
try {
scheduleNextOperation(false)
} catch (e: TargetTerminateException) {
// If deadlock detected let's try to unblock one thread and continue.
if (e.status == -1) {
for (thread in registeredThreads.values) {
if (thread.state == ThreadState.Paused) {
thread.state = ThreadState.Running
thread.unblock()
break
}
}
}
}
}
}

Expand Down Expand Up @@ -375,7 +406,7 @@ object GlobalContext {
// synchronized(lock) {
// lock.unlock();
// }
while (!lockManager.lock(lock, t, true, false)) {
while (!lockManager.lock(lock, t, true, false) && !errorFound) {
registeredThreads[t]?.state = ThreadState.Paused

// We want to block current thread because we do
Expand All @@ -384,6 +415,10 @@ object GlobalContext {
// threads hold the same lock.
scheduleNextOperation(true)
}

if (errorFound) {
throw TargetTerminateException(-2)
}
}

fun monitorEnter(lock: Any) {
Expand Down
14 changes: 1 addition & 13 deletions core/src/main/kotlin/cmu/pasta/sfuzz/core/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmu.pasta.sfuzz.core
import cmu.pasta.sfuzz.core.runtime.AnalysisResult
import cmu.pasta.sfuzz.runtime.Delegate
import cmu.pasta.sfuzz.runtime.Runtime
import cmu.pasta.sfuzz.runtime.TargetTerminateException
import java.lang.reflect.InvocationTargetException
import java.nio.file.Paths
import kotlin.io.path.ExperimentalPathApi
Expand Down Expand Up @@ -39,19 +38,8 @@ fun run(config: Configuration) {
}
Runtime.onMainExit()
} catch (e: InvocationTargetException) {
val cause = e.cause
if (cause is TargetTerminateException) {
Runtime.onMainExit()
if (cause.status != 0) {
println("target terminated: ${cause.status}")
println("iter: $i")
}
} else {
println("target terminated: ${cause}")
println("iter: $i")
}
GlobalContext.errorFound = true
// GlobalContext.checkErrorAndExit()
Runtime.onMainExit()
}
Runtime.DELEGATE = Delegate()
GlobalContext.done(AnalysisResult.COMPLETE)
Expand Down
16 changes: 11 additions & 5 deletions core/src/main/kotlin/cmu/pasta/sfuzz/core/RuntimeDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ class RuntimeDelegate : Delegate() {
skipFunctionEntered.set(1 + skipFunctionEntered.get())
return
}
GlobalContext.lockLock(l)
entered.set(false)
skipFunctionEntered.set(skipFunctionEntered.get() + 1)
try {
GlobalContext.lockLock(l)
} finally {
entered.set(false)
skipFunctionEntered.set(skipFunctionEntered.get() + 1)
}
}

override fun onLockLockDone(l: ReentrantLock?) {
Expand Down Expand Up @@ -132,8 +135,11 @@ class RuntimeDelegate : Delegate() {

override fun onMonitorEnter(o: Any) {
if (checkEntered()) return
GlobalContext.monitorEnter(o)
entered.set(false)
try {
GlobalContext.monitorEnter(o)
} finally {
entered.set(false)
}
}

override fun onMonitorExit(o: Any) {
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/kotlin/cmu/pasta/sfuzz/core/ThreadContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class ThreadContext(val thread: Thread, val index: Int) {
}

fun unblock() {
sync.unblock()
if (sync.isBlocked) {
sync.unblock()
}
}

fun checkInterrupt() {
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/kotlin/cmu/pasta/sfuzz/core/concurrency/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ package cmu.pasta.sfuzz.core.concurrency
// for signals.
class Sync(val goal: Int) : Any() {
private var count = 0
var isBlocked = false

@Synchronized
fun block() {
if (count == goal) {
count = 0
return
}
isBlocked = true
// We don't need synchronized here because
// it is already inside a synchronized method
while (count < goal) {
Expand All @@ -21,16 +23,14 @@ class Sync(val goal: Int) : Any() {
// to unblock this operation.
}
}
isBlocked = false
// At this point no concurrency.
count = 0
}

@Synchronized
fun unblock() {
count += 1
if (count > goal) {
println("??")
}
assert(count <= goal)
if (count == goal) {
(this as Object).notify()
Expand Down
75 changes: 24 additions & 51 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,13 @@ tasks.test {
useJUnitPlatform()
}

tasks.register<JavaExec>("runExample") {
var jdk = project(":jdk")
classpath = sourceSets["main"].runtimeClasspath
mainClass.set("example.Main")
executable("${jdk.layout.buildDirectory.get().asFile}/java-inst/bin/java")
}

tasks.compileJava {
options.compilerArgs.addAll(listOf("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED"))
}

tasks.register<JavaExec>("run") {
val agentPath: String by rootProject.extra
val jdk = project(":jdk")
val instrumentation = project(":instrumentation")
classpath = sourceSets["main"].runtimeClasspath
executable("${jdk.layout.buildDirectory.get().asFile}/java-inst/bin/java")
mainClass.set("cmu.pasta.sfuzz.core.MainKt")
args = listOf("example.Main", "-o", "${layout.buildDirectory.get().asFile}/report", "--scheduler", "fifo")
jvmArgs("-agentpath:$agentPath")
jvmArgs("-javaagent:${instrumentation.layout.buildDirectory.get().asFile}/libs/${instrumentation.name}-${instrumentation.version}-all.jar")
doFirst {
// Printing the full command
println("Executing command: ${executable} ${jvmArgs!!.joinToString(" ")} -cp ${classpath.asPath} ${mainClass.get()} ${args!!.joinToString(" ")}")
}
}

tasks.register<JavaExec>("replay") {
tasks.withType<JavaExec> {
val agentPath: String by rootProject.extra
val jdk = project(":jdk")
val cp = properties["classpath"] as String
val main = properties["mainClass"] as String
val mainName = main.split(".").last()
val instrumentation = project(":instrumentation")
classpath = sourceSets["main"].runtimeClasspath + files(cp)
classpath = sourceSets["main"].runtimeClasspath
executable("${jdk.layout.buildDirectory.get().asFile}/java-inst/bin/java")
mainClass.set("cmu.pasta.sfuzz.core.MainKt")
args = listOf(main, "main", "--path", "//Users/aoli/repos/sfuzz-benchmark/schedules/$mainName.csv", "--scheduler", "replay", "--logger", "csv")
mainClass = "cmu.pasta.sfuzz.core.MainKt"
jvmArgs("-agentpath:$agentPath")
jvmArgs("-javaagent:${instrumentation.layout.buildDirectory.get().asFile}/libs/${instrumentation.name}-${instrumentation.version}-all.jar")
jvmArgs("-ea")
Expand All @@ -63,23 +32,27 @@ tasks.register<JavaExec>("replay") {
}
}

tasks.register<JavaExec>("runBench") {
val agentPath: String by rootProject.extra
val jdk = project(":jdk")
val cp = properties["classpath"] as String
val main = properties["mainClass"] as String
val instrumentation = project(":instrumentation")
classpath = sourceSets["main"].runtimeClasspath + files(cp)
executable("${jdk.layout.buildDirectory.get().asFile}/java-inst/bin/java")
mainClass.set("cmu.pasta.sfuzz.core.MainKt")
args = listOf(main, "main", "-o", "${layout.buildDirectory.get().asFile}/report", "--scheduler", "random", "--logger", "csv", "--iter", "1000")
jvmArgs("-agentpath:$agentPath")
jvmArgs("-javaagent:${instrumentation.layout.buildDirectory.get().asFile}/libs/${instrumentation.name}-${instrumentation.version}-all.jar")
jvmArgs("-ea")
doFirst {
// Printing the full command
println("Executing command: ${executable} ${jvmArgs!!.joinToString(" ")} -cp ${classpath.asPath} ${mainClass.get()} ${args!!.joinToString(" ")}")
}

tasks.compileJava {
options.compilerArgs.addAll(listOf("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED"))
}

tasks.register<JavaExec>("runExample") {
args = listOf("example.Main", "-o", "${layout.buildDirectory.get().asFile}/report", "--scheduler", "fifo")
}

tasks.register<JavaExec>("replay") {
val cp = properties["classpath"] as String? ?: ""
val main = properties["mainClass"] as String? ?: ""
classpath = classpath + files(cp)
args = listOf("cmu.pasta.sfuzz.benchmark.sctbench.cs.$main", "main", "--path", "//Users/aoli/repos/sfuzz-benchmark/schedules/$main.csv", "--scheduler", "replay", "--logger", "csv")
}

tasks.register<JavaExec>("runSCT") {
val cp = properties["classpath"] as String? ?: ""
val main = properties["mainClass"] as String? ?: ""
classpath = classpath + files(cp)
args = listOf("cmu.pasta.sfuzz.benchmark.sctbench.cs.$main", "main", "-o", "${layout.buildDirectory.get().asFile}/report", "--scheduler", "random", "--logger", "csv", "--iter", "1000")
}

tasks.register<JavaExec>("runArithmeticProgBad") {
Expand Down

0 comments on commit 059f6c9

Please sign in to comment.