1 /* 2 * Copyright (c) 2013, 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 28 #include "jni_tools.h" 29 #include "nsk_tools.h" 30 #include "JVMTITools.h" 31 #include "jvmti_tools.h" 32 33 extern "C" { 34 35 36 static jvmtiEnv *test_jvmti = NULL; 37 static jvmtiCapabilities caps; 38 39 /* 40 * Redefine a class with a new version (class file in byte array). 41 * 42 * native static public boolean redefineClassIntl(Class<?> clz, byte[] classFile); 43 * 44 * @param clz class for redefinition 45 * @param classFile new version as a byte array 46 * @return false if any errors occurred during class redefinition 47 */ 48 JNIEXPORT jboolean JNICALL 49 Java_vm_runtime_defmeth_shared_Util_redefineClassIntl(JNIEnv *env, jclass clazz, jclass clazzToRedefine, jbyteArray bytecodeArray) { 50 jvmtiClassDefinition classDef; 51 jboolean result = JNI_TRUE; 52 53 if (!NSK_VERIFY(env != NULL) || !NSK_VERIFY(clazzToRedefine != NULL) || !NSK_VERIFY(bytecodeArray != NULL)) { 54 return JNI_FALSE; 55 } 56 57 classDef.klass = clazzToRedefine; 58 if (!NSK_JNI_VERIFY(env, 59 (classDef.class_byte_count = /* jsize */ NSK_CPP_STUB2(GetArrayLength, env, bytecodeArray)) > 0)) { 60 return JNI_FALSE; 61 } 62 63 if (!NSK_JNI_VERIFY(env, 64 (classDef.class_bytes = (const unsigned char *) /* jbyte* */ NSK_CPP_STUB3(GetByteArrayElements, env, bytecodeArray, NULL)) != NULL)) { 65 return JNI_FALSE; 66 } 67 68 if (!NSK_JVMTI_VERIFY( 69 NSK_CPP_STUB3(RedefineClasses, test_jvmti, 1, &classDef))) { 70 result = JNI_FALSE; 71 } 72 73 // Need to cleanup reference to byte[] whether RedefineClasses succeeded or not 74 if (!NSK_JNI_VERIFY_VOID(env, 75 NSK_CPP_STUB4(ReleaseByteArrayElements, env, bytecodeArray, (jbyte*)classDef.class_bytes, JNI_ABORT))) { 76 return JNI_FALSE; 77 } 78 79 return result; 80 } 81 82 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 83 /* init framework and parse options */ 84 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 85 return JNI_ERR; 86 87 /* create JVMTI environment */ 88 if (!NSK_VERIFY((test_jvmti = 89 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 90 return JNI_ERR; 91 92 memset(&caps, 0, sizeof(jvmtiCapabilities)); 93 caps.can_redefine_classes = 1; 94 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, 95 test_jvmti, &caps))) 96 return JNI_ERR; 97 98 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetCapabilities, 99 test_jvmti, &caps))) 100 return JNI_ERR; 101 102 if (!caps.can_redefine_classes) 103 printf("Warning: RedefineClasses is not implemented\n"); 104 105 return JNI_OK; 106 } 107 108 JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { 109 return Agent_Initialize(jvm, options, reserved); 110 } 111 112 JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) { 113 return Agent_Initialize(jvm, options, reserved); 114 } 115 116 }