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