Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 1 | /* |
| 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 Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 17 | import java.io.BufferedReader; |
| 18 | import java.io.File; |
| 19 | import java.io.FileReader; |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 20 | import java.lang.ref.WeakReference; |
| 21 | import java.lang.reflect.Constructor; |
| 22 | import java.lang.reflect.Method; |
| 23 | |
| 24 | public class Main { |
| 25 | static final String DEX_FILE = System.getenv("DEX_LOCATION") + "/141-class-unload-ex.jar"; |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 26 | static final String LIBRARY_SEARCH_PATH = System.getProperty("java.library.path"); |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 27 | static String nativeLibraryName; |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 28 | |
| 29 | public static void main(String[] args) throws Exception { |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 30 | nativeLibraryName = args[0]; |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 31 | Class<?> pathClassLoader = Class.forName("dalvik.system.PathClassLoader"); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 32 | if (pathClassLoader == null) { |
| 33 | throw new AssertionError("Couldn't find path class loader class"); |
| 34 | } |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 35 | Constructor<?> constructor = |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 36 | pathClassLoader.getDeclaredConstructor(String.class, String.class, ClassLoader.class); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 37 | try { |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 38 | testUnloadClass(constructor); |
| 39 | testUnloadLoader(constructor); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 40 | // Test that we don't unload if we have an instance. |
| 41 | testNoUnloadInstance(constructor); |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 42 | // Test JNI_OnLoad and JNI_OnUnload. |
| 43 | testLoadAndUnloadLibrary(constructor); |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 44 | // Test that stack traces keep the classes live. |
| 45 | testStackTrace(constructor); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 46 | // Stress test to make sure we dont leak memory. |
| 47 | stressTest(constructor); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 48 | // Test that the oat files are unloaded. |
| 49 | testOatFilesUnloaded(getPid()); |
neo.chae | a2d1b28 | 2016-11-08 08:40:46 +0900 | [diff] [blame] | 50 | // Test that objects keep class loader live for sticky GC. |
| 51 | testStickyUnload(constructor); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 52 | } catch (Exception e) { |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 53 | e.printStackTrace(); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 57 | 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 Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 72 | private static void stressTest(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 73 | for (int i = 0; i <= 100; ++i) { |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 74 | setUpUnloadLoader(constructor, false); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 75 | if (i % 10 == 0) { |
| 76 | Runtime.getRuntime().gc(); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 81 | private static void testUnloadClass(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 82 | WeakReference<Class> klass = setUpUnloadClassWeak(constructor); |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 83 | // No strong references to class loader, should get unloaded. |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 84 | Runtime.getRuntime().gc(); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 85 | WeakReference<Class> klass2 = setUpUnloadClassWeak(constructor); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 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 Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 92 | private static void testUnloadLoader(Constructor<?> constructor) |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 93 | throws Exception { |
| 94 | WeakReference<ClassLoader> loader = setUpUnloadLoader(constructor, true); |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 95 | // No strong references to class loader, should get unloaded. |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 96 | Runtime.getRuntime().gc(); |
| 97 | // If the weak reference is cleared, then it was unloaded. |
| 98 | System.out.println(loader.get()); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 101 | private static void testStackTrace(Constructor<?> constructor) throws Exception { |
| 102 | Class<?> klass = setUpUnloadClass(constructor); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 103 | WeakReference<Class> weak_klass = new WeakReference(klass); |
| 104 | Method stackTraceMethod = klass.getDeclaredMethod("generateStackTrace"); |
| 105 | Throwable throwable = (Throwable) stackTraceMethod.invoke(klass); |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 106 | stackTraceMethod = null; |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 107 | klass = null; |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 108 | Runtime.getRuntime().gc(); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 109 | boolean isNull = weak_klass.get() == null; |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 110 | System.out.println("class null " + isNull + " " + throwable.getMessage()); |
| 111 | } |
| 112 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 113 | private static void testLoadAndUnloadLibrary(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 114 | WeakReference<ClassLoader> loader = setUpLoadLibrary(constructor); |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 115 | // No strong references to class loader, should get unloaded. |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 116 | Runtime.getRuntime().gc(); |
| 117 | // If the weak reference is cleared, then it was unloaded. |
| 118 | System.out.println(loader.get()); |
| 119 | } |
| 120 | |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 121 | private static Object testNoUnloadHelper(ClassLoader loader) throws Exception { |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 122 | Class<?> intHolder = loader.loadClass("IntHolder"); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 123 | 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 Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 136 | private static Pair testNoUnloadInstanceHelper(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 137 | 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 Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 143 | private static void testNoUnloadInstance(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 144 | Pair p = testNoUnloadInstanceHelper(constructor); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 145 | Runtime.getRuntime().gc(); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 146 | // If the class loader was unloded too early due to races, just pass the test. |
| 147 | boolean isNull = p.classLoader.get() == null; |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 148 | System.out.println("loader null " + isNull); |
| 149 | } |
| 150 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 151 | private static Class<?> setUpUnloadClass(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 152 | ClassLoader loader = (ClassLoader) constructor.newInstance( |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 153 | DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 154 | Class<?> intHolder = loader.loadClass("IntHolder"); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 155 | 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 Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 162 | waitForCompilation(intHolder); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 163 | return intHolder; |
| 164 | } |
| 165 | |
neo.chae | a2d1b28 | 2016-11-08 08:40:46 +0900 | [diff] [blame] | 166 | 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 Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 190 | private static WeakReference<Class> setUpUnloadClassWeak(Constructor<?> constructor) |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 191 | throws Exception { |
| 192 | return new WeakReference<Class>(setUpUnloadClass(constructor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 195 | private static WeakReference<ClassLoader> setUpUnloadLoader(Constructor<?> constructor, |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 196 | boolean waitForCompilation) |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 197 | throws Exception { |
| 198 | ClassLoader loader = (ClassLoader) constructor.newInstance( |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 199 | DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 200 | Class<?> intHolder = loader.loadClass("IntHolder"); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 201 | Method setValue = intHolder.getDeclaredMethod("setValue", Integer.TYPE); |
| 202 | setValue.invoke(intHolder, 2); |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 203 | if (waitForCompilation) { |
| 204 | waitForCompilation(intHolder); |
| 205 | } |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 206 | return new WeakReference(loader); |
| 207 | } |
| 208 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 209 | private static void waitForCompilation(Class<?> intHolder) throws Exception { |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 210 | // 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 Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 218 | private static WeakReference<ClassLoader> setUpLoadLibrary(Constructor<?> constructor) |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 219 | throws Exception { |
| 220 | ClassLoader loader = (ClassLoader) constructor.newInstance( |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 221 | DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 222 | Class<?> intHolder = loader.loadClass("IntHolder"); |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 223 | Method loadLibrary = intHolder.getDeclaredMethod("loadLibrary", String.class); |
| 224 | loadLibrary.invoke(intHolder, nativeLibraryName); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 225 | waitForCompilation(intHolder); |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 226 | return new WeakReference(loader); |
| 227 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 228 | |
| 229 | private static int getPid() throws Exception { |
| 230 | return Integer.parseInt(new File("/proc/self").getCanonicalFile().getName()); |
| 231 | } |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 232 | } |