Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [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 | #include "class_loader_context.h" |
| 18 | |
| 19 | #include "base/dchecked_vector.h" |
| 20 | #include "base/stl_util.h" |
| 21 | #include "class_linker.h" |
| 22 | #include "dex_file.h" |
| 23 | #include "oat_file_assistant.h" |
| 24 | #include "runtime.h" |
| 25 | #include "scoped_thread_state_change-inl.h" |
| 26 | #include "thread.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | static constexpr char kPathClassLoaderString[] = "PCL"; |
| 31 | static constexpr char kDelegateLastClassLoaderString[] = "DLC"; |
| 32 | static constexpr char kClassLoaderOpeningMark = '['; |
| 33 | static constexpr char kClassLoaderClosingMark = ']'; |
| 34 | static constexpr char kClassLoaderSep = ';'; |
| 35 | static constexpr char kClasspathSep = ':'; |
| 36 | |
| 37 | ClassLoaderContext::ClassLoaderContext() |
| 38 | : special_shared_library_(false), |
| 39 | dex_files_open_attempted_(false), |
| 40 | dex_files_open_result_(false) {} |
| 41 | |
| 42 | std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Create(const std::string& spec) { |
| 43 | std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext()); |
| 44 | if (result->Parse(spec)) { |
| 45 | return result; |
| 46 | } else { |
| 47 | return nullptr; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // The expected format is: "ClassLoaderType1[ClasspathElem1:ClasspathElem2...]". |
| 52 | bool ClassLoaderContext::ParseClassLoaderSpec(const std::string& class_loader_spec, |
| 53 | ClassLoaderType class_loader_type) { |
| 54 | const char* class_loader_type_str = GetClassLoaderTypeName(class_loader_type); |
| 55 | size_t type_str_size = strlen(class_loader_type_str); |
| 56 | |
| 57 | CHECK_EQ(0, class_loader_spec.compare(0, type_str_size, class_loader_type_str)); |
| 58 | |
| 59 | // Check the opening and closing markers. |
| 60 | if (class_loader_spec[type_str_size] != kClassLoaderOpeningMark) { |
| 61 | return false; |
| 62 | } |
| 63 | if (class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderClosingMark) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | // At this point we know the format is ok; continue and extract the classpath. |
| 68 | // Note that class loaders with an empty class path are allowed. |
| 69 | std::string classpath = class_loader_spec.substr(type_str_size + 1, |
| 70 | class_loader_spec.length() - type_str_size - 2); |
| 71 | |
| 72 | class_loader_chain_.push_back(ClassLoaderInfo(class_loader_type)); |
| 73 | Split(classpath, kClasspathSep, &class_loader_chain_.back().classpath); |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | // Extracts the class loader type from the given spec. |
| 79 | // Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not |
| 80 | // recognized. |
| 81 | ClassLoaderContext::ClassLoaderType |
| 82 | ClassLoaderContext::ExtractClassLoaderType(const std::string& class_loader_spec) { |
| 83 | const ClassLoaderType kValidTypes[] = {kPathClassLoader, kDelegateLastClassLoader}; |
| 84 | for (const ClassLoaderType& type : kValidTypes) { |
| 85 | const char* type_str = GetClassLoaderTypeName(type); |
| 86 | if (class_loader_spec.compare(0, strlen(type_str), type_str) == 0) { |
| 87 | return type; |
| 88 | } |
| 89 | } |
| 90 | return kInvalidClassLoader; |
| 91 | } |
| 92 | |
| 93 | // The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]... |
| 94 | // ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader). |
| 95 | // ClasspathElem is the path of dex/jar/apk file. |
| 96 | bool ClassLoaderContext::Parse(const std::string& spec) { |
| 97 | if (spec.empty()) { |
| 98 | LOG(ERROR) << "Empty string passed to Parse"; |
| 99 | return false; |
| 100 | } |
| 101 | // Stop early if we detect the special shared library, which may be passed as the classpath |
| 102 | // for dex2oat when we want to skip the shared libraries check. |
| 103 | if (spec == OatFile::kSpecialSharedLibrary) { |
| 104 | LOG(INFO) << "The ClassLoaderContext is a special shared library."; |
| 105 | special_shared_library_ = true; |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | std::vector<std::string> class_loaders; |
| 110 | Split(spec, kClassLoaderSep, &class_loaders); |
| 111 | |
| 112 | for (const std::string& class_loader : class_loaders) { |
| 113 | ClassLoaderType type = ExtractClassLoaderType(class_loader); |
| 114 | if (type == kInvalidClassLoader) { |
| 115 | LOG(ERROR) << "Invalid class loader type: " << class_loader; |
| 116 | return false; |
| 117 | } |
| 118 | if (!ParseClassLoaderSpec(class_loader, type)) { |
| 119 | LOG(ERROR) << "Invalid class loader spec: " << class_loader; |
| 120 | return false; |
| 121 | } |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | // Opens requested class path files and appends them to opened_dex_files. If the dex files have |
| 127 | // been stripped, this opens them from their oat files (which get added to opened_oat_files). |
| 128 | bool ClassLoaderContext::OpenDexFiles(InstructionSet isa, const std::string& classpath_dir) { |
| 129 | CHECK(!dex_files_open_attempted_) << "OpenDexFiles should not be called twice"; |
| 130 | |
| 131 | dex_files_open_attempted_ = true; |
| 132 | // Assume we can open all dex files. If not, we will set this to false as we go. |
| 133 | dex_files_open_result_ = true; |
| 134 | |
| 135 | if (special_shared_library_) { |
| 136 | // Nothing to open if the context is a special shared library. |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | // Note that we try to open all dex files even if some fail. |
| 141 | // We may get resource-only apks which we cannot load. |
| 142 | // TODO(calin): Refine the dex opening interface to be able to tell if an archive contains |
| 143 | // no dex files. So that we can distinguish the real failures... |
| 144 | for (ClassLoaderInfo& info : class_loader_chain_) { |
| 145 | for (const std::string& cp_elem : info.classpath) { |
| 146 | // If path is relative, append it to the provided base directory. |
| 147 | std::string location = cp_elem; |
| 148 | if (location[0] != '/') { |
| 149 | location = classpath_dir + '/' + location; |
| 150 | } |
| 151 | std::string error_msg; |
| 152 | // When opening the dex files from the context we expect their checksum to match their |
| 153 | // contents. So pass true to verify_checksum. |
| 154 | if (!DexFile::Open(location.c_str(), |
| 155 | location.c_str(), |
| 156 | /*verify_checksum*/ true, |
| 157 | &error_msg, |
| 158 | &info.opened_dex_files)) { |
| 159 | // If we fail to open the dex file because it's been stripped, try to open the dex file |
| 160 | // from its corresponding oat file. |
| 161 | // This could happen when we need to recompile a pre-build whose dex code has been stripped. |
| 162 | // (for example, if the pre-build is only quicken and we want to re-compile it |
| 163 | // speed-profile). |
| 164 | // TODO(calin): Use the vdex directly instead of going through the oat file. |
| 165 | OatFileAssistant oat_file_assistant(location.c_str(), isa, false); |
| 166 | std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile()); |
| 167 | std::vector<std::unique_ptr<const DexFile>> oat_dex_files; |
| 168 | if (oat_file != nullptr && |
| 169 | OatFileAssistant::LoadDexFiles(*oat_file, location, &oat_dex_files)) { |
| 170 | info.opened_oat_files.push_back(std::move(oat_file)); |
| 171 | info.opened_dex_files.insert(info.opened_dex_files.end(), |
| 172 | std::make_move_iterator(oat_dex_files.begin()), |
| 173 | std::make_move_iterator(oat_dex_files.end())); |
| 174 | } else { |
| 175 | LOG(WARNING) << "Could not open dex files from location: " << location; |
| 176 | dex_files_open_result_ = false; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return dex_files_open_result_; |
| 183 | } |
| 184 | |
| 185 | bool ClassLoaderContext::RemoveLocationsFromClassPaths( |
| 186 | const dchecked_vector<std::string>& locations) { |
| 187 | CHECK(!dex_files_open_attempted_) |
| 188 | << "RemoveLocationsFromClasspaths cannot be call after OpenDexFiles"; |
| 189 | |
| 190 | std::set<std::string> canonical_locations; |
| 191 | for (const std::string& location : locations) { |
| 192 | canonical_locations.insert(DexFile::GetDexCanonicalLocation(location.c_str())); |
| 193 | } |
| 194 | bool removed_locations = false; |
| 195 | for (ClassLoaderInfo& info : class_loader_chain_) { |
| 196 | size_t initial_size = info.classpath.size(); |
| 197 | auto kept_it = std::remove_if( |
| 198 | info.classpath.begin(), |
| 199 | info.classpath.end(), |
| 200 | [canonical_locations](const std::string& location) { |
| 201 | return ContainsElement(canonical_locations, |
| 202 | DexFile::GetDexCanonicalLocation(location.c_str())); |
| 203 | }); |
| 204 | info.classpath.erase(kept_it, info.classpath.end()); |
| 205 | if (initial_size != info.classpath.size()) { |
| 206 | removed_locations = true; |
| 207 | } |
| 208 | } |
| 209 | return removed_locations; |
| 210 | } |
| 211 | |
| 212 | std::string ClassLoaderContext::EncodeContextForOatFile(const std::string& base_dir) const { |
| 213 | CheckDexFilesOpened("EncodeContextForOatFile"); |
| 214 | if (special_shared_library_) { |
| 215 | return OatFile::kSpecialSharedLibrary; |
| 216 | } |
| 217 | |
| 218 | if (class_loader_chain_.empty()) { |
| 219 | return ""; |
| 220 | } |
| 221 | |
| 222 | // TODO(calin): Transition period: assume we only have a classloader until |
| 223 | // the oat file assistant implements the full class loader check. |
| 224 | CHECK_EQ(1u, class_loader_chain_.size()); |
| 225 | |
| 226 | return OatFile::EncodeDexFileDependencies(MakeNonOwningPointerVector( |
| 227 | class_loader_chain_[0].opened_dex_files), base_dir); |
| 228 | } |
| 229 | |
| 230 | jobject ClassLoaderContext::CreateClassLoader( |
| 231 | const std::vector<const DexFile*>& compilation_sources) const { |
| 232 | CheckDexFilesOpened("CreateClassLoader"); |
| 233 | |
| 234 | Thread* self = Thread::Current(); |
| 235 | ScopedObjectAccess soa(self); |
| 236 | |
| 237 | std::vector<const DexFile*> class_path_files; |
| 238 | |
| 239 | // TODO(calin): Transition period: assume we only have a classloader until |
| 240 | // the oat file assistant implements the full class loader check. |
| 241 | if (!class_loader_chain_.empty()) { |
| 242 | CHECK_EQ(1u, class_loader_chain_.size()); |
| 243 | CHECK_EQ(kPathClassLoader, class_loader_chain_[0].type); |
| 244 | class_path_files = MakeNonOwningPointerVector(class_loader_chain_[0].opened_dex_files); |
| 245 | } |
| 246 | |
| 247 | // Classpath: first the class-path given; then the dex files we'll compile. |
| 248 | // Thus we'll resolve the class-path first. |
| 249 | class_path_files.insert(class_path_files.end(), |
| 250 | compilation_sources.begin(), |
| 251 | compilation_sources.end()); |
| 252 | |
| 253 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 254 | return class_linker->CreatePathClassLoader(self, class_path_files); |
| 255 | } |
| 256 | |
| 257 | std::vector<const DexFile*> ClassLoaderContext::FlattenOpenedDexFiles() const { |
| 258 | CheckDexFilesOpened("FlattenOpenedDexFiles"); |
| 259 | |
| 260 | std::vector<const DexFile*> result; |
| 261 | for (const ClassLoaderInfo& info : class_loader_chain_) { |
| 262 | for (const std::unique_ptr<const DexFile>& dex_file : info.opened_dex_files) { |
| 263 | result.push_back(dex_file.get()); |
| 264 | } |
| 265 | } |
| 266 | return result; |
| 267 | } |
| 268 | |
| 269 | const char* ClassLoaderContext::GetClassLoaderTypeName(ClassLoaderType type) { |
| 270 | switch (type) { |
| 271 | case kPathClassLoader: return kPathClassLoaderString; |
| 272 | case kDelegateLastClassLoader: return kDelegateLastClassLoaderString; |
| 273 | default: |
| 274 | LOG(FATAL) << "Invalid class loader type " << type; |
| 275 | UNREACHABLE(); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void ClassLoaderContext::CheckDexFilesOpened(const std::string& calling_method) const { |
| 280 | CHECK(dex_files_open_attempted_) |
| 281 | << "Dex files were not successfully opened before the call to " << calling_method |
| 282 | << "attempt=" << dex_files_open_attempted_ << ", result=" << dex_files_open_result_; |
| 283 | } |
| 284 | } // namespace art |
| 285 | |