Skip to content

8340491: Thread stack-base assertion should report which thread has the un-set stack #21102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

dholmes-ora
Copy link
Member

@dholmes-ora dholmes-ora commented Sep 20, 2024

Please review this simple enhancement to an assertion so we can identify which thread had the problem.

I had to move the function to the cpp file due to include file issues.

We are limited in what we can print due to this being used very early in the thread initialization process - in particular no ResourceMarks are possible so we can't print the name.

Testing:

Thanks


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-8340491: Thread stack-base assertion should report which thread has the un-set stack (Enhancement - P3)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 21102

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

Using diff file

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

Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Sep 20, 2024

👋 Welcome back dholmes! 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 Sep 20, 2024

@dholmes-ora 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:

8340491: Thread stack-base assertion should report which thread has the un-set stack

Reviewed-by: shade, kevinw, stefank

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 129 new commits pushed to the master branch:

  • 988a531: 8340181: Shenandoah: Cleanup ShenandoahRuntime stubs
  • 822a773: 8340605: Open source several AWT PopupMenu tests
  • 6514aef: 8340419: ZGC: Create an UseLargePages adaptation of TestAllocateHeapAt.java
  • ae4d2f1: 8340621: Open source several AWT List tests
  • dd56990: 8340639: Open source few more AWT List tests
  • ade17ec: 8340560: Open Source several AWT/2D font and rendering tests
  • 73ebb84: 8340721: Clarify special case handling of unboxedType and getWildcardType
  • ed140f5: 8341101: [ARM32] Error: ShouldNotReachHere() in TemplateInterpreterGenerator::generate_math_entry after 8338694
  • 082125d: 8340404: CharsetProvider specification updates
  • a7bfced: 8337679: Memset warning in src/hotspot/share/adlc/adlArena.cpp
  • ... and 119 more: https://git.openjdk.org/jdk/compare/fdc16a373459cb2311316448c765b1bee5c73694...master

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 added the rfr Pull request is ready for review label Sep 20, 2024
@openjdk
Copy link

openjdk bot commented Sep 20, 2024

@dholmes-ora The following label will be automatically applied to this pull request:

  • hotspot-runtime

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-runtime hotspot-runtime-dev@openjdk.org label Sep 20, 2024
@mlbridge
Copy link

mlbridge bot commented Sep 20, 2024

Webrevs

Copy link
Member

@stefank stefank left a comment

Choose a reason for hiding this comment

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

Looks fine, but could you explain when this is needed?

If it is the current thread that is crashes then we already print the thread info.

If it is crashing when called on another thread, it might be easier to add informative asserts in that sub-system instead. You could for example add a non-asserting getter and perform all relevant checks on the caller side.

