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 boolean
  28  * @modules java.base/jdk.internal.misc
  29  * @run testng/othervm -Diters=100   -Xint                   JdkInternalMiscUnsafeAccessTestBoolean
  30  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestBoolean
  31  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  JdkInternalMiscUnsafeAccessTestBoolean
  32  * @run testng/othervm -Diters=20000                         JdkInternalMiscUnsafeAccessTestBoolean
  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 JdkInternalMiscUnsafeAccessTestBoolean {
  42     static final int ITERS = Integer.getInteger("iters", 1);
  43     static final int WEAK_ATTEMPTS = Integer.getInteger("weakAttempts", 10);
  44 
  45     static final jdk.internal.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 = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
  60             f.setAccessible(true);
  61             UNSAFE = (jdk.internal.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 = JdkInternalMiscUnsafeAccessTestBoolean.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 = JdkInternalMiscUnsafeAccessTestBoolean.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(boolean[].class);
  82         int ascale = UNSAFE.arrayIndexScale(boolean[].class);
  83         ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale);
  84     }
  85 
  86     static boolean static_v;
  87 
  88     boolean v;
  89 
  90     @Test
  91     public void testFieldInstance() {
  92         JdkInternalMiscUnsafeAccessTestBoolean t = new JdkInternalMiscUnsafeAccessTestBoolean();
  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         boolean[] array = new boolean[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.putBoolean(base, offset, true);
 120             boolean x = UNSAFE.getBoolean(base, offset);
 121             assertEquals(x, true, "set boolean value");
 122         }
 123 
 124         // Volatile
 125         {
 126             UNSAFE.putBooleanVolatile(base, offset, false);
 127             boolean x = UNSAFE.getBooleanVolatile(base, offset);
 128             assertEquals(x, false, "putVolatile boolean value");
 129         }
 130 
 131 
 132         // Lazy
 133         {
 134             UNSAFE.putBooleanRelease(base, offset, true);
 135             boolean x = UNSAFE.getBooleanAcquire(base, offset);
 136             assertEquals(x, true, "putRelease boolean value");
 137         }
 138 
 139         // Opaque
 140         {
 141             UNSAFE.putBooleanOpaque(base, offset, false);
 142             boolean x = UNSAFE.getBooleanOpaque(base, offset);
 143             assertEquals(x, false, "putOpaque boolean value");
 144         }
 145 
 146 
 147 
 148     }
 149 
 150 }
 151