blob: 595c70d0f661213e30502715f209787b108bee09 [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 {
Nicolas Geoffray021c5f22016-12-16 11:22:05 +000058 System.loadLibrary(nativeLibraryName);
59 // Stop the JIT to ensure its threads and work queue are not keeping classes
60 // artifically alive.
61 stopJit();
62 Runtime.getRuntime().gc();
63 System.runFinalization();
Mathieu Chartiere58991b2015-10-13 07:59:34 -070064 BufferedReader reader = new BufferedReader(new FileReader ("/proc/" + pid + "/maps"));
65 String line;
66 int count = 0;
Mathieu Chartiere58991b2015-10-13 07:59:34 -070067 while ((line = reader.readLine()) != null) {
68 if (line.contains("@141-class-unload-ex.jar")) {
69 System.out.println(line);
70 ++count;
71 }
72 }
73 System.out.println("Number of loaded unload-ex maps " + count);
Nicolas Geoffray021c5f22016-12-16 11:22:05 +000074 startJit();
Mathieu Chartiere58991b2015-10-13 07:59:34 -070075 }
76
Andreas Gampe166aaee2016-07-18 08:27:23 -070077 private static void stressTest(Constructor<?> constructor) throws Exception {
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070078 for (int i = 0; i <= 100; ++i) {
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070079 setUpUnloadLoader(constructor, false);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070080 if (i % 10 == 0) {
81 Runtime.getRuntime().gc();
82 }
83 }
84 }
85
Andreas Gampe166aaee2016-07-18 08:27:23 -070086 private static void testUnloadClass(Constructor<?> constructor) throws Exception {
Mathieu Chartierb0e861c2016-06-30 15:20:26 -070087 WeakReference<Class> klass = setUpUnloadClassWeak(constructor);
Roland Levillain91d65e02016-01-19 15:59:16 +000088 // No strong references to class loader, should get unloaded.
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070089 Runtime.getRuntime().gc();
Mathieu Chartierb0e861c2016-06-30 15:20:26 -070090 WeakReference<Class> klass2 = setUpUnloadClassWeak(constructor);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070091 Runtime.getRuntime().gc();
92 // If the weak reference is cleared, then it was unloaded.
93 System.out.println(klass.get());
94 System.out.println(klass2.get());
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070095 }
96
Andreas Gampe166aaee2016-07-18 08:27:23 -070097 private static void testUnloadLoader(Constructor<?> constructor)
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070098 throws Exception {
99 WeakReference<ClassLoader> loader = setUpUnloadLoader(constructor, true);
Roland Levillain91d65e02016-01-19 15:59:16 +0000100 // No strong references to class loader, should get unloaded.
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700101 Runtime.getRuntime().gc();
102 // If the weak reference is cleared, then it was unloaded.
103 System.out.println(loader.get());
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700104 }
105
Andreas Gampe166aaee2016-07-18 08:27:23 -0700106 private static void testStackTrace(Constructor<?> constructor) throws Exception {
107 Class<?> klass = setUpUnloadClass(constructor);
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700108 WeakReference<Class> weak_klass = new WeakReference(klass);
109 Method stackTraceMethod = klass.getDeclaredMethod("generateStackTrace");
110 Throwable throwable = (Throwable) stackTraceMethod.invoke(klass);
Mathieu Chartier910e8272015-09-30 09:24:22 -0700111 stackTraceMethod = null;
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700112 klass = null;
Mathieu Chartier910e8272015-09-30 09:24:22 -0700113 Runtime.getRuntime().gc();
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700114 boolean isNull = weak_klass.get() == null;
Mathieu Chartier910e8272015-09-30 09:24:22 -0700115 System.out.println("class null " + isNull + " " + throwable.getMessage());
116 }
117
Andreas Gampe166aaee2016-07-18 08:27:23 -0700118 private static void testLoadAndUnloadLibrary(Constructor<?> constructor) throws Exception {
Mathieu Chartier598302a2015-09-23 14:52:39 -0700119 WeakReference<ClassLoader> loader = setUpLoadLibrary(constructor);
Roland Levillain91d65e02016-01-19 15:59:16 +0000120 // No strong references to class loader, should get unloaded.
Mathieu Chartier598302a2015-09-23 14:52:39 -0700121 Runtime.getRuntime().gc();
122 // If the weak reference is cleared, then it was unloaded.
123 System.out.println(loader.get());
124 }
125
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700126 private static Object testNoUnloadHelper(ClassLoader loader) throws Exception {
Andreas Gampe166aaee2016-07-18 08:27:23 -0700127 Class<?> intHolder = loader.loadClass("IntHolder");
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700128 return intHolder.newInstance();
129 }
130
131 static class Pair {
132 public Pair(Object o, ClassLoader l) {
133 object = o;
134 classLoader = new WeakReference<ClassLoader>(l);
135 }
136
137 public Object object;
138 public WeakReference<ClassLoader> classLoader;
139 }
140
Andreas Gampe166aaee2016-07-18 08:27:23 -0700141 private static Pair testNoUnloadInstanceHelper(Constructor<?> constructor) throws Exception {
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700142 ClassLoader loader = (ClassLoader) constructor.newInstance(
143 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
144 Object o = testNoUnloadHelper(loader);
145 return new Pair(o, loader);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700146 }
147
Andreas Gampe166aaee2016-07-18 08:27:23 -0700148 private static void testNoUnloadInstance(Constructor<?> constructor) throws Exception {
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700149 Pair p = testNoUnloadInstanceHelper(constructor);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700150 Runtime.getRuntime().gc();
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700151 // If the class loader was unloded too early due to races, just pass the test.
152 boolean isNull = p.classLoader.get() == null;
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700153 System.out.println("loader null " + isNull);
154 }
155
Andreas Gampe166aaee2016-07-18 08:27:23 -0700156 private static Class<?> setUpUnloadClass(Constructor<?> constructor) throws Exception {
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700157 ClassLoader loader = (ClassLoader) constructor.newInstance(
Dimitry Ivanov3a79b632016-04-28 16:29:22 -0700158 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
Andreas Gampe166aaee2016-07-18 08:27:23 -0700159 Class<?> intHolder = loader.loadClass("IntHolder");
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700160 Method getValue = intHolder.getDeclaredMethod("getValue");
161 Method setValue = intHolder.getDeclaredMethod("setValue", Integer.TYPE);
162 // Make sure we don't accidentally preserve the value in the int holder, the class
163 // initializer should be re-run.
164 System.out.println((int) getValue.invoke(intHolder));
165 setValue.invoke(intHolder, 2);
166 System.out.println((int) getValue.invoke(intHolder));
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700167 waitForCompilation(intHolder);
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700168 return intHolder;
169 }
170
neo.chaea2d1b282016-11-08 08:40:46 +0900171 private static Object allocObjectInOtherClassLoader(Constructor<?> constructor)
172 throws Exception {
173 ClassLoader loader = (ClassLoader) constructor.newInstance(
174 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
175 return loader.loadClass("IntHolder").newInstance();
176 }
177
178 // Regression test for public issue 227182.
179 private static void testStickyUnload(Constructor<?> constructor) throws Exception {
180 String s = "";
181 for (int i = 0; i < 10; ++i) {
182 s = "";
183 // The object is the only thing preventing the class loader from being unloaded.
184 Object o = allocObjectInOtherClassLoader(constructor);
185 for (int j = 0; j < 1000; ++j) {
186 s += j + " ";
187 }
188 // Make sure the object still has a valid class (hasn't been incorrectly unloaded).
189 s += o.getClass().getName();
190 o = null;
191 }
192 System.out.println("Too small " + (s.length() < 1000));
193 }
194
Andreas Gampe166aaee2016-07-18 08:27:23 -0700195 private static WeakReference<Class> setUpUnloadClassWeak(Constructor<?> constructor)
Mathieu Chartierb0e861c2016-06-30 15:20:26 -0700196 throws Exception {
197 return new WeakReference<Class>(setUpUnloadClass(constructor));
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700198 }
199
Andreas Gampe166aaee2016-07-18 08:27:23 -0700200 private static WeakReference<ClassLoader> setUpUnloadLoader(Constructor<?> constructor,
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700201 boolean waitForCompilation)
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700202 throws Exception {
203 ClassLoader loader = (ClassLoader) constructor.newInstance(
Dimitry Ivanov3a79b632016-04-28 16:29:22 -0700204 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
Andreas Gampe166aaee2016-07-18 08:27:23 -0700205 Class<?> intHolder = loader.loadClass("IntHolder");
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700206 Method setValue = intHolder.getDeclaredMethod("setValue", Integer.TYPE);
207 setValue.invoke(intHolder, 2);
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700208 if (waitForCompilation) {
209 waitForCompilation(intHolder);
210 }
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700211 return new WeakReference(loader);
212 }
213
Andreas Gampe166aaee2016-07-18 08:27:23 -0700214 private static void waitForCompilation(Class<?> intHolder) throws Exception {
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700215 // Load the native library so that we can call waitForCompilation.
216 Method loadLibrary = intHolder.getDeclaredMethod("loadLibrary", String.class);
217 loadLibrary.invoke(intHolder, nativeLibraryName);
218 // Wait for JIT compilation to finish since the async threads may prevent unloading.
219 Method waitForCompilation = intHolder.getDeclaredMethod("waitForCompilation");
220 waitForCompilation.invoke(intHolder);
221 }
222
Andreas Gampe166aaee2016-07-18 08:27:23 -0700223 private static WeakReference<ClassLoader> setUpLoadLibrary(Constructor<?> constructor)
Mathieu Chartier598302a2015-09-23 14:52:39 -0700224 throws Exception {
225 ClassLoader loader = (ClassLoader) constructor.newInstance(
Dimitry Ivanov3a79b632016-04-28 16:29:22 -0700226 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
Andreas Gampe166aaee2016-07-18 08:27:23 -0700227 Class<?> intHolder = loader.loadClass("IntHolder");
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700228 Method loadLibrary = intHolder.getDeclaredMethod("loadLibrary", String.class);
229 loadLibrary.invoke(intHolder, nativeLibraryName);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000230 waitForCompilation(intHolder);
Mathieu Chartier598302a2015-09-23 14:52:39 -0700231 return new WeakReference(loader);
232 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700233
234 private static int getPid() throws Exception {
235 return Integer.parseInt(new File("/proc/self").getCanonicalFile().getName());
236 }
Nicolas Geoffray021c5f22016-12-16 11:22:05 +0000237
238 public static native void stopJit();
239 public static native void startJit();
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700240}