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