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 #ifndef ART_RUNTIME_CLASS_LOADER_CONTEXT_H_ 18 #define ART_RUNTIME_CLASS_LOADER_CONTEXT_H_ 19 20 #include <string> 21 #include <vector> 22 23 #include "arch/instruction_set.h" 24 #include "base/dchecked_vector.h" 25 #include "handle_scope.h" 26 #include "mirror/class_loader.h" 27 #include "scoped_thread_state_change.h" 28 29 namespace art { 30 31 class DexFile; 32 class OatFile; 33 34 // Utility class which holds the class loader context used during compilation/verification. 35 class ClassLoaderContext { 36 public: 37 ~ClassLoaderContext(); 38 39 // Opens requested class path files and appends them to ClassLoaderInfo::opened_dex_files. 40 // If the dex files have been stripped, the method opens them from their oat files which are added 41 // to ClassLoaderInfo::opened_oat_files. The 'classpath_dir' argument specifies the directory to 42 // use for the relative class paths. 43 // Returns true if all dex files where successfully opened. 44 // It may be called only once per ClassLoaderContext. Subsequent calls will return the same 45 // result without doing anything. 46 // 47 // This will replace the class path locations with the locations of the opened dex files. 48 // (Note that one dex file can contain multidexes. Each multidex will be added to the classpath 49 // separately.) 50 // 51 // Note that a "false" return could mean that either an apk/jar contained no dex files or 52 // that we hit a I/O or checksum mismatch error. 53 // TODO(calin): Currently there's no easy way to tell the difference. 54 // 55 // TODO(calin): we're forced to complicate the flow in this class with a different 56 // OpenDexFiles step because the current dex2oat flow requires the dex files be opened before 57 // the class loader is created. Consider reworking the dex2oat part. 58 bool OpenDexFiles(InstructionSet isa, const std::string& classpath_dir); 59 60 // Remove the specified compilation sources from all classpaths present in this context. 61 // Should only be called before the first call to OpenDexFiles(). 62 bool RemoveLocationsFromClassPaths(const dchecked_vector<std::string>& compilation_sources); 63 64 // Creates the entire class loader hierarchy according to the current context. 65 // Returns the first class loader from the chain. 66 // 67 // For example: if the context was built from the spec 68 // "ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]..." 69 // the method returns the class loader correponding to ClassLoader1. The parent chain will be 70 // ClassLoader1 --> ClassLoader2 --> ... --> BootClassLoader. 71 // 72 // The compilation sources are appended to the classpath of the first class loader (in the above 73 // example ClassLoader1). 74 // 75 // If the context is empty, this method only creates a single PathClassLoader with the 76 // given compilation_sources. 77 // 78 // Notes: 79 // 1) the objects are not completely set up. Do not use this outside of tests and the compiler. 80 // 2) should only be called before the first call to OpenDexFiles(). 81 jobject CreateClassLoader(const std::vector<const DexFile*>& compilation_sources) const; 82 83 // Encodes the context as a string suitable to be added in oat files. 84 // (so that it can be read and verified at runtime against the actual class 85 // loader hierarchy). 86 // Should only be called if OpenDexFiles() returned true. 87 // E.g. if the context is PCL[a.dex:b.dex] this will return 88 // "PCL[a.dex*a_checksum*b.dex*a_checksum]". 89 std::string EncodeContextForOatFile(const std::string& base_dir) const; 90 91 // Encodes the context as a string suitable to be passed to dex2oat. 92 // This is the same as EncodeContextForOatFile but without adding the checksums 93 // and only adding each dex files once (no multidex). 94 // Should only be called if OpenDexFiles() returned true. 95 std::string EncodeContextForDex2oat(const std::string& base_dir) const; 96 97 // Flattens the opened dex files into the given vector. 98 // Should only be called if OpenDexFiles() returned true. 99 std::vector<const DexFile*> FlattenOpenedDexFiles() const; 100 101 // Verifies that the current context is identical to the context encoded as `context_spec`. 102 // Identical means: 103 // - the number and type of the class loaders from the chain matches 104 // - the class loader from the same position have the same classpath 105 // (the order and checksum of the dex files matches) 106 // This should be called after OpenDexFiles(). 107 bool VerifyClassLoaderContextMatch(const std::string& context_spec) const; 108 109 // Creates the class loader context from the given string. 110 // The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]... 111 // ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader). 112 // ClasspathElem is the path of dex/jar/apk file. 113 // 114 // The spec represents a class loader chain with the natural interpretation: 115 // ClassLoader1 has ClassLoader2 as parent which has ClassLoader3 as a parent and so on. 116 // The last class loader is assumed to have the BootClassLoader as a parent. 117 // 118 // Note that we allowed class loaders with an empty class path in order to support a custom 119 // class loader for the source dex files. 120 static std::unique_ptr<ClassLoaderContext> Create(const std::string& spec); 121 122 // Creates a context for the given class_loader and dex_elements. 123 // The method will walk the parent chain starting from `class_loader` and add their dex files 124 // to the current class loaders chain. The `dex_elements` will be added at the end of the 125 // classpath belonging to the `class_loader` argument. 126 // The ownership of the opened dex files will be retained by the given `class_loader`. 127 // If there are errors in processing the class loader chain (e.g. unsupported elements) the 128 // method returns null. 129 static std::unique_ptr<ClassLoaderContext> CreateContextForClassLoader(jobject class_loader, 130 jobjectArray dex_elements); 131 132 // Returns the default class loader context to be used when none is specified. 133 // This will return a context with a single and empty PathClassLoader. 134 static std::unique_ptr<ClassLoaderContext> Default(); 135 136 private: 137 enum ClassLoaderType { 138 kInvalidClassLoader = 0, 139 kPathClassLoader = 1, 140 kDelegateLastClassLoader = 2 141 }; 142 143 struct ClassLoaderInfo { 144 // The type of this class loader. 145 ClassLoaderType type; 146 // The list of class path elements that this loader loads. 147 // Note that this list may contain relative paths. 148 std::vector<std::string> classpath; 149 // The list of class path elements checksums. 150 // May be empty if the checksums are not given when the context is created. 151 std::vector<uint32_t> checksums; 152 // After OpenDexFiles is called this holds the opened dex files. 153 std::vector<std::unique_ptr<const DexFile>> opened_dex_files; 154 // After OpenDexFiles, in case some of the dex files were opened from their oat files 155 // this holds the list of opened oat files. 156 std::vector<std::unique_ptr<OatFile>> opened_oat_files; 157 ClassLoaderInfoClassLoaderInfo158 explicit ClassLoaderInfo(ClassLoaderType cl_type) : type(cl_type) {} 159 }; 160 161 // Creates an empty context (with no class loaders). 162 ClassLoaderContext(); 163 164 // Constructs an empty context. 165 // `owns_the_dex_files` specifies whether or not the context will own the opened dex files 166 // present in the class loader chain. If `owns_the_dex_files` is true then OpenDexFiles cannot 167 // be called on this context (dex_files_open_attempted_ and dex_files_open_result_ will be set 168 // to true as well) 169 explicit ClassLoaderContext(bool owns_the_dex_files); 170 171 // Reads the class loader spec in place and returns true if the spec is valid and the 172 // compilation context was constructed. 173 bool Parse(const std::string& spec, bool parse_checksums = false); 174 175 // Attempts to parse a single class loader spec for the given class_loader_type. 176 // If successful the class loader spec will be added to the chain. 177 // Returns whether or not the operation was successful. 178 bool ParseClassLoaderSpec(const std::string& class_loader_spec, 179 ClassLoaderType class_loader_type, 180 bool parse_checksums = false); 181 182 // CHECKs that the dex files were opened (OpenDexFiles was called and set dex_files_open_result_ 183 // to true). Aborts if not. The `calling_method` is used in the log message to identify the source 184 // of the call. 185 void CheckDexFilesOpened(const std::string& calling_method) const; 186 187 // Adds the `class_loader` info to the context. 188 // The dex file present in `dex_elements` array (if not null) will be added at the end of 189 // the classpath. 190 bool AddInfoToContextFromClassLoader(ScopedObjectAccessAlreadyRunnable& soa, 191 Handle<mirror::ClassLoader> class_loader, 192 Handle<mirror::ObjectArray<mirror::Object>> dex_elements) 193 REQUIRES_SHARED(Locks::mutator_lock_); 194 195 // Encodes the context as a string suitable to be passed to dex2oat or to be added to the 196 // oat file as the class path key. 197 // If for_dex2oat is true, the encoding adds each file once (i.e. it does not add multidex 198 // location). Otherwise, for oat files, the encoding adds all the dex files (including multidex) 199 // together with their checksums. 200 // Should only be called if OpenDexFiles() returned true. 201 std::string EncodeContext(const std::string& base_dir, bool for_dex2oat) const; 202 203 // Extracts the class loader type from the given spec. 204 // Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not 205 // recognized. 206 static ClassLoaderType ExtractClassLoaderType(const std::string& class_loader_spec); 207 208 // Returns the string representation of the class loader type. 209 // The returned format can be used when parsing a context spec. 210 static const char* GetClassLoaderTypeName(ClassLoaderType type); 211 212 // Returns the WellKnownClass for the given class loader type. 213 static jclass GetClassLoaderClass(ClassLoaderType type); 214 215 // The class loader chain represented as a vector. 216 // The parent of class_loader_chain_[i] is class_loader_chain_[i++]. 217 // The parent of the last element is assumed to be the boot class loader. 218 std::vector<ClassLoaderInfo> class_loader_chain_; 219 220 // Whether or not the class loader context should be ignored at runtime when loading the oat 221 // files. When true, dex2oat will use OatFile::kSpecialSharedLibrary as the classpath key in 222 // the oat file. 223 // TODO(calin): Can we get rid of this and cover all relevant use cases? 224 // (e.g. packages using prebuild system packages as shared libraries b/36480683) 225 bool special_shared_library_; 226 227 // Whether or not OpenDexFiles() was called. 228 bool dex_files_open_attempted_; 229 // The result of the last OpenDexFiles() operation. 230 bool dex_files_open_result_; 231 232 // Whether or not the context owns the opened dex and oat files. 233 // If true, the opened dex files will be de-allocated when the context is destructed. 234 // If false, the objects will continue to be alive. 235 // Note that for convenience the the opened dex/oat files are stored as unique pointers 236 // which will release their ownership in the destructor based on this flag. 237 const bool owns_the_dex_files_; 238 239 friend class ClassLoaderContextTest; 240 241 DISALLOW_COPY_AND_ASSIGN(ClassLoaderContext); 242 }; 243 244 } // namespace art 245 #endif // ART_RUNTIME_CLASS_LOADER_CONTEXT_H_ 246