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
24 #include <stdio.h>
25 #include <string.h>
26 #include "jvmti.h"
27 #include "agent_common.h"
28 #include "JVMTITools.h"
29
30 extern "C" {
31
32
33 #define PASSED 0
34 #define STATUS_FAILED 2
35
36 #define RETURN_FAILED errCode = STATUS_FAILED; fflush(0); return
37
38 static jvmtiEnv *jvmti = NULL;
39 static jvmtiCapabilities caps;
40 static jvmtiEventCallbacks callbacks;
41 static jint errCode = PASSED;
42 static jboolean printdump = JNI_TRUE;
43
44 static jmethodID midRun = NULL;
45 static jmethodID midCountDownString = NULL;
46 static jmethodID midCheckPoint = NULL;
47
48 static jint framesExpected = 0;
49 static jint framesCount = 0;
50 static jint methodExitEventCount = 0;
51
52 static const char *cls_exp = "Lnsk/jvmti/unit/ForceEarlyReturn/earlyretstr$earlyretThread;";
53
54 static jstring str_exp = NULL;
55 static const char *sig_exp = "(I)Ljava/lang/String;";
56 static const char *name_exp = "countDownString";
57
58 static const char *argName = "nestingCount";
59
check(jvmtiEnv * jvmti_env,jthread thr,jmethodID mid,jlocation loc,jint frame_no)60 void check(jvmtiEnv *jvmti_env, jthread thr, jmethodID mid,
61 jlocation loc, jint frame_no) {
62 jvmtiError err;
63 jclass cls;
64 jlocation loc_exp = (frame_no == 0) ? 0x15 : 0xd;
65 char *sigClass, *name, *sig, *generic;
66 jvmtiLocalVariableEntry *table = NULL;
67 jint entryCount = 0;
68 jint argValue;
69 jint j;
70
71 err = jvmti_env->GetMethodDeclaringClass(mid, &cls);
72 if (err != JVMTI_ERROR_NONE) {
73 printf("(GetMethodDeclaringClass#%d) unexpected error: %s (%d)\n",
74 frame_no, TranslateError(err), err);
75 RETURN_FAILED;
76 }
77
78 err = jvmti_env->GetClassSignature(cls, &sigClass, &generic);
79 if (err != JVMTI_ERROR_NONE) {
80 printf("(GetClassSignature#%d) unexpected error: %s (%d)\n",
81 frame_no, TranslateError(err), err);
82 RETURN_FAILED;
83 }
84
85 err = jvmti_env->GetMethodName(mid, &name, &sig, &generic);
86 if (err != JVMTI_ERROR_NONE) {
87 printf("(GetMethodName#%d) unexpected error: %s (%d)\n",
88 frame_no, TranslateError(err), err);
89 RETURN_FAILED;
90 }
91
92 /* Get Local Variable Table to be able to get the argument value
93 * from current method frame and compare it with the expected value
94 */
95 err = jvmti_env->GetLocalVariableTable(mid, &entryCount, &table);
96 if (err != JVMTI_ERROR_NONE) {
97 printf("(GetLocalVariableTable#%d) unexpected error: %s (%d)\n",
98 frame_no, TranslateError(err), err);
99 RETURN_FAILED;
100 }
101 if (table != NULL) {
102 for (j = 0; j < entryCount; j++) {
103 if (strcmp(table[j].name, argName) == 0) {
104 err = jvmti_env->GetLocalInt(thr, 0,
105 table[j].slot, &argValue);
106 if (err != JVMTI_ERROR_NONE) {
107 printf("(GetLocalInt#%d) unexpected error: %s (%d)\n",
108 frame_no, TranslateError(err), err);
109 RETURN_FAILED;
110 }
111 }
112 }
113 }
114
115 if (printdump == JNI_TRUE) {
116 printf("\n>>> step %d: \"%s.%s%s\"\n", frame_no, sigClass, name, sig);
117 printf(">>> location: 0x%x%08x", (jint)(loc >> 32), (jint)loc);
118 printf(", arg value: %d\n", argValue);
119 }
120
121 if (sigClass == NULL || strcmp(sigClass, cls_exp) != 0) {
122 printf("(step %d) Wrong class sig: \"%s\",\n", frame_no, sigClass);
123 printf(" expected: \"%s\"\n", cls_exp);
124 RETURN_FAILED;
125 }
126 if (name == NULL || strcmp(name, name_exp) != 0) {
127 printf("(step %d) wrong method name: \"%s\",", frame_no, name);
128 printf(" expected: \"%s\"\n", name_exp);
129 RETURN_FAILED;
130 }
131 if (sig == NULL || strcmp(sig, sig_exp) != 0) {
132 printf("(step %d) wrong method sig: \"%s\",", frame_no, sig);
133 printf(" expected: \"%s\"\n", sig_exp);
134 RETURN_FAILED;
135 }
136 if (loc != loc_exp) {
137 printf("(step %d) wrong location: 0x%x%08x,",
138 frame_no, (jint)(loc >> 32), (jint)loc);
139 printf(" expected: 0x%x\n", (jint)loc_exp);
140 RETURN_FAILED;
141 }
142 if (argValue != frame_no) {
143 printf("(step %d) wrong argument value: %d,", frame_no, argValue);
144 printf(" expected: %d\n", frame_no);
145 RETURN_FAILED;
146 }
147
148 if (sigClass != NULL) {
149 jvmti_env->Deallocate((unsigned char*)sigClass);
150 }
151 if (name != NULL) {
152 jvmti_env->Deallocate((unsigned char*)name);
153 }
154 if (sig != NULL) {
155 jvmti_env->Deallocate((unsigned char*)sig);
156 }
157 if (table != NULL) {
158 for (j = 0; j < entryCount; j++) {
159 jvmti_env->Deallocate((unsigned char*)(table[j].name));
160 jvmti_env->Deallocate((unsigned char*)(table[j].signature));
161 }
162 jvmti_env->Deallocate((unsigned char*)table);
163 }
164 if (methodExitEventCount != (framesCount + 1)) {
165 printf("(step %d) wrong methodExitEventCount: %d,",
166 frame_no, methodExitEventCount);
167 printf(" expected: %d\n", framesCount + 1);
168 RETURN_FAILED;
169 }
170 fflush(0);
171 }
172
Breakpoint(jvmtiEnv * jvmti_env,JNIEnv * env,jthread thread,jmethodID method,jlocation location)173 void JNICALL Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *env,
174 jthread thread, jmethodID method, jlocation location) {
175 jvmtiError err;
176
177 if (midCheckPoint != method) {
178 printf("bp: don't know where we get called from");
179 RETURN_FAILED;
180 }
181
182 if (printdump == JNI_TRUE) {
183 printf(">>> breakpoint in checkPoint\n");
184 }
185
186 err = jvmti_env->ClearBreakpoint(midCheckPoint, 0);
187 if (err != JVMTI_ERROR_NONE) {
188 printf("(ClearBreakpoint) unexpected error: %s (%d)\n",
189 TranslateError(err), err);
190 RETURN_FAILED;
191 }
192
193 err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
194 JVMTI_EVENT_SINGLE_STEP, thread);
195 if (err != JVMTI_ERROR_NONE) {
196 printf("Cannot enable single step events: %s (%d)\n",
197 TranslateError(err), err);
198 RETURN_FAILED;
199 }
200
201 err = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE,
202 JVMTI_EVENT_METHOD_EXIT, thread);
203 if (err != JVMTI_ERROR_NONE) {
204 printf("Cannot enable method exit events: %s (%d)\n",
205 TranslateError(err), err);
206 RETURN_FAILED;
207 }
208
209 err = jvmti_env->ForceEarlyReturnVoid(thread);
210 if (err != JVMTI_ERROR_NONE) {
211 printf("(ForceEarlyReturnVoid) unexpected error: %s (%d)\n",
212 TranslateError(err), err);
213 RETURN_FAILED;
214 }
215 fflush(0);
216 }
217
SingleStep(jvmtiEnv * jvmti_env,JNIEnv * env,jthread thread,jmethodID method,jlocation location)218 void JNICALL SingleStep(jvmtiEnv *jvmti_env, JNIEnv *env,
219 jthread thread, jmethodID method, jlocation location) {
220 jvmtiError err;
221
222 if (method == midRun) {
223 if (printdump == JNI_TRUE) {
224 printf(">>> returned early %d frames till method \"run()\"\n",
225 framesCount);
226 }
227
228 err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE,
229 JVMTI_EVENT_SINGLE_STEP, thread);
230 if (err != JVMTI_ERROR_NONE) {
231 printf("Cannot disable single step events: %s (%d)\n",
232 TranslateError(err), err);
233 RETURN_FAILED;
234 }
235 err = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE,
236 JVMTI_EVENT_METHOD_EXIT, thread);
237 if (err != JVMTI_ERROR_NONE) {
238 printf("Cannot disable method exit events: %s (%d)\n",
239 TranslateError(err), err);
240 RETURN_FAILED;
241 }
242 } else {
243 check(jvmti_env, thread, method, location, framesCount);
244 framesCount++;
245 err = jvmti_env->ForceEarlyReturnObject(thread, str_exp);
246 if (err != JVMTI_ERROR_NONE) {
247 printf("(ForceEarlyReturnObject) unexpected error: %s (%d)\n",
248 TranslateError(err), err);
249 RETURN_FAILED;
250 }
251 }
252 fflush(0);
253 }
254
MethodExit(jvmtiEnv * jvmti_env,JNIEnv * env,jthread thread,jmethodID method,jboolean was_popped_by_exception,jvalue value)255 void JNICALL MethodExit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread,
256 jmethodID method, jboolean was_popped_by_exception, jvalue value) {
257 const char* ret_str;
258 const char* exp_str;
259 jstring ret_val = (jstring) value.l;
260
261 methodExitEventCount++;
262 printf("MethodExit event: methodExitEventCount=%d\n", methodExitEventCount);
263 if (method == midRun || method == midCheckPoint) {
264 return;
265 }
266 if (method == midCountDownString) {
267 ret_str = env->GetStringUTFChars(ret_val, 0);
268 exp_str = env->GetStringUTFChars(str_exp, 0);
269 printf("Expected string: \"%s\"\n", exp_str);
270 printf("Returned string: \"%s\"\n", ret_str);
271 if (was_popped_by_exception) {
272 printf("Method was_popped_by_exception unexpectedly\n");
273 errCode = STATUS_FAILED;
274 }
275 }
276 fflush(0);
277 }
278
279 #ifdef STATIC_BUILD
Agent_OnLoad_earlyretstr(JavaVM * jvm,char * options,void * reserved)280 JNIEXPORT jint JNICALL Agent_OnLoad_earlyretstr(JavaVM *jvm, char *options, void *reserved) {
281 return Agent_Initialize(jvm, options, reserved);
282 }
Agent_OnAttach_earlyretstr(JavaVM * jvm,char * options,void * reserved)283 JNIEXPORT jint JNICALL Agent_OnAttach_earlyretstr(JavaVM *jvm, char *options, void *reserved) {
284 return Agent_Initialize(jvm, options, reserved);
285 }
JNI_OnLoad_earlyretstr(JavaVM * jvm,char * options,void * reserved)286 JNIEXPORT jint JNI_OnLoad_earlyretstr(JavaVM *jvm, char *options, void *reserved) {
287 return JNI_VERSION_1_8;
288 }
289 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)290 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
291 jvmtiError err;
292 jint res;
293
294 if (options != NULL && strcmp(options, "printdump") == 0) {
295 printf("Printdump is turned on!\n");
296
297 printdump = JNI_TRUE;
298 }
299
300 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
301 if (res != JNI_OK || jvmti == NULL) {
302 printf("Wrong error code of a valid call to GetEnv!\n");
303 return JNI_ERR;
304 }
305
306 err = jvmti->GetPotentialCapabilities(&caps);
307 if (err != JVMTI_ERROR_NONE) {
308 printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
309 TranslateError(err), err);
310 return JNI_ERR;
311 }
312
313 err = jvmti->AddCapabilities(&caps);
314 if (err != JVMTI_ERROR_NONE) {
315 printf("(AddCapabilities) unexpected error: %s (%d)\n",
316 TranslateError(err), err);
317 return JNI_ERR;
318 }
319
320 err = jvmti->GetCapabilities(&caps);
321 if (err != JVMTI_ERROR_NONE) {
322 printf("(GetCapabilities) unexpected error: %s (%d)\n",
323 TranslateError(err), err);
324 return JNI_ERR;
325 }
326
327 if (!caps.can_force_early_return) {
328 printf("Warning: ForceEarlyReturn is not implemented\n");
329 }
330
331 if (caps.can_generate_breakpoint_events &&
332 caps.can_generate_method_exit_events &&
333 caps.can_generate_single_step_events)
334 {
335 callbacks.Breakpoint = &Breakpoint;
336 callbacks.SingleStep = &SingleStep;
337 callbacks.MethodExit = &MethodExit;
338 err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
339 if (err != JVMTI_ERROR_NONE) {
340 printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
341 TranslateError(err), err);
342 return JNI_ERR;
343 }
344 } else {
345 printf("Warning: Breakpoint or SingleStep event are not implemented\n");
346 }
347
348 return JNI_OK;
349 }
350
351 JNIEXPORT void JNICALL
Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretstr_getReady(JNIEnv * env,jclass c,jclass cls,jint depth,jstring expected_str)352 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretstr_getReady(
353 JNIEnv *env, jclass c, jclass cls, jint depth, jstring expected_str) {
354 jvmtiError err;
355
356 if (jvmti == NULL) {
357 printf("JVMTI client was not properly loaded!\n");
358 RETURN_FAILED;
359 }
360
361 if (!caps.can_force_early_return ||
362 !caps.can_generate_breakpoint_events ||
363 !caps.can_generate_method_exit_events ||
364 !caps.can_generate_single_step_events) {
365 return;
366 }
367
368 midRun = env->GetMethodID(cls, "run", "()V");
369 if (midRun == NULL) {
370 printf("Cannot find Method ID for method run\n");
371 RETURN_FAILED;
372 }
373
374 midCheckPoint = env->GetMethodID(cls, "checkPoint", "()V");
375 if (midCheckPoint == NULL) {
376 printf("Cannot find Method ID for method checkPoint\n");
377 RETURN_FAILED;
378 }
379
380 midCountDownString = env->GetMethodID(cls, "countDownString", sig_exp);
381 if (midCountDownString == NULL) {
382 printf("Cannot find Method ID for method countDownString\n");
383 RETURN_FAILED;
384 }
385
386 err = jvmti->SetBreakpoint(midCheckPoint, 0);
387 if (err != JVMTI_ERROR_NONE) {
388 printf("(SetBreakpoint) unexpected error: %s (%d)\n",
389 TranslateError(err), err);
390 RETURN_FAILED;
391 }
392
393 err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
394 JVMTI_EVENT_BREAKPOINT, NULL);
395 if (err != JVMTI_ERROR_NONE) {
396 printf("Failed to enable BREAKPOINT event: %s (%d)\n",
397 TranslateError(err), err);
398 RETURN_FAILED;
399 } else {
400 str_exp = (jstring) env->NewGlobalRef(expected_str);
401 framesExpected = depth;
402 }
403 }
404
405 JNIEXPORT jint JNICALL
Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretstr_check(JNIEnv * env,jclass cls)406 Java_nsk_jvmti_unit_ForceEarlyReturn_earlyretstr_check(JNIEnv *env, jclass cls) {
407 if (framesCount != framesExpected) {
408 printf("Wrong number of returned early frames: %d, expected: %d\n",
409 framesCount, framesExpected);
410 errCode = STATUS_FAILED;
411 }
412 fflush(0);
413 return errCode;
414 }
415
416 }
417