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 "ExceptionCheckingJniEnv.hpp" 28 #include "jni_tools.h" 29 #include "jvmti_tools.h" 30 31 extern "C" { 32 33 /* scaffold objects */ 34 static jvmtiEnv *jvmti = NULL; 35 static jlong timeout = 0; 36 37 #define TESTED_CLASS_NAME "nsk/jvmti/scenarios/bcinstr/BI01/bi01t001a" 38 39 static jint newClassSize; 40 static unsigned char* newClassBytes; 41 static jvmtiClassDefinition oldClassDef; 42 43 /* ============================================================================= */ 44 /* 45 * Class: nsk_jvmti_scenarios_bcinstr_BI01_bi01t001 46 * Method: setNewByteCode 47 * Signature: ([B)Z 48 */ 49 JNIEXPORT jboolean JNICALL 50 Java_nsk_jvmti_scenarios_bcinstr_BI01_bi01t001_setNewByteCode(JNIEnv *jni, 51 jobject o, 52 jbyteArray byteCode) { 53 ExceptionCheckingJniEnvPtr jni_env(jni); 54 jbyte* elements; 55 jboolean isCopy; 56 57 newClassSize = jni_env->GetArrayLength(byteCode, TRACE_JNI_CALL); 58 if (newClassSize <= 0) { 59 nsk_jvmti_setFailStatus(); 60 return NSK_FALSE; 61 } 62 NSK_DISPLAY1("\t... got array size: %d\n", newClassSize); 63 64 elements = jni_env->GetByteArrayElements(byteCode, &isCopy, TRACE_JNI_CALL); 65 NSK_DISPLAY1("\t... got elements list: 0x%p\n", (void*)elements); 66 67 if (!NSK_JVMTI_VERIFY(jvmti->Allocate(newClassSize, &newClassBytes))) { 68 nsk_jvmti_setFailStatus(); 69 return NSK_FALSE; 70 } 71 NSK_DISPLAY1("\t... created bytes array: 0x%p\n", (void*)newClassBytes); 72 73 { 74 int j; 75 for (j = 0; j < newClassSize; j++) 76 newClassBytes[j] = (unsigned char)elements[j]; 77 } 78 NSK_DISPLAY1("\t... copied bytecode: %d bytes\n", (int)newClassSize); 79 80 NSK_DISPLAY1("\t... release elements list: 0x%p\n", (void*)elements); 81 jni_env->ReleaseByteArrayElements(byteCode, elements, JNI_ABORT, TRACE_JNI_CALL); 82 NSK_DISPLAY0("\t... released\n"); 83 return NSK_TRUE; 84 } 85 86 /* ============================================================================= */ 87 /* 88 * Class: nsk_jvmti_scenarios_bcinstr_BI01_bi01t001 89 * Method: setClass 90 * Signature: (Ljava/lang/Class;)V 91 */ 92 JNIEXPORT void JNICALL 93 Java_nsk_jvmti_scenarios_bcinstr_BI01_bi01t001_setClass(JNIEnv *jni, 94 jobject o, jclass cls) { 95 ExceptionCheckingJniEnvPtr jni_env(jni); 96 oldClassDef.klass = (jclass) jni_env->NewGlobalRef(cls, TRACE_JNI_CALL); 97 } 98 99 /* ============================================================================= */ 100 101 /** Callback function for ClassFileLoadHook event. */ 102 JNIEXPORT void JNICALL 103 cbClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv* jni_env, 104 jclass class_being_redefined, jobject loader, const char* name, 105 jobject protection_domain, jint class_data_len, 106 const unsigned char* class_data, jint* new_class_data_len, 107 unsigned char** new_class_data) { 108 109 if (name == NULL || strcmp(name, TESTED_CLASS_NAME)) { 110 return; 111 } 112 113 NSK_DISPLAY3("CLASS_FILE_LOAD_HOOK event: %s\n\treceived bytecode: 0x%p:%d\n", 114 name, (void *)class_data, class_data_len); 115 if (nsk_getVerboseMode()) { 116 nsk_printHexBytes(" ", 16, class_data_len, class_data); 117 } 118 119 { 120 /*store original byte code, it will be used to do final redefinition*/ 121 int j; 122 unsigned char *arr; 123 124 oldClassDef.class_byte_count = class_data_len; 125 if (!NSK_JVMTI_VERIFY(jvmti_env->Allocate(class_data_len, &arr))) { 126 nsk_jvmti_setFailStatus(); 127 return; 128 } 129 for (j = 0; j < class_data_len; j++) { 130 arr[j] = class_data[j]; 131 } 132 oldClassDef.class_bytes = arr; 133 } 134 135 *new_class_data_len = newClassSize; 136 *new_class_data = newClassBytes; 137 138 NSK_DISPLAY2("Replace with new bytecode: 0x%p:%d\n", 139 (void*)newClassBytes, 140 (int)newClassSize); 141 if (nsk_getVerboseMode()) { 142 nsk_printHexBytes(" ", 16, newClassSize, 143 newClassBytes); 144 } 145 } 146 147 /* ============================================================================= */ 148 149 /** Agent algorithm. */ 150 static void JNICALL 151 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) { 152 ExceptionCheckingJniEnvPtr jni(agentJNI); 153 154 /*Wait for debuggee to read new byte codes nsk_jvmti_waitForSync#1*/ 155 NSK_DISPLAY0("Wait for debuggee to read new byte codes nsk_jvmti_waitForSync#1\n"); 156 if (!nsk_jvmti_waitForSync(timeout)) 157 return; 158 159 if (!nsk_jvmti_resumeSync()) 160 return; 161 162 NSK_DISPLAY0("Wait for debuggee to load tested class by classLoader\n"); 163 /*Wait for debuggee to load next class nsk_jvmti_waitForSync#2*/ 164 if (!nsk_jvmti_waitForSync(timeout)) 165 return; 166 167 if (!nsk_jvmti_resumeSync()) 168 return; 169 170 /*Wait for debuggee to check instrumentation code works nsk_jvmti_waitForSync#3*/ 171 NSK_DISPLAY0("Wait for debuggee to check instrumentation code works nsk_jvmti_waitForSync#3\n"); 172 if (!nsk_jvmti_waitForSync(timeout)) 173 return; 174 175 NSK_DISPLAY0("Notification disabled for CLASS_FILE_LOAD_HOOK event\n"); 176 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_DISABLE, 177 JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, 178 NULL))) { 179 nsk_jvmti_setFailStatus(); 180 return; 181 } 182 183 if (!nsk_jvmti_resumeSync()) 184 return; 185 186 /*Wait for debuggee to set classes to be redefined nsk_jvmti_waitForSync#4*/ 187 NSK_DISPLAY0("Wait for debuggee to set classes to be redefined nsk_jvmti_waitForSync#4\n"); 188 if (!nsk_jvmti_waitForSync(timeout)) 189 return; 190 191 NSK_DISPLAY0("Redfine class with old byte code\n"); 192 NSK_DISPLAY3("class definition:\n\t0x%p, 0x%p:%d\n", 193 oldClassDef.klass, 194 oldClassDef.class_bytes, 195 oldClassDef.class_byte_count); 196 if (nsk_getVerboseMode()) { 197 nsk_printHexBytes(" ", 16, oldClassDef.class_byte_count, 198 oldClassDef.class_bytes); 199 } 200 if (!NSK_JVMTI_VERIFY(jvmti->RedefineClasses(1, &oldClassDef))) { 201 nsk_jvmti_setFailStatus(); 202 return; 203 } 204 205 if (!nsk_jvmti_resumeSync()) 206 return; 207 208 /*Wait for debuggee to check old byte code works nsk_jvmti_waitForSync#5*/ 209 NSK_DISPLAY0("Wait for debuggee to check old byte code works nsk_jvmti_waitForSync#5\n"); 210 if (!nsk_jvmti_waitForSync(timeout)) 211 return; 212 213 jni->DeleteGlobalRef(oldClassDef.klass, TRACE_JNI_CALL); 214 215 NSK_DISPLAY0("Let debuggee to finish\n"); 216 if (!nsk_jvmti_resumeSync()) 217 return; 218 219 } 220 221 /* ============================================================================= */ 222 223 /** Agent library initialization. */ 224 #ifdef STATIC_BUILD 225 JNIEXPORT jint JNICALL Agent_OnLoad_bi01t001(JavaVM *jvm, char *options, void *reserved) { 226 return Agent_Initialize(jvm, options, reserved); 227 } 228 JNIEXPORT jint JNICALL Agent_OnAttach_bi01t001(JavaVM *jvm, char *options, void *reserved) { 229 return Agent_Initialize(jvm, options, reserved); 230 } 231 JNIEXPORT jint JNI_OnLoad_bi01t001(JavaVM *jvm, char *options, void *reserved) { 232 return JNI_VERSION_1_8; 233 } 234 #endif 235 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 236 237 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 238 return JNI_ERR; 239 240 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 241 242 jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved); 243 if (!NSK_VERIFY(jvmti != NULL)) 244 return JNI_ERR; 245 246 { 247 jvmtiCapabilities caps; 248 memset(&caps, 0, sizeof(caps)); 249 250 caps.can_redefine_classes = 1; 251 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) 252 return JNI_ERR; 253 } 254 255 NSK_DISPLAY0("Set callback for CLASS_FILE_LOAD_HOOK event\n"); 256 { 257 jvmtiEventCallbacks callbacks; 258 jint size = (jint)sizeof(callbacks); 259 260 memset(&callbacks, 0, size); 261 callbacks.ClassFileLoadHook = cbClassFileLoadHook; 262 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, size))) { 263 return JNI_ERR; 264 } 265 } 266 267 NSK_DISPLAY0("Set notification enabled for CLASS_FILE_LOAD_HOOK event\n"); 268 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, 269 JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, 270 NULL))) { 271 nsk_jvmti_setFailStatus(); 272 return NSK_FALSE; 273 } 274 275 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 276 return JNI_ERR; 277 278 return JNI_OK; 279 } 280 281 /* ============================================================================= */ 282 283 284 }