8210242: vmTestbase/nsk/stress/jni/jnistress001.java crashes with EXCEPTION_ACCESS_VIOLATION on windows-x86
Summary: Non-NUL-terminated string was passed to %s - use %.*s to specify the actual length.
Reviewed-by: lfoltan, hseigel
--- a/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/jnihelper.h Thu Oct 25 14:23:26 2018 -0700
+++ b/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/jnihelper.h Thu Oct 25 19:12:39 2018 -0400
@@ -21,6 +21,16 @@
* questions.
*/
+#include <stdlib.h>
+
+// checked malloc to trap OOM conditions
+static void* c_malloc(JNIEnv* env, size_t size) {
+ void* ret = malloc(size);
+ if (ret == NULL)
+ env->FatalError("malloc failed");
+ return ret;
+}
+
// Asserts every exception as fatal one
#define CE {\
if (env->ExceptionOccurred())\
--- a/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/libjnistress001.cpp Thu Oct 25 14:23:26 2018 -0700
+++ b/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/libjnistress001.cpp Thu Oct 25 19:12:39 2018 -0400
@@ -66,11 +66,11 @@
env->MonitorEnter(jobj); CE
if (!allocs) {
- element = (CHAR_ARRAY *)malloc(sizeof(CHAR_ARRAY));
- element->str = (const char **)malloc(nstr*sizeof(const char *));
- element->checkstr = (char **)malloc(nstr*sizeof(char *));
+ element = (CHAR_ARRAY *)c_malloc(env, sizeof(CHAR_ARRAY));
+ element->str = (const char **)c_malloc(env, nstr*sizeof(const char *));
+ element->checkstr = (char **)c_malloc(env, nstr*sizeof(char *));
for (j=0;j<nstr;j++)
- element->checkstr[j] = (char *)malloc(DIGESTLENGTH*sizeof(char));
+ element->checkstr[j] = (char *)c_malloc(env, DIGESTLENGTH*sizeof(char));
}
for(j=0;j<DIGESTLENGTH;j++) {
digest[j]=0;
@@ -148,6 +148,7 @@
static long len=0;
static unsigned int equal=1;
char *elem;
+ int elem_len = -1;
const char *clsName = "nsk/stress/jni/JNIter001";
const char *name="setpass";
@@ -160,30 +161,32 @@
env->MonitorEnter(jobj); CE
if (!index) {
- javachars = (JCHAR_ARRAY *)malloc(sizeof(JCHAR_ARRAY));
- javachars->str = (const jchar **)malloc(nstr*sizeof(const jchar *));
- javachars->checkstr = (char **)malloc(nstr*sizeof(char *));
- javachars->size = (int *)malloc(nstr*sizeof(int));
+ javachars = (JCHAR_ARRAY *)c_malloc(env, sizeof(JCHAR_ARRAY));
+ javachars->str = (const jchar **)c_malloc(env, nstr*sizeof(const jchar *));
+ javachars->checkstr = (char **)c_malloc(env, nstr*sizeof(char *));
+ javachars->size = (int *)c_malloc(env, nstr*sizeof(int));
for (j=0;j<nstr;j++)
- javachars->checkstr[j] = (char *)malloc(DIGESTLENGTH*sizeof(char));
+ javachars->checkstr[j] = (char *)c_malloc(env, DIGESTLENGTH*sizeof(char));
}
for(j=0;j<DIGESTLENGTH;j++) {
digest[j]=0;
}
javachars->str[index] = env->GetStringChars(jstr,0); CE
javachars->size[index] = env->GetStringUTFLength(jstr); CE
- len += javachars->size[index];
- elem = (char*) malloc(javachars->size[index]*sizeof(char));
- for (j=0; j < javachars->size[index]; j++) {
+ elem_len = javachars->size[index];
+ len += elem_len;
+ elem = (char*) c_malloc(env, elem_len*sizeof(char));
+ for (j=0; j < elem_len; j++) {
elem[j] = (char) javachars->str[index][j];
}
+
//memcpy(digest, elem, javachars->size[index]);
- for(j=0;j<javachars->size[index]; j++) {
+ for(j=0;j<elem_len; j++) {
digest[j % DIGESTLENGTH]+=elem[j];
}
memcpy(javachars->checkstr[index++],digest,DIGESTLENGTH);
if (index%printperiod==0) {
- printf("Check string sum for thread %s is ",elem);
+ printf("Check string sum for thread %.*s is ", elem_len, elem);
for (j=0;j<DIGESTLENGTH;j++)
printf("%02x", digest[j]);
printf("\n");
@@ -191,9 +194,9 @@
free(elem);
if (index==nstr) {
printf("JNI Unicode strings memory=%ld\n",len);
- tmpstr=env->NewString(javachars->str[index-1],javachars->size[index-1]); CE
+ tmpstr=env->NewString(javachars->str[index-1],elem_len); CE
for (j=0; j<nstr; j++) {
- elem = (char*) malloc(javachars->size[j]*sizeof(char));
+ elem = (char*) c_malloc(env, javachars->size[j]*sizeof(char));
for (i=0; i < javachars->size[j]; i++) {
elem[i] = (char) javachars->str[j][i];
}
@@ -237,7 +240,7 @@
return(tmpstr);
}
env->MonitorExit(jobj); CE
- return(env->NewString(javachars->str[index-1],javachars->size[index-1]));
+ return(env->NewString(javachars->str[index-1],elem_len));
}
}