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