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