Main.java revision b0e861cb829f190f91579637f93a096fc1a680b7
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
17import java.io.BufferedReader;
18import java.io.File;
19import java.io.FileReader;
20import 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";
26    static final String LIBRARY_SEARCH_PATH = System.getProperty("java.library.path");
27    static String nativeLibraryName;
28
29    public static void main(String[] args) throws Exception {
30        nativeLibraryName = args[0];
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 =
36            pathClassLoader.getDeclaredConstructor(String.class, String.class, ClassLoader.class);
37        try {
38            testUnloadClass(constructor);
39            testUnloadLoader(constructor);
40            // Test that we don't unload if we have an instance.
41            testNoUnloadInstance(constructor);
42            // Test JNI_OnLoad and JNI_OnUnload.
43            testLoadAndUnloadLibrary(constructor);
44            // Test that stack traces keep the classes live.
45            testStackTrace(constructor);
46            // Stress test to make sure we dont leak memory.
47            stressTest(constructor);
48            // Test that the oat files are unloaded.
49            testOatFilesUnloaded(getPid());
50        } catch (Exception e) {
51            e.printStackTrace();
52        }
53    }
54
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
70    private static void stressTest(Constructor constructor) throws Exception {
71        for (int i = 0; i <= 100; ++i) {
72            setUpUnloadLoader(constructor, false);
73            if (i % 10 == 0) {
74                Runtime.getRuntime().gc();
75            }
76        }
77    }
78
79    private static void testUnloadClass(Constructor constructor) throws Exception {
80        WeakReference<Class> klass = setUpUnloadClassWeak(constructor);
81        // No strong references to class loader, should get unloaded.
82        Runtime.getRuntime().gc();
83        WeakReference<Class> klass2 = setUpUnloadClassWeak(constructor);
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());
88    }
89
90    private static void testUnloadLoader(Constructor constructor)
91        throws Exception {
92      WeakReference<ClassLoader> loader = setUpUnloadLoader(constructor, true);
93      // No strong references to class loader, should get unloaded.
94      Runtime.getRuntime().gc();
95      // If the weak reference is cleared, then it was unloaded.
96      System.out.println(loader.get());
97    }
98
99    private static void testStackTrace(Constructor constructor) throws Exception {
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);
104        stackTraceMethod = null;
105        klass = null;
106        Runtime.getRuntime().gc();
107        boolean isNull = weak_klass.get() == null;
108        System.out.println("class null " + isNull + " " + throwable.getMessage());
109    }
110
111    private static void testLoadAndUnloadLibrary(Constructor constructor) throws Exception {
112        WeakReference<ClassLoader> loader = setUpLoadLibrary(constructor);
113        // No strong references to class loader, should get unloaded.
114        Runtime.getRuntime().gc();
115        // If the weak reference is cleared, then it was unloaded.
116        System.out.println(loader.get());
117    }
118
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);
139    }
140
141    private static void testNoUnloadInstance(Constructor constructor) throws Exception {
142        Pair p = testNoUnloadInstanceHelper(constructor);
143        Runtime.getRuntime().gc();
144        // If the class loader was unloded too early due to races, just pass the test.
145        boolean isNull = p.classLoader.get() == null;
146        System.out.println("loader null " + isNull);
147    }
148
149    private static Class setUpUnloadClass(Constructor constructor) throws Exception {
150        ClassLoader loader = (ClassLoader) constructor.newInstance(
151            DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
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));
160        waitForCompilation(intHolder);
161        return intHolder;
162    }
163
164    private static WeakReference<Class> setUpUnloadClassWeak(Constructor constructor)
165            throws Exception {
166        return new WeakReference<Class>(setUpUnloadClass(constructor));
167    }
168
169    private static WeakReference<ClassLoader> setUpUnloadLoader(Constructor constructor,
170                                                                boolean waitForCompilation)
171        throws Exception {
172        ClassLoader loader = (ClassLoader) constructor.newInstance(
173            DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
174        Class intHolder = loader.loadClass("IntHolder");
175        Method setValue = intHolder.getDeclaredMethod("setValue", Integer.TYPE);
176        setValue.invoke(intHolder, 2);
177        if (waitForCompilation) {
178            waitForCompilation(intHolder);
179        }
180        return new WeakReference(loader);
181    }
182
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
192    private static WeakReference<ClassLoader> setUpLoadLibrary(Constructor constructor)
193        throws Exception {
194        ClassLoader loader = (ClassLoader) constructor.newInstance(
195            DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
196        Class intHolder = loader.loadClass("IntHolder");
197        Method loadLibrary = intHolder.getDeclaredMethod("loadLibrary", String.class);
198        loadLibrary.invoke(intHolder, nativeLibraryName);
199        waitForCompilation(intHolder);
200        return new WeakReference(loader);
201    }
202
203    private static int getPid() throws Exception {
204      return Integer.parseInt(new File("/proc/self").getCanonicalFile().getName());
205    }
206}
207