xref: /aosp_15_r20/art/runtime/oat/oat_file_assistant.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2014 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 "oat_file_assistant.h"
18 
19 #include <sys/stat.h>
20 
21 #include <memory>
22 #include <optional>
23 #include <sstream>
24 #include <vector>
25 
26 #include "android-base/file.h"
27 #include "android-base/logging.h"
28 #include "android-base/properties.h"
29 #include "android-base/stringprintf.h"
30 #include "android-base/strings.h"
31 #include "arch/instruction_set.h"
32 #include "base/array_ref.h"
33 #include "base/compiler_filter.h"
34 #include "base/file_utils.h"
35 #include "base/globals.h"
36 #include "base/logging.h"  // For VLOG.
37 #include "base/macros.h"
38 #include "base/os.h"
39 #include "base/stl_util.h"
40 #include "base/systrace.h"
41 #include "base/utils.h"
42 #include "base/zip_archive.h"
43 #include "class_linker.h"
44 #include "class_loader_context.h"
45 #include "dex/art_dex_file_loader.h"
46 #include "dex/dex_file_loader.h"
47 #include "exec_utils.h"
48 #include "gc/heap.h"
49 #include "gc/space/image_space.h"
50 #include "image.h"
51 #include "oat.h"
52 #include "oat_file_assistant_context.h"
53 #include "runtime.h"
54 #include "runtime_globals.h"
55 #include "scoped_thread_state_change-inl.h"
56 #include "vdex_file.h"
57 #include "zlib.h"
58 
59 namespace art HIDDEN {
60 
61 using ::android::base::ConsumePrefix;
62 using ::android::base::StringPrintf;
63 
64 static constexpr const char* kAnonymousDexPrefix = "Anonymous-DexFile@";
65 
operator <<(std::ostream & stream,const OatFileAssistant::OatStatus status)66 std::ostream& operator<<(std::ostream& stream, const OatFileAssistant::OatStatus status) {
67   switch (status) {
68     case OatFileAssistant::kOatCannotOpen:
69       stream << "kOatCannotOpen";
70       break;
71     case OatFileAssistant::kOatDexOutOfDate:
72       stream << "kOatDexOutOfDate";
73       break;
74     case OatFileAssistant::kOatBootImageOutOfDate:
75       stream << "kOatBootImageOutOfDate";
76       break;
77     case OatFileAssistant::kOatUpToDate:
78       stream << "kOatUpToDate";
79       break;
80     case OatFileAssistant::kOatContextOutOfDate:
81       stream << "kOatContextOutOfDate";
82       break;
83   }
84 
85   return stream;
86 }
87 
OatFileAssistant(const char * dex_location,const InstructionSet isa,ClassLoaderContext * context,bool load_executable,bool only_load_trusted_executable,OatFileAssistantContext * ofa_context)88 OatFileAssistant::OatFileAssistant(const char* dex_location,
89                                    const InstructionSet isa,
90                                    ClassLoaderContext* context,
91                                    bool load_executable,
92                                    bool only_load_trusted_executable,
93                                    OatFileAssistantContext* ofa_context)
94     : OatFileAssistant(dex_location,
95                        isa,
96                        context,
97                        load_executable,
98                        only_load_trusted_executable,
99                        ofa_context,
100                        /*vdex_fd=*/-1,
101                        /*oat_fd=*/-1,
102                        /*zip_fd=*/-1) {}
103 
OatFileAssistant(const char * dex_location,const InstructionSet isa,ClassLoaderContext * context,bool load_executable,bool only_load_trusted_executable,OatFileAssistantContext * ofa_context,int vdex_fd,int oat_fd,int zip_fd)104 OatFileAssistant::OatFileAssistant(const char* dex_location,
105                                    const InstructionSet isa,
106                                    ClassLoaderContext* context,
107                                    bool load_executable,
108                                    bool only_load_trusted_executable,
109                                    OatFileAssistantContext* ofa_context,
110                                    int vdex_fd,
111                                    int oat_fd,
112                                    int zip_fd)
113     : context_(context),
114       isa_(isa),
115       load_executable_(load_executable),
116       only_load_trusted_executable_(only_load_trusted_executable),
117       odex_(this, /*is_oat_location=*/false),
118       oat_(this, /*is_oat_location=*/true),
119       vdex_for_odex_(this, /*is_oat_location=*/false),
120       vdex_for_oat_(this, /*is_oat_location=*/true),
121       dm_for_odex_(this, /*is_oat_location=*/false),
122       dm_for_oat_(this, /*is_oat_location=*/true),
123       zip_fd_(zip_fd) {
124   CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location";
125   CHECK_IMPLIES(load_executable, context != nullptr) << "Loading executable without a context";
126 
127   if (zip_fd < 0) {
128     CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd
129                         << " oat_fd=" << oat_fd;
130     CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd
131                          << " vdex_fd=" << vdex_fd;
132     CHECK(!UseFdToReadFiles());
133   } else {
134     CHECK(UseFdToReadFiles());
135   }
136 
137   dex_location_.assign(dex_location);
138 
139   Runtime* runtime = Runtime::Current();
140 
141   if (load_executable_ && runtime == nullptr) {
142     LOG(WARNING) << "OatFileAssistant: Load executable specified, "
143                  << "but no active runtime is found. Will not attempt to load executable.";
144     load_executable_ = false;
145   }
146 
147   if (load_executable_ && isa != kRuntimeQuickCodeISA) {
148     LOG(WARNING) << "OatFileAssistant: Load executable specified, "
149                  << "but isa is not kRuntimeQuickCodeISA. Will not attempt to load executable.";
150     load_executable_ = false;
151   }
152 
153   if (ofa_context == nullptr) {
154     CHECK(runtime != nullptr) << "runtime_options is not provided, and no active runtime is found.";
155     ofa_context_ = std::make_unique<OatFileAssistantContext>(runtime);
156   } else {
157     ofa_context_ = ofa_context;
158   }
159 
160   if (runtime == nullptr) {
161     // We need `MemMap` for mapping files. We don't have to initialize it when there is a runtime
162     // because the runtime initializes it.
163     MemMap::Init();
164   }
165 
166   // Get the odex filename.
167   std::string error_msg;
168   std::string odex_file_name;
169   if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) {
170     odex_.Reset(odex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
171     std::string vdex_file_name = GetVdexFilename(odex_file_name);
172     // We dup FDs as the odex_ will claim ownership.
173     vdex_for_odex_.Reset(vdex_file_name,
174                          UseFdToReadFiles(),
175                          DupCloexec(zip_fd),
176                          DupCloexec(vdex_fd),
177                          DupCloexec(oat_fd));
178 
179     std::string dm_file_name = GetDmFilename(dex_location_);
180     dm_for_odex_.Reset(dm_file_name,
181                        UseFdToReadFiles(),
182                        DupCloexec(zip_fd),
183                        DupCloexec(vdex_fd),
184                        DupCloexec(oat_fd));
185   } else {
186     LOG(WARNING) << "Failed to determine odex file name: " << error_msg;
187   }
188 
189   if (!UseFdToReadFiles()) {
190     // Get the oat filename.
191     std::string oat_file_name;
192     if (DexLocationToOatFilename(dex_location_,
193                                  isa_,
194                                  GetRuntimeOptions().deny_art_apex_data_files,
195                                  &oat_file_name,
196                                  &error_msg)) {
197       oat_.Reset(oat_file_name, /*use_fd=*/false);
198       std::string vdex_file_name = GetVdexFilename(oat_file_name);
199       vdex_for_oat_.Reset(vdex_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
200       std::string dm_file_name = GetDmFilename(dex_location);
201       dm_for_oat_.Reset(dm_file_name, UseFdToReadFiles(), zip_fd, vdex_fd, oat_fd);
202     } else if (kIsTargetAndroid) {
203       // No need to warn on host. We are probably in oatdump, where we only need OatFileAssistant to
204       // validate BCP checksums.
205       LOG(WARNING) << "Failed to determine oat file name for dex location " << dex_location_ << ": "
206                    << error_msg;
207     }
208   }
209 
210   // Check if the dex directory is writable.
211   // This will be needed in most uses of OatFileAssistant and so it's OK to
212   // compute it eagerly. (the only use which will not make use of it is
213   // OatFileAssistant::GetStatusDump())
214   size_t pos = dex_location_.rfind('/');
215   if (pos == std::string::npos) {
216     LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_;
217   } else if (!UseFdToReadFiles()) {
218     // We cannot test for parent access when using file descriptors. That's ok
219     // because in this case we will always pick the odex file anyway.
220     std::string parent = dex_location_.substr(0, pos);
221     if (access(parent.c_str(), W_OK) == 0) {
222       dex_parent_writable_ = true;
223     } else {
224       VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno);
225     }
226   }
227 }
228 
Create(const std::string & filename,const std::string & isa_str,const std::optional<std::string> & context_str,bool load_executable,bool only_load_trusted_executable,OatFileAssistantContext * ofa_context,std::unique_ptr<ClassLoaderContext> * context,std::string * error_msg)229 std::unique_ptr<OatFileAssistant> OatFileAssistant::Create(
230     const std::string& filename,
231     const std::string& isa_str,
232     const std::optional<std::string>& context_str,
233     bool load_executable,
234     bool only_load_trusted_executable,
235     OatFileAssistantContext* ofa_context,
236     /*out*/ std::unique_ptr<ClassLoaderContext>* context,
237     /*out*/ std::string* error_msg) {
238   InstructionSet isa = GetInstructionSetFromString(isa_str.c_str());
239   if (isa == InstructionSet::kNone) {
240     *error_msg = StringPrintf("Instruction set '%s' is invalid", isa_str.c_str());
241     return nullptr;
242   }
243 
244   std::unique_ptr<ClassLoaderContext> tmp_context = nullptr;
245   if (context_str.has_value()) {
246     tmp_context = ClassLoaderContext::Create(context_str.value());
247     if (tmp_context == nullptr) {
248       *error_msg = StringPrintf("Class loader context '%s' is invalid", context_str->c_str());
249       return nullptr;
250     }
251 
252     if (!tmp_context->OpenDexFiles(android::base::Dirname(filename),
253                                    /*context_fds=*/{},
254                                    /*only_read_checksums=*/true)) {
255       *error_msg =
256           StringPrintf("Failed to load class loader context files for '%s' with context '%s'",
257                        filename.c_str(),
258                        context_str->c_str());
259       return nullptr;
260     }
261   }
262 
263   auto assistant = std::make_unique<OatFileAssistant>(filename.c_str(),
264                                                       isa,
265                                                       tmp_context.get(),
266                                                       load_executable,
267                                                       only_load_trusted_executable,
268                                                       ofa_context);
269 
270   *context = std::move(tmp_context);
271   return assistant;
272 }
273 
UseFdToReadFiles()274 bool OatFileAssistant::UseFdToReadFiles() { return zip_fd_ >= 0; }
275 
IsInBootClassPath()276 bool OatFileAssistant::IsInBootClassPath() {
277   // Note: We check the current boot class path, regardless of the ISA
278   // specified by the user. This is okay, because the boot class path should
279   // be the same for all ISAs.
280   // TODO: Can we verify the boot class path is the same for all ISAs?
281   for (const std::string& boot_class_path_location :
282        GetRuntimeOptions().boot_class_path_locations) {
283     if (boot_class_path_location == dex_location_) {
284       VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path";
285       return true;
286     }
287   }
288   return false;
289 }
290 
GetDexOptTrigger(CompilerFilter::Filter target_compiler_filter,bool profile_changed,bool downgrade)291 OatFileAssistant::DexOptTrigger OatFileAssistant::GetDexOptTrigger(
292     CompilerFilter::Filter target_compiler_filter, bool profile_changed, bool downgrade) {
293   if (downgrade) {
294     // The caller's intention is to downgrade the compiler filter. We should only re-compile if the
295     // target compiler filter is worse than the current one.
296     return DexOptTrigger{.targetFilterIsWorse = true};
297   }
298 
299   // This is the usual case. The caller's intention is to see if a better oat file can be generated.
300   DexOptTrigger dexopt_trigger{
301       .targetFilterIsBetter = true, .primaryBootImageBecomesUsable = true, .needExtraction = true};
302   if (profile_changed && CompilerFilter::DependsOnProfile(target_compiler_filter)) {
303     // Since the profile has been changed, we should re-compile even if the compilation does not
304     // make the compiler filter better.
305     dexopt_trigger.targetFilterIsSame = true;
306   }
307   return dexopt_trigger;
308 }
309 
GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,bool profile_changed,bool downgrade)310 int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
311                                       bool profile_changed,
312                                       bool downgrade) {
313   OatFileInfo& info = GetBestInfo();
314   if (info.CheckDisableCompactDex()) {  // TODO(b/256664509): Clean this up.
315     VLOG(oat) << "Should recompile: disable cdex";
316     return kDex2OatFromScratch;
317   }
318   DexOptNeeded dexopt_needed = info.GetDexOptNeeded(
319       target_compiler_filter, GetDexOptTrigger(target_compiler_filter, profile_changed, downgrade));
320   if (dexopt_needed != kNoDexOptNeeded && (&info == &dm_for_oat_ || &info == &dm_for_odex_)) {
321     // The usable vdex file is in the DM file. This information cannot be encoded in the integer.
322     // Return kDex2OatFromScratch so that neither the vdex in the "oat" location nor the vdex in the
323     // "odex" location will be picked by installd.
324     return kDex2OatFromScratch;
325   }
326   if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) {
327     return dexopt_needed;
328   }
329   return -dexopt_needed;
330 }
331 
GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,DexOptTrigger dexopt_trigger,DexOptStatus * dexopt_status)332 bool OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,
333                                        DexOptTrigger dexopt_trigger,
334                                        /*out*/ DexOptStatus* dexopt_status) {
335   OatFileInfo& info = GetBestInfo();
336   if (info.CheckDisableCompactDex()) {  // TODO(b/256664509): Clean this up.
337     dexopt_status->location_ = kLocationNoneOrError;
338     return true;
339   }
340   DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target_compiler_filter, dexopt_trigger);
341   dexopt_status->location_ = GetLocation(info);
342   return dexopt_needed != kNoDexOptNeeded;
343 }
344 
IsUpToDate()345 bool OatFileAssistant::IsUpToDate() { return GetBestInfo().Status() == kOatUpToDate; }
346 
GetBestOatFile()347 std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() {
348   return GetBestInfo().ReleaseFileForUse();
349 }
350 
GetStatusDump()351 std::string OatFileAssistant::GetStatusDump() {
352   std::ostringstream status;
353   bool oat_file_exists = false;
354   bool odex_file_exists = false;
355   if (oat_.Status() != kOatCannotOpen) {
356     // If we can open the file, Filename should not return null.
357     CHECK(oat_.Filename() != nullptr);
358 
359     oat_file_exists = true;
360     status << *oat_.Filename() << "[status=" << oat_.Status() << ", ";
361     const OatFile* file = oat_.GetFile();
362     if (file == nullptr) {
363       // If the file is null even though the status is not kOatCannotOpen, it
364       // means we must have a vdex file with no corresponding oat file. In
365       // this case we cannot determine the compilation filter. Indicate that
366       // we have only the vdex file instead.
367       status << "vdex-only";
368     } else {
369       status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
370     }
371   }
372 
373   if (odex_.Status() != kOatCannotOpen) {
374     // If we can open the file, Filename should not return null.
375     CHECK(odex_.Filename() != nullptr);
376 
377     odex_file_exists = true;
378     if (oat_file_exists) {
379       status << "] ";
380     }
381     status << *odex_.Filename() << "[status=" << odex_.Status() << ", ";
382     const OatFile* file = odex_.GetFile();
383     if (file == nullptr) {
384       status << "vdex-only";
385     } else {
386       status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter());
387     }
388   }
389 
390   if (!oat_file_exists && !odex_file_exists) {
391     status << "invalid[";
392   }
393 
394   status << "]";
395   return status.str();
396 }
397 
LoadDexFiles(const OatFile & oat_file,const char * dex_location)398 std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles(
399     const OatFile& oat_file, const char* dex_location) {
400   std::vector<std::unique_ptr<const DexFile>> dex_files;
401   if (LoadDexFiles(oat_file, dex_location, &dex_files)) {
402     return dex_files;
403   } else {
404     return std::vector<std::unique_ptr<const DexFile>>();
405   }
406 }
407 
LoadDexFiles(const OatFile & oat_file,const std::string & dex_location,std::vector<std::unique_ptr<const DexFile>> * out_dex_files)408 bool OatFileAssistant::LoadDexFiles(const OatFile& oat_file,
409                                     const std::string& dex_location,
410                                     std::vector<std::unique_ptr<const DexFile>>* out_dex_files) {
411   // Load the main dex file.
412   std::string error_msg;
413   const OatDexFile* oat_dex_file = oat_file.GetOatDexFile(dex_location.c_str(), &error_msg);
414   if (oat_dex_file == nullptr) {
415     LOG(WARNING) << error_msg;
416     return false;
417   }
418 
419   std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
420   if (dex_file.get() == nullptr) {
421     LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
422     return false;
423   }
424   out_dex_files->push_back(std::move(dex_file));
425 
426   // Load the rest of the multidex entries
427   for (size_t i = 1;; i++) {
428     std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str());
429     oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str());
430     if (oat_dex_file == nullptr) {
431       // There are no more multidex entries to load.
432       break;
433     }
434 
435     dex_file = oat_dex_file->OpenDexFile(&error_msg);
436     if (dex_file.get() == nullptr) {
437       LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg;
438       return false;
439     }
440     out_dex_files->push_back(std::move(dex_file));
441   }
442   return true;
443 }
444 
HasDexFiles(std::string * error_msg)445 std::optional<bool> OatFileAssistant::HasDexFiles(std::string* error_msg) {
446   ScopedTrace trace("HasDexFiles");
447   std::optional<std::uint32_t> checksum;
448   if (!GetRequiredDexChecksum(&checksum, error_msg)) {
449     return std::nullopt;
450   }
451   return checksum.has_value();
452 }
453 
OdexFileStatus()454 OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() { return odex_.Status(); }
455 
OatFileStatus()456 OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() { return oat_.Status(); }
457 
DexChecksumUpToDate(const OatFile & file,std::string * error_msg)458 bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) {
459   if (!file.ContainsDexCode()) {
460     // We've already checked during oat file creation that the dex files loaded
461     // from external files have the same checksums as the ones in the vdex file.
462     return true;
463   }
464   ScopedTrace trace("DexChecksumUpToDate");
465   std::optional<std::uint32_t> dex_checksum;
466   if (!GetRequiredDexChecksum(&dex_checksum, error_msg)) {
467     return false;
468   }
469   if (!dex_checksum.has_value()) {
470     LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date.";
471     return true;
472   }
473 
474   std::vector<const OatDexFile*> oat_dex_files;
475   uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount();
476   for (uint32_t i = 0; i < number_of_dex_files; i++) {
477     std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str());
478     const OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str());
479     if (oat_dex_file == nullptr) {
480       *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str());
481       return false;
482     }
483     oat_dex_files.push_back(oat_dex_file);
484   }
485   uint32_t oat_checksum = DexFileLoader::GetMultiDexChecksum(oat_dex_files);
486 
487   CHECK(dex_checksum.has_value());
488   if (dex_checksum != oat_checksum) {
489     VLOG(oat) << "Checksum does not match: " << std::hex << file.GetLocation() << " ("
490               << oat_checksum << ") vs " << dex_location_ << " (" << *dex_checksum << ")";
491     return false;
492   }
493 
494   return true;
495 }
496 
GivenOatFileStatus(const OatFile & file)497 OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) {
498   // Verify the ART_USE_READ_BARRIER state.
499   // TODO: Don't fully reject files due to read barrier state. If they contain
500   // compiled code and are otherwise okay, we should return something like
501   // kOatRelocationOutOfDate. If they don't contain compiled code, the read
502   // barrier state doesn't matter.
503   if (file.GetOatHeader().IsConcurrentCopying() != gUseReadBarrier) {
504     return kOatCannotOpen;
505   }
506 
507   // Verify the dex checksum.
508   std::string error_msg;
509   if (!DexChecksumUpToDate(file, &error_msg)) {
510     LOG(ERROR) << error_msg;
511     return kOatDexOutOfDate;
512   }
513 
514   CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter();
515 
516   // Verify the image checksum
517   if (file.IsBackedByVdexOnly()) {
518     VLOG(oat) << "Image checksum test skipped for vdex file " << file.GetLocation();
519   } else if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) {
520     if (!ValidateBootClassPathChecksums(file)) {
521       VLOG(oat) << "Oat image checksum does not match image checksum.";
522       return kOatBootImageOutOfDate;
523     }
524     if (!gc::space::ImageSpace::ValidateApexVersions(
525             file.GetOatHeader(),
526             GetOatFileAssistantContext()->GetApexVersions(),
527             file.GetLocation(),
528             &error_msg)) {
529       VLOG(oat) << error_msg;
530       return kOatBootImageOutOfDate;
531     }
532   } else {
533     VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter;
534   }
535 
536   // The constraint is only enforced if the zip has uncompressed dex code.
537   if (only_load_trusted_executable_ &&
538       !LocationIsTrusted(file.GetLocation(), !GetRuntimeOptions().deny_art_apex_data_files) &&
539       file.ContainsDexCode() && ZipFileOnlyContainsUncompressedDex()) {
540     LOG(ERROR) << "Not loading " << dex_location_
541                << ": oat file has dex code, but APK has uncompressed dex code";
542     return kOatDexOutOfDate;
543   }
544 
545   if (!ClassLoaderContextIsOkay(file)) {
546     return kOatContextOutOfDate;
547   }
548 
549   return kOatUpToDate;
550 }
551 
AnonymousDexVdexLocation(const std::vector<const DexFile::Header * > & headers,InstructionSet isa,std::string * dex_location,std::string * vdex_filename)552 bool OatFileAssistant::AnonymousDexVdexLocation(const std::vector<const DexFile::Header*>& headers,
553                                                 InstructionSet isa,
554                                                 /* out */ std::string* dex_location,
555                                                 /* out */ std::string* vdex_filename) {
556   // Normally, OatFileAssistant should not assume that there is an active runtime. However, we
557   // reference the runtime here. This is okay because we are in a static function that is unrelated
558   // to other parts of OatFileAssistant.
559   DCHECK(Runtime::Current() != nullptr);
560 
561   uint32_t checksum = adler32(0L, Z_NULL, 0);
562   for (const DexFile::Header* header : headers) {
563     checksum = adler32_combine(
564         checksum, header->checksum_, header->file_size_ - DexFile::kNumNonChecksumBytes);
565   }
566 
567   const std::string& data_dir = Runtime::Current()->GetProcessDataDirectory();
568   if (data_dir.empty() || Runtime::Current()->IsZygote()) {
569     *dex_location = StringPrintf("%s%u", kAnonymousDexPrefix, checksum);
570     return false;
571   }
572   *dex_location = StringPrintf("%s/%s%u.jar", data_dir.c_str(), kAnonymousDexPrefix, checksum);
573 
574   std::string odex_filename;
575   std::string error_msg;
576   if (!DexLocationToOdexFilename(*dex_location, isa, &odex_filename, &error_msg)) {
577     LOG(WARNING) << "Could not get odex filename for " << *dex_location << ": " << error_msg;
578     return false;
579   }
580 
581   *vdex_filename = GetVdexFilename(odex_filename);
582   return true;
583 }
584 
IsAnonymousVdexBasename(const std::string & basename)585 bool OatFileAssistant::IsAnonymousVdexBasename(const std::string& basename) {
586   DCHECK(basename.find('/') == std::string::npos);
587   // `basename` must have format: <kAnonymousDexPrefix><checksum><kVdexExtension>
588   if (basename.size() < strlen(kAnonymousDexPrefix) + strlen(kVdexExtension) + 1 ||
589       !basename.starts_with(kAnonymousDexPrefix) ||
590       !basename.ends_with(kVdexExtension)) {
591     return false;
592   }
593   // Check that all characters between the prefix and extension are decimal digits.
594   for (size_t i = strlen(kAnonymousDexPrefix); i < basename.size() - strlen(kVdexExtension); ++i) {
595     if (!std::isdigit(basename[i])) {
596       return false;
597     }
598   }
599   return true;
600 }
601 
DexLocationToOdexFilename(const std::string & location,InstructionSet isa,std::string * odex_filename,std::string * error_msg)602 bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location,
603                                                  InstructionSet isa,
604                                                  std::string* odex_filename,
605                                                  std::string* error_msg) {
606   CHECK(odex_filename != nullptr);
607   CHECK(error_msg != nullptr);
608 
609   // For a DEX file on /apex, check if there is an odex file on /system. If so, and the file exists,
610   // use it.
611   if (LocationIsOnApex(location)) {
612     const std::string system_file = GetSystemOdexFilenameForApex(location, isa);
613     if (OS::FileExists(system_file.c_str(), /*check_file_type=*/true)) {
614       *odex_filename = system_file;
615       return true;
616     } else if (errno != ENOENT) {
617       PLOG(ERROR) << "Could not check odex file " << system_file;
618     }
619   }
620 
621   // The odex file name is formed by replacing the dex_location extension with
622   // .odex and inserting an oat/<isa> directory. For example:
623   //   location = /foo/bar/baz.jar
624   //   odex_location = /foo/bar/oat/<isa>/baz.odex
625 
626   // Find the directory portion of the dex location and add the oat/<isa>
627   // directory.
628   size_t pos = location.rfind('/');
629   if (pos == std::string::npos) {
630     *error_msg = "Dex location " + location + " has no directory.";
631     return false;
632   }
633   std::string dir = location.substr(0, pos + 1);
634   // Add the oat directory.
635   dir += "oat";
636 
637   // Add the isa directory
638   dir += "/" + std::string(GetInstructionSetString(isa));
639 
640   // Get the base part of the file without the extension.
641   std::string file = location.substr(pos + 1);
642   pos = file.rfind('.');
643   std::string base = pos != std::string::npos ? file.substr(0, pos) : file;
644 
645   *odex_filename = dir + "/" + base + kOdexExtension;
646   return true;
647 }
648 
DexLocationToOatFilename(const std::string & location,InstructionSet isa,std::string * oat_filename,std::string * error_msg)649 bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
650                                                 InstructionSet isa,
651                                                 std::string* oat_filename,
652                                                 std::string* error_msg) {
653   DCHECK(Runtime::Current() != nullptr);
654   return DexLocationToOatFilename(
655       location, isa, Runtime::Current()->DenyArtApexDataFiles(), oat_filename, error_msg);
656 }
657 
DexLocationToOatFilename(const std::string & location,InstructionSet isa,bool deny_art_apex_data_files,std::string * oat_filename,std::string * error_msg)658 bool OatFileAssistant::DexLocationToOatFilename(const std::string& location,
659                                                 InstructionSet isa,
660                                                 bool deny_art_apex_data_files,
661                                                 std::string* oat_filename,
662                                                 std::string* error_msg) {
663   CHECK(oat_filename != nullptr);
664   CHECK(error_msg != nullptr);
665 
666   // Check if `location` could have an oat file in the ART APEX data directory. If so, and the
667   // file exists, use it.
668   const std::string apex_data_file = GetApexDataOdexFilename(location, isa);
669   if (!apex_data_file.empty() && !deny_art_apex_data_files) {
670     if (OS::FileExists(apex_data_file.c_str(), /*check_file_type=*/true)) {
671       *oat_filename = apex_data_file;
672       return true;
673     } else if (errno != ENOENT) {
674       PLOG(ERROR) << "Could not check odex file " << apex_data_file;
675     }
676   }
677 
678   // If ANDROID_DATA is not set, return false instead of aborting.
679   // This can occur for preopt when using a class loader context.
680   if (GetAndroidDataSafe(error_msg).empty()) {
681     *error_msg = "GetAndroidDataSafe failed: " + *error_msg;
682     return false;
683   }
684 
685   std::string dalvik_cache;
686   bool have_android_data = false;
687   bool dalvik_cache_exists = false;
688   bool is_global_cache = false;
689   GetDalvikCache(GetInstructionSetString(isa),
690                  /*create_if_absent=*/true,
691                  &dalvik_cache,
692                  &have_android_data,
693                  &dalvik_cache_exists,
694                  &is_global_cache);
695   if (!dalvik_cache_exists) {
696     *error_msg = "Dalvik cache directory does not exist";
697     return false;
698   }
699 
700   // TODO: The oat file assistant should be the definitive place for
701   // determining the oat file name from the dex location, not
702   // GetDalvikCacheFilename.
703   return GetDalvikCacheFilename(location, dalvik_cache, oat_filename, error_msg);
704 }
705 
GetRequiredDexChecksum(std::optional<uint32_t> * checksum,std::string * error)706 bool OatFileAssistant::GetRequiredDexChecksum(std::optional<uint32_t>* checksum,
707                                               std::string* error) {
708   if (!required_dex_checksums_attempted_) {
709     required_dex_checksums_attempted_ = true;
710 
711     File file(zip_fd_, /*check_usage=*/false);
712     ArtDexFileLoader dex_loader(&file, dex_location_);
713     std::optional<uint32_t> checksum2;
714     std::string error2;
715     if (dex_loader.GetMultiDexChecksum(
716             &checksum2, &error2, &zip_file_only_contains_uncompressed_dex_)) {
717       cached_required_dex_checksums_ = checksum2;
718       cached_required_dex_checksums_error_ = std::nullopt;
719     } else {
720       cached_required_dex_checksums_ = std::nullopt;
721       cached_required_dex_checksums_error_ = error2;
722     }
723     file.Release();  // Don't close the file yet (we have only read the checksum).
724   }
725 
726   if (cached_required_dex_checksums_error_.has_value()) {
727     *error = cached_required_dex_checksums_error_.value();
728     DCHECK(!error->empty());
729     return false;
730   }
731 
732   if (!cached_required_dex_checksums_.has_value()) {
733     // The only valid case here is for APKs without dex files.
734     VLOG(oat) << "No dex file found in " << dex_location_;
735   }
736   *checksum = cached_required_dex_checksums_;
737   return true;
738 }
739 
ValidateBootClassPathChecksums(OatFileAssistantContext * ofa_context,InstructionSet isa,std::string_view oat_checksums,std::string_view oat_boot_class_path,std::string * error_msg)740 bool OatFileAssistant::ValidateBootClassPathChecksums(OatFileAssistantContext* ofa_context,
741                                                       InstructionSet isa,
742                                                       std::string_view oat_checksums,
743                                                       std::string_view oat_boot_class_path,
744                                                       /*out*/ std::string* error_msg) {
745   const std::vector<std::string>& bcp_locations =
746       ofa_context->GetRuntimeOptions().boot_class_path_locations;
747 
748   if (oat_checksums.empty() || oat_boot_class_path.empty()) {
749     *error_msg = oat_checksums.empty() ? "Empty checksums" : "Empty boot class path";
750     return false;
751   }
752 
753   size_t oat_bcp_size = gc::space::ImageSpace::CheckAndCountBCPComponents(
754       oat_boot_class_path, ArrayRef<const std::string>(bcp_locations), error_msg);
755   if (oat_bcp_size == static_cast<size_t>(-1)) {
756     DCHECK(!error_msg->empty());
757     return false;
758   }
759   DCHECK_LE(oat_bcp_size, bcp_locations.size());
760 
761   size_t bcp_index = 0;
762   size_t boot_image_index = 0;
763   bool found_d = false;
764 
765   while (bcp_index < oat_bcp_size) {
766     static_assert(gc::space::ImageSpace::kImageChecksumPrefix == 'i', "Format prefix check");
767     static_assert(gc::space::ImageSpace::kDexFileChecksumPrefix == 'd', "Format prefix check");
768     if (oat_checksums.starts_with("i") && !found_d) {
769       const std::vector<OatFileAssistantContext::BootImageInfo>& boot_image_info_list =
770           ofa_context->GetBootImageInfoList(isa);
771       if (boot_image_index >= boot_image_info_list.size()) {
772         *error_msg = StringPrintf("Missing boot image for %s, remaining checksums: %s",
773                                   bcp_locations[bcp_index].c_str(),
774                                   std::string(oat_checksums).c_str());
775         return false;
776       }
777 
778       const OatFileAssistantContext::BootImageInfo& boot_image_info =
779           boot_image_info_list[boot_image_index];
780       if (!ConsumePrefix(&oat_checksums, boot_image_info.checksum)) {
781         *error_msg = StringPrintf("Image checksum mismatch, expected %s to start with %s",
782                                   std::string(oat_checksums).c_str(),
783                                   boot_image_info.checksum.c_str());
784         return false;
785       }
786 
787       bcp_index += boot_image_info.component_count;
788       boot_image_index++;
789     } else if (oat_checksums.starts_with("d")) {
790       found_d = true;
791       const std::vector<std::string>* bcp_checksums =
792           ofa_context->GetBcpChecksums(bcp_index, error_msg);
793       if (bcp_checksums == nullptr) {
794         return false;
795       }
796       oat_checksums.remove_prefix(1u);
797       for (const std::string& checksum : *bcp_checksums) {
798         if (!ConsumePrefix(&oat_checksums, checksum)) {
799           *error_msg = StringPrintf(
800               "Dex checksum mismatch for bootclasspath file %s, expected %s to start with %s",
801               bcp_locations[bcp_index].c_str(),
802               std::string(oat_checksums).c_str(),
803               checksum.c_str());
804           return false;
805         }
806       }
807 
808       bcp_index++;
809     } else {
810       *error_msg = StringPrintf("Unexpected checksums, expected %s to start with %s",
811                                 std::string(oat_checksums).c_str(),
812                                 found_d ? "'d'" : "'i' or 'd'");
813       return false;
814     }
815 
816     if (bcp_index < oat_bcp_size) {
817       if (!ConsumePrefix(&oat_checksums, ":")) {
818         if (oat_checksums.empty()) {
819           *error_msg =
820               StringPrintf("Checksum too short, missing %zu components", oat_bcp_size - bcp_index);
821         } else {
822           *error_msg = StringPrintf("Missing ':' separator at start of %s",
823                                     std::string(oat_checksums).c_str());
824         }
825         return false;
826       }
827     }
828   }
829 
830   if (!oat_checksums.empty()) {
831     *error_msg =
832         StringPrintf("Checksum too long, unexpected tail: %s", std::string(oat_checksums).c_str());
833     return false;
834   }
835 
836   return true;
837 }
838 
ValidateBootClassPathChecksums(const OatFile & oat_file)839 bool OatFileAssistant::ValidateBootClassPathChecksums(const OatFile& oat_file) {
840   // Get the checksums and the BCP from the oat file.
841   const char* oat_boot_class_path_checksums =
842       oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
843   const char* oat_boot_class_path =
844       oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey);
845   if (oat_boot_class_path_checksums == nullptr || oat_boot_class_path == nullptr) {
846     return false;
847   }
848 
849   std::string error_msg;
850   bool result = ValidateBootClassPathChecksums(GetOatFileAssistantContext(),
851                                                isa_,
852                                                oat_boot_class_path_checksums,
853                                                oat_boot_class_path,
854                                                &error_msg);
855   if (!result) {
856     VLOG(oat) << "Failed to verify checksums of oat file " << oat_file.GetLocation()
857               << " error: " << error_msg;
858     return false;
859   }
860 
861   return true;
862 }
863 
IsPrimaryBootImageUsable()864 bool OatFileAssistant::IsPrimaryBootImageUsable() {
865   return !GetOatFileAssistantContext()->GetBootImageInfoList(isa_).empty();
866 }
867 
GetBestInfo()868 OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() {
869   ScopedTrace trace("GetBestInfo");
870   // TODO(calin): Document the side effects of class loading when
871   // running dalvikvm command line.
872   if (dex_parent_writable_ || UseFdToReadFiles()) {
873     // If the parent of the dex file is writable it means that we can
874     // create the odex file. In this case we unconditionally pick the odex
875     // as the best oat file. This corresponds to the regular use case when
876     // apps gets installed or when they load private, secondary dex file.
877     // For apps on the system partition the odex location will not be
878     // writable and thus the oat location might be more up to date.
879 
880     // If the odex is not useable, and we have a useable vdex, return the vdex
881     // instead.
882     VLOG(oat) << ART_FORMAT("GetBestInfo checking odex next to the dex file ({})",
883                             odex_.DisplayFilename());
884     if (!odex_.IsUseable()) {
885       VLOG(oat) << ART_FORMAT("GetBestInfo checking vdex next to the dex file ({})",
886                               vdex_for_odex_.DisplayFilename());
887       if (vdex_for_odex_.IsUseable()) {
888         return vdex_for_odex_;
889       }
890       VLOG(oat) << ART_FORMAT("GetBestInfo checking dm ({})", dm_for_odex_.DisplayFilename());
891       if (dm_for_odex_.IsUseable()) {
892         return dm_for_odex_;
893       }
894     }
895     return odex_;
896   }
897 
898   // We cannot write to the odex location. This must be a system app.
899 
900   // If the oat location is useable take it.
901   VLOG(oat) << ART_FORMAT("GetBestInfo checking odex in dalvik-cache ({})", oat_.DisplayFilename());
902   if (oat_.IsUseable()) {
903     return oat_;
904   }
905 
906   // The oat file is not useable but the odex file might be up to date.
907   // This is an indication that we are dealing with an up to date prebuilt
908   // (that doesn't need relocation).
909   VLOG(oat) << ART_FORMAT("GetBestInfo checking odex next to the dex file ({})",
910                           odex_.DisplayFilename());
911   if (odex_.IsUseable()) {
912     return odex_;
913   }
914 
915   // Look for a useable vdex file.
916   VLOG(oat) << ART_FORMAT("GetBestInfo checking vdex in dalvik-cache ({})",
917                           vdex_for_oat_.DisplayFilename());
918   if (vdex_for_oat_.IsUseable()) {
919     return vdex_for_oat_;
920   }
921   VLOG(oat) << ART_FORMAT("GetBestInfo checking vdex next to the dex file ({})",
922                           vdex_for_odex_.DisplayFilename());
923   if (vdex_for_odex_.IsUseable()) {
924     return vdex_for_odex_;
925   }
926   VLOG(oat) << ART_FORMAT("GetBestInfo checking dm ({})", dm_for_oat_.DisplayFilename());
927   if (dm_for_oat_.IsUseable()) {
928     return dm_for_oat_;
929   }
930   // TODO(jiakaiz): Is this the same as above?
931   VLOG(oat) << ART_FORMAT("GetBestInfo checking dm ({})", dm_for_odex_.DisplayFilename());
932   if (dm_for_odex_.IsUseable()) {
933     return dm_for_odex_;
934   }
935 
936   // We got into the worst situation here:
937   // - the oat location is not useable
938   // - the prebuild odex location is not up to date
939   // - the vdex-only file is not useable
940   // - and we don't have the original dex file anymore (stripped).
941   // Pick the odex if it exists, or the oat if not.
942   VLOG(oat) << "GetBestInfo no usable artifacts";
943   return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_;
944 }
945 
OpenImageSpace(const OatFile * oat_file)946 std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) {
947   DCHECK(oat_file != nullptr);
948   std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), kArtExtension);
949   if (art_file.empty()) {
950     return nullptr;
951   }
952   std::string error_msg;
953   std::unique_ptr<gc::space::ImageSpace> ret =
954       gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg);
955   if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) {
956     LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg;
957   }
958   return ret;
959 }
960 
OatFileInfo(OatFileAssistant * oat_file_assistant,bool is_oat_location)961 OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant,
962                                            bool is_oat_location)
963     : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location) {}
964 
IsOatLocation()965 bool OatFileAssistant::OatFileInfo::IsOatLocation() { return is_oat_location_; }
966 
Filename()967 const std::string* OatFileAssistant::OatFileInfo::Filename() {
968   return filename_provided_ ? &filename_ : nullptr;
969 }
970 
DisplayFilename()971 const char* OatFileAssistant::OatFileInfo::DisplayFilename() {
972   return filename_provided_ ? filename_.c_str() : "unknown";
973 }
974 
IsUseable()975 bool OatFileAssistant::OatFileInfo::IsUseable() {
976   ScopedTrace trace("IsUseable");
977   switch (Status()) {
978     case kOatCannotOpen:
979     case kOatDexOutOfDate:
980     case kOatContextOutOfDate:
981     case kOatBootImageOutOfDate:
982       return false;
983 
984     case kOatUpToDate:
985       return true;
986   }
987 }
988 
Status()989 OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() {
990   ScopedTrace trace("Status");
991   if (!status_attempted_) {
992     status_attempted_ = true;
993     const OatFile* file = GetFile();
994     if (file == nullptr) {
995       status_ = kOatCannotOpen;
996     } else {
997       status_ = oat_file_assistant_->GivenOatFileStatus(*file);
998       VLOG(oat) << file->GetLocation() << " is " << status_ << " with filter "
999                 << file->GetCompilerFilter();
1000     }
1001   }
1002   return status_;
1003 }
1004 
GetDexOptNeeded(CompilerFilter::Filter target_compiler_filter,const DexOptTrigger dexopt_trigger)1005 OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded(
1006     CompilerFilter::Filter target_compiler_filter, const DexOptTrigger dexopt_trigger) {
1007   if (IsUseable()) {
1008     return ShouldRecompileForFilter(target_compiler_filter, dexopt_trigger) ? kDex2OatForFilter :
1009                                                                               kNoDexOptNeeded;
1010   }
1011 
1012   // In this case, the oat file is not usable. If the caller doesn't seek for a better compiler
1013   // filter (e.g., the caller wants to downgrade), then we should not recompile.
1014   if (!dexopt_trigger.targetFilterIsBetter) {
1015     return kNoDexOptNeeded;
1016   }
1017 
1018   if (Status() == kOatBootImageOutOfDate) {
1019     return kDex2OatForBootImage;
1020   }
1021 
1022   std::string error_msg;
1023   std::optional<bool> has_dex_files = oat_file_assistant_->HasDexFiles(&error_msg);
1024   if (has_dex_files.has_value()) {
1025     if (*has_dex_files) {
1026       return kDex2OatFromScratch;
1027     } else {
1028       // No dex file, so there is nothing we need to do.
1029       return kNoDexOptNeeded;
1030     }
1031   } else {
1032     // Unable to open the dex file, so there is nothing we can do.
1033     LOG(WARNING) << error_msg;
1034     return kNoDexOptNeeded;
1035   }
1036 }
1037 
GetFile()1038 const OatFile* OatFileAssistant::OatFileInfo::GetFile() {
1039   CHECK(!file_released_) << "GetFile called after oat file released.";
1040   if (load_attempted_) {
1041     return file_.get();
1042   }
1043   load_attempted_ = true;
1044   if (!filename_provided_) {
1045     return nullptr;
1046   }
1047 
1048   if (LocationIsOnArtApexData(filename_) &&
1049       oat_file_assistant_->GetRuntimeOptions().deny_art_apex_data_files) {
1050     LOG(WARNING) << "OatFileAssistant rejected file " << filename_
1051                  << ": ART apexdata is untrusted.";
1052     return nullptr;
1053   }
1054 
1055   std::string error_msg;
1056   bool executable = oat_file_assistant_->load_executable_;
1057   if (filename_.ends_with(kVdexExtension)) {
1058     executable = false;
1059     // Check to see if there is a vdex file we can make use of.
1060     std::unique_ptr<VdexFile> vdex;
1061     if (use_fd_) {
1062       if (vdex_fd_ >= 0) {
1063         struct stat s;
1064         int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s));
1065         if (rc == -1) {
1066           error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno));
1067         } else {
1068           vdex = VdexFile::Open(vdex_fd_,
1069                                 s.st_size,
1070                                 filename_,
1071                                 /*writable=*/false,
1072                                 /*low_4gb=*/false,
1073                                 &error_msg);
1074         }
1075       }
1076     } else {
1077       vdex = VdexFile::Open(filename_,
1078                             /*writable=*/false,
1079                             /*low_4gb=*/false,
1080                             &error_msg);
1081     }
1082     if (vdex == nullptr) {
1083       VLOG(oat) << "unable to open vdex file " << filename_ << ": " << error_msg;
1084     } else {
1085       file_.reset(OatFile::OpenFromVdex(zip_fd_,
1086                                         std::move(vdex),
1087                                         oat_file_assistant_->dex_location_,
1088                                         oat_file_assistant_->context_,
1089                                         &error_msg));
1090     }
1091   } else if (filename_.ends_with(kDmExtension)) {
1092     executable = false;
1093     // Check to see if there is a vdex file we can make use of.
1094     std::unique_ptr<ZipArchive> dm_file(ZipArchive::Open(filename_.c_str(), &error_msg));
1095     if (dm_file != nullptr) {
1096       std::unique_ptr<VdexFile> vdex(VdexFile::OpenFromDm(filename_, *dm_file));
1097       if (vdex != nullptr) {
1098         file_.reset(OatFile::OpenFromVdex(zip_fd_,
1099                                           std::move(vdex),
1100                                           oat_file_assistant_->dex_location_,
1101                                           oat_file_assistant_->context_,
1102                                           &error_msg));
1103       }
1104     }
1105   } else {
1106     if (executable && oat_file_assistant_->only_load_trusted_executable_) {
1107       executable = LocationIsTrusted(filename_, /*trust_art_apex_data_files=*/true);
1108     }
1109     VLOG(oat) << "Loading " << filename_ << " with executable: " << executable;
1110 
1111     if (gPageSize != kMinPageSize) {
1112       LOG(WARNING) << "Loading odex files is only supported on devices with 4K page size";
1113       return nullptr;
1114     }
1115 
1116     if (use_fd_) {
1117       if (oat_fd_ >= 0 && vdex_fd_ >= 0) {
1118         ArrayRef<const std::string> dex_locations(&oat_file_assistant_->dex_location_,
1119                                                   /*size=*/1u);
1120         file_.reset(OatFile::Open(zip_fd_,
1121                                   vdex_fd_,
1122                                   oat_fd_,
1123                                   filename_,
1124                                   executable,
1125                                   /*low_4gb=*/false,
1126                                   dex_locations,
1127                                   /*dex_files=*/{},
1128                                   /*reservation=*/nullptr,
1129                                   &error_msg));
1130       }
1131     } else {
1132       file_.reset(OatFile::Open(/*zip_fd=*/-1,
1133                                 filename_,
1134                                 filename_,
1135                                 executable,
1136                                 /*low_4gb=*/false,
1137                                 oat_file_assistant_->dex_location_,
1138                                 &error_msg));
1139     }
1140   }
1141   if (file_.get() == nullptr) {
1142     VLOG(oat) << "OatFileAssistant test for existing oat file " << filename_ << ": " << error_msg;
1143   } else {
1144     VLOG(oat) << "Successfully loaded " << filename_ << " with executable: " << executable;
1145   }
1146   return file_.get();
1147 }
1148 
ShouldRecompileForFilter(CompilerFilter::Filter target,const DexOptTrigger dexopt_trigger)1149 bool OatFileAssistant::OatFileInfo::ShouldRecompileForFilter(CompilerFilter::Filter target,
1150                                                              const DexOptTrigger dexopt_trigger) {
1151   const OatFile* file = GetFile();
1152   DCHECK(file != nullptr);
1153 
1154   if (CompilerFilter::IsBetter(target, CompilerFilter::kVerify) && gPageSize != kMinPageSize) {
1155     // Prevent infinite recompilations during background dexopt on 16K page devices.
1156     VLOG(oat) << "Adjusting target filter to 'verify' because loading odex files is only supported "
1157                  "on devices with 4K page size";
1158     target = CompilerFilter::kVerify;
1159   }
1160 
1161   CompilerFilter::Filter current = file->GetCompilerFilter();
1162   if (dexopt_trigger.targetFilterIsBetter && CompilerFilter::IsBetter(target, current)) {
1163     VLOG(oat) << ART_FORMAT("Should recompile: targetFilterIsBetter (current: {}, target: {})",
1164                             CompilerFilter::NameOfFilter(current),
1165                             CompilerFilter::NameOfFilter(target));
1166     return true;
1167   }
1168   if (dexopt_trigger.targetFilterIsSame && current == target) {
1169     VLOG(oat) << ART_FORMAT("Should recompile: targetFilterIsSame (current: {}, target: {})",
1170                             CompilerFilter::NameOfFilter(current),
1171                             CompilerFilter::NameOfFilter(target));
1172     return true;
1173   }
1174   if (dexopt_trigger.targetFilterIsWorse && CompilerFilter::IsBetter(current, target)) {
1175     VLOG(oat) << ART_FORMAT("Should recompile: targetFilterIsWorse (current: {}, target: {})",
1176                             CompilerFilter::NameOfFilter(current),
1177                             CompilerFilter::NameOfFilter(target));
1178     return true;
1179   }
1180 
1181   // Don't regress the compiler filter for the triggers handled below.
1182   if (CompilerFilter::IsBetter(current, target)) {
1183     VLOG(oat) << "Should not recompile: current filter is better";
1184     return false;
1185   }
1186 
1187   if (dexopt_trigger.primaryBootImageBecomesUsable &&
1188       CompilerFilter::IsAotCompilationEnabled(current)) {
1189     // If the oat file has been compiled without an image, and the runtime is
1190     // now running with an image loaded from disk, return that we need to
1191     // re-compile. The recompilation will generate a better oat file, and with an app
1192     // image for profile guided compilation.
1193     // However, don't recompile for "verify". Although verification depends on the boot image, the
1194     // penalty of being verified without a boot image is low. Consider the case where a dex file
1195     // is verified by "ab-ota", we don't want it to be re-verified by "boot-after-ota".
1196     const char* oat_boot_class_path_checksums =
1197         file->GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
1198     if (oat_boot_class_path_checksums != nullptr &&
1199         oat_boot_class_path_checksums[0] != 'i' &&
1200         oat_file_assistant_->IsPrimaryBootImageUsable()) {
1201       DCHECK(!file->GetOatHeader().RequiresImage());
1202       VLOG(oat) << "Should recompile: primaryBootImageBecomesUsable";
1203       return true;
1204     }
1205   }
1206 
1207   if (dexopt_trigger.needExtraction && !file->ContainsDexCode() &&
1208       !oat_file_assistant_->ZipFileOnlyContainsUncompressedDex()) {
1209     VLOG(oat) << "Should recompile: needExtraction";
1210     return true;
1211   }
1212 
1213   VLOG(oat) << "Should not recompile";
1214   return false;
1215 }
1216 
ClassLoaderContextIsOkay(const OatFile & oat_file) const1217 bool OatFileAssistant::ClassLoaderContextIsOkay(const OatFile& oat_file) const {
1218   if (context_ == nullptr) {
1219     // The caller requests to skip the check.
1220     return true;
1221   }
1222 
1223   if (oat_file.IsBackedByVdexOnly()) {
1224     // Only a vdex file, we don't depend on the class loader context.
1225     return true;
1226   }
1227 
1228   if (!CompilerFilter::IsVerificationEnabled(oat_file.GetCompilerFilter())) {
1229     // If verification is not enabled we don't need to verify the class loader context and we
1230     // assume it's ok.
1231     return true;
1232   }
1233 
1234   ClassLoaderContext::VerificationResult matches =
1235       context_->VerifyClassLoaderContextMatch(oat_file.GetClassLoaderContext(),
1236                                               /*verify_names=*/true,
1237                                               /*verify_checksums=*/true);
1238   if (matches == ClassLoaderContext::VerificationResult::kMismatch) {
1239     VLOG(oat) << "ClassLoaderContext check failed. Context was " << oat_file.GetClassLoaderContext()
1240               << ". The expected context is "
1241               << context_->EncodeContextForOatFile(android::base::Dirname(dex_location_));
1242     return false;
1243   }
1244   return true;
1245 }
1246 
IsExecutable()1247 bool OatFileAssistant::OatFileInfo::IsExecutable() {
1248   const OatFile* file = GetFile();
1249   return (file != nullptr && file->IsExecutable());
1250 }
1251 
Reset()1252 void OatFileAssistant::OatFileInfo::Reset() {
1253   load_attempted_ = false;
1254   file_.reset();
1255   status_attempted_ = false;
1256 }
1257 
Reset(const std::string & filename,bool use_fd,int zip_fd,int vdex_fd,int oat_fd)1258 void OatFileAssistant::OatFileInfo::Reset(
1259     const std::string& filename, bool use_fd, int zip_fd, int vdex_fd, int oat_fd) {
1260   filename_provided_ = true;
1261   filename_ = filename;
1262   use_fd_ = use_fd;
1263   zip_fd_ = zip_fd;
1264   vdex_fd_ = vdex_fd;
1265   oat_fd_ = oat_fd;
1266   Reset();
1267 }
1268 
ReleaseFile()1269 std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() {
1270   file_released_ = true;
1271   return std::move(file_);
1272 }
1273 
ReleaseFileForUse()1274 std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() {
1275   ScopedTrace trace("ReleaseFileForUse");
1276   if (Status() == kOatUpToDate) {
1277     return ReleaseFile();
1278   }
1279 
1280   return std::unique_ptr<OatFile>();
1281 }
1282 
1283 // Check if we should reject vdex containing cdex code as part of the cdex
1284 // deprecation.
1285 // TODO(b/256664509): Clean this up.
CheckDisableCompactDex()1286 bool OatFileAssistant::OatFileInfo::CheckDisableCompactDex() {
1287   const OatFile* oat_file = GetFile();
1288   if (oat_file == nullptr) {
1289     return false;
1290   }
1291   const VdexFile* vdex_file = oat_file->GetVdexFile();
1292   return vdex_file != nullptr && vdex_file->HasDexSection() &&
1293          !vdex_file->HasOnlyStandardDexFiles();
1294 }
1295 
1296 // TODO(calin): we could provide a more refined status here
1297 // (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to
1298 // track more experiments but adds extra complexity.
GetOptimizationStatus(const std::string & filename,InstructionSet isa,std::string * out_compilation_filter,std::string * out_compilation_reason,OatFileAssistantContext * ofa_context)1299 void OatFileAssistant::GetOptimizationStatus(const std::string& filename,
1300                                              InstructionSet isa,
1301                                              std::string* out_compilation_filter,
1302                                              std::string* out_compilation_reason,
1303                                              OatFileAssistantContext* ofa_context) {
1304   // It may not be possible to load an oat file executable (e.g., selinux restrictions). Load
1305   // non-executable and check the status manually.
1306   OatFileAssistant oat_file_assistant(filename.c_str(),
1307                                       isa,
1308                                       /*context=*/nullptr,
1309                                       /*load_executable=*/false,
1310                                       /*only_load_trusted_executable=*/false,
1311                                       ofa_context);
1312   std::string out_odex_location;  // unused
1313   std::string out_odex_status;    // unused
1314   Location out_location;          // unused
1315   oat_file_assistant.GetOptimizationStatus(&out_odex_location,
1316                                            out_compilation_filter,
1317                                            out_compilation_reason,
1318                                            &out_odex_status,
1319                                            &out_location);
1320 }
1321 
GetOptimizationStatus(std::string * out_odex_location,std::string * out_compilation_filter,std::string * out_compilation_reason,std::string * out_odex_status,Location * out_location)1322 void OatFileAssistant::GetOptimizationStatus(std::string* out_odex_location,
1323                                              std::string* out_compilation_filter,
1324                                              std::string* out_compilation_reason,
1325                                              std::string* out_odex_status,
1326                                              Location* out_location) {
1327   OatFileInfo& oat_file_info = GetBestInfo();
1328   const OatFile* oat_file = oat_file_info.GetFile();
1329   *out_location = GetLocation(oat_file_info);
1330 
1331   if (oat_file == nullptr) {
1332     std::string error_msg;
1333     std::optional<bool> has_dex_files = HasDexFiles(&error_msg);
1334     if (!has_dex_files.has_value()) {
1335       *out_odex_location = "error";
1336       *out_compilation_filter = "unknown";
1337       *out_compilation_reason = "unknown";
1338       // This happens when we cannot open the APK/JAR.
1339       *out_odex_status = "io-error-no-apk";
1340     } else if (!has_dex_files.value()) {
1341       *out_odex_location = "none";
1342       *out_compilation_filter = "unknown";
1343       *out_compilation_reason = "unknown";
1344       // This happens when the APK/JAR doesn't contain any DEX file.
1345       *out_odex_status = "no-dex-code";
1346     } else {
1347       *out_odex_location = "error";
1348       *out_compilation_filter = "run-from-apk";
1349       *out_compilation_reason = "unknown";
1350       // This mostly happens when we cannot open the oat file.
1351       // Note that it's different than kOatCannotOpen.
1352       // TODO: The design of getting the BestInfo is not ideal, as it's not very clear what's the
1353       // difference between a nullptr and kOatcannotOpen. The logic should be revised and improved.
1354       *out_odex_status = "io-error-no-oat";
1355     }
1356     return;
1357   }
1358 
1359   *out_odex_location = oat_file->GetLocation();
1360   OatStatus status = oat_file_info.Status();
1361   const char* reason = oat_file->GetCompilationReason();
1362   *out_compilation_reason = reason == nullptr ? "unknown" : reason;
1363 
1364   // If the oat file is invalid, the vdex file will be picked, so the status is `kOatUpToDate`. If
1365   // the vdex file is also invalid, then either `oat_file` is nullptr, or `status` is
1366   // `kOatDexOutOfDate`.
1367   DCHECK(status == kOatUpToDate || status == kOatDexOutOfDate);
1368 
1369   switch (status) {
1370     case kOatUpToDate:
1371       *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter());
1372       *out_odex_status = "up-to-date";
1373       return;
1374 
1375     case kOatCannotOpen:
1376     case kOatBootImageOutOfDate:
1377     case kOatContextOutOfDate:
1378       // These should never happen, but be robust.
1379       *out_compilation_filter = "unexpected";
1380       *out_compilation_reason = "unexpected";
1381       *out_odex_status = "unexpected";
1382       return;
1383 
1384     case kOatDexOutOfDate:
1385       *out_compilation_filter = "run-from-apk-fallback";
1386       *out_odex_status = "apk-more-recent";
1387       return;
1388   }
1389   LOG(FATAL) << "Unreachable";
1390   UNREACHABLE();
1391 }
1392 
ZipFileOnlyContainsUncompressedDex()1393 bool OatFileAssistant::ZipFileOnlyContainsUncompressedDex() {
1394   // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums.
1395   std::optional<uint32_t> checksum;
1396   std::string error_msg;
1397   if (!GetRequiredDexChecksum(&checksum, &error_msg)) {
1398     LOG(ERROR) << error_msg;
1399   }
1400   return zip_file_only_contains_uncompressed_dex_;
1401 }
1402 
GetLocation(OatFileInfo & info)1403 OatFileAssistant::Location OatFileAssistant::GetLocation(OatFileInfo& info) {
1404   if (info.IsUseable()) {
1405     if (&info == &dm_for_oat_ || &info == &dm_for_odex_) {
1406       return kLocationDm;
1407     } else if (info.IsOatLocation()) {
1408       return kLocationOat;
1409     } else {
1410       return kLocationOdex;
1411     }
1412   } else {
1413     return kLocationNoneOrError;
1414   }
1415 }
1416 
1417 }  // namespace art
1418