1 /*
  2  * Copyright (c) 2025, 2026, 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  * @test
 26  * @summary Tests heapwalking API (FollowReferences, IterateThroughHeap, GetObjectsWithTags) for value objects.
 27  * @requires vm.jvmti
 28  * @modules java.base/jdk.internal.vm.annotation java.base/jdk.internal.value
 29  * @enablePreview
 30  * @run main/othervm/native -agentlib:ValueHeapwalkingTest
 31  *                          -XX:+UnlockDiagnosticVMOptions
 32  *                          -XX:+UseArrayFlattening
 33  *                          -XX:+UseFieldFlattening
 34  *                          -XX:+UseAtomicValueFlattening
 35  *                          -XX:+UseNullableValueFlattening
 36  *                          -XX:+PrintInlineLayout
 37  *                          -XX:+PrintFlatArrayLayout
 38  *                          -Xlog:jvmti+table
 39  *                          ValueHeapwalkingTest
 40  */
 41 
 42 import java.lang.ref.Reference;
 43 import java.util.ArrayList;
 44 import java.util.Arrays;
 45 import java.util.List;
 46 import jdk.internal.value.ValueClass;
 47 import jdk.internal.vm.annotation.NullRestricted;
 48 
 49 import java.lang.reflect.Field;
 50 
 51 public class ValueHeapwalkingTest {
 52 
 53     static value class Value {
 54         int v;
 55         Value() {
 56             this(0);
 57         }
 58         Value(int v) {
 59             this.v = v;
 60         }
 61     }
 62 
 63     // flat object has flat field (address of the field is the same as address of the object)
 64     static value class Value2 {
 65         @NullRestricted
 66         public Value v1;
 67         @NullRestricted
 68         public Value v2;
 69         Value2() {
 70             this(0);
 71         }
 72         Value2(int i) {
 73             this.v1 = new Value(i);
 74             this.v2 = new Value(i+1);
 75             super();
 76         }
 77     }
 78 
 79     static value class ValueHolder {
 80         public Value v1;
 81         @NullRestricted
 82         public Value v2;
 83         public Value v_null;
 84 
 85         public Value2 v2_1;
 86         @NullRestricted
 87         public Value2 v2_2;
 88 
 89         public Value[] v_arr;
 90         public Value2[] v2_arr;
 91 
 92         public ValueHolder(int seed) throws Exception {
 93             v1 = new Value(seed);
 94             v2 = new Value(seed + 1);
 95             v_null = null;
 96 
 97             v2_1 = new Value2(seed + 6);
 98             v2_2 = new Value2(seed + 8);
 99 
100             v_arr = createValueArray(seed);
101             v2_arr = createValue2Array(seed);
102         }
103     }
104 
105     static Value[] createValueArray(int seed) throws Exception {
106         Value[] arr = (Value[])ValueClass.newNullableAtomicArray(Value.class, 5);
107         for (int i = 0; i < arr.length; i++) {
108             arr[i] = i == 2 ? null : new Value(seed + 10 + i);
109         }
110         return arr;
111     }
112 
113     static Value2[] createValue2Array(int seed) throws Exception {
114         Value2[] arr = (Value2[])ValueClass.newNullRestrictedNonAtomicArray(Value2.class, 5, Value2.class.newInstance());
115         for (int i = 0; i < arr.length; i++) {
116             arr[i] = new Value2(seed + 20 + i * 2);
117         }
118         return arr;
119     }
120 
121     static final int TAG_VALUE_CLASS = 1;
122     static final int TAG_VALUE2_CLASS = 2;
123     static final int TAG_HOLDER_CLASS = 3;
124     static final int TAG_VALUE_ARRAY = 4;
125     static final int TAG_VALUE2_ARRAY = 5;
126 
127     static final int TAG_MIN = TAG_VALUE_CLASS;
128     static final int TAG_MAX = TAG_VALUE2_ARRAY;
129 
130     static String tagStr(int tag) {
131         String suffix = " (tag " + tag + ")";
132         switch (tag) {
133         case TAG_VALUE_CLASS: return "Value class" + suffix;
134         case TAG_VALUE2_CLASS: return "Value2 class" + suffix;
135         case TAG_HOLDER_CLASS: return "ValueHolder class" + suffix;
136         case TAG_VALUE_ARRAY: return "Value[] object" + suffix;
137         case TAG_VALUE2_ARRAY: return "Value2[] object" + suffix;
138         }
139         return "Unknown" + suffix;
140     }
141 
142     static native void setTag(Object object, long tag);
143     static native long getTag(Object object);
144 
145     static native void reset();
146 
147     static native void followReferences();
148 
149     static native void iterateThroughHeap();
150 
151     static native int count(int classTag);
152     static native int refCount(int fromTag, int toTag);
153     static native int primitiveFieldCount(int tag);
154 
155     static native long getMaxTag();
156 
157     static native int getObjectWithTags(long minTag, long maxTag, Object[] objects, long[] tags);
158 
159 
160     // counts non-null elements in the array
161     static <T> int nonNullCount(T[] array) {
162         return (int)Arrays.stream(array).filter(e -> e != null).count();
163     }
164 
165     static void verifyMinCount(int classTag, int minCount) {
166         int count = count(classTag);
167         String msg = tagStr(classTag) + " count: " + count + ", min expected: " + minCount;
168         if (count < minCount) {
169             throw new RuntimeException(msg);
170         }
171         System.out.println(msg);
172     }
173 
174     static void verifyRefCount(int tagFrom, int tagTo, int expectedCount) {
175         int count = refCount(tagFrom, tagTo);
176         String msg = "Ref.count from " + tagStr(tagFrom) + " to " + tagStr(tagTo) + ": "
177                    + count + ", expected: " + expectedCount;
178         if (count !=  expectedCount) {
179             throw new RuntimeException(msg);
180         }
181         System.out.println(msg);
182     }
183 
184     static void verifyPrimitiveFieldCount(int classTag, int expectedCount) {
185         int count = primitiveFieldCount(classTag);
186         String msg = "Primitive field count from " + tagStr(classTag) + ": "
187                    + count + ", expected: " + expectedCount;
188         if (count !=  expectedCount) {
189             throw new RuntimeException(msg);
190         }
191         System.out.println(msg);
192     }
193 
194 
195     static void printObject(Object obj) {
196         printObject("", obj);
197     }
198 
199     static void printObject(String prefix, Object obj) {
200         if (obj == null) {
201             System.out.println(prefix + "null");
202             return;
203           }
204 
205         Class<?> clazz = obj.getClass();
206         System.out.println(prefix + "Object (class " + clazz.getName() + ", tag = " + getTag(obj) + ", class tag = " + getTag(clazz));
207 
208         if (clazz.isArray()) {
209             Class<?> elementType = clazz.getComponentType();
210             int length = java.lang.reflect.Array.getLength(obj);
211             System.out.println(prefix + "Array of " + elementType + ", length = " + length + " [");
212             for (int i = 0; i < length; i++) {
213                 Object element = java.lang.reflect.Array.get(obj, i);
214                 if (elementType.isPrimitive()) {
215                     if (i == 0) {
216                         System.out.print(prefix + "  ");
217                     } else {
218                         System.out.print(", ");
219                     }
220                     System.out.print(prefix + "(" + i + "):" + element);
221                 } else {
222                     System.out.println(prefix + "(" + i + "):" + "NOT primitive");
223                     printObject(prefix + "  ", element);
224                 }
225             }
226             System.out.println(prefix + "]");
227         } else {
228             while (clazz != null && clazz != Object.class) {
229                 Field[] fields = clazz.getDeclaredFields();
230                 for (Field field : fields) {
231                     try {
232                         field.setAccessible(true);
233                         Object value = field.get(obj);
234                         Class<?> fieldType = field.getType();
235                         System.out.println(prefix + "- " + field.getName() + " (" + fieldType + ") = " + value);
236 
237                         if (!fieldType.isPrimitive()) {
238                             printObject(prefix + "  ", value);
239                         }
240                     } catch (IllegalAccessException | java.lang.reflect.InaccessibleObjectException e) {
241                         System.err.println("  Error accessing field " + field.getName() + ": " + e.getMessage());
242                     }
243                 }
244                 clazz = clazz.getSuperclass();
245             }
246         }
247     }
248 
249     public static void main(String[] args) throws Exception {
250         System.loadLibrary("ValueHeapwalkingTest");
251         ValueHolder holder = new ValueHolder(10);
252 
253         setTag(Value.class, TAG_VALUE_CLASS);
254         setTag(Value2.class, TAG_VALUE2_CLASS);
255         setTag(ValueHolder.class, TAG_HOLDER_CLASS);
256         setTag(holder.v_arr, TAG_VALUE_ARRAY);
257         setTag(holder.v2_arr, TAG_VALUE2_ARRAY);
258 
259         reset();
260         System.out.println(">>iterateThroughHeap");
261         iterateThroughHeap();
262         System.out.println("<<iterateThroughHeap");
263 
264         // IterateThroughHeap reports reachable and unreachable objects,
265         // so verify only minimum count.
266         for (int i = TAG_MIN; i <= TAG_MAX; i++) {
267             System.out.println(tagStr(i) + " count: " + count(i));
268         }
269         int reachableValueHolderCount = 1;
270         // v2_1, v2_2, v2_arr
271         int reachableValue2Count = reachableValueHolderCount * (2 + nonNullCount(holder.v2_arr));
272         // ValueHolder: v1, v2, v_arr
273         // Value2: v1, v2
274         int reachableValueCount = reachableValueHolderCount * (2 + nonNullCount(holder.v_arr))
275                                 + reachableValue2Count * 2;
276         verifyMinCount(TAG_VALUE_CLASS, reachableValueCount);
277         verifyMinCount(TAG_VALUE2_CLASS, reachableValue2Count);
278         verifyMinCount(TAG_HOLDER_CLASS, reachableValueHolderCount);
279         // For each Value object 1 primitive field must be reported.
280         verifyPrimitiveFieldCount(TAG_VALUE_CLASS, count(TAG_VALUE_CLASS));
281 
282         reset();  // to reset primitiveFieldCount
283         System.out.println(">>followReferences");
284         followReferences();
285         System.out.println("<<followReferences");
286 
287         long maxTag = getMaxTag();
288 
289         for (int i = TAG_MIN; i <= TAG_MAX; i++) {
290             for (int j = TAG_MIN; j <= TAG_MAX; j++) {
291                 System.out.println("Reference from " + tagStr(i) + " to " + tagStr(j) + ": " + refCount(i, j));
292             }
293         }
294 
295         printObject(holder);
296 
297         // ValueHolder: v1, v2
298         verifyRefCount(TAG_HOLDER_CLASS, TAG_VALUE_CLASS, 2);
299         // ValueHolder: v2_1, v2_2
300         verifyRefCount(TAG_HOLDER_CLASS, TAG_VALUE_CLASS, 2);
301         // ValueHolder: v_arr
302         verifyRefCount(TAG_HOLDER_CLASS, TAG_VALUE_ARRAY, 1);
303         // ValueHolder: v2_arr
304         verifyRefCount(TAG_HOLDER_CLASS, TAG_VALUE2_ARRAY, 1);
305 
306         // References from arrays to their elements
307         verifyRefCount(TAG_VALUE_ARRAY, TAG_VALUE_CLASS, nonNullCount(holder.v_arr));
308         verifyRefCount(TAG_VALUE2_ARRAY, TAG_VALUE2_CLASS, nonNullCount(holder.v2_arr));
309 
310         // Each Value2 object must have 2 references to Value object (v1, v2).
311         verifyRefCount(TAG_VALUE2_CLASS, TAG_VALUE_CLASS, reachableValue2Count * 2);
312 
313         // For each Value object 1 primitive field must be reported.
314         verifyPrimitiveFieldCount(TAG_VALUE_CLASS, reachableValueCount);
315 
316         System.out.println(">>followReferences (2)");
317         followReferences();
318         System.out.println("<<followReferences (2)");
319 
320         long maxTag2 = getMaxTag();
321         // no new objects are expected to be tagged
322         if (maxTag != maxTag2) {
323             throw new RuntimeException("maxTag (" + maxTag + ") not equal to maxTag2(" + maxTag2 + ")");
324         }
325 
326         Object[] objects = new Object[1024];
327         long tags[] = new long[1024];
328         System.out.println(">>getObjectWithTags, maxTag = " + maxTag);
329         int count = getObjectWithTags(1, maxTag, objects, tags);
330         System.out.println("getObjectWithTags returned " + count);
331         for (int i = 0; i < count; i++) {
332             System.out.println("  [" + i + "] " + objects[i] + ", tag = " + tags[i]);
333             if (objects[i] == null || tags[i] == 0) {
334                 throw new RuntimeException("unexpected object");
335             }
336         }
337         int expectedTaggedCount = 5 // TAG_VALUE_CLASS/TAG_VALUE2_CLASS/TAG_HOLDER_CLASS/TAG_VALUE_ARRAY/TAG_VALUE2_ARRAY
338                 + reachableValue2Count + reachableValueCount;
339         if (count !=  expectedTaggedCount) {
340             throw new RuntimeException("unexpected getObjectWithTags result: " + count
341                                      + ", expected " + expectedTaggedCount);
342         }
343 
344         Reference.reachabilityFence(holder);
345     }
346 }