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_bytecodes
35 #define CAPABILITY_STR "can_get_bytecodes"
36 
37 /* The test checks capability can_get_bytecodes
38  * and correspondent function GetBytecodes.
39  *
40  * Testcases:
41  *   1. Check if GetPotentialCapabilities returns the capability
42  *   2. Add the capability during Onload phase
43  *   3. Check if GetCapabilities returns the capability
44  *   4. Relinquish the capability during Onload phase
45  *   5. Check if GetCapabilities does not return the capability
46  *   6. Add back the capability and check with GetCapabilities
47  *   7. Check that only correspondent function work and functions of
48  *      other capabilities return JVMTI_ERROR_MUST_POSSESS_CAPABILITY
49  *   8. Check if VM exits well with the capability has not been relinquished
50  */
51 
52 /* ========================================================================== */
53 
54 /* scaffold objects */
55 static JNIEnv* jni = NULL;
56 static jvmtiEnv *jvmti = NULL;
57 static jlong timeout = 0;
58 
59 /* test objects */
60 static jthread thread = NULL;
61 static jclass klass = NULL;
62 static jmethodID method = NULL;
63 static jfieldID field = NULL;
64 
65 /* ========================================================================== */
66 
prepare()67 static int prepare() {
68     const char* THREAD_NAME = "Debuggee Thread";
69     jvmtiThreadInfo info;
70     jthread *threads = NULL;
71     jint threads_count = 0;
72     int i;
73 
74     NSK_DISPLAY0("Prepare: find tested thread\n");
75 
76     /* get all live threads */
77     if (!NSK_JVMTI_VERIFY(
78            NSK_CPP_STUB3(GetAllThreads, jvmti, &threads_count, &threads)))
79         return NSK_FALSE;
80 
81     if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
82         return NSK_FALSE;
83 
84     /* find tested thread */
85     for (i = 0; i < threads_count; i++) {
86         if (!NSK_VERIFY(threads[i] != NULL))
87             return NSK_FALSE;
88 
89         /* get thread information */
90         if (!NSK_JVMTI_VERIFY(
91                 NSK_CPP_STUB3(GetThreadInfo, jvmti, threads[i], &info)))
92             return NSK_FALSE;
93 
94         NSK_DISPLAY3("    thread #%d (%s): %p\n", i, info.name, threads[i]);
95 
96         /* find by name */
97         if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) {
98             thread = threads[i];
99         }
100     }
101 
102     /* deallocate threads list */
103     if (!NSK_JVMTI_VERIFY(
104             NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threads)))
105         return NSK_FALSE;
106 
107     /* get tested thread class */
108     if (!NSK_JNI_VERIFY(jni, (klass =
109             NSK_CPP_STUB2(GetObjectClass, jni, thread)) != NULL))
110         return NSK_FALSE;
111 
112     /* get tested thread method 'run' */
113     if (!NSK_JNI_VERIFY(jni, (method =
114             NSK_CPP_STUB4(GetMethodID, jni, klass, "run", "()V")) != NULL))
115         return NSK_FALSE;
116 
117     /* get tested thread field 'waitingMonitor' */
118     if (!NSK_JNI_VERIFY(jni, (field =
119             NSK_CPP_STUB4(GetFieldID, jni, klass,
120                 "waitingMonitor", "Ljava/lang/Object;")) != NULL))
121         return NSK_FALSE;
122 
123     return NSK_TRUE;
124 }
125 
126 /* ========================================================================== */
127 
128 /* Check "can_suspend" functions
129  */
checkSuspend()130 static int checkSuspend() {
131     jvmtiError err;
132 
133     NSK_DISPLAY0("Checking negative: SuspendThread\n");
134     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
135             NSK_CPP_STUB2(SuspendThread, jvmti, thread)))
136         return NSK_FALSE;
137 
138     NSK_DISPLAY0("Checking negative: ResumeThread\n");
139     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
140             NSK_CPP_STUB2(ResumeThread, jvmti, thread)))
141         return NSK_FALSE;
142 
143     NSK_DISPLAY0("Checking negative: SuspendThreadList\n");
144     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
145             NSK_CPP_STUB4(SuspendThreadList, jvmti, 1, &thread, &err)))
146         return NSK_FALSE;
147 
148     NSK_DISPLAY0("Checking negative: ResumeThreadList\n");
149     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
150             NSK_CPP_STUB4(ResumeThreadList, jvmti, 1, &thread, &err)))
151         return NSK_FALSE;
152 
153     return NSK_TRUE;
154 }
155 
156 /* Check "can_signal_thread" functions
157  */
checkSignalThread()158 static int checkSignalThread() {
159     const char* THREAD_DEATH_CLASS_NAME = "java/lang/ThreadDeath";
160     const char* THREAD_DEATH_CTOR_NAME = "<init>";
161     const char* THREAD_DEATH_CTOR_SIGNATURE = "()V";
162     jclass cls = NULL;
163     jmethodID ctor = NULL;
164     jobject exception = NULL;
165 
166     if (!NSK_JNI_VERIFY(jni, (cls =
167             NSK_CPP_STUB2(FindClass, jni, THREAD_DEATH_CLASS_NAME)) != NULL))
168         return NSK_FALSE;
169 
170     if (!NSK_JNI_VERIFY(jni, (ctor =
171             NSK_CPP_STUB4(GetMethodID, jni, cls,
172                 THREAD_DEATH_CTOR_NAME, THREAD_DEATH_CTOR_SIGNATURE)) != NULL))
173         return NSK_FALSE;
174 
175     if (!NSK_JNI_VERIFY(jni, (exception =
176             NSK_CPP_STUB3(NewObject, jni, cls, ctor)) != NULL))
177         return NSK_FALSE;
178 
179     NSK_DISPLAY0("Checking negative: StopThread\n");
180     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
181             NSK_CPP_STUB3(StopThread, jvmti, thread, exception)))
182         return NSK_FALSE;
183 
184     NSK_DISPLAY0("Checking negative: InterruptThread\n");
185     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
186             NSK_CPP_STUB2(InterruptThread, jvmti, thread)))
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 positive: GetBytecodes\n");
460     if (!NSK_JVMTI_VERIFY(
461             NSK_CPP_STUB4(GetBytecodes, jvmti, method, &count, &bytecodes)))
462         return NSK_FALSE;
463     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, jvmti, 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_cm01t002(JavaVM * jvm,char * options,void * reserved)566 JNIEXPORT jint JNICALL Agent_OnLoad_cm01t002(JavaVM *jvm, char *options, void *reserved) {
567     return Agent_Initialize(jvm, options, reserved);
568 }
Agent_OnAttach_cm01t002(JavaVM * jvm,char * options,void * reserved)569 JNIEXPORT jint JNICALL Agent_OnAttach_cm01t002(JavaVM *jvm, char *options, void *reserved) {
570     return Agent_Initialize(jvm, options, reserved);
571 }
JNI_OnLoad_cm01t002(JavaVM * jvm,char * options,void * reserved)572 JNIEXPORT jint JNI_OnLoad_cm01t002(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