1 /* 2 * Copyright (c) 2016, 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 /* 25 * A simple way to test JVMTI ClassFileLoadHook. See ../testlibrary_tests/SimpleClassFileLoadHookTest.java 26 * for an example. 27 */ 28 #include <stdio.h> 29 #include <stdarg.h> 30 #include <stdlib.h> 31 #include <string.h> 32 33 #include <jvmti.h> 34 #include <jni.h> 35 36 static char* CLASS_NAME = NULL; 37 static char* FROM = NULL; 38 static char* TO = NULL; 39 static jvmtiEnv *jvmti = NULL; 40 static jvmtiEventCallbacks callbacks; 41 42 /** 43 * For all classes whose name equals to CLASS_NAME, we replace all occurrence of FROM to TO 44 * in the classfile data. CLASS_NAME must be a binary class name. 45 * 46 * FROM is usually chosen as part of a UTF8 string in the class file. For example, if the 47 * original class file has 48 * String getXXX() { return "theXXX";} 49 * You can set FROM=XXX, TO=YYY to rewrite the class to be 50 * String getYYY() { return "theYYY";} 51 * 52 * Please note that the replacement is NOT limited just the UTF8 strings, but rather applies 53 * to all the bytes in the classfile. So if you pick a very short FROM string like X, 54 * it may override any POP2 bytecodes, which have the value 88 (ascii 'X'). 55 * 56 * A good FROM string to use is 'cellphone', where the first 4 bytes represent the bytecode 57 * sequence DADD/LSUB/IDIV/IDIV, which does not appear in valid bytecode streams. 58 */ 59 void JNICALL 60 ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *env, jclass class_beeing_redefined, 61 jobject loader, const char* name, jobject protection_domain, 62 jint class_data_len, const unsigned char* class_data, 63 jint *new_class_data_len, unsigned char** new_class_data) { 64 65 if (name != NULL && (strcmp(name, CLASS_NAME) == 0)) { 66 size_t n = strlen(FROM); 67 unsigned char* new_data; 68 69 if ((*jvmti)->Allocate(jvmti, class_data_len, &new_data) == JNI_OK) { 70 const unsigned char* s = class_data; 71 unsigned char* d = new_data; 72 unsigned char* end = d + class_data_len; 73 int count = 0; 74 75 fprintf(stderr, "found class to be hooked: %s - rewriting ...\n", name); 76 77 while (d + n < end) { 78 if (memcmp(s, FROM, n) == 0) { 79 memcpy(d, TO, n); 80 s += n; 81 d += n; 82 count++; 83 } else { 84 *d++ = *s++; 85 } 86 } 87 while (d < end) { 88 *d++ = *s++; 89 } 90 91 *new_class_data_len = class_data_len; 92 *new_class_data = new_data; 93 94 fprintf(stderr, "Rewriting done. Replaced %d occurrence(s) of \"%s\" to \"%s\"\n", count, FROM, TO); 95 } 96 } 97 } 98 99 static int early = 0; 100 101 static jint init_options(char *options) { 102 char* class_name; 103 char* from; 104 char* to; 105 106 fprintf(stderr, "Agent library loaded with options = %s\n", options); 107 if (options != NULL && strncmp(options, "-early,", 7) == 0) { 108 early = 1; 109 options += 7; 110 } 111 if ((class_name = options) != NULL && 112 (from = strchr(class_name, ',')) != NULL && (from[1] != 0)) { 113 *from = 0; 114 from++; 115 if ((to = strchr(from, ',')) != NULL && (to[1] != 0)) { 116 *to = 0; 117 to++; 118 if (strchr(to, ',') == NULL && 119 strlen(to) == strlen(from) && 120 strlen(class_name) > 0 && 121 strlen(to) > 0) { 122 CLASS_NAME = strdup(class_name); 123 FROM = strdup(from); 124 TO = strdup(to); 125 fprintf(stderr, "CLASS_NAME = %s, FROM = %s, TO = %s\n", 126 CLASS_NAME, FROM, TO); 127 return JNI_OK; 128 } 129 } 130 } 131 fprintf(stderr, 132 "Incorrect options. You need to start the JVM with -agentlib:ClassFileLoadHook=<classname>,<from>,<to>\n" 133 "where <classname> is the class you want to hook, <from> is the string in the classfile to be replaced\n" 134 "with <to>. <from> and <to> must have the same length. Example:\n" 135 " @run main/native -agentlib:ClassFileLoadHook=Foo,XXX,YYY ClassFileLoadHookTest\n"); 136 return JNI_ERR; 137 } 138 139 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 140 int rc; 141 jvmtiCapabilities caps; 142 143 if ((rc = (*jvm)->GetEnv(jvm, (void **)&jvmti, JVMTI_VERSION_1_1)) != JNI_OK) { 144 fprintf(stderr, "Unable to create jvmtiEnv, GetEnv failed, error = %d\n", rc); 145 return JNI_ERR; 146 } 147 if ((rc = init_options(options)) != JNI_OK) { 148 return JNI_ERR; 149 } 150 151 memset(&caps, 0, sizeof(caps)); 152 153 caps.can_redefine_classes = 1; 154 if (early) { 155 fprintf(stderr, "can_generate_all_class_hook_events/can_generate_early_vmstart/can_generate_early_class_hook_events == 1\n"); 156 caps.can_generate_all_class_hook_events = 1; 157 caps.can_generate_early_class_hook_events = 1; 158 } 159 if ((rc = (*jvmti)->AddCapabilities(jvmti, &caps)) != JNI_OK) { 160 fprintf(stderr, "AddCapabilities failed, error = %d\n", rc); 161 return JNI_ERR; 162 } 163 164 (void) memset(&callbacks, 0, sizeof(callbacks)); 165 callbacks.ClassFileLoadHook = &ClassFileLoadHook; 166 if ((rc = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks))) != JNI_OK) { 167 fprintf(stderr, "SetEventCallbacks failed, error = %d\n", rc); 168 return JNI_ERR; 169 } 170 171 if ((rc = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, 172 JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)) != JNI_OK) { 173 fprintf(stderr, "SetEventNotificationMode failed, error = %d\n", rc); 174 return JNI_ERR; 175 } 176 177 return JNI_OK; 178 } 179 180 JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { 181 return Agent_Initialize(jvm, options, reserved); 182 } 183 184 JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) { 185 return Agent_Initialize(jvm, options, reserved); 186 }