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 <stdlib.h>
  25 #include <string.h>
  26 #include "jni_tools.h"
  27 #include "agent_common.h"
  28 #include "jvmti_tools.h"
  29 
  30 #define PASSED 0
  31 #define STATUS_FAILED 2
  32 
  33 extern "C" {
  34 
  35 /* ========================================================================== */
  36 
  37 /* scaffold objects */
  38 static jlong timeout = 0;
  39 
  40 /* test objects */
  41 static jint klass_byte_count = 0;
  42 static unsigned char *klass_bytes = NULL;
  43 static int ClassFileLoadHookEventFlag = NSK_FALSE;
  44 
  45 const char* CLASS_NAME = "nsk/jvmti/scenarios/multienv/MA07/ma07t001a";
  46 static const jint magicNumber_1 = 0x12345678;
  47 static const jint magicNumber_2 = (jint)0x87654321;
  48 static const unsigned char newMagicNumber = 0x1;
  49 
  50 /* ========================================================================== */
  51 
  52 /** callback functions **/
  53 
  54 static void JNICALL
  55 ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
  56         jclass class_being_redefined, jobject loader,
  57         const char* name, jobject protection_domain,
  58         jint class_data_len, const unsigned char* class_data,
  59         jint *new_class_data_len, unsigned char** new_class_data) {
  60     int found_1 = NSK_FALSE;
  61     int found_2 = NSK_FALSE;
  62     jint magicIndex = 0;
  63     jint i;
  64 
  65     if (name != NULL && (strcmp(name, CLASS_NAME) == 0)) {
  66         ClassFileLoadHookEventFlag = NSK_TRUE;
  67         NSK_DISPLAY0("ClassFileLoadHook event\n");
  68 
  69         if (!NSK_VERIFY(class_being_redefined == NULL)) {
  70             nsk_jvmti_setFailStatus();
  71             return;
  72         }
  73 
  74         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(Allocate,
  75                 jvmti_env, class_data_len, &klass_bytes)))
  76             nsk_jvmti_setFailStatus();
  77 
  78         else {
  79             memcpy(klass_bytes, class_data, class_data_len);
  80             klass_byte_count = class_data_len;
  81 
  82             for (i = 0; i < klass_byte_count - 3; i++) {
  83                 if (((jint)klass_bytes[i+3] |
  84                     ((jint)klass_bytes[i+2] << 8) |
  85                     ((jint)klass_bytes[i+1] << 16) |
  86                     ((jint)klass_bytes[i] << 24)) == magicNumber_1) {
  87                     NSK_DISPLAY2("index of 0x%x: %d\n", magicNumber_1, i);
  88                     found_1 = NSK_TRUE;
  89                 } else if (((jint)klass_bytes[i+3] |
  90                     ((jint)klass_bytes[i+2] << 8) |
  91                     ((jint)klass_bytes[i+1] << 16) |
  92                     ((jint)klass_bytes[i] << 24)) == magicNumber_2) {
  93                     NSK_DISPLAY2("index of 0x%x: %d\n", magicNumber_2, i);
  94                     magicIndex = i;
  95                     found_2 = NSK_TRUE;
  96                 }
  97             }
  98 
  99             if (!NSK_VERIFY(!found_1)) {
 100                 NSK_COMPLAIN1("magic number 0x%x unexpectedly found\n",
 101                     magicNumber_1);
 102                 nsk_jvmti_setFailStatus();
 103             }
 104 
 105             if (!NSK_VERIFY(found_2)) {
 106                 NSK_COMPLAIN1("magic number 0x%x not found\n", magicNumber_2);
 107                 nsk_jvmti_setFailStatus();
 108             }
 109 
 110             if (found_2) {
 111                 NSK_DISPLAY1("Instrumenting with %d\n", newMagicNumber);
 112                 klass_bytes[magicIndex] = 0;
 113                 klass_bytes[magicIndex+1] = 0;
 114                 klass_bytes[magicIndex+2] = 0;
 115                 klass_bytes[magicIndex+3] = newMagicNumber;
 116                 *new_class_data = klass_bytes;
 117                 *new_class_data_len = klass_byte_count;
 118             }
 119         }
 120     }
 121 }
 122 
 123 /* ========================================================================== */
 124 
 125 /** Agent algorithm. */
 126 static void JNICALL
 127 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 128 
 129     if (!nsk_jvmti_waitForSync(timeout))
 130         return;
 131 
 132     if (!NSK_VERIFY(ClassFileLoadHookEventFlag)) {
 133         NSK_COMPLAIN0("Missing ClassFileLoadHook event\n");
 134         nsk_jvmti_setFailStatus();
 135     }
 136 
 137     if (!nsk_jvmti_resumeSync())
 138         return;
 139 }
 140 
 141 /* ========================================================================== */
 142 
 143 /** Agent library initialization. */
 144 #ifdef STATIC_BUILD
 145 JNIEXPORT jint JNICALL Agent_OnLoad_ma07t001a(JavaVM *jvm, char *options, void *reserved) {
 146     return Agent_Initialize(jvm, options, reserved);
 147 }
 148 JNIEXPORT jint JNICALL Agent_OnAttach_ma07t001a(JavaVM *jvm, char *options, void *reserved) {
 149     return Agent_Initialize(jvm, options, reserved);
 150 }
 151 JNIEXPORT jint JNI_OnLoad_ma07t001a(JavaVM *jvm, char *options, void *reserved) {
 152     return JNI_VERSION_1_8;
 153 }
 154 #endif
 155 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 156     jvmtiEnv* jvmti = NULL;
 157     jvmtiEventCallbacks callbacks;
 158 
 159     NSK_DISPLAY0("Agent_OnLoad\n");
 160 
 161     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 162         return JNI_ERR;
 163 
 164     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 165 
 166     if (!NSK_VERIFY((jvmti =
 167             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 168         return JNI_ERR;
 169 
 170     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 171         return JNI_ERR;
 172 
 173     memset(&callbacks, 0, sizeof(callbacks));
 174     callbacks.ClassFileLoadHook = &ClassFileLoadHook;
 175     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 176         return JNI_ERR;
 177 
 178     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 179             jvmti, JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))
 180         return JNI_ERR;
 181 
 182     return JNI_OK;
 183 }
 184 
 185 /* ========================================================================== */
 186 
 187 }