Another alternative could be to leverage the VMErrorCallback functionality I introduced a little while ago. With that you can register a callback to be called by the hs_err printer if the current thread crashes. With that you can have much more context about what the calling code was doing and what information that thread has available. That would look something like this (I've not compiled this, so there might be errors Edit: updated the code):

diff --git a/src/hotspot/share/nmt/memMapPrinter.cpp b/src/hotspot/share/nmt/memMapPrinter.cpp
index 5f920b102a9..a2aae39b0ca 100644
--- a/src/hotspot/share/nmt/memMapPrinter.cpp
+++ b/src/hotspot/share/nmt/memMapPrinter.cpp
@@ -44,6 +44,7 @@
 #include "runtime/vmThread.hpp"
 #include "utilities/globalDefinitions.hpp"
 #include "utilities/ostream.hpp"
+#include "utilities/vmError.hpp"

 // Note: throughout this code we will use the term "VMA" for OS system level memory mapping

@@ -164,8 +165,29 @@ class CachedNMTInformation : public VirtualMemoryWalker {

 /////// Thread information //////////////////////////

+class VMAInspectionErrorCallback : public VMErrorCallback {
+  const void* const   _from;
+  const void* const   _to;
+  const Thread* const _thread;
+
+public:
+  VMAInspectionErrorCallback(const void* from, const void* to, const Thread* thread) :
+    _from(from), _to(to), _thread(thread) {}
+
+  void call(outputStream* st) override {
+    st->print_cr("Crashing while printing VMA details for " PTR_FORMAT " " PTR_FORMAT " for thread %s with id: %d",
+                 p2i(_from),
+                 p2i(_to),
+                 _thread->name(),
+                 _thread->osthread() != nullptr ? _thread->osthread()->thread_id() : 0);
+  }
+};
+
 // Given a VMA [from, to) and a thread, check if vma intersects with thread stack
 static bool vma_touches_thread_stack(const void* from, const void* to, const Thread* t) {
+  VMAInspectionErrorCallback on_error(from, to, t);
+  VMErrorCallbackMark mark(&on_error);
+
   // Java thread stacks (and sometimes also other threads) have guard pages. Therefore they typically occupy
   // at least two distinct neighboring VMAs. Therefore we typically have a 1:n relationshipt between thread
   // stack and vma.

Note that in this context we do have a ResourceMark, so it should be fine to print the name.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 20, 2024
@dholmes-ora
Copy link
Member Author

Thanks for the review @stefank !

If it is crashing when called on another thread, it might be easier to add informative asserts in that sub-system instead. You could for example add a non-asserting getter and perform all relevant checks on the caller side.

The assert is firing when stack_base() is called on another thread. In that case we had nothing to tell us which thread it was, hence this enhancement to provide that information. We want this asserting getter to be used, and for the assert to fire .

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Sep 20, 2024
@@ -532,7 +532,7 @@ class Thread: public ThreadShadow {

public:
// Stack overflow support
address stack_base() const { assert(_stack_base != nullptr,"Sanity check"); return _stack_base; }
address stack_base() const DEBUG_ONLY(;) NOT_DEBUG({ return _stack_base; })
Copy link
Member

Choose a reason for hiding this comment

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

I think it is a bit cleaner to "just" outline the checking method like:

thread.hpp:
  address stack_base() const           { assert_stack_base(); return _stack_base; }
  void assert_stack_base() const NOT_DEBUG_RETURN;

thread.cpp:
#ifdef ASSERT
void  Thread::assert_stack_base() const {
  // Note: can't report Thread::name() here as that can require a ResourceMark which we
  // can't use because this gets called too early in the thread initialization.
  assert(_stack_base != nullptr, "Stack base not yet set for thread id:%d (0 if not set)",
         osthread() != nullptr ? osthread()->thread_id() : 0);
}
#endif

Copy link
Member

Choose a reason for hiding this comment

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

This is the way we do it in existing code, e.g.

void assert_is_frame_safe(const frame& f) NOT_DEBUG_RETURN;

Copy link
Member Author

Choose a reason for hiding this comment

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

So many ways to achieve the same thing ...

I would not want assert_stack_base in the public API.

I did need to fix the ifdef though.

Copy link
Member

@shipilev shipilev Sep 20, 2024

Choose a reason for hiding this comment

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

DEBUG_ONLY(;) just triggers me, sorry. We have NOT_DEBUG_RETURN exactly for this reason: mark the methods that are only defined in debug builds, and shortcut otherwise, without inventing new things :)

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

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

Only have a nitpicking comment, see above ^

@stefank
Copy link
Member

stefank commented Sep 20, 2024

Thanks for the review @stefank !

If it is crashing when called on another thread, it might be easier to add informative asserts in that sub-system instead. You could for example add a non-asserting getter and perform all relevant checks on the caller side.

The assert is firing when stack_base() is called on another thread. In that case we had nothing to tell us which thread it was, hence this enhancement to provide that information.

That was fully understood and I think you are missing my point. I was trying to convey that it might be better to rewrite the calling code to perform its own checks / logging instead of relying on a "low-level" assert in Thread. 2c.

@dholmes-ora
Copy link
Member Author

I was trying to convey that it might be better to rewrite the calling code to perform its own checks / logging instead of relying on a "low-level" assert in Thread.

The calling code is of course being fixed (or at least the cause dealt with one way or another). The point here was to simply enhance the existing assertion to be more informative for the next time it gets triggered.

@stefank
Copy link
Member

stefank commented Sep 23, 2024

I was trying to convey that it might be better to rewrite the calling code to perform its own checks / logging instead of relying on a "low-level" assert in Thread.

The calling code is of course being fixed (or at least the cause dealt with one way or another). The point here was to simply enhance the existing assertion to be more informative for the next time it gets triggered.

Right. And that was what I was curious to know more about because I think there would be better ways to get better diagnostics by changing code there. Anyways, I'll leave you to deal with that in a separate issue.

@dholmes-ora
Copy link
Member Author

Anyone want to add the second review or shall I just abort this?

Copy link
Contributor

@kevinjwalls kevinjwalls 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, and useful!

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Sep 30, 2024
Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

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

Well, I am still nitpicking about DEBUG_ONLY(;), but I can live with it.

@stefank
Copy link
Member

stefank commented Sep 30, 2024

This PR is much more complicated than it needs to be.

  1. We would probably be fine with always having the implementation in the .cpp file. I don't think that we need to optimize away the call for this getter. I don't see hot code using this function.

  2. If we really want to optimize it away, we can just stick the assert in the header just like we already do with related functions:

  bool is_in_stack_range(address adr, address limit, bool inclusive) const {
    assert(stack_base() > limit && limit >= stack_end(), "limit is outside of stack");
    return stack_base() > adr && (inclusive ? adr >= limit : adr > limit);
  }

There's no point in trying to separate the debug/release builds into .cpp/.hpp files if the related functions already put the assert in the header.

@dholmes-ora
Copy link
Member Author

we can just stick the assert in the header just like we already do with related functions:

@stefank as mentioned in the description I ran into include file issues referring to osthread()->thread_id() in thread.hpp, as we only have a forward decl of OSThread.

@dholmes-ora
Copy link
Member Author

Thanks for the review @kevinjwalls !

@stefank
Copy link
Member

stefank commented Sep 30, 2024

we can just stick the assert in the header just like we already do with related functions:

@stefank as mentioned in the description I ran into include file issues referring to osthread()->thread_id() in thread.hpp, as we only have a forward decl of OSThread.

OK. Did you consider my bullet (1) at all?

@stefank
Copy link
Member

stefank commented Sep 30, 2024

we can just stick the assert in the header just like we already do with related functions:

@stefank as mentioned in the description I ran into include file issues referring to osthread()->thread_id() in thread.hpp, as we only have a forward decl of OSThread.

OK. Did you consider my bullet (1) at all?

I re-read the PR and see that this is what you started with and that Thomas requested this to be inlined.

@stefank
Copy link
Member

stefank commented Sep 30, 2024

FTR, I'm not blocking this, I'm just a little annoyed that we unnecessarily (IMHO) uglify the code.

@dholmes-ora
Copy link
Member Author

Okay thanks for the reviews.

/integrate

@openjdk
Copy link

openjdk bot commented Sep 30, 2024

Going to push as commit 31858fc.
Since your change was applied there have been 142 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 Sep 30, 2024
@openjdk openjdk bot closed this Sep 30, 2024
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Sep 30, 2024
@openjdk
Copy link

openjdk bot commented Sep 30, 2024

@dholmes-ora Pushed as commit 31858fc.

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

@dholmes-ora dholmes-ora deleted the 8340491-stack-assert branch September 30, 2024 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotspot-runtime hotspot-runtime-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

5 participants