test/compiler/7190310/Test7190310_unsafe.java

Thu, 11 Apr 2013 09:39:57 -0700

author
katleman
date
Thu, 11 Apr 2013 09:39:57 -0700
changeset 4883
5dcfeb396fed
parent 0
f90c822e73f8
permissions
-rw-r--r--

Added tag jdk8-b85 for changeset 42fe530cd478

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

mercurial