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