Skip to content

Conversation

@alexmenkov
Copy link

@alexmenkov alexmenkov commented Jul 3, 2025

The fix updates java_lang_Thread::async_get_stack_trace() (used by java.lang.Thread.getStackTrace() to get stack trace for platform and mounted virtual threads) to correctly use ThreadListHandle for thread protection.

Testing: tier1..5


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8361103: java_lang_Thread::async_get_stack_trace does not properly protect JavaThread (Bug - P3)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26119/head:pull/26119
$ git checkout pull/26119

Update a local copy of the PR:
$ git checkout pull/26119
$ git pull https://git.openjdk.org/jdk.git pull/26119/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26119

View PR using the GUI difftool:
$ git pr show -t 26119

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26119.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 3, 2025

👋 Welcome back amenkov! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Jul 3, 2025

@alexmenkov This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8361103: java_lang_Thread::async_get_stack_trace does not properly protect JavaThread

Reviewed-by: sspitsyn, dholmes

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 166 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot changed the title 8361103 8361103: java_lang_Thread::async_get_stack_trace does not properly protect JavaThread Jul 3, 2025
@openjdk openjdk bot added the rfr Pull request is ready for review label Jul 3, 2025
@openjdk
Copy link

openjdk bot commented Jul 3, 2025

@alexmenkov The following label will be automatically applied to this pull request:

  • hotspot

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the hotspot hotspot-dev@openjdk.org label Jul 3, 2025
@mlbridge
Copy link

mlbridge bot commented Jul 3, 2025

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good. A couple of minor nits/queries.

Thanks

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jul 8, 2025
@dholmes-ora
Copy link
Member

I'm also unclear if this code in the handshake is sufficient for dealing with the case where we have a mounted virtual thread initially, but it is unmounted before the handshake gets to execute:

    if (java_lang_VirtualThread::is_instance(_java_thread())) {
        // if (thread->vthread() != _java_thread()) // We might be inside a System.executeOnCarrierThread
        const ContinuationEntry* ce = thread->vthread_continuation();
        if (ce == nullptr || ce->cont_oop(thread) != java_lang_VirtualThread::continuation(_java_thread())) {
          return; // not mounted
        }
      }

The commented line seems an inaccurate characterization as we could be executing a completely different virtual thread on this carrier now - which is not related to executeOnCarrierThread.

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revoking my approval as there may be further issues.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jul 8, 2025
@alexmenkov
Copy link
Author

I'm also unclear if this code in the handshake is sufficient for dealing with the case where we have a mounted virtual thread initially, but it is unmounted before the handshake gets to execute:

    if (java_lang_VirtualThread::is_instance(_java_thread())) {
        // if (thread->vthread() != _java_thread()) // We might be inside a System.executeOnCarrierThread
        const ContinuationEntry* ce = thread->vthread_continuation();
        if (ce == nullptr || ce->cont_oop(thread) != java_lang_VirtualThread::continuation(_java_thread())) {
          return; // not mounted
        }
      }

The commented line seems an inaccurate characterization as we could be executing a completely different virtual thread on this carrier now - which is not related to executeOnCarrierThread.

I suppose the comment is obsolete. I don't see executeOnCarrierThread in the current codebase.
The condition checks if there were some changes in vthread/carrier:
ce == nullptr - the carries has no mounted vthread now (i.e. the vthread has been unmounted);
ce->cont_oop(thread) != java_lang_VirtualThread::continuation(_java_thread()) - the carrier has other vthread mounted;
Even if there were unmount/mount the same vthread on the carrier, it's fine (we need vthread stack trace and we are in handshake with its carrier)

@AlanBateman
Copy link
Contributor

I suppose the comment is obsolete. I don't see executeOnCarrierThread in the current codebase. The condition checks if there were some changes in vthread/carrier: ce == nullptr - the carries has no mounted vthread now (i.e. the vthread has been unmounted); ce->cont_oop(thread) != java_lang_VirtualThread::continuation(_java_thread()) - the carrier has other vthread mounted; Even if there were unmount/mount the same vthread on the carrier, it's fine (we need vthread stack trace and we are in handshake with its carrier)

I suspect it dates back to when there were temporary transitions and where the thread identity had to be temporarily changed to the carrier thread. We've been able to remove that complexity, which eliminates complexity from JVMTI, and maybe here too.

