1 /*
2  * Copyright (c) 2003, 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 "jni_tools.h"
27 #include "agent_common.h"
28 #include "jvmti_tools.h"
29 
30 extern "C" {
31 
32 #define CAPABILITY can_suspend
33 #define CAPABILITY_STR "can_suspend"
34 
35 /* The test checks capability can_suspend
36  * and correspondent function:
37  *     SuspendThread
38  *     SuspendThreadList
39  *     ResumeThread
40  *     ResumeThreadList
41  *
42  * Testcases:
43  *   1. Check if GetPotentialCapabilities returns the capability
44  *   2. Add the capability during Live phase
45  *   3. Check if GetCapabilities returns the capability
46  *   4. Check that only correspondent functions work and functions of
47  *      other capabilities return JVMTI_ERROR_MUST_POSSESS_CAPABILITY
48  *   5. Relinquish the capability during Live phase
49  *   6. Check if GetCapabilities does not return the capability
50  *   7. Check that correspondent to relinquished capability functions
51  *      return JVMTI_ERROR_MUST_POSSESS_CAPABILITY
52  *   8. Add back the capability and check with GetCapabilities
53  *   9. Check if VM exits well with the capability has not been relinquished
54  */
55 
56 /* ========================================================================== */
57 
58 /* scaffold objects */
59 static JNIEnv* jni = NULL;
60 static jvmtiEnv *jvmti = NULL;
61 static jlong timeout = 0;
62 
63 /* test objects */
64 static jthread thread = NULL;
65 static jclass klass = NULL;
66 static jmethodID method = NULL;
67 static jfieldID field = NULL;
68 
69 /* ========================================================================== */
70 
prepare()71 static int prepare() {
72     const char* THREAD_NAME = "Debuggee Thread";
73     jvmtiThreadInfo info;
74     jthread *threads = NULL;
75     jint threads_count = 0;
76     int i;
77 
78     NSK_DISPLAY0("Prepare: find tested thread\n");
79 
80     /* get all live threads */
81     if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threads_count, &threads)))
82         return NSK_FALSE;
83 
84     if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
85         return NSK_FALSE;
86 
87     /* find tested thread */
88     for (i = 0; i < threads_count; i++) {
89         if (!NSK_VERIFY(threads[i] != NULL))
90             return NSK_FALSE;
91 
92         /* get thread information */
93         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(threads[i], &info)))
94             return NSK_FALSE;
95 
96         NSK_DISPLAY3("    thread #%d (%s): %p\n", i, info.name, threads[i]);
97 
98         /* find by name */
99         if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) {
100             thread = threads[i];
101         }
102     }
103 
104     /* deallocate threads list */
105     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads)))
106         return NSK_FALSE;
107 
108     /* get tested thread class */
109     if (!NSK_JNI_VERIFY(jni, (klass = jni->GetObjectClass(thread)) != NULL))
110         return NSK_FALSE;
111 
112     /* get tested thread method 'run' */
113     if (!NSK_JNI_VERIFY(jni, (method = jni->GetMethodID(klass, "run", "()V")) != NULL))
114         return NSK_FALSE;
115 
116     /* get tested thread field 'waitingMonitor' */
117     if (!NSK_JNI_VERIFY(jni, (field =
118             jni->GetFieldID(klass, "waitingMonitor", "Ljava/lang/Object;")) != NULL))
119         return NSK_FALSE;
120 
121     return NSK_TRUE;
122 }
123 
124 /* ========================================================================== */
125 
126 /* Check GetPotentialCapabilities function
127  */
checkGetPotentialCapabilities()128 static int checkGetPotentialCapabilities() {
129     jvmtiCapabilities caps;
130 
131     if (!NSK_JVMTI_VERIFY(jvmti->GetPotentialCapabilities(&caps)))
132         return NSK_FALSE;
133     if (!caps.CAPABILITY) {
134         NSK_COMPLAIN1("GetPotentialCapabilities does not return \"%s\" capability\n",
135             CAPABILITY_STR);
136         return NSK_FALSE;
137     }
138 
139     return NSK_TRUE;
140 }
141 
142 /* Check AddCapabilities function
143  */
checkAddCapabilities()144 static int checkAddCapabilities() {
145     jvmtiCapabilities caps;
146 
147     memset(&caps, 0, sizeof(caps));
148     caps.CAPABILITY = 1;
149     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
150         return NSK_FALSE;
151 
152     return NSK_TRUE;
153 }
154 
155 /* Check GetCapabilities function
156  */
checkGetCapabilities(int owe)157 static int checkGetCapabilities(int owe) {
158     jvmtiCapabilities caps;
159 
160     memset(&caps, 0, sizeof(caps));
161     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
162         return NSK_FALSE;
163     if (owe && !caps.CAPABILITY) {
164         NSK_COMPLAIN1("GetCapabilities does not return \"%s\" capability\n",
165             CAPABILITY_STR);
166         return NSK_FALSE;
167     } else if (!owe && caps.CAPABILITY) {
168         NSK_COMPLAIN1("GetCapabilities returns relinquished \"%s\" capability\n",
169             CAPABILITY_STR);
170         return NSK_FALSE;
171     }
172 
173     return NSK_TRUE;
174 }
175 
176 /* Check RelinquishCapabilities function
177  */
checkRelinquishCapabilities()178 static int checkRelinquishCapabilities() {
179     jvmtiCapabilities caps;
180 
181     memset(&caps, 0, sizeof(caps));
182     caps.CAPABILITY = 1;
183     if (!NSK_JVMTI_VERIFY(jvmti->RelinquishCapabilities(&caps)))
184         return NSK_FALSE;
185 
186     return NSK_TRUE;
187 }
188 
189 /* ========================================================================== */
190 
191 /* Check "can_suspend" functions
192  */
checkSuspend(int positive)193 static int checkSuspend(int positive) {
194     jvmtiError err;
195 
196     if (positive) {
197         NSK_DISPLAY0("Checking positive: SuspendThread\n");
198         if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(thread)))
199             return NSK_FALSE;
200 
201         NSK_DISPLAY0("Checking positive: ResumeThread\n");
202         if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(thread)))
203             return NSK_FALSE;
204 
205         NSK_DISPLAY0("Checking positive: SuspendThreadList\n");
206         if (!NSK_JVMTI_VERIFY(jvmti->SuspendThreadList(1, &thread, &err)))
207             return NSK_FALSE;
208 
209         NSK_DISPLAY0("Checking positive: ResumeThreadList\n");
210         if (!NSK_JVMTI_VERIFY(jvmti->ResumeThreadList(1, &thread, &err)))
211             return NSK_FALSE;
212     } else {
213         NSK_DISPLAY0("Checking negative: SuspendThread\n");
214         if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
215                 jvmti->SuspendThread(thread)))
216             return NSK_FALSE;
217 
218         NSK_DISPLAY0("Checking negative: ResumeThread\n");
219         if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
220                 jvmti->ResumeThread(thread)))
221             return NSK_FALSE;
222 
223         NSK_DISPLAY0("Checking negative: SuspendThreadList\n");
224         if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
225                 jvmti->SuspendThreadList(1, &thread, &err)))
226             return NSK_FALSE;
227 
228         NSK_DISPLAY0("Checking negative: ResumeThreadList\n");
229         if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
230                 jvmti->ResumeThreadList(1, &thread, &err)))
231             return NSK_FALSE;
232     }
233 
234     return NSK_TRUE;
235 }
236 
237 /* Check "can_signal_thread" functions
238  */
checkSignalThread()239 static int checkSignalThread() {
240     const char* THREAD_DEATH_CLASS_NAME = "java/lang/ThreadDeath";
241     const char* THREAD_DEATH_CTOR_NAME = "<init>";
242     const char* THREAD_DEATH_CTOR_SIGNATURE = "()V";
243     jclass cls = NULL;
244     jmethodID ctor = NULL;
245     jobject exception = NULL;
246 
247     if (!NSK_JNI_VERIFY(jni, (cls = jni->FindClass(THREAD_DEATH_CLASS_NAME)) != NULL))
248         return NSK_FALSE;
249 
250     if (!NSK_JNI_VERIFY(jni, (ctor =
251             jni->GetMethodID(cls, THREAD_DEATH_CTOR_NAME, THREAD_DEATH_CTOR_SIGNATURE)) != NULL))
252         return NSK_FALSE;
253 
254     if (!NSK_JNI_VERIFY(jni, (exception = jni->NewObject(cls, ctor)) != NULL))
255         return NSK_FALSE;
256 
257     NSK_DISPLAY0("Checking negative: StopThread\n");
258     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
259             jvmti->StopThread(thread, exception)))
260         return NSK_FALSE;
261 
262     NSK_DISPLAY0("Checking negative: InterruptThread\n");
263     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY, jvmti->InterruptThread(thread)))
264         return NSK_FALSE;
265 
266     return NSK_TRUE;
267 }
268 
269 /* Check "can_get_owned_monitor_info" function
270  */
checkGetOwnedMonitorInfo()271 static int checkGetOwnedMonitorInfo() {
272     jint count;
273     jobject *monitors = NULL;
274 
275     NSK_DISPLAY0("Checking negative: GetOwnedMonitorInfo\n");
276     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
277             jvmti->GetOwnedMonitorInfo(thread, &count, &monitors)))
278         return NSK_FALSE;
279 
280     return NSK_TRUE;
281 }
282 
283 /* Check "can_get_current_contended_monitor" function
284  */
checkGetCurrentContendedMonitor()285 static int checkGetCurrentContendedMonitor() {
286     jobject monitor = NULL;
287 
288     NSK_DISPLAY0("Checking negative: GetCurrentContendedMonitor\n");
289     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
290             jvmti->GetCurrentContendedMonitor(thread, &monitor)))
291         return NSK_FALSE;
292 
293     return NSK_TRUE;
294 }
295 
296 /* Check "can_pop_frame" function
297  */
checkPopFrame()298 static int checkPopFrame() {
299     NSK_DISPLAY0("Checking negative: PopFrame\n");
300     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY, jvmti->PopFrame(thread)))
301         return NSK_FALSE;
302 
303     return NSK_TRUE;
304 }
305 
306 /* Check "can_tag_objects" functions
307  */
308 
309 static jvmtiIterationControl JNICALL
HeapObject(jlong class_tag,jlong size,jlong * tag_ptr,void * user_data)310 HeapObject(jlong class_tag, jlong size, jlong *tag_ptr, void *user_data) {
311     return JVMTI_ITERATION_ABORT;
312 }
313 
314 static jvmtiIterationControl JNICALL
HeapRoot(jvmtiHeapRootKind root_kind,jlong class_tag,jlong size,jlong * tag_ptr,void * user_data)315 HeapRoot(jvmtiHeapRootKind root_kind, jlong class_tag, jlong size,
316         jlong *tag_ptr, void *user_data) {
317     return JVMTI_ITERATION_ABORT;
318 }
319 
320 static jvmtiIterationControl JNICALL
StackReference(jvmtiHeapRootKind root_kind,jlong class_tag,jlong size,jlong * tag_ptr,jlong thread_tag,jint depth,jmethodID method,jint slot,void * user_data)321 StackReference(jvmtiHeapRootKind root_kind, jlong class_tag, jlong size,
322         jlong *tag_ptr, jlong thread_tag, jint depth, jmethodID method,
323         jint slot, void *user_data) {
324     return JVMTI_ITERATION_ABORT;
325 }
326 
327 static jvmtiIterationControl JNICALL
ObjectReference(jvmtiObjectReferenceKind reference_kind,jlong class_tag,jlong size,jlong * tag_ptr,jlong referrer_tag,jint referrer_index,void * user_data)328 ObjectReference(jvmtiObjectReferenceKind reference_kind, jlong class_tag,
329         jlong size, jlong *tag_ptr, jlong referrer_tag,
330         jint referrer_index, void *user_data) {
331     return JVMTI_ITERATION_ABORT;
332 }
333 
checkHeapFunctions()334 static int checkHeapFunctions() {
335     const jlong TAG_VALUE = (123456789L);
336     jlong tag;
337     jint count;
338     jobject *res_objects = NULL;
339     jlong *res_tags = NULL;
340     jint dummy_user_data = 0;
341 
342     NSK_DISPLAY0("Checking negative: SetTag\n");
343     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
344             jvmti->SetTag(thread, TAG_VALUE)))
345         return NSK_FALSE;
346 
347     NSK_DISPLAY0("Checking negative: GetTag\n");
348     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY, jvmti->GetTag(thread, &tag)))
349         return NSK_FALSE;
350 
351     NSK_DISPLAY0("Checking negative: GetObjectsWithTags\n");
352     tag = TAG_VALUE;
353     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
354             jvmti->GetObjectsWithTags(1, &tag, &count, &res_objects, &res_tags)))
355         return NSK_FALSE;
356 
357     NSK_DISPLAY0("Checking negative: IterateOverHeap\n");
358     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
359             jvmti->IterateOverHeap(JVMTI_HEAP_OBJECT_TAGGED, HeapObject, &dummy_user_data)))
360         return NSK_FALSE;
361 
362     NSK_DISPLAY0("Checking negative: IterateOverInstancesOfClass\n");
363     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
364             jvmti->IterateOverInstancesOfClass(klass,
365                                                JVMTI_HEAP_OBJECT_UNTAGGED,
366                                                HeapObject,
367                                                &dummy_user_data)))
368         return NSK_FALSE;
369 
370     NSK_DISPLAY0("Checking negative: IterateOverObjectsReachableFromObject\n");
371     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
372             jvmti->IterateOverObjectsReachableFromObject(thread,
373                                                          ObjectReference,
374                                                          &dummy_user_data)))
375         return NSK_FALSE;
376 
377     NSK_DISPLAY0("Checking negative: IterateOverReachableObjects\n");
378     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
379             jvmti->IterateOverReachableObjects(HeapRoot,
380                                                StackReference,
381                                                ObjectReference,
382                                                &dummy_user_data)))
383         return NSK_FALSE;
384 
385     return NSK_TRUE;
386 }
387 
388 /* Check "can_access_local_variables" functions
389  */
checkLocalVariableFunctions()390 static int checkLocalVariableFunctions() {
391     jint count;
392     jvmtiLocalVariableEntry *local_variable_table = NULL;
393     jobject object_value;
394     jint int_value;
395     jlong long_value;
396     jfloat float_value;
397     jdouble double_value;
398 
399     NSK_DISPLAY0("Checking negative: GetLocalVariableTable\n");
400     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
401             jvmti->GetLocalVariableTable(method, &count, &local_variable_table)))
402         return NSK_FALSE;
403 
404     NSK_DISPLAY0("Checking negative: GetLocalObject\n");
405     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
406             jvmti->GetLocalObject(thread, 0, 0, &object_value)))
407         return NSK_FALSE;
408 
409     NSK_DISPLAY0("Checking negative: GetLocalInt\n");
410     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
411             jvmti->GetLocalInt(thread, 0, 0, &int_value)))
412         return NSK_FALSE;
413 
414     NSK_DISPLAY0("Checking negative: GetLocalLong\n");
415     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
416             jvmti->GetLocalLong(thread, 0, 0, &long_value)))
417         return NSK_FALSE;
418 
419     NSK_DISPLAY0("Checking negative: GetLocalFloat\n");
420     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
421             jvmti->GetLocalFloat(thread, 0, 0, &float_value)))
422         return NSK_FALSE;
423 
424     NSK_DISPLAY0("Checking negative: GetLocalDouble\n");
425     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
426             jvmti->GetLocalDouble(thread, 0, 0, &double_value)))
427         return NSK_FALSE;
428 
429     NSK_DISPLAY0("Checking negative: SetLocalObject\n");
430     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
431             jvmti->SetLocalObject(thread, 0, 0, thread)))
432         return NSK_FALSE;
433 
434     NSK_DISPLAY0("Checking negative: SetLocalInt\n");
435     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
436             jvmti->SetLocalInt(thread, 0, 0, (jint)0)))
437         return NSK_FALSE;
438 
439     NSK_DISPLAY0("Checking negative: SetLocalLong\n");
440     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
441             jvmti->SetLocalLong(thread, 0, 0, (jlong)0)))
442         return NSK_FALSE;
443 
444     NSK_DISPLAY0("Checking negative: SetLocalFloat\n");
445     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
446             jvmti->SetLocalFloat(thread, 0, 0, (jfloat)0.0)))
447         return NSK_FALSE;
448 
449     NSK_DISPLAY0("Checking negative: SetLocalDouble\n");
450     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
451             jvmti->SetLocalDouble(thread, 0, 0, (jdouble)0.0)))
452         return NSK_FALSE;
453 
454     return NSK_TRUE;
455 }
456 
457 /* Check "can_get_source_info" functions
458  */
checkSourceInfoFunctions()459 static int checkSourceInfoFunctions() {
460     char *name;
461     jint count;
462     jvmtiLineNumberEntry *line_number_table = NULL;
463 
464     NSK_DISPLAY0("Checking negative: GetSourceFileName\n");
465     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
466             jvmti->GetSourceFileName(klass, &name)))
467         return NSK_FALSE;
468 
469     NSK_DISPLAY0("Checking negative: GetSourceDebugExtension\n");
470     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
471             jvmti->GetSourceDebugExtension(klass, &name)))
472         return NSK_FALSE;
473 
474     NSK_DISPLAY0("Checking negative: GetLineNumberTable\n");
475     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
476             jvmti->GetLineNumberTable(method, &count, &line_number_table)))
477         return NSK_FALSE;
478 
479     return NSK_TRUE;
480 }
481 
482 /* Check "can_redefine_classes" functions
483  */
checkRedefineClasses()484 static int checkRedefineClasses() {
485     jvmtiClassDefinition class_def;
486 
487     NSK_DISPLAY0("Checking negative: RedefineClasses\n");
488     class_def.klass = klass;
489     class_def.class_byte_count = 0;
490     class_def.class_bytes = NULL;
491     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
492             jvmti->RedefineClasses(1, &class_def)))
493         return NSK_FALSE;
494 
495     return NSK_TRUE;
496 }
497 
498 /* Check "can_get_monitor_info" function
499  */
checkGetObjectMonitorUsage()500 static int checkGetObjectMonitorUsage() {
501     jvmtiMonitorUsage monitor_info;
502 
503     NSK_DISPLAY0("Checking negative: GetObjectMonitorUsage\n");
504     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
505             jvmti->GetObjectMonitorUsage(thread, &monitor_info)))
506         return NSK_FALSE;
507 
508     return NSK_TRUE;
509 }
510 
511 /* Check "can_get_synthetic_attribute" functions
512  */
checkIsSyntheticFunctions()513 static int checkIsSyntheticFunctions() {
514     jboolean is_synthetic;
515 
516     NSK_DISPLAY0("Checking negative: IsFieldSynthetic\n");
517     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
518             jvmti->IsFieldSynthetic(klass, field, &is_synthetic)))
519         return NSK_FALSE;
520 
521     NSK_DISPLAY0("Checking negative: IsMethodSynthetic\n");
522     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
523             jvmti->IsMethodSynthetic(method, &is_synthetic)))
524         return NSK_FALSE;
525 
526     return NSK_TRUE;
527 }
528 
529 /* Check "can_get_bytecodes" function
530  */
checkGetBytecodes()531 static int checkGetBytecodes() {
532     jint count;
533     unsigned char *bytecodes;
534 
535     NSK_DISPLAY0("Checking negative: GetBytecodes\n");
536     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
537             jvmti->GetBytecodes(method, &count, &bytecodes)))
538         return NSK_FALSE;
539 
540     return NSK_TRUE;
541 }
542 
543 /* Check "can_get_current_thread_cpu_time" function
544  */
checkGetCurrentThreadCpuTime()545 static int checkGetCurrentThreadCpuTime() {
546     jvmtiTimerInfo info;
547     jlong nanos;
548 
549     NSK_DISPLAY0("Checking negative: GetCurrentThreadCpuTimerInfo\n");
550     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
551             jvmti->GetCurrentThreadCpuTimerInfo(&info)))
552         return NSK_FALSE;
553 
554     NSK_DISPLAY0("Checking negative: GetCurrentThreadCpuTime\n");
555     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
556             jvmti->GetCurrentThreadCpuTime(&nanos)))
557         return NSK_FALSE;
558 
559     return NSK_TRUE;
560 }
561 
562 /* Check "can_get_thread_cpu_time" function
563  */
checkGetThreadCpuTime()564 static int checkGetThreadCpuTime() {
565     jvmtiTimerInfo info;
566     jlong nanos;
567 
568     NSK_DISPLAY0("Checking negative: GetThreadCpuTimerInfo\n");
569     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
570             jvmti->GetThreadCpuTimerInfo(&info)))
571         return NSK_FALSE;
572 
573     NSK_DISPLAY0("Checking negative: GetThreadCpuTime\n");
574     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
575             jvmti->GetThreadCpuTime(thread, &nanos)))
576         return NSK_FALSE;
577 
578     return NSK_TRUE;
579 }
580 
581 /* ========================================================================== */
582 
583 /* agent algorithm
584  */
585 static void JNICALL
agentProc(jvmtiEnv * jvmti,JNIEnv * agentJNI,void * arg)586 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
587     jni = agentJNI;
588 
589     /* wait for initial sync */
590     if (!nsk_jvmti_waitForSync(timeout))
591         return;
592 
593     if (!prepare()) {
594         nsk_jvmti_setFailStatus();
595         return;
596     }
597 
598     /* testcase #1: check GetPotentialCapabilities */
599     NSK_DISPLAY0("Testcase #1: check if GetPotentialCapabilities returns the capability\n");
600     if (!checkGetPotentialCapabilities()) {
601         nsk_jvmti_setFailStatus();
602         return;
603     }
604 
605     /* testcase #2: add the capability during Live phase */
606     NSK_DISPLAY0("Testcase #2: add the capability during Live phase\n");
607     if (!checkAddCapabilities()) {
608         nsk_jvmti_setFailStatus();
609         return;
610     }
611 
612     /* testcase #3: check if GetCapabilities returns the capability */
613     NSK_DISPLAY0("Testcase #3: check if GetCapabilities returns the capability\n");
614     if (!checkGetCapabilities(NSK_TRUE)) {
615         nsk_jvmti_setFailStatus();
616         return;
617     }
618 
619     /* testcase #4: check that only correspondent function work */
620     NSK_DISPLAY0("Testcase #4: check that only correspondent function work but not others\n");
621     if (!checkSuspend(NSK_TRUE))
622         nsk_jvmti_setFailStatus();
623     if (!checkSignalThread())
624         nsk_jvmti_setFailStatus();
625     if (!checkGetOwnedMonitorInfo())
626         nsk_jvmti_setFailStatus();
627     if (!checkGetCurrentContendedMonitor())
628         nsk_jvmti_setFailStatus();
629     if (!checkPopFrame())
630         nsk_jvmti_setFailStatus();
631     if (!checkHeapFunctions())
632         nsk_jvmti_setFailStatus();
633     if (!checkLocalVariableFunctions())
634         nsk_jvmti_setFailStatus();
635     if (!checkSourceInfoFunctions())
636         nsk_jvmti_setFailStatus();
637     if (!checkRedefineClasses())
638         nsk_jvmti_setFailStatus();
639     if (!checkGetObjectMonitorUsage())
640         nsk_jvmti_setFailStatus();
641     if (!checkIsSyntheticFunctions())
642         nsk_jvmti_setFailStatus();
643     if (!checkGetBytecodes())
644         nsk_jvmti_setFailStatus();
645     if (!checkGetCurrentThreadCpuTime())
646         nsk_jvmti_setFailStatus();
647     if (!checkGetThreadCpuTime())
648         nsk_jvmti_setFailStatus();
649 
650     /* testcase #5: relinquish the capability during Live phase */
651     NSK_DISPLAY0("Testcase #5: relinquish the capability during Live phase\n");
652     if (!checkRelinquishCapabilities()) {
653         nsk_jvmti_setFailStatus();
654         return;
655     }
656 
657     /* testcase #6: check if GetCapabilities does not return the capability */
658     NSK_DISPLAY0("Testcase #6: check if GetCapabilities does not return the capability\n");
659     if (!checkGetCapabilities(NSK_FALSE)) {
660         nsk_jvmti_setFailStatus();
661         return;
662     }
663 
664     /* testcase #7: check that the capability function does not work */
665     if (!checkSuspend(NSK_FALSE))
666         nsk_jvmti_setFailStatus();
667 
668     /* testcase #8: add back the capability and check with GetCapabilities */
669     NSK_DISPLAY0("Testcase #8: add back the capability and check with GetCapabilities\n");
670     if (!checkAddCapabilities()) {
671         nsk_jvmti_setFailStatus();
672         return;
673     }
674     if (!checkGetCapabilities(NSK_TRUE)) {
675         nsk_jvmti_setFailStatus();
676         return;
677     }
678 
679     /* testcase #9: exits with the capability has not been relinquished */
680     NSK_DISPLAY0("Testcase #9: check if VM exits well with the capability has not been relinquished\n");
681 
682     /* resume debugee after last sync */
683     if (!nsk_jvmti_resumeSync())
684         return;
685 }
686 
687 /* ========================================================================== */
688 
689 /* agent library initialization
690  */
691 #ifdef STATIC_BUILD
Agent_OnLoad_cm01t018(JavaVM * jvm,char * options,void * reserved)692 JNIEXPORT jint JNICALL Agent_OnLoad_cm01t018(JavaVM *jvm, char *options, void *reserved) {
693     return Agent_Initialize(jvm, options, reserved);
694 }
Agent_OnAttach_cm01t018(JavaVM * jvm,char * options,void * reserved)695 JNIEXPORT jint JNICALL Agent_OnAttach_cm01t018(JavaVM *jvm, char *options, void *reserved) {
696     return Agent_Initialize(jvm, options, reserved);
697 }
JNI_OnLoad_cm01t018(JavaVM * jvm,char * options,void * reserved)698 JNIEXPORT jint JNI_OnLoad_cm01t018(JavaVM *jvm, char *options, void *reserved) {
699     return JNI_VERSION_1_8;
700 }
701 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)702 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
703 
704     /* init framework and parse options */
705     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
706         return JNI_ERR;
707 
708     timeout = nsk_jvmti_getWaitTime() * 60000;
709     NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout);
710 
711     /* create JVMTI environment */
712     if (!NSK_VERIFY((jvmti =
713             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
714         return JNI_ERR;
715 
716     /* register agent proc and arg */
717     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
718         return JNI_ERR;
719 
720     return JNI_OK;
721 }
722 
723 /* ========================================================================== */
724 
725 }
726