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 jvmtiEnv *jvmti = NULL;
  37 static jlong timeout = 0;
  38 static jrawMonitorID syncLock = NULL;
  39 
  40 /* constant names */
  41 #define STEP_NUMBER 3
  42 #define OBJECT_NUMBER 100
  43 #define JVMTI_EVENT_COUNT   (int)(JVMTI_MAX_EVENT_TYPE_VAL - JVMTI_MIN_EVENT_TYPE_VAL + 1)
  44 
  45 static int eventCount[JVMTI_EVENT_COUNT];
  46 static int newEventCount[JVMTI_EVENT_COUNT];
  47 
  48 /* ============================================================================= */
  49 
  50 
  51 /*
  52  * Class:     nsk_jvmti_scenarios_events_EM02_em02t006
  53  * Method:    setTag
  54  * Signature: (Ljava/lang/Object;J)Z
  55  */
  56 JNIEXPORT jboolean JNICALL
  57 Java_nsk_jvmti_scenarios_events_EM02_em02t006_setTag(JNIEnv *env,
  58                         jobject o, jobject object, jlong tag) {
  59 
  60     if (!NSK_JVMTI_VERIFY(jvmti->SetTag(object, tag))) {
  61         NSK_COMPLAIN0("TEST FAILED: unable to set tag for a tested object\n");
  62         return NSK_FALSE;
  63     }
  64     return NSK_TRUE;
  65 }
  66 
  67 static void
  68 showEventStatistics(int step) {
  69     int i;
  70     const char* str;
  71     int *currentCounts = (step == 1) ? &eventCount[0] : &newEventCount[0];
  72 
  73     NSK_DISPLAY0("\n");
  74     NSK_DISPLAY1("Event statistics for %d step:\n", step);
  75     NSK_DISPLAY0("-----------------------------\n");
  76     for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
  77         if (currentCounts[i] > 0) {
  78             str = TranslateEvent((jvmtiEvent)(i+JVMTI_MIN_EVENT_TYPE_VAL));
  79             NSK_DISPLAY2("%-40s %7d\n", str, currentCounts[i]);
  80         }
  81     }
  82 }
  83 
  84 /* ========================================================================== */
  85 
  86 int checkEvents(int step) {
  87     int i;
  88     jvmtiEvent curr;
  89     int result = NSK_TRUE;
  90     int *currentCounts;
  91     int isExpected = 0;
  92 
  93     switch (step) {
  94         case 1:
  95             currentCounts = &eventCount[0];
  96             break;
  97 
  98         case 2:
  99         case 3:
 100             currentCounts = &newEventCount[0];
 101             break;
 102 
 103         default:
 104             NSK_COMPLAIN1("Unexpected step no: %d\n", step);
 105             return NSK_FALSE;
 106     }
 107 
 108     for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
 109 
 110         curr = (jvmtiEvent) (i + JVMTI_MIN_EVENT_TYPE_VAL);
 111 
 112         switch (step) {
 113             case 1:
 114                 isExpected = ((curr == JVMTI_EVENT_OBJECT_FREE)
 115                                 || (curr == JVMTI_EVENT_VM_INIT));
 116                 break;
 117 
 118             case 2:
 119                 isExpected = (curr == JVMTI_EVENT_OBJECT_FREE);
 120                 break;
 121 
 122             case 3:
 123                 isExpected = (curr == JVMTI_EVENT_VM_DEATH);
 124                 break;
 125         }
 126 
 127         if (isExpected) {
 128             if (currentCounts[i] < 0 || currentCounts[i] > OBJECT_NUMBER) {
 129                     NSK_COMPLAIN2("Unexpected events number %7d for %s\n\texpected value must be greater than 1\n",
 130                                         currentCounts[i],
 131                                         TranslateEvent(curr));
 132                 result = NSK_FALSE;
 133             }
 134         } else {
 135 
 136             if (currentCounts[i] > 0) {
 137                 NSK_COMPLAIN2("Unexpected event %s was sent %d times\n",
 138                                     TranslateEvent(curr),
 139                                     currentCounts[i]);
 140                 result = NSK_FALSE;
 141             }
 142         }
 143     }
 144 
 145     return result;
 146 }
 147 
 148 static void
 149 changeCount(jvmtiEvent event, int *currentCounts) {
 150 
 151     if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(syncLock)))
 152         nsk_jvmti_setFailStatus();
 153 
 154     currentCounts[event - JVMTI_MIN_EVENT_TYPE_VAL]++;
 155 
 156     if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(syncLock)))
 157         nsk_jvmti_setFailStatus();
 158 
 159 }
 160 
 161 /* ============================================================================= */
 162 
 163 /* callbacks */
 164 JNIEXPORT void JNICALL
 165 cbVMInit(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread) {
 166     changeCount(JVMTI_EVENT_VM_INIT, &eventCount[0]);
 167 }
 168 
 169 JNIEXPORT void JNICALL
 170 cbVMDeath(jvmtiEnv* jvmti, JNIEnv* jni_env) {
 171     changeCount(JVMTI_EVENT_VM_DEATH, &newEventCount[0]);
 172     showEventStatistics(STEP_NUMBER);
 173     if (!checkEvents(STEP_NUMBER))
 174         nsk_jvmti_setFailStatus();
 175 
 176     if (!NSK_JVMTI_VERIFY(jvmti->DestroyRawMonitor(syncLock)))
 177         nsk_jvmti_setFailStatus();
 178 
 179 }
 180 
 181 void JNICALL
 182 cbException(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 183                 jmethodID method, jlocation location, jobject exception,
 184                 jmethodID catch_method, jlocation catch_location) {
 185     changeCount(JVMTI_EVENT_EXCEPTION, &eventCount[0]);
 186 }
 187 
 188 void JNICALL
 189 cbExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 190                 jmethodID method, jlocation location, jobject exception) {
 191     changeCount(JVMTI_EVENT_EXCEPTION_CATCH, &eventCount[0]);
 192 }
 193 
 194 void JNICALL
 195 cbSingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 196                 jmethodID method, jlocation location) {
 197     changeCount(JVMTI_EVENT_SINGLE_STEP, &eventCount[0]);
 198 }
 199 
 200 void JNICALL
 201 cbFramePop(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 202                 jmethodID method, jboolean was_popped_by_exception) {
 203     changeCount(JVMTI_EVENT_FRAME_POP, &eventCount[0]);
 204 }
 205 
 206 void JNICALL
 207 cbBreakpoint(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 208                 jmethodID method, jlocation location) {
 209     changeCount(JVMTI_EVENT_BREAKPOINT, &eventCount[0]);
 210 }
 211 
 212 void JNICALL
 213 cbFieldAccess(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 214                 jmethodID method, jlocation location, jclass field_klass,
 215                 jobject object, jfieldID field) {
 216     changeCount(JVMTI_EVENT_FIELD_ACCESS, &eventCount[0]);
 217 }
 218 
 219 void JNICALL
 220 cbFieldModification(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 221                 jmethodID method, jlocation location, jclass field_klass,
 222                 jobject object, jfieldID field, char signature_type,
 223                 jvalue new_value) {
 224     changeCount(JVMTI_EVENT_FIELD_MODIFICATION, &eventCount[0]);
 225 }
 226 
 227 void JNICALL
 228 cbMethodEntry(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 229                 jmethodID method) {
 230     changeCount(JVMTI_EVENT_METHOD_ENTRY, &eventCount[0]);
 231 }
 232 
 233 void JNICALL
 234 cbMethodExit(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 235                 jmethodID method, jboolean was_popped_by_exception,
 236                 jvalue return_value) {
 237     changeCount(JVMTI_EVENT_METHOD_EXIT, &eventCount[0]);
 238 }
 239 
 240 void JNICALL
 241 cbNativeMethodBind(jvmtiEnv *jvmti_env, JNIEnv* jni_env,jthread thread,
 242                 jmethodID method, void* address, void** new_address_ptr) {
 243     changeCount(JVMTI_EVENT_NATIVE_METHOD_BIND, &eventCount[0]);
 244 }
 245 
 246 void JNICALL
 247 cbMonitorWait(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 248                     jobject object, jlong tout) {
 249 
 250     changeCount(JVMTI_EVENT_MONITOR_WAIT, &eventCount[0]);
 251 }
 252 
 253 void JNICALL
 254 cbMonitorWaited(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 255                     jobject object, jboolean timed_out) {
 256 
 257     changeCount(JVMTI_EVENT_MONITOR_WAITED, &eventCount[0]);
 258 }
 259 
 260 JNIEXPORT void JNICALL
 261 cbMonitorContendedEnter(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread,
 262                             jobject object) {
 263 
 264     changeCount(JVMTI_EVENT_MONITOR_CONTENDED_ENTER, &eventCount[0]);
 265 }
 266 
 267 void JNICALL
 268 cbMonitorContendedEntered(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 269                             jobject object) {
 270 
 271     changeCount(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, &eventCount[0]);
 272 }
 273 
 274 void JNICALL
 275 cbCompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size,
 276                 const void* code_addr, jint map_length,
 277                 const jvmtiAddrLocationMap* map, const void* compile_info) {
 278     changeCount(JVMTI_EVENT_COMPILED_METHOD_LOAD, &eventCount[0]);
 279 }
 280 
 281 void JNICALL
 282 cbCompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method,
 283                 const void* code_addr) {
 284     changeCount(JVMTI_EVENT_COMPILED_METHOD_UNLOAD, &eventCount[0]);
 285 }
 286 
 287 void JNICALL
 288 cbGarbageCollectionStart(jvmtiEnv *jvmti_env) {
 289     changeCount(JVMTI_EVENT_GARBAGE_COLLECTION_START, &eventCount[0]);
 290 }
 291 
 292 void JNICALL
 293 cbGarbageCollectionFinish(jvmtiEnv *jvmti_env) {
 294     changeCount(JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, &eventCount[0]);
 295 }
 296 
 297 void JNICALL
 298 cbObjectFree(jvmtiEnv *jvmti_env, jlong tag) {
 299 
 300     changeCount(JVMTI_EVENT_OBJECT_FREE, &eventCount[0]);
 301     if (tag < 1 || tag > OBJECT_NUMBER) {
 302         NSK_COMPLAIN1("Unexpected tag value %lld\n", tag);
 303         nsk_jvmti_setFailStatus();
 304     }
 305 }
 306 
 307 void JNICALL
 308 cbNewObjectFree(jvmtiEnv *jvmti_env, jlong tag) {
 309 
 310     changeCount(JVMTI_EVENT_OBJECT_FREE, &newEventCount[0]);
 311     if (tag < 1 || tag > OBJECT_NUMBER) {
 312         NSK_COMPLAIN1("Unexpected tag value %lld\n", tag);
 313         nsk_jvmti_setFailStatus();
 314     }
 315 }
 316 
 317 void JNICALL
 318 cbVMObjectAlloc(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
 319                     jobject object, jclass object_klass, jlong size) {
 320 
 321     changeCount(JVMTI_EVENT_VM_OBJECT_ALLOC, &eventCount[0]);
 322 }
 323 
 324 /* ============================================================================= */
 325 
 326 static int enableEvent(jvmtiEvent event) {
 327 
 328     if (nsk_jvmti_isOptionalEvent(event)
 329             && (event != JVMTI_EVENT_OBJECT_FREE)) {
 330         if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
 331                 jvmti->SetEventNotificationMode(JVMTI_ENABLE, event, NULL))) {
 332             NSK_COMPLAIN1("Unexpected error enabling %s\n",
 333                 TranslateEvent(event));
 334             return NSK_FALSE;
 335         }
 336     } else {
 337         if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, event, NULL))) {
 338             NSK_COMPLAIN1("Unexpected error enabling %s\n",
 339                 TranslateEvent(event));
 340             return NSK_FALSE;
 341         }
 342     }
 343 
 344     return NSK_TRUE;
 345 }
 346 
 347 /**
 348  * Enable or disable tested events.
 349  */
 350 static int enableEventList() {
 351 
 352     int i, result;
 353 
 354     result = enableEvent(JVMTI_EVENT_VM_INIT);
 355 
 356     result = result && enableEvent(JVMTI_EVENT_VM_DEATH);
 357 
 358     /* enabling optional events */
 359     for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
 360         jvmtiEvent event = (jvmtiEvent)(i+JVMTI_MIN_EVENT_TYPE_VAL);
 361 
 362         if (nsk_jvmti_isOptionalEvent(event))
 363             result = result && enableEvent(event);
 364     }
 365 
 366     if (result == NSK_FALSE) {
 367         nsk_jvmti_setFailStatus();
 368         return NSK_FALSE;
 369     }
 370 
 371     return NSK_TRUE;
 372 }
 373 
 374 /* ============================================================================= */
 375 
 376 static int
 377 setCallBacks(int step) {
 378 
 379     int i;
 380 
 381     jvmtiEventCallbacks eventCallbacks;
 382     memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 383 
 384     switch (step) {
 385         case 1:
 386             for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
 387                 eventCount[i] = 0;
 388             }
 389 
 390             eventCallbacks.VMInit                    = cbVMInit;
 391             eventCallbacks.Exception                 = cbException;
 392             eventCallbacks.ExceptionCatch            = cbExceptionCatch;
 393             eventCallbacks.SingleStep                = cbSingleStep;
 394             eventCallbacks.FramePop                  = cbFramePop;
 395             eventCallbacks.Breakpoint                = cbBreakpoint;
 396             eventCallbacks.FieldAccess               = cbFieldAccess;
 397             eventCallbacks.FieldModification         = cbFieldModification;
 398             eventCallbacks.MethodEntry               = cbMethodEntry;
 399             eventCallbacks.MethodExit                = cbMethodExit;
 400             eventCallbacks.NativeMethodBind          = cbNativeMethodBind;
 401             eventCallbacks.CompiledMethodLoad        = cbCompiledMethodLoad;
 402             eventCallbacks.CompiledMethodUnload      = cbCompiledMethodUnload;
 403             eventCallbacks.MonitorWait               = cbMonitorWait;
 404             eventCallbacks.MonitorWaited             = cbMonitorWaited;
 405             eventCallbacks.MonitorContendedEnter     = cbMonitorContendedEnter;
 406             eventCallbacks.MonitorContendedEntered   = cbMonitorContendedEntered;
 407             eventCallbacks.GarbageCollectionStart    = cbGarbageCollectionStart;
 408             eventCallbacks.GarbageCollectionFinish   = cbGarbageCollectionFinish;
 409             eventCallbacks.ObjectFree                = cbObjectFree;
 410             eventCallbacks.VMObjectAlloc             = cbVMObjectAlloc;
 411             break;
 412 
 413         case 2:
 414             for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
 415                 newEventCount[i] = 0;
 416             }
 417 
 418             eventCallbacks.ObjectFree                = cbNewObjectFree;
 419             break;
 420 
 421         case 3:
 422             for (i = 0; i < JVMTI_EVENT_COUNT; i++) {
 423                 newEventCount[i] = 0;
 424             }
 425 
 426             eventCallbacks.VMDeath                   = cbVMDeath;
 427             break;
 428 
 429     }
 430     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))))
 431         return NSK_FALSE;
 432 
 433     return NSK_TRUE;
 434 }
 435 
 436 /* ============================================================================= */
 437 
 438 /** Agent algorithm. */
 439 static void JNICALL
 440 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
 441 
 442     int i;
 443 
 444     for (i = 1; i <= STEP_NUMBER; i++) {
 445 
 446         if (!nsk_jvmti_waitForSync(timeout))
 447             return;
 448 
 449         NSK_DISPLAY0("Check received events\n");
 450 
 451         if (i < STEP_NUMBER) {
 452             showEventStatistics(i);
 453             if (!checkEvents(i))
 454                 nsk_jvmti_setFailStatus();
 455 
 456             if (!setCallBacks(i + 1)) {
 457                 return;
 458             }
 459         }
 460 
 461         if (!nsk_jvmti_resumeSync())
 462             return;
 463     }
 464 
 465 }
 466 
 467 /* ============================================================================= */
 468 
 469 /** Agent library initialization. */
 470 #ifdef STATIC_BUILD
 471 JNIEXPORT jint JNICALL Agent_OnLoad_em02t006(JavaVM *jvm, char *options, void *reserved) {
 472     return Agent_Initialize(jvm, options, reserved);
 473 }
 474 JNIEXPORT jint JNICALL Agent_OnAttach_em02t006(JavaVM *jvm, char *options, void *reserved) {
 475     return Agent_Initialize(jvm, options, reserved);
 476 }
 477 JNIEXPORT jint JNI_OnLoad_em02t006(JavaVM *jvm, char *options, void *reserved) {
 478     return JNI_VERSION_1_8;
 479 }
 480 #endif
 481 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 482 
 483     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 484         return JNI_ERR;
 485 
 486     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 487 
 488     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 489         return JNI_ERR;
 490 
 491     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_syncLock", &syncLock))) {
 492         nsk_jvmti_setFailStatus();
 493         return JNI_ERR;
 494     }
 495 
 496     {
 497         jvmtiCapabilities caps;
 498         memset(&caps, 0, sizeof(caps));
 499 
 500         caps.can_tag_objects = 1;
 501         caps.can_generate_object_free_events = 1;
 502         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
 503             return JNI_ERR;
 504     }
 505 
 506     if (!setCallBacks(1)) {
 507         return JNI_ERR;
 508     }
 509 
 510     if (!enableEventList()) {
 511         return JNI_ERR;
 512     }
 513 
 514     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 515         return JNI_ERR;
 516 
 517     return JNI_OK;
 518 }
 519 
 520 /* ============================================================================= */
 521 
 522 
 523 }