1 /*
   2  * Copyright (c) 2015, 2023, 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  * @run testng/othervm -Diters=10   -Xint                                                   VarHandleTestAccessString
  27  *
  28  * @comment Set CompileThresholdScaling to 0.1 so that the warmup loop sets to 2000 iterations
  29  *          to hit compilation thresholds
  30  *
  31  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:TieredStopAtLevel=1 VarHandleTestAccessString
  32  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1                         VarHandleTestAccessString
  33  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:-TieredCompilation  VarHandleTestAccessString
  34  */
  35 
  36 import org.testng.annotations.BeforeClass;
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Test;
  39 
  40 import java.lang.invoke.MethodHandles;
  41 import java.lang.invoke.VarHandle;
  42 import java.util.ArrayList;
  43 import java.util.Arrays;
  44 import java.util.List;
  45 
  46 import static org.testng.Assert.*;
  47 
  48 public class VarHandleTestAccessString extends VarHandleBaseTest {
  49     static final String static_final_v = "foo";
  50 
  51     static String static_v;
  52 
  53     final String final_v = "foo";
  54 
  55     String v;
  56 
  57     static final String static_final_v2 = "foo";
  58 
  59     static String static_v2;
  60 
  61     final String final_v2 = "foo";
  62 
  63     String v2;
  64 
  65     VarHandle vhFinalField;
  66 
  67     VarHandle vhField;
  68 
  69     VarHandle vhStaticField;
  70 
  71     VarHandle vhStaticFinalField;
  72 
  73     VarHandle vhArray;
  74 
  75     VarHandle vhArrayObject;
  76 
  77     VarHandle[] allocate(boolean same) {
  78         List<VarHandle> vhs = new ArrayList<>();
  79 
  80         String postfix = same ? "" : "2";
  81         VarHandle vh;
  82         try {
  83             vh = MethodHandles.lookup().findVarHandle(
  84                     VarHandleTestAccessString.class, "final_v" + postfix, String.class);
  85             vhs.add(vh);
  86 
  87             vh = MethodHandles.lookup().findVarHandle(
  88                     VarHandleTestAccessString.class, "v" + postfix, String.class);
  89             vhs.add(vh);
  90 
  91             vh = MethodHandles.lookup().findStaticVarHandle(
  92                 VarHandleTestAccessString.class, "static_final_v" + postfix, String.class);
  93             vhs.add(vh);
  94 
  95             vh = MethodHandles.lookup().findStaticVarHandle(
  96                 VarHandleTestAccessString.class, "static_v" + postfix, String.class);
  97             vhs.add(vh);
  98 
  99             if (same) {
 100                 vh = MethodHandles.arrayElementVarHandle(String[].class);
 101             }
 102             else {
 103                 vh = MethodHandles.arrayElementVarHandle(int[].class);
 104             }
 105             vhs.add(vh);
 106         } catch (Exception e) {
 107             throw new InternalError(e);
 108         }
 109         return vhs.toArray(new VarHandle[0]);
 110     }
 111 
 112     @BeforeClass
 113     public void setup() throws Exception {
 114         vhFinalField = MethodHandles.lookup().findVarHandle(
 115                 VarHandleTestAccessString.class, "final_v", String.class);
 116 
 117         vhField = MethodHandles.lookup().findVarHandle(
 118                 VarHandleTestAccessString.class, "v", String.class);
 119 
 120         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 121             VarHandleTestAccessString.class, "static_final_v", String.class);
 122 
 123         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 124             VarHandleTestAccessString.class, "static_v", String.class);
 125 
 126         vhArray = MethodHandles.arrayElementVarHandle(String[].class);
 127         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);
 128     }
 129 
 130 
 131     @DataProvider
 132     public Object[][] varHandlesProvider() throws Exception {
 133         List<VarHandle> vhs = new ArrayList<>();
 134         vhs.add(vhField);
 135         vhs.add(vhStaticField);
 136         vhs.add(vhArray);
 137 
 138         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
 139     }
 140 
 141     @Test
 142     public void testEquals() {
 143         VarHandle[] vhs1 = allocate(true);
 144         VarHandle[] vhs2 = allocate(true);
 145 
 146         for (int i = 0; i < vhs1.length; i++) {
 147             for (int j = 0; j < vhs1.length; j++) {
 148                 if (i != j) {
 149                     assertNotEquals(vhs1[i], vhs1[j]);
 150                     assertNotEquals(vhs1[i], vhs2[j]);
 151                 }
 152             }
 153         }
 154 
 155         VarHandle[] vhs3 = allocate(false);
 156         for (int i = 0; i < vhs1.length; i++) {
 157             assertNotEquals(vhs1[i], vhs3[i]);
 158         }
 159     }
 160 
 161     @Test(dataProvider = "varHandlesProvider")
 162     public void testIsAccessModeSupported(VarHandle vh) {
 163         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
 164         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
 165         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
 166         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 167         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
 168         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 169         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 170         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 171 
 172         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 173         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 174         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 175         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 176         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 177         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 178         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
 179         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
 180         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
 181         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_ACQUIRE));
 182         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET_RELEASE));
 183 
 184         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 185         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE));
 186         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD_RELEASE));
 187 
 188         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR));
 189         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE));
 190         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE));
 191         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND));
 192         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
 193         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
 194         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
 195         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
 196         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
 197     }
 198 
 199 
 200     @DataProvider
 201     public Object[][] typesProvider() throws Exception {
 202         List<Object[]> types = new ArrayList<>();
 203         types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessString.class)});
 204         types.add(new Object[] {vhStaticField, Arrays.asList()});
 205         types.add(new Object[] {vhArray, Arrays.asList(String[].class, int.class)});
 206 
 207         return types.stream().toArray(Object[][]::new);
 208     }
 209 
 210     @Test(dataProvider = "typesProvider")
 211     public void testTypes(VarHandle vh, List<Class<?>> pts) {
 212         assertEquals(vh.varType(), String.class);
 213 
 214         assertEquals(vh.coordinateTypes(), pts);
 215 
 216         testTypes(vh);
 217     }
 218 
 219 
 220     @Test
 221     public void testLookupInstanceToStatic() {
 222         checkIAE("Lookup of static final field to instance final field", () -> {
 223             MethodHandles.lookup().findStaticVarHandle(
 224                     VarHandleTestAccessString.class, "final_v", String.class);
 225         });
 226 
 227         checkIAE("Lookup of static field to instance field", () -> {
 228             MethodHandles.lookup().findStaticVarHandle(
 229                     VarHandleTestAccessString.class, "v", String.class);
 230         });
 231     }
 232 
 233     @Test
 234     public void testLookupStaticToInstance() {
 235         checkIAE("Lookup of instance final field to static final field", () -> {
 236             MethodHandles.lookup().findVarHandle(
 237                 VarHandleTestAccessString.class, "static_final_v", String.class);
 238         });
 239 
 240         checkIAE("Lookup of instance field to static field", () -> {
 241             vhStaticField = MethodHandles.lookup().findVarHandle(
 242                 VarHandleTestAccessString.class, "static_v", String.class);
 243         });
 244     }
 245 
 246 
 247     @DataProvider
 248     public Object[][] accessTestCaseProvider() throws Exception {
 249         List<AccessTestCase<?>> cases = new ArrayList<>();
 250 
 251         cases.add(new VarHandleAccessTestCase("Instance final field",
 252                                               vhFinalField, vh -> testInstanceFinalField(this, vh)));
 253         cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
 254                                               vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
 255                                               false));
 256 
 257         cases.add(new VarHandleAccessTestCase("Static final field",
 258                                               vhStaticFinalField, VarHandleTestAccessString::testStaticFinalField));
 259         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 260                                               vhStaticFinalField, VarHandleTestAccessString::testStaticFinalFieldUnsupported,
 261                                               false));
 262 
 263         cases.add(new VarHandleAccessTestCase("Instance field",
 264                                               vhField, vh -> testInstanceField(this, vh)));
 265         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 266                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 267                                               false));
 268 
 269         cases.add(new VarHandleAccessTestCase("Static field",
 270                                               vhStaticField, VarHandleTestAccessString::testStaticField));
 271         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 272                                               vhStaticField, VarHandleTestAccessString::testStaticFieldUnsupported,
 273                                               false));
 274 
 275         cases.add(new VarHandleAccessTestCase("Array",
 276                                               vhArray, VarHandleTestAccessString::testArray));
 277         cases.add(new VarHandleAccessTestCase("Array Object[]",
 278                                               vhArrayObject, VarHandleTestAccessString::testArray));
 279         cases.add(new VarHandleAccessTestCase("Array unsupported",
 280                                               vhArray, VarHandleTestAccessString::testArrayUnsupported,
 281                                               false));
 282         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 283                                               vhArray, VarHandleTestAccessString::testArrayIndexOutOfBounds,
 284                                               false));
 285         cases.add(new VarHandleAccessTestCase("Array store exception",
 286                                               vhArrayObject, VarHandleTestAccessString::testArrayStoreException,
 287                                               false));
 288         // Work around issue with jtreg summary reporting which truncates
 289         // the String result of Object.toString to 30 characters, hence
 290         // the first dummy argument
 291         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 292     }
 293 
 294     @Test(dataProvider = "accessTestCaseProvider")
 295     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 296         T t = atc.get();
 297         int iters = atc.requiresLoop() ? ITERS : 1;
 298         for (int c = 0; c < iters; c++) {
 299             atc.testAccess(t);
 300         }
 301     }
 302 
 303     static void testInstanceFinalField(VarHandleTestAccessString recv, VarHandle vh) {
 304         // Plain
 305         {
 306             String x = (String) vh.get(recv);
 307             assertEquals(x, "foo", "get String value");
 308         }
 309 
 310 
 311         // Volatile
 312         {
 313             String x = (String) vh.getVolatile(recv);
 314             assertEquals(x, "foo", "getVolatile String value");
 315         }
 316 
 317         // Lazy
 318         {
 319             String x = (String) vh.getAcquire(recv);
 320             assertEquals(x, "foo", "getRelease String value");
 321         }
 322 
 323         // Opaque
 324         {
 325             String x = (String) vh.getOpaque(recv);
 326             assertEquals(x, "foo", "getOpaque String value");
 327         }
 328     }
 329 
 330     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessString recv, VarHandle vh) {
 331         checkUOE(() -> {
 332             vh.set(recv, "bar");
 333         });
 334 
 335         checkUOE(() -> {
 336             vh.setVolatile(recv, "bar");
 337         });
 338 
 339         checkUOE(() -> {
 340             vh.setRelease(recv, "bar");
 341         });
 342 
 343         checkUOE(() -> {
 344             vh.setOpaque(recv, "bar");
 345         });
 346 
 347 
 348         checkUOE(() -> {
 349             String o = (String) vh.getAndAdd(recv, "foo");
 350         });
 351 
 352         checkUOE(() -> {
 353             String o = (String) vh.getAndAddAcquire(recv, "foo");
 354         });
 355 
 356         checkUOE(() -> {
 357             String o = (String) vh.getAndAddRelease(recv, "foo");
 358         });
 359 
 360         checkUOE(() -> {
 361             String o = (String) vh.getAndBitwiseOr(recv, "foo");
 362         });
 363 
 364         checkUOE(() -> {
 365             String o = (String) vh.getAndBitwiseOrAcquire(recv, "foo");
 366         });
 367 
 368         checkUOE(() -> {
 369             String o = (String) vh.getAndBitwiseOrRelease(recv, "foo");
 370         });
 371 
 372         checkUOE(() -> {
 373             String o = (String) vh.getAndBitwiseAnd(recv, "foo");
 374         });
 375 
 376         checkUOE(() -> {
 377             String o = (String) vh.getAndBitwiseAndAcquire(recv, "foo");
 378         });
 379 
 380         checkUOE(() -> {
 381             String o = (String) vh.getAndBitwiseAndRelease(recv, "foo");
 382         });
 383 
 384         checkUOE(() -> {
 385             String o = (String) vh.getAndBitwiseXor(recv, "foo");
 386         });
 387 
 388         checkUOE(() -> {
 389             String o = (String) vh.getAndBitwiseXorAcquire(recv, "foo");
 390         });
 391 
 392         checkUOE(() -> {
 393             String o = (String) vh.getAndBitwiseXorRelease(recv, "foo");
 394         });
 395     }
 396 
 397 
 398     static void testStaticFinalField(VarHandle vh) {
 399         // Plain
 400         {
 401             String x = (String) vh.get();
 402             assertEquals(x, "foo", "get String value");
 403         }
 404 
 405 
 406         // Volatile
 407         {
 408             String x = (String) vh.getVolatile();
 409             assertEquals(x, "foo", "getVolatile String value");
 410         }
 411 
 412         // Lazy
 413         {
 414             String x = (String) vh.getAcquire();
 415             assertEquals(x, "foo", "getRelease String value");
 416         }
 417 
 418         // Opaque
 419         {
 420             String x = (String) vh.getOpaque();
 421             assertEquals(x, "foo", "getOpaque String value");
 422         }
 423     }
 424 
 425     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 426         checkUOE(() -> {
 427             vh.set("bar");
 428         });
 429 
 430         checkUOE(() -> {
 431             vh.setVolatile("bar");
 432         });
 433 
 434         checkUOE(() -> {
 435             vh.setRelease("bar");
 436         });
 437 
 438         checkUOE(() -> {
 439             vh.setOpaque("bar");
 440         });
 441 
 442 
 443         checkUOE(() -> {
 444             String o = (String) vh.getAndAdd("foo");
 445         });
 446 
 447         checkUOE(() -> {
 448             String o = (String) vh.getAndAddAcquire("foo");
 449         });
 450 
 451         checkUOE(() -> {
 452             String o = (String) vh.getAndAddRelease("foo");
 453         });
 454 
 455         checkUOE(() -> {
 456             String o = (String) vh.getAndBitwiseOr("foo");
 457         });
 458 
 459         checkUOE(() -> {
 460             String o = (String) vh.getAndBitwiseOrAcquire("foo");
 461         });
 462 
 463         checkUOE(() -> {
 464             String o = (String) vh.getAndBitwiseOrRelease("foo");
 465         });
 466 
 467         checkUOE(() -> {
 468             String o = (String) vh.getAndBitwiseAnd("foo");
 469         });
 470 
 471         checkUOE(() -> {
 472             String o = (String) vh.getAndBitwiseAndAcquire("foo");
 473         });
 474 
 475         checkUOE(() -> {
 476             String o = (String) vh.getAndBitwiseAndRelease("foo");
 477         });
 478 
 479         checkUOE(() -> {
 480             String o = (String) vh.getAndBitwiseXor("foo");
 481         });
 482 
 483         checkUOE(() -> {
 484             String o = (String) vh.getAndBitwiseXorAcquire("foo");
 485         });
 486 
 487         checkUOE(() -> {
 488             String o = (String) vh.getAndBitwiseXorRelease("foo");
 489         });
 490     }
 491 
 492 
 493     static void testInstanceField(VarHandleTestAccessString recv, VarHandle vh) {
 494         // Plain
 495         {
 496             vh.set(recv, "foo");
 497             String x = (String) vh.get(recv);
 498             assertEquals(x, "foo", "set String value");
 499         }
 500 
 501 
 502         // Volatile
 503         {
 504             vh.setVolatile(recv, "bar");
 505             String x = (String) vh.getVolatile(recv);
 506             assertEquals(x, "bar", "setVolatile String value");
 507         }
 508 
 509         // Lazy
 510         {
 511             vh.setRelease(recv, "foo");
 512             String x = (String) vh.getAcquire(recv);
 513             assertEquals(x, "foo", "setRelease String value");
 514         }
 515 
 516         // Opaque
 517         {
 518             vh.setOpaque(recv, "bar");
 519             String x = (String) vh.getOpaque(recv);
 520             assertEquals(x, "bar", "setOpaque String value");
 521         }
 522 
 523         vh.set(recv, "foo");
 524 
 525         // Compare
 526         {
 527             boolean r = vh.compareAndSet(recv, "foo", "bar");
 528             assertEquals(r, true, "success compareAndSet String");
 529             String x = (String) vh.get(recv);
 530             assertEquals(x, "bar", "success compareAndSet String value");
 531         }
 532 
 533         {
 534             boolean r = vh.compareAndSet(recv, "foo", "baz");
 535             assertEquals(r, false, "failing compareAndSet String");
 536             String x = (String) vh.get(recv);
 537             assertEquals(x, "bar", "failing compareAndSet String value");
 538         }
 539 
 540         {
 541             String r = (String) vh.compareAndExchange(recv, "bar", "foo");
 542             assertEquals(r, "bar", "success compareAndExchange String");
 543             String x = (String) vh.get(recv);
 544             assertEquals(x, "foo", "success compareAndExchange String value");
 545         }
 546 
 547         {
 548             String r = (String) vh.compareAndExchange(recv, "bar", "baz");
 549             assertEquals(r, "foo", "failing compareAndExchange String");
 550             String x = (String) vh.get(recv);
 551             assertEquals(x, "foo", "failing compareAndExchange String value");
 552         }
 553 
 554         {
 555             String r = (String) vh.compareAndExchangeAcquire(recv, "foo", "bar");
 556             assertEquals(r, "foo", "success compareAndExchangeAcquire String");
 557             String x = (String) vh.get(recv);
 558             assertEquals(x, "bar", "success compareAndExchangeAcquire String value");
 559         }
 560 
 561         {
 562             String r = (String) vh.compareAndExchangeAcquire(recv, "foo", "baz");
 563             assertEquals(r, "bar", "failing compareAndExchangeAcquire String");
 564             String x = (String) vh.get(recv);
 565             assertEquals(x, "bar", "failing compareAndExchangeAcquire String value");
 566         }
 567 
 568         {
 569             String r = (String) vh.compareAndExchangeRelease(recv, "bar", "foo");
 570             assertEquals(r, "bar", "success compareAndExchangeRelease String");
 571             String x = (String) vh.get(recv);
 572             assertEquals(x, "foo", "success compareAndExchangeRelease String value");
 573         }
 574 
 575         {
 576             String r = (String) vh.compareAndExchangeRelease(recv, "bar", "baz");
 577             assertEquals(r, "foo", "failing compareAndExchangeRelease String");
 578             String x = (String) vh.get(recv);
 579             assertEquals(x, "foo", "failing compareAndExchangeRelease String value");
 580         }
 581 
 582         {
 583             boolean success = false;
 584             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 585                 success = vh.weakCompareAndSetPlain(recv, "foo", "bar");
 586                 if (!success) weakDelay();
 587             }
 588             assertEquals(success, true, "success weakCompareAndSetPlain String");
 589             String x = (String) vh.get(recv);
 590             assertEquals(x, "bar", "success weakCompareAndSetPlain String value");
 591         }
 592 
 593         {
 594             boolean success = vh.weakCompareAndSetPlain(recv, "foo", "baz");
 595             assertEquals(success, false, "failing weakCompareAndSetPlain String");
 596             String x = (String) vh.get(recv);
 597             assertEquals(x, "bar", "failing weakCompareAndSetPlain String value");
 598         }
 599 
 600         {
 601             boolean success = false;
 602             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 603                 success = vh.weakCompareAndSetAcquire(recv, "bar", "foo");
 604                 if (!success) weakDelay();
 605             }
 606             assertEquals(success, true, "success weakCompareAndSetAcquire String");
 607             String x = (String) vh.get(recv);
 608             assertEquals(x, "foo", "success weakCompareAndSetAcquire String");
 609         }
 610 
 611         {
 612             boolean success = vh.weakCompareAndSetAcquire(recv, "bar", "baz");
 613             assertEquals(success, false, "failing weakCompareAndSetAcquire String");
 614             String x = (String) vh.get(recv);
 615             assertEquals(x, "foo", "failing weakCompareAndSetAcquire String value");
 616         }
 617 
 618         {
 619             boolean success = false;
 620             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 621                 success = vh.weakCompareAndSetRelease(recv, "foo", "bar");
 622                 if (!success) weakDelay();
 623             }
 624             assertEquals(success, true, "success weakCompareAndSetRelease String");
 625             String x = (String) vh.get(recv);
 626             assertEquals(x, "bar", "success weakCompareAndSetRelease String");
 627         }
 628 
 629         {
 630             boolean success = vh.weakCompareAndSetRelease(recv, "foo", "baz");
 631             assertEquals(success, false, "failing weakCompareAndSetRelease String");
 632             String x = (String) vh.get(recv);
 633             assertEquals(x, "bar", "failing weakCompareAndSetRelease String value");
 634         }
 635 
 636         {
 637             boolean success = false;
 638             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 639                 success = vh.weakCompareAndSet(recv, "bar", "foo");
 640                 if (!success) weakDelay();
 641             }
 642             assertEquals(success, true, "success weakCompareAndSet String");
 643             String x = (String) vh.get(recv);
 644             assertEquals(x, "foo", "success weakCompareAndSet String value");
 645         }
 646 
 647         {
 648             boolean success = vh.weakCompareAndSet(recv, "bar", "baz");
 649             assertEquals(success, false, "failing weakCompareAndSet String");
 650             String x = (String) vh.get(recv);
 651             assertEquals(x, "foo", "failing weakCompareAndSet String value");
 652         }
 653 
 654         // Compare set and get
 655         {
 656             vh.set(recv, "foo");
 657 
 658             String o = (String) vh.getAndSet(recv, "bar");
 659             assertEquals(o, "foo", "getAndSet String");
 660             String x = (String) vh.get(recv);
 661             assertEquals(x, "bar", "getAndSet String value");
 662         }
 663 
 664         {
 665             vh.set(recv, "foo");
 666 
 667             String o = (String) vh.getAndSetAcquire(recv, "bar");
 668             assertEquals(o, "foo", "getAndSetAcquire String");
 669             String x = (String) vh.get(recv);
 670             assertEquals(x, "bar", "getAndSetAcquire String value");
 671         }
 672 
 673         {
 674             vh.set(recv, "foo");
 675 
 676             String o = (String) vh.getAndSetRelease(recv, "bar");
 677             assertEquals(o, "foo", "getAndSetRelease String");
 678             String x = (String) vh.get(recv);
 679             assertEquals(x, "bar", "getAndSetRelease String value");
 680         }
 681 
 682 
 683     }
 684 
 685     static void testInstanceFieldUnsupported(VarHandleTestAccessString recv, VarHandle vh) {
 686 
 687         checkUOE(() -> {
 688             String o = (String) vh.getAndAdd(recv, "foo");
 689         });
 690 
 691         checkUOE(() -> {
 692             String o = (String) vh.getAndAddAcquire(recv, "foo");
 693         });
 694 
 695         checkUOE(() -> {
 696             String o = (String) vh.getAndAddRelease(recv, "foo");
 697         });
 698 
 699         checkUOE(() -> {
 700             String o = (String) vh.getAndBitwiseOr(recv, "foo");
 701         });
 702 
 703         checkUOE(() -> {
 704             String o = (String) vh.getAndBitwiseOrAcquire(recv, "foo");
 705         });
 706 
 707         checkUOE(() -> {
 708             String o = (String) vh.getAndBitwiseOrRelease(recv, "foo");
 709         });
 710 
 711         checkUOE(() -> {
 712             String o = (String) vh.getAndBitwiseAnd(recv, "foo");
 713         });
 714 
 715         checkUOE(() -> {
 716             String o = (String) vh.getAndBitwiseAndAcquire(recv, "foo");
 717         });
 718 
 719         checkUOE(() -> {
 720             String o = (String) vh.getAndBitwiseAndRelease(recv, "foo");
 721         });
 722 
 723         checkUOE(() -> {
 724             String o = (String) vh.getAndBitwiseXor(recv, "foo");
 725         });
 726 
 727         checkUOE(() -> {
 728             String o = (String) vh.getAndBitwiseXorAcquire(recv, "foo");
 729         });
 730 
 731         checkUOE(() -> {
 732             String o = (String) vh.getAndBitwiseXorRelease(recv, "foo");
 733         });
 734     }
 735 
 736 
 737     static void testStaticField(VarHandle vh) {
 738         // Plain
 739         {
 740             vh.set("foo");
 741             String x = (String) vh.get();
 742             assertEquals(x, "foo", "set String value");
 743         }
 744 
 745 
 746         // Volatile
 747         {
 748             vh.setVolatile("bar");
 749             String x = (String) vh.getVolatile();
 750             assertEquals(x, "bar", "setVolatile String value");
 751         }
 752 
 753         // Lazy
 754         {
 755             vh.setRelease("foo");
 756             String x = (String) vh.getAcquire();
 757             assertEquals(x, "foo", "setRelease String value");
 758         }
 759 
 760         // Opaque
 761         {
 762             vh.setOpaque("bar");
 763             String x = (String) vh.getOpaque();
 764             assertEquals(x, "bar", "setOpaque String value");
 765         }
 766 
 767         vh.set("foo");
 768 
 769         // Compare
 770         {
 771             boolean r = vh.compareAndSet("foo", "bar");
 772             assertEquals(r, true, "success compareAndSet String");
 773             String x = (String) vh.get();
 774             assertEquals(x, "bar", "success compareAndSet String value");
 775         }
 776 
 777         {
 778             boolean r = vh.compareAndSet("foo", "baz");
 779             assertEquals(r, false, "failing compareAndSet String");
 780             String x = (String) vh.get();
 781             assertEquals(x, "bar", "failing compareAndSet String value");
 782         }
 783 
 784         {
 785             String r = (String) vh.compareAndExchange("bar", "foo");
 786             assertEquals(r, "bar", "success compareAndExchange String");
 787             String x = (String) vh.get();
 788             assertEquals(x, "foo", "success compareAndExchange String value");
 789         }
 790 
 791         {
 792             String r = (String) vh.compareAndExchange("bar", "baz");
 793             assertEquals(r, "foo", "failing compareAndExchange String");
 794             String x = (String) vh.get();
 795             assertEquals(x, "foo", "failing compareAndExchange String value");
 796         }
 797 
 798         {
 799             String r = (String) vh.compareAndExchangeAcquire("foo", "bar");
 800             assertEquals(r, "foo", "success compareAndExchangeAcquire String");
 801             String x = (String) vh.get();
 802             assertEquals(x, "bar", "success compareAndExchangeAcquire String value");
 803         }
 804 
 805         {
 806             String r = (String) vh.compareAndExchangeAcquire("foo", "baz");
 807             assertEquals(r, "bar", "failing compareAndExchangeAcquire String");
 808             String x = (String) vh.get();
 809             assertEquals(x, "bar", "failing compareAndExchangeAcquire String value");
 810         }
 811 
 812         {
 813             String r = (String) vh.compareAndExchangeRelease("bar", "foo");
 814             assertEquals(r, "bar", "success compareAndExchangeRelease String");
 815             String x = (String) vh.get();
 816             assertEquals(x, "foo", "success compareAndExchangeRelease String value");
 817         }
 818 
 819         {
 820             String r = (String) vh.compareAndExchangeRelease("bar", "baz");
 821             assertEquals(r, "foo", "failing compareAndExchangeRelease String");
 822             String x = (String) vh.get();
 823             assertEquals(x, "foo", "failing compareAndExchangeRelease String value");
 824         }
 825 
 826         {
 827             boolean success = false;
 828             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 829                 success = vh.weakCompareAndSetPlain("foo", "bar");
 830                 if (!success) weakDelay();
 831             }
 832             assertEquals(success, true, "success weakCompareAndSetPlain String");
 833             String x = (String) vh.get();
 834             assertEquals(x, "bar", "success weakCompareAndSetPlain String value");
 835         }
 836 
 837         {
 838             boolean success = vh.weakCompareAndSetPlain("foo", "baz");
 839             assertEquals(success, false, "failing weakCompareAndSetPlain String");
 840             String x = (String) vh.get();
 841             assertEquals(x, "bar", "failing weakCompareAndSetPlain String value");
 842         }
 843 
 844         {
 845             boolean success = false;
 846             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 847                 success = vh.weakCompareAndSetAcquire("bar", "foo");
 848                 if (!success) weakDelay();
 849             }
 850             assertEquals(success, true, "success weakCompareAndSetAcquire String");
 851             String x = (String) vh.get();
 852             assertEquals(x, "foo", "success weakCompareAndSetAcquire String");
 853         }
 854 
 855         {
 856             boolean success = vh.weakCompareAndSetAcquire("bar", "baz");
 857             assertEquals(success, false, "failing weakCompareAndSetAcquire String");
 858             String x = (String) vh.get();
 859             assertEquals(x, "foo", "failing weakCompareAndSetAcquire String value");
 860         }
 861 
 862         {
 863             boolean success = false;
 864             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 865                 success = vh.weakCompareAndSetRelease("foo", "bar");
 866                 if (!success) weakDelay();
 867             }
 868             assertEquals(success, true, "success weakCompareAndSetRelease String");
 869             String x = (String) vh.get();
 870             assertEquals(x, "bar", "success weakCompareAndSetRelease String");
 871         }
 872 
 873         {
 874             boolean success = vh.weakCompareAndSetRelease("foo", "baz");
 875             assertEquals(success, false, "failing weakCompareAndSetRelease String");
 876             String x = (String) vh.get();
 877             assertEquals(x, "bar", "failing weakCompareAndSetRelease String value");
 878         }
 879 
 880         {
 881             boolean success = false;
 882             for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
 883                 success = vh.weakCompareAndSet("bar", "foo");
 884                 if (!success) weakDelay();
 885             }
 886             assertEquals(success, true, "success weakCompareAndSet String");
 887             String x = (String) vh.get();
 888             assertEquals(x, "foo", "success weakCompareAndSet String");
 889         }
 890 
 891         {
 892             boolean success = vh.weakCompareAndSet("bar", "baz");
 893             assertEquals(success, false, "failing weakCompareAndSet String");
 894             String x = (String) vh.get();
 895             assertEquals(x, "foo", "failing weakCompareAndSet String value");
 896         }
 897 
 898         // Compare set and get
 899         {
 900             vh.set("foo");
 901 
 902             String o = (String) vh.getAndSet("bar");
 903             assertEquals(o, "foo", "getAndSet String");
 904             String x = (String) vh.get();
 905             assertEquals(x, "bar", "getAndSet String value");
 906         }
 907 
 908         {
 909             vh.set("foo");
 910 
 911             String o = (String) vh.getAndSetAcquire("bar");
 912             assertEquals(o, "foo", "getAndSetAcquire String");
 913             String x = (String) vh.get();
 914             assertEquals(x, "bar", "getAndSetAcquire String value");
 915         }
 916 
 917         {
 918             vh.set("foo");
 919 
 920             String o = (String) vh.getAndSetRelease("bar");
 921             assertEquals(o, "foo", "getAndSetRelease String");
 922             String x = (String) vh.get();
 923             assertEquals(x, "bar", "getAndSetRelease String value");
 924         }
 925 
 926 
 927     }
 928 
 929     static void testStaticFieldUnsupported(VarHandle vh) {
 930 
 931         checkUOE(() -> {
 932             String o = (String) vh.getAndAdd("foo");
 933         });
 934 
 935         checkUOE(() -> {
 936             String o = (String) vh.getAndAddAcquire("foo");
 937         });
 938 
 939         checkUOE(() -> {
 940             String o = (String) vh.getAndAddRelease("foo");
 941         });
 942 
 943         checkUOE(() -> {
 944             String o = (String) vh.getAndBitwiseOr("foo");
 945         });
 946 
 947         checkUOE(() -> {
 948             String o = (String) vh.getAndBitwiseOrAcquire("foo");
 949         });
 950 
 951         checkUOE(() -> {
 952             String o = (String) vh.getAndBitwiseOrRelease("foo");
 953         });
 954 
 955         checkUOE(() -> {
 956             String o = (String) vh.getAndBitwiseAnd("foo");
 957         });
 958 
 959         checkUOE(() -> {
 960             String o = (String) vh.getAndBitwiseAndAcquire("foo");
 961         });
 962 
 963         checkUOE(() -> {
 964             String o = (String) vh.getAndBitwiseAndRelease("foo");
 965         });
 966 
 967         checkUOE(() -> {
 968             String o = (String) vh.getAndBitwiseXor("foo");
 969         });
 970 
 971         checkUOE(() -> {
 972             String o = (String) vh.getAndBitwiseXorAcquire("foo");
 973         });
 974 
 975         checkUOE(() -> {
 976             String o = (String) vh.getAndBitwiseXorRelease("foo");
 977         });
 978     }
 979 
 980 
 981     static void testArray(VarHandle vh) {
 982         String[] array = new String[10];
 983 
 984         for (int i = 0; i < array.length; i++) {
 985             // Plain
 986             {
 987                 vh.set(array, i, "foo");
 988                 String x = (String) vh.get(array, i);
 989                 assertEquals(x, "foo", "get String value");
 990             }
 991 
 992 
 993             // Volatile
 994             {
 995                 vh.setVolatile(array, i, "bar");
 996                 String x = (String) vh.getVolatile(array, i);
 997                 assertEquals(x, "bar", "setVolatile String value");
 998             }
 999 
1000             // Lazy
1001             {
1002                 vh.setRelease(array, i, "foo");
1003                 String x = (String) vh.getAcquire(array, i);
1004                 assertEquals(x, "foo", "setRelease String value");
1005             }
1006 
1007             // Opaque
1008             {
1009                 vh.setOpaque(array, i, "bar");
1010                 String x = (String) vh.getOpaque(array, i);
1011                 assertEquals(x, "bar", "setOpaque String value");
1012             }
1013 
1014             vh.set(array, i, "foo");
1015 
1016             // Compare
1017             {
1018                 boolean r = vh.compareAndSet(array, i, "foo", "bar");
1019                 assertEquals(r, true, "success compareAndSet String");
1020                 String x = (String) vh.get(array, i);
1021                 assertEquals(x, "bar", "success compareAndSet String value");
1022             }
1023 
1024             {
1025                 boolean r = vh.compareAndSet(array, i, "foo", "baz");
1026                 assertEquals(r, false, "failing compareAndSet String");
1027                 String x = (String) vh.get(array, i);
1028                 assertEquals(x, "bar", "failing compareAndSet String value");
1029             }
1030 
1031             {
1032                 String r = (String) vh.compareAndExchange(array, i, "bar", "foo");
1033                 assertEquals(r, "bar", "success compareAndExchange String");
1034                 String x = (String) vh.get(array, i);
1035                 assertEquals(x, "foo", "success compareAndExchange String value");
1036             }
1037 
1038             {
1039                 String r = (String) vh.compareAndExchange(array, i, "bar", "baz");
1040                 assertEquals(r, "foo", "failing compareAndExchange String");
1041                 String x = (String) vh.get(array, i);
1042                 assertEquals(x, "foo", "failing compareAndExchange String value");
1043             }
1044 
1045             {
1046                 String r = (String) vh.compareAndExchangeAcquire(array, i, "foo", "bar");
1047                 assertEquals(r, "foo", "success compareAndExchangeAcquire String");
1048                 String x = (String) vh.get(array, i);
1049                 assertEquals(x, "bar", "success compareAndExchangeAcquire String value");
1050             }
1051 
1052             {
1053                 String r = (String) vh.compareAndExchangeAcquire(array, i, "foo", "baz");
1054                 assertEquals(r, "bar", "failing compareAndExchangeAcquire String");
1055                 String x = (String) vh.get(array, i);
1056                 assertEquals(x, "bar", "failing compareAndExchangeAcquire String value");
1057             }
1058 
1059             {
1060                 String r = (String) vh.compareAndExchangeRelease(array, i, "bar", "foo");
1061                 assertEquals(r, "bar", "success compareAndExchangeRelease String");
1062                 String x = (String) vh.get(array, i);
1063                 assertEquals(x, "foo", "success compareAndExchangeRelease String value");
1064             }
1065 
1066             {
1067                 String r = (String) vh.compareAndExchangeRelease(array, i, "bar", "baz");
1068                 assertEquals(r, "foo", "failing compareAndExchangeRelease String");
1069                 String x = (String) vh.get(array, i);
1070                 assertEquals(x, "foo", "failing compareAndExchangeRelease String value");
1071             }
1072 
1073             {
1074                 boolean success = false;
1075                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1076                     success = vh.weakCompareAndSetPlain(array, i, "foo", "bar");
1077                     if (!success) weakDelay();
1078                 }
1079                 assertEquals(success, true, "success weakCompareAndSetPlain String");
1080                 String x = (String) vh.get(array, i);
1081                 assertEquals(x, "bar", "success weakCompareAndSetPlain String value");
1082             }
1083 
1084             {
1085                 boolean success = vh.weakCompareAndSetPlain(array, i, "foo", "baz");
1086                 assertEquals(success, false, "failing weakCompareAndSetPlain String");
1087                 String x = (String) vh.get(array, i);
1088                 assertEquals(x, "bar", "failing weakCompareAndSetPlain String value");
1089             }
1090 
1091             {
1092                 boolean success = false;
1093                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1094                     success = vh.weakCompareAndSetAcquire(array, i, "bar", "foo");
1095                     if (!success) weakDelay();
1096                 }
1097                 assertEquals(success, true, "success weakCompareAndSetAcquire String");
1098                 String x = (String) vh.get(array, i);
1099                 assertEquals(x, "foo", "success weakCompareAndSetAcquire String");
1100             }
1101 
1102             {
1103                 boolean success = vh.weakCompareAndSetAcquire(array, i, "bar", "baz");
1104                 assertEquals(success, false, "failing weakCompareAndSetAcquire String");
1105                 String x = (String) vh.get(array, i);
1106                 assertEquals(x, "foo", "failing weakCompareAndSetAcquire String value");
1107             }
1108 
1109             {
1110                 boolean success = false;
1111                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1112                     success = vh.weakCompareAndSetRelease(array, i, "foo", "bar");
1113                     if (!success) weakDelay();
1114                 }
1115                 assertEquals(success, true, "success weakCompareAndSetRelease String");
1116                 String x = (String) vh.get(array, i);
1117                 assertEquals(x, "bar", "success weakCompareAndSetRelease String");
1118             }
1119 
1120             {
1121                 boolean success = vh.weakCompareAndSetRelease(array, i, "foo", "baz");
1122                 assertEquals(success, false, "failing weakCompareAndSetRelease String");
1123                 String x = (String) vh.get(array, i);
1124                 assertEquals(x, "bar", "failing weakCompareAndSetRelease String value");
1125             }
1126 
1127             {
1128                 boolean success = false;
1129                 for (int c = 0; c < WEAK_ATTEMPTS && !success; c++) {
1130                     success = vh.weakCompareAndSet(array, i, "bar", "foo");
1131                     if (!success) weakDelay();
1132                 }
1133                 assertEquals(success, true, "success weakCompareAndSet String");
1134                 String x = (String) vh.get(array, i);
1135                 assertEquals(x, "foo", "success weakCompareAndSet String");
1136             }
1137 
1138             {
1139                 boolean success = vh.weakCompareAndSet(array, i, "bar", "baz");
1140                 assertEquals(success, false, "failing weakCompareAndSet String");
1141                 String x = (String) vh.get(array, i);
1142                 assertEquals(x, "foo", "failing weakCompareAndSet String value");
1143             }
1144 
1145             // Compare set and get
1146             {
1147                 vh.set(array, i, "foo");
1148 
1149                 String o = (String) vh.getAndSet(array, i, "bar");
1150                 assertEquals(o, "foo", "getAndSet String");
1151                 String x = (String) vh.get(array, i);
1152                 assertEquals(x, "bar", "getAndSet String value");
1153             }
1154 
1155             {
1156                 vh.set(array, i, "foo");
1157 
1158                 String o = (String) vh.getAndSetAcquire(array, i, "bar");
1159                 assertEquals(o, "foo", "getAndSetAcquire String");
1160                 String x = (String) vh.get(array, i);
1161                 assertEquals(x, "bar", "getAndSetAcquire String value");
1162             }
1163 
1164             {
1165                 vh.set(array, i, "foo");
1166 
1167                 String o = (String) vh.getAndSetRelease(array, i, "bar");
1168                 assertEquals(o, "foo", "getAndSetRelease String");
1169                 String x = (String) vh.get(array, i);
1170                 assertEquals(x, "bar", "getAndSetRelease String value");
1171             }
1172 
1173 
1174         }
1175     }
1176 
1177     static void testArrayUnsupported(VarHandle vh) {
1178         String[] array = new String[10];
1179 
1180         int i = 0;
1181 
1182         checkUOE(() -> {
1183             String o = (String) vh.getAndAdd(array, i, "foo");
1184         });
1185 
1186         checkUOE(() -> {
1187             String o = (String) vh.getAndAddAcquire(array, i, "foo");
1188         });
1189 
1190         checkUOE(() -> {
1191             String o = (String) vh.getAndAddRelease(array, i, "foo");
1192         });
1193 
1194         checkUOE(() -> {
1195             String o = (String) vh.getAndBitwiseOr(array, i, "foo");
1196         });
1197 
1198         checkUOE(() -> {
1199             String o = (String) vh.getAndBitwiseOrAcquire(array, i, "foo");
1200         });
1201 
1202         checkUOE(() -> {
1203             String o = (String) vh.getAndBitwiseOrRelease(array, i, "foo");
1204         });
1205 
1206         checkUOE(() -> {
1207             String o = (String) vh.getAndBitwiseAnd(array, i, "foo");
1208         });
1209 
1210         checkUOE(() -> {
1211             String o = (String) vh.getAndBitwiseAndAcquire(array, i, "foo");
1212         });
1213 
1214         checkUOE(() -> {
1215             String o = (String) vh.getAndBitwiseAndRelease(array, i, "foo");
1216         });
1217 
1218         checkUOE(() -> {
1219             String o = (String) vh.getAndBitwiseXor(array, i, "foo");
1220         });
1221 
1222         checkUOE(() -> {
1223             String o = (String) vh.getAndBitwiseXorAcquire(array, i, "foo");
1224         });
1225 
1226         checkUOE(() -> {
1227             String o = (String) vh.getAndBitwiseXorRelease(array, i, "foo");
1228         });
1229     }
1230 
1231     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
1232         String[] array = new String[10];
1233 
1234         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
1235             final int ci = i;
1236 
1237             checkAIOOBE(() -> {
1238                 String x = (String) vh.get(array, ci);
1239             });
1240 
1241             checkAIOOBE(() -> {
1242                 vh.set(array, ci, "foo");
1243             });
1244 
1245             checkAIOOBE(() -> {
1246                 String x = (String) vh.getVolatile(array, ci);
1247             });
1248 
1249             checkAIOOBE(() -> {
1250                 vh.setVolatile(array, ci, "foo");
1251             });
1252 
1253             checkAIOOBE(() -> {
1254                 String x = (String) vh.getAcquire(array, ci);
1255             });
1256 
1257             checkAIOOBE(() -> {
1258                 vh.setRelease(array, ci, "foo");
1259             });
1260 
1261             checkAIOOBE(() -> {
1262                 String x = (String) vh.getOpaque(array, ci);
1263             });
1264 
1265             checkAIOOBE(() -> {
1266                 vh.setOpaque(array, ci, "foo");
1267             });
1268 
1269             checkAIOOBE(() -> {
1270                 boolean r = vh.compareAndSet(array, ci, "foo", "bar");
1271             });
1272 
1273             checkAIOOBE(() -> {
1274                 String r = (String) vh.compareAndExchange(array, ci, "bar", "foo");
1275             });
1276 
1277             checkAIOOBE(() -> {
1278                 String r = (String) vh.compareAndExchangeAcquire(array, ci, "bar", "foo");
1279             });
1280 
1281             checkAIOOBE(() -> {
1282                 String r = (String) vh.compareAndExchangeRelease(array, ci, "bar", "foo");
1283             });
1284 
1285             checkAIOOBE(() -> {
1286                 boolean r = vh.weakCompareAndSetPlain(array, ci, "foo", "bar");
1287             });
1288 
1289             checkAIOOBE(() -> {
1290                 boolean r = vh.weakCompareAndSet(array, ci, "foo", "bar");
1291             });
1292 
1293             checkAIOOBE(() -> {
1294                 boolean r = vh.weakCompareAndSetAcquire(array, ci, "foo", "bar");
1295             });
1296 
1297             checkAIOOBE(() -> {
1298                 boolean r = vh.weakCompareAndSetRelease(array, ci, "foo", "bar");
1299             });
1300 
1301             checkAIOOBE(() -> {
1302                 String o = (String) vh.getAndSet(array, ci, "foo");
1303             });
1304 
1305             checkAIOOBE(() -> {
1306                 String o = (String) vh.getAndSetAcquire(array, ci, "foo");
1307             });
1308 
1309             checkAIOOBE(() -> {
1310                 String o = (String) vh.getAndSetRelease(array, ci, "foo");
1311             });
1312 
1313 
1314         }
1315     }
1316 
1317     static void testArrayStoreException(VarHandle vh) throws Throwable {
1318         Object[] array = new String[10];
1319         Arrays.fill(array, "foo");
1320         Object value = new Object();
1321 
1322         // Set
1323         checkASE(() -> {
1324             vh.set(array, 0, value);
1325         });
1326 
1327         // SetVolatile
1328         checkASE(() -> {
1329             vh.setVolatile(array, 0, value);
1330         });
1331 
1332         // SetOpaque
1333         checkASE(() -> {
1334             vh.setOpaque(array, 0, value);
1335         });
1336 
1337         // SetRelease
1338         checkASE(() -> {
1339             vh.setRelease(array, 0, value);
1340         });
1341 
1342         // CompareAndSet
1343         checkASE(() -> { // receiver reference class
1344             boolean r = vh.compareAndSet(array, 0, "foo", value);
1345         });
1346 
1347         // WeakCompareAndSet
1348         checkASE(() -> { // receiver reference class
1349             boolean r = vh.weakCompareAndSetPlain(array, 0, "foo", value);
1350         });
1351 
1352         // WeakCompareAndSetVolatile
1353         checkASE(() -> { // receiver reference class
1354             boolean r = vh.weakCompareAndSet(array, 0, "foo", value);
1355         });
1356 
1357         // WeakCompareAndSetAcquire
1358         checkASE(() -> { // receiver reference class
1359             boolean r = vh.weakCompareAndSetAcquire(array, 0, "foo", value);
1360         });
1361 
1362         // WeakCompareAndSetRelease
1363         checkASE(() -> { // receiver reference class
1364             boolean r = vh.weakCompareAndSetRelease(array, 0, "foo", value);
1365         });
1366 
1367         // CompareAndExchange
1368         checkASE(() -> { // receiver reference class
1369             String x = (String) vh.compareAndExchange(array, 0, "foo", value);
1370         });
1371 
1372         // CompareAndExchangeAcquire
1373         checkASE(() -> { // receiver reference class
1374             String x = (String) vh.compareAndExchangeAcquire(array, 0, "foo", value);
1375         });
1376 
1377         // CompareAndExchangeRelease
1378         checkASE(() -> { // receiver reference class
1379             String x = (String) vh.compareAndExchangeRelease(array, 0, "foo", value);
1380         });
1381 
1382         // GetAndSet
1383         checkASE(() -> { // receiver reference class
1384             String x = (String) vh.getAndSet(array, 0, value);
1385         });
1386 
1387         // GetAndSetAcquire
1388         checkASE(() -> { // receiver reference class
1389             String x = (String) vh.getAndSetAcquire(array, 0, value);
1390         });
1391 
1392         // GetAndSetRelease
1393         checkASE(() -> { // receiver reference class
1394             String x = (String) vh.getAndSetRelease(array, 0, value);
1395         });
1396     }
1397 }
1398