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