1 /* 2 * Copyright (c) 2003, 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 <stdio.h> 25 #include <string.h> 26 #include <jvmti.h> 27 #include "agent_common.h" 28 #include "JVMTITools.h" 29 30 extern "C" { 31 32 33 #define STATUS_FAILED 2 34 #define PASSED 0 35 36 static jvmtiEnv *jvmti = NULL; 37 static jvmtiCapabilities caps; 38 static jvmtiEventCallbacks callbacks; 39 static jint result = PASSED; 40 static jboolean printdump = JNI_FALSE; 41 42 const char* CLASS_NAME = "nsk/jvmti/unit/events/redefineCFLH/JvmtiTestr"; 43 44 void JNICALL 45 VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) { 46 if (printdump == JNI_TRUE) { 47 printf("VMInit event received\n"); 48 } 49 } 50 51 void JNICALL 52 ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *env, 53 jclass redefined_class, 54 jobject loader, const char* name, 55 jobject protection_domain, 56 jint class_data_len, 57 const unsigned char* class_data, 58 jint* new_class_data_len, 59 unsigned char** new_class_data) { 60 61 jvmtiError err; 62 int len = sizeof(jint); 63 char *sig; 64 char *gen; 65 66 if (name != NULL && (strcmp(name, CLASS_NAME) == 0)) { 67 if (printdump == JNI_TRUE) { 68 printf("Received class file load hook event for class %s\n", name); 69 } 70 71 if (redefined_class != NULL) { 72 err = jvmti->GetClassSignature(redefined_class, &sig, &gen); 73 if (err != JVMTI_ERROR_NONE) { 74 printf("(GetClassSignature) unexpected error: %s (%d)\n", 75 TranslateError(err), err); 76 result = STATUS_FAILED; 77 } else if (printdump == JNI_TRUE) { 78 printf("redefined class name signature is %s\n", sig); 79 } 80 } 81 82 err = jvmti->Allocate(class_data_len, new_class_data); 83 if (err != JVMTI_ERROR_NONE) { 84 printf("(Allocate) unexpected error: %s (%d)\n", 85 TranslateError(err), err); 86 result = STATUS_FAILED; 87 } else { 88 *new_class_data_len = class_data_len; 89 memcpy(*new_class_data, class_data, class_data_len); 90 } 91 } 92 } 93 94 #ifdef STATIC_BUILD 95 JNIEXPORT jint JNICALL Agent_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) { 96 return Agent_Initialize(jvm, options, reserved); 97 } 98 JNIEXPORT jint JNICALL Agent_OnAttach_JvmtiTest(JavaVM *jvm, char *options, void *reserved) { 99 return Agent_Initialize(jvm, options, reserved); 100 } 101 JNIEXPORT jint JNI_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) { 102 return JNI_VERSION_1_8; 103 } 104 #endif 105 jint Agent_Initialize(JavaVM *vm, char *options, void *reserved) { 106 jint res; 107 jvmtiError err; 108 109 if (options != NULL && strcmp(options, "printdump") == 0) { 110 printdump = JNI_TRUE; 111 } 112 113 if ((res = vm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1)) != JNI_OK) { 114 printf("%s: Failed to call GetEnv: error=%d\n", __FILE__, res); 115 return JNI_ERR; 116 } 117 118 err = jvmti->GetPotentialCapabilities(&caps); 119 if (err != JVMTI_ERROR_NONE) { 120 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n", 121 TranslateError(err), err); 122 return JNI_ERR; 123 } 124 125 err = jvmti->AddCapabilities(&caps); 126 if (err != JVMTI_ERROR_NONE) { 127 printf("(AddCapabilities) unexpected error: %s (%d)\n", 128 TranslateError(err), err); 129 return JNI_ERR; 130 } 131 132 err = jvmti->GetCapabilities(&caps); 133 if (err != JVMTI_ERROR_NONE) { 134 printf("(GetCapabilities) unexpected error: %s (%d)\n", 135 TranslateError(err), err); 136 return JNI_ERR; 137 } 138 139 if (!caps.can_redefine_classes) { 140 printf("Warning: RedefineClasses is not implemented\n"); 141 } 142 143 callbacks.VMInit = &VMInit; 144 callbacks.ClassFileLoadHook = &ClassFileLoadHook; 145 err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)); 146 if (err != JVMTI_ERROR_NONE) { 147 printf("(SetEventCallbacks) unexpected error: %s (%d)\n", 148 TranslateError(err), err); 149 return JNI_ERR; 150 } 151 152 if ((err = (jvmti->SetEventNotificationMode(JVMTI_ENABLE, 153 JVMTI_EVENT_VM_INIT, NULL))) != JVMTI_ERROR_NONE) { 154 printf("Failed to enable event JVMTI_EVENT_VM_INIT: %s (%d)\n", 155 TranslateError(err), err); 156 return JNI_ERR; 157 } 158 159 if ((err = (jvmti->SetEventNotificationMode(JVMTI_ENABLE, 160 JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL))) != JVMTI_ERROR_NONE) { 161 printf("Failed to enable event JVMTI_EVENT_CLASS_FILE_LOAD_HOOK: %s (%d)\n", 162 TranslateError(err), err); 163 return JNI_ERR; 164 } 165 166 return JNI_OK; 167 } 168 169 JNIEXPORT jint JNICALL 170 Java_nsk_jvmti_unit_events_redefineCFLH_JvmtiTest_makeRedefinition(JNIEnv *env, 171 jclass cls, jint fl, jclass redefCls, jbyteArray classBytes) { 172 jvmtiClassDefinition classDef; 173 jvmtiError err; 174 175 if (jvmti == NULL) { 176 printf("JVMTI client was not properly loaded!\n"); 177 return STATUS_FAILED; 178 } 179 180 if (!caps.can_redefine_classes) { 181 return PASSED; 182 } 183 184 /* filling the structure jvmtiClassDefinition */ 185 classDef.klass = redefCls; 186 classDef.class_byte_count = env->GetArrayLength(classBytes); 187 classDef.class_bytes = (unsigned char *) env->GetByteArrayElements(classBytes, NULL); 188 189 if (fl == 2) { 190 printf(">>>>>>>> Invoke RedefineClasses():\n"); 191 printf("\tnew class byte count=%d\n", classDef.class_byte_count); 192 } 193 err = jvmti->RedefineClasses(1, &classDef); 194 if (err != JVMTI_ERROR_NONE) { 195 printf("%s: Failed to call RedefineClasses():\n", __FILE__); 196 printf("\tthe function returned error %d: %s\n", 197 err, TranslateError(err)); 198 printf("\tFor more info about this error see the JVMTI spec.\n"); 199 return STATUS_FAILED; 200 } 201 if (fl == 2) 202 printf("<<<<<<<< RedefineClasses() is successfully done\n"); 203 204 return PASSED; 205 } 206 207 JNIEXPORT jint JNICALL 208 Java_nsk_jvmti_unit_events_redefineCFLH_JvmtiTest_GetResult(JNIEnv *env, jclass cls) { 209 return result; 210 } 211 212 }