1 /* 2 * Copyright (C) 2011 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 package dalvik.system; 18 19 import java.io.File; 20 import java.net.URL; 21 import java.nio.ByteBuffer; 22 import java.util.ArrayList; 23 import java.util.Enumeration; 24 import java.util.List; 25 26 /** 27 * Base class for common functionality between various dex-based 28 * {@link ClassLoader} implementations. 29 */ 30 public class BaseDexClassLoader extends ClassLoader { 31 32 /** 33 * Hook for customizing how dex files loads are reported. 34 * 35 * This enables the framework to monitor the use of dex files. The 36 * goal is to simplify the mechanism for optimizing foreign dex files and 37 * enable further optimizations of secondary dex files. 38 * 39 * The reporting happens only when new instances of BaseDexClassLoader 40 * are constructed and will be active only after this field is set with 41 * {@link BaseDexClassLoader#setReporter}. 42 */ 43 /* @NonNull */ private static volatile Reporter reporter = null; 44 45 private final DexPathList pathList; 46 47 /** 48 * Constructs an instance. 49 * Note that all the *.jar and *.apk files from {@code dexPath} might be 50 * first extracted in-memory before the code is loaded. This can be avoided 51 * by passing raw dex files (*.dex) in the {@code dexPath}. 52 * 53 * @param dexPath the list of jar/apk files containing classes and 54 * resources, delimited by {@code File.pathSeparator}, which 55 * defaults to {@code ":"} on Android. 56 * @param optimizedDirectory this parameter is deprecated and has no effect 57 * @param librarySearchPath the list of directories containing native 58 * libraries, delimited by {@code File.pathSeparator}; may be 59 * {@code null} 60 * @param parent the parent class loader 61 */ 62 public BaseDexClassLoader(String dexPath, File optimizedDirectory, 63 String librarySearchPath, ClassLoader parent) { 64 super(parent); 65 this.pathList = new DexPathList(this, dexPath, librarySearchPath, null); 66 67 if (reporter != null) { 68 reportClassLoaderChain(); 69 } 70 } 71 72 /** 73 * Reports the current class loader chain to the registered {@code reporter}. 74 * The chain is reported only if all its elements are {@code BaseDexClassLoader}. 75 */ 76 private void reportClassLoaderChain() { 77 ArrayList<BaseDexClassLoader> classLoadersChain = new ArrayList<>(); 78 ArrayList<String> classPaths = new ArrayList<>(); 79 80 classLoadersChain.add(this); 81 classPaths.add(String.join(File.pathSeparator, pathList.getDexPaths())); 82 83 boolean onlySawSupportedClassLoaders = true; 84 ClassLoader bootClassLoader = ClassLoader.getSystemClassLoader().getParent(); 85 ClassLoader current = getParent(); 86 87 while (current != null && current != bootClassLoader) { 88 if (current instanceof BaseDexClassLoader) { 89 BaseDexClassLoader bdcCurrent = (BaseDexClassLoader) current; 90 classLoadersChain.add(bdcCurrent); 91 classPaths.add(String.join(File.pathSeparator, bdcCurrent.pathList.getDexPaths())); 92 } else { 93 onlySawSupportedClassLoaders = false; 94 break; 95 } 96 current = current.getParent(); 97 } 98 99 if (onlySawSupportedClassLoaders) { 100 reporter.report(classLoadersChain, classPaths); 101 } 102 } 103 104 /** 105 * Constructs an instance. 106 * 107 * dexFile must be an in-memory representation of a full dexFile. 108 * 109 * @param dexFiles the array of in-memory dex files containing classes. 110 * @param parent the parent class loader 111 * 112 * @hide 113 */ 114 public BaseDexClassLoader(ByteBuffer[] dexFiles, ClassLoader parent) { 115 // TODO We should support giving this a library search path maybe. 116 super(parent); 117 this.pathList = new DexPathList(this, dexFiles); 118 } 119 120 @Override 121 protected Class<?> findClass(String name) throws ClassNotFoundException { 122 List<Throwable> suppressedExceptions = new ArrayList<Throwable>(); 123 Class c = pathList.findClass(name, suppressedExceptions); 124 if (c == null) { 125 ClassNotFoundException cnfe = new ClassNotFoundException( 126 "Didn't find class \"" + name + "\" on path: " + pathList); 127 for (Throwable t : suppressedExceptions) { 128 cnfe.addSuppressed(t); 129 } 130 throw cnfe; 131 } 132 return c; 133 } 134 135 /** 136 * @hide 137 */ 138 public void addDexPath(String dexPath) { 139 pathList.addDexPath(dexPath, null /*optimizedDirectory*/); 140 } 141 142 @Override 143 protected URL findResource(String name) { 144 return pathList.findResource(name); 145 } 146 147 @Override 148 protected Enumeration<URL> findResources(String name) { 149 return pathList.findResources(name); 150 } 151 152 @Override 153 public String findLibrary(String name) { 154 return pathList.findLibrary(name); 155 } 156 157 /** 158 * Returns package information for the given package. 159 * Unfortunately, instances of this class don't really have this 160 * information, and as a non-secure {@code ClassLoader}, it isn't 161 * even required to, according to the spec. Yet, we want to 162 * provide it, in order to make all those hopeful callers of 163 * {@code myClass.getPackage().getName()} happy. Thus we construct 164 * a {@code Package} object the first time it is being requested 165 * and fill most of the fields with dummy values. The {@code 166 * Package} object is then put into the {@code ClassLoader}'s 167 * package cache, so we see the same one next time. We don't 168 * create {@code Package} objects for {@code null} arguments or 169 * for the default package. 170 * 171 * <p>There is a limited chance that we end up with multiple 172 * {@code Package} objects representing the same package: It can 173 * happen when when a package is scattered across different JAR 174 * files which were loaded by different {@code ClassLoader} 175 * instances. This is rather unlikely, and given that this whole 176 * thing is more or less a workaround, probably not worth the 177 * effort to address. 178 * 179 * @param name the name of the class 180 * @return the package information for the class, or {@code null} 181 * if there is no package information available for it 182 */ 183 @Override 184 protected synchronized Package getPackage(String name) { 185 if (name != null && !name.isEmpty()) { 186 Package pack = super.getPackage(name); 187 188 if (pack == null) { 189 pack = definePackage(name, "Unknown", "0.0", "Unknown", 190 "Unknown", "0.0", "Unknown", null); 191 } 192 193 return pack; 194 } 195 196 return null; 197 } 198 199 /** 200 * @hide 201 */ 202 public String getLdLibraryPath() { 203 StringBuilder result = new StringBuilder(); 204 for (File directory : pathList.getNativeLibraryDirectories()) { 205 if (result.length() > 0) { 206 result.append(':'); 207 } 208 result.append(directory); 209 } 210 211 return result.toString(); 212 } 213 214 @Override public String toString() { 215 return getClass().getName() + "[" + pathList + "]"; 216 } 217 218 /** 219 * Sets the reporter for dex load notifications. 220 * Once set, all new instances of BaseDexClassLoader will report upon 221 * constructions the loaded dex files. 222 * 223 * @param newReporter the new Reporter. Setting null will cancel reporting. 224 * @hide 225 */ 226 public static void setReporter(Reporter newReporter) { 227 reporter = newReporter; 228 } 229 230 /** 231 * @hide 232 */ 233 public static Reporter getReporter() { 234 return reporter; 235 } 236 237 /** 238 * @hide 239 */ 240 public interface Reporter { 241 /** 242 * Reports the construction of a BaseDexClassLoader and provides information about the 243 * class loader chain. 244 * Note that this only reports if all class loader in the chain are BaseDexClassLoader. 245 * 246 * @param classLoadersChain the chain of class loaders used during the construction of the 247 * class loader. The first element is the BaseDexClassLoader being constructed, 248 * the second element is its parent, and so on. 249 * @param classPaths the class paths of the class loaders present in 250 * {@param classLoadersChain}. The first element corresponds to the first class 251 * loader and so on. A classpath is represented as a list of dex files separated by 252 * {@code File.pathSeparator}. 253 */ 254 void report(List<BaseDexClassLoader> classLoadersChain, List<String> classPaths); 255 } 256 } 257