1 /*
   2  * Copyright (c) 2016, 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 8161720
  27  * @modules java.base/jdk.internal.misc
  28  * @run main/bootclasspath/othervm -Xint UnsafeOnHeapBooleanTest
  29  * @run main/bootclasspath/othervm -Xcomp -XX:+TieredCompilation -XX:TieredStopAtLevel=3 UnsafeOnHeapBooleanTest
  30  * @run main/bootclasspath/othervm -Xcomp -XX:-TieredCompilation UnsafeOnHeapBooleanTest
  31  */
  32 
  33 import jdk.internal.misc.Unsafe;
  34 
  35 import java.lang.reflect.Field;
  36 
  37 public class UnsafeOnHeapBooleanTest {
  38 
  39     static short static_v;
  40 
  41     public static void main(String args[]) {
  42 
  43         System.out.println("### Test started");
  44 
  45         Unsafe UNSAFE = Unsafe.getUnsafe();
  46 
  47         try {
  48 
  49             // Write two bytes into the static field
  50             // UnsafeOnHeapBooleanTest.static_v write two values. Both
  51             // bytes correspond to the boolean value 'true'.
  52             Field staticVField = UnsafeOnHeapBooleanTest.class.getDeclaredField("static_v");
  53             Object base = UNSAFE.staticFieldBase(staticVField);
  54             long offset = UNSAFE.staticFieldOffset(staticVField);
  55             UNSAFE.putShort(base, offset, (short)0x0204);
  56 
  57             // Read two bytes from the static field
  58             // UnsafeOnHeapBooleanTest.static_v (as booleans).
  59             boolean bool0 = UNSAFE.getBoolean(base, offset + 0);
  60             boolean bool1 = UNSAFE.getBoolean(base, offset + 1);
  61             boolean result = bool0 & bool1;
  62 
  63             // Check if the two 'true' boolean values were normalized
  64             // (i.e., reduced from the range 1...255 to 1).
  65             if (!bool0 || !bool1 || !result) {
  66                 System.out.println("Some of the results below are wrong");
  67                 System.out.println("bool0 is: " + bool0);
  68                 System.out.println("bool1 is: " + bool1);
  69                 System.out.println("bool0 & bool1 is: " + result);
  70                 System.out.println("===================================");
  71                 throw new RuntimeException("### Test failed");
  72             } else {
  73                 System.out.println("Test generated correct results");
  74                 System.out.println("bool0 is: " + bool0);
  75                 System.out.println("bool1 is: " + bool1);
  76                 System.out.println("bool0 & bool1 is: " + result);
  77                 System.out.println("===================================");
  78             }
  79         } catch (NoSuchFieldException e) {
  80             throw new RuntimeException("### Test failure: static field UnsafeOnHeapBooleanTest.static_v was not found");
  81         }
  82 
  83         System.out.println("### Test passed");
  84 
  85     }
  86 }