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