@alexmenkov
Copy link
Author

  • added mounted vthread's carrier support to ThreadsListHandle
  • renamed fields/variables in GetStackTraceHandshakeClosure
  • removed obsolete comment about executeOnCarrierThread

@dholmes-ora
Copy link
Member

Added extra argument to cv_internal_thread_to_JavaThread to return carrrier's JavaThread.

@alexmenkov I think we need a separate JBS issue for this and have runtime fix it. I want to be sure we are fixing it in the right/best way. I don't think we need the extra argument, but need to examine the existing usages. Chances are that any code trying to deal with virtual thread's via the API is actually doing it wrong and will need fixing anyway. I will file a bug.

@dholmes-ora
Copy link
Member

Filed JDK-8361912

@alexmenkov
Copy link
Author

Added extra argument to cv_internal_thread_to_JavaThread to return carrrier's JavaThread.

@alexmenkov I think we need a separate JBS issue for this and have runtime fix it. I want to be sure we are fixing it in the right/best way. I don't think we need the extra argument, but need to examine the existing usages. Chances are that any code trying to deal with virtual thread's via the API is actually doing it wrong and will need fixing anyway. I will file a bug.

I think just update the method to use carrier is error prone and callers should explicitly request the functionality.
I'm fine with separate issue, will remove changes in threadSMR.* and restore original handling of virtual thread carrier

@dholmes-ora
Copy link
Member

@alexmenkov Alex, the form of this fix will be determined by whatever we end up doing for JDK-8361912. I don't see any point in re-doing this fix later, so I suggest just putting this on hold for now. I will ensure the other issue is fixed within the next week.

@AlanBateman
Copy link
Contributor

I've created JDK-8364343 with a stress test that tickles several asserts that we can use for follow-on issues.

class GetStackTraceHandshakeClosure : public HandshakeClosure {
public:
const Handle _java_thread;
const Handle _thread_oop;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this change! Very confusing otherwise.


bool carrier = false;
if (java_lang_VirtualThread::is_instance(_java_thread())) {
// if (thread->vthread() != _java_thread()) // We might be inside a System.executeOnCarrierThread
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to check with @AlanBateman whether this check is also needed (he does both in his draft PR).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThreadSnapshotFactory::get_thread_snapshot needs to fail (and the caller retry) if called while the target virtual thread is in transition. This is because it captures the thread state in addition to the stack trace, and several other properties. So this is why (in the draft changes) it checks that thread identity is set and that the continuation is mounted.

java_lang_Thread::async_get_stack_trace just needs to walk the continuation stack so checking that the continuation is mounted should be enough here.

Comment on lines 1936 to 1939
if (ce == nullptr || ce->cont_oop(java_thread) != java_lang_VirtualThread::continuation(_thread_oop())) {
// Target thread has been unmounted.
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we set carrier = true if we don't return here? Or am I misunderstanding what "carrier" means here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"carrier" means target is a platform thread with mounted virtual thread.
When carrier == true vframeStream skips frames of the mounted virtual thread.
Here target is a virtual thread, so we need frames starting from the last frame

Copy link
Member

@dholmes-ora dholmes-ora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good (a couple of typos). Thanks.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 6, 2025
Co-authored-by: David Holmes <62092539+dholmes-ora@users.noreply.github.com>
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Aug 6, 2025
Copy link
Contributor

@sspitsyn sspitsyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I've posted one nit though.


GetStackTraceHandshakeClosure(Handle java_thread) :
HandshakeClosure("GetStackTraceHandshakeClosure"), _java_thread(java_thread), _depth(0), _retry_handshake(false),
GetStackTraceHandshakeClosure(Handle thread_oop) :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd suggest to rename: thread_oop => thread_h

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (thread_oop => thread_h and _thread_oop => _thread_h)

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 12, 2025
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Aug 12, 2025
@openjdk openjdk bot added the ready Pull request is ready to be integrated label Aug 12, 2025
@alexmenkov
Copy link
Author

Thank you for the review @dholmes-ora and @sspitsyn
/integrate

@openjdk
Copy link

openjdk bot commented Aug 13, 2025

Going to push as commit ecbdd34.
Since your change was applied there have been 174 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Aug 13, 2025
@openjdk openjdk bot closed this Aug 13, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Aug 13, 2025
@openjdk
Copy link

openjdk bot commented Aug 13, 2025

@alexmenkov Pushed as commit ecbdd34.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@alexmenkov alexmenkov deleted the tlh_stack_trace branch August 13, 2025 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotspot hotspot-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

5 participants