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