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