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/othervm -Xint UnsafeOffHeapBooleanTest 1
  29  * @run main/othervm -XX:+TieredCompilation -XX:TieredStopAtLevel=3 -Xbatch  UnsafeOffHeapBooleanTest 20000
  30  * @run main/othervm -XX:-TieredCompilation -Xbatch UnsafeOffHeapBooleanTest 20000
  31  */
  32 
  33 
  34 import java.lang.reflect.Field;
  35 import jdk.internal.misc.Unsafe;
  36 
  37 public class UnsafeOffHeapBooleanTest {
  38     static boolean bool0 = false, bool1 = false, result = false;
  39     static Unsafe UNSAFE = Unsafe.getUnsafe();
  40     static long offHeapMemory;
  41 
  42     public static void test() {
  43         // Write two bytes to the off-heap memory location, both
  44         // bytes correspond to the boolean value 'true'.
  45         UNSAFE.putShort(null, offHeapMemory, (short)0x0204);
  46 
  47         // Read two bytes from the storage allocated above (as booleans).
  48         bool0 = UNSAFE.getBoolean(null, offHeapMemory + 0);
  49         bool1 = UNSAFE.getBoolean(null, offHeapMemory + 1);
  50         result = bool0 & bool1;
  51     }
  52 
  53     public static void main(String args[]) {
  54         System.out.println("### Test started");
  55 
  56         if (args.length != 1) {
  57             throw new RuntimeException("### Test failure: test called with incorrect number of arguments");
  58         }
  59 
  60         // Allocate two bytes of storage.
  61         offHeapMemory = UNSAFE.allocateMemory(2);
  62 
  63         try {
  64             for (int i = 0; i < Integer.parseInt(args[0]); i++) {
  65                 test();
  66             }
  67 
  68             // Check if the two 'true' boolean values were normalized
  69             // (i.e., reduced from the range 1...255 to 1).
  70             if (!bool0 || !bool1 || !result) {
  71                 System.out.println("Some of the results below are wrong");
  72                 System.out.println("bool0 is: " + bool0);
  73                 System.out.println("bool1 is: " + bool1);
  74                 System.out.println("bool0 & bool1 is: " + result);
  75                 System.out.println("===================================");
  76                 throw new RuntimeException("### Test failed");
  77             } else {
  78                 System.out.println("Test generated correct results");
  79                 System.out.println("bool0 is: " + bool0);
  80                 System.out.println("bool1 is: " + bool1);
  81                 System.out.println("bool0 & bool1 is: " + result);
  82                 System.out.println("===================================");
  83             }
  84         } catch (NumberFormatException e) {
  85             throw new RuntimeException("### Test failure: test called with incorrectly formatted parameter");
  86         }
  87 
  88         UNSAFE.freeMemory(offHeapMemory);
  89 
  90         System.out.println("### Test passed");
  91     }
  92 }