1 /*
   2  * Copyright (c) 2016, 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)\n", count);
  95       }
  96     }
  97 }
  98 
  99 static jint init_options(char *options) {
 100   char* class_name;
 101   char* from;
 102   char* to;
 103 
 104   fprintf(stderr, "Agent library loaded with options = %s\n", options);
 105   if ((class_name = options) != NULL &&
 106       (from = strchr(class_name, ',')) != NULL && (from[1] != 0)) {
 107     *from = 0;
 108     from++;
 109     if ((to = strchr(from, ',')) != NULL && (to[1] != 0)) {
 110       *to = 0;
 111       to++;
 112       if (strchr(to, ',') == NULL &&
 113           strlen(to) == strlen(from) &&
 114           strlen(class_name) > 0 &&
 115           strlen(to) > 0) {
 116         CLASS_NAME = strdup(class_name);
 117         FROM = strdup(from);
 118         TO = strdup(to);
 119         fprintf(stderr, "CLASS_NAME = %s, FROM = %s, TO = %s\n",
 120                 CLASS_NAME, FROM, TO);
 121         return JNI_OK;
 122       }
 123     }
 124   }
 125   fprintf(stderr,
 126           "Incorrect options. You need to start the JVM with -agentlib:ClassFileLoadHook=<classname>,<from>,<to>\n"
 127           "where <classname> is the class you want to hook, <from> is the string in the classfile to be replaced\n"
 128           "with <to>.  <from> and <to> must have the same length. Example:\n"
 129           "    @run main/native -agentlib:ClassFileLoadHook=Foo,XXX,YYY ClassFileLoadHookTest\n");
 130   return JNI_ERR;
 131 }
 132 
 133 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 134   int rc;
 135 
 136   if ((rc = (*jvm)->GetEnv(jvm, (void **)&jvmti, JVMTI_VERSION_1_1)) != JNI_OK) {
 137     fprintf(stderr, "Unable to create jvmtiEnv, GetEnv failed, error = %d\n", rc);
 138     return JNI_ERR;
 139   }
 140   if ((rc = init_options(options)) != JNI_OK) {
 141     return JNI_ERR;
 142   }
 143 
 144   (void) memset(&callbacks, 0, sizeof(callbacks));
 145   callbacks.ClassFileLoadHook = &ClassFileLoadHook;
 146   if ((rc = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks))) != JNI_OK) {
 147     fprintf(stderr, "SetEventCallbacks failed, error = %d\n", rc);
 148     return JNI_ERR;
 149   }
 150 
 151   if ((rc = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
 152                                                JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)) != JNI_OK) {
 153     fprintf(stderr, "SetEventNotificationMode failed, error = %d\n", rc);
 154     return JNI_ERR;
 155   }
 156 
 157   return JNI_OK;
 158 }
 159 
 160 JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
 161   return Agent_Initialize(jvm, options, reserved);
 162 }
 163 
 164 JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
 165   return Agent_Initialize(jvm, options, reserved);
 166 }