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 extern "C" {
31 
32 #define CAPABILITY can_suspend
33 #define CAPABILITY_STR "can_suspend"
34 
35 /* The test checks capability can_suspend
36  * and correspondent function:
37  *     SuspendThread
38  *     SuspendThreadList
39  *     ResumeThread
40  *     ResumeThreadList
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(jvmti->GetAllThreads(&threads_count, &threads)))
80         return NSK_FALSE;
81 
82     if (!NSK_VERIFY(threads_count > 0 && threads != NULL))
83         return NSK_FALSE;
84 
85     /* find tested thread */
86     for (i = 0; i < threads_count; i++) {
87         if (!NSK_VERIFY(threads[i] != NULL))
88             return NSK_FALSE;
89 
90         /* get thread information */
91         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(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(jvmti->Deallocate((unsigned char*)threads)))
104         return NSK_FALSE;
105 
106     /* get tested thread class */
107     if (!NSK_JNI_VERIFY(jni, (klass = jni->GetObjectClass(thread)) != NULL))
108         return NSK_FALSE;
109 
110     /* get tested thread method 'run' */
111     if (!NSK_JNI_VERIFY(jni, (method = jni->GetMethodID(klass, "run", "()V")) != NULL))
112         return NSK_FALSE;
113 
114     /* get tested thread field 'waitingMonitor' */
115     if (!NSK_JNI_VERIFY(jni, (field =
116             jni->GetFieldID(klass, "waitingMonitor", "Ljava/lang/Object;")) != NULL))
117         return NSK_FALSE;
118 
119     return NSK_TRUE;
120 }
121 
122 /* ========================================================================== */
123 
124 /* Check "can_suspend" functions
125  */
checkSuspend()126 static int checkSuspend() {
127     jvmtiError err;
128 
129     NSK_DISPLAY0("Checking positive: SuspendThread\n");
130     if (!NSK_JVMTI_VERIFY(jvmti->SuspendThread(thread)))
131         return NSK_FALSE;
132 
133     NSK_DISPLAY0("Checking positive: ResumeThread\n");
134     if (!NSK_JVMTI_VERIFY(jvmti->ResumeThread(thread)))
135         return NSK_FALSE;
136 
137     NSK_DISPLAY0("Checking positive: SuspendThreadList\n");
138     if (!NSK_JVMTI_VERIFY(jvmti->SuspendThreadList(1, &thread, &err)))
139         return NSK_FALSE;
140 
141     NSK_DISPLAY0("Checking positive: ResumeThreadList\n");
142     if (!NSK_JVMTI_VERIFY(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 negative: GetThreadCpuTimerInfo\n");
480     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
481             jvmti->GetThreadCpuTimerInfo(&info)))
482         return NSK_FALSE;
483 
484     NSK_DISPLAY0("Checking negative: GetThreadCpuTime\n");
485     if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
486             jvmti->GetThreadCpuTime(thread, &nanos)))
487         return NSK_FALSE;
488 
489     return NSK_TRUE;
490 }
491 
492 /* ========================================================================== */
493 
494 /* agent algorithm */
495 static void JNICALL
agentProc(jvmtiEnv * jvmti,JNIEnv * agentJNI,void * arg)496 agentProc(jvmtiEnv* jvmti, JNIEnv* agentJNI, void* arg) {
497     jni = agentJNI;
498 
499     /* wait for initial sync */
500     if (!nsk_jvmti_waitForSync(timeout))
501         return;
502 
503     if (!prepare()) {
504         nsk_jvmti_setFailStatus();
505         return;
506     }
507 
508     /* testcase #7: check that only correspondent function work */
509     NSK_DISPLAY0("Testcase #7: check that only correspondent function work but not others\n");
510     if (!checkSuspend())
511         nsk_jvmti_setFailStatus();
512     if (!checkSignalThread())
513         nsk_jvmti_setFailStatus();
514     if (!checkGetOwnedMonitorInfo())
515         nsk_jvmti_setFailStatus();
516     if (!checkGetCurrentContendedMonitor())
517         nsk_jvmti_setFailStatus();
518     if (!checkPopFrame())
519         nsk_jvmti_setFailStatus();
520     if (!checkHeapFunctions())
521         nsk_jvmti_setFailStatus();
522     if (!checkLocalVariableFunctions())
523         nsk_jvmti_setFailStatus();
524     if (!checkSourceInfoFunctions())
525         nsk_jvmti_setFailStatus();
526     if (!checkRedefineClasses())
527         nsk_jvmti_setFailStatus();
528     if (!checkGetObjectMonitorUsage())
529         nsk_jvmti_setFailStatus();
530     if (!checkIsSyntheticFunctions())
531         nsk_jvmti_setFailStatus();
532     if (!checkGetBytecodes())
533         nsk_jvmti_setFailStatus();
534     if (!checkGetCurrentThreadCpuTime())
535         nsk_jvmti_setFailStatus();
536     if (!checkGetThreadCpuTime())
537         nsk_jvmti_setFailStatus();
538 
539     /* testcase #8: exits with the capability has not been relinquished */
540     NSK_DISPLAY0("Testcase #8: check if VM exits well with the capability has not been relinquished\n");
541 
542     /* resume debugee after last sync */
543     if (!nsk_jvmti_resumeSync())
544         return;
545 }
546 
547 /* ========================================================================== */
548 
549 /* agent library initialization */
550 #ifdef STATIC_BUILD
Agent_OnLoad_cm01t012(JavaVM * jvm,char * options,void * reserved)551 JNIEXPORT jint JNICALL Agent_OnLoad_cm01t012(JavaVM *jvm, char *options, void *reserved) {
552     return Agent_Initialize(jvm, options, reserved);
553 }
Agent_OnAttach_cm01t012(JavaVM * jvm,char * options,void * reserved)554 JNIEXPORT jint JNICALL Agent_OnAttach_cm01t012(JavaVM *jvm, char *options, void *reserved) {
555     return Agent_Initialize(jvm, options, reserved);
556 }
JNI_OnLoad_cm01t012(JavaVM * jvm,char * options,void * reserved)557 JNIEXPORT jint JNI_OnLoad_cm01t012(JavaVM *jvm, char *options, void *reserved) {
558     return JNI_VERSION_1_8;
559 }
560 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)561 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
562     jvmtiCapabilities caps;
563 
564     /* init framework and parse options */
565     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
566         return JNI_ERR;
567 
568     timeout = nsk_jvmti_getWaitTime() * 60000;
569     NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout);
570 
571     /* create JVMTI environment */
572     if (!NSK_VERIFY((jvmti =
573             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
574         return JNI_ERR;
575 
576     /* register agent proc and arg */
577     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
578         return JNI_ERR;
579 
580     /* testcase #1: check GetPotentialCapabilities */
581     NSK_DISPLAY0("Testcase #1: check if GetPotentialCapabilities returns the capability\n");
582     if (!NSK_JVMTI_VERIFY(jvmti->GetPotentialCapabilities(&caps)))
583         return JNI_ERR;
584     if (!caps.CAPABILITY) {
585         NSK_COMPLAIN1("GetPotentialCapabilities does not return \"%s\" capability\n",
586             CAPABILITY_STR);
587         return JNI_ERR;
588     }
589 
590     /* testcase #2: add the capability during Onload phase */
591     NSK_DISPLAY0("Testcase #2: add the capability during Onload phase\n");
592     memset(&caps, 0, sizeof(caps));
593     caps.CAPABILITY = 1;
594     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
595         return JNI_ERR;
596 
597     /* testcase #3: check if GetCapabilities returns the capability */
598     NSK_DISPLAY0("Testcase #3: check if GetCapabilities returns the capability\n");
599     memset(&caps, 0, sizeof(caps));
600     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
601         return JNI_ERR;
602     if (!caps.CAPABILITY) {
603         NSK_COMPLAIN1("GetCapabilities does not return \"%s\" capability\n",
604             CAPABILITY_STR);
605         return JNI_ERR;
606     }
607 
608     /* testcase #4: relinquish the capability during Onload phase */
609     NSK_DISPLAY0("Testcase #4: relinquish the capability during Onload phase\n");
610     memset(&caps, 0, sizeof(caps));
611     caps.CAPABILITY = 1;
612     if (!NSK_JVMTI_VERIFY(jvmti->RelinquishCapabilities(&caps)))
613         return JNI_ERR;
614 
615     /* testcase #5: check if GetCapabilities does not return the capability */
616     NSK_DISPLAY0("Testcase #5: check if GetCapabilities does not return the capability\n");
617     memset(&caps, 0, sizeof(caps));
618     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
619         return JNI_ERR;
620     if (caps.CAPABILITY) {
621         NSK_COMPLAIN1("GetCapabilities returns relinquished \"%s\" capability\n",
622             CAPABILITY_STR);
623         return JNI_ERR;
624     }
625 
626     /* testcase #6: add back the capability and check with GetCapabilities */
627     NSK_DISPLAY0("Testcase #6: add back the capability and check with GetCapabilities\n");
628     memset(&caps, 0, sizeof(caps));
629     caps.CAPABILITY = 1;
630     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
631         return JNI_ERR;
632     memset(&caps, 0, sizeof(caps));
633     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
634         return JNI_ERR;
635     if (!caps.CAPABILITY) {
636         NSK_COMPLAIN1("GetCapabilities does not return \"%s\" capability\n",
637             CAPABILITY_STR);
638         return JNI_ERR;
639     }
640 
641     return JNI_OK;
642 }
643 
644 /* ========================================================================== */
645 
646 }
647