Skip to content

Commit

Permalink
8283849: AsyncGetCallTrace may crash JVM on guarantee
Browse files Browse the repository at this point in the history
Reviewed-by: mdoerr
Backport-of: 1963985
  • Loading branch information
Jaroslav Bachorik committed Jun 20, 2022
1 parent 8e94c68 commit 684f12e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/hotspot/share/code/codeCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,14 +621,23 @@ bool CodeCache::contains(nmethod *nm) {
return contains((void *)nm);
}

static bool is_in_asgct() {
Thread* current_thread = Thread::current_or_null_safe();
return current_thread != NULL && current_thread->is_Java_thread() && ((JavaThread*)current_thread)->in_asgct();
}

// This method is safe to call without holding the CodeCache_lock, as long as a dead CodeBlob is not
// looked up (i.e., one that has been marked for deletion). It only depends on the _segmap to contain
// valid indices, which it will always do, as long as the CodeBlob is not in the process of being recycled.
CodeBlob* CodeCache::find_blob(void* start) {
CodeBlob* result = find_blob_unsafe(start);
// We could potentially look up non_entrant methods
guarantee(result == NULL || !result->is_zombie() || result->is_locked_by_vm() || VMError::is_error_reported(), "unsafe access to zombie method");
return result;
bool is_zombie = result != NULL && result->is_zombie();
bool is_result_safe = !is_zombie || result->is_locked_by_vm() || VMError::is_error_reported();
guarantee(is_result_safe || is_in_asgct(), "unsafe access to zombie method");
// When in ASGCT the previous gurantee will pass for a zombie method but we still don't want that code blob returned in order
// to minimize the chance of accessing dead memory
return is_result_safe ? result : NULL;
}

// Lookup that does not fail if you lookup a zombie method (if you call this, be sure to know
Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/share/prims/forte.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,9 @@ void AsyncGetCallTrace(ASGCT_CallTrace *trace, jint depth, void* ucontext) {
return;
}

// !important! make sure all to call thread->set_in_asgct(false) before every return
thread->set_in_asgct(true);

switch (thread->thread_state()) {
case _thread_new:
case _thread_uninitialized:
Expand Down Expand Up @@ -608,6 +611,7 @@ void AsyncGetCallTrace(ASGCT_CallTrace *trace, jint depth, void* ucontext) {
trace->num_frames = ticks_unknown_state; // -7
break;
}
thread->set_in_asgct(false);
}


Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/runtime/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,7 @@ void JavaThread::initialize() {
clear_must_deopt_id();
set_monitor_chunks(NULL);
set_next(NULL);
_in_asgct = false;
_on_thread_list = false;
_thread_state = _thread_new;
_terminated = _not_terminated;
Expand Down
5 changes: 5 additions & 0 deletions src/hotspot/share/runtime/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ class JavaThread: public Thread {
friend class WhiteBox;
private:
JavaThread* _next; // The next thread in the Threads list
bool _in_asgct; // Is set when this JavaThread is handling ASGCT call
bool _on_thread_list; // Is set when this JavaThread is added to the Threads list
oop _threadObj; // The Java level thread object

Expand Down Expand Up @@ -2054,6 +2055,10 @@ class JavaThread: public Thread {
bool is_attaching_via_jni() const { return _jni_attach_state == _attaching_via_jni; }
bool has_attached_via_jni() const { return is_attaching_via_jni() || _jni_attach_state == _attached_via_jni; }
inline void set_done_attaching_via_jni();

// AsyncGetCallTrace support
inline bool in_asgct(void) {return _in_asgct;}
inline void set_in_asgct(bool value) {_in_asgct = value;}
};

// Inline implementation of JavaThread::current
Expand Down

1 comment on commit 684f12e

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.