1 /*
   2  * Copyright (c) 2004, 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 <stdlib.h>
  25 #include <string.h>
  26 #include "jni_tools.h"
  27 #include "agent_common.h"
  28 #include "jvmti_tools.h"
  29 
  30 #define PASSED 0
  31 #define STATUS_FAILED 2
  32 
  33 extern "C" {
  34 
  35 /* ========================================================================== */
  36 
  37 /* scaffold objects */
  38 static jlong timeout = 0;
  39 
  40 /* test objects */
  41 static jobject threadDeath = NULL;
  42 static jthread runningThread = NULL;
  43 static jthread waitingThread = NULL;
  44 static jthread sleepingThread = NULL;
  45 
  46 /* ========================================================================== */
  47 
  48 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) {
  49     const char* RUNNING_THREAD_NAME = "DebuggeeRunningThread";
  50     const char* WAITING_THREAD_NAME = "DebuggeeWaitingThread";
  51     const char* SLEEPING_THREAD_NAME = "DebuggeeSleepingThread";
  52     const char* THREAD_DEATH_CLASS_NAME = "java/lang/ThreadDeath";
  53     const char* THREAD_DEATH_CTOR_NAME = "<init>";
  54     const char* THREAD_DEATH_CTOR_SIGNATURE = "()V";
  55     jvmtiThreadInfo info;
  56     jthread *threads = NULL;
  57     jint threads_count = 0;
  58     jclass cls = NULL;
  59     jmethodID ctor = NULL;
  60     int i;
  61 
  62     NSK_DISPLAY0("Prepare: find tested threads\n");
  63 
  64     /* get all live threads */
  65     if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threads_count, &threads)))
  66         return NSK_FALSE;
  67 
  68     if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
  69         return NSK_FALSE;
  70 
  71     /* find tested thread */
  72     for (i = 0; i < threads_count; i++) {
  73         if (!NSK_VERIFY(threads[i] != NULL))
  74             return NSK_FALSE;
  75 
  76         /* get thread information */
  77         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(threads[i], &info)))
  78             return NSK_FALSE;
  79 
  80         NSK_DISPLAY3("    thread #%d (%s): %p\n", i, info.name, threads[i]);
  81 
  82         /* find by name */
  83         if (info.name != NULL) {
  84             if (strcmp(info.name, RUNNING_THREAD_NAME) == 0) {
  85                 runningThread = threads[i];
  86             } else if (strcmp(info.name, WAITING_THREAD_NAME) == 0) {
  87                 waitingThread = threads[i];
  88             } else if (strcmp(info.name, SLEEPING_THREAD_NAME) == 0) {
  89                 sleepingThread = threads[i];
  90             }
  91         }
  92     }
  93 
  94     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads)))
  95         return NSK_FALSE;
  96 
  97     NSK_DISPLAY0("Prepare: create new instance of ThreadDeath exception\n");
  98 
  99     if (!NSK_JNI_VERIFY(jni, (cls = jni->FindClass(THREAD_DEATH_CLASS_NAME)) != NULL))
 100         return NSK_FALSE;
 101 
 102     if (!NSK_JNI_VERIFY(jni, (ctor =
 103             jni->GetMethodID(cls, THREAD_DEATH_CTOR_NAME, THREAD_DEATH_CTOR_SIGNATURE)) != NULL))
 104         return NSK_FALSE;
 105 
 106     if (!NSK_JNI_VERIFY(jni, (threadDeath = jni->NewObject(cls, ctor)) != NULL))
 107         return NSK_FALSE;
 108 
 109     return NSK_TRUE;
 110 }
 111 
 112 /* ========================================================================== */
 113 
 114 /** Agent algorithm. */
 115 static void JNICALL
 116 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 117 
 118     if (!nsk_jvmti_waitForSync(timeout))
 119         return;
 120 
 121     if (!prepare(jvmti, jni)) {
 122         nsk_jvmti_setFailStatus();
 123         return;
 124     }
 125 
 126     NSK_DISPLAY0("Testcase #1: call StopThread for runningThread\n");
 127     if (!NSK_VERIFY(runningThread != NULL)) {
 128         nsk_jvmti_setFailStatus();
 129     } else {
 130         if (!NSK_JVMTI_VERIFY(jvmti->StopThread(runningThread, threadDeath)))
 131             nsk_jvmti_setFailStatus();
 132     }
 133 
 134     NSK_DISPLAY0("Testcase #2: call StopThread for waitingThread\n");
 135     if (!NSK_VERIFY(waitingThread != NULL)) {
 136         nsk_jvmti_setFailStatus();
 137     } else {
 138         if (!NSK_JVMTI_VERIFY(jvmti->StopThread(waitingThread, threadDeath)))
 139             nsk_jvmti_setFailStatus();
 140     }
 141 
 142     NSK_DISPLAY0("Testcase #3: call StopThread for sleepingThread\n");
 143     if (!NSK_VERIFY(sleepingThread != NULL)) {
 144         nsk_jvmti_setFailStatus();
 145     } else {
 146         if (!NSK_JVMTI_VERIFY(jvmti->StopThread(sleepingThread, threadDeath)))
 147             nsk_jvmti_setFailStatus();
 148     }
 149 
 150     if (!nsk_jvmti_resumeSync())
 151         return;
 152 }
 153 
 154 /* ========================================================================== */
 155 
 156 /** Agent library initialization. */
 157 #ifdef STATIC_BUILD
 158 JNIEXPORT jint JNICALL Agent_OnLoad_stopthrd007(JavaVM *jvm, char *options, void *reserved) {
 159     return Agent_Initialize(jvm, options, reserved);
 160 }
 161 JNIEXPORT jint JNICALL Agent_OnAttach_stopthrd007(JavaVM *jvm, char *options, void *reserved) {
 162     return Agent_Initialize(jvm, options, reserved);
 163 }
 164 JNIEXPORT jint JNI_OnLoad_stopthrd007(JavaVM *jvm, char *options, void *reserved) {
 165     return JNI_VERSION_1_8;
 166 }
 167 #endif
 168 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 169     jvmtiEnv* jvmti = NULL;
 170     jvmtiCapabilities caps;
 171 
 172     NSK_DISPLAY0("Agent_OnLoad\n");
 173 
 174     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 175         return JNI_ERR;
 176 
 177     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 178 
 179     if (!NSK_VERIFY((jvmti =
 180             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 181         return JNI_ERR;
 182 
 183     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 184         return JNI_ERR;
 185 
 186     memset(&caps, 0, sizeof(caps));
 187     caps.can_signal_thread = 1;
 188     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 189         return JNI_ERR;
 190     }
 191 
 192     return JNI_OK;
 193 }
 194 
 195 /* ========================================================================== */
 196 
 197 }