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