LockInstrumentationTest.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2016, 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 */
23package org.graalvm.compiler.api.directives.test;
24
25import java.io.IOException;
26
27import org.junit.Assert;
28import org.junit.Test;
29
30import org.graalvm.compiler.api.directives.GraalDirectives;
31import org.graalvm.compiler.core.common.GraalOptions;
32import org.graalvm.compiler.core.test.GraalCompilerTest;
33import org.graalvm.compiler.options.OptionValue;
34import org.graalvm.compiler.options.OptionValue.OverrideScope;
35
36import jdk.internal.org.objectweb.asm.Opcodes;
37import jdk.vm.ci.code.InstalledCode;
38import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
39import jdk.vm.ci.meta.ResolvedJavaMethod;
40
41@SuppressWarnings("try")
42public class LockInstrumentationTest extends GraalCompilerTest {
43
44    private TinyInstrumentor instrumentor;
45
46    public LockInstrumentationTest() {
47        HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) getResolvedJavaMethod(ClassA.class, "notInlinedMethod");
48        method.setNotInlineable();
49
50        try {
51            instrumentor = new TinyInstrumentor(LockInstrumentationTest.class, "instrumentation");
52        } catch (IOException e) {
53            Assert.fail("unable to initialize the instrumentor: " + e);
54        }
55    }
56
57    public static class ClassA {
58
59        // This method should be marked as not inlineable
60        public void notInlinedMethod() {
61        }
62
63    }
64
65    public static final Object lock = new Object();
66    public static boolean lockAfterCheckPoint;
67    public static boolean checkpoint;
68
69    private static void resetFlags() {
70        lockAfterCheckPoint = false;
71        checkpoint = false;
72    }
73
74    static void instrumentation() {
75        GraalDirectives.instrumentationBeginForPredecessor();
76        lockAfterCheckPoint = checkpoint;
77        GraalDirectives.instrumentationEnd();
78    }
79
80    public static void lockSnippet() {
81        synchronized (lock) {
82            checkpoint = true;
83            ClassA a = new ClassA();
84            a.notInlinedMethod();
85        }
86    }
87
88    @Test
89    public void testLock() {
90        try (OverrideScope s = OptionValue.override(GraalOptions.UseGraalInstrumentation, true)) {
91            Class<?> clazz = instrumentor.instrument(LockInstrumentationTest.class, "lockSnippet", Opcodes.MONITORENTER);
92            ResolvedJavaMethod method = getResolvedJavaMethod(clazz, "lockSnippet");
93            executeExpected(method, null); // ensure the method is fully resolved
94            resetFlags();
95            // The monitorenter anchors. We expect the instrumentation set the flag before passing
96            // the checkpoint.
97            InstalledCode code = getCode(method);
98            code.executeVarargs();
99            Assert.assertFalse("expected lock was performed before checkpoint", lockAfterCheckPoint);
100        } catch (Throwable e) {
101            throw new AssertionError(e);
102        }
103    }
104
105    public static void postponeLockSnippet() {
106        ClassA a = new ClassA();
107
108        synchronized (a) {
109            checkpoint = true;
110            a.notInlinedMethod();
111        }
112
113    }
114
115    @Test
116    public void testNonEscapeLock() {
117        try (OverrideScope s = OptionValue.override(GraalOptions.UseGraalInstrumentation, true)) {
118            Class<?> clazz = instrumentor.instrument(LockInstrumentationTest.class, "postponeLockSnippet", Opcodes.MONITORENTER);
119            ResolvedJavaMethod method = getResolvedJavaMethod(clazz, "postponeLockSnippet");
120            executeExpected(method, null); // ensure the method is fully resolved
121            resetFlags();
122            // The lock in the snippet will be relocated before the invocation to
123            // notInlinedMethod(), i.e., after the checkpoint. We expect the instrumentation follows
124            // and flag will be set to true.
125            InstalledCode code = getCode(method);
126            code.executeVarargs();
127            Assert.assertTrue("expected lock was performed after checkpoint", lockAfterCheckPoint);
128        } catch (Throwable e) {
129            throw new AssertionError(e);
130        }
131    }
132
133}
134