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