blob: 8949f5a32461a1d76d99a462b391c79e71c2b7e6 [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"
25#include "base/stl_util.h"
26#include "class_linker.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070027#include "class_loader_utils.h"
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +000028#include "class_root.h"
David Sehr013fd802018-01-11 22:55:24 -080029#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080030#include "dex/dex_file.h"
31#include "dex/dex_file_loader.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070032#include "handle_scope-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010033#include "jni/jni_internal.h"
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +000034#include "mirror/object_array-alloc-inl.h"
35#include "nativehelper/scoped_local_ref.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070036#include "oat_file_assistant.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070037#include "obj_ptr-inl.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070038#include "runtime.h"
39#include "scoped_thread_state_change-inl.h"
40#include "thread.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070041#include "well_known_classes.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070042
43namespace art {
44
45static constexpr char kPathClassLoaderString[] = "PCL";
46static constexpr char kDelegateLastClassLoaderString[] = "DLC";
David Brazdil1a9ac532019-03-05 11:57:13 +000047static constexpr char kInMemoryDexClassLoaderString[] = "IMC";
Calin Juravle87e2cb62017-06-13 21:48:45 -070048static constexpr char kClassLoaderOpeningMark = '[';
49static constexpr char kClassLoaderClosingMark = ']';
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000050static constexpr char kClassLoaderSharedLibraryOpeningMark = '{';
51static constexpr char kClassLoaderSharedLibraryClosingMark = '}';
52static constexpr char kClassLoaderSharedLibrarySeparator = '#';
Calin Juravle7b0648a2017-07-07 18:40:50 -070053static constexpr char kClassLoaderSeparator = ';';
54static constexpr char kClasspathSeparator = ':';
55static constexpr char kDexFileChecksumSeparator = '*';
David Brazdil1a1398e2019-03-25 17:04:47 +000056static constexpr char kInMemoryDexClassLoaderClasspathMagic[] = "<unknown>";
Calin Juravle87e2cb62017-06-13 21:48:45 -070057
58ClassLoaderContext::ClassLoaderContext()
59 : special_shared_library_(false),
60 dex_files_open_attempted_(false),
Calin Juravle57d0acc2017-07-11 17:41:30 -070061 dex_files_open_result_(false),
Calin Juravle41acdc12017-07-18 17:45:32 -070062 owns_the_dex_files_(true) {}
Calin Juravle57d0acc2017-07-11 17:41:30 -070063
64ClassLoaderContext::ClassLoaderContext(bool owns_the_dex_files)
65 : special_shared_library_(false),
66 dex_files_open_attempted_(true),
67 dex_files_open_result_(true),
68 owns_the_dex_files_(owns_the_dex_files) {}
69
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000070// Utility method to add parent and shared libraries of `info` into
71// the `work_list`.
72static void AddToWorkList(
73 ClassLoaderContext::ClassLoaderInfo* info,
74 std::vector<ClassLoaderContext::ClassLoaderInfo*>& work_list) {
75 if (info->parent != nullptr) {
76 work_list.push_back(info->parent.get());
77 }
78 for (size_t i = 0; i < info->shared_libraries.size(); ++i) {
79 work_list.push_back(info->shared_libraries[i].get());
80 }
81}
82
Calin Juravle57d0acc2017-07-11 17:41:30 -070083ClassLoaderContext::~ClassLoaderContext() {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000084 if (!owns_the_dex_files_ && class_loader_chain_ != nullptr) {
Calin Juravle57d0acc2017-07-11 17:41:30 -070085 // If the context does not own the dex/oat files release the unique pointers to
86 // make sure we do not de-allocate them.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000087 std::vector<ClassLoaderInfo*> work_list;
88 work_list.push_back(class_loader_chain_.get());
89 while (!work_list.empty()) {
90 ClassLoaderInfo* info = work_list.back();
91 work_list.pop_back();
92 for (std::unique_ptr<OatFile>& oat_file : info->opened_oat_files) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -070093 oat_file.release(); // NOLINT b/117926937
Calin Juravle57d0acc2017-07-11 17:41:30 -070094 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000095 for (std::unique_ptr<const DexFile>& dex_file : info->opened_dex_files) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -070096 dex_file.release(); // NOLINT b/117926937
Calin Juravle57d0acc2017-07-11 17:41:30 -070097 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000098 AddToWorkList(info, work_list);
Calin Juravle57d0acc2017-07-11 17:41:30 -070099 }
100 }
101}
Calin Juravle87e2cb62017-06-13 21:48:45 -0700102
Calin Juravle19915892017-08-03 17:10:36 +0000103std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Default() {
104 return Create("");
105}
106
Calin Juravle87e2cb62017-06-13 21:48:45 -0700107std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Create(const std::string& spec) {
108 std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext());
109 if (result->Parse(spec)) {
110 return result;
111 } else {
112 return nullptr;
113 }
114}
115
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000116static size_t FindMatchingSharedLibraryCloseMarker(const std::string& spec,
117 size_t shared_library_open_index) {
118 // Counter of opened shared library marker we've encountered so far.
119 uint32_t counter = 1;
120 // The index at which we're operating in the loop.
121 uint32_t string_index = shared_library_open_index + 1;
122 size_t shared_library_close = std::string::npos;
123 while (counter != 0) {
124 shared_library_close =
125 spec.find_first_of(kClassLoaderSharedLibraryClosingMark, string_index);
126 size_t shared_library_open =
127 spec.find_first_of(kClassLoaderSharedLibraryOpeningMark, string_index);
128 if (shared_library_close == std::string::npos) {
129 // No matching closing marker. Return an error.
130 break;
131 }
132
133 if ((shared_library_open == std::string::npos) ||
134 (shared_library_close < shared_library_open)) {
135 // We have seen a closing marker. Decrement the counter.
136 --counter;
137 // Move the search index forward.
138 string_index = shared_library_close + 1;
139 } else {
140 // New nested opening marker. Increment the counter and move the search
141 // index after the marker.
142 ++counter;
143 string_index = shared_library_open + 1;
144 }
145 }
146 return shared_library_close;
147}
148
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000149// The expected format is:
150// "ClassLoaderType1[ClasspathElem1*Checksum1:ClasspathElem2*Checksum2...]{ClassLoaderType2[...]}".
Calin Juravle7b0648a2017-07-07 18:40:50 -0700151// The checksum part of the format is expected only if parse_cheksums is true.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000152std::unique_ptr<ClassLoaderContext::ClassLoaderInfo> ClassLoaderContext::ParseClassLoaderSpec(
153 const std::string& class_loader_spec,
154 bool parse_checksums) {
155 ClassLoaderType class_loader_type = ExtractClassLoaderType(class_loader_spec);
156 if (class_loader_type == kInvalidClassLoader) {
157 return nullptr;
158 }
David Brazdil1a9ac532019-03-05 11:57:13 +0000159
160 // InMemoryDexClassLoader's dex location is always bogus. Special-case it.
161 if (class_loader_type == kInMemoryDexClassLoader) {
162 if (parse_checksums) {
163 // Make sure that OpenDexFiles() will never be attempted on this context
164 // because the dex locations of IMC do not correspond to real files.
165 CHECK(!dex_files_open_attempted_ || !dex_files_open_result_)
166 << "Parsing spec not supported when context created from a ClassLoader object";
167 dex_files_open_attempted_ = true;
168 dex_files_open_result_ = false;
169 } else {
170 // Checksums are not provided and dex locations themselves have no meaning
171 // (although we keep them in the spec to simplify parsing). Treat this as
172 // an unknown class loader.
David Brazdil1a1398e2019-03-25 17:04:47 +0000173 // We can hit this case if dex2oat is invoked with a spec containing IMC.
174 // Because the dex file data is only available at runtime, we cannot proceed.
David Brazdil1a9ac532019-03-05 11:57:13 +0000175 return nullptr;
176 }
177 }
178
Calin Juravle87e2cb62017-06-13 21:48:45 -0700179 const char* class_loader_type_str = GetClassLoaderTypeName(class_loader_type);
180 size_t type_str_size = strlen(class_loader_type_str);
181
182 CHECK_EQ(0, class_loader_spec.compare(0, type_str_size, class_loader_type_str));
183
184 // Check the opening and closing markers.
185 if (class_loader_spec[type_str_size] != kClassLoaderOpeningMark) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000186 return nullptr;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700187 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000188 if ((class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderClosingMark) &&
189 (class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderSharedLibraryClosingMark)) {
190 return nullptr;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700191 }
192
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000193 size_t closing_index = class_loader_spec.find_first_of(kClassLoaderClosingMark);
194
Calin Juravle87e2cb62017-06-13 21:48:45 -0700195 // At this point we know the format is ok; continue and extract the classpath.
196 // Note that class loaders with an empty class path are allowed.
197 std::string classpath = class_loader_spec.substr(type_str_size + 1,
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000198 closing_index - type_str_size - 1);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700199
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000200 std::unique_ptr<ClassLoaderInfo> info(new ClassLoaderInfo(class_loader_type));
Calin Juravle7b0648a2017-07-07 18:40:50 -0700201
202 if (!parse_checksums) {
David Brazdil1a1398e2019-03-25 17:04:47 +0000203 DCHECK(class_loader_type != kInMemoryDexClassLoader);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000204 Split(classpath, kClasspathSeparator, &info->classpath);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700205 } else {
206 std::vector<std::string> classpath_elements;
207 Split(classpath, kClasspathSeparator, &classpath_elements);
208 for (const std::string& element : classpath_elements) {
209 std::vector<std::string> dex_file_with_checksum;
210 Split(element, kDexFileChecksumSeparator, &dex_file_with_checksum);
211 if (dex_file_with_checksum.size() != 2) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000212 return nullptr;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700213 }
214 uint32_t checksum = 0;
Tom Cherry7bc8e8f2018-10-05 14:34:13 -0700215 if (!android::base::ParseUint(dex_file_with_checksum[1].c_str(), &checksum)) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000216 return nullptr;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700217 }
David Brazdil1a1398e2019-03-25 17:04:47 +0000218 if ((class_loader_type == kInMemoryDexClassLoader) &&
219 (dex_file_with_checksum[0] != kInMemoryDexClassLoaderClasspathMagic)) {
220 return nullptr;
221 }
David Brazdil1a9ac532019-03-05 11:57:13 +0000222
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000223 info->classpath.push_back(dex_file_with_checksum[0]);
224 info->checksums.push_back(checksum);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700225 }
226 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700227
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000228 if ((class_loader_spec[class_loader_spec.length() - 1] == kClassLoaderSharedLibraryClosingMark) &&
229 (class_loader_spec[class_loader_spec.length() - 2] != kClassLoaderSharedLibraryOpeningMark)) {
230 // Non-empty list of shared libraries.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000231 size_t start_index = class_loader_spec.find_first_of(kClassLoaderSharedLibraryOpeningMark);
232 if (start_index == std::string::npos) {
233 return nullptr;
234 }
235 std::string shared_libraries_spec =
236 class_loader_spec.substr(start_index + 1, class_loader_spec.length() - start_index - 2);
237 std::vector<std::string> shared_libraries;
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000238 size_t cursor = 0;
239 while (cursor != shared_libraries_spec.length()) {
240 size_t shared_library_separator =
241 shared_libraries_spec.find_first_of(kClassLoaderSharedLibrarySeparator, cursor);
242 size_t shared_library_open =
243 shared_libraries_spec.find_first_of(kClassLoaderSharedLibraryOpeningMark, cursor);
244 std::string shared_library_spec;
245 if (shared_library_separator == std::string::npos) {
246 // Only one shared library, for example:
247 // PCL[...]
248 shared_library_spec =
249 shared_libraries_spec.substr(cursor, shared_libraries_spec.length() - cursor);
250 cursor = shared_libraries_spec.length();
251 } else if ((shared_library_open == std::string::npos) ||
252 (shared_library_open > shared_library_separator)) {
253 // We found a shared library without nested shared libraries, for example:
254 // PCL[...]#PCL[...]{...}
255 shared_library_spec =
256 shared_libraries_spec.substr(cursor, shared_library_separator - cursor);
257 cursor = shared_library_separator + 1;
258 } else {
259 // The shared library contains nested shared libraries. Find the matching closing shared
260 // marker for it.
261 size_t closing_marker =
262 FindMatchingSharedLibraryCloseMarker(shared_libraries_spec, shared_library_open);
263 if (closing_marker == std::string::npos) {
264 // No matching closing marker, return an error.
265 return nullptr;
266 }
267 shared_library_spec = shared_libraries_spec.substr(cursor, closing_marker + 1 - cursor);
268 cursor = closing_marker + 1;
269 if (cursor != shared_libraries_spec.length() &&
270 shared_libraries_spec[cursor] == kClassLoaderSharedLibrarySeparator) {
271 // Pass the shared library separator marker.
272 ++cursor;
273 }
274 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000275 std::unique_ptr<ClassLoaderInfo> shared_library(
276 ParseInternal(shared_library_spec, parse_checksums));
277 if (shared_library == nullptr) {
278 return nullptr;
279 }
280 info->shared_libraries.push_back(std::move(shared_library));
281 }
282 }
283
284 return info;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700285}
286
287// Extracts the class loader type from the given spec.
288// Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not
289// recognized.
290ClassLoaderContext::ClassLoaderType
291ClassLoaderContext::ExtractClassLoaderType(const std::string& class_loader_spec) {
David Brazdil1a9ac532019-03-05 11:57:13 +0000292 const ClassLoaderType kValidTypes[] = { kPathClassLoader,
293 kDelegateLastClassLoader,
294 kInMemoryDexClassLoader };
Calin Juravle87e2cb62017-06-13 21:48:45 -0700295 for (const ClassLoaderType& type : kValidTypes) {
296 const char* type_str = GetClassLoaderTypeName(type);
297 if (class_loader_spec.compare(0, strlen(type_str), type_str) == 0) {
298 return type;
299 }
300 }
301 return kInvalidClassLoader;
302}
303
304// The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]...
305// ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader).
306// ClasspathElem is the path of dex/jar/apk file.
Calin Juravle7b0648a2017-07-07 18:40:50 -0700307bool ClassLoaderContext::Parse(const std::string& spec, bool parse_checksums) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700308 if (spec.empty()) {
Calin Juravle1a509c82017-07-24 16:51:21 -0700309 // By default we load the dex files in a PathClassLoader.
310 // So an empty spec is equivalent to an empty PathClassLoader (this happens when running
311 // tests)
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000312 class_loader_chain_.reset(new ClassLoaderInfo(kPathClassLoader));
Calin Juravle7b0648a2017-07-07 18:40:50 -0700313 return true;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700314 }
Calin Juravle7b0648a2017-07-07 18:40:50 -0700315
Calin Juravle87e2cb62017-06-13 21:48:45 -0700316 // Stop early if we detect the special shared library, which may be passed as the classpath
317 // for dex2oat when we want to skip the shared libraries check.
318 if (spec == OatFile::kSpecialSharedLibrary) {
319 LOG(INFO) << "The ClassLoaderContext is a special shared library.";
320 special_shared_library_ = true;
321 return true;
322 }
323
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000324 CHECK(class_loader_chain_ == nullptr);
325 class_loader_chain_.reset(ParseInternal(spec, parse_checksums));
326 return class_loader_chain_ != nullptr;
327}
Calin Juravle87e2cb62017-06-13 21:48:45 -0700328
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000329ClassLoaderContext::ClassLoaderInfo* ClassLoaderContext::ParseInternal(
330 const std::string& spec, bool parse_checksums) {
331 CHECK(!spec.empty());
332 CHECK_NE(spec, OatFile::kSpecialSharedLibrary);
333 std::string remaining = spec;
334 std::unique_ptr<ClassLoaderInfo> first(nullptr);
335 ClassLoaderInfo* previous_iteration = nullptr;
336 while (!remaining.empty()) {
337 std::string class_loader_spec;
338 size_t first_class_loader_separator = remaining.find_first_of(kClassLoaderSeparator);
339 size_t first_shared_library_open =
340 remaining.find_first_of(kClassLoaderSharedLibraryOpeningMark);
341 if (first_class_loader_separator == std::string::npos) {
342 // Only one class loader, for example:
343 // PCL[...]
344 class_loader_spec = remaining;
345 remaining = "";
346 } else if ((first_shared_library_open == std::string::npos) ||
347 (first_shared_library_open > first_class_loader_separator)) {
348 // We found a class loader spec without shared libraries, for example:
349 // PCL[...];PCL[...]{...}
350 class_loader_spec = remaining.substr(0, first_class_loader_separator);
351 remaining = remaining.substr(first_class_loader_separator + 1,
352 remaining.size() - first_class_loader_separator - 1);
353 } else {
354 // The class loader spec contains shared libraries. Find the matching closing
355 // shared library marker for it.
356
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000357 uint32_t shared_library_close =
358 FindMatchingSharedLibraryCloseMarker(remaining, first_shared_library_open);
359 if (shared_library_close == std::string::npos) {
360 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
361 return nullptr;
362 }
363 class_loader_spec = remaining.substr(0, shared_library_close + 1);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000364
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000365 // Compute the remaining string to analyze.
366 if (remaining.size() == shared_library_close + 1) {
367 remaining = "";
368 } else if ((remaining.size() == shared_library_close + 2) ||
369 (remaining.at(shared_library_close + 1) != kClassLoaderSeparator)) {
370 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
371 return nullptr;
372 } else {
373 remaining = remaining.substr(shared_library_close + 2,
374 remaining.size() - shared_library_close - 2);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000375 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700376 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000377
378 std::unique_ptr<ClassLoaderInfo> info =
379 ParseClassLoaderSpec(class_loader_spec, parse_checksums);
380 if (info == nullptr) {
381 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
382 return nullptr;
383 }
384 if (first == nullptr) {
Andreas Gampe41c911f2018-11-19 11:34:16 -0800385 first = std::move(info);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000386 previous_iteration = first.get();
387 } else {
388 CHECK(previous_iteration != nullptr);
Andreas Gampe41c911f2018-11-19 11:34:16 -0800389 previous_iteration->parent = std::move(info);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000390 previous_iteration = previous_iteration->parent.get();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700391 }
392 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000393 return first.release();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700394}
395
396// Opens requested class path files and appends them to opened_dex_files. If the dex files have
397// been stripped, this opens them from their oat files (which get added to opened_oat_files).
David Brazdil89821862019-03-19 13:57:43 +0000398bool ClassLoaderContext::OpenDexFiles(InstructionSet isa,
399 const std::string& classpath_dir,
400 const std::vector<int>& fds) {
Calin Juravlec5b215f2017-09-12 14:49:37 -0700401 if (dex_files_open_attempted_) {
402 // Do not attempt to re-open the files if we already tried.
403 return dex_files_open_result_;
404 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700405
406 dex_files_open_attempted_ = true;
407 // Assume we can open all dex files. If not, we will set this to false as we go.
408 dex_files_open_result_ = true;
409
410 if (special_shared_library_) {
411 // Nothing to open if the context is a special shared library.
412 return true;
413 }
414
415 // Note that we try to open all dex files even if some fail.
416 // We may get resource-only apks which we cannot load.
417 // TODO(calin): Refine the dex opening interface to be able to tell if an archive contains
418 // no dex files. So that we can distinguish the real failures...
David Sehr013fd802018-01-11 22:55:24 -0800419 const ArtDexFileLoader dex_file_loader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000420 std::vector<ClassLoaderInfo*> work_list;
Nicolas Geoffray9893c472018-11-13 15:39:53 +0000421 CHECK(class_loader_chain_ != nullptr);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000422 work_list.push_back(class_loader_chain_.get());
David Brazdil89821862019-03-19 13:57:43 +0000423 size_t dex_file_index = 0;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000424 while (!work_list.empty()) {
425 ClassLoaderInfo* info = work_list.back();
426 work_list.pop_back();
David Brazdil1a1398e2019-03-25 17:04:47 +0000427 DCHECK(info->type != kInMemoryDexClassLoader) << __FUNCTION__ << " not supported for IMC";
428
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000429 size_t opened_dex_files_index = info->opened_dex_files.size();
430 for (const std::string& cp_elem : info->classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700431 // If path is relative, append it to the provided base directory.
Calin Juravle92003fe2017-09-06 02:22:57 +0000432 std::string location = cp_elem;
433 if (location[0] != '/' && !classpath_dir.empty()) {
Nicolas Geoffray06ffecf2017-11-14 10:31:54 +0000434 location = classpath_dir + (classpath_dir.back() == '/' ? "" : "/") + location;
Calin Juravle821a2592017-08-11 14:33:38 -0700435 }
436
David Brazdil89821862019-03-19 13:57:43 +0000437 // If file descriptors were provided for the class loader context dex paths,
438 // get the descriptor which correponds to this dex path. We assume the `fds`
439 // vector follows the same order as a flattened class loader context.
440 int fd = -1;
441 if (!fds.empty()) {
442 if (dex_file_index >= fds.size()) {
443 LOG(WARNING) << "Number of FDs is smaller than number of dex files in the context";
444 dex_files_open_result_ = false;
445 return false;
446 }
447
448 fd = fds[dex_file_index++];
449 DCHECK_GE(fd, 0);
450 }
451
Calin Juravle87e2cb62017-06-13 21:48:45 -0700452 std::string error_msg;
453 // When opening the dex files from the context we expect their checksum to match their
454 // contents. So pass true to verify_checksum.
David Brazdil89821862019-03-19 13:57:43 +0000455 if (fd < 0) {
456 if (!dex_file_loader.Open(location.c_str(),
457 location.c_str(),
458 Runtime::Current()->IsVerificationEnabled(),
459 /*verify_checksum=*/ true,
460 &error_msg,
461 &info->opened_dex_files)) {
462 // If we fail to open the dex file because it's been stripped, try to
463 // open the dex file from its corresponding oat file.
464 // This could happen when we need to recompile a pre-build whose dex
465 // code has been stripped (for example, if the pre-build is only
466 // quicken and we want to re-compile it speed-profile).
467 // TODO(calin): Use the vdex directly instead of going through the oat file.
468 OatFileAssistant oat_file_assistant(location.c_str(), isa, false);
469 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
470 std::vector<std::unique_ptr<const DexFile>> oat_dex_files;
471 if (oat_file != nullptr &&
472 OatFileAssistant::LoadDexFiles(*oat_file, location, &oat_dex_files)) {
473 info->opened_oat_files.push_back(std::move(oat_file));
474 info->opened_dex_files.insert(info->opened_dex_files.end(),
475 std::make_move_iterator(oat_dex_files.begin()),
476 std::make_move_iterator(oat_dex_files.end()));
477 } else {
478 LOG(WARNING) << "Could not open dex files from location: " << location;
479 dex_files_open_result_ = false;
480 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700481 }
David Brazdil89821862019-03-19 13:57:43 +0000482 } else if (!dex_file_loader.Open(fd,
483 location.c_str(),
484 Runtime::Current()->IsVerificationEnabled(),
485 /*verify_checksum=*/ true,
486 &error_msg,
487 &info->opened_dex_files)) {
488 LOG(WARNING) << "Could not open dex files from fd " << fd << " for location: " << location;
489 dex_files_open_result_ = false;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700490 }
491 }
Calin Juravlec5b215f2017-09-12 14:49:37 -0700492
493 // We finished opening the dex files from the classpath.
494 // Now update the classpath and the checksum with the locations of the dex files.
495 //
496 // We do this because initially the classpath contains the paths of the dex files; and
497 // some of them might be multi-dexes. So in order to have a consistent view we replace all the
498 // file paths with the actual dex locations being loaded.
499 // This will allow the context to VerifyClassLoaderContextMatch which expects or multidex
500 // location in the class paths.
501 // Note that this will also remove the paths that could not be opened.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000502 info->original_classpath = std::move(info->classpath);
503 info->classpath.clear();
504 info->checksums.clear();
505 for (size_t k = opened_dex_files_index; k < info->opened_dex_files.size(); k++) {
506 std::unique_ptr<const DexFile>& dex = info->opened_dex_files[k];
507 info->classpath.push_back(dex->GetLocation());
508 info->checksums.push_back(dex->GetLocationChecksum());
Calin Juravlec5b215f2017-09-12 14:49:37 -0700509 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000510 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700511 }
512
David Brazdil89821862019-03-19 13:57:43 +0000513 // Check that if file descriptors were provided, there were exactly as many
514 // as we have encountered while iterating over this class loader context.
515 if (dex_file_index != fds.size()) {
516 LOG(WARNING) << fds.size() << " FDs provided but only " << dex_file_index
517 << " dex files are in the class loader context";
518 dex_files_open_result_ = false;
519 }
520
Calin Juravle87e2cb62017-06-13 21:48:45 -0700521 return dex_files_open_result_;
522}
523
524bool ClassLoaderContext::RemoveLocationsFromClassPaths(
525 const dchecked_vector<std::string>& locations) {
526 CHECK(!dex_files_open_attempted_)
527 << "RemoveLocationsFromClasspaths cannot be call after OpenDexFiles";
528
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000529 if (class_loader_chain_ == nullptr) {
530 return false;
531 }
532
Calin Juravle87e2cb62017-06-13 21:48:45 -0700533 std::set<std::string> canonical_locations;
534 for (const std::string& location : locations) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700535 canonical_locations.insert(DexFileLoader::GetDexCanonicalLocation(location.c_str()));
Calin Juravle87e2cb62017-06-13 21:48:45 -0700536 }
537 bool removed_locations = false;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000538 std::vector<ClassLoaderInfo*> work_list;
539 work_list.push_back(class_loader_chain_.get());
540 while (!work_list.empty()) {
541 ClassLoaderInfo* info = work_list.back();
542 work_list.pop_back();
543 size_t initial_size = info->classpath.size();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700544 auto kept_it = std::remove_if(
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000545 info->classpath.begin(),
546 info->classpath.end(),
Calin Juravle87e2cb62017-06-13 21:48:45 -0700547 [canonical_locations](const std::string& location) {
548 return ContainsElement(canonical_locations,
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700549 DexFileLoader::GetDexCanonicalLocation(location.c_str()));
Calin Juravle87e2cb62017-06-13 21:48:45 -0700550 });
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000551 info->classpath.erase(kept_it, info->classpath.end());
552 if (initial_size != info->classpath.size()) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700553 removed_locations = true;
554 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000555 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700556 }
557 return removed_locations;
558}
559
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700560std::string ClassLoaderContext::EncodeContextForDex2oat(const std::string& base_dir) const {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700561 return EncodeContext(base_dir, /*for_dex2oat=*/ true, /*stored_context=*/ nullptr);
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700562}
563
Mathieu Chartierc4440772018-04-16 14:40:56 -0700564std::string ClassLoaderContext::EncodeContextForOatFile(const std::string& base_dir,
565 ClassLoaderContext* stored_context) const {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700566 return EncodeContext(base_dir, /*for_dex2oat=*/ false, stored_context);
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700567}
568
569std::string ClassLoaderContext::EncodeContext(const std::string& base_dir,
Mathieu Chartierc4440772018-04-16 14:40:56 -0700570 bool for_dex2oat,
571 ClassLoaderContext* stored_context) const {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700572 CheckDexFilesOpened("EncodeContextForOatFile");
573 if (special_shared_library_) {
574 return OatFile::kSpecialSharedLibrary;
575 }
576
Mathieu Chartierc4440772018-04-16 14:40:56 -0700577 if (stored_context != nullptr) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000578 DCHECK_EQ(GetParentChainSize(), stored_context->GetParentChainSize());
Mathieu Chartierc4440772018-04-16 14:40:56 -0700579 }
580
Calin Juravle7b0648a2017-07-07 18:40:50 -0700581 std::ostringstream out;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000582 if (class_loader_chain_ == nullptr) {
Calin Juravle1a509c82017-07-24 16:51:21 -0700583 // We can get in this situation if the context was created with a class path containing the
584 // source dex files which were later removed (happens during run-tests).
585 out << GetClassLoaderTypeName(kPathClassLoader)
586 << kClassLoaderOpeningMark
587 << kClassLoaderClosingMark;
588 return out.str();
589 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700590
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000591 EncodeContextInternal(
592 *class_loader_chain_,
593 base_dir,
594 for_dex2oat,
595 (stored_context == nullptr ? nullptr : stored_context->class_loader_chain_.get()),
596 out);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700597 return out.str();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700598}
599
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000600void ClassLoaderContext::EncodeContextInternal(const ClassLoaderInfo& info,
601 const std::string& base_dir,
602 bool for_dex2oat,
603 ClassLoaderInfo* stored_info,
604 std::ostringstream& out) const {
605 out << GetClassLoaderTypeName(info.type);
606 out << kClassLoaderOpeningMark;
607 std::set<std::string> seen_locations;
608 SafeMap<std::string, std::string> remap;
609 if (stored_info != nullptr) {
610 for (size_t k = 0; k < info.original_classpath.size(); ++k) {
611 // Note that we don't care if the same name appears twice.
612 remap.Put(info.original_classpath[k], stored_info->classpath[k]);
613 }
614 }
615 for (size_t k = 0; k < info.opened_dex_files.size(); k++) {
616 const std::unique_ptr<const DexFile>& dex_file = info.opened_dex_files[k];
617 if (for_dex2oat) {
618 // dex2oat only needs the base location. It cannot accept multidex locations.
619 // So ensure we only add each file once.
620 bool new_insert = seen_locations.insert(
621 DexFileLoader::GetBaseLocation(dex_file->GetLocation())).second;
622 if (!new_insert) {
623 continue;
624 }
625 }
626 std::string location = dex_file->GetLocation();
627 // If there is a stored class loader remap, fix up the multidex strings.
628 if (!remap.empty()) {
629 std::string base_dex_location = DexFileLoader::GetBaseLocation(location);
630 auto it = remap.find(base_dex_location);
631 CHECK(it != remap.end()) << base_dex_location;
632 location = it->second + DexFileLoader::GetMultiDexSuffix(location);
633 }
634 if (k > 0) {
635 out << kClasspathSeparator;
636 }
David Brazdil1a1398e2019-03-25 17:04:47 +0000637 if (info.type == kInMemoryDexClassLoader) {
638 out << kInMemoryDexClassLoaderClasspathMagic;
639 } else if (!base_dir.empty() && location.substr(0, base_dir.length()) == base_dir) {
640 // Find paths that were relative and convert them back from absolute.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000641 out << location.substr(base_dir.length() + 1).c_str();
642 } else {
643 out << location.c_str();
644 }
645 // dex2oat does not need the checksums.
646 if (!for_dex2oat) {
647 out << kDexFileChecksumSeparator;
648 out << dex_file->GetLocationChecksum();
649 }
650 }
651 out << kClassLoaderClosingMark;
652
653 if (!info.shared_libraries.empty()) {
654 out << kClassLoaderSharedLibraryOpeningMark;
655 for (uint32_t i = 0; i < info.shared_libraries.size(); ++i) {
656 if (i > 0) {
657 out << kClassLoaderSharedLibrarySeparator;
658 }
659 EncodeContextInternal(
660 *info.shared_libraries[i].get(),
661 base_dir,
662 for_dex2oat,
663 (stored_info == nullptr ? nullptr : stored_info->shared_libraries[i].get()),
664 out);
665 }
666 out << kClassLoaderSharedLibraryClosingMark;
667 }
668 if (info.parent != nullptr) {
669 out << kClassLoaderSeparator;
670 EncodeContextInternal(
671 *info.parent.get(),
672 base_dir,
673 for_dex2oat,
674 (stored_info == nullptr ? nullptr : stored_info->parent.get()),
675 out);
676 }
677}
678
679// Returns the WellKnownClass for the given class loader type.
680static jclass GetClassLoaderClass(ClassLoaderContext::ClassLoaderType type) {
681 switch (type) {
682 case ClassLoaderContext::kPathClassLoader:
683 return WellKnownClasses::dalvik_system_PathClassLoader;
684 case ClassLoaderContext::kDelegateLastClassLoader:
685 return WellKnownClasses::dalvik_system_DelegateLastClassLoader;
David Brazdil1a9ac532019-03-05 11:57:13 +0000686 case ClassLoaderContext::kInMemoryDexClassLoader:
687 return WellKnownClasses::dalvik_system_InMemoryDexClassLoader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000688 case ClassLoaderContext::kInvalidClassLoader: break; // will fail after the switch.
689 }
690 LOG(FATAL) << "Invalid class loader type " << type;
691 UNREACHABLE();
692}
693
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000694static std::string FlattenClasspath(const std::vector<std::string>& classpath) {
695 return android::base::Join(classpath, ':');
696}
697
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000698static ObjPtr<mirror::ClassLoader> CreateClassLoaderInternal(
699 Thread* self,
700 ScopedObjectAccess& soa,
701 const ClassLoaderContext::ClassLoaderInfo& info,
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000702 bool for_shared_library,
703 VariableSizedHandleScope& map_scope,
704 std::map<std::string, Handle<mirror::ClassLoader>>& canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000705 bool add_compilation_sources,
706 const std::vector<const DexFile*>& compilation_sources)
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000707 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000708 if (for_shared_library) {
709 // Check if the shared library has already been created.
710 auto search = canonicalized_libraries.find(FlattenClasspath(info.classpath));
711 if (search != canonicalized_libraries.end()) {
712 return search->second.Get();
713 }
714 }
715
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000716 StackHandleScope<3> hs(self);
717 MutableHandle<mirror::ObjectArray<mirror::ClassLoader>> libraries(
718 hs.NewHandle<mirror::ObjectArray<mirror::ClassLoader>>(nullptr));
719
720 if (!info.shared_libraries.empty()) {
721 libraries.Assign(mirror::ObjectArray<mirror::ClassLoader>::Alloc(
722 self,
723 GetClassRoot<mirror::ObjectArray<mirror::ClassLoader>>(),
724 info.shared_libraries.size()));
725 for (uint32_t i = 0; i < info.shared_libraries.size(); ++i) {
726 // We should only add the compilation sources to the first class loader.
727 libraries->Set(i,
728 CreateClassLoaderInternal(
729 self,
730 soa,
731 *info.shared_libraries[i].get(),
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000732 /* for_shared_library= */ true,
733 map_scope,
734 canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000735 /* add_compilation_sources= */ false,
736 compilation_sources));
737 }
738 }
739
740 MutableHandle<mirror::ClassLoader> parent = hs.NewHandle<mirror::ClassLoader>(nullptr);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000741 if (info.parent != nullptr) {
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000742 // We should only add the compilation sources to the first class loader.
743 parent.Assign(CreateClassLoaderInternal(
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000744 self,
745 soa,
746 *info.parent.get(),
747 /* for_shared_library= */ false,
748 map_scope,
749 canonicalized_libraries,
750 /* add_compilation_sources= */ false,
751 compilation_sources));
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000752 }
753 std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector(
754 info.opened_dex_files);
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000755 if (add_compilation_sources) {
756 // For the first class loader, its classpath comes first, followed by compilation sources.
757 // This ensures that whenever we need to resolve classes from it the classpath elements
758 // come first.
759 class_path_files.insert(class_path_files.end(),
760 compilation_sources.begin(),
761 compilation_sources.end());
762 }
763 Handle<mirror::Class> loader_class = hs.NewHandle<mirror::Class>(
764 soa.Decode<mirror::Class>(GetClassLoaderClass(info.type)));
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000765 ObjPtr<mirror::ClassLoader> loader =
766 Runtime::Current()->GetClassLinker()->CreateWellKnownClassLoader(
767 self,
768 class_path_files,
769 loader_class,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000770 parent,
771 libraries);
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000772 if (for_shared_library) {
773 canonicalized_libraries[FlattenClasspath(info.classpath)] =
774 map_scope.NewHandle<mirror::ClassLoader>(loader);
775 }
776 return loader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000777}
778
Calin Juravle87e2cb62017-06-13 21:48:45 -0700779jobject ClassLoaderContext::CreateClassLoader(
780 const std::vector<const DexFile*>& compilation_sources) const {
781 CheckDexFilesOpened("CreateClassLoader");
782
783 Thread* self = Thread::Current();
784 ScopedObjectAccess soa(self);
785
Calin Juravlec79470d2017-07-12 17:37:42 -0700786 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700787
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000788 if (class_loader_chain_ == nullptr) {
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000789 CHECK(special_shared_library_);
Calin Juravlec79470d2017-07-12 17:37:42 -0700790 return class_linker->CreatePathClassLoader(self, compilation_sources);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700791 }
792
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000793 // Create a map of canonicalized shared libraries. As we're holding objects,
794 // we're creating a variable size handle scope to put handles in the map.
795 VariableSizedHandleScope map_scope(self);
796 std::map<std::string, Handle<mirror::ClassLoader>> canonicalized_libraries;
797
798 // Create the class loader.
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000799 ObjPtr<mirror::ClassLoader> loader =
800 CreateClassLoaderInternal(self,
801 soa,
802 *class_loader_chain_.get(),
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000803 /* for_shared_library= */ false,
804 map_scope,
805 canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000806 /* add_compilation_sources= */ true,
807 compilation_sources);
808 // Make it a global ref and return.
809 ScopedLocalRef<jobject> local_ref(
810 soa.Env(), soa.Env()->AddLocalReference<jobject>(loader));
811 return soa.Env()->NewGlobalRef(local_ref.get());
Calin Juravle87e2cb62017-06-13 21:48:45 -0700812}
813
814std::vector<const DexFile*> ClassLoaderContext::FlattenOpenedDexFiles() const {
815 CheckDexFilesOpened("FlattenOpenedDexFiles");
816
817 std::vector<const DexFile*> result;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000818 if (class_loader_chain_ == nullptr) {
819 return result;
820 }
821 std::vector<ClassLoaderInfo*> work_list;
822 work_list.push_back(class_loader_chain_.get());
823 while (!work_list.empty()) {
824 ClassLoaderInfo* info = work_list.back();
825 work_list.pop_back();
826 for (const std::unique_ptr<const DexFile>& dex_file : info->opened_dex_files) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700827 result.push_back(dex_file.get());
828 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000829 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700830 }
831 return result;
832}
833
David Brazdil89821862019-03-19 13:57:43 +0000834std::string ClassLoaderContext::FlattenDexPaths() const {
835 if (class_loader_chain_ == nullptr) {
836 return "";
837 }
838
839 std::vector<std::string> result;
840 std::vector<ClassLoaderInfo*> work_list;
841 work_list.push_back(class_loader_chain_.get());
842 while (!work_list.empty()) {
843 ClassLoaderInfo* info = work_list.back();
844 work_list.pop_back();
845 for (const std::string& dex_path : info->classpath) {
846 result.push_back(dex_path);
847 }
848 AddToWorkList(info, work_list);
849 }
850 return FlattenClasspath(result);
851}
852
Calin Juravle87e2cb62017-06-13 21:48:45 -0700853const char* ClassLoaderContext::GetClassLoaderTypeName(ClassLoaderType type) {
854 switch (type) {
855 case kPathClassLoader: return kPathClassLoaderString;
856 case kDelegateLastClassLoader: return kDelegateLastClassLoaderString;
David Brazdil1a9ac532019-03-05 11:57:13 +0000857 case kInMemoryDexClassLoader: return kInMemoryDexClassLoaderString;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700858 default:
859 LOG(FATAL) << "Invalid class loader type " << type;
860 UNREACHABLE();
861 }
862}
863
864void ClassLoaderContext::CheckDexFilesOpened(const std::string& calling_method) const {
865 CHECK(dex_files_open_attempted_)
866 << "Dex files were not successfully opened before the call to " << calling_method
867 << "attempt=" << dex_files_open_attempted_ << ", result=" << dex_files_open_result_;
868}
Calin Juravle7b0648a2017-07-07 18:40:50 -0700869
Calin Juravle57d0acc2017-07-11 17:41:30 -0700870// Collects the dex files from the give Java dex_file object. Only the dex files with
871// at least 1 class are collected. If a null java_dex_file is passed this method does nothing.
872static bool CollectDexFilesFromJavaDexFile(ObjPtr<mirror::Object> java_dex_file,
873 ArtField* const cookie_field,
874 std::vector<const DexFile*>* out_dex_files)
875 REQUIRES_SHARED(Locks::mutator_lock_) {
876 if (java_dex_file == nullptr) {
877 return true;
878 }
879 // On the Java side, the dex files are stored in the cookie field.
880 mirror::LongArray* long_array = cookie_field->GetObject(java_dex_file)->AsLongArray();
881 if (long_array == nullptr) {
882 // This should never happen so log a warning.
883 LOG(ERROR) << "Unexpected null cookie";
884 return false;
885 }
886 int32_t long_array_size = long_array->GetLength();
887 // Index 0 from the long array stores the oat file. The dex files start at index 1.
888 for (int32_t j = 1; j < long_array_size; ++j) {
Vladimir Marko78baed52018-10-11 10:44:58 +0100889 const DexFile* cp_dex_file =
890 reinterpret_cast64<const DexFile*>(long_array->GetWithoutChecks(j));
Calin Juravle57d0acc2017-07-11 17:41:30 -0700891 if (cp_dex_file != nullptr && cp_dex_file->NumClassDefs() > 0) {
892 // TODO(calin): It's unclear why the dex files with no classes are skipped here and when
893 // cp_dex_file can be null.
894 out_dex_files->push_back(cp_dex_file);
895 }
896 }
897 return true;
898}
899
900// Collects all the dex files loaded by the given class loader.
901// Returns true for success or false if an unexpected state is discovered (e.g. a null dex cookie,
902// a null list of dex elements or a null dex element).
903static bool CollectDexFilesFromSupportedClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
904 Handle<mirror::ClassLoader> class_loader,
905 std::vector<const DexFile*>* out_dex_files)
906 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil1a9ac532019-03-05 11:57:13 +0000907 CHECK(IsPathOrDexClassLoader(soa, class_loader) ||
908 IsDelegateLastClassLoader(soa, class_loader) ||
909 IsInMemoryDexClassLoader(soa, class_loader));
Calin Juravle57d0acc2017-07-11 17:41:30 -0700910
911 // All supported class loaders inherit from BaseDexClassLoader.
912 // We need to get the DexPathList and loop through it.
913 ArtField* const cookie_field =
914 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
915 ArtField* const dex_file_field =
916 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
917 ObjPtr<mirror::Object> dex_path_list =
918 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)->
919 GetObject(class_loader.Get());
920 CHECK(cookie_field != nullptr);
921 CHECK(dex_file_field != nullptr);
922 if (dex_path_list == nullptr) {
923 // This may be null if the current class loader is under construction and it does not
924 // have its fields setup yet.
925 return true;
926 }
927 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
928 ObjPtr<mirror::Object> dex_elements_obj =
929 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)->
930 GetObject(dex_path_list);
931 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
932 // at the mCookie which is a DexFile vector.
933 if (dex_elements_obj == nullptr) {
934 // TODO(calin): It's unclear if we should just assert here. For now be prepared for the worse
935 // and assume we have no elements.
936 return true;
937 } else {
938 StackHandleScope<1> hs(soa.Self());
939 Handle<mirror::ObjectArray<mirror::Object>> dex_elements(
940 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()));
941 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
942 mirror::Object* element = dex_elements->GetWithoutChecks(i);
943 if (element == nullptr) {
944 // Should never happen, log an error and break.
945 // TODO(calin): It's unclear if we should just assert here.
946 // This code was propagated to oat_file_manager from the class linker where it would
947 // throw a NPE. For now, return false which will mark this class loader as unsupported.
948 LOG(ERROR) << "Unexpected null in the dex element list";
949 return false;
950 }
951 ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element);
952 if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) {
953 return false;
954 }
955 }
956 }
957
958 return true;
959}
960
961static bool GetDexFilesFromDexElementsArray(
962 ScopedObjectAccessAlreadyRunnable& soa,
963 Handle<mirror::ObjectArray<mirror::Object>> dex_elements,
964 std::vector<const DexFile*>* out_dex_files) REQUIRES_SHARED(Locks::mutator_lock_) {
965 DCHECK(dex_elements != nullptr);
966
967 ArtField* const cookie_field =
968 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
969 ArtField* const dex_file_field =
970 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
971 ObjPtr<mirror::Class> const element_class = soa.Decode<mirror::Class>(
972 WellKnownClasses::dalvik_system_DexPathList__Element);
973 ObjPtr<mirror::Class> const dexfile_class = soa.Decode<mirror::Class>(
974 WellKnownClasses::dalvik_system_DexFile);
975
976 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
977 mirror::Object* element = dex_elements->GetWithoutChecks(i);
978 // We can hit a null element here because this is invoked with a partially filled dex_elements
979 // array from DexPathList. DexPathList will open each dex sequentially, each time passing the
980 // list of dex files which were opened before.
981 if (element == nullptr) {
982 continue;
983 }
984
985 // We support this being dalvik.system.DexPathList$Element and dalvik.system.DexFile.
986 // TODO(calin): Code caried over oat_file_manager: supporting both classes seem to be
987 // a historical glitch. All the java code opens dex files using an array of Elements.
988 ObjPtr<mirror::Object> dex_file;
989 if (element_class == element->GetClass()) {
990 dex_file = dex_file_field->GetObject(element);
991 } else if (dexfile_class == element->GetClass()) {
992 dex_file = element;
993 } else {
994 LOG(ERROR) << "Unsupported element in dex_elements: "
995 << mirror::Class::PrettyClass(element->GetClass());
996 return false;
997 }
998
999 if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) {
1000 return false;
1001 }
1002 }
1003 return true;
1004}
1005
1006// Adds the `class_loader` info to the `context`.
1007// The dex file present in `dex_elements` array (if not null) will be added at the end of
1008// the classpath.
1009// This method is recursive (w.r.t. the class loader parent) and will stop once it reaches the
1010// BootClassLoader. Note that the class loader chain is expected to be short.
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001011bool ClassLoaderContext::CreateInfoFromClassLoader(
Calin Juravle57d0acc2017-07-11 17:41:30 -07001012 ScopedObjectAccessAlreadyRunnable& soa,
1013 Handle<mirror::ClassLoader> class_loader,
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001014 Handle<mirror::ObjectArray<mirror::Object>> dex_elements,
1015 ClassLoaderInfo* child_info,
1016 bool is_shared_library)
Calin Juravle57d0acc2017-07-11 17:41:30 -07001017 REQUIRES_SHARED(Locks::mutator_lock_) {
1018 if (ClassLinker::IsBootClassLoader(soa, class_loader.Get())) {
1019 // Nothing to do for the boot class loader as we don't add its dex files to the context.
1020 return true;
1021 }
1022
1023 ClassLoaderContext::ClassLoaderType type;
1024 if (IsPathOrDexClassLoader(soa, class_loader)) {
1025 type = kPathClassLoader;
1026 } else if (IsDelegateLastClassLoader(soa, class_loader)) {
1027 type = kDelegateLastClassLoader;
David Brazdil1a9ac532019-03-05 11:57:13 +00001028 } else if (IsInMemoryDexClassLoader(soa, class_loader)) {
1029 type = kInMemoryDexClassLoader;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001030 } else {
1031 LOG(WARNING) << "Unsupported class loader";
1032 return false;
1033 }
1034
1035 // Inspect the class loader for its dex files.
1036 std::vector<const DexFile*> dex_files_loaded;
1037 CollectDexFilesFromSupportedClassLoader(soa, class_loader, &dex_files_loaded);
1038
1039 // If we have a dex_elements array extract its dex elements now.
1040 // This is used in two situations:
1041 // 1) when a new ClassLoader is created DexPathList will open each dex file sequentially
1042 // passing the list of already open dex files each time. This ensures that we see the
1043 // correct context even if the ClassLoader under construction is not fully build.
1044 // 2) when apk splits are loaded on the fly, the framework will load their dex files by
1045 // appending them to the current class loader. When the new code paths are loaded in
1046 // BaseDexClassLoader, the paths already present in the class loader will be passed
1047 // in the dex_elements array.
1048 if (dex_elements != nullptr) {
1049 GetDexFilesFromDexElementsArray(soa, dex_elements, &dex_files_loaded);
1050 }
1051
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001052 ClassLoaderInfo* info = new ClassLoaderContext::ClassLoaderInfo(type);
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001053 // Attach the `ClassLoaderInfo` now, before populating dex files, as only the
1054 // `ClassLoaderContext` knows whether these dex files should be deleted or not.
1055 if (child_info == nullptr) {
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001056 class_loader_chain_.reset(info);
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001057 } else if (is_shared_library) {
1058 child_info->shared_libraries.push_back(std::unique_ptr<ClassLoaderInfo>(info));
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001059 } else {
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001060 child_info->parent.reset(info);
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001061 }
1062
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001063 // Now that `info` is in the chain, populate dex files.
Calin Juravle57d0acc2017-07-11 17:41:30 -07001064 for (const DexFile* dex_file : dex_files_loaded) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001065 info->classpath.push_back(dex_file->GetLocation());
1066 info->checksums.push_back(dex_file->GetLocationChecksum());
1067 info->opened_dex_files.emplace_back(dex_file);
Calin Juravle57d0acc2017-07-11 17:41:30 -07001068 }
1069
Calin Juravle57d0acc2017-07-11 17:41:30 -07001070 // Note that dex_elements array is null here. The elements are considered to be part of the
1071 // current class loader and are not passed to the parents.
1072 ScopedNullHandle<mirror::ObjectArray<mirror::Object>> null_dex_elements;
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001073
1074 // Add the shared libraries.
1075 StackHandleScope<3> hs(Thread::Current());
1076 ArtField* field =
1077 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders);
1078 ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get());
1079 if (raw_shared_libraries != nullptr) {
1080 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries =
1081 hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>());
1082 MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr);
1083 for (int32_t i = 0; i < shared_libraries->GetLength(); ++i) {
1084 temp_loader.Assign(shared_libraries->Get(i));
1085 if (!CreateInfoFromClassLoader(
1086 soa, temp_loader, null_dex_elements, info, /*is_shared_library=*/ true)) {
1087 return false;
1088 }
1089 }
1090 }
1091
1092 // We created the ClassLoaderInfo for the current loader. Move on to its parent.
1093 Handle<mirror::ClassLoader> parent = hs.NewHandle(class_loader->GetParent());
1094 if (!CreateInfoFromClassLoader(
1095 soa, parent, null_dex_elements, info, /*is_shared_library=*/ false)) {
1096 return false;
1097 }
1098 return true;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001099}
1100
1101std::unique_ptr<ClassLoaderContext> ClassLoaderContext::CreateContextForClassLoader(
1102 jobject class_loader,
1103 jobjectArray dex_elements) {
Calin Juravle3f918642017-07-11 19:04:20 -07001104 CHECK(class_loader != nullptr);
1105
Calin Juravle57d0acc2017-07-11 17:41:30 -07001106 ScopedObjectAccess soa(Thread::Current());
1107 StackHandleScope<2> hs(soa.Self());
1108 Handle<mirror::ClassLoader> h_class_loader =
1109 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader));
1110 Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements =
1111 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements));
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001112 std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext(/*owns_the_dex_files=*/ false));
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001113 if (!result->CreateInfoFromClassLoader(
1114 soa, h_class_loader, h_dex_elements, nullptr, /*is_shared_library=*/ false)) {
Calin Juravle57d0acc2017-07-11 17:41:30 -07001115 return nullptr;
1116 }
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001117 return result;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001118}
1119
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001120static bool IsAbsoluteLocation(const std::string& location) {
1121 return !location.empty() && location[0] == '/';
1122}
1123
Mathieu Chartieradc90862018-05-11 13:03:06 -07001124ClassLoaderContext::VerificationResult ClassLoaderContext::VerifyClassLoaderContextMatch(
1125 const std::string& context_spec,
1126 bool verify_names,
1127 bool verify_checksums) const {
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001128 if (verify_names || verify_checksums) {
1129 DCHECK(dex_files_open_attempted_);
1130 DCHECK(dex_files_open_result_);
1131 }
Calin Juravlec5b215f2017-09-12 14:49:37 -07001132
Calin Juravle3f918642017-07-11 19:04:20 -07001133 ClassLoaderContext expected_context;
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001134 if (!expected_context.Parse(context_spec, verify_checksums)) {
Calin Juravle3f918642017-07-11 19:04:20 -07001135 LOG(WARNING) << "Invalid class loader context: " << context_spec;
Mathieu Chartieradc90862018-05-11 13:03:06 -07001136 return VerificationResult::kMismatch;
Calin Juravle3f918642017-07-11 19:04:20 -07001137 }
1138
Calin Juravlec5b215f2017-09-12 14:49:37 -07001139 // Special shared library contexts always match. They essentially instruct the runtime
1140 // to ignore the class path check because the oat file is known to be loaded in different
1141 // contexts. OatFileManager will further verify if the oat file can be loaded based on the
1142 // collision check.
Mathieu Chartieradc90862018-05-11 13:03:06 -07001143 if (expected_context.special_shared_library_) {
1144 // Special case where we are the only entry in the class path.
Nicolas Geoffray9893c472018-11-13 15:39:53 +00001145 if (class_loader_chain_ != nullptr &&
1146 class_loader_chain_->parent == nullptr &&
1147 class_loader_chain_->classpath.size() == 0) {
Mathieu Chartieradc90862018-05-11 13:03:06 -07001148 return VerificationResult::kVerifies;
1149 }
1150 return VerificationResult::kForcedToSkipChecks;
1151 } else if (special_shared_library_) {
1152 return VerificationResult::kForcedToSkipChecks;
Calin Juravle3f918642017-07-11 19:04:20 -07001153 }
1154
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001155 ClassLoaderInfo* info = class_loader_chain_.get();
1156 ClassLoaderInfo* expected = expected_context.class_loader_chain_.get();
1157 CHECK(info != nullptr);
1158 CHECK(expected != nullptr);
1159 if (!ClassLoaderInfoMatch(*info, *expected, context_spec, verify_names, verify_checksums)) {
Mathieu Chartieradc90862018-05-11 13:03:06 -07001160 return VerificationResult::kMismatch;
Calin Juravle3f918642017-07-11 19:04:20 -07001161 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001162 return VerificationResult::kVerifies;
1163}
Calin Juravle3f918642017-07-11 19:04:20 -07001164
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001165bool ClassLoaderContext::ClassLoaderInfoMatch(
1166 const ClassLoaderInfo& info,
1167 const ClassLoaderInfo& expected_info,
1168 const std::string& context_spec,
1169 bool verify_names,
1170 bool verify_checksums) const {
1171 if (info.type != expected_info.type) {
1172 LOG(WARNING) << "ClassLoaderContext type mismatch"
1173 << ". expected=" << GetClassLoaderTypeName(expected_info.type)
1174 << ", found=" << GetClassLoaderTypeName(info.type)
1175 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1176 return false;
1177 }
1178 if (info.classpath.size() != expected_info.classpath.size()) {
1179 LOG(WARNING) << "ClassLoaderContext classpath size mismatch"
1180 << ". expected=" << expected_info.classpath.size()
1181 << ", found=" << info.classpath.size()
Andreas Gampe7d0f81c2017-07-25 18:25:41 -07001182 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001183 return false;
1184 }
Calin Juravle3f918642017-07-11 19:04:20 -07001185
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001186 if (verify_checksums) {
1187 DCHECK_EQ(info.classpath.size(), info.checksums.size());
1188 DCHECK_EQ(expected_info.classpath.size(), expected_info.checksums.size());
1189 }
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001190
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001191 if (verify_names) {
Calin Juravle3f918642017-07-11 19:04:20 -07001192 for (size_t k = 0; k < info.classpath.size(); k++) {
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001193 // Compute the dex location that must be compared.
1194 // We shouldn't do a naive comparison `info.classpath[k] == expected_info.classpath[k]`
1195 // because even if they refer to the same file, one could be encoded as a relative location
1196 // and the other as an absolute one.
1197 bool is_dex_name_absolute = IsAbsoluteLocation(info.classpath[k]);
1198 bool is_expected_dex_name_absolute = IsAbsoluteLocation(expected_info.classpath[k]);
1199 std::string dex_name;
1200 std::string expected_dex_name;
1201
1202 if (is_dex_name_absolute == is_expected_dex_name_absolute) {
1203 // If both locations are absolute or relative then compare them as they are.
1204 // This is usually the case for: shared libraries and secondary dex files.
1205 dex_name = info.classpath[k];
1206 expected_dex_name = expected_info.classpath[k];
1207 } else if (is_dex_name_absolute) {
1208 // The runtime name is absolute but the compiled name (the expected one) is relative.
1209 // This is the case for split apks which depend on base or on other splits.
1210 dex_name = info.classpath[k];
1211 expected_dex_name = OatFile::ResolveRelativeEncodedDexLocation(
1212 info.classpath[k].c_str(), expected_info.classpath[k]);
Calin Juravle92003fe2017-09-06 02:22:57 +00001213 } else if (is_expected_dex_name_absolute) {
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001214 // The runtime name is relative but the compiled name is absolute.
1215 // There is no expected use case that would end up here as dex files are always loaded
1216 // with their absolute location. However, be tolerant and do the best effort (in case
1217 // there are unexpected new use case...).
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001218 dex_name = OatFile::ResolveRelativeEncodedDexLocation(
1219 expected_info.classpath[k].c_str(), info.classpath[k]);
1220 expected_dex_name = expected_info.classpath[k];
Calin Juravle92003fe2017-09-06 02:22:57 +00001221 } else {
1222 // Both locations are relative. In this case there's not much we can be sure about
1223 // except that the names are the same. The checksum will ensure that the files are
1224 // are same. This should not happen outside testing and manual invocations.
1225 dex_name = info.classpath[k];
1226 expected_dex_name = expected_info.classpath[k];
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001227 }
1228
1229 // Compare the locations.
1230 if (dex_name != expected_dex_name) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001231 LOG(WARNING) << "ClassLoaderContext classpath element mismatch"
Calin Juravle3f918642017-07-11 19:04:20 -07001232 << ". expected=" << expected_info.classpath[k]
Andreas Gampe7d0f81c2017-07-25 18:25:41 -07001233 << ", found=" << info.classpath[k]
1234 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001235 return false;
Calin Juravle3f918642017-07-11 19:04:20 -07001236 }
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001237
1238 // Compare the checksums.
Calin Juravle3f918642017-07-11 19:04:20 -07001239 if (info.checksums[k] != expected_info.checksums[k]) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001240 LOG(WARNING) << "ClassLoaderContext classpath element checksum mismatch"
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001241 << ". expected=" << expected_info.checksums[k]
1242 << ", found=" << info.checksums[k]
1243 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001244 return false;
Calin Juravle3f918642017-07-11 19:04:20 -07001245 }
1246 }
1247 }
Calin Juravle3f918642017-07-11 19:04:20 -07001248
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001249 if (info.shared_libraries.size() != expected_info.shared_libraries.size()) {
1250 LOG(WARNING) << "ClassLoaderContext shared library size mismatch. "
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001251 << "Expected=" << expected_info.shared_libraries.size()
1252 << ", found=" << info.shared_libraries.size()
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001253 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1254 return false;
Calin Juravlec79470d2017-07-12 17:37:42 -07001255 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001256 for (size_t i = 0; i < info.shared_libraries.size(); ++i) {
1257 if (!ClassLoaderInfoMatch(*info.shared_libraries[i].get(),
1258 *expected_info.shared_libraries[i].get(),
1259 context_spec,
1260 verify_names,
1261 verify_checksums)) {
1262 return false;
1263 }
1264 }
1265 if (info.parent.get() == nullptr) {
1266 if (expected_info.parent.get() != nullptr) {
1267 LOG(WARNING) << "ClassLoaderContext parent mismatch. "
1268 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1269 return false;
1270 }
1271 return true;
1272 } else if (expected_info.parent.get() == nullptr) {
1273 LOG(WARNING) << "ClassLoaderContext parent mismatch. "
1274 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1275 return false;
1276 } else {
1277 return ClassLoaderInfoMatch(*info.parent.get(),
1278 *expected_info.parent.get(),
1279 context_spec,
1280 verify_names,
1281 verify_checksums);
1282 }
Calin Juravlec79470d2017-07-12 17:37:42 -07001283}
1284
Calin Juravle87e2cb62017-06-13 21:48:45 -07001285} // namespace art