1 /*
   2  * Copyright (c) 1998, 2017, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "util.h"
  27 #include "transport.h"
  28 #include "debugLoop.h"
  29 #include "debugDispatch.h"
  30 #include "standardHandlers.h"
  31 #include "inStream.h"
  32 #include "outStream.h"
  33 #include "threadControl.h"
  34 
  35 
  36 static void JNICALL reader(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg);
  37 static void enqueue(jdwpPacket *p);
  38 static jboolean dequeue(jdwpPacket *p);
  39 static void notifyTransportError(void);
  40 
  41 struct PacketList {
  42     jdwpPacket packet;
  43     struct PacketList *next;
  44 };
  45 
  46 static volatile struct PacketList *cmdQueue;
  47 static jrawMonitorID cmdQueueLock;
  48 static jrawMonitorID vmDeathLock;
  49 static jboolean transportError;
  50 
  51 static jboolean
  52 lastCommand(jdwpCmdPacket *cmd)
  53 {
  54     if ((cmd->cmdSet == JDWP_COMMAND_SET(VirtualMachine)) &&
  55         ((cmd->cmd == JDWP_COMMAND(VirtualMachine, Dispose)) ||
  56          (cmd->cmd == JDWP_COMMAND(VirtualMachine, Exit)))) {
  57         return JNI_TRUE;
  58     } else {
  59         return JNI_FALSE;
  60     }
  61 }
  62 
  63 void
  64 debugLoop_initialize(void)
  65 {
  66     vmDeathLock = debugMonitorCreate("JDWP VM_DEATH Lock");
  67 }
  68 
  69 void
  70 debugLoop_sync(void)
  71 {
  72     debugMonitorEnter(vmDeathLock);
  73     debugMonitorExit(vmDeathLock);
  74 }
  75 
  76 /*
  77  * This is where all the work gets done.
  78  */
  79 
  80 void
  81 debugLoop_run(void)
  82 {
  83     jboolean shouldListen;
  84     jdwpPacket p;
  85     jvmtiStartFunction func;
  86     JNIEnv *env = getEnv();
  87 
  88     /* Initialize all statics */
  89     /* We may be starting a new connection after an error */
  90     cmdQueue = NULL;
  91     cmdQueueLock = debugMonitorCreate("JDWP Command Queue Lock");
  92     transportError = JNI_FALSE;
  93 
  94     shouldListen = JNI_TRUE;
  95 
  96     func = &reader;
  97     (void)spawnNewThread(func, NULL, "JDWP Command Reader");
  98 
  99     standardHandlers_onConnect();
 100     threadControl_onConnect();
 101 
 102     /* Okay, start reading cmds! */
 103     while (shouldListen) {
 104         if (!dequeue(&p)) {
 105             break;
 106         }
 107 
 108         if (p.type.cmd.flags & JDWPTRANSPORT_FLAGS_REPLY) {
 109             /*
 110              * Its a reply packet.
 111              */
 112            continue;
 113         } else {
 114             /*
 115              * Its a cmd packet.
 116              */
 117             jdwpCmdPacket *cmd = &p.type.cmd;
 118             PacketInputStream in;
 119             PacketOutputStream out;
 120             CommandHandler func;
 121 
 122             /* Should reply be sent to sender.
 123              * For error handling, assume yes, since
 124              * only VM/exit does not reply
 125              */
 126             jboolean replyToSender = JNI_TRUE;
 127 
 128             /*
 129              * For all commands we hold the vmDeathLock
 130              * while executing and replying to the command. This ensures
 131              * that a command after VM_DEATH will be allowed to complete
 132              * before the thread posting the VM_DEATH continues VM
 133              * termination.
 134              */
 135             debugMonitorEnter(vmDeathLock);
 136 
 137             /* Initialize the input and output streams */
 138             inStream_init(&in, p);
 139             outStream_initReply(&out, inStream_id(&in));
 140 
 141             LOG_MISC(("Command set %d, command %d", cmd->cmdSet, cmd->cmd));
 142 
 143             func = debugDispatch_getHandler(cmd->cmdSet,cmd->cmd);
 144             if (func == NULL) {
 145                 /* we've never heard of this, so I guess we
 146                  * haven't implemented it.
 147                  * Handle gracefully for future expansion
 148                  * and platform / vendor expansion.
 149                  */
 150                 outStream_setError(&out, JDWP_ERROR(NOT_IMPLEMENTED));
 151             } else if (gdata->vmDead &&
 152              ((cmd->cmdSet) != JDWP_COMMAND_SET(VirtualMachine))) {
 153                 /* Protect the VM from calls while dead.
 154                  * VirtualMachine cmdSet quietly ignores some cmds
 155                  * after VM death, so, it sends it's own errors.
 156                  */
 157                 outStream_setError(&out, JDWP_ERROR(VM_DEAD));
 158             } else {
 159               /* Provide 64 localrefs by default. */
 160               /* fiber fixme: Now that this default set of localrefs is in place, we can remove a
 161                * bunch of WITH_LOCAL_REFS/END_WITH_LOCAL_REFS blocks. The only ones needed are
 162                * ones that could potentially go over 64, likes ones within loops. Note this only
 163                * refers to command handlers called from here, not all uses of WITH_LOCAL_REFS. */
 164                 WITH_LOCAL_REFS(env, 64) {
 165                     /* Call the command handler */
 166                     replyToSender = func(&in, &out);
 167                 } END_WITH_LOCAL_REFS(env);
 168             }
 169 
 170             /* Reply to the sender */
 171             if (replyToSender) {
 172                 if (inStream_error(&in)) {
 173                     outStream_setError(&out, inStream_error(&in));
 174                 }
 175                 outStream_sendReply(&out);
 176             }
 177 
 178             /*
 179              * Release the vmDeathLock as the reply has been posted.
 180              */
 181             debugMonitorExit(vmDeathLock);
 182 
 183             inStream_destroy(&in);
 184             outStream_destroy(&out);
 185 
 186             shouldListen = !lastCommand(cmd);
 187         }
 188     }
 189     threadControl_onDisconnect();
 190     standardHandlers_onDisconnect();
 191 
 192     /*
 193      * Cut off the transport immediately. This has the effect of
 194      * cutting off any events that the eventHelper thread might
 195      * be trying to send.
 196      */
 197     transport_close();
 198     debugMonitorDestroy(cmdQueueLock);
 199 
 200     /* Reset for a new connection to this VM if it's still alive */
 201     if ( ! gdata->vmDead ) {
 202         debugInit_reset(getEnv());
 203     }
 204 }
 205 
 206 /* Command reader */
 207 static void JNICALL
 208 reader(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg)
 209 {
 210     jdwpPacket packet;
 211     jdwpCmdPacket *cmd;
 212     jboolean shouldListen = JNI_TRUE;
 213 
 214     LOG_MISC(("Begin reader thread"));
 215 
 216     while (shouldListen) {
 217         jint rc;
 218 
 219         rc = transport_receivePacket(&packet);
 220 
 221         /* I/O error or EOF */
 222         if (rc != 0 || (rc == 0 && packet.type.cmd.len == 0)) {
 223             shouldListen = JNI_FALSE;
 224             notifyTransportError();
 225         } else if (packet.type.cmd.flags != JDWPTRANSPORT_FLAGS_NONE) {
 226             /*
 227              * Close the connection when we get a jdwpCmdPacket with an
 228              * invalid flags field value. This is a protocol violation
 229              * so we drop the connection. Also this could be a web
 230              * browser generating an HTTP request that passes the JDWP
 231              * handshake. HTTP requests requires that everything be in
 232              * the ASCII printable range so a flags value of
 233              * JDWPTRANSPORT_FLAGS_NONE(0) cannot be generated via HTTP.
 234              */
 235             ERROR_MESSAGE(("Received jdwpPacket with flags != 0x%d (actual=0x%x) when a jdwpCmdPacket was expected.",
 236                            JDWPTRANSPORT_FLAGS_NONE, packet.type.cmd.flags));
 237             shouldListen = JNI_FALSE;
 238             notifyTransportError();
 239         } else {
 240             cmd = &packet.type.cmd;
 241 
 242             LOG_MISC(("Command set %d, command %d", cmd->cmdSet, cmd->cmd));
 243 
 244             /*
 245              * FIXME! We need to deal with high priority
 246              * packets and queue flushes!
 247              */
 248             enqueue(&packet);
 249 
 250             shouldListen = !lastCommand(cmd);
 251         }
 252     }
 253     LOG_MISC(("End reader thread"));
 254 }
 255 
 256 /*
 257  * The current system for queueing packets is highly
 258  * inefficient, and should be rewritten! It'd be nice
 259  * to avoid any additional memory allocations.
 260  */
 261 
 262 static void
 263 enqueue(jdwpPacket *packet)
 264 {
 265     struct PacketList *pL;
 266     struct PacketList *walker;
 267 
 268     pL = jvmtiAllocate((jint)sizeof(struct PacketList));
 269     if (pL == NULL) {
 270         EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"packet list");
 271     }
 272 
 273     pL->packet = *packet;
 274     pL->next = NULL;
 275 
 276     debugMonitorEnter(cmdQueueLock);
 277 
 278     if (cmdQueue == NULL) {
 279         cmdQueue = pL;
 280         debugMonitorNotify(cmdQueueLock);
 281     } else {
 282         walker = (struct PacketList *)cmdQueue;
 283         while (walker->next != NULL)
 284             walker = walker->next;
 285 
 286         walker->next = pL;
 287     }
 288 
 289     debugMonitorExit(cmdQueueLock);
 290 }
 291 
 292 static jboolean
 293 dequeue(jdwpPacket *packet) {
 294     struct PacketList *node = NULL;
 295 
 296     debugMonitorEnter(cmdQueueLock);
 297 
 298     while (!transportError && (cmdQueue == NULL)) {
 299         debugMonitorWait(cmdQueueLock);
 300     }
 301 
 302     if (cmdQueue != NULL) {
 303         node = (struct PacketList *)cmdQueue;
 304         cmdQueue = node->next;
 305     }
 306     debugMonitorExit(cmdQueueLock);
 307 
 308     if (node != NULL) {
 309         *packet = node->packet;
 310         jvmtiDeallocate(node);
 311     }
 312     return (node != NULL);
 313 }
 314 
 315 static void
 316 notifyTransportError(void) {
 317     debugMonitorEnter(cmdQueueLock);
 318     transportError = JNI_TRUE;
 319     debugMonitorNotify(cmdQueueLock);
 320     debugMonitorExit(cmdQueueLock);
 321 }