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