blob: 428bf305001323653bbc655f29eb7b4df4b03f9f [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"
Vladimir Markobdc93b42019-03-29 16:12:04 +000034#include "mirror/class_loader-inl.h"
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +000035#include "mirror/object_array-alloc-inl.h"
36#include "nativehelper/scoped_local_ref.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070037#include "oat_file_assistant.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070038#include "obj_ptr-inl.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070039#include "runtime.h"
40#include "scoped_thread_state_change-inl.h"
41#include "thread.h"
Calin Juravle57d0acc2017-07-11 17:41:30 -070042#include "well_known_classes.h"
Calin Juravle87e2cb62017-06-13 21:48:45 -070043
44namespace art {
45
46static constexpr char kPathClassLoaderString[] = "PCL";
47static constexpr char kDelegateLastClassLoaderString[] = "DLC";
David Brazdil1a9ac532019-03-05 11:57:13 +000048static constexpr char kInMemoryDexClassLoaderString[] = "IMC";
Calin Juravle87e2cb62017-06-13 21:48:45 -070049static constexpr char kClassLoaderOpeningMark = '[';
50static constexpr char kClassLoaderClosingMark = ']';
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000051static constexpr char kClassLoaderSharedLibraryOpeningMark = '{';
52static constexpr char kClassLoaderSharedLibraryClosingMark = '}';
53static constexpr char kClassLoaderSharedLibrarySeparator = '#';
Calin Juravle7b0648a2017-07-07 18:40:50 -070054static constexpr char kClassLoaderSeparator = ';';
55static constexpr char kClasspathSeparator = ':';
56static constexpr char kDexFileChecksumSeparator = '*';
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.
173 return nullptr;
174 }
175 }
176
Calin Juravle87e2cb62017-06-13 21:48:45 -0700177 const char* class_loader_type_str = GetClassLoaderTypeName(class_loader_type);
178 size_t type_str_size = strlen(class_loader_type_str);
179
180 CHECK_EQ(0, class_loader_spec.compare(0, type_str_size, class_loader_type_str));
181
182 // Check the opening and closing markers.
183 if (class_loader_spec[type_str_size] != kClassLoaderOpeningMark) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000184 return nullptr;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700185 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000186 if ((class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderClosingMark) &&
187 (class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderSharedLibraryClosingMark)) {
188 return nullptr;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700189 }
190
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000191 size_t closing_index = class_loader_spec.find_first_of(kClassLoaderClosingMark);
192
Calin Juravle87e2cb62017-06-13 21:48:45 -0700193 // At this point we know the format is ok; continue and extract the classpath.
194 // Note that class loaders with an empty class path are allowed.
195 std::string classpath = class_loader_spec.substr(type_str_size + 1,
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000196 closing_index - type_str_size - 1);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700197
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000198 std::unique_ptr<ClassLoaderInfo> info(new ClassLoaderInfo(class_loader_type));
Calin Juravle7b0648a2017-07-07 18:40:50 -0700199
200 if (!parse_checksums) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000201 Split(classpath, kClasspathSeparator, &info->classpath);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700202 } else {
203 std::vector<std::string> classpath_elements;
204 Split(classpath, kClasspathSeparator, &classpath_elements);
205 for (const std::string& element : classpath_elements) {
206 std::vector<std::string> dex_file_with_checksum;
207 Split(element, kDexFileChecksumSeparator, &dex_file_with_checksum);
208 if (dex_file_with_checksum.size() != 2) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000209 return nullptr;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700210 }
211 uint32_t checksum = 0;
Tom Cherry7bc8e8f2018-10-05 14:34:13 -0700212 if (!android::base::ParseUint(dex_file_with_checksum[1].c_str(), &checksum)) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000213 return nullptr;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700214 }
David Brazdil1a9ac532019-03-05 11:57:13 +0000215
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000216 info->classpath.push_back(dex_file_with_checksum[0]);
217 info->checksums.push_back(checksum);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700218 }
219 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700220
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000221 if ((class_loader_spec[class_loader_spec.length() - 1] == kClassLoaderSharedLibraryClosingMark) &&
222 (class_loader_spec[class_loader_spec.length() - 2] != kClassLoaderSharedLibraryOpeningMark)) {
223 // Non-empty list of shared libraries.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000224 size_t start_index = class_loader_spec.find_first_of(kClassLoaderSharedLibraryOpeningMark);
225 if (start_index == std::string::npos) {
226 return nullptr;
227 }
228 std::string shared_libraries_spec =
229 class_loader_spec.substr(start_index + 1, class_loader_spec.length() - start_index - 2);
230 std::vector<std::string> shared_libraries;
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000231 size_t cursor = 0;
232 while (cursor != shared_libraries_spec.length()) {
233 size_t shared_library_separator =
234 shared_libraries_spec.find_first_of(kClassLoaderSharedLibrarySeparator, cursor);
235 size_t shared_library_open =
236 shared_libraries_spec.find_first_of(kClassLoaderSharedLibraryOpeningMark, cursor);
237 std::string shared_library_spec;
238 if (shared_library_separator == std::string::npos) {
239 // Only one shared library, for example:
240 // PCL[...]
241 shared_library_spec =
242 shared_libraries_spec.substr(cursor, shared_libraries_spec.length() - cursor);
243 cursor = shared_libraries_spec.length();
244 } else if ((shared_library_open == std::string::npos) ||
245 (shared_library_open > shared_library_separator)) {
246 // We found a shared library without nested shared libraries, for example:
247 // PCL[...]#PCL[...]{...}
248 shared_library_spec =
249 shared_libraries_spec.substr(cursor, shared_library_separator - cursor);
250 cursor = shared_library_separator + 1;
251 } else {
252 // The shared library contains nested shared libraries. Find the matching closing shared
253 // marker for it.
254 size_t closing_marker =
255 FindMatchingSharedLibraryCloseMarker(shared_libraries_spec, shared_library_open);
256 if (closing_marker == std::string::npos) {
257 // No matching closing marker, return an error.
258 return nullptr;
259 }
260 shared_library_spec = shared_libraries_spec.substr(cursor, closing_marker + 1 - cursor);
261 cursor = closing_marker + 1;
262 if (cursor != shared_libraries_spec.length() &&
263 shared_libraries_spec[cursor] == kClassLoaderSharedLibrarySeparator) {
264 // Pass the shared library separator marker.
265 ++cursor;
266 }
267 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000268 std::unique_ptr<ClassLoaderInfo> shared_library(
269 ParseInternal(shared_library_spec, parse_checksums));
270 if (shared_library == nullptr) {
271 return nullptr;
272 }
273 info->shared_libraries.push_back(std::move(shared_library));
274 }
275 }
276
277 return info;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700278}
279
280// Extracts the class loader type from the given spec.
281// Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not
282// recognized.
283ClassLoaderContext::ClassLoaderType
284ClassLoaderContext::ExtractClassLoaderType(const std::string& class_loader_spec) {
David Brazdil1a9ac532019-03-05 11:57:13 +0000285 const ClassLoaderType kValidTypes[] = { kPathClassLoader,
286 kDelegateLastClassLoader,
287 kInMemoryDexClassLoader };
Calin Juravle87e2cb62017-06-13 21:48:45 -0700288 for (const ClassLoaderType& type : kValidTypes) {
289 const char* type_str = GetClassLoaderTypeName(type);
290 if (class_loader_spec.compare(0, strlen(type_str), type_str) == 0) {
291 return type;
292 }
293 }
294 return kInvalidClassLoader;
295}
296
297// The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]...
298// ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader).
299// ClasspathElem is the path of dex/jar/apk file.
Calin Juravle7b0648a2017-07-07 18:40:50 -0700300bool ClassLoaderContext::Parse(const std::string& spec, bool parse_checksums) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700301 if (spec.empty()) {
Calin Juravle1a509c82017-07-24 16:51:21 -0700302 // By default we load the dex files in a PathClassLoader.
303 // So an empty spec is equivalent to an empty PathClassLoader (this happens when running
304 // tests)
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000305 class_loader_chain_.reset(new ClassLoaderInfo(kPathClassLoader));
Calin Juravle7b0648a2017-07-07 18:40:50 -0700306 return true;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700307 }
Calin Juravle7b0648a2017-07-07 18:40:50 -0700308
Calin Juravle87e2cb62017-06-13 21:48:45 -0700309 // Stop early if we detect the special shared library, which may be passed as the classpath
310 // for dex2oat when we want to skip the shared libraries check.
311 if (spec == OatFile::kSpecialSharedLibrary) {
312 LOG(INFO) << "The ClassLoaderContext is a special shared library.";
313 special_shared_library_ = true;
314 return true;
315 }
316
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000317 CHECK(class_loader_chain_ == nullptr);
318 class_loader_chain_.reset(ParseInternal(spec, parse_checksums));
319 return class_loader_chain_ != nullptr;
320}
Calin Juravle87e2cb62017-06-13 21:48:45 -0700321
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000322ClassLoaderContext::ClassLoaderInfo* ClassLoaderContext::ParseInternal(
323 const std::string& spec, bool parse_checksums) {
324 CHECK(!spec.empty());
325 CHECK_NE(spec, OatFile::kSpecialSharedLibrary);
326 std::string remaining = spec;
327 std::unique_ptr<ClassLoaderInfo> first(nullptr);
328 ClassLoaderInfo* previous_iteration = nullptr;
329 while (!remaining.empty()) {
330 std::string class_loader_spec;
331 size_t first_class_loader_separator = remaining.find_first_of(kClassLoaderSeparator);
332 size_t first_shared_library_open =
333 remaining.find_first_of(kClassLoaderSharedLibraryOpeningMark);
334 if (first_class_loader_separator == std::string::npos) {
335 // Only one class loader, for example:
336 // PCL[...]
337 class_loader_spec = remaining;
338 remaining = "";
339 } else if ((first_shared_library_open == std::string::npos) ||
340 (first_shared_library_open > first_class_loader_separator)) {
341 // We found a class loader spec without shared libraries, for example:
342 // PCL[...];PCL[...]{...}
343 class_loader_spec = remaining.substr(0, first_class_loader_separator);
344 remaining = remaining.substr(first_class_loader_separator + 1,
345 remaining.size() - first_class_loader_separator - 1);
346 } else {
347 // The class loader spec contains shared libraries. Find the matching closing
348 // shared library marker for it.
349
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000350 uint32_t shared_library_close =
351 FindMatchingSharedLibraryCloseMarker(remaining, first_shared_library_open);
352 if (shared_library_close == std::string::npos) {
353 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
354 return nullptr;
355 }
356 class_loader_spec = remaining.substr(0, shared_library_close + 1);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000357
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000358 // Compute the remaining string to analyze.
359 if (remaining.size() == shared_library_close + 1) {
360 remaining = "";
361 } else if ((remaining.size() == shared_library_close + 2) ||
362 (remaining.at(shared_library_close + 1) != kClassLoaderSeparator)) {
363 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
364 return nullptr;
365 } else {
366 remaining = remaining.substr(shared_library_close + 2,
367 remaining.size() - shared_library_close - 2);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000368 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700369 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000370
371 std::unique_ptr<ClassLoaderInfo> info =
372 ParseClassLoaderSpec(class_loader_spec, parse_checksums);
373 if (info == nullptr) {
374 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
375 return nullptr;
376 }
377 if (first == nullptr) {
Andreas Gampe41c911f2018-11-19 11:34:16 -0800378 first = std::move(info);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000379 previous_iteration = first.get();
380 } else {
381 CHECK(previous_iteration != nullptr);
Andreas Gampe41c911f2018-11-19 11:34:16 -0800382 previous_iteration->parent = std::move(info);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000383 previous_iteration = previous_iteration->parent.get();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700384 }
385 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000386 return first.release();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700387}
388
389// Opens requested class path files and appends them to opened_dex_files. If the dex files have
390// been stripped, this opens them from their oat files (which get added to opened_oat_files).
David Brazdil89821862019-03-19 13:57:43 +0000391bool ClassLoaderContext::OpenDexFiles(InstructionSet isa,
392 const std::string& classpath_dir,
393 const std::vector<int>& fds) {
Calin Juravlec5b215f2017-09-12 14:49:37 -0700394 if (dex_files_open_attempted_) {
395 // Do not attempt to re-open the files if we already tried.
396 return dex_files_open_result_;
397 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700398
399 dex_files_open_attempted_ = true;
400 // Assume we can open all dex files. If not, we will set this to false as we go.
401 dex_files_open_result_ = true;
402
403 if (special_shared_library_) {
404 // Nothing to open if the context is a special shared library.
405 return true;
406 }
407
408 // Note that we try to open all dex files even if some fail.
409 // We may get resource-only apks which we cannot load.
410 // TODO(calin): Refine the dex opening interface to be able to tell if an archive contains
411 // no dex files. So that we can distinguish the real failures...
David Sehr013fd802018-01-11 22:55:24 -0800412 const ArtDexFileLoader dex_file_loader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000413 std::vector<ClassLoaderInfo*> work_list;
Nicolas Geoffray9893c472018-11-13 15:39:53 +0000414 CHECK(class_loader_chain_ != nullptr);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000415 work_list.push_back(class_loader_chain_.get());
David Brazdil89821862019-03-19 13:57:43 +0000416 size_t dex_file_index = 0;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000417 while (!work_list.empty()) {
418 ClassLoaderInfo* info = work_list.back();
419 work_list.pop_back();
420 size_t opened_dex_files_index = info->opened_dex_files.size();
421 for (const std::string& cp_elem : info->classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700422 // If path is relative, append it to the provided base directory.
Calin Juravle92003fe2017-09-06 02:22:57 +0000423 std::string location = cp_elem;
424 if (location[0] != '/' && !classpath_dir.empty()) {
Nicolas Geoffray06ffecf2017-11-14 10:31:54 +0000425 location = classpath_dir + (classpath_dir.back() == '/' ? "" : "/") + location;
Calin Juravle821a2592017-08-11 14:33:38 -0700426 }
427
David Brazdil89821862019-03-19 13:57:43 +0000428 // If file descriptors were provided for the class loader context dex paths,
429 // get the descriptor which correponds to this dex path. We assume the `fds`
430 // vector follows the same order as a flattened class loader context.
431 int fd = -1;
432 if (!fds.empty()) {
433 if (dex_file_index >= fds.size()) {
434 LOG(WARNING) << "Number of FDs is smaller than number of dex files in the context";
435 dex_files_open_result_ = false;
436 return false;
437 }
438
439 fd = fds[dex_file_index++];
440 DCHECK_GE(fd, 0);
441 }
442
Calin Juravle87e2cb62017-06-13 21:48:45 -0700443 std::string error_msg;
444 // When opening the dex files from the context we expect their checksum to match their
445 // contents. So pass true to verify_checksum.
David Brazdil89821862019-03-19 13:57:43 +0000446 if (fd < 0) {
447 if (!dex_file_loader.Open(location.c_str(),
448 location.c_str(),
449 Runtime::Current()->IsVerificationEnabled(),
450 /*verify_checksum=*/ true,
451 &error_msg,
452 &info->opened_dex_files)) {
453 // If we fail to open the dex file because it's been stripped, try to
454 // open the dex file from its corresponding oat file.
455 // This could happen when we need to recompile a pre-build whose dex
456 // code has been stripped (for example, if the pre-build is only
457 // quicken and we want to re-compile it speed-profile).
458 // TODO(calin): Use the vdex directly instead of going through the oat file.
459 OatFileAssistant oat_file_assistant(location.c_str(), isa, false);
460 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
461 std::vector<std::unique_ptr<const DexFile>> oat_dex_files;
462 if (oat_file != nullptr &&
463 OatFileAssistant::LoadDexFiles(*oat_file, location, &oat_dex_files)) {
464 info->opened_oat_files.push_back(std::move(oat_file));
465 info->opened_dex_files.insert(info->opened_dex_files.end(),
466 std::make_move_iterator(oat_dex_files.begin()),
467 std::make_move_iterator(oat_dex_files.end()));
468 } else {
469 LOG(WARNING) << "Could not open dex files from location: " << location;
470 dex_files_open_result_ = false;
471 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700472 }
David Brazdil89821862019-03-19 13:57:43 +0000473 } else if (!dex_file_loader.Open(fd,
474 location.c_str(),
475 Runtime::Current()->IsVerificationEnabled(),
476 /*verify_checksum=*/ true,
477 &error_msg,
478 &info->opened_dex_files)) {
479 LOG(WARNING) << "Could not open dex files from fd " << fd << " for location: " << location;
480 dex_files_open_result_ = false;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700481 }
482 }
Calin Juravlec5b215f2017-09-12 14:49:37 -0700483
484 // We finished opening the dex files from the classpath.
485 // Now update the classpath and the checksum with the locations of the dex files.
486 //
487 // We do this because initially the classpath contains the paths of the dex files; and
488 // some of them might be multi-dexes. So in order to have a consistent view we replace all the
489 // file paths with the actual dex locations being loaded.
490 // This will allow the context to VerifyClassLoaderContextMatch which expects or multidex
491 // location in the class paths.
492 // Note that this will also remove the paths that could not be opened.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000493 info->original_classpath = std::move(info->classpath);
494 info->classpath.clear();
495 info->checksums.clear();
496 for (size_t k = opened_dex_files_index; k < info->opened_dex_files.size(); k++) {
497 std::unique_ptr<const DexFile>& dex = info->opened_dex_files[k];
498 info->classpath.push_back(dex->GetLocation());
499 info->checksums.push_back(dex->GetLocationChecksum());
Calin Juravlec5b215f2017-09-12 14:49:37 -0700500 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000501 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700502 }
503
David Brazdil89821862019-03-19 13:57:43 +0000504 // Check that if file descriptors were provided, there were exactly as many
505 // as we have encountered while iterating over this class loader context.
506 if (dex_file_index != fds.size()) {
507 LOG(WARNING) << fds.size() << " FDs provided but only " << dex_file_index
508 << " dex files are in the class loader context";
509 dex_files_open_result_ = false;
510 }
511
Calin Juravle87e2cb62017-06-13 21:48:45 -0700512 return dex_files_open_result_;
513}
514
515bool ClassLoaderContext::RemoveLocationsFromClassPaths(
516 const dchecked_vector<std::string>& locations) {
517 CHECK(!dex_files_open_attempted_)
518 << "RemoveLocationsFromClasspaths cannot be call after OpenDexFiles";
519
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000520 if (class_loader_chain_ == nullptr) {
521 return false;
522 }
523
Calin Juravle87e2cb62017-06-13 21:48:45 -0700524 std::set<std::string> canonical_locations;
525 for (const std::string& location : locations) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700526 canonical_locations.insert(DexFileLoader::GetDexCanonicalLocation(location.c_str()));
Calin Juravle87e2cb62017-06-13 21:48:45 -0700527 }
528 bool removed_locations = false;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000529 std::vector<ClassLoaderInfo*> work_list;
530 work_list.push_back(class_loader_chain_.get());
531 while (!work_list.empty()) {
532 ClassLoaderInfo* info = work_list.back();
533 work_list.pop_back();
534 size_t initial_size = info->classpath.size();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700535 auto kept_it = std::remove_if(
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000536 info->classpath.begin(),
537 info->classpath.end(),
Calin Juravle87e2cb62017-06-13 21:48:45 -0700538 [canonical_locations](const std::string& location) {
539 return ContainsElement(canonical_locations,
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700540 DexFileLoader::GetDexCanonicalLocation(location.c_str()));
Calin Juravle87e2cb62017-06-13 21:48:45 -0700541 });
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000542 info->classpath.erase(kept_it, info->classpath.end());
543 if (initial_size != info->classpath.size()) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700544 removed_locations = true;
545 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000546 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700547 }
548 return removed_locations;
549}
550
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700551std::string ClassLoaderContext::EncodeContextForDex2oat(const std::string& base_dir) const {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700552 return EncodeContext(base_dir, /*for_dex2oat=*/ true, /*stored_context=*/ nullptr);
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700553}
554
Mathieu Chartierc4440772018-04-16 14:40:56 -0700555std::string ClassLoaderContext::EncodeContextForOatFile(const std::string& base_dir,
556 ClassLoaderContext* stored_context) const {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700557 return EncodeContext(base_dir, /*for_dex2oat=*/ false, stored_context);
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700558}
559
560std::string ClassLoaderContext::EncodeContext(const std::string& base_dir,
Mathieu Chartierc4440772018-04-16 14:40:56 -0700561 bool for_dex2oat,
562 ClassLoaderContext* stored_context) const {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700563 CheckDexFilesOpened("EncodeContextForOatFile");
564 if (special_shared_library_) {
565 return OatFile::kSpecialSharedLibrary;
566 }
567
Mathieu Chartierc4440772018-04-16 14:40:56 -0700568 if (stored_context != nullptr) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000569 DCHECK_EQ(GetParentChainSize(), stored_context->GetParentChainSize());
Mathieu Chartierc4440772018-04-16 14:40:56 -0700570 }
571
Calin Juravle7b0648a2017-07-07 18:40:50 -0700572 std::ostringstream out;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000573 if (class_loader_chain_ == nullptr) {
Calin Juravle1a509c82017-07-24 16:51:21 -0700574 // We can get in this situation if the context was created with a class path containing the
575 // source dex files which were later removed (happens during run-tests).
576 out << GetClassLoaderTypeName(kPathClassLoader)
577 << kClassLoaderOpeningMark
578 << kClassLoaderClosingMark;
579 return out.str();
580 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700581
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000582 EncodeContextInternal(
583 *class_loader_chain_,
584 base_dir,
585 for_dex2oat,
586 (stored_context == nullptr ? nullptr : stored_context->class_loader_chain_.get()),
587 out);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700588 return out.str();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700589}
590
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000591void ClassLoaderContext::EncodeContextInternal(const ClassLoaderInfo& info,
592 const std::string& base_dir,
593 bool for_dex2oat,
594 ClassLoaderInfo* stored_info,
595 std::ostringstream& out) const {
596 out << GetClassLoaderTypeName(info.type);
597 out << kClassLoaderOpeningMark;
598 std::set<std::string> seen_locations;
599 SafeMap<std::string, std::string> remap;
600 if (stored_info != nullptr) {
601 for (size_t k = 0; k < info.original_classpath.size(); ++k) {
602 // Note that we don't care if the same name appears twice.
603 remap.Put(info.original_classpath[k], stored_info->classpath[k]);
604 }
605 }
606 for (size_t k = 0; k < info.opened_dex_files.size(); k++) {
607 const std::unique_ptr<const DexFile>& dex_file = info.opened_dex_files[k];
608 if (for_dex2oat) {
609 // dex2oat only needs the base location. It cannot accept multidex locations.
610 // So ensure we only add each file once.
611 bool new_insert = seen_locations.insert(
612 DexFileLoader::GetBaseLocation(dex_file->GetLocation())).second;
613 if (!new_insert) {
614 continue;
615 }
616 }
617 std::string location = dex_file->GetLocation();
618 // If there is a stored class loader remap, fix up the multidex strings.
619 if (!remap.empty()) {
620 std::string base_dex_location = DexFileLoader::GetBaseLocation(location);
621 auto it = remap.find(base_dex_location);
622 CHECK(it != remap.end()) << base_dex_location;
623 location = it->second + DexFileLoader::GetMultiDexSuffix(location);
624 }
625 if (k > 0) {
626 out << kClasspathSeparator;
627 }
Nicolas Geoffray93d99f32019-03-27 08:56:02 +0000628 // Find paths that were relative and convert them back from absolute.
629 if (!base_dir.empty() && location.substr(0, base_dir.length()) == base_dir) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000630 out << location.substr(base_dir.length() + 1).c_str();
631 } else {
632 out << location.c_str();
633 }
634 // dex2oat does not need the checksums.
635 if (!for_dex2oat) {
636 out << kDexFileChecksumSeparator;
637 out << dex_file->GetLocationChecksum();
638 }
639 }
640 out << kClassLoaderClosingMark;
641
642 if (!info.shared_libraries.empty()) {
643 out << kClassLoaderSharedLibraryOpeningMark;
644 for (uint32_t i = 0; i < info.shared_libraries.size(); ++i) {
645 if (i > 0) {
646 out << kClassLoaderSharedLibrarySeparator;
647 }
648 EncodeContextInternal(
649 *info.shared_libraries[i].get(),
650 base_dir,
651 for_dex2oat,
652 (stored_info == nullptr ? nullptr : stored_info->shared_libraries[i].get()),
653 out);
654 }
655 out << kClassLoaderSharedLibraryClosingMark;
656 }
657 if (info.parent != nullptr) {
658 out << kClassLoaderSeparator;
659 EncodeContextInternal(
660 *info.parent.get(),
661 base_dir,
662 for_dex2oat,
663 (stored_info == nullptr ? nullptr : stored_info->parent.get()),
664 out);
665 }
666}
667
668// Returns the WellKnownClass for the given class loader type.
669static jclass GetClassLoaderClass(ClassLoaderContext::ClassLoaderType type) {
670 switch (type) {
671 case ClassLoaderContext::kPathClassLoader:
672 return WellKnownClasses::dalvik_system_PathClassLoader;
673 case ClassLoaderContext::kDelegateLastClassLoader:
674 return WellKnownClasses::dalvik_system_DelegateLastClassLoader;
David Brazdil1a9ac532019-03-05 11:57:13 +0000675 case ClassLoaderContext::kInMemoryDexClassLoader:
676 return WellKnownClasses::dalvik_system_InMemoryDexClassLoader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000677 case ClassLoaderContext::kInvalidClassLoader: break; // will fail after the switch.
678 }
679 LOG(FATAL) << "Invalid class loader type " << type;
680 UNREACHABLE();
681}
682
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000683static std::string FlattenClasspath(const std::vector<std::string>& classpath) {
684 return android::base::Join(classpath, ':');
685}
686
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000687static ObjPtr<mirror::ClassLoader> CreateClassLoaderInternal(
688 Thread* self,
689 ScopedObjectAccess& soa,
690 const ClassLoaderContext::ClassLoaderInfo& info,
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000691 bool for_shared_library,
692 VariableSizedHandleScope& map_scope,
693 std::map<std::string, Handle<mirror::ClassLoader>>& canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000694 bool add_compilation_sources,
695 const std::vector<const DexFile*>& compilation_sources)
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000696 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000697 if (for_shared_library) {
698 // Check if the shared library has already been created.
699 auto search = canonicalized_libraries.find(FlattenClasspath(info.classpath));
700 if (search != canonicalized_libraries.end()) {
701 return search->second.Get();
702 }
703 }
704
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000705 StackHandleScope<3> hs(self);
706 MutableHandle<mirror::ObjectArray<mirror::ClassLoader>> libraries(
707 hs.NewHandle<mirror::ObjectArray<mirror::ClassLoader>>(nullptr));
708
709 if (!info.shared_libraries.empty()) {
710 libraries.Assign(mirror::ObjectArray<mirror::ClassLoader>::Alloc(
711 self,
712 GetClassRoot<mirror::ObjectArray<mirror::ClassLoader>>(),
713 info.shared_libraries.size()));
714 for (uint32_t i = 0; i < info.shared_libraries.size(); ++i) {
715 // We should only add the compilation sources to the first class loader.
716 libraries->Set(i,
717 CreateClassLoaderInternal(
718 self,
719 soa,
720 *info.shared_libraries[i].get(),
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000721 /* for_shared_library= */ true,
722 map_scope,
723 canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000724 /* add_compilation_sources= */ false,
725 compilation_sources));
726 }
727 }
728
729 MutableHandle<mirror::ClassLoader> parent = hs.NewHandle<mirror::ClassLoader>(nullptr);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000730 if (info.parent != nullptr) {
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000731 // We should only add the compilation sources to the first class loader.
732 parent.Assign(CreateClassLoaderInternal(
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000733 self,
734 soa,
735 *info.parent.get(),
736 /* for_shared_library= */ false,
737 map_scope,
738 canonicalized_libraries,
739 /* add_compilation_sources= */ false,
740 compilation_sources));
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000741 }
742 std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector(
743 info.opened_dex_files);
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000744 if (add_compilation_sources) {
745 // For the first class loader, its classpath comes first, followed by compilation sources.
746 // This ensures that whenever we need to resolve classes from it the classpath elements
747 // come first.
748 class_path_files.insert(class_path_files.end(),
749 compilation_sources.begin(),
750 compilation_sources.end());
751 }
752 Handle<mirror::Class> loader_class = hs.NewHandle<mirror::Class>(
753 soa.Decode<mirror::Class>(GetClassLoaderClass(info.type)));
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000754 ObjPtr<mirror::ClassLoader> loader =
755 Runtime::Current()->GetClassLinker()->CreateWellKnownClassLoader(
756 self,
757 class_path_files,
758 loader_class,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000759 parent,
760 libraries);
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000761 if (for_shared_library) {
762 canonicalized_libraries[FlattenClasspath(info.classpath)] =
763 map_scope.NewHandle<mirror::ClassLoader>(loader);
764 }
765 return loader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000766}
767
Calin Juravle87e2cb62017-06-13 21:48:45 -0700768jobject ClassLoaderContext::CreateClassLoader(
769 const std::vector<const DexFile*>& compilation_sources) const {
770 CheckDexFilesOpened("CreateClassLoader");
771
772 Thread* self = Thread::Current();
773 ScopedObjectAccess soa(self);
774
Calin Juravlec79470d2017-07-12 17:37:42 -0700775 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700776
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000777 if (class_loader_chain_ == nullptr) {
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000778 CHECK(special_shared_library_);
Calin Juravlec79470d2017-07-12 17:37:42 -0700779 return class_linker->CreatePathClassLoader(self, compilation_sources);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700780 }
781
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000782 // Create a map of canonicalized shared libraries. As we're holding objects,
783 // we're creating a variable size handle scope to put handles in the map.
784 VariableSizedHandleScope map_scope(self);
785 std::map<std::string, Handle<mirror::ClassLoader>> canonicalized_libraries;
786
787 // Create the class loader.
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000788 ObjPtr<mirror::ClassLoader> loader =
789 CreateClassLoaderInternal(self,
790 soa,
791 *class_loader_chain_.get(),
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000792 /* for_shared_library= */ false,
793 map_scope,
794 canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000795 /* add_compilation_sources= */ true,
796 compilation_sources);
797 // Make it a global ref and return.
798 ScopedLocalRef<jobject> local_ref(
799 soa.Env(), soa.Env()->AddLocalReference<jobject>(loader));
800 return soa.Env()->NewGlobalRef(local_ref.get());
Calin Juravle87e2cb62017-06-13 21:48:45 -0700801}
802
803std::vector<const DexFile*> ClassLoaderContext::FlattenOpenedDexFiles() const {
804 CheckDexFilesOpened("FlattenOpenedDexFiles");
805
806 std::vector<const DexFile*> result;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000807 if (class_loader_chain_ == nullptr) {
808 return result;
809 }
810 std::vector<ClassLoaderInfo*> work_list;
811 work_list.push_back(class_loader_chain_.get());
812 while (!work_list.empty()) {
813 ClassLoaderInfo* info = work_list.back();
814 work_list.pop_back();
815 for (const std::unique_ptr<const DexFile>& dex_file : info->opened_dex_files) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700816 result.push_back(dex_file.get());
817 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000818 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700819 }
820 return result;
821}
822
David Brazdil89821862019-03-19 13:57:43 +0000823std::string ClassLoaderContext::FlattenDexPaths() const {
824 if (class_loader_chain_ == nullptr) {
825 return "";
826 }
827
828 std::vector<std::string> result;
829 std::vector<ClassLoaderInfo*> work_list;
830 work_list.push_back(class_loader_chain_.get());
831 while (!work_list.empty()) {
832 ClassLoaderInfo* info = work_list.back();
833 work_list.pop_back();
834 for (const std::string& dex_path : info->classpath) {
835 result.push_back(dex_path);
836 }
837 AddToWorkList(info, work_list);
838 }
839 return FlattenClasspath(result);
840}
841
Calin Juravle87e2cb62017-06-13 21:48:45 -0700842const char* ClassLoaderContext::GetClassLoaderTypeName(ClassLoaderType type) {
843 switch (type) {
844 case kPathClassLoader: return kPathClassLoaderString;
845 case kDelegateLastClassLoader: return kDelegateLastClassLoaderString;
David Brazdil1a9ac532019-03-05 11:57:13 +0000846 case kInMemoryDexClassLoader: return kInMemoryDexClassLoaderString;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700847 default:
848 LOG(FATAL) << "Invalid class loader type " << type;
849 UNREACHABLE();
850 }
851}
852
853void ClassLoaderContext::CheckDexFilesOpened(const std::string& calling_method) const {
854 CHECK(dex_files_open_attempted_)
855 << "Dex files were not successfully opened before the call to " << calling_method
856 << "attempt=" << dex_files_open_attempted_ << ", result=" << dex_files_open_result_;
857}
Calin Juravle7b0648a2017-07-07 18:40:50 -0700858
Calin Juravle57d0acc2017-07-11 17:41:30 -0700859// Collects the dex files from the give Java dex_file object. Only the dex files with
860// at least 1 class are collected. If a null java_dex_file is passed this method does nothing.
861static bool CollectDexFilesFromJavaDexFile(ObjPtr<mirror::Object> java_dex_file,
862 ArtField* const cookie_field,
863 std::vector<const DexFile*>* out_dex_files)
864 REQUIRES_SHARED(Locks::mutator_lock_) {
865 if (java_dex_file == nullptr) {
866 return true;
867 }
868 // On the Java side, the dex files are stored in the cookie field.
Vladimir Marko4617d582019-03-28 13:48:31 +0000869 ObjPtr<mirror::LongArray> long_array = cookie_field->GetObject(java_dex_file)->AsLongArray();
Calin Juravle57d0acc2017-07-11 17:41:30 -0700870 if (long_array == nullptr) {
871 // This should never happen so log a warning.
872 LOG(ERROR) << "Unexpected null cookie";
873 return false;
874 }
875 int32_t long_array_size = long_array->GetLength();
876 // Index 0 from the long array stores the oat file. The dex files start at index 1.
877 for (int32_t j = 1; j < long_array_size; ++j) {
Vladimir Marko78baed52018-10-11 10:44:58 +0100878 const DexFile* cp_dex_file =
879 reinterpret_cast64<const DexFile*>(long_array->GetWithoutChecks(j));
Calin Juravle57d0acc2017-07-11 17:41:30 -0700880 if (cp_dex_file != nullptr && cp_dex_file->NumClassDefs() > 0) {
881 // TODO(calin): It's unclear why the dex files with no classes are skipped here and when
882 // cp_dex_file can be null.
883 out_dex_files->push_back(cp_dex_file);
884 }
885 }
886 return true;
887}
888
889// Collects all the dex files loaded by the given class loader.
890// Returns true for success or false if an unexpected state is discovered (e.g. a null dex cookie,
891// a null list of dex elements or a null dex element).
892static bool CollectDexFilesFromSupportedClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
893 Handle<mirror::ClassLoader> class_loader,
894 std::vector<const DexFile*>* out_dex_files)
895 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil1a9ac532019-03-05 11:57:13 +0000896 CHECK(IsPathOrDexClassLoader(soa, class_loader) ||
897 IsDelegateLastClassLoader(soa, class_loader) ||
898 IsInMemoryDexClassLoader(soa, class_loader));
Calin Juravle57d0acc2017-07-11 17:41:30 -0700899
900 // All supported class loaders inherit from BaseDexClassLoader.
901 // We need to get the DexPathList and loop through it.
902 ArtField* const cookie_field =
903 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
904 ArtField* const dex_file_field =
905 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
906 ObjPtr<mirror::Object> dex_path_list =
907 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)->
908 GetObject(class_loader.Get());
909 CHECK(cookie_field != nullptr);
910 CHECK(dex_file_field != nullptr);
911 if (dex_path_list == nullptr) {
912 // This may be null if the current class loader is under construction and it does not
913 // have its fields setup yet.
914 return true;
915 }
916 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
917 ObjPtr<mirror::Object> dex_elements_obj =
918 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)->
919 GetObject(dex_path_list);
920 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
921 // at the mCookie which is a DexFile vector.
922 if (dex_elements_obj == nullptr) {
923 // TODO(calin): It's unclear if we should just assert here. For now be prepared for the worse
924 // and assume we have no elements.
925 return true;
926 } else {
927 StackHandleScope<1> hs(soa.Self());
928 Handle<mirror::ObjectArray<mirror::Object>> dex_elements(
929 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()));
930 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
Vladimir Marko423bebb2019-03-26 15:17:21 +0000931 ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i);
Calin Juravle57d0acc2017-07-11 17:41:30 -0700932 if (element == nullptr) {
933 // Should never happen, log an error and break.
934 // TODO(calin): It's unclear if we should just assert here.
935 // This code was propagated to oat_file_manager from the class linker where it would
936 // throw a NPE. For now, return false which will mark this class loader as unsupported.
937 LOG(ERROR) << "Unexpected null in the dex element list";
938 return false;
939 }
940 ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element);
941 if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) {
942 return false;
943 }
944 }
945 }
946
947 return true;
948}
949
950static bool GetDexFilesFromDexElementsArray(
951 ScopedObjectAccessAlreadyRunnable& soa,
952 Handle<mirror::ObjectArray<mirror::Object>> dex_elements,
953 std::vector<const DexFile*>* out_dex_files) REQUIRES_SHARED(Locks::mutator_lock_) {
954 DCHECK(dex_elements != nullptr);
955
956 ArtField* const cookie_field =
957 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
958 ArtField* const dex_file_field =
959 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
Vladimir Marko0984e482019-03-27 16:41:41 +0000960 const ObjPtr<mirror::Class> element_class = soa.Decode<mirror::Class>(
Calin Juravle57d0acc2017-07-11 17:41:30 -0700961 WellKnownClasses::dalvik_system_DexPathList__Element);
Vladimir Marko0984e482019-03-27 16:41:41 +0000962 const ObjPtr<mirror::Class> dexfile_class = soa.Decode<mirror::Class>(
Calin Juravle57d0acc2017-07-11 17:41:30 -0700963 WellKnownClasses::dalvik_system_DexFile);
964
965 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
Vladimir Marko423bebb2019-03-26 15:17:21 +0000966 ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i);
Calin Juravle57d0acc2017-07-11 17:41:30 -0700967 // We can hit a null element here because this is invoked with a partially filled dex_elements
968 // array from DexPathList. DexPathList will open each dex sequentially, each time passing the
969 // list of dex files which were opened before.
970 if (element == nullptr) {
971 continue;
972 }
973
974 // We support this being dalvik.system.DexPathList$Element and dalvik.system.DexFile.
975 // TODO(calin): Code caried over oat_file_manager: supporting both classes seem to be
976 // a historical glitch. All the java code opens dex files using an array of Elements.
977 ObjPtr<mirror::Object> dex_file;
978 if (element_class == element->GetClass()) {
979 dex_file = dex_file_field->GetObject(element);
980 } else if (dexfile_class == element->GetClass()) {
981 dex_file = element;
982 } else {
983 LOG(ERROR) << "Unsupported element in dex_elements: "
984 << mirror::Class::PrettyClass(element->GetClass());
985 return false;
986 }
987
988 if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) {
989 return false;
990 }
991 }
992 return true;
993}
994
995// Adds the `class_loader` info to the `context`.
996// The dex file present in `dex_elements` array (if not null) will be added at the end of
997// the classpath.
998// This method is recursive (w.r.t. the class loader parent) and will stop once it reaches the
999// BootClassLoader. Note that the class loader chain is expected to be short.
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001000bool ClassLoaderContext::CreateInfoFromClassLoader(
Calin Juravle57d0acc2017-07-11 17:41:30 -07001001 ScopedObjectAccessAlreadyRunnable& soa,
1002 Handle<mirror::ClassLoader> class_loader,
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001003 Handle<mirror::ObjectArray<mirror::Object>> dex_elements,
1004 ClassLoaderInfo* child_info,
1005 bool is_shared_library)
Calin Juravle57d0acc2017-07-11 17:41:30 -07001006 REQUIRES_SHARED(Locks::mutator_lock_) {
1007 if (ClassLinker::IsBootClassLoader(soa, class_loader.Get())) {
1008 // Nothing to do for the boot class loader as we don't add its dex files to the context.
1009 return true;
1010 }
1011
1012 ClassLoaderContext::ClassLoaderType type;
1013 if (IsPathOrDexClassLoader(soa, class_loader)) {
1014 type = kPathClassLoader;
1015 } else if (IsDelegateLastClassLoader(soa, class_loader)) {
1016 type = kDelegateLastClassLoader;
David Brazdil1a9ac532019-03-05 11:57:13 +00001017 } else if (IsInMemoryDexClassLoader(soa, class_loader)) {
1018 type = kInMemoryDexClassLoader;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001019 } else {
1020 LOG(WARNING) << "Unsupported class loader";
1021 return false;
1022 }
1023
1024 // Inspect the class loader for its dex files.
1025 std::vector<const DexFile*> dex_files_loaded;
1026 CollectDexFilesFromSupportedClassLoader(soa, class_loader, &dex_files_loaded);
1027
1028 // If we have a dex_elements array extract its dex elements now.
1029 // This is used in two situations:
1030 // 1) when a new ClassLoader is created DexPathList will open each dex file sequentially
1031 // passing the list of already open dex files each time. This ensures that we see the
1032 // correct context even if the ClassLoader under construction is not fully build.
1033 // 2) when apk splits are loaded on the fly, the framework will load their dex files by
1034 // appending them to the current class loader. When the new code paths are loaded in
1035 // BaseDexClassLoader, the paths already present in the class loader will be passed
1036 // in the dex_elements array.
1037 if (dex_elements != nullptr) {
1038 GetDexFilesFromDexElementsArray(soa, dex_elements, &dex_files_loaded);
1039 }
1040
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001041 ClassLoaderInfo* info = new ClassLoaderContext::ClassLoaderInfo(type);
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001042 // Attach the `ClassLoaderInfo` now, before populating dex files, as only the
1043 // `ClassLoaderContext` knows whether these dex files should be deleted or not.
1044 if (child_info == nullptr) {
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001045 class_loader_chain_.reset(info);
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001046 } else if (is_shared_library) {
1047 child_info->shared_libraries.push_back(std::unique_ptr<ClassLoaderInfo>(info));
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001048 } else {
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001049 child_info->parent.reset(info);
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001050 }
1051
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001052 // Now that `info` is in the chain, populate dex files.
Calin Juravle57d0acc2017-07-11 17:41:30 -07001053 for (const DexFile* dex_file : dex_files_loaded) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001054 info->classpath.push_back(dex_file->GetLocation());
1055 info->checksums.push_back(dex_file->GetLocationChecksum());
1056 info->opened_dex_files.emplace_back(dex_file);
Calin Juravle57d0acc2017-07-11 17:41:30 -07001057 }
1058
Calin Juravle57d0acc2017-07-11 17:41:30 -07001059 // Note that dex_elements array is null here. The elements are considered to be part of the
1060 // current class loader and are not passed to the parents.
1061 ScopedNullHandle<mirror::ObjectArray<mirror::Object>> null_dex_elements;
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001062
1063 // Add the shared libraries.
1064 StackHandleScope<3> hs(Thread::Current());
1065 ArtField* field =
1066 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders);
1067 ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get());
1068 if (raw_shared_libraries != nullptr) {
1069 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries =
1070 hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>());
1071 MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr);
1072 for (int32_t i = 0; i < shared_libraries->GetLength(); ++i) {
1073 temp_loader.Assign(shared_libraries->Get(i));
1074 if (!CreateInfoFromClassLoader(
1075 soa, temp_loader, null_dex_elements, info, /*is_shared_library=*/ true)) {
1076 return false;
1077 }
1078 }
1079 }
1080
1081 // We created the ClassLoaderInfo for the current loader. Move on to its parent.
1082 Handle<mirror::ClassLoader> parent = hs.NewHandle(class_loader->GetParent());
1083 if (!CreateInfoFromClassLoader(
1084 soa, parent, null_dex_elements, info, /*is_shared_library=*/ false)) {
1085 return false;
1086 }
1087 return true;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001088}
1089
1090std::unique_ptr<ClassLoaderContext> ClassLoaderContext::CreateContextForClassLoader(
1091 jobject class_loader,
1092 jobjectArray dex_elements) {
Calin Juravle3f918642017-07-11 19:04:20 -07001093 CHECK(class_loader != nullptr);
1094
Calin Juravle57d0acc2017-07-11 17:41:30 -07001095 ScopedObjectAccess soa(Thread::Current());
1096 StackHandleScope<2> hs(soa.Self());
1097 Handle<mirror::ClassLoader> h_class_loader =
1098 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader));
1099 Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements =
1100 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements));
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001101 std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext(/*owns_the_dex_files=*/ false));
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001102 if (!result->CreateInfoFromClassLoader(
1103 soa, h_class_loader, h_dex_elements, nullptr, /*is_shared_library=*/ false)) {
Calin Juravle57d0acc2017-07-11 17:41:30 -07001104 return nullptr;
1105 }
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001106 return result;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001107}
1108
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001109static bool IsAbsoluteLocation(const std::string& location) {
1110 return !location.empty() && location[0] == '/';
1111}
1112
Mathieu Chartieradc90862018-05-11 13:03:06 -07001113ClassLoaderContext::VerificationResult ClassLoaderContext::VerifyClassLoaderContextMatch(
1114 const std::string& context_spec,
1115 bool verify_names,
1116 bool verify_checksums) const {
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001117 if (verify_names || verify_checksums) {
1118 DCHECK(dex_files_open_attempted_);
1119 DCHECK(dex_files_open_result_);
1120 }
Calin Juravlec5b215f2017-09-12 14:49:37 -07001121
Calin Juravle3f918642017-07-11 19:04:20 -07001122 ClassLoaderContext expected_context;
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001123 if (!expected_context.Parse(context_spec, verify_checksums)) {
Calin Juravle3f918642017-07-11 19:04:20 -07001124 LOG(WARNING) << "Invalid class loader context: " << context_spec;
Mathieu Chartieradc90862018-05-11 13:03:06 -07001125 return VerificationResult::kMismatch;
Calin Juravle3f918642017-07-11 19:04:20 -07001126 }
1127
Calin Juravlec5b215f2017-09-12 14:49:37 -07001128 // Special shared library contexts always match. They essentially instruct the runtime
1129 // to ignore the class path check because the oat file is known to be loaded in different
1130 // contexts. OatFileManager will further verify if the oat file can be loaded based on the
1131 // collision check.
Mathieu Chartieradc90862018-05-11 13:03:06 -07001132 if (expected_context.special_shared_library_) {
1133 // Special case where we are the only entry in the class path.
Nicolas Geoffray9893c472018-11-13 15:39:53 +00001134 if (class_loader_chain_ != nullptr &&
1135 class_loader_chain_->parent == nullptr &&
1136 class_loader_chain_->classpath.size() == 0) {
Mathieu Chartieradc90862018-05-11 13:03:06 -07001137 return VerificationResult::kVerifies;
1138 }
1139 return VerificationResult::kForcedToSkipChecks;
1140 } else if (special_shared_library_) {
1141 return VerificationResult::kForcedToSkipChecks;
Calin Juravle3f918642017-07-11 19:04:20 -07001142 }
1143
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001144 ClassLoaderInfo* info = class_loader_chain_.get();
1145 ClassLoaderInfo* expected = expected_context.class_loader_chain_.get();
1146 CHECK(info != nullptr);
1147 CHECK(expected != nullptr);
1148 if (!ClassLoaderInfoMatch(*info, *expected, context_spec, verify_names, verify_checksums)) {
Mathieu Chartieradc90862018-05-11 13:03:06 -07001149 return VerificationResult::kMismatch;
Calin Juravle3f918642017-07-11 19:04:20 -07001150 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001151 return VerificationResult::kVerifies;
1152}
Calin Juravle3f918642017-07-11 19:04:20 -07001153
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001154bool ClassLoaderContext::ClassLoaderInfoMatch(
1155 const ClassLoaderInfo& info,
1156 const ClassLoaderInfo& expected_info,
1157 const std::string& context_spec,
1158 bool verify_names,
1159 bool verify_checksums) const {
1160 if (info.type != expected_info.type) {
1161 LOG(WARNING) << "ClassLoaderContext type mismatch"
1162 << ". expected=" << GetClassLoaderTypeName(expected_info.type)
1163 << ", found=" << GetClassLoaderTypeName(info.type)
1164 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1165 return false;
1166 }
1167 if (info.classpath.size() != expected_info.classpath.size()) {
1168 LOG(WARNING) << "ClassLoaderContext classpath size mismatch"
1169 << ". expected=" << expected_info.classpath.size()
1170 << ", found=" << info.classpath.size()
Andreas Gampe7d0f81c2017-07-25 18:25:41 -07001171 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001172 return false;
1173 }
Calin Juravle3f918642017-07-11 19:04:20 -07001174
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001175 if (verify_checksums) {
1176 DCHECK_EQ(info.classpath.size(), info.checksums.size());
1177 DCHECK_EQ(expected_info.classpath.size(), expected_info.checksums.size());
1178 }
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001179
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001180 if (verify_names) {
Calin Juravle3f918642017-07-11 19:04:20 -07001181 for (size_t k = 0; k < info.classpath.size(); k++) {
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001182 // Compute the dex location that must be compared.
1183 // We shouldn't do a naive comparison `info.classpath[k] == expected_info.classpath[k]`
1184 // because even if they refer to the same file, one could be encoded as a relative location
1185 // and the other as an absolute one.
1186 bool is_dex_name_absolute = IsAbsoluteLocation(info.classpath[k]);
1187 bool is_expected_dex_name_absolute = IsAbsoluteLocation(expected_info.classpath[k]);
1188 std::string dex_name;
1189 std::string expected_dex_name;
1190
1191 if (is_dex_name_absolute == is_expected_dex_name_absolute) {
1192 // If both locations are absolute or relative then compare them as they are.
1193 // This is usually the case for: shared libraries and secondary dex files.
1194 dex_name = info.classpath[k];
1195 expected_dex_name = expected_info.classpath[k];
1196 } else if (is_dex_name_absolute) {
1197 // The runtime name is absolute but the compiled name (the expected one) is relative.
1198 // This is the case for split apks which depend on base or on other splits.
1199 dex_name = info.classpath[k];
1200 expected_dex_name = OatFile::ResolveRelativeEncodedDexLocation(
1201 info.classpath[k].c_str(), expected_info.classpath[k]);
Calin Juravle92003fe2017-09-06 02:22:57 +00001202 } else if (is_expected_dex_name_absolute) {
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001203 // The runtime name is relative but the compiled name is absolute.
1204 // There is no expected use case that would end up here as dex files are always loaded
1205 // with their absolute location. However, be tolerant and do the best effort (in case
1206 // there are unexpected new use case...).
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001207 dex_name = OatFile::ResolveRelativeEncodedDexLocation(
1208 expected_info.classpath[k].c_str(), info.classpath[k]);
1209 expected_dex_name = expected_info.classpath[k];
Calin Juravle92003fe2017-09-06 02:22:57 +00001210 } else {
1211 // Both locations are relative. In this case there's not much we can be sure about
1212 // except that the names are the same. The checksum will ensure that the files are
1213 // are same. This should not happen outside testing and manual invocations.
1214 dex_name = info.classpath[k];
1215 expected_dex_name = expected_info.classpath[k];
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001216 }
1217
1218 // Compare the locations.
1219 if (dex_name != expected_dex_name) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001220 LOG(WARNING) << "ClassLoaderContext classpath element mismatch"
Calin Juravle3f918642017-07-11 19:04:20 -07001221 << ". expected=" << expected_info.classpath[k]
Andreas Gampe7d0f81c2017-07-25 18:25:41 -07001222 << ", found=" << info.classpath[k]
1223 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001224 return false;
Calin Juravle3f918642017-07-11 19:04:20 -07001225 }
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001226
1227 // Compare the checksums.
Calin Juravle3f918642017-07-11 19:04:20 -07001228 if (info.checksums[k] != expected_info.checksums[k]) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001229 LOG(WARNING) << "ClassLoaderContext classpath element checksum mismatch"
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001230 << ". expected=" << expected_info.checksums[k]
1231 << ", found=" << info.checksums[k]
1232 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001233 return false;
Calin Juravle3f918642017-07-11 19:04:20 -07001234 }
1235 }
1236 }
Calin Juravle3f918642017-07-11 19:04:20 -07001237
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001238 if (info.shared_libraries.size() != expected_info.shared_libraries.size()) {
1239 LOG(WARNING) << "ClassLoaderContext shared library size mismatch. "
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001240 << "Expected=" << expected_info.shared_libraries.size()
1241 << ", found=" << info.shared_libraries.size()
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001242 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1243 return false;
Calin Juravlec79470d2017-07-12 17:37:42 -07001244 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001245 for (size_t i = 0; i < info.shared_libraries.size(); ++i) {
1246 if (!ClassLoaderInfoMatch(*info.shared_libraries[i].get(),
1247 *expected_info.shared_libraries[i].get(),
1248 context_spec,
1249 verify_names,
1250 verify_checksums)) {
1251 return false;
1252 }
1253 }
1254 if (info.parent.get() == nullptr) {
1255 if (expected_info.parent.get() != nullptr) {
1256 LOG(WARNING) << "ClassLoaderContext parent mismatch. "
1257 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1258 return false;
1259 }
1260 return true;
1261 } else if (expected_info.parent.get() == nullptr) {
1262 LOG(WARNING) << "ClassLoaderContext parent mismatch. "
1263 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1264 return false;
1265 } else {
1266 return ClassLoaderInfoMatch(*info.parent.get(),
1267 *expected_info.parent.get(),
1268 context_spec,
1269 verify_names,
1270 verify_checksums);
1271 }
Calin Juravlec79470d2017-07-12 17:37:42 -07001272}
1273
Calin Juravle87e2cb62017-06-13 21:48:45 -07001274} // namespace art