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