blob: 212c894bd5a44c564a9a4609ae19c09b44057a7b [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
2 * Copyright (C) 2010 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
Andreas Gampea727e372015-08-25 09:22:37 -070017import java.lang.reflect.InvocationTargetException;
18import java.lang.reflect.Method;
19import java.lang.reflect.Modifier;
jeffhao5d1ac922011-09-29 17:41:15 -070020
21/*
22 * Entry point and tests that are expected to succeed.
23 */
24public class Main {
25 /**
26 * Drives tests.
27 */
28 public static void main(String[] args) {
Mathieu Chartier6d87eba2015-08-27 18:03:24 -070029 System.loadLibrary(args[0]);
Andreas Gampe03ec9302015-08-27 17:41:47 -070030 if (!hasOatFile() || runtimeIsSoftFail() || isInterpreted()) {
31 // Some tests ensure that the verifier was able to guarantee balanced locking by
32 // asserting that the test function is running as compiled code. But skip this now,
33 // as this seems to be a non-compiled code test configuration.
34 disableStackFrameAsserts();
35 }
36
jeffhao5d1ac922011-09-29 17:41:15 -070037 Main m = new Main();
38
39 m.recursiveSync(0);
40
41 m.nestedMayThrow(false);
42 try {
43 m.nestedMayThrow(true);
44 System.err.println("nestedThrow(true) did not throw");
45 } catch (MyException me) {}
46 System.out.println("nestedMayThrow ok");
47
48 m.constantLock();
49 System.out.println("constantLock ok");
50
51 m.notExcessiveNesting();
jeffhao5d1ac922011-09-29 17:41:15 -070052
53 m.notNested();
54 System.out.println("notNested ok");
55
56 Object obj1 = new Object();
57 Object obj2 = new Object();
58
Andreas Gampe03ec9302015-08-27 17:41:47 -070059 TwoPath.twoPath(obj1, obj2, 0);
jeffhao5d1ac922011-09-29 17:41:15 -070060 System.out.println("twoPath ok");
61
62 m.triplet(obj1, obj2, 0);
63 System.out.println("triplet ok");
Andreas Gampea727e372015-08-25 09:22:37 -070064
Andreas Gampea727e372015-08-25 09:22:37 -070065 runSmaliTests();
jeffhao5d1ac922011-09-29 17:41:15 -070066 }
67
68 /**
69 * Recursive synchronized method.
70 */
71 synchronized void recursiveSync(int iter) {
Andreas Gampe03ec9302015-08-27 17:41:47 -070072 assertIsManaged();
jeffhao5d1ac922011-09-29 17:41:15 -070073 if (iter < 40) {
74 recursiveSync(iter+1);
75 } else {
76 System.out.println("recursiveSync ok");
77 }
78 }
79
80 /**
81 * Tests simple nesting, with and without a throw.
82 */
83 void nestedMayThrow(boolean doThrow) {
Andreas Gampe03ec9302015-08-27 17:41:47 -070084 assertIsManaged();
jeffhao5d1ac922011-09-29 17:41:15 -070085 synchronized (this) {
86 synchronized (Main.class) {
87 synchronized (new Object()) {
88 synchronized(Class.class) {
89 if (doThrow) {
90 throw new MyException();
91 }
92 }
93 }
94 }
95 }
96 }
97
98 /**
99 * Exercises bug 3215458.
100 */
101 void constantLock() {
Andreas Gampe03ec9302015-08-27 17:41:47 -0700102 assertIsManaged();
jeffhao5d1ac922011-09-29 17:41:15 -0700103 Class thing = Thread.class;
104 synchronized (Thread.class) {}
105 }
106
107 /**
108 * Confirms that we can have 32 nested monitors on one method.
109 */
110 void notExcessiveNesting() {
Andreas Gampe03ec9302015-08-27 17:41:47 -0700111 assertIsManaged();
jeffhao5d1ac922011-09-29 17:41:15 -0700112 synchronized (this) { // 1
113 synchronized (this) { // 2
114 synchronized (this) { // 3
115 synchronized (this) { // 4
116 synchronized (this) { // 5
117 synchronized (this) { // 6
118 synchronized (this) { // 7
119 synchronized (this) { // 8
120 synchronized (this) { // 9
121 synchronized (this) { // 10
122 synchronized (this) { // 11
123 synchronized (this) { // 12
124 synchronized (this) { // 13
125 synchronized (this) { // 14
126 synchronized (this) { // 15
127 synchronized (this) { // 16
128 synchronized (this) { // 17
129 synchronized (this) { // 18
130 synchronized (this) { // 19
131 synchronized (this) { // 20
132 synchronized (this) { // 21
133 synchronized (this) { // 22
134 synchronized (this) { // 23
135 synchronized (this) { // 24
136 synchronized (this) { // 25
137 synchronized (this) { // 26
138 synchronized (this) { // 27
139 synchronized (this) { // 28
140 synchronized (this) { // 29
141 synchronized (this) { // 30
142 synchronized (this) { // 31
143 synchronized (this) { // 32
144 }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
145 }
146
147 /**
148 * Confirms that we can have more than 32 non-nested monitors in one
149 * method.
150 */
151 void notNested() {
Andreas Gampe03ec9302015-08-27 17:41:47 -0700152 assertIsManaged();
jeffhao5d1ac922011-09-29 17:41:15 -0700153 synchronized (this) {} // 1
154 synchronized (this) {} // 2
155 synchronized (this) {} // 3
156 synchronized (this) {} // 4
157 synchronized (this) {} // 5
158 synchronized (this) {} // 6
159 synchronized (this) {} // 7
160 synchronized (this) {} // 8
161 synchronized (this) {} // 9
162 synchronized (this) {} // 10
163 synchronized (this) {} // 11
164 synchronized (this) {} // 12
165 synchronized (this) {} // 13
166 synchronized (this) {} // 14
167 synchronized (this) {} // 15
168 synchronized (this) {} // 16
169 synchronized (this) {} // 17
170 synchronized (this) {} // 18
171 synchronized (this) {} // 19
172 synchronized (this) {} // 20
173 synchronized (this) {} // 21
174 synchronized (this) {} // 22
175 synchronized (this) {} // 23
176 synchronized (this) {} // 24
177 synchronized (this) {} // 25
178 synchronized (this) {} // 26
179 synchronized (this) {} // 27
180 synchronized (this) {} // 28
181 synchronized (this) {} // 29
182 synchronized (this) {} // 30
183 synchronized (this) {} // 31
184 synchronized (this) {} // 32
185 synchronized (this) {} // 33
186 synchronized (this) {} // 34
187 }
188
189 /* does nothing but ensure that the compiler doesn't discard an object */
190 private void doNothing(Object obj) {}
191
192 /**
jeffhao5d1ac922011-09-29 17:41:15 -0700193 * Lock the monitor two or three times, and make use of the locked or
194 * unlocked object.
195 */
196 public void triplet(Object obj1, Object obj2, int x) {
197 Object localObj;
198
199 synchronized (obj1) {
200 synchronized(obj1) {
201 if (x == 0) {
202 synchronized(obj1) {
203 localObj = obj2;
204 }
205 } else {
206 localObj = obj1;
207 }
208 }
209 }
210
211 doNothing(localObj);
212 }
Andreas Gampea727e372015-08-25 09:22:37 -0700213
214 // Smali testing code.
215 private static void runSmaliTests() {
216 runTest("OK", new Object[] { new Object(), new Object() }, null);
217 runTest("TooDeep", new Object[] { new Object() }, null);
218 runTest("NotStructuredOverUnlock", new Object[] { new Object() },
219 IllegalMonitorStateException.class);
Andreas Gampe03ec9302015-08-27 17:41:47 -0700220 runTest("NotStructuredUnderUnlock", new Object[] { new Object() },
221 IllegalMonitorStateException.class);
Andreas Gampea727e372015-08-25 09:22:37 -0700222 runTest("UnbalancedJoin", new Object[] { new Object(), new Object() }, null);
223 runTest("UnbalancedStraight", new Object[] { new Object(), new Object() }, null);
Andreas Gampe895bb5f2015-10-14 12:55:48 -0700224 runTest("NullLocks", new Object[] { false }, null);
225 runTest("NullLocks", new Object[] { true }, NullPointerException.class);
Andreas Gampea727e372015-08-25 09:22:37 -0700226 }
227
228 private static void runTest(String className, Object[] parameters, Class<?> excType) {
Andreas Gampea727e372015-08-25 09:22:37 -0700229 try {
230 Class<?> c = Class.forName(className);
231
232 Method[] methods = c.getDeclaredMethods();
233
234 // For simplicity we assume that test methods are not overloaded. So searching by name
235 // will give us the method we need to run.
236 Method method = null;
237 for (Method m : methods) {
238 if (m.getName().equals("run")) {
239 method = m;
240 break;
241 }
242 }
243
244 if (method == null) {
245 System.out.println("Could not find test method for " + className);
246 } else if (!Modifier.isStatic(method.getModifiers())) {
247 System.out.println("Test method for " + className + " is not static.");
248 } else {
249 method.invoke(null, parameters);
250 if (excType != null) {
251 System.out.println("Expected an exception in " + className);
252 }
253 }
254 } catch (Throwable exc) {
255 if (excType == null) {
256 System.out.println("Did not expect exception " + exc + " for " + className);
257 exc.printStackTrace(System.out);
258 } else if (exc instanceof InvocationTargetException && exc.getCause() != null &&
259 exc.getCause().getClass().equals(excType)) {
260 // Expected exception is wrapped in InvocationTargetException.
261 } else if (!excType.equals(exc.getClass())) {
262 System.out.println("Expected " + excType.getName() + ", but got " + exc.getClass());
263 } else {
264 // Expected exception, do nothing.
265 }
266 }
267 }
268
269 // Helpers for the smali code.
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700270 public static native void assertIsInterpreted();
271 public static native void assertIsManaged();
Andreas Gampe89df7bf2015-09-30 13:13:21 -0700272 public static native boolean hasOatFile();
273 public static native boolean runtimeIsSoftFail();
Andreas Gampe0dfc9bc2015-09-30 17:13:59 -0700274 public static native boolean isInterpreted();
Andreas Gampe03ec9302015-08-27 17:41:47 -0700275 public static native void disableStackFrameAsserts();
jeffhao5d1ac922011-09-29 17:41:15 -0700276}