1 /* 2 * Copyright (c) 2015, 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 org.graalvm.compiler.java.GraphBuilderPhase; 26 import org.graalvm.compiler.nodes.StructuredGraph; 27 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; 28 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins; 29 import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins; 30 import org.graalvm.compiler.phases.OptimisticOptimizations; 31 import org.junit.Test; 32 import org.objectweb.asm.ClassWriter; 33 import org.objectweb.asm.Label; 34 import org.objectweb.asm.MethodVisitor; 35 import org.objectweb.asm.Opcodes; 36 37 import jdk.vm.ci.code.BailoutException; 38 import jdk.vm.ci.meta.ResolvedJavaMethod; 39 40 /** 41 * Exercise handling of unbalanced monitor operations by the parser. Algorithmically Graal assumes 42 * that locks are statically block structured but that isn't something enforced by the bytecodes. In 43 * HotSpot a dataflow is performed to ensure they are properly structured and methods with 44 * unstructured locking aren't compiled and fall back to the interpreter. Having the Graal parser 45 * handle this directly is simplifying for targets of Graal since they don't have to provide a data 46 * flow that checks this property. 47 */ 48 public class UnbalancedMonitorsTest extends GraalCompilerTest { 49 private static final String CLASS_NAME = UnbalancedMonitorsTest.class.getName(); 50 private static final String INNER_CLASS_NAME = CLASS_NAME + "$UnbalancedMonitors"; 51 private static final String CLASS_NAME_INTERNAL = CLASS_NAME.replace('.', '/'); 52 private static final String INNER_CLASS_NAME_INTERNAL = INNER_CLASS_NAME.replace('.', '/'); 53 54 public UnbalancedMonitorsTest() { 55 exportPackage(JAVA_BASE, "jdk.internal.org.objectweb.asm"); 56 } 57 58 private static AsmLoader LOADER = new AsmLoader(UnbalancedMonitorsTest.class.getClassLoader()); 59 60 @Test 61 public void runWrongOrder() throws Exception { 62 checkForBailout("wrongOrder"); 63 } 64 65 @Test 66 public void runTooFewExits() throws Exception { 67 checkForBailout("tooFewExits"); 68 } 69 70 @Test 71 public void runTooManyExits() throws Exception { 72 checkForBailout("tooManyExits"); 73 } 74 75 @Test 76 public void runTooFewExitsExceptional() throws Exception { 77 checkForBailout("tooFewExitsExceptional"); 78 } 79 80 @Test 81 public void runTooManyExitsExceptional() throws Exception { 82 checkForBailout("tooManyExitsExceptional"); 83 } 84 85 private void checkForBailout(String name) throws ClassNotFoundException { 86 ResolvedJavaMethod method = getResolvedJavaMethod(LOADER.findClass(INNER_CLASS_NAME), name); 87 try { 88 StructuredGraph graph = new StructuredGraph.Builder(getInitialOptions()).method(method).build(); 89 Plugins plugins = new Plugins(new InvocationPlugins(getMetaAccess())); 90 GraphBuilderConfiguration graphBuilderConfig = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true); 91 OptimisticOptimizations optimisticOpts = OptimisticOptimizations.NONE; 92 93 GraphBuilderPhase.Instance graphBuilder = new GraphBuilderPhase.Instance(getMetaAccess(), getProviders().getStampProvider(), null, null, graphBuilderConfig, optimisticOpts, null); 94 graphBuilder.apply(graph); 95 } catch (BailoutException e) { 96 if (e.getMessage().contains("unbalanced monitors")) { 97 return; 98 } 99 throw e; 100 } 101 assertTrue("should have bailed out", false); 102 } 103 104 static class Gen implements Opcodes { 105 106 // @formatter:off 107 // Template class used with Bytecode Outline to generate ASM code 108 // public static class UnbalancedMonitors { 109 // 110 // public UnbalancedMonitors() { 111 // } 112 // 113 // public Object wrongOrder(Object a, Object b) { 114 // synchronized (a) { 115 // synchronized (b) { 116 // return b; 117 // } 118 // } 119 // } 120 // 121 // public Object tooFewExits(Object a, Object b) { 122 // synchronized (a) { 123 // synchronized (b) { 124 // return b; 125 // } 126 // } 127 // } 128 // 129 // public boolean tooFewExitsExceptional(Object a, Object b) { 130 // synchronized (a) { 131 // synchronized (b) { 132 // return b.equals(a); 133 // } 134 // } 135 // } 136 // } 137 // @formatter:on 138 139 public static byte[] generateClass() { 140 141 ClassWriter cw = new ClassWriter(0); 142 143 cw.visit(52, ACC_SUPER | ACC_PUBLIC, INNER_CLASS_NAME_INTERNAL, null, "java/lang/Object", null); 144 145 cw.visitSource("UnbalancedMonitorsTest.java", null); 146 147 cw.visitInnerClass(INNER_CLASS_NAME_INTERNAL, CLASS_NAME_INTERNAL, "UnbalancedMonitors", ACC_STATIC); 148 149 visitConstructor(cw); 150 visitWrongOrder(cw); 151 visitBlockStructured(cw, true, false); 152 visitBlockStructured(cw, true, true); 153 visitBlockStructured(cw, false, false); 154 visitBlockStructured(cw, false, true); 155 cw.visitEnd(); 156 157 return cw.toByteArray(); 158 } 159 160 private static void visitBlockStructured(ClassWriter cw, boolean normalReturnError, boolean tooMany) { 161 String name = (tooMany ? "tooMany" : "tooFew") + "Exits" + (normalReturnError ? "" : "Exceptional"); 162 // Generate too many or too few exits down the either the normal or exceptional return 163 // paths 164 int exceptionalExitCount = normalReturnError ? 1 : (tooMany ? 2 : 0); 165 int normalExitCount = normalReturnError ? (tooMany ? 2 : 0) : 1; 166 MethodVisitor mv; 167 mv = cw.visitMethod(ACC_PUBLIC, name, "(Ljava/lang/Object;Ljava/lang/Object;)Z", null, null); 168 mv.visitCode(); 169 Label l0 = new Label(); 170 Label l1 = new Label(); 171 Label l2 = new Label(); 172 mv.visitTryCatchBlock(l0, l1, l2, null); 173 Label l3 = new Label(); 174 mv.visitTryCatchBlock(l2, l3, l2, null); 175 Label l4 = new Label(); 176 Label l5 = new Label(); 177 Label l6 = new Label(); 178 mv.visitTryCatchBlock(l4, l5, l6, null); 179 Label l7 = new Label(); 180 mv.visitTryCatchBlock(l2, l7, l6, null); 181 Label l8 = new Label(); 182 mv.visitLabel(l8); 183 mv.visitVarInsn(ALOAD, 1); 184 mv.visitInsn(DUP); 185 mv.visitVarInsn(ASTORE, 3); 186 mv.visitInsn(MONITORENTER); 187 mv.visitLabel(l4); 188 mv.visitVarInsn(ALOAD, 2); 189 mv.visitInsn(DUP); 190 mv.visitVarInsn(ASTORE, 4); 191 mv.visitInsn(MONITORENTER); 192 mv.visitLabel(l0); 193 mv.visitVarInsn(ALOAD, 2); 194 mv.visitVarInsn(ALOAD, 1); 195 mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false); 196 mv.visitVarInsn(ALOAD, 4); 197 mv.visitInsn(MONITOREXIT); 198 mv.visitLabel(l1); 199 for (int i = 0; i < normalExitCount; i++) { 200 mv.visitVarInsn(ALOAD, 3); 201 mv.visitInsn(MONITOREXIT); 202 } 203 mv.visitLabel(l5); 204 mv.visitInsn(IRETURN); 205 mv.visitLabel(l2); 206 mv.visitFrame(Opcodes.F_FULL, 5, new Object[]{INNER_CLASS_NAME_INTERNAL, "java/lang/Object", "java/lang/Object", "java/lang/Object", 207 "java/lang/Object"}, 1, new Object[]{"java/lang/Throwable"}); 208 mv.visitVarInsn(ALOAD, 4); 209 mv.visitInsn(MONITOREXIT); 210 mv.visitLabel(l3); 211 mv.visitInsn(ATHROW); 212 mv.visitLabel(l6); 213 mv.visitFrame(Opcodes.F_FULL, 4, new Object[]{INNER_CLASS_NAME_INTERNAL, "java/lang/Object", "java/lang/Object", "java/lang/Object"}, 1, 214 new Object[]{"java/lang/Throwable"}); 215 for (int i = 0; i < exceptionalExitCount; i++) { 216 mv.visitVarInsn(ALOAD, 3); 217 mv.visitInsn(MONITOREXIT); 218 } 219 mv.visitLabel(l7); 220 mv.visitInsn(ATHROW); 221 Label l9 = new Label(); 222 mv.visitLabel(l9); 223 mv.visitMaxs(2, 5); 224 mv.visitEnd(); 225 } 226 227 private static void visitWrongOrder(ClassWriter cw) { 228 MethodVisitor mv; 229 mv = cw.visitMethod(ACC_PUBLIC, "wrongOrder", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, null); 230 mv.visitCode(); 231 Label l0 = new Label(); 232 Label l1 = new Label(); 233 Label l2 = new Label(); 234 mv.visitTryCatchBlock(l0, l1, l2, null); 235 Label l3 = new Label(); 236 mv.visitTryCatchBlock(l2, l3, l2, null); 237 Label l4 = new Label(); 238 Label l5 = new Label(); 239 Label l6 = new Label(); 240 mv.visitTryCatchBlock(l4, l5, l6, null); 241 Label l7 = new Label(); 242 mv.visitTryCatchBlock(l2, l7, l6, null); 243 Label l8 = new Label(); 244 mv.visitLabel(l8); 245 mv.visitVarInsn(ALOAD, 1); 246 mv.visitInsn(DUP); 247 mv.visitVarInsn(ASTORE, 3); 248 mv.visitInsn(MONITORENTER); 249 mv.visitLabel(l4); 250 mv.visitVarInsn(ALOAD, 2); 251 mv.visitInsn(DUP); 252 mv.visitVarInsn(ASTORE, 4); 253 mv.visitInsn(MONITORENTER); 254 mv.visitLabel(l0); 255 mv.visitVarInsn(ALOAD, 2); 256 mv.visitVarInsn(ALOAD, 3); 257 mv.visitInsn(MONITOREXIT); 258 mv.visitLabel(l1); 259 // Swapped exit order with exit above 260 mv.visitVarInsn(ALOAD, 4); 261 mv.visitInsn(MONITOREXIT); 262 mv.visitLabel(l5); 263 mv.visitInsn(ARETURN); 264 mv.visitLabel(l2); 265 mv.visitFrame(Opcodes.F_FULL, 5, new Object[]{INNER_CLASS_NAME_INTERNAL, "java/lang/Object", "java/lang/Object", "java/lang/Object", 266 "java/lang/Object"}, 1, new Object[]{"java/lang/Throwable"}); 267 mv.visitVarInsn(ALOAD, 4); 268 mv.visitInsn(MONITOREXIT); 269 mv.visitLabel(l3); 270 mv.visitInsn(ATHROW); 271 mv.visitLabel(l6); 272 mv.visitFrame(Opcodes.F_FULL, 4, new Object[]{INNER_CLASS_NAME_INTERNAL, "java/lang/Object", "java/lang/Object", "java/lang/Object"}, 1, 273 new Object[]{"java/lang/Throwable"}); 274 mv.visitVarInsn(ALOAD, 3); 275 mv.visitInsn(MONITOREXIT); 276 mv.visitLabel(l7); 277 mv.visitInsn(ATHROW); 278 Label l9 = new Label(); 279 mv.visitLabel(l9); 280 mv.visitMaxs(2, 5); 281 mv.visitEnd(); 282 } 283 284 private static void visitConstructor(ClassWriter cw) { 285 MethodVisitor mv; 286 mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 287 mv.visitCode(); 288 Label l0 = new Label(); 289 mv.visitLabel(l0); 290 mv.visitVarInsn(ALOAD, 0); 291 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); 292 Label l1 = new Label(); 293 mv.visitLabel(l1); 294 mv.visitInsn(RETURN); 295 Label l2 = new Label(); 296 mv.visitLabel(l2); 297 mv.visitMaxs(1, 1); 298 mv.visitEnd(); 299 } 300 } 301 302 public static class AsmLoader extends ClassLoader { 303 Class<?> loaded; 304 305 public AsmLoader(ClassLoader parent) { 306 super(parent); 307 } 308 309 @Override 310 protected Class<?> findClass(String name) throws ClassNotFoundException { 311 if (name.equals(INNER_CLASS_NAME)) { 312 if (loaded != null) { 313 return loaded; 314 } 315 byte[] bytes = Gen.generateClass(); 316 return (loaded = defineClass(name, bytes, 0, bytes.length)); 317 } else { 318 return super.findClass(name); 319 } 320 } 321 } 322 }