1 /* 2 * Copyright (C) 2019 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 dalvik.system.PathClassLoader; 18 import dalvik.system.VMRuntime; 19 20 import java.lang.ref.WeakReference; 21 import java.lang.reflect.Constructor; 22 import java.util.concurrent.atomic.AtomicBoolean; 23 24 public class Main { 25 static final String DEX_FILE = System.getenv("DEX_LOCATION") + "/1002-notify-startup.jar"; 26 static final String LIBRARY_SEARCH_PATH = System.getProperty("java.library.path"); 27 static AtomicBoolean completed = new AtomicBoolean(false); 28 main(String[] args)29 public static void main(String[] args) { 30 System.loadLibrary(args[0]); 31 System.out.println("Startup completed: " + hasStartupCompleted()); 32 Thread workerThread = new WorkerThread(); 33 workerThread.start(); 34 do { 35 resetStartupCompleted(); 36 VMRuntime.getRuntime().notifyStartupCompleted(); 37 Thread.yield(); 38 } while (!completed.get()); 39 try { 40 workerThread.join(); 41 } catch (Throwable e) { 42 System.err.println(e); 43 } 44 System.out.println("Startup completed: " + hasStartupCompleted()); 45 } 46 47 private static class WorkerThread extends Thread { 48 static final int NUM_ITERATIONS = 100; 49 $noinline$loadClassInLoader()50 private WeakReference<Class<?>> $noinline$loadClassInLoader() throws Exception { 51 ClassLoader loader = new PathClassLoader( 52 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader()); 53 Class ret = loader.loadClass("Main"); 54 return new WeakReference(ret); 55 } 56 run()57 public void run() { 58 for (int i = 0; i < NUM_ITERATIONS; ++i) { 59 try { 60 WeakReference<Class<?>> ref = $noinline$loadClassInLoader(); 61 Runtime.getRuntime().gc(); 62 Thread.yield(); 63 // Don't validate the unloading since app images will keep classes live (for now). 64 } catch (Throwable e) { 65 System.err.println(e); 66 break; 67 } 68 } 69 completed.set(true); 70 } 71 } 72 hasStartupCompleted()73 private static native boolean hasStartupCompleted(); resetStartupCompleted()74 private static native void resetStartupCompleted(); 75 } 76