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 int
  28  * @modules jdk.unsupported/sun.misc
  29  * @run testng/othervm -Diters=100   -Xint                   SunMiscUnsafeAccessTestInt
  30  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 SunMiscUnsafeAccessTestInt
  31  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  SunMiscUnsafeAccessTestInt
  32  * @run testng/othervm -Diters=20000                         SunMiscUnsafeAccessTestInt
  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 SunMiscUnsafeAccessTestInt {
  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 = SunMiscUnsafeAccessTestInt.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 = SunMiscUnsafeAccessTestInt.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(int[].class);
  82         int ascale = UNSAFE.arrayIndexScale(int[].class);
  83         ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
  84     }
  85 
  86     static int static_v;
  87 
  88     int v;
  89 
  90     @Test
  91     public void testFieldInstance() {
  92         SunMiscUnsafeAccessTestInt t = new SunMiscUnsafeAccessTestInt();
  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         int[] array = new int[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     @Test
 116     public void testArrayOffHeap() {
 117         int size = 10;
 118         long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
 119         try {
 120             for (int c = 0; c < ITERS; c++) {
 121                 for (int i = 0; i < size; i++) {
 122                     testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
 123                 }
 124             }
 125         } finally {
 126             UNSAFE.freeMemory(address);
 127         }
 128     }
 129 
 130     @Test
 131     public void testArrayOffHeapDirect() {
 132         int size = 10;
 133         long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
 134         try {
 135             for (int c = 0; c < ITERS; c++) {
 136                 for (int i = 0; i < size; i++) {
 137                     testAccess((((long) i) << ARRAY_SHIFT) + address);
 138                 }
 139             }
 140         } finally {
 141             UNSAFE.freeMemory(address);
 142         }
 143     }
 144 
 145     static void testAccess(Object base, long offset) {
 146         // Plain
 147         {
 148             UNSAFE.putInt(base, offset, 1);
 149             int x = UNSAFE.getInt(base, offset);
 150             assertEquals(x, 1, "set int value");
 151         }
 152 
 153         // Volatile
 154         {
 155             UNSAFE.putIntVolatile(base, offset, 2);
 156             int x = UNSAFE.getIntVolatile(base, offset);
 157             assertEquals(x, 2, "putVolatile int value");
 158         }
 159 
 160         // Lazy
 161         {
 162             UNSAFE.putOrderedInt(base, offset, 1);
 163             int x = UNSAFE.getIntVolatile(base, offset);
 164             assertEquals(x, 1, "putRelease int value");
 165         }
 166 
 167 
 168 
 169         UNSAFE.putInt(base, offset, 1);
 170 
 171         // Compare
 172         {
 173             boolean r = UNSAFE.compareAndSwapInt(base, offset, 1, 2);
 174             assertEquals(r, true, "success compareAndSwap int");
 175             int x = UNSAFE.getInt(base, offset);
 176             assertEquals(x, 2, "success compareAndSwap int value");
 177         }
 178 
 179         {
 180             boolean r = UNSAFE.compareAndSwapInt(base, offset, 1, 3);
 181             assertEquals(r, false, "failing compareAndSwap int");
 182             int x = UNSAFE.getInt(base, offset);
 183             assertEquals(x, 2, "failing compareAndSwap int value");
 184         }
 185 
 186         UNSAFE.putInt(base, offset, 2);
 187 
 188         // Compare set and get
 189         {
 190             int o = UNSAFE.getAndSetInt(base, offset, 1);
 191             assertEquals(o, 2, "getAndSet int");
 192             int x = UNSAFE.getInt(base, offset);
 193             assertEquals(x, 1, "getAndSet int value");
 194         }
 195 
 196         UNSAFE.putInt(base, offset, 1);
 197 
 198         // get and add, add and get
 199         {
 200             int o = UNSAFE.getAndAddInt(base, offset, 2);
 201             assertEquals(o, 1, "getAndAdd int");
 202             int x = UNSAFE.getInt(base, offset);
 203             assertEquals(x, 1 + 2, "getAndAdd int");
 204         }
 205     }
 206 
 207     static void testAccess(long address) {
 208         // Plain
 209         {
 210             UNSAFE.putInt(address, 1);
 211             int x = UNSAFE.getInt(address);
 212             assertEquals(x, 1, "set int value");
 213         }
 214     }
 215 }
 216