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