Path: blob/master/src/jdk.jdi/share/native/libdt_shmem/SharedMemoryConnection.c
41149 views
/*1* Copyright (c) 1999, 2017, 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*/24#include <string.h>25#include <stdlib.h>26#include <stddef.h>27#include <jni.h>28#include "SharedMemory.h"29#include "com_sun_tools_jdi_SharedMemoryConnection.h"30#include "jdwpTransport.h"31#include "shmemBase.h"32#include "sys.h"3334/*35* JNI interface to the shared memory transport. These JNI methods36* call the base shared memory support to do the real work.37*38* That is, this is the front-ends interface to our shared memory39* communication code.40*/4142/*43* Cached architecture44*/45static int byte_ordering_known;46static int is_big_endian;474849/*50* Returns 1 if big endian architecture51*/52static int isBigEndian() {53if (!byte_ordering_known) {54unsigned int i = 0xff000000;55if (((char *)(&i))[0] != 0) {56is_big_endian = 1;57} else {58is_big_endian = 0;59}60byte_ordering_known = 1;61}62return is_big_endian;63}6465/*66* Convert to big endian67*/68static jint intToBigInt(jint i) {69unsigned int b[4];70if (isBigEndian()) {71return i;72}73b[0] = (i >> 24) & 0xff;74b[1] = (i >> 16) & 0xff;75b[2] = (i >> 8) & 0xff;76b[3] = i & 0xff;7778/*79* It doesn't matter that jint is signed as we are or'ing80* and hence end up with the correct bits.81*/82return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0];83}8485/*86* Convert unsigned short to big endian87*/88static unsigned short shortToBigShort(unsigned short s) {89unsigned int b[2];90if (isBigEndian()) {91return s;92}93b[0] = (s >> 8) & 0xff;94b[1] = s & 0xff;95return (b[1] << 8) + b[0];96}9798/*99* Create a byte[] from a packet struct. All data in the byte array100* is JDWP packet suitable for wire transmission. That is, all fields,101* and data are in big-endian format as required by the JDWP102* specification.103*/104static jbyteArray105packetToByteArray(JNIEnv *env, jdwpPacket *str)106{107jbyteArray array;108jsize data_length;109jint total_length;110jint tmpInt;111112total_length = str->type.cmd.len;113data_length = total_length - JDWP_HEADER_SIZE;114115/* total packet length is header + data */116array = (*env)->NewByteArray(env, total_length);117if ((*env)->ExceptionOccurred(env)) {118return NULL;119}120121/* First 4 bytes of packet are the length (in big endian format) */122tmpInt = intToBigInt((unsigned int)total_length);123(*env)->SetByteArrayRegion(env, array, 0, 4, (const jbyte *)&tmpInt);124125/* Next 4 bytes are the id field */126tmpInt = intToBigInt(str->type.cmd.id);127(*env)->SetByteArrayRegion(env, array, 4, 4, (const jbyte *)&tmpInt);128129/* next byte is the flags */130(*env)->SetByteArrayRegion(env, array, 8, 1, (const jbyte *)&(str->type.cmd.flags));131132/* next two bytes are either the error code or the command set/command */133if (str->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {134short s = shortToBigShort(str->type.reply.errorCode);135(*env)->SetByteArrayRegion(env, array, 9, 2, (const jbyte *)&(s));136} else {137(*env)->SetByteArrayRegion(env, array, 9, 1, (const jbyte *)&(str->type.cmd.cmdSet));138(*env)->SetByteArrayRegion(env, array, 10, 1, (const jbyte *)&(str->type.cmd.cmd));139}140141/* finally the data */142143if (data_length > 0) {144(*env)->SetByteArrayRegion(env, array, JDWP_HEADER_SIZE,145data_length, str->type.cmd.data);146if ((*env)->ExceptionOccurred(env)) {147return NULL;148}149}150151return array;152}153154/*155* Fill a packet struct from a byte array. The byte array is a156* JDWP packet suitable for wire transmission. That is, all fields,157* and data are in big-endian format as required by the JDWP158* specification. We thus need to convert the fields from big159* endian to the platform endian.160*161* The jbyteArray provided to this function is assumed to162* of a length than is equal or greater than the length of163* the JDWP packet that is contains.164*/165static void166byteArrayToPacket(JNIEnv *env, jbyteArray b, jdwpPacket *str)167{168jsize total_length, data_length;169jbyte *data;170unsigned char pktHeader[JDWP_HEADER_SIZE];171172/*173* Get the packet header174*/175(*env)->GetByteArrayRegion(env, b, 0, sizeof(pktHeader), pktHeader);176if ((*env)->ExceptionOccurred(env)) {177/* b shorter than sizeof(pktHeader) */178return;179}180181total_length = (int)pktHeader[3] | ((int)pktHeader[2] << 8) |182((int)pktHeader[1] << 16) | ((int)pktHeader[0] << 24);183184if (total_length < sizeof(pktHeader)) {185throwException(env, "java/lang/IllegalArgumentException",186"JDWP header is incorrect");187return;188}189190/*191* The id field is in big endian (also errorCode field in the case192* of reply packets).193*/194str->type.cmd.id = (int)pktHeader[7] | ((int)pktHeader[6] << 8) |195((int)pktHeader[5] << 16) | ((int)pktHeader[4] << 24);196197str->type.cmd.flags = (jbyte)pktHeader[8];198199if (str->type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {200str->type.reply.errorCode = (int)pktHeader[9] + ((int)pktHeader[10] << 8);201} else {202/* command packet */203str->type.cmd.cmdSet = (jbyte)pktHeader[9];204str->type.cmd.cmd = (jbyte)pktHeader[10];205}206207/*208* The length of the JDWP packet is sizeof(pktHeader) + data209*/210data_length = total_length - sizeof(pktHeader);211212if (data_length == 0) {213data = NULL;214} else {215data = malloc(data_length);216if (data == NULL) {217throwException(env, "java/lang/OutOfMemoryError",218"Unable to allocate command data buffer");219return;220}221222(*env)->GetByteArrayRegion(env, b, sizeof(pktHeader), /*sizeof(CmdPacket)+4*/ data_length, data);223if ((*env)->ExceptionOccurred(env)) {224free(data);225return;226}227}228229str->type.cmd.len = total_length;230str->type.cmd.data = data;231}232233static void234freePacketData(jdwpPacket *packet)235{236if (packet->type.cmd.len > 0) {237free(packet->type.cmd.data);238}239}240241/*242* Class: com_sun_tools_jdi_SharedMemoryConnection243* Method: close0244* Signature: (J)V245*/246JNIEXPORT void JNICALL Java_com_sun_tools_jdi_SharedMemoryConnection_close0247(JNIEnv *env, jobject thisObject, jlong id)248{249SharedMemoryConnection *connection = ID_TO_CONNECTION(id);250shmemBase_closeConnection(connection);251}252253/*254* Class: com_sun_tools_jdi_SharedMemoryConnection255* Method: receiveByte0256* Signature: (J)B257*/258JNIEXPORT jbyte JNICALL Java_com_sun_tools_jdi_SharedMemoryConnection_receiveByte0259(JNIEnv *env, jobject thisObject, jlong id)260{261SharedMemoryConnection *connection = ID_TO_CONNECTION(id);262jbyte b = 0;263jint rc;264265rc = shmemBase_receiveByte(connection, &b);266if (rc != SYS_OK) {267throwShmemException(env, "shmemBase_receiveByte failed", rc);268}269270return b;271}272273/*274* Class: com_sun_tools_jdi_SharedMemoryConnection275* Method: receivePacket0276* Signature: (JLcom/sun/tools/jdi/Packet;)V277*/278JNIEXPORT jbyteArray JNICALL Java_com_sun_tools_jdi_SharedMemoryConnection_receivePacket0279(JNIEnv *env, jobject thisObject, jlong id)280{281SharedMemoryConnection *connection = ID_TO_CONNECTION(id);282jdwpPacket packet;283jint rc;284285rc = shmemBase_receivePacket(connection, &packet);286if (rc != SYS_OK) {287throwShmemException(env, "shmemBase_receivePacket failed", rc);288return NULL;289} else {290jbyteArray array = packetToByteArray(env, &packet);291292/* Free the packet even if there was an exception above */293freePacketData(&packet);294return array;295}296}297298/*299* Class: com_sun_tools_jdi_SharedMemoryConnection300* Method: sendByte0301* Signature: (JB)V302*/303JNIEXPORT void JNICALL Java_com_sun_tools_jdi_SharedMemoryConnection_sendByte0304(JNIEnv *env, jobject thisObject, jlong id, jbyte b)305{306SharedMemoryConnection *connection = ID_TO_CONNECTION(id);307jint rc;308309rc = shmemBase_sendByte(connection, b);310if (rc != SYS_OK) {311throwShmemException(env, "shmemBase_sendByte failed", rc);312}313}314315/*316* Class: com_sun_tools_jdi_SharedMemoryConnection317* Method: sendPacket0318* Signature: (JLcom/sun/tools/jdi/Packet;)V319*/320JNIEXPORT void JNICALL Java_com_sun_tools_jdi_SharedMemoryConnection_sendPacket0321(JNIEnv *env, jobject thisObject, jlong id, jbyteArray b)322{323SharedMemoryConnection *connection = ID_TO_CONNECTION(id);324jdwpPacket packet;325jint rc;326327byteArrayToPacket(env, b, &packet);328if ((*env)->ExceptionOccurred(env)) {329return;330}331332rc = shmemBase_sendPacket(connection, &packet);333if (rc != SYS_OK) {334throwShmemException(env, "shmemBase_sendPacket failed", rc);335}336freePacketData(&packet);337}338339340