blob: 2a6e9442e8cde572e5f50d58354c285c42a8a752 [file] [log] [blame]
Mathieu Chartier05aa4d32015-09-19 12:44:38 -07001/*
2 * Copyright (C) 2015 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
Mathieu Chartiere58991b2015-10-13 07:59:34 -070017import java.io.BufferedReader;
18import java.io.File;
19import java.io.FileReader;
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070020import java.lang.ref.WeakReference;
21import java.lang.reflect.Constructor;
22import java.lang.reflect.Method;
23
24public class Main {
25 static final String DEX_FILE = System.getenv("DEX_LOCATION") + "/141-class-unload-ex.jar";
Dimitry Ivanov3a79b632016-04-28 16:29:22 -070026 static final String LIBRARY_SEARCH_PATH = System.getProperty("java.library.path");
Mathieu Chartier598302a2015-09-23 14:52:39 -070027 static String nativeLibraryName;
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070028
29 public static void main(String[] args) throws Exception {
Mathieu Chartier598302a2015-09-23 14:52:39 -070030 nativeLibraryName = args[0];
Andreas Gampe166aaee2016-07-18 08:27:23 -070031 Class<?> pathClassLoader = Class.forName("dalvik.system.PathClassLoader");
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070032 if (pathClassLoader == null) {
33 throw new AssertionError("Couldn't find path class loader class");
34 }
Andreas Gampe166aaee2016-07-18 08:27:23 -070035 Constructor<?> constructor =
Dimitry Ivanov3a79b632016-04-28 16:29:22 -070036 pathClassLoader.getDeclaredConstructor(String.class, String.class, ClassLoader.class);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070037 try {
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070038 testUnloadClass(constructor);
39 testUnloadLoader(constructor);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070040 // Test that we don't unload if we have an instance.
41 testNoUnloadInstance(constructor);
Mathieu Chartier598302a2015-09-23 14:52:39 -070042 // Test JNI_OnLoad and JNI_OnUnload.
43 testLoadAndUnloadLibrary(constructor);
Mathieu Chartier910e8272015-09-30 09:24:22 -070044 // Test that stack traces keep the classes live.
45 testStackTrace(constructor);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070046 // Stress test to make sure we dont leak memory.
47 stressTest(constructor);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070048 // Test that the oat files are unloaded.
49 testOatFilesUnloaded(getPid());
neo.chaea2d1b282016-11-08 08:40:46 +090050 // Test that objects keep class loader live for sticky GC.
51 testStickyUnload(constructor);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070052 } catch (Exception e) {
Dimitry Ivanov3a79b632016-04-28 16:29:22 -070053 e.printStackTrace();
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070054 }
55 }
56
Mathieu Chartiere58991b2015-10-13 07:59:34 -070057 private static void testOatFilesUnloaded(int pid) throws Exception {
58 BufferedReader reader = new BufferedReader(new FileReader ("/proc/" + pid + "/maps"));
59 String line;
60 int count = 0;
61 Runtime.getRuntime().gc();
62 System.runFinalization();
63 while ((line = reader.readLine()) != null) {
64 if (line.contains("@141-class-unload-ex.jar")) {
65 System.out.println(line);
66 ++count;
67 }
68 }
69 System.out.println("Number of loaded unload-ex maps " + count);
70 }
71
Andreas Gampe166aaee2016-07-18 08:27:23 -070072 private static void stressTest(Constructor<?> constructor) throws Exception {
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070073 for (int i = 0; i <= 100; ++i) {
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070074 setUpUnloadLoader(constructor, false);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070075 if (i % 10 == 0) {
76 Runtime.getRuntime().gc();
77 }
78 }
79 }
80
Andreas Gampe166aaee2016-07-18 08:27:23 -070081 private static void testUnloadClass(Constructor<?> constructor) throws Exception {
Mathieu Chartierb0e861c2016-06-30 15:20:26 -070082 WeakReference<Class> klass = setUpUnloadClassWeak(constructor);
Roland Levillain91d65e02016-01-19 15:59:16 +000083 // No strong references to class loader, should get unloaded.
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070084 Runtime.getRuntime().gc();
Mathieu Chartierb0e861c2016-06-30 15:20:26 -070085 WeakReference<Class> klass2 = setUpUnloadClassWeak(constructor);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070086 Runtime.getRuntime().gc();
87 // If the weak reference is cleared, then it was unloaded.
88 System.out.println(klass.get());
89 System.out.println(klass2.get());
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070090 }
91
Andreas Gampe166aaee2016-07-18 08:27:23 -070092 private static void testUnloadLoader(Constructor<?> constructor)
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070093 throws Exception {
94 WeakReference<ClassLoader> loader = setUpUnloadLoader(constructor, true);
Roland Levillain91d65e02016-01-19 15:59:16 +000095 // No strong references to class loader, should get unloaded.
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070096 Runtime.getRuntime().gc();
97 // If the weak reference is cleared, then it was unloaded.
98 System.out.println(loader.get());
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070099 }
100
Andreas Gampe166aaee2016-07-18 08:27:23 -0700101 private static void testStackTrace(Constructor<?> constructor) throws Exception {
102 Class<?> klass = setUpUnloadClass(constructor);
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700103 WeakReference<Class> weak_klass = new WeakReference(klass);
104 Method stackTraceMethod = klass.getDeclaredMethod("generateStackTrace");
105 Throwable throwable = (Throwable) stackTraceMethod.invoke(klass);
Mathieu Chartier910e8272015-09-30 09:24:22 -0700106 stackTraceMethod = null;
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700107 klass = null;
Mathieu Chartier910e8272015-09-30 09:24:22 -0700108 Runtime.getRuntime().gc();
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700109 boolean isNull = weak_klass.get() == null;
Mathieu Chartier910e8272015-09-30 09:24:22 -0700110 System.out.println("class null " + isNull + " " + throwable.getMessage());
111 }
112
Andreas Gampe166aaee2016-07-18 08:27:23 -0700113 private static void testLoadAndUnloadLibrary(Constructor<?> constructor) throws Exception {
Mathieu Chartier598302a2015-09-23 14:52:39 -0700114 WeakReference<ClassLoader> loader = setUpLoadLibrary(constructor);
Roland Levillain91d65e02016-01-19 15:59:16 +0000115 // No strong references to class loader, should get unloaded.
Mathieu Chartier598302a2015-09-23 14:52:39 -0700116 Runtime.getRuntime().gc();
117 // If the weak reference is cleared, then it was unloaded.
118 System.out.println(loader.get());
119 }
120
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700121 private static Object testNoUnloadHelper(ClassLoader loader) throws Exception {
Andreas Gampe166aaee2016-07-18 08:27:23 -0700122 Class<?> intHolder = loader.loadClass("IntHolder");
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700123 return intHolder.newInstance();
124 }
125
126 static class Pair {
127 public Pair(Object o, ClassLoader l) {
128 object = o;
129 classLoader = new WeakReference<ClassLoader>(l);
130 }
131
132 public Object object;
133 public WeakReference<ClassLoader> classLoader;
134 }
135
Andreas Gampe166aaee2016-07-18 08:27:23 -0700136 private static Pair testNoUnloadInstanceHelper(Constructor<?> constructor) throws Exception {
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700137 ClassLoader loader = (ClassLoader) constructor.newInstance(
138 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
139 Object o = testNoUnloadHelper(loader);
140 return new Pair(o, loader);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700141 }
142
Andreas Gampe166aaee2016-07-18 08:27:23 -0700143 private static void testNoUnloadInstance(Constructor<?> constructor) throws Exception {
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700144 Pair p = testNoUnloadInstanceHelper(constructor);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700145 Runtime.getRuntime().gc();
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700146 // If the class loader was unloded too early due to races, just pass the test.
147 boolean isNull = p.classLoader.get() == null;
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700148 System.out.println("loader null " + isNull);
149 }
150
Andreas Gampe166aaee2016-07-18 08:27:23 -0700151 private static Class<?> setUpUnloadClass(Constructor<?> constructor) throws Exception {
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700152 ClassLoader loader = (ClassLoader) constructor.newInstance(
Dimitry Ivanov3a79b632016-04-28 16:29:22 -0700153 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
Andreas Gampe166aaee2016-07-18 08:27:23 -0700154 Class<?> intHolder = loader.loadClass("IntHolder");
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700155 Method getValue = intHolder.getDeclaredMethod("getValue");
156 Method setValue = intHolder.getDeclaredMethod("setValue", Integer.TYPE);
157 // Make sure we don't accidentally preserve the value in the int holder, the class
158 // initializer should be re-run.
159 System.out.println((int) getValue.invoke(intHolder));
160 setValue.invoke(intHolder, 2);
161 System.out.println((int) getValue.invoke(intHolder));
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700162 waitForCompilation(intHolder);
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700163 return intHolder;
164 }
165
neo.chaea2d1b282016-11-08 08:40:46 +0900166 private static Object allocObjectInOtherClassLoader(Constructor<?> constructor)
167 throws Exception {
168 ClassLoader loader = (ClassLoader) constructor.newInstance(
169 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
170 return loader.loadClass("IntHolder").newInstance();
171 }
172
173 // Regression test for public issue 227182.
174 private static void testStickyUnload(Constructor<?> constructor) throws Exception {
175 String s = "";
176 for (int i = 0; i < 10; ++i) {
177 s = "";
178 // The object is the only thing preventing the class loader from being unloaded.
179 Object o = allocObjectInOtherClassLoader(constructor);
180 for (int j = 0; j < 1000; ++j) {
181 s += j + " ";
182 }
183 // Make sure the object still has a valid class (hasn't been incorrectly unloaded).
184 s += o.getClass().getName();
185 o = null;
186 }
187 System.out.println("Too small " + (s.length() < 1000));
188 }
189
Andreas Gampe166aaee2016-07-18 08:27:23 -0700190 private static WeakReference<Class> setUpUnloadClassWeak(Constructor<?> constructor)
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700191 throws Exception {
192 return new WeakReference<Class>(setUpUnloadClass(constructor));
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700193 }
194
Andreas Gampe166aaee2016-07-18 08:27:23 -0700195 private static WeakReference<ClassLoader> setUpUnloadLoader(Constructor<?> constructor,
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700196 boolean waitForCompilation)
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700197 throws Exception {
198 ClassLoader loader = (ClassLoader) constructor.newInstance(
Dimitry Ivanov3a79b632016-04-28 16:29:22 -0700199 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
Andreas Gampe166aaee2016-07-18 08:27:23 -0700200 Class<?> intHolder = loader.loadClass("IntHolder");
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700201 Method setValue = intHolder.getDeclaredMethod("setValue", Integer.TYPE);
202 setValue.invoke(intHolder, 2);
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700203 if (waitForCompilation) {
204 waitForCompilation(intHolder);
205 }
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700206 return new WeakReference(loader);
207 }
208
Andreas Gampe166aaee2016-07-18 08:27:23 -0700209 private static void waitForCompilation(Class<?> intHolder) throws Exception {
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700210 // Load the native library so that we can call waitForCompilation.
211 Method loadLibrary = intHolder.getDeclaredMethod("loadLibrary", String.class);
212 loadLibrary.invoke(intHolder, nativeLibraryName);
213 // Wait for JIT compilation to finish since the async threads may prevent unloading.
214 Method waitForCompilation = intHolder.getDeclaredMethod("waitForCompilation");
215 waitForCompilation.invoke(intHolder);
216 }
217
Andreas Gampe166aaee2016-07-18 08:27:23 -0700218 private static WeakReference<ClassLoader> setUpLoadLibrary(Constructor<?> constructor)
Mathieu Chartier598302a2015-09-23 14:52:39 -0700219 throws Exception {
220 ClassLoader loader = (ClassLoader) constructor.newInstance(
Dimitry Ivanov3a79b632016-04-28 16:29:22 -0700221 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
Andreas Gampe166aaee2016-07-18 08:27:23 -0700222 Class<?> intHolder = loader.loadClass("IntHolder");
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700223 Method loadLibrary = intHolder.getDeclaredMethod("loadLibrary", String.class);
224 loadLibrary.invoke(intHolder, nativeLibraryName);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000225 waitForCompilation(intHolder);
Mathieu Chartier598302a2015-09-23 14:52:39 -0700226 return new WeakReference(loader);
227 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700228
229 private static int getPid() throws Exception {
230 return Integer.parseInt(new File("/proc/self").getCanonicalFile().getName());
231 }
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700232}