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 int 28 * @modules java.base/jdk.internal.misc 29 * @run testng/othervm -Diters=100 -Xint JdkInternalMiscUnsafeAccessTestInt 30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 JdkInternalMiscUnsafeAccessTestInt 31 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation JdkInternalMiscUnsafeAccessTestInt 32 * @run testng/othervm -Diters=20000 JdkInternalMiscUnsafeAccessTestInt 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 JdkInternalMiscUnsafeAccessTestInt { 42 static final int ITERS = Integer.getInteger("iters", 1); 43 44 static final jdk.internal.misc.Unsafe UNSAFE; 45 46 static final long V_OFFSET; 47 48 static final Object STATIC_V_BASE; 49 50 static final long STATIC_V_OFFSET; 51 52 static int ARRAY_OFFSET; 53 54 static int ARRAY_SHIFT; 55 56 static { 57 try { 58 Field f = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe"); 59 f.setAccessible(true); 60 UNSAFE = (jdk.internal.misc.Unsafe) f.get(null); 61 } catch (Exception e) { 62 throw new RuntimeException("Unable to get Unsafe instance.", e); 63 } 64 65 try { 66 Field staticVField = JdkInternalMiscUnsafeAccessTestInt.class.getDeclaredField("static_v"); 67 STATIC_V_BASE = UNSAFE.staticFieldBase(staticVField); 68 STATIC_V_OFFSET = UNSAFE.staticFieldOffset(staticVField); 69 } catch (Exception e) { 70 throw new RuntimeException(e); 71 } 72 73 try { 74 Field vField = JdkInternalMiscUnsafeAccessTestInt.class.getDeclaredField("v"); 75 V_OFFSET = UNSAFE.objectFieldOffset(vField); 76 } catch (Exception e) { 77 throw new RuntimeException(e); 78 } 79 80 ARRAY_OFFSET = UNSAFE.arrayBaseOffset(int[].class); 81 int ascale = UNSAFE.arrayIndexScale(int[].class); 82 ARRAY_SHIFT = 31 - Integer.numberOfLeadingZeros(ascale); 83 } 84 85 static int static_v; 86 87 int v; 88 89 @Test 90 public void testFieldInstance() { 91 JdkInternalMiscUnsafeAccessTestInt t = new JdkInternalMiscUnsafeAccessTestInt(); 92 for (int c = 0; c < ITERS; c++) { 93 testAccess(t, V_OFFSET); 94 } 95 } 96 97 @Test 98 public void testFieldStatic() { 99 for (int c = 0; c < ITERS; c++) { 100 testAccess(STATIC_V_BASE, STATIC_V_OFFSET); 101 } 102 } 103 104 @Test 105 public void testArray() { 106 int[] array = new int[10]; 107 for (int c = 0; c < ITERS; c++) { 108 for (int i = 0; i < array.length; i++) { 109 testAccess(array, (((long) i) << ARRAY_SHIFT) + ARRAY_OFFSET); 110 } 111 } 112 } 113 114 @Test 115 public void testArrayOffHeap() { 116 int size = 10; 117 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT); 118 try { 119 for (int c = 0; c < ITERS; c++) { 120 for (int i = 0; i < size; i++) { 121 testAccess(null, (((long) i) << ARRAY_SHIFT) + address); 122 } 123 } 124 } finally { 125 UNSAFE.freeMemory(address); 126 } 127 } 128 129 @Test 130 public void testArrayOffHeapDirect() { 131 int size = 10; 132 long address = UNSAFE.allocateMemory(size << ARRAY_SHIFT); 133 try { 134 for (int c = 0; c < ITERS; c++) { 135 for (int i = 0; i < size; i++) { 136 testAccess((((long) i) << ARRAY_SHIFT) + address); 137 } 138 } 139 } finally { 140 UNSAFE.freeMemory(address); 141 } 142 } 143 144 static void testAccess(Object base, long offset) { 145 // Plain 146 { 147 UNSAFE.putInt(base, offset, 1); 148 int x = UNSAFE.getInt(base, offset); 149 assertEquals(x, 1, "set int value"); 150 } 151 152 // Volatile 153 { 154 UNSAFE.putIntVolatile(base, offset, 2); 155 int x = UNSAFE.getIntVolatile(base, offset); 156 assertEquals(x, 2, "putVolatile int value"); 157 } 158 159 160 // Lazy 161 { 162 UNSAFE.putIntRelease(base, offset, 1); 163 int x = UNSAFE.getIntAcquire(base, offset); 164 assertEquals(x, 1, "putRelease int value"); 165 } 166 167 // Opaque 168 { 169 UNSAFE.putIntOpaque(base, offset, 2); 170 int x = UNSAFE.getIntOpaque(base, offset); 171 assertEquals(x, 2, "putOpaque int value"); 172 } 173 174 // Unaligned 175 { 176 UNSAFE.putIntUnaligned(base, offset, 2); 177 int x = UNSAFE.getIntUnaligned(base, offset); 178 assertEquals(x, 2, "putUnaligned int value"); 179 } 180 181 { 182 UNSAFE.putIntUnaligned(base, offset, 1, true); 183 int x = UNSAFE.getIntUnaligned(base, offset, true); 184 assertEquals(x, 1, "putUnaligned big endian int value"); 185 } 186 187 { 188 UNSAFE.putIntUnaligned(base, offset, 2, false); 189 int x = UNSAFE.getIntUnaligned(base, offset, false); 190 assertEquals(x, 2, "putUnaligned little endian int value"); 191 } 192 193 UNSAFE.putInt(base, offset, 1); 194 195 // Compare 196 { 197 boolean r = UNSAFE.compareAndSwapInt(base, offset, 1, 2); 198 assertEquals(r, true, "success compareAndSwap int"); 199 int x = UNSAFE.getInt(base, offset); 200 assertEquals(x, 2, "success compareAndSwap int value"); 201 } 202 203 { 204 boolean r = UNSAFE.compareAndSwapInt(base, offset, 1, 3); 205 assertEquals(r, false, "failing compareAndSwap int"); 206 int x = UNSAFE.getInt(base, offset); 207 assertEquals(x, 2, "failing compareAndSwap int value"); 208 } 209 210 // Advanced compare 211 { 212 int r = UNSAFE.compareAndExchangeIntVolatile(base, offset, 2, 1); 213 assertEquals(r, 2, "success compareAndExchangeVolatile int"); 214 int x = UNSAFE.getInt(base, offset); 215 assertEquals(x, 1, "success compareAndExchangeVolatile int value"); 216 } 217 218 { 219 int r = UNSAFE.compareAndExchangeIntVolatile(base, offset, 2, 3); 220 assertEquals(r, 1, "failing compareAndExchangeVolatile int"); 221 int x = UNSAFE.getInt(base, offset); 222 assertEquals(x, 1, "failing compareAndExchangeVolatile int value"); 223 } 224 225 { 226 int r = UNSAFE.compareAndExchangeIntAcquire(base, offset, 1, 2); 227 assertEquals(r, 1, "success compareAndExchangeAcquire int"); 228 int x = UNSAFE.getInt(base, offset); 229 assertEquals(x, 2, "success compareAndExchangeAcquire int value"); 230 } 231 232 { 233 int r = UNSAFE.compareAndExchangeIntAcquire(base, offset, 1, 3); 234 assertEquals(r, 2, "failing compareAndExchangeAcquire int"); 235 int x = UNSAFE.getInt(base, offset); 236 assertEquals(x, 2, "failing compareAndExchangeAcquire int value"); 237 } 238 239 { 240 int r = UNSAFE.compareAndExchangeIntRelease(base, offset, 2, 1); 241 assertEquals(r, 2, "success compareAndExchangeRelease int"); 242 int x = UNSAFE.getInt(base, offset); 243 assertEquals(x, 1, "success compareAndExchangeRelease int value"); 244 } 245 246 { 247 int r = UNSAFE.compareAndExchangeIntRelease(base, offset, 2, 3); 248 assertEquals(r, 1, "failing compareAndExchangeRelease int"); 249 int x = UNSAFE.getInt(base, offset); 250 assertEquals(x, 1, "failing compareAndExchangeRelease int value"); 251 } 252 253 { 254 boolean r = UNSAFE.weakCompareAndSwapInt(base, offset, 1, 2); 255 assertEquals(r, true, "weakCompareAndSwap int"); 256 int x = UNSAFE.getInt(base, offset); 257 assertEquals(x, 2, "weakCompareAndSwap int value"); 258 } 259 260 { 261 boolean r = UNSAFE.weakCompareAndSwapIntAcquire(base, offset, 2, 1); 262 assertEquals(r, true, "weakCompareAndSwapAcquire int"); 263 int x = UNSAFE.getInt(base, offset); 264 assertEquals(x, 1, "weakCompareAndSwapAcquire int"); 265 } 266 267 { 268 boolean r = UNSAFE.weakCompareAndSwapIntRelease(base, offset, 1, 2); 269 assertEquals(r, true, "weakCompareAndSwapRelease int"); 270 int x = UNSAFE.getInt(base, offset); 271 assertEquals(x, 2, "weakCompareAndSwapRelease int"); 272 } 273 274 // Compare set and get 275 { 276 int o = UNSAFE.getAndSetInt(base, offset, 1); 277 assertEquals(o, 2, "getAndSet int"); 278 int x = UNSAFE.getInt(base, offset); 279 assertEquals(x, 1, "getAndSet int value"); 280 } 281 282 UNSAFE.putInt(base, offset, 1); 283 284 // get and add, add and get 285 { 286 int o = UNSAFE.getAndAddInt(base, offset, 2); 287 assertEquals(o, 1, "getAndAdd int"); 288 int x = UNSAFE.getInt(base, offset); 289 assertEquals(x, 1 + 2, "weakCompareAndSwapRelease int"); 290 } 291 } 292 293 static void testAccess(long address) { 294 // Plain 295 { 296 UNSAFE.putInt(address, 1); 297 int x = UNSAFE.getInt(address); 298 assertEquals(x, 1, "set int value"); 299 } 300 } 301 } 302 303