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]; |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 31 | 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 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()); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 50 | } catch (Exception e) { |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 51 | e.printStackTrace(); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 55 | private static void testOatFilesUnloaded(int pid) throws Exception { |
| 56 | BufferedReader reader = new BufferedReader(new FileReader ("/proc/" + pid + "/maps")); |
| 57 | String line; |
| 58 | int count = 0; |
| 59 | Runtime.getRuntime().gc(); |
| 60 | System.runFinalization(); |
| 61 | while ((line = reader.readLine()) != null) { |
| 62 | if (line.contains("@141-class-unload-ex.jar")) { |
| 63 | System.out.println(line); |
| 64 | ++count; |
| 65 | } |
| 66 | } |
| 67 | System.out.println("Number of loaded unload-ex maps " + count); |
| 68 | } |
| 69 | |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 70 | private static void stressTest(Constructor constructor) throws Exception { |
| 71 | for (int i = 0; i <= 100; ++i) { |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 72 | setUpUnloadLoader(constructor, false); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 73 | if (i % 10 == 0) { |
| 74 | Runtime.getRuntime().gc(); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 79 | private static void testUnloadClass(Constructor constructor) throws Exception { |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame^] | 80 | WeakReference<Class> klass = setUpUnloadClassWeak(constructor); |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 81 | // No strong references to class loader, should get unloaded. |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 82 | Runtime.getRuntime().gc(); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame^] | 83 | WeakReference<Class> klass2 = setUpUnloadClassWeak(constructor); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 84 | Runtime.getRuntime().gc(); |
| 85 | // If the weak reference is cleared, then it was unloaded. |
| 86 | System.out.println(klass.get()); |
| 87 | System.out.println(klass2.get()); |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private static void testUnloadLoader(Constructor constructor) |
| 91 | throws Exception { |
| 92 | WeakReference<ClassLoader> loader = setUpUnloadLoader(constructor, true); |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 93 | // No strong references to class loader, should get unloaded. |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 94 | Runtime.getRuntime().gc(); |
| 95 | // If the weak reference is cleared, then it was unloaded. |
| 96 | System.out.println(loader.get()); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 99 | private static void testStackTrace(Constructor constructor) throws Exception { |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame^] | 100 | Class klass = setUpUnloadClass(constructor); |
| 101 | WeakReference<Class> weak_klass = new WeakReference(klass); |
| 102 | Method stackTraceMethod = klass.getDeclaredMethod("generateStackTrace"); |
| 103 | Throwable throwable = (Throwable) stackTraceMethod.invoke(klass); |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 104 | stackTraceMethod = null; |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame^] | 105 | klass = null; |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 106 | Runtime.getRuntime().gc(); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame^] | 107 | boolean isNull = weak_klass.get() == null; |
Mathieu Chartier | 910e827 | 2015-09-30 09:24:22 -0700 | [diff] [blame] | 108 | System.out.println("class null " + isNull + " " + throwable.getMessage()); |
| 109 | } |
| 110 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 111 | private static void testLoadAndUnloadLibrary(Constructor constructor) throws Exception { |
| 112 | WeakReference<ClassLoader> loader = setUpLoadLibrary(constructor); |
Roland Levillain | 91d65e0 | 2016-01-19 15:59:16 +0000 | [diff] [blame] | 113 | // No strong references to class loader, should get unloaded. |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 114 | Runtime.getRuntime().gc(); |
| 115 | // If the weak reference is cleared, then it was unloaded. |
| 116 | System.out.println(loader.get()); |
| 117 | } |
| 118 | |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame^] | 119 | private static Object testNoUnloadHelper(ClassLoader loader) throws Exception { |
| 120 | Class intHolder = loader.loadClass("IntHolder"); |
| 121 | return intHolder.newInstance(); |
| 122 | } |
| 123 | |
| 124 | static class Pair { |
| 125 | public Pair(Object o, ClassLoader l) { |
| 126 | object = o; |
| 127 | classLoader = new WeakReference<ClassLoader>(l); |
| 128 | } |
| 129 | |
| 130 | public Object object; |
| 131 | public WeakReference<ClassLoader> classLoader; |
| 132 | } |
| 133 | |
| 134 | private static Pair testNoUnloadInstanceHelper(Constructor constructor) throws Exception { |
| 135 | ClassLoader loader = (ClassLoader) constructor.newInstance( |
| 136 | DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); |
| 137 | Object o = testNoUnloadHelper(loader); |
| 138 | return new Pair(o, loader); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | private static void testNoUnloadInstance(Constructor constructor) throws Exception { |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame^] | 142 | Pair p = testNoUnloadInstanceHelper(constructor); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 143 | Runtime.getRuntime().gc(); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame^] | 144 | // If the class loader was unloded too early due to races, just pass the test. |
| 145 | boolean isNull = p.classLoader.get() == null; |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 146 | System.out.println("loader null " + isNull); |
| 147 | } |
| 148 | |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame^] | 149 | private static Class setUpUnloadClass(Constructor constructor) throws Exception { |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 150 | ClassLoader loader = (ClassLoader) constructor.newInstance( |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 151 | DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 152 | Class intHolder = loader.loadClass("IntHolder"); |
| 153 | Method getValue = intHolder.getDeclaredMethod("getValue"); |
| 154 | Method setValue = intHolder.getDeclaredMethod("setValue", Integer.TYPE); |
| 155 | // Make sure we don't accidentally preserve the value in the int holder, the class |
| 156 | // initializer should be re-run. |
| 157 | System.out.println((int) getValue.invoke(intHolder)); |
| 158 | setValue.invoke(intHolder, 2); |
| 159 | System.out.println((int) getValue.invoke(intHolder)); |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 160 | waitForCompilation(intHolder); |
Mathieu Chartier | b0e861c | 2016-06-30 15:20:26 -0700 | [diff] [blame^] | 161 | return intHolder; |
| 162 | } |
| 163 | |
| 164 | private static WeakReference<Class> setUpUnloadClassWeak(Constructor constructor) |
| 165 | throws Exception { |
| 166 | return new WeakReference<Class>(setUpUnloadClass(constructor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 169 | private static WeakReference<ClassLoader> setUpUnloadLoader(Constructor constructor, |
| 170 | boolean waitForCompilation) |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 171 | throws Exception { |
| 172 | ClassLoader loader = (ClassLoader) constructor.newInstance( |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 173 | DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 174 | Class intHolder = loader.loadClass("IntHolder"); |
| 175 | Method setValue = intHolder.getDeclaredMethod("setValue", Integer.TYPE); |
| 176 | setValue.invoke(intHolder, 2); |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 177 | if (waitForCompilation) { |
| 178 | waitForCompilation(intHolder); |
| 179 | } |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 180 | return new WeakReference(loader); |
| 181 | } |
| 182 | |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 183 | private static void waitForCompilation(Class intHolder) throws Exception { |
| 184 | // Load the native library so that we can call waitForCompilation. |
| 185 | Method loadLibrary = intHolder.getDeclaredMethod("loadLibrary", String.class); |
| 186 | loadLibrary.invoke(intHolder, nativeLibraryName); |
| 187 | // Wait for JIT compilation to finish since the async threads may prevent unloading. |
| 188 | Method waitForCompilation = intHolder.getDeclaredMethod("waitForCompilation"); |
| 189 | waitForCompilation.invoke(intHolder); |
| 190 | } |
| 191 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 192 | private static WeakReference<ClassLoader> setUpLoadLibrary(Constructor constructor) |
| 193 | throws Exception { |
| 194 | ClassLoader loader = (ClassLoader) constructor.newInstance( |
Dimitry Ivanov | 3a79b63 | 2016-04-28 16:29:22 -0700 | [diff] [blame] | 195 | DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 196 | Class intHolder = loader.loadClass("IntHolder"); |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 197 | Method loadLibrary = intHolder.getDeclaredMethod("loadLibrary", String.class); |
| 198 | loadLibrary.invoke(intHolder, nativeLibraryName); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 199 | waitForCompilation(intHolder); |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 200 | return new WeakReference(loader); |
| 201 | } |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 202 | |
| 203 | private static int getPid() throws Exception { |
| 204 | return Integer.parseInt(new File("/proc/self").getCanonicalFile().getName()); |
| 205 | } |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 206 | } |