blob: e811afdb589e05ef37f04077e01ee766d1a57951 [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 = '*';
Calin Juravle87e2cb62017-06-13 21:48:45 -070056
57ClassLoaderContext::ClassLoaderContext()
58 : special_shared_library_(false),
59 dex_files_open_attempted_(false),
Calin Juravle57d0acc2017-07-11 17:41:30 -070060 dex_files_open_result_(false),
Calin Juravle41acdc12017-07-18 17:45:32 -070061 owns_the_dex_files_(true) {}
Calin Juravle57d0acc2017-07-11 17:41:30 -070062
63ClassLoaderContext::ClassLoaderContext(bool owns_the_dex_files)
64 : special_shared_library_(false),
65 dex_files_open_attempted_(true),
66 dex_files_open_result_(true),
67 owns_the_dex_files_(owns_the_dex_files) {}
68
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000069// Utility method to add parent and shared libraries of `info` into
70// the `work_list`.
71static void AddToWorkList(
72 ClassLoaderContext::ClassLoaderInfo* info,
73 std::vector<ClassLoaderContext::ClassLoaderInfo*>& work_list) {
74 if (info->parent != nullptr) {
75 work_list.push_back(info->parent.get());
76 }
77 for (size_t i = 0; i < info->shared_libraries.size(); ++i) {
78 work_list.push_back(info->shared_libraries[i].get());
79 }
80}
81
Calin Juravle57d0acc2017-07-11 17:41:30 -070082ClassLoaderContext::~ClassLoaderContext() {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000083 if (!owns_the_dex_files_ && class_loader_chain_ != nullptr) {
Calin Juravle57d0acc2017-07-11 17:41:30 -070084 // If the context does not own the dex/oat files release the unique pointers to
85 // make sure we do not de-allocate them.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000086 std::vector<ClassLoaderInfo*> work_list;
87 work_list.push_back(class_loader_chain_.get());
88 while (!work_list.empty()) {
89 ClassLoaderInfo* info = work_list.back();
90 work_list.pop_back();
91 for (std::unique_ptr<OatFile>& oat_file : info->opened_oat_files) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -070092 oat_file.release(); // NOLINT b/117926937
Calin Juravle57d0acc2017-07-11 17:41:30 -070093 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000094 for (std::unique_ptr<const DexFile>& dex_file : info->opened_dex_files) {
Andreas Gampeafaf7f82018-10-16 11:32:38 -070095 dex_file.release(); // NOLINT b/117926937
Calin Juravle57d0acc2017-07-11 17:41:30 -070096 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +000097 AddToWorkList(info, work_list);
Calin Juravle57d0acc2017-07-11 17:41:30 -070098 }
99 }
100}
Calin Juravle87e2cb62017-06-13 21:48:45 -0700101
Calin Juravle19915892017-08-03 17:10:36 +0000102std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Default() {
103 return Create("");
104}
105
Calin Juravle87e2cb62017-06-13 21:48:45 -0700106std::unique_ptr<ClassLoaderContext> ClassLoaderContext::Create(const std::string& spec) {
107 std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext());
108 if (result->Parse(spec)) {
109 return result;
110 } else {
111 return nullptr;
112 }
113}
114
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000115static size_t FindMatchingSharedLibraryCloseMarker(const std::string& spec,
116 size_t shared_library_open_index) {
117 // Counter of opened shared library marker we've encountered so far.
118 uint32_t counter = 1;
119 // The index at which we're operating in the loop.
120 uint32_t string_index = shared_library_open_index + 1;
121 size_t shared_library_close = std::string::npos;
122 while (counter != 0) {
123 shared_library_close =
124 spec.find_first_of(kClassLoaderSharedLibraryClosingMark, string_index);
125 size_t shared_library_open =
126 spec.find_first_of(kClassLoaderSharedLibraryOpeningMark, string_index);
127 if (shared_library_close == std::string::npos) {
128 // No matching closing marker. Return an error.
129 break;
130 }
131
132 if ((shared_library_open == std::string::npos) ||
133 (shared_library_close < shared_library_open)) {
134 // We have seen a closing marker. Decrement the counter.
135 --counter;
136 // Move the search index forward.
137 string_index = shared_library_close + 1;
138 } else {
139 // New nested opening marker. Increment the counter and move the search
140 // index after the marker.
141 ++counter;
142 string_index = shared_library_open + 1;
143 }
144 }
145 return shared_library_close;
146}
147
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000148// The expected format is:
149// "ClassLoaderType1[ClasspathElem1*Checksum1:ClasspathElem2*Checksum2...]{ClassLoaderType2[...]}".
Calin Juravle7b0648a2017-07-07 18:40:50 -0700150// The checksum part of the format is expected only if parse_cheksums is true.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000151std::unique_ptr<ClassLoaderContext::ClassLoaderInfo> ClassLoaderContext::ParseClassLoaderSpec(
152 const std::string& class_loader_spec,
153 bool parse_checksums) {
154 ClassLoaderType class_loader_type = ExtractClassLoaderType(class_loader_spec);
155 if (class_loader_type == kInvalidClassLoader) {
156 return nullptr;
157 }
David Brazdil1a9ac532019-03-05 11:57:13 +0000158
159 // InMemoryDexClassLoader's dex location is always bogus. Special-case it.
160 if (class_loader_type == kInMemoryDexClassLoader) {
161 if (parse_checksums) {
162 // Make sure that OpenDexFiles() will never be attempted on this context
163 // because the dex locations of IMC do not correspond to real files.
164 CHECK(!dex_files_open_attempted_ || !dex_files_open_result_)
165 << "Parsing spec not supported when context created from a ClassLoader object";
166 dex_files_open_attempted_ = true;
167 dex_files_open_result_ = false;
168 } else {
169 // Checksums are not provided and dex locations themselves have no meaning
170 // (although we keep them in the spec to simplify parsing). Treat this as
171 // an unknown class loader.
172 return nullptr;
173 }
174 }
175
Calin Juravle87e2cb62017-06-13 21:48:45 -0700176 const char* class_loader_type_str = GetClassLoaderTypeName(class_loader_type);
177 size_t type_str_size = strlen(class_loader_type_str);
178
179 CHECK_EQ(0, class_loader_spec.compare(0, type_str_size, class_loader_type_str));
180
181 // Check the opening and closing markers.
182 if (class_loader_spec[type_str_size] != kClassLoaderOpeningMark) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000183 return nullptr;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700184 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000185 if ((class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderClosingMark) &&
186 (class_loader_spec[class_loader_spec.length() - 1] != kClassLoaderSharedLibraryClosingMark)) {
187 return nullptr;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700188 }
189
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000190 size_t closing_index = class_loader_spec.find_first_of(kClassLoaderClosingMark);
191
Calin Juravle87e2cb62017-06-13 21:48:45 -0700192 // At this point we know the format is ok; continue and extract the classpath.
193 // Note that class loaders with an empty class path are allowed.
194 std::string classpath = class_loader_spec.substr(type_str_size + 1,
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000195 closing_index - type_str_size - 1);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700196
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000197 std::unique_ptr<ClassLoaderInfo> info(new ClassLoaderInfo(class_loader_type));
Calin Juravle7b0648a2017-07-07 18:40:50 -0700198
199 if (!parse_checksums) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000200 Split(classpath, kClasspathSeparator, &info->classpath);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700201 } else {
202 std::vector<std::string> classpath_elements;
203 Split(classpath, kClasspathSeparator, &classpath_elements);
204 for (const std::string& element : classpath_elements) {
205 std::vector<std::string> dex_file_with_checksum;
206 Split(element, kDexFileChecksumSeparator, &dex_file_with_checksum);
207 if (dex_file_with_checksum.size() != 2) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000208 return nullptr;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700209 }
210 uint32_t checksum = 0;
Tom Cherry7bc8e8f2018-10-05 14:34:13 -0700211 if (!android::base::ParseUint(dex_file_with_checksum[1].c_str(), &checksum)) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000212 return nullptr;
Calin Juravle7b0648a2017-07-07 18:40:50 -0700213 }
David Brazdil1a9ac532019-03-05 11:57:13 +0000214
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000215 info->classpath.push_back(dex_file_with_checksum[0]);
216 info->checksums.push_back(checksum);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700217 }
218 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700219
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000220 if ((class_loader_spec[class_loader_spec.length() - 1] == kClassLoaderSharedLibraryClosingMark) &&
221 (class_loader_spec[class_loader_spec.length() - 2] != kClassLoaderSharedLibraryOpeningMark)) {
222 // Non-empty list of shared libraries.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000223 size_t start_index = class_loader_spec.find_first_of(kClassLoaderSharedLibraryOpeningMark);
224 if (start_index == std::string::npos) {
225 return nullptr;
226 }
227 std::string shared_libraries_spec =
228 class_loader_spec.substr(start_index + 1, class_loader_spec.length() - start_index - 2);
229 std::vector<std::string> shared_libraries;
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000230 size_t cursor = 0;
231 while (cursor != shared_libraries_spec.length()) {
232 size_t shared_library_separator =
233 shared_libraries_spec.find_first_of(kClassLoaderSharedLibrarySeparator, cursor);
234 size_t shared_library_open =
235 shared_libraries_spec.find_first_of(kClassLoaderSharedLibraryOpeningMark, cursor);
236 std::string shared_library_spec;
237 if (shared_library_separator == std::string::npos) {
238 // Only one shared library, for example:
239 // PCL[...]
240 shared_library_spec =
241 shared_libraries_spec.substr(cursor, shared_libraries_spec.length() - cursor);
242 cursor = shared_libraries_spec.length();
243 } else if ((shared_library_open == std::string::npos) ||
244 (shared_library_open > shared_library_separator)) {
245 // We found a shared library without nested shared libraries, for example:
246 // PCL[...]#PCL[...]{...}
247 shared_library_spec =
248 shared_libraries_spec.substr(cursor, shared_library_separator - cursor);
249 cursor = shared_library_separator + 1;
250 } else {
251 // The shared library contains nested shared libraries. Find the matching closing shared
252 // marker for it.
253 size_t closing_marker =
254 FindMatchingSharedLibraryCloseMarker(shared_libraries_spec, shared_library_open);
255 if (closing_marker == std::string::npos) {
256 // No matching closing marker, return an error.
257 return nullptr;
258 }
259 shared_library_spec = shared_libraries_spec.substr(cursor, closing_marker + 1 - cursor);
260 cursor = closing_marker + 1;
261 if (cursor != shared_libraries_spec.length() &&
262 shared_libraries_spec[cursor] == kClassLoaderSharedLibrarySeparator) {
263 // Pass the shared library separator marker.
264 ++cursor;
265 }
266 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000267 std::unique_ptr<ClassLoaderInfo> shared_library(
268 ParseInternal(shared_library_spec, parse_checksums));
269 if (shared_library == nullptr) {
270 return nullptr;
271 }
272 info->shared_libraries.push_back(std::move(shared_library));
273 }
274 }
275
276 return info;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700277}
278
279// Extracts the class loader type from the given spec.
280// Return ClassLoaderContext::kInvalidClassLoader if the class loader type is not
281// recognized.
282ClassLoaderContext::ClassLoaderType
283ClassLoaderContext::ExtractClassLoaderType(const std::string& class_loader_spec) {
David Brazdil1a9ac532019-03-05 11:57:13 +0000284 const ClassLoaderType kValidTypes[] = { kPathClassLoader,
285 kDelegateLastClassLoader,
286 kInMemoryDexClassLoader };
Calin Juravle87e2cb62017-06-13 21:48:45 -0700287 for (const ClassLoaderType& type : kValidTypes) {
288 const char* type_str = GetClassLoaderTypeName(type);
289 if (class_loader_spec.compare(0, strlen(type_str), type_str) == 0) {
290 return type;
291 }
292 }
293 return kInvalidClassLoader;
294}
295
296// The format: ClassLoaderType1[ClasspathElem1:ClasspathElem2...];ClassLoaderType2[...]...
297// ClassLoaderType is either "PCL" (PathClassLoader) or "DLC" (DelegateLastClassLoader).
298// ClasspathElem is the path of dex/jar/apk file.
Calin Juravle7b0648a2017-07-07 18:40:50 -0700299bool ClassLoaderContext::Parse(const std::string& spec, bool parse_checksums) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700300 if (spec.empty()) {
Calin Juravle1a509c82017-07-24 16:51:21 -0700301 // By default we load the dex files in a PathClassLoader.
302 // So an empty spec is equivalent to an empty PathClassLoader (this happens when running
303 // tests)
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000304 class_loader_chain_.reset(new ClassLoaderInfo(kPathClassLoader));
Calin Juravle7b0648a2017-07-07 18:40:50 -0700305 return true;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700306 }
Calin Juravle7b0648a2017-07-07 18:40:50 -0700307
Calin Juravle87e2cb62017-06-13 21:48:45 -0700308 // Stop early if we detect the special shared library, which may be passed as the classpath
309 // for dex2oat when we want to skip the shared libraries check.
310 if (spec == OatFile::kSpecialSharedLibrary) {
311 LOG(INFO) << "The ClassLoaderContext is a special shared library.";
312 special_shared_library_ = true;
313 return true;
314 }
315
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000316 CHECK(class_loader_chain_ == nullptr);
317 class_loader_chain_.reset(ParseInternal(spec, parse_checksums));
318 return class_loader_chain_ != nullptr;
319}
Calin Juravle87e2cb62017-06-13 21:48:45 -0700320
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000321ClassLoaderContext::ClassLoaderInfo* ClassLoaderContext::ParseInternal(
322 const std::string& spec, bool parse_checksums) {
323 CHECK(!spec.empty());
324 CHECK_NE(spec, OatFile::kSpecialSharedLibrary);
325 std::string remaining = spec;
326 std::unique_ptr<ClassLoaderInfo> first(nullptr);
327 ClassLoaderInfo* previous_iteration = nullptr;
328 while (!remaining.empty()) {
329 std::string class_loader_spec;
330 size_t first_class_loader_separator = remaining.find_first_of(kClassLoaderSeparator);
331 size_t first_shared_library_open =
332 remaining.find_first_of(kClassLoaderSharedLibraryOpeningMark);
333 if (first_class_loader_separator == std::string::npos) {
334 // Only one class loader, for example:
335 // PCL[...]
336 class_loader_spec = remaining;
337 remaining = "";
338 } else if ((first_shared_library_open == std::string::npos) ||
339 (first_shared_library_open > first_class_loader_separator)) {
340 // We found a class loader spec without shared libraries, for example:
341 // PCL[...];PCL[...]{...}
342 class_loader_spec = remaining.substr(0, first_class_loader_separator);
343 remaining = remaining.substr(first_class_loader_separator + 1,
344 remaining.size() - first_class_loader_separator - 1);
345 } else {
346 // The class loader spec contains shared libraries. Find the matching closing
347 // shared library marker for it.
348
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000349 uint32_t shared_library_close =
350 FindMatchingSharedLibraryCloseMarker(remaining, first_shared_library_open);
351 if (shared_library_close == std::string::npos) {
352 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
353 return nullptr;
354 }
355 class_loader_spec = remaining.substr(0, shared_library_close + 1);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000356
Nicolas Geoffrayf378fff2018-11-19 12:52:26 +0000357 // Compute the remaining string to analyze.
358 if (remaining.size() == shared_library_close + 1) {
359 remaining = "";
360 } else if ((remaining.size() == shared_library_close + 2) ||
361 (remaining.at(shared_library_close + 1) != kClassLoaderSeparator)) {
362 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
363 return nullptr;
364 } else {
365 remaining = remaining.substr(shared_library_close + 2,
366 remaining.size() - shared_library_close - 2);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000367 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700368 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000369
370 std::unique_ptr<ClassLoaderInfo> info =
371 ParseClassLoaderSpec(class_loader_spec, parse_checksums);
372 if (info == nullptr) {
373 LOG(ERROR) << "Invalid class loader spec: " << class_loader_spec;
374 return nullptr;
375 }
376 if (first == nullptr) {
Andreas Gampe41c911f2018-11-19 11:34:16 -0800377 first = std::move(info);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000378 previous_iteration = first.get();
379 } else {
380 CHECK(previous_iteration != nullptr);
Andreas Gampe41c911f2018-11-19 11:34:16 -0800381 previous_iteration->parent = std::move(info);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000382 previous_iteration = previous_iteration->parent.get();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700383 }
384 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000385 return first.release();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700386}
387
388// Opens requested class path files and appends them to opened_dex_files. If the dex files have
389// been stripped, this opens them from their oat files (which get added to opened_oat_files).
David Brazdil89821862019-03-19 13:57:43 +0000390bool ClassLoaderContext::OpenDexFiles(InstructionSet isa,
391 const std::string& classpath_dir,
392 const std::vector<int>& fds) {
Calin Juravlec5b215f2017-09-12 14:49:37 -0700393 if (dex_files_open_attempted_) {
394 // Do not attempt to re-open the files if we already tried.
395 return dex_files_open_result_;
396 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700397
398 dex_files_open_attempted_ = true;
399 // Assume we can open all dex files. If not, we will set this to false as we go.
400 dex_files_open_result_ = true;
401
402 if (special_shared_library_) {
403 // Nothing to open if the context is a special shared library.
404 return true;
405 }
406
407 // Note that we try to open all dex files even if some fail.
408 // We may get resource-only apks which we cannot load.
409 // TODO(calin): Refine the dex opening interface to be able to tell if an archive contains
410 // no dex files. So that we can distinguish the real failures...
David Sehr013fd802018-01-11 22:55:24 -0800411 const ArtDexFileLoader dex_file_loader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000412 std::vector<ClassLoaderInfo*> work_list;
Nicolas Geoffray9893c472018-11-13 15:39:53 +0000413 CHECK(class_loader_chain_ != nullptr);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000414 work_list.push_back(class_loader_chain_.get());
David Brazdil89821862019-03-19 13:57:43 +0000415 size_t dex_file_index = 0;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000416 while (!work_list.empty()) {
417 ClassLoaderInfo* info = work_list.back();
418 work_list.pop_back();
419 size_t opened_dex_files_index = info->opened_dex_files.size();
420 for (const std::string& cp_elem : info->classpath) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700421 // If path is relative, append it to the provided base directory.
Calin Juravle92003fe2017-09-06 02:22:57 +0000422 std::string location = cp_elem;
423 if (location[0] != '/' && !classpath_dir.empty()) {
Nicolas Geoffray06ffecf2017-11-14 10:31:54 +0000424 location = classpath_dir + (classpath_dir.back() == '/' ? "" : "/") + location;
Calin Juravle821a2592017-08-11 14:33:38 -0700425 }
426
David Brazdil89821862019-03-19 13:57:43 +0000427 // If file descriptors were provided for the class loader context dex paths,
428 // get the descriptor which correponds to this dex path. We assume the `fds`
429 // vector follows the same order as a flattened class loader context.
430 int fd = -1;
431 if (!fds.empty()) {
432 if (dex_file_index >= fds.size()) {
433 LOG(WARNING) << "Number of FDs is smaller than number of dex files in the context";
434 dex_files_open_result_ = false;
435 return false;
436 }
437
438 fd = fds[dex_file_index++];
439 DCHECK_GE(fd, 0);
440 }
441
Calin Juravle87e2cb62017-06-13 21:48:45 -0700442 std::string error_msg;
443 // When opening the dex files from the context we expect their checksum to match their
444 // contents. So pass true to verify_checksum.
David Brazdil89821862019-03-19 13:57:43 +0000445 if (fd < 0) {
446 if (!dex_file_loader.Open(location.c_str(),
447 location.c_str(),
448 Runtime::Current()->IsVerificationEnabled(),
449 /*verify_checksum=*/ true,
450 &error_msg,
451 &info->opened_dex_files)) {
452 // If we fail to open the dex file because it's been stripped, try to
453 // open the dex file from its corresponding oat file.
454 // This could happen when we need to recompile a pre-build whose dex
455 // code has been stripped (for example, if the pre-build is only
456 // quicken and we want to re-compile it speed-profile).
457 // TODO(calin): Use the vdex directly instead of going through the oat file.
458 OatFileAssistant oat_file_assistant(location.c_str(), isa, false);
459 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
460 std::vector<std::unique_ptr<const DexFile>> oat_dex_files;
461 if (oat_file != nullptr &&
462 OatFileAssistant::LoadDexFiles(*oat_file, location, &oat_dex_files)) {
463 info->opened_oat_files.push_back(std::move(oat_file));
464 info->opened_dex_files.insert(info->opened_dex_files.end(),
465 std::make_move_iterator(oat_dex_files.begin()),
466 std::make_move_iterator(oat_dex_files.end()));
467 } else {
468 LOG(WARNING) << "Could not open dex files from location: " << location;
469 dex_files_open_result_ = false;
470 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700471 }
David Brazdil89821862019-03-19 13:57:43 +0000472 } else if (!dex_file_loader.Open(fd,
473 location.c_str(),
474 Runtime::Current()->IsVerificationEnabled(),
475 /*verify_checksum=*/ true,
476 &error_msg,
477 &info->opened_dex_files)) {
478 LOG(WARNING) << "Could not open dex files from fd " << fd << " for location: " << location;
479 dex_files_open_result_ = false;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700480 }
481 }
Calin Juravlec5b215f2017-09-12 14:49:37 -0700482
483 // We finished opening the dex files from the classpath.
484 // Now update the classpath and the checksum with the locations of the dex files.
485 //
486 // We do this because initially the classpath contains the paths of the dex files; and
487 // some of them might be multi-dexes. So in order to have a consistent view we replace all the
488 // file paths with the actual dex locations being loaded.
489 // This will allow the context to VerifyClassLoaderContextMatch which expects or multidex
490 // location in the class paths.
491 // Note that this will also remove the paths that could not be opened.
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000492 info->original_classpath = std::move(info->classpath);
493 info->classpath.clear();
494 info->checksums.clear();
495 for (size_t k = opened_dex_files_index; k < info->opened_dex_files.size(); k++) {
496 std::unique_ptr<const DexFile>& dex = info->opened_dex_files[k];
497 info->classpath.push_back(dex->GetLocation());
498 info->checksums.push_back(dex->GetLocationChecksum());
Calin Juravlec5b215f2017-09-12 14:49:37 -0700499 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000500 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700501 }
502
David Brazdil89821862019-03-19 13:57:43 +0000503 // Check that if file descriptors were provided, there were exactly as many
504 // as we have encountered while iterating over this class loader context.
505 if (dex_file_index != fds.size()) {
506 LOG(WARNING) << fds.size() << " FDs provided but only " << dex_file_index
507 << " dex files are in the class loader context";
508 dex_files_open_result_ = false;
509 }
510
Calin Juravle87e2cb62017-06-13 21:48:45 -0700511 return dex_files_open_result_;
512}
513
514bool ClassLoaderContext::RemoveLocationsFromClassPaths(
515 const dchecked_vector<std::string>& locations) {
516 CHECK(!dex_files_open_attempted_)
517 << "RemoveLocationsFromClasspaths cannot be call after OpenDexFiles";
518
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000519 if (class_loader_chain_ == nullptr) {
520 return false;
521 }
522
Calin Juravle87e2cb62017-06-13 21:48:45 -0700523 std::set<std::string> canonical_locations;
524 for (const std::string& location : locations) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700525 canonical_locations.insert(DexFileLoader::GetDexCanonicalLocation(location.c_str()));
Calin Juravle87e2cb62017-06-13 21:48:45 -0700526 }
527 bool removed_locations = false;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000528 std::vector<ClassLoaderInfo*> work_list;
529 work_list.push_back(class_loader_chain_.get());
530 while (!work_list.empty()) {
531 ClassLoaderInfo* info = work_list.back();
532 work_list.pop_back();
533 size_t initial_size = info->classpath.size();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700534 auto kept_it = std::remove_if(
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000535 info->classpath.begin(),
536 info->classpath.end(),
Calin Juravle87e2cb62017-06-13 21:48:45 -0700537 [canonical_locations](const std::string& location) {
538 return ContainsElement(canonical_locations,
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700539 DexFileLoader::GetDexCanonicalLocation(location.c_str()));
Calin Juravle87e2cb62017-06-13 21:48:45 -0700540 });
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000541 info->classpath.erase(kept_it, info->classpath.end());
542 if (initial_size != info->classpath.size()) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700543 removed_locations = true;
544 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000545 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700546 }
547 return removed_locations;
548}
549
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700550std::string ClassLoaderContext::EncodeContextForDex2oat(const std::string& base_dir) const {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700551 return EncodeContext(base_dir, /*for_dex2oat=*/ true, /*stored_context=*/ nullptr);
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700552}
553
Mathieu Chartierc4440772018-04-16 14:40:56 -0700554std::string ClassLoaderContext::EncodeContextForOatFile(const std::string& base_dir,
555 ClassLoaderContext* stored_context) const {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700556 return EncodeContext(base_dir, /*for_dex2oat=*/ false, stored_context);
Calin Juravle27e0d1f2017-07-26 00:16:07 -0700557}
558
559std::string ClassLoaderContext::EncodeContext(const std::string& base_dir,
Mathieu Chartierc4440772018-04-16 14:40:56 -0700560 bool for_dex2oat,
561 ClassLoaderContext* stored_context) const {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700562 CheckDexFilesOpened("EncodeContextForOatFile");
563 if (special_shared_library_) {
564 return OatFile::kSpecialSharedLibrary;
565 }
566
Mathieu Chartierc4440772018-04-16 14:40:56 -0700567 if (stored_context != nullptr) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000568 DCHECK_EQ(GetParentChainSize(), stored_context->GetParentChainSize());
Mathieu Chartierc4440772018-04-16 14:40:56 -0700569 }
570
Calin Juravle7b0648a2017-07-07 18:40:50 -0700571 std::ostringstream out;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000572 if (class_loader_chain_ == nullptr) {
Calin Juravle1a509c82017-07-24 16:51:21 -0700573 // We can get in this situation if the context was created with a class path containing the
574 // source dex files which were later removed (happens during run-tests).
575 out << GetClassLoaderTypeName(kPathClassLoader)
576 << kClassLoaderOpeningMark
577 << kClassLoaderClosingMark;
578 return out.str();
579 }
Calin Juravle87e2cb62017-06-13 21:48:45 -0700580
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000581 EncodeContextInternal(
582 *class_loader_chain_,
583 base_dir,
584 for_dex2oat,
585 (stored_context == nullptr ? nullptr : stored_context->class_loader_chain_.get()),
586 out);
Calin Juravle7b0648a2017-07-07 18:40:50 -0700587 return out.str();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700588}
589
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000590void ClassLoaderContext::EncodeContextInternal(const ClassLoaderInfo& info,
591 const std::string& base_dir,
592 bool for_dex2oat,
593 ClassLoaderInfo* stored_info,
594 std::ostringstream& out) const {
595 out << GetClassLoaderTypeName(info.type);
596 out << kClassLoaderOpeningMark;
597 std::set<std::string> seen_locations;
598 SafeMap<std::string, std::string> remap;
599 if (stored_info != nullptr) {
600 for (size_t k = 0; k < info.original_classpath.size(); ++k) {
601 // Note that we don't care if the same name appears twice.
602 remap.Put(info.original_classpath[k], stored_info->classpath[k]);
603 }
604 }
605 for (size_t k = 0; k < info.opened_dex_files.size(); k++) {
606 const std::unique_ptr<const DexFile>& dex_file = info.opened_dex_files[k];
607 if (for_dex2oat) {
608 // dex2oat only needs the base location. It cannot accept multidex locations.
609 // So ensure we only add each file once.
610 bool new_insert = seen_locations.insert(
611 DexFileLoader::GetBaseLocation(dex_file->GetLocation())).second;
612 if (!new_insert) {
613 continue;
614 }
615 }
616 std::string location = dex_file->GetLocation();
617 // If there is a stored class loader remap, fix up the multidex strings.
618 if (!remap.empty()) {
619 std::string base_dex_location = DexFileLoader::GetBaseLocation(location);
620 auto it = remap.find(base_dex_location);
621 CHECK(it != remap.end()) << base_dex_location;
622 location = it->second + DexFileLoader::GetMultiDexSuffix(location);
623 }
624 if (k > 0) {
625 out << kClasspathSeparator;
626 }
Nicolas Geoffray93d99f32019-03-27 08:56:02 +0000627 // Find paths that were relative and convert them back from absolute.
628 if (!base_dir.empty() && location.substr(0, base_dir.length()) == base_dir) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000629 out << location.substr(base_dir.length() + 1).c_str();
630 } else {
631 out << location.c_str();
632 }
633 // dex2oat does not need the checksums.
634 if (!for_dex2oat) {
635 out << kDexFileChecksumSeparator;
636 out << dex_file->GetLocationChecksum();
637 }
638 }
639 out << kClassLoaderClosingMark;
640
641 if (!info.shared_libraries.empty()) {
642 out << kClassLoaderSharedLibraryOpeningMark;
643 for (uint32_t i = 0; i < info.shared_libraries.size(); ++i) {
644 if (i > 0) {
645 out << kClassLoaderSharedLibrarySeparator;
646 }
647 EncodeContextInternal(
648 *info.shared_libraries[i].get(),
649 base_dir,
650 for_dex2oat,
651 (stored_info == nullptr ? nullptr : stored_info->shared_libraries[i].get()),
652 out);
653 }
654 out << kClassLoaderSharedLibraryClosingMark;
655 }
656 if (info.parent != nullptr) {
657 out << kClassLoaderSeparator;
658 EncodeContextInternal(
659 *info.parent.get(),
660 base_dir,
661 for_dex2oat,
662 (stored_info == nullptr ? nullptr : stored_info->parent.get()),
663 out);
664 }
665}
666
667// Returns the WellKnownClass for the given class loader type.
668static jclass GetClassLoaderClass(ClassLoaderContext::ClassLoaderType type) {
669 switch (type) {
670 case ClassLoaderContext::kPathClassLoader:
671 return WellKnownClasses::dalvik_system_PathClassLoader;
672 case ClassLoaderContext::kDelegateLastClassLoader:
673 return WellKnownClasses::dalvik_system_DelegateLastClassLoader;
David Brazdil1a9ac532019-03-05 11:57:13 +0000674 case ClassLoaderContext::kInMemoryDexClassLoader:
675 return WellKnownClasses::dalvik_system_InMemoryDexClassLoader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000676 case ClassLoaderContext::kInvalidClassLoader: break; // will fail after the switch.
677 }
678 LOG(FATAL) << "Invalid class loader type " << type;
679 UNREACHABLE();
680}
681
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000682static std::string FlattenClasspath(const std::vector<std::string>& classpath) {
683 return android::base::Join(classpath, ':');
684}
685
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000686static ObjPtr<mirror::ClassLoader> CreateClassLoaderInternal(
687 Thread* self,
688 ScopedObjectAccess& soa,
689 const ClassLoaderContext::ClassLoaderInfo& info,
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000690 bool for_shared_library,
691 VariableSizedHandleScope& map_scope,
692 std::map<std::string, Handle<mirror::ClassLoader>>& canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000693 bool add_compilation_sources,
694 const std::vector<const DexFile*>& compilation_sources)
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000695 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000696 if (for_shared_library) {
697 // Check if the shared library has already been created.
698 auto search = canonicalized_libraries.find(FlattenClasspath(info.classpath));
699 if (search != canonicalized_libraries.end()) {
700 return search->second.Get();
701 }
702 }
703
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000704 StackHandleScope<3> hs(self);
705 MutableHandle<mirror::ObjectArray<mirror::ClassLoader>> libraries(
706 hs.NewHandle<mirror::ObjectArray<mirror::ClassLoader>>(nullptr));
707
708 if (!info.shared_libraries.empty()) {
709 libraries.Assign(mirror::ObjectArray<mirror::ClassLoader>::Alloc(
710 self,
711 GetClassRoot<mirror::ObjectArray<mirror::ClassLoader>>(),
712 info.shared_libraries.size()));
713 for (uint32_t i = 0; i < info.shared_libraries.size(); ++i) {
714 // We should only add the compilation sources to the first class loader.
715 libraries->Set(i,
716 CreateClassLoaderInternal(
717 self,
718 soa,
719 *info.shared_libraries[i].get(),
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000720 /* for_shared_library= */ true,
721 map_scope,
722 canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000723 /* add_compilation_sources= */ false,
724 compilation_sources));
725 }
726 }
727
728 MutableHandle<mirror::ClassLoader> parent = hs.NewHandle<mirror::ClassLoader>(nullptr);
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000729 if (info.parent != nullptr) {
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000730 // We should only add the compilation sources to the first class loader.
731 parent.Assign(CreateClassLoaderInternal(
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000732 self,
733 soa,
734 *info.parent.get(),
735 /* for_shared_library= */ false,
736 map_scope,
737 canonicalized_libraries,
738 /* add_compilation_sources= */ false,
739 compilation_sources));
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000740 }
741 std::vector<const DexFile*> class_path_files = MakeNonOwningPointerVector(
742 info.opened_dex_files);
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000743 if (add_compilation_sources) {
744 // For the first class loader, its classpath comes first, followed by compilation sources.
745 // This ensures that whenever we need to resolve classes from it the classpath elements
746 // come first.
747 class_path_files.insert(class_path_files.end(),
748 compilation_sources.begin(),
749 compilation_sources.end());
750 }
751 Handle<mirror::Class> loader_class = hs.NewHandle<mirror::Class>(
752 soa.Decode<mirror::Class>(GetClassLoaderClass(info.type)));
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000753 ObjPtr<mirror::ClassLoader> loader =
754 Runtime::Current()->GetClassLinker()->CreateWellKnownClassLoader(
755 self,
756 class_path_files,
757 loader_class,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000758 parent,
759 libraries);
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000760 if (for_shared_library) {
761 canonicalized_libraries[FlattenClasspath(info.classpath)] =
762 map_scope.NewHandle<mirror::ClassLoader>(loader);
763 }
764 return loader;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000765}
766
Calin Juravle87e2cb62017-06-13 21:48:45 -0700767jobject ClassLoaderContext::CreateClassLoader(
768 const std::vector<const DexFile*>& compilation_sources) const {
769 CheckDexFilesOpened("CreateClassLoader");
770
771 Thread* self = Thread::Current();
772 ScopedObjectAccess soa(self);
773
Calin Juravlec79470d2017-07-12 17:37:42 -0700774 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Calin Juravle87e2cb62017-06-13 21:48:45 -0700775
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000776 if (class_loader_chain_ == nullptr) {
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000777 CHECK(special_shared_library_);
Calin Juravlec79470d2017-07-12 17:37:42 -0700778 return class_linker->CreatePathClassLoader(self, compilation_sources);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700779 }
780
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000781 // Create a map of canonicalized shared libraries. As we're holding objects,
782 // we're creating a variable size handle scope to put handles in the map.
783 VariableSizedHandleScope map_scope(self);
784 std::map<std::string, Handle<mirror::ClassLoader>> canonicalized_libraries;
785
786 // Create the class loader.
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000787 ObjPtr<mirror::ClassLoader> loader =
788 CreateClassLoaderInternal(self,
789 soa,
790 *class_loader_chain_.get(),
Nicolas Geoffraycb2e1dd2018-11-20 11:15:13 +0000791 /* for_shared_library= */ false,
792 map_scope,
793 canonicalized_libraries,
Nicolas Geoffray6b9fd8c2018-11-16 10:25:42 +0000794 /* add_compilation_sources= */ true,
795 compilation_sources);
796 // Make it a global ref and return.
797 ScopedLocalRef<jobject> local_ref(
798 soa.Env(), soa.Env()->AddLocalReference<jobject>(loader));
799 return soa.Env()->NewGlobalRef(local_ref.get());
Calin Juravle87e2cb62017-06-13 21:48:45 -0700800}
801
802std::vector<const DexFile*> ClassLoaderContext::FlattenOpenedDexFiles() const {
803 CheckDexFilesOpened("FlattenOpenedDexFiles");
804
805 std::vector<const DexFile*> result;
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000806 if (class_loader_chain_ == nullptr) {
807 return result;
808 }
809 std::vector<ClassLoaderInfo*> work_list;
810 work_list.push_back(class_loader_chain_.get());
811 while (!work_list.empty()) {
812 ClassLoaderInfo* info = work_list.back();
813 work_list.pop_back();
814 for (const std::unique_ptr<const DexFile>& dex_file : info->opened_dex_files) {
Calin Juravle87e2cb62017-06-13 21:48:45 -0700815 result.push_back(dex_file.get());
816 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +0000817 AddToWorkList(info, work_list);
Calin Juravle87e2cb62017-06-13 21:48:45 -0700818 }
819 return result;
820}
821
David Brazdil89821862019-03-19 13:57:43 +0000822std::string ClassLoaderContext::FlattenDexPaths() const {
823 if (class_loader_chain_ == nullptr) {
824 return "";
825 }
826
827 std::vector<std::string> result;
828 std::vector<ClassLoaderInfo*> work_list;
829 work_list.push_back(class_loader_chain_.get());
830 while (!work_list.empty()) {
831 ClassLoaderInfo* info = work_list.back();
832 work_list.pop_back();
833 for (const std::string& dex_path : info->classpath) {
834 result.push_back(dex_path);
835 }
836 AddToWorkList(info, work_list);
837 }
838 return FlattenClasspath(result);
839}
840
Calin Juravle87e2cb62017-06-13 21:48:45 -0700841const char* ClassLoaderContext::GetClassLoaderTypeName(ClassLoaderType type) {
842 switch (type) {
843 case kPathClassLoader: return kPathClassLoaderString;
844 case kDelegateLastClassLoader: return kDelegateLastClassLoaderString;
David Brazdil1a9ac532019-03-05 11:57:13 +0000845 case kInMemoryDexClassLoader: return kInMemoryDexClassLoaderString;
Calin Juravle87e2cb62017-06-13 21:48:45 -0700846 default:
847 LOG(FATAL) << "Invalid class loader type " << type;
848 UNREACHABLE();
849 }
850}
851
852void ClassLoaderContext::CheckDexFilesOpened(const std::string& calling_method) const {
853 CHECK(dex_files_open_attempted_)
854 << "Dex files were not successfully opened before the call to " << calling_method
855 << "attempt=" << dex_files_open_attempted_ << ", result=" << dex_files_open_result_;
856}
Calin Juravle7b0648a2017-07-07 18:40:50 -0700857
Calin Juravle57d0acc2017-07-11 17:41:30 -0700858// Collects the dex files from the give Java dex_file object. Only the dex files with
859// at least 1 class are collected. If a null java_dex_file is passed this method does nothing.
860static bool CollectDexFilesFromJavaDexFile(ObjPtr<mirror::Object> java_dex_file,
861 ArtField* const cookie_field,
862 std::vector<const DexFile*>* out_dex_files)
863 REQUIRES_SHARED(Locks::mutator_lock_) {
864 if (java_dex_file == nullptr) {
865 return true;
866 }
867 // On the Java side, the dex files are stored in the cookie field.
868 mirror::LongArray* long_array = cookie_field->GetObject(java_dex_file)->AsLongArray();
869 if (long_array == nullptr) {
870 // This should never happen so log a warning.
871 LOG(ERROR) << "Unexpected null cookie";
872 return false;
873 }
874 int32_t long_array_size = long_array->GetLength();
875 // Index 0 from the long array stores the oat file. The dex files start at index 1.
876 for (int32_t j = 1; j < long_array_size; ++j) {
Vladimir Marko78baed52018-10-11 10:44:58 +0100877 const DexFile* cp_dex_file =
878 reinterpret_cast64<const DexFile*>(long_array->GetWithoutChecks(j));
Calin Juravle57d0acc2017-07-11 17:41:30 -0700879 if (cp_dex_file != nullptr && cp_dex_file->NumClassDefs() > 0) {
880 // TODO(calin): It's unclear why the dex files with no classes are skipped here and when
881 // cp_dex_file can be null.
882 out_dex_files->push_back(cp_dex_file);
883 }
884 }
885 return true;
886}
887
888// Collects all the dex files loaded by the given class loader.
889// Returns true for success or false if an unexpected state is discovered (e.g. a null dex cookie,
890// a null list of dex elements or a null dex element).
891static bool CollectDexFilesFromSupportedClassLoader(ScopedObjectAccessAlreadyRunnable& soa,
892 Handle<mirror::ClassLoader> class_loader,
893 std::vector<const DexFile*>* out_dex_files)
894 REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil1a9ac532019-03-05 11:57:13 +0000895 CHECK(IsPathOrDexClassLoader(soa, class_loader) ||
896 IsDelegateLastClassLoader(soa, class_loader) ||
897 IsInMemoryDexClassLoader(soa, class_loader));
Calin Juravle57d0acc2017-07-11 17:41:30 -0700898
899 // All supported class loaders inherit from BaseDexClassLoader.
900 // We need to get the DexPathList and loop through it.
901 ArtField* const cookie_field =
902 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
903 ArtField* const dex_file_field =
904 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
905 ObjPtr<mirror::Object> dex_path_list =
906 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList)->
907 GetObject(class_loader.Get());
908 CHECK(cookie_field != nullptr);
909 CHECK(dex_file_field != nullptr);
910 if (dex_path_list == nullptr) {
911 // This may be null if the current class loader is under construction and it does not
912 // have its fields setup yet.
913 return true;
914 }
915 // DexPathList has an array dexElements of Elements[] which each contain a dex file.
916 ObjPtr<mirror::Object> dex_elements_obj =
917 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements)->
918 GetObject(dex_path_list);
919 // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look
920 // at the mCookie which is a DexFile vector.
921 if (dex_elements_obj == nullptr) {
922 // TODO(calin): It's unclear if we should just assert here. For now be prepared for the worse
923 // and assume we have no elements.
924 return true;
925 } else {
926 StackHandleScope<1> hs(soa.Self());
927 Handle<mirror::ObjectArray<mirror::Object>> dex_elements(
928 hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()));
929 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
Vladimir Marko423bebb2019-03-26 15:17:21 +0000930 ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i);
Calin Juravle57d0acc2017-07-11 17:41:30 -0700931 if (element == nullptr) {
932 // Should never happen, log an error and break.
933 // TODO(calin): It's unclear if we should just assert here.
934 // This code was propagated to oat_file_manager from the class linker where it would
935 // throw a NPE. For now, return false which will mark this class loader as unsupported.
936 LOG(ERROR) << "Unexpected null in the dex element list";
937 return false;
938 }
939 ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element);
940 if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) {
941 return false;
942 }
943 }
944 }
945
946 return true;
947}
948
949static bool GetDexFilesFromDexElementsArray(
950 ScopedObjectAccessAlreadyRunnable& soa,
951 Handle<mirror::ObjectArray<mirror::Object>> dex_elements,
952 std::vector<const DexFile*>* out_dex_files) REQUIRES_SHARED(Locks::mutator_lock_) {
953 DCHECK(dex_elements != nullptr);
954
955 ArtField* const cookie_field =
956 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie);
957 ArtField* const dex_file_field =
958 jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile);
959 ObjPtr<mirror::Class> const element_class = soa.Decode<mirror::Class>(
960 WellKnownClasses::dalvik_system_DexPathList__Element);
961 ObjPtr<mirror::Class> const dexfile_class = soa.Decode<mirror::Class>(
962 WellKnownClasses::dalvik_system_DexFile);
963
964 for (int32_t i = 0; i < dex_elements->GetLength(); ++i) {
Vladimir Marko423bebb2019-03-26 15:17:21 +0000965 ObjPtr<mirror::Object> element = dex_elements->GetWithoutChecks(i);
Calin Juravle57d0acc2017-07-11 17:41:30 -0700966 // We can hit a null element here because this is invoked with a partially filled dex_elements
967 // array from DexPathList. DexPathList will open each dex sequentially, each time passing the
968 // list of dex files which were opened before.
969 if (element == nullptr) {
970 continue;
971 }
972
973 // We support this being dalvik.system.DexPathList$Element and dalvik.system.DexFile.
974 // TODO(calin): Code caried over oat_file_manager: supporting both classes seem to be
975 // a historical glitch. All the java code opens dex files using an array of Elements.
976 ObjPtr<mirror::Object> dex_file;
977 if (element_class == element->GetClass()) {
978 dex_file = dex_file_field->GetObject(element);
979 } else if (dexfile_class == element->GetClass()) {
980 dex_file = element;
981 } else {
982 LOG(ERROR) << "Unsupported element in dex_elements: "
983 << mirror::Class::PrettyClass(element->GetClass());
984 return false;
985 }
986
987 if (!CollectDexFilesFromJavaDexFile(dex_file, cookie_field, out_dex_files)) {
988 return false;
989 }
990 }
991 return true;
992}
993
994// Adds the `class_loader` info to the `context`.
995// The dex file present in `dex_elements` array (if not null) will be added at the end of
996// the classpath.
997// This method is recursive (w.r.t. the class loader parent) and will stop once it reaches the
998// BootClassLoader. Note that the class loader chain is expected to be short.
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000999bool ClassLoaderContext::CreateInfoFromClassLoader(
Calin Juravle57d0acc2017-07-11 17:41:30 -07001000 ScopedObjectAccessAlreadyRunnable& soa,
1001 Handle<mirror::ClassLoader> class_loader,
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001002 Handle<mirror::ObjectArray<mirror::Object>> dex_elements,
1003 ClassLoaderInfo* child_info,
1004 bool is_shared_library)
Calin Juravle57d0acc2017-07-11 17:41:30 -07001005 REQUIRES_SHARED(Locks::mutator_lock_) {
1006 if (ClassLinker::IsBootClassLoader(soa, class_loader.Get())) {
1007 // Nothing to do for the boot class loader as we don't add its dex files to the context.
1008 return true;
1009 }
1010
1011 ClassLoaderContext::ClassLoaderType type;
1012 if (IsPathOrDexClassLoader(soa, class_loader)) {
1013 type = kPathClassLoader;
1014 } else if (IsDelegateLastClassLoader(soa, class_loader)) {
1015 type = kDelegateLastClassLoader;
David Brazdil1a9ac532019-03-05 11:57:13 +00001016 } else if (IsInMemoryDexClassLoader(soa, class_loader)) {
1017 type = kInMemoryDexClassLoader;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001018 } else {
1019 LOG(WARNING) << "Unsupported class loader";
1020 return false;
1021 }
1022
1023 // Inspect the class loader for its dex files.
1024 std::vector<const DexFile*> dex_files_loaded;
1025 CollectDexFilesFromSupportedClassLoader(soa, class_loader, &dex_files_loaded);
1026
1027 // If we have a dex_elements array extract its dex elements now.
1028 // This is used in two situations:
1029 // 1) when a new ClassLoader is created DexPathList will open each dex file sequentially
1030 // passing the list of already open dex files each time. This ensures that we see the
1031 // correct context even if the ClassLoader under construction is not fully build.
1032 // 2) when apk splits are loaded on the fly, the framework will load their dex files by
1033 // appending them to the current class loader. When the new code paths are loaded in
1034 // BaseDexClassLoader, the paths already present in the class loader will be passed
1035 // in the dex_elements array.
1036 if (dex_elements != nullptr) {
1037 GetDexFilesFromDexElementsArray(soa, dex_elements, &dex_files_loaded);
1038 }
1039
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001040 ClassLoaderInfo* info = new ClassLoaderContext::ClassLoaderInfo(type);
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001041 // Attach the `ClassLoaderInfo` now, before populating dex files, as only the
1042 // `ClassLoaderContext` knows whether these dex files should be deleted or not.
1043 if (child_info == nullptr) {
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001044 class_loader_chain_.reset(info);
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001045 } else if (is_shared_library) {
1046 child_info->shared_libraries.push_back(std::unique_ptr<ClassLoaderInfo>(info));
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001047 } else {
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001048 child_info->parent.reset(info);
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001049 }
1050
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001051 // Now that `info` is in the chain, populate dex files.
Calin Juravle57d0acc2017-07-11 17:41:30 -07001052 for (const DexFile* dex_file : dex_files_loaded) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001053 info->classpath.push_back(dex_file->GetLocation());
1054 info->checksums.push_back(dex_file->GetLocationChecksum());
1055 info->opened_dex_files.emplace_back(dex_file);
Calin Juravle57d0acc2017-07-11 17:41:30 -07001056 }
1057
Calin Juravle57d0acc2017-07-11 17:41:30 -07001058 // Note that dex_elements array is null here. The elements are considered to be part of the
1059 // current class loader and are not passed to the parents.
1060 ScopedNullHandle<mirror::ObjectArray<mirror::Object>> null_dex_elements;
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001061
1062 // Add the shared libraries.
1063 StackHandleScope<3> hs(Thread::Current());
1064 ArtField* field =
1065 jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders);
1066 ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get());
1067 if (raw_shared_libraries != nullptr) {
1068 Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries =
1069 hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>());
1070 MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr);
1071 for (int32_t i = 0; i < shared_libraries->GetLength(); ++i) {
1072 temp_loader.Assign(shared_libraries->Get(i));
1073 if (!CreateInfoFromClassLoader(
1074 soa, temp_loader, null_dex_elements, info, /*is_shared_library=*/ true)) {
1075 return false;
1076 }
1077 }
1078 }
1079
1080 // We created the ClassLoaderInfo for the current loader. Move on to its parent.
1081 Handle<mirror::ClassLoader> parent = hs.NewHandle(class_loader->GetParent());
1082 if (!CreateInfoFromClassLoader(
1083 soa, parent, null_dex_elements, info, /*is_shared_library=*/ false)) {
1084 return false;
1085 }
1086 return true;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001087}
1088
1089std::unique_ptr<ClassLoaderContext> ClassLoaderContext::CreateContextForClassLoader(
1090 jobject class_loader,
1091 jobjectArray dex_elements) {
Calin Juravle3f918642017-07-11 19:04:20 -07001092 CHECK(class_loader != nullptr);
1093
Calin Juravle57d0acc2017-07-11 17:41:30 -07001094 ScopedObjectAccess soa(Thread::Current());
1095 StackHandleScope<2> hs(soa.Self());
1096 Handle<mirror::ClassLoader> h_class_loader =
1097 hs.NewHandle(soa.Decode<mirror::ClassLoader>(class_loader));
1098 Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements =
1099 hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements));
Nicolas Geoffray1717a492018-11-30 01:02:50 +00001100 std::unique_ptr<ClassLoaderContext> result(new ClassLoaderContext(/*owns_the_dex_files=*/ false));
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001101 if (!result->CreateInfoFromClassLoader(
1102 soa, h_class_loader, h_dex_elements, nullptr, /*is_shared_library=*/ false)) {
Calin Juravle57d0acc2017-07-11 17:41:30 -07001103 return nullptr;
1104 }
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001105 return result;
Calin Juravle57d0acc2017-07-11 17:41:30 -07001106}
1107
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001108static bool IsAbsoluteLocation(const std::string& location) {
1109 return !location.empty() && location[0] == '/';
1110}
1111
Mathieu Chartieradc90862018-05-11 13:03:06 -07001112ClassLoaderContext::VerificationResult ClassLoaderContext::VerifyClassLoaderContextMatch(
1113 const std::string& context_spec,
1114 bool verify_names,
1115 bool verify_checksums) const {
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001116 if (verify_names || verify_checksums) {
1117 DCHECK(dex_files_open_attempted_);
1118 DCHECK(dex_files_open_result_);
1119 }
Calin Juravlec5b215f2017-09-12 14:49:37 -07001120
Calin Juravle3f918642017-07-11 19:04:20 -07001121 ClassLoaderContext expected_context;
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001122 if (!expected_context.Parse(context_spec, verify_checksums)) {
Calin Juravle3f918642017-07-11 19:04:20 -07001123 LOG(WARNING) << "Invalid class loader context: " << context_spec;
Mathieu Chartieradc90862018-05-11 13:03:06 -07001124 return VerificationResult::kMismatch;
Calin Juravle3f918642017-07-11 19:04:20 -07001125 }
1126
Calin Juravlec5b215f2017-09-12 14:49:37 -07001127 // Special shared library contexts always match. They essentially instruct the runtime
1128 // to ignore the class path check because the oat file is known to be loaded in different
1129 // contexts. OatFileManager will further verify if the oat file can be loaded based on the
1130 // collision check.
Mathieu Chartieradc90862018-05-11 13:03:06 -07001131 if (expected_context.special_shared_library_) {
1132 // Special case where we are the only entry in the class path.
Nicolas Geoffray9893c472018-11-13 15:39:53 +00001133 if (class_loader_chain_ != nullptr &&
1134 class_loader_chain_->parent == nullptr &&
1135 class_loader_chain_->classpath.size() == 0) {
Mathieu Chartieradc90862018-05-11 13:03:06 -07001136 return VerificationResult::kVerifies;
1137 }
1138 return VerificationResult::kForcedToSkipChecks;
1139 } else if (special_shared_library_) {
1140 return VerificationResult::kForcedToSkipChecks;
Calin Juravle3f918642017-07-11 19:04:20 -07001141 }
1142
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001143 ClassLoaderInfo* info = class_loader_chain_.get();
1144 ClassLoaderInfo* expected = expected_context.class_loader_chain_.get();
1145 CHECK(info != nullptr);
1146 CHECK(expected != nullptr);
1147 if (!ClassLoaderInfoMatch(*info, *expected, context_spec, verify_names, verify_checksums)) {
Mathieu Chartieradc90862018-05-11 13:03:06 -07001148 return VerificationResult::kMismatch;
Calin Juravle3f918642017-07-11 19:04:20 -07001149 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001150 return VerificationResult::kVerifies;
1151}
Calin Juravle3f918642017-07-11 19:04:20 -07001152
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001153bool ClassLoaderContext::ClassLoaderInfoMatch(
1154 const ClassLoaderInfo& info,
1155 const ClassLoaderInfo& expected_info,
1156 const std::string& context_spec,
1157 bool verify_names,
1158 bool verify_checksums) const {
1159 if (info.type != expected_info.type) {
1160 LOG(WARNING) << "ClassLoaderContext type mismatch"
1161 << ". expected=" << GetClassLoaderTypeName(expected_info.type)
1162 << ", found=" << GetClassLoaderTypeName(info.type)
1163 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1164 return false;
1165 }
1166 if (info.classpath.size() != expected_info.classpath.size()) {
1167 LOG(WARNING) << "ClassLoaderContext classpath size mismatch"
1168 << ". expected=" << expected_info.classpath.size()
1169 << ", found=" << info.classpath.size()
Andreas Gampe7d0f81c2017-07-25 18:25:41 -07001170 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001171 return false;
1172 }
Calin Juravle3f918642017-07-11 19:04:20 -07001173
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001174 if (verify_checksums) {
1175 DCHECK_EQ(info.classpath.size(), info.checksums.size());
1176 DCHECK_EQ(expected_info.classpath.size(), expected_info.checksums.size());
1177 }
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07001178
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001179 if (verify_names) {
Calin Juravle3f918642017-07-11 19:04:20 -07001180 for (size_t k = 0; k < info.classpath.size(); k++) {
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001181 // Compute the dex location that must be compared.
1182 // We shouldn't do a naive comparison `info.classpath[k] == expected_info.classpath[k]`
1183 // because even if they refer to the same file, one could be encoded as a relative location
1184 // and the other as an absolute one.
1185 bool is_dex_name_absolute = IsAbsoluteLocation(info.classpath[k]);
1186 bool is_expected_dex_name_absolute = IsAbsoluteLocation(expected_info.classpath[k]);
1187 std::string dex_name;
1188 std::string expected_dex_name;
1189
1190 if (is_dex_name_absolute == is_expected_dex_name_absolute) {
1191 // If both locations are absolute or relative then compare them as they are.
1192 // This is usually the case for: shared libraries and secondary dex files.
1193 dex_name = info.classpath[k];
1194 expected_dex_name = expected_info.classpath[k];
1195 } else if (is_dex_name_absolute) {
1196 // The runtime name is absolute but the compiled name (the expected one) is relative.
1197 // This is the case for split apks which depend on base or on other splits.
1198 dex_name = info.classpath[k];
1199 expected_dex_name = OatFile::ResolveRelativeEncodedDexLocation(
1200 info.classpath[k].c_str(), expected_info.classpath[k]);
Calin Juravle92003fe2017-09-06 02:22:57 +00001201 } else if (is_expected_dex_name_absolute) {
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001202 // The runtime name is relative but the compiled name is absolute.
1203 // There is no expected use case that would end up here as dex files are always loaded
1204 // with their absolute location. However, be tolerant and do the best effort (in case
1205 // there are unexpected new use case...).
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001206 dex_name = OatFile::ResolveRelativeEncodedDexLocation(
1207 expected_info.classpath[k].c_str(), info.classpath[k]);
1208 expected_dex_name = expected_info.classpath[k];
Calin Juravle92003fe2017-09-06 02:22:57 +00001209 } else {
1210 // Both locations are relative. In this case there's not much we can be sure about
1211 // except that the names are the same. The checksum will ensure that the files are
1212 // are same. This should not happen outside testing and manual invocations.
1213 dex_name = info.classpath[k];
1214 expected_dex_name = expected_info.classpath[k];
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001215 }
1216
1217 // Compare the locations.
1218 if (dex_name != expected_dex_name) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001219 LOG(WARNING) << "ClassLoaderContext classpath element mismatch"
Calin Juravle3f918642017-07-11 19:04:20 -07001220 << ". expected=" << expected_info.classpath[k]
Andreas Gampe7d0f81c2017-07-25 18:25:41 -07001221 << ", found=" << info.classpath[k]
1222 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001223 return false;
Calin Juravle3f918642017-07-11 19:04:20 -07001224 }
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001225
1226 // Compare the checksums.
Calin Juravle3f918642017-07-11 19:04:20 -07001227 if (info.checksums[k] != expected_info.checksums[k]) {
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001228 LOG(WARNING) << "ClassLoaderContext classpath element checksum mismatch"
Calin Juravle1e96a5d2017-09-05 17:10:48 -07001229 << ". expected=" << expected_info.checksums[k]
1230 << ", found=" << info.checksums[k]
1231 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001232 return false;
Calin Juravle3f918642017-07-11 19:04:20 -07001233 }
1234 }
1235 }
Calin Juravle3f918642017-07-11 19:04:20 -07001236
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001237 if (info.shared_libraries.size() != expected_info.shared_libraries.size()) {
1238 LOG(WARNING) << "ClassLoaderContext shared library size mismatch. "
Nicolas Geoffraye1672732018-11-30 01:09:49 +00001239 << "Expected=" << expected_info.shared_libraries.size()
1240 << ", found=" << info.shared_libraries.size()
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001241 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1242 return false;
Calin Juravlec79470d2017-07-12 17:37:42 -07001243 }
Nicolas Geoffray06af3b42018-10-29 10:39:04 +00001244 for (size_t i = 0; i < info.shared_libraries.size(); ++i) {
1245 if (!ClassLoaderInfoMatch(*info.shared_libraries[i].get(),
1246 *expected_info.shared_libraries[i].get(),
1247 context_spec,
1248 verify_names,
1249 verify_checksums)) {
1250 return false;
1251 }
1252 }
1253 if (info.parent.get() == nullptr) {
1254 if (expected_info.parent.get() != nullptr) {
1255 LOG(WARNING) << "ClassLoaderContext parent mismatch. "
1256 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1257 return false;
1258 }
1259 return true;
1260 } else if (expected_info.parent.get() == nullptr) {
1261 LOG(WARNING) << "ClassLoaderContext parent mismatch. "
1262 << " (" << context_spec << " | " << EncodeContextForOatFile("") << ")";
1263 return false;
1264 } else {
1265 return ClassLoaderInfoMatch(*info.parent.get(),
1266 *expected_info.parent.get(),
1267 context_spec,
1268 verify_names,
1269 verify_checksums);
1270 }
Calin Juravlec79470d2017-07-12 17:37:42 -07001271}
1272
Calin Juravle87e2cb62017-06-13 21:48:45 -07001273} // namespace art