blob: 17a6049dbfb7aa843bc0ebaf11d4d64d9ea16fac [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];
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070031 Class pathClassLoader = Class.forName("dalvik.system.PathClassLoader");
32 if (pathClassLoader == null) {
33 throw new AssertionError("Couldn't find path class loader class");
34 }
35 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 a Method keeping the class live.
41 testNoUnloadInvoke(constructor);
42 // Test that we don't unload if we have an instance.
43 testNoUnloadInstance(constructor);
Mathieu Chartier598302a2015-09-23 14:52:39 -070044 // Test JNI_OnLoad and JNI_OnUnload.
45 testLoadAndUnloadLibrary(constructor);
Mathieu Chartier910e8272015-09-30 09:24:22 -070046 // Test that stack traces keep the classes live.
47 testStackTrace(constructor);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070048 // Stress test to make sure we dont leak memory.
49 stressTest(constructor);
Mathieu Chartiere58991b2015-10-13 07:59:34 -070050 // Test that the oat files are unloaded.
51 testOatFilesUnloaded(getPid());
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
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070072 private static void stressTest(Constructor constructor) throws Exception {
73 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
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -070081 private static void testUnloadClass(Constructor constructor) throws Exception {
Mathieu Chartier05aa4d32015-09-19 12:44:38 -070082 WeakReference<Class> klass = setUpUnloadClass(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();
85 WeakReference<Class> klass2 = setUpUnloadClass(constructor);
86 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
92 private static void testUnloadLoader(Constructor constructor)
93 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
Mathieu Chartier910e8272015-09-30 09:24:22 -0700101 private static void testStackTrace(Constructor constructor) throws Exception {
102 WeakReference<Class> klass = setUpUnloadClass(constructor);
103 Method stackTraceMethod = klass.get().getDeclaredMethod("generateStackTrace");
104 Throwable throwable = (Throwable) stackTraceMethod.invoke(klass.get());
105 stackTraceMethod = null;
106 Runtime.getRuntime().gc();
107 boolean isNull = klass.get() == null;
108 System.out.println("class null " + isNull + " " + throwable.getMessage());
109 }
110
Mathieu Chartier598302a2015-09-23 14:52:39 -0700111 private static void testLoadAndUnloadLibrary(Constructor constructor) throws Exception {
112 WeakReference<ClassLoader> loader = setUpLoadLibrary(constructor);
Roland Levillain91d65e02016-01-19 15:59:16 +0000113 // No strong references to class loader, should get unloaded.
Mathieu Chartier598302a2015-09-23 14:52:39 -0700114 Runtime.getRuntime().gc();
115 // If the weak reference is cleared, then it was unloaded.
116 System.out.println(loader.get());
117 }
118
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700119 private static void testNoUnloadInvoke(Constructor constructor) throws Exception {
120 WeakReference<ClassLoader> loader =
121 new WeakReference((ClassLoader) constructor.newInstance(
Dimitry Ivanov3a79b632016-04-28 16:29:22 -0700122 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()));
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700123 WeakReference<Class> intHolder = new WeakReference(loader.get().loadClass("IntHolder"));
124 intHolder.get().getDeclaredMethod("runGC").invoke(intHolder.get());
125 boolean isNull = loader.get() == null;
126 System.out.println("loader null " + isNull);
127 }
128
129 private static void testNoUnloadInstance(Constructor constructor) throws Exception {
130 WeakReference<ClassLoader> loader =
131 new WeakReference((ClassLoader) constructor.newInstance(
Dimitry Ivanov3a79b632016-04-28 16:29:22 -0700132 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()));
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700133 WeakReference<Class> intHolder = new WeakReference(loader.get().loadClass("IntHolder"));
134 Object o = intHolder.get().newInstance();
135 Runtime.getRuntime().gc();
136 boolean isNull = loader.get() == null;
137 System.out.println("loader null " + isNull);
138 }
139
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700140 private static WeakReference<Class> setUpUnloadClass(Constructor constructor) throws Exception {
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700141 ClassLoader loader = (ClassLoader) constructor.newInstance(
Dimitry Ivanov3a79b632016-04-28 16:29:22 -0700142 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700143 Class intHolder = loader.loadClass("IntHolder");
144 Method getValue = intHolder.getDeclaredMethod("getValue");
145 Method setValue = intHolder.getDeclaredMethod("setValue", Integer.TYPE);
146 // Make sure we don't accidentally preserve the value in the int holder, the class
147 // initializer should be re-run.
148 System.out.println((int) getValue.invoke(intHolder));
149 setValue.invoke(intHolder, 2);
150 System.out.println((int) getValue.invoke(intHolder));
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700151 waitForCompilation(intHolder);
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700152 return new WeakReference(intHolder);
153 }
154
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700155 private static WeakReference<ClassLoader> setUpUnloadLoader(Constructor constructor,
156 boolean waitForCompilation)
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700157 throws Exception {
158 ClassLoader loader = (ClassLoader) constructor.newInstance(
Dimitry Ivanov3a79b632016-04-28 16:29:22 -0700159 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700160 Class intHolder = loader.loadClass("IntHolder");
161 Method setValue = intHolder.getDeclaredMethod("setValue", Integer.TYPE);
162 setValue.invoke(intHolder, 2);
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700163 if (waitForCompilation) {
164 waitForCompilation(intHolder);
165 }
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700166 return new WeakReference(loader);
167 }
168
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700169 private static void waitForCompilation(Class intHolder) throws Exception {
170 // Load the native library so that we can call waitForCompilation.
171 Method loadLibrary = intHolder.getDeclaredMethod("loadLibrary", String.class);
172 loadLibrary.invoke(intHolder, nativeLibraryName);
173 // Wait for JIT compilation to finish since the async threads may prevent unloading.
174 Method waitForCompilation = intHolder.getDeclaredMethod("waitForCompilation");
175 waitForCompilation.invoke(intHolder);
176 }
177
Mathieu Chartier598302a2015-09-23 14:52:39 -0700178 private static WeakReference<ClassLoader> setUpLoadLibrary(Constructor constructor)
179 throws Exception {
180 ClassLoader loader = (ClassLoader) constructor.newInstance(
Dimitry Ivanov3a79b632016-04-28 16:29:22 -0700181 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
Mathieu Chartier598302a2015-09-23 14:52:39 -0700182 Class intHolder = loader.loadClass("IntHolder");
Mathieu Chartiera50f9cf2015-09-25 11:34:45 -0700183 Method loadLibrary = intHolder.getDeclaredMethod("loadLibrary", String.class);
184 loadLibrary.invoke(intHolder, nativeLibraryName);
Nicolas Geoffrayb6e20ae2016-03-07 14:29:04 +0000185 waitForCompilation(intHolder);
Mathieu Chartier598302a2015-09-23 14:52:39 -0700186 return new WeakReference(loader);
187 }
Mathieu Chartiere58991b2015-10-13 07:59:34 -0700188
189 private static int getPid() throws Exception {
190 return Integer.parseInt(new File("/proc/self").getCanonicalFile().getName());
191 }
Mathieu Chartier05aa4d32015-09-19 12:44:38 -0700192}