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 "jni_tools.h" 28 #include "jvmti_tools.h" 29 30 extern "C" { 31 32 /* scaffold objects */ 33 static jlong timeout = 0; 34 35 #define PATH_TO_NEW_BYTECODE "pathToNewByteCode" 36 #define TESTED_CLASS_NAME "java/lang/Object" 37 38 static jint newClassSize; 39 static unsigned char* newClassBytes; 40 41 static jvmtiClassDefinition classDef; 42 43 /* ============================================================================= */ 44 45 int readNewBytecode(jvmtiEnv* jvmti) { 46 47 char filename[256]; 48 FILE *bytecode; 49 const char *pathToByteCode = nsk_jvmti_findOptionValue(PATH_TO_NEW_BYTECODE); 50 jint read_bytes; 51 52 if (pathToByteCode) 53 sprintf(filename,"%s/%s/%s.class", 54 pathToByteCode, "newclass02", TESTED_CLASS_NAME); 55 else 56 sprintf(filename,"%s/%s.class", 57 "newclass02", TESTED_CLASS_NAME); 58 59 NSK_DISPLAY1("Reading new bytecode for java.lang.Object\n\tfile name: %s\n", 60 filename); 61 62 bytecode = fopen(filename, "rb"); 63 if (bytecode == NULL) { 64 NSK_COMPLAIN0("error opening file\n"); 65 return NSK_FALSE; 66 } 67 68 fseek(bytecode, 0, SEEK_END); 69 classDef.class_byte_count = ftell(bytecode); 70 rewind(bytecode); 71 72 if (!NSK_JVMTI_VERIFY(jvmti->Allocate(classDef.class_byte_count, &newClassBytes))) { 73 NSK_COMPLAIN0("buffer couldn't be allocated\n"); 74 return NSK_FALSE; 75 } 76 classDef.class_bytes = newClassBytes; 77 read_bytes = (jint) fread(newClassBytes, 1, 78 classDef.class_byte_count, bytecode); 79 fclose(bytecode); 80 if (read_bytes != classDef.class_byte_count) { 81 NSK_COMPLAIN0("error reading file\n"); 82 return NSK_FALSE; 83 } 84 85 return NSK_TRUE; 86 } 87 88 /* ============================================================================= */ 89 90 /** Agent algorithm. */ 91 static void JNICALL 92 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 93 94 /*Wait for debuggee to set classes to be redefined nsk_jvmti_waitForSync#4*/ 95 NSK_DISPLAY0("Wait for debuggee to set classes to be redefined nsk_jvmti_waitForSync#4\n"); 96 if (!nsk_jvmti_waitForSync(timeout)) 97 return; 98 99 NSK_DISPLAY1("Find class: %s\n", TESTED_CLASS_NAME); 100 classDef.klass = jni->FindClass(TESTED_CLASS_NAME); 101 if (!NSK_JNI_VERIFY(jni, classDef.klass != NULL)) { 102 nsk_jvmti_setFailStatus(); 103 return; 104 } 105 106 classDef.klass = (jclass) jni->NewGlobalRef(classDef.klass); 107 if (!NSK_JNI_VERIFY(jni, classDef.klass != NULL)) { 108 nsk_jvmti_setFailStatus(); 109 return; 110 } 111 112 NSK_DISPLAY0("Redfine class with new byte code\n"); 113 NSK_DISPLAY3("class definition:\n\t0x%p, 0x%p:%d\n", 114 classDef.klass, 115 classDef.class_bytes, 116 classDef.class_byte_count); 117 if (nsk_getVerboseMode()) { 118 nsk_printHexBytes(" ", 16, classDef.class_byte_count, 119 classDef.class_bytes); 120 } 121 if (!NSK_JVMTI_VERIFY(jvmti->RedefineClasses(1, &classDef))) { 122 nsk_jvmti_setFailStatus(); 123 return; 124 } 125 126 jni->DeleteGlobalRef(classDef.klass); 127 128 if (!nsk_jvmti_resumeSync()) 129 return; 130 } 131 132 /* ============================================================================= */ 133 134 /** Agent library initialization. */ 135 #ifdef STATIC_BUILD 136 JNIEXPORT jint JNICALL Agent_OnLoad_bi04t002(JavaVM *jvm, char *options, void *reserved) { 137 return Agent_Initialize(jvm, options, reserved); 138 } 139 JNIEXPORT jint JNICALL Agent_OnAttach_bi04t002(JavaVM *jvm, char *options, void *reserved) { 140 return Agent_Initialize(jvm, options, reserved); 141 } 142 JNIEXPORT jint JNI_OnLoad_bi04t002(JavaVM *jvm, char *options, void *reserved) { 143 return JNI_VERSION_1_8; 144 } 145 #endif 146 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 147 jvmtiEnv *jvmti = NULL; 148 149 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 150 return JNI_ERR; 151 152 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 153 154 jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved); 155 if (!NSK_VERIFY(jvmti != NULL)) 156 return JNI_ERR; 157 158 { 159 jvmtiCapabilities caps; 160 memset(&caps, 0, sizeof(caps)); 161 162 caps.can_redefine_classes = 1; 163 caps.can_redefine_any_class = 1; 164 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) 165 return JNI_ERR; 166 } 167 168 if (!NSK_VERIFY(readNewBytecode(jvmti))) 169 return JNI_ERR; 170 171 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 172 return JNI_ERR; 173 174 return JNI_OK; 175 } 176 177 /* ============================================================================= */ 178 179 180 }