Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
8289091: move oop safety check from SharedRuntime::get_java_tid() to …
…JavaThread::threadObj()

Reviewed-by: rehn, dholmes
  • Loading branch information
Daniel D. Daugherty committed Jul 5, 2022
1 parent 29ea642 commit 30e134e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
3 changes: 0 additions & 3 deletions src/hotspot/share/runtime/sharedRuntime.cpp
Expand Up @@ -997,9 +997,6 @@ JRT_END

jlong SharedRuntime::get_java_tid(Thread* thread) {
if (thread != NULL && thread->is_Java_thread()) {
Thread* current = Thread::current();
guarantee(current != thread || JavaThread::cast(thread)->is_oop_safe(),
"current cannot touch oops after its GC barrier is detached.");
oop obj = JavaThread::cast(thread)->threadObj();
return (obj == NULL) ? 0 : java_lang_Thread::thread_id(obj);
}
Expand Down
58 changes: 44 additions & 14 deletions src/hotspot/share/runtime/thread.cpp
Expand Up @@ -794,6 +794,10 @@ void JavaThread::set_threadOopHandles(oop p) {
}

oop JavaThread::threadObj() const {
Thread* current = Thread::current_or_null_safe();
assert(current != nullptr, "cannot be called by a detached thread");
guarantee(current != this || JavaThread::cast(current)->is_oop_safe(),
"current cannot touch oops after its GC barrier is detached.");
return _threadObj.resolve();
}

Expand Down Expand Up @@ -2144,9 +2148,15 @@ void JavaThread::print_name_on_error(outputStream* st, char *buf, int buflen) co
// JavaThread::print() is that we can't grab lock or allocate memory.
void JavaThread::print_on_error(outputStream* st, char *buf, int buflen) const {
st->print("%s \"%s\"", type_name(), get_thread_name_string(buf, buflen));
oop thread_obj = threadObj();
if (thread_obj != NULL) {
if (java_lang_Thread::is_daemon(thread_obj)) st->print(" daemon");
Thread* current = Thread::current_or_null_safe();
assert(current != nullptr, "cannot be called by a detached thread");
if (!current->is_Java_thread() || JavaThread::cast(current)->is_oop_safe()) {
// Only access threadObj() if current thread is not a JavaThread
// or if it is a JavaThread that can safely access oops.
oop thread_obj = threadObj();
if (thread_obj != nullptr) {
if (java_lang_Thread::is_daemon(thread_obj)) st->print(" daemon");
}
}
st->print(" [");
st->print("%s", _get_thread_state_name(_thread_state));
Expand Down Expand Up @@ -2205,23 +2215,43 @@ const char* JavaThread::name() const {
// descriptive string if there is no set name.
const char* JavaThread::get_thread_name_string(char* buf, int buflen) const {
const char* name_str;
oop thread_obj = threadObj();
if (thread_obj != NULL) {
oop name = java_lang_Thread::name(thread_obj);
if (name != NULL) {
if (buf == NULL) {
name_str = java_lang_String::as_utf8_string(name);
#ifdef ASSERT
Thread* current = Thread::current_or_null_safe();
assert(current != nullptr, "cannot be called by a detached thread");
if (!current->is_Java_thread() || JavaThread::cast(current)->is_oop_safe()) {
// Only access threadObj() if current thread is not a JavaThread
// or if it is a JavaThread that can safely access oops.
#endif
oop thread_obj = threadObj();
if (thread_obj != NULL) {
oop name = java_lang_Thread::name(thread_obj);
if (name != NULL) {
if (buf == NULL) {
name_str = java_lang_String::as_utf8_string(name);
} else {
name_str = java_lang_String::as_utf8_string(name, buf, buflen);
}
} else if (is_attaching_via_jni()) { // workaround for 6412693 - see 6404306
name_str = "<no-name - thread is attaching>";
} else {
name_str = java_lang_String::as_utf8_string(name, buf, buflen);
name_str = "<un-named>";
}
} else if (is_attaching_via_jni()) { // workaround for 6412693 - see 6404306
name_str = "<no-name - thread is attaching>";
} else {
name_str = "<un-named>";
name_str = Thread::name();
}
#ifdef ASSERT
} else {
name_str = Thread::name();
// Current JavaThread has exited...
if (current == this) {
// ... and is asking about itself:
name_str = "<no-name - current JavaThread has exited>";
} else {
// ... and it can't safely determine this JavaThread's name so
// use the default thread name.
name_str = Thread::name();
}
}
#endif
assert(name_str != NULL, "unexpected NULL thread name");
return name_str;
}
Expand Down

1 comment on commit 30e134e

@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.