8201224: Make string buffer size dynamic in mlvmJvmtiUtils.c
Summary: Calculate the string size for the buffer first
Reviewed-by: amenkov, sspitsyn, iklam
--- a/test/hotspot/jtreg/vmTestbase/vm/mlvm/indy/func/jvmti/share/IndyRedefineClass.c Tue Aug 28 17:03:16 2018 +0200
+++ b/test/hotspot/jtreg/vmTestbase/vm/mlvm/indy/func/jvmti/share/IndyRedefineClass.c Wed Aug 22 09:33:18 2018 -0700
@@ -165,8 +165,14 @@
char * locStr;
gIsSingleStepWorking = JNI_TRUE;
locStr = locationToString(jvmti_env, method, location);
- NSK_DISPLAY1("Single step event: %s\n", locStr);
- free(locStr);
+
+ if (locStr == NULL) {
+ NSK_DISPLAY0("Error in Single step event: locationToString failed\n");
+ gIsErrorOccured = JNI_TRUE;
+ } else {
+ NSK_DISPLAY1("Single step event: %s\n", locStr);
+ free(locStr);
+ }
popFrameLogic(jvmti_env, thread);
}
--- a/test/hotspot/jtreg/vmTestbase/vm/mlvm/indy/func/jvmti/stepBreakPopReturn/stepBreakPopReturn.c Tue Aug 28 17:03:16 2018 +0200
+++ b/test/hotspot/jtreg/vmTestbase/vm/mlvm/indy/func/jvmti/stepBreakPopReturn/stepBreakPopReturn.c Wed Aug 22 09:33:18 2018 -0700
@@ -37,6 +37,7 @@
static jboolean gIsMethodEntryWorking = JNI_FALSE;
static jboolean gIsSingleStepWorking = JNI_FALSE;
static jboolean gIsBreakpointWorking = JNI_FALSE;
+static jboolean gErrorHappened = JNI_FALSE;
static jboolean gIsBreakpointSet = JNI_FALSE;
static jboolean gIsFirstCall = JNI_TRUE;
@@ -63,7 +64,8 @@
if ( ! gIsDebuggerCompatible )
NSK_DISPLAY1("Breakpoint event fired? %i\n", gIsBreakpointWorking);
- return gIsMethodEntryWorking && gIsSingleStepWorking && (gIsBreakpointWorking || gIsDebuggerCompatible);
+ return gIsMethodEntryWorking && !gErrorHappened && gIsSingleStepWorking
+ && (gIsBreakpointWorking || gIsDebuggerCompatible);
}
static void JNICALL
@@ -103,8 +105,14 @@
gIsSingleStepWorking = JNI_TRUE;
locStr = locationToString(jvmti_env, method, location);
- NSK_DISPLAY1("Single step event: %s\n", locStr);
- free(locStr);
+
+ if (locStr == NULL) {
+ NSK_DISPLAY0("Error: Single step event has no location\n");
+ gErrorHappened = JNI_TRUE;
+ } else {
+ NSK_DISPLAY1("Single step event: %s\n", locStr);
+ free(locStr);
+ }
NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, gJvmtiEnv, JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, NULL));
@@ -140,8 +148,13 @@
gIsBreakpointWorking = JNI_TRUE;
locStr = locationToString(jvmti_env, method, location);
- NSK_DISPLAY1("Breakpoint event at: %s\n", locStr);
- free(locStr);
+ if (locStr == NULL) {
+ NSK_DISPLAY0("Error: Breakpoint event has no location\n");
+ gErrorHappened = JNI_TRUE;
+ } else {
+ NSK_DISPLAY1("Breakpoint event at: %s\n", locStr);
+ free(locStr);
+ }
NSK_JVMTI_VERIFY(NSK_CPP_STUB3(ClearBreakpoint, jvmti_env, method, location));
NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, gJvmtiEnv, JVMTI_DISABLE, JVMTI_EVENT_BREAKPOINT, NULL));
--- a/test/hotspot/jtreg/vmTestbase/vm/mlvm/share/mlvmJvmtiUtils.c Tue Aug 28 17:03:16 2018 +0200
+++ b/test/hotspot/jtreg/vmTestbase/vm/mlvm/share/mlvmJvmtiUtils.c Wed Aug 22 09:33:18 2018 -0700
@@ -76,18 +76,31 @@
char * locationToString(jvmtiEnv * pJvmtiEnv, jmethodID method, jlocation location) {
struct MethodName * pMN;
- // gcc 7.3 claims that snprintf below can output between 6 and 531 bytes. Setting buffer size to 600.
- char r[600];
+ int len;
+ char * result;
+ const char * const format = "%s .%s :" JLONG_FORMAT;
pMN = getMethodName(pJvmtiEnv, method);
if ( ! pMN )
return strdup("NONE");
- snprintf(r, sizeof(r), "%s .%s :%lx", pMN->classSig, pMN->methodName, (long) location);
+ len = snprintf(NULL, 0, format, pMN->classSig, pMN->methodName, location) + 1;
+
+ if (len <= 0) {
+ free(pMN);
+ return NULL;
+ }
+
+ result = malloc(len);
+ if (result == NULL) {
+ free(pMN);
+ return NULL;
+ }
+
+ snprintf(result, len, format, pMN->classSig, pMN->methodName, location);
free(pMN);
-
- return strdup(r);
+ return result;
}
void * getTLS(jvmtiEnv * pJvmtiEnv, jthread thread, jsize sizeToAllocate) {