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