1 /*
   2  * Copyright (c) 2007, 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 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <string.h>
  26 #include <jni.h>
  27 #include <jvmti.h>
  28 #include <aod.h>
  29 #include <jvmti_aod.h>
  30 
  31 extern "C" {
  32 
  33 #define OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach022/ClassForAllocationEventsTest;"
  34 
  35 static jvmtiEnv* jvmti;
  36 static Options* options = NULL;
  37 static const char* agentName;
  38 
  39 static jvmtiEvent testEvents[] = {JVMTI_EVENT_OBJECT_FREE, JVMTI_EVENT_VM_OBJECT_ALLOC};
  40 static const int testEventsNumber = 2;
  41 
  42 static volatile int taggedObjectsCounter = 0;
  43 static volatile int freedObjectsCounter = 0;
  44 
  45 static jrawMonitorID objectTagMonitor;
  46 static jrawMonitorID objectFreeMonitor;
  47 
  48 volatile int success = 1;
  49 
  50 volatile int agentFinished;
  51 
  52 void shutdownAgent(JNIEnv* jni) {
  53     if (agentFinished)
  54         return;
  55 
  56     if (!nsk_jvmti_aod_disableEvents(jvmti, testEvents, testEventsNumber))
  57         success = 0;
  58 
  59     nsk_aod_agentFinished(jni, agentName, success);
  60 
  61     agentFinished = 1;
  62 }
  63 
  64 JNIEXPORT jboolean JNICALL
  65 Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent(JNIEnv * jni,
  66         jclass klass, jint expectedTaggedObjectsCounter) {
  67 
  68     if (taggedObjectsCounter != expectedTaggedObjectsCounter) {
  69         success = 0;
  70         NSK_COMPLAIN2("ERROR: unexpected taggedObjectsCounter: %d (expected value is %d)\n", taggedObjectsCounter, expectedTaggedObjectsCounter);
  71     }
  72 
  73     if (taggedObjectsCounter != freedObjectsCounter) {
  74         success = 0;
  75         NSK_COMPLAIN2("ERROR: taggedObjectsCounter != freedObjectsCounter (taggedObjectsCounter: %d, freedObjectsCounter: %d)\n",
  76                 taggedObjectsCounter, freedObjectsCounter);
  77     }
  78 
  79     shutdownAgent(jni);
  80 
  81     return success ? JNI_TRUE : JNI_FALSE;
  82 }
  83 
  84 void JNICALL objectFreeHandler(jvmtiEnv *jvmti, jlong tag) {
  85     NSK_DISPLAY2("%s: ObjectFree event received (object tag: %ld)\n", agentName, tag);
  86 
  87     if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectFreeMonitor))) {
  88         freedObjectsCounter++;
  89 
  90         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectFreeMonitor))) {
  91             success = 0;
  92         }
  93     } else {
  94         success = 0;
  95     }
  96 }
  97 
  98 #define ATTACH022_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach022/attach022Target"
  99 
 100 int registerNativeMethods(JNIEnv* jni) {
 101     jclass appClass;
 102     JNINativeMethod nativeMethods[] = {
 103             {(char*)"shutdownAgent", (char*)"(I)Z",
 104             (void*) Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent}};
 105     jint nativeMethodsNumber = 1;
 106 
 107     if (!NSK_JNI_VERIFY(jni, (appClass =
 108         jni->FindClass(ATTACH022_TARGET_APP_CLASS_NAME)) != NULL)) {
 109         return NSK_FALSE;
 110     }
 111 
 112     if (!NSK_JNI_VERIFY(jni,
 113             (jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber) == 0))) {
 114         return NSK_FALSE;
 115     }
 116 
 117     return NSK_TRUE;
 118 }
 119 
 120 void JNICALL vmObjectAllocHandler(jvmtiEnv * jvmti,
 121         JNIEnv * jni,
 122         jthread thread,
 123         jobject object,
 124         jclass object_class,
 125         jlong size) {
 126     char className[MAX_STRING_LENGTH];
 127 
 128     if (!nsk_jvmti_aod_getClassName(jvmti, object_class, className)) {
 129         success = 0;
 130         shutdownAgent(jni);
 131         return;
 132     }
 133 
 134     NSK_DISPLAY2("%s: ObjectAlloc event received (object class: %s)\n", agentName, className);
 135 
 136     if (!strcmp(className, OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME)) {
 137         if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectTagMonitor))) {
 138             jlong tagValue = taggedObjectsCounter + 1;
 139 
 140             if (!NSK_JVMTI_VERIFY(jvmti->SetTag(object, tagValue))) {
 141                 NSK_COMPLAIN1("%s: failed to set tag\n", agentName);
 142                 success = 0;
 143             } else {
 144                 NSK_DISPLAY2("%s: object was tagged (tag value: %ld)\n", agentName, tagValue);
 145                 taggedObjectsCounter++;
 146             }
 147 
 148             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectTagMonitor))) {
 149                 success = 0;
 150             }
 151         } else {
 152             success = 0;
 153         }
 154     }
 155 
 156     if (!success) {
 157         NSK_COMPLAIN1("%s: error happened during agent work, stop agent\n", agentName);
 158         shutdownAgent(jni);
 159     }
 160 }
 161 
 162 #ifdef STATIC_BUILD
 163 JNIEXPORT jint JNI_OnLoad_attach022Agent00(JavaVM *jvm, char *options, void *reserved) {
 164     return JNI_VERSION_1_8;
 165 }
 166 #endif
 167 
 168 JNIEXPORT jint JNICALL
 169 #ifdef STATIC_BUILD
 170 Agent_OnAttach_attach022Agent00(JavaVM *vm, char *optionsString, void *reserved)
 171 #else
 172 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 173 #endif
 174 {
 175     jvmtiEventCallbacks eventCallbacks;
 176     jvmtiCapabilities caps;
 177     JNIEnv* jni;
 178 
 179     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 180         return JNI_ERR;
 181 
 182     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 183 
 184     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 185         return JNI_ERR;
 186 
 187     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 188         return JNI_ERR;
 189 
 190     if (!NSK_VERIFY(registerNativeMethods(jni))) {
 191         return JNI_ERR;
 192     }
 193 
 194     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectTagMonitor", &objectTagMonitor))) {
 195         return JNI_ERR;
 196     }
 197 
 198     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectFreeMonitor", &objectFreeMonitor))) {
 199         return JNI_ERR;
 200     }
 201 
 202     memset(&caps, 0, sizeof(caps));
 203     caps.can_tag_objects = 1;
 204     caps.can_generate_object_free_events = 1;
 205     caps.can_generate_vm_object_alloc_events = 1;
 206     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 207         return JNI_ERR;
 208     }
 209 
 210     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 211     eventCallbacks.ObjectFree = objectFreeHandler;
 212     eventCallbacks.VMObjectAlloc = vmObjectAllocHandler;
 213     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
 214         return JNI_ERR;
 215     }
 216 
 217     if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
 218         return JNI_ERR;
 219     }
 220 
 221     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 222 
 223     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 224         return JNI_ERR;
 225 
 226     return JNI_OK;
 227 }
 228 
 229 }