1 /* 2 * Copyright (c) 2015, 2018, 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 // Android-added: package for test. 25 package test.java.lang.invoke.VarHandles; 26 27 /* 28 * @test 29 * @bug 8154556 30 * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestByteArrayAsLong 31 * @run testng/othervm -Diters=20000 VarHandleTestByteArrayAsLong 32 * @run testng/othervm -Diters=20000 -XX:-TieredCompilation VarHandleTestByteArrayAsLong 33 */ 34 35 import org.testng.annotations.DataProvider; 36 import org.testng.annotations.Test; 37 38 import java.lang.invoke.MethodHandles; 39 import java.lang.invoke.VarHandle; 40 import java.nio.ByteBuffer; 41 import java.nio.ByteOrder; 42 import java.util.ArrayList; 43 import java.util.Arrays; 44 import java.util.EnumSet; 45 import java.util.List; 46 47 import static org.testng.Assert.*; 48 49 public class VarHandleTestByteArrayAsLong extends VarHandleBaseByteArrayTest { 50 static final int SIZE = Long.BYTES; 51 52 static final long VALUE_1 = 0x0102030405060708L; 53 54 static final long VALUE_2 = 0x1112131415161718L; 55 56 static final long VALUE_3 = 0xFFFEFDFCFBFAF9F8L; 57 58 59 @Override setupVarHandleSources()60 public void setupVarHandleSources() { 61 // Combinations of VarHandle byte[] or ByteBuffer 62 vhss = new ArrayList<>(); 63 for (MemoryMode endianess : Arrays.asList(MemoryMode.BIG_ENDIAN, MemoryMode.LITTLE_ENDIAN)) { 64 65 ByteOrder bo = endianess == MemoryMode.BIG_ENDIAN 66 ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; 67 VarHandleSource aeh = new VarHandleSource( 68 MethodHandles.byteArrayViewVarHandle(long[].class, bo), 69 endianess, MemoryMode.READ_WRITE); 70 vhss.add(aeh); 71 72 VarHandleSource bbh = new VarHandleSource( 73 MethodHandles.byteBufferViewVarHandle(long[].class, bo), 74 endianess, MemoryMode.READ_WRITE); 75 vhss.add(bbh); 76 } 77 } 78 79 80 @Test(dataProvider = "varHandlesProvider") testIsAccessModeSupported(VarHandleSource vhs)81 public void testIsAccessModeSupported(VarHandleSource vhs) { 82 VarHandle vh = vhs.s; 83 84 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET)); 85 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET)); 86 87 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE)); 88 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE)); 89 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE)); 90 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE)); 91 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE)); 92 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE)); 93 94 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET)); 95 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE)); 96 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)); 97 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)); 98 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN)); 99 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)); 100 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE)); 101 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE)); 102 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET)); 103 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE)); 104 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE)); 105 106 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD)); 107 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE)); 108 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE)); 109 110 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR)); 111 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE)); 112 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE)); 113 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND)); 114 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE)); 115 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE)); 116 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR)); 117 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE)); 118 assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE)); 119 } 120 121 @Test(dataProvider = "typesProvider") testTypes(VarHandle vh, List<java.lang.Class<?>> pts)122 public void testTypes(VarHandle vh, List<java.lang.Class<?>> pts) { 123 assertEquals(vh.varType(), long.class); 124 125 assertEquals(vh.coordinateTypes(), pts); 126 127 testTypes(vh); 128 } 129 130 131 @DataProvider accessTestCaseProvider()132 public Object[][] accessTestCaseProvider() throws Exception { 133 List<AccessTestCase<?>> cases = new ArrayList<>(); 134 135 for (ByteArrayViewSource<?> bav : bavss) { 136 for (VarHandleSource vh : vhss) { 137 if (vh.matches(bav)) { 138 if (bav instanceof ByteArraySource) { 139 ByteArraySource bas = (ByteArraySource) bav; 140 141 cases.add(new VarHandleSourceAccessTestCase( 142 "read write", bav, vh, h -> testArrayReadWrite(bas, h), 143 true)); 144 cases.add(new VarHandleSourceAccessTestCase( 145 "null array", bav, vh, h -> testArrayNPE(bas, h), 146 false)); 147 cases.add(new VarHandleSourceAccessTestCase( 148 "unsupported", bav, vh, h -> testArrayUnsupported(bas, h), 149 false)); 150 cases.add(new VarHandleSourceAccessTestCase( 151 "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bas, h), 152 false)); 153 cases.add(new VarHandleSourceAccessTestCase( 154 "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bas, h), 155 false)); 156 } 157 else { 158 ByteBufferSource bbs = (ByteBufferSource) bav; 159 160 if (MemoryMode.READ_WRITE.isSet(bav.memoryModes)) { 161 cases.add(new VarHandleSourceAccessTestCase( 162 "read write", bav, vh, h -> testArrayReadWrite(bbs, h), 163 true)); 164 } 165 else { 166 cases.add(new VarHandleSourceAccessTestCase( 167 "read only", bav, vh, h -> testArrayReadOnly(bbs, h), 168 true)); 169 } 170 171 cases.add(new VarHandleSourceAccessTestCase( 172 "null buffer", bav, vh, h -> testArrayNPE(bbs, h), 173 false)); 174 cases.add(new VarHandleSourceAccessTestCase( 175 "unsupported", bav, vh, h -> testArrayUnsupported(bbs, h), 176 false)); 177 cases.add(new VarHandleSourceAccessTestCase( 178 "index out of bounds", bav, vh, h -> testArrayIndexOutOfBounds(bbs, h), 179 false)); 180 cases.add(new VarHandleSourceAccessTestCase( 181 "misaligned access", bav, vh, h -> testArrayMisalignedAccess(bbs, h), 182 false)); 183 } 184 } 185 } 186 } 187 188 // Work around issue with jtreg summary reporting which truncates 189 // the String result of Object.toString to 30 characters, hence 190 // the first dummy argument 191 return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new); 192 } 193 194 @Test(dataProvider = "accessTestCaseProvider") testAccess(String desc, AccessTestCase<T> atc)195 public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable { 196 T t = atc.get(); 197 int iters = atc.requiresLoop() ? ITERS : 1; 198 for (int c = 0; c < iters; c++) { 199 atc.testAccess(t); 200 } 201 } 202 203 testArrayNPE(ByteArraySource bs, VarHandleSource vhs)204 static void testArrayNPE(ByteArraySource bs, VarHandleSource vhs) { 205 VarHandle vh = vhs.s; 206 byte[] array = null; 207 int ci = 1; 208 209 checkNPE(() -> { 210 long x = (long) vh.get(array, ci); 211 }); 212 213 checkNPE(() -> { 214 vh.set(array, ci, VALUE_1); 215 }); 216 217 checkNPE(() -> { 218 long x = (long) vh.getVolatile(array, ci); 219 }); 220 221 checkNPE(() -> { 222 long x = (long) vh.getAcquire(array, ci); 223 }); 224 225 checkNPE(() -> { 226 long x = (long) vh.getOpaque(array, ci); 227 }); 228 229 checkNPE(() -> { 230 vh.setVolatile(array, ci, VALUE_1); 231 }); 232 233 checkNPE(() -> { 234 vh.setRelease(array, ci, VALUE_1); 235 }); 236 237 checkNPE(() -> { 238 vh.setOpaque(array, ci, VALUE_1); 239 }); 240 241 checkNPE(() -> { 242 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 243 }); 244 245 checkNPE(() -> { 246 long r = (long) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 247 }); 248 249 checkNPE(() -> { 250 long r = (long) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 251 }); 252 253 checkNPE(() -> { 254 long r = (long) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 255 }); 256 257 checkNPE(() -> { 258 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 259 }); 260 261 checkNPE(() -> { 262 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 263 }); 264 265 checkNPE(() -> { 266 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 267 }); 268 269 checkNPE(() -> { 270 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 271 }); 272 273 checkNPE(() -> { 274 long o = (long) vh.getAndSet(array, ci, VALUE_1); 275 }); 276 277 checkNPE(() -> { 278 long o = (long) vh.getAndSetAcquire(array, ci, VALUE_1); 279 }); 280 281 checkNPE(() -> { 282 long o = (long) vh.getAndSetRelease(array, ci, VALUE_1); 283 }); 284 285 checkNPE(() -> { 286 long o = (long) vh.getAndAdd(array, ci, VALUE_1); 287 }); 288 289 checkNPE(() -> { 290 long o = (long) vh.getAndAddAcquire(array, ci, VALUE_1); 291 }); 292 293 checkNPE(() -> { 294 long o = (long) vh.getAndAddRelease(array, ci, VALUE_1); 295 }); 296 297 checkNPE(() -> { 298 long o = (long) vh.getAndBitwiseOr(array, ci, VALUE_1); 299 }); 300 301 checkNPE(() -> { 302 long o = (long) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 303 }); 304 305 checkNPE(() -> { 306 long o = (long) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 307 }); 308 309 checkNPE(() -> { 310 long o = (long) vh.getAndBitwiseAnd(array, ci, VALUE_1); 311 }); 312 313 checkNPE(() -> { 314 long o = (long) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 315 }); 316 317 checkNPE(() -> { 318 long o = (long) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 319 }); 320 321 checkNPE(() -> { 322 long o = (long) vh.getAndBitwiseXor(array, ci, VALUE_1); 323 }); 324 325 checkNPE(() -> { 326 long o = (long) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 327 }); 328 329 checkNPE(() -> { 330 long o = (long) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 331 }); 332 } 333 testArrayNPE(ByteBufferSource bs, VarHandleSource vhs)334 static void testArrayNPE(ByteBufferSource bs, VarHandleSource vhs) { 335 VarHandle vh = vhs.s; 336 ByteBuffer array = null; 337 int ci = 1; 338 339 checkNPE(() -> { 340 long x = (long) vh.get(array, ci); 341 }); 342 343 checkNPE(() -> { 344 vh.set(array, ci, VALUE_1); 345 }); 346 347 checkNPE(() -> { 348 long x = (long) vh.getVolatile(array, ci); 349 }); 350 351 checkNPE(() -> { 352 long x = (long) vh.getAcquire(array, ci); 353 }); 354 355 checkNPE(() -> { 356 long x = (long) vh.getOpaque(array, ci); 357 }); 358 359 checkNPE(() -> { 360 vh.setVolatile(array, ci, VALUE_1); 361 }); 362 363 checkNPE(() -> { 364 vh.setRelease(array, ci, VALUE_1); 365 }); 366 367 checkNPE(() -> { 368 vh.setOpaque(array, ci, VALUE_1); 369 }); 370 371 checkNPE(() -> { 372 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 373 }); 374 375 checkNPE(() -> { 376 long r = (long) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 377 }); 378 379 checkNPE(() -> { 380 long r = (long) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 381 }); 382 383 checkNPE(() -> { 384 long r = (long) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 385 }); 386 387 checkNPE(() -> { 388 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 389 }); 390 391 checkNPE(() -> { 392 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 393 }); 394 395 checkNPE(() -> { 396 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 397 }); 398 399 checkNPE(() -> { 400 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 401 }); 402 403 checkNPE(() -> { 404 long o = (long) vh.getAndSet(array, ci, VALUE_1); 405 }); 406 407 checkNPE(() -> { 408 long o = (long) vh.getAndSetAcquire(array, ci, VALUE_1); 409 }); 410 411 checkNPE(() -> { 412 long o = (long) vh.getAndSetRelease(array, ci, VALUE_1); 413 }); 414 415 checkNPE(() -> { 416 long o = (long) vh.getAndAdd(array, ci, VALUE_1); 417 }); 418 419 checkNPE(() -> { 420 long o = (long) vh.getAndAddAcquire(array, ci, VALUE_1); 421 }); 422 423 checkNPE(() -> { 424 long o = (long) vh.getAndAddRelease(array, ci, VALUE_1); 425 }); 426 427 checkNPE(() -> { 428 long o = (long) vh.getAndBitwiseOr(array, ci, VALUE_1); 429 }); 430 431 checkNPE(() -> { 432 long o = (long) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 433 }); 434 435 checkNPE(() -> { 436 long o = (long) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 437 }); 438 439 checkNPE(() -> { 440 long o = (long) vh.getAndBitwiseAnd(array, ci, VALUE_1); 441 }); 442 443 checkNPE(() -> { 444 long o = (long) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 445 }); 446 447 checkNPE(() -> { 448 long o = (long) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 449 }); 450 451 checkNPE(() -> { 452 long o = (long) vh.getAndBitwiseXor(array, ci, VALUE_1); 453 }); 454 455 checkNPE(() -> { 456 long o = (long) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 457 }); 458 459 checkNPE(() -> { 460 long o = (long) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 461 }); 462 } 463 testArrayUnsupported(ByteArraySource bs, VarHandleSource vhs)464 static void testArrayUnsupported(ByteArraySource bs, VarHandleSource vhs) { 465 VarHandle vh = vhs.s; 466 byte[] array = bs.s; 467 int ci = 1; 468 469 470 471 } 472 testArrayUnsupported(ByteBufferSource bs, VarHandleSource vhs)473 static void testArrayUnsupported(ByteBufferSource bs, VarHandleSource vhs) { 474 VarHandle vh = vhs.s; 475 ByteBuffer array = bs.s; 476 int ci = 0; 477 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); 478 479 if (readOnly) { 480 checkROBE(() -> { 481 vh.set(array, ci, VALUE_1); 482 }); 483 } 484 485 if (readOnly) { 486 checkROBE(() -> { 487 vh.setVolatile(array, ci, VALUE_1); 488 }); 489 490 checkROBE(() -> { 491 vh.setRelease(array, ci, VALUE_1); 492 }); 493 494 checkROBE(() -> { 495 vh.setOpaque(array, ci, VALUE_1); 496 }); 497 498 checkROBE(() -> { 499 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 500 }); 501 502 checkROBE(() -> { 503 long r = (long) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 504 }); 505 506 checkROBE(() -> { 507 long r = (long) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 508 }); 509 510 checkROBE(() -> { 511 long r = (long) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 512 }); 513 514 checkROBE(() -> { 515 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 516 }); 517 518 checkROBE(() -> { 519 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 520 }); 521 522 checkROBE(() -> { 523 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 524 }); 525 526 checkROBE(() -> { 527 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 528 }); 529 530 checkROBE(() -> { 531 long o = (long) vh.getAndSet(array, ci, VALUE_1); 532 }); 533 534 checkROBE(() -> { 535 long o = (long) vh.getAndSetAcquire(array, ci, VALUE_1); 536 }); 537 538 checkROBE(() -> { 539 long o = (long) vh.getAndSetRelease(array, ci, VALUE_1); 540 }); 541 542 543 checkROBE(() -> { 544 long o = (long) vh.getAndAdd(array, ci, VALUE_1); 545 }); 546 547 checkROBE(() -> { 548 long o = (long) vh.getAndAddAcquire(array, ci, VALUE_1); 549 }); 550 551 checkROBE(() -> { 552 long o = (long) vh.getAndAddRelease(array, ci, VALUE_1); 553 }); 554 555 checkROBE(() -> { 556 long o = (long) vh.getAndBitwiseOr(array, ci, VALUE_1); 557 }); 558 559 checkROBE(() -> { 560 long o = (long) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 561 }); 562 563 checkROBE(() -> { 564 long o = (long) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 565 }); 566 567 checkROBE(() -> { 568 long o = (long) vh.getAndBitwiseAnd(array, ci, VALUE_1); 569 }); 570 571 checkROBE(() -> { 572 long o = (long) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 573 }); 574 575 checkROBE(() -> { 576 long o = (long) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 577 }); 578 579 checkROBE(() -> { 580 long o = (long) vh.getAndBitwiseXor(array, ci, VALUE_1); 581 }); 582 583 checkROBE(() -> { 584 long o = (long) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 585 }); 586 587 checkROBE(() -> { 588 long o = (long) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 589 }); 590 } 591 else { 592 } 593 } 594 595 testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs)596 static void testArrayIndexOutOfBounds(ByteArraySource bs, VarHandleSource vhs) throws Throwable { 597 VarHandle vh = vhs.s; 598 byte[] array = bs.s; 599 600 int length = array.length - SIZE + 1; 601 for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { 602 final int ci = i; 603 604 checkIOOBE(() -> { 605 long x = (long) vh.get(array, ci); 606 }); 607 608 checkIOOBE(() -> { 609 vh.set(array, ci, VALUE_1); 610 }); 611 612 checkIOOBE(() -> { 613 long x = (long) vh.getVolatile(array, ci); 614 }); 615 616 checkIOOBE(() -> { 617 long x = (long) vh.getAcquire(array, ci); 618 }); 619 620 checkIOOBE(() -> { 621 long x = (long) vh.getOpaque(array, ci); 622 }); 623 624 checkIOOBE(() -> { 625 vh.setVolatile(array, ci, VALUE_1); 626 }); 627 628 checkIOOBE(() -> { 629 vh.setRelease(array, ci, VALUE_1); 630 }); 631 632 checkIOOBE(() -> { 633 vh.setOpaque(array, ci, VALUE_1); 634 }); 635 636 checkIOOBE(() -> { 637 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 638 }); 639 640 checkIOOBE(() -> { 641 long r = (long) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 642 }); 643 644 checkIOOBE(() -> { 645 long r = (long) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 646 }); 647 648 checkIOOBE(() -> { 649 long r = (long) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 650 }); 651 652 checkIOOBE(() -> { 653 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 654 }); 655 656 checkIOOBE(() -> { 657 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 658 }); 659 660 checkIOOBE(() -> { 661 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 662 }); 663 664 checkIOOBE(() -> { 665 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 666 }); 667 668 checkIOOBE(() -> { 669 long o = (long) vh.getAndSet(array, ci, VALUE_1); 670 }); 671 672 checkIOOBE(() -> { 673 long o = (long) vh.getAndSetAcquire(array, ci, VALUE_1); 674 }); 675 676 checkIOOBE(() -> { 677 long o = (long) vh.getAndSetRelease(array, ci, VALUE_1); 678 }); 679 680 checkIOOBE(() -> { 681 long o = (long) vh.getAndAdd(array, ci, VALUE_1); 682 }); 683 684 checkIOOBE(() -> { 685 long o = (long) vh.getAndAddAcquire(array, ci, VALUE_1); 686 }); 687 688 checkIOOBE(() -> { 689 long o = (long) vh.getAndAddRelease(array, ci, VALUE_1); 690 }); 691 692 checkIOOBE(() -> { 693 long o = (long) vh.getAndBitwiseOr(array, ci, VALUE_1); 694 }); 695 696 checkIOOBE(() -> { 697 long o = (long) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 698 }); 699 700 checkIOOBE(() -> { 701 long o = (long) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 702 }); 703 704 checkIOOBE(() -> { 705 long o = (long) vh.getAndBitwiseAnd(array, ci, VALUE_1); 706 }); 707 708 checkIOOBE(() -> { 709 long o = (long) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 710 }); 711 712 checkIOOBE(() -> { 713 long o = (long) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 714 }); 715 716 checkIOOBE(() -> { 717 long o = (long) vh.getAndBitwiseXor(array, ci, VALUE_1); 718 }); 719 720 checkIOOBE(() -> { 721 long o = (long) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 722 }); 723 724 checkIOOBE(() -> { 725 long o = (long) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 726 }); 727 728 } 729 } 730 testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs)731 static void testArrayIndexOutOfBounds(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { 732 VarHandle vh = vhs.s; 733 ByteBuffer array = bs.s; 734 735 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); 736 737 int length = array.limit() - SIZE + 1; 738 for (int i : new int[]{-1, Integer.MIN_VALUE, length, length + 1, Integer.MAX_VALUE}) { 739 final int ci = i; 740 741 checkIOOBE(() -> { 742 long x = (long) vh.get(array, ci); 743 }); 744 745 if (!readOnly) { 746 checkIOOBE(() -> { 747 vh.set(array, ci, VALUE_1); 748 }); 749 } 750 751 checkIOOBE(() -> { 752 long x = (long) vh.getVolatile(array, ci); 753 }); 754 755 checkIOOBE(() -> { 756 long x = (long) vh.getAcquire(array, ci); 757 }); 758 759 checkIOOBE(() -> { 760 long x = (long) vh.getOpaque(array, ci); 761 }); 762 763 if (!readOnly) { 764 checkIOOBE(() -> { 765 vh.setVolatile(array, ci, VALUE_1); 766 }); 767 768 checkIOOBE(() -> { 769 vh.setRelease(array, ci, VALUE_1); 770 }); 771 772 checkIOOBE(() -> { 773 vh.setOpaque(array, ci, VALUE_1); 774 }); 775 776 checkIOOBE(() -> { 777 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 778 }); 779 780 checkIOOBE(() -> { 781 long r = (long) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 782 }); 783 784 checkIOOBE(() -> { 785 long r = (long) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 786 }); 787 788 checkIOOBE(() -> { 789 long r = (long) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 790 }); 791 792 checkIOOBE(() -> { 793 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 794 }); 795 796 checkIOOBE(() -> { 797 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 798 }); 799 800 checkIOOBE(() -> { 801 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 802 }); 803 804 checkIOOBE(() -> { 805 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 806 }); 807 808 checkIOOBE(() -> { 809 long o = (long) vh.getAndSet(array, ci, VALUE_1); 810 }); 811 812 checkIOOBE(() -> { 813 long o = (long) vh.getAndSetAcquire(array, ci, VALUE_1); 814 }); 815 816 checkIOOBE(() -> { 817 long o = (long) vh.getAndSetRelease(array, ci, VALUE_1); 818 }); 819 820 checkIOOBE(() -> { 821 long o = (long) vh.getAndAdd(array, ci, VALUE_1); 822 }); 823 824 checkIOOBE(() -> { 825 long o = (long) vh.getAndAddAcquire(array, ci, VALUE_1); 826 }); 827 828 checkIOOBE(() -> { 829 long o = (long) vh.getAndAddRelease(array, ci, VALUE_1); 830 }); 831 832 checkIOOBE(() -> { 833 long o = (long) vh.getAndBitwiseOr(array, ci, VALUE_1); 834 }); 835 836 checkIOOBE(() -> { 837 long o = (long) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 838 }); 839 840 checkIOOBE(() -> { 841 long o = (long) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 842 }); 843 844 checkIOOBE(() -> { 845 long o = (long) vh.getAndBitwiseAnd(array, ci, VALUE_1); 846 }); 847 848 checkIOOBE(() -> { 849 long o = (long) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 850 }); 851 852 checkIOOBE(() -> { 853 long o = (long) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 854 }); 855 856 checkIOOBE(() -> { 857 long o = (long) vh.getAndBitwiseXor(array, ci, VALUE_1); 858 }); 859 860 checkIOOBE(() -> { 861 long o = (long) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 862 }); 863 864 checkIOOBE(() -> { 865 long o = (long) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 866 }); 867 } 868 } 869 } 870 testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs)871 static void testArrayMisalignedAccess(ByteArraySource bs, VarHandleSource vhs) throws Throwable { 872 VarHandle vh = vhs.s; 873 byte[] array = bs.s; 874 875 int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); 876 877 int length = array.length - SIZE + 1; 878 for (int i = 0; i < length; i++) { 879 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 880 final int ci = i; 881 882 if (!iAligned) { 883 checkISE(() -> { 884 long x = (long) vh.getVolatile(array, ci); 885 }); 886 887 checkISE(() -> { 888 long x = (long) vh.getAcquire(array, ci); 889 }); 890 891 checkISE(() -> { 892 long x = (long) vh.getOpaque(array, ci); 893 }); 894 895 checkISE(() -> { 896 vh.setVolatile(array, ci, VALUE_1); 897 }); 898 899 checkISE(() -> { 900 vh.setRelease(array, ci, VALUE_1); 901 }); 902 903 checkISE(() -> { 904 vh.setOpaque(array, ci, VALUE_1); 905 }); 906 907 checkISE(() -> { 908 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 909 }); 910 911 checkISE(() -> { 912 long r = (long) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 913 }); 914 915 checkISE(() -> { 916 long r = (long) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 917 }); 918 919 checkISE(() -> { 920 long r = (long) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 921 }); 922 923 checkISE(() -> { 924 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 925 }); 926 927 checkISE(() -> { 928 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 929 }); 930 931 checkISE(() -> { 932 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 933 }); 934 935 checkISE(() -> { 936 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 937 }); 938 939 checkISE(() -> { 940 long o = (long) vh.getAndSet(array, ci, VALUE_1); 941 }); 942 943 checkISE(() -> { 944 long o = (long) vh.getAndSetAcquire(array, ci, VALUE_1); 945 }); 946 947 checkISE(() -> { 948 long o = (long) vh.getAndSetRelease(array, ci, VALUE_1); 949 }); 950 951 checkISE(() -> { 952 long o = (long) vh.getAndAdd(array, ci, VALUE_1); 953 }); 954 955 checkISE(() -> { 956 long o = (long) vh.getAndAddAcquire(array, ci, VALUE_1); 957 }); 958 959 checkISE(() -> { 960 long o = (long) vh.getAndAddRelease(array, ci, VALUE_1); 961 }); 962 963 checkISE(() -> { 964 long o = (long) vh.getAndBitwiseOr(array, ci, VALUE_1); 965 }); 966 967 checkISE(() -> { 968 long o = (long) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 969 }); 970 971 checkISE(() -> { 972 long o = (long) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 973 }); 974 975 checkISE(() -> { 976 long o = (long) vh.getAndBitwiseAnd(array, ci, VALUE_1); 977 }); 978 979 checkISE(() -> { 980 long o = (long) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 981 }); 982 983 checkISE(() -> { 984 long o = (long) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 985 }); 986 987 checkISE(() -> { 988 long o = (long) vh.getAndBitwiseXor(array, ci, VALUE_1); 989 }); 990 991 checkISE(() -> { 992 long o = (long) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 993 }); 994 995 checkISE(() -> { 996 long o = (long) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 997 }); 998 } 999 } 1000 } 1001 testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs)1002 static void testArrayMisalignedAccess(ByteBufferSource bs, VarHandleSource vhs) throws Throwable { 1003 VarHandle vh = vhs.s; 1004 ByteBuffer array = bs.s; 1005 1006 boolean readOnly = MemoryMode.READ_ONLY.isSet(bs.memoryModes); 1007 int misalignmentAtZero = array.alignmentOffset(0, SIZE); 1008 1009 int length = array.limit() - SIZE + 1; 1010 for (int i = 0; i < length; i++) { 1011 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 1012 final int ci = i; 1013 1014 if (!iAligned) { 1015 checkISE(() -> { 1016 long x = (long) vh.getVolatile(array, ci); 1017 }); 1018 1019 checkISE(() -> { 1020 long x = (long) vh.getAcquire(array, ci); 1021 }); 1022 1023 checkISE(() -> { 1024 long x = (long) vh.getOpaque(array, ci); 1025 }); 1026 1027 if (!readOnly) { 1028 checkISE(() -> { 1029 vh.setVolatile(array, ci, VALUE_1); 1030 }); 1031 1032 checkISE(() -> { 1033 vh.setRelease(array, ci, VALUE_1); 1034 }); 1035 1036 checkISE(() -> { 1037 vh.setOpaque(array, ci, VALUE_1); 1038 }); 1039 1040 checkISE(() -> { 1041 boolean r = vh.compareAndSet(array, ci, VALUE_1, VALUE_2); 1042 }); 1043 1044 checkISE(() -> { 1045 long r = (long) vh.compareAndExchange(array, ci, VALUE_2, VALUE_1); 1046 }); 1047 1048 checkISE(() -> { 1049 long r = (long) vh.compareAndExchangeAcquire(array, ci, VALUE_2, VALUE_1); 1050 }); 1051 1052 checkISE(() -> { 1053 long r = (long) vh.compareAndExchangeRelease(array, ci, VALUE_2, VALUE_1); 1054 }); 1055 1056 checkISE(() -> { 1057 boolean r = vh.weakCompareAndSetPlain(array, ci, VALUE_1, VALUE_2); 1058 }); 1059 1060 checkISE(() -> { 1061 boolean r = vh.weakCompareAndSet(array, ci, VALUE_1, VALUE_2); 1062 }); 1063 1064 checkISE(() -> { 1065 boolean r = vh.weakCompareAndSetAcquire(array, ci, VALUE_1, VALUE_2); 1066 }); 1067 1068 checkISE(() -> { 1069 boolean r = vh.weakCompareAndSetRelease(array, ci, VALUE_1, VALUE_2); 1070 }); 1071 1072 checkISE(() -> { 1073 long o = (long) vh.getAndSet(array, ci, VALUE_1); 1074 }); 1075 1076 checkISE(() -> { 1077 long o = (long) vh.getAndSetAcquire(array, ci, VALUE_1); 1078 }); 1079 1080 checkISE(() -> { 1081 long o = (long) vh.getAndSetRelease(array, ci, VALUE_1); 1082 }); 1083 1084 checkISE(() -> { 1085 long o = (long) vh.getAndAdd(array, ci, VALUE_1); 1086 }); 1087 1088 checkISE(() -> { 1089 long o = (long) vh.getAndAddAcquire(array, ci, VALUE_1); 1090 }); 1091 1092 checkISE(() -> { 1093 long o = (long) vh.getAndAddRelease(array, ci, VALUE_1); 1094 }); 1095 1096 checkISE(() -> { 1097 long o = (long) vh.getAndBitwiseOr(array, ci, VALUE_1); 1098 }); 1099 1100 checkISE(() -> { 1101 long o = (long) vh.getAndBitwiseOrAcquire(array, ci, VALUE_1); 1102 }); 1103 1104 checkISE(() -> { 1105 long o = (long) vh.getAndBitwiseOrRelease(array, ci, VALUE_1); 1106 }); 1107 1108 checkISE(() -> { 1109 long o = (long) vh.getAndBitwiseAnd(array, ci, VALUE_1); 1110 }); 1111 1112 checkISE(() -> { 1113 long o = (long) vh.getAndBitwiseAndAcquire(array, ci, VALUE_1); 1114 }); 1115 1116 checkISE(() -> { 1117 long o = (long) vh.getAndBitwiseAndRelease(array, ci, VALUE_1); 1118 }); 1119 1120 checkISE(() -> { 1121 long o = (long) vh.getAndBitwiseXor(array, ci, VALUE_1); 1122 }); 1123 1124 checkISE(() -> { 1125 long o = (long) vh.getAndBitwiseXorAcquire(array, ci, VALUE_1); 1126 }); 1127 1128 checkISE(() -> { 1129 long o = (long) vh.getAndBitwiseXorRelease(array, ci, VALUE_1); 1130 }); 1131 } 1132 } 1133 } 1134 } 1135 testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs)1136 static void testArrayReadWrite(ByteArraySource bs, VarHandleSource vhs) { 1137 VarHandle vh = vhs.s; 1138 byte[] array = bs.s; 1139 1140 int misalignmentAtZero = ByteBuffer.wrap(array).alignmentOffset(0, SIZE); 1141 1142 bs.fill((byte) 0xff); 1143 int length = array.length - SIZE + 1; 1144 for (int i = 0; i < length; i++) { 1145 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 1146 1147 // Plain 1148 { 1149 vh.set(array, i, VALUE_1); 1150 long x = (long) vh.get(array, i); 1151 assertEquals(x, VALUE_1, "get long value"); 1152 } 1153 1154 1155 if (iAligned) { 1156 // Volatile 1157 { 1158 vh.setVolatile(array, i, VALUE_2); 1159 long x = (long) vh.getVolatile(array, i); 1160 assertEquals(x, VALUE_2, "setVolatile long value"); 1161 } 1162 1163 // Lazy 1164 { 1165 vh.setRelease(array, i, VALUE_1); 1166 long x = (long) vh.getAcquire(array, i); 1167 assertEquals(x, VALUE_1, "setRelease long value"); 1168 } 1169 1170 // Opaque 1171 { 1172 vh.setOpaque(array, i, VALUE_2); 1173 long x = (long) vh.getOpaque(array, i); 1174 assertEquals(x, VALUE_2, "setOpaque long value"); 1175 } 1176 1177 vh.set(array, i, VALUE_1); 1178 1179 // Compare 1180 { 1181 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2); 1182 assertEquals(r, true, "success compareAndSet long"); 1183 long x = (long) vh.get(array, i); 1184 assertEquals(x, VALUE_2, "success compareAndSet long value"); 1185 } 1186 1187 { 1188 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3); 1189 assertEquals(r, false, "failing compareAndSet long"); 1190 long x = (long) vh.get(array, i); 1191 assertEquals(x, VALUE_2, "failing compareAndSet long value"); 1192 } 1193 1194 { 1195 long r = (long) vh.compareAndExchange(array, i, VALUE_2, VALUE_1); 1196 assertEquals(r, VALUE_2, "success compareAndExchange long"); 1197 long x = (long) vh.get(array, i); 1198 assertEquals(x, VALUE_1, "success compareAndExchange long value"); 1199 } 1200 1201 { 1202 long r = (long) vh.compareAndExchange(array, i, VALUE_2, VALUE_3); 1203 assertEquals(r, VALUE_1, "failing compareAndExchange long"); 1204 long x = (long) vh.get(array, i); 1205 assertEquals(x, VALUE_1, "failing compareAndExchange long value"); 1206 } 1207 1208 { 1209 long r = (long) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2); 1210 assertEquals(r, VALUE_1, "success compareAndExchangeAcquire long"); 1211 long x = (long) vh.get(array, i); 1212 assertEquals(x, VALUE_2, "success compareAndExchangeAcquire long value"); 1213 } 1214 1215 { 1216 long r = (long) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3); 1217 assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire long"); 1218 long x = (long) vh.get(array, i); 1219 assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire long value"); 1220 } 1221 1222 { 1223 long r = (long) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1); 1224 assertEquals(r, VALUE_2, "success compareAndExchangeRelease long"); 1225 long x = (long) vh.get(array, i); 1226 assertEquals(x, VALUE_1, "success compareAndExchangeRelease long value"); 1227 } 1228 1229 { 1230 long r = (long) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3); 1231 assertEquals(r, VALUE_1, "failing compareAndExchangeRelease long"); 1232 long x = (long) vh.get(array, i); 1233 assertEquals(x, VALUE_1, "failing compareAndExchangeRelease long value"); 1234 } 1235 1236 { 1237 boolean success = false; 1238 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1239 success = vh.weakCompareAndSetPlain(array, i, VALUE_1, VALUE_2); 1240 } 1241 assertEquals(success, true, "weakCompareAndSetPlain long"); 1242 long x = (long) vh.get(array, i); 1243 assertEquals(x, VALUE_2, "weakCompareAndSetPlain long value"); 1244 } 1245 1246 { 1247 boolean success = false; 1248 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1249 success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1); 1250 } 1251 assertEquals(success, true, "weakCompareAndSetAcquire long"); 1252 long x = (long) vh.get(array, i); 1253 assertEquals(x, VALUE_1, "weakCompareAndSetAcquire long"); 1254 } 1255 1256 { 1257 boolean success = false; 1258 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1259 success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2); 1260 } 1261 assertEquals(success, true, "weakCompareAndSetRelease long"); 1262 long x = (long) vh.get(array, i); 1263 assertEquals(x, VALUE_2, "weakCompareAndSetRelease long"); 1264 } 1265 1266 { 1267 boolean success = false; 1268 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1269 success = vh.weakCompareAndSet(array, i, VALUE_2, VALUE_1); 1270 } 1271 assertEquals(success, true, "weakCompareAndSet long"); 1272 long x = (long) vh.get(array, i); 1273 assertEquals(x, VALUE_1, "weakCompareAndSet long"); 1274 } 1275 1276 // Compare set and get 1277 { 1278 vh.set(array, i, VALUE_1); 1279 1280 long o = (long) vh.getAndSet(array, i, VALUE_2); 1281 assertEquals(o, VALUE_1, "getAndSet long"); 1282 long x = (long) vh.get(array, i); 1283 assertEquals(x, VALUE_2, "getAndSet long value"); 1284 } 1285 1286 { 1287 vh.set(array, i, VALUE_1); 1288 1289 long o = (long) vh.getAndSetAcquire(array, i, VALUE_2); 1290 assertEquals(o, VALUE_1, "getAndSetAcquire long"); 1291 long x = (long) vh.get(array, i); 1292 assertEquals(x, VALUE_2, "getAndSetAcquire long value"); 1293 } 1294 1295 { 1296 vh.set(array, i, VALUE_1); 1297 1298 long o = (long) vh.getAndSetRelease(array, i, VALUE_2); 1299 assertEquals(o, VALUE_1, "getAndSetRelease long"); 1300 long x = (long) vh.get(array, i); 1301 assertEquals(x, VALUE_2, "getAndSetRelease long value"); 1302 } 1303 1304 // get and add, add and get 1305 { 1306 vh.set(array, i, VALUE_1); 1307 1308 long o = (long) vh.getAndAdd(array, i, VALUE_2); 1309 assertEquals(o, VALUE_1, "getAndAdd long"); 1310 long x = (long) vh.get(array, i); 1311 assertEquals(x, VALUE_1 + VALUE_2, "getAndAdd long value"); 1312 } 1313 1314 { 1315 vh.set(array, i, VALUE_1); 1316 1317 long o = (long) vh.getAndAddAcquire(array, i, VALUE_2); 1318 assertEquals(o, VALUE_1, "getAndAddAcquire long"); 1319 long x = (long) vh.get(array, i); 1320 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddAcquire long value"); 1321 } 1322 1323 { 1324 vh.set(array, i, VALUE_1); 1325 1326 long o = (long) vh.getAndAddRelease(array, i, VALUE_2); 1327 assertEquals(o, VALUE_1, "getAndAddRelease long"); 1328 long x = (long) vh.get(array, i); 1329 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddRelease long value"); 1330 } 1331 1332 // get and bitwise or 1333 { 1334 vh.set(array, i, VALUE_1); 1335 1336 long o = (long) vh.getAndBitwiseOr(array, i, VALUE_2); 1337 assertEquals(o, VALUE_1, "getAndBitwiseOr long"); 1338 long x = (long) vh.get(array, i); 1339 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOr long value"); 1340 } 1341 1342 { 1343 vh.set(array, i, VALUE_1); 1344 1345 long o = (long) vh.getAndBitwiseOrAcquire(array, i, VALUE_2); 1346 assertEquals(o, VALUE_1, "getAndBitwiseOrAcquire long"); 1347 long x = (long) vh.get(array, i); 1348 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrAcquire long value"); 1349 } 1350 1351 { 1352 vh.set(array, i, VALUE_1); 1353 1354 long o = (long) vh.getAndBitwiseOrRelease(array, i, VALUE_2); 1355 assertEquals(o, VALUE_1, "getAndBitwiseOrRelease long"); 1356 long x = (long) vh.get(array, i); 1357 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrRelease long value"); 1358 } 1359 1360 // get and bitwise and 1361 { 1362 vh.set(array, i, VALUE_1); 1363 1364 long o = (long) vh.getAndBitwiseAnd(array, i, VALUE_2); 1365 assertEquals(o, VALUE_1, "getAndBitwiseAnd long"); 1366 long x = (long) vh.get(array, i); 1367 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAnd long value"); 1368 } 1369 1370 { 1371 vh.set(array, i, VALUE_1); 1372 1373 long o = (long) vh.getAndBitwiseAndAcquire(array, i, VALUE_2); 1374 assertEquals(o, VALUE_1, "getAndBitwiseAndAcquire long"); 1375 long x = (long) vh.get(array, i); 1376 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndAcquire long value"); 1377 } 1378 1379 { 1380 vh.set(array, i, VALUE_1); 1381 1382 long o = (long) vh.getAndBitwiseAndRelease(array, i, VALUE_2); 1383 assertEquals(o, VALUE_1, "getAndBitwiseAndRelease long"); 1384 long x = (long) vh.get(array, i); 1385 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndRelease long value"); 1386 } 1387 1388 // get and bitwise xor 1389 { 1390 vh.set(array, i, VALUE_1); 1391 1392 long o = (long) vh.getAndBitwiseXor(array, i, VALUE_2); 1393 assertEquals(o, VALUE_1, "getAndBitwiseXor long"); 1394 long x = (long) vh.get(array, i); 1395 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXor long value"); 1396 } 1397 1398 { 1399 vh.set(array, i, VALUE_1); 1400 1401 long o = (long) vh.getAndBitwiseXorAcquire(array, i, VALUE_2); 1402 assertEquals(o, VALUE_1, "getAndBitwiseXorAcquire long"); 1403 long x = (long) vh.get(array, i); 1404 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorAcquire long value"); 1405 } 1406 1407 { 1408 vh.set(array, i, VALUE_1); 1409 1410 long o = (long) vh.getAndBitwiseXorRelease(array, i, VALUE_2); 1411 assertEquals(o, VALUE_1, "getAndBitwiseXorRelease long"); 1412 long x = (long) vh.get(array, i); 1413 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorRelease long value"); 1414 } 1415 } 1416 } 1417 } 1418 1419 testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs)1420 static void testArrayReadWrite(ByteBufferSource bs, VarHandleSource vhs) { 1421 VarHandle vh = vhs.s; 1422 ByteBuffer array = bs.s; 1423 1424 int misalignmentAtZero = array.alignmentOffset(0, SIZE); 1425 1426 bs.fill((byte) 0xff); 1427 int length = array.limit() - SIZE + 1; 1428 for (int i = 0; i < length; i++) { 1429 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 1430 1431 // Plain 1432 { 1433 vh.set(array, i, VALUE_1); 1434 long x = (long) vh.get(array, i); 1435 assertEquals(x, VALUE_1, "get long value"); 1436 } 1437 1438 if (iAligned) { 1439 // Volatile 1440 { 1441 vh.setVolatile(array, i, VALUE_2); 1442 long x = (long) vh.getVolatile(array, i); 1443 assertEquals(x, VALUE_2, "setVolatile long value"); 1444 } 1445 1446 // Lazy 1447 { 1448 vh.setRelease(array, i, VALUE_1); 1449 long x = (long) vh.getAcquire(array, i); 1450 assertEquals(x, VALUE_1, "setRelease long value"); 1451 } 1452 1453 // Opaque 1454 { 1455 vh.setOpaque(array, i, VALUE_2); 1456 long x = (long) vh.getOpaque(array, i); 1457 assertEquals(x, VALUE_2, "setOpaque long value"); 1458 } 1459 1460 vh.set(array, i, VALUE_1); 1461 1462 // Compare 1463 { 1464 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_2); 1465 assertEquals(r, true, "success compareAndSet long"); 1466 long x = (long) vh.get(array, i); 1467 assertEquals(x, VALUE_2, "success compareAndSet long value"); 1468 } 1469 1470 { 1471 boolean r = vh.compareAndSet(array, i, VALUE_1, VALUE_3); 1472 assertEquals(r, false, "failing compareAndSet long"); 1473 long x = (long) vh.get(array, i); 1474 assertEquals(x, VALUE_2, "failing compareAndSet long value"); 1475 } 1476 1477 { 1478 long r = (long) vh.compareAndExchange(array, i, VALUE_2, VALUE_1); 1479 assertEquals(r, VALUE_2, "success compareAndExchange long"); 1480 long x = (long) vh.get(array, i); 1481 assertEquals(x, VALUE_1, "success compareAndExchange long value"); 1482 } 1483 1484 { 1485 long r = (long) vh.compareAndExchange(array, i, VALUE_2, VALUE_3); 1486 assertEquals(r, VALUE_1, "failing compareAndExchange long"); 1487 long x = (long) vh.get(array, i); 1488 assertEquals(x, VALUE_1, "failing compareAndExchange long value"); 1489 } 1490 1491 { 1492 long r = (long) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_2); 1493 assertEquals(r, VALUE_1, "success compareAndExchangeAcquire long"); 1494 long x = (long) vh.get(array, i); 1495 assertEquals(x, VALUE_2, "success compareAndExchangeAcquire long value"); 1496 } 1497 1498 { 1499 long r = (long) vh.compareAndExchangeAcquire(array, i, VALUE_1, VALUE_3); 1500 assertEquals(r, VALUE_2, "failing compareAndExchangeAcquire long"); 1501 long x = (long) vh.get(array, i); 1502 assertEquals(x, VALUE_2, "failing compareAndExchangeAcquire long value"); 1503 } 1504 1505 { 1506 long r = (long) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_1); 1507 assertEquals(r, VALUE_2, "success compareAndExchangeRelease long"); 1508 long x = (long) vh.get(array, i); 1509 assertEquals(x, VALUE_1, "success compareAndExchangeRelease long value"); 1510 } 1511 1512 { 1513 long r = (long) vh.compareAndExchangeRelease(array, i, VALUE_2, VALUE_3); 1514 assertEquals(r, VALUE_1, "failing compareAndExchangeRelease long"); 1515 long x = (long) vh.get(array, i); 1516 assertEquals(x, VALUE_1, "failing compareAndExchangeRelease long value"); 1517 } 1518 1519 { 1520 boolean success = false; 1521 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1522 success = vh.weakCompareAndSetPlain(array, i, VALUE_1, VALUE_2); 1523 } 1524 assertEquals(success, true, "weakCompareAndSetPlain long"); 1525 long x = (long) vh.get(array, i); 1526 assertEquals(x, VALUE_2, "weakCompareAndSetPlain long value"); 1527 } 1528 1529 { 1530 boolean success = false; 1531 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1532 success = vh.weakCompareAndSetAcquire(array, i, VALUE_2, VALUE_1); 1533 } 1534 assertEquals(success, true, "weakCompareAndSetAcquire long"); 1535 long x = (long) vh.get(array, i); 1536 assertEquals(x, VALUE_1, "weakCompareAndSetAcquire long"); 1537 } 1538 1539 { 1540 boolean success = false; 1541 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1542 success = vh.weakCompareAndSetRelease(array, i, VALUE_1, VALUE_2); 1543 } 1544 assertEquals(success, true, "weakCompareAndSetRelease long"); 1545 long x = (long) vh.get(array, i); 1546 assertEquals(x, VALUE_2, "weakCompareAndSetRelease long"); 1547 } 1548 1549 { 1550 boolean success = false; 1551 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) { 1552 success = vh.weakCompareAndSet(array, i, VALUE_2, VALUE_1); 1553 } 1554 assertEquals(success, true, "weakCompareAndSet long"); 1555 long x = (long) vh.get(array, i); 1556 assertEquals(x, VALUE_1, "weakCompareAndSet long"); 1557 } 1558 1559 // Compare set and get 1560 { 1561 vh.set(array, i, VALUE_1); 1562 1563 long o = (long) vh.getAndSet(array, i, VALUE_2); 1564 assertEquals(o, VALUE_1, "getAndSet long"); 1565 long x = (long) vh.get(array, i); 1566 assertEquals(x, VALUE_2, "getAndSet long value"); 1567 } 1568 1569 { 1570 vh.set(array, i, VALUE_1); 1571 1572 long o = (long) vh.getAndSetAcquire(array, i, VALUE_2); 1573 assertEquals(o, VALUE_1, "getAndSetAcquire long"); 1574 long x = (long) vh.get(array, i); 1575 assertEquals(x, VALUE_2, "getAndSetAcquire long value"); 1576 } 1577 1578 { 1579 vh.set(array, i, VALUE_1); 1580 1581 long o = (long) vh.getAndSetRelease(array, i, VALUE_2); 1582 assertEquals(o, VALUE_1, "getAndSetRelease long"); 1583 long x = (long) vh.get(array, i); 1584 assertEquals(x, VALUE_2, "getAndSetRelease long value"); 1585 } 1586 1587 // get and add, add and get 1588 { 1589 vh.set(array, i, VALUE_1); 1590 1591 long o = (long) vh.getAndAdd(array, i, VALUE_2); 1592 assertEquals(o, VALUE_1, "getAndAdd long"); 1593 long x = (long) vh.get(array, i); 1594 assertEquals(x, VALUE_1 + VALUE_2, "getAndAdd long value"); 1595 } 1596 1597 { 1598 vh.set(array, i, VALUE_1); 1599 1600 long o = (long) vh.getAndAddAcquire(array, i, VALUE_2); 1601 assertEquals(o, VALUE_1, "getAndAddAcquire long"); 1602 long x = (long) vh.get(array, i); 1603 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddAcquire long value"); 1604 } 1605 1606 { 1607 vh.set(array, i, VALUE_1); 1608 1609 long o = (long) vh.getAndAddRelease(array, i, VALUE_2); 1610 assertEquals(o, VALUE_1, "getAndAddRelease long"); 1611 long x = (long) vh.get(array, i); 1612 assertEquals(x, VALUE_1 + VALUE_2, "getAndAddRelease long value"); 1613 } 1614 1615 // get and bitwise or 1616 { 1617 vh.set(array, i, VALUE_1); 1618 1619 long o = (long) vh.getAndBitwiseOr(array, i, VALUE_2); 1620 assertEquals(o, VALUE_1, "getAndBitwiseOr long"); 1621 long x = (long) vh.get(array, i); 1622 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOr long value"); 1623 } 1624 1625 { 1626 vh.set(array, i, VALUE_1); 1627 1628 long o = (long) vh.getAndBitwiseOrAcquire(array, i, VALUE_2); 1629 assertEquals(o, VALUE_1, "getAndBitwiseOrAcquire long"); 1630 long x = (long) vh.get(array, i); 1631 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrAcquire long value"); 1632 } 1633 1634 { 1635 vh.set(array, i, VALUE_1); 1636 1637 long o = (long) vh.getAndBitwiseOrRelease(array, i, VALUE_2); 1638 assertEquals(o, VALUE_1, "getAndBitwiseOrRelease long"); 1639 long x = (long) vh.get(array, i); 1640 assertEquals(x, VALUE_1 | VALUE_2, "getAndBitwiseOrRelease long value"); 1641 } 1642 1643 // get and bitwise and 1644 { 1645 vh.set(array, i, VALUE_1); 1646 1647 long o = (long) vh.getAndBitwiseAnd(array, i, VALUE_2); 1648 assertEquals(o, VALUE_1, "getAndBitwiseAnd long"); 1649 long x = (long) vh.get(array, i); 1650 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAnd long value"); 1651 } 1652 1653 { 1654 vh.set(array, i, VALUE_1); 1655 1656 long o = (long) vh.getAndBitwiseAndAcquire(array, i, VALUE_2); 1657 assertEquals(o, VALUE_1, "getAndBitwiseAndAcquire long"); 1658 long x = (long) vh.get(array, i); 1659 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndAcquire long value"); 1660 } 1661 1662 { 1663 vh.set(array, i, VALUE_1); 1664 1665 long o = (long) vh.getAndBitwiseAndRelease(array, i, VALUE_2); 1666 assertEquals(o, VALUE_1, "getAndBitwiseAndRelease long"); 1667 long x = (long) vh.get(array, i); 1668 assertEquals(x, VALUE_1 & VALUE_2, "getAndBitwiseAndRelease long value"); 1669 } 1670 1671 // get and bitwise xor 1672 { 1673 vh.set(array, i, VALUE_1); 1674 1675 long o = (long) vh.getAndBitwiseXor(array, i, VALUE_2); 1676 assertEquals(o, VALUE_1, "getAndBitwiseXor long"); 1677 long x = (long) vh.get(array, i); 1678 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXor long value"); 1679 } 1680 1681 { 1682 vh.set(array, i, VALUE_1); 1683 1684 long o = (long) vh.getAndBitwiseXorAcquire(array, i, VALUE_2); 1685 assertEquals(o, VALUE_1, "getAndBitwiseXorAcquire long"); 1686 long x = (long) vh.get(array, i); 1687 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorAcquire long value"); 1688 } 1689 1690 { 1691 vh.set(array, i, VALUE_1); 1692 1693 long o = (long) vh.getAndBitwiseXorRelease(array, i, VALUE_2); 1694 assertEquals(o, VALUE_1, "getAndBitwiseXorRelease long"); 1695 long x = (long) vh.get(array, i); 1696 assertEquals(x, VALUE_1 ^ VALUE_2, "getAndBitwiseXorRelease long value"); 1697 } 1698 } 1699 } 1700 } 1701 testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs)1702 static void testArrayReadOnly(ByteBufferSource bs, VarHandleSource vhs) { 1703 VarHandle vh = vhs.s; 1704 ByteBuffer array = bs.s; 1705 1706 int misalignmentAtZero = array.alignmentOffset(0, SIZE); 1707 1708 ByteBuffer bb = ByteBuffer.allocate(SIZE); 1709 bb.order(MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); 1710 bs.fill(bb.putLong(0, VALUE_2).array()); 1711 1712 int length = array.limit() - SIZE + 1; 1713 for (int i = 0; i < length; i++) { 1714 boolean iAligned = ((i + misalignmentAtZero) & (SIZE - 1)) == 0; 1715 1716 long v = MemoryMode.BIG_ENDIAN.isSet(vhs.memoryModes) 1717 ? rotateLeft(VALUE_2, (i % SIZE) << 3) 1718 : rotateRight(VALUE_2, (i % SIZE) << 3); 1719 // Plain 1720 { 1721 long x = (long) vh.get(array, i); 1722 assertEquals(x, v, "get long value"); 1723 } 1724 1725 if (iAligned) { 1726 // Volatile 1727 { 1728 long x = (long) vh.getVolatile(array, i); 1729 assertEquals(x, v, "getVolatile long value"); 1730 } 1731 1732 // Lazy 1733 { 1734 long x = (long) vh.getAcquire(array, i); 1735 assertEquals(x, v, "getRelease long value"); 1736 } 1737 1738 // Opaque 1739 { 1740 long x = (long) vh.getOpaque(array, i); 1741 assertEquals(x, v, "getOpaque long value"); 1742 } 1743 } 1744 } 1745 } 1746 1747 } 1748 1749