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_bytecodes
33 #define CAPABILITY_STR "can_get_bytecodes"
34 
35 /* The test checks capability can_get_bytecodes
36  * and correspondent function GetBytecodes.
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 work 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()476 static int checkGetObjectMonitorUsage() {
477     jvmtiMonitorUsage monitor_info;
478 
479     NSK_DISPLAY0("Checking negative: GetObjectMonitorUsage\n");
480     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
481             jvmti->GetObjectMonitorUsage(thread, &monitor_info)))
482         return NSK_FALSE;
483 
484     return NSK_TRUE;
485 }
486 
487 /* Check "can_get_synthetic_attribute" functions
488  */
checkIsSyntheticFunctions()489 static int checkIsSyntheticFunctions() {
490     jboolean is_synthetic;
491 
492     NSK_DISPLAY0("Checking negative: IsFieldSynthetic\n");
493     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
494             jvmti->IsFieldSynthetic(klass, field, &is_synthetic)))
495         return NSK_FALSE;
496 
497     NSK_DISPLAY0("Checking negative: IsMethodSynthetic\n");
498     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
499             jvmti->IsMethodSynthetic(method, &is_synthetic)))
500         return NSK_FALSE;
501 
502     return NSK_TRUE;
503 }
504 
505 /* Check "can_get_bytecodes" function
506  */
checkGetBytecodes(int positive)507 static int checkGetBytecodes(int positive) {
508     jint count;
509     unsigned char *bytecodes;
510 
511     if (positive) {
512         NSK_DISPLAY0("Checking positive: GetBytecodes\n");
513         if (!NSK_JVMTI_VERIFY(jvmti->GetBytecodes(method, &count, &bytecodes)))
514             return NSK_FALSE;
515         if (!NSK_JVMTI_VERIFY(jvmti->Deallocate(bytecodes)))
516             return NSK_FALSE;
517     } else {
518         NSK_DISPLAY0("Checking negative: GetBytecodes\n");
519         if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
520                 jvmti->GetBytecodes(method, &count, &bytecodes)))
521             return NSK_FALSE;
522     }
523 
524     return NSK_TRUE;
525 }
526 
527 /* Check "can_get_current_thread_cpu_time" function
528  */
checkGetCurrentThreadCpuTime()529 static int checkGetCurrentThreadCpuTime() {
530     jvmtiTimerInfo info;
531     jlong nanos;
532 
533     NSK_DISPLAY0("Checking negative: GetCurrentThreadCpuTimerInfo\n");
534     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
535             jvmti->GetCurrentThreadCpuTimerInfo(&info)))
536         return NSK_FALSE;
537 
538     NSK_DISPLAY0("Checking negative: GetCurrentThreadCpuTime\n");
539     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
540             jvmti->GetCurrentThreadCpuTime(&nanos)))
541         return NSK_FALSE;
542 
543     return NSK_TRUE;
544 }
545 
546 /* Check "can_get_thread_cpu_time" function
547  */
checkGetThreadCpuTime()548 static int checkGetThreadCpuTime() {
549     jvmtiTimerInfo info;
550     jlong nanos;
551 
552     NSK_DISPLAY0("Checking negative: GetThreadCpuTimerInfo\n");
553     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
554             jvmti->GetThreadCpuTimerInfo(&info)))
555         return NSK_FALSE;
556 
557     NSK_DISPLAY0("Checking negative: GetThreadCpuTime\n");
558     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
559             jvmti->GetThreadCpuTime(thread, &nanos)))
560         return NSK_FALSE;
561 
562     return NSK_TRUE;
563 }
564 
565 /* ========================================================================== */
566 
567 /* agent algorithm
568  */
569 static void JNICALL
agentProc(jvmtiEnv * jvmti,JNIEnv * agentJNI,void * arg)570 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
571     jni = agentJNI;
572 
573     /* wait for initial sync */
574     if (!nsk_jvmti_waitForSync(timeout))
575         return;
576 
577     if (!prepare()) {
578         nsk_jvmti_setFailStatus();
579         return;
580     }
581 
582     /* testcase #1: check GetPotentialCapabilities */
583     NSK_DISPLAY0("Testcase #1: check if GetPotentialCapabilities returns the capability\n");
584     if (!checkGetPotentialCapabilities()) {
585         nsk_jvmti_setFailStatus();
586         return;
587     }
588 
589     /* testcase #2: add the capability during Live phase */
590     NSK_DISPLAY0("Testcase #2: add the capability during Live phase\n");
591     if (!checkAddCapabilities()) {
592         nsk_jvmti_setFailStatus();
593         return;
594     }
595 
596     /* testcase #3: check if GetCapabilities returns the capability */
597     NSK_DISPLAY0("Testcase #3: check if GetCapabilities returns the capability\n");
598     if (!checkGetCapabilities(NSK_TRUE)) {
599         nsk_jvmti_setFailStatus();
600         return;
601     }
602 
603     /* testcase #4: check that only correspondent function work */
604     NSK_DISPLAY0("Testcase #4: check that only correspondent function work but not others\n");
605     if (!checkSuspend())
606         nsk_jvmti_setFailStatus();
607     if (!checkSignalThread())
608         nsk_jvmti_setFailStatus();
609     if (!checkGetOwnedMonitorInfo())
610         nsk_jvmti_setFailStatus();
611     if (!checkGetCurrentContendedMonitor())
612         nsk_jvmti_setFailStatus();
613     if (!checkPopFrame())
614         nsk_jvmti_setFailStatus();
615     if (!checkHeapFunctions())
616         nsk_jvmti_setFailStatus();
617     if (!checkLocalVariableFunctions())
618         nsk_jvmti_setFailStatus();
619     if (!checkSourceInfoFunctions())
620         nsk_jvmti_setFailStatus();
621     if (!checkRedefineClasses())
622         nsk_jvmti_setFailStatus();
623     if (!checkGetObjectMonitorUsage())
624         nsk_jvmti_setFailStatus();
625     if (!checkIsSyntheticFunctions())
626         nsk_jvmti_setFailStatus();
627     if (!checkGetBytecodes(NSK_TRUE))
628         nsk_jvmti_setFailStatus();
629     if (!checkGetCurrentThreadCpuTime())
630         nsk_jvmti_setFailStatus();
631     if (!checkGetThreadCpuTime())
632         nsk_jvmti_setFailStatus();
633 
634     /* testcase #5: relinquish the capability during Live phase */
635     NSK_DISPLAY0("Testcase #5: relinquish the capability during Live phase\n");
636     if (!checkRelinquishCapabilities()) {
637         nsk_jvmti_setFailStatus();
638         return;
639     }
640 
641     /* testcase #6: check if GetCapabilities does not return the capability */
642     NSK_DISPLAY0("Testcase #6: check if GetCapabilities does not return the capability\n");
643     if (!checkGetCapabilities(NSK_FALSE)) {
644         nsk_jvmti_setFailStatus();
645         return;
646     }
647 
648     /* testcase #7: check that the capability function does not work */
649     if (!checkGetBytecodes(NSK_FALSE))
650         nsk_jvmti_setFailStatus();
651 
652     /* testcase #8: add back the capability and check with GetCapabilities */
653     NSK_DISPLAY0("Testcase #8: add back the capability and check with GetCapabilities\n");
654     if (!checkAddCapabilities()) {
655         nsk_jvmti_setFailStatus();
656         return;
657     }
658     if (!checkGetCapabilities(NSK_TRUE)) {
659         nsk_jvmti_setFailStatus();
660         return;
661     }
662 
663     /* testcase #9: exits with the capability has not been relinquished */
664     NSK_DISPLAY0("Testcase #9: check if VM exits well with the capability has not been relinquished\n");
665 
666     /* resume debugee after last sync */
667     if (!nsk_jvmti_resumeSync())
668         return;
669 }
670 
671 /* ========================================================================== */
672 
673 /* agent library initialization
674  */
675 #ifdef STATIC_BUILD
Agent_OnLoad_cm01t014(JavaVM * jvm,char * options,void * reserved)676 JNIEXPORT jint JNICALL Agent_OnLoad_cm01t014(JavaVM *jvm, char *options, void *reserved) {
677     return Agent_Initialize(jvm, options, reserved);
678 }
Agent_OnAttach_cm01t014(JavaVM * jvm,char * options,void * reserved)679 JNIEXPORT jint JNICALL Agent_OnAttach_cm01t014(JavaVM *jvm, char *options, void *reserved) {
680     return Agent_Initialize(jvm, options, reserved);
681 }
JNI_OnLoad_cm01t014(JavaVM * jvm,char * options,void * reserved)682 JNIEXPORT jint JNI_OnLoad_cm01t014(JavaVM *jvm, char *options, void *reserved) {
683     return JNI_VERSION_1_8;
684 }
685 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)686 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
687 
688     /* init framework and parse options */
689     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
690         return JNI_ERR;
691 
692     timeout = nsk_jvmti_getWaitTime() * 60000;
693     NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout);
694 
695     /* create JVMTI environment */
696     if (!NSK_VERIFY((jvmti =
697             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
698         return JNI_ERR;
699 
700     /* register agent proc and arg */
701     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
702         return JNI_ERR;
703 
704     return JNI_OK;
705 }
706 
707 /* ========================================================================== */
708 
709 }
710