annotate test/hotspot/jtreg/vmTestbase/nsk/stress/jni/libjnistress001.cpp @ 53223:bd8c721954a4

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
author dholmes
date Thu, 25 Oct 2018 19:12:39 -0400
parents 79dc492c00ab
children 52be2c714a2f
rev   line source
lmesnik@50753 1 /*
lmesnik@50753 2 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
lmesnik@50753 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
lmesnik@50753 4 *
lmesnik@50753 5 * This code is free software; you can redistribute it and/or modify it
lmesnik@50753 6 * under the terms of the GNU General Public License version 2 only, as
lmesnik@50753 7 * published by the Free Software Foundation.
lmesnik@50753 8 *
lmesnik@50753 9 * This code is distributed in the hope that it will be useful, but WITHOUT
lmesnik@50753 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
lmesnik@50753 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
lmesnik@50753 12 * version 2 for more details (a copy is included in the LICENSE file that
lmesnik@50753 13 * accompanied this code).
lmesnik@50753 14 *
lmesnik@50753 15 * You should have received a copy of the GNU General Public License version
lmesnik@50753 16 * 2 along with this work; if not, write to the Free Software Foundation,
lmesnik@50753 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
lmesnik@50753 18 *
lmesnik@50753 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
lmesnik@50753 20 * or visit www.oracle.com if you need additional information or have any
lmesnik@50753 21 * questions.
lmesnik@50753 22 */
lmesnik@50753 23
lmesnik@50753 24 #include <jni.h>
lmesnik@50753 25 #include <stdio.h>
lmesnik@50753 26 /* Changed from strings.h to string.h for Windows. */
lmesnik@50753 27 #include <string.h>
lmesnik@50753 28 #include <stdlib.h>
lmesnik@50753 29 #include "jnihelper.h"
lmesnik@50753 30
iignatyev@52294 31 extern "C" {
iignatyev@52294 32
lmesnik@50753 33 #define DIGESTLENGTH 16
lmesnik@50753 34
lmesnik@50753 35 typedef struct {
lmesnik@50753 36 const char **str;
lmesnik@50753 37 char **checkstr;
lmesnik@50753 38 } CHAR_ARRAY;
lmesnik@50753 39
lmesnik@50753 40 typedef struct {
lmesnik@50753 41 const jchar **str;
lmesnik@50753 42 char **checkstr;
lmesnik@50753 43 int *size;
lmesnik@50753 44 } JCHAR_ARRAY;
lmesnik@50753 45
lmesnik@50753 46 JNIEXPORT jstring JNICALL
lmesnik@50753 47 Java_nsk_stress_jni_JNIter001_jnistress (JNIEnv *env, jobject jobj, jstring jstr,
lmesnik@50753 48 jint nstr, jint printperiod) {
lmesnik@50753 49
lmesnik@50753 50 int i,j;
lmesnik@50753 51 size_t k;
lmesnik@50753 52 static CHAR_ARRAY *element;
lmesnik@50753 53 unsigned char digest[DIGESTLENGTH];
lmesnik@50753 54 static int allocs=0;
lmesnik@50753 55 static size_t strsize=0;
lmesnik@50753 56 static unsigned int compared=1;
lmesnik@50753 57
lmesnik@50753 58 const char *clsName = "nsk/stress/jni/JNIter001";
lmesnik@50753 59 const char *name="setpass";
lmesnik@50753 60 const char *sig="(Z)V";
lmesnik@50753 61 const char *halt="halt";
lmesnik@50753 62 const char *haltSig="()V";
lmesnik@50753 63 jstring tmpstr;
lmesnik@50753 64 jclass clazz;
lmesnik@50753 65 jmethodID methodID;
lmesnik@50753 66
iignatyev@52294 67 env->MonitorEnter(jobj); CE
lmesnik@50753 68 if (!allocs) {
dholmes@53223 69 element = (CHAR_ARRAY *)c_malloc(env, sizeof(CHAR_ARRAY));
dholmes@53223 70 element->str = (const char **)c_malloc(env, nstr*sizeof(const char *));
dholmes@53223 71 element->checkstr = (char **)c_malloc(env, nstr*sizeof(char *));
lmesnik@50753 72 for (j=0;j<nstr;j++)
dholmes@53223 73 element->checkstr[j] = (char *)c_malloc(env, DIGESTLENGTH*sizeof(char));
lmesnik@50753 74 }
lmesnik@50753 75 for(j=0;j<DIGESTLENGTH;j++) {
lmesnik@50753 76 digest[j]=0;
lmesnik@50753 77 }
iignatyev@52294 78 element->str[allocs] = env->GetStringUTFChars(jstr,0); CE
lmesnik@50753 79 if (strlen(element->str[allocs]) !=
iignatyev@52294 80 (size_t) env->GetStringUTFLength(jstr))
lmesnik@50753 81 printf("Length is wrong in string No. %d\n",allocs);
lmesnik@50753 82 else
lmesnik@50753 83 strsize += strlen(element->str[allocs])+1;
lmesnik@50753 84 for (k = 0; k < strlen(element->str[allocs]); k++) {
lmesnik@50753 85 digest[k % DIGESTLENGTH] += element->str[allocs][k];
lmesnik@50753 86 }
lmesnik@50753 87 memcpy(element->checkstr[allocs],digest,DIGESTLENGTH);
lmesnik@50753 88 allocs++;
lmesnik@50753 89 if (allocs%printperiod==0) {
lmesnik@50753 90 printf("Check string for thread %s is ", element->str[allocs-1]);
lmesnik@50753 91 for (j=0;j<DIGESTLENGTH;j++)
lmesnik@50753 92 printf("%02x", digest[j]);
lmesnik@50753 93 printf("\n");
lmesnik@50753 94 }
lmesnik@50753 95 if (allocs==nstr) {
lmesnik@50753 96 printf("JNI UTF8 strings memory=%zd\n", strsize);
iignatyev@52294 97 tmpstr=env->NewStringUTF(element->str[allocs-1]); CE
lmesnik@50753 98 for (j=0; j<nstr; j++) {
lmesnik@50753 99
lmesnik@50753 100 for(i=0;i<DIGESTLENGTH;i++) {
lmesnik@50753 101 digest[i]=0;
lmesnik@50753 102 }
lmesnik@50753 103 for (k=0; k < strlen(element->str[j]); k++) {
lmesnik@50753 104 digest[k % DIGESTLENGTH] += element->str[j][k];
lmesnik@50753 105 }
lmesnik@50753 106 if (memcmp(digest,element->checkstr[j],DIGESTLENGTH)==0) {
iignatyev@52294 107 env->ReleaseStringUTFChars(jstr,element->str[j]); CE
lmesnik@50753 108 element->str[j] = NULL;
lmesnik@50753 109 element->checkstr[j] = NULL;
lmesnik@50753 110 }
lmesnik@50753 111 else {
lmesnik@50753 112 compared=0;
lmesnik@50753 113 printf("The element No. %d has been corrupted %s vs %s\n",j, element->str[j],element->checkstr[j]);
lmesnik@50753 114 printf("Digest string is %s [", element->str[j]);
lmesnik@50753 115 for (i=0;i<DIGESTLENGTH;i++)
lmesnik@50753 116 printf("ch[%d]=%02x", i, digest[i]);
lmesnik@50753 117 printf("Digest end\n");
lmesnik@50753 118 }
lmesnik@50753 119 }
lmesnik@50753 120 allocs=0;
lmesnik@50753 121 strsize = 0;
lmesnik@50753 122 for (j=0;j<nstr;j++)
lmesnik@50753 123 free(element->checkstr[j]);
lmesnik@50753 124 free(element->checkstr);
lmesnik@50753 125 free((void *)(element->str));
lmesnik@50753 126 free(element);
iignatyev@52294 127 clazz=env->FindClass(clsName); CE
lmesnik@50753 128 if (!compared) {
iignatyev@52294 129 methodID=env->GetStaticMethodID(clazz, name, sig); CE
iignatyev@52294 130 env->CallStaticVoidMethod(clazz, methodID, JNI_FALSE); CE
lmesnik@50753 131 }
iignatyev@52294 132 //methodID=env->GetStaticMethodID(clazz, halt, haltSig); CE
iignatyev@52294 133 //env->CallStaticVoidMethod(clazz, methodID); CE
lmesnik@50753 134 return(tmpstr);
lmesnik@50753 135 }
iignatyev@52294 136 env->MonitorExit(jobj); CE
iignatyev@52294 137 return (env->NewStringUTF(element->str[allocs-1]));
lmesnik@50753 138 }
lmesnik@50753 139
lmesnik@50753 140 JNIEXPORT jstring JNICALL
lmesnik@50753 141 Java_nsk_stress_jni_JNIter001_jnistress1(JNIEnv *env, jobject jobj, jstring jstr,
lmesnik@50753 142 jint nstr, jint printperiod) {
lmesnik@50753 143
lmesnik@50753 144 int i,j;
lmesnik@50753 145 static JCHAR_ARRAY *javachars;
lmesnik@50753 146 unsigned char digest[DIGESTLENGTH];
lmesnik@50753 147 static int index=0;
lmesnik@50753 148 static long len=0;
lmesnik@50753 149 static unsigned int equal=1;
lmesnik@50753 150 char *elem;
dholmes@53223 151 int elem_len = -1;
lmesnik@50753 152
lmesnik@50753 153 const char *clsName = "nsk/stress/jni/JNIter001";
lmesnik@50753 154 const char *name="setpass";
lmesnik@50753 155 const char *sig="(Z)V";
lmesnik@50753 156 const char *halt="halt";
lmesnik@50753 157 const char *haltSig="()V";
lmesnik@50753 158 jstring tmpstr;
lmesnik@50753 159 jclass clazz;
lmesnik@50753 160 jmethodID methodID;
lmesnik@50753 161
iignatyev@52294 162 env->MonitorEnter(jobj); CE
lmesnik@50753 163 if (!index) {
dholmes@53223 164 javachars = (JCHAR_ARRAY *)c_malloc(env, sizeof(JCHAR_ARRAY));
dholmes@53223 165 javachars->str = (const jchar **)c_malloc(env, nstr*sizeof(const jchar *));
dholmes@53223 166 javachars->checkstr = (char **)c_malloc(env, nstr*sizeof(char *));
dholmes@53223 167 javachars->size = (int *)c_malloc(env, nstr*sizeof(int));
lmesnik@50753 168 for (j=0;j<nstr;j++)
dholmes@53223 169 javachars->checkstr[j] = (char *)c_malloc(env, DIGESTLENGTH*sizeof(char));
lmesnik@50753 170 }
lmesnik@50753 171 for(j=0;j<DIGESTLENGTH;j++) {
lmesnik@50753 172 digest[j]=0;
lmesnik@50753 173 }
iignatyev@52294 174 javachars->str[index] = env->GetStringChars(jstr,0); CE
iignatyev@52294 175 javachars->size[index] = env->GetStringUTFLength(jstr); CE
dholmes@53223 176 elem_len = javachars->size[index];
dholmes@53223 177 len += elem_len;
dholmes@53223 178 elem = (char*) c_malloc(env, elem_len*sizeof(char));
dholmes@53223 179 for (j=0; j < elem_len; j++) {
lmesnik@50753 180 elem[j] = (char) javachars->str[index][j];
lmesnik@50753 181 }
dholmes@53223 182
lmesnik@50753 183 //memcpy(digest, elem, javachars->size[index]);
dholmes@53223 184 for(j=0;j<elem_len; j++) {
lmesnik@50753 185 digest[j % DIGESTLENGTH]+=elem[j];
lmesnik@50753 186 }
lmesnik@50753 187 memcpy(javachars->checkstr[index++],digest,DIGESTLENGTH);
lmesnik@50753 188 if (index%printperiod==0) {
dholmes@53223 189 printf("Check string sum for thread %.*s is ", elem_len, elem);
lmesnik@50753 190 for (j=0;j<DIGESTLENGTH;j++)
lmesnik@50753 191 printf("%02x", digest[j]);
lmesnik@50753 192 printf("\n");
lmesnik@50753 193 }
lmesnik@50753 194 free(elem);
lmesnik@50753 195 if (index==nstr) {
lmesnik@50753 196 printf("JNI Unicode strings memory=%ld\n",len);
dholmes@53223 197 tmpstr=env->NewString(javachars->str[index-1],elem_len); CE
lmesnik@50753 198 for (j=0; j<nstr; j++) {
dholmes@53223 199 elem = (char*) c_malloc(env, javachars->size[j]*sizeof(char));
lmesnik@50753 200 for (i=0; i < javachars->size[j]; i++) {
lmesnik@50753 201 elem[i] = (char) javachars->str[j][i];
lmesnik@50753 202 }
lmesnik@50753 203 //memcpy(digest, elem, javachars->size[j]);
lmesnik@50753 204 for(i=0;i<DIGESTLENGTH;i++) {
lmesnik@50753 205 digest[i]=0;
lmesnik@50753 206 }
lmesnik@50753 207 for(i=0;i<javachars->size[j]; i++) {
lmesnik@50753 208 digest[i % DIGESTLENGTH]+=elem[i];
lmesnik@50753 209 }
lmesnik@50753 210 free(elem);
lmesnik@50753 211 if (memcmp(digest,javachars->checkstr[j],javachars->size[j])==0) {
iignatyev@52294 212 env->ReleaseStringChars(jstr,javachars->str[j]); CE
lmesnik@50753 213 javachars->str[j] = NULL;
lmesnik@50753 214 javachars->checkstr[j] = NULL;
lmesnik@50753 215 javachars->size[j] = 0;
lmesnik@50753 216 }
lmesnik@50753 217 else {
lmesnik@50753 218 equal=0;
lmesnik@50753 219 printf("The Unicode element No. %d has been corrupted\n",j);
lmesnik@50753 220 for(i=0;i<DIGESTLENGTH;i++) {
lmesnik@50753 221 printf("digest[%d]=%02x checkstr[%d]=%02x\n",i,digest[i],i,javachars->checkstr[j][i]);
lmesnik@50753 222 }
lmesnik@50753 223 }
lmesnik@50753 224 }
lmesnik@50753 225 index=0;
lmesnik@50753 226 len = 0;
lmesnik@50753 227 for (j=0;j<nstr;j++)
lmesnik@50753 228 free(javachars->checkstr[j]);
lmesnik@50753 229 free(javachars->checkstr);
lmesnik@50753 230 free((void *)(javachars->str));
lmesnik@50753 231 free(javachars->size);
lmesnik@50753 232 free(javachars);
iignatyev@52294 233 clazz=env->FindClass(clsName); CE
lmesnik@50753 234 if (!equal) {
iignatyev@52294 235 methodID=env->GetStaticMethodID(clazz, name, sig); CE
iignatyev@52294 236 env->CallStaticVoidMethod(clazz, methodID, JNI_FALSE); CE
lmesnik@50753 237 }
iignatyev@52294 238 //methodID=env->GetStaticMethodID(clazz, halt, haltSig); CE
iignatyev@52294 239 //env->CallStaticVoidMethod(clazz, methodID); CE
lmesnik@50753 240 return(tmpstr);
lmesnik@50753 241 }
iignatyev@52294 242 env->MonitorExit(jobj); CE
dholmes@53223 243 return(env->NewString(javachars->str[index-1],elem_len));
lmesnik@50753 244 }
iignatyev@52294 245
iignatyev@52294 246 }