1 /*
   2  * Copyright (c) 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 8143628
  27  * @summary Test unsafe access for Object
  28  *
  29  * @modules jdk.unsupported/sun.misc
  30  * @run testng/othervm -Diters=100   -Xint                   compiler.unsafe.SunMiscUnsafeAccessTestObject
  31  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 compiler.unsafe.SunMiscUnsafeAccessTestObject
  32  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  compiler.unsafe.SunMiscUnsafeAccessTestObject
  33  * @run testng/othervm -Diters=20000                         compiler.unsafe.SunMiscUnsafeAccessTestObject
  34  */
  35 
  36 package compiler.unsafe;
  37 
  38 import org.testng.annotations.Test;
  39 
  40 import java.lang.reflect.Field;
  41 
  42 import static org.testng.Assert.*;
  43 
  44 public class SunMiscUnsafeAccessTestObject {
  45     static final int ITERS = Integer.getInteger("iters", 1);
  46     static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
  47 
  48     static final sun.misc.Unsafe UNSAFE;
  49 
  50     static final long V_OFFSET;
  51 
  52     static final Object STATIC_V_BASE;
  53 
  54     static final long STATIC_V_OFFSET;
  55 
  56     static int ARRAY_OFFSET;
  57 
  58     static int ARRAY_SHIFT;
  59 
  60     static {
  61         try {
  62             Field f = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
  63             f.setAccessible(true);
  64             UNSAFE = (sun.misc.Unsafe) f.get(null);
  65         } catch (Exception e) {
  66             throw new RuntimeException("Unable to get Unsafe instance.", e);
  67         }
  68 
  69         try {
  70             Field staticVField = SunMiscUnsafeAccessTestObject.class.getDeclaredField("static_v");
  71             STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField);
  72             STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField);
  73         } catch (Exception e) {
  74             throw new RuntimeException(e);
  75         }
  76 
  77         try {
  78             Field vField = SunMiscUnsafeAccessTestObject.class.getDeclaredField("v");
  79             V_OFFSET = UNSAFE.objectFieldOffset(vField);
  80         } catch (Exception e) {
  81             throw new RuntimeException(e);
  82         }
  83 
  84         ARRAY_OFFSET = UNSAFE.arrayBaseOffset(Object[].class);
  85         int ascale = UNSAFE.arrayIndexScale(Object[].class);
  86         ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
  87     }
  88 
  89     static Object static_v;
  90 
  91     Object v;
  92 
  93     @Test
  94     public void testFieldInstance() {
  95         SunMiscUnsafeAccessTestObject t = new SunMiscUnsafeAccessTestObject();
  96         for (int c = 0; c < ITERS; c++) {
  97             testAccess(t, V_OFFSET);
  98         }
  99     }
 100 
 101     @Test
 102     public void testFieldStatic() {
 103         for (int c = 0; c < ITERS; c++) {
 104             testAccess(STATIC_V_BASE, STATIC_V_OFFSET);
 105         }
 106     }
 107 
 108     @Test
 109     public void testArray() {
 110         Object[] array = new Object[10];
 111         for (int c = 0; c < ITERS; c++) {
 112             for (int i = 0; i < array.length; i++) {
 113                 testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET);
 114             }
 115         }
 116     }
 117 
 118 
 119     static void testAccess(Object base, long offset) {
 120         // Plain
 121         {
 122             UNSAFE.putObject(base, offset, "foo");
 123             Object x = UNSAFE.getObject(base, offset);
 124             assertEquals(x, "foo", "set Object value");
 125         }
 126 
 127         // Volatile
 128         {
 129             UNSAFE.putObjectVolatile(base, offset, "bar");
 130             Object x = UNSAFE.getObjectVolatile(base, offset);
 131             assertEquals(x, "bar", "putVolatile Object value");
 132         }
 133 
 134         // Lazy
 135         {
 136             UNSAFE.putOrderedObject(base, offset, "foo");
 137             Object x = UNSAFE.getObjectVolatile(base, offset);
 138             assertEquals(x, "foo", "putRelease Object value");
 139         }
 140 
 141 
 142 
 143         UNSAFE.putObject(base, offset, "foo");
 144 
 145         // Compare
 146         {
 147             boolean r = UNSAFE.compareAndSwapObject(base, offset, "foo", "bar");
 148             assertEquals(r, true, "success compareAndSwap Object");
 149             Object x = UNSAFE.getObject(base, offset);
 150             assertEquals(x, "bar", "success compareAndSwap Object value");
 151         }
 152 
 153         {
 154             boolean r = UNSAFE.compareAndSwapObject(base, offset, "foo", "baz");
 155             assertEquals(r, false, "failing compareAndSwap Object");
 156             Object x = UNSAFE.getObject(base, offset);
 157             assertEquals(x, "bar", "failing compareAndSwap Object value");
 158         }
 159 
 160         UNSAFE.putObject(base, offset, "bar");
 161 
 162         // Compare set and get
 163         {
 164             Object o = UNSAFE.getAndSetObject(base, offset, "foo");
 165             assertEquals(o, "bar", "getAndSet Object");
 166             Object x = UNSAFE.getObject(base, offset);
 167             assertEquals(x, "foo", "getAndSet Object value");
 168         }
 169 
 170     }
 171 
 172 }
 173