changeset 60216:61ee15b9a1ac

8239462: jdk.hotspot.agent misses some ReleaseStringUTFChars calls in case of early returns Reviewed-by: clanger, amenkov, sspitsyn
author mbaesken
date Mon, 24 Feb 2020 09:59:31 +0100
parents afeefc19ecb9
children 582e5f2661c3
files src/jdk.hotspot.agent/linux/native/libsaproc/LinuxDebuggerLocal.cpp src/jdk.hotspot.agent/macosx/native/libsaproc/MacosxDebuggerLocal.m src/jdk.hotspot.agent/solaris/native/libsaproc/saproc.cpp
diffstat 3 files changed, 52 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.hotspot.agent/linux/native/libsaproc/LinuxDebuggerLocal.cpp	Wed Feb 26 00:07:21 2020 -0500
+++ b/src/jdk.hotspot.agent/linux/native/libsaproc/LinuxDebuggerLocal.cpp	Mon Feb 24 09:59:31 2020 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
  * Copyright (c) 2019, NTT DATA.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
@@ -64,6 +64,28 @@
 #include "sun_jvm_hotspot_debugger_aarch64_AARCH64ThreadContext.h"
 #endif
 
+class AutoJavaString {
+  JNIEnv* m_env;
+  jstring m_str;
+  const char* m_buf;
+
+public:
+  // check env->ExceptionOccurred() after ctor
+  AutoJavaString(JNIEnv* env, jstring str)
+    : m_env(env), m_str(str), m_buf(env->GetStringUTFChars(str, NULL)) {
+  }
+
+  ~AutoJavaString() {
+    if (m_buf) {
+      m_env->ReleaseStringUTFChars(m_str, m_buf);
+    }
+  }
+
+  operator const char* () const {
+    return m_buf;
+  }
+};
+
 static jfieldID p_ps_prochandle_ID = 0;
 static jfieldID threadList_ID = 0;
 static jfieldID loadObjectList_ID = 0;
@@ -234,7 +256,8 @@
   if (saaltroot != NULL) {
     free(saaltroot);
   }
