43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.core.test;
|
|
26 |
|
|
27 |
import java.io.ByteArrayOutputStream;
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.io.InputStream;
|
|
30 |
import java.lang.reflect.Constructor;
|
|
31 |
import java.util.HashMap;
|
|
32 |
|
46640
|
33 |
import org.graalvm.compiler.debug.DebugContext;
|
|
34 |
import org.graalvm.compiler.java.GraphBuilderPhase;
|
|
35 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
36 |
import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
|
|
37 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
|
|
38 |
import org.graalvm.compiler.nodes.java.RegisterFinalizerNode;
|
|
39 |
import org.graalvm.compiler.options.OptionValues;
|
|
40 |
import org.graalvm.compiler.phases.OptimisticOptimizations;
|
|
41 |
import org.graalvm.compiler.phases.common.CanonicalizerPhase;
|
|
42 |
import org.graalvm.compiler.phases.tiers.HighTierContext;
|
|
43 |
import org.junit.Assert;
|
|
44 |
import org.junit.Test;
|
|
45 |
|
43972
|
46 |
import jdk.vm.ci.meta.Assumptions;
|
|
47 |
import jdk.vm.ci.meta.Assumptions.Assumption;
|
|
48 |
import jdk.vm.ci.meta.Assumptions.LeafType;
|
|
49 |
import jdk.vm.ci.meta.Assumptions.NoFinalizableSubclass;
|
|
50 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
51 |
|
|
52 |
public class FinalizableSubclassTest extends GraalCompilerTest {
|
|
53 |
|
|
54 |
/**
|
|
55 |
* used as template to generate class files at runtime.
|
|
56 |
*/
|
|
57 |
public static class NoFinalizerEverAAAA {
|
|
58 |
}
|
|
59 |
|
|
60 |
public static class NoFinalizerYetAAAA {
|
|
61 |
}
|
|
62 |
|
|
63 |
public static final class WithFinalizerAAAA extends NoFinalizerYetAAAA {
|
|
64 |
|
51436
|
65 |
@SuppressWarnings("deprecation")
|
43972
|
66 |
@Override
|
|
67 |
protected void finalize() throws Throwable {
|
|
68 |
super.finalize();
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
private StructuredGraph parseAndProcess(Class<?> cl, AllowAssumptions allowAssumptions) {
|
|
73 |
Constructor<?>[] constructors = cl.getConstructors();
|
|
74 |
Assert.assertTrue(constructors.length == 1);
|
|
75 |
final ResolvedJavaMethod javaMethod = getMetaAccess().lookupJavaMethod(constructors[0]);
|
46640
|
76 |
OptionValues options = getInitialOptions();
|
47798
|
77 |
StructuredGraph graph = new StructuredGraph.Builder(options, getDebugContext(options, null, javaMethod), allowAssumptions).method(javaMethod).build();
|
43972
|
78 |
|
|
79 |
GraphBuilderConfiguration conf = GraphBuilderConfiguration.getSnippetDefault(getDefaultGraphBuilderPlugins());
|
54328
|
80 |
new GraphBuilderPhase.Instance(getProviders(), conf, OptimisticOptimizations.ALL, null).apply(graph);
|
43972
|
81 |
HighTierContext context = new HighTierContext(getProviders(), getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL);
|
52910
|
82 |
createInliningPhase().apply(graph, context);
|
43972
|
83 |
new CanonicalizerPhase().apply(graph, context);
|
|
84 |
return graph;
|
|
85 |
}
|
|
86 |
|
|
87 |
private void checkForRegisterFinalizeNode(Class<?> cl, boolean shouldContainFinalizer, AllowAssumptions allowAssumptions) {
|
|
88 |
StructuredGraph graph = parseAndProcess(cl, allowAssumptions);
|
|
89 |
Assert.assertTrue(graph.getNodes().filter(RegisterFinalizerNode.class).count() == (shouldContainFinalizer ? 1 : 0));
|
|
90 |
int noFinalizerAssumption = 0;
|
|
91 |
Assumptions assumptions = graph.getAssumptions();
|
|
92 |
if (assumptions != null) {
|
|
93 |
for (Assumption a : assumptions) {
|
|
94 |
if (a instanceof NoFinalizableSubclass) {
|
|
95 |
noFinalizerAssumption++;
|
|
96 |
} else if (a instanceof LeafType) {
|
|
97 |
// Need to also allow leaf type assumption instead of no finalizable subclass
|
|
98 |
// assumption.
|
|
99 |
noFinalizerAssumption++;
|
|
100 |
}
|
|
101 |
}
|
|
102 |
}
|
|
103 |
Assert.assertTrue(noFinalizerAssumption == (shouldContainFinalizer ? 0 : 1));
|
|
104 |
}
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Use a custom class loader to generate classes, to make sure the given classes are loaded in
|
|
108 |
* correct order.
|
|
109 |
*/
|
|
110 |
@Test
|
|
111 |
public void test1() throws ClassNotFoundException {
|
46640
|
112 |
DebugContext debug = getDebugContext();
|
43972
|
113 |
for (int i = 0; i < 2; i++) {
|
46640
|
114 |
ClassTemplateLoader loader = new ClassTemplateLoader(debug);
|
43972
|
115 |
checkForRegisterFinalizeNode(loader.findClass("NoFinalizerEverAAAA"), true, AllowAssumptions.NO);
|
|
116 |
checkForRegisterFinalizeNode(loader.findClass("NoFinalizerEverAAAA"), false, AllowAssumptions.YES);
|
|
117 |
|
|
118 |
checkForRegisterFinalizeNode(loader.findClass("NoFinalizerYetAAAA"), false, AllowAssumptions.YES);
|
|
119 |
|
|
120 |
checkForRegisterFinalizeNode(loader.findClass("WithFinalizerAAAA"), true, AllowAssumptions.YES);
|
|
121 |
checkForRegisterFinalizeNode(loader.findClass("NoFinalizerYetAAAA"), true, AllowAssumptions.YES);
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
private static class ClassTemplateLoader extends ClassLoader {
|
|
126 |
|
|
127 |
private static int loaderInstance = 0;
|
|
128 |
|
|
129 |
private final String replaceTo;
|
|
130 |
private HashMap<String, Class<?>> cache = new HashMap<>();
|
|
131 |
|
46640
|
132 |
private final DebugContext debug;
|
|
133 |
|
|
134 |
ClassTemplateLoader(DebugContext debug) {
|
43972
|
135 |
loaderInstance++;
|
46640
|
136 |
this.debug = debug;
|
43972
|
137 |
replaceTo = String.format("%04d", loaderInstance);
|
|
138 |
}
|
|
139 |
|
|
140 |
@Override
|
|
141 |
protected Class<?> findClass(final String name) throws ClassNotFoundException {
|
|
142 |
String nameReplaced = name.replaceAll("AAAA", replaceTo);
|
|
143 |
if (cache.containsKey(nameReplaced)) {
|
|
144 |
return cache.get(nameReplaced);
|
|
145 |
}
|
|
146 |
|
|
147 |
// copy classfile to byte array
|
|
148 |
byte[] classData = null;
|
|
149 |
try {
|
|
150 |
InputStream is = FinalizableSubclassTest.class.getResourceAsStream("FinalizableSubclassTest$" + name + ".class");
|
|
151 |
assert is != null;
|
|
152 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
153 |
|
|
154 |
byte[] buf = new byte[1024];
|
|
155 |
int size;
|
|
156 |
while ((size = is.read(buf, 0, buf.length)) != -1) {
|
|
157 |
baos.write(buf, 0, size);
|
|
158 |
}
|
|
159 |
baos.flush();
|
|
160 |
classData = baos.toByteArray();
|
|
161 |
} catch (IOException e) {
|
|
162 |
Assert.fail("can't access class: " + name);
|
|
163 |
}
|
46640
|
164 |
|
|
165 |
dumpStringsInByteArray(debug, classData);
|
43972
|
166 |
|
|
167 |
// replace all occurrences of "AAAA" in classfile
|
|
168 |
int index = -1;
|
|
169 |
while ((index = indexOfAAAA(classData, index + 1)) != -1) {
|
|
170 |
replaceAAAA(classData, index, replaceTo);
|
|
171 |
}
|
46640
|
172 |
dumpStringsInByteArray(debug, classData);
|
43972
|
173 |
|
|
174 |
Class<?> c = defineClass(null, classData, 0, classData.length);
|
|
175 |
cache.put(nameReplaced, c);
|
|
176 |
return c;
|
|
177 |
}
|
|
178 |
|
|
179 |
private static int indexOfAAAA(byte[] b, int index) {
|
|
180 |
for (int i = index; i < b.length; i++) {
|
|
181 |
boolean match = true;
|
|
182 |
for (int j = i; j < i + 4; j++) {
|
|
183 |
if (b[j] != (byte) 'A') {
|
|
184 |
match = false;
|
|
185 |
break;
|
|
186 |
}
|
|
187 |
}
|
|
188 |
if (match) {
|
|
189 |
return i;
|
|
190 |
}
|
|
191 |
}
|
|
192 |
return -1;
|
|
193 |
}
|
|
194 |
|
|
195 |
private static void replaceAAAA(byte[] b, int index, String replacer) {
|
|
196 |
assert replacer.length() == 4;
|
|
197 |
for (int i = index; i < index + 4; i++) {
|
|
198 |
b[i] = (byte) replacer.charAt(i - index);
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
46640
|
202 |
private static void dumpStringsInByteArray(DebugContext debug, byte[] b) {
|
43972
|
203 |
boolean wasChar = true;
|
|
204 |
StringBuilder sb = new StringBuilder();
|
|
205 |
for (Byte x : b) {
|
|
206 |
// check for [a-zA-Z0-9]
|
|
207 |
if ((x >= 0x41 && x <= 0x7a) || (x >= 0x30 && x <= 0x39)) {
|
|
208 |
if (!wasChar) {
|
46640
|
209 |
debug.log(sb + "");
|
43972
|
210 |
sb.setLength(0);
|
|
211 |
}
|
|
212 |
sb.append(String.format("%c", x));
|
|
213 |
wasChar = true;
|
|
214 |
} else {
|
|
215 |
wasChar = false;
|
|
216 |
}
|
|
217 |
}
|
46640
|
218 |
debug.log(sb + "");
|
43972
|
219 |
}
|
|
220 |
}
|
|
221 |
}
|