Skip to content

Commit f9aadb9

Browse files
author
Alex Menkov
committed
8303489: Add a test to verify classes in vmStruct have unique vtables
Reviewed-by: cjplummer, sspitsyn
1 parent 595645c commit f9aadb9

File tree

2 files changed

+180
-2
lines changed

2 files changed

+180
-2
lines changed

src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/types/basic/BasicTypeDataBase.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ public long getOopSize() {
156156
return VM.getVM().getOopSize();
157157
}
158158

159-
Map<Type, Address> typeToVtbl = new HashMap<>();
159+
private Map<Type, Address> typeToVtbl = new HashMap<>();
160160

161-
private Address vtblForType(Type type) {
161+
public Address vtblForType(Type type) {
162162
Address vtblAddr = typeToVtbl.get(type);
163163
if (vtblAddr == null) {
164164
vtblAddr = vtblAccess.getVtblForType(type);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright (c) 2023, 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+
*
27+
* @library /test/lib
28+
* @requires vm.hasSA
29+
* @modules jdk.hotspot.agent/sun.jvm.hotspot
30+
* jdk.hotspot.agent/sun.jvm.hotspot.debugger
31+
* jdk.hotspot.agent/sun.jvm.hotspot.types
32+
* jdk.hotspot.agent/sun.jvm.hotspot.types.basic
33+
*
34+
* @run main/othervm UniqueVtableTest
35+
*/
36+
37+
import java.util.ArrayList;
38+
import java.util.Iterator;
39+
import java.util.HashMap;
40+
import java.util.List;
41+
import java.util.Map;
42+
43+
import sun.jvm.hotspot.HotSpotAgent;
44+
import sun.jvm.hotspot.debugger.Address;
45+
import sun.jvm.hotspot.types.Type;
46+
import sun.jvm.hotspot.types.basic.BasicTypeDataBase;
47+
48+
import jdk.test.lib.apps.LingeredApp;
49+
import jdk.test.lib.SA.SATestUtils;
50+
51+
52+
public class UniqueVtableTest {
53+
54+
private HotSpotAgent agent;
55+
56+
public UniqueVtableTest() {
57+
}
58+
59+
private static String type2String(Type t) {
60+
return t + " (extends " + t.getSuperclass() + ")";
61+
}
62+
63+
private static void log(Object o) {
64+
System.out.println(o);
65+
}
66+
67+
private void attach(long pid) throws Throwable {
68+
agent = new HotSpotAgent();
69+
log("Attaching to process ID " + pid + "...");
70+
agent.attach((int) pid);
71+
log("Attached successfully.");
72+
}
73+
74+
private void detach() {
75+
if (agent != null) {
76+
agent.detach();
77+
}
78+
}
79+
80+
private void runTest() throws Throwable {
81+
Map<Address, List<Type>> vtableToTypesMap = new HashMap<>();
82+
Iterator<Type> it = agent.getTypeDataBase().getTypes();
83+
int dupsFound = 0;
84+
// TypeDataBase knows nothing about vtables,
85+
// but actually agent.getTypeDataBase() returns HotSpotTypeDataBase (extends BasicTypeDataBase)
86+
// and BasicTypeDataBase has a method to get vtable for Types.
87+
BasicTypeDataBase typeDB = (BasicTypeDataBase)(agent.getTypeDataBase());
88+
int total = 0;
89+
int vm_classes_with_vtable = 0;
90+
int vm_classes_without_vtable = 0;
91+
while (it.hasNext()) {
92+
total++;
93+
Type t = it.next();
94+
Address vtable = typeDB.vtblForType(t);
95+
if (vtable != null) {
96+
vm_classes_with_vtable++;
97+
List<Type> typeList = vtableToTypesMap.get(vtable);
98+
if (typeList == null) {
99+
vtableToTypesMap.put(vtable, new ArrayList<>(List.of(t)));
100+
} else {
101+
// duplicate found
102+
dupsFound++;
103+
typeList.add(t);
104+
}
105+
}
106+
107+
// IntegerType/StringType/JavaPrimitiveType/OopType/PointerType types
108+
// are expected to have no vtable.
109+
// Log classes which might need vtable.
110+
if (vtable == null
111+
&& !t.isCIntegerType()
112+
&& !t.isCStringType()
113+
&& !t.isJavaPrimitiveType()
114+
&& !t.isOopType()
115+
&& !t.isPointerType()) {
116+
vm_classes_without_vtable++;
117+
log("vtable is null for " + type2String(t));
118+
}
119+
}
120+
log("total: " + total
121+
+ ", vm_classes_with_vtable: " + vm_classes_with_vtable
122+
+ ", vm_classes_without_vtable: " + vm_classes_without_vtable);
123+
if (dupsFound > 0) {
124+
vtableToTypesMap.forEach((vtable, list) -> {
125+
if (list.size() > 1) {
126+
log("Duplicate vtable: " + vtable + ": ");
127+
list.forEach(t -> log(" - " + type2String(t)));
128+
}
129+
});
130+
throw new RuntimeException("Duplicate vtable(s) found: " + dupsFound);
131+
}
132+
}
133+
134+
private void run() throws Throwable {
135+
Throwable reasonToFail = null;
136+
LingeredApp app = null;
137+
try {
138+
app = LingeredApp.startApp();
139+
attach(app.getPid());
140+
runTest();
141+
} catch (Throwable ex) {
142+
reasonToFail = ex;
143+
} finally {
144+
try {
145+
detach();
146+
} catch (Exception ex) {
147+
log("detach error:");
148+
ex.printStackTrace(System.out);
149+
// do not override original error
150+
if (reasonToFail != null) {
151+
reasonToFail = ex;
152+
}
153+
}
154+
try {
155+
LingeredApp.stopApp(app);
156+
} catch (Exception ex) {
157+
log("LingeredApp.stopApp error:");
158+
ex.printStackTrace(System.out);
159+
// do not override original error
160+
if (reasonToFail != null) {
161+
reasonToFail = ex;
162+
}
163+
}
164+
}
165+
if (reasonToFail != null) {
166+
throw reasonToFail;
167+
}
168+
}
169+
170+
public static void main(String... args) throws Throwable {
171+
SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.
172+
173+
UniqueVtableTest test = new UniqueVtableTest();
174+
175+
test.run();
176+
}
177+
178+
}

0 commit comments

Comments
 (0)