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