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 
  89         freedObjectsCounter++;
  90 
  91         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectFreeMonitor))) {
  92             success = 0;
  93         }
  94     } else {
  95         success = 0;
  96     }
  97 }
  98 
  99 #define ATTACH022_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach022/attach022Target"
 100 
 101 int registerNativeMethods(JNIEnv* jni) {
 102     jclass appClass;
 103     JNINativeMethod nativeMethods[] = {
 104             {(char*)"shutdownAgent", (char*)"(I)Z",
 105             (void*) Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent}};
 106     jint nativeMethodsNumber = 1;
 107 
 108     if (!NSK_JNI_VERIFY(jni, (appClass =
 109         jni->FindClass(ATTACH022_TARGET_APP_CLASS_NAME)) != NULL)) {
 110         return NSK_FALSE;
 111     }
 112 
 113     if (!NSK_JNI_VERIFY(jni,
 114             (jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber) == 0))) {
 115         return NSK_FALSE;
 116     }
 117 
 118     return NSK_TRUE;
 119 }
 120 
 121 void JNICALL vmObjectAllocHandler(jvmtiEnv * jvmti,
 122         JNIEnv * jni,
 123         jthread thread,
 124         jobject object,
 125         jclass object_class,
 126         jlong size) {
 127     char className[MAX_STRING_LENGTH];
 128 
 129     if (!nsk_jvmti_aod_getClassName(jvmti, object_class, className)) {
 130         success = 0;
 131         shutdownAgent(jni);
 132         return;
 133     }
 134 
 135     NSK_DISPLAY2("%s: ObjectAlloc event received (object class: %s)\n", agentName, className);
 136 
 137     if (!strcmp(className, OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME)) {
 138         if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectTagMonitor))) {
 139             jlong tagValue = taggedObjectsCounter + 1;
 140 
 141 
 142             if (!NSK_JVMTI_VERIFY(jvmti->SetTag(object, tagValue))) {
 143                 NSK_COMPLAIN1("%s: failed to set tag\n", agentName);
 144                 success = 0;
 145             } else {
 146                 NSK_DISPLAY2("%s: object was tagged (tag value: %ld)\n", agentName, tagValue);
 147                 taggedObjectsCounter++;
 148             }
 149 
 150             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectTagMonitor))) {
 151                 success = 0;
 152             }
 153         } else {
 154             success = 0;
 155         }
 156     }
 157 
 158     if (!success) {
 159         NSK_COMPLAIN1("%s: error happened during agent work, stop agent\n", agentName);
 160         shutdownAgent(jni);
 161     }
 162 }
 163 
 164 #ifdef STATIC_BUILD
 165 JNIEXPORT jint JNI_OnLoad_attach022Agent00(JavaVM *jvm, char *options, void *reserved) {
 166     return JNI_VERSION_1_8;
 167 }
 168 #endif
 169 
 170 JNIEXPORT jint JNICALL
 171 #ifdef STATIC_BUILD
 172 Agent_OnAttach_attach022Agent00(JavaVM *vm, char *optionsString, void *reserved)
 173 #else
 174 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 175 #endif
 176 {
 177     jvmtiEventCallbacks eventCallbacks;
 178     jvmtiCapabilities caps;
 179     JNIEnv* jni;
 180 
 181     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 182         return JNI_ERR;
 183 
 184     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 185 
 186     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 187         return JNI_ERR;
 188 
 189     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 190         return JNI_ERR;
 191 
 192     if (!NSK_VERIFY(registerNativeMethods(jni))) {
 193         return JNI_ERR;
 194     }
 195 
 196     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectTagMonitor", &objectTagMonitor))) {
 197         return JNI_ERR;
 198     }
 199 
 200     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectFreeMonitor", &objectFreeMonitor))) {
 201         return JNI_ERR;
 202     }
 203 
 204     memset(&caps, 0, sizeof(caps));
 205     caps.can_tag_objects = 1;
 206     caps.can_generate_object_free_events = 1;
 207     caps.can_generate_vm_object_alloc_events = 1;
 208     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)) ) {
 209         return JNI_ERR;
 210     }
 211 
 212     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 213     eventCallbacks.ObjectFree = objectFreeHandler;
 214     eventCallbacks.VMObjectAlloc = vmObjectAllocHandler;
 215     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))) ) {
 216         return JNI_ERR;
 217     }
 218 
 219     if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
 220         return JNI_ERR;
 221     }
 222 
 223     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 224 
 225     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 226         return JNI_ERR;
 227 
 228     return JNI_OK;
 229 }
 230 
 231 }