Skip to content

Commit

Permalink
Fix NPE if thread.interrupt is called but not started. (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al authored Jan 22, 2025
1 parent 588f2d0 commit 0be2be6
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 9 deletions.
15 changes: 8 additions & 7 deletions core/src/main/kotlin/org/pastalab/fray/core/RunContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ class RunContext(val config: Configuration) {
val context = registeredThreads[t.id]!!
mainExiting = true
while (registeredThreads.any {
it.value.state != ThreadState.Completed && it.value != context
it.value.state != ThreadState.Completed &&
it.value.state != ThreadState.Created &&
it.value != context
}) {
try {
context.state = ThreadState.Enabled
Expand Down Expand Up @@ -230,14 +232,17 @@ class RunContext(val config: Configuration) {
executor.shutdown()
}

fun threadStart(t: Thread) {
fun threadCreateDone(t: Thread) {
val originalHanlder = t.uncaughtExceptionHandler
val handler = UncaughtExceptionHandler { t, e ->
onReportError(e)
originalHanlder?.uncaughtException(t, e)
}
t.setUncaughtExceptionHandler(handler)
registeredThreads[t.id] = ThreadContext(t, registeredThreads.size, this)
registeredThreads[t.threadId()] = ThreadContext(t, registeredThreads.size, this)
}

fun threadStart(t: Thread) {
syncManager.createWait(t, 1)
}

Expand All @@ -246,10 +251,6 @@ class RunContext(val config: Configuration) {
syncManager.wait(t)
}

fun monitorEnterDone(lock: Any) {
syncManager.wait(lock)
}

fun threadPark() {
val t = Thread.currentThread()
val context = registeredThreads[t.id]!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class RuntimeDelegate(val context: RunContext) : org.pastalab.fray.runtime.Deleg
entered.set(false)
}

override fun onThreadCreateDone(t: Thread) {
if (checkEntered()) return
context.threadCreateDone(t)
entered.set(false)
}

override fun onThreadStart(t: Thread) {
if (checkEntered()) {
onSkipMethod("thread.start")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.pastalab.fray.rmi.ThreadState

class ThreadContext(val thread: Thread, val index: Int, context: RunContext) {
val localRandomProbe = context.config.randomnessProvider.nextInt()
var state = ThreadState.Paused
var state = ThreadState.Created
var unparkSignaled = false
var interruptSignaled = false
var isExiting = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ class ThreadInstrumenter(cv: ClassVisitor) : ClassVisitorBase(cv, Thread::class.
signature: String?,
exceptions: Array<out String>?
): MethodVisitor {
if (name == "<init>" &&
descriptor ==
"(Ljava/lang/ThreadGroup;Ljava/lang/String;ILjava/lang/Runnable;JLjava/security/AccessControlContext;)V") {
return MethodExitVisitor(
mv,
org.pastalab.fray.runtime.Runtime::onThreadCreateDone,
access,
name,
descriptor,
true,
false,
false)
}
if (name == "start") {
val eMv =
MethodExitVisitor(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.pastalab.fray.test.success.thread;

public class ThreadInterruptionWithoutStart {

public static void main(String[] args) {
Thread t = new Thread();
t.interrupt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.pastalab.fray.test.success.rwlock.ReentrantReadWriteLockDowngradingNoDeadlock;
import org.pastalab.fray.test.success.rwlock.ReentrantReadWriteLockNoDeadlock;
import org.pastalab.fray.test.success.stampedlock.StampedLockTryLockNoDeadlock;
import org.pastalab.fray.test.success.thread.ThreadInterruptionWithoutStart;

import java.util.*;

Expand Down Expand Up @@ -69,8 +70,9 @@ public void testOne() throws Throwable {
new ExecutionInfo(
new LambdaExecutor(() -> {
try {
ReentrantReadWriteLockDowngradingNoDeadlock.main(new String[]{});
ThreadInterruptionWithoutStart.main(new String[]{});
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum class ThreadState {
Running,
Paused,
Completed,
Created,
}

data class ThreadInfo(
Expand Down
3 changes: 3 additions & 0 deletions runtime/src/main/java/org/pastalab/fray/runtime/Delegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

public class Delegate {

public void onThreadCreateDone(Thread t) {
}

public void onThreadStart(Thread t) {
}

Expand Down
4 changes: 4 additions & 0 deletions runtime/src/main/java/org/pastalab/fray/runtime/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
public class Runtime {
public static Delegate DELEGATE = new Delegate();

public static void onThreadCreateDone(Thread t) {
DELEGATE.onThreadCreateDone(t);
}

public static void onThreadStart(Thread t) {
DELEGATE.onThreadStart(t);
}
Expand Down

0 comments on commit 0be2be6

Please sign in to comment.