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 #include "ExceptionCheckingJniEnv.hpp" 31 32 extern "C" { 33 34 /* 35 * Expected agent work scenario: 36 * - receive ClassFileLoadHook event for class 'ClassToRedefine' 37 * - receive ClassLoad event for class 'ClassToRedefine' and redefine class from ClassLoad event handler 38 * - receive one more ClassFileLoadHook event for class 'ClassToRedefine' 39 * - receive ClassPrepare event for class 'ClassToRedefine' and finish work 40 */ 41 42 #define REDEFINED_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach002/ClassToRedefine;" 43 #define REDEFINED_CLASS_FILE_NAME "nsk/jvmti/AttachOnDemand/attach002/ClassToRedefine" 44 45 // class name in the ClassFileLoadHook callback 46 #define REDEFINED_CLASS_NAME_INTERNAL "nsk/jvmti/AttachOnDemand/attach002/ClassToRedefine" 47 48 static Options* options = NULL; 49 static const char* agentName; 50 51 static volatile jboolean agentGotCapabilities = JNI_FALSE; 52 53 static jvmtiEvent testEvents[] = { 54 JVMTI_EVENT_CLASS_LOAD, 55 JVMTI_EVENT_CLASS_PREPARE, 56 JVMTI_EVENT_CLASS_FILE_LOAD_HOOK }; 57 58 static const int testEventsNumber = 3; 59 60 static volatile int classLoadReceived = 0; 61 static volatile int classFileLoadHookReceived = 0; 62 63 JNIEXPORT jboolean JNICALL 64 Java_nsk_jvmti_AttachOnDemand_attach002_attach002Target_agentGotCapabilities(JNIEnv * jni, 65 jclass klass, jobject obj) { 66 return agentGotCapabilities; 67 } 68 69 #define ATTACH002_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach002/attach002Target" 70 71 void registerNativeMethods(JNIEnv* jni_env) { 72 ExceptionCheckingJniEnvPtr jni(jni_env); 73 jclass appClass; 74 JNINativeMethod nativeMethods[] = { 75 { (char*) "agentGotCapabilities", (char*) "()Z", (void*) Java_nsk_jvmti_AttachOnDemand_attach002_attach002Target_agentGotCapabilities } }; 76 jint nativeMethodsNumber = 1; 77 78 appClass = jni->FindClass(ATTACH002_TARGET_APP_CLASS_NAME, TRACE_JNI_CALL); 79 jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber, TRACE_JNI_CALL); 80 } 81 82 void JNICALL classLoadHandler( 83 jvmtiEnv *jvmti, 84 JNIEnv* jni, 85 jthread thread, 86 jclass klass) { 87 char className[MAX_STRING_LENGTH]; 88 89 if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) { 90 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni); 91 return; 92 } 93 94 NSK_DISPLAY2("%s: ClassLoad event was received for class '%s'\n", agentName, className); 95 96 if (!strcmp(className, REDEFINED_CLASS_NAME)) { 97 98 classLoadReceived = 1; 99 100 NSK_DISPLAY1("%s: redefining class\n", agentName); 101 102 if (!NSK_VERIFY(nsk_jvmti_aod_redefineClass(options, jvmti, klass, REDEFINED_CLASS_FILE_NAME))) { 103 NSK_COMPLAIN1("%s: failed to redefine class\n", agentName); 104 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni); 105 } 106 } 107 } 108 109 void JNICALL classPrepareHandler( 110 jvmtiEnv *jvmti, 111 JNIEnv* jni, 112 jthread thread, 113 jclass klass) { 114 char className[MAX_STRING_LENGTH]; 115 116 if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) { 117 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni); 118 return; 119 } 120 121 NSK_DISPLAY2("%s: ClassPrepare event received for class '%s'\n", agentName, REDEFINED_CLASS_NAME); 122 123 if (!strcmp(className, REDEFINED_CLASS_NAME)) { 124 int success = 1; 125 126 if (!classLoadReceived) { 127 success = 0; 128 NSK_COMPLAIN2("%s: expected ClassLoad event wasn't received for class '%s'\n", agentName, REDEFINED_CLASS_NAME); 129 } 130 131 /* 132 * ClassFileLoadHook event should be received twice - when class is loaded and when class is redefined 133 */ 134 if (classFileLoadHookReceived != 2) { 135 success = 0; 136 NSK_COMPLAIN2("%s: expected ClassFileLoadHook event wasn't received for class '%s'\n", agentName, REDEFINED_CLASS_NAME); 137 } 138 139 nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni); 140 } 141 } 142 143 void JNICALL classFileLoadHoockHandler( 144 jvmtiEnv * jvmti, 145 JNIEnv * jni, 146 jclass class_beeing_redefined, 147 jobject loader, 148 const char * name, 149 jobject protection_domain, 150 jint class_data_len, 151 const unsigned char * class_data, 152 jint * new_class_data_len, 153 unsigned char** new_class_data) { 154 155 if (name != NULL) { 156 NSK_DISPLAY2("%s: ClassFileLoadHook event received for class '%s'\n", agentName, name); 157 if (!strcmp(name, REDEFINED_CLASS_NAME_INTERNAL)) { 158 classFileLoadHookReceived++; 159 } 160 } else { 161 NSK_DISPLAY1("%s: ClassFileLoadHook event received for class with NULL name\n", agentName); 162 } 163 } 164 165 #ifdef STATIC_BUILD 166 JNIEXPORT jint JNI_OnLoad_attach002Agent00(JavaVM *jvm, char *options, void *reserved) { 167 return JNI_VERSION_1_8; 168 } 169 #endif 170 171 JNIEXPORT jint JNICALL 172 #ifdef STATIC_BUILD 173 Agent_OnAttach_attach002Agent00(JavaVM *vm, char *optionsString, void *reserved) 174 #else 175 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved) 176 #endif 177 { 178 jvmtiEventCallbacks eventCallbacks; 179 jvmtiCapabilities caps; 180 jvmtiEnv* jvmti = NULL; 181 JNIEnv* jni = NULL; 182 183 options = (Options*) nsk_aod_createOptions(optionsString); 184 if (!NSK_VERIFY(options != NULL)) 185 return JNI_ERR; 186 187 agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION); 188 189 jni = (JNIEnv*) nsk_aod_createJNIEnv(vm); 190 if (jni == NULL) 191 return NSK_FALSE; 192 193 jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved); 194 if (!NSK_VERIFY(jvmti != NULL)) 195 return JNI_ERR; 196 197 registerNativeMethods(jni); 198 199 memset(&caps, 0, sizeof(caps)); 200 caps.can_generate_all_class_hook_events = 1; 201 caps.can_redefine_classes = 1; 202 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) { 203 /* 204 * If VM is run with -Xshare:on agent can't get required capabilities (see 6718407) 205 */ 206 NSK_DISPLAY1("%s: warning: agent failed to get required capabilities, agent finishing\n", agentName); 207 208 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) 209 return JNI_ERR; 210 211 nsk_aod_agentFinished(jni, agentName, 1); 212 } else { 213 agentGotCapabilities = JNI_TRUE; 214 215 memset(&eventCallbacks,0, sizeof(eventCallbacks)); 216 eventCallbacks.ClassLoad = classLoadHandler; 217 eventCallbacks.ClassPrepare = classPrepareHandler; 218 eventCallbacks.ClassFileLoadHook = classFileLoadHoockHandler; 219 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) { 220 return JNI_ERR; 221 } 222 223 if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) { 224 return JNI_ERR; 225 } 226 227 NSK_DISPLAY1("%s: initialization was done\n", agentName); 228 229 if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName))) 230 return JNI_ERR; 231 } 232 233 return JNI_OK; 234 } 235 236 }