Path: blob/master/test/hotspot/jtreg/testlibrary/jvmti/libSimpleClassFileLoadHook.c
40943 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* A simple way to test JVMTI ClassFileLoadHook. See ../testlibrary_tests/jvmti/SimpleClassFileLoadHookTest.java25* for an example.26*/27#include <stdio.h>28#include <stdarg.h>29#include <stdlib.h>30#include <string.h>3132#include <jvmti.h>33#include <jni.h>3435static char* CLASS_NAME = NULL;36static char* FROM = NULL;37static char* TO = NULL;38static jvmtiEnv *jvmti = NULL;39static jvmtiEventCallbacks callbacks;4041/**42* For all classes whose name equals to CLASS_NAME, we replace all occurrence of FROM to TO43* in the classfile data. CLASS_NAME must be a binary class name.44*45* FROM is usually chosen as part of a UTF8 string in the class file. For example, if the46* original class file has47* String getXXX() { return "theXXX";}48* You can set FROM=XXX, TO=YYY to rewrite the class to be49* String getYYY() { return "theYYY";}50*51* Please note that the replacement is NOT limited just the UTF8 strings, but rather applies52* to all the bytes in the classfile. So if you pick a very short FROM string like X,53* it may override any POP2 bytecodes, which have the value 88 (ascii 'X').54*55* A good FROM string to use is 'cellphone', where the first 4 bytes represent the bytecode56* sequence DADD/LSUB/IDIV/IDIV, which does not appear in valid bytecode streams.57*/58void JNICALL59ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *env, jclass class_beeing_redefined,60jobject loader, const char* name, jobject protection_domain,61jint class_data_len, const unsigned char* class_data,62jint *new_class_data_len, unsigned char** new_class_data) {6364if (name != NULL && (strcmp(name, CLASS_NAME) == 0)) {65size_t n = strlen(FROM);66unsigned char* new_data;6768if ((*jvmti)->Allocate(jvmti, class_data_len, &new_data) == JNI_OK) {69const unsigned char* s = class_data;70unsigned char* d = new_data;71unsigned char* end = d + class_data_len;72int count = 0;7374fprintf(stderr, "found class to be hooked: %s - rewriting ...\n", name);7576while (d + n < end) {77if (memcmp(s, FROM, n) == 0) {78memcpy(d, TO, n);79s += n;80d += n;81count++;82} else {83*d++ = *s++;84}85}86while (d < end) {87*d++ = *s++;88}8990*new_class_data_len = class_data_len;91*new_class_data = new_data;9293fprintf(stderr, "Rewriting done. Replaced %d occurrence(s) of \"%s\" to \"%s\"\n", count, FROM, TO);94}95}96}9798static int early = 0;99100static jint init_options(char *options) {101char* class_name;102char* from;103char* to;104105fprintf(stderr, "Agent library loaded with options = %s\n", options);106if (options != NULL && strncmp(options, "-early,", 7) == 0) {107early = 1;108options += 7;109}110if ((class_name = options) != NULL &&111(from = strchr(class_name, ',')) != NULL && (from[1] != 0)) {112*from = 0;113from++;114if ((to = strchr(from, ',')) != NULL && (to[1] != 0)) {115*to = 0;116to++;117if (strchr(to, ',') == NULL &&118strlen(to) == strlen(from) &&119strlen(class_name) > 0 &&120strlen(to) > 0) {121CLASS_NAME = strdup(class_name);122FROM = strdup(from);123TO = strdup(to);124fprintf(stderr, "CLASS_NAME = %s, FROM = %s, TO = %s\n",125CLASS_NAME, FROM, TO);126return JNI_OK;127}128}129}130fprintf(stderr,131"Incorrect options. You need to start the JVM with -agentlib:ClassFileLoadHook=<classname>,<from>,<to>\n"132"where <classname> is the class you want to hook, <from> is the string in the classfile to be replaced\n"133"with <to>. <from> and <to> must have the same length. Example:\n"134" @run main/native -agentlib:ClassFileLoadHook=Foo,XXX,YYY ClassFileLoadHookTest\n");135return JNI_ERR;136}137138static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {139int rc;140jvmtiCapabilities caps;141142if ((rc = (*jvm)->GetEnv(jvm, (void **)&jvmti, JVMTI_VERSION_1_1)) != JNI_OK) {143fprintf(stderr, "Unable to create jvmtiEnv, GetEnv failed, error = %d\n", rc);144return JNI_ERR;145}146if ((rc = init_options(options)) != JNI_OK) {147return JNI_ERR;148}149150memset(&caps, 0, sizeof(caps));151152caps.can_redefine_classes = 1;153if (early) {154fprintf(stderr, "can_generate_all_class_hook_events/can_generate_early_vmstart/can_generate_early_class_hook_events == 1\n");155caps.can_generate_all_class_hook_events = 1;156caps.can_generate_early_class_hook_events = 1;157}158if ((rc = (*jvmti)->AddCapabilities(jvmti, &caps)) != JNI_OK) {159fprintf(stderr, "AddCapabilities failed, error = %d\n", rc);160return JNI_ERR;161}162163(void) memset(&callbacks, 0, sizeof(callbacks));164callbacks.ClassFileLoadHook = &ClassFileLoadHook;165if ((rc = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks))) != JNI_OK) {166fprintf(stderr, "SetEventCallbacks failed, error = %d\n", rc);167return JNI_ERR;168}169170if ((rc = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,171JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)) != JNI_OK) {172fprintf(stderr, "SetEventNotificationMode failed, error = %d\n", rc);173return JNI_ERR;174}175176return JNI_OK;177}178179JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {180return Agent_Initialize(jvm, options, reserved);181}182183JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {184return Agent_Initialize(jvm, options, reserved);185}186187188