• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.invoke.MethodType;
18 import java.lang.ref.Reference;
19 import java.lang.ref.WeakReference;
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.Method;
22 
23 public class Main {
24     private static final String DEX_FILE =
25         System.getenv("DEX_LOCATION") + "/2276-const-method-type-gc-cleanup-ex.jar";
26     private static final String LIBRARY_SEARCH_PATH = System.getProperty("java.library.path");
27 
main(String[] args)28     public static void main(String[] args) throws Throwable {
29         System.loadLibrary(args[0]);
30 
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 
38         // Identical to Worker.returnStringMethodType() and is captured by JIT-ed code.
39         MethodType returnStringMethodType = MethodType.methodType(String.class);
40 
41         for (int i = 0; i < 10; ++i) {
42             callDoWork(constructor);
43         }
44 
45         Reference.reachabilityFence(returnStringMethodType);
46     }
47 
callDoWork(Constructor constructor)48     private static void callDoWork(Constructor constructor) throws Throwable {
49         WeakReference loaderRef = $noinline$doRealWork(constructor);
50 
51         doUnload();
52 
53         if (loaderRef.refersTo(null)) {
54             System.out.println("ClassLoader was unloaded");
55         }
56     }
57 
$noinline$doRealWork(Constructor constructor)58     private static WeakReference $noinline$doRealWork(Constructor constructor) throws Throwable {
59         ClassLoader loader = (ClassLoader) constructor.newInstance(
60                 DEX_FILE, LIBRARY_SEARCH_PATH, ClassLoader.getSystemClassLoader());
61 
62         Class workerClass = loader.loadClass("Worker");
63 
64         if (workerClass.getClassLoader() != loader) {
65             throw new AssertionError("The class was loaded by a wrong ClassLoader");
66         }
67 
68         ensureJitCompiled(workerClass, "doWork");
69 
70         Method m = workerClass.getDeclaredMethod("doWork");
71         m.invoke(null);
72 
73         WeakReference loaderRef = new WeakReference(loader);
74 
75         m = null;
76         workerClass = null;
77         loader = null;
78 
79         return loaderRef;
80     }
81 
doUnload()82     private static void doUnload() {
83         for (int i = 0; i < 3; ++i) {
84             Runtime.getRuntime().gc();
85             System.runFinalization();
86         }
87     }
88 
ensureJitCompiled(Class<?> cls, String methodName)89     public static native void ensureJitCompiled(Class<?> cls, String methodName);
90 }
91