Skip to content

Commit

Permalink
Don't try to setup_jvmti_thread_state for obj allocation sampling if …
Browse files Browse the repository at this point in the history
…the current thread is attaching from native and is allocating the thread oop. That's to make sure we don't create a 'partial' JvmtiThreadState.
  • Loading branch information
jianglizhou committed Nov 14, 2023
1 parent 959305b commit c2f83e8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/hotspot/share/prims/jvmtiExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3139,6 +3139,15 @@ bool JvmtiSampledObjectAllocEventCollector::object_alloc_is_safe_to_sample() {
return false;
}

// If the current thread is attaching from native and its thread oop is being
// allocated, things are not ready for allocation sampling.
if (thread->is_Java_thread()) {
JavaThread* jt = JavaThread::cast(thread);
if (jt->is_attaching_via_jni() && jt->threadObj() == nullptr) {
return false;
}
}

if (MultiArray_lock->owner() == thread) {
return false;
}
Expand Down
16 changes: 11 additions & 5 deletions src/hotspot/share/prims/jvmtiThreadState.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@ inline JvmtiThreadState* JvmtiThreadState::state_for_while_locked(JavaThread *th
return nullptr;
}

#ifdef ASSERT
// Make sure we don't see an incomplete state. An incomplete state can cause
// a duplicate JvmtiThreadState being created below and bound to the 'thread'
// incorrectly, which leads to stale JavaThread* from the JvmtiThreadState
// after the thread exits.
if (state != nullptr) {
assert(state->get_thread_oop() != nullptr, "incomplete state");
}
#endif

if (thread_oop == nullptr) { // Then thread should not be null (see assert above).
thread_oop = thread->jvmti_vthread() != nullptr ? thread->jvmti_vthread() : thread->threadObj();
}
// The state->get_thread_oop() may be null if the state is created during
// the allocation of the thread oop when a native thread is attaching. Make
// sure we don't create a new state for the JavaThread.
if (state == nullptr || (state->get_thread_oop() != nullptr &&
state->get_thread_oop() != thread_oop)) {
if (state == nullptr || state->get_thread_oop() != thread_oop) {
// Check if java_lang_Thread already has a link to the JvmtiThreadState.
if (thread_oop != nullptr) { // thread_oop can be null during early VMStart.
state = java_lang_Thread::jvmti_thread_state(thread_oop);
Expand Down

0 comments on commit c2f83e8

Please sign in to comment.