1 /*
2  * Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 #include <stdio.h>
25 #include <string.h>
26 #include "jni_tools.h"
27 #include "agent_common.h"
28 #include "jvmti_tools.h"
29 
30 extern "C" {
31 
32 #define CAPABILITY can_get_thread_cpu_time
33 #define CAPABILITY_STR "can_get_thread_cpu_time"
34 
35 /* The test checks capability can_get_thread_cpu_time
36  * and correspondent functions:
37  *     GetThreadCpuTimerInfo
38  *     GetThreadCpuTime
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(jvmti->GetAllThreads(&threads_count, &threads)))
78         return NSK_FALSE;
79 
80     if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
81         return NSK_FALSE;
82 
83     /* find tested thread */
84     for (i = 0; i < threads_count; i++) {
85         if (!NSK_VERIFY(threads[i] != NULL))
86             return NSK_FALSE;
87 
88         /* get thread information */
89         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(threads[i], &info)))
90             return NSK_FALSE;
91 
92         NSK_DISPLAY3("    thread #%d (%s): %p\n", i, info.name, threads[i]);
93 
94         /* find by name */
95         if (info.name != NULL && (strcmp(info.name, THREAD_NAME) == 0)) {
96             thread = threads[i];
97         }
98     }
99 
100     /* deallocate threads list */
101     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads)))
102         return NSK_FALSE;
103 
104     /* get tested thread class */
105     if (!NSK_JNI_VERIFY(jni, (klass = jni->GetObjectClass(thread)) != NULL))
106         return NSK_FALSE;
107 
108     /* get tested thread method 'run' */
109     if (!NSK_JNI_VERIFY(jni, (method = jni->GetMethodID(klass, "run", "()V")) != NULL))
110         return NSK_FALSE;
111 
112     /* get tested thread field 'waitingMonitor' */
113     if (!NSK_JNI_VERIFY(jni, (field =
114             jni->GetFieldID(klass, "waitingMonitor", "Ljava/lang/Object;")) != NULL))
115         return NSK_FALSE;
116 
117     return NSK_TRUE;
118 }
119 
120 /* ========================================================================== */
121 
122 /* Check "can_suspend" functions
123  */
checkSuspend()124 static int checkSuspend() {
125     jvmtiError err;
126 
127     NSK_DISPLAY0("Checking negative: SuspendThread\n");
128     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY, jvmti->SuspendThread(thread)))
129         return NSK_FALSE;
130 
131     NSK_DISPLAY0("Checking negative: ResumeThread\n");
132     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY, jvmti->ResumeThread(thread)))
133         return NSK_FALSE;
134 
135     NSK_DISPLAY0("Checking negative: SuspendThreadList\n");
136     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
137             jvmti->SuspendThreadList(1, &thread, &err)))
138         return NSK_FALSE;
139 
140     NSK_DISPLAY0("Checking negative: ResumeThreadList\n");
141     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
142             jvmti->ResumeThreadList(1, &thread, &err)))
143         return NSK_FALSE;
144 
145     return NSK_TRUE;
146 }
147 
148 /* Check "can_signal_thread" functions
149  */
checkSignalThread()150 static int checkSignalThread() {
151     const char* THREAD_DEATH_CLASS_NAME = "java/lang/ThreadDeath";
152     const char* THREAD_DEATH_CTOR_NAME = "<init>";
153     const char* THREAD_DEATH_CTOR_SIGNATURE = "()V";
154     jclass cls = NULL;
155     jmethodID ctor = NULL;
156     jobject exception = NULL;
157 
158     if (!NSK_JNI_VERIFY(jni, (cls = jni->FindClass(THREAD_DEATH_CLASS_NAME)) != NULL))
159         return NSK_FALSE;
160 
161     if (!NSK_JNI_VERIFY(jni, (ctor =
162             jni->GetMethodID(cls, THREAD_DEATH_CTOR_NAME, THREAD_DEATH_CTOR_SIGNATURE)) != NULL))
163         return NSK_FALSE;
164 
165     if (!NSK_JNI_VERIFY(jni, (exception = jni->NewObject(cls, ctor)) != NULL))
166         return NSK_FALSE;
167 
168     NSK_DISPLAY0("Checking negative: StopThread\n");
169     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
170             jvmti->StopThread(thread, exception)))
171         return NSK_FALSE;
172 
173     NSK_DISPLAY0("Checking negative: InterruptThread\n");
174     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY, jvmti->InterruptThread(thread)))
175         return NSK_FALSE;
176 
177     return NSK_TRUE;
178 }
179 
180 /* Check "can_get_owned_monitor_info" function
181  */
checkGetOwnedMonitorInfo()182 static int checkGetOwnedMonitorInfo() {
183     jint count;
184     jobject *monitors = NULL;
185 
186     NSK_DISPLAY0("Checking negative: GetOwnedMonitorInfo\n");
187     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
188             jvmti->GetOwnedMonitorInfo(thread, &count, &monitors)))
189         return NSK_FALSE;
190 
191     return NSK_TRUE;
192 }
193 
194 /* Check "can_get_current_contended_monitor" function
195  */
checkGetCurrentContendedMonitor()196 static int checkGetCurrentContendedMonitor() {
197     jobject monitor = NULL;
198 
199     NSK_DISPLAY0("Checking negative: GetCurrentContendedMonitor\n");
200     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
201             jvmti->GetCurrentContendedMonitor(thread, &monitor)))
202         return NSK_FALSE;
203 
204     return NSK_TRUE;
205 }
206 
207 /* Check "can_pop_frame" function
208  */
checkPopFrame()209 static int checkPopFrame() {
210     NSK_DISPLAY0("Checking negative: PopFrame\n");
211     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY, jvmti->PopFrame(thread)))
212         return NSK_FALSE;
213 
214     return NSK_TRUE;
215 }
216 
217 /* Check "can_tag_objects" functions
218  */
219 
220 static jvmtiIterationControl JNICALL
HeapObject(jlong class_tag,jlong size,jlong * tag_ptr,void * user_data)221 HeapObject(jlong class_tag, jlong size, jlong *tag_ptr, void *user_data) {
222     return JVMTI_ITERATION_ABORT;
223 }
224 
225 static jvmtiIterationControl JNICALL
HeapRoot(jvmtiHeapRootKind root_kind,jlong class_tag,jlong size,jlong * tag_ptr,void * user_data)226 HeapRoot(jvmtiHeapRootKind root_kind, jlong class_tag, jlong size,
227         jlong *tag_ptr, void *user_data) {
228     return JVMTI_ITERATION_ABORT;
229 }
230 
231 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)232 StackReference(jvmtiHeapRootKind root_kind, jlong class_tag, jlong size,
233         jlong *tag_ptr, jlong thread_tag, jint depth, jmethodID method,
234         jint slot, void *user_data) {
235     return JVMTI_ITERATION_ABORT;
236 }
237 
238 static jvmtiIterationControl JNICALL
ObjectReference(jvmtiObjectReferenceKind reference_kind,jlong class_tag,jlong size,jlong * tag_ptr,jlong referrer_tag,jint referrer_index,void * user_data)239 ObjectReference(jvmtiObjectReferenceKind reference_kind, jlong class_tag,
240         jlong size, jlong *tag_ptr, jlong referrer_tag,
241         jint referrer_index, void *user_data) {
242     return JVMTI_ITERATION_ABORT;
243 }
244 
checkHeapFunctions()245 static int checkHeapFunctions() {
246     const jlong TAG_VALUE = (123456789L);
247     jlong tag;
248     jint count;
249     jobject *res_objects = NULL;
250     jlong *res_tags = NULL;
251     jint dummy_user_data = 0;
252 
253     NSK_DISPLAY0("Checking negative: SetTag\n");
254     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
255             jvmti->SetTag(thread, TAG_VALUE)))
256         return NSK_FALSE;
257 
258     NSK_DISPLAY0("Checking negative: GetTag\n");
259     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY, jvmti->GetTag(thread, &tag)))
260         return NSK_FALSE;
261 
262     NSK_DISPLAY0("Checking negative: GetObjectsWithTags\n");
263     tag = TAG_VALUE;
264     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
265             jvmti->GetObjectsWithTags(1, &tag, &count, &res_objects, &res_tags)))
266         return NSK_FALSE;
267 
268     NSK_DISPLAY0("Checking negative: IterateOverHeap\n");
269     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
270             jvmti->IterateOverHeap(JVMTI_HEAP_OBJECT_TAGGED, HeapObject, &dummy_user_data)))
271         return NSK_FALSE;
272 
273     NSK_DISPLAY0("Checking negative: IterateOverInstancesOfClass\n");
274     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
275             jvmti->IterateOverInstancesOfClass(klass,
276                                                JVMTI_HEAP_OBJECT_UNTAGGED,
277                                                HeapObject,
278                                                &dummy_user_data)))
279         return NSK_FALSE;
280 
281     NSK_DISPLAY0("Checking negative: IterateOverObjectsReachableFromObject\n");
282     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
283             jvmti->IterateOverObjectsReachableFromObject(thread,
284                                                          ObjectReference,
285                                                          &dummy_user_data)))
286         return NSK_FALSE;
287 
288     NSK_DISPLAY0("Checking negative: IterateOverReachableObjects\n");
289     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
290             jvmti->IterateOverReachableObjects(HeapRoot,
291                                                StackReference,
292                                                ObjectReference,
293                                                &dummy_user_data)))
294         return NSK_FALSE;
295 
296     return NSK_TRUE;
297 }
298 
299 /* Check "can_access_local_variables" functions
300  */
checkLocalVariableFunctions()301 static int checkLocalVariableFunctions() {
302     jint count;
303     jvmtiLocalVariableEntry *local_variable_table = NULL;
304     jobject object_value;
305     jint int_value;
306     jlong long_value;
307     jfloat float_value;
308     jdouble double_value;
309 
310     NSK_DISPLAY0("Checking negative: GetLocalVariableTable\n");
311     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
312             jvmti->GetLocalVariableTable(method, &count, &local_variable_table)))
313         return NSK_FALSE;
314 
315     NSK_DISPLAY0("Checking negative: GetLocalObject\n");
316     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
317             jvmti->GetLocalObject(thread, 0, 0, &object_value)))
318         return NSK_FALSE;
319 
320     NSK_DISPLAY0("Checking negative: GetLocalInt\n");
321     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
322             jvmti->GetLocalInt(thread, 0, 0, &int_value)))
323         return NSK_FALSE;
324 
325     NSK_DISPLAY0("Checking negative: GetLocalLong\n");
326     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
327             jvmti->GetLocalLong(thread, 0, 0, &long_value)))
328         return NSK_FALSE;
329 
330     NSK_DISPLAY0("Checking negative: GetLocalFloat\n");
331     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
332             jvmti->GetLocalFloat(thread, 0, 0, &float_value)))
333         return NSK_FALSE;
334 
335     NSK_DISPLAY0("Checking negative: GetLocalDouble\n");
336     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
337             jvmti->GetLocalDouble(thread, 0, 0, &double_value)))
338         return NSK_FALSE;
339 
340     NSK_DISPLAY0("Checking negative: SetLocalObject\n");
341     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
342             jvmti->SetLocalObject(thread, 0, 0, thread)))
343         return NSK_FALSE;
344 
345     NSK_DISPLAY0("Checking negative: SetLocalInt\n");
346     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
347             jvmti->SetLocalInt(thread, 0, 0, (jint)0)))
348         return NSK_FALSE;
349 
350     NSK_DISPLAY0("Checking negative: SetLocalLong\n");
351     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
352             jvmti->SetLocalLong(thread, 0, 0, (jlong)0)))
353         return NSK_FALSE;
354 
355     NSK_DISPLAY0("Checking negative: SetLocalFloat\n");
356     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
357             jvmti->SetLocalFloat(thread, 0, 0, (jfloat)0.0)))
358         return NSK_FALSE;
359 
360     NSK_DISPLAY0("Checking negative: SetLocalDouble\n");
361     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
362             jvmti->SetLocalDouble(thread, 0, 0, (jdouble)0.0)))
363         return NSK_FALSE;
364 
365     return NSK_TRUE;
366 }
367 
368 /* Check "can_get_source_info" functions
369  */
checkSourceInfoFunctions()370 static int checkSourceInfoFunctions() {
371     char *name;
372     jint count;
373     jvmtiLineNumberEntry *line_number_table = NULL;
374 
375     NSK_DISPLAY0("Checking negative: GetSourceFileName\n");
376     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
377             jvmti->GetSourceFileName(klass, &name)))
378         return NSK_FALSE;
379 
380     NSK_DISPLAY0("Checking negative: GetSourceDebugExtension\n");
381     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
382             jvmti->GetSourceDebugExtension(klass, &name)))
383         return NSK_FALSE;
384 
385     NSK_DISPLAY0("Checking negative: GetLineNumberTable\n");
386     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
387             jvmti->GetLineNumberTable(method, &count, &line_number_table)))
388         return NSK_FALSE;
389 
390     return NSK_TRUE;
391 }
392 
393 /* Check "can_redefine_classes" functions
394  */
checkRedefineClasses()395 static int checkRedefineClasses() {
396     jvmtiClassDefinition class_def;
397 
398     NSK_DISPLAY0("Checking negative: RedefineClasses\n");
399     class_def.klass = klass;
400     class_def.class_byte_count = 0;
401     class_def.class_bytes = NULL;
402     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
403             jvmti->RedefineClasses(1, &class_def)))
404         return NSK_FALSE;
405 
406     return NSK_TRUE;
407 }
408 
409 /* Check "can_get_monitor_info" function
410  */
checkGetObjectMonitorUsage()411 static int checkGetObjectMonitorUsage() {
412     jvmtiMonitorUsage monitor_info;
413 
414     NSK_DISPLAY0("Checking negative: GetObjectMonitorUsage\n");
415     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
416             jvmti->GetObjectMonitorUsage(thread, &monitor_info)))
417         return NSK_FALSE;
418 
419     return NSK_TRUE;
420 }
421 
422 /* Check "can_get_synthetic_attribute" functions
423  */
checkIsSyntheticFunctions()424 static int checkIsSyntheticFunctions() {
425     jboolean is_synthetic;
426 
427     NSK_DISPLAY0("Checking negative: IsFieldSynthetic\n");
428     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
429             jvmti->IsFieldSynthetic(klass, field, &is_synthetic)))
430         return NSK_FALSE;
431 
432     NSK_DISPLAY0("Checking negative: IsMethodSynthetic\n");
433     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
434             jvmti->IsMethodSynthetic(method, &is_synthetic)))
435         return NSK_FALSE;
436 
437     return NSK_TRUE;
438 }
439 
440 /* Check "can_get_bytecodes" function
441  */
checkGetBytecodes()442 static int checkGetBytecodes() {
443     jint count;
444     unsigned char *bytecodes;
445 
446     NSK_DISPLAY0("Checking negative: GetBytecodes\n");
447     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
448             jvmti->GetBytecodes(method, &count, &bytecodes)))
449         return NSK_FALSE;
450 
451     return NSK_TRUE;
452 }
453 
454 /* Check "can_get_current_thread_cpu_time" function
455  */
checkGetCurrentThreadCpuTime()456 static int checkGetCurrentThreadCpuTime() {
457     jvmtiTimerInfo info;
458     jlong nanos;
459 
460     NSK_DISPLAY0("Checking negative: GetCurrentThreadCpuTimerInfo\n");
461     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
462             jvmti->GetCurrentThreadCpuTimerInfo(&info)))
463         return NSK_FALSE;
464 
465     NSK_DISPLAY0("Checking negative: GetCurrentThreadCpuTime\n");
466     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
467             jvmti->GetCurrentThreadCpuTime(&nanos)))
468         return NSK_FALSE;
469 
470     return NSK_TRUE;
471 }
472 
473 /* Check "can_get_thread_cpu_time" function
474  */
checkGetThreadCpuTime()475 static int checkGetThreadCpuTime() {
476     jvmtiTimerInfo info;
477     jlong nanos;
478 
479     NSK_DISPLAY0("Checking positive: GetThreadCpuTimerInfo\n");
480     if (!NSK_JVMTI_VERIFY(jvmti->GetThreadCpuTimerInfo(&info)))
481         return NSK_FALSE;
482 
483     NSK_DISPLAY0("Checking positive: checkGetThreadCpuTime\n");
484     if (!NSK_JVMTI_VERIFY(jvmti->GetThreadCpuTime(thread, &nanos)))
485         return NSK_FALSE;
486 
487     return NSK_TRUE;
488 }
489 
490 /* ========================================================================== */
491 
492 /* agent algorithm */
493 static void JNICALL
agentProc(jvmtiEnv * jvmti,JNIEnv * agentJNI,void * arg)494 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
495     jni = agentJNI;
496 
497     /* wait for initial sync */
498     if (!nsk_jvmti_waitForSync(timeout))
499         return;
500 
501     if (!prepare()) {
502         nsk_jvmti_setFailStatus();
503         return;
504     }
505 
506     /* testcase #7: check that only correspondent function work */
507     NSK_DISPLAY0("Testcase #7: check that only correspondent function work but not others\n");
508     if (!checkSuspend())
509         nsk_jvmti_setFailStatus();
510     if (!checkSignalThread())
511         nsk_jvmti_setFailStatus();
512     if (!checkGetOwnedMonitorInfo())
513         nsk_jvmti_setFailStatus();
514     if (!checkGetCurrentContendedMonitor())
515         nsk_jvmti_setFailStatus();
516     if (!checkPopFrame())
517         nsk_jvmti_setFailStatus();
518     if (!checkHeapFunctions())
519         nsk_jvmti_setFailStatus();
520     if (!checkLocalVariableFunctions())
521         nsk_jvmti_setFailStatus();
522     if (!checkSourceInfoFunctions())
523         nsk_jvmti_setFailStatus();
524     if (!checkRedefineClasses())
525         nsk_jvmti_setFailStatus();
526     if (!checkGetObjectMonitorUsage())
527         nsk_jvmti_setFailStatus();
528     if (!checkIsSyntheticFunctions())
529         nsk_jvmti_setFailStatus();
530     if (!checkGetBytecodes())
531         nsk_jvmti_setFailStatus();
532     if (!checkGetCurrentThreadCpuTime())
533         nsk_jvmti_setFailStatus();
534     if (!checkGetThreadCpuTime())
535         nsk_jvmti_setFailStatus();
536 
537     /* testcase #8: exits with the capability has not been relinquished */
538     NSK_DISPLAY0("Testcase #8: check if VM exits well with the capability has not been relinquished\n");
539 
540     /* resume debugee after last sync */
541     if (!nsk_jvmti_resumeSync())
542         return;
543 }
544 
545 /* ========================================================================== */
546 
547 /* agent library initialization */
548 #ifdef STATIC_BUILD
Agent_OnLoad_cm01t021(JavaVM * jvm,char * options,void * reserved)549 JNIEXPORT jint JNICALL Agent_OnLoad_cm01t021(JavaVM *jvm, char *options, void *reserved) {
550     return Agent_Initialize(jvm, options, reserved);
551 }
Agent_OnAttach_cm01t021(JavaVM * jvm,char * options,void * reserved)552 JNIEXPORT jint JNICALL Agent_OnAttach_cm01t021(JavaVM *jvm, char *options, void *reserved) {
553     return Agent_Initialize(jvm, options, reserved);
554 }
JNI_OnLoad_cm01t021(JavaVM * jvm,char * options,void * reserved)555 JNIEXPORT jint JNI_OnLoad_cm01t021(JavaVM *jvm, char *options, void *reserved) {
556     return JNI_VERSION_1_8;
557 }
558 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)559 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
560     jvmtiCapabilities caps;
561 
562     /* init framework and parse options */
563     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
564         return JNI_ERR;
565 
566     timeout = nsk_jvmti_getWaitTime() * 60000;
567     NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout);
568 
569     /* create JVMTI environment */
570     if (!NSK_VERIFY((jvmti =
571             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
572         return JNI_ERR;
573 
574     /* register agent proc and arg */
575     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
576         return JNI_ERR;
577 
578     /* testcase #1: check GetPotentialCapabilities */
579     NSK_DISPLAY0("Testcase #1: check if GetPotentialCapabilities returns the capability\n");
580     if (!NSK_JVMTI_VERIFY(jvmti->GetPotentialCapabilities(&caps)))
581         return JNI_ERR;
582     if (!caps.CAPABILITY) {
583         NSK_COMPLAIN1("GetPotentialCapabilities does not return \"%s\" capability\n",
584             CAPABILITY_STR);
585         return JNI_ERR;
586     }
587 
588     /* testcase #2: add the capability during Onload phase */
589     NSK_DISPLAY0("Testcase #2: add the capability during Onload phase\n");
590     memset(&caps, 0, sizeof(caps));
591     caps.CAPABILITY = 1;
592     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
593         return JNI_ERR;
594 
595     /* testcase #3: check if GetCapabilities returns the capability */
596     NSK_DISPLAY0("Testcase #3: check if GetCapabilities returns the capability\n");
597     memset(&caps, 0, sizeof(caps));
598     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
599         return JNI_ERR;
600     if (!caps.CAPABILITY) {
601         NSK_COMPLAIN1("GetCapabilities does not return \"%s\" capability\n",
602             CAPABILITY_STR);
603         return JNI_ERR;
604     }
605 
606     /* testcase #4: relinquish the capability during Onload phase */
607     NSK_DISPLAY0("Testcase #4: relinquish the capability during Onload phase\n");
608     memset(&caps, 0, sizeof(caps));
609     caps.CAPABILITY = 1;
610     if (!NSK_JVMTI_VERIFY(jvmti->RelinquishCapabilities(&caps)))
611         return JNI_ERR;
612 
613     /* testcase #5: check if GetCapabilities does not return the capability */
614     NSK_DISPLAY0("Testcase #5: check if GetCapabilities does not return the capability\n");
615     memset(&caps, 0, sizeof(caps));
616     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
617         return JNI_ERR;
618     if (caps.CAPABILITY) {
619         NSK_COMPLAIN1("GetCapabilities returns relinquished \"%s\" capability\n",
620             CAPABILITY_STR);
621         return JNI_ERR;
622     }
623 
624     /* testcase #6: add back the capability and check with GetCapabilities */
625     NSK_DISPLAY0("Testcase #6: add back the capability and check with GetCapabilities\n");
626     memset(&caps, 0, sizeof(caps));
627     caps.CAPABILITY = 1;
628     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
629         return JNI_ERR;
630     memset(&caps, 0, sizeof(caps));
631     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
632         return JNI_ERR;
633     if (!caps.CAPABILITY) {
634         NSK_COMPLAIN1("GetCapabilities does not return \"%s\" capability\n",
635             CAPABILITY_STR);
636         return JNI_ERR;
637     }
638 
639     return JNI_OK;
640 }
641 
642 /* ========================================================================== */
643 
644 }
645