From 15190816f704f2e8681bc3e2d74832828a574106 Mon Sep 17 00:00:00 2001
From: Serguei Spitsyn <sspitsyn@openjdk.org>
Date: Wed, 24 Apr 2024 11:43:49 +0000
Subject: [PATCH] 8330303: Crash: assert(_target_jt == nullptr ||
 _target_jt->vthread() == target_h()) failed

Reviewed-by: pchilanomate, cjplummer, lmesnik
---
 src/hotspot/share/prims/jvmtiEnvBase.cpp        | 4 +++-
 src/hotspot/share/prims/jvmtiEnvBase.hpp        | 8 ++++++--
 src/hotspot/share/prims/jvmtiEnvThreadState.cpp | 3 ++-
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/hotspot/share/prims/jvmtiEnvBase.cpp b/src/hotspot/share/prims/jvmtiEnvBase.cpp
index 778cc17ffe0..4ed01df0b3d 100644
--- a/src/hotspot/share/prims/jvmtiEnvBase.cpp
+++ b/src/hotspot/share/prims/jvmtiEnvBase.cpp
@@ -2076,7 +2076,9 @@ GetSingleStackTraceClosure::do_thread(Thread *target) {
 
 void
 GetSingleStackTraceClosure::do_vthread(Handle target_h) {
-  assert(_target_jt == nullptr || _target_jt->vthread() == target_h(), "sanity check");
+  // Use jvmti_vthread() instead of vthread() as target could have temporarily changed
+  // identity to carrier thread (see VirtualThread.switchToCarrierThread).
+  assert(_target_jt == nullptr || _target_jt->jvmti_vthread() == target_h(), "sanity check");
   doit();
 }
 
diff --git a/src/hotspot/share/prims/jvmtiEnvBase.hpp b/src/hotspot/share/prims/jvmtiEnvBase.hpp
index 57fa059b2d8..00de17a8a6b 100644
--- a/src/hotspot/share/prims/jvmtiEnvBase.hpp
+++ b/src/hotspot/share/prims/jvmtiEnvBase.hpp
@@ -506,7 +506,9 @@ public:
   }
   void do_vthread(Handle target_h) {
     assert(_target_jt != nullptr, "sanity check");
-    assert(_target_jt->vthread() == target_h(), "sanity check");
+    // Use jvmti_vthread() instead of vthread() as target could have temporarily changed
+    // identity to carrier thread (see VirtualThread.switchToCarrierThread).
+    assert(_target_jt->jvmti_vthread() == target_h(), "sanity check");
     doit(_target_jt); // mounted virtual thread
   }
 };
@@ -526,7 +528,9 @@ public:
   }
   void do_vthread(Handle target_h) {
     assert(_target_jt != nullptr, "sanity check");
-    assert(_target_jt->vthread() == target_h(), "sanity check");
+    // Use jvmti_vthread() instead of vthread() as target could have temporarily changed
+    // identity to carrier thread (see VirtualThread.switchToCarrierThread).
+    assert(_target_jt->jvmti_vthread() == target_h(), "sanity check");
     doit(_target_jt); // mounted virtual thread
   }
 };
diff --git a/src/hotspot/share/prims/jvmtiEnvThreadState.cpp b/src/hotspot/share/prims/jvmtiEnvThreadState.cpp
index f61f9415bbc..b531961e7a5 100644
--- a/src/hotspot/share/prims/jvmtiEnvThreadState.cpp
+++ b/src/hotspot/share/prims/jvmtiEnvThreadState.cpp
@@ -298,7 +298,8 @@ class GetCurrentLocationClosure : public JvmtiUnitedHandshakeClosure {
   }
   void do_vthread(Handle target_h) {
     assert(_target_jt == nullptr || !_target_jt->is_exiting(), "sanity check");
-    // use jvmti_vthread() as vthread() can be outdated
+    // Use jvmti_vthread() instead of vthread() as target could have temporarily changed
+    // identity to carrier thread (see VirtualThread.switchToCarrierThread).
     assert(_target_jt == nullptr || _target_jt->jvmti_vthread() == target_h(), "sanity check");
     ResourceMark rm;
     javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());