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