From ec0a924f9372bfc792cab6a114d0566e5d5981bc Mon Sep 17 00:00:00 2001 From: Ao Li Date: Fri, 21 Jun 2024 10:31:09 -0400 Subject: [PATCH] intercept isInterrupted method. --- core/src/main/kotlin/cmu/pasta/fray/core/GlobalContext.kt | 4 ++++ .../src/main/kotlin/cmu/pasta/fray/core/RuntimeDelegate.kt | 7 +++++++ examples/src/main/kotlin/JUnitRunner.kt | 2 +- .../fray/instrumentation/visitors/ThreadInstrumenter.kt | 4 ++++ runtime/src/main/java/cmu/pasta/fray/runtime/Delegate.java | 3 +++ runtime/src/main/java/cmu/pasta/fray/runtime/Runtime.java | 3 +++ 6 files changed, 22 insertions(+), 1 deletion(-) diff --git a/core/src/main/kotlin/cmu/pasta/fray/core/GlobalContext.kt b/core/src/main/kotlin/cmu/pasta/fray/core/GlobalContext.kt index fd018d98..2bbdc4d3 100644 --- a/core/src/main/kotlin/cmu/pasta/fray/core/GlobalContext.kt +++ b/core/src/main/kotlin/cmu/pasta/fray/core/GlobalContext.kt @@ -234,6 +234,10 @@ object GlobalContext { registeredThreads[t.id]?.block() } + fun threadIsInterrupted(t: Thread, result: Boolean): Boolean { + return result || registeredThreads[t.id]!!.interruptSignaled + } + fun threadGetState(t: Thread, state: Thread.State): Thread.State { if (state == Thread.State.WAITING || state == Thread.State.TIMED_WAITING || diff --git a/core/src/main/kotlin/cmu/pasta/fray/core/RuntimeDelegate.kt b/core/src/main/kotlin/cmu/pasta/fray/core/RuntimeDelegate.kt index c9a24fb7..57d8f7b5 100644 --- a/core/src/main/kotlin/cmu/pasta/fray/core/RuntimeDelegate.kt +++ b/core/src/main/kotlin/cmu/pasta/fray/core/RuntimeDelegate.kt @@ -683,4 +683,11 @@ class RuntimeDelegate : Delegate() { return true } } + + override fun onThreadIsInterrupted(result: Boolean, t: Thread): Boolean { + if (checkEntered()) return result + val isInterrupted = GlobalContext.threadIsInterrupted(t, result) + entered.set(false) + return isInterrupted + } } diff --git a/examples/src/main/kotlin/JUnitRunner.kt b/examples/src/main/kotlin/JUnitRunner.kt index 501c9f6f..dd993c1b 100644 --- a/examples/src/main/kotlin/JUnitRunner.kt +++ b/examples/src/main/kotlin/JUnitRunner.kt @@ -13,7 +13,7 @@ fun main(args: Array) { val result = JUnitCore().run(request) if (!result.wasSuccessful()) { - val failureReport = result.failures.joinToString { "\n =========== \n" } + val failureReport = result.failures.joinToString("\n =========== \n") throw RuntimeException(failureReport) } } diff --git a/instrumentation/src/main/kotlin/cmu/pasta/fray/instrumentation/visitors/ThreadInstrumenter.kt b/instrumentation/src/main/kotlin/cmu/pasta/fray/instrumentation/visitors/ThreadInstrumenter.kt index b9a05520..5cf41cb2 100644 --- a/instrumentation/src/main/kotlin/cmu/pasta/fray/instrumentation/visitors/ThreadInstrumenter.kt +++ b/instrumentation/src/main/kotlin/cmu/pasta/fray/instrumentation/visitors/ThreadInstrumenter.kt @@ -30,6 +30,10 @@ class ThreadInstrumenter(cv: ClassVisitor) : ClassVisitorBase(cv, Thread::class. return MethodExitVisitor( mv, Runtime::onThreadGetAndClearInterrupt, access, name, descriptor, true, false, false) } + if (name == "isInterrupted") { + return MethodExitVisitor( + mv, Runtime::onThreadIsInterrupted, access, name, descriptor, true, false, false) + } if (name == "clearInterrupt") { return MethodExitVisitor( mv, Runtime::onThreadClearInterrupt, access, name, descriptor, true, false, false) diff --git a/runtime/src/main/java/cmu/pasta/fray/runtime/Delegate.java b/runtime/src/main/java/cmu/pasta/fray/runtime/Delegate.java index 48c835cb..d1d085fc 100644 --- a/runtime/src/main/java/cmu/pasta/fray/runtime/Delegate.java +++ b/runtime/src/main/java/cmu/pasta/fray/runtime/Delegate.java @@ -242,5 +242,8 @@ public void onConditionAwaitUninterruptibly(Condition object) { public void onConditionAwaitUninterruptiblyDone(Condition object) { } + public boolean onThreadIsInterrupted(boolean result, Thread t) { + return result; + } } diff --git a/runtime/src/main/java/cmu/pasta/fray/runtime/Runtime.java b/runtime/src/main/java/cmu/pasta/fray/runtime/Runtime.java index c73c9b81..f1c1d004 100644 --- a/runtime/src/main/java/cmu/pasta/fray/runtime/Runtime.java +++ b/runtime/src/main/java/cmu/pasta/fray/runtime/Runtime.java @@ -330,4 +330,7 @@ public static void onConditionAwaitUninterruptiblyDone(Condition object) { DELEGATE.onConditionAwaitUninterruptiblyDone(object); } + public static boolean onThreadIsInterrupted(boolean result, Thread t) { + return DELEGATE.onThreadIsInterrupted(result, t); + } }