-  const char *path = env->GetStringUTFChars(altroot, JNI_FALSE);
+  const char *path = env->GetStringUTFChars(altroot, NULL);
+  if (path == NULL) { return; }
   /*
    * `saaltroot` is used for putenv().
    * So we need to keep this memory.
@@ -281,27 +304,19 @@
 extern "C"
 JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_attach0__Ljava_lang_String_2Ljava_lang_String_2
   (JNIEnv *env, jobject this_obj, jstring execName, jstring coreName) {
-  const char *execName_cstr;
-  const char *coreName_cstr;
-  jboolean isCopy;
   struct ps_prochandle* ph;
-
-  execName_cstr = env->GetStringUTFChars(execName, &isCopy);
+  AutoJavaString execName_cstr(env, execName);
   CHECK_EXCEPTION;
-  coreName_cstr = env->GetStringUTFChars(coreName, &isCopy);
+  AutoJavaString coreName_cstr(env, coreName);
   CHECK_EXCEPTION;
 
   verifyBitness(env, execName_cstr);
   CHECK_EXCEPTION;
 
   if ( (ph = Pgrab_core(execName_cstr, coreName_cstr)) == NULL) {
-    env->ReleaseStringUTFChars(execName, execName_cstr);
-    env->ReleaseStringUTFChars(coreName, coreName_cstr);
     THROW_NEW_DEBUGGER_EXCEPTION("Can't attach to the core file");
   }
   env->SetLongField(this_obj, p_ps_prochandle_ID, (jlong)(intptr_t)ph);
-  env->ReleaseStringUTFChars(execName, execName_cstr);
-  env->ReleaseStringUTFChars(coreName, coreName_cstr);
   fillThreadsAndLoadObjects(env, this_obj, ph);
 }
 
@@ -331,25 +346,15 @@
 extern "C"
 JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_lookupByName0
   (JNIEnv *env, jobject this_obj, jstring objectName, jstring symbolName) {
-  const char *objectName_cstr, *symbolName_cstr;
   jlong addr;
   jboolean isCopy;
   struct ps_prochandle* ph = get_proc_handle(env, this_obj);
-
-  objectName_cstr = NULL;
-  if (objectName != NULL) {
-    objectName_cstr = env->GetStringUTFChars(objectName, &isCopy);
-    CHECK_EXCEPTION_(0);
-  }
-  symbolName_cstr = env->GetStringUTFChars(symbolName, &isCopy);
+  AutoJavaString objectName_cstr(env, objectName);
+  CHECK_EXCEPTION_(0);
+  AutoJavaString symbolName_cstr(env, symbolName);
   CHECK_EXCEPTION_(0);
 
   addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
-
-  if (objectName_cstr != NULL) {
-    env->ReleaseStringUTFChars(objectName, objectName_cstr);
-  }
-  env->ReleaseStringUTFChars(symbolName, symbolName_cstr);
   return addr;
 }
 
@@ -594,7 +599,10 @@
   int status;
   jstring result = NULL;
 
-  const char *sym = env->GetStringUTFChars(jsym, JNI_FALSE);
+  const char *sym = env->GetStringUTFChars(jsym, NULL);
+  if (sym == NULL) {
+    THROW_NEW_DEBUGGER_EXCEPTION_("Error getting symbol string", NULL);
+  }
   char *demangled = abi::__cxa_demangle(sym, NULL, 0, &status);
   env->ReleaseStringUTFChars(jsym, sym);
   if ((demangled != NULL) && (status == 0)) {
--- a/src/jdk.hotspot.agent/macosx/native/libsaproc/MacosxDebuggerLocal.m	Wed Feb 26 00:07:21 2020 -0500
+++ b/src/jdk.hotspot.agent/macosx/native/libsaproc/MacosxDebuggerLocal.m	Mon Feb 24 09:59:31 2020 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -243,7 +243,12 @@
     CHECK_EXCEPTION_(0);
   }
   symbolName_cstr = (*env)->GetStringUTFChars(env, symbolName, &isCopy);
-  CHECK_EXCEPTION_(0);
+  if ((*env)->ExceptionOccurred(env)) {
+    if (objectName_cstr != NULL) {
+      (*env)->ReleaseStringUTFChars(env, objectName, objectName_cstr);
+    }
+    return 0;
+  }
 
   print_debug("look for %s \n", symbolName_cstr);
   addr = (jlong) lookup_symbol(ph, objectName_cstr, symbolName_cstr);
@@ -963,7 +968,10 @@
   execName_cstr = (*env)->GetStringUTFChars(env, execName, &isCopy);
   CHECK_EXCEPTION;
   coreName_cstr = (*env)->GetStringUTFChars(env, coreName, &isCopy);
-  CHECK_EXCEPTION;
+  if ((*env)->ExceptionOccurred(env)) {
+    (*env)->ReleaseStringUTFChars(env, execName, execName_cstr);
+    return;
+  }
 
   print_debug("attach: %s %s\n", execName_cstr, coreName_cstr);
 
--- a/src/jdk.hotspot.agent/solaris/native/libsaproc/saproc.cpp	Wed Feb 26 00:07:21 2020 -0500
+++ b/src/jdk.hotspot.agent/solaris/native/libsaproc/saproc.cpp	Mon Feb 24 09:59:31 2020 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1184,7 +1184,12 @@
    }
 
    const char* symbolName_cstr = env->GetStringUTFChars(symbolName, &isCopy);
-   CHECK_EXCEPTION_(0);
+   if (env->ExceptionOccurred()) {
+     if (objectName_cstr != PR_OBJ_EVERY) {
+       env->ReleaseStringUTFChars(objectName, objectName_cstr);
+     }
+     return 0;
+   }
 
    psaddr_t symbol_addr = (psaddr_t) 0;
    ps_pglobal_lookup((struct ps_prochandle*) p_ps_prochandle,  objectName_cstr,