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