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 long 28 * @modules java.base/jdk.internal.misc 29 * @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestLong 30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestLong 31 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestLong 32 * @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestLong 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 JdkInternalMiscUnsafeAccessTestLong { 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 = JdkInternalMiscUnsafeAccessTestLong.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 = JdkInternalMiscUnsafeAccessTestLong.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(long[].class); 82 int ascale = UNSAFE.arrayIndexScale(long[].class); 83 ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale); 84 } 85 86 static long static_v; 87 88 long v; 89 90 @Test 91 public void testFieldInstance() { 92 JdkInternalMiscUnsafeAccessTestLong t = new JdkInternalMiscUnsafeAccessTestLong(); 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 long[] array = new long[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 @Test 116 public void testArrayOffHeap() { 117 int size = 10; 118 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT); 119 try { 120 for (int c = 0; c < ITERS; c++) { 121 for (int i = 0; i < size; i++) { 122 testAccess(null, (((long) i) << ARRAY_SHIFT) + address); 123 } 124 } 125 } finally { 126 UNSAFE.freeMemory(address); 127 } 128 } 129 130 @Test 131 public void testArrayOffHeapDirect() { 132 int size = 10; 133 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT); 134 try { 135 for (int c = 0; c < ITERS; c++) { 136 for (int i = 0; i < size; i++) { 137 testAccess((((long) i) << ARRAY_SHIFT) + address); 138 } 139 } 140 } finally { 141 UNSAFE.freeMemory(address); 142 } 143 } 144 145 static void testAccess(Object base, long offset) { 146 // Plain 147 { 148 UNSAFE.putLong(base, offset, 0x0123456789ABCDEFL); 149 long x = UNSAFE.getLong(base, offset); 150 assertEquals(x, 0x0123456789ABCDEFL, "set long value"); 151 } 152 153 // Volatile 154 { 155 UNSAFE.putLongVolatile(base, offset, 0xCAFEBABECAFEBABEL); 156 long x = UNSAFE.getLongVolatile(base, offset); 157 assertEquals(x, 0xCAFEBABECAFEBABEL, "putVolatile long value"); 158 } 159 160 161 // Lazy 162 { 163 UNSAFE.putLongRelease(base, offset, 0x0123456789ABCDEFL); 164 long x = UNSAFE.getLongAcquire(base, offset); 165 assertEquals(x, 0x0123456789ABCDEFL, "putRelease long value"); 166 } 167 168 // Opaque 169 { 170 UNSAFE.putLongOpaque(base, offset, 0xCAFEBABECAFEBABEL); 171 long x = UNSAFE.getLongOpaque(base, offset); 172 assertEquals(x, 0xCAFEBABECAFEBABEL, "putOpaque long value"); 173 } 174 175 // Unaligned 176 { 177 UNSAFE.putLongUnaligned(base, offset, 0xCAFEBABECAFEBABEL); 178 long x = UNSAFE.getLongUnaligned(base, offset); 179 assertEquals(x, 0xCAFEBABECAFEBABEL, "putUnaligned long value"); 180 } 181 182 { 183 UNSAFE.putLongUnaligned(base, offset, 0x0123456789ABCDEFL, true); 184 long x = UNSAFE.getLongUnaligned(base, offset, true); 185 assertEquals(x, 0x0123456789ABCDEFL, "putUnaligned big endian long value"); 186 } 187 188 { 189 UNSAFE.putLongUnaligned(base, offset, 0xCAFEBABECAFEBABEL, false); 190 long x = UNSAFE.getLongUnaligned(base, offset, false); 191 assertEquals(x, 0xCAFEBABECAFEBABEL, "putUnaligned little endian long value"); 192 } 193 194 UNSAFE.putLong(base, offset, 0x0123456789ABCDEFL); 195 196 // Compare 197 { 198 boolean r = UNSAFE.compareAndSwapLong(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 199 assertEquals(r, true, "success compareAndSwap long"); 200 long x = UNSAFE.getLong(base, offset); 201 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndSwap long value"); 202 } 203 204 { 205 boolean r = UNSAFE.compareAndSwapLong(base, offset, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 206 assertEquals(r, false, "failing compareAndSwap long"); 207 long x = UNSAFE.getLong(base, offset); 208 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndSwap long value"); 209 } 210 211 // Advanced compare 212 { 213 long r = UNSAFE.compareAndExchangeLongVolatile(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 214 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeVolatile long"); 215 long x = UNSAFE.getLong(base, offset); 216 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeVolatile long value"); 217 } 218 219 { 220 long r = UNSAFE.compareAndExchangeLongVolatile(base, offset, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 221 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long"); 222 long x = UNSAFE.getLong(base, offset); 223 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeVolatile long value"); 224 } 225 226 { 227 long r = UNSAFE.compareAndExchangeLongAcquire(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 228 assertEquals(r, 0x0123456789ABCDEFL, "success compareAndExchangeAcquire long"); 229 long x = UNSAFE.getLong(base, offset); 230 assertEquals(x, 0xCAFEBABECAFEBABEL, "success compareAndExchangeAcquire long value"); 231 } 232 233 { 234 long r = UNSAFE.compareAndExchangeLongAcquire(base, offset, 0x0123456789ABCDEFL, 0xDEADBEEFDEADBEEFL); 235 assertEquals(r, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long"); 236 long x = UNSAFE.getLong(base, offset); 237 assertEquals(x, 0xCAFEBABECAFEBABEL, "failing compareAndExchangeAcquire long value"); 238 } 239 240 { 241 long r = UNSAFE.compareAndExchangeLongRelease(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 242 assertEquals(r, 0xCAFEBABECAFEBABEL, "success compareAndExchangeRelease long"); 243 long x = UNSAFE.getLong(base, offset); 244 assertEquals(x, 0x0123456789ABCDEFL, "success compareAndExchangeRelease long value"); 245 } 246 247 { 248 long r = UNSAFE.compareAndExchangeLongRelease(base, offset, 0xCAFEBABECAFEBABEL, 0xDEADBEEFDEADBEEFL); 249 assertEquals(r, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long"); 250 long x = UNSAFE.getLong(base, offset); 251 assertEquals(x, 0x0123456789ABCDEFL, "failing compareAndExchangeRelease long value"); 252 } 253 254 { 255 boolean success = false; 256 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 257 success = UNSAFE.weakCompareAndSwapLong(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 258 } 259 assertEquals(success, true, "weakCompareAndSwap long"); 260 long x = UNSAFE.getLong(base, offset); 261 assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSwap long value"); 262 } 263 264 { 265 boolean success = false; 266 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 267 success = UNSAFE.weakCompareAndSwapLongAcquire(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 268 } 269 assertEquals(success, true, "weakCompareAndSwapAcquire long"); 270 long x = UNSAFE.getLong(base, offset); 271 assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSwapAcquire long"); 272 } 273 274 { 275 boolean success = false; 276 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 277 success = UNSAFE.weakCompareAndSwapLongRelease(base, offset, 0x0123456789ABCDEFL, 0xCAFEBABECAFEBABEL); 278 } 279 assertEquals(success, true, "weakCompareAndSwapRelease long"); 280 long x = UNSAFE.getLong(base, offset); 281 assertEquals(x, 0xCAFEBABECAFEBABEL, "weakCompareAndSwapRelease long"); 282 } 283 284 { 285 boolean success = false; 286 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 287 success = UNSAFE.weakCompareAndSwapLongVolatile(base, offset, 0xCAFEBABECAFEBABEL, 0x0123456789ABCDEFL); 288 } 289 assertEquals(success, true, "weakCompareAndSwapVolatile long"); 290 long x = UNSAFE.getLong(base, offset); 291 assertEquals(x, 0x0123456789ABCDEFL, "weakCompareAndSwapVolatile long"); 292 } 293 294 UNSAFE.putLong(base, offset, 0xCAFEBABECAFEBABEL); 295 296 // Compare set and get 297 { 298 long o = UNSAFE.getAndSetLong(base, offset, 0x0123456789ABCDEFL); 299 assertEquals(o, 0xCAFEBABECAFEBABEL, "getAndSet long"); 300 long x = UNSAFE.getLong(base, offset); 301 assertEquals(x, 0x0123456789ABCDEFL, "getAndSet long value"); 302 } 303 304 UNSAFE.putLong(base, offset, 0x0123456789ABCDEFL); 305 306 // get and add, add and get 307 { 308 long o = UNSAFE.getAndAddLong(base, offset, 0xCAFEBABECAFEBABEL); 309 assertEquals(o, 0x0123456789ABCDEFL, "getAndAdd long"); 310 long x = UNSAFE.getLong(base, offset); 311 assertEquals(x, (long)(0x0123456789ABCDEFL + 0xCAFEBABECAFEBABEL), "getAndAdd long"); 312 } 313 } 314 315 static void testAccess(long address) { 316 // Plain 317 { 318 UNSAFE.putLong(address, 0x0123456789ABCDEFL); 319 long x = UNSAFE.getLong(address); 320 assertEquals(x, 0x0123456789ABCDEFL, "set long value"); 321 } 322 } 323 } 324