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 | |
Andreas Gampe | f941170 | 2018-09-06 17:16:57 -0700 | [diff] [blame] | 19 | #include <android-base/parseint.h> |
Nicolas Geoffray | cb2e1dd | 2018-11-20 11:15:13 +0000 | [diff] [blame] | 20 | #include <android-base/strings.h> |
Andreas Gampe | f941170 | 2018-09-06 17:16:57 -0700 | [diff] [blame] | 21 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 23 | #include "base/casts.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 24 | #include "base/dchecked_vector.h" |
Andreas Gampe | 19f5416 | 2019-05-14 16:16:28 -0700 | [diff] [blame] | 25 | #include "base/file_utils.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 26 | #include "base/stl_util.h" |
| 27 | #include "class_linker.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 28 | #include "class_loader_utils.h" |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 29 | #include "class_root.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 30 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 31 | #include "dex/dex_file.h" |
| 32 | #include "dex/dex_file_loader.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 33 | #include "handle_scope-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 34 | #include "jni/jni_internal.h" |
Vladimir Marko | bdc93b4 | 2019-03-29 16:12:04 +0000 | [diff] [blame] | 35 | #include "mirror/class_loader-inl.h" |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 36 | #include "mirror/object.h" |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 37 | #include "mirror/object_array-alloc-inl.h" |
| 38 | #include "nativehelper/scoped_local_ref.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 39 | #include "oat_file_assistant.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 40 | #include "obj_ptr-inl.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 41 | #include "runtime.h" |
| 42 | #include "scoped_thread_state_change-inl.h" |
| 43 | #include "thread.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 44 | #include "well_known_classes.h" |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 45 | |
| 46 | namespace art { |
| 47 | |
| 48 | static constexpr char kPathClassLoaderString[] = "PCL"; |
| 49 | static constexpr char kDelegateLastClassLoaderString[] = "DLC"; |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 50 | static constexpr char kInMemoryDexClassLoaderString[] = "IMC"; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 51 | static constexpr char kClassLoaderOpeningMark = '['; |
| 52 | static constexpr char kClassLoaderClosingMark = ']'; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 53 | static constexpr char kClassLoaderSharedLibraryOpeningMark = '{'; |
| 54 | static constexpr char kClassLoaderSharedLibraryClosingMark = '}'; |
| 55 | static constexpr char kClassLoaderSharedLibrarySeparator = '#'; |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 56 | static constexpr char kClassLoaderSeparator = ';'; |
| 57 | static constexpr char kClasspathSeparator = ':'; |
| 58 | static constexpr char kDexFileChecksumSeparator = '*'; |
David Brazdil | 93d339d | 2019-03-27 09:56:45 +0000 | [diff] [blame] | 59 | static constexpr char kInMemoryDexClassLoaderDexLocationMagic[] = "<unknown>"; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 60 | |
| 61 | ClassLoaderContext::ClassLoaderContext() |
| 62 | : special_shared_library_(false), |
| 63 | dex_files_open_attempted_(false), |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 64 | dex_files_open_result_(false), |
Calin Juravle | 41acdc1 | 2017-07-18 17:45:32 -0700 | [diff] [blame] | 65 | owns_the_dex_files_(true) {} |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 66 | |
| 67 | ClassLoaderContext::ClassLoaderContext(bool owns_the_dex_files) |
| 68 | : special_shared_library_(false), |
| 69 | dex_files_open_attempted_(true), |
| 70 | dex_files_open_result_(true), |
| 71 | owns_the_dex_files_(owns_the_dex_files) {} |
| 72 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 73 | // Utility method to add parent and shared libraries of `info` into |
| 74 | // the `work_list`. |
| 75 | static void AddToWorkList( |
| 76 | ClassLoaderContext::ClassLoaderInfo* info, |
| 77 | std::vector<ClassLoaderContext::ClassLoaderInfo*>& work_list) { |
| 78 | if (info->parent != nullptr) { |
| 79 | work_list.push_back(info->parent.get()); |
| 80 | } |
| 81 | for (size_t i = 0; i < info->shared_libraries.size(); ++i) { |
| 82 | work_list.push_back(info->shared_libraries[i].get()); |
| 83 | } |
| 84 | } |
| 85 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 86 | ClassLoaderContext::~ClassLoaderContext() { |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 87 | if (!owns_the_dex_files_ && class_loader_chain_ != nullptr) { |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 88 | // If the context does not own the dex/oat files release the unique pointers to |
| 89 | // make sure we do not de-allocate them. |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 90 | std::vector<ClassLoaderInfo*> work_list; |
| 91 | work_list.push_back(class_loader_chain_.get()); |
| 92 | while (!work_list.empty()) { |
| 93 | ClassLoaderInfo* info = work_list.back(); |
| 94 | work_list.pop_back(); |
| 95 | for (std::unique_ptr<OatFile>& oat_file : info->opened_oat_files) { |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 96 | oat_file.release(); // NOLINT b/117926937 |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 97 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 98 | for (std::unique_ptr<const DexFile>& dex_file : info->opened_dex_files) { |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 99 | dex_file.release(); // NOLINT b/117926937 |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 100 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 101 | AddToWorkList(info, work_list); |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 105 | |
Calin Juravle | 1991589 | 2017-08-03 17:10:36 +0000 | [diff] [blame] | 106 | std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Default() { |
| 107 | return Create(""); |
| 108 | } |
| 109 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 110 | std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Create(const std::string& spec) { |
| 111 | std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext()); |
| 112 | if (result->Parse(spec)) { |
| 113 | return result; |
| 114 | } else { |
| 115 | return nullptr; |
| 116 | } |
| 117 | } |
| 118 | |
Nicolas Geoffray | f378fff | 2018-11-19 12:52:26 +0000 | [diff] [blame] | 119 | static size_t FindMatchingSharedLibraryCloseMarker(const std::string& spec, |
| 120 | size_t shared_library_open_index) { |
| 121 | // Counter of opened shared library marker we've encountered so far. |
| 122 | uint32_t counter = 1; |
| 123 | // The index at which we're operating in the loop. |
| 124 | uint32_t string_index = shared_library_open_index + 1; |
| 125 | size_t shared_library_close = std::string::npos; |
| 126 | while (counter != 0) { |
| 127 | shared_library_close = |
| 128 | spec.find_first_of(kClassLoaderSharedLibraryClosingMark, string_index); |
| 129 | size_t shared_library_open = |
| 130 | spec.find_first_of(kClassLoaderSharedLibraryOpeningMark, string_index); |
| 131 | if (shared_library_close == std::string::npos) { |
| 132 | // No matching closing marker. Return an error. |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | if ((shared_library_open == std::string::npos) || |
| 137 | (shared_library_close < shared_library_open)) { |
| 138 | // We have seen a closing marker. Decrement the counter. |
| 139 | --counter; |
| 140 | // Move the search index forward. |
| 141 | string_index = shared_library_close + 1; |
| 142 | } else { |
| 143 | // New nested opening marker. Increment the counter and move the search |
| 144 | // index after the marker. |
| 145 | ++counter; |
| 146 | string_index = shared_library_open + 1; |
| 147 | } |
| 148 | } |
| 149 | return shared_library_close; |
| 150 | } |
| 151 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 152 | // The expected format is: |
| 153 | // "ClassLoaderType1[ClasspathElem1*Checksum1:ClasspathElem2*Checksum2...]{ClassLoaderType2[...]}". |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 154 | // The checksum part of the format is expected only if parse_cheksums is true. |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 155 | std::unique_ptr<ClassLoaderContext::ClassLoaderInfo> ClassLoaderContext::ParseClassLoaderSpec( |
| 156 | const std::string& class_loader_spec, |
| 157 | bool parse_checksums) { |
| 158 | ClassLoaderType class_loader_type = ExtractClassLoaderType(class_loader_spec); |
| 159 | if (class_loader_type == kInvalidClassLoader) { |
| 160 | return nullptr; |
| 161 | } |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 162 | |
| 163 | // InMemoryDexClassLoader's dex location is always bogus. Special-case it. |
| 164 | if (class_loader_type == kInMemoryDexClassLoader) { |
| 165 | if (parse_checksums) { |
| 166 | // Make sure that OpenDexFiles() will never be attempted on this context |
| 167 | // because the dex locations of IMC do not correspond to real files. |
| 168 | CHECK(!dex_files_open_attempted_ || !dex_files_open_result_) |
| 169 | << "Parsing spec not supported when context created from a ClassLoader object"; |
| 170 | dex_files_open_attempted_ = true; |
| 171 | dex_files_open_result_ = false; |
| 172 | } else { |
| 173 | // Checksums are not provided and dex locations themselves have no meaning |
| 174 | // (although we keep them in the spec to simplify parsing). Treat this as |
| 175 | // an unknown class loader. |
David Brazdil | 93d339d | 2019-03-27 09:56:45 +0000 | [diff] [blame] | 176 | // We can hit this case if dex2oat is invoked with a spec containing IMC. |
| 177 | // Because the dex file data is only available at runtime, we cannot proceed. |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 178 | return nullptr; |
| 179 | } |
| 180 | } |
| 181 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 182 | const char* class_loader_type_str = GetClassLoaderTypeName(class_loader_type); |
| 183 | size_t type_str_size = strlen(class_loader_type_str); |
| 184 | |
| 185 | CHECK_EQ(0, class_loader_spec.compare(0, type_str_size, class_loader_type_str)); |
| 186 | |
| 187 | // Check the opening and closing markers. |
| 188 | if (class_loader_spec[type_str_size] != kClassLoaderOpeningMark) { |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 189 | return nullptr; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 190 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 191 | if ((class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderClosingMark) && |
| 192 | (class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderSharedLibraryClosingMark)) { |
| 193 | return nullptr; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 196 | size_t closing_index = class_loader_spec.find_first_of(kClassLoaderClosingMark); |
| 197 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 198 | // At this point we know the format is ok; continue and extract the classpath. |
| 199 | // Note that class loaders with an empty class path are allowed. |
| 200 | std::string classpath = class_loader_spec.substr(type_str_size + 1, |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 201 | closing_index - type_str_size - 1); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 202 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 203 | std::unique_ptr<ClassLoaderInfo> info(new ClassLoaderInfo(class_loader_type)); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 204 | |
| 205 | if (!parse_checksums) { |
David Brazdil | 93d339d | 2019-03-27 09:56:45 +0000 | [diff] [blame] | 206 | DCHECK(class_loader_type != kInMemoryDexClassLoader); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 207 | Split(classpath, kClasspathSeparator, &info->classpath); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 208 | } else { |
| 209 | std::vector<std::string> classpath_elements; |
| 210 | Split(classpath, kClasspathSeparator, &classpath_elements); |
| 211 | for (const std::string& element : classpath_elements) { |
| 212 | std::vector<std::string> dex_file_with_checksum; |
| 213 | Split(element, kDexFileChecksumSeparator, &dex_file_with_checksum); |
| 214 | if (dex_file_with_checksum.size() != 2) { |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 215 | return nullptr; |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 216 | } |
| 217 | uint32_t checksum = 0; |
Tom Cherry | 7bc8e8f | 2018-10-05 14:34:13 -0700 | [diff] [blame] | 218 | if (!android::base::ParseUint(dex_file_with_checksum[1].c_str(), &checksum)) { |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 219 | return nullptr; |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 220 | } |
David Brazdil | 93d339d | 2019-03-27 09:56:45 +0000 | [diff] [blame] | 221 | if ((class_loader_type == kInMemoryDexClassLoader) && |
| 222 | (dex_file_with_checksum[0] != kInMemoryDexClassLoaderDexLocationMagic)) { |
| 223 | return nullptr; |
| 224 | } |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 225 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 226 | info->classpath.push_back(dex_file_with_checksum[0]); |
| 227 | info->checksums.push_back(checksum); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 230 | |
Nicolas Geoffray | f378fff | 2018-11-19 12:52:26 +0000 | [diff] [blame] | 231 | if ((class_loader_spec[class_loader_spec.length() - 1] == kClassLoaderSharedLibraryClosingMark) && |
| 232 | (class_loader_spec[class_loader_spec.length() - 2] != kClassLoaderSharedLibraryOpeningMark)) { |
| 233 | // Non-empty list of shared libraries. |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 234 | size_t start_index = class_loader_spec.find_first_of(kClassLoaderSharedLibraryOpeningMark); |
| 235 | if (start_index == std::string::npos) { |
| 236 | return nullptr; |
| 237 | } |
| 238 | std::string shared_libraries_spec = |
| 239 | class_loader_spec.substr(start_index + 1, class_loader_spec.length() - start_index - 2); |
| 240 | std::vector<std::string> shared_libraries; |
Nicolas Geoffray | f378fff | 2018-11-19 12:52:26 +0000 | [diff] [blame] | 241 | size_t cursor = 0; |
| 242 | while (cursor != shared_libraries_spec.length()) { |
| 243 | size_t shared_library_separator = |
| 244 | shared_libraries_spec.find_first_of(kClassLoaderSharedLibrarySeparator, cursor); |
| 245 | size_t shared_library_open = |
| 246 | shared_libraries_spec.find_first_of(kClassLoaderSharedLibraryOpeningMark, cursor); |
| 247 | std::string shared_library_spec; |
| 248 | if (shared_library_separator == std::string::npos) { |
| 249 | // Only one shared library, for example: |
| 250 | // PCL[...] |
| 251 | shared_library_spec = |
| 252 | shared_libraries_spec.substr(cursor, shared_libraries_spec.length() - cursor); |
| 253 | cursor = shared_libraries_spec.length(); |
| 254 | } else if ((shared_library_open == std::string::npos) || |
| 255 | (shared_library_open > shared_library_separator)) { |
| 256 | // We found a shared library without nested shared libraries, for example: |
| 257 | // PCL[...]#PCL[...]{...} |
| 258 | shared_library_spec = |
| 259 | shared_libraries_spec.substr(cursor, shared_library_separator - cursor); |
| 260 | cursor = shared_library_separator + 1; |
| 261 | } else { |
| 262 | // The shared library contains nested shared libraries. Find the matching closing shared |
| 263 | // marker for it. |
| 264 | size_t closing_marker = |
| 265 | FindMatchingSharedLibraryCloseMarker(shared_libraries_spec, shared_library_open); |
| 266 | if (closing_marker == std::string::npos) { |
| 267 | // No matching closing marker, return an error. |
| 268 | return nullptr; |
| 269 | } |
| 270 | shared_library_spec = shared_libraries_spec.substr(cursor, closing_marker + 1 - cursor); |
| 271 | cursor = closing_marker + 1; |
| 272 | if (cursor != shared_libraries_spec.length() && |
| 273 | shared_libraries_spec[cursor] == kClassLoaderSharedLibrarySeparator) { |
| 274 | // Pass the shared library separator marker. |
| 275 | ++cursor; |
| 276 | } |
| 277 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 278 | std::unique_ptr<ClassLoaderInfo> shared_library( |
| 279 | ParseInternal(shared_library_spec, parse_checksums)); |
| 280 | if (shared_library == nullptr) { |
| 281 | return nullptr; |
| 282 | } |
| 283 | info->shared_libraries.push_back(std::move(shared_library)); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return info; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | // Extracts the class loader type from the given spec. |
| 291 | // Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not |
| 292 | // recognized. |
| 293 | ClassLoaderContext::ClassLoaderType |
| 294 | ClassLoaderContext::ExtractClassLoaderType(const std::string& class_loader_spec) { |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 295 | const ClassLoaderType kValidTypes[] = { kPathClassLoader, |
| 296 | kDelegateLastClassLoader, |
| 297 | kInMemoryDexClassLoader }; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 298 | for (const ClassLoaderType& type : kValidTypes) { |
| 299 | const char* type_str = GetClassLoaderTypeName(type); |
| 300 | if (class_loader_spec.compare(0, strlen(type_str), type_str) == 0) { |
| 301 | return type; |
| 302 | } |
| 303 | } |
| 304 | return kInvalidClassLoader; |
| 305 | } |
| 306 | |
| 307 | // The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]... |
| 308 | // ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader). |
| 309 | // ClasspathElem is the path of dex/jar/apk file. |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 310 | bool ClassLoaderContext::Parse(const std::string& spec, bool parse_checksums) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 311 | if (spec.empty()) { |
Calin Juravle | 1a509c8 | 2017-07-24 16:51:21 -0700 | [diff] [blame] | 312 | // By default we load the dex files in a PathClassLoader. |
| 313 | // So an empty spec is equivalent to an empty PathClassLoader (this happens when running |
| 314 | // tests) |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 315 | class_loader_chain_.reset(new ClassLoaderInfo(kPathClassLoader)); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 316 | return true; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 317 | } |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 318 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 319 | // Stop early if we detect the special shared library, which may be passed as the classpath |
| 320 | // for dex2oat when we want to skip the shared libraries check. |
| 321 | if (spec == OatFile::kSpecialSharedLibrary) { |
| 322 | LOG(INFO) << "The ClassLoaderContext is a special shared library."; |
| 323 | special_shared_library_ = true; |
| 324 | return true; |
| 325 | } |
| 326 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 327 | CHECK(class_loader_chain_ == nullptr); |
| 328 | class_loader_chain_.reset(ParseInternal(spec, parse_checksums)); |
| 329 | return class_loader_chain_ != nullptr; |
| 330 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 331 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 332 | ClassLoaderContext::ClassLoaderInfo* ClassLoaderContext::ParseInternal( |
| 333 | const std::string& spec, bool parse_checksums) { |
| 334 | CHECK(!spec.empty()); |
| 335 | CHECK_NE(spec, OatFile::kSpecialSharedLibrary); |
| 336 | std::string remaining = spec; |
| 337 | std::unique_ptr<ClassLoaderInfo> first(nullptr); |
| 338 | ClassLoaderInfo* previous_iteration = nullptr; |
| 339 | while (!remaining.empty()) { |
| 340 | std::string class_loader_spec; |
| 341 | size_t first_class_loader_separator = remaining.find_first_of(kClassLoaderSeparator); |
| 342 | size_t first_shared_library_open = |
| 343 | remaining.find_first_of(kClassLoaderSharedLibraryOpeningMark); |
| 344 | if (first_class_loader_separator == std::string::npos) { |
| 345 | // Only one class loader, for example: |
| 346 | // PCL[...] |
| 347 | class_loader_spec = remaining; |
| 348 | remaining = ""; |
| 349 | } else if ((first_shared_library_open == std::string::npos) || |
| 350 | (first_shared_library_open > first_class_loader_separator)) { |
| 351 | // We found a class loader spec without shared libraries, for example: |
| 352 | // PCL[...];PCL[...]{...} |
| 353 | class_loader_spec = remaining.substr(0, first_class_loader_separator); |
| 354 | remaining = remaining.substr(first_class_loader_separator + 1, |
| 355 | remaining.size() - first_class_loader_separator - 1); |
| 356 | } else { |
| 357 | // The class loader spec contains shared libraries. Find the matching closing |
| 358 | // shared library marker for it. |
| 359 | |
Yi Kong | d5fe17e | 2019-10-01 16:18:47 -0700 | [diff] [blame] | 360 | size_t shared_library_close = |
Nicolas Geoffray | f378fff | 2018-11-19 12:52:26 +0000 | [diff] [blame] | 361 | FindMatchingSharedLibraryCloseMarker(remaining, first_shared_library_open); |
| 362 | if (shared_library_close == std::string::npos) { |
| 363 | LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec; |
| 364 | return nullptr; |
| 365 | } |
| 366 | class_loader_spec = remaining.substr(0, shared_library_close + 1); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 367 | |
Nicolas Geoffray | f378fff | 2018-11-19 12:52:26 +0000 | [diff] [blame] | 368 | // Compute the remaining string to analyze. |
| 369 | if (remaining.size() == shared_library_close + 1) { |
| 370 | remaining = ""; |
| 371 | } else if ((remaining.size() == shared_library_close + 2) || |
| 372 | (remaining.at(shared_library_close + 1) != kClassLoaderSeparator)) { |
| 373 | LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec; |
| 374 | return nullptr; |
| 375 | } else { |
| 376 | remaining = remaining.substr(shared_library_close + 2, |
| 377 | remaining.size() - shared_library_close - 2); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 378 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 379 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 380 | |
| 381 | std::unique_ptr<ClassLoaderInfo> info = |
| 382 | ParseClassLoaderSpec(class_loader_spec, parse_checksums); |
| 383 | if (info == nullptr) { |
| 384 | LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec; |
| 385 | return nullptr; |
| 386 | } |
| 387 | if (first == nullptr) { |
Andreas Gampe | 41c911f | 2018-11-19 11:34:16 -0800 | [diff] [blame] | 388 | first = std::move(info); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 389 | previous_iteration = first.get(); |
| 390 | } else { |
| 391 | CHECK(previous_iteration != nullptr); |
Andreas Gampe | 41c911f | 2018-11-19 11:34:16 -0800 | [diff] [blame] | 392 | previous_iteration->parent = std::move(info); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 393 | previous_iteration = previous_iteration->parent.get(); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 396 | return first.release(); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // Opens requested class path files and appends them to opened_dex_files. If the dex files have |
| 400 | // been stripped, this opens them from their oat files (which get added to opened_oat_files). |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 401 | bool ClassLoaderContext::OpenDexFiles(InstructionSet isa, |
| 402 | const std::string& classpath_dir, |
| 403 | const std::vector<int>& fds) { |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 404 | if (dex_files_open_attempted_) { |
| 405 | // Do not attempt to re-open the files if we already tried. |
| 406 | return dex_files_open_result_; |
| 407 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 408 | |
| 409 | dex_files_open_attempted_ = true; |
| 410 | // Assume we can open all dex files. If not, we will set this to false as we go. |
| 411 | dex_files_open_result_ = true; |
| 412 | |
| 413 | if (special_shared_library_) { |
| 414 | // Nothing to open if the context is a special shared library. |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | // Note that we try to open all dex files even if some fail. |
| 419 | // We may get resource-only apks which we cannot load. |
| 420 | // TODO(calin): Refine the dex opening interface to be able to tell if an archive contains |
| 421 | // no dex files. So that we can distinguish the real failures... |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 422 | const ArtDexFileLoader dex_file_loader; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 423 | std::vector<ClassLoaderInfo*> work_list; |
Nicolas Geoffray | 9893c47 | 2018-11-13 15:39:53 +0000 | [diff] [blame] | 424 | CHECK(class_loader_chain_ != nullptr); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 425 | work_list.push_back(class_loader_chain_.get()); |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 426 | size_t dex_file_index = 0; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 427 | while (!work_list.empty()) { |
| 428 | ClassLoaderInfo* info = work_list.back(); |
| 429 | work_list.pop_back(); |
David Brazdil | 93d339d | 2019-03-27 09:56:45 +0000 | [diff] [blame] | 430 | DCHECK(info->type != kInMemoryDexClassLoader) << __FUNCTION__ << " not supported for IMC"; |
| 431 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 432 | size_t opened_dex_files_index = info->opened_dex_files.size(); |
| 433 | for (const std::string& cp_elem : info->classpath) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 434 | // If path is relative, append it to the provided base directory. |
Calin Juravle | 92003fe | 2017-09-06 02:22:57 +0000 | [diff] [blame] | 435 | std::string location = cp_elem; |
| 436 | if (location[0] != '/' && !classpath_dir.empty()) { |
Nicolas Geoffray | 06ffecf | 2017-11-14 10:31:54 +0000 | [diff] [blame] | 437 | location = classpath_dir + (classpath_dir.back() == '/' ? "" : "/") + location; |
Calin Juravle | 821a259 | 2017-08-11 14:33:38 -0700 | [diff] [blame] | 438 | } |
| 439 | |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 440 | // If file descriptors were provided for the class loader context dex paths, |
| 441 | // get the descriptor which correponds to this dex path. We assume the `fds` |
| 442 | // vector follows the same order as a flattened class loader context. |
| 443 | int fd = -1; |
| 444 | if (!fds.empty()) { |
| 445 | if (dex_file_index >= fds.size()) { |
| 446 | LOG(WARNING) << "Number of FDs is smaller than number of dex files in the context"; |
| 447 | dex_files_open_result_ = false; |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | fd = fds[dex_file_index++]; |
| 452 | DCHECK_GE(fd, 0); |
| 453 | } |
| 454 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 455 | std::string error_msg; |
| 456 | // When opening the dex files from the context we expect their checksum to match their |
| 457 | // contents. So pass true to verify_checksum. |
Nicolas Geoffray | bf770550 | 2020-03-16 14:02:31 +0000 | [diff] [blame] | 458 | // We don't need to do structural dex file verification, we only need to |
| 459 | // check the checksum, so pass false to verify. |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 460 | if (fd < 0) { |
| 461 | if (!dex_file_loader.Open(location.c_str(), |
| 462 | location.c_str(), |
Nicolas Geoffray | bf770550 | 2020-03-16 14:02:31 +0000 | [diff] [blame] | 463 | /*verify=*/ false, |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 464 | /*verify_checksum=*/ true, |
| 465 | &error_msg, |
| 466 | &info->opened_dex_files)) { |
| 467 | // If we fail to open the dex file because it's been stripped, try to |
| 468 | // open the dex file from its corresponding oat file. |
| 469 | // This could happen when we need to recompile a pre-build whose dex |
| 470 | // code has been stripped (for example, if the pre-build is only |
| 471 | // quicken and we want to re-compile it speed-profile). |
| 472 | // TODO(calin): Use the vdex directly instead of going through the oat file. |
| 473 | OatFileAssistant oat_file_assistant(location.c_str(), isa, false); |
| 474 | std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile()); |
| 475 | std::vector<std::unique_ptr<const DexFile>> oat_dex_files; |
| 476 | if (oat_file != nullptr && |
| 477 | OatFileAssistant::LoadDexFiles(*oat_file, location, &oat_dex_files)) { |
| 478 | info->opened_oat_files.push_back(std::move(oat_file)); |
| 479 | info->opened_dex_files.insert(info->opened_dex_files.end(), |
| 480 | std::make_move_iterator(oat_dex_files.begin()), |
| 481 | std::make_move_iterator(oat_dex_files.end())); |
| 482 | } else { |
| 483 | LOG(WARNING) << "Could not open dex files from location: " << location; |
| 484 | dex_files_open_result_ = false; |
| 485 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 486 | } |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 487 | } else if (!dex_file_loader.Open(fd, |
| 488 | location.c_str(), |
Nicolas Geoffray | bf770550 | 2020-03-16 14:02:31 +0000 | [diff] [blame] | 489 | /*verify=*/ false, |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 490 | /*verify_checksum=*/ true, |
| 491 | &error_msg, |
| 492 | &info->opened_dex_files)) { |
| 493 | LOG(WARNING) << "Could not open dex files from fd " << fd << " for location: " << location; |
| 494 | dex_files_open_result_ = false; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 497 | |
| 498 | // We finished opening the dex files from the classpath. |
| 499 | // Now update the classpath and the checksum with the locations of the dex files. |
| 500 | // |
| 501 | // We do this because initially the classpath contains the paths of the dex files; and |
| 502 | // some of them might be multi-dexes. So in order to have a consistent view we replace all the |
| 503 | // file paths with the actual dex locations being loaded. |
| 504 | // This will allow the context to VerifyClassLoaderContextMatch which expects or multidex |
| 505 | // location in the class paths. |
| 506 | // Note that this will also remove the paths that could not be opened. |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 507 | info->original_classpath = std::move(info->classpath); |
| 508 | info->classpath.clear(); |
| 509 | info->checksums.clear(); |
| 510 | for (size_t k = opened_dex_files_index; k < info->opened_dex_files.size(); k++) { |
| 511 | std::unique_ptr<const DexFile>& dex = info->opened_dex_files[k]; |
| 512 | info->classpath.push_back(dex->GetLocation()); |
| 513 | info->checksums.push_back(dex->GetLocationChecksum()); |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 514 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 515 | AddToWorkList(info, work_list); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 516 | } |
| 517 | |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 518 | // Check that if file descriptors were provided, there were exactly as many |
| 519 | // as we have encountered while iterating over this class loader context. |
| 520 | if (dex_file_index != fds.size()) { |
| 521 | LOG(WARNING) << fds.size() << " FDs provided but only " << dex_file_index |
| 522 | << " dex files are in the class loader context"; |
| 523 | dex_files_open_result_ = false; |
| 524 | } |
| 525 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 526 | return dex_files_open_result_; |
| 527 | } |
| 528 | |
| 529 | bool ClassLoaderContext::RemoveLocationsFromClassPaths( |
| 530 | const dchecked_vector<std::string>& locations) { |
| 531 | CHECK(!dex_files_open_attempted_) |
| 532 | << "RemoveLocationsFromClasspaths cannot be call after OpenDexFiles"; |
| 533 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 534 | if (class_loader_chain_ == nullptr) { |
| 535 | return false; |
| 536 | } |
| 537 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 538 | std::set<std::string> canonical_locations; |
| 539 | for (const std::string& location : locations) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 540 | canonical_locations.insert(DexFileLoader::GetDexCanonicalLocation(location.c_str())); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 541 | } |
| 542 | bool removed_locations = false; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 543 | std::vector<ClassLoaderInfo*> work_list; |
| 544 | work_list.push_back(class_loader_chain_.get()); |
| 545 | while (!work_list.empty()) { |
| 546 | ClassLoaderInfo* info = work_list.back(); |
| 547 | work_list.pop_back(); |
| 548 | size_t initial_size = info->classpath.size(); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 549 | auto kept_it = std::remove_if( |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 550 | info->classpath.begin(), |
| 551 | info->classpath.end(), |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 552 | [canonical_locations](const std::string& location) { |
| 553 | return ContainsElement(canonical_locations, |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 554 | DexFileLoader::GetDexCanonicalLocation(location.c_str())); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 555 | }); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 556 | info->classpath.erase(kept_it, info->classpath.end()); |
| 557 | if (initial_size != info->classpath.size()) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 558 | removed_locations = true; |
| 559 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 560 | AddToWorkList(info, work_list); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 561 | } |
| 562 | return removed_locations; |
| 563 | } |
| 564 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 565 | std::string ClassLoaderContext::EncodeContextForDex2oat(const std::string& base_dir) const { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 566 | return EncodeContext(base_dir, /*for_dex2oat=*/ true, /*stored_context=*/ nullptr); |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 569 | std::string ClassLoaderContext::EncodeContextForOatFile(const std::string& base_dir, |
| 570 | ClassLoaderContext* stored_context) const { |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 571 | return EncodeContext(base_dir, /*for_dex2oat=*/ false, stored_context); |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 574 | std::map<std::string, std::string> |
| 575 | ClassLoaderContext::EncodeClassPathContexts(const std::string& base_dir) const { |
| 576 | CheckDexFilesOpened("EncodeClassPathContexts"); |
| 577 | if (class_loader_chain_ == nullptr) { |
| 578 | return std::map<std::string, std::string>{}; |
| 579 | } |
| 580 | |
| 581 | std::map<std::string, std::string> results; |
| 582 | std::vector<std::string> dex_locations; |
| 583 | std::vector<uint32_t> checksums; |
| 584 | dex_locations.reserve(class_loader_chain_->original_classpath.size()); |
| 585 | |
| 586 | std::ostringstream encoded_libs_and_parent_stream; |
| 587 | EncodeSharedLibAndParent(*class_loader_chain_, |
| 588 | base_dir, |
| 589 | /*for_dex2oat=*/true, |
| 590 | /*stored_info=*/nullptr, |
| 591 | encoded_libs_and_parent_stream); |
| 592 | std::string encoded_libs_and_parent(encoded_libs_and_parent_stream.str()); |
| 593 | |
| 594 | std::set<std::string> seen_locations; |
| 595 | for (const std::string& path : class_loader_chain_->classpath) { |
| 596 | // The classpath will contain multiple entries for multidex files, so make sure this is the |
| 597 | // first time we're seeing this file. |
| 598 | const std::string base_location(DexFileLoader::GetBaseLocation(path)); |
| 599 | if (!seen_locations.insert(base_location).second) { |
| 600 | continue; |
| 601 | } |
| 602 | |
| 603 | std::ostringstream out; |
| 604 | EncodeClassPath(base_dir, dex_locations, checksums, class_loader_chain_->type, out); |
| 605 | out << encoded_libs_and_parent; |
| 606 | results.emplace(base_location, out.str()); |
| 607 | |
| 608 | dex_locations.push_back(base_location); |
| 609 | } |
| 610 | |
| 611 | return results; |
| 612 | } |
| 613 | |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 614 | std::string ClassLoaderContext::EncodeContext(const std::string& base_dir, |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 615 | bool for_dex2oat, |
| 616 | ClassLoaderContext* stored_context) const { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 617 | CheckDexFilesOpened("EncodeContextForOatFile"); |
| 618 | if (special_shared_library_) { |
| 619 | return OatFile::kSpecialSharedLibrary; |
| 620 | } |
| 621 | |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 622 | if (stored_context != nullptr) { |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 623 | DCHECK_EQ(GetParentChainSize(), stored_context->GetParentChainSize()); |
Mathieu Chartier | c444077 | 2018-04-16 14:40:56 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 626 | std::ostringstream out; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 627 | if (class_loader_chain_ == nullptr) { |
Calin Juravle | 1a509c8 | 2017-07-24 16:51:21 -0700 | [diff] [blame] | 628 | // We can get in this situation if the context was created with a class path containing the |
| 629 | // source dex files which were later removed (happens during run-tests). |
| 630 | out << GetClassLoaderTypeName(kPathClassLoader) |
| 631 | << kClassLoaderOpeningMark |
| 632 | << kClassLoaderClosingMark; |
| 633 | return out.str(); |
| 634 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 635 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 636 | EncodeContextInternal( |
| 637 | *class_loader_chain_, |
| 638 | base_dir, |
| 639 | for_dex2oat, |
| 640 | (stored_context == nullptr ? nullptr : stored_context->class_loader_chain_.get()), |
| 641 | out); |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 642 | return out.str(); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Dan Zimmerman | 7d511d9 | 2019-12-23 07:00:51 -0800 | [diff] [blame] | 645 | void ClassLoaderContext::EncodeClassPath(const std::string& base_dir, |
| 646 | const std::vector<std::string>& dex_locations, |
| 647 | const std::vector<uint32_t>& checksums, |
| 648 | ClassLoaderType type, |
| 649 | std::ostringstream& out) const { |
| 650 | CHECK(checksums.empty() || dex_locations.size() == checksums.size()); |
| 651 | out << GetClassLoaderTypeName(type); |
| 652 | out << kClassLoaderOpeningMark; |
| 653 | const size_t len = dex_locations.size(); |
| 654 | for (size_t k = 0; k < len; k++) { |
| 655 | std::string location = dex_locations[k]; |
| 656 | if (k > 0) { |
| 657 | out << kClasspathSeparator; |
| 658 | } |
| 659 | if (type == kInMemoryDexClassLoader) { |
| 660 | out << kInMemoryDexClassLoaderDexLocationMagic; |
| 661 | } else if (!base_dir.empty() |
| 662 | && location.substr(0, base_dir.length()) == base_dir) { |
| 663 | // Find paths that were relative and convert them back from absolute. |
| 664 | out << location.substr(base_dir.length() + 1).c_str(); |
| 665 | } else { |
| 666 | out << location.c_str(); |
| 667 | } |
| 668 | if (!checksums.empty()) { |
| 669 | out << kDexFileChecksumSeparator; |
| 670 | out << checksums[k]; |
| 671 | } |
| 672 | } |
| 673 | out << kClassLoaderClosingMark; |
| 674 | } |
| 675 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 676 | void ClassLoaderContext::EncodeContextInternal(const ClassLoaderInfo& info, |
| 677 | const std::string& base_dir, |
| 678 | bool for_dex2oat, |
| 679 | ClassLoaderInfo* stored_info, |
| 680 | std::ostringstream& out) const { |
Dan Zimmerman | 7d511d9 | 2019-12-23 07:00:51 -0800 | [diff] [blame] | 681 | std::vector<std::string> locations; |
| 682 | std::vector<uint32_t> checksums; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 683 | std::set<std::string> seen_locations; |
| 684 | SafeMap<std::string, std::string> remap; |
| 685 | if (stored_info != nullptr) { |
| 686 | for (size_t k = 0; k < info.original_classpath.size(); ++k) { |
| 687 | // Note that we don't care if the same name appears twice. |
| 688 | remap.Put(info.original_classpath[k], stored_info->classpath[k]); |
| 689 | } |
| 690 | } |
Dan Zimmerman | 7d511d9 | 2019-12-23 07:00:51 -0800 | [diff] [blame] | 691 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 692 | for (size_t k = 0; k < info.opened_dex_files.size(); k++) { |
| 693 | const std::unique_ptr<const DexFile>& dex_file = info.opened_dex_files[k]; |
| 694 | if (for_dex2oat) { |
| 695 | // dex2oat only needs the base location. It cannot accept multidex locations. |
| 696 | // So ensure we only add each file once. |
| 697 | bool new_insert = seen_locations.insert( |
| 698 | DexFileLoader::GetBaseLocation(dex_file->GetLocation())).second; |
| 699 | if (!new_insert) { |
| 700 | continue; |
| 701 | } |
| 702 | } |
Dan Zimmerman | 7d511d9 | 2019-12-23 07:00:51 -0800 | [diff] [blame] | 703 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 704 | std::string location = dex_file->GetLocation(); |
| 705 | // If there is a stored class loader remap, fix up the multidex strings. |
| 706 | if (!remap.empty()) { |
| 707 | std::string base_dex_location = DexFileLoader::GetBaseLocation(location); |
| 708 | auto it = remap.find(base_dex_location); |
| 709 | CHECK(it != remap.end()) << base_dex_location; |
| 710 | location = it->second + DexFileLoader::GetMultiDexSuffix(location); |
| 711 | } |
Dan Zimmerman | 7d511d9 | 2019-12-23 07:00:51 -0800 | [diff] [blame] | 712 | locations.emplace_back(std::move(location)); |
| 713 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 714 | // dex2oat does not need the checksums. |
| 715 | if (!for_dex2oat) { |
Dan Zimmerman | 7d511d9 | 2019-12-23 07:00:51 -0800 | [diff] [blame] | 716 | checksums.push_back(dex_file->GetLocationChecksum()); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
Dan Zimmerman | 7d511d9 | 2019-12-23 07:00:51 -0800 | [diff] [blame] | 719 | EncodeClassPath(base_dir, locations, checksums, info.type, out); |
| 720 | EncodeSharedLibAndParent(info, base_dir, for_dex2oat, stored_info, out); |
| 721 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 722 | |
Dan Zimmerman | 7d511d9 | 2019-12-23 07:00:51 -0800 | [diff] [blame] | 723 | void ClassLoaderContext::EncodeSharedLibAndParent(const ClassLoaderInfo& info, |
| 724 | const std::string& base_dir, |
| 725 | bool for_dex2oat, |
| 726 | ClassLoaderInfo* stored_info, |
| 727 | std::ostringstream& out) const { |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 728 | if (!info.shared_libraries.empty()) { |
| 729 | out << kClassLoaderSharedLibraryOpeningMark; |
| 730 | for (uint32_t i = 0; i < info.shared_libraries.size(); ++i) { |
| 731 | if (i > 0) { |
| 732 | out << kClassLoaderSharedLibrarySeparator; |
| 733 | } |
| 734 | EncodeContextInternal( |
| 735 | *info.shared_libraries[i].get(), |
| 736 | base_dir, |
| 737 | for_dex2oat, |
| 738 | (stored_info == nullptr ? nullptr : stored_info->shared_libraries[i].get()), |
| 739 | out); |
| 740 | } |
| 741 | out << kClassLoaderSharedLibraryClosingMark; |
| 742 | } |
| 743 | if (info.parent != nullptr) { |
| 744 | out << kClassLoaderSeparator; |
| 745 | EncodeContextInternal( |
| 746 | *info.parent.get(), |
| 747 | base_dir, |
| 748 | for_dex2oat, |
| 749 | (stored_info == nullptr ? nullptr : stored_info->parent.get()), |
| 750 | out); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | // Returns the WellKnownClass for the given class loader type. |
| 755 | static jclass GetClassLoaderClass(ClassLoaderContext::ClassLoaderType type) { |
| 756 | switch (type) { |
| 757 | case ClassLoaderContext::kPathClassLoader: |
| 758 | return WellKnownClasses::dalvik_system_PathClassLoader; |
| 759 | case ClassLoaderContext::kDelegateLastClassLoader: |
| 760 | return WellKnownClasses::dalvik_system_DelegateLastClassLoader; |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 761 | case ClassLoaderContext::kInMemoryDexClassLoader: |
| 762 | return WellKnownClasses::dalvik_system_InMemoryDexClassLoader; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 763 | case ClassLoaderContext::kInvalidClassLoader: break; // will fail after the switch. |
| 764 | } |
| 765 | LOG(FATAL) << "Invalid class loader type " << type; |
| 766 | UNREACHABLE(); |
| 767 | } |
| 768 | |
Nicolas Geoffray | cb2e1dd | 2018-11-20 11:15:13 +0000 | [diff] [blame] | 769 | static std::string FlattenClasspath(const std::vector<std::string>& classpath) { |
| 770 | return android::base::Join(classpath, ':'); |
| 771 | } |
| 772 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 773 | static ObjPtr<mirror::ClassLoader> CreateClassLoaderInternal( |
| 774 | Thread* self, |
| 775 | ScopedObjectAccess& soa, |
| 776 | const ClassLoaderContext::ClassLoaderInfo& info, |
Nicolas Geoffray | cb2e1dd | 2018-11-20 11:15:13 +0000 | [diff] [blame] | 777 | bool for_shared_library, |
| 778 | VariableSizedHandleScope& map_scope, |
| 779 | std::map<std::string, Handle<mirror::ClassLoader>>& canonicalized_libraries, |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 780 | bool add_compilation_sources, |
| 781 | const std::vector<const DexFile*>& compilation_sources) |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 782 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | cb2e1dd | 2018-11-20 11:15:13 +0000 | [diff] [blame] | 783 | if (for_shared_library) { |
| 784 | // Check if the shared library has already been created. |
| 785 | auto search = canonicalized_libraries.find(FlattenClasspath(info.classpath)); |
| 786 | if (search != canonicalized_libraries.end()) { |
| 787 | return search->second.Get(); |
| 788 | } |
| 789 | } |
| 790 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 791 | StackHandleScope<3> hs(self); |
| 792 | MutableHandle<mirror::ObjectArray<mirror::ClassLoader>> libraries( |
| 793 | hs.NewHandle<mirror::ObjectArray<mirror::ClassLoader>>(nullptr)); |
| 794 | |
| 795 | if (!info.shared_libraries.empty()) { |
| 796 | libraries.Assign(mirror::ObjectArray<mirror::ClassLoader>::Alloc( |
| 797 | self, |
| 798 | GetClassRoot<mirror::ObjectArray<mirror::ClassLoader>>(), |
| 799 | info.shared_libraries.size())); |
| 800 | for (uint32_t i = 0; i < info.shared_libraries.size(); ++i) { |
| 801 | // We should only add the compilation sources to the first class loader. |
| 802 | libraries->Set(i, |
| 803 | CreateClassLoaderInternal( |
| 804 | self, |
| 805 | soa, |
| 806 | *info.shared_libraries[i].get(), |
Nicolas Geoffray | cb2e1dd | 2018-11-20 11:15:13 +0000 | [diff] [blame] | 807 | /* for_shared_library= */ true, |
| 808 | map_scope, |
| 809 | canonicalized_libraries, |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 810 | /* add_compilation_sources= */ false, |
| 811 | compilation_sources)); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | MutableHandle<mirror::ClassLoader> parent = hs.NewHandle<mirror::ClassLoader>(nullptr); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 816 | if (info.parent != nullptr) { |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 817 | // We should only add the compilation sources to the first class loader. |
| 818 | parent.Assign(CreateClassLoaderInternal( |
Nicolas Geoffray | cb2e1dd | 2018-11-20 11:15:13 +0000 | [diff] [blame] | 819 | self, |
| 820 | soa, |
| 821 | *info.parent.get(), |
| 822 | /* for_shared_library= */ false, |
| 823 | map_scope, |
| 824 | canonicalized_libraries, |
| 825 | /* add_compilation_sources= */ false, |
| 826 | compilation_sources)); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 827 | } |
| 828 | std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector( |
| 829 | info.opened_dex_files); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 830 | if (add_compilation_sources) { |
| 831 | // For the first class loader, its classpath comes first, followed by compilation sources. |
| 832 | // This ensures that whenever we need to resolve classes from it the classpath elements |
| 833 | // come first. |
| 834 | class_path_files.insert(class_path_files.end(), |
| 835 | compilation_sources.begin(), |
| 836 | compilation_sources.end()); |
| 837 | } |
| 838 | Handle<mirror::Class> loader_class = hs.NewHandle<mirror::Class>( |
| 839 | soa.Decode<mirror::Class>(GetClassLoaderClass(info.type))); |
Nicolas Geoffray | cb2e1dd | 2018-11-20 11:15:13 +0000 | [diff] [blame] | 840 | ObjPtr<mirror::ClassLoader> loader = |
| 841 | Runtime::Current()->GetClassLinker()->CreateWellKnownClassLoader( |
| 842 | self, |
| 843 | class_path_files, |
| 844 | loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 845 | parent, |
| 846 | libraries); |
Nicolas Geoffray | cb2e1dd | 2018-11-20 11:15:13 +0000 | [diff] [blame] | 847 | if (for_shared_library) { |
| 848 | canonicalized_libraries[FlattenClasspath(info.classpath)] = |
| 849 | map_scope.NewHandle<mirror::ClassLoader>(loader); |
| 850 | } |
| 851 | return loader; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 854 | jobject ClassLoaderContext::CreateClassLoader( |
| 855 | const std::vector<const DexFile*>& compilation_sources) const { |
| 856 | CheckDexFilesOpened("CreateClassLoader"); |
| 857 | |
| 858 | Thread* self = Thread::Current(); |
| 859 | ScopedObjectAccess soa(self); |
| 860 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 861 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 862 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 863 | if (class_loader_chain_ == nullptr) { |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 864 | CHECK(special_shared_library_); |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 865 | return class_linker->CreatePathClassLoader(self, compilation_sources); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Nicolas Geoffray | cb2e1dd | 2018-11-20 11:15:13 +0000 | [diff] [blame] | 868 | // Create a map of canonicalized shared libraries. As we're holding objects, |
| 869 | // we're creating a variable size handle scope to put handles in the map. |
| 870 | VariableSizedHandleScope map_scope(self); |
| 871 | std::map<std::string, Handle<mirror::ClassLoader>> canonicalized_libraries; |
| 872 | |
| 873 | // Create the class loader. |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 874 | ObjPtr<mirror::ClassLoader> loader = |
| 875 | CreateClassLoaderInternal(self, |
| 876 | soa, |
| 877 | *class_loader_chain_.get(), |
Nicolas Geoffray | cb2e1dd | 2018-11-20 11:15:13 +0000 | [diff] [blame] | 878 | /* for_shared_library= */ false, |
| 879 | map_scope, |
| 880 | canonicalized_libraries, |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 881 | /* add_compilation_sources= */ true, |
| 882 | compilation_sources); |
| 883 | // Make it a global ref and return. |
| 884 | ScopedLocalRef<jobject> local_ref( |
| 885 | soa.Env(), soa.Env()->AddLocalReference<jobject>(loader)); |
| 886 | return soa.Env()->NewGlobalRef(local_ref.get()); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | std::vector<const DexFile*> ClassLoaderContext::FlattenOpenedDexFiles() const { |
| 890 | CheckDexFilesOpened("FlattenOpenedDexFiles"); |
| 891 | |
| 892 | std::vector<const DexFile*> result; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 893 | if (class_loader_chain_ == nullptr) { |
| 894 | return result; |
| 895 | } |
| 896 | std::vector<ClassLoaderInfo*> work_list; |
| 897 | work_list.push_back(class_loader_chain_.get()); |
| 898 | while (!work_list.empty()) { |
| 899 | ClassLoaderInfo* info = work_list.back(); |
| 900 | work_list.pop_back(); |
| 901 | for (const std::unique_ptr<const DexFile>& dex_file : info->opened_dex_files) { |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 902 | result.push_back(dex_file.get()); |
| 903 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 904 | AddToWorkList(info, work_list); |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 905 | } |
| 906 | return result; |
| 907 | } |
| 908 | |
David Brazdil | 8982186 | 2019-03-19 13:57:43 +0000 | [diff] [blame] | 909 | std::string ClassLoaderContext::FlattenDexPaths() const { |
| 910 | if (class_loader_chain_ == nullptr) { |
| 911 | return ""; |
| 912 | } |
| 913 | |
| 914 | std::vector<std::string> result; |
| 915 | std::vector<ClassLoaderInfo*> work_list; |
| 916 | work_list.push_back(class_loader_chain_.get()); |
| 917 | while (!work_list.empty()) { |
| 918 | ClassLoaderInfo* info = work_list.back(); |
| 919 | work_list.pop_back(); |
| 920 | for (const std::string& dex_path : info->classpath) { |
| 921 | result.push_back(dex_path); |
| 922 | } |
| 923 | AddToWorkList(info, work_list); |
| 924 | } |
| 925 | return FlattenClasspath(result); |
| 926 | } |
| 927 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 928 | const char* ClassLoaderContext::GetClassLoaderTypeName(ClassLoaderType type) { |
| 929 | switch (type) { |
| 930 | case kPathClassLoader: return kPathClassLoaderString; |
| 931 | case kDelegateLastClassLoader: return kDelegateLastClassLoaderString; |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 932 | case kInMemoryDexClassLoader: return kInMemoryDexClassLoaderString; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 933 | default: |
| 934 | LOG(FATAL) << "Invalid class loader type " << type; |
| 935 | UNREACHABLE(); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | void ClassLoaderContext::CheckDexFilesOpened(const std::string& calling_method) const { |
| 940 | CHECK(dex_files_open_attempted_) |
| 941 | << "Dex files were not successfully opened before the call to " << calling_method |
| 942 | << "attempt=" << dex_files_open_attempted_ << ", result=" << dex_files_open_result_; |
| 943 | } |
Calin Juravle | 7b0648a | 2017-07-07 18:40:50 -0700 | [diff] [blame] | 944 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 945 | // Collects the dex files from the give Java dex_file object. Only the dex files with |
| 946 | // at least 1 class are collected. If a null java_dex_file is passed this method does nothing. |
| 947 | static bool CollectDexFilesFromJavaDexFile(ObjPtr<mirror::Object> java_dex_file, |
| 948 | ArtField* const cookie_field, |
| 949 | std::vector<const DexFile*>* out_dex_files) |
| 950 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 951 | if (java_dex_file == nullptr) { |
| 952 | return true; |
| 953 | } |
| 954 | // On the Java side, the dex files are stored in the cookie field. |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 955 | ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(java_dex_file)->AsLongArray(); |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 956 | if (long_array == nullptr) { |
| 957 | // This should never happen so log a warning. |
| 958 | LOG(ERROR) << "Unexpected null cookie"; |
| 959 | return false; |
| 960 | } |
| 961 | int32_t long_array_size = long_array->GetLength(); |
| 962 | // Index 0 from the long array stores the oat file. The dex files start at index 1. |
| 963 | for (int32_t j = 1; j < long_array_size; ++j) { |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 964 | const DexFile* cp_dex_file = |
| 965 | reinterpret_cast64<const DexFile*>(long_array->GetWithoutChecks(j)); |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 966 | if (cp_dex_file != nullptr && cp_dex_file->NumClassDefs() > 0) { |
| 967 | // TODO(calin): It's unclear why the dex files with no classes are skipped here and when |
| 968 | // cp_dex_file can be null. |
| 969 | out_dex_files->push_back(cp_dex_file); |
| 970 | } |
| 971 | } |
| 972 | return true; |
| 973 | } |
| 974 | |
| 975 | // Collects all the dex files loaded by the given class loader. |
| 976 | // Returns true for success or false if an unexpected state is discovered (e.g. a null dex cookie, |
| 977 | // a null list of dex elements or a null dex element). |
| 978 | static bool CollectDexFilesFromSupportedClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 979 | Handle<mirror::ClassLoader> class_loader, |
| 980 | std::vector<const DexFile*>* out_dex_files) |
| 981 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 982 | CHECK(IsInstanceOfBaseDexClassLoader(soa, class_loader)); |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 983 | |
| 984 | // All supported class loaders inherit from BaseDexClassLoader. |
| 985 | // We need to get the DexPathList and loop through it. |
| 986 | ArtField* const cookie_field = |
| 987 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 988 | ArtField* const dex_file_field = |
| 989 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
| 990 | ObjPtr<mirror::Object> dex_path_list = |
| 991 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)-> |
| 992 | GetObject(class_loader.Get()); |
| 993 | CHECK(cookie_field != nullptr); |
| 994 | CHECK(dex_file_field != nullptr); |
| 995 | if (dex_path_list == nullptr) { |
| 996 | // This may be null if the current class loader is under construction and it does not |
| 997 | // have its fields setup yet. |
| 998 | return true; |
| 999 | } |
| 1000 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 1001 | ObjPtr<mirror::Object> dex_elements_obj = |
| 1002 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 1003 | GetObject(dex_path_list); |
| 1004 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 1005 | // at the mCookie which is a DexFile vector. |
| 1006 | if (dex_elements_obj == nullptr) { |
| 1007 | // TODO(calin): It's unclear if we should just assert here. For now be prepared for the worse |
| 1008 | // and assume we have no elements. |
| 1009 | return true; |
| 1010 | } else { |
| 1011 | StackHandleScope<1> hs(soa.Self()); |
| 1012 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements( |
| 1013 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>())); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1014 | for (auto element : dex_elements.Iterate<mirror::Object>()) { |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1015 | if (element == nullptr) { |
| 1016 | // Should never happen, log an error and break. |
| 1017 | // TODO(calin): It's unclear if we should just assert here. |
| 1018 | // This code was propagated to oat_file_manager from the class linker where it would |
| 1019 | // throw a NPE. For now, return false which will mark this class loader as unsupported. |
| 1020 | LOG(ERROR) << "Unexpected null in the dex element list"; |
| 1021 | return false; |
| 1022 | } |
| 1023 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
| 1024 | if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) { |
| 1025 | return false; |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | return true; |
| 1031 | } |
| 1032 | |
| 1033 | static bool GetDexFilesFromDexElementsArray( |
| 1034 | ScopedObjectAccessAlreadyRunnable& soa, |
| 1035 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements, |
| 1036 | std::vector<const DexFile*>* out_dex_files) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1037 | DCHECK(dex_elements != nullptr); |
| 1038 | |
| 1039 | ArtField* const cookie_field = |
| 1040 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 1041 | ArtField* const dex_file_field = |
| 1042 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 1043 | const ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>( |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1044 | WellKnownClasses::dalvik_system_DexPathList__Element); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 1045 | const ObjPtr<mirror::Class> dexfile_class = soa.Decode<mirror::Class>( |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1046 | WellKnownClasses::dalvik_system_DexFile); |
| 1047 | |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1048 | for (auto element : dex_elements.Iterate<mirror::Object>()) { |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1049 | // We can hit a null element here because this is invoked with a partially filled dex_elements |
| 1050 | // array from DexPathList. DexPathList will open each dex sequentially, each time passing the |
| 1051 | // list of dex files which were opened before. |
| 1052 | if (element == nullptr) { |
| 1053 | continue; |
| 1054 | } |
| 1055 | |
| 1056 | // We support this being dalvik.system.DexPathList$Element and dalvik.system.DexFile. |
| 1057 | // TODO(calin): Code caried over oat_file_manager: supporting both classes seem to be |
| 1058 | // a historical glitch. All the java code opens dex files using an array of Elements. |
| 1059 | ObjPtr<mirror::Object> dex_file; |
| 1060 | if (element_class == element->GetClass()) { |
| 1061 | dex_file = dex_file_field->GetObject(element); |
| 1062 | } else if (dexfile_class == element->GetClass()) { |
| 1063 | dex_file = element; |
| 1064 | } else { |
| 1065 | LOG(ERROR) << "Unsupported element in dex_elements: " |
| 1066 | << mirror::Class::PrettyClass(element->GetClass()); |
| 1067 | return false; |
| 1068 | } |
| 1069 | |
| 1070 | if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) { |
| 1071 | return false; |
| 1072 | } |
| 1073 | } |
| 1074 | return true; |
| 1075 | } |
| 1076 | |
| 1077 | // Adds the `class_loader` info to the `context`. |
| 1078 | // The dex file present in `dex_elements` array (if not null) will be added at the end of |
| 1079 | // the classpath. |
| 1080 | // This method is recursive (w.r.t. the class loader parent) and will stop once it reaches the |
| 1081 | // BootClassLoader. Note that the class loader chain is expected to be short. |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 1082 | bool ClassLoaderContext::CreateInfoFromClassLoader( |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1083 | ScopedObjectAccessAlreadyRunnable& soa, |
| 1084 | Handle<mirror::ClassLoader> class_loader, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 1085 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements, |
| 1086 | ClassLoaderInfo* child_info, |
| 1087 | bool is_shared_library) |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1088 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1089 | if (ClassLinker::IsBootClassLoader(soa, class_loader.Get())) { |
| 1090 | // Nothing to do for the boot class loader as we don't add its dex files to the context. |
| 1091 | return true; |
| 1092 | } |
| 1093 | |
| 1094 | ClassLoaderContext::ClassLoaderType type; |
| 1095 | if (IsPathOrDexClassLoader(soa, class_loader)) { |
| 1096 | type = kPathClassLoader; |
| 1097 | } else if (IsDelegateLastClassLoader(soa, class_loader)) { |
| 1098 | type = kDelegateLastClassLoader; |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 1099 | } else if (IsInMemoryDexClassLoader(soa, class_loader)) { |
| 1100 | type = kInMemoryDexClassLoader; |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1101 | } else { |
| 1102 | LOG(WARNING) << "Unsupported class loader"; |
| 1103 | return false; |
| 1104 | } |
| 1105 | |
| 1106 | // Inspect the class loader for its dex files. |
| 1107 | std::vector<const DexFile*> dex_files_loaded; |
| 1108 | CollectDexFilesFromSupportedClassLoader(soa, class_loader, &dex_files_loaded); |
| 1109 | |
| 1110 | // If we have a dex_elements array extract its dex elements now. |
| 1111 | // This is used in two situations: |
| 1112 | // 1) when a new ClassLoader is created DexPathList will open each dex file sequentially |
| 1113 | // passing the list of already open dex files each time. This ensures that we see the |
| 1114 | // correct context even if the ClassLoader under construction is not fully build. |
| 1115 | // 2) when apk splits are loaded on the fly, the framework will load their dex files by |
| 1116 | // appending them to the current class loader. When the new code paths are loaded in |
| 1117 | // BaseDexClassLoader, the paths already present in the class loader will be passed |
| 1118 | // in the dex_elements array. |
| 1119 | if (dex_elements != nullptr) { |
| 1120 | GetDexFilesFromDexElementsArray(soa, dex_elements, &dex_files_loaded); |
| 1121 | } |
| 1122 | |
Nicolas Geoffray | 1717a49 | 2018-11-30 01:02:50 +0000 | [diff] [blame] | 1123 | ClassLoaderInfo* info = new ClassLoaderContext::ClassLoaderInfo(type); |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 1124 | // Attach the `ClassLoaderInfo` now, before populating dex files, as only the |
| 1125 | // `ClassLoaderContext` knows whether these dex files should be deleted or not. |
| 1126 | if (child_info == nullptr) { |
Nicolas Geoffray | 1717a49 | 2018-11-30 01:02:50 +0000 | [diff] [blame] | 1127 | class_loader_chain_.reset(info); |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 1128 | } else if (is_shared_library) { |
| 1129 | child_info->shared_libraries.push_back(std::unique_ptr<ClassLoaderInfo>(info)); |
Nicolas Geoffray | 1717a49 | 2018-11-30 01:02:50 +0000 | [diff] [blame] | 1130 | } else { |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 1131 | child_info->parent.reset(info); |
Nicolas Geoffray | 1717a49 | 2018-11-30 01:02:50 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 1134 | // Now that `info` is in the chain, populate dex files. |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1135 | for (const DexFile* dex_file : dex_files_loaded) { |
David Brazdil | 93d339d | 2019-03-27 09:56:45 +0000 | [diff] [blame] | 1136 | // Dex location of dex files loaded with InMemoryDexClassLoader is always bogus. |
| 1137 | // Use a magic value for the classpath instead. |
| 1138 | info->classpath.push_back((type == kInMemoryDexClassLoader) |
| 1139 | ? kInMemoryDexClassLoaderDexLocationMagic |
| 1140 | : dex_file->GetLocation()); |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1141 | info->checksums.push_back(dex_file->GetLocationChecksum()); |
| 1142 | info->opened_dex_files.emplace_back(dex_file); |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1145 | // Note that dex_elements array is null here. The elements are considered to be part of the |
| 1146 | // current class loader and are not passed to the parents. |
| 1147 | ScopedNullHandle<mirror::ObjectArray<mirror::Object>> null_dex_elements; |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 1148 | |
| 1149 | // Add the shared libraries. |
| 1150 | StackHandleScope<3> hs(Thread::Current()); |
| 1151 | ArtField* field = |
| 1152 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
| 1153 | ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get()); |
| 1154 | if (raw_shared_libraries != nullptr) { |
| 1155 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries = |
| 1156 | hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>()); |
| 1157 | MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1158 | for (auto library : shared_libraries.Iterate<mirror::ClassLoader>()) { |
| 1159 | temp_loader.Assign(library); |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 1160 | if (!CreateInfoFromClassLoader( |
| 1161 | soa, temp_loader, null_dex_elements, info, /*is_shared_library=*/ true)) { |
| 1162 | return false; |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | // We created the ClassLoaderInfo for the current loader. Move on to its parent. |
| 1168 | Handle<mirror::ClassLoader> parent = hs.NewHandle(class_loader->GetParent()); |
| 1169 | if (!CreateInfoFromClassLoader( |
| 1170 | soa, parent, null_dex_elements, info, /*is_shared_library=*/ false)) { |
| 1171 | return false; |
| 1172 | } |
| 1173 | return true; |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | std::unique_ptr<ClassLoaderContext> ClassLoaderContext::CreateContextForClassLoader( |
| 1177 | jobject class_loader, |
| 1178 | jobjectArray dex_elements) { |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1179 | CHECK(class_loader != nullptr); |
| 1180 | |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1181 | ScopedObjectAccess soa(Thread::Current()); |
| 1182 | StackHandleScope<2> hs(soa.Self()); |
| 1183 | Handle<mirror::ClassLoader> h_class_loader = |
| 1184 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)); |
| 1185 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements = |
| 1186 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements)); |
Nicolas Geoffray | 1717a49 | 2018-11-30 01:02:50 +0000 | [diff] [blame] | 1187 | std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext(/*owns_the_dex_files=*/ false)); |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 1188 | if (!result->CreateInfoFromClassLoader( |
| 1189 | soa, h_class_loader, h_dex_elements, nullptr, /*is_shared_library=*/ false)) { |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1190 | return nullptr; |
| 1191 | } |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 1192 | return result; |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 1195 | std::map<std::string, std::string> |
| 1196 | ClassLoaderContext::EncodeClassPathContextsForClassLoader(jobject class_loader) { |
| 1197 | std::unique_ptr<ClassLoaderContext> clc = |
| 1198 | ClassLoaderContext::CreateContextForClassLoader(class_loader, nullptr); |
| 1199 | if (clc != nullptr) { |
| 1200 | return clc->EncodeClassPathContexts(""); |
| 1201 | } |
| 1202 | |
| 1203 | ScopedObjectAccess soa(Thread::Current()); |
| 1204 | StackHandleScope<1> hs(soa.Self()); |
| 1205 | Handle<mirror::ClassLoader> h_class_loader = |
| 1206 | hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader)); |
| 1207 | if (!IsInstanceOfBaseDexClassLoader(soa, h_class_loader)) { |
| 1208 | return std::map<std::string, std::string>{}; |
| 1209 | } |
| 1210 | |
| 1211 | std::vector<const DexFile*> dex_files_loaded; |
| 1212 | CollectDexFilesFromSupportedClassLoader(soa, h_class_loader, &dex_files_loaded); |
| 1213 | |
| 1214 | std::map<std::string, std::string> results; |
| 1215 | for (const DexFile* dex_file : dex_files_loaded) { |
| 1216 | results.emplace(DexFileLoader::GetBaseLocation(dex_file->GetLocation()), |
| 1217 | ClassLoaderContext::kUnsupportedClassLoaderContextEncoding); |
| 1218 | } |
| 1219 | return results; |
| 1220 | } |
| 1221 | |
Dan Zimmerman | c9fa770 | 2020-01-31 13:35:12 -0800 | [diff] [blame] | 1222 | bool ClassLoaderContext::IsValidEncoding(const std::string& possible_encoded_class_loader_context) { |
| 1223 | return ClassLoaderContext::Create(possible_encoded_class_loader_context.c_str()) != nullptr |
| 1224 | || possible_encoded_class_loader_context == kUnsupportedClassLoaderContextEncoding; |
| 1225 | } |
| 1226 | |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 1227 | ClassLoaderContext::VerificationResult ClassLoaderContext::VerifyClassLoaderContextMatch( |
| 1228 | const std::string& context_spec, |
| 1229 | bool verify_names, |
| 1230 | bool verify_checksums) const { |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1231 | if (verify_names || verify_checksums) { |
| 1232 | DCHECK(dex_files_open_attempted_); |
| 1233 | DCHECK(dex_files_open_result_); |
| 1234 | } |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 1235 | |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1236 | ClassLoaderContext expected_context; |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1237 | if (!expected_context.Parse(context_spec, verify_checksums)) { |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1238 | LOG(WARNING) << "Invalid class loader context: " << context_spec; |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 1239 | return VerificationResult::kMismatch; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Calin Juravle | c5b215f | 2017-09-12 14:49:37 -0700 | [diff] [blame] | 1242 | // Special shared library contexts always match. They essentially instruct the runtime |
| 1243 | // to ignore the class path check because the oat file is known to be loaded in different |
| 1244 | // contexts. OatFileManager will further verify if the oat file can be loaded based on the |
| 1245 | // collision check. |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 1246 | if (expected_context.special_shared_library_) { |
| 1247 | // Special case where we are the only entry in the class path. |
Nicolas Geoffray | 9893c47 | 2018-11-13 15:39:53 +0000 | [diff] [blame] | 1248 | if (class_loader_chain_ != nullptr && |
| 1249 | class_loader_chain_->parent == nullptr && |
| 1250 | class_loader_chain_->classpath.size() == 0) { |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 1251 | return VerificationResult::kVerifies; |
| 1252 | } |
| 1253 | return VerificationResult::kForcedToSkipChecks; |
| 1254 | } else if (special_shared_library_) { |
| 1255 | return VerificationResult::kForcedToSkipChecks; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1258 | ClassLoaderInfo* info = class_loader_chain_.get(); |
| 1259 | ClassLoaderInfo* expected = expected_context.class_loader_chain_.get(); |
| 1260 | CHECK(info != nullptr); |
| 1261 | CHECK(expected != nullptr); |
| 1262 | if (!ClassLoaderInfoMatch(*info, *expected, context_spec, verify_names, verify_checksums)) { |
Mathieu Chartier | adc9086 | 2018-05-11 13:03:06 -0700 | [diff] [blame] | 1263 | return VerificationResult::kMismatch; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1264 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1265 | return VerificationResult::kVerifies; |
| 1266 | } |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1267 | |
Vladimir Marko | 36ec598 | 2019-11-28 10:55:16 +0000 | [diff] [blame] | 1268 | // Returns true if absolute `path` ends with relative `suffix` starting at |
| 1269 | // a directory name boundary, i.e. after a '/'. For example, "foo/bar" |
| 1270 | // is a valid suffix of "/data/foo/bar" but not "/data-foo/bar". |
| 1271 | static inline bool AbsolutePathHasRelativeSuffix(const std::string& path, |
| 1272 | const std::string& suffix) { |
| 1273 | DCHECK(IsAbsoluteLocation(path)); |
| 1274 | DCHECK(!IsAbsoluteLocation(suffix)); |
| 1275 | return (path.size() > suffix.size()) && |
| 1276 | (path[path.size() - suffix.size() - 1u] == '/') && |
| 1277 | (std::string_view(path).substr(/*pos*/ path.size() - suffix.size()) == suffix); |
| 1278 | } |
| 1279 | |
Calin Juravle | b495e7f | 2020-04-06 19:29:45 -0700 | [diff] [blame] | 1280 | // Returns true if the given dex names are mathing, false otherwise. |
| 1281 | static bool AreDexNameMatching(const std::string& actual_dex_name, |
| 1282 | const std::string& expected_dex_name) { |
| 1283 | // Compute the dex location that must be compared. |
| 1284 | // We shouldn't do a naive comparison `actual_dex_name == expected_dex_name` |
| 1285 | // because even if they refer to the same file, one could be encoded as a relative location |
| 1286 | // and the other as an absolute one. |
| 1287 | bool is_dex_name_absolute = IsAbsoluteLocation(actual_dex_name); |
| 1288 | bool is_expected_dex_name_absolute = IsAbsoluteLocation(expected_dex_name); |
| 1289 | bool dex_names_match = false; |
| 1290 | |
| 1291 | if (is_dex_name_absolute == is_expected_dex_name_absolute) { |
| 1292 | // If both locations are absolute or relative then compare them as they are. |
| 1293 | // This is usually the case for: shared libraries and secondary dex files. |
| 1294 | dex_names_match = (actual_dex_name == expected_dex_name); |
| 1295 | } else if (is_dex_name_absolute) { |
| 1296 | // The runtime name is absolute but the compiled name (the expected one) is relative. |
| 1297 | // This is the case for split apks which depend on base or on other splits. |
| 1298 | dex_names_match = |
| 1299 | AbsolutePathHasRelativeSuffix(actual_dex_name, expected_dex_name); |
| 1300 | } else if (is_expected_dex_name_absolute) { |
| 1301 | // The runtime name is relative but the compiled name is absolute. |
| 1302 | // There is no expected use case that would end up here as dex files are always loaded |
| 1303 | // with their absolute location. However, be tolerant and do the best effort (in case |
| 1304 | // there are unexpected new use case...). |
| 1305 | dex_names_match = |
| 1306 | AbsolutePathHasRelativeSuffix(expected_dex_name, actual_dex_name); |
| 1307 | } else { |
| 1308 | // Both locations are relative. In this case there's not much we can be sure about |
| 1309 | // except that the names are the same. The checksum will ensure that the files are |
| 1310 | // are same. This should not happen outside testing and manual invocations. |
| 1311 | dex_names_match = (actual_dex_name == expected_dex_name); |
| 1312 | } |
| 1313 | |
| 1314 | return dex_names_match; |
| 1315 | } |
| 1316 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1317 | bool ClassLoaderContext::ClassLoaderInfoMatch( |
| 1318 | const ClassLoaderInfo& info, |
| 1319 | const ClassLoaderInfo& expected_info, |
| 1320 | const std::string& context_spec, |
| 1321 | bool verify_names, |
| 1322 | bool verify_checksums) const { |
| 1323 | if (info.type != expected_info.type) { |
| 1324 | LOG(WARNING) << "ClassLoaderContext type mismatch" |
| 1325 | << ". expected=" << GetClassLoaderTypeName(expected_info.type) |
| 1326 | << ", found=" << GetClassLoaderTypeName(info.type) |
| 1327 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
| 1328 | return false; |
| 1329 | } |
| 1330 | if (info.classpath.size() != expected_info.classpath.size()) { |
| 1331 | LOG(WARNING) << "ClassLoaderContext classpath size mismatch" |
| 1332 | << ". expected=" << expected_info.classpath.size() |
| 1333 | << ", found=" << info.classpath.size() |
Andreas Gampe | 7d0f81c | 2017-07-25 18:25:41 -0700 | [diff] [blame] | 1334 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1335 | return false; |
| 1336 | } |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1337 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1338 | if (verify_checksums) { |
| 1339 | DCHECK_EQ(info.classpath.size(), info.checksums.size()); |
| 1340 | DCHECK_EQ(expected_info.classpath.size(), expected_info.checksums.size()); |
| 1341 | } |
Mathieu Chartier | f5abfc4 | 2018-03-23 21:51:54 -0700 | [diff] [blame] | 1342 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1343 | if (verify_names) { |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1344 | for (size_t k = 0; k < info.classpath.size(); k++) { |
Calin Juravle | b495e7f | 2020-04-06 19:29:45 -0700 | [diff] [blame] | 1345 | bool dex_names_match = AreDexNameMatching(info.classpath[k], expected_info.classpath[k]); |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 1346 | |
| 1347 | // Compare the locations. |
Vladimir Marko | 36ec598 | 2019-11-28 10:55:16 +0000 | [diff] [blame] | 1348 | if (!dex_names_match) { |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1349 | LOG(WARNING) << "ClassLoaderContext classpath element mismatch" |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1350 | << ". expected=" << expected_info.classpath[k] |
Andreas Gampe | 7d0f81c | 2017-07-25 18:25:41 -0700 | [diff] [blame] | 1351 | << ", found=" << info.classpath[k] |
| 1352 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1353 | return false; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1354 | } |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 1355 | |
| 1356 | // Compare the checksums. |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1357 | if (info.checksums[k] != expected_info.checksums[k]) { |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1358 | LOG(WARNING) << "ClassLoaderContext classpath element checksum mismatch" |
Calin Juravle | 1e96a5d | 2017-09-05 17:10:48 -0700 | [diff] [blame] | 1359 | << ". expected=" << expected_info.checksums[k] |
| 1360 | << ", found=" << info.checksums[k] |
| 1361 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1362 | return false; |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | } |
Calin Juravle | 3f91864 | 2017-07-11 19:04:20 -0700 | [diff] [blame] | 1366 | |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1367 | if (info.shared_libraries.size() != expected_info.shared_libraries.size()) { |
| 1368 | LOG(WARNING) << "ClassLoaderContext shared library size mismatch. " |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 1369 | << "Expected=" << expected_info.shared_libraries.size() |
| 1370 | << ", found=" << info.shared_libraries.size() |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1371 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
| 1372 | return false; |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 1373 | } |
Nicolas Geoffray | 06af3b4 | 2018-10-29 10:39:04 +0000 | [diff] [blame] | 1374 | for (size_t i = 0; i < info.shared_libraries.size(); ++i) { |
| 1375 | if (!ClassLoaderInfoMatch(*info.shared_libraries[i].get(), |
| 1376 | *expected_info.shared_libraries[i].get(), |
| 1377 | context_spec, |
| 1378 | verify_names, |
| 1379 | verify_checksums)) { |
| 1380 | return false; |
| 1381 | } |
| 1382 | } |
| 1383 | if (info.parent.get() == nullptr) { |
| 1384 | if (expected_info.parent.get() != nullptr) { |
| 1385 | LOG(WARNING) << "ClassLoaderContext parent mismatch. " |
| 1386 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
| 1387 | return false; |
| 1388 | } |
| 1389 | return true; |
| 1390 | } else if (expected_info.parent.get() == nullptr) { |
| 1391 | LOG(WARNING) << "ClassLoaderContext parent mismatch. " |
| 1392 | << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")"; |
| 1393 | return false; |
| 1394 | } else { |
| 1395 | return ClassLoaderInfoMatch(*info.parent.get(), |
| 1396 | *expected_info.parent.get(), |
| 1397 | context_spec, |
| 1398 | verify_names, |
| 1399 | verify_checksums); |
| 1400 | } |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
Calin Juravle | b495e7f | 2020-04-06 19:29:45 -0700 | [diff] [blame] | 1403 | std::vector<const DexFile*> ClassLoaderContext::CheckForDuplicateDexFiles( |
| 1404 | const std::vector<const DexFile*>& dex_files_to_check) { |
| 1405 | DCHECK(dex_files_open_attempted_); |
| 1406 | DCHECK(dex_files_open_result_); |
| 1407 | |
| 1408 | std::vector<const DexFile*> result; |
| 1409 | |
| 1410 | if (special_shared_library_) { |
| 1411 | return result; |
| 1412 | } |
| 1413 | |
| 1414 | std::vector<ClassLoaderInfo*> work_list; |
| 1415 | work_list.push_back(class_loader_chain_.get()); |
| 1416 | while (!work_list.empty()) { |
| 1417 | ClassLoaderInfo* info = work_list.back(); |
| 1418 | work_list.pop_back(); |
| 1419 | for (size_t k = 0; k < info->classpath.size(); k++) { |
| 1420 | for (const DexFile* dex_file : dex_files_to_check) { |
| 1421 | if (info->checksums[k] == dex_file->GetLocationChecksum() |
| 1422 | && AreDexNameMatching(info->classpath[k], dex_file->GetLocation())) { |
| 1423 | result.push_back(dex_file); |
| 1424 | } |
| 1425 | } |
| 1426 | } |
| 1427 | AddToWorkList(info, work_list); |
| 1428 | } |
| 1429 | |
| 1430 | return result; |
| 1431 | } |
| 1432 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 1433 | } // namespace art |