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 final static int BYTES_PER_FLOAT = Float.SIZE / Byte.SIZE; 568 569 private static final VarHandle vh; 570 571 static { 572 try { 573 vh = MethodHandles.byteBufferViewVarHandle(float[].class, ByteOrder.LITTLE_ENDIAN); 574 } catch (Exception e) { 575 throw new RuntimeException(e); 576 } 577 } 578 579 @Override doTest()580 protected void doTest() { 581 ByteBuffer[] buffers = 582 new ByteBuffer[] { 583 ByteBuffer.allocateDirect(16), 584 ByteBuffer.allocate(37), 585 ByteBuffer.wrap(new byte[27], 3, 27 - 3) 586 }; 587 for (ByteBuffer buffer : buffers) { 588 assertThrowsIOOBE(() -> vh.get(buffer, -1)); 589 assertThrowsIOOBE(() -> vh.get(buffer, Integer.MIN_VALUE)); 590 assertThrowsIOOBE(() -> vh.get(buffer, Integer.MAX_VALUE)); 591 assertThrowsIOOBE(() -> vh.get(buffer, buffer.limit())); 592 assertThrowsIOOBE(() -> vh.get(buffer, buffer.limit() - BYTES_PER_FLOAT + 1)); 593 vh.get(buffer, buffer.limit() - BYTES_PER_FLOAT); 594 } 595 } 596 main(String[] args)597 public static void main(String[] args) { 598 new ByteBufferViewOutOfBoundsIndexTest().run(); 599 } 600 } 601 602 public static class ByteBufferViewUnalignedAccessesIndexTest extends VarHandleUnitTest { 603 private static final VarHandle vh; 604 605 static { 606 try { 607 vh = MethodHandles.byteBufferViewVarHandle(int[].class, ByteOrder.BIG_ENDIAN); 608 } catch (Exception e) { 609 throw new RuntimeException(e); 610 } 611 } 612 613 @Override doTest()614 protected void doTest() { 615 ByteBuffer[] buffers = 616 new ByteBuffer[] { 617 ByteBuffer.allocateDirect(16), 618 ByteBuffer.allocate(37), 619 ByteBuffer.wrap(new byte[27], 3, 27 - 3) 620 }; 621 622 for (ByteBuffer buffer : buffers) { 623 int alignedIndex = VarHandleUnitTestHelpers.alignedOffset_int(buffer, 0); 624 for (int i = alignedIndex; i < Integer.SIZE / 8; ++i) { 625 // No exceptions are expected for GET and SET 626 // accessors irrespective of the access alignment. 627 vh.set(buffer, i, 380); 628 vh.get(buffer, i); 629 // Other accessors raise an IllegalStateException if 630 // the access is unaligned. 631 try { 632 vh.compareAndExchange(buffer, i, 777, 320); 633 assertTrue(i == alignedIndex); 634 } catch (IllegalStateException ex) { 635 assertFalse(i == alignedIndex); 636 } 637 try { 638 vh.compareAndExchangeAcquire(buffer, i, 320, 767); 639 assertTrue(i == alignedIndex); 640 } catch (IllegalStateException ex) { 641 assertFalse(i == alignedIndex); 642 } 643 try { 644 vh.compareAndExchangeRelease(buffer, i, 767, 321); 645 assertTrue(i == alignedIndex); 646 } catch (IllegalStateException ex) { 647 assertFalse(i == alignedIndex); 648 } 649 try { 650 vh.compareAndSet(buffer, i, 767, 321); 651 assertTrue(i == alignedIndex); 652 } catch (IllegalStateException ex) { 653 assertFalse(i == alignedIndex); 654 } 655 try { 656 vh.getAcquire(buffer, i); 657 assertTrue(i == alignedIndex); 658 } catch (IllegalStateException ex) { 659 assertFalse(i == alignedIndex); 660 } 661 try { 662 vh.getAndAdd(buffer, i, 117); 663 assertTrue(i == alignedIndex); 664 } catch (IllegalStateException ex) { 665 assertFalse(i == alignedIndex); 666 } 667 try { 668 vh.getAndAddAcquire(buffer, i, 117); 669 assertTrue(i == alignedIndex); 670 } catch (IllegalStateException ex) { 671 assertFalse(i == alignedIndex); 672 } 673 try { 674 vh.getAndAddRelease(buffer, i, 117); 675 assertTrue(i == alignedIndex); 676 } catch (IllegalStateException ex) { 677 assertFalse(i == alignedIndex); 678 } 679 try { 680 vh.getAndBitwiseAnd(buffer, i, 118); 681 assertTrue(i == alignedIndex); 682 } catch (IllegalStateException ex) { 683 assertFalse(i == alignedIndex); 684 } 685 try { 686 vh.getAndBitwiseAndAcquire(buffer, i, 118); 687 assertTrue(i == alignedIndex); 688 } catch (IllegalStateException ex) { 689 assertFalse(i == alignedIndex); 690 } 691 try { 692 vh.getAndBitwiseAndRelease(buffer, i, 118); 693 assertTrue(i == alignedIndex); 694 } catch (IllegalStateException ex) { 695 assertFalse(i == alignedIndex); 696 } 697 try { 698 vh.getAndBitwiseOr(buffer, i, 118); 699 assertTrue(i == alignedIndex); 700 } catch (IllegalStateException ex) { 701 assertFalse(i == alignedIndex); 702 } 703 try { 704 vh.getAndBitwiseOrAcquire(buffer, i, 118); 705 assertTrue(i == alignedIndex); 706 } catch (IllegalStateException ex) { 707 assertFalse(i == alignedIndex); 708 } 709 try { 710 vh.getAndBitwiseOrRelease(buffer, i, 118); 711 assertTrue(i == alignedIndex); 712 } catch (IllegalStateException ex) { 713 assertFalse(i == alignedIndex); 714 } 715 try { 716 vh.getAndBitwiseXor(buffer, i, 118); 717 assertTrue(i == alignedIndex); 718 } catch (IllegalStateException ex) { 719 assertFalse(i == alignedIndex); 720 } 721 try { 722 vh.getAndBitwiseXorAcquire(buffer, i, 118); 723 assertTrue(i == alignedIndex); 724 } catch (IllegalStateException ex) { 725 assertFalse(i == alignedIndex); 726 } 727 try { 728 vh.getAndBitwiseXorRelease(buffer, i, 118); 729 assertTrue(i == alignedIndex); 730 } catch (IllegalStateException ex) { 731 assertFalse(i == alignedIndex); 732 } 733 try { 734 vh.getAndSet(buffer, i, 117); 735 assertTrue(i == alignedIndex); 736 } catch (IllegalStateException ex) { 737 assertFalse(i == alignedIndex); 738 } 739 try { 740 vh.getAndSetAcquire(buffer, i, 117); 741 assertTrue(i == alignedIndex); 742 } catch (IllegalStateException ex) { 743 assertFalse(i == alignedIndex); 744 } 745 try { 746 vh.getAndSetRelease(buffer, i, 117); 747 assertTrue(i == alignedIndex); 748 } catch (IllegalStateException ex) { 749 assertFalse(i == alignedIndex); 750 } 751 try { 752 vh.getOpaque(buffer, i); 753 assertTrue(i == alignedIndex); 754 } catch (IllegalStateException ex) { 755 assertFalse(i == alignedIndex); 756 } 757 try { 758 vh.getVolatile(buffer, i); 759 assertTrue(i == alignedIndex); 760 } catch (IllegalStateException ex) { 761 assertFalse(i == alignedIndex); 762 } 763 try { 764 vh.setOpaque(buffer, i, 777); 765 assertTrue(i == alignedIndex); 766 } catch (IllegalStateException ex) { 767 assertFalse(i == alignedIndex); 768 } 769 try { 770 vh.setRelease(buffer, i, 319); 771 assertTrue(i == alignedIndex); 772 } catch (IllegalStateException ex) { 773 assertFalse(i == alignedIndex); 774 } 775 try { 776 vh.setVolatile(buffer, i, 787); 777 assertTrue(i == alignedIndex); 778 } catch (IllegalStateException ex) { 779 assertFalse(i == alignedIndex); 780 } 781 try { 782 vh.weakCompareAndSet(buffer, i, 787, 340); 783 assertTrue(i == alignedIndex); 784 } catch (IllegalStateException ex) { 785 assertFalse(i == alignedIndex); 786 } 787 try { 788 vh.weakCompareAndSetAcquire(buffer, i, 787, 340); 789 assertTrue(i == alignedIndex); 790 } catch (IllegalStateException ex) { 791 assertFalse(i == alignedIndex); 792 } 793 try { 794 vh.weakCompareAndSetPlain(buffer, i, 787, 340); 795 assertTrue(i == alignedIndex); 796 } catch (IllegalStateException ex) { 797 assertFalse(i == alignedIndex); 798 } 799 try { 800 vh.weakCompareAndSetRelease(buffer, i, 787, 340); 801 assertTrue(i == alignedIndex); 802 } catch (IllegalStateException ex) { 803 assertFalse(i == alignedIndex); 804 } 805 } 806 } 807 } 808 main(String[] args)809 public static void main(String[] args) { 810 new ByteBufferViewUnalignedAccessesIndexTest().run(); 811 } 812 } 813 814 public static class ByteBufferViewBadIndexTypeTest extends VarHandleUnitTest { 815 private static final VarHandle vh; 816 817 static { 818 try { 819 vh = MethodHandles.byteBufferViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN); 820 } catch (Exception e) { 821 throw new RuntimeException(e); 822 } 823 } 824 825 @Override doTest()826 protected void doTest() { 827 ByteBuffer[] buffers = 828 new ByteBuffer[] { 829 ByteBuffer.allocateDirect(16), 830 ByteBuffer.allocate(16), 831 ByteBuffer.wrap(new byte[32], 4, 32 - 4) 832 }; 833 834 for (ByteBuffer buffer : buffers) { 835 // Boxed index goes through argument conversion so no exception expected. 836 vh.get(buffer, Integer.valueOf(3)); 837 vh.get(buffer, Short.valueOf((short) 3)); 838 vh.get(buffer, Byte.valueOf((byte) 7)); 839 try { 840 vh.get(buffer, System.out); 841 failUnreachable(); 842 } catch (WrongMethodTypeException ex) { 843 } 844 } 845 } 846 main(String[] args)847 public static void main(String[] args) { 848 new ByteBufferViewBadIndexTypeTest().run(); 849 } 850 } 851 852 public static class ByteBufferViewMissingIndexTest extends VarHandleUnitTest { 853 private static final VarHandle vh; 854 855 static { 856 try { 857 vh = MethodHandles.byteBufferViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN); 858 } catch (Exception e) { 859 throw new RuntimeException(e); 860 } 861 } 862 863 @Override doTest()864 protected void doTest() { 865 ByteBuffer[] buffers = 866 new ByteBuffer[] { 867 ByteBuffer.allocateDirect(16), 868 ByteBuffer.allocate(16), 869 ByteBuffer.wrap(new byte[32], 4, 32 - 4) 870 }; 871 for (ByteBuffer buffer : buffers) { 872 try { 873 vh.get(buffer); 874 failUnreachable(); 875 } catch (WrongMethodTypeException ex) { 876 } 877 } 878 } 879 main(String[] args)880 public static void main(String[] args) { 881 new ByteBufferViewMissingIndexTest().run(); 882 } 883 } 884 885 public static class ByteBufferViewBadByteBufferTest extends VarHandleUnitTest { 886 private static final VarHandle vh; 887 888 static { 889 try { 890 vh = MethodHandles.byteBufferViewVarHandle(int[].class, ByteOrder.LITTLE_ENDIAN); 891 } catch (Exception e) { 892 throw new RuntimeException(e); 893 } 894 } 895 896 @Override doTest()897 protected void doTest() { 898 if (VarHandleUnitTestHelpers.isRunningOnAndroid()) { 899 ByteBuffer buffer = null; 900 // The RI does not like this test 901 try { 902 vh.get(buffer, 3); 903 failUnreachable(); 904 } catch (NullPointerException ex) { 905 } 906 } 907 try { 908 vh.get(System.err, 3); 909 failUnreachable(); 910 } catch (ClassCastException ex) { 911 } 912 } 913 main(String[] args)914 public static void main(String[] args) { 915 new ByteBufferViewBadByteBufferTest().run(); 916 } 917 } 918 main(String[] args)919 public static void main(String[] args) { 920 FieldCoordinateTypeTest.main(args); 921 922 ArrayElementOutOfBoundsIndexTest.main(args); 923 ArrayElementBadIndexTypeTest.main(args); 924 ArrayElementNullArrayTest.main(args); 925 ArrayElementWrongArrayTypeTest.main(args); 926 ArrayElementMissingIndexTest.main(args); 927 928 ByteArrayViewOutOfBoundsIndexTest.main(args); 929 ByteArrayViewUnalignedAccessesIndexTest.main(args); 930 ByteArrayViewBadIndexTypeTest.main(args); 931 ByteArrayViewMissingIndexTest.main(args); 932 ByteArrayViewBadByteArrayTest.main(args); 933 934 ByteBufferViewOutOfBoundsIndexTest.main(args); 935 ByteBufferViewUnalignedAccessesIndexTest.main(args); 936 ByteBufferViewBadIndexTypeTest.main(args); 937 ByteBufferViewMissingIndexTest.main(args); 938 ByteBufferViewBadByteBufferTest.main(args); 939 } 940 } 941