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