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