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