1 /*
 2  * Copyright (c) 2025, 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 import jdk.internal.vm.annotation.Strict;
38 
39 public class HeapwalkDupClasses {
40 
41     static native int tagWithFollowReferences(long tag);
42     static native int tagWithIterateOverReachableObjects(long tag);
43     static native Object[] getObjectsWithTags(long tag);
44 
45     public static void main(String[] args) throws Exception {
46         System.loadLibrary("HeapwalkDupClasses");
47 
48         Integer instance = new Integer(0);
49         Object[] testObjects = new Object[] {
50             new Integer[5],
51             ValueClass.newNullableAtomicArray(Integer.class, 5),
52             ValueClass.newNullRestrictedNonAtomicArray(Integer.class, 5, instance)
53         };
54 
55         for (long tag = 1; tag <= 2; tag++) {
56             int taggedClasses;
57             if (tag == 1) {
58                 System.out.println("FollowReferences");
59                 taggedClasses = tagWithFollowReferences(tag);
60             } else {
61                 System.out.println("IterateOverReachableObjects");
62                 taggedClasses = tagWithIterateOverReachableObjects(tag);
63             }
64             System.out.println("Tagged " + taggedClasses + " classes");
65 
66             Object[] taggedObjects = getObjectsWithTags(tag);
67             System.out.println("Tagged objects (total " + taggedObjects.length + "):");
68 
69             int duplicates = 0;
70             boolean foundTestObjectClass[] = new boolean[testObjects.length];
71 
72             for (int i = 0; i < taggedObjects.length; i++) {
73                 System.out.println("[" + i + "] " + taggedObjects[i]);
74                 for (int j = 0; j < i; j++) {
75                     if (taggedObjects[i].equals(taggedObjects[j])) {
76                         duplicates++;
77                         System.out.println("  ERROR: duplicate (" + j + ")");
78                     }
79                 }
80                 for (int j = 0; j < testObjects.length; j++) {
81                     if (taggedObjects[i].equals(testObjects[j].getClass())) {
82                         foundTestObjectClass[j] = true;
83                         System.out.println("  FOUND expected array class");
84                     }
85                 }
86             }
87             if (duplicates != 0) {
88             throw new RuntimeException("Found " + duplicates + " duplicate classes");
89             }
90             for (int i = 0; i < foundTestObjectClass.length; i++) {
91                 if (!foundTestObjectClass[i]) {
92                     throw new RuntimeException("Expected class not found: " + testObjects[i].getClass());
93                 }
94             }
95         }
96     }
97 }