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