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 /*
25  * Unit tests for JVMTI IterateOverReachableObjects and
26  * IterateOverObjectsReachableFromObject functions.
27  *
28  */
29 
30 package nsk.jvmti.unit.heap;
31 
32 import nsk.share.jvmti.unit.*;
33 import java.io.PrintStream;
34 
35 public class HeapWalkTests {
36 
37     final static int JCK_STATUS_BASE = 95;
38     final static int PASSED = 0;
39     final static int FAILED = 2;
40 
main(String args[])41     public static void main(String args[]) {
42         args = nsk.share.jvmti.JVMTITest.commonInit(args);
43 
44         // produce JCK-like exit status.
45         System.exit(run(args, System.out) + JCK_STATUS_BASE);
46     }
47 
run(String args[], PrintStream out)48     public static int run(String args[], PrintStream out) {
49         test1();
50         test2();
51         test3();
52         return PASSED;
53     }
54 
test1()55     private static void test1() {
56         long tag;
57 
58         // This test uses the heap_root_callback to tag all the roots
59 
60         // It then examines a few known roots and checks that they
61         // are tagged.
62 
63         // create a global reference
64         Object o = new Object();
65         Heap.newGlobalRef(o);
66 
67         // make sure current thread isn't tagged
68         Heap.setTag(Thread.currentThread(), 0);
69 
70         Heap.setHeapRootCallback();
71         Heap.iterateOverReachableObjects();
72 
73         // o must be JNI global - there's no other way it can be a root
74 
75         tag = Heap.getTag(o);
76 
77         if (tag != Heap.JVMTI_HEAP_ROOT_JNI_GLOBAL) {
78             throw new RuntimeException("JNI global should have been tagged");
79         }
80 
81         // Current thread is root but can't assume it will be
82         // tagged with JVMTI_HEAP_ROOT_JNI_GLOBAL
83 
84         tag = Heap.getTag(Thread.currentThread());
85         if (tag == 0) {
86             throw new RuntimeException("Current thread isn't tagged");
87         }
88     }
89 
test2()90     private static void test2() {
91         long tag;
92 
93         // This test uses the stack_ref_callback to tag all references
94         // from the thread stacks. The callback tags all objects with the tag
95         // of the thread.
96 
97         Object o = new Object();
98 
99         Heap.setTag(Thread.currentThread(), 888);
100 
101         Heap.setStackRefCallback();
102         Heap.iterateOverReachableObjects();
103 
104         tag = Heap.getTag(o);
105         if (tag != 888) {
106             throw new RuntimeException("stack local not tagged correctly");
107         }
108     }
109 
110 
111     // used by test3
112     static class Foo {
113         private Object fld;
114 
Foo()115         Foo() {
116             fld = new Object();
117         }
118 
field()119         Object field() {
120             return fld;
121         }
122 
123         public static Object static_field = new Object();
124     }
125 
126     private static int failures = 0;
127 
check(Object o, long tag)128     private static void check(Object o, long tag) {
129         long actual_tag = Heap.getTag(o);
130         if (actual_tag != tag) {
131             if (actual_tag == 0) {
132                 System.err.println(o + " is not tagged!");
133             } else {
134                 System.err.println(o + " is incorrectly tagged");
135             }
136             failures++;
137         }
138     }
139 
test3()140     private static void test3() {
141         long tag;
142 
143         // This test tags an object, and then calls IterateOverObjectsReachableFromObject
144         // The callback tags all objects with a tag value of 777.
145 
146         Foo foo = new Foo();
147 
148         Heap.setObjectRefCallback();
149         Heap.iterateOverObjectsReachableFromObject(foo);
150 
151         check(Foo.class, 777);
152         check(Foo.static_field, 777);
153         check(foo.field(), 777);
154 
155         if (failures > 0) {
156             throw new RuntimeException("IterateOverObjectsReachableFromObject test failed");
157         }
158     }
159 }
160