blob: a4aaf131e2578b279ed7fe62ca02e8d1a7226ad9 [file] [log] [blame]
Calin Juravle87e2cb62017-06-13 21:48:45 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "class_loader_context.h"
18
Andreas Gampef9411702018-09-06 17:16:57 -070019#include <android-base/parseint.h>
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +000020#include <android-base/strings.h>
Andreas Gampef9411702018-09-06 17:16:57 -070021
Calin Juravle57d0acc2017-07-11 17:41:30 -070022#include "art_field-inl.h"
Vladimir Marko78baed52018-10-11 10:44:58 +010023#include "base/casts.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070024#include "base/dchecked_vector.h"
Andreas Gampe19f54162019-05-14 16:16:28 -070025#include "base/file_utils.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070026#include "base/stl_util.h"
27#include "class_linker.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070028#include "class_loader_utils.h"
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +000029#include "class_root.h"
David Sehr013fd802018-01-11 22:55:24 -080030#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080031#include "dex/dex_file.h"
32#include "dex/dex_file_loader.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070033#include "handle_scope-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010034#include "jni/jni_internal.h"
Vladimir Markobdc93b42019-03-29 16:12:04 +000035#include "mirror/class_loader-inl.h"
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +000036#include "mirror/object_array-alloc-inl.h"
37#include "nativehelper/scoped_local_ref.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070038#include "oat_file_assistant.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070039#include "obj_ptr-inl.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070040#include "runtime.h"
41#include "scoped_thread_state_change-inl.h"
42#include "thread.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070043#include "well_known_classes.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070044
45namespace art {
46
47static constexpr char kPathClassLoaderString[] = "PCL";
48static constexpr char kDelegateLastClassLoaderString[] = "DLC";
David Brazdil1a9ac532019-03-05 11:57:13 +000049static constexpr char kInMemoryDexClassLoaderString[] = "IMC";
Calin Juravle87e2cb62017-06-13 21:48:45 -070050static constexpr char kClassLoaderOpeningMark = '[';
51static constexpr char kClassLoaderClosingMark = ']';
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000052static constexpr char kClassLoaderSharedLibraryOpeningMark = '{';
53static constexpr char kClassLoaderSharedLibraryClosingMark = '}';
54static constexpr char kClassLoaderSharedLibrarySeparator = '#';
Calin Juravle7b0648a2017-07-07 18:40:50 -070055static constexpr char kClassLoaderSeparator = ';';
56static constexpr char kClasspathSeparator = ':';
57static constexpr char kDexFileChecksumSeparator = '*';
David Brazdil93d339d2019-03-27 09:56:45 +000058static constexpr char kInMemoryDexClassLoaderDexLocationMagic[] = "<unknown>";
Calin Juravle87e2cb62017-06-13 21:48:45 -070059
60ClassLoaderContext::ClassLoaderContext()
61 : special_shared_library_(false),
62 dex_files_open_attempted_(false),
Calin Juravle57d0acc2017-07-11 17:41:30 -070063 dex_files_open_result_(false),
Calin Juravle41acdc12017-07-18 17:45:32 -070064 owns_the_dex_files_(true) {}
Calin Juravle57d0acc2017-07-11 17:41:30 -070065
66ClassLoaderContext::ClassLoaderContext(bool owns_the_dex_files)
67 : special_shared_library_(false),
68 dex_files_open_attempted_(true),
69 dex_files_open_result_(true),
70 owns_the_dex_files_(owns_the_dex_files) {}
71
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000072// Utility method to add parent and shared libraries of `info` into
73// the `work_list`.
74static void AddToWorkList(
75 ClassLoaderContext::ClassLoaderInfo* info,
76 std::vector<ClassLoaderContext::ClassLoaderInfo*>& work_list) {
77 if (info->parent != nullptr) {
78 work_list.push_back(info->parent.get());
79 }
80 for (size_t i = 0; i < info->shared_libraries.size(); ++i) {
81 work_list.push_back(info->shared_libraries[i].get());
82 }
83}
84
Calin Juravle57d0acc2017-07-11 17:41:30 -070085ClassLoaderContext::~ClassLoaderContext() {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000086 if (!owns_the_dex_files_ && class_loader_chain_ != nullptr) {
Calin Juravle57d0acc2017-07-11 17:41:30 -070087 // If the context does not own the dex/oat files release the unique pointers to
88 // make sure we do not de-allocate them.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000089 std::vector<ClassLoaderInfo*> work_list;
90 work_list.push_back(class_loader_chain_.get());
91 while (!work_list.empty()) {
92 ClassLoaderInfo* info = work_list.back();
93 work_list.pop_back();
94 for (std::unique_ptr<OatFile>& oat_file : info->opened_oat_files) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -070095 oat_file.release(); // NOLINT b/117926937
Calin Juravle57d0acc2017-07-11 17:41:30 -070096 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000097 for (std::unique_ptr<const DexFile>& dex_file : info->opened_dex_files) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -070098 dex_file.release(); // NOLINT b/117926937
Calin Juravle57d0acc2017-07-11 17:41:30 -070099 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000100 AddToWorkList(info, work_list);
Calin Juravle57d0acc2017-07-11 17:41:30 -0700101 }
102 }
103}
Calin Juravle87e2cb62017-06-13 21:48:45 -0700104
Calin Juravle19915892017-08-03 17:10:36 +0000105std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Default() {
106 return Create("");
107}
108
Calin Juravle87e2cb62017-06-13 21:48:45 -0700109std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Create(const std::string& spec) {
110 std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext());
111 if (result->Parse(spec)) {
112 return result;
113 } else {
114 return nullptr;
115 }
116}
117
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000118static size_t FindMatchingSharedLibraryCloseMarker(const std::string& spec,
119 size_t shared_library_open_index) {
120 // Counter of opened shared library marker we've encountered so far.
121 uint32_t counter = 1;
122 // The index at which we're operating in the loop.
123 uint32_t string_index = shared_library_open_index + 1;
124 size_t shared_library_close = std::string::npos;
125 while (counter != 0) {
126 shared_library_close =
127 spec.find_first_of(kClassLoaderSharedLibraryClosingMark, string_index);
128 size_t shared_library_open =
129 spec.find_first_of(kClassLoaderSharedLibraryOpeningMark, string_index);
130 if (shared_library_close == std::string::npos) {
131 // No matching closing marker. Return an error.
132 break;
133 }
134
135 if ((shared_library_open == std::string::npos) ||
136 (shared_library_close < shared_library_open)) {
137 // We have seen a closing marker. Decrement the counter.
138 --counter;
139 // Move the search index forward.
140 string_index = shared_library_close + 1;
141 } else {
142 // New nested opening marker. Increment the counter and move the search
143 // index after the marker.
144 ++counter;
145 string_index = shared_library_open + 1;
146 }
147 }
148 return shared_library_close;
149}
150
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000151// The expected format is:
152// "ClassLoaderType1[ClasspathElem1*Checksum1:ClasspathElem2*Checksum2...]{ClassLoaderType2[...]}".
Calin Juravle7b0648a2017-07-07 18:40:50 -0700153// The checksum part of the format is expected only if parse_cheksums is true.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000154std::unique_ptr<ClassLoaderContext::ClassLoaderInfo> ClassLoaderContext::ParseClassLoaderSpec(
155 const std::string& class_loader_spec,
156 bool parse_checksums) {
157 ClassLoaderType class_loader_type = ExtractClassLoaderType(class_loader_spec);
158 if (class_loader_type == kInvalidClassLoader) {
159 return nullptr;
160 }
David Brazdil1a9ac532019-03-05 11:57:13 +0000161
162 // InMemoryDexClassLoader's dex location is always bogus. Special-case it.
163 if (class_loader_type == kInMemoryDexClassLoader) {
164 if (parse_checksums) {
165 // Make sure that OpenDexFiles() will never be attempted on this context
166 // because the dex locations of IMC do not correspond to real files.
167 CHECK(!dex_files_open_attempted_ || !dex_files_open_result_)
168 << "Parsing spec not supported when context created from a ClassLoader object";
169 dex_files_open_attempted_ = true;
170 dex_files_open_result_ = false;
171 } else {
172 // Checksums are not provided and dex locations themselves have no meaning
173 // (although we keep them in the spec to simplify parsing). Treat this as
174 // an unknown class loader.
David Brazdil93d339d2019-03-27 09:56:45 +0000175 // We can hit this case if dex2oat is invoked with a spec containing IMC.
176 // Because the dex file data is only available at runtime, we cannot proceed.
David Brazdil1a9ac532019-03-05 11:57:13 +0000177 return nullptr;
178 }
179 }
180
Calin Juravle87e2cb62017-06-13 21:48:45 -0700181 const char* class_loader_type_str = GetClassLoaderTypeName(class_loader_type);
182 size_t type_str_size = strlen(class_loader_type_str);
183
184 CHECK_EQ(0, class_loader_spec.compare(0, type_str_size, class_loader_type_str));
185
186 // Check the opening and closing markers.
187 if (class_loader_spec[type_str_size] != kClassLoaderOpeningMark) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000188 return nullptr;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700189 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000190 if ((class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderClosingMark) &&
191 (class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderSharedLibraryClosingMark)) {
192 return nullptr;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700193 }
194
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000195 size_t closing_index = class_loader_spec.find_first_of(kClassLoaderClosingMark);
196
Calin Juravle87e2cb62017-06-13 21:48:45 -0700197 // At this point we know the format is ok; continue and extract the classpath.
198 // Note that class loaders with an empty class path are allowed.
199 std::string classpath = class_loader_spec.substr(type_str_size + 1,
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000200 closing_index - type_str_size - 1);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700201
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000202 std::unique_ptr<ClassLoaderInfo> info(new ClassLoaderInfo(class_loader_type));
Calin Juravle7b0648a2017-07-07 18:40:50 -0700203
204 if (!parse_checksums) {
David Brazdil93d339d2019-03-27 09:56:45 +0000205 DCHECK(class_loader_type != kInMemoryDexClassLoader);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000206 Split(classpath, kClasspathSeparator, &info->classpath);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700207 } else {
208 std::vector<std::string> classpath_elements;
209 Split(classpath, kClasspathSeparator, &classpath_elements);
210 for (const std::string& element : classpath_elements) {
211 std::vector<std::string> dex_file_with_checksum;
212 Split(element, kDexFileChecksumSeparator, &dex_file_with_checksum);
213 if (dex_file_with_checksum.size() != 2) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000214 return nullptr;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700215 }
216 uint32_t checksum = 0;
Tom Cherry7bc8e8f2018-10-05 14:34:13 -0700217 if (!android::base::ParseUint(dex_file_with_checksum[1].c_str(), &checksum)) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000218 return nullptr;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700219 }
David Brazdil93d339d2019-03-27 09:56:45 +0000220 if ((class_loader_type == kInMemoryDexClassLoader) &&
221 (dex_file_with_checksum[0] != kInMemoryDexClassLoaderDexLocationMagic)) {
222 return nullptr;
223 }
David Brazdil1a9ac532019-03-05 11:57:13 +0000224
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000225 info->classpath.push_back(dex_file_with_checksum[0]);
226 info->checksums.push_back(checksum);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700227 }
228 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700229
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000230 if ((class_loader_spec[class_loader_spec.length() - 1] == kClassLoaderSharedLibraryClosingMark) &&
231 (class_loader_spec[class_loader_spec.length() - 2] != kClassLoaderSharedLibraryOpeningMark)) {
232 // Non-empty list of shared libraries.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000233 size_t start_index = class_loader_spec.find_first_of(kClassLoaderSharedLibraryOpeningMark);
234 if (start_index == std::string::npos) {
235 return nullptr;
236 }
237 std::string shared_libraries_spec =
238 class_loader_spec.substr(start_index + 1, class_loader_spec.length() - start_index - 2);
239 std::vector<std::string> shared_libraries;
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000240 size_t cursor = 0;
241 while (cursor != shared_libraries_spec.length()) {
242 size_t shared_library_separator =
243 shared_libraries_spec.find_first_of(kClassLoaderSharedLibrarySeparator, cursor);
244 size_t shared_library_open =
245 shared_libraries_spec.find_first_of(kClassLoaderSharedLibraryOpeningMark, cursor);
246 std::string shared_library_spec;
247 if (shared_library_separator == std::string::npos) {
248 // Only one shared library, for example:
249 // PCL[...]
250 shared_library_spec =
251 shared_libraries_spec.substr(cursor, shared_libraries_spec.length() - cursor);
252 cursor = shared_libraries_spec.length();
253 } else if ((shared_library_open == std::string::npos) ||
254 (shared_library_open > shared_library_separator)) {
255 // We found a shared library without nested shared libraries, for example:
256 // PCL[...]#PCL[...]{...}
257 shared_library_spec =
258 shared_libraries_spec.substr(cursor, shared_library_separator - cursor);
259 cursor = shared_library_separator + 1;
260 } else {
261 // The shared library contains nested shared libraries. Find the matching closing shared
262 // marker for it.
263 size_t closing_marker =
264 FindMatchingSharedLibraryCloseMarker(shared_libraries_spec, shared_library_open);
265 if (closing_marker == std::string::npos) {
266 // No matching closing marker, return an error.
267 return nullptr;
268 }
269 shared_library_spec = shared_libraries_spec.substr(cursor, closing_marker + 1 - cursor);
270 cursor = closing_marker + 1;
271 if (cursor != shared_libraries_spec.length() &&
272 shared_libraries_spec[cursor] == kClassLoaderSharedLibrarySeparator) {
273 // Pass the shared library separator marker.
274 ++cursor;
275 }
276 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000277 std::unique_ptr<ClassLoaderInfo> shared_library(
278 ParseInternal(shared_library_spec, parse_checksums));
279 if (shared_library == nullptr) {
280 return nullptr;
281 }
282 info->shared_libraries.push_back(std::move(shared_library));
283 }
284 }
285
286 return info;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700287}
288
289// Extracts the class loader type from the given spec.
290// Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not
291// recognized.
292ClassLoaderContext::ClassLoaderType
293ClassLoaderContext::ExtractClassLoaderType(const std::string& class_loader_spec) {
David Brazdil1a9ac532019-03-05 11:57:13 +0000294 const ClassLoaderType kValidTypes[] = { kPathClassLoader,
295 kDelegateLastClassLoader,
296 kInMemoryDexClassLoader };
Calin Juravle87e2cb62017-06-13 21:48:45 -0700297 for (const ClassLoaderType& type : kValidTypes) {
298 const char* type_str = GetClassLoaderTypeName(type);
299 if (class_loader_spec.compare(0, strlen(type_str), type_str) == 0) {
300 return type;
301 }
302 }
303 return kInvalidClassLoader;
304}
305
306// The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]...
307// ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader).
308// ClasspathElem is the path of dex/jar/apk file.
Calin Juravle7b0648a2017-07-07 18:40:50 -0700309bool ClassLoaderContext::Parse(const std::string& spec, bool parse_checksums) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700310 if (spec.empty()) {
Calin Juravle1a509c82017-07-24 16:51:21 -0700311 // By default we load the dex files in a PathClassLoader.
312 // So an empty spec is equivalent to an empty PathClassLoader (this happens when running
313 // tests)
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000314 class_loader_chain_.reset(new ClassLoaderInfo(kPathClassLoader));
Calin Juravle7b0648a2017-07-07 18:40:50 -0700315 return true;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700316 }
Calin Juravle7b0648a2017-07-07 18:40:50 -0700317
Calin Juravle87e2cb62017-06-13 21:48:45 -0700318 // Stop early if we detect the special shared library, which may be passed as the classpath
319 // for dex2oat when we want to skip the shared libraries check.
320 if (spec == OatFile::kSpecialSharedLibrary) {
321 LOG(INFO) << "The ClassLoaderContext is a special shared library.";
322 special_shared_library_ = true;
323 return true;
324 }
325
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000326 CHECK(class_loader_chain_ == nullptr);
327 class_loader_chain_.reset(ParseInternal(spec, parse_checksums));
328 return class_loader_chain_ != nullptr;
329}
Calin Juravle87e2cb62017-06-13 21:48:45 -0700330
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000331ClassLoaderContext::ClassLoaderInfo* ClassLoaderContext::ParseInternal(
332 const std::string& spec, bool parse_checksums) {
333 CHECK(!spec.empty());
334 CHECK_NE(spec, OatFile::kSpecialSharedLibrary);
335 std::string remaining = spec;
336 std::unique_ptr<ClassLoaderInfo> first(nullptr);
337 ClassLoaderInfo* previous_iteration = nullptr;
338 while (!remaining.empty()) {
339 std::string class_loader_spec;
340 size_t first_class_loader_separator = remaining.find_first_of(kClassLoaderSeparator);
341 size_t first_shared_library_open =
342 remaining.find_first_of(kClassLoaderSharedLibraryOpeningMark);
343 if (first_class_loader_separator == std::string::npos) {
344 // Only one class loader, for example:
345 // PCL[...]
346 class_loader_spec = remaining;
347 remaining = "";
348 } else if ((first_shared_library_open == std::string::npos) ||
349 (first_shared_library_open > first_class_loader_separator)) {
350 // We found a class loader spec without shared libraries, for example:
351 // PCL[...];PCL[...]{...}
352 class_loader_spec = remaining.substr(0, first_class_loader_separator);
353 remaining = remaining.substr(first_class_loader_separator + 1,
354 remaining.size() - first_class_loader_separator - 1);
355 } else {
356 // The class loader spec contains shared libraries. Find the matching closing
357 // shared library marker for it.
358
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000359 uint32_t shared_library_close =
360 FindMatchingSharedLibraryCloseMarker(remaining, first_shared_library_open);
361 if (shared_library_close == std::string::npos) {
362 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
363 return nullptr;
364 }
365 class_loader_spec = remaining.substr(0, shared_library_close + 1);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000366
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000367 // Compute the remaining string to analyze.
368 if (remaining.size() == shared_library_close + 1) {
369 remaining = "";
370 } else if ((remaining.size() == shared_library_close + 2) ||
371 (remaining.at(shared_library_close + 1) != kClassLoaderSeparator)) {
372 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
373 return nullptr;
374 } else {
375 remaining = remaining.substr(shared_library_close + 2,
376 remaining.size() - shared_library_close - 2);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000377 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700378 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000379
380 std::unique_ptr<ClassLoaderInfo> info =
381 ParseClassLoaderSpec(class_loader_spec, parse_checksums);
382 if (info == nullptr) {
383 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
384 return nullptr;
385 }
386 if (first == nullptr) {
Andreas Gampe41c911f2018-11-19 11:34:16 -0800387 first = std::move(info);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000388 previous_iteration = first.get();
389 } else {
390 CHECK(previous_iteration != nullptr);
Andreas Gampe41c911f2018-11-19 11:34:16 -0800391 previous_iteration->parent = std::move(info);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000392 previous_iteration = previous_iteration->parent.get();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700393 }
394 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000395 return first.release();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700396}
397
398// Opens requested class path files and appends them to opened_dex_files. If the dex files have
399// been stripped, this opens them from their oat files (which get added to opened_oat_files).
David Brazdil89821862019-03-19 13:57:43 +0000400bool ClassLoaderContext::OpenDexFiles(InstructionSet isa,
401 const std::string& classpath_dir,
402 const std::vector<int>& fds) {
Calin Juravlec5b215f2017-09-12 14:49:37 -0700403 if (dex_files_open_attempted_) {
404 // Do not attempt to re-open the files if we already tried.
405 return dex_files_open_result_;
406 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700407
408 dex_files_open_attempted_ = true;
409 // Assume we can open all dex files. If not, we will set this to false as we go.
410 dex_files_open_result_ = true;
411
412 if (special_shared_library_) {
413 // Nothing to open if the context is a special shared library.
414 return true;
415 }
416
417 // Note that we try to open all dex files even if some fail.
418 // We may get resource-only apks which we cannot load.
419 // TODO(calin): Refine the dex opening interface to be able to tell if an archive contains
420 // no dex files. So that we can distinguish the real failures...
David Sehr013fd802018-01-11 22:55:24 -0800421 const ArtDexFileLoader dex_file_loader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000422 std::vector<ClassLoaderInfo*> work_list;
Nicolas Geoffray9893c472018-11-13 15:39:53 +0000423 CHECK(class_loader_chain_ != nullptr);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000424 work_list.push_back(class_loader_chain_.get());
David Brazdil89821862019-03-19 13:57:43 +0000425 size_t dex_file_index = 0;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000426 while (!work_list.empty()) {
427 ClassLoaderInfo* info = work_list.back();
428 work_list.pop_back();
David Brazdil93d339d2019-03-27 09:56:45 +0000429 DCHECK(info->type != kInMemoryDexClassLoader) << __FUNCTION__ << " not supported for IMC";
430
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000431 size_t opened_dex_files_index = info->opened_dex_files.size();
432 for (const std::string& cp_elem : info->classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700433 // If path is relative, append it to the provided base directory.
Calin Juravle92003fe2017-09-06 02:22:57 +0000434 std::string location = cp_elem;
435 if (location[0] != '/' && !classpath_dir.empty()) {
Nicolas Geoffray06ffecf2017-11-14 10:31:54 +0000436 location = classpath_dir + (classpath_dir.back() == '/' ? "" : "/") + location;
Calin Juravle821a2592017-08-11 14:33:38 -0700437 }
438
David Brazdil89821862019-03-19 13:57:43 +0000439 // If file descriptors were provided for the class loader context dex paths,
440 // get the descriptor which correponds to this dex path. We assume the `fds`
441 // vector follows the same order as a flattened class loader context.
442 int fd = -1;
443 if (!fds.empty()) {
444 if (dex_file_index >= fds.size()) {
445 LOG(WARNING) << "Number of FDs is smaller than number of dex files in the context";
446 dex_files_open_result_ = false;
447 return false;
448 }
449
450 fd = fds[dex_file_index++];
451 DCHECK_GE(fd, 0);
452 }
453
Calin Juravle87e2cb62017-06-13 21:48:45 -0700454 std::string error_msg;
455 // When opening the dex files from the context we expect their checksum to match their
456 // contents. So pass true to verify_checksum.
David Brazdil89821862019-03-19 13:57:43 +0000457 if (fd < 0) {
458 if (!dex_file_loader.Open(location.c_str(),
459 location.c_str(),
460 Runtime::Current()->IsVerificationEnabled(),
461 /*verify_checksum=*/ true,
462 &error_msg,
463 &info->opened_dex_files)) {
464 // If we fail to open the dex file because it's been stripped, try to
465 // open the dex file from its corresponding oat file.
466 // This could happen when we need to recompile a pre-build whose dex
467 // code has been stripped (for example, if the pre-build is only
468 // quicken and we want to re-compile it speed-profile).
469 // TODO(calin): Use the vdex directly instead of going through the oat file.
470 OatFileAssistant oat_file_assistant(location.c_str(), isa, false);
471 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
472 std::vector<std::unique_ptr<const DexFile>> oat_dex_files;
473 if (oat_file != nullptr &&
474 OatFileAssistant::LoadDexFiles(*oat_file, location, &oat_dex_files)) {
475 info->opened_oat_files.push_back(std::move(oat_file));
476 info->opened_dex_files.insert(info->opened_dex_files.end(),
477 std::make_move_iterator(oat_dex_files.begin()),
478 std::make_move_iterator(oat_dex_files.end()));
479 } else {
480 LOG(WARNING) << "Could not open dex files from location: " << location;
481 dex_files_open_result_ = false;
482 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700483 }
David Brazdil89821862019-03-19 13:57:43 +0000484 } else if (!dex_file_loader.Open(fd,
485 location.c_str(),
486 Runtime::Current()->IsVerificationEnabled(),
487 /*verify_checksum=*/ true,
488 &error_msg,
489 &info->opened_dex_files)) {
490 LOG(WARNING) << "Could not open dex files from fd " << fd << " for location: " << location;
491 dex_files_open_result_ = false;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700492 }
493 }
Calin Juravlec5b215f2017-09-12 14:49:37 -0700494
495 // We finished opening the dex files from the classpath.
496 // Now update the classpath and the checksum with the locations of the dex files.
497 //
498 // We do this because initially the classpath contains the paths of the dex files; and
499 // some of them might be multi-dexes. So in order to have a consistent view we replace all the
500 // file paths with the actual dex locations being loaded.
501 // This will allow the context to VerifyClassLoaderContextMatch which expects or multidex
502 // location in the class paths.
503 // Note that this will also remove the paths that could not be opened.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000504 info->original_classpath = std::move(info->classpath);
505 info->classpath.clear();
506 info->checksums.clear();
507 for (size_t k = opened_dex_files_index; k < info->opened_dex_files.size(); k++) {
508 std::unique_ptr<const DexFile>& dex = info->opened_dex_files[k];
509 info->classpath.push_back(dex->GetLocation());
510 info->checksums.push_back(dex->GetLocationChecksum());
Calin Juravlec5b215f2017-09-12 14:49:37 -0700511 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000512 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700513 }
514
David Brazdil89821862019-03-19 13:57:43 +0000515 // Check that if file descriptors were provided, there were exactly as many
516 // as we have encountered while iterating over this class loader context.
517 if (dex_file_index != fds.size()) {
518 LOG(WARNING) << fds.size() << " FDs provided but only " << dex_file_index
519 << " dex files are in the class loader context";
520 dex_files_open_result_ = false;
521 }
522
Calin Juravle87e2cb62017-06-13 21:48:45 -0700523 return dex_files_open_result_;
524}
525
526bool ClassLoaderContext::RemoveLocationsFromClassPaths(
527 const dchecked_vector<std::string>& locations) {
528 CHECK(!dex_files_open_attempted_)
529 << "RemoveLocationsFromClasspaths cannot be call after OpenDexFiles";
530
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000531 if (class_loader_chain_ == nullptr) {
532 return false;
533 }
534
Calin Juravle87e2cb62017-06-13 21:48:45 -0700535 std::set<std::string> canonical_locations;
536 for (const std::string& location : locations) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700537 canonical_locations.insert(DexFileLoader::GetDexCanonicalLocation(location.c_str()));
Calin Juravle87e2cb62017-06-13 21:48:45 -0700538 }
539 bool removed_locations = false;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000540 std::vector<ClassLoaderInfo*> work_list;
541 work_list.push_back(class_loader_chain_.get());
542 while (!work_list.empty()) {
543 ClassLoaderInfo* info = work_list.back();
544 work_list.pop_back();
545 size_t initial_size = info->classpath.size();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700546 auto kept_it = std::remove_if(
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000547 info->classpath.begin(),
548 info->classpath.end(),
Calin Juravle87e2cb62017-06-13 21:48:45 -0700549 [canonical_locations](const std::string& location) {
550 return ContainsElement(canonical_locations,
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700551 DexFileLoader::GetDexCanonicalLocation(location.c_str()));
Calin Juravle87e2cb62017-06-13 21:48:45 -0700552 });
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000553 info->classpath.erase(kept_it, info->classpath.end());
554 if (initial_size != info->classpath.size()) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700555 removed_locations = true;
556 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000557 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700558 }
559 return removed_locations;
560}
561
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700562std::string ClassLoaderContext::EncodeContextForDex2oat(const std::string& base_dir) const {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700563 return EncodeContext(base_dir, /*for_dex2oat=*/ true, /*stored_context=*/ nullptr);
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700564}
565
Mathieu Chartierc4440772018-04-16 14:40:56 -0700566std::string ClassLoaderContext::EncodeContextForOatFile(const std::string& base_dir,
567 ClassLoaderContext* stored_context) const {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700568 return EncodeContext(base_dir, /*for_dex2oat=*/ false, stored_context);
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700569}
570
571std::string ClassLoaderContext::EncodeContext(const std::string& base_dir,
Mathieu Chartierc4440772018-04-16 14:40:56 -0700572 bool for_dex2oat,
573 ClassLoaderContext* stored_context) const {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700574 CheckDexFilesOpened("EncodeContextForOatFile");
575 if (special_shared_library_) {
576 return OatFile::kSpecialSharedLibrary;
577 }
578
Mathieu Chartierc4440772018-04-16 14:40:56 -0700579 if (stored_context != nullptr) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000580 DCHECK_EQ(GetParentChainSize(), stored_context->GetParentChainSize());
Mathieu Chartierc4440772018-04-16 14:40:56 -0700581 }
582
Calin Juravle7b0648a2017-07-07 18:40:50 -0700583 std::ostringstream out;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000584 if (class_loader_chain_ == nullptr) {
Calin Juravle1a509c82017-07-24 16:51:21 -0700585 // We can get in this situation if the context was created with a class path containing the
586 // source dex files which were later removed (happens during run-tests).
587 out << GetClassLoaderTypeName(kPathClassLoader)
588 << kClassLoaderOpeningMark
589 << kClassLoaderClosingMark;
590 return out.str();
591 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700592
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000593 EncodeContextInternal(
594 *class_loader_chain_,
595 base_dir,
596 for_dex2oat,
597 (stored_context == nullptr ? nullptr : stored_context->class_loader_chain_.get()),
598 out);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700599 return out.str();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700600}
601
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000602void ClassLoaderContext::EncodeContextInternal(const ClassLoaderInfo& info,
603 const std::string& base_dir,
604 bool for_dex2oat,
605 ClassLoaderInfo* stored_info,
606 std::ostringstream& out) const {
607 out << GetClassLoaderTypeName(info.type);
608 out << kClassLoaderOpeningMark;
609 std::set<std::string> seen_locations;
610 SafeMap<std::string, std::string> remap;
611 if (stored_info != nullptr) {
612 for (size_t k = 0; k < info.original_classpath.size(); ++k) {
613 // Note that we don't care if the same name appears twice.
614 remap.Put(info.original_classpath[k], stored_info->classpath[k]);
615 }
616 }
617 for (size_t k = 0; k < info.opened_dex_files.size(); k++) {
618 const std::unique_ptr<const DexFile>& dex_file = info.opened_dex_files[k];
619 if (for_dex2oat) {
620 // dex2oat only needs the base location. It cannot accept multidex locations.
621 // So ensure we only add each file once.
622 bool new_insert = seen_locations.insert(
623 DexFileLoader::GetBaseLocation(dex_file->GetLocation())).second;
624 if (!new_insert) {
625 continue;
626 }
627 }
628 std::string location = dex_file->GetLocation();
629 // If there is a stored class loader remap, fix up the multidex strings.
630 if (!remap.empty()) {
631 std::string base_dex_location = DexFileLoader::GetBaseLocation(location);
632 auto it = remap.find(base_dex_location);
633 CHECK(it != remap.end()) << base_dex_location;
634 location = it->second + DexFileLoader::GetMultiDexSuffix(location);
635 }
636 if (k > 0) {
637 out << kClasspathSeparator;
638 }
David Brazdil93d339d2019-03-27 09:56:45 +0000639 if (info.type == kInMemoryDexClassLoader) {
640 out << kInMemoryDexClassLoaderDexLocationMagic;
641 } else if (!base_dir.empty() && location.substr(0, base_dir.length()) == base_dir) {
642 // Find paths that were relative and convert them back from absolute.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000643 out << location.substr(base_dir.length() + 1).c_str();
644 } else {
645 out << location.c_str();
646 }
647 // dex2oat does not need the checksums.
648 if (!for_dex2oat) {
649 out << kDexFileChecksumSeparator;
650 out << dex_file->GetLocationChecksum();
651 }
652 }
653 out << kClassLoaderClosingMark;
654
655 if (!info.shared_libraries.empty()) {
656 out << kClassLoaderSharedLibraryOpeningMark;
657 for (uint32_t i = 0; i < info.shared_libraries.size(); ++i) {
658 if (i > 0) {
659 out << kClassLoaderSharedLibrarySeparator;
660 }
661 EncodeContextInternal(
662 *info.shared_libraries[i].get(),
663 base_dir,
664 for_dex2oat,
665 (stored_info == nullptr ? nullptr : stored_info->shared_libraries[i].get()),
666 out);
667 }
668 out << kClassLoaderSharedLibraryClosingMark;
669 }
670 if (info.parent != nullptr) {
671 out << kClassLoaderSeparator;
672 EncodeContextInternal(
673 *info.parent.get(),
674 base_dir,
675 for_dex2oat,
676 (stored_info == nullptr ? nullptr : stored_info->parent.get()),
677 out);
678 }
679}
680
681// Returns the WellKnownClass for the given class loader type.
682static jclass GetClassLoaderClass(ClassLoaderContext::ClassLoaderType type) {
683 switch (type) {
684 case ClassLoaderContext::kPathClassLoader:
685 return WellKnownClasses::dalvik_system_PathClassLoader;
686 case ClassLoaderContext::kDelegateLastClassLoader:
687 return WellKnownClasses::dalvik_system_DelegateLastClassLoader;
David Brazdil1a9ac532019-03-05 11:57:13 +0000688 case ClassLoaderContext::kInMemoryDexClassLoader:
689 return WellKnownClasses::dalvik_system_InMemoryDexClassLoader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000690 case ClassLoaderContext::kInvalidClassLoader: break; // will fail after the switch.
691 }
692 LOG(FATAL) << "Invalid class loader type " << type;
693 UNREACHABLE();
694}
695
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000696static std::string FlattenClasspath(const std::vector<std::string>& classpath) {
697 return android::base::Join(classpath, ':');
698}
699
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000700static ObjPtr<mirror::ClassLoader> CreateClassLoaderInternal(
701 Thread* self,
702 ScopedObjectAccess& soa,
703 const ClassLoaderContext::ClassLoaderInfo& info,
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000704 bool for_shared_library,
705 VariableSizedHandleScope& map_scope,
706 std::map<std::string, Handle<mirror::ClassLoader>>& canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000707 bool add_compilation_sources,
708 const std::vector<const DexFile*>& compilation_sources)
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000709 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000710 if (for_shared_library) {
711 // Check if the shared library has already been created.
712 auto search = canonicalized_libraries.find(FlattenClasspath(info.classpath));
713 if (search != canonicalized_libraries.end()) {
714 return search->second.Get();
715 }
716 }
717
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000718 StackHandleScope<3> hs(self);
719 MutableHandle<mirror::ObjectArray<mirror::ClassLoader>> libraries(
720 hs.NewHandle<mirror::ObjectArray<mirror::ClassLoader>>(nullptr));
721
722 if (!info.shared_libraries.empty()) {
723 libraries.Assign(mirror::ObjectArray<mirror::ClassLoader>::Alloc(
724 self,
725 GetClassRoot<mirror::ObjectArray<mirror::ClassLoader>>(),
726 info.shared_libraries.size()));
727 for (uint32_t i = 0; i < info.shared_libraries.size(); ++i) {
728 // We should only add the compilation sources to the first class loader.
729 libraries->Set(i,
730 CreateClassLoaderInternal(
731 self,
732 soa,
733 *info.shared_libraries[i].get(),
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000734 /* for_shared_library= */ true,
735 map_scope,
736 canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000737 /* add_compilation_sources= */ false,
738 compilation_sources));
739 }
740 }
741
742 MutableHandle<mirror::ClassLoader> parent = hs.NewHandle<mirror::ClassLoader>(nullptr);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000743 if (info.parent != nullptr) {
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000744 // We should only add the compilation sources to the first class loader.
745 parent.Assign(CreateClassLoaderInternal(
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000746 self,
747 soa,
748 *info.parent.get(),
749 /* for_shared_library= */ false,
750 map_scope,
751 canonicalized_libraries,
752 /* add_compilation_sources= */ false,
753 compilation_sources));
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000754 }
755 std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector(
756 info.opened_dex_files);
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000757 if (add_compilation_sources) {
758 // For the first class loader, its classpath comes first, followed by compilation sources.
759 // This ensures that whenever we need to resolve classes from it the classpath elements
760 // come first.
761 class_path_files.insert(class_path_files.end(),
762 compilation_sources.begin(),
763 compilation_sources.end());
764 }
765 Handle<mirror::Class> loader_class = hs.NewHandle<mirror::Class>(
766 soa.Decode<mirror::Class>(GetClassLoaderClass(info.type)));
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000767 ObjPtr<mirror::ClassLoader> loader =
768 Runtime::Current()->GetClassLinker()->CreateWellKnownClassLoader(
769 self,
770 class_path_files,
771 loader_class,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000772 parent,
773 libraries);
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000774 if (for_shared_library) {
775 canonicalized_libraries[FlattenClasspath(info.classpath)] =
776 map_scope.NewHandle<mirror::ClassLoader>(loader);
777 }
778 return loader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000779}
780
Calin Juravle87e2cb62017-06-13 21:48:45 -0700781jobject ClassLoaderContext::CreateClassLoader(
782 const std::vector<const DexFile*>& compilation_sources) const {
783 CheckDexFilesOpened("CreateClassLoader");
784
785 Thread* self = Thread::Current();
786 ScopedObjectAccess soa(self);
787
Calin Juravlec79470d2017-07-12 17:37:42 -0700788 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700789
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000790 if (class_loader_chain_ == nullptr) {
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000791 CHECK(special_shared_library_);
Calin Juravlec79470d2017-07-12 17:37:42 -0700792 return class_linker->CreatePathClassLoader(self, compilation_sources);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700793 }
794
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000795 // Create a map of canonicalized shared libraries. As we're holding objects,
796 // we're creating a variable size handle scope to put handles in the map.
797 VariableSizedHandleScope map_scope(self);
798 std::map<std::string, Handle<mirror::ClassLoader>> canonicalized_libraries;
799
800 // Create the class loader.
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000801 ObjPtr<mirror::ClassLoader> loader =
802 CreateClassLoaderInternal(self,
803 soa,
804 *class_loader_chain_.get(),
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000805 /* for_shared_library= */ false,
806 map_scope,
807 canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000808 /* add_compilation_sources= */ true,
809 compilation_sources);
810 // Make it a global ref and return.
811 ScopedLocalRef<jobject> local_ref(
812 soa.Env(), soa.Env()->AddLocalReference<jobject>(loader));
813 return soa.Env()->NewGlobalRef(local_ref.get());
Calin Juravle87e2cb62017-06-13 21:48:45 -0700814}
815
816std::vector<const DexFile*> ClassLoaderContext::FlattenOpenedDexFiles() const {
817 CheckDexFilesOpened("FlattenOpenedDexFiles");
818
819 std::vector<const DexFile*> result;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000820 if (class_loader_chain_ == nullptr) {
821 return result;
822 }
823 std::vector<ClassLoaderInfo*> work_list;
824 work_list.push_back(class_loader_chain_.get());
825 while (!work_list.empty()) {
826 ClassLoaderInfo* info = work_list.back();
827 work_list.pop_back();
828 for (const std::unique_ptr<const DexFile>& dex_file : info->opened_dex_files) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700829 result.push_back(dex_file.get());
830 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000831 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700832 }
833 return result;
834}
835
David Brazdil89821862019-03-19 13:57:43 +0000836std::string ClassLoaderContext::FlattenDexPaths() const {
837 if (class_loader_chain_ == nullptr) {
838 return "";
839 }
840
841 std::vector<std::string> result;
842 std::vector<ClassLoaderInfo*> work_list;
843 work_list.push_back(class_loader_chain_.get());
844 while (!work_list.empty()) {
845 ClassLoaderInfo* info = work_list.back();
846 work_list.pop_back();
847 for (const std::string& dex_path : info->classpath) {
848 result.push_back(dex_path);
849 }
850 AddToWorkList(info, work_list);
851 }
852 return FlattenClasspath(result);
853}
854
Calin Juravle87e2cb62017-06-13 21:48:45 -0700855const char* ClassLoaderContext::GetClassLoaderTypeName(ClassLoaderType type) {
856 switch (type) {
857 case kPathClassLoader: return kPathClassLoaderString;
858 case kDelegateLastClassLoader: return kDelegateLastClassLoaderString;
David Brazdil1a9ac532019-03-05 11:57:13 +0000859 case kInMemoryDexClassLoader: return kInMemoryDexClassLoaderString;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700860 default:
861 LOG(FATAL) << "Invalid class loader type " << type;
862 UNREACHABLE();
863 }
864}
865
866void ClassLoaderContext::CheckDexFilesOpened(const std::string& calling_method) const {
867 CHECK(dex_files_open_attempted_)
868 << "Dex files were not successfully opened before the call to " << calling_method
869 << "attempt=" << dex_files_open_attempted_ << ", result=" << dex_files_open_result_;
870}
Calin Juravle7b0648a2017-07-07 18:40:50 -0700871
Calin Juravle57d0acc2017-07-11 17:41:30 -0700872// Collects the dex files from the give Java dex_file object. Only the dex files with
873// at least 1 class are collected. If a null java_dex_file is passed this method does nothing.
874static bool CollectDexFilesFromJavaDexFile(ObjPtr<mirror::Object> java_dex_file,
875 ArtField* const cookie_field,
876 std::vector<const DexFile*>* out_dex_files)
877 REQUIRES_SHARED(Locks::mutator_lock_) {
878 if (java_dex_file == nullptr) {
879 return true;
880 }
881 // On the Java side, the dex files are stored in the cookie field.
Vladimir Marko4617d582019-03-28 13:48:31 +0000882 ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(java_dex_file)->AsLongArray();
Calin Juravle57d0acc2017-07-11 17:41:30 -0700883 if (long_array == nullptr) {
884 // This should never happen so log a warning.
885 LOG(ERROR) << "Unexpected null cookie";
886 return false;
887 }
888 int32_t long_array_size = long_array->GetLength();
889 // Index 0 from the long array stores the oat file. The dex files start at index 1.
890 for (int32_t j = 1; j < long_array_size; ++j) {
Vladimir Marko78baed52018-10-11 10:44:58 +0100891 const DexFile* cp_dex_file =
892 reinterpret_cast64<const DexFile*>(long_array->GetWithoutChecks(j));
Calin Juravle57d0acc2017-07-11 17:41:30 -0700893 if (cp_dex_file != nullptr && cp_dex_file->NumClassDefs() > 0) {
894 // TODO(calin): It's unclear why the dex files with no classes are skipped here and when
895 // cp_dex_file can be null.
896 out_dex_files->push_back(cp_dex_file);
897 }
898 }
899 return true;
900}
901
902// Collects all the dex files loaded by the given class loader.
903// Returns true for success or false if an unexpected state is discovered (e.g. a null dex cookie,
904// a null list of dex elements or a null dex element).
905static bool CollectDexFilesFromSupportedClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
906 Handle<mirror::ClassLoader> class_loader,
907 std::vector<const DexFile*>* out_dex_files)
908 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil1a9ac532019-03-05 11:57:13 +0000909 CHECK(IsPathOrDexClassLoader(soa, class_loader) ||
910 IsDelegateLastClassLoader(soa, class_loader) ||
911 IsInMemoryDexClassLoader(soa, class_loader));
Calin Juravle57d0acc2017-07-11 17:41:30 -0700912
913 // All supported class loaders inherit from BaseDexClassLoader.
914 // We need to get the DexPathList and loop through it.
915 ArtField* const cookie_field =
916 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
917 ArtField* const dex_file_field =
918 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
919 ObjPtr<mirror::Object> dex_path_list =
920 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)->
921 GetObject(class_loader.Get());
922 CHECK(cookie_field != nullptr);
923 CHECK(dex_file_field != nullptr);
924 if (dex_path_list == nullptr) {
925 // This may be null if the current class loader is under construction and it does not
926 // have its fields setup yet.
927 return true;
928 }
929 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
930 ObjPtr<mirror::Object> dex_elements_obj =
931 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)->
932 GetObject(dex_path_list);
933 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
934 // at the mCookie which is a DexFile vector.
935 if (dex_elements_obj == nullptr) {
936 // TODO(calin): It's unclear if we should just assert here. For now be prepared for the worse
937 // and assume we have no elements.
938 return true;
939 } else {
940 StackHandleScope<1> hs(soa.Self());
941 Handle<mirror::ObjectArray<mirror::Object>> dex_elements(
942 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()));
943 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
Vladimir Marko423bebb2019-03-26 15:17:21 +0000944 ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i);
Calin Juravle57d0acc2017-07-11 17:41:30 -0700945 if (element == nullptr) {
946 // Should never happen, log an error and break.
947 // TODO(calin): It's unclear if we should just assert here.
948 // This code was propagated to oat_file_manager from the class linker where it would
949 // throw a NPE. For now, return false which will mark this class loader as unsupported.
950 LOG(ERROR) << "Unexpected null in the dex element list";
951 return false;
952 }
953 ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element);
954 if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) {
955 return false;
956 }
957 }
958 }
959
960 return true;
961}
962
963static bool GetDexFilesFromDexElementsArray(
964 ScopedObjectAccessAlreadyRunnable& soa,
965 Handle<mirror::ObjectArray<mirror::Object>> dex_elements,
966 std::vector<const DexFile*>* out_dex_files) REQUIRES_SHARED(Locks::mutator_lock_) {
967 DCHECK(dex_elements != nullptr);
968
969 ArtField* const cookie_field =
970 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
971 ArtField* const dex_file_field =
972 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
Vladimir Marko0984e482019-03-27 16:41:41 +0000973 const ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(
Calin Juravle57d0acc2017-07-11 17:41:30 -0700974 WellKnownClasses::dalvik_system_DexPathList__Element);
Vladimir Marko0984e482019-03-27 16:41:41 +0000975 const ObjPtr<mirror::Class> dexfile_class = soa.Decode<mirror::Class>(
Calin Juravle57d0acc2017-07-11 17:41:30 -0700976 WellKnownClasses::dalvik_system_DexFile);
977
978 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
Vladimir Marko423bebb2019-03-26 15:17:21 +0000979 ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i);
Calin Juravle57d0acc2017-07-11 17:41:30 -0700980 // We can hit a null element here because this is invoked with a partially filled dex_elements
981 // array from DexPathList. DexPathList will open each dex sequentially, each time passing the
982 // list of dex files which were opened before.
983 if (element == nullptr) {
984 continue;
985 }
986
987 // We support this being dalvik.system.DexPathList$Element and dalvik.system.DexFile.
988 // TODO(calin): Code caried over oat_file_manager: supporting both classes seem to be
989 // a historical glitch. All the java code opens dex files using an array of Elements.
990 ObjPtr<mirror::Object> dex_file;
991 if (element_class == element->GetClass()) {
992 dex_file = dex_file_field->GetObject(element);
993 } else if (dexfile_class == element->GetClass()) {
994 dex_file = element;
995 } else {
996 LOG(ERROR) << "Unsupported element in dex_elements: "
997 << mirror::Class::PrettyClass(element->GetClass());
998 return false;
999 }
1000
1001 if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) {
1002 return false;
1003 }
1004 }
1005 return true;
1006}
1007
1008// Adds the `class_loader` info to the `context`.
1009// The dex file present in `dex_elements` array (if not null) will be added at the end of
1010// the classpath.
1011// This method is recursive (w.r.t. the class loader parent) and will stop once it reaches the
1012// BootClassLoader. Note that the class loader chain is expected to be short.
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001013bool ClassLoaderContext::CreateInfoFromClassLoader(
Calin Juravle57d0acc2017-07-11 17:41:30 -07001014 ScopedObjectAccessAlreadyRunnable& soa,
1015 Handle<mirror::ClassLoader> class_loader,
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001016 Handle<mirror::ObjectArray<mirror::Object>> dex_elements,
1017 ClassLoaderInfo* child_info,
1018 bool is_shared_library)
Calin Juravle57d0acc2017-07-11 17:41:30 -07001019 REQUIRES_SHARED(Locks::mutator_lock_) {
1020 if (ClassLinker::IsBootClassLoader(soa, class_loader.Get())) {
1021 // Nothing to do for the boot class loader as we don't add its dex files to the context.
1022 return true;
1023 }
1024
1025 ClassLoaderContext::ClassLoaderType type;
1026 if (IsPathOrDexClassLoader(soa, class_loader)) {
1027 type = kPathClassLoader;
1028 } else if (IsDelegateLastClassLoader(soa, class_loader)) {
1029 type = kDelegateLastClassLoader;
David Brazdil1a9ac532019-03-05 11:57:13 +00001030 } else if (IsInMemoryDexClassLoader(soa, class_loader)) {
1031 type = kInMemoryDexClassLoader;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001032 } else {
1033 LOG(WARNING) << "Unsupported class loader";
1034 return false;
1035 }
1036
1037 // Inspect the class loader for its dex files.
1038 std::vector<const DexFile*> dex_files_loaded;
1039 CollectDexFilesFromSupportedClassLoader(soa, class_loader, &dex_files_loaded);
1040
1041 // If we have a dex_elements array extract its dex elements now.
1042 // This is used in two situations:
1043 // 1) when a new ClassLoader is created DexPathList will open each dex file sequentially
1044 // passing the list of already open dex files each time. This ensures that we see the
1045 // correct context even if the ClassLoader under construction is not fully build.
1046 // 2) when apk splits are loaded on the fly, the framework will load their dex files by
1047 // appending them to the current class loader. When the new code paths are loaded in
1048 // BaseDexClassLoader, the paths already present in the class loader will be passed
1049 // in the dex_elements array.
1050 if (dex_elements != nullptr) {
1051 GetDexFilesFromDexElementsArray(soa, dex_elements, &dex_files_loaded);
1052 }
1053
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001054 ClassLoaderInfo* info = new ClassLoaderContext::ClassLoaderInfo(type);
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001055 // Attach the `ClassLoaderInfo` now, before populating dex files, as only the
1056 // `ClassLoaderContext` knows whether these dex files should be deleted or not.
1057 if (child_info == nullptr) {
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001058 class_loader_chain_.reset(info);
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001059 } else if (is_shared_library) {
1060 child_info->shared_libraries.push_back(std::unique_ptr<ClassLoaderInfo>(info));
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001061 } else {
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001062 child_info->parent.reset(info);
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001063 }
1064
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001065 // Now that `info` is in the chain, populate dex files.
Calin Juravle57d0acc2017-07-11 17:41:30 -07001066 for (const DexFile* dex_file : dex_files_loaded) {
David Brazdil93d339d2019-03-27 09:56:45 +00001067 // Dex location of dex files loaded with InMemoryDexClassLoader is always bogus.
1068 // Use a magic value for the classpath instead.
1069 info->classpath.push_back((type == kInMemoryDexClassLoader)
1070 ? kInMemoryDexClassLoaderDexLocationMagic
1071 : dex_file->GetLocation());
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001072 info->checksums.push_back(dex_file->GetLocationChecksum());
1073 info->opened_dex_files.emplace_back(dex_file);
Calin Juravle57d0acc2017-07-11 17:41:30 -07001074 }
1075
Calin Juravle57d0acc2017-07-11 17:41:30 -07001076 // Note that dex_elements array is null here. The elements are considered to be part of the
1077 // current class loader and are not passed to the parents.
1078 ScopedNullHandle<mirror::ObjectArray<mirror::Object>> null_dex_elements;
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001079
1080 // Add the shared libraries.
1081 StackHandleScope<3> hs(Thread::Current());
1082 ArtField* field =
1083 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders);
1084 ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get());
1085 if (raw_shared_libraries != nullptr) {
1086 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries =
1087 hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>());
1088 MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr);
1089 for (int32_t i = 0; i < shared_libraries->GetLength(); ++i) {
1090 temp_loader.Assign(shared_libraries->Get(i));
1091 if (!CreateInfoFromClassLoader(
1092 soa, temp_loader, null_dex_elements, info, /*is_shared_library=*/ true)) {
1093 return false;
1094 }
1095 }
1096 }
1097
1098 // We created the ClassLoaderInfo for the current loader. Move on to its parent.
1099 Handle<mirror::ClassLoader> parent = hs.NewHandle(class_loader->GetParent());
1100 if (!CreateInfoFromClassLoader(
1101 soa, parent, null_dex_elements, info, /*is_shared_library=*/ false)) {
1102 return false;
1103 }
1104 return true;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001105}
1106
1107std::unique_ptr<ClassLoaderContext> ClassLoaderContext::CreateContextForClassLoader(
1108 jobject class_loader,
1109 jobjectArray dex_elements) {
Calin Juravle3f918642017-07-11 19:04:20 -07001110 CHECK(class_loader != nullptr);
1111
Calin Juravle57d0acc2017-07-11 17:41:30 -07001112 ScopedObjectAccess soa(Thread::Current());
1113 StackHandleScope<2> hs(soa.Self());
1114 Handle<mirror::ClassLoader> h_class_loader =
1115 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader));
1116 Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements =
1117 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements));
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001118 std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext(/*owns_the_dex_files=*/ false));
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001119 if (!result->CreateInfoFromClassLoader(
1120 soa, h_class_loader, h_dex_elements, nullptr, /*is_shared_library=*/ false)) {
Calin Juravle57d0acc2017-07-11 17:41:30 -07001121 return nullptr;
1122 }
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001123 return result;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001124}
1125
Mathieu Chartieradc90862018-05-11 13:03:06 -07001126ClassLoaderContext::VerificationResult ClassLoaderContext::VerifyClassLoaderContextMatch(
1127 const std::string& context_spec,
1128 bool verify_names,
1129 bool verify_checksums) const {
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001130 if (verify_names || verify_checksums) {
1131 DCHECK(dex_files_open_attempted_);
1132 DCHECK(dex_files_open_result_);
1133 }
Calin Juravlec5b215f2017-09-12 14:49:37 -07001134
Calin Juravle3f918642017-07-11 19:04:20 -07001135 ClassLoaderContext expected_context;
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001136 if (!expected_context.Parse(context_spec, verify_checksums)) {
Calin Juravle3f918642017-07-11 19:04:20 -07001137 LOG(WARNING) << "Invalid class loader context: " << context_spec;
Mathieu Chartieradc90862018-05-11 13:03:06 -07001138 return VerificationResult::kMismatch;
Calin Juravle3f918642017-07-11 19:04:20 -07001139 }
1140
Calin Juravlec5b215f2017-09-12 14:49:37 -07001141 // Special shared library contexts always match. They essentially instruct the runtime
1142 // to ignore the class path check because the oat file is known to be loaded in different
1143 // contexts. OatFileManager will further verify if the oat file can be loaded based on the
1144 // collision check.
Mathieu Chartieradc90862018-05-11 13:03:06 -07001145 if (expected_context.special_shared_library_) {
1146 // Special case where we are the only entry in the class path.
Nicolas Geoffray9893c472018-11-13 15:39:53 +00001147 if (class_loader_chain_ != nullptr &&
1148 class_loader_chain_->parent == nullptr &&
1149 class_loader_chain_->classpath.size() == 0) {
Mathieu Chartieradc90862018-05-11 13:03:06 -07001150 return VerificationResult::kVerifies;
1151 }
1152 return VerificationResult::kForcedToSkipChecks;
1153 } else if (special_shared_library_) {
1154 return VerificationResult::kForcedToSkipChecks;
Calin Juravle3f918642017-07-11 19:04:20 -07001155 }
1156
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001157 ClassLoaderInfo* info = class_loader_chain_.get();
1158 ClassLoaderInfo* expected = expected_context.class_loader_chain_.get();
1159 CHECK(info != nullptr);
1160 CHECK(expected != nullptr);
1161 if (!ClassLoaderInfoMatch(*info, *expected, context_spec, verify_names, verify_checksums)) {
Mathieu Chartieradc90862018-05-11 13:03:06 -07001162 return VerificationResult::kMismatch;
Calin Juravle3f918642017-07-11 19:04:20 -07001163 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001164 return VerificationResult::kVerifies;
1165}
Calin Juravle3f918642017-07-11 19:04:20 -07001166
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001167bool ClassLoaderContext::ClassLoaderInfoMatch(
1168 const ClassLoaderInfo& info,
1169 const ClassLoaderInfo& expected_info,
1170 const std::string& context_spec,
1171 bool verify_names,
1172 bool verify_checksums) const {
1173 if (info.type != expected_info.type) {
1174 LOG(WARNING) << "ClassLoaderContext type mismatch"
1175 << ". expected=" << GetClassLoaderTypeName(expected_info.type)
1176 << ", found=" << GetClassLoaderTypeName(info.type)
1177 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1178 return false;
1179 }
1180 if (info.classpath.size() != expected_info.classpath.size()) {
1181 LOG(WARNING) << "ClassLoaderContext classpath size mismatch"
1182 << ". expected=" << expected_info.classpath.size()
1183 << ", found=" << info.classpath.size()
Andreas Gampe7d0f81c2017-07-25 18:25:41 -07001184 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001185 return false;
1186 }
Calin Juravle3f918642017-07-11 19:04:20 -07001187
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001188 if (verify_checksums) {
1189 DCHECK_EQ(info.classpath.size(), info.checksums.size());
1190 DCHECK_EQ(expected_info.classpath.size(), expected_info.checksums.size());
1191 }
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001192
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001193 if (verify_names) {
Calin Juravle3f918642017-07-11 19:04:20 -07001194 for (size_t k = 0; k < info.classpath.size(); k++) {
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001195 // Compute the dex location that must be compared.
1196 // We shouldn't do a naive comparison `info.classpath[k] == expected_info.classpath[k]`
1197 // because even if they refer to the same file, one could be encoded as a relative location
1198 // and the other as an absolute one.
1199 bool is_dex_name_absolute = IsAbsoluteLocation(info.classpath[k]);
1200 bool is_expected_dex_name_absolute = IsAbsoluteLocation(expected_info.classpath[k]);
1201 std::string dex_name;
1202 std::string expected_dex_name;
1203
1204 if (is_dex_name_absolute == is_expected_dex_name_absolute) {
1205 // If both locations are absolute or relative then compare them as they are.
1206 // This is usually the case for: shared libraries and secondary dex files.
1207 dex_name = info.classpath[k];
1208 expected_dex_name = expected_info.classpath[k];
1209 } else if (is_dex_name_absolute) {
1210 // The runtime name is absolute but the compiled name (the expected one) is relative.
1211 // This is the case for split apks which depend on base or on other splits.
1212 dex_name = info.classpath[k];
David Brazdil3e8aae02019-03-26 18:48:02 +00001213 OatFile::ResolveRelativeEncodedDexLocation(info.classpath[k].c_str(),
1214 expected_info.classpath[k],
1215 &expected_dex_name);
Calin Juravle92003fe2017-09-06 02:22:57 +00001216 } else if (is_expected_dex_name_absolute) {
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001217 // The runtime name is relative but the compiled name is absolute.
1218 // There is no expected use case that would end up here as dex files are always loaded
1219 // with their absolute location. However, be tolerant and do the best effort (in case
1220 // there are unexpected new use case...).
David Brazdil3e8aae02019-03-26 18:48:02 +00001221 OatFile::ResolveRelativeEncodedDexLocation(expected_info.classpath[k].c_str(),
1222 info.classpath[k],
1223 &dex_name);
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001224 expected_dex_name = expected_info.classpath[k];
Calin Juravle92003fe2017-09-06 02:22:57 +00001225 } else {
1226 // Both locations are relative. In this case there's not much we can be sure about
1227 // except that the names are the same. The checksum will ensure that the files are
1228 // are same. This should not happen outside testing and manual invocations.
1229 dex_name = info.classpath[k];
1230 expected_dex_name = expected_info.classpath[k];
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001231 }
1232
1233 // Compare the locations.
1234 if (dex_name != expected_dex_name) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001235 LOG(WARNING) << "ClassLoaderContext classpath element mismatch"
Calin Juravle3f918642017-07-11 19:04:20 -07001236 << ". expected=" << expected_info.classpath[k]
Andreas Gampe7d0f81c2017-07-25 18:25:41 -07001237 << ", found=" << info.classpath[k]
1238 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001239 return false;
Calin Juravle3f918642017-07-11 19:04:20 -07001240 }
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001241
1242 // Compare the checksums.
Calin Juravle3f918642017-07-11 19:04:20 -07001243 if (info.checksums[k] != expected_info.checksums[k]) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001244 LOG(WARNING) << "ClassLoaderContext classpath element checksum mismatch"
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001245 << ". expected=" << expected_info.checksums[k]
1246 << ", found=" << info.checksums[k]
1247 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001248 return false;
Calin Juravle3f918642017-07-11 19:04:20 -07001249 }
1250 }
1251 }
Calin Juravle3f918642017-07-11 19:04:20 -07001252
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001253 if (info.shared_libraries.size() != expected_info.shared_libraries.size()) {
1254 LOG(WARNING) << "ClassLoaderContext shared library size mismatch. "
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001255 << "Expected=" << expected_info.shared_libraries.size()
1256 << ", found=" << info.shared_libraries.size()
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001257 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1258 return false;
Calin Juravlec79470d2017-07-12 17:37:42 -07001259 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001260 for (size_t i = 0; i < info.shared_libraries.size(); ++i) {
1261 if (!ClassLoaderInfoMatch(*info.shared_libraries[i].get(),
1262 *expected_info.shared_libraries[i].get(),
1263 context_spec,
1264 verify_names,
1265 verify_checksums)) {
1266 return false;
1267 }
1268 }
1269 if (info.parent.get() == nullptr) {
1270 if (expected_info.parent.get() != nullptr) {
1271 LOG(WARNING) << "ClassLoaderContext parent mismatch. "
1272 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1273 return false;
1274 }
1275 return true;
1276 } else if (expected_info.parent.get() == nullptr) {
1277 LOG(WARNING) << "ClassLoaderContext parent mismatch. "
1278 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1279 return false;
1280 } else {
1281 return ClassLoaderInfoMatch(*info.parent.get(),
1282 *expected_info.parent.get(),
1283 context_spec,
1284 verify_names,
1285 verify_checksums);
1286 }
Calin Juravlec79470d2017-07-12 17:37:42 -07001287}
1288
Calin Juravle87e2cb62017-06-13 21:48:45 -07001289} // namespace art