1 /*
2  * Copyright (C) 2016 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.Method;
18 import java.lang.reflect.InvocationTargetException;
19 
20 public class Main {
assertIsInterpreted()21   public static native void assertIsInterpreted();
ensureJitCompiled(Class<?> cls, String methodName)22   public static native void ensureJitCompiled(Class<?> cls, String methodName);
23 
assertEqual(String expected, String actual)24   public static void assertEqual(String expected, String actual) {
25     if (!expected.equals(actual)) {
26       throw new Error("Assertion failed: " + expected + " != " + actual);
27     }
28   }
29 
assertEqual(byte[] expected, String actual)30   public static void assertEqual(byte[] expected, String actual) throws Exception {
31     String str = new String(expected, "UTF8");
32     if (!str.equals(actual)) {
33       throw new Error("Assertion failed: " + str + " != " + actual);
34     }
35   }
36 
37   // Create an empty int[] to force loading the int[] class before compiling
38   // TestCase.deoptimizeNewInstance.
39   // This makes sure the compiler can properly type int[] and not bail.
40   static int[] emptyArray = new int[0];
41 
main(String[] args)42   public static void main(String[] args) throws Throwable {
43     System.loadLibrary(args[0]);
44     Class<?> c = Class.forName("TestCase");
45     String testString = "Hello world";
46     byte[] testData = testString.getBytes("UTF8");
47 
48     {
49       Method m = c.getMethod("vregAliasing", byte[].class);
50       String result = (String) m.invoke(null, new Object[] { testData });
51       assertEqual(testString, result);
52     }
53 
54     {
55       c.getMethod("compareNewInstance").invoke(null, (Object[]) null);
56     }
57 
58     {
59       // If the JIT is enabled, ensure it has compiled the method to force the deopt.
60       ensureJitCompiled(c, "deoptimizeNewInstance");
61       Method m = c.getMethod("deoptimizeNewInstance", int[].class, byte[].class);
62       try {
63         m.invoke(null, new Object[] { new int[] { 1, 2, 3 }, testData });
64       } catch (InvocationTargetException ex) {
65         if (ex.getCause() instanceof ArrayIndexOutOfBoundsException) {
66           // Expected.
67         } else {
68           throw ex.getCause();
69         }
70       }
71     }
72 
73     {
74       Method m = c.getMethod("removeNewInstance", byte[].class);
75       String result = (String) m.invoke(null, new Object[] { testData });
76       assertEqual(testString, result);
77     }
78 
79     {
80       Method m = c.getMethod("irreducibleLoopAndStringInit1", byte[].class, boolean.class);
81       String result = (String) m.invoke(null, new Object[] { testData, true });
82       assertEqual(testString, result);
83       result = (String) m.invoke(null, new Object[] { testData, false });
84       assertEqual(testString, result);
85     }
86     {
87       Method m = c.getMethod("irreducibleLoopAndStringInit2", byte[].class, boolean.class);
88       String result = (String) m.invoke(null, new Object[] { testData, true });
89       assertEqual(testString, result);
90       result = (String) m.invoke(null, new Object[] { testData, false });
91       assertEqual(testString, result);
92     }
93     {
94       Method m = c.getMethod("irreducibleLoopAndStringInit3", byte[].class, boolean.class);
95       String result = (String) m.invoke(null, new Object[] { testData, true });
96       assertEqual(testString, result);
97       result = (String) m.invoke(null, new Object[] { testData, false });
98       assertEqual(testString, result);
99     }
100     {
101       Method m = c.getMethod("loopAndStringInit", byte[].class, boolean.class);
102       String result = (String) m.invoke(null, new Object[] { testData, true });
103       assertEqual(testString, result);
104       result = (String) m.invoke(null, new Object[] { testData, false });
105       assertEqual(testString, result);
106     }
107     {
108       Method m = c.getMethod("loopAndStringInitAlias", byte[].class, boolean.class);
109       String result = (String) m.invoke(null, new Object[] { testData, true });
110       assertEqual(testString, result);
111       result = (String) m.invoke(null, new Object[] { testData, false });
112       assertEqual(testString, result);
113     }
114     {
115       Method m = c.getMethod("loopAndStringInitAndTest", byte[].class, boolean.class);
116       String result = (String) m.invoke(null, new Object[] { testData, true });
117       assertEqual(testString, result);
118       result = (String) m.invoke(null, new Object[] { testData, false });
119       assertEqual(testString, result);
120     }
121     {
122       // If the JIT is enabled, ensure it has compiled the method to force the deopt.
123       ensureJitCompiled(c, "deoptimizeNewInstanceAfterLoop");
124       Method m = c.getMethod(
125           "deoptimizeNewInstanceAfterLoop", int[].class, byte[].class, int.class);
126       try {
127         m.invoke(null, new Object[] { new int[] { 1, 2, 3 }, testData, 0 });
128       } catch (InvocationTargetException ex) {
129         if (ex.getCause() instanceof ArrayIndexOutOfBoundsException) {
130           // Expected.
131         } else {
132           throw ex.getCause();
133         }
134       }
135     }
136     {
137       Method m = c.getMethod("loopAndStringInitAndPhi", byte[].class, boolean.class);
138       String result = (String) m.invoke(null, new Object[] { testData, true });
139       assertEqual(testString, result);
140       result = (String) m.invoke(null, new Object[] { testData, false });
141       assertEqual(testString, result);
142     }
143     {
144       Method m =
145           c.getMethod("loopAndTwoStringInitAndPhi", byte[].class, boolean.class, boolean.class);
146       String result = (String) m.invoke(null, new Object[] { testData, false, false });
147       assertEqual(testString, result);
148       result = (String) m.invoke(null, new Object[] { testData, false, true });
149       assertEqual(testString, result);
150     }
151     {
152       Method m = c.getMethod("stringAndCatch", byte[].class, boolean.class);
153       String result = (String) m.invoke(null, new Object[] { testData, false });
154       assertEqual(testString, result);
155     }
156     {
157       Method m = c.getMethod("stringAndCatch2", byte[].class, boolean.class);
158       String result = (String) m.invoke(null, new Object[] { testData, false });
159       assertEqual(testString, result);
160     }
161     {
162       Method m = c.getMethod("stringAndCatch3", byte[].class, boolean.class);
163       String result = (String) m.invoke(null, new Object[] { testData, false });
164       assertEqual(testString, result);
165     }
166   }
167 
$noinline$HiddenNull()168   public static Object $noinline$HiddenNull() {
169     return null;
170   }
171 }
172