1 /*
2  * Copyright (C) 2014 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.reflect.InvocationTargetException;
18 import java.lang.reflect.Method;
19 
20 // Note that $opt$ is a marker for the optimizing compiler to test
21 // it does compile the method.
22 
23 public class Main extends TestCase {
main(String[] args)24   public static void main(String[] args) throws Exception {
25     $opt$TestAllocations();
26     $opt$TestWithInitializations();
27     $opt$TestNegativeValueNewByteArray();
28     $opt$TestNegativeValueNewCharArray();
29     testNegativeArraySize();
30     testSmaliFilledNewArray();
31     testSmaliFillArrayData();
32     testSmaliVerifyError();
33   }
34 
$opt$TestAllocations()35   static void $opt$TestAllocations() {
36     float[] a = new float[1];
37     assertEquals(1, a.length);
38 
39     double[] b = new double[2];
40     assertEquals(2, b.length);
41 
42     long[] c = new long[3];
43     assertEquals(3, c.length);
44 
45     int[] d = new int[4];
46     assertEquals(4, d.length);
47 
48     short[] e = new short[5];
49     assertEquals(5, e.length);
50 
51     char[] f = new char[6];
52     assertEquals(6, f.length);
53 
54     byte[] g = new byte[7];
55     assertEquals(7, g.length);
56 
57     boolean[] h = new boolean[8];
58     assertEquals(8, h.length);
59 
60     Object[] i = new Object[9];
61     assertEquals(9, i.length);
62   }
63 
$opt$TestWithInitializations()64   static void $opt$TestWithInitializations() {
65     float[] a = { 1.2f };
66     assertEquals(1, a.length);
67     assertEquals(1.2f, a[0]);
68 
69     double[] b = { 4.3, 1.2 };
70     assertEquals(2, b.length);
71     assertEquals(4.3, b[0]);
72     assertEquals(1.2, b[1]);
73 
74     long[] c = { 4L, 5L };
75     assertEquals(2, c.length);
76     assertEquals(4L, c[0]);
77     assertEquals(5L, c[1]);
78 
79     int[] d = {1, 2, 3};
80     assertEquals(3, d.length);
81     assertEquals(1, d[0]);
82     assertEquals(2, d[1]);
83     assertEquals(3, d[2]);
84 
85     short[] e = {4, 5, 6};
86     assertEquals(3, e.length);
87     assertEquals(4, e[0]);
88     assertEquals(5, e[1]);
89     assertEquals(6, e[2]);
90 
91     char[] f = {'a', 'b'};
92     assertEquals(2, f.length);
93     assertEquals('a', f[0]);
94     assertEquals('b', f[1]);
95 
96     byte[] g = {7, 8, 9};
97     assertEquals(3, g.length);
98     assertEquals(7, g[0]);
99     assertEquals(8, g[1]);
100     assertEquals(9, g[2]);
101 
102     boolean[] h = {true, false};
103     assertEquals(2, h.length);
104     assertEquals(true, h[0]);
105     assertEquals(false, h[1]);
106 
107     Object obj1 = new Object();
108     Object obj2 = new Object();
109     Object[] i = {obj1, obj2};
110     assertEquals(2, i.length);
111     assertEquals(obj1, i[0]);
112     assertEquals(obj2, i[1]);
113   }
114 
$opt$TestNegativeValueNewByteArray()115   static void $opt$TestNegativeValueNewByteArray() {
116     // Use an array initializer to hint the use of filled-new-array.
117     byte[] a = { (byte)0xa0, (byte)0xa1, (byte)0xa2, (byte)0xa3,
118                  (byte)0xa4, (byte)0xa5, (byte)0xa6, (byte)0xa7 };
119     for (int i = 0; i < a.length; i++) {
120       assertEquals((byte)0xa0 + i, a[i]);
121     }
122   }
123 
$opt$TestNegativeValueNewCharArray()124   static void $opt$TestNegativeValueNewCharArray() {
125     // Use an array initializer to hint the use of filled-new-array.
126     char[] a = { (char)0xa000, (char)0xa001, (char)0xa002, (char)0xa003,
127                  (char)0xa004, (char)0xa005, (char)0xa006, (char)0xa007 };
128     for (int i = 0; i < a.length; i++) {
129       assertEquals((char)0xa000 + i, a[i]);
130     }
131   }
132 
testNegativeArraySize()133   static void testNegativeArraySize() {
134     int i = 0;
135     try {
136       $opt$TestNegativeArraySize();
137     } catch (NegativeArraySizeException e) {
138       i = 1;
139     }
140     assertEquals(i, 1);
141   }
142 
$opt$TestNegativeArraySize()143   static int[] $opt$TestNegativeArraySize() {
144     int[] array = new int[-1];
145     return null;
146   }
147 
testSmaliFilledNewArray()148   public static void testSmaliFilledNewArray() throws Exception {
149     Class<?> c = Class.forName("FilledNewArray");
150 
151     {
152       Method m = c.getMethod("newInt", Integer.TYPE, Integer.TYPE, Integer.TYPE);
153       Object[] args = {new Integer(1), new Integer(2), new Integer(3)};
154       int[] result = (int[])m.invoke(null, args);
155       assertEquals(3, result.length);
156       assertEquals(1, result[0]);
157       assertEquals(2, result[1]);
158       assertEquals(3, result[2]);
159     }
160 
161     {
162       Method m = c.getMethod("newRef", Object.class, Object.class);
163       Object[] args = {new Integer(1), new Integer(2)};
164       Object[] result = (Object[])m.invoke(null, args);
165       assertEquals(2, result.length);
166       assertEquals(args[0], result[0]);
167       assertEquals(args[1], result[1]);
168     }
169 
170     {
171       Method m = c.getMethod("newArray", int[].class, int[].class);
172       Object[] args = {new int[0], new int[1]};
173       Object[] result = (Object[])m.invoke(null, args);
174       assertEquals(2, result.length);
175       assertEquals(args[0], result[0]);
176       assertEquals(args[1], result[1]);
177     }
178 
179     {
180       Method m = c.getMethod("newIntRange", Integer.TYPE, Integer.TYPE, Integer.TYPE);
181       Object[] args = {new Integer(1), new Integer(2), new Integer(3)};
182       int[] result = (int[])m.invoke(null, args);
183       assertEquals(3, result.length);
184       assertEquals(1, result[0]);
185       assertEquals(2, result[1]);
186       assertEquals(3, result[2]);
187     }
188 
189     {
190       Method m = c.getMethod("newRefRange", Object.class, Object.class);
191       Object[] args = {new Integer(1), new Integer(2)};
192       Object[] result = (Object[])m.invoke(null, args);
193       assertEquals(2, result.length);
194       assertEquals(args[0], result[0]);
195       assertEquals(args[1], result[1]);
196     }
197 
198     {
199       Method m = c.getMethod("newArrayRange", int[].class, int[].class);
200       Object[] args = {new int[0], new int[1]};
201       Object[] result = (Object[])m.invoke(null, args);
202       assertEquals(2, result.length);
203       assertEquals(args[0], result[0]);
204       assertEquals(args[1], result[1]);
205     }
206   }
207 
testSmaliVerifyError()208   public static void testSmaliVerifyError() throws Exception {
209     Error error = null;
210     // Ensure the elements in filled-new-array must be assignable
211     // to the array component type.
212     try {
213       Class.forName("FilledNewArrayVerifyError");
214     } catch (VerifyError e) {
215       error = e;
216     }
217     assertNotNull(error);
218   }
219 
testSmaliFillArrayData()220   public static void testSmaliFillArrayData() throws Exception {
221     Class<?> c = Class.forName("FillArrayData");
222     {
223       Method m = c.getMethod("emptyIntArray", int[].class);
224       int[] array = new int[0];
225       Object[] args = { array };
226       m.invoke(null, args);
227       assertEquals(0, array.length);
228 
229       array = new int[2];
230       args[0] = array;
231       m.invoke(null, args);
232       // Test that nothing has been written to the array.
233       assertEquals(0, array[0]);
234       assertEquals(0, array[1]);
235 
236       array = new int[] { 42, -42 };
237       args[0] = array;
238       m.invoke(null, args);
239       // Test that nothing has been written to the array.
240       assertEquals(42, array[0]);
241       assertEquals(-42, array[1]);
242 
243       Throwable exception = null;
244       args[0] = null;
245       try {
246         m.invoke(null, args);
247       } catch (InvocationTargetException e) {
248         exception = e.getCause();
249         assertTrue(exception instanceof NullPointerException);
250       }
251       assertNotNull(exception);
252     }
253 
254     {
255       Method m = c.getMethod("intArray", int[].class);
256       int[] array = new int[7];
257       Object[] args = { array };
258       m.invoke(null, args);
259       assertEquals(7, array.length);
260       assertEquals(1, array[0]);
261       assertEquals(2, array[1]);
262       assertEquals(3, array[2]);
263       assertEquals(4, array[3]);
264       assertEquals(5, array[4]);
265       assertEquals(0, array[5]);
266       assertEquals(0, array[6]);
267 
268       array = new int[2];
269       args[0] = array;
270       Throwable exception = null;
271       try {
272         m.invoke(null, args);
273       } catch (InvocationTargetException e) {
274         exception = e.getCause();
275         assertTrue(exception instanceof IndexOutOfBoundsException);
276       }
277       assertNotNull(exception);
278       exception = null;
279       // Test that nothing has been written to the array.
280       assertEquals(0, array[0]);
281       assertEquals(0, array[1]);
282 
283       args[0] = null;
284       try {
285         m.invoke(null, args);
286       } catch (InvocationTargetException e) {
287         exception = e.getCause();
288         assertTrue(exception instanceof NullPointerException);
289       }
290       assertNotNull(exception);
291     }
292 
293     {
294       Method m = c.getMethod("intArrayFillInstructionAfterData", int[].class);
295       int[] array = new int[7];
296       Object[] args = { array };
297       m.invoke(null, args);
298       assertEquals(7, array.length);
299       assertEquals(1, array[0]);
300       assertEquals(2, array[1]);
301       assertEquals(3, array[2]);
302       assertEquals(4, array[3]);
303       assertEquals(5, array[4]);
304       assertEquals(0, array[5]);
305       assertEquals(0, array[6]);
306 
307       array = new int[2];
308       args[0] = array;
309       Throwable exception = null;
310       try {
311         m.invoke(null, args);
312       } catch (InvocationTargetException e) {
313         exception = e.getCause();
314         assertTrue(exception instanceof IndexOutOfBoundsException);
315       }
316       assertNotNull(exception);
317       exception = null;
318       // Test that nothing has been written to the array.
319       assertEquals(0, array[0]);
320       assertEquals(0, array[1]);
321 
322       args[0] = null;
323       try {
324         m.invoke(null, args);
325       } catch (InvocationTargetException e) {
326         exception = e.getCause();
327         assertTrue(exception instanceof NullPointerException);
328       }
329       assertNotNull(exception);
330     }
331 
332     {
333       Method m = c.getMethod("shortArray", short[].class);
334       short[] array = new short[7];
335       Object[] args = { array };
336       m.invoke(null, args);
337       assertEquals(7, array.length);
338       assertEquals(1, array[0]);
339       assertEquals(2, array[1]);
340       assertEquals(3, array[2]);
341       assertEquals(4, array[3]);
342       assertEquals(5, array[4]);
343       assertEquals(0, array[5]);
344       assertEquals(0, array[6]);
345 
346       array = new short[2];
347       args[0] = array;
348       Throwable exception = null;
349       try {
350         m.invoke(null, args);
351       } catch (InvocationTargetException e) {
352         exception = e.getCause();
353         assertTrue(exception instanceof IndexOutOfBoundsException);
354       }
355       assertNotNull(exception);
356       exception = null;
357       // Test that nothing has been written to the array.
358       assertEquals(0, array[0]);
359       assertEquals(0, array[1]);
360 
361       args[0] = null;
362       try {
363         m.invoke(null, args);
364       } catch (InvocationTargetException e) {
365         exception = e.getCause();
366         assertTrue(exception instanceof NullPointerException);
367       }
368       assertNotNull(exception);
369     }
370 
371     {
372       Method m = c.getMethod("longArray", long[].class);
373       long[] array = new long[7];
374       Object[] args = { array };
375       m.invoke(null, args);
376       assertEquals(7, array.length);
377       assertEquals(1L, array[0]);
378       assertEquals(2L, array[1]);
379       assertEquals(3L, array[2]);
380       assertEquals(4L, array[3]);
381       assertEquals(5L, array[4]);
382       assertEquals(0L, array[5]);
383       assertEquals(0L, array[6]);
384 
385       array = new long[2];
386       args[0] = array;
387       Throwable exception = null;
388       try {
389         m.invoke(null, args);
390       } catch (InvocationTargetException e) {
391         exception = e.getCause();
392         assertTrue(exception instanceof IndexOutOfBoundsException);
393       }
394       assertNotNull(exception);
395       exception = null;
396       // Test that nothing has been written to the array.
397       assertEquals(0, array[0]);
398       assertEquals(0, array[1]);
399 
400       args[0] = null;
401       try {
402         m.invoke(null, args);
403       } catch (InvocationTargetException e) {
404         exception = e.getCause();
405         assertTrue(exception instanceof NullPointerException);
406       }
407       assertNotNull(exception);
408     }
409 
410     {
411       Method m = c.getMethod("charArray", char[].class);
412       char[] array = new char[7];
413       Object[] args = { array };
414       m.invoke(null, args);
415       assertEquals(7, array.length);
416       assertEquals(1, array[0]);
417       assertEquals(2, array[1]);
418       assertEquals(3, array[2]);
419       assertEquals(4, array[3]);
420       assertEquals(5, array[4]);
421       assertEquals(0, array[5]);
422       assertEquals(0, array[6]);
423 
424       array = new char[2];
425       args[0] = array;
426       Throwable exception = null;
427       try {
428         m.invoke(null, args);
429       } catch (InvocationTargetException e) {
430         exception = e.getCause();
431         assertTrue(exception instanceof IndexOutOfBoundsException);
432       }
433       assertNotNull(exception);
434       exception = null;
435       // Test that nothing has been written to the array.
436       assertEquals(0, array[0]);
437       assertEquals(0, array[1]);
438 
439       args[0] = null;
440       try {
441         m.invoke(null, args);
442       } catch (InvocationTargetException e) {
443         exception = e.getCause();
444         assertTrue(exception instanceof NullPointerException);
445       }
446       assertNotNull(exception);
447     }
448 
449     {
450       Method m = c.getMethod("byteArray", byte[].class);
451       byte[] array = new byte[7];
452       Object[] args = { array };
453       m.invoke(null, args);
454       assertEquals(7, array.length);
455       assertEquals(1, array[0]);
456       assertEquals(2, array[1]);
457       assertEquals(3, array[2]);
458       assertEquals(4, array[3]);
459       assertEquals(5, array[4]);
460       assertEquals(0, array[5]);
461       assertEquals(0, array[6]);
462 
463       array = new byte[2];
464       args[0] = array;
465       Throwable exception = null;
466       try {
467         m.invoke(null, args);
468       } catch (InvocationTargetException e) {
469         exception = e.getCause();
470         assertTrue(exception instanceof IndexOutOfBoundsException);
471       }
472       assertNotNull(exception);
473       exception = null;
474       // Test that nothing has been written to the array.
475       assertEquals(0, array[0]);
476       assertEquals(0, array[1]);
477 
478       args[0] = null;
479       try {
480         m.invoke(null, args);
481       } catch (InvocationTargetException e) {
482         exception = e.getCause();
483         assertTrue(exception instanceof NullPointerException);
484       }
485       assertNotNull(exception);
486     }
487 
488     {
489       Method m = c.getMethod("booleanArray", boolean[].class);
490       boolean[] array = new boolean[5];
491       Object[] args = { array };
492       m.invoke(null, args);
493       assertEquals(5, array.length);
494       assertEquals(false, array[0]);
495       assertEquals(true, array[1]);
496       assertEquals(true, array[2]);
497       assertEquals(false, array[3]);
498       assertEquals(false, array[4]);
499 
500       array = new boolean[2];
501       args[0] = array;
502       Throwable exception = null;
503       try {
504         m.invoke(null, args);
505       } catch (InvocationTargetException e) {
506         exception = e.getCause();
507         assertTrue(exception instanceof IndexOutOfBoundsException);
508       }
509       assertNotNull(exception);
510       exception = null;
511       // Test that nothing has been written to the array.
512       assertEquals(false, array[0]);
513       assertEquals(false, array[1]);
514 
515       args[0] = null;
516       try {
517         m.invoke(null, args);
518       } catch (InvocationTargetException e) {
519         exception = e.getCause();
520         assertTrue(exception instanceof NullPointerException);
521       }
522       assertNotNull(exception);
523     }
524   }
525 }
526