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