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 "jni_tools.h"
28 #include "jvmti_tools.h"
29 #include "JVMTITools.h"
30 
31 extern "C" {
32 
33 /* ============================================================================= */
34 
35 /* scaffold objects */
36 static JNIEnv* jni = NULL;
37 static jvmtiEnv *jvmti = NULL;
38 static jlong timeout = 0;
39 static jrawMonitorID syncLock = NULL;
40 
41 /* constant names */
42 #define JVMTI_EVENT_COUNT   (int)(JVMTI_MAX_EVENT_TYPE_VAL - JVMTI_MIN_EVENT_TYPE_VAL + 1)
43 #define EXPECTED_CLASS_NAME "nsk.jvmti.scenarios.events.EM06.em06t001a"
44 #define CLASS_LOADER_COUNT_PARAM "classLoaderCount"
45 
46 static int classLoaderCount = 0;
47 static int classloadEventCount = 0;
48 static int classprepareEventCount = 0;
49 
50 /* ============================================================================= */
51 
52 /* callbacks */
53 
54 void
handler(jvmtiEvent event,jvmtiEnv * jvmti,JNIEnv * jni_env,jthread thread,jclass klass)55 handler(jvmtiEvent event, jvmtiEnv* jvmti, JNIEnv* jni_env,
56                     jthread thread, jclass klass) {
57 
58     jmethodID methodID;
59     jclass classObject;
60     jstring jclassName;
61     const char *className;
62 
63     if (!NSK_JNI_VERIFY(jni_env, (classObject = jni_env->GetObjectClass(klass)) != NULL)) {
64         nsk_jvmti_setFailStatus();
65         return;
66     }
67 
68     if (!NSK_JNI_VERIFY(jni_env, (methodID =
69             jni_env->GetMethodID(classObject, "getName", "()Ljava/lang/String;")) != NULL)) {
70         nsk_jvmti_setFailStatus();
71         return;
72     }
73 
74     jclassName = (jstring) jni_env->CallObjectMethod(klass, methodID);
75 
76     className = jni_env->GetStringUTFChars(jclassName, 0);
77 
78     if (className != NULL && (strcmp(className, EXPECTED_CLASS_NAME) == 0)) {
79 
80         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(syncLock)))
81             nsk_jvmti_setFailStatus();
82 
83         switch (event) {
84             case JVMTI_EVENT_CLASS_LOAD:
85                 classloadEventCount++; break;
86             case JVMTI_EVENT_CLASS_PREPARE:
87                 classprepareEventCount++; break;
88             default:
89                 NSK_COMPLAIN1("Unexpected event %s", TranslateEvent(event));
90                 nsk_jvmti_setFailStatus();
91         }
92 
93         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(syncLock)))
94             nsk_jvmti_setFailStatus();
95 
96     }
97 
98     jni_env->ReleaseStringUTFChars(jclassName, className);
99 }
100 
101 JNIEXPORT void JNICALL
cbClassLoad(jvmtiEnv * jvmti,JNIEnv * jni_env,jthread thread,jclass klass)102 cbClassLoad(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, jclass klass) {
103 
104     handler(JVMTI_EVENT_CLASS_LOAD, jvmti, jni_env, thread, klass);
105 }
106 
107 JNIEXPORT void JNICALL
cbClassPrepare(jvmtiEnv * jvmti,JNIEnv * jni_env,jthread thread,jclass klass)108 cbClassPrepare(jvmtiEnv* jvmti, JNIEnv* jni_env, jthread thread, jclass klass) {
109 
110     handler(JVMTI_EVENT_CLASS_PREPARE, jvmti, jni_env, thread, klass);
111 }
112 
113 /* ============================================================================= */
114 
115 static int
enableEvent(jvmtiEventMode enable,jvmtiEvent event)116 enableEvent(jvmtiEventMode enable, jvmtiEvent event) {
117     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(enable, event, NULL))) {
118         nsk_jvmti_setFailStatus();
119         return NSK_FALSE;
120     }
121 
122     return NSK_TRUE;
123 }
124 
125 /* ============================================================================= */
126 
127 /**
128  * Testcase: check tested events.
129  *   - check if expected events received for each method
130  *
131  * Returns NSK_TRUE if test may continue; or NSK_FALSE for test break.
132  */
checkEvents()133 int checkEvents() {
134 
135     int result = NSK_TRUE;
136 
137     if (classloadEventCount == classLoaderCount) {
138         NSK_DISPLAY1("Expected number of JVMTI_EVENT_CLASS_LOAD events %d\n",
139                             classloadEventCount);
140     } else {
141         NSK_COMPLAIN2("Unexpected number of JVMTI_EVENT_CLASS_LOAD events %d\n\texpected value %d\n",
142                             classloadEventCount,
143                             classLoaderCount);
144         result = NSK_FALSE;
145     }
146 
147     if (classprepareEventCount == classLoaderCount) {
148         NSK_DISPLAY1("Expected number of JVMTI_EVENT_CLASS_PREPARE events %d\n",
149                             classloadEventCount);
150     } else {
151         NSK_COMPLAIN2("Unexpected number of JVMTI_EVENT_CLASS_PREPARE events %d\n\texpected value %d\n",
152                             classprepareEventCount,
153                             classLoaderCount);
154         result = NSK_FALSE;
155     }
156 
157     return result;
158 }
159 
160 /* ============================================================================= */
161 
162 static int
setCallBacks()163 setCallBacks() {
164     jvmtiEventCallbacks eventCallbacks;
165     memset(&eventCallbacks, 0, sizeof(eventCallbacks));
166 
167     eventCallbacks.ClassLoad    = cbClassLoad;
168     eventCallbacks.ClassPrepare = cbClassPrepare;
169 
170     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))))
171         return NSK_FALSE;
172 
173     return NSK_TRUE;
174 }
175 
176 /* ============================================================================= */
177 
178 /** Agent algorithm. */
179 static void JNICALL
agentProc(jvmtiEnv * jvmti,JNIEnv * agentJNI,void * arg)180 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
181 
182     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_syncLock", &syncLock))) {
183         nsk_jvmti_setFailStatus();
184         return;
185     }
186 
187     jni = agentJNI;
188 
189     NSK_DISPLAY0("Wait for debuggee to become ready\n");
190     if (!nsk_jvmti_waitForSync(timeout))
191         return;
192 
193     if (!setCallBacks()) {
194         return;
195     }
196 
197     if (!enableEvent(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD)
198             || !enableEvent(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE)) {
199         NSK_COMPLAIN0("Events could not be enabled");
200         nsk_jvmti_setFailStatus();
201         return;
202     }
203 
204     NSK_DISPLAY0("Let debuggee to load class\n");
205     if (!nsk_jvmti_resumeSync())
206         return;
207 
208     if (!nsk_jvmti_waitForSync(timeout))
209         return;
210 
211     if (!checkEvents()) {
212         nsk_jvmti_setFailStatus();
213     }
214 
215     if (!enableEvent(JVMTI_DISABLE, JVMTI_EVENT_CLASS_LOAD)
216             || !enableEvent(JVMTI_DISABLE, JVMTI_EVENT_CLASS_PREPARE)) {
217         NSK_COMPLAIN0("Events could not be disabled");
218         nsk_jvmti_setFailStatus();
219     }
220 
221     NSK_DISPLAY0("Let debuggee to finish\n");
222     if (!nsk_jvmti_resumeSync())
223         return;
224 
225     if (!NSK_JVMTI_VERIFY(jvmti->DestroyRawMonitor(syncLock)))
226         nsk_jvmti_setFailStatus();
227 
228 }
229 
230 /* ============================================================================= */
231 
232 /** Agent library initialization. */
233 #ifdef STATIC_BUILD
Agent_OnLoad_em06t001(JavaVM * jvm,char * options,void * reserved)234 JNIEXPORT jint JNICALL Agent_OnLoad_em06t001(JavaVM *jvm, char *options, void *reserved) {
235     return Agent_Initialize(jvm, options, reserved);
236 }
Agent_OnAttach_em06t001(JavaVM * jvm,char * options,void * reserved)237 JNIEXPORT jint JNICALL Agent_OnAttach_em06t001(JavaVM *jvm, char *options, void *reserved) {
238     return Agent_Initialize(jvm, options, reserved);
239 }
JNI_OnLoad_em06t001(JavaVM * jvm,char * options,void * reserved)240 JNIEXPORT jint JNI_OnLoad_em06t001(JavaVM *jvm, char *options, void *reserved) {
241     return JNI_VERSION_1_8;
242 }
243 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)244 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
245 
246     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
247         return JNI_ERR;
248 
249     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
250     classLoaderCount = nsk_jvmti_findOptionIntValue(CLASS_LOADER_COUNT_PARAM, 100);
251 
252     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
253         return JNI_ERR;
254 
255     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
256         return JNI_ERR;
257 
258     return JNI_OK;
259 }
260 
261 /* ============================================================================= */
262 
263 
264 }
265