1 /*
   2  * Copyright (c) 2017, 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.hotspot.test;
  26 
  27 import java.util.function.Function;
  28 
  29 import org.graalvm.compiler.test.GraalTest;
  30 import org.junit.Assume;
  31 import org.junit.Rule;
  32 import org.junit.Test;
  33 import org.junit.rules.TestRule;
  34 
  35 import jdk.vm.ci.code.InstalledCode;
  36 import jdk.vm.ci.code.InvalidInstalledCodeException;
  37 import jdk.vm.ci.code.stack.InspectedFrame;
  38 import jdk.vm.ci.code.stack.StackIntrospection;
  39 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
  40 import jdk.vm.ci.meta.ResolvedJavaMethod;
  41 
  42 /**
  43  * Create a single object which is referenced from a local, the expression stack and the lock state
  44  * and then ensure that identity is maintained when the frame is forced to be materialized by
  45  * {@link InspectedFrame#materializeVirtualObjects(boolean)}.
  46  */
  47 public class HotSpotStackIntrospectionTest extends HotSpotGraalCompilerTest {
  48 
  49     @Rule public TestRule timeout = createTimeoutSeconds(20);
  50 
  51     static StackIntrospection stackIntrospection = HotSpotJVMCIRuntime.runtime().getHostJVMCIBackend().getStackIntrospection();
  52     static volatile int v;
  53 
  54     public static void testSynchronizedSnippet(Function<Void, Void> f) {
  55         Object a = new Object();
  56         synchronized (a) {
  57             testOnStack(a, forceFrameState(a, f), a);
  58             // This object should be locked so try to notify on it
  59             a.notify();
  60         }
  61     }
  62 
  63     public static void testSnippet(Function<Void, Void> f) {
  64         Object a = new Object();
  65         testOnStack(a, forceFrameState(a, f), a);
  66     }
  67 
  68     private static void testOnStack(Object a, Object a2, Object a3) {
  69         if (a != a2 || a != a3) {
  70             throw new InternalError();
  71         }
  72     }
  73 
  74     private static Object forceFrameState(Object a, Function<Void, Void> f) {
  75         // Use a volatile store to ensure a FrameState is captured after this point.
  76         v++;
  77         f.apply(null);
  78         return a;
  79     }
  80 
  81     @Test
  82     public void run() throws InvalidInstalledCodeException {
  83         // The JDK9 bits are currently broken
  84         Assume.assumeTrue(GraalTest.Java8OrEarlier);
  85         test("testSnippet");
  86     }
  87 
  88     @Test
  89     public void runSynchronized() throws InvalidInstalledCodeException {
  90         // The JDK9 bits are currently broken
  91         Assume.assumeTrue(GraalTest.Java8OrEarlier);
  92         test("testSynchronizedSnippet");
  93     }
  94 
  95     private void test(String name) throws InvalidInstalledCodeException {
  96         ResolvedJavaMethod method = getMetaAccess().lookupJavaMethod(getMethod(name));
  97         Function<Void, Void> f = o -> {
  98             stackIntrospection.iterateFrames(null, null, 0, frame -> {
  99                 if (frame.getMethod().equals(method)) {
 100                     frame.materializeVirtualObjects(true);
 101                 }
 102                 return null;
 103             });
 104             return null;
 105         };
 106         InstalledCode code = getCode(method);
 107         code.executeVarargs(f);
 108     }
 109 
 110 }