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