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 { |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 58 | 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 Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 64 | BufferedReader reader = new BufferedReader(new FileReader ("/proc/" + pid + "/maps")); |
| 65 | String line; |
| 66 | int count = 0; |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 67 | 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 Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 74 | startJit(); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 77 | private static void stressTest(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 78 | for (int i = 0; i <= 100; ++i) { |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 79 | setUpUnloadLoader(constructor, false); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 80 | if (i % 10 == 0) { |
| 81 | Runtime.getRuntime().gc(); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 86 | private static void testUnloadClass(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 87 | WeakReference<Class> klass = setUpUnloadClassWeak(constructor); |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 88 | // No strong references to class loader, should get unloaded. |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 89 | Runtime.getRuntime().gc(); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 90 | WeakReference<Class> klass2 = setUpUnloadClassWeak(constructor); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 91 | 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 Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 97 | private static void testUnloadLoader(Constructor<?> constructor) |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 98 | throws Exception { |
| 99 | WeakReference<ClassLoader> loader = setUpUnloadLoader(constructor, true); |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 100 | // No strong references to class loader, should get unloaded. |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 101 | Runtime.getRuntime().gc(); |
| 102 | // If the weak reference is cleared, then it was unloaded. |
| 103 | System.out.println(loader.get()); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 106 | private static void testStackTrace(Constructor<?> constructor) throws Exception { |
| 107 | Class<?> klass = setUpUnloadClass(constructor); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 108 | WeakReference<Class> weak_klass = new WeakReference(klass); |
| 109 | Method stackTraceMethod = klass.getDeclaredMethod("generateStackTrace"); |
| 110 | Throwable throwable = (Throwable) stackTraceMethod.invoke(klass); |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 111 | stackTraceMethod = null; |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 112 | klass = null; |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 113 | Runtime.getRuntime().gc(); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 114 | boolean isNull = weak_klass.get() == null; |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 115 | System.out.println("class null " + isNull + " " + throwable.getMessage()); |
| 116 | } |
| 117 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 118 | private static void testLoadAndUnloadLibrary(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 119 | WeakReference<ClassLoader> loader = setUpLoadLibrary(constructor); |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 120 | // No strong references to class loader, should get unloaded. |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 121 | Runtime.getRuntime().gc(); |
| 122 | // If the weak reference is cleared, then it was unloaded. |
| 123 | System.out.println(loader.get()); |
| 124 | } |
| 125 | |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 126 | private static Object testNoUnloadHelper(ClassLoader loader) throws Exception { |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 127 | Class<?> intHolder = loader.loadClass("IntHolder"); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 128 | 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 Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 141 | private static Pair testNoUnloadInstanceHelper(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 142 | 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 Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 148 | private static void testNoUnloadInstance(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 149 | Pair p = testNoUnloadInstanceHelper(constructor); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 150 | Runtime.getRuntime().gc(); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 151 | // If the class loader was unloded too early due to races, just pass the test. |
| 152 | boolean isNull = p.classLoader.get() == null; |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 153 | System.out.println("loader null " + isNull); |
| 154 | } |
| 155 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 156 | private static Class<?> setUpUnloadClass(Constructor<?> constructor) throws Exception { |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 157 | ClassLoader loader = (ClassLoader) constructor.newInstance( |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 158 | DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 159 | Class<?> intHolder = loader.loadClass("IntHolder"); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 160 | 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 Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 167 | waitForCompilation(intHolder); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 168 | return intHolder; |
| 169 | } |
| 170 | |
neo.chae | a2d1b28 | 2016-11-08 08:40:46 +0900 | [diff] [blame] | 171 | 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 Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 195 | private static WeakReference<Class> setUpUnloadClassWeak(Constructor<?> constructor) |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame] | 196 | throws Exception { |
| 197 | return new WeakReference<Class>(setUpUnloadClass(constructor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 200 | private static WeakReference<ClassLoader> setUpUnloadLoader(Constructor<?> constructor, |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 201 | boolean waitForCompilation) |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 202 | throws Exception { |
| 203 | ClassLoader loader = (ClassLoader) constructor.newInstance( |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 204 | DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 205 | Class<?> intHolder = loader.loadClass("IntHolder"); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 206 | Method setValue = intHolder.getDeclaredMethod("setValue", Integer.TYPE); |
| 207 | setValue.invoke(intHolder, 2); |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 208 | if (waitForCompilation) { |
| 209 | waitForCompilation(intHolder); |
| 210 | } |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 211 | return new WeakReference(loader); |
| 212 | } |
| 213 | |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 214 | private static void waitForCompilation(Class<?> intHolder) throws Exception { |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 215 | // 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 Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 223 | private static WeakReference<ClassLoader> setUpLoadLibrary(Constructor<?> constructor) |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 224 | throws Exception { |
| 225 | ClassLoader loader = (ClassLoader) constructor.newInstance( |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 226 | DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 227 | Class<?> intHolder = loader.loadClass("IntHolder"); |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 228 | Method loadLibrary = intHolder.getDeclaredMethod("loadLibrary", String.class); |
| 229 | loadLibrary.invoke(intHolder, nativeLibraryName); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 230 | waitForCompilation(intHolder); |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 231 | return new WeakReference(loader); |
| 232 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 233 | |
| 234 | private static int getPid() throws Exception { |
| 235 | return Integer.parseInt(new File("/proc/self").getCanonicalFile().getName()); |
| 236 | } |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 237 | |
| 238 | public static native void stopJit(); |
| 239 | public static native void startJit(); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 240 | } |