1 /*
   2  * Copyright (c) 2011, 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.hotspot.test;
  24 
  25 import static org.hamcrest.CoreMatchers.not;
  26 import static org.junit.Assert.assertNotNull;
  27 import static org.junit.Assert.assertNull;
  28 import static org.junit.Assert.assertThat;
  29 
  30 import java.util.Arrays;
  31 import java.util.List;
  32 
  33 import org.junit.Test;
  34 
  35 import org.graalvm.compiler.code.CompilationResult;
  36 import org.graalvm.compiler.core.test.GraalCompilerTest;
  37 import org.graalvm.compiler.debug.DebugContext;
  38 import org.graalvm.compiler.debug.GraalError;
  39 
  40 import jdk.vm.ci.code.BytecodeFrame;
  41 import jdk.vm.ci.code.InstalledCode;
  42 import jdk.vm.ci.code.StackLockValue;
  43 import jdk.vm.ci.code.site.Call;
  44 import jdk.vm.ci.code.site.Infopoint;
  45 import jdk.vm.ci.meta.ResolvedJavaMethod;
  46 
  47 public class HotSpotMonitorValueTest extends GraalCompilerTest {
  48 
  49     @Override
  50     protected InstalledCode addMethod(DebugContext debug, ResolvedJavaMethod method, CompilationResult compResult) {
  51         for (Infopoint i : compResult.getInfopoints()) {
  52             if (i instanceof Call) {
  53                 Call call = (Call) i;
  54                 if (call.target instanceof ResolvedJavaMethod) {
  55                     ResolvedJavaMethod target = (ResolvedJavaMethod) call.target;
  56                     if (target.equals(lookupObjectWait())) {
  57                         BytecodeFrame frame = call.debugInfo.frame();
  58                         BytecodeFrame caller = frame.caller();
  59                         assertNotNull(caller);
  60                         assertNull(caller.caller());
  61                         assertDeepEquals(2, frame.numLocks);
  62                         assertDeepEquals(2, caller.numLocks);
  63                         StackLockValue lock1 = (StackLockValue) frame.getLockValue(0);
  64                         StackLockValue lock2 = (StackLockValue) frame.getLockValue(1);
  65                         StackLockValue lock3 = (StackLockValue) caller.getLockValue(0);
  66                         StackLockValue lock4 = (StackLockValue) caller.getLockValue(1);
  67 
  68                         List<StackLockValue> locks = Arrays.asList(lock1, lock2, lock3, lock4);
  69                         for (StackLockValue lock : locks) {
  70                             for (StackLockValue other : locks) {
  71                                 if (other != lock) {
  72                                     // Every lock must have a different stack slot
  73                                     assertThat(lock.getSlot(), not(other.getSlot()));
  74                                 }
  75                             }
  76                         }
  77                         assertDeepEquals(lock3.getOwner(), lock4.getOwner());
  78                         assertThat(lock1.getOwner(), not(lock2.getOwner()));
  79                         return super.addMethod(debug, method, compResult);
  80                     }
  81                 }
  82             }
  83         }
  84         throw new AssertionError("Could not find debug info for call to Object.wait(long)");
  85     }
  86 
  87     private ResolvedJavaMethod lookupObjectWait() {
  88         try {
  89             return getMetaAccess().lookupJavaMethod(Object.class.getDeclaredMethod("wait", long.class));
  90         } catch (Exception e) {
  91             throw new GraalError("Could not find Object.wait(long): %s", e);
  92         }
  93     }
  94 
  95     @Test
  96     public void test() {
  97         test("testSnippet", "a", "b");
  98     }
  99 
 100     private static void locks2(Object a, Object b) throws InterruptedException {
 101         synchronized (a) {
 102             synchronized (b) {
 103                 a.wait(5);
 104             }
 105         }
 106     }
 107 
 108     public static void testSnippet(Object a, Object b) throws InterruptedException {
 109         synchronized (a) {
 110             synchronized (a) {
 111                 locks2(a, b);
 112             }
 113         }
 114     }
 115 }