3965N/A/*
3965N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3965N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3965N/A *
3965N/A * This code is free software; you can redistribute it and/or modify it
3965N/A * under the terms of the GNU General Public License version 2 only, as
3965N/A * published by the Free Software Foundation.
3965N/A *
3965N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3965N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3965N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3965N/A * version 2 for more details (a copy is included in the LICENSE file that
3965N/A * accompanied this code).
3965N/A *
3965N/A * You should have received a copy of the GNU General Public License version
3965N/A * 2 along with this work; if not, write to the Free Software Foundation,
3965N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3965N/A *
3965N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3965N/A * or visit www.oracle.com if you need additional information or have any
3965N/A * questions.
3965N/A *
3965N/A */
3965N/A
3965N/A/*
3965N/A * @test
3965N/A * @bug 7190310
3965N/A * @summary Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops
3965N/A * @run main/othervm -Xbatch Test7190310_unsafe
3965N/A */
3965N/A
3965N/Aimport java.lang.ref.*;
3965N/Aimport java.lang.reflect.*;
3965N/Aimport sun.misc.Unsafe;
3965N/A
3965N/Apublic class Test7190310_unsafe {
3965N/A
3965N/A static class TestObject {
3965N/A public String toString() {
3965N/A return "TestObject";
3965N/A }
3965N/A };
3965N/A
3965N/A private static TestObject str = new TestObject();
3965N/A private static final WeakReference ref = new WeakReference(str);
3965N/A
3965N/A private TestObject obj;
3965N/A
3965N/A public static void main(String[] args) throws Exception {
3965N/A Class c = Test7190310_unsafe.class.getClassLoader().loadClass("sun.misc.Unsafe");
3965N/A Field f = c.getDeclaredField("theUnsafe");
3965N/A f.setAccessible(true);
3965N/A Unsafe unsafe = (Unsafe)f.get(c);
3965N/A
3965N/A f = Reference.class.getDeclaredField("referent");
3965N/A f.setAccessible(true);
3965N/A long referent_offset = unsafe.objectFieldOffset(f);
3965N/A
3965N/A Test7190310_unsafe t = new Test7190310_unsafe();
3965N/A TestObject o = new TestObject();
3965N/A t.obj = o;
3965N/A
3965N/A // Warmup (compile methods)
3965N/A System.err.println("Warmup");
3965N/A Object obj = null;
3965N/A for (int i = 0; i < 11000; i++) {
3965N/A obj = getRef0(ref);
3965N/A }
3965N/A for (int i = 0; i < 11000; i++) {
3965N/A obj = getRef1(unsafe, ref, referent_offset);
3965N/A }
3965N/A for (int i = 0; i < 11000; i++) {
3965N/A obj = getRef2(unsafe, ref, referent_offset);
3965N/A }
3965N/A for (int i = 0; i < 11000; i++) {
3965N/A obj = getRef3(unsafe, ref, referent_offset);
3965N/A }
3965N/A for (int i = 0; i < 11000; i++) {
3965N/A obj = getRef4(unsafe, t, referent_offset);
3965N/A }
3965N/A
3965N/A // Access verification
3965N/A System.err.println("Verification");
3965N/A if (!verifyGet(referent_offset, unsafe)) {
3965N/A System.exit(97);
3965N/A }
3965N/A
3965N/A obj = getRef3(unsafe, t, referent_offset);
3965N/A if (obj != o) {
3965N/A System.out.println("FAILED: unsafe.getObject(Object, " + referent_offset + ") " + obj + " != " + o);
3965N/A System.exit(97);
3965N/A }
3965N/A obj = getRef4(unsafe, t, referent_offset);
3965N/A if (obj != o) {
3965N/A System.out.println("FAILED: unsafe.getObject(Test7190310, " + referent_offset + ") " + obj + " != " + o);
3965N/A System.exit(97);
3965N/A }
3965N/A }
3965N/A
3965N/A static boolean verifyGet(long referent_offset, Unsafe unsafe) throws Exception {
3965N/A // Access verification
3965N/A System.out.println("referent: " + str);
3965N/A Object obj = getRef0(ref);
3965N/A if (obj != str) {
3965N/A System.out.println("FAILED: weakRef.get() " + obj + " != " + str);
3965N/A return false;
3965N/A }
3965N/A obj = getRef1(unsafe, ref, referent_offset);
3965N/A if (obj != str) {
3965N/A System.out.println("FAILED: unsafe.getObject(weakRef, " + referent_offset + ") " + obj + " != " + str);
3965N/A return false;
3965N/A }
3965N/A obj = getRef2(unsafe, ref, referent_offset);
3965N/A if (obj != str) {
3965N/A System.out.println("FAILED: unsafe.getObject(abstRef, " + referent_offset + ") " + obj + " != " + str);
3965N/A return false;
3965N/A }
3965N/A obj = getRef3(unsafe, ref, referent_offset);
3965N/A if (obj != str) {
3965N/A System.out.println("FAILED: unsafe.getObject(Object, " + referent_offset + ") " + obj + " != " + str);
3965N/A return false;
3965N/A }
3965N/A return true;
3965N/A }
3965N/A
3965N/A static Object getRef0(WeakReference ref) throws Exception {
3965N/A return ref.get();
3965N/A }
3965N/A static Object getRef1(Unsafe unsafe, WeakReference ref, long referent_offset) throws Exception {
3965N/A return unsafe.getObject(ref, referent_offset);
3965N/A }
3965N/A static Object getRef2(Unsafe unsafe, Reference ref, long referent_offset) throws Exception {
3965N/A return unsafe.getObject(ref, referent_offset);
3965N/A }
3965N/A static Object getRef3(Unsafe unsafe, Object ref, long referent_offset) throws Exception {
3965N/A return unsafe.getObject(ref, referent_offset);
3965N/A }
3965N/A static Object getRef4(Unsafe unsafe, Test7190310_unsafe ref, long referent_offset) throws Exception {
3965N/A return unsafe.getObject(ref, referent_offset);
3965N/A }
3965N/A}
3965N/A