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