1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import java.lang.invoke.MethodHandles; 18 import java.lang.invoke.VarHandle; 19 import java.lang.invoke.WrongMethodTypeException; 20 import java.nio.ByteBuffer; 21 import java.nio.ByteOrder; 22 23 public class VarHandleBadCoordinateTests { 24 public static class FieldCoordinateTypeTest extends VarHandleUnitTest { 25 private static final VarHandle vh; 26 27 public static class A { 28 public byte field; 29 } 30 31 public static class B extends A { 32 private byte other_field; 33 } 34 35 public static class C {} 36 37 static { 38 try { 39 vh = MethodHandles.lookup().findVarHandle(A.class, "field", byte.class); 40 } catch (Exception e) { 41 throw new RuntimeException(e); 42 } 43 } 44 45 @Override doTest()46 protected void doTest() { 47 vh.compareAndSet(new A(), (byte) 0, (byte) 3); 48 vh.compareAndSet(new B(), (byte) 0, (byte) 3); 49 try { 50 vh.compareAndSet(new C(), (byte) 0, (byte) 3); 51 failUnreachable(); 52 } catch (ClassCastException ex) { 53 } 54 try { 55 vh.compareAndSet(0xbad0bad0, (byte) 0, (byte) 3); 56 failUnreachable(); 57 } catch (WrongMethodTypeException ex) { 58 } 59 try { 60 vh.compareAndSet(0xbad0bad0, (byte) 0, Integer.MAX_VALUE); 61 failUnreachable(); 62 } catch (WrongMethodTypeException ex) { 63 } 64 try { 65 vh.compareAndSet(0xbad0bad0, (byte) 0); 66 failUnreachable(); 67 } catch (WrongMethodTypeException ex) { 68 } 69 try { 70 vh.compareAndSet(new A(), (byte) 0, Integer.MAX_VALUE); 71 failUnreachable(); 72 } catch (WrongMethodTypeException ex) { 73 } 74 try { 75 vh.compareAndSet((A) null, (byte) 0, (byte) 3); 76 failUnreachable(); 77 } catch (NullPointerException ex) { 78 } 79 try { 80 byte unused = (byte) vh.get(); 81 failUnreachable(); 82 } catch (WrongMethodTypeException ex) { 83 } 84 } 85 main(String[] args)86 public static void main(String[] args) { 87 new FieldCoordinateTypeTest().run(); 88 } 89 } 90 91 public static class ArrayElementOutOfBoundsIndexTest extends VarHandleUnitTest { 92 private static final VarHandle vh; 93 94 static { 95 try { 96 vh = MethodHandles.arrayElementVarHandle(long[].class); 97 } catch (Exception e) { 98 throw new RuntimeException(e); 99 } 100 } 101 102 @Override doTest()103 protected void doTest() { 104 long[] values = new long[33]; 105 try { 106 vh.get(values, -1); 107 failUnreachable(); 108 } catch (ArrayIndexOutOfBoundsException ex) { 109 } 110 try { 111 vh.get(values, values.length); 112 failUnreachable(); 113 } catch (ArrayIndexOutOfBoundsException ex) { 114 } 115 try { 116 vh.get(values, Integer.MAX_VALUE - 1); 117 failUnreachable(); 118 } catch (ArrayIndexOutOfBoundsException ex) { 119 } 120 } 121 main(String[] args)122 public static void main(String[] args) { 123 new ArrayElementOutOfBoundsIndexTest().run(); 124 } 125 } 126 127 public static class ArrayElementBadIndexTypeTest extends VarHandleUnitTest { 128 private static final VarHandle vh; 129 130 static { 131 try { 132 vh = MethodHandles.arrayElementVarHandle(long[].class); 133 } catch (Exception e) { 134 throw new RuntimeException(e); 135 } 136 } 137 138 @Override doTest()139 protected void doTest() { 140 long[] values = new long[33]; 141 vh.set(values, Integer.valueOf(3), Long.MIN_VALUE); 142 vh.set(values, Byte.valueOf((byte) 0), Long.MIN_VALUE); 143 try { 144 vh.set(values, 3.3f, Long.MAX_VALUE); 145 failUnreachable(); 146 } catch (WrongMethodTypeException ex) { 147 } 148 } 149 main(String[] args)150 public static void main(String[] args) { 151 new ArrayElementBadIndexTypeTest().run(); 152 } 153 } 154 155 public static class ArrayElementNullArrayTest extends VarHandleUnitTest { 156 private static final VarHandle vh; 157 158 static { 159 try { 160 vh = MethodHandles.arrayElementVarHandle(long[].class); 161 } catch (Exception e) { 162 throw new RuntimeException(e); 163 } 164 } 165 166 @Override doTest()167 protected void doTest() { 168 long[] values = null; 169 try { 170 vh.get(values); 171 failUnreachable(); 172 } catch (WrongMethodTypeException ex) { 173 } 174 } 175 main(String[] args)176 public static void main(String[] args) { 177 new ArrayElementNullArrayTest().run(); 178 } 179 } 180 181 public static class ArrayElementWrongArrayTypeTest extends VarHandleUnitTest { 182 private static final VarHandle vh; 183 184 static { 185 try { 186 vh = MethodHandles.arrayElementVarHandle(long[].class); 187 } catch (Exception e) { 188 throw new RuntimeException(e); 189 } 190 } 191 192 @Override doTest()193 protected void doTest() { 194 try { 195 vh.get(new char[10], 0); 196 failUnreachable(); 197 } catch (ClassCastException ex) { 198 } 199 } 200 main(String[] args)201 public static void main(String[] args) { 202 new ArrayElementWrongArrayTypeTest().run(); 203 } 204 } 205 206 public static class ArrayElementMissingIndexTest extends VarHandleUnitTest { 207 private static final VarHandle vh; 208 209 static { 210 try { 211 vh = MethodHandles.arrayElementVarHandle(long[].class); 212 } catch (Exception e) { 213 throw new RuntimeException(e); 214 } 215 } 216 217 @Override doTest()218 protected void doTest() { 219 long[] values = new long[33]; 220 try { 221 vh.get(values); 222 failUnreachable(); 223 } catch (WrongMethodTypeException ex) { 224 } 225 } 226 main(String[] args)227 public static void main(String[] args) { 228 new ArrayElementMissingIndexTest().run(); 229 } 230 } 231 232 public static class ByteArrayViewOutOfBoundsIndexTest extends VarHandleUnitTest { 233 private static final VarHandle vh; 234 235 static { 236 try { 237 vh = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.BIG_ENDIAN); 238 } catch (Exception e) { 239 throw new RuntimeException(e); 240 } 241 } 242 243 @Override doTest()244 protected void doTest() { 245 byte[] bytes = new byte[16]; 246 try { 247 vh.get(bytes, -1); 248 failUnreachable(); 249 } catch (IndexOutOfBoundsException ex) { 250 } 251 try { 252 vh.get(bytes, bytes.length); 253 failUnreachable(); 254 } catch (IndexOutOfBoundsException ex) { 255 } 256 try { 257 vh.get(bytes, Integer.MAX_VALUE - 1); 258 failUnreachable(); 259 } catch (IndexOutOfBoundsException ex) { 260 } 261 try { 262 vh.get(bytes, bytes.length - Integer.SIZE / 8 + 1); 263 failUnreachable(); 264 } catch (IndexOutOfBoundsException ex) { 265 } 266 vh.get(bytes, bytes.length - Integer.SIZE / 8); 267 } 268 main(String[] args)269 public static void main(String[] args) { 270 new ByteArrayViewOutOfBoundsIndexTest().run(); 271 } 272 } 273 274 public static class ByteArrayViewUnalignedAccessesIndexTest extends VarHandleUnitTest { 275 private static final VarHandle vh; 276 277 static { 278 try { 279 vh = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.BIG_ENDIAN); 280 } catch (Exception e) { 281 throw new RuntimeException(e); 282 } 283 } 284 285 @Override doTest()286 protected void doTest() { 287 byte[] bytes = new byte[33]; 288 289 int alignedIndex = VarHandleUnitTestHelpers.alignedOffset_int(bytes, 0); 290 for (int i = alignedIndex; i < Integer.SIZE / 8; ++i) { 291 // No exceptions are expected for GET and SET 292 // accessors irrespective of the access alignment. 293 vh.set(bytes, i, 380); 294 vh.get(bytes, i); 295 // Other accessors raise an IllegalStateException if 296 // the access is unaligned. 297 try { 298 vh.compareAndExchange(bytes, i, 777, 320); 299 assertTrue(i == alignedIndex); 300 } catch (IllegalStateException ex) { 301 assertFalse(i == alignedIndex); 302 } 303 try { 304 vh.compareAndExchangeAcquire(bytes, i, 320, 767); 305 assertTrue(i == alignedIndex); 306 } catch (IllegalStateException ex) { 307 assertFalse(i == alignedIndex); 308 } 309 try { 310 vh.compareAndExchangeRelease(bytes, i, 767, 321); 311 assertTrue(i == alignedIndex); 312 } catch (IllegalStateException ex) { 313 assertFalse(i == alignedIndex); 314 } 315 try { 316 vh.compareAndSet(bytes, i, 767, 321); 317 assertTrue(i == alignedIndex); 318 } catch (IllegalStateException ex) { 319 assertFalse(i == alignedIndex); 320 } 321 try { 322 vh.getAcquire(bytes, i); 323 assertTrue(i == alignedIndex); 324 } catch (IllegalStateException ex) { 325 assertFalse(i == alignedIndex); 326 } 327 try { 328 vh.getAndAdd(bytes, i, 117); 329 assertTrue(i == alignedIndex); 330 } catch (IllegalStateException ex) { 331 assertFalse(i == alignedIndex); 332 } 333 try { 334 vh.getAndAddAcquire(bytes, i, 117); 335 assertTrue(i == alignedIndex); 336 } catch (IllegalStateException ex) { 337 assertFalse(i == alignedIndex); 338 } 339 try { 340 vh.getAndAddRelease(bytes, i, 117); 341 assertTrue(i == alignedIndex); 342 } catch (IllegalStateException ex) { 343 assertFalse(i == alignedIndex); 344 } 345 try { 346 vh.getAndBitwiseAnd(bytes, i, 118); 347 assertTrue(i == alignedIndex); 348 } catch (IllegalStateException ex) { 349 assertFalse(i == alignedIndex); 350 } 351 try { 352 vh.getAndBitwiseAndAcquire(bytes, i, 118); 353 assertTrue(i == alignedIndex); 354 } catch (IllegalStateException ex) { 355 assertFalse(i == alignedIndex); 356 } 357 try { 358 vh.getAndBitwiseAndRelease(bytes, i, 118); 359 assertTrue(i == alignedIndex); 360 } catch (IllegalStateException ex) { 361 assertFalse(i == alignedIndex); 362 } 363 try { 364 vh.getAndBitwiseOr(bytes, i, 118); 365 assertTrue(i == alignedIndex); 366 } catch (IllegalStateException ex) { 367 assertFalse(i == alignedIndex); 368 } 369 try { 370 vh.getAndBitwiseOrAcquire(bytes, i, 118); 371 assertTrue(i == alignedIndex); 372 } catch (IllegalStateException ex) { 373 assertFalse(i == alignedIndex); 374 } 375 try { 376 vh.getAndBitwiseOrRelease(bytes, i, 118); 377 assertTrue(i == alignedIndex); 378 } catch (IllegalStateException ex) { 379 assertFalse(i == alignedIndex); 380 } 381 try { 382 vh.getAndBitwiseXor(bytes, i, 118); 383 assertTrue(i == alignedIndex); 384 } catch (IllegalStateException ex) { 385 assertFalse(i == alignedIndex); 386 } 387 try { 388 vh.getAndBitwiseXorAcquire(bytes, i, 118); 389 assertTrue(i == alignedIndex); 390 } catch (IllegalStateException ex) { 391 assertFalse(i == alignedIndex); 392 } 393 try { 394 vh.getAndBitwiseXorRelease(bytes, i, 118); 395 assertTrue(i == alignedIndex); 396 } catch (IllegalStateException ex) { 397 assertFalse(i == alignedIndex); 398 } 399 try { 400 vh.getAndSet(bytes, i, 117); 401 assertTrue(i == alignedIndex); 402 } catch (IllegalStateException ex) { 403 assertFalse(i == alignedIndex); 404 } 405 try { 406 vh.getAndSetAcquire(bytes, i, 117); 407 assertTrue(i == alignedIndex); 408 } catch (IllegalStateException ex) { 409 assertFalse(i == alignedIndex); 410 } 411 try { 412 vh.getAndSetRelease(bytes, i, 117); 413 assertTrue(i == alignedIndex); 414 } catch (IllegalStateException ex) { 415 assertFalse(i == alignedIndex); 416 } 417 try { 418 vh.getOpaque(bytes, i); 419 assertTrue(i == alignedIndex); 420 } catch (IllegalStateException ex) { 421 assertFalse(i == alignedIndex); 422 } 423 try { 424 vh.getVolatile(bytes, i); 425 assertTrue(i == alignedIndex); 426 } catch (IllegalStateException ex) { 427 assertFalse(i == alignedIndex); 428 } 429 try { 430 vh.setOpaque(bytes, i, 777); 431 assertTrue(i == alignedIndex); 432 } catch (IllegalStateException ex) { 433 assertFalse(i == alignedIndex); 434 } 435 try { 436 vh.setRelease(bytes, i, 319); 437 assertTrue(i == alignedIndex); 438 } catch (IllegalStateException ex) { 439 assertFalse(i == alignedIndex); 440 } 441 try { 442 vh.setVolatile(bytes, i, 787); 443 assertTrue(i == alignedIndex); 444 } catch (IllegalStateException ex) { 445 assertFalse(i == alignedIndex); 446 } 447 try { 448 vh.weakCompareAndSet(bytes, i, 787, 340); 449 assertTrue(i == alignedIndex); 450 } catch (IllegalStateException ex) { 451 assertFalse(i == alignedIndex); 452 } 453 try { 454 vh.weakCompareAndSetAcquire(bytes, i, 787, 340); 455 assertTrue(i == alignedIndex); 456 } catch (IllegalStateException ex) { 457 assertFalse(i == alignedIndex); 458 } 459 try { 460 vh.weakCompareAndSetPlain(bytes, i, 787, 340); 461 assertTrue(i == alignedIndex); 462 } catch (IllegalStateException ex) { 463 assertFalse(i == alignedIndex); 464 } 465 try { 466 vh.weakCompareAndSetRelease(bytes, i, 787, 340); 467 assertTrue(i == alignedIndex); 468 } catch (IllegalStateException ex) { 469 assertFalse(i == alignedIndex); 470 } 471 } 472 } 473 main(String[] args)474 public static void main(String[] args) { 475 new ByteArrayViewUnalignedAccessesIndexTest().run(); 476 } 477 } 478 479 public static class ByteArrayViewBadIndexTypeTest extends VarHandleUnitTest { 480 private static final VarHandle vh; 481 482 static { 483 try { 484 vh = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN); 485 } catch (Exception e) { 486 throw new RuntimeException(e); 487 } 488 } 489 490 @Override doTest()491 protected void doTest() { 492 byte[] bytes = new byte[16]; 493 // Boxed index goes through argument conversion so no exception expected. 494 vh.get(bytes, Integer.valueOf(3)); 495 vh.get(bytes, Short.valueOf((short) 3)); 496 497 try { 498 vh.get(bytes, System.out); 499 failUnreachable(); 500 } catch (WrongMethodTypeException ex) { 501 } 502 } 503 main(String[] args)504 public static void main(String[] args) { 505 new ByteArrayViewBadIndexTypeTest().run(); 506 } 507 } 508 509 public static class ByteArrayViewMissingIndexTest extends VarHandleUnitTest { 510 private static final VarHandle vh; 511 512 static { 513 try { 514 vh = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN); 515 } catch (Exception e) { 516 throw new RuntimeException(e); 517 } 518 } 519 520 @Override doTest()521 protected void doTest() { 522 byte[] bytes = new byte[16]; 523 try { 524 vh.get(bytes); 525 failUnreachable(); 526 } catch (WrongMethodTypeException ex) { 527 } 528 } 529 main(String[] args)530 public static void main(String[] args) { 531 new ByteArrayViewMissingIndexTest().run(); 532 } 533 } 534 535 public static class ByteArrayViewBadByteArrayTest extends VarHandleUnitTest { 536 private static final VarHandle vh; 537 538 static { 539 try { 540 vh = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN); 541 } catch (Exception e) { 542 throw new RuntimeException(e); 543 } 544 } 545 546 @Override doTest()547 protected void doTest() { 548 byte[] bytes = null; 549 try { 550 vh.get(bytes, Integer.valueOf(3)); 551 failUnreachable(); 552 } catch (NullPointerException ex) { 553 } 554 try { 555 vh.get(System.err, Integer.valueOf(3)); 556 failUnreachable(); 557 } catch (ClassCastException ex) { 558 } 559 } 560 main(String[] args)561 public static void main(String[] args) { 562 new ByteArrayViewBadByteArrayTest().run(); 563 } 564 } 565 566 public static class ByteBufferViewOutOfBoundsIndexTest extends VarHandleUnitTest { 567 private static final VarHandle vh; 568 569 static { 570 try { 571 vh = MethodHandles.byteBufferViewVarHandle(float[].class, ByteOrder.LITTLE_ENDIAN); 572 } catch (Exception e) { 573 throw new RuntimeException(e); 574 } 575 } 576 577 @Override doTest()578 protected void doTest() { 579 ByteBuffer[] buffers = 580 new ByteBuffer[] { 581 ByteBuffer.allocateDirect(16), 582 ByteBuffer.allocate(37), 583 ByteBuffer.wrap(new byte[27], 3, 27 - 3) 584 }; 585 for (ByteBuffer buffer : buffers) { 586 try { 587 vh.get(buffer, -1); 588 failUnreachable(); 589 } catch (IndexOutOfBoundsException ex) { 590 } 591 try { 592 vh.get(buffer, buffer.limit()); 593 failUnreachable(); 594 } catch (IndexOutOfBoundsException ex) { 595 } 596 try { 597 vh.get(buffer, Integer.MAX_VALUE - 1); 598 failUnreachable(); 599 } catch (IndexOutOfBoundsException ex) { 600 } 601 try { 602 vh.get(buffer, buffer.limit() - Integer.SIZE / 8 + 1); 603 failUnreachable(); 604 } catch (IndexOutOfBoundsException ex) { 605 } 606 vh.get(buffer, buffer.limit() - Integer.SIZE / 8); 607 } 608 } 609 main(String[] args)610 public static void main(String[] args) { 611 new ByteBufferViewOutOfBoundsIndexTest().run(); 612 } 613 } 614 615 public static class ByteBufferViewUnalignedAccessesIndexTest extends VarHandleUnitTest { 616 private static final VarHandle vh; 617 618 static { 619 try { 620 vh = MethodHandles.byteBufferViewVarHandle(int[].class, ByteOrder.BIG_ENDIAN); 621 } catch (Exception e) { 622 throw new RuntimeException(e); 623 } 624 } 625 626 @Override doTest()627 protected void doTest() { 628 ByteBuffer[] buffers = 629 new ByteBuffer[] { 630 ByteBuffer.allocateDirect(16), 631 ByteBuffer.allocate(37), 632 ByteBuffer.wrap(new byte[27], 3, 27 - 3) 633 }; 634 635 for (ByteBuffer buffer : buffers) { 636 int alignedIndex = VarHandleUnitTestHelpers.alignedOffset_int(buffer, 0); 637 for (int i = alignedIndex; i < Integer.SIZE / 8; ++i) { 638 // No exceptions are expected for GET and SET 639 // accessors irrespective of the access alignment. 640 vh.set(buffer, i, 380); 641 vh.get(buffer, i); 642 // Other accessors raise an IllegalStateException if 643 // the access is unaligned. 644 try { 645 vh.compareAndExchange(buffer, i, 777, 320); 646 assertTrue(i == alignedIndex); 647 } catch (IllegalStateException ex) { 648 assertFalse(i == alignedIndex); 649 } 650 try { 651 vh.compareAndExchangeAcquire(buffer, i, 320, 767); 652 assertTrue(i == alignedIndex); 653 } catch (IllegalStateException ex) { 654 assertFalse(i == alignedIndex); 655 } 656 try { 657 vh.compareAndExchangeRelease(buffer, i, 767, 321); 658 assertTrue(i == alignedIndex); 659 } catch (IllegalStateException ex) { 660 assertFalse(i == alignedIndex); 661 } 662 try { 663 vh.compareAndSet(buffer, i, 767, 321); 664 assertTrue(i == alignedIndex); 665 } catch (IllegalStateException ex) { 666 assertFalse(i == alignedIndex); 667 } 668 try { 669 vh.getAcquire(buffer, i); 670 assertTrue(i == alignedIndex); 671 } catch (IllegalStateException ex) { 672 assertFalse(i == alignedIndex); 673 } 674 try { 675 vh.getAndAdd(buffer, i, 117); 676 assertTrue(i == alignedIndex); 677 } catch (IllegalStateException ex) { 678 assertFalse(i == alignedIndex); 679 } 680 try { 681 vh.getAndAddAcquire(buffer, i, 117); 682 assertTrue(i == alignedIndex); 683 } catch (IllegalStateException ex) { 684 assertFalse(i == alignedIndex); 685 } 686 try { 687 vh.getAndAddRelease(buffer, i, 117); 688 assertTrue(i == alignedIndex); 689 } catch (IllegalStateException ex) { 690 assertFalse(i == alignedIndex); 691 } 692 try { 693 vh.getAndBitwiseAnd(buffer, i, 118); 694 assertTrue(i == alignedIndex); 695 } catch (IllegalStateException ex) { 696 assertFalse(i == alignedIndex); 697 } 698 try { 699 vh.getAndBitwiseAndAcquire(buffer, i, 118); 700 assertTrue(i == alignedIndex); 701 } catch (IllegalStateException ex) { 702 assertFalse(i == alignedIndex); 703 } 704 try { 705 vh.getAndBitwiseAndRelease(buffer, i, 118); 706 assertTrue(i == alignedIndex); 707 } catch (IllegalStateException ex) { 708 assertFalse(i == alignedIndex); 709 } 710 try { 711 vh.getAndBitwiseOr(buffer, i, 118); 712 assertTrue(i == alignedIndex); 713 } catch (IllegalStateException ex) { 714 assertFalse(i == alignedIndex); 715 } 716 try { 717 vh.getAndBitwiseOrAcquire(buffer, i, 118); 718 assertTrue(i == alignedIndex); 719 } catch (IllegalStateException ex) { 720 assertFalse(i == alignedIndex); 721 } 722 try { 723 vh.getAndBitwiseOrRelease(buffer, i, 118); 724 assertTrue(i == alignedIndex); 725 } catch (IllegalStateException ex) { 726 assertFalse(i == alignedIndex); 727 } 728 try { 729 vh.getAndBitwiseXor(buffer, i, 118); 730 assertTrue(i == alignedIndex); 731 } catch (IllegalStateException ex) { 732 assertFalse(i == alignedIndex); 733 } 734 try { 735 vh.getAndBitwiseXorAcquire(buffer, i, 118); 736 assertTrue(i == alignedIndex); 737 } catch (IllegalStateException ex) { 738 assertFalse(i == alignedIndex); 739 } 740 try { 741 vh.getAndBitwiseXorRelease(buffer, i, 118); 742 assertTrue(i == alignedIndex); 743 } catch (IllegalStateException ex) { 744 assertFalse(i == alignedIndex); 745 } 746 try { 747 vh.getAndSet(buffer, i, 117); 748 assertTrue(i == alignedIndex); 749 } catch (IllegalStateException ex) { 750 assertFalse(i == alignedIndex); 751 } 752 try { 753 vh.getAndSetAcquire(buffer, i, 117); 754 assertTrue(i == alignedIndex); 755 } catch (IllegalStateException ex) { 756 assertFalse(i == alignedIndex); 757 } 758 try { 759 vh.getAndSetRelease(buffer, i, 117); 760 assertTrue(i == alignedIndex); 761 } catch (IllegalStateException ex) { 762 assertFalse(i == alignedIndex); 763 } 764 try { 765 vh.getOpaque(buffer, i); 766 assertTrue(i == alignedIndex); 767 } catch (IllegalStateException ex) { 768 assertFalse(i == alignedIndex); 769 } 770 try { 771 vh.getVolatile(buffer, i); 772 assertTrue(i == alignedIndex); 773 } catch (IllegalStateException ex) { 774 assertFalse(i == alignedIndex); 775 } 776 try { 777 vh.setOpaque(buffer, i, 777); 778 assertTrue(i == alignedIndex); 779 } catch (IllegalStateException ex) { 780 assertFalse(i == alignedIndex); 781 } 782 try { 783 vh.setRelease(buffer, i, 319); 784 assertTrue(i == alignedIndex); 785 } catch (IllegalStateException ex) { 786 assertFalse(i == alignedIndex); 787 } 788 try { 789 vh.setVolatile(buffer, i, 787); 790 assertTrue(i == alignedIndex); 791 } catch (IllegalStateException ex) { 792 assertFalse(i == alignedIndex); 793 } 794 try { 795 vh.weakCompareAndSet(buffer, i, 787, 340); 796 assertTrue(i == alignedIndex); 797 } catch (IllegalStateException ex) { 798 assertFalse(i == alignedIndex); 799 } 800 try { 801 vh.weakCompareAndSetAcquire(buffer, i, 787, 340); 802 assertTrue(i == alignedIndex); 803 } catch (IllegalStateException ex) { 804 assertFalse(i == alignedIndex); 805 } 806 try { 807 vh.weakCompareAndSetPlain(buffer, i, 787, 340); 808 assertTrue(i == alignedIndex); 809 } catch (IllegalStateException ex) { 810 assertFalse(i == alignedIndex); 811 } 812 try { 813 vh.weakCompareAndSetRelease(buffer, i, 787, 340); 814 assertTrue(i == alignedIndex); 815 } catch (IllegalStateException ex) { 816 assertFalse(i == alignedIndex); 817 } 818 } 819 } 820 } 821 main(String[] args)822 public static void main(String[] args) { 823 new ByteBufferViewUnalignedAccessesIndexTest().run(); 824 } 825 } 826 827 public static class ByteBufferViewBadIndexTypeTest extends VarHandleUnitTest { 828 private static final VarHandle vh; 829 830 static { 831 try { 832 vh = MethodHandles.byteBufferViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN); 833 } catch (Exception e) { 834 throw new RuntimeException(e); 835 } 836 } 837 838 @Override doTest()839 protected void doTest() { 840 ByteBuffer[] buffers = 841 new ByteBuffer[] { 842 ByteBuffer.allocateDirect(16), 843 ByteBuffer.allocate(16), 844 ByteBuffer.wrap(new byte[32], 4, 32 - 4) 845 }; 846 847 for (ByteBuffer buffer : buffers) { 848 // Boxed index goes through argument conversion so no exception expected. 849 vh.get(buffer, Integer.valueOf(3)); 850 vh.get(buffer, Short.valueOf((short) 3)); 851 vh.get(buffer, Byte.valueOf((byte) 7)); 852 try { 853 vh.get(buffer, System.out); 854 failUnreachable(); 855 } catch (WrongMethodTypeException ex) { 856 } 857 } 858 } 859 main(String[] args)860 public static void main(String[] args) { 861 new ByteBufferViewBadIndexTypeTest().run(); 862 } 863 } 864 865 public static class ByteBufferViewMissingIndexTest extends VarHandleUnitTest { 866 private static final VarHandle vh; 867 868 static { 869 try { 870 vh = MethodHandles.byteBufferViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN); 871 } catch (Exception e) { 872 throw new RuntimeException(e); 873 } 874 } 875 876 @Override doTest()877 protected void doTest() { 878 ByteBuffer[] buffers = 879 new ByteBuffer[] { 880 ByteBuffer.allocateDirect(16), 881 ByteBuffer.allocate(16), 882 ByteBuffer.wrap(new byte[32], 4, 32 - 4) 883 }; 884 for (ByteBuffer buffer : buffers) { 885 try { 886 vh.get(buffer); 887 failUnreachable(); 888 } catch (WrongMethodTypeException ex) { 889 } 890 } 891 } 892 main(String[] args)893 public static void main(String[] args) { 894 new ByteBufferViewMissingIndexTest().run(); 895 } 896 } 897 898 public static class ByteBufferViewBadByteBufferTest extends VarHandleUnitTest { 899 private static final VarHandle vh; 900 901 static { 902 try { 903 vh = MethodHandles.byteBufferViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN); 904 } catch (Exception e) { 905 throw new RuntimeException(e); 906 } 907 } 908 909 @Override doTest()910 protected void doTest() { 911 if (VarHandleUnitTestHelpers.isRunningOnAndroid()) { 912 ByteBuffer buffer = null; 913 // The RI does not like this test 914 try { 915 vh.get(buffer, 3); 916 failUnreachable(); 917 } catch (NullPointerException ex) { 918 } 919 } 920 try { 921 vh.get(System.err, 3); 922 failUnreachable(); 923 } catch (ClassCastException ex) { 924 } 925 } 926 main(String[] args)927 public static void main(String[] args) { 928 new ByteBufferViewBadByteBufferTest().run(); 929 } 930 } 931 main(String[] args)932 public static void main(String[] args) { 933 FieldCoordinateTypeTest.main(args); 934 935 ArrayElementOutOfBoundsIndexTest.main(args); 936 ArrayElementBadIndexTypeTest.main(args); 937 ArrayElementNullArrayTest.main(args); 938 ArrayElementWrongArrayTypeTest.main(args); 939 ArrayElementMissingIndexTest.main(args); 940 941 ByteArrayViewOutOfBoundsIndexTest.main(args); 942 ByteArrayViewUnalignedAccessesIndexTest.main(args); 943 ByteArrayViewBadIndexTypeTest.main(args); 944 ByteArrayViewMissingIndexTest.main(args); 945 ByteArrayViewBadByteArrayTest.main(args); 946 947 ByteBufferViewOutOfBoundsIndexTest.main(args); 948 ByteBufferViewUnalignedAccessesIndexTest.main(args); 949 ByteBufferViewBadIndexTypeTest.main(args); 950 ByteBufferViewMissingIndexTest.main(args); 951 ByteBufferViewBadByteBufferTest.main(args); 952 } 953 } 954