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