1 /*
   2  * Copyright (c) 2007, 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 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <string.h>
  26 #include <jni.h>
  27 #include <jvmti.h>
  28 #include <aod.h>
  29 #include <jvmti_aod.h>
  30 #include "ExceptionCheckingJniEnv.hpp"
  31 
  32 extern "C" {
  33 
  34 /*
  35  * Expected agent work scenario:
  36  *  - receive ClassFileLoadHook event for class 'ClassToRedefine'
  37  *  - receive ClassLoad event for class 'ClassToRedefine' and redefine class from ClassLoad event handler
  38  *  - receive one more ClassFileLoadHook event for class 'ClassToRedefine'
  39  *  - receive ClassPrepare event for class 'ClassToRedefine' and finish work
  40  */
  41 
  42 #define REDEFINED_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach002/ClassToRedefine;"
  43 #define REDEFINED_CLASS_FILE_NAME "nsk/jvmti/AttachOnDemand/attach002/ClassToRedefine"
  44 
  45 // class name in the ClassFileLoadHook callback
  46 #define REDEFINED_CLASS_NAME_INTERNAL "nsk/jvmti/AttachOnDemand/attach002/ClassToRedefine"
  47 
  48 static Options* options = NULL;
  49 static const char* agentName;
  50 
  51 static volatile jboolean agentGotCapabilities = JNI_FALSE;
  52 
  53 static jvmtiEvent testEvents[] = {
  54         JVMTI_EVENT_CLASS_LOAD,
  55         JVMTI_EVENT_CLASS_PREPARE,
  56         JVMTI_EVENT_CLASS_FILE_LOAD_HOOK};
  57 
  58 static const int testEventsNumber = 3;
  59 
  60 static volatile int classLoadReceived = 0;
  61 static volatile int classFileLoadHookReceived = 0;
  62 
  63 JNIEXPORT jboolean JNICALL
  64 Java_nsk_jvmti_AttachOnDemand_attach002_attach002Target_agentGotCapabilities(JNIEnv * jni,
  65         jclass klass, jobject obj) {
  66     return agentGotCapabilities;
  67 }
  68 
  69 #define ATTACH002_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach002/attach002Target"
  70 
  71 int registerNativeMethods(JNIEnv* jni_env) {
  72     ExceptionCheckingJniEnvPtr jni(jni_env);
  73     jclass appClass;
  74     JNINativeMethod nativeMethods[] = {
  75             {(char*) "agentGotCapabilities", (char*) "()Z", (void*) Java_nsk_jvmti_AttachOnDemand_attach002_attach002Target_agentGotCapabilities}};
  76     jint nativeMethodsNumber = 1;
  77 
  78     appClass = jni->FindClass(ATTACH002_TARGET_APP_CLASS_NAME, TRACE_JNI_CALL);
  79     if (appClass == NULL) {
  80         return NSK_FALSE;
  81     }
  82 
  83     if (jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber, TRACE_JNI_CALL) != 0) {
  84         return NSK_FALSE;
  85     }
  86 
  87     return NSK_TRUE;
  88 }
  89 
  90 void JNICALL  classLoadHandler(
  91         jvmtiEnv *jvmti,
  92         JNIEnv* jni,
  93         jthread thread,
  94         jclass klass) {
  95     char className[MAX_STRING_LENGTH];
  96 
  97     if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {
  98         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
  99         return;
 100     }
 101 
 102     NSK_DISPLAY2("%s: ClassLoad event was received for class '%s'\n", agentName, className);
 103 
 104     if (!strcmp(className, REDEFINED_CLASS_NAME)) {
 105 
 106         classLoadReceived = 1;
 107 
 108         NSK_DISPLAY1("%s: redefining class\n", agentName);
 109 
 110         if (!NSK_VERIFY(nsk_jvmti_aod_redefineClass(options, jvmti, klass, REDEFINED_CLASS_FILE_NAME))) {
 111             NSK_COMPLAIN1("%s: failed to redefine class\n", agentName);
 112             nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
 113         }
 114     }
 115 }
 116 
 117 void JNICALL classPrepareHandler(
 118         jvmtiEnv *jvmti,
 119         JNIEnv* jni,
 120         jthread thread,
 121         jclass klass) {
 122     char className[MAX_STRING_LENGTH];
 123 
 124     if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {
 125         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
 126         return;
 127     }
 128 
 129     NSK_DISPLAY2("%s: ClassPrepare event received for class '%s'\n", agentName, REDEFINED_CLASS_NAME);
 130 
 131     if (!strcmp(className, REDEFINED_CLASS_NAME)) {
 132         int success = 1;
 133 
 134         if (!classLoadReceived) {
 135             success = 0;
 136             NSK_COMPLAIN2("%s: expected ClassLoad event wasn't received for class '%s'\n", agentName, REDEFINED_CLASS_NAME);
 137         }
 138 
 139         /*
 140          * ClassFileLoadHook event should be received twice - when class is loaded and when class is redefined
 141          */
 142         if (classFileLoadHookReceived != 2) {
 143             success = 0;
 144             NSK_COMPLAIN2("%s: expected ClassFileLoadHook event wasn't received for class '%s'\n", agentName, REDEFINED_CLASS_NAME);
 145         }
 146 
 147         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
 148     }
 149 }
 150 
 151 void JNICALL classFileLoadHoockHandler(
 152         jvmtiEnv * jvmti,
 153         JNIEnv * jni,
 154         jclass class_beeing_redefined,
 155         jobject loader,
 156         const char * name,
 157         jobject protection_domain,
 158         jint class_data_len,
 159         const unsigned char * class_data,
 160         jint * new_class_data_len,
 161         unsigned char** new_class_data) {
 162 
 163     if (name != NULL) {
 164         NSK_DISPLAY2("%s: ClassFileLoadHook event received for class '%s'\n", agentName, name);
 165         if (!strcmp(name, REDEFINED_CLASS_NAME_INTERNAL)) {
 166             classFileLoadHookReceived++;
 167         }
 168     } else {
 169         NSK_DISPLAY1("%s: ClassFileLoadHook event received for class with NULL name\n", agentName);
 170     }
 171 }
 172 
 173 #ifdef STATIC_BUILD
 174 JNIEXPORT jint JNI_OnLoad_attach002Agent00(JavaVM *jvm, char *options, void *reserved) {
 175     return JNI_VERSION_1_8;
 176 }
 177 #endif
 178 
 179 JNIEXPORT jint JNICALL
 180 #ifdef STATIC_BUILD
 181 Agent_OnAttach_attach002Agent00(JavaVM *vm, char *optionsString, void *reserved)
 182 #else
 183 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 184 #endif
 185 {
 186     jvmtiEventCallbacks eventCallbacks;
 187     jvmtiCapabilities caps;
 188     jvmtiEnv* jvmti = NULL;
 189     JNIEnv* jni = NULL;
 190 
 191     options = (Options*) nsk_aod_createOptions(optionsString);
 192     if (!NSK_VERIFY(options != NULL))
 193         return JNI_ERR;
 194 
 195     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 196 
 197     jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
 198     if (jni == NULL)
 199         return NSK_FALSE;
 200 
 201     jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
 202     if (!NSK_VERIFY(jvmti != NULL))
 203         return JNI_ERR;
 204 
 205     if (!NSK_VERIFY(registerNativeMethods(jni))) {
 206         return JNI_ERR;
 207     }
 208 
 209     memset(&caps, 0, sizeof(caps));
 210     caps.can_generate_all_class_hook_events = 1;
 211     caps.can_redefine_classes = 1;
 212     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 213         /*
 214          * If VM is run with -Xshare:on agent can't get required capabilities (see 6718407)
 215          */
 216         NSK_DISPLAY1("%s: warning: agent failed to get required capabilities, agent finishing\n", agentName);
 217 
 218         if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 219             return JNI_ERR;
 220 
 221         nsk_aod_agentFinished(jni, agentName, 1);
 222     } else {
 223         agentGotCapabilities = JNI_TRUE;
 224 
 225         memset(&eventCallbacks,0, sizeof(eventCallbacks));
 226         eventCallbacks.ClassLoad = classLoadHandler;
 227         eventCallbacks.ClassPrepare = classPrepareHandler;
 228         eventCallbacks.ClassFileLoadHook = classFileLoadHoockHandler;
 229         if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
 230             return JNI_ERR;
 231         }
 232 
 233         if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
 234             return JNI_ERR;
 235         }
 236 
 237         NSK_DISPLAY1("%s: initialization was done\n", agentName);
 238 
 239         if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 240             return JNI_ERR;
 241     }
 242 
 243     return JNI_OK;
 244 }
 245 
 246 }