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 UNSAFE.putBoolean(base, offset, true); 148 149 // Compare 150 { 151 boolean r = UNSAFE.compareAndSwapBoolean(base, offset, true, false); 152 assertEquals(r, true, "success compareAndSwap boolean"); 153 boolean x = UNSAFE.getBoolean(base, offset); 154 assertEquals(x, false, "success compareAndSwap boolean value"); 155 } 156 157 { 158 boolean r = UNSAFE.compareAndSwapBoolean(base, offset, true, false); 159 assertEquals(r, false, "failing compareAndSwap boolean"); 160 boolean x = UNSAFE.getBoolean(base, offset); 161 assertEquals(x, false, "failing compareAndSwap boolean value"); 162 } 163 164 // Advanced compare 165 { 166 boolean r = UNSAFE.compareAndExchangeBooleanVolatile(base, offset, false, true); 167 assertEquals(r, false, "success compareAndExchangeVolatile boolean"); 168 boolean x = UNSAFE.getBoolean(base, offset); 169 assertEquals(x, true, "success compareAndExchangeVolatile boolean value"); 170 } 171 172 { 173 boolean r = UNSAFE.compareAndExchangeBooleanVolatile(base, offset, false, false); 174 assertEquals(r, true, "failing compareAndExchangeVolatile boolean"); 175 boolean x = UNSAFE.getBoolean(base, offset); 176 assertEquals(x, true, "failing compareAndExchangeVolatile boolean value"); 177 } 178 179 { 180 boolean r = UNSAFE.compareAndExchangeBooleanAcquire(base, offset, true, false); 181 assertEquals(r, true, "success compareAndExchangeAcquire boolean"); 182 boolean x = UNSAFE.getBoolean(base, offset); 183 assertEquals(x, false, "success compareAndExchangeAcquire boolean value"); 184 } 185 186 { 187 boolean r = UNSAFE.compareAndExchangeBooleanAcquire(base, offset, true, false); 188 assertEquals(r, false, "failing compareAndExchangeAcquire boolean"); 189 boolean x = UNSAFE.getBoolean(base, offset); 190 assertEquals(x, false, "failing compareAndExchangeAcquire boolean value"); 191 } 192 193 { 194 boolean r = UNSAFE.compareAndExchangeBooleanRelease(base, offset, false, true); 195 assertEquals(r, false, "success compareAndExchangeRelease boolean"); 196 boolean x = UNSAFE.getBoolean(base, offset); 197 assertEquals(x, true, "success compareAndExchangeRelease boolean value"); 198 } 199 200 { 201 boolean r = UNSAFE.compareAndExchangeBooleanRelease(base, offset, false, false); 202 assertEquals(r, true, "failing compareAndExchangeRelease boolean"); 203 boolean x = UNSAFE.getBoolean(base, offset); 204 assertEquals(x, true, "failing compareAndExchangeRelease boolean value"); 205 } 206 207 { 208 boolean success = false; 209 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 210 success = UNSAFE.weakCompareAndSwapBoolean(base, offset, true, false); 211 } 212 assertEquals(success, true, "weakCompareAndSwap boolean"); 213 boolean x = UNSAFE.getBoolean(base, offset); 214 assertEquals(x, false, "weakCompareAndSwap boolean value"); 215 } 216 217 { 218 boolean success = false; 219 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 220 success = UNSAFE.weakCompareAndSwapBooleanAcquire(base, offset, false, true); 221 } 222 assertEquals(success, true, "weakCompareAndSwapAcquire boolean"); 223 boolean x = UNSAFE.getBoolean(base, offset); 224 assertEquals(x, true, "weakCompareAndSwapAcquire boolean"); 225 } 226 227 { 228 boolean success = false; 229 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 230 success = UNSAFE.weakCompareAndSwapBooleanRelease(base, offset, true, false); 231 } 232 assertEquals(success, true, "weakCompareAndSwapRelease boolean"); 233 boolean x = UNSAFE.getBoolean(base, offset); 234 assertEquals(x, false, "weakCompareAndSwapRelease boolean"); 235 } 236 237 { 238 boolean success = false; 239 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 240 success = UNSAFE.weakCompareAndSwapBooleanVolatile(base, offset, false, true); 241 } 242 assertEquals(success, true, "weakCompareAndSwapVolatile boolean"); 243 boolean x = UNSAFE.getBoolean(base, offset); 244 assertEquals(x, true, "weakCompareAndSwapVolatile boolean"); 245 } 246 247 UNSAFE.putBoolean(base, offset, false); 248 249 // Compare set and get 250 { 251 boolean o = UNSAFE.getAndSetBoolean(base, offset, true); 252 assertEquals(o, false, "getAndSet boolean"); 253 boolean x = UNSAFE.getBoolean(base, offset); 254 assertEquals(x, true, "getAndSet boolean value"); 255 } 256 257 } 258 259 } 260