Skip to content
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

Be defensive when working with potentially unitialized thread structures #84

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions ddprof-lib/src/main/cpp/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void Profiler::onThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
if (_thread_filter.enabled()) {
_thread_filter.remove(tid);
}
updateThreadName(jvmti, jni, thread);
updateThreadName(jvmti, jni, thread, true);

_cpu_engine->registerThread(tid);
_wall_engine->registerThread(tid);
Expand All @@ -127,7 +127,7 @@ void Profiler::onThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
if (_thread_filter.enabled()) {
_thread_filter.remove(tid);
}
updateThreadName(jvmti, jni, thread);
updateThreadName(jvmti, jni, thread, true);

_cpu_engine->unregisterThread(tid);
// unregister here because JNI callers generally don't know about thread exits
Expand Down Expand Up @@ -889,10 +889,17 @@ void Profiler::setupSignalHandlers() {
}
}

void Profiler::updateThreadName(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
/**
* Update thread name for the given thread
*/
void Profiler::updateThreadName(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread, bool self) {
JitWriteProtection jit(true); // workaround for JDK-8262896
jvmtiThreadInfo thread_info;
int native_thread_id = VMThread::nativeThreadId(jni, thread);
if (native_thread_id < 0 && self) {
// if updating the current thread, use the native thread id from the ProfilerThread
native_thread_id = ProfiledThread::currentTid();
}
if (native_thread_id >= 0 && jvmti->GetThreadInfo(thread, &thread_info) == 0) {
jlong java_thread_id = VMThread::javaThreadId(jni, thread);
_thread_info.set(native_thread_id, thread_info.name, java_thread_id);
Expand Down
2 changes: 1 addition & 1 deletion ddprof-lib/src/main/cpp/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class Profiler {
int getJavaTraceInternal(jvmtiFrameInfo* jvmti_frames, ASGCT_CallFrame* frames, int max_depth);
int convertFrames(jvmtiFrameInfo* jvmti_frames, ASGCT_CallFrame* frames, int num_frames);
void fillFrameTypes(ASGCT_CallFrame* frames, int num_frames, NMethod* nmethod);
void updateThreadName(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread);
void updateThreadName(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread, bool self = false);
void updateJavaThreadNames();
void updateNativeThreadNames();
void mangle(const char* name, char* buf, size_t size);
Expand Down
2 changes: 1 addition & 1 deletion ddprof-lib/src/main/cpp/vmStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ class VMThread : VMStructs {
}

static VMThread* fromJavaThread(JNIEnv* env, jthread thread) {
return (VMThread*)(uintptr_t)env->GetLongField(thread, _eetop);
return _eetop != NULL && thread != NULL ? (VMThread*)(uintptr_t)env->GetLongField(thread, _eetop) : NULL;
}

static VMThread* fromEnv(JNIEnv* env) {
Expand Down
Loading