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 magicIndex = i; 90 } else if (((jint)klass_bytes[i+3] | 91 ((jint)klass_bytes[i+2] << 8) | 92 ((jint)klass_bytes[i+1] << 16) | 93 ((jint)klass_bytes[i] << 24)) == magicNumber_2) { 94 NSK_DISPLAY2("index of 0x%x: %d\n", magicNumber_2, i); 95 found_2 = NSK_TRUE; 96 } 97 } 98 99 if (!NSK_VERIFY(found_1)) { 100 NSK_COMPLAIN1("magic number 0x%x not found\n", magicNumber_1); 101 nsk_jvmti_setFailStatus(); 102 } 103 104 if (!NSK_VERIFY(found_2)) { 105 NSK_COMPLAIN1("magic number 0x%x not found\n", magicNumber_2); 106 nsk_jvmti_setFailStatus(); 107 } 108 109 if (found_1) { 110 NSK_DISPLAY1("Instrumenting with %d\n", newMagicNumber); 111 klass_bytes[magicIndex] = 0; 112 klass_bytes[magicIndex+1] = 0; 113 klass_bytes[magicIndex+2] = 0; 114 klass_bytes[magicIndex+3] = newMagicNumber; 115 *new_class_data = klass_bytes; 116 *new_class_data_len = klass_byte_count; 117 } 118 } 119 } 120 } 121 122 /* ========================================================================== */ 123 124 /** Agent algorithm. */ 125 static void JNICALL 126 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 127 128 if (!nsk_jvmti_waitForSync(timeout)) 129 return; 130 131 if (!NSK_VERIFY(ClassFileLoadHookEventFlag)) { 132 NSK_COMPLAIN0("Missing ClassFileLoadHook event\n"); 133 nsk_jvmti_setFailStatus(); 134 } 135 136 if (!nsk_jvmti_resumeSync()) 137 return; 138 } 139 140 /* ========================================================================== */ 141 142 /** Agent library initialization. */ 143 #ifdef STATIC_BUILD 144 JNIEXPORT jint JNICALL Agent_OnLoad_ma07t001(JavaVM *jvm, char *options, void *reserved) { 145 return Agent_Initialize(jvm, options, reserved); 146 } 147 JNIEXPORT jint JNICALL Agent_OnAttach_ma07t001(JavaVM *jvm, char *options, void *reserved) { 148 return Agent_Initialize(jvm, options, reserved); 149 } 150 JNIEXPORT jint JNI_OnLoad_ma07t001(JavaVM *jvm, char *options, void *reserved) { 151 return JNI_VERSION_1_8; 152 } 153 #endif 154 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 155 jvmtiEnv* jvmti = NULL; 156 jvmtiEventCallbacks callbacks; 157 158 NSK_DISPLAY0("Agent_OnLoad\n"); 159 160 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 161 return JNI_ERR; 162 163 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 164 165 if (!NSK_VERIFY((jvmti = 166 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 167 return JNI_ERR; 168 169 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 170 return JNI_ERR; 171 172 memset(&callbacks, 0, sizeof(callbacks)); 173 callbacks.ClassFileLoadHook = &ClassFileLoadHook; 174 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks))) 175 return JNI_ERR; 176 177 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 178 jvmti, JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL))) 179 return JNI_ERR; 180 181 return JNI_OK; 182 } 183 184 /* ========================================================================== */ 185 186 }