Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/stress/jni/libjnistress004.cpp
41155 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <jni.h>24#include <stdio.h>25#include <stdlib.h>26#include <string.h>27#include "jnihelper.h"2829extern "C" {3031#define DIGESTLENGTH 163233JNIEXPORT jcharArray JNICALL34Java_nsk_stress_jni_JNIter004_CheckSum (JNIEnv *env, jobject jobj, jstring jstr) {3536unsigned char digest[DIGESTLENGTH];37jchar *tmp;38static jint upper = 0;39jcharArray jArr;40int i;41const jchar *critstr;42char *str;43jint len=env->GetStringUTFLength(jstr); CE4445for (i=0;i<DIGESTLENGTH;i++) {46digest[i]=0;47}48str=(char *)malloc(len*sizeof(char));49/* const char *threadName=env->GetStringUTFChars(jstr, 0); */5051env->MonitorEnter(jobj); CE52if (upper == 0) tmp = (jchar *) malloc(DIGESTLENGTH*sizeof(char));53if (env->ExceptionOccurred()) {54env->ExceptionDescribe();55env->ExceptionClear();56}57critstr=env->GetStringCritical(jstr, 0); CE58for (i=0;i<len;i++)59str[i] = (char) critstr[i];60env->ReleaseStringCritical(jstr,critstr); CE61for (i=0;i<len;i++) {62digest[i % DIGESTLENGTH]+=str[i];63}64free(str);6566if (env->ExceptionOccurred()) {67env->ExceptionDescribe();68env->ExceptionClear();69}70memcpy(tmp,digest,DIGESTLENGTH);71jArr=env->NewCharArray(DIGESTLENGTH/sizeof(jchar)); CE72len=env->GetArrayLength(jArr); CE73env->SetCharArrayRegion(jArr,0,len,tmp); CE74/* ++upper; */75env->MonitorExit(jobj); CE76return jArr;77}7879JNIEXPORT jboolean JNICALL80Java_nsk_stress_jni_JNIter004_CheckCompare (JNIEnv *env, jobject jobj, jstring jstr,81jcharArray cArr, jint limit) {8283unsigned char digest[DIGESTLENGTH];84jchar *tmp;85/* jcharArray jArr; */86const jchar *critstr;87jint strlen;88char *str;89jboolean ret=JNI_TRUE;90int i;91static jint upper = 0;92jint len;93jchar *ch;9495for (i=0;i<DIGESTLENGTH;i++) {96digest[i]=0;97}98strlen = env->GetStringUTFLength(jstr); CE99str = (char *)malloc(strlen*sizeof(char));100101len =env->GetArrayLength(cArr); CE102103env->MonitorEnter(jobj); CE104if (upper>limit) {105env->MonitorExit(jobj); CE106return JNI_FALSE;107}108tmp=(jchar *)malloc(DIGESTLENGTH*sizeof(char));109if (env->ExceptionOccurred()) {110env->ExceptionDescribe();111env->ExceptionClear();112}113critstr=env->GetStringCritical(jstr, 0); CE114for (i=0;i<strlen;i++)115str[i] = (char) critstr[i];116env->ReleaseStringCritical(jstr,critstr); CE117for (i=0;i<strlen; i++) {118digest[i % DIGESTLENGTH]+=str[i % DIGESTLENGTH];119}120121free(str);122123if (env->ExceptionOccurred()) {124env->ExceptionDescribe();125env->ExceptionClear();126}127memcpy(tmp,digest,DIGESTLENGTH);128129/* jArr=env->NewCharArray(DIGESTLENGTH/sizeof(jchar)); */130/* len=env->GetArrayLength(jArr); */131/* env->SetCharArrayRegion(jArr,0,len,tmp); */132/* ++upper; */133/* env->MonitorExit(jobj); */134135/* Compare */136/* env->MonitorEnter(jobj); */137138ch=(jchar *)env->GetPrimitiveArrayCritical(cArr,0); CE139140printf("Comparing: ");141for (i=0;i<len;i++)142if (ch[i] != tmp[i]) {143printf("Error in %d\n",i);144printf("ch[%d]=%02x tmp[%d]=%02x\n",i,ch[i],i,tmp[i]);145ret=JNI_FALSE;146}147else {148printf("ch[%d]=%02x tmp[%d]=%02x\n",i,ch[i],i,tmp[i]);149}150printf("\n");151env->ReleasePrimitiveArrayCritical(cArr,ch,0); CE152++upper;153if (!(upper % 500))154fprintf(stderr,"There are %d elements now.\n", upper);155if (upper == limit) {156jclass clazz;157jmethodID methodID;158char *name = (char*) "halt";159char *sig = (char*) "()V";160161clazz=env->GetObjectClass(jobj); CE162methodID=env->GetStaticMethodID(clazz, name, sig); CE163env->CallStaticVoidMethod(clazz, methodID); CE164free(tmp);165ret=JNI_TRUE;166}167env->MonitorExit(jobj); CE168return ret;169}170171}172173174