1 /* 2 * Copyright (c) 2013, 2018, 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 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 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 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 65 @SuppressWarnings("deprecation") 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]); 76 OptionValues options = getInitialOptions(); 77 StructuredGraph graph = new StructuredGraph.Builder(options, getDebugContext(options, null, javaMethod), allowAssumptions).method(javaMethod).build(); 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 createInliningPhase().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 DebugContext debug = getDebugContext(); 114 for (int i = 0; i < 2; i++) { 115 ClassTemplateLoader loader = new ClassTemplateLoader(debug); 116 checkForRegisterFinalizeNode(loader.findClass("NoFinalizerEverAAAA"), true, AllowAssumptions.NO); 117 checkForRegisterFinalizeNode(loader.findClass("NoFinalizerEverAAAA"), false, AllowAssumptions.YES); 118 119 checkForRegisterFinalizeNode(loader.findClass("NoFinalizerYetAAAA"), false, AllowAssumptions.YES); 120 121 checkForRegisterFinalizeNode(loader.findClass("WithFinalizerAAAA"), true, AllowAssumptions.YES); 122 checkForRegisterFinalizeNode(loader.findClass("NoFinalizerYetAAAA"), true, AllowAssumptions.YES); 123 } 124 } 125 126 private static class ClassTemplateLoader extends ClassLoader { 127 128 private static int loaderInstance = 0; 129 130 private final String replaceTo; 131 private HashMap<String, Class<?>> cache = new HashMap<>(); 132 133 private final DebugContext debug; 134 135 ClassTemplateLoader(DebugContext debug) { 136 loaderInstance++; 137 this.debug = debug; 138 replaceTo = String.format("%04d", loaderInstance); 139 } 140 141 @Override 142 protected Class<?> findClass(final String name) throws ClassNotFoundException { 143 String nameReplaced = name.replaceAll("AAAA", replaceTo); 144 if (cache.containsKey(nameReplaced)) { 145 return cache.get(nameReplaced); 146 } 147 148 // copy classfile to byte array 149 byte[] classData = null; 150 try { 151 InputStream is = FinalizableSubclassTest.class.getResourceAsStream("FinalizableSubclassTest$" + name + ".class"); 152 assert is != null; 153 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 154 155 byte[] buf = new byte[1024]; 156 int size; 157 while ((size = is.read(buf, 0, buf.length)) != -1) { 158 baos.write(buf, 0, size); 159 } 160 baos.flush(); 161 classData = baos.toByteArray(); 162 } catch (IOException e) { 163 Assert.fail("can't access class: " + name); 164 } 165 166 dumpStringsInByteArray(debug, classData); 167 168 // replace all occurrences of "AAAA" in classfile 169 int index = -1; 170 while ((index = indexOfAAAA(classData, index + 1)) != -1) { 171 replaceAAAA(classData, index, replaceTo); 172 } 173 dumpStringsInByteArray(debug, classData); 174 175 Class<?> c = defineClass(null, classData, 0, classData.length); 176 cache.put(nameReplaced, c); 177 return c; 178 } 179 180 private static int indexOfAAAA(byte[] b, int index) { 181 for (int i = index; i < b.length; i++) { 182 boolean match = true; 183 for (int j = i; j < i + 4; j++) { 184 if (b[j] != (byte) 'A') { 185 match = false; 186 break; 187 } 188 } 189 if (match) { 190 return i; 191 } 192 } 193 return -1; 194 } 195 196 private static void replaceAAAA(byte[] b, int index, String replacer) { 197 assert replacer.length() == 4; 198 for (int i = index; i < index + 4; i++) { 199 b[i] = (byte) replacer.charAt(i - index); 200 } 201 } 202 203 private static void dumpStringsInByteArray(DebugContext debug, byte[] b) { 204 boolean wasChar = true; 205 StringBuilder sb = new StringBuilder(); 206 for (Byte x : b) { 207 // check for [a-zA-Z0-9] 208 if ((x >= 0x41 && x <= 0x7a) || (x >= 0x30 && x <= 0x39)) { 209 if (!wasChar) { 210 debug.log(sb + ""); 211 sb.setLength(0); 212 } 213 sb.append(String.format("%c", x)); 214 wasChar = true; 215 } else { 216 wasChar = false; 217 } 218 } 219 debug.log(sb + ""); 220 } 221 } 222 }