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