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  * @bug 8368799
27  * @summary Verify heapwalking API does not report array classes several times.
28  * @requires vm.jvmti
29  * @modules java.base/jdk.internal.vm.annotation java.base/jdk.internal.value
30  * @enablePreview
31  * @run main/othervm/native -agentlib:HeapwalkDupClasses HeapwalkDupClasses
32  */
33 
34 import java.lang.ref.Reference;
35 import jdk.internal.value.ValueClass;
36 import jdk.internal.vm.annotation.NullRestricted;
37 
38 public class HeapwalkDupClasses {
39 
40     static native int tagWithFollowReferences(long tag);
41     static native int tagWithIterateOverReachableObjects(long tag);
42     static native Object[] getObjectsWithTags(long tag);
43 
44     public static void main(String[] args) throws Exception {
45         System.loadLibrary("HeapwalkDupClasses");
46 
47         Integer instance = new Integer(0);
48         Object[] testObjects = new Object[] {
49             new Integer[5],
50             ValueClass.newNullableAtomicArray(Integer.class, 5),
51             ValueClass.newNullRestrictedNonAtomicArray(Integer.class, 5, instance)
52         };
53 
54         for (long tag = 1; tag <= 2; tag++) {
55             int taggedClasses;
56             if (tag == 1) {
57                 System.out.println("FollowReferences");
58                 taggedClasses = tagWithFollowReferences(tag);
59             } else {
60                 System.out.println("IterateOverReachableObjects");
61                 taggedClasses = tagWithIterateOverReachableObjects(tag);
62             }
63             System.out.println("Tagged " + taggedClasses + " classes");
64 
65             Object[] taggedObjects = getObjectsWithTags(tag);
66             System.out.println("Tagged objects (total " + taggedObjects.length + "):");
67 
68             int duplicates = 0;
69             boolean foundTestObjectClass[] = new boolean[testObjects.length];
70 
71             for (int i = 0; i < taggedObjects.length; i++) {
72                 System.out.println("[" + i + "] " + taggedObjects[i]);
73                 for (int j = 0; j < i; j++) {
74                     if (taggedObjects[i].equals(taggedObjects[j])) {
75                         duplicates++;
76                         System.out.println("  ERROR: duplicate (" + j + ")");
77                     }
78                 }
79                 for (int j = 0; j < testObjects.length; j++) {
80                     if (taggedObjects[i].equals(testObjects[j].getClass())) {
81                         foundTestObjectClass[j] = true;
82                         System.out.println("  FOUND expected array class");
83                     }
84                 }
85             }
86             if (duplicates != 0) {
87             throw new RuntimeException("Found " + duplicates + " duplicate classes");
88             }
89             for (int i = 0; i < foundTestObjectClass.length; i++) {
90                 if (!foundTestObjectClass[i]) {
91                     throw new RuntimeException("Expected class not found: " + testObjects[i].getClass());
92                 }
93             }
94         }
95     }
96 }