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