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 "ExceptionCheckingJniEnv.hpp"
  28 #include "jni_tools.h"
  29 #include "jvmti_tools.h"
  30 
  31 extern "C" {
  32 
  33 /* scaffold objects */
  34 static jvmtiEnv *jvmti = NULL;
  35 static jlong timeout = 0;
  36 
  37 #define TESTED_CLASS_NAME   "nsk/jvmti/scenarios/bcinstr/BI01/bi01t001a"
  38 
  39 static jint newClassSize;
  40 static unsigned char* newClassBytes;
  41 static jvmtiClassDefinition oldClassDef;
  42 
  43 /* ============================================================================= */
  44 /*
  45  * Class:     nsk_jvmti_scenarios_bcinstr_BI01_bi01t001
  46  * Method:    setNewByteCode
  47  * Signature: ([B)Z
  48  */
  49 JNIEXPORT jboolean JNICALL
  50 Java_nsk_jvmti_scenarios_bcinstr_BI01_bi01t001_setNewByteCode(JNIEnv *jni,
  51                                                               jobject o,
  52                                                               jbyteArray byteCode) {
  53     ExceptionCheckingJniEnvPtr jni_env(jni);
  54     jbyte* elements;
  55     jboolean isCopy;
  56 
  57     newClassSize = jni_env->GetArrayLength(byteCode, TRACE_JNI_CALL);
  58     if (newClassSize <= 0) {
  59         nsk_jvmti_setFailStatus();
  60         return NSK_FALSE;
  61     }
  62     NSK_DISPLAY1("\t... got array size: %d\n", newClassSize);
  63 
  64     elements = jni_env->GetByteArrayElements(byteCode, &isCopy, TRACE_JNI_CALL);
  65     if (elements == NULL) {
  66         nsk_jvmti_setFailStatus();
  67         return NSK_FALSE;
  68     }
  69     NSK_DISPLAY1("\t... got elements list: 0x%p\n", (void*)elements);
  70 
  71     if (!NSK_JVMTI_VERIFY(jvmti->Allocate(newClassSize, &newClassBytes))) {
  72         nsk_jvmti_setFailStatus();
  73         return NSK_FALSE;
  74     }
  75     NSK_DISPLAY1("\t... created bytes array: 0x%p\n", (void*)newClassBytes);
  76 
  77     {
  78         int j;
  79         for (j = 0; j < newClassSize; j++)
  80             newClassBytes[j] = (unsigned char)elements[j];
  81     }
  82     NSK_DISPLAY1("\t... copied bytecode: %d bytes\n", (int)newClassSize);
  83 
  84     NSK_DISPLAY1("\t... release elements list: 0x%p\n", (void*)elements);
  85     jni_env->ReleaseByteArrayElements(byteCode, elements, JNI_ABORT, TRACE_JNI_CALL);
  86     NSK_DISPLAY0("\t... released\n");
  87     return NSK_TRUE;
  88 }
  89 
  90 /* ============================================================================= */
  91 /*
  92  * Class:     nsk_jvmti_scenarios_bcinstr_BI01_bi01t001
  93  * Method:    setClass
  94  * Signature: (Ljava/lang/Class;)V
  95  */
  96 JNIEXPORT void JNICALL
  97 Java_nsk_jvmti_scenarios_bcinstr_BI01_bi01t001_setClass(JNIEnv *jni,
  98                         jobject o, jclass cls) {
  99     ExceptionCheckingJniEnvPtr jni_env(jni);
 100     oldClassDef.klass = (jclass) jni_env->NewGlobalRef(cls, TRACE_JNI_CALL);
 101     if (oldClassDef.klass == NULL) {
 102         nsk_jvmti_setFailStatus();
 103     }
 104 }
 105 
 106 /* ============================================================================= */
 107 
 108 /** Callback function for ClassFileLoadHook event. */
 109 JNIEXPORT void JNICALL
 110 cbClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv* jni_env,
 111             jclass class_being_redefined, jobject loader, const char* name,
 112             jobject protection_domain, jint class_data_len,
 113             const unsigned char* class_data, jint* new_class_data_len,
 114             unsigned char** new_class_data) {
 115 
 116     if (name == NULL || strcmp(name, TESTED_CLASS_NAME)) {
 117         return;
 118     }
 119 
 120     NSK_DISPLAY3("CLASS_FILE_LOAD_HOOK event: %s\n\treceived bytecode: 0x%p:%d\n",
 121                         name, (void *)class_data, class_data_len);
 122     if (nsk_getVerboseMode()) {
 123         nsk_printHexBytes("   ", 16, class_data_len, class_data);
 124     }
 125 
 126     {
 127         /*store original byte code, it will be used to do final redefinition*/
 128         int j;
 129         unsigned char *arr;
 130 
 131         oldClassDef.class_byte_count = class_data_len;
 132         if (!NSK_JVMTI_VERIFY(jvmti_env->Allocate(class_data_len, &arr))) {
 133             nsk_jvmti_setFailStatus();
 134             return;
 135         }
 136         for (j = 0; j < class_data_len; j++) {
 137             arr[j] = class_data[j];
 138         }
 139         oldClassDef.class_bytes = arr;
 140     }
 141 
 142     *new_class_data_len = newClassSize;
 143     *new_class_data = newClassBytes;
 144 
 145     NSK_DISPLAY2("Replace with new bytecode: 0x%p:%d\n",
 146                                 (void*)newClassBytes,
 147                                 (int)newClassSize);
 148     if (nsk_getVerboseMode()) {
 149         nsk_printHexBytes("   ", 16, newClassSize,
 150                                 newClassBytes);
 151     }
 152 }
 153 
 154 /* ============================================================================= */
 155 
 156 /** Agent algorithm. */
 157 static void JNICALL
 158 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
 159     ExceptionCheckingJniEnvPtr jni(agentJNI);
 160 
 161     /*Wait for debuggee to read new byte codes nsk_jvmti_waitForSync#1*/
 162     NSK_DISPLAY0("Wait for debuggee to read new byte codes nsk_jvmti_waitForSync#1\n");
 163     if (!nsk_jvmti_waitForSync(timeout))
 164         return;
 165 
 166     if (!nsk_jvmti_resumeSync())
 167         return;
 168 
 169     NSK_DISPLAY0("Wait for debuggee to load tested class by classLoader\n");
 170     /*Wait for debuggee to load next class nsk_jvmti_waitForSync#2*/
 171     if (!nsk_jvmti_waitForSync(timeout))
 172         return;
 173 
 174     if (!nsk_jvmti_resumeSync())
 175         return;
 176 
 177     /*Wait for debuggee to check instrumentation code works nsk_jvmti_waitForSync#3*/
 178     NSK_DISPLAY0("Wait for debuggee to check instrumentation code works nsk_jvmti_waitForSync#3\n");
 179     if (!nsk_jvmti_waitForSync(timeout))
 180         return;
 181 
 182     NSK_DISPLAY0("Notification disabled for CLASS_FILE_LOAD_HOOK event\n");
 183     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_DISABLE,
 184                                                           JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
 185                                                           NULL))) {
 186         nsk_jvmti_setFailStatus();
 187         return;
 188     }
 189 
 190     if (!nsk_jvmti_resumeSync())
 191         return;
 192 
 193     /*Wait for debuggee to set classes to be redefined nsk_jvmti_waitForSync#4*/
 194     NSK_DISPLAY0("Wait for debuggee to set classes to be redefined nsk_jvmti_waitForSync#4\n");
 195     if (!nsk_jvmti_waitForSync(timeout))
 196         return;
 197 
 198     NSK_DISPLAY0("Redfine class with old byte code\n");
 199     NSK_DISPLAY3("class definition:\n\t0x%p, 0x%p:%d\n",
 200                     oldClassDef.klass,
 201                     oldClassDef.class_bytes,
 202                     oldClassDef.class_byte_count);
 203     if (nsk_getVerboseMode()) {
 204         nsk_printHexBytes("   ", 16, oldClassDef.class_byte_count,
 205                                 oldClassDef.class_bytes);
 206     }
 207     if (!NSK_JVMTI_VERIFY(jvmti->RedefineClasses(1, &oldClassDef))) {
 208         nsk_jvmti_setFailStatus();
 209         return;
 210     }
 211 
 212     if (!nsk_jvmti_resumeSync())
 213         return;
 214 
 215     /*Wait for debuggee to check old byte code works nsk_jvmti_waitForSync#5*/
 216     NSK_DISPLAY0("Wait for debuggee to check old byte code works nsk_jvmti_waitForSync#5\n");
 217     if (!nsk_jvmti_waitForSync(timeout))
 218         return;
 219 
 220     jni->DeleteGlobalRef(oldClassDef.klass, TRACE_JNI_CALL);
 221 
 222     NSK_DISPLAY0("Let debuggee to finish\n");
 223     if (!nsk_jvmti_resumeSync())
 224         return;
 225 
 226 }
 227 
 228 /* ============================================================================= */
 229 
 230 /** Agent library initialization. */
 231 #ifdef STATIC_BUILD
 232 JNIEXPORT jint JNICALL Agent_OnLoad_bi01t001(JavaVM *jvm, char *options, void *reserved) {
 233     return Agent_Initialize(jvm, options, reserved);
 234 }
 235 JNIEXPORT jint JNICALL Agent_OnAttach_bi01t001(JavaVM *jvm, char *options, void *reserved) {
 236     return Agent_Initialize(jvm, options, reserved);
 237 }
 238 JNIEXPORT jint JNI_OnLoad_bi01t001(JavaVM *jvm, char *options, void *reserved) {
 239     return JNI_VERSION_1_8;
 240 }
 241 #endif
 242 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 243 
 244     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 245         return JNI_ERR;
 246 
 247     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 248 
 249     jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved);
 250     if (!NSK_VERIFY(jvmti != NULL))
 251         return JNI_ERR;
 252 
 253     {
 254         jvmtiCapabilities caps;
 255         memset(&caps, 0, sizeof(caps));
 256 
 257         caps.can_redefine_classes = 1;
 258         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
 259             return JNI_ERR;
 260     }
 261 
 262     NSK_DISPLAY0("Set callback for CLASS_FILE_LOAD_HOOK event\n");
 263     {
 264         jvmtiEventCallbacks callbacks;
 265         jint size = (jint)sizeof(callbacks);
 266 
 267         memset(&callbacks, 0, size);
 268         callbacks.ClassFileLoadHook = cbClassFileLoadHook;
 269         if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, size))) {
 270             return JNI_ERR;
 271         }
 272     }
 273 
 274     NSK_DISPLAY0("Set notification enabled for CLASS_FILE_LOAD_HOOK event\n");
 275     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE,
 276                                                           JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,
 277                                                           NULL))) {
 278         nsk_jvmti_setFailStatus();
 279         return NSK_FALSE;
 280     }
 281 
 282     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 283         return JNI_ERR;
 284 
 285     return JNI_OK;
 286 }
 287 
 288 /* ============================================================================= */
 289 
 290 
 291 }