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 short
  28  *
  29  * @modules java.base/jdk.internal.misc:+open
  30  * @run testng/othervm -Diters=100   -Xint                   compiler.unsafe.JdkInternalMiscUnsafeAccessTestShort
  31  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 compiler.unsafe.JdkInternalMiscUnsafeAccessTestShort
  32  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  compiler.unsafe.JdkInternalMiscUnsafeAccessTestShort
  33  * @run testng/othervm -Diters=20000                         compiler.unsafe.JdkInternalMiscUnsafeAccessTestShort
  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 JdkInternalMiscUnsafeAccessTestShort {
  45     static final int ITERS = Integer.getInteger("iters", 1);
  46     static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
  47 
  48     static final jdk.internal.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 = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
  63             f.setAccessible(true);
  64             UNSAFE = (jdk.internal.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 = JdkInternalMiscUnsafeAccessTestShort.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 = JdkInternalMiscUnsafeAccessTestShort.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(short[].class);
  85         int ascale = UNSAFE.arrayIndexScale(short[].class);
  86         ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
  87     }
  88 
  89     static short static_v;
  90 
  91     short v;
  92 
  93     @Test
  94     public void testFieldInstance() {
  95         JdkInternalMiscUnsafeAccessTestShort t = new JdkInternalMiscUnsafeAccessTestShort();
  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         short[] array = new short[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     @Test
 119     public void testArrayOffHeap() {
 120         int size = 10;
 121         long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
 122         try {
 123             for (int c = 0; c < ITERS; c++) {
 124                 for (int i = 0; i < size; i++) {
 125                     testAccess(null, (((long) i) << ARRAY_SHIFT) + address);
 126                 }
 127             }
 128         } finally {
 129             UNSAFE.freeMemory(address);
 130         }
 131     }
 132 
 133     @Test
 134     public void testArrayOffHeapDirect() {
 135         int size = 10;
 136         long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT);
 137         try {
 138             for (int c = 0; c < ITERS; c++) {
 139                 for (int i = 0; i < size; i++) {
 140                     testAccess((((long) i) << ARRAY_SHIFT) + address);
 141                 }
 142             }
 143         } finally {
 144             UNSAFE.freeMemory(address);
 145         }
 146     }
 147 
 148     static void testAccess(Object base, long offset) {
 149         // Plain
 150         {
 151             UNSAFE.putShort(base, offset, (short)0x0123);
 152             short x = UNSAFE.getShort(base, offset);
 153             assertEquals(x, (short)0x0123, "set short value");
 154         }
 155 
 156         // Volatile
 157         {
 158             UNSAFE.putShortVolatile(base, offset, (short)0x4567);
 159             short x = UNSAFE.getShortVolatile(base, offset);
 160             assertEquals(x, (short)0x4567, "putVolatile short value");
 161         }
 162 
 163 
 164         // Lazy
 165         {
 166             UNSAFE.putShortRelease(base, offset, (short)0x0123);
 167             short x = UNSAFE.getShortAcquire(base, offset);
 168             assertEquals(x, (short)0x0123, "putRelease short value");
 169         }
 170 
 171         // Opaque
 172         {
 173             UNSAFE.putShortOpaque(base, offset, (short)0x4567);
 174             short x = UNSAFE.getShortOpaque(base, offset);
 175             assertEquals(x, (short)0x4567, "putOpaque short value");
 176         }
 177 
 178         // Unaligned
 179         {
 180             UNSAFE.putShortUnaligned(base, offset, (short)0x4567);
 181             short x = UNSAFE.getShortUnaligned(base, offset);
 182             assertEquals(x, (short)0x4567, "putUnaligned short value");
 183         }
 184 
 185         {
 186             UNSAFE.putShortUnaligned(base, offset, (short)0x0123, true);
 187             short x = UNSAFE.getShortUnaligned(base, offset, true);
 188             assertEquals(x, (short)0x0123, "putUnaligned big endian short value");
 189         }
 190 
 191         {
 192             UNSAFE.putShortUnaligned(base, offset, (short)0x4567, false);
 193             short x = UNSAFE.getShortUnaligned(base, offset, false);
 194             assertEquals(x, (short)0x4567, "putUnaligned little endian short value");
 195         }
 196 
 197         UNSAFE.putShort(base, offset, (short)0x0123);
 198 
 199         // Compare
 200         {
 201             boolean r = UNSAFE.compareAndSwapShort(base, offset, (short)0x0123, (short)0x4567);
 202             assertEquals(r, true, "success compareAndSwap short");
 203             short x = UNSAFE.getShort(base, offset);
 204             assertEquals(x, (short)0x4567, "success compareAndSwap short value");
 205         }
 206 
 207         {
 208             boolean r = UNSAFE.compareAndSwapShort(base, offset, (short)0x0123, (short)0x89AB);
 209             assertEquals(r, false, "failing compareAndSwap short");
 210             short x = UNSAFE.getShort(base, offset);
 211             assertEquals(x, (short)0x4567, "failing compareAndSwap short value");
 212         }
 213 
 214         // Advanced compare
 215         {
 216             short r = UNSAFE.compareAndExchangeShortVolatile(base, offset, (short)0x4567, (short)0x0123);
 217             assertEquals(r, (short)0x4567, "success compareAndExchangeVolatile short");
 218             short x = UNSAFE.getShort(base, offset);
 219             assertEquals(x, (short)0x0123, "success compareAndExchangeVolatile short value");
 220         }
 221 
 222         {
 223             short r = UNSAFE.compareAndExchangeShortVolatile(base, offset, (short)0x4567, (short)0x89AB);
 224             assertEquals(r, (short)0x0123, "failing compareAndExchangeVolatile short");
 225             short x = UNSAFE.getShort(base, offset);
 226             assertEquals(x, (short)0x0123, "failing compareAndExchangeVolatile short value");
 227         }
 228 
 229         {
 230             short r = UNSAFE.compareAndExchangeShortAcquire(base, offset, (short)0x0123, (short)0x4567);
 231             assertEquals(r, (short)0x0123, "success compareAndExchangeAcquire short");
 232             short x = UNSAFE.getShort(base, offset);
 233             assertEquals(x, (short)0x4567, "success compareAndExchangeAcquire short value");
 234         }
 235 
 236         {
 237             short r = UNSAFE.compareAndExchangeShortAcquire(base, offset, (short)0x0123, (short)0x89AB);
 238             assertEquals(r, (short)0x4567, "failing compareAndExchangeAcquire short");
 239             short x = UNSAFE.getShort(base, offset);
 240             assertEquals(x, (short)0x4567, "failing compareAndExchangeAcquire short value");
 241         }
 242 
 243         {
 244             short r = UNSAFE.compareAndExchangeShortRelease(base, offset, (short)0x4567, (short)0x0123);
 245             assertEquals(r, (short)0x4567, "success compareAndExchangeRelease short");
 246             short x = UNSAFE.getShort(base, offset);
 247             assertEquals(x, (short)0x0123, "success compareAndExchangeRelease short value");
 248         }
 249 
 250         {
 251             short r = UNSAFE.compareAndExchangeShortRelease(base, offset, (short)0x4567, (short)0x89AB);
 252             assertEquals(r, (short)0x0123, "failing compareAndExchangeRelease short");
 253             short x = UNSAFE.getShort(base, offset);
 254             assertEquals(x, (short)0x0123, "failing compareAndExchangeRelease short value");
 255         }
 256 
 257         {
 258             boolean success = false;
 259             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 260                 success = UNSAFE.weakCompareAndSwapShort(base, offset, (short)0x0123, (short)0x4567);
 261             }
 262             assertEquals(success, true, "weakCompareAndSwap short");
 263             short x = UNSAFE.getShort(base, offset);
 264             assertEquals(x, (short)0x4567, "weakCompareAndSwap short value");
 265         }
 266 
 267         {
 268             boolean success = false;
 269             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 270                 success = UNSAFE.weakCompareAndSwapShortAcquire(base, offset, (short)0x4567, (short)0x0123);
 271             }
 272             assertEquals(success, true, "weakCompareAndSwapAcquire short");
 273             short x = UNSAFE.getShort(base, offset);
 274             assertEquals(x, (short)0x0123, "weakCompareAndSwapAcquire short");
 275         }
 276 
 277         {
 278             boolean success = false;
 279             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 280                 success = UNSAFE.weakCompareAndSwapShortRelease(base, offset, (short)0x0123, (short)0x4567);
 281             }
 282             assertEquals(success, true, "weakCompareAndSwapRelease short");
 283             short x = UNSAFE.getShort(base, offset);
 284             assertEquals(x, (short)0x4567, "weakCompareAndSwapRelease short");
 285         }
 286 
 287         {
 288             boolean success = false;
 289             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 290                 success = UNSAFE.weakCompareAndSwapShortVolatile(base, offset, (short)0x4567, (short)0x0123);
 291             }
 292             assertEquals(success, true, "weakCompareAndSwapVolatile short");
 293             short x = UNSAFE.getShort(base, offset);
 294             assertEquals(x, (short)0x0123, "weakCompareAndSwapVolatile short");
 295         }
 296 
 297         UNSAFE.putShort(base, offset, (short)0x4567);
 298 
 299         // Compare set and get
 300         {
 301             short o = UNSAFE.getAndSetShort(base, offset, (short)0x0123);
 302             assertEquals(o, (short)0x4567, "getAndSet short");
 303             short x = UNSAFE.getShort(base, offset);
 304             assertEquals(x, (short)0x0123, "getAndSet short value");
 305         }
 306 
 307         UNSAFE.putShort(base, offset, (short)0x0123);
 308 
 309         // get and add, add and get
 310         {
 311             short o = UNSAFE.getAndAddShort(base, offset, (short)0x4567);
 312             assertEquals(o, (short)0x0123, "getAndAdd short");
 313             short x = UNSAFE.getShort(base, offset);
 314             assertEquals(x, (short)((short)0x0123 + (short)0x4567), "getAndAdd short");
 315         }
 316     }
 317 
 318     static void testAccess(long address) {
 319         // Plain
 320         {
 321             UNSAFE.putShort(address, (short)0x0123);
 322             short x = UNSAFE.getShort(address);
 323             assertEquals(x, (short)0x0123, "set short value");
 324         }
 325     }
 326 }
 327