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 jthread threadForStop = NULL;
  42 static jthread threadForInterrupt = NULL;
  43 static int ThreadDeathFlag = 0;
  44 static int InterruptedExceptionFlag = 0;
  45 
  46 /* ========================================================================== */
  47 
  48 /** callback functions **/
  49 
  50 const char* THREAD_DEATH_CLASS_SIG = "Ljava/lang/ThreadDeath;";
  51 const char* INTERRUPTED_EXCEPTION_CLASS_SIG = "Ljava/lang/InterruptedException;";
  52 static void JNICALL
  53 Exception(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread,
  54         jmethodID method, jlocation location, jobject exception,
  55         jmethodID catch_method, jlocation catch_location) {
  56     jclass klass = NULL;
  57     char *signature = NULL;
  58 
  59     if (!NSK_JNI_VERIFY(jni_env, (klass =
  60             NSK_CPP_STUB2(GetObjectClass, jni_env, exception)) != NULL)) {
  61         nsk_jvmti_setFailStatus();
  62         return;
  63     }
  64 
  65     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature, jvmti_env,
  66             klass, &signature, NULL))) {
  67         nsk_jvmti_setFailStatus();
  68         return;
  69     }
  70 
  71     if (!NSK_VERIFY(signature != NULL)) {
  72         nsk_jvmti_setFailStatus();
  73         return;
  74     }
  75 
  76     NSK_DISPLAY1("Exception event: %s\n", signature);
  77 
  78     if (NSK_CPP_STUB3(IsSameObject, jni_env, threadForInterrupt, thread)) {
  79         if (strcmp(signature, INTERRUPTED_EXCEPTION_CLASS_SIG) == 0) {
  80             InterruptedExceptionFlag++;
  81         } else {
  82             NSK_COMPLAIN1("Unexpected exception in DebuggeeThreadForInterrupt: %s\n",
  83                 signature);
  84             nsk_jvmti_setFailStatus();
  85         }
  86     } else if (NSK_CPP_STUB3(IsSameObject, jni_env, threadForStop, thread)) {
  87         if (strcmp(signature, THREAD_DEATH_CLASS_SIG) == 0) {
  88             ThreadDeathFlag++;
  89         } else {
  90             NSK_COMPLAIN1("Unexpected exception in DebuggeeThreadForStop: %s\n",
  91                 signature);
  92             nsk_jvmti_setFailStatus();
  93         }
  94     }
  95 
  96     NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature);
  97 }
  98 
  99 /* ========================================================================== */
 100 
 101 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) {
 102     const char* STOP_THREAD_NAME = "DebuggeeThreadForStop";
 103     const char* INTERRUPT_THREAD_NAME = "DebuggeeThreadForInterrupt";
 104     jvmtiThreadInfo info;
 105     jthread *threads = NULL;
 106     jint threads_count = 0;
 107     int i;
 108 
 109     NSK_DISPLAY0("Prepare: find tested thread\n");
 110 
 111     /* get all live threads */
 112     if (!NSK_JVMTI_VERIFY(
 113            NSK_CPP_STUB3(GetAllThreads, jvmti, &threads_count, &threads)))
 114         return NSK_FALSE;
 115 
 116     if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
 117         return NSK_FALSE;
 118 
 119     /* find tested thread */
 120     for (i = 0; i < threads_count; i++) {
 121         if (!NSK_VERIFY(threads[i] != NULL))
 122             return NSK_FALSE;
 123 
 124         /* get thread information */
 125         if (!NSK_JVMTI_VERIFY(
 126                 NSK_CPP_STUB3(GetThreadInfo, jvmti, threads[i], &info)))
 127             return NSK_FALSE;
 128 
 129         NSK_DISPLAY3("    thread #%d (%s): %p\n", i, info.name, threads[i]);
 130 
 131         /* find by name */
 132         if (info.name != NULL) {
 133             if (strcmp(info.name, STOP_THREAD_NAME) == 0) {
 134                 threadForStop = threads[i];
 135             } else if (strcmp(info.name, INTERRUPT_THREAD_NAME) == 0) {
 136                 threadForInterrupt = threads[i];
 137             }
 138         }
 139     }
 140 
 141     /* deallocate threads list */
 142     if (!NSK_JVMTI_VERIFY(
 143             NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threads)))
 144         return NSK_FALSE;
 145 
 146     if (threadForStop == NULL) {
 147         NSK_COMPLAIN0("DebuggeeThreadForStop not found");
 148         return NSK_FALSE;
 149     }
 150 
 151     if (threadForInterrupt == NULL) {
 152         NSK_COMPLAIN0("DebuggeeThreadForInterrupt not found");
 153         return NSK_FALSE;
 154     }
 155 
 156     if (!NSK_JNI_VERIFY(jni, (threadForStop =
 157             NSK_CPP_STUB2(NewGlobalRef, jni, threadForStop)) != NULL))
 158         return NSK_FALSE;
 159 
 160     if (!NSK_JNI_VERIFY(jni, (threadForInterrupt =
 161             NSK_CPP_STUB2(NewGlobalRef, jni, threadForInterrupt)) != NULL))
 162         return NSK_FALSE;
 163 
 164     /* enable event */
 165     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 166             jvmti, JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL)))
 167         return NSK_FALSE;
 168 
 169     return NSK_TRUE;
 170 }
 171 
 172 /* ========================================================================== */
 173 
 174 /** Agent algorithm. */
 175 static void JNICALL
 176 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 177 
 178     if (!nsk_jvmti_waitForSync(timeout))
 179         return;
 180 
 181     if (!prepare(jvmti, jni)) {
 182         nsk_jvmti_setFailStatus();
 183         return;
 184     }
 185 
 186     /* resume debugee and wait for sync */
 187     if (!nsk_jvmti_resumeSync())
 188         return;
 189     if (!nsk_jvmti_waitForSync(timeout))
 190         return;
 191 
 192     NSK_DISPLAY1("ThreadDeath received: %d\n", ThreadDeathFlag);
 193     if (!NSK_VERIFY(ThreadDeathFlag))
 194         nsk_jvmti_setFailStatus();
 195 
 196     NSK_DISPLAY1("InterruptedException received: %d\n",
 197         InterruptedExceptionFlag);
 198     if (!NSK_VERIFY(InterruptedExceptionFlag))
 199         nsk_jvmti_setFailStatus();
 200 
 201     /* disable event */
 202     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 203             jvmti, JVMTI_DISABLE, JVMTI_EVENT_EXCEPTION, NULL)))
 204         nsk_jvmti_setFailStatus();
 205 
 206     NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadForStop));
 207     NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threadForInterrupt));
 208 
 209     if (!nsk_jvmti_resumeSync())
 210         return;
 211 }
 212 
 213 /* ========================================================================== */
 214 
 215 /** Agent library initialization. */
 216 #ifdef STATIC_BUILD
 217 JNIEXPORT jint JNICALL Agent_OnLoad_ma08t001a(JavaVM *jvm, char *options, void *reserved) {
 218     return Agent_Initialize(jvm, options, reserved);
 219 }
 220 JNIEXPORT jint JNICALL Agent_OnAttach_ma08t001a(JavaVM *jvm, char *options, void *reserved) {
 221     return Agent_Initialize(jvm, options, reserved);
 222 }
 223 JNIEXPORT jint JNI_OnLoad_ma08t001a(JavaVM *jvm, char *options, void *reserved) {
 224     return JNI_VERSION_1_8;
 225 }
 226 #endif
 227 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 228     jvmtiEnv* jvmti = NULL;
 229     jvmtiCapabilities caps;
 230     jvmtiEventCallbacks callbacks;
 231 
 232     NSK_DISPLAY0("Agent_OnLoad\n");
 233 
 234     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 235         return JNI_ERR;
 236 
 237     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 238 
 239     if (!NSK_VERIFY((jvmti =
 240             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 241         return JNI_ERR;
 242 
 243     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 244         return JNI_ERR;
 245 
 246     memset(&caps, 0, sizeof(caps));
 247     caps.can_generate_exception_events = 1;
 248     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
 249         return JNI_ERR;
 250     }
 251 
 252     memset(&callbacks, 0, sizeof(callbacks));
 253     callbacks.Exception = &Exception;
 254     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 255         return JNI_ERR;
 256 
 257     return JNI_OK;
 258 }
 259 
 260 /* ========================================================================== */
 261 
 262 }