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