Skip to content

Commit

Permalink
refactor testcase and change the location of fix codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jia-wei-tang committed Jul 30, 2024
1 parent 723b1ec commit 1b0de48
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
11 changes: 5 additions & 6 deletions src/hotspot/share/prims/jvmtiExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,8 @@ class JvmtiClassFileLoadHookPoster : public StackObj {
_cached_class_file_ptr = cache_ptr;
_has_been_modified = false;

if (_thread->is_in_any_VTMS_transition()) {
return; // no events should be posted if thread is in any VTMS transition
}
assert(!_thread->is_in_any_VTMS_transition(), "CFLH events are not allowed in any VTMS transition");

_state = JvmtiExport::get_jvmti_thread_state(_thread);
if (_state != nullptr) {
_class_being_redefined = _state->get_class_being_redefined();
Expand Down Expand Up @@ -965,9 +964,6 @@ class JvmtiClassFileLoadHookPoster : public StackObj {
}

void post() {
if (_thread->is_in_any_VTMS_transition()) {
return; // no events should be posted if thread is in any VTMS transition
}
post_all_envs();
copy_modified_data();
}
Expand Down Expand Up @@ -1097,6 +1093,9 @@ bool JvmtiExport::post_class_file_load_hook(Symbol* h_name,
if (JavaThread::current()->is_in_tmp_VTMS_transition()) {
return false; // skip CFLH events in tmp VTMS transition
}
if (JavaThread::current()->is_in_any_VTMS_transition()) {
return false; // no events should be posted if thread is in any VTMS transition
}

JvmtiClassFileLoadHookPoster poster(h_name, class_loader,
h_protection_domain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@

/*
* @test
* @summary javaagent+tracePinnedThreads will cause jvm crash/ run into deadlock when the virtual thread is pinned
* @summary javaagent + tracePinnedThreads will cause jvm crash/ run into deadlock when the virtual thread is pinned
* @library /test/lib
* @requires os.family == "linux"
* @requires os.arch != "riscv64"
* @modules java.base/java.lang:+open
* @build TestPinCaseWithTrace
* @build TestPinCaseWithCFLH
* @run driver jdk.test.lib.util.JavaAgentBuilder
* TestPinCaseWithTrace TestPinCaseWithTrace.jar
* @run main/othervm/timeout=100 -Djdk.virtualThreadScheduler.maxPoolSize=1 -Djdk.tracePinnedThreads=full -javaagent:TestPinCaseWithTrace.jar TestPinCaseWithTrace
* TestPinCaseWithCFLH TestPinCaseWithCFLH.jar
* @run main/othervm/timeout=100 -Djdk.virtualThreadScheduler.maxPoolSize=1 -Djdk.tracePinnedThreads=full -javaagent:TestPinCaseWithCFLH.jar TestPinCaseWithCFLH
*/
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;

public class TestPinCaseWithTrace {
public class TestPinCaseWithCFLH {

public static class TestClassFileTransformer implements ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
Expand All @@ -51,16 +51,19 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
instrumentation.addTransformer(new TestClassFileTransformer());
}

private static int result = 0;

public static void main(String[] args) throws Exception{
Thread t1 = Thread.ofVirtual().name("vthread-1").start(() -> {
System.out.println("call native: " + nativeFuncPin(1));
result = nativeFuncPin(1);
});
t1.join();
assert result == 4;
}

static int native2Java(int b) {
try {
Thread.sleep(500); // try yield, will pin, javaagent + tracePinnedThreads will crash here (because of the class `PinnedThreadPrinter`)
Thread.sleep(500); // try yield, will pin, javaagent + tracePinnedThreads should not lead to crash (because of the class `PinnedThreadPrinter`)
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -71,6 +74,6 @@ static int native2Java(int b) {
private static native int nativeFuncPin(int x);

static {
System.loadLibrary("PinJNI");
System.loadLibrary("TestPinCaseWithCFLH");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
* questions.
*/

#include "jni.h"
#include <jni.h>

extern "C" {

JNIEXPORT jint JNICALL
Java_TestPinCaseWithTrace_nativeFuncPin(JNIEnv* env, jclass klass, jint x) {
jmethodID m = (*env)->GetStaticMethodID(env, klass, "native2Java", "(I)I");
jint r = (*env)->CallStaticIntMethod(env, klass, m, x+1);
Java_TestPinCaseWithCFLH_nativeFuncPin(JNIEnv* env, jclass klass, jint x) {
jmethodID m = env->GetStaticMethodID(klass, "native2Java", "(I)I");
jint r = env->CallStaticIntMethod(klass, m, x+1);
return r + 1;
}

} // extern "C"

0 comments on commit 1b0de48

Please sign in to comment.