Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/back/ArrayReferenceImpl.c
38765 views
/*1* Copyright (c) 1998, 2005, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "util.h"26#include "ArrayReferenceImpl.h"27#include "inStream.h"28#include "outStream.h"2930static jboolean31length(PacketInputStream *in, PacketOutputStream *out)32{33JNIEnv *env = getEnv();34jsize arrayLength;3536jarray array = inStream_readArrayRef(env, in);37if (inStream_error(in)) {38return JNI_TRUE;39}4041arrayLength = JNI_FUNC_PTR(env,GetArrayLength)(env, array);42(void)outStream_writeInt(out, arrayLength);43return JNI_TRUE;44}4546static void *47newComponents(PacketOutputStream *out, jint length, size_t nbytes)48{49void *ptr = NULL;5051if ( length > 0 ) {52ptr = jvmtiAllocate(length*((jint)nbytes));53if ( ptr == NULL ) {54outStream_setError(out, JDWP_ERROR(OUT_OF_MEMORY));55} else {56(void)memset(ptr, 0, length*nbytes);57}58}59return ptr;60}6162static void63deleteComponents(void *ptr)64{65jvmtiDeallocate(ptr);66}6768static void69writeBooleanComponents(JNIEnv *env, PacketOutputStream *out,70jarray array, jint index, jint length)71{72jboolean *components;7374components = newComponents(out, length, sizeof(jboolean));75if (components != NULL) {76jint i;77JNI_FUNC_PTR(env,GetBooleanArrayRegion)(env, array, index, length, components);78for (i = 0; i < length; i++) {79(void)outStream_writeBoolean(out, components[i]);80}81deleteComponents(components);82}83}8485static void86writeByteComponents(JNIEnv *env, PacketOutputStream *out,87jarray array, jint index, jint length)88{89jbyte *components;9091components = newComponents(out, length, sizeof(jbyte));92if (components != NULL) {93jint i;94JNI_FUNC_PTR(env,GetByteArrayRegion)(env, array, index, length, components);95for (i = 0; i < length; i++) {96(void)outStream_writeByte(out, components[i]);97}98deleteComponents(components);99}100}101102static void103writeCharComponents(JNIEnv *env, PacketOutputStream *out,104jarray array, jint index, jint length)105{106jchar *components;107108components = newComponents(out, length, sizeof(jchar));109if (components != NULL) {110jint i;111JNI_FUNC_PTR(env,GetCharArrayRegion)(env, array, index, length, components);112for (i = 0; i < length; i++) {113(void)outStream_writeChar(out, components[i]);114}115deleteComponents(components);116}117}118119static void120writeShortComponents(JNIEnv *env, PacketOutputStream *out,121jarray array, jint index, jint length)122{123jshort *components;124125components = newComponents(out, length, sizeof(jshort));126if (components != NULL) {127jint i;128JNI_FUNC_PTR(env,GetShortArrayRegion)(env, array, index, length, components);129for (i = 0; i < length; i++) {130(void)outStream_writeShort(out, components[i]);131}132deleteComponents(components);133}134}135136static void137writeIntComponents(JNIEnv *env, PacketOutputStream *out,138jarray array, jint index, jint length)139{140jint *components;141142components = newComponents(out, length, sizeof(jint));143if (components != NULL) {144jint i;145JNI_FUNC_PTR(env,GetIntArrayRegion)(env, array, index, length, components);146for (i = 0; i < length; i++) {147(void)outStream_writeInt(out, components[i]);148}149deleteComponents(components);150}151}152153static void154writeLongComponents(JNIEnv *env, PacketOutputStream *out,155jarray array, jint index, jint length)156{157jlong *components;158159components = newComponents(out, length, sizeof(jlong));160if (components != NULL) {161jint i;162JNI_FUNC_PTR(env,GetLongArrayRegion)(env, array, index, length, components);163for (i = 0; i < length; i++) {164(void)outStream_writeLong(out, components[i]);165}166deleteComponents(components);167}168}169170static void171writeFloatComponents(JNIEnv *env, PacketOutputStream *out,172jarray array, jint index, jint length)173{174jfloat *components;175176components = newComponents(out, length, sizeof(jfloat));177if (components != NULL) {178jint i;179JNI_FUNC_PTR(env,GetFloatArrayRegion)(env, array, index, length, components);180for (i = 0; i < length; i++) {181(void)outStream_writeFloat(out, components[i]);182}183deleteComponents(components);184}185}186187static void188writeDoubleComponents(JNIEnv *env, PacketOutputStream *out,189jarray array, jint index, jint length)190{191jdouble *components;192193components = newComponents(out, length, sizeof(jdouble));194if (components != NULL) {195jint i;196JNI_FUNC_PTR(env,GetDoubleArrayRegion)(env, array, index, length, components);197for (i = 0; i < length; i++) {198(void)outStream_writeDouble(out, components[i]);199}200deleteComponents(components);201}202}203204static void205writeObjectComponents(JNIEnv *env, PacketOutputStream *out,206jarray array, jint index, jint length)207{208209WITH_LOCAL_REFS(env, length) {210211int i;212jobject component;213214for (i = 0; i < length; i++) {215component = JNI_FUNC_PTR(env,GetObjectArrayElement)(env, array, index + i);216if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {217/* cleared by caller */218break;219}220(void)outStream_writeByte(out, specificTypeKey(env, component));221(void)outStream_writeObjectRef(env, out, component);222}223224} END_WITH_LOCAL_REFS(env);225}226227static jboolean228getValues(PacketInputStream *in, PacketOutputStream *out)229{230JNIEnv *env = getEnv();231jint arrayLength;232jarray array;233jint index;234jint length;235236array = inStream_readArrayRef(env, in);237if (inStream_error(in)) {238return JNI_TRUE;239}240index = inStream_readInt(in);241if (inStream_error(in)) {242return JNI_TRUE;243}244length = inStream_readInt(in);245if (inStream_error(in)) {246return JNI_TRUE;247}248249arrayLength = JNI_FUNC_PTR(env,GetArrayLength)(env, array);250251if (length == -1) {252length = arrayLength - index;253}254255if ((index < 0) || (index > arrayLength - 1)) {256outStream_setError(out, JDWP_ERROR(INVALID_INDEX));257return JNI_TRUE;258}259260if ((length < 0) || (length + index > arrayLength)) {261outStream_setError(out, JDWP_ERROR(INVALID_LENGTH));262return JNI_TRUE;263}264265WITH_LOCAL_REFS(env, 1) {266267jclass arrayClass;268char *signature = NULL;269char *componentSignature;270jbyte typeKey;271jvmtiError error;272273arrayClass = JNI_FUNC_PTR(env,GetObjectClass)(env, array);274error = classSignature(arrayClass, &signature, NULL);275if (error != JVMTI_ERROR_NONE) {276goto err;277}278componentSignature = &signature[1];279typeKey = componentSignature[0];280281(void)outStream_writeByte(out, typeKey);282(void)outStream_writeInt(out, length);283284if (isObjectTag(typeKey)) {285writeObjectComponents(env, out, array, index, length);286} else {287switch (typeKey) {288case JDWP_TAG(BYTE):289writeByteComponents(env, out, array, index, length);290break;291292case JDWP_TAG(CHAR):293writeCharComponents(env, out, array, index, length);294break;295296case JDWP_TAG(FLOAT):297writeFloatComponents(env, out, array, index, length);298break;299300case JDWP_TAG(DOUBLE):301writeDoubleComponents(env, out, array, index, length);302break;303304case JDWP_TAG(INT):305writeIntComponents(env, out, array, index, length);306break;307308case JDWP_TAG(LONG):309writeLongComponents(env, out, array, index, length);310break;311312case JDWP_TAG(SHORT):313writeShortComponents(env, out, array, index, length);314break;315316case JDWP_TAG(BOOLEAN):317writeBooleanComponents(env, out, array, index, length);318break;319320default:321outStream_setError(out, JDWP_ERROR(INVALID_TAG));322break;323}324}325326jvmtiDeallocate(signature);327328err:;329330} END_WITH_LOCAL_REFS(env);331332if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {333outStream_setError(out, JDWP_ERROR(INTERNAL));334JNI_FUNC_PTR(env,ExceptionClear)(env);335}336337return JNI_TRUE;338}339340static jdwpError341readBooleanComponents(JNIEnv *env, PacketInputStream *in,342jarray array, int index, int length)343{344int i;345jboolean component;346347for (i = 0; (i < length) && !inStream_error(in); i++) {348component = inStream_readBoolean(in);349JNI_FUNC_PTR(env,SetBooleanArrayRegion)(env, array, index + i, 1, &component);350}351return inStream_error(in);352}353354static jdwpError355readByteComponents(JNIEnv *env, PacketInputStream *in,356jarray array, int index, int length)357{358int i;359jbyte component;360361for (i = 0; (i < length) && !inStream_error(in); i++) {362component = inStream_readByte(in);363JNI_FUNC_PTR(env,SetByteArrayRegion)(env, array, index + i, 1, &component);364}365return inStream_error(in);366}367368static jdwpError369readCharComponents(JNIEnv *env, PacketInputStream *in,370jarray array, int index, int length)371{372int i;373jchar component;374375for (i = 0; (i < length) && !inStream_error(in); i++) {376component = inStream_readChar(in);377JNI_FUNC_PTR(env,SetCharArrayRegion)(env, array, index + i, 1, &component);378}379return inStream_error(in);380}381382static jdwpError383readShortComponents(JNIEnv *env, PacketInputStream *in,384jarray array, int index, int length)385{386int i;387jshort component;388389for (i = 0; (i < length) && !inStream_error(in); i++) {390component = inStream_readShort(in);391JNI_FUNC_PTR(env,SetShortArrayRegion)(env, array, index + i, 1, &component);392}393return inStream_error(in);394}395396static jdwpError397readIntComponents(JNIEnv *env, PacketInputStream *in,398jarray array, int index, int length)399{400int i;401jint component;402403for (i = 0; (i < length) && !inStream_error(in); i++) {404component = inStream_readInt(in);405JNI_FUNC_PTR(env,SetIntArrayRegion)(env, array, index + i, 1, &component);406}407return inStream_error(in);408}409410static jdwpError411readLongComponents(JNIEnv *env, PacketInputStream *in,412jarray array, int index, int length)413{414int i;415jlong component;416417for (i = 0; (i < length) && !inStream_error(in); i++) {418component = inStream_readLong(in);419JNI_FUNC_PTR(env,SetLongArrayRegion)(env, array, index + i, 1, &component);420}421return inStream_error(in);422}423424static jdwpError425readFloatComponents(JNIEnv *env, PacketInputStream *in,426jarray array, int index, int length)427{428int i;429jfloat component;430431for (i = 0; (i < length) && !inStream_error(in); i++) {432component = inStream_readFloat(in);433JNI_FUNC_PTR(env,SetFloatArrayRegion)(env, array, index + i, 1, &component);434}435return inStream_error(in);436}437438static jdwpError439readDoubleComponents(JNIEnv *env, PacketInputStream *in,440jarray array, int index, int length)441{442int i;443jdouble component;444445for (i = 0; (i < length) && !inStream_error(in); i++) {446component = inStream_readDouble(in);447JNI_FUNC_PTR(env,SetDoubleArrayRegion)(env, array, index + i, 1, &component);448}449return inStream_error(in);450}451452453static jdwpError454readObjectComponents(JNIEnv *env, PacketInputStream *in,455jarray array, int index, int length)456/* char *componentSignature) */457{458int i;459460for (i = 0; i < length; i++) {461jobject object = inStream_readObjectRef(env, in);462463JNI_FUNC_PTR(env,SetObjectArrayElement)(env, array, index + i, object);464if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {465/* caller will clear */466break;467}468}469470return JDWP_ERROR(NONE);471}472473474static jboolean475setValues(PacketInputStream *in, PacketOutputStream *out)476{477JNIEnv *env = getEnv();478jdwpError serror = JDWP_ERROR(NONE);479int arrayLength;480jarray array;481jint index;482jint length;483484array = inStream_readArrayRef(env, in);485if (inStream_error(in)) {486return JNI_TRUE;487}488index = inStream_readInt(in);489if (inStream_error(in)) {490return JNI_TRUE;491}492length = inStream_readInt(in);493if (inStream_error(in)) {494return JNI_TRUE;495}496497arrayLength = JNI_FUNC_PTR(env,GetArrayLength)(env, array);498499if ((index < 0) || (index > arrayLength - 1)) {500outStream_setError(out, JDWP_ERROR(INVALID_INDEX));501return JNI_TRUE;502}503504if ((length < 0) || (length + index > arrayLength)) {505outStream_setError(out, JDWP_ERROR(INVALID_LENGTH));506return JNI_TRUE;507}508509WITH_LOCAL_REFS(env, 1) {510511jclass arrayClass;512char *signature = NULL;513char *componentSignature;514jvmtiError error;515516arrayClass = JNI_FUNC_PTR(env,GetObjectClass)(env, array);517error = classSignature(arrayClass, &signature, NULL);518if (error != JVMTI_ERROR_NONE) {519goto err;520}521componentSignature = &signature[1];522523switch (componentSignature[0]) {524case JDWP_TAG(OBJECT):525case JDWP_TAG(ARRAY):526serror = readObjectComponents(env, in, array, index, length);527break;528529case JDWP_TAG(BYTE):530serror = readByteComponents(env, in, array, index, length);531break;532533case JDWP_TAG(CHAR):534serror = readCharComponents(env, in, array, index, length);535break;536537case JDWP_TAG(FLOAT):538serror = readFloatComponents(env, in, array, index, length);539break;540541case JDWP_TAG(DOUBLE):542serror = readDoubleComponents(env, in, array, index, length);543break;544545case JDWP_TAG(INT):546serror = readIntComponents(env, in, array, index, length);547break;548549case JDWP_TAG(LONG):550serror = readLongComponents(env, in, array, index, length);551break;552553case JDWP_TAG(SHORT):554serror = readShortComponents(env, in, array, index, length);555break;556557case JDWP_TAG(BOOLEAN):558serror = readBooleanComponents(env, in, array, index, length);559break;560561default:562{563ERROR_MESSAGE(("Invalid array component signature: %s",564componentSignature));565EXIT_ERROR(AGENT_ERROR_INVALID_OBJECT,NULL);566}567break;568}569570jvmtiDeallocate(signature);571572err:;573574} END_WITH_LOCAL_REFS(env);575576if (JNI_FUNC_PTR(env,ExceptionOccurred)(env)) {577/*578* TO DO: Check exception type579*/580serror = JDWP_ERROR(TYPE_MISMATCH);581JNI_FUNC_PTR(env,ExceptionClear)(env);582}583584outStream_setError(out, serror);585return JNI_TRUE;586}587588589void *ArrayReference_Cmds[] = { (void *)0x3590,(void *)length591,(void *)getValues592,(void *)setValues};593594595