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