Nicolas Geoffray | 48b40cc | 2017-08-07 16:52:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | import java.lang.reflect.Constructor; |
| 18 | |
| 19 | public class Main { |
| 20 | static final String DEX_FILE = |
| 21 | System.getenv("DEX_LOCATION") + "/661-classloader-allocator-ex.jar"; |
| 22 | static final String LIBRARY_SEARCH_PATH = System.getProperty("java.library.path"); |
| 23 | |
| 24 | private static void doUnloading() { |
| 25 | // Stop the JIT to ensure its threads and work queue are not keeping classes |
| 26 | // artifically alive. |
| 27 | stopJit(); |
| 28 | // Do multiple GCs to prevent rare flakiness if some other thread is keeping the |
| 29 | // classloader live. |
| 30 | for (int i = 0; i < 5; ++i) { |
| 31 | Runtime.getRuntime().gc(); |
| 32 | } |
| 33 | startJit(); |
| 34 | } |
| 35 | |
| 36 | public static void main(String[] args) throws Exception { |
| 37 | System.loadLibrary(args[0]); |
| 38 | loadClass(); |
| 39 | doUnloading(); |
| 40 | // fetchProfiles iterate over the ProfilingInfo, we used to crash in the presence |
| 41 | // of unloaded copied methods. |
| 42 | fetchProfiles(); |
| 43 | } |
| 44 | |
| 45 | public static void loadClass() throws Exception { |
| 46 | Class<?> pathClassLoader = Class.forName("dalvik.system.PathClassLoader"); |
| 47 | if (pathClassLoader == null) { |
| 48 | throw new AssertionError("Couldn't find path class loader class"); |
| 49 | } |
| 50 | Constructor<?> constructor = |
| 51 | pathClassLoader.getDeclaredConstructor(String.class, String.class, ClassLoader.class); |
| 52 | ClassLoader loader = (ClassLoader) constructor.newInstance( |
| 53 | DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); |
| 54 | Class<?> otherClass = loader.loadClass("p1.OtherClass"); |
| 55 | ensureJitCompiled(otherClass, "foo"); |
| 56 | } |
| 57 | |
| 58 | public static native void ensureJitCompiled(Class<?> cls, String methodName); |
| 59 | public static native void fetchProfiles(); |
| 60 | public static native void stopJit(); |
| 61 | public static native void startJit(); |
| 62 | } |