001/*
002 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.test;
024
025import static org.hamcrest.CoreMatchers.*;
026import static org.junit.Assert.*;
027
028import java.util.*;
029
030import jdk.internal.jvmci.code.*;
031import jdk.internal.jvmci.code.CompilationResult.*;
032import jdk.internal.jvmci.common.*;
033import jdk.internal.jvmci.meta.*;
034
035import org.junit.*;
036
037import com.oracle.graal.compiler.test.*;
038
039public class HotSpotMonitorValueTest extends GraalCompilerTest {
040
041    @Override
042    protected InstalledCode addMethod(ResolvedJavaMethod method, CompilationResult compResult) {
043        for (Infopoint i : compResult.getInfopoints()) {
044            if (i instanceof Call) {
045                Call call = (Call) i;
046                if (call.target instanceof ResolvedJavaMethod) {
047                    ResolvedJavaMethod target = (ResolvedJavaMethod) call.target;
048                    if (target.equals(lookupObjectWait())) {
049                        BytecodeFrame frame = call.debugInfo.frame();
050                        BytecodeFrame caller = frame.caller();
051                        assertNotNull(caller);
052                        assertNull(caller.caller());
053                        assertDeepEquals(2, frame.numLocks);
054                        assertDeepEquals(2, caller.numLocks);
055                        StackLockValue lock1 = (StackLockValue) frame.getLockValue(0);
056                        StackLockValue lock2 = (StackLockValue) frame.getLockValue(1);
057                        StackLockValue lock3 = (StackLockValue) caller.getLockValue(0);
058                        StackLockValue lock4 = (StackLockValue) caller.getLockValue(1);
059
060                        List<StackLockValue> locks = Arrays.asList(lock1, lock2, lock3, lock4);
061                        for (StackLockValue lock : locks) {
062                            for (StackLockValue other : locks) {
063                                if (other != lock) {
064                                    // Every lock must have a different stack slot
065                                    assertThat(lock.getSlot(), not(other.getSlot()));
066                                }
067                            }
068                        }
069                        assertDeepEquals(lock3.getOwner(), lock4.getOwner());
070                        assertThat(lock1.getOwner(), not(lock2.getOwner()));
071                        return super.addMethod(method, compResult);
072                    }
073                }
074            }
075        }
076        throw new AssertionError("Could not find debug info for call to Object.wait(long)");
077    }
078
079    private ResolvedJavaMethod lookupObjectWait() {
080        try {
081            return getMetaAccess().lookupJavaMethod(Object.class.getDeclaredMethod("wait", long.class));
082        } catch (Exception e) {
083            throw new JVMCIError("Could not find Object.wait(long): %s", e);
084        }
085    }
086
087    @Test
088    public void test() {
089        test("testSnippet", "a", "b");
090    }
091
092    private static void locks2(Object a, Object b) throws InterruptedException {
093        synchronized (a) {
094            synchronized (b) {
095                a.wait(5);
096            }
097        }
098    }
099
100    public static void testSnippet(Object a, Object b) throws InterruptedException {
101        synchronized (a) {
102            synchronized (a) {
103                locks2(a, b);
104            }
105        }
106    }
107}