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