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