8239462: jdk.hotspot.agent misses some ReleaseStringUTFChars calls in case of early returns

Reviewed-by: clanger, amenkov, sspitsyn
This commit is contained in:
Matthias Baesken 2020-02-24 09:59:31 +01:00
parent 2298819af8
commit dd393fadc4
3 changed files with 52 additions and 31 deletions

View File

@ -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 @@ JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_se
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 @@ JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_at
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 @@ JNIEXPORT void JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal_de
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 @@ JNIEXPORT jstring JNICALL Java_sun_jvm_hotspot_debugger_linux_LinuxDebuggerLocal
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)) {

View File

@ -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 @@ jlong lookupByNameIncore(
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 @@ Java_sun_jvm_hotspot_debugger_bsd_BsdDebuggerLocal_attach0__Ljava_lang_String_2L
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);

View File

@ -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 @@ JNIEXPORT jlong JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_loo
}
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,