Test7190310_unsafe.java revision 12158:0fe2815ffa74
1/* 2 * Copyright (c) 2012, 2015, 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 * @test 26 * @bug 7190310 27 * @summary Inlining WeakReference.get(), and hoisting $referent may lead to non-terminating loops 28 * @modules java.base/jdk.internal.misc 29 * 30 * @run main/othervm -Xbatch compiler.c2.Test7190310_unsafe 31 */ 32 33package compiler.c2; 34 35import jdk.internal.misc.Unsafe; 36 37import java.lang.ref.Reference; 38import java.lang.ref.WeakReference; 39import java.lang.reflect.Field; 40 41public class Test7190310_unsafe { 42 43 static class TestObject { 44 public String toString() { 45 return "TestObject"; 46 } 47 } 48 49 ; 50 51 private static TestObject str = new TestObject(); 52 private static final WeakReference ref = new WeakReference(str); 53 54 private TestObject obj; 55 56 public static void main(String[] args) throws Exception { 57 Class c = Test7190310_unsafe.class.getClassLoader().loadClass("jdk.internal.misc.Unsafe"); 58 Field f = c.getDeclaredField("theUnsafe"); 59 f.setAccessible(true); 60 Unsafe unsafe = (Unsafe) f.get(c); 61 62 f = Reference.class.getDeclaredField("referent"); 63 f.setAccessible(true); 64 long referent_offset = unsafe.objectFieldOffset(f); 65 66 Test7190310_unsafe t = new Test7190310_unsafe(); 67 TestObject o = new TestObject(); 68 t.obj = o; 69 70 // Warmup (compile methods) 71 System.err.println("Warmup"); 72 Object obj = null; 73 for (int i = 0; i < 11000; i++) { 74 obj = getRef0(ref); 75 } 76 for (int i = 0; i < 11000; i++) { 77 obj = getRef1(unsafe, ref, referent_offset); 78 } 79 for (int i = 0; i < 11000; i++) { 80 obj = getRef2(unsafe, ref, referent_offset); 81 } 82 for (int i = 0; i < 11000; i++) { 83 obj = getRef3(unsafe, ref, referent_offset); 84 } 85 for (int i = 0; i < 11000; i++) { 86 obj = getRef4(unsafe, t, referent_offset); 87 } 88 89 // Access verification 90 System.err.println("Verification"); 91 if (!verifyGet(referent_offset, unsafe)) { 92 System.exit(97); 93 } 94 95 obj = getRef3(unsafe, t, referent_offset); 96 if (obj != o) { 97 System.out.println("FAILED: unsafe.getObject(Object, " + referent_offset + ") " + obj + " != " + o); 98 System.exit(97); 99 } 100 obj = getRef4(unsafe, t, referent_offset); 101 if (obj != o) { 102 System.out.println("FAILED: unsafe.getObject(Test7190310, " + referent_offset + ") " + obj + " != " + o); 103 System.exit(97); 104 } 105 } 106 107 static boolean verifyGet(long referent_offset, Unsafe unsafe) throws Exception { 108 // Access verification 109 System.out.println("referent: " + str); 110 Object obj = getRef0(ref); 111 if (obj != str) { 112 System.out.println("FAILED: weakRef.get() " + obj + " != " + str); 113 return false; 114 } 115 obj = getRef1(unsafe, ref, referent_offset); 116 if (obj != str) { 117 System.out.println("FAILED: unsafe.getObject(weakRef, " + referent_offset + ") " + obj + " != " + str); 118 return false; 119 } 120 obj = getRef2(unsafe, ref, referent_offset); 121 if (obj != str) { 122 System.out.println("FAILED: unsafe.getObject(abstRef, " + referent_offset + ") " + obj + " != " + str); 123 return false; 124 } 125 obj = getRef3(unsafe, ref, referent_offset); 126 if (obj != str) { 127 System.out.println("FAILED: unsafe.getObject(Object, " + referent_offset + ") " + obj + " != " + str); 128 return false; 129 } 130 return true; 131 } 132 133 static Object getRef0(WeakReference ref) throws Exception { 134 return ref.get(); 135 } 136 137 static Object getRef1(Unsafe unsafe, WeakReference ref, long referent_offset) throws Exception { 138 return unsafe.getObject(ref, referent_offset); 139 } 140 141 static Object getRef2(Unsafe unsafe, Reference ref, long referent_offset) throws Exception { 142 return unsafe.getObject(ref, referent_offset); 143 } 144 145 static Object getRef3(Unsafe unsafe, Object ref, long referent_offset) throws Exception { 146 return unsafe.getObject(ref, referent_offset); 147 } 148 149 static Object getRef4(Unsafe unsafe, Test7190310_unsafe ref, long referent_offset) throws Exception { 150 return unsafe.getObject(ref, referent_offset); 151 } 152} 153 154