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