Skip to content

Commit

Permalink
Be defensive when working with potentially unitialized thread structu…
Browse files Browse the repository at this point in the history
…res (#84)
  • Loading branch information
jbachorik authored Apr 10, 2024
1 parent 0ddf730 commit 923da08
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
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

0 comments on commit 923da08

Please sign in to comment.