Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 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 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 19 | #include <sstream> |
| 20 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 21 | #include <sys/stat.h> |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 22 | |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 24 | #include "android-base/strings.h" |
| 25 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 26 | #include "base/file_utils.h" |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 27 | #include "base/logging.h" // For VLOG. |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 28 | #include "base/os.h" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 29 | #include "base/stl_util.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 30 | #include "base/utils.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 31 | #include "class_linker.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 32 | #include "compiler_filter.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 33 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 34 | #include "dex/dex_file_loader.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 35 | #include "exec_utils.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 36 | #include "gc/heap.h" |
| 37 | #include "gc/space/image_space.h" |
| 38 | #include "image.h" |
| 39 | #include "oat.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 40 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 41 | #include "scoped_thread_state_change-inl.h" |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 42 | #include "vdex_file.h" |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 43 | #include "class_loader_context.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 44 | |
| 45 | namespace art { |
| 46 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 47 | using android::base::StringPrintf; |
| 48 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 49 | std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) { |
| 50 | switch (status) { |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 51 | case OatFileAssistant::kOatCannotOpen: |
| 52 | stream << "kOatCannotOpen"; |
| 53 | break; |
| 54 | case OatFileAssistant::kOatDexOutOfDate: |
| 55 | stream << "kOatDexOutOfDate"; |
| 56 | break; |
| 57 | case OatFileAssistant::kOatBootImageOutOfDate: |
| 58 | stream << "kOatBootImageOutOfDate"; |
| 59 | break; |
| 60 | case OatFileAssistant::kOatRelocationOutOfDate: |
| 61 | stream << "kOatRelocationOutOfDate"; |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 62 | break; |
| 63 | case OatFileAssistant::kOatUpToDate: |
| 64 | stream << "kOatUpToDate"; |
| 65 | break; |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 66 | default: |
| 67 | UNREACHABLE(); |
| 68 | } |
| 69 | |
| 70 | return stream; |
| 71 | } |
| 72 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 73 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 74 | const InstructionSet isa, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 75 | bool load_executable, |
| 76 | bool only_load_system_executable) |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 77 | : OatFileAssistant(dex_location, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 78 | isa, |
| 79 | load_executable, |
| 80 | only_load_system_executable, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 81 | -1 /* vdex_fd */, |
| 82 | -1 /* oat_fd */, |
| 83 | -1 /* zip_fd */) {} |
| 84 | |
| 85 | |
| 86 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 87 | const InstructionSet isa, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 88 | bool load_executable, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 89 | bool only_load_system_executable, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 90 | int vdex_fd, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 91 | int oat_fd, |
| 92 | int zip_fd) |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 93 | : isa_(isa), |
| 94 | load_executable_(load_executable), |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 95 | only_load_system_executable_(only_load_system_executable), |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 96 | odex_(this, /*is_oat_location*/ false), |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 97 | oat_(this, /*is_oat_location*/ true), |
| 98 | zip_fd_(zip_fd) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 99 | CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location"; |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 100 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 101 | if (zip_fd < 0) { |
| 102 | CHECK_LE(oat_fd, 0) << "zip_fd must be provided with valid oat_fd. zip_fd=" << zip_fd |
| 103 | << " oat_fd=" << oat_fd; |
| 104 | CHECK_LE(vdex_fd, 0) << "zip_fd must be provided with valid vdex_fd. zip_fd=" << zip_fd |
| 105 | << " vdex_fd=" << vdex_fd;; |
| 106 | } |
| 107 | |
Calin Juravle | 099f8d6 | 2017-09-05 19:04:04 -0700 | [diff] [blame] | 108 | dex_location_.assign(dex_location); |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 109 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 110 | if (load_executable_ && isa != kRuntimeISA) { |
| 111 | LOG(WARNING) << "OatFileAssistant: Load executable specified, " |
| 112 | << "but isa is not kRuntimeISA. Will not attempt to load executable."; |
| 113 | load_executable_ = false; |
| 114 | } |
| 115 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 116 | // Get the odex filename. |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 117 | std::string error_msg; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 118 | std::string odex_file_name; |
| 119 | if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) { |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 120 | odex_.Reset(odex_file_name, UseFdToReadFiles(), vdex_fd, oat_fd); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 121 | } else { |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 122 | LOG(WARNING) << "Failed to determine odex file name: " << error_msg; |
| 123 | } |
| 124 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 125 | if (!UseFdToReadFiles()) { |
| 126 | // Get the oat filename. |
| 127 | std::string oat_file_name; |
| 128 | if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) { |
| 129 | oat_.Reset(oat_file_name, false /* use_fd */); |
| 130 | } else { |
| 131 | LOG(WARNING) << "Failed to determine oat file name for dex location " |
| 132 | << dex_location_ << ": " << error_msg; |
| 133 | } |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Check if the dex directory is writable. |
| 137 | // This will be needed in most uses of OatFileAssistant and so it's OK to |
| 138 | // compute it eagerly. (the only use which will not make use of it is |
| 139 | // OatFileAssistant::GetStatusDump()) |
| 140 | size_t pos = dex_location_.rfind('/'); |
| 141 | if (pos == std::string::npos) { |
| 142 | LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 143 | } else if (!UseFdToReadFiles()) { |
| 144 | // We cannot test for parent access when using file descriptors. That's ok |
| 145 | // because in this case we will always pick the odex file anyway. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 146 | std::string parent = dex_location_.substr(0, pos); |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 147 | if (access(parent.c_str(), W_OK) == 0) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 148 | dex_parent_writable_ = true; |
| 149 | } else { |
| 150 | VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno); |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 151 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 152 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | OatFileAssistant::~OatFileAssistant() { |
| 156 | // Clean up the lock file. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 157 | if (flock_.get() != nullptr) { |
| 158 | unlink(flock_->GetPath().c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 162 | bool OatFileAssistant::UseFdToReadFiles() { |
| 163 | return zip_fd_ >= 0; |
| 164 | } |
| 165 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 166 | bool OatFileAssistant::IsInBootClassPath() { |
| 167 | // Note: We check the current boot class path, regardless of the ISA |
| 168 | // specified by the user. This is okay, because the boot class path should |
| 169 | // be the same for all ISAs. |
| 170 | // TODO: Can we verify the boot class path is the same for all ISAs? |
| 171 | Runtime* runtime = Runtime::Current(); |
| 172 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 173 | const auto& boot_class_path = class_linker->GetBootClassPath(); |
| 174 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 175 | if (boot_class_path[i]->GetLocation() == dex_location_) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 176 | VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path"; |
| 177 | return true; |
| 178 | } |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | bool OatFileAssistant::Lock(std::string* error_msg) { |
| 184 | CHECK(error_msg != nullptr); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 185 | CHECK(flock_.get() == nullptr) << "OatFileAssistant::Lock already acquired"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 186 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 187 | // Note the lock will only succeed for secondary dex files and in test |
| 188 | // environment. |
| 189 | // |
| 190 | // The lock *will fail* for all primary apks in a production environment. |
| 191 | // The app does not have permissions to create locks next to its dex location |
| 192 | // (be it system, data or vendor parition). We also cannot use the odex or |
| 193 | // oat location for the same reasoning. |
| 194 | // |
| 195 | // This is best effort and if it fails it's unlikely that we will be able |
| 196 | // to generate oat files anyway. |
| 197 | std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 198 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 199 | flock_ = LockedFile::Open(lock_file_name.c_str(), error_msg); |
| 200 | if (flock_.get() == nullptr) { |
Vladimir Marko | 66fdcbd | 2016-04-05 14:19:08 +0100 | [diff] [blame] | 201 | unlink(lock_file_name.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 207 | int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, |
| 208 | bool profile_changed, |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 209 | bool downgrade, |
| 210 | ClassLoaderContext* class_loader_context) { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 211 | OatFileInfo& info = GetBestInfo(); |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 212 | DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, |
| 213 | profile_changed, |
| 214 | downgrade, |
| 215 | class_loader_context); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 216 | if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) { |
| 217 | return dexopt_needed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 218 | } |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 219 | return -dexopt_needed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 222 | // Figure out the currently specified compile filter option in the runtime. |
| 223 | // Returns true on success, false if the compiler filter is invalid, in which |
| 224 | // case error_msg describes the problem. |
| 225 | static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter, |
| 226 | std::string* error_msg) { |
| 227 | CHECK(filter != nullptr); |
| 228 | CHECK(error_msg != nullptr); |
| 229 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 230 | *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 231 | for (StringPiece option : Runtime::Current()->GetCompilerOptions()) { |
| 232 | if (option.starts_with("--compiler-filter=")) { |
| 233 | const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data(); |
| 234 | if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) { |
| 235 | *error_msg = std::string("Unknown --compiler-filter value: ") |
| 236 | + std::string(compiler_filter_string); |
| 237 | return false; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | return true; |
| 242 | } |
| 243 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 244 | bool OatFileAssistant::IsUpToDate() { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 245 | return GetBestInfo().Status() == kOatUpToDate; |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 248 | OatFileAssistant::ResultOfAttemptToUpdate |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 249 | OatFileAssistant::MakeUpToDate(bool profile_changed, |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 250 | ClassLoaderContext* class_loader_context, |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 251 | std::string* error_msg) { |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 252 | // The method doesn't use zip_fd_ and directly opens dex files at dex_locations_. |
| 253 | CHECK_EQ(-1, zip_fd_) << "MakeUpToDate should not be called with zip_fd"; |
| 254 | |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 255 | CompilerFilter::Filter target; |
| 256 | if (!GetRuntimeCompilerFilterOption(&target, error_msg)) { |
| 257 | return kUpdateNotAttempted; |
| 258 | } |
| 259 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 260 | OatFileInfo& info = GetBestInfo(); |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 261 | // TODO(calin, jeffhao): the context should really be passed to GetDexOptNeeded: b/62269291. |
| 262 | // This is actually not trivial in the current logic as it will interact with the collision |
| 263 | // check: |
| 264 | // - currently, if the context does not match but we have no collisions we still accept the |
| 265 | // oat file. |
| 266 | // - if GetDexOptNeeded would return kDex2OatFromScratch for a context mismatch and we make |
| 267 | // the oat code up to date the collision check becomes useless. |
| 268 | // - however, MakeUpToDate will not always succeed (e.g. for primary apks, or for dex files |
| 269 | // loaded in other processes). So it boils down to how far do we want to complicate |
| 270 | // the logic in order to enable the use of oat files. Maybe its time to try simplify it. |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 271 | switch (info.GetDexOptNeeded( |
| 272 | target, profile_changed, /*downgrade*/ false, class_loader_context)) { |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 273 | case kNoDexOptNeeded: |
| 274 | return kUpdateSucceeded; |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 275 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 276 | // TODO: For now, don't bother with all the different ways we can call |
| 277 | // dex2oat to generate the oat file. Always generate the oat file as if it |
| 278 | // were kDex2OatFromScratch. |
| 279 | case kDex2OatFromScratch: |
| 280 | case kDex2OatForBootImage: |
| 281 | case kDex2OatForRelocation: |
| 282 | case kDex2OatForFilter: |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 283 | return GenerateOatFileNoChecks(info, target, class_loader_context, error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 284 | } |
| 285 | UNREACHABLE(); |
| 286 | } |
| 287 | |
| 288 | std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 289 | return GetBestInfo().ReleaseFileForUse(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 292 | std::string OatFileAssistant::GetStatusDump() { |
| 293 | std::ostringstream status; |
| 294 | bool oat_file_exists = false; |
| 295 | bool odex_file_exists = false; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 296 | if (oat_.Status() != kOatCannotOpen) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 297 | // If we can open the file, Filename should not return null. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 298 | CHECK(oat_.Filename() != nullptr); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 299 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 300 | oat_file_exists = true; |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 301 | status << *oat_.Filename() << "[status=" << oat_.Status() << ", "; |
| 302 | const OatFile* file = oat_.GetFile(); |
| 303 | if (file == nullptr) { |
| 304 | // If the file is null even though the status is not kOatCannotOpen, it |
| 305 | // means we must have a vdex file with no corresponding oat file. In |
| 306 | // this case we cannot determine the compilation filter. Indicate that |
| 307 | // we have only the vdex file instead. |
| 308 | status << "vdex-only"; |
| 309 | } else { |
| 310 | status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter()); |
| 311 | } |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 314 | if (odex_.Status() != kOatCannotOpen) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 315 | // If we can open the file, Filename should not return null. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 316 | CHECK(odex_.Filename() != nullptr); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 317 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 318 | odex_file_exists = true; |
| 319 | if (oat_file_exists) { |
| 320 | status << "] "; |
| 321 | } |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 322 | status << *odex_.Filename() << "[status=" << odex_.Status() << ", "; |
| 323 | const OatFile* file = odex_.GetFile(); |
| 324 | if (file == nullptr) { |
| 325 | status << "vdex-only"; |
| 326 | } else { |
| 327 | status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter()); |
| 328 | } |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | if (!oat_file_exists && !odex_file_exists) { |
| 332 | status << "invalid["; |
| 333 | } |
| 334 | |
| 335 | status << "]"; |
| 336 | return status.str(); |
| 337 | } |
| 338 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 339 | std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles( |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 340 | const OatFile &oat_file, const char *dex_location) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 341 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 342 | if (LoadDexFiles(oat_file, dex_location, &dex_files)) { |
| 343 | return dex_files; |
| 344 | } else { |
| 345 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 346 | } |
| 347 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 348 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 349 | bool OatFileAssistant::LoadDexFiles( |
| 350 | const OatFile &oat_file, |
| 351 | const std::string& dex_location, |
| 352 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 353 | // Load the main dex file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 354 | std::string error_msg; |
| 355 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile( |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 356 | dex_location.c_str(), nullptr, &error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 357 | if (oat_dex_file == nullptr) { |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 358 | LOG(WARNING) << error_msg; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 359 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 362 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 363 | if (dex_file.get() == nullptr) { |
| 364 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 365 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 366 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 367 | out_dex_files->push_back(std::move(dex_file)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 368 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 369 | // Load the rest of the multidex entries |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 370 | for (size_t i = 1;; i++) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 371 | std::string multidex_dex_location = DexFileLoader::GetMultiDexLocation(i, dex_location.c_str()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 372 | oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 373 | if (oat_dex_file == nullptr) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 374 | // There are no more multidex entries to load. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 375 | break; |
| 376 | } |
| 377 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 378 | dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 379 | if (dex_file.get() == nullptr) { |
| 380 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 381 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 382 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 383 | out_dex_files->push_back(std::move(dex_file)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 384 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 385 | return true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 388 | bool OatFileAssistant::HasOriginalDexFiles() { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 389 | // Ensure GetRequiredDexChecksums has been run so that |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 390 | // has_original_dex_files_ is initialized. We don't care about the result of |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 391 | // GetRequiredDexChecksums. |
| 392 | GetRequiredDexChecksums(); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 393 | return has_original_dex_files_; |
| 394 | } |
| 395 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 396 | OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 397 | return odex_.Status(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 400 | OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 401 | return oat_.Status(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 404 | bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 405 | const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums(); |
| 406 | if (required_dex_checksums == nullptr) { |
| 407 | LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date."; |
| 408 | return true; |
| 409 | } |
| 410 | |
Nicolas Geoffray | 3a29355 | 2018-03-02 10:52:16 +0000 | [diff] [blame^] | 411 | uint32_t number_of_dex_files = file.GetVerifierDepsHeader().GetNumberOfDexFiles(); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 412 | if (required_dex_checksums->size() != number_of_dex_files) { |
| 413 | *error_msg = StringPrintf("expected %zu dex files but found %u", |
| 414 | required_dex_checksums->size(), |
| 415 | number_of_dex_files); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 416 | return false; |
Andreas Gampe | f8cd890 | 2017-01-18 16:05:01 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 419 | for (uint32_t i = 0; i < number_of_dex_files; i++) { |
| 420 | uint32_t expected_checksum = (*required_dex_checksums)[i]; |
| 421 | uint32_t actual_checksum = file.GetLocationChecksum(i); |
| 422 | if (expected_checksum != actual_checksum) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 423 | std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 424 | *error_msg = StringPrintf("Dex checksum does not match for dex: %s." |
| 425 | "Expected: %u, actual: %u", |
| 426 | dex.c_str(), |
| 427 | expected_checksum, |
| 428 | actual_checksum); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 429 | return false; |
| 430 | } |
| 431 | } |
| 432 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 433 | return true; |
| 434 | } |
| 435 | |
| 436 | bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 437 | const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums(); |
| 438 | if (required_dex_checksums == nullptr) { |
| 439 | LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date."; |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount(); |
| 444 | if (required_dex_checksums->size() != number_of_dex_files) { |
| 445 | *error_msg = StringPrintf("expected %zu dex files but found %u", |
| 446 | required_dex_checksums->size(), |
| 447 | number_of_dex_files); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 448 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 451 | for (uint32_t i = 0; i < number_of_dex_files; i++) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 452 | std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 453 | uint32_t expected_checksum = (*required_dex_checksums)[i]; |
| 454 | const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr); |
| 455 | if (oat_dex_file == nullptr) { |
| 456 | *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str()); |
| 457 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 458 | } |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 459 | uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum(); |
| 460 | if (expected_checksum != actual_checksum) { |
| 461 | VLOG(oat) << "Dex checksum does not match for dex: " << dex |
| 462 | << ". Expected: " << expected_checksum |
| 463 | << ", Actual: " << actual_checksum; |
| 464 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 465 | } |
| 466 | } |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 467 | return true; |
| 468 | } |
| 469 | |
| 470 | OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) { |
| 471 | // Verify the ART_USE_READ_BARRIER state. |
| 472 | // TODO: Don't fully reject files due to read barrier state. If they contain |
| 473 | // compiled code and are otherwise okay, we should return something like |
| 474 | // kOatRelocationOutOfDate. If they don't contain compiled code, the read |
| 475 | // barrier state doesn't matter. |
| 476 | const bool is_cc = file.GetOatHeader().IsConcurrentCopying(); |
| 477 | constexpr bool kRuntimeIsCC = kUseReadBarrier; |
| 478 | if (is_cc != kRuntimeIsCC) { |
| 479 | return kOatCannotOpen; |
| 480 | } |
| 481 | |
| 482 | // Verify the dex checksum. |
| 483 | std::string error_msg; |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 484 | VdexFile* vdex = file.GetVdexFile(); |
| 485 | if (!DexChecksumUpToDate(*vdex, &error_msg)) { |
| 486 | LOG(ERROR) << error_msg; |
| 487 | return kOatDexOutOfDate; |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 488 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 489 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 490 | CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter(); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 491 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 492 | // Verify the image checksum |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 493 | if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) { |
| 494 | const ImageInfo* image_info = GetImageInfo(); |
| 495 | if (image_info == nullptr) { |
| 496 | VLOG(oat) << "No image for oat image checksum to match against."; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 497 | |
Richard Uhler | 76f5cb6 | 2016-04-04 13:30:16 -0700 | [diff] [blame] | 498 | if (HasOriginalDexFiles()) { |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 499 | return kOatBootImageOutOfDate; |
Richard Uhler | 76f5cb6 | 2016-04-04 13:30:16 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | // If there is no original dex file to fall back to, grudgingly accept |
| 503 | // the oat file. This could technically lead to crashes, but there's no |
| 504 | // way we could find a better oat file to use for this dex location, |
| 505 | // and it's better than being stuck in a boot loop with no way out. |
| 506 | // The problem will hopefully resolve itself the next time the runtime |
| 507 | // starts up. |
| 508 | LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. " |
| 509 | << "Allow oat file use. This is potentially dangerous."; |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 510 | } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) { |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 511 | VLOG(oat) << "Oat image checksum does not match image checksum."; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 512 | return kOatBootImageOutOfDate; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 513 | } |
| 514 | } else { |
| 515 | VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 518 | // zip_file_only_contains_uncompressed_dex_ is only set during fetching the dex checksums. |
| 519 | DCHECK(required_dex_checksums_attempted_); |
| 520 | if (only_load_system_executable_ && |
| 521 | !LocationIsOnSystem(file.GetLocation().c_str()) && |
| 522 | file.ContainsDexCode() && |
| 523 | zip_file_only_contains_uncompressed_dex_) { |
| 524 | LOG(ERROR) << "Not loading " |
| 525 | << dex_location_ |
| 526 | << ": oat file has dex code, but APK has uncompressed dex code"; |
| 527 | return kOatDexOutOfDate; |
| 528 | } |
| 529 | |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 530 | if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) { |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 531 | if (!file.IsPic()) { |
| 532 | const ImageInfo* image_info = GetImageInfo(); |
| 533 | if (image_info == nullptr) { |
| 534 | VLOG(oat) << "No image to check oat relocation against."; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 535 | return kOatRelocationOutOfDate; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 536 | } |
Richard Uhler | a62d2f0 | 2016-03-18 15:05:30 -0700 | [diff] [blame] | 537 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 538 | // Verify the oat_data_begin recorded for the image in the oat file matches |
| 539 | // the actual oat_data_begin for boot.oat in the image. |
| 540 | const OatHeader& oat_header = file.GetOatHeader(); |
| 541 | uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin(); |
| 542 | if (oat_data_begin != image_info->oat_data_begin) { |
| 543 | VLOG(oat) << file.GetLocation() << |
| 544 | ": Oat file image oat_data_begin (" << oat_data_begin << ")" |
| 545 | << " does not match actual image oat_data_begin (" |
| 546 | << image_info->oat_data_begin << ")"; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 547 | return kOatRelocationOutOfDate; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 548 | } |
Richard Uhler | a62d2f0 | 2016-03-18 15:05:30 -0700 | [diff] [blame] | 549 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 550 | // Verify the oat_patch_delta recorded for the image in the oat file matches |
| 551 | // the actual oat_patch_delta for the image. |
| 552 | int32_t oat_patch_delta = oat_header.GetImagePatchDelta(); |
| 553 | if (oat_patch_delta != image_info->patch_delta) { |
| 554 | VLOG(oat) << file.GetLocation() << |
| 555 | ": Oat file image patch delta (" << oat_patch_delta << ")" |
| 556 | << " does not match actual image patch delta (" |
| 557 | << image_info->patch_delta << ")"; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 558 | return kOatRelocationOutOfDate; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 559 | } |
| 560 | } else { |
| 561 | // Oat files compiled in PIC mode do not require relocation. |
| 562 | VLOG(oat) << "Oat relocation test skipped for PIC oat file"; |
| 563 | } |
| 564 | } else { |
| 565 | VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 566 | } |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 567 | return kOatUpToDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 570 | static bool DexLocationToOdexNames(const std::string& location, |
| 571 | InstructionSet isa, |
| 572 | std::string* odex_filename, |
| 573 | std::string* oat_dir, |
| 574 | std::string* isa_dir, |
| 575 | std::string* error_msg) { |
| 576 | CHECK(odex_filename != nullptr); |
| 577 | CHECK(error_msg != nullptr); |
| 578 | |
| 579 | // The odex file name is formed by replacing the dex_location extension with |
| 580 | // .odex and inserting an oat/<isa> directory. For example: |
| 581 | // location = /foo/bar/baz.jar |
| 582 | // odex_location = /foo/bar/oat/<isa>/baz.odex |
| 583 | |
| 584 | // Find the directory portion of the dex location and add the oat/<isa> |
| 585 | // directory. |
| 586 | size_t pos = location.rfind('/'); |
| 587 | if (pos == std::string::npos) { |
| 588 | *error_msg = "Dex location " + location + " has no directory."; |
| 589 | return false; |
| 590 | } |
| 591 | std::string dir = location.substr(0, pos+1); |
| 592 | // Add the oat directory. |
| 593 | dir += "oat"; |
| 594 | if (oat_dir != nullptr) { |
| 595 | *oat_dir = dir; |
| 596 | } |
| 597 | // Add the isa directory |
| 598 | dir += "/" + std::string(GetInstructionSetString(isa)); |
| 599 | if (isa_dir != nullptr) { |
| 600 | *isa_dir = dir; |
| 601 | } |
| 602 | |
| 603 | // Get the base part of the file without the extension. |
| 604 | std::string file = location.substr(pos+1); |
| 605 | pos = file.rfind('.'); |
| 606 | if (pos == std::string::npos) { |
| 607 | *error_msg = "Dex location " + location + " has no extension."; |
| 608 | return false; |
| 609 | } |
| 610 | std::string base = file.substr(0, pos); |
| 611 | |
| 612 | *odex_filename = dir + "/" + base + ".odex"; |
| 613 | return true; |
| 614 | } |
| 615 | |
| 616 | // Prepare a subcomponent of the odex directory. |
| 617 | // (i.e. create and set the expected permissions on the path `dir`). |
| 618 | static bool PrepareDirectory(const std::string& dir, std::string* error_msg) { |
| 619 | struct stat dir_stat; |
| 620 | if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) { |
| 621 | // The directory exists. Check if it is indeed a directory. |
| 622 | if (!S_ISDIR(dir_stat.st_mode)) { |
| 623 | *error_msg = dir + " is not a dir"; |
| 624 | return false; |
| 625 | } else { |
| 626 | // The dir is already on disk. |
| 627 | return true; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | // Failed to stat. We need to create the directory. |
| 632 | if (errno != ENOENT) { |
| 633 | *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno); |
| 634 | return false; |
| 635 | } |
| 636 | |
| 637 | mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH; |
| 638 | if (mkdir(dir.c_str(), mode) != 0) { |
| 639 | *error_msg = "Could not create dir " + dir + ":" + strerror(errno); |
| 640 | return false; |
| 641 | } |
| 642 | if (chmod(dir.c_str(), mode) != 0) { |
| 643 | *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno); |
| 644 | return false; |
| 645 | } |
| 646 | return true; |
| 647 | } |
| 648 | |
| 649 | // Prepares the odex directory for the given dex location. |
| 650 | static bool PrepareOdexDirectories(const std::string& dex_location, |
| 651 | const std::string& expected_odex_location, |
| 652 | InstructionSet isa, |
| 653 | std::string* error_msg) { |
| 654 | std::string actual_odex_location; |
| 655 | std::string oat_dir; |
| 656 | std::string isa_dir; |
| 657 | if (!DexLocationToOdexNames( |
| 658 | dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) { |
| 659 | return false; |
| 660 | } |
| 661 | DCHECK_EQ(expected_odex_location, actual_odex_location); |
| 662 | |
| 663 | if (!PrepareDirectory(oat_dir, error_msg)) { |
| 664 | return false; |
| 665 | } |
| 666 | if (!PrepareDirectory(isa_dir, error_msg)) { |
| 667 | return false; |
| 668 | } |
| 669 | return true; |
| 670 | } |
| 671 | |
Calin Juravle | d4215bb | 2017-09-20 11:54:21 -0700 | [diff] [blame] | 672 | class Dex2oatFileWrapper { |
| 673 | public: |
| 674 | explicit Dex2oatFileWrapper(File* file) |
| 675 | : file_(file), |
| 676 | unlink_file_at_destruction_(true) { |
| 677 | } |
| 678 | |
| 679 | ~Dex2oatFileWrapper() { |
| 680 | if (unlink_file_at_destruction_ && (file_ != nullptr)) { |
| 681 | file_->Erase(/*unlink*/ true); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | File* GetFile() { return file_.get(); } |
| 686 | |
| 687 | void DisableUnlinkAtDestruction() { |
| 688 | unlink_file_at_destruction_ = false; |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 689 | } |
Calin Juravle | d4215bb | 2017-09-20 11:54:21 -0700 | [diff] [blame] | 690 | |
| 691 | private: |
| 692 | std::unique_ptr<File> file_; |
| 693 | bool unlink_file_at_destruction_; |
| 694 | }; |
| 695 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 696 | OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks( |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 697 | OatFileAssistant::OatFileInfo& info, |
| 698 | CompilerFilter::Filter filter, |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 699 | const ClassLoaderContext* class_loader_context, |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame] | 700 | std::string* error_msg) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 701 | CHECK(error_msg != nullptr); |
| 702 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 703 | Runtime* runtime = Runtime::Current(); |
| 704 | if (!runtime->IsDex2OatEnabled()) { |
| 705 | *error_msg = "Generation of oat file for dex location " + dex_location_ |
| 706 | + " not attempted because dex2oat is disabled."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 707 | return kUpdateNotAttempted; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 710 | if (info.Filename() == nullptr) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 711 | *error_msg = "Generation of oat file for dex location " + dex_location_ |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 712 | + " not attempted because the oat file name could not be determined."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 713 | return kUpdateNotAttempted; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 714 | } |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 715 | const std::string& oat_file_name = *info.Filename(); |
Calin Juravle | 367b9d8 | 2017-05-15 18:18:39 -0700 | [diff] [blame] | 716 | const std::string& vdex_file_name = GetVdexFilename(oat_file_name); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 717 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 718 | // dex2oat ignores missing dex files and doesn't report an error. |
| 719 | // Check explicitly here so we can detect the error properly. |
| 720 | // TODO: Why does dex2oat behave that way? |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 721 | struct stat dex_path_stat; |
| 722 | if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) { |
| 723 | *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 724 | return kUpdateNotAttempted; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 725 | } |
| 726 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 727 | // If this is the odex location, we need to create the odex file layout (../oat/isa/..) |
| 728 | if (!info.IsOatLocation()) { |
| 729 | if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) { |
| 730 | return kUpdateNotAttempted; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | // Set the permissions for the oat and the vdex files. |
| 735 | // The user always gets read and write while the group and others propagate |
| 736 | // the reading access of the original dex file. |
| 737 | mode_t file_mode = S_IRUSR | S_IWUSR | |
| 738 | (dex_path_stat.st_mode & S_IRGRP) | |
| 739 | (dex_path_stat.st_mode & S_IROTH); |
| 740 | |
Calin Juravle | d4215bb | 2017-09-20 11:54:21 -0700 | [diff] [blame] | 741 | Dex2oatFileWrapper vdex_file_wrapper(OS::CreateEmptyFile(vdex_file_name.c_str())); |
| 742 | File* vdex_file = vdex_file_wrapper.GetFile(); |
| 743 | if (vdex_file == nullptr) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 744 | *error_msg = "Generation of oat file " + oat_file_name |
| 745 | + " not attempted because the vdex file " + vdex_file_name |
| 746 | + " could not be opened."; |
| 747 | return kUpdateNotAttempted; |
| 748 | } |
| 749 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 750 | if (fchmod(vdex_file->Fd(), file_mode) != 0) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 751 | *error_msg = "Generation of oat file " + oat_file_name |
| 752 | + " not attempted because the vdex file " + vdex_file_name |
| 753 | + " could not be made world readable."; |
| 754 | return kUpdateNotAttempted; |
| 755 | } |
| 756 | |
Calin Juravle | d4215bb | 2017-09-20 11:54:21 -0700 | [diff] [blame] | 757 | Dex2oatFileWrapper oat_file_wrapper(OS::CreateEmptyFile(oat_file_name.c_str())); |
| 758 | File* oat_file = oat_file_wrapper.GetFile(); |
| 759 | if (oat_file == nullptr) { |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 760 | *error_msg = "Generation of oat file " + oat_file_name |
| 761 | + " not attempted because the oat file could not be created."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 762 | return kUpdateNotAttempted; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 765 | if (fchmod(oat_file->Fd(), file_mode) != 0) { |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 766 | *error_msg = "Generation of oat file " + oat_file_name |
| 767 | + " not attempted because the oat file could not be made world readable."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 768 | return kUpdateNotAttempted; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | std::vector<std::string> args; |
| 772 | args.push_back("--dex-file=" + dex_location_); |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 773 | args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd())); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 774 | args.push_back("--oat-fd=" + std::to_string(oat_file->Fd())); |
| 775 | args.push_back("--oat-location=" + oat_file_name); |
Calin Juravle | 07c6d72 | 2017-06-07 17:06:12 +0000 | [diff] [blame] | 776 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 777 | const std::string dex2oat_context = class_loader_context == nullptr |
| 778 | ? OatFile::kSpecialSharedLibrary |
| 779 | : class_loader_context->EncodeContextForDex2oat(/*base_dir*/ ""); |
| 780 | args.push_back("--class-loader-context=" + dex2oat_context); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 781 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 782 | if (!Dex2Oat(args, error_msg)) { |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 783 | return kUpdateFailed; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 784 | } |
| 785 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 786 | if (vdex_file->FlushCloseOrErase() != 0) { |
| 787 | *error_msg = "Unable to close vdex file " + vdex_file_name; |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 788 | return kUpdateFailed; |
| 789 | } |
| 790 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 791 | if (oat_file->FlushCloseOrErase() != 0) { |
| 792 | *error_msg = "Unable to close oat file " + oat_file_name; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 793 | return kUpdateFailed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 794 | } |
| 795 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 796 | // Mark that the odex file has changed and we should try to reload. |
| 797 | info.Reset(); |
Calin Juravle | d4215bb | 2017-09-20 11:54:21 -0700 | [diff] [blame] | 798 | // We have compiled successfully. Disable the auto-unlink. |
| 799 | vdex_file_wrapper.DisableUnlinkAtDestruction(); |
| 800 | oat_file_wrapper.DisableUnlinkAtDestruction(); |
| 801 | |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 802 | return kUpdateSucceeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args, |
| 806 | std::string* error_msg) { |
| 807 | Runtime* runtime = Runtime::Current(); |
| 808 | std::string image_location = ImageLocation(); |
| 809 | if (image_location.empty()) { |
| 810 | *error_msg = "No image location found for Dex2Oat."; |
| 811 | return false; |
| 812 | } |
| 813 | |
| 814 | std::vector<std::string> argv; |
| 815 | argv.push_back(runtime->GetCompilerExecutable()); |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 816 | if (runtime->IsJavaDebuggable()) { |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 817 | argv.push_back("--debuggable"); |
| 818 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 819 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 820 | |
| 821 | if (!runtime->IsVerificationEnabled()) { |
| 822 | argv.push_back("--compiler-filter=verify-none"); |
| 823 | } |
| 824 | |
| 825 | if (runtime->MustRelocateIfPossible()) { |
| 826 | argv.push_back("--runtime-arg"); |
| 827 | argv.push_back("-Xrelocate"); |
| 828 | } else { |
| 829 | argv.push_back("--runtime-arg"); |
| 830 | argv.push_back("-Xnorelocate"); |
| 831 | } |
| 832 | |
| 833 | if (!kIsTargetBuild) { |
| 834 | argv.push_back("--host"); |
| 835 | } |
| 836 | |
| 837 | argv.push_back("--boot-image=" + image_location); |
| 838 | |
| 839 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 840 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 841 | |
| 842 | argv.insert(argv.end(), args.begin(), args.end()); |
| 843 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 844 | std::string command_line(android::base::Join(argv, ' ')); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 845 | return Exec(argv, error_msg); |
| 846 | } |
| 847 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 848 | bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location, |
| 849 | InstructionSet isa, |
| 850 | std::string* odex_filename, |
| 851 | std::string* error_msg) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 852 | return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 853 | } |
| 854 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 855 | bool OatFileAssistant::DexLocationToOatFilename(const std::string& location, |
| 856 | InstructionSet isa, |
| 857 | std::string* oat_filename, |
| 858 | std::string* error_msg) { |
| 859 | CHECK(oat_filename != nullptr); |
| 860 | CHECK(error_msg != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 861 | |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame] | 862 | std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa)); |
| 863 | if (cache_dir.empty()) { |
| 864 | *error_msg = "Dalvik cache directory does not exist"; |
| 865 | return false; |
| 866 | } |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 867 | |
| 868 | // TODO: The oat file assistant should be the definitive place for |
| 869 | // determining the oat file name from the dex location, not |
| 870 | // GetDalvikCacheFilename. |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 871 | return GetDalvikCacheFilename(location.c_str(), cache_dir.c_str(), oat_filename, error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 872 | } |
| 873 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 874 | std::string OatFileAssistant::ImageLocation() { |
| 875 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 876 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
| 877 | runtime->GetHeap()->GetBootImageSpaces(); |
| 878 | if (image_spaces.empty()) { |
| 879 | return ""; |
| 880 | } |
| 881 | return image_spaces[0]->GetImageLocation(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 884 | const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() { |
| 885 | if (!required_dex_checksums_attempted_) { |
| 886 | required_dex_checksums_attempted_ = true; |
| 887 | required_dex_checksums_found_ = false; |
| 888 | cached_required_dex_checksums_.clear(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 889 | std::string error_msg; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 890 | const ArtDexFileLoader dex_file_loader; |
| 891 | if (dex_file_loader.GetMultiDexChecksums(dex_location_.c_str(), |
| 892 | &cached_required_dex_checksums_, |
| 893 | &error_msg, |
Nicolas Geoffray | 66ff8a8 | 2018-02-28 13:27:55 +0000 | [diff] [blame] | 894 | zip_fd_, |
| 895 | &zip_file_only_contains_uncompressed_dex_)) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 896 | required_dex_checksums_found_ = true; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 897 | has_original_dex_files_ = true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 898 | } else { |
| 899 | // This can happen if the original dex file has been stripped from the |
| 900 | // apk. |
| 901 | VLOG(oat) << "OatFileAssistant: " << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 902 | has_original_dex_files_ = false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 903 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 904 | // Get the checksums from the odex if we can. |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 905 | const OatFile* odex_file = odex_.GetFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 906 | if (odex_file != nullptr) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 907 | required_dex_checksums_found_ = true; |
| 908 | for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 909 | std::string dex = DexFileLoader::GetMultiDexLocation(i, dex_location_.c_str()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 910 | const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr); |
| 911 | if (odex_dex_file == nullptr) { |
| 912 | required_dex_checksums_found_ = false; |
| 913 | break; |
| 914 | } |
| 915 | cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | } |
| 919 | } |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 920 | return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 921 | } |
| 922 | |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 923 | std::unique_ptr<OatFileAssistant::ImageInfo> |
| 924 | OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) { |
| 925 | CHECK(error_msg != nullptr); |
| 926 | |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 927 | Runtime* runtime = Runtime::Current(); |
Richard Uhler | 8f10486 | 2017-02-22 12:34:01 +0000 | [diff] [blame] | 928 | std::unique_ptr<ImageInfo> info(new ImageInfo()); |
| 929 | info->location = runtime->GetImageLocation(); |
| 930 | |
| 931 | std::unique_ptr<ImageHeader> image_header( |
| 932 | gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg)); |
| 933 | if (image_header == nullptr) { |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 934 | return nullptr; |
| 935 | } |
| 936 | |
Richard Uhler | 8f10486 | 2017-02-22 12:34:01 +0000 | [diff] [blame] | 937 | info->oat_checksum = image_header->GetOatChecksum(); |
| 938 | info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()); |
| 939 | info->patch_delta = image_header->GetPatchDelta(); |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 940 | return info; |
| 941 | } |
| 942 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 943 | const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() { |
| 944 | if (!image_info_load_attempted_) { |
| 945 | image_info_load_attempted_ = true; |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 946 | std::string error_msg; |
| 947 | cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg); |
| 948 | if (cached_image_info_ == nullptr) { |
| 949 | LOG(WARNING) << "Unable to get runtime image info: " << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 950 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 951 | } |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 952 | return cached_image_info_.get(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 953 | } |
| 954 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 955 | OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 956 | // TODO(calin): Document the side effects of class loading when |
| 957 | // running dalvikvm command line. |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 958 | if (dex_parent_writable_ || UseFdToReadFiles()) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 959 | // If the parent of the dex file is writable it means that we can |
| 960 | // create the odex file. In this case we unconditionally pick the odex |
| 961 | // as the best oat file. This corresponds to the regular use case when |
| 962 | // apps gets installed or when they load private, secondary dex file. |
| 963 | // For apps on the system partition the odex location will not be |
| 964 | // writable and thus the oat location might be more up to date. |
| 965 | return odex_; |
| 966 | } |
| 967 | |
| 968 | // We cannot write to the odex location. This must be a system app. |
| 969 | |
| 970 | // If the oat location is usable take it. |
| 971 | if (oat_.IsUseable()) { |
| 972 | return oat_; |
| 973 | } |
| 974 | |
| 975 | // The oat file is not usable but the odex file might be up to date. |
| 976 | // This is an indication that we are dealing with an up to date prebuilt |
| 977 | // (that doesn't need relocation). |
| 978 | if (odex_.Status() == kOatUpToDate) { |
| 979 | return odex_; |
| 980 | } |
| 981 | |
| 982 | // The oat file is not usable and the odex file is not up to date. |
| 983 | // However we have access to the original dex file which means we can make |
| 984 | // the oat location up to date. |
| 985 | if (HasOriginalDexFiles()) { |
| 986 | return oat_; |
| 987 | } |
| 988 | |
| 989 | // We got into the worst situation here: |
| 990 | // - the oat location is not usable |
| 991 | // - the prebuild odex location is not up to date |
| 992 | // - and we don't have the original dex file anymore (stripped). |
| 993 | // Pick the odex if it exists, or the oat if not. |
| 994 | return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_; |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 997 | std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 998 | DCHECK(oat_file != nullptr); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 999 | std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art"); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1000 | if (art_file.empty()) { |
| 1001 | return nullptr; |
| 1002 | } |
| 1003 | std::string error_msg; |
| 1004 | ScopedObjectAccess soa(Thread::Current()); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1005 | std::unique_ptr<gc::space::ImageSpace> ret = |
| 1006 | gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg); |
Mathieu Chartier | e778fc7 | 2016-01-25 20:11:28 -0800 | [diff] [blame] | 1007 | if (ret == nullptr && (VLOG_IS_ON(image) || OS::FileExists(art_file.c_str()))) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1008 | LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg; |
| 1009 | } |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 1013 | OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant, |
| 1014 | bool is_oat_location) |
| 1015 | : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location) |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1016 | {} |
| 1017 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 1018 | bool OatFileAssistant::OatFileInfo::IsOatLocation() { |
| 1019 | return is_oat_location_; |
| 1020 | } |
| 1021 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1022 | const std::string* OatFileAssistant::OatFileInfo::Filename() { |
| 1023 | return filename_provided_ ? &filename_ : nullptr; |
| 1024 | } |
| 1025 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 1026 | bool OatFileAssistant::OatFileInfo::IsUseable() { |
| 1027 | switch (Status()) { |
| 1028 | case kOatCannotOpen: |
| 1029 | case kOatDexOutOfDate: |
| 1030 | case kOatBootImageOutOfDate: return false; |
| 1031 | |
| 1032 | case kOatRelocationOutOfDate: |
| 1033 | case kOatUpToDate: return true; |
| 1034 | } |
| 1035 | UNREACHABLE(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() { |
| 1039 | if (!status_attempted_) { |
| 1040 | status_attempted_ = true; |
| 1041 | const OatFile* file = GetFile(); |
| 1042 | if (file == nullptr) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 1043 | // Check to see if there is a vdex file we can make use of. |
| 1044 | std::string error_msg; |
Calin Juravle | 367b9d8 | 2017-05-15 18:18:39 -0700 | [diff] [blame] | 1045 | std::string vdex_filename = GetVdexFilename(filename_); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 1046 | std::unique_ptr<VdexFile> vdex; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 1047 | if (use_fd_) { |
| 1048 | if (vdex_fd_ >= 0) { |
| 1049 | struct stat s; |
| 1050 | int rc = TEMP_FAILURE_RETRY(fstat(vdex_fd_, &s)); |
| 1051 | if (rc == -1) { |
| 1052 | error_msg = StringPrintf("Failed getting length of the vdex file %s.", strerror(errno)); |
| 1053 | } else { |
| 1054 | vdex = VdexFile::Open(vdex_fd_, |
| 1055 | s.st_size, |
| 1056 | vdex_filename, |
| 1057 | false /*writable*/, |
| 1058 | false /*low_4gb*/, |
| 1059 | false /* unquicken */, |
| 1060 | &error_msg); |
| 1061 | } |
| 1062 | } |
| 1063 | } else { |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 1064 | vdex = VdexFile::Open(vdex_filename, |
| 1065 | false /*writeable*/, |
| 1066 | false /*low_4gb*/, |
| 1067 | false /*unquicken*/, |
| 1068 | &error_msg); |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 1069 | } |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 1070 | if (vdex == nullptr) { |
| 1071 | status_ = kOatCannotOpen; |
| 1072 | VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg; |
| 1073 | } else { |
| 1074 | if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) { |
| 1075 | // The vdex file does not contain enough information to determine |
| 1076 | // whether it is up to date with respect to the boot image, so we |
| 1077 | // assume it is out of date. |
| 1078 | VLOG(oat) << error_msg; |
| 1079 | status_ = kOatBootImageOutOfDate; |
| 1080 | } else { |
| 1081 | status_ = kOatDexOutOfDate; |
| 1082 | } |
| 1083 | } |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1084 | } else { |
| 1085 | status_ = oat_file_assistant_->GivenOatFileStatus(*file); |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1086 | VLOG(oat) << file->GetLocation() << " is " << status_ |
| 1087 | << " with filter " << file->GetCompilerFilter(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | return status_; |
| 1091 | } |
| 1092 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1093 | OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded( |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1094 | CompilerFilter::Filter target, |
| 1095 | bool profile_changed, |
| 1096 | bool downgrade, |
| 1097 | ClassLoaderContext* context) { |
| 1098 | |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 1099 | bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target); |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 1100 | bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade); |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1101 | bool class_loader_context_okay = ClassLoaderContextIsOkay(context); |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1102 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1103 | // Only check the filter and relocation if the class loader context is ok. |
| 1104 | // If it is not, we will return kDex2OatFromScratch as the compilation needs to be redone. |
| 1105 | if (class_loader_context_okay) { |
| 1106 | if (filter_okay && Status() == kOatUpToDate) { |
| 1107 | // The oat file is in good shape as is. |
| 1108 | return kNoDexOptNeeded; |
| 1109 | } |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1110 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1111 | if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) { |
| 1112 | // If no compilation is desired, then it doesn't matter if the oat |
| 1113 | // file needs relocation. It's in good shape as is. |
| 1114 | return kNoDexOptNeeded; |
| 1115 | } |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1116 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1117 | if (filter_okay && Status() == kOatRelocationOutOfDate) { |
| 1118 | return kDex2OatForRelocation; |
| 1119 | } |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1120 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1121 | if (IsUseable()) { |
| 1122 | return kDex2OatForFilter; |
| 1123 | } |
Nicolas Geoffray | 08e9eed | 2017-04-25 17:36:51 +0100 | [diff] [blame] | 1124 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1125 | if (Status() == kOatBootImageOutOfDate) { |
| 1126 | return kDex2OatForBootImage; |
| 1127 | } |
Nicolas Geoffray | 08e9eed | 2017-04-25 17:36:51 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | if (oat_file_assistant_->HasOriginalDexFiles()) { |
| 1131 | return kDex2OatFromScratch; |
| 1132 | } else { |
| 1133 | // Otherwise there is nothing we can do, even if we want to. |
| 1134 | return kNoDexOptNeeded; |
| 1135 | } |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1138 | const OatFile* OatFileAssistant::OatFileInfo::GetFile() { |
| 1139 | CHECK(!file_released_) << "GetFile called after oat file released."; |
| 1140 | if (!load_attempted_) { |
| 1141 | load_attempted_ = true; |
| 1142 | if (filename_provided_) { |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 1143 | bool executable = oat_file_assistant_->load_executable_; |
| 1144 | if (executable && oat_file_assistant_->only_load_system_executable_) { |
| 1145 | executable = LocationIsOnSystem(filename_.c_str()); |
| 1146 | } |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1147 | std::string error_msg; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 1148 | if (use_fd_) { |
| 1149 | if (oat_fd_ >= 0 && vdex_fd_ >= 0) { |
| 1150 | file_.reset(OatFile::Open(vdex_fd_, |
| 1151 | oat_fd_, |
| 1152 | filename_.c_str(), |
| 1153 | nullptr, |
| 1154 | nullptr, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 1155 | executable, |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 1156 | false /* low_4gb */, |
| 1157 | oat_file_assistant_->dex_location_.c_str(), |
| 1158 | &error_msg)); |
| 1159 | } |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 1160 | } else { |
| 1161 | file_.reset(OatFile::Open(filename_.c_str(), |
| 1162 | filename_.c_str(), |
| 1163 | nullptr, |
| 1164 | nullptr, |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 1165 | executable, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 1166 | false /* low_4gb */, |
| 1167 | oat_file_assistant_->dex_location_.c_str(), |
| 1168 | &error_msg)); |
| 1169 | } |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1170 | if (file_.get() == nullptr) { |
| 1171 | VLOG(oat) << "OatFileAssistant test for existing oat file " |
| 1172 | << filename_ << ": " << error_msg; |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | return file_.get(); |
| 1177 | } |
| 1178 | |
| 1179 | bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay( |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 1180 | CompilerFilter::Filter target, bool profile_changed, bool downgrade) { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1181 | const OatFile* file = GetFile(); |
| 1182 | if (file == nullptr) { |
| 1183 | return false; |
| 1184 | } |
| 1185 | |
| 1186 | CompilerFilter::Filter current = file->GetCompilerFilter(); |
| 1187 | if (profile_changed && CompilerFilter::DependsOnProfile(current)) { |
| 1188 | VLOG(oat) << "Compiler filter not okay because Profile changed"; |
| 1189 | return false; |
| 1190 | } |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 1191 | return downgrade ? !CompilerFilter::IsBetter(current, target) : |
| 1192 | CompilerFilter::IsAsGoodAs(current, target); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1195 | bool OatFileAssistant::OatFileInfo::ClassLoaderContextIsOkay(ClassLoaderContext* context) { |
| 1196 | if (context == nullptr) { |
| 1197 | VLOG(oat) << "ClassLoaderContext check ignored: null context"; |
| 1198 | return true; |
| 1199 | } |
| 1200 | |
| 1201 | const OatFile* file = GetFile(); |
| 1202 | if (file == nullptr) { |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1203 | // No oat file means we have nothing to verify. |
| 1204 | return true; |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1207 | size_t dir_index = oat_file_assistant_->dex_location_.rfind('/'); |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1208 | std::string classpath_dir = (dir_index != std::string::npos) |
Calin Juravle | 20c4644 | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1209 | ? oat_file_assistant_->dex_location_.substr(0, dir_index) |
Calin Juravle | 44e5efa | 2017-09-12 00:54:26 -0700 | [diff] [blame] | 1210 | : ""; |
| 1211 | |
| 1212 | if (!context->OpenDexFiles(oat_file_assistant_->isa_, classpath_dir)) { |
| 1213 | VLOG(oat) << "ClassLoaderContext check failed: dex files from the context could not be opened"; |
| 1214 | return false; |
| 1215 | } |
| 1216 | |
| 1217 | bool result = context->VerifyClassLoaderContextMatch(file->GetClassLoaderContext()); |
| 1218 | if (!result) { |
| 1219 | VLOG(oat) << "ClassLoaderContext check failed. Context was " |
| 1220 | << file->GetClassLoaderContext() |
| 1221 | << ". The expected context is " << context->EncodeContextForOatFile(classpath_dir); |
| 1222 | } |
| 1223 | return result; |
| 1224 | } |
| 1225 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1226 | bool OatFileAssistant::OatFileInfo::IsExecutable() { |
| 1227 | const OatFile* file = GetFile(); |
| 1228 | return (file != nullptr && file->IsExecutable()); |
| 1229 | } |
| 1230 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1231 | void OatFileAssistant::OatFileInfo::Reset() { |
| 1232 | load_attempted_ = false; |
| 1233 | file_.reset(); |
| 1234 | status_attempted_ = false; |
| 1235 | } |
| 1236 | |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 1237 | void OatFileAssistant::OatFileInfo::Reset(const std::string& filename, bool use_fd, int vdex_fd, |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 1238 | int oat_fd) { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1239 | filename_provided_ = true; |
| 1240 | filename_ = filename; |
Shubham Ajmera | c12bf4c | 2017-10-24 16:59:42 -0700 | [diff] [blame] | 1241 | use_fd_ = use_fd; |
Shubham Ajmera | b22dea0 | 2017-10-04 18:36:41 -0700 | [diff] [blame] | 1242 | vdex_fd_ = vdex_fd; |
| 1243 | oat_fd_ = oat_fd; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1244 | Reset(); |
| 1245 | } |
| 1246 | |
| 1247 | std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() { |
| 1248 | file_released_ = true; |
| 1249 | return std::move(file_); |
| 1250 | } |
| 1251 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1252 | std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() { |
Richard Uhler | 3e580bc | 2016-11-08 16:23:07 +0000 | [diff] [blame] | 1253 | if (Status() == kOatUpToDate) { |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1254 | return ReleaseFile(); |
| 1255 | } |
| 1256 | |
| 1257 | VLOG(oat) << "Oat File Assistant: No relocated oat file found," |
| 1258 | << " attempting to fall back to interpreting oat file instead."; |
| 1259 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 1260 | if (Status() == kOatRelocationOutOfDate && !IsExecutable()) { |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1261 | return ReleaseFile(); |
| 1262 | } |
| 1263 | |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 1264 | switch (Status()) { |
| 1265 | case kOatBootImageOutOfDate: |
Andreas Gampe | 8f81de5 | 2018-03-06 08:39:21 -0800 | [diff] [blame] | 1266 | // OutOfDate may be either a mismatched image, or a missing image. |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 1267 | if (oat_file_assistant_->HasOriginalDexFiles()) { |
Andreas Gampe | 8f81de5 | 2018-03-06 08:39:21 -0800 | [diff] [blame] | 1268 | // If there are original dex files, it is better to use them (to avoid a potential |
| 1269 | // quickening mismatch because the boot image changed). |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 1270 | break; |
| 1271 | } |
Andreas Gampe | 8f81de5 | 2018-03-06 08:39:21 -0800 | [diff] [blame] | 1272 | // If we do not accept the oat file, we may not have access to dex bytecode at all. Grudgingly |
| 1273 | // go forward. |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 1274 | FALLTHROUGH_INTENDED; |
| 1275 | |
| 1276 | case kOatRelocationOutOfDate: |
| 1277 | // We are loading an oat file for runtime use that needs relocation. |
| 1278 | // Reload the file non-executable to ensure that we interpret out of the |
| 1279 | // dex code in the oat file rather than trying to execute the unrelocated |
| 1280 | // compiled code. |
| 1281 | oat_file_assistant_->load_executable_ = false; |
| 1282 | Reset(); |
| 1283 | if (IsUseable()) { |
| 1284 | CHECK(!IsExecutable()); |
| 1285 | return ReleaseFile(); |
| 1286 | } |
| 1287 | break; |
| 1288 | |
| 1289 | case kOatUpToDate: |
| 1290 | case kOatCannotOpen: |
| 1291 | case kOatDexOutOfDate: |
| 1292 | break; |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1293 | } |
Andreas Gampe | 9ea84d0 | 2018-03-02 09:32:03 -0800 | [diff] [blame] | 1294 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1295 | return std::unique_ptr<OatFile>(); |
| 1296 | } |
Calin Juravle | 5f9a801 | 2018-02-12 20:27:46 -0800 | [diff] [blame] | 1297 | |
| 1298 | // TODO(calin): we could provide a more refined status here |
| 1299 | // (e.g. run from uncompressed apk, run with vdex but not oat etc). It will allow us to |
| 1300 | // track more experiments but adds extra complexity. |
| 1301 | void OatFileAssistant::GetOptimizationStatus( |
| 1302 | const std::string& filename, |
| 1303 | InstructionSet isa, |
| 1304 | std::string* out_compilation_filter, |
| 1305 | std::string* out_compilation_reason) { |
| 1306 | // Try to load the oat file as we would do at runtime. |
| 1307 | OatFileAssistant oat_file_assistant(filename.c_str(), isa, true /* load_executable */); |
| 1308 | std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile(); |
| 1309 | |
| 1310 | if (oat_file == nullptr) { |
| 1311 | *out_compilation_filter = "run-from-apk"; |
| 1312 | *out_compilation_reason = "unknown"; |
| 1313 | } else { |
| 1314 | *out_compilation_filter = CompilerFilter::NameOfFilter(oat_file->GetCompilerFilter()); |
| 1315 | const char* reason = oat_file->GetCompilationReason(); |
| 1316 | *out_compilation_reason = reason == nullptr ? "unknown" : reason; |
| 1317 | } |
| 1318 | } |
| 1319 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1320 | } // namespace art |