1 /*
   2  * Copyright (c) 2004, 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 <string.h>
  25 #include "jvmti.h"
  26 #include "agent_common.h"
  27 #include "jni_tools.h"
  28 #include "jvmti_tools.h"
  29 
  30 extern "C" {
  31 
  32 /* scaffold objects */
  33 static jlong timeout = 0;
  34 
  35 #define PATH_TO_NEW_BYTECODE "pathToNewByteCode"
  36 #define TESTED_CLASS_NAME "java/lang/Object"
  37 
  38 static jint newClassSize;
  39 static unsigned char* newClassBytes;
  40 
  41 static jvmtiClassDefinition classDef;
  42 
  43 /* ============================================================================= */
  44 
  45 int readNewBytecode(jvmtiEnv* jvmti) {
  46 
  47     char filename[256];
  48     FILE *bytecode;
  49     const char *pathToByteCode = nsk_jvmti_findOptionValue(PATH_TO_NEW_BYTECODE);
  50     jint read_bytes;
  51 
  52     if (pathToByteCode)
  53         sprintf(filename,"%s/%s/%s.class",
  54                     pathToByteCode, "newclass02", TESTED_CLASS_NAME);
  55     else
  56         sprintf(filename,"%s/%s.class",
  57                     "newclass02", TESTED_CLASS_NAME);
  58 
  59     NSK_DISPLAY1("Reading new bytecode for java.lang.Object\n\tfile name: %s\n",
  60                         filename);
  61 
  62     bytecode = fopen(filename, "rb");
  63     if (bytecode == NULL) {
  64         NSK_COMPLAIN0("error opening file\n");
  65         return NSK_FALSE;
  66     }
  67 
  68     fseek(bytecode, 0, SEEK_END);
  69     classDef.class_byte_count = ftell(bytecode);
  70     rewind(bytecode);
  71 
  72     if (!NSK_JVMTI_VERIFY(jvmti->Allocate(classDef.class_byte_count, &newClassBytes))) {
  73         NSK_COMPLAIN0("buffer couldn't be allocated\n");
  74         return NSK_FALSE;
  75     }
  76     classDef.class_bytes = newClassBytes;
  77     read_bytes = (jint) fread(newClassBytes, 1,
  78                             classDef.class_byte_count, bytecode);
  79     fclose(bytecode);
  80     if (read_bytes != classDef.class_byte_count) {
  81         NSK_COMPLAIN0("error reading file\n");
  82         return NSK_FALSE;
  83     }
  84 
  85     return NSK_TRUE;
  86 }
  87 
  88 /* ============================================================================= */
  89 
  90 /** Agent algorithm. */
  91 static void JNICALL
  92 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  93 
  94     /*Wait for debuggee to set classes to be redefined nsk_jvmti_waitForSync#4*/
  95     NSK_DISPLAY0("Wait for debuggee to set classes to be redefined nsk_jvmti_waitForSync#4\n");
  96     if (!nsk_jvmti_waitForSync(timeout))
  97         return;
  98 
  99     NSK_DISPLAY1("Find class: %s\n", TESTED_CLASS_NAME);
 100     if (!NSK_JNI_VERIFY(jni, (classDef.klass = jni->FindClass(TESTED_CLASS_NAME)) != NULL)) {
 101         nsk_jvmti_setFailStatus();
 102         return;
 103     }
 104 
 105     if (!NSK_JNI_VERIFY(jni, (classDef.klass = (jclass)
 106             jni->NewGlobalRef(classDef.klass)) != NULL)) {
 107         nsk_jvmti_setFailStatus();
 108         return;
 109     }
 110 
 111     NSK_DISPLAY0("Redfine class with new byte code\n");
 112     NSK_DISPLAY3("class definition:\n\t0x%p, 0x%p:%d\n",
 113                     classDef.klass,
 114                     classDef.class_bytes,
 115                     classDef.class_byte_count);
 116     if (nsk_getVerboseMode()) {
 117         nsk_printHexBytes("   ", 16, classDef.class_byte_count,
 118                                 classDef.class_bytes);
 119     }
 120     if (!NSK_JVMTI_VERIFY(jvmti->RedefineClasses(1, &classDef))) {
 121         nsk_jvmti_setFailStatus();
 122         return;
 123     }
 124 
 125     jni->DeleteGlobalRef(classDef.klass);
 126 
 127     if (!nsk_jvmti_resumeSync())
 128         return;
 129 }
 130 
 131 /* ============================================================================= */
 132 
 133 /** Agent library initialization. */
 134 #ifdef STATIC_BUILD
 135 JNIEXPORT jint JNICALL Agent_OnLoad_bi04t002(JavaVM *jvm, char *options, void *reserved) {
 136     return Agent_Initialize(jvm, options, reserved);
 137 }
 138 JNIEXPORT jint JNICALL Agent_OnAttach_bi04t002(JavaVM *jvm, char *options, void *reserved) {
 139     return Agent_Initialize(jvm, options, reserved);
 140 }
 141 JNIEXPORT jint JNI_OnLoad_bi04t002(JavaVM *jvm, char *options, void *reserved) {
 142     return JNI_VERSION_1_8;
 143 }
 144 #endif
 145 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 146     jvmtiEnv *jvmti = NULL;
 147 
 148     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 149         return JNI_ERR;
 150 
 151     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 152 
 153     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 154         return JNI_ERR;
 155 
 156     {
 157         jvmtiCapabilities caps;
 158         memset(&caps, 0, sizeof(caps));
 159 
 160         caps.can_redefine_classes = 1;
 161         caps.can_redefine_any_class = 1;
 162         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
 163             return JNI_ERR;
 164     }
 165 
 166     if (!NSK_VERIFY(readNewBytecode(jvmti)))
 167         return JNI_ERR;
 168 
 169     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 170         return JNI_ERR;
 171 
 172     return JNI_OK;
 173 }
 174 
 175 /* ============================================================================= */
 176 
 177 
 178 }