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