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 <string.h>
  25 #include "jvmti.h"
  26 #include "agent_common.h"
  27 #include "jni_tools.h"
  28 #include "jvmti_tools.h"
  29 #include "JVMTITools.h"
  30 
  31 extern "C" {
  32 
  33 /* ============================================================================= */
  34 
  35 /* scaffold objects */
  36 static JNIEnv* jni = NULL;
  37 static jvmtiEnv *jvmti = NULL;
  38 static jlong timeout = 0;
  39 static jrawMonitorID syncLock = NULL;
  40 
  41 /* constant names */
  42 #define JVMTI_EVENT_COUNT   (int)(JVMTI_MAX_EVENT_TYPE_VAL - JVMTI_MIN_EVENT_TYPE_VAL + 1)
  43 #define EXPECTED_CLASS_NAME "nsk.jvmti.scenarios.events.EM06.em06t001a"
  44 #define CLASS_LOADER_COUNT_PARAM "classLoaderCount"
  45 
  46 static int classLoaderCount = 0;
  47 static int classloadEventCount = 0;
  48 static int classprepareEventCount = 0;
  49 
  50 /* ============================================================================= */
  51 
  52 /* callbacks */
  53 
  54 void
  55 handler(jvmtiEvent event, jvmtiEnv* jvmti, JNIEnv* jni_env,
  56                     jthread thread, jclass klass) {
  57 
  58     jmethodID methodID;
  59     jclass classObject;
  60     jstring jclassName;
  61     const char *className;
  62 
  63     if (!NSK_JNI_VERIFY(jni_env, (classObject =
  64             NSK_CPP_STUB2(GetObjectClass, jni_env, klass)) != NULL)) {
  65         nsk_jvmti_setFailStatus();
  66         return;
  67     }
  68 
  69     if (!NSK_JNI_VERIFY(jni_env, (methodID =
  70             NSK_CPP_STUB4(GetMethodID, jni_env, classObject,
  71                         "getName", "()Ljava/lang/String;")) != NULL)) {
  72         nsk_jvmti_setFailStatus();
  73         return;
  74     }
  75 
  76     jclassName = (jstring) NSK_CPP_STUB3(CallObjectMethod, jni_env, klass, methodID);
  77 
  78     className = NSK_CPP_STUB3(GetStringUTFChars, jni_env, jclassName, 0);
  79 
  80     if ( className != NULL && (strcmp(className, EXPECTED_CLASS_NAME)==0) ) {
  81 
  82         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, syncLock)))
  83             nsk_jvmti_setFailStatus();
  84 
  85         switch (event) {
  86             case JVMTI_EVENT_CLASS_LOAD:
  87                 classloadEventCount++; break;
  88             case JVMTI_EVENT_CLASS_PREPARE:
  89                 classprepareEventCount++; break;
  90             default:
  91                 NSK_COMPLAIN1("Unexpected event %s", TranslateEvent(event));
  92                 nsk_jvmti_setFailStatus();
  93         }
  94 
  95         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, syncLock)))
  96             nsk_jvmti_setFailStatus();
  97 
  98     }
  99 
 100     NSK_CPP_STUB3(ReleaseStringUTFChars, jni_env, jclassName, className);
 101 }
 102 
 103 JNIEXPORT void JNICALL
 104 cbClassLoad(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, jclass klass) {
 105 
 106     handler(JVMTI_EVENT_CLASS_LOAD, jvmti, jni_env, thread, klass);
 107 }
 108 
 109 JNIEXPORT void JNICALL
 110 cbClassPrepare(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, jclass klass) {
 111 
 112     handler(JVMTI_EVENT_CLASS_PREPARE, jvmti, jni_env, thread, klass);
 113 }
 114 
 115 /* ============================================================================= */
 116 
 117 static int
 118 enableEvent(jvmtiEventMode enable, jvmtiEvent event) {
 119     if (!NSK_JVMTI_VERIFY(
 120             NSK_CPP_STUB4(SetEventNotificationMode, jvmti, enable,
 121                                             event, NULL))) {
 122         nsk_jvmti_setFailStatus();
 123         return NSK_FALSE;
 124     }
 125 
 126     return NSK_TRUE;
 127 }
 128 
 129 /* ============================================================================= */
 130 
 131 /**
 132  * Testcase: check tested events.
 133  *   - check if expected events received for each method
 134  *
 135  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
 136  */
 137 int checkEvents() {
 138 
 139     int result = NSK_TRUE;
 140 
 141     if (classloadEventCount == classLoaderCount) {
 142         NSK_DISPLAY1("Expected number of JVMTI_EVENT_CLASS_LOAD events %d\n",
 143                             classloadEventCount);
 144     } else {
 145         NSK_COMPLAIN2("Unexpected number of JVMTI_EVENT_CLASS_LOAD events %d\n\texpected value %d\n",
 146                             classloadEventCount,
 147                             classLoaderCount);
 148         result = NSK_FALSE;
 149     }
 150 
 151     if (classprepareEventCount == classLoaderCount) {
 152         NSK_DISPLAY1("Expected number of JVMTI_EVENT_CLASS_PREPARE events %d\n",
 153                             classloadEventCount);
 154     } else {
 155         NSK_COMPLAIN2("Unexpected number of JVMTI_EVENT_CLASS_PREPARE events %d\n\texpected value %d\n",
 156                             classprepareEventCount,
 157                             classLoaderCount);
 158         result = NSK_FALSE;
 159     }
 160 
 161     return result;
 162 }
 163 
 164 /* ============================================================================= */
 165 
 166 static int
 167 setCallBacks() {
 168     jvmtiEventCallbacks eventCallbacks;
 169     memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 170 
 171     eventCallbacks.ClassLoad    = cbClassLoad;
 172     eventCallbacks.ClassPrepare = cbClassPrepare;
 173 
 174     if (!NSK_JVMTI_VERIFY(
 175             NSK_CPP_STUB3(SetEventCallbacks, jvmti,
 176                                 &eventCallbacks,
 177                                 sizeof(eventCallbacks))))
 178         return NSK_FALSE;
 179 
 180     return NSK_TRUE;
 181 }
 182 
 183 /* ============================================================================= */
 184 
 185 /** Agent algorithm. */
 186 static void JNICALL
 187 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
 188 
 189     if (!NSK_JVMTI_VERIFY(
 190             NSK_CPP_STUB3(CreateRawMonitor, jvmti, "_syncLock", &syncLock))) {
 191         nsk_jvmti_setFailStatus();
 192         return;
 193     }
 194 
 195     jni = agentJNI;
 196 
 197     NSK_DISPLAY0("Wait for debuggee to become ready\n");
 198     if (!nsk_jvmti_waitForSync(timeout))
 199         return;
 200 
 201     if (!setCallBacks()) {
 202         return;
 203     }
 204 
 205     if (!enableEvent(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD)
 206             || !enableEvent(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE)) {
 207         NSK_COMPLAIN0("Events could not be enabled");
 208         nsk_jvmti_setFailStatus();
 209         return;
 210     }
 211 
 212     NSK_DISPLAY0("Let debuggee to load class\n");
 213     if (!nsk_jvmti_resumeSync())
 214         return;
 215 
 216     if (!nsk_jvmti_waitForSync(timeout))
 217         return;
 218 
 219     if (!checkEvents()) {
 220         nsk_jvmti_setFailStatus();
 221     }
 222 
 223     if (!enableEvent(JVMTI_DISABLE, JVMTI_EVENT_CLASS_LOAD)
 224             || !enableEvent(JVMTI_DISABLE, JVMTI_EVENT_CLASS_PREPARE)) {
 225         NSK_COMPLAIN0("Events could not be disabled");
 226         nsk_jvmti_setFailStatus();
 227     }
 228 
 229     NSK_DISPLAY0("Let debuggee to finish\n");
 230     if (!nsk_jvmti_resumeSync())
 231         return;
 232 
 233     if (!NSK_JVMTI_VERIFY(
 234             NSK_CPP_STUB2(DestroyRawMonitor, jvmti, syncLock)))
 235         nsk_jvmti_setFailStatus();
 236 
 237 }
 238 
 239 /* ============================================================================= */
 240 
 241 /** Agent library initialization. */
 242 #ifdef STATIC_BUILD
 243 JNIEXPORT jint JNICALL Agent_OnLoad_em06t001(JavaVM *jvm, char *options, void *reserved) {
 244     return Agent_Initialize(jvm, options, reserved);
 245 }
 246 JNIEXPORT jint JNICALL Agent_OnAttach_em06t001(JavaVM *jvm, char *options, void *reserved) {
 247     return Agent_Initialize(jvm, options, reserved);
 248 }
 249 JNIEXPORT jint JNI_OnLoad_em06t001(JavaVM *jvm, char *options, void *reserved) {
 250     return JNI_VERSION_1_8;
 251 }
 252 #endif
 253 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 254 
 255     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 256         return JNI_ERR;
 257 
 258     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 259     classLoaderCount = nsk_jvmti_findOptionIntValue(CLASS_LOADER_COUNT_PARAM, 100);
 260 
 261     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 262         return JNI_ERR;
 263 
 264     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 265         return JNI_ERR;
 266 
 267     return JNI_OK;
 268 }
 269 
 270 /* ============================================================================= */
 271 
 272 
 273 }