From cc149e8516aa30f02b6ec4874e16782b21f35ee4 Mon Sep 17 00:00:00 2001 From: Zhengyu Gu Date: Mon, 15 Sep 2025 08:46:10 -0400 Subject: [PATCH] Bypass JDK-8313796 workaround for the fixed JDK versions --- ddprof-lib/src/main/cpp/profiler.cpp | 20 ++++++++++++++++++-- ddprof-lib/src/main/cpp/profiler.h | 4 ++++ ddprof-lib/src/main/cpp/vmEntry.cpp | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ddprof-lib/src/main/cpp/profiler.cpp b/ddprof-lib/src/main/cpp/profiler.cpp index e51b3dc70..65f7e6b2e 100644 --- a/ddprof-lib/src/main/cpp/profiler.cpp +++ b/ddprof-lib/src/main/cpp/profiler.cpp @@ -43,6 +43,7 @@ // can be still accessed concurrently during VM termination Profiler *const Profiler::_instance = new Profiler(); volatile bool Profiler::_signals_initialized = false; +volatile bool Profiler::_need_JDK_8313796_workaround = true; static void (*orig_trapHandler)(int signo, siginfo_t *siginfo, void *ucontext); static void (*orig_segvHandler)(int signo, siginfo_t *siginfo, void *ucontext); @@ -917,8 +918,9 @@ bool Profiler::crashHandler(int signo, siginfo_t *siginfo, void *ucontext) { ddprof::StackWalker::checkFault(thrd); - // Workaround for JDK-8313796. Setting cstack=dwarf also helps - if (VMStructs::isInterpretedFrameValidFunc((const void *)pc) && + // Workaround for JDK-8313796 if needed. Setting cstack=dwarf also helps + if (_need_JDK_8313796_workaround && + VMStructs::isInterpretedFrameValidFunc((const void *)pc) && frame.skipFaultInstruction()) { if (thrd != nullptr) { thrd->exitCrashHandler(); @@ -1101,6 +1103,20 @@ Error Profiler::checkJvmCapabilities() { return Error::OK; } +void Profiler::check_JDK_8313796_workaround() { + int java_version = VM::java_version(); + int java_update_version = VM::java_update_version(); + + // JDK-8313796 has been fixed in JDK 22 and backported to + // JDK versions 11.0.21, 17.0.9 and 21.0.1 + bool fixed_version = java_version >= 22 || + (java_version == 11 && java_update_version >= 21) || + (java_version == 17 && java_update_version >= 9) || + (java_version == 21 && java_update_version >= 1); + _need_JDK_8313796_workaround = !fixed_version; +} + + Error Profiler::start(Arguments &args, bool reset) { MutexLocker ml(_state_lock); if (_state > IDLE) { diff --git a/ddprof-lib/src/main/cpp/profiler.h b/ddprof-lib/src/main/cpp/profiler.h index 1e8e9902f..5b1b48fee 100644 --- a/ddprof-lib/src/main/cpp/profiler.h +++ b/ddprof-lib/src/main/cpp/profiler.h @@ -67,6 +67,9 @@ class Profiler { // signal handlers static volatile bool _signals_initialized; + // JDK_8313796 workaround for unfixed versions + static volatile bool _need_JDK_8313796_workaround; + Mutex _state_lock; State _state; // class unload hook @@ -152,6 +155,7 @@ class Profiler { void unlockAll(); static bool crashHandler(int signo, siginfo_t *siginfo, void *ucontext); + static void check_JDK_8313796_workaround(); static Profiler *const _instance; diff --git a/ddprof-lib/src/main/cpp/vmEntry.cpp b/ddprof-lib/src/main/cpp/vmEntry.cpp index da8435933..de2f93fb3 100644 --- a/ddprof-lib/src/main/cpp/vmEntry.cpp +++ b/ddprof-lib/src/main/cpp/vmEntry.cpp @@ -459,6 +459,7 @@ bool VM::initProfilerBridge(JavaVM *vm, bool attach) { // Run late initialization when JVM is ready void VM::ready(jvmtiEnv *jvmti, JNIEnv *jni) { + Profiler::check_JDK_8313796_workaround(); Profiler::setupSignalHandlers(); { JitWriteProtection jit(true);