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