1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @run testng/othervm -Diters=10    -Xint                   VarHandleTestAccessBoolean
  27  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessBoolean
  28  * @run testng/othervm -Diters=20000                         VarHandleTestAccessBoolean
  29  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccessBoolean
  30  */
  31 
  32 import org.testng.annotations.BeforeClass;
  33 import org.testng.annotations.DataProvider;
  34 import org.testng.annotations.Test;
  35 
  36 import java.lang.invoke.MethodHandles;
  37 import java.lang.invoke.VarHandle;
  38 import java.util.ArrayList;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 
  42 import static org.testng.Assert.*;
  43 
  44 public class VarHandleTestAccessBoolean extends VarHandleBaseTest {
  45     static final boolean static_final_v = true;
  46 
  47     static boolean static_v;
  48 
  49     final boolean final_v = true;
  50 
  51     boolean v;
  52 
  53     VarHandle vhFinalField;
  54 
  55     VarHandle vhField;
  56 
  57     VarHandle vhStaticField;
  58 
  59     VarHandle vhStaticFinalField;
  60 
  61     VarHandle vhArray;
  62 
  63     @BeforeClass
  64     public void setup() throws Exception {
  65         vhFinalField = MethodHandles.lookup().findVarHandle(
  66                 VarHandleTestAccessBoolean.class, "final_v", boolean.class);
  67 
  68         vhField = MethodHandles.lookup().findVarHandle(
  69                 VarHandleTestAccessBoolean.class, "v", boolean.class);
  70 
  71         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  72             VarHandleTestAccessBoolean.class, "static_final_v", boolean.class);
  73 
  74         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  75             VarHandleTestAccessBoolean.class, "static_v", boolean.class);
  76 
  77         vhArray = MethodHandles.arrayElementVarHandle(boolean[].class);
  78     }
  79 
  80 
  81     @DataProvider
  82     public Object[][] varHandlesProvider() throws Exception {
  83         List<VarHandle> vhs = new ArrayList<>();
  84         vhs.add(vhField);
  85         vhs.add(vhStaticField);
  86         vhs.add(vhArray);
  87 
  88         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
  89     }
  90 
  91     @Test(dataProvider = "varHandlesProvider")
  92     public void testIsAccessModeSupported(VarHandle vh) {
  93         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
  94         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
  95         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
  96         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
  97         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
  98         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
  99         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 100         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 101 
 102         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 103         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_VOLATILE));
 104         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 105         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 106         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 107         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_VOLATILE));
 108         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));
 109         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE));
 110         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_SET));
 111 
 112         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_ADD));
 113         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.ADD_AND_GET));
 114     }
 115 
 116 
 117     @DataProvider
 118     public Object[][] typesProvider() throws Exception {
 119         List<Object[]> types = new ArrayList<>();
 120         types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessBoolean.class)});
 121         types.add(new Object[] {vhStaticField, Arrays.asList()});
 122         types.add(new Object[] {vhArray, Arrays.asList(boolean[].class, int.class)});
 123 
 124         return types.stream().toArray(Object[][]::new);
 125     }
 126 
 127     @Test(dataProvider = "typesProvider")
 128     public void testTypes(VarHandle vh, List<Class<?>> pts) {
 129         assertEquals(vh.varType(), boolean.class);
 130 
 131         assertEquals(vh.coordinateTypes(), pts);
 132 
 133         testTypes(vh);
 134     }
 135 
 136 
 137     @Test
 138     public void testLookupInstanceToStatic() {
 139         checkIAE("Lookup of static final field to instance final field", () -> {
 140             MethodHandles.lookup().findStaticVarHandle(
 141                     VarHandleTestAccessBoolean.class, "final_v", boolean.class);
 142         });
 143 
 144         checkIAE("Lookup of static field to instance field", () -> {
 145             MethodHandles.lookup().findStaticVarHandle(
 146                     VarHandleTestAccessBoolean.class, "v", boolean.class);
 147         });
 148     }
 149 
 150     @Test
 151     public void testLookupStaticToInstance() {
 152         checkIAE("Lookup of instance final field to static final field", () -> {
 153             MethodHandles.lookup().findVarHandle(
 154                 VarHandleTestAccessBoolean.class, "static_final_v", boolean.class);
 155         });
 156 
 157         checkIAE("Lookup of instance field to static field", () -> {
 158             vhStaticField = MethodHandles.lookup().findVarHandle(
 159                 VarHandleTestAccessBoolean.class, "static_v", boolean.class);
 160         });
 161     }
 162 
 163 
 164     @DataProvider
 165     public Object[][] accessTestCaseProvider() throws Exception {
 166         List<AccessTestCase<?>> cases = new ArrayList<>();
 167 
 168         cases.add(new VarHandleAccessTestCase("Instance final field",
 169                                               vhFinalField, vh -> testInstanceFinalField(this, vh)));
 170         cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
 171                                               vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
 172                                               false));
 173 
 174         cases.add(new VarHandleAccessTestCase("Static final field",
 175                                               vhStaticFinalField, VarHandleTestAccessBoolean::testStaticFinalField));
 176         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 177                                               vhStaticFinalField, VarHandleTestAccessBoolean::testStaticFinalFieldUnsupported,
 178                                               false));
 179 
 180         cases.add(new VarHandleAccessTestCase("Instance field",
 181                                               vhField, vh -> testInstanceField(this, vh)));
 182         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 183                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 184                                               false));
 185 
 186         cases.add(new VarHandleAccessTestCase("Static field",
 187                                               vhStaticField, VarHandleTestAccessBoolean::testStaticField));
 188         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 189                                               vhStaticField, VarHandleTestAccessBoolean::testStaticFieldUnsupported,
 190                                               false));
 191 
 192         cases.add(new VarHandleAccessTestCase("Array",
 193                                               vhArray, VarHandleTestAccessBoolean::testArray));
 194         cases.add(new VarHandleAccessTestCase("Array unsupported",
 195                                               vhArray, VarHandleTestAccessBoolean::testArrayUnsupported,
 196                                               false));
 197         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 198                                               vhArray, VarHandleTestAccessBoolean::testArrayIndexOutOfBounds,
 199                                               false));
 200 
 201         // Work around issue with jtreg summary reporting which truncates
 202         // the String result of Object.toString to 30 characters, hence
 203         // the first dummy argument
 204         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 205     }
 206 
 207     @Test(dataProvider = "accessTestCaseProvider")
 208     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 209         T t = atc.get();
 210         int iters = atc.requiresLoop() ? ITERS : 1;
 211         for (int c = 0; c < iters; c++) {
 212             atc.testAccess(t);
 213         }
 214     }
 215 
 216 
 217 
 218 
 219     static void testInstanceFinalField(VarHandleTestAccessBoolean recv, VarHandle vh) {
 220         // Plain
 221         {
 222             boolean x = (boolean) vh.get(recv);
 223             assertEquals(x, true, "get boolean value");
 224         }
 225 
 226 
 227         // Volatile
 228         {
 229             boolean x = (boolean) vh.getVolatile(recv);
 230             assertEquals(x, true, "getVolatile boolean value");
 231         }
 232 
 233         // Lazy
 234         {
 235             boolean x = (boolean) vh.getAcquire(recv);
 236             assertEquals(x, true, "getRelease boolean value");
 237         }
 238 
 239         // Opaque
 240         {
 241             boolean x = (boolean) vh.getOpaque(recv);
 242             assertEquals(x, true, "getOpaque boolean value");
 243         }
 244     }
 245 
 246     static void testInstanceFinalFieldUnsupported(VarHandleTestAccessBoolean recv, VarHandle vh) {
 247         checkUOE(() -> {
 248             vh.set(recv, false);
 249         });
 250 
 251         checkUOE(() -> {
 252             vh.setVolatile(recv, false);
 253         });
 254 
 255         checkUOE(() -> {
 256             vh.setRelease(recv, false);
 257         });
 258 
 259         checkUOE(() -> {
 260             vh.setOpaque(recv, false);
 261         });
 262 
 263         checkUOE(() -> {
 264             boolean r = vh.compareAndSet(recv, true, false);
 265         });
 266 
 267         checkUOE(() -> {
 268             boolean r = (boolean) vh.compareAndExchangeVolatile(recv, true, false);
 269         });
 270 
 271         checkUOE(() -> {
 272             boolean r = (boolean) vh.compareAndExchangeAcquire(recv, true, false);
 273         });
 274 
 275         checkUOE(() -> {
 276             boolean r = (boolean) vh.compareAndExchangeRelease(recv, true, false);
 277         });
 278 
 279         checkUOE(() -> {
 280             boolean r = vh.weakCompareAndSet(recv, true, false);
 281         });
 282 
 283         checkUOE(() -> {
 284             boolean r = vh.weakCompareAndSetVolatile(recv, true, false);
 285         });
 286 
 287         checkUOE(() -> {
 288             boolean r = vh.weakCompareAndSetAcquire(recv, true, false);
 289         });
 290 
 291         checkUOE(() -> {
 292             boolean r = vh.weakCompareAndSetRelease(recv, true, false);
 293         });
 294 
 295         checkUOE(() -> {
 296             boolean o = (boolean) vh.getAndAdd(recv, true);
 297         });
 298 
 299         checkUOE(() -> {
 300             boolean o = (boolean) vh.addAndGet(recv, true);
 301         });
 302     }
 303 
 304 
 305     static void testStaticFinalField(VarHandle vh) {
 306         // Plain
 307         {
 308             boolean x = (boolean) vh.get();
 309             assertEquals(x, true, "get boolean value");
 310         }
 311 
 312 
 313         // Volatile
 314         {
 315             boolean x = (boolean) vh.getVolatile();
 316             assertEquals(x, true, "getVolatile boolean value");
 317         }
 318 
 319         // Lazy
 320         {
 321             boolean x = (boolean) vh.getAcquire();
 322             assertEquals(x, true, "getRelease boolean value");
 323         }
 324 
 325         // Opaque
 326         {
 327             boolean x = (boolean) vh.getOpaque();
 328             assertEquals(x, true, "getOpaque boolean value");
 329         }
 330     }
 331 
 332     static void testStaticFinalFieldUnsupported(VarHandle vh) {
 333         checkUOE(() -> {
 334             vh.set(false);
 335         });
 336 
 337         checkUOE(() -> {
 338             vh.setVolatile(false);
 339         });
 340 
 341         checkUOE(() -> {
 342             vh.setRelease(false);
 343         });
 344 
 345         checkUOE(() -> {
 346             vh.setOpaque(false);
 347         });
 348 
 349         checkUOE(() -> {
 350             boolean r = vh.compareAndSet(true, false);
 351         });
 352 
 353         checkUOE(() -> {
 354             boolean r = (boolean) vh.compareAndExchangeVolatile(true, false);
 355         });
 356 
 357         checkUOE(() -> {
 358             boolean r = (boolean) vh.compareAndExchangeAcquire(true, false);
 359         });
 360 
 361         checkUOE(() -> {
 362             boolean r = (boolean) vh.compareAndExchangeRelease(true, false);
 363         });
 364 
 365         checkUOE(() -> {
 366             boolean r = vh.weakCompareAndSet(true, false);
 367         });
 368 
 369         checkUOE(() -> {
 370             boolean r = vh.weakCompareAndSetVolatile(true, false);
 371         });
 372 
 373         checkUOE(() -> {
 374             boolean r = vh.weakCompareAndSetAcquire(true, false);
 375         });
 376 
 377         checkUOE(() -> {
 378             boolean r = vh.weakCompareAndSetRelease(true, false);
 379         });
 380 
 381         checkUOE(() -> {
 382             boolean o = (boolean) vh.getAndAdd(true);
 383         });
 384 
 385         checkUOE(() -> {
 386             boolean o = (boolean) vh.addAndGet(true);
 387         });
 388     }
 389 
 390 
 391     static void testInstanceField(VarHandleTestAccessBoolean recv, VarHandle vh) {
 392         // Plain
 393         {
 394             vh.set(recv, true);
 395             boolean x = (boolean) vh.get(recv);
 396             assertEquals(x, true, "set boolean value");
 397         }
 398 
 399 
 400         // Volatile
 401         {
 402             vh.setVolatile(recv, false);
 403             boolean x = (boolean) vh.getVolatile(recv);
 404             assertEquals(x, false, "setVolatile boolean value");
 405         }
 406 
 407         // Lazy
 408         {
 409             vh.setRelease(recv, true);
 410             boolean x = (boolean) vh.getAcquire(recv);
 411             assertEquals(x, true, "setRelease boolean value");
 412         }
 413 
 414         // Opaque
 415         {
 416             vh.setOpaque(recv, false);
 417             boolean x = (boolean) vh.getOpaque(recv);
 418             assertEquals(x, false, "setOpaque boolean value");
 419         }
 420 
 421 
 422     }
 423 
 424     static void testInstanceFieldUnsupported(VarHandleTestAccessBoolean recv, VarHandle vh) {
 425         checkUOE(() -> {
 426             boolean r = vh.compareAndSet(recv, true, false);
 427         });
 428 
 429         checkUOE(() -> {
 430             boolean r = (boolean) vh.compareAndExchangeVolatile(recv, true, false);
 431         });
 432 
 433         checkUOE(() -> {
 434             boolean r = (boolean) vh.compareAndExchangeAcquire(recv, true, false);
 435         });
 436 
 437         checkUOE(() -> {
 438             boolean r = (boolean) vh.compareAndExchangeRelease(recv, true, false);
 439         });
 440 
 441         checkUOE(() -> {
 442             boolean r = vh.weakCompareAndSet(recv, true, false);
 443         });
 444 
 445         checkUOE(() -> {
 446             boolean r = vh.weakCompareAndSetVolatile(recv, true, false);
 447         });
 448 
 449         checkUOE(() -> {
 450             boolean r = vh.weakCompareAndSetAcquire(recv, true, false);
 451         });
 452 
 453         checkUOE(() -> {
 454             boolean r = vh.weakCompareAndSetRelease(recv, true, false);
 455         });
 456 
 457         checkUOE(() -> {
 458             boolean o = (boolean) vh.getAndAdd(recv, true);
 459         });
 460 
 461         checkUOE(() -> {
 462             boolean o = (boolean) vh.addAndGet(recv, true);
 463         });
 464     }
 465 
 466 
 467     static void testStaticField(VarHandle vh) {
 468         // Plain
 469         {
 470             vh.set(true);
 471             boolean x = (boolean) vh.get();
 472             assertEquals(x, true, "set boolean value");
 473         }
 474 
 475 
 476         // Volatile
 477         {
 478             vh.setVolatile(false);
 479             boolean x = (boolean) vh.getVolatile();
 480             assertEquals(x, false, "setVolatile boolean value");
 481         }
 482 
 483         // Lazy
 484         {
 485             vh.setRelease(true);
 486             boolean x = (boolean) vh.getAcquire();
 487             assertEquals(x, true, "setRelease boolean value");
 488         }
 489 
 490         // Opaque
 491         {
 492             vh.setOpaque(false);
 493             boolean x = (boolean) vh.getOpaque();
 494             assertEquals(x, false, "setOpaque boolean value");
 495         }
 496 
 497 
 498     }
 499 
 500     static void testStaticFieldUnsupported(VarHandle vh) {
 501         checkUOE(() -> {
 502             boolean r = vh.compareAndSet(true, false);
 503         });
 504 
 505         checkUOE(() -> {
 506             boolean r = (boolean) vh.compareAndExchangeVolatile(true, false);
 507         });
 508 
 509         checkUOE(() -> {
 510             boolean r = (boolean) vh.compareAndExchangeAcquire(true, false);
 511         });
 512 
 513         checkUOE(() -> {
 514             boolean r = (boolean) vh.compareAndExchangeRelease(true, false);
 515         });
 516 
 517         checkUOE(() -> {
 518             boolean r = vh.weakCompareAndSet(true, false);
 519         });
 520 
 521         checkUOE(() -> {
 522             boolean r = vh.weakCompareAndSetVolatile(true, false);
 523         });
 524 
 525         checkUOE(() -> {
 526             boolean r = vh.weakCompareAndSetAcquire(true, false);
 527         });
 528 
 529         checkUOE(() -> {
 530             boolean r = vh.weakCompareAndSetRelease(true, false);
 531         });
 532 
 533         checkUOE(() -> {
 534             boolean o = (boolean) vh.getAndAdd(true);
 535         });
 536 
 537         checkUOE(() -> {
 538             boolean o = (boolean) vh.addAndGet(true);
 539         });
 540     }
 541 
 542 
 543     static void testArray(VarHandle vh) {
 544         boolean[] array = new boolean[10];
 545 
 546         for (int i = 0; i < array.length; i++) {
 547             // Plain
 548             {
 549                 vh.set(array, i, true);
 550                 boolean x = (boolean) vh.get(array, i);
 551                 assertEquals(x, true, "get boolean value");
 552             }
 553 
 554 
 555             // Volatile
 556             {
 557                 vh.setVolatile(array, i, false);
 558                 boolean x = (boolean) vh.getVolatile(array, i);
 559                 assertEquals(x, false, "setVolatile boolean value");
 560             }
 561 
 562             // Lazy
 563             {
 564                 vh.setRelease(array, i, true);
 565                 boolean x = (boolean) vh.getAcquire(array, i);
 566                 assertEquals(x, true, "setRelease boolean value");
 567             }
 568 
 569             // Opaque
 570             {
 571                 vh.setOpaque(array, i, false);
 572                 boolean x = (boolean) vh.getOpaque(array, i);
 573                 assertEquals(x, false, "setOpaque boolean value");
 574             }
 575 
 576 
 577         }
 578     }
 579 
 580     static void testArrayUnsupported(VarHandle vh) {
 581         boolean[] array = new boolean[10];
 582 
 583         int i = 0;
 584         checkUOE(() -> {
 585             boolean r = vh.compareAndSet(array, i, true, false);
 586         });
 587 
 588         checkUOE(() -> {
 589             boolean r = (boolean) vh.compareAndExchangeVolatile(array, i, true, false);
 590         });
 591 
 592         checkUOE(() -> {
 593             boolean r = (boolean) vh.compareAndExchangeAcquire(array, i, true, false);
 594         });
 595 
 596         checkUOE(() -> {
 597             boolean r = (boolean) vh.compareAndExchangeRelease(array, i, true, false);
 598         });
 599 
 600         checkUOE(() -> {
 601             boolean r = vh.weakCompareAndSet(array, i, true, false);
 602         });
 603 
 604         checkUOE(() -> {
 605             boolean r = vh.weakCompareAndSetVolatile(array, i, true, false);
 606         });
 607 
 608         checkUOE(() -> {
 609             boolean r = vh.weakCompareAndSetAcquire(array, i, true, false);
 610         });
 611 
 612         checkUOE(() -> {
 613             boolean r = vh.weakCompareAndSetRelease(array, i, true, false);
 614         });
 615 
 616         checkUOE(() -> {
 617             boolean o = (boolean) vh.getAndAdd(array, i, true);
 618         });
 619 
 620         checkUOE(() -> {
 621             boolean o = (boolean) vh.addAndGet(array, i, true);
 622         });
 623     }
 624 
 625     static void testArrayIndexOutOfBounds(VarHandle vh) throws Throwable {
 626         boolean[] array = new boolean[10];
 627 
 628         for (int i : new int[]{-1, Integer.MIN_VALUE, 10, 11, Integer.MAX_VALUE}) {
 629             final int ci = i;
 630 
 631             checkIOOBE(() -> {
 632                 boolean x = (boolean) vh.get(array, ci);
 633             });
 634 
 635             checkIOOBE(() -> {
 636                 vh.set(array, ci, true);
 637             });
 638 
 639             checkIOOBE(() -> {
 640                 boolean x = (boolean) vh.getVolatile(array, ci);
 641             });
 642 
 643             checkIOOBE(() -> {
 644                 vh.setVolatile(array, ci, true);
 645             });
 646 
 647             checkIOOBE(() -> {
 648                 boolean x = (boolean) vh.getAcquire(array, ci);
 649             });
 650 
 651             checkIOOBE(() -> {
 652                 vh.setRelease(array, ci, true);
 653             });
 654 
 655             checkIOOBE(() -> {
 656                 boolean x = (boolean) vh.getOpaque(array, ci);
 657             });
 658 
 659             checkIOOBE(() -> {
 660                 vh.setOpaque(array, ci, true);
 661             });
 662 
 663 
 664         }
 665     }
 666 }
 667