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