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 "jvmti.h" 27 #include "agent_common.h" 28 #include "jni_tools.h" 29 #include "jvmti_tools.h" 30 31 extern "C" { 32 33 #define PASSED 0 34 #define STATUS_FAILED 2 35 36 /* ============================================================================= */ 37 38 static jlong timeout = 0; 39 static const char* segment = NULL; 40 static const char* EXP_CLASS_SIGNATURE = "Lnsk/jvmti/scenarios/general_functions/GF04/gf04t001;"; 41 static jrawMonitorID countLock; 42 static jboolean classLoadReceived = JNI_FALSE, classPrepareReceived = JNI_FALSE; 43 static jint result = PASSED; 44 45 /* ============================================================================= */ 46 47 /** 48 * Add segment to bootstrap classloader path. 49 * @returns NSK_FALSE if any error occured. 50 */ 51 static int addSegment(jvmtiEnv* jvmti, const char segment[], const char where[]) { 52 void* storage = NULL; 53 54 NSK_DISPLAY1("Add segment: %s\n", segment); 55 if (!NSK_JVMTI_VERIFY(jvmti->AddToBootstrapClassLoaderSearch(segment))) { 56 return NSK_FALSE; 57 } 58 NSK_DISPLAY0(" ... added\n"); 59 60 return NSK_TRUE; 61 } 62 63 static void setupLock(jvmtiEnv *jvmti_env, JNIEnv *jni_env) { 64 if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorEnter(countLock))) 65 jni_env->FatalError("failed to enter a raw monitor\n"); 66 } 67 68 static void setoffLock(jvmtiEnv *jvmti_env, JNIEnv *jni_env) { 69 if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorExit(countLock))) 70 jni_env->FatalError("failed to exit a raw monitor\n"); 71 } 72 73 JNIEXPORT jint JNICALL 74 Java_nsk_jvmti_scenarios_general_1functions_GF04_gf04t001_check( 75 JNIEnv *env, jobject obj) { 76 if (result == PASSED && classLoadReceived == JNI_TRUE && classPrepareReceived == JNI_TRUE) { 77 return PASSED; 78 } 79 return STATUS_FAILED; 80 } 81 82 /* ============================================================================= */ 83 84 /** callback for ClassLoad event **/ 85 void JNICALL 86 ClassLoad(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread, jclass klass) { 87 char *sig, *generic; 88 89 setupLock(jvmti_env, env); 90 91 if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &sig, &generic))) { 92 result = STATUS_FAILED; 93 } 94 95 if (strcmp(sig, EXP_CLASS_SIGNATURE) == 0) { 96 NSK_DISPLAY1("CHECK PASSED: ClassLoad event received for the class \"%s\" as expected\n", 97 sig); 98 classLoadReceived = JNI_TRUE; 99 100 if (!NSK_JVMTI_VERIFY(jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_CLASS_LOAD, NULL))) { 101 result = STATUS_FAILED; 102 } else { 103 NSK_DISPLAY0("ClassLoad event disabled\n"); 104 } 105 } 106 107 setoffLock(jvmti_env, env); 108 } 109 110 /** callback for ClassPrepare event **/ 111 void JNICALL 112 ClassPrepare(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread, jclass klass) { 113 char *sig, *generic; 114 115 setupLock(jvmti_env, env); 116 117 if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &sig, &generic))) { 118 result = STATUS_FAILED; 119 } 120 121 if (strcmp(sig, EXP_CLASS_SIGNATURE) == 0) { 122 NSK_DISPLAY1("CHECK PASSED: ClassPrepare event received for the class \"%s\" as expected\n", 123 sig); 124 classPrepareReceived = JNI_TRUE; 125 126 if (!NSK_JVMTI_VERIFY(jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_CLASS_PREPARE, NULL))) { 127 result = STATUS_FAILED; 128 } else { 129 NSK_DISPLAY0("ClassPrepare event disabled\n"); 130 } 131 } 132 133 setoffLock(jvmti_env, env); 134 } 135 136 /* ============================================================================= */ 137 138 /** Agent library initialization. */ 139 #ifdef STATIC_BUILD 140 JNIEXPORT jint JNICALL Agent_OnLoad_gf04t001(JavaVM *jvm, char *options, void *reserved) { 141 return Agent_Initialize(jvm, options, reserved); 142 } 143 JNIEXPORT jint JNICALL Agent_OnAttach_gf04t001(JavaVM *jvm, char *options, void *reserved) { 144 return Agent_Initialize(jvm, options, reserved); 145 } 146 JNIEXPORT jint JNI_OnLoad_gf04t001(JavaVM *jvm, char *options, void *reserved) { 147 return JNI_VERSION_1_8; 148 } 149 #endif 150 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 151 jvmtiEnv* jvmti = NULL; 152 153 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 154 return JNI_ERR; 155 156 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 157 158 segment = nsk_jvmti_findOptionStringValue("segment", NULL); 159 if (!NSK_VERIFY(segment != NULL)) 160 return JNI_ERR; 161 162 if (!NSK_VERIFY((jvmti = 163 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 164 return JNI_ERR; 165 166 if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("eventLock", &countLock))) 167 return JNI_ERR; 168 169 NSK_DISPLAY0("Add bootstrap class load segment in Agent_OnLoad()\n"); 170 if (!addSegment(jvmti, segment, "Agent_OnLoad()")) { 171 return JNI_ERR; 172 } 173 174 NSK_DISPLAY0("Setting callbacks for events:\n"); 175 { 176 jvmtiEventCallbacks callbacks; 177 jint size = (jint)sizeof(callbacks); 178 179 memset(&callbacks, 0, sizeof(callbacks)); 180 callbacks.ClassLoad = &ClassLoad; 181 callbacks.ClassPrepare = &ClassPrepare; 182 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, size))) { 183 return JNI_ERR; 184 } 185 } 186 NSK_DISPLAY0(" ... set\n"); 187 188 NSK_DISPLAY0("Enabling events: \n"); 189 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL))) { 190 return JNI_ERR; 191 } else { 192 NSK_DISPLAY0(" ... ClassLoad enabled\n"); 193 } 194 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL))) { 195 return JNI_ERR; 196 } else { 197 NSK_DISPLAY0(" ... ClassPrepare enabled\n"); 198 } 199 200 return JNI_OK; 201 } 202 203 /* ============================================================================= */ 204 205 }