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 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 19 | #include "art_field-inl.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 20 | #include "base/dchecked_vector.h" |
| 21 | #include "base/stl_util.h" |
| 22 | #include "class_linker.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 23 | #include "class_loader_utils.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 24 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 25 | #include "dex/dex_file.h" |
| 26 | #include "dex/dex_file_loader.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 27 | #include "handle_scope-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 28 | #include "jni/jni_internal.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 29 | #include "oat_file_assistant.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 30 | #include "obj_ptr-inl.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 31 | #include "runtime.h" |
| 32 | #include "scoped_thread_state_change-inl.h" |
| 33 | #include "thread.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 34 | #include "well_known_classes.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | static constexpr char kPathClassLoaderString[] = "PCL"; |
| 39 | static constexpr char kDelegateLastClassLoaderString[] = "DLC"; |
| 40 | static constexpr char kClassLoaderOpeningMark = '['; |
| 41 | static constexpr char kClassLoaderClosingMark = ']'; |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 42 | static constexpr char kClassLoaderSeparator = ';'; |
| 43 | static constexpr char kClasspathSeparator = ':'; |
| 44 | static constexpr char kDexFileChecksumSeparator = '*'; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 45 | |
| 46 | ClassLoaderContext::ClassLoaderContext() |
| 47 | : special_shared_library_(false), |
| 48 | dex_files_open_attempted_(false), |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 49 | dex_files_open_result_(false), |
Calin Juravle | 41acdc1 | 2017-07-18 17:45:32 -0700 | [diff] [blame] | 50 | owns_the_dex_files_(true) {} |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 51 | |
| 52 | ClassLoaderContext::ClassLoaderContext(bool owns_the_dex_files) |
| 53 | : special_shared_library_(false), |
| 54 | dex_files_open_attempted_(true), |
| 55 | dex_files_open_result_(true), |
| 56 | owns_the_dex_files_(owns_the_dex_files) {} |
| 57 | |
| 58 | ClassLoaderContext::~ClassLoaderContext() { |
| 59 | if (!owns_the_dex_files_) { |
| 60 | // If the context does not own the dex/oat files release the unique pointers to |
| 61 | // make sure we do not de-allocate them. |
| 62 | for (ClassLoaderInfo& info : class_loader_chain_) { |
| 63 | for (std::unique_ptr<OatFile>& oat_file : info.opened_oat_files) { |
| 64 | oat_file.release(); |
| 65 | } |
| 66 | for (std::unique_ptr<const DexFile>& dex_file : info.opened_dex_files) { |
| 67 | dex_file.release(); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 72 | |
Calin Juravle | 1991589 | 2017-08-03 17:10:36 +0000 | [diff] [blame] | 73 | std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Default() { |
| 74 | return Create(""); |
| 75 | } |
| 76 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 77 | std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Create(const std::string& spec) { |
| 78 | std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext()); |
| 79 | if (result->Parse(spec)) { |
| 80 | return result; |
| 81 | } else { |
| 82 | return nullptr; |
| 83 | } |
| 84 | } |
| 85 | |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 86 | // The expected format is: "ClassLoaderType1[ClasspathElem1*Checksum1:ClasspathElem2*Checksum2...]". |
| 87 | // The checksum part of the format is expected only if parse_cheksums is true. |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 88 | bool ClassLoaderContext::ParseClassLoaderSpec(const std::string& class_loader_spec, |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 89 | ClassLoaderType class_loader_type, |
| 90 | bool parse_checksums) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 91 | const char* class_loader_type_str = GetClassLoaderTypeName(class_loader_type); |
| 92 | size_t type_str_size = strlen(class_loader_type_str); |
| 93 | |
| 94 | CHECK_EQ(0, class_loader_spec.compare(0, type_str_size, class_loader_type_str)); |
| 95 | |
| 96 | // Check the opening and closing markers. |
| 97 | if (class_loader_spec[type_str_size] != kClassLoaderOpeningMark) { |
| 98 | return false; |
| 99 | } |
| 100 | if (class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderClosingMark) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | // At this point we know the format is ok; continue and extract the classpath. |
| 105 | // Note that class loaders with an empty class path are allowed. |
| 106 | std::string classpath = class_loader_spec.substr(type_str_size + 1, |
| 107 | class_loader_spec.length() - type_str_size - 2); |
| 108 | |
| 109 | class_loader_chain_.push_back(ClassLoaderInfo(class_loader_type)); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 110 | |
| 111 | if (!parse_checksums) { |
| 112 | Split(classpath, kClasspathSeparator, &class_loader_chain_.back().classpath); |
| 113 | } else { |
| 114 | std::vector<std::string> classpath_elements; |
| 115 | Split(classpath, kClasspathSeparator, &classpath_elements); |
| 116 | for (const std::string& element : classpath_elements) { |
| 117 | std::vector<std::string> dex_file_with_checksum; |
| 118 | Split(element, kDexFileChecksumSeparator, &dex_file_with_checksum); |
| 119 | if (dex_file_with_checksum.size() != 2) { |
| 120 | return false; |
| 121 | } |
| 122 | uint32_t checksum = 0; |
| 123 | if (!ParseInt(dex_file_with_checksum[1].c_str(), &checksum)) { |
| 124 | return false; |
| 125 | } |
| 126 | class_loader_chain_.back().classpath.push_back(dex_file_with_checksum[0]); |
| 127 | class_loader_chain_.back().checksums.push_back(checksum); |
| 128 | } |
| 129 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | // Extracts the class loader type from the given spec. |
| 135 | // Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not |
| 136 | // recognized. |
| 137 | ClassLoaderContext::ClassLoaderType |
| 138 | ClassLoaderContext::ExtractClassLoaderType(const std::string& class_loader_spec) { |
| 139 | const ClassLoaderType kValidTypes[] = {kPathClassLoader, kDelegateLastClassLoader}; |
| 140 | for (const ClassLoaderType& type : kValidTypes) { |
| 141 | const char* type_str = GetClassLoaderTypeName(type); |
| 142 | if (class_loader_spec.compare(0, strlen(type_str), type_str) == 0) { |
| 143 | return type; |
| 144 | } |
| 145 | } |
| 146 | return kInvalidClassLoader; |
| 147 | } |
| 148 | |
| 149 | // The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]... |
| 150 | // ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader). |
| 151 | // ClasspathElem is the path of dex/jar/apk file. |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 152 | bool ClassLoaderContext::Parse(const std::string& spec, bool parse_checksums) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 153 | if (spec.empty()) { |
Calin Juravle | 1a509c8 | 2017-07-24 16:51:21 -0700 | [diff] [blame] | 154 | // By default we load the dex files in a PathClassLoader. |
| 155 | // So an empty spec is equivalent to an empty PathClassLoader (this happens when running |
| 156 | // tests) |
| 157 | class_loader_chain_.push_back(ClassLoaderInfo(kPathClassLoader)); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 158 | return true; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 159 | } |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 160 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 161 | // Stop early if we detect the special shared library, which may be passed as the classpath |
| 162 | // for dex2oat when we want to skip the shared libraries check. |
| 163 | if (spec == OatFile::kSpecialSharedLibrary) { |
| 164 | LOG(INFO) << "The ClassLoaderContext is a special shared library."; |
| 165 | special_shared_library_ = true; |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | std::vector<std::string> class_loaders; |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 170 | Split(spec, kClassLoaderSeparator, &class_loaders); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 171 | |
| 172 | for (const std::string& class_loader : class_loaders) { |
| 173 | ClassLoaderType type = ExtractClassLoaderType(class_loader); |
| 174 | if (type == kInvalidClassLoader) { |
| 175 | LOG(ERROR) << "Invalid class loader type: " << class_loader; |
| 176 | return false; |
| 177 | } |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 178 | if (!ParseClassLoaderSpec(class_loader, type, parse_checksums)) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 179 | LOG(ERROR) << "Invalid class loader spec: " << class_loader; |
| 180 | return false; |
| 181 | } |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | // Opens requested class path files and appends them to opened_dex_files. If the dex files have |
| 187 | // been stripped, this opens them from their oat files (which get added to opened_oat_files). |
| 188 | bool ClassLoaderContext::OpenDexFiles(InstructionSet isa, const std::string& classpath_dir) { |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 189 | if (dex_files_open_attempted_) { |
| 190 | // Do not attempt to re-open the files if we already tried. |
| 191 | return dex_files_open_result_; |
| 192 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 193 | |
| 194 | dex_files_open_attempted_ = true; |
| 195 | // Assume we can open all dex files. If not, we will set this to false as we go. |
| 196 | dex_files_open_result_ = true; |
| 197 | |
| 198 | if (special_shared_library_) { |
| 199 | // Nothing to open if the context is a special shared library. |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | // Note that we try to open all dex files even if some fail. |
| 204 | // We may get resource-only apks which we cannot load. |
| 205 | // TODO(calin): Refine the dex opening interface to be able to tell if an archive contains |
| 206 | // no dex files. So that we can distinguish the real failures... |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 207 | const ArtDexFileLoader dex_file_loader; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 208 | for (ClassLoaderInfo& info : class_loader_chain_) { |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 209 | size_t opened_dex_files_index = info.opened_dex_files.size(); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 210 | for (const std::string& cp_elem : info.classpath) { |
| 211 | // If path is relative, append it to the provided base directory. |
Calin Juravle | 92003fe | 2017-09-06 02:22:57 +0000 | [diff] [blame] | 212 | std::string location = cp_elem; |
| 213 | if (location[0] != '/' && !classpath_dir.empty()) { |
Nicolas Geoffray | 06ffecf | 2017-11-14 10:31:54 +0000 | [diff] [blame] | 214 | location = classpath_dir + (classpath_dir.back() == '/' ? "" : "/") + location; |
Calin Juravle | 821a259 | 2017-08-11 14:33:38 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 217 | std::string error_msg; |
| 218 | // When opening the dex files from the context we expect their checksum to match their |
| 219 | // contents. So pass true to verify_checksum. |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 220 | if (!dex_file_loader.Open(location.c_str(), |
| 221 | location.c_str(), |
| 222 | Runtime::Current()->IsVerificationEnabled(), |
| 223 | /*verify_checksum*/ true, |
| 224 | &error_msg, |
| 225 | &info.opened_dex_files)) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 226 | // If we fail to open the dex file because it's been stripped, try to open the dex file |
| 227 | // from its corresponding oat file. |
| 228 | // This could happen when we need to recompile a pre-build whose dex code has been stripped. |
| 229 | // (for example, if the pre-build is only quicken and we want to re-compile it |
| 230 | // speed-profile). |
| 231 | // TODO(calin): Use the vdex directly instead of going through the oat file. |
| 232 | OatFileAssistant oat_file_assistant(location.c_str(), isa, false); |
| 233 | std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile()); |
| 234 | std::vector<std::unique_ptr<const DexFile>> oat_dex_files; |
| 235 | if (oat_file != nullptr && |
| 236 | OatFileAssistant::LoadDexFiles(*oat_file, location, &oat_dex_files)) { |
| 237 | info.opened_oat_files.push_back(std::move(oat_file)); |
| 238 | info.opened_dex_files.insert(info.opened_dex_files.end(), |
| 239 | std::make_move_iterator(oat_dex_files.begin()), |
| 240 | std::make_move_iterator(oat_dex_files.end())); |
| 241 | } else { |
| 242 | LOG(WARNING) << "Could not open dex files from location: " << location; |
| 243 | dex_files_open_result_ = false; |
| 244 | } |
| 245 | } |
| 246 | } |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 247 | |
| 248 | // We finished opening the dex files from the classpath. |
| 249 | // Now update the classpath and the checksum with the locations of the dex files. |
| 250 | // |
| 251 | // We do this because initially the classpath contains the paths of the dex files; and |
| 252 | // some of them might be multi-dexes. So in order to have a consistent view we replace all the |
| 253 | // file paths with the actual dex locations being loaded. |
| 254 | // This will allow the context to VerifyClassLoaderContextMatch which expects or multidex |
| 255 | // location in the class paths. |
| 256 | // Note that this will also remove the paths that could not be opened. |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 257 | info.original_classpath = std::move(info.classpath); |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 258 | info.classpath.clear(); |
| 259 | info.checksums.clear(); |
| 260 | for (size_t k = opened_dex_files_index; k < info.opened_dex_files.size(); k++) { |
| 261 | std::unique_ptr<const DexFile>& dex = info.opened_dex_files[k]; |
| 262 | info.classpath.push_back(dex->GetLocation()); |
| 263 | info.checksums.push_back(dex->GetLocationChecksum()); |
| 264 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | return dex_files_open_result_; |
| 268 | } |
| 269 | |
| 270 | bool ClassLoaderContext::RemoveLocationsFromClassPaths( |
| 271 | const dchecked_vector<std::string>& locations) { |
| 272 | CHECK(!dex_files_open_attempted_) |
| 273 | << "RemoveLocationsFromClasspaths cannot be call after OpenDexFiles"; |
| 274 | |
| 275 | std::set<std::string> canonical_locations; |
| 276 | for (const std::string& location : locations) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 277 | canonical_locations.insert(DexFileLoader::GetDexCanonicalLocation(location.c_str())); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 278 | } |
| 279 | bool removed_locations = false; |
| 280 | for (ClassLoaderInfo& info : class_loader_chain_) { |
| 281 | size_t initial_size = info.classpath.size(); |
| 282 | auto kept_it = std::remove_if( |
| 283 | info.classpath.begin(), |
| 284 | info.classpath.end(), |
| 285 | [canonical_locations](const std::string& location) { |
| 286 | return ContainsElement(canonical_locations, |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 287 | DexFileLoader::GetDexCanonicalLocation(location.c_str())); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 288 | }); |
| 289 | info.classpath.erase(kept_it, info.classpath.end()); |
| 290 | if (initial_size != info.classpath.size()) { |
| 291 | removed_locations = true; |
| 292 | } |
| 293 | } |
| 294 | return removed_locations; |
| 295 | } |
| 296 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 297 | std::string ClassLoaderContext::EncodeContextForDex2oat(const std::string& base_dir) const { |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 298 | return EncodeContext(base_dir, /*for_dex2oat*/ true, /*stored_context*/ nullptr); |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 301 | std::string ClassLoaderContext::EncodeContextForOatFile(const std::string& base_dir, |
| 302 | ClassLoaderContext* stored_context) const { |
| 303 | return EncodeContext(base_dir, /*for_dex2oat*/ false, stored_context); |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | std::string ClassLoaderContext::EncodeContext(const std::string& base_dir, |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 307 | bool for_dex2oat, |
| 308 | ClassLoaderContext* stored_context) const { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 309 | CheckDexFilesOpened("EncodeContextForOatFile"); |
| 310 | if (special_shared_library_) { |
| 311 | return OatFile::kSpecialSharedLibrary; |
| 312 | } |
| 313 | |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 314 | if (stored_context != nullptr) { |
| 315 | DCHECK_EQ(class_loader_chain_.size(), stored_context->class_loader_chain_.size()); |
| 316 | } |
| 317 | |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 318 | std::ostringstream out; |
Calin Juravle | 1a509c8 | 2017-07-24 16:51:21 -0700 | [diff] [blame] | 319 | if (class_loader_chain_.empty()) { |
| 320 | // We can get in this situation if the context was created with a class path containing the |
| 321 | // source dex files which were later removed (happens during run-tests). |
| 322 | out << GetClassLoaderTypeName(kPathClassLoader) |
| 323 | << kClassLoaderOpeningMark |
| 324 | << kClassLoaderClosingMark; |
| 325 | return out.str(); |
| 326 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 327 | |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 328 | for (size_t i = 0; i < class_loader_chain_.size(); i++) { |
| 329 | const ClassLoaderInfo& info = class_loader_chain_[i]; |
| 330 | if (i > 0) { |
| 331 | out << kClassLoaderSeparator; |
| 332 | } |
| 333 | out << GetClassLoaderTypeName(info.type); |
| 334 | out << kClassLoaderOpeningMark; |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 335 | std::set<std::string> seen_locations; |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 336 | SafeMap<std::string, std::string> remap; |
| 337 | if (stored_context != nullptr) { |
| 338 | DCHECK_EQ(info.original_classpath.size(), |
| 339 | stored_context->class_loader_chain_[i].classpath.size()); |
| 340 | for (size_t k = 0; k < info.original_classpath.size(); ++k) { |
| 341 | // Note that we don't care if the same name appears twice. |
| 342 | remap.Put(info.original_classpath[k], stored_context->class_loader_chain_[i].classpath[k]); |
| 343 | } |
| 344 | } |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 345 | for (size_t k = 0; k < info.opened_dex_files.size(); k++) { |
| 346 | const std::unique_ptr<const DexFile>& dex_file = info.opened_dex_files[k]; |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 347 | if (for_dex2oat) { |
| 348 | // dex2oat only needs the base location. It cannot accept multidex locations. |
| 349 | // So ensure we only add each file once. |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 350 | bool new_insert = seen_locations.insert( |
| 351 | DexFileLoader::GetBaseLocation(dex_file->GetLocation())).second; |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 352 | if (!new_insert) { |
| 353 | continue; |
| 354 | } |
| 355 | } |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 356 | std::string location = dex_file->GetLocation(); |
| 357 | // If there is a stored class loader remap, fix up the multidex strings. |
| 358 | if (!remap.empty()) { |
| 359 | std::string base_dex_location = DexFileLoader::GetBaseLocation(location); |
| 360 | auto it = remap.find(base_dex_location); |
| 361 | CHECK(it != remap.end()) << base_dex_location; |
| 362 | location = it->second + DexFileLoader::GetMultiDexSuffix(location); |
| 363 | } |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 364 | if (k > 0) { |
| 365 | out << kClasspathSeparator; |
| 366 | } |
| 367 | // Find paths that were relative and convert them back from absolute. |
| 368 | if (!base_dir.empty() && location.substr(0, base_dir.length()) == base_dir) { |
| 369 | out << location.substr(base_dir.length() + 1).c_str(); |
| 370 | } else { |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 371 | out << location.c_str(); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 372 | } |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 373 | // dex2oat does not need the checksums. |
| 374 | if (!for_dex2oat) { |
| 375 | out << kDexFileChecksumSeparator; |
| 376 | out << dex_file->GetLocationChecksum(); |
| 377 | } |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 378 | } |
| 379 | out << kClassLoaderClosingMark; |
| 380 | } |
| 381 | return out.str(); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | jobject ClassLoaderContext::CreateClassLoader( |
| 385 | const std::vector<const DexFile*>& compilation_sources) const { |
| 386 | CheckDexFilesOpened("CreateClassLoader"); |
| 387 | |
| 388 | Thread* self = Thread::Current(); |
| 389 | ScopedObjectAccess soa(self); |
| 390 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 391 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 392 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 393 | if (class_loader_chain_.empty()) { |
| 394 | return class_linker->CreatePathClassLoader(self, compilation_sources); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 397 | // Create the class loaders starting from the top most parent (the one on the last position |
| 398 | // in the chain) but omit the first class loader which will contain the compilation_sources and |
| 399 | // needs special handling. |
| 400 | jobject current_parent = nullptr; // the starting parent is the BootClassLoader. |
| 401 | for (size_t i = class_loader_chain_.size() - 1; i > 0; i--) { |
| 402 | std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector( |
| 403 | class_loader_chain_[i].opened_dex_files); |
| 404 | current_parent = class_linker->CreateWellKnownClassLoader( |
| 405 | self, |
| 406 | class_path_files, |
| 407 | GetClassLoaderClass(class_loader_chain_[i].type), |
| 408 | current_parent); |
| 409 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 410 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 411 | // We set up all the parents. Move on to create the first class loader. |
| 412 | // Its classpath comes first, followed by compilation sources. This ensures that whenever |
| 413 | // we need to resolve classes from it the classpath elements come first. |
| 414 | |
| 415 | std::vector<const DexFile*> first_class_loader_classpath = MakeNonOwningPointerVector( |
| 416 | class_loader_chain_[0].opened_dex_files); |
| 417 | first_class_loader_classpath.insert(first_class_loader_classpath.end(), |
| 418 | compilation_sources.begin(), |
| 419 | compilation_sources.end()); |
| 420 | |
| 421 | return class_linker->CreateWellKnownClassLoader( |
| 422 | self, |
| 423 | first_class_loader_classpath, |
| 424 | GetClassLoaderClass(class_loader_chain_[0].type), |
| 425 | current_parent); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | std::vector<const DexFile*> ClassLoaderContext::FlattenOpenedDexFiles() const { |
| 429 | CheckDexFilesOpened("FlattenOpenedDexFiles"); |
| 430 | |
| 431 | std::vector<const DexFile*> result; |
| 432 | for (const ClassLoaderInfo& info : class_loader_chain_) { |
| 433 | for (const std::unique_ptr<const DexFile>& dex_file : info.opened_dex_files) { |
| 434 | result.push_back(dex_file.get()); |
| 435 | } |
| 436 | } |
| 437 | return result; |
| 438 | } |
| 439 | |
| 440 | const char* ClassLoaderContext::GetClassLoaderTypeName(ClassLoaderType type) { |
| 441 | switch (type) { |
| 442 | case kPathClassLoader: return kPathClassLoaderString; |
| 443 | case kDelegateLastClassLoader: return kDelegateLastClassLoaderString; |
| 444 | default: |
| 445 | LOG(FATAL) << "Invalid class loader type " << type; |
| 446 | UNREACHABLE(); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | void ClassLoaderContext::CheckDexFilesOpened(const std::string& calling_method) const { |
| 451 | CHECK(dex_files_open_attempted_) |
| 452 | << "Dex files were not successfully opened before the call to " << calling_method |
| 453 | << "attempt=" << dex_files_open_attempted_ << ", result=" << dex_files_open_result_; |
| 454 | } |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 455 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 456 | // Collects the dex files from the give Java dex_file object. Only the dex files with |
| 457 | // at least 1 class are collected. If a null java_dex_file is passed this method does nothing. |
| 458 | static bool CollectDexFilesFromJavaDexFile(ObjPtr<mirror::Object> java_dex_file, |
| 459 | ArtField* const cookie_field, |
| 460 | std::vector<const DexFile*>* out_dex_files) |
| 461 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 462 | if (java_dex_file == nullptr) { |
| 463 | return true; |
| 464 | } |
| 465 | // On the Java side, the dex files are stored in the cookie field. |
| 466 | mirror::LongArray* long_array = cookie_field->GetObject(java_dex_file)->AsLongArray(); |
| 467 | if (long_array == nullptr) { |
| 468 | // This should never happen so log a warning. |
| 469 | LOG(ERROR) << "Unexpected null cookie"; |
| 470 | return false; |
| 471 | } |
| 472 | int32_t long_array_size = long_array->GetLength(); |
| 473 | // Index 0 from the long array stores the oat file. The dex files start at index 1. |
| 474 | for (int32_t j = 1; j < long_array_size; ++j) { |
| 475 | const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( |
| 476 | long_array->GetWithoutChecks(j))); |
| 477 | if (cp_dex_file != nullptr && cp_dex_file->NumClassDefs() > 0) { |
| 478 | // TODO(calin): It's unclear why the dex files with no classes are skipped here and when |
| 479 | // cp_dex_file can be null. |
| 480 | out_dex_files->push_back(cp_dex_file); |
| 481 | } |
| 482 | } |
| 483 | return true; |
| 484 | } |
| 485 | |
| 486 | // Collects all the dex files loaded by the given class loader. |
| 487 | // Returns true for success or false if an unexpected state is discovered (e.g. a null dex cookie, |
| 488 | // a null list of dex elements or a null dex element). |
| 489 | static bool CollectDexFilesFromSupportedClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 490 | Handle<mirror::ClassLoader> class_loader, |
| 491 | std::vector<const DexFile*>* out_dex_files) |
| 492 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 493 | CHECK(IsPathOrDexClassLoader(soa, class_loader) || IsDelegateLastClassLoader(soa, class_loader)); |
| 494 | |
| 495 | // All supported class loaders inherit from BaseDexClassLoader. |
| 496 | // We need to get the DexPathList and loop through it. |
| 497 | ArtField* const cookie_field = |
| 498 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 499 | ArtField* const dex_file_field = |
| 500 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
| 501 | ObjPtr<mirror::Object> dex_path_list = |
| 502 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)-> |
| 503 | GetObject(class_loader.Get()); |
| 504 | CHECK(cookie_field != nullptr); |
| 505 | CHECK(dex_file_field != nullptr); |
| 506 | if (dex_path_list == nullptr) { |
| 507 | // This may be null if the current class loader is under construction and it does not |
| 508 | // have its fields setup yet. |
| 509 | return true; |
| 510 | } |
| 511 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 512 | ObjPtr<mirror::Object> dex_elements_obj = |
| 513 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 514 | GetObject(dex_path_list); |
| 515 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 516 | // at the mCookie which is a DexFile vector. |
| 517 | if (dex_elements_obj == nullptr) { |
| 518 | // TODO(calin): It's unclear if we should just assert here. For now be prepared for the worse |
| 519 | // and assume we have no elements. |
| 520 | return true; |
| 521 | } else { |
| 522 | StackHandleScope<1> hs(soa.Self()); |
| 523 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements( |
| 524 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>())); |
| 525 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 526 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 527 | if (element == nullptr) { |
| 528 | // Should never happen, log an error and break. |
| 529 | // TODO(calin): It's unclear if we should just assert here. |
| 530 | // This code was propagated to oat_file_manager from the class linker where it would |
| 531 | // throw a NPE. For now, return false which will mark this class loader as unsupported. |
| 532 | LOG(ERROR) << "Unexpected null in the dex element list"; |
| 533 | return false; |
| 534 | } |
| 535 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
| 536 | if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) { |
| 537 | return false; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | return true; |
| 543 | } |
| 544 | |
| 545 | static bool GetDexFilesFromDexElementsArray( |
| 546 | ScopedObjectAccessAlreadyRunnable& soa, |
| 547 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements, |
| 548 | std::vector<const DexFile*>* out_dex_files) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 549 | DCHECK(dex_elements != nullptr); |
| 550 | |
| 551 | ArtField* const cookie_field = |
| 552 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 553 | ArtField* const dex_file_field = |
| 554 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
| 555 | ObjPtr<mirror::Class> const element_class = soa.Decode<mirror::Class>( |
| 556 | WellKnownClasses::dalvik_system_DexPathList__Element); |
| 557 | ObjPtr<mirror::Class> const dexfile_class = soa.Decode<mirror::Class>( |
| 558 | WellKnownClasses::dalvik_system_DexFile); |
| 559 | |
| 560 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 561 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 562 | // We can hit a null element here because this is invoked with a partially filled dex_elements |
| 563 | // array from DexPathList. DexPathList will open each dex sequentially, each time passing the |
| 564 | // list of dex files which were opened before. |
| 565 | if (element == nullptr) { |
| 566 | continue; |
| 567 | } |
| 568 | |
| 569 | // We support this being dalvik.system.DexPathList$Element and dalvik.system.DexFile. |
| 570 | // TODO(calin): Code caried over oat_file_manager: supporting both classes seem to be |
| 571 | // a historical glitch. All the java code opens dex files using an array of Elements. |
| 572 | ObjPtr<mirror::Object> dex_file; |
| 573 | if (element_class == element->GetClass()) { |
| 574 | dex_file = dex_file_field->GetObject(element); |
| 575 | } else if (dexfile_class == element->GetClass()) { |
| 576 | dex_file = element; |
| 577 | } else { |
| 578 | LOG(ERROR) << "Unsupported element in dex_elements: " |
| 579 | << mirror::Class::PrettyClass(element->GetClass()); |
| 580 | return false; |
| 581 | } |
| 582 | |
| 583 | if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) { |
| 584 | return false; |
| 585 | } |
| 586 | } |
| 587 | return true; |
| 588 | } |
| 589 | |
| 590 | // Adds the `class_loader` info to the `context`. |
| 591 | // The dex file present in `dex_elements` array (if not null) will be added at the end of |
| 592 | // the classpath. |
| 593 | // This method is recursive (w.r.t. the class loader parent) and will stop once it reaches the |
| 594 | // BootClassLoader. Note that the class loader chain is expected to be short. |
| 595 | bool ClassLoaderContext::AddInfoToContextFromClassLoader( |
| 596 | ScopedObjectAccessAlreadyRunnable& soa, |
| 597 | Handle<mirror::ClassLoader> class_loader, |
| 598 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements) |
| 599 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 600 | if (ClassLinker::IsBootClassLoader(soa, class_loader.Get())) { |
| 601 | // Nothing to do for the boot class loader as we don't add its dex files to the context. |
| 602 | return true; |
| 603 | } |
| 604 | |
| 605 | ClassLoaderContext::ClassLoaderType type; |
| 606 | if (IsPathOrDexClassLoader(soa, class_loader)) { |
| 607 | type = kPathClassLoader; |
| 608 | } else if (IsDelegateLastClassLoader(soa, class_loader)) { |
| 609 | type = kDelegateLastClassLoader; |
| 610 | } else { |
| 611 | LOG(WARNING) << "Unsupported class loader"; |
| 612 | return false; |
| 613 | } |
| 614 | |
| 615 | // Inspect the class loader for its dex files. |
| 616 | std::vector<const DexFile*> dex_files_loaded; |
| 617 | CollectDexFilesFromSupportedClassLoader(soa, class_loader, &dex_files_loaded); |
| 618 | |
| 619 | // If we have a dex_elements array extract its dex elements now. |
| 620 | // This is used in two situations: |
| 621 | // 1) when a new ClassLoader is created DexPathList will open each dex file sequentially |
| 622 | // passing the list of already open dex files each time. This ensures that we see the |
| 623 | // correct context even if the ClassLoader under construction is not fully build. |
| 624 | // 2) when apk splits are loaded on the fly, the framework will load their dex files by |
| 625 | // appending them to the current class loader. When the new code paths are loaded in |
| 626 | // BaseDexClassLoader, the paths already present in the class loader will be passed |
| 627 | // in the dex_elements array. |
| 628 | if (dex_elements != nullptr) { |
| 629 | GetDexFilesFromDexElementsArray(soa, dex_elements, &dex_files_loaded); |
| 630 | } |
| 631 | |
| 632 | class_loader_chain_.push_back(ClassLoaderContext::ClassLoaderInfo(type)); |
| 633 | ClassLoaderInfo& info = class_loader_chain_.back(); |
| 634 | for (const DexFile* dex_file : dex_files_loaded) { |
| 635 | info.classpath.push_back(dex_file->GetLocation()); |
| 636 | info.checksums.push_back(dex_file->GetLocationChecksum()); |
| 637 | info.opened_dex_files.emplace_back(dex_file); |
| 638 | } |
| 639 | |
| 640 | // We created the ClassLoaderInfo for the current loader. Move on to its parent. |
| 641 | |
| 642 | StackHandleScope<1> hs(Thread::Current()); |
| 643 | Handle<mirror::ClassLoader> parent = hs.NewHandle(class_loader->GetParent()); |
| 644 | |
| 645 | // Note that dex_elements array is null here. The elements are considered to be part of the |
| 646 | // current class loader and are not passed to the parents. |
| 647 | ScopedNullHandle<mirror::ObjectArray<mirror::Object>> null_dex_elements; |
| 648 | return AddInfoToContextFromClassLoader(soa, parent, null_dex_elements); |
| 649 | } |
| 650 | |
| 651 | std::unique_ptr<ClassLoaderContext> ClassLoaderContext::CreateContextForClassLoader( |
| 652 | jobject class_loader, |
| 653 | jobjectArray dex_elements) { |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 654 | CHECK(class_loader != nullptr); |
| 655 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 656 | ScopedObjectAccess soa(Thread::Current()); |
| 657 | StackHandleScope<2> hs(soa.Self()); |
| 658 | Handle<mirror::ClassLoader> h_class_loader = |
| 659 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)); |
| 660 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements = |
| 661 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements)); |
| 662 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 663 | std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext(/*owns_the_dex_files*/ false)); |
| 664 | if (result->AddInfoToContextFromClassLoader(soa, h_class_loader, h_dex_elements)) { |
| 665 | return result; |
| 666 | } else { |
| 667 | return nullptr; |
| 668 | } |
| 669 | } |
| 670 | |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 671 | static bool IsAbsoluteLocation(const std::string& location) { |
| 672 | return !location.empty() && location[0] == '/'; |
| 673 | } |
| 674 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 675 | ClassLoaderContext::VerificationResult ClassLoaderContext::VerifyClassLoaderContextMatch( |
| 676 | const std::string& context_spec, |
| 677 | bool verify_names, |
| 678 | bool verify_checksums) const { |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 679 | if (verify_names || verify_checksums) { |
| 680 | DCHECK(dex_files_open_attempted_); |
| 681 | DCHECK(dex_files_open_result_); |
| 682 | } |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 683 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 684 | ClassLoaderContext expected_context; |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 685 | if (!expected_context.Parse(context_spec, verify_checksums)) { |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 686 | LOG(WARNING) << "Invalid class loader context: " << context_spec; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 687 | return VerificationResult::kMismatch; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 690 | // Special shared library contexts always match. They essentially instruct the runtime |
| 691 | // to ignore the class path check because the oat file is known to be loaded in different |
| 692 | // contexts. OatFileManager will further verify if the oat file can be loaded based on the |
| 693 | // collision check. |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 694 | if (expected_context.special_shared_library_) { |
| 695 | // Special case where we are the only entry in the class path. |
| 696 | if (class_loader_chain_.size() == 1 && class_loader_chain_[0].classpath.size() == 0) { |
| 697 | return VerificationResult::kVerifies; |
| 698 | } |
| 699 | return VerificationResult::kForcedToSkipChecks; |
| 700 | } else if (special_shared_library_) { |
| 701 | return VerificationResult::kForcedToSkipChecks; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | if (expected_context.class_loader_chain_.size() != class_loader_chain_.size()) { |
| 705 | LOG(WARNING) << "ClassLoaderContext size mismatch. expected=" |
| 706 | << expected_context.class_loader_chain_.size() |
Andreas Gampe | 7d0f81c | 2017-07-25 18:25:41 -0700 | [diff] [blame] | 707 | << ", actual=" << class_loader_chain_.size() |
| 708 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 709 | return VerificationResult::kMismatch; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | for (size_t i = 0; i < class_loader_chain_.size(); i++) { |
| 713 | const ClassLoaderInfo& info = class_loader_chain_[i]; |
| 714 | const ClassLoaderInfo& expected_info = expected_context.class_loader_chain_[i]; |
| 715 | if (info.type != expected_info.type) { |
| 716 | LOG(WARNING) << "ClassLoaderContext type mismatch for position " << i |
| 717 | << ". expected=" << GetClassLoaderTypeName(expected_info.type) |
Andreas Gampe | 7d0f81c | 2017-07-25 18:25:41 -0700 | [diff] [blame] | 718 | << ", found=" << GetClassLoaderTypeName(info.type) |
| 719 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 720 | return VerificationResult::kMismatch; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 721 | } |
| 722 | if (info.classpath.size() != expected_info.classpath.size()) { |
| 723 | LOG(WARNING) << "ClassLoaderContext classpath size mismatch for position " << i |
| 724 | << ". expected=" << expected_info.classpath.size() |
Andreas Gampe | 7d0f81c | 2017-07-25 18:25:41 -0700 | [diff] [blame] | 725 | << ", found=" << info.classpath.size() |
| 726 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 727 | return VerificationResult::kMismatch; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 730 | if (verify_checksums) { |
| 731 | DCHECK_EQ(info.classpath.size(), info.checksums.size()); |
| 732 | DCHECK_EQ(expected_info.classpath.size(), expected_info.checksums.size()); |
| 733 | } |
| 734 | |
| 735 | if (!verify_names) { |
| 736 | continue; |
| 737 | } |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 738 | |
| 739 | for (size_t k = 0; k < info.classpath.size(); k++) { |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 740 | // Compute the dex location that must be compared. |
| 741 | // We shouldn't do a naive comparison `info.classpath[k] == expected_info.classpath[k]` |
| 742 | // because even if they refer to the same file, one could be encoded as a relative location |
| 743 | // and the other as an absolute one. |
| 744 | bool is_dex_name_absolute = IsAbsoluteLocation(info.classpath[k]); |
| 745 | bool is_expected_dex_name_absolute = IsAbsoluteLocation(expected_info.classpath[k]); |
| 746 | std::string dex_name; |
| 747 | std::string expected_dex_name; |
| 748 | |
| 749 | if (is_dex_name_absolute == is_expected_dex_name_absolute) { |
| 750 | // If both locations are absolute or relative then compare them as they are. |
| 751 | // This is usually the case for: shared libraries and secondary dex files. |
| 752 | dex_name = info.classpath[k]; |
| 753 | expected_dex_name = expected_info.classpath[k]; |
| 754 | } else if (is_dex_name_absolute) { |
| 755 | // The runtime name is absolute but the compiled name (the expected one) is relative. |
| 756 | // This is the case for split apks which depend on base or on other splits. |
| 757 | dex_name = info.classpath[k]; |
| 758 | expected_dex_name = OatFile::ResolveRelativeEncodedDexLocation( |
| 759 | info.classpath[k].c_str(), expected_info.classpath[k]); |
Calin Juravle | 92003fe | 2017-09-06 02:22:57 +0000 | [diff] [blame] | 760 | } else if (is_expected_dex_name_absolute) { |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 761 | // The runtime name is relative but the compiled name is absolute. |
| 762 | // There is no expected use case that would end up here as dex files are always loaded |
| 763 | // with their absolute location. However, be tolerant and do the best effort (in case |
| 764 | // there are unexpected new use case...). |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 765 | dex_name = OatFile::ResolveRelativeEncodedDexLocation( |
| 766 | expected_info.classpath[k].c_str(), info.classpath[k]); |
| 767 | expected_dex_name = expected_info.classpath[k]; |
Calin Juravle | 92003fe | 2017-09-06 02:22:57 +0000 | [diff] [blame] | 768 | } else { |
| 769 | // Both locations are relative. In this case there's not much we can be sure about |
| 770 | // except that the names are the same. The checksum will ensure that the files are |
| 771 | // are same. This should not happen outside testing and manual invocations. |
| 772 | dex_name = info.classpath[k]; |
| 773 | expected_dex_name = expected_info.classpath[k]; |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | // Compare the locations. |
| 777 | if (dex_name != expected_dex_name) { |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 778 | LOG(WARNING) << "ClassLoaderContext classpath element mismatch for position " << i |
| 779 | << ". expected=" << expected_info.classpath[k] |
Andreas Gampe | 7d0f81c | 2017-07-25 18:25:41 -0700 | [diff] [blame] | 780 | << ", found=" << info.classpath[k] |
| 781 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 782 | return VerificationResult::kMismatch; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 783 | } |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 784 | |
| 785 | // Compare the checksums. |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 786 | if (info.checksums[k] != expected_info.checksums[k]) { |
| 787 | LOG(WARNING) << "ClassLoaderContext classpath element checksum mismatch for position " << i |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 788 | << ". expected=" << expected_info.checksums[k] |
| 789 | << ", found=" << info.checksums[k] |
| 790 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 791 | return VerificationResult::kMismatch; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | } |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 795 | return VerificationResult::kVerifies; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 798 | jclass ClassLoaderContext::GetClassLoaderClass(ClassLoaderType type) { |
| 799 | switch (type) { |
| 800 | case kPathClassLoader: return WellKnownClasses::dalvik_system_PathClassLoader; |
| 801 | case kDelegateLastClassLoader: return WellKnownClasses::dalvik_system_DelegateLastClassLoader; |
| 802 | case kInvalidClassLoader: break; // will fail after the switch. |
| 803 | } |
| 804 | LOG(FATAL) << "Invalid class loader type " << type; |
| 805 | UNREACHABLE(); |
| 806 | } |
| 807 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 808 | } // namespace art |