Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "oat_file_assistant.h" |
| 18 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 19 | #include <sstream> |
| 20 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 21 | #include <sys/stat.h> |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 22 | |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 23 | #include "android-base/stringprintf.h" |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 24 | #include "android-base/strings.h" |
| 25 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 26 | #include "base/logging.h" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 27 | #include "base/stl_util.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 28 | #include "class_linker.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 29 | #include "compiler_filter.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 30 | #include "exec_utils.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 31 | #include "gc/heap.h" |
| 32 | #include "gc/space/image_space.h" |
| 33 | #include "image.h" |
| 34 | #include "oat.h" |
| 35 | #include "os.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 36 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 37 | #include "scoped_thread_state_change-inl.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 38 | #include "utils.h" |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 39 | #include "vdex_file.h" |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame^] | 40 | #include "class_loader_context.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 41 | |
| 42 | namespace art { |
| 43 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 44 | using android::base::StringPrintf; |
| 45 | |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 46 | std::ostream& operator << (std::ostream& stream, const OatFileAssistant::OatStatus status) { |
| 47 | switch (status) { |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 48 | case OatFileAssistant::kOatCannotOpen: |
| 49 | stream << "kOatCannotOpen"; |
| 50 | break; |
| 51 | case OatFileAssistant::kOatDexOutOfDate: |
| 52 | stream << "kOatDexOutOfDate"; |
| 53 | break; |
| 54 | case OatFileAssistant::kOatBootImageOutOfDate: |
| 55 | stream << "kOatBootImageOutOfDate"; |
| 56 | break; |
| 57 | case OatFileAssistant::kOatRelocationOutOfDate: |
| 58 | stream << "kOatRelocationOutOfDate"; |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 59 | break; |
| 60 | case OatFileAssistant::kOatUpToDate: |
| 61 | stream << "kOatUpToDate"; |
| 62 | break; |
Narayan Kamath | 8943c1d | 2016-05-02 13:14:48 +0100 | [diff] [blame] | 63 | default: |
| 64 | UNREACHABLE(); |
| 65 | } |
| 66 | |
| 67 | return stream; |
| 68 | } |
| 69 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 70 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 71 | const InstructionSet isa, |
| 72 | bool load_executable) |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 73 | : isa_(isa), |
| 74 | load_executable_(load_executable), |
| 75 | odex_(this, /*is_oat_location*/ false), |
| 76 | oat_(this, /*is_oat_location*/ true) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 77 | CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location"; |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 78 | |
| 79 | // Try to get the realpath for the dex location. |
| 80 | // |
| 81 | // This is OK with respect to dalvik cache naming scheme because we never |
| 82 | // generate oat files starting from symlinks which go into dalvik cache. |
| 83 | // (recall that the oat files in dalvik cache are encoded by replacing '/' |
| 84 | // with '@' in the path). |
| 85 | // The boot image oat files (which are symlinked in dalvik-cache) are not |
| 86 | // loaded via the oat file assistant. |
| 87 | // |
| 88 | // The only case when the dex location may resolve to a different path |
| 89 | // is for secondary dex files (e.g. /data/user/0 symlinks to /data/data and |
| 90 | // the app is free to create its own internal layout). Related to this it is |
| 91 | // worthwhile to mention that installd resolves the secondary dex location |
| 92 | // before calling dex2oat. |
| 93 | UniqueCPtr<const char[]> dex_location_real(realpath(dex_location, nullptr)); |
| 94 | if (dex_location_real != nullptr) { |
| 95 | dex_location_.assign(dex_location_real.get()); |
| 96 | } else { |
| 97 | // If we can't get the realpath of the location there's not much point in trying to move on. |
| 98 | PLOG(ERROR) << "Could not get the realpath of dex_location " << dex_location; |
| 99 | return; |
| 100 | } |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 101 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 102 | if (load_executable_ && isa != kRuntimeISA) { |
| 103 | LOG(WARNING) << "OatFileAssistant: Load executable specified, " |
| 104 | << "but isa is not kRuntimeISA. Will not attempt to load executable."; |
| 105 | load_executable_ = false; |
| 106 | } |
| 107 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 108 | // Get the odex filename. |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 109 | std::string error_msg; |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 110 | std::string odex_file_name; |
| 111 | if (DexLocationToOdexFilename(dex_location_, isa_, &odex_file_name, &error_msg)) { |
| 112 | odex_.Reset(odex_file_name); |
| 113 | } else { |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 114 | LOG(WARNING) << "Failed to determine odex file name: " << error_msg; |
| 115 | } |
| 116 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 117 | // Get the oat filename. |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 118 | std::string oat_file_name; |
| 119 | if (DexLocationToOatFilename(dex_location_, isa_, &oat_file_name, &error_msg)) { |
| 120 | oat_.Reset(oat_file_name); |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 121 | } else { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 122 | LOG(WARNING) << "Failed to determine oat file name for dex location " |
Calin Juravle | 9bfc6bb | 2017-05-04 00:13:50 +0000 | [diff] [blame] | 123 | << dex_location_ << ": " << error_msg; |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Check if the dex directory is writable. |
| 127 | // This will be needed in most uses of OatFileAssistant and so it's OK to |
| 128 | // compute it eagerly. (the only use which will not make use of it is |
| 129 | // OatFileAssistant::GetStatusDump()) |
| 130 | size_t pos = dex_location_.rfind('/'); |
| 131 | if (pos == std::string::npos) { |
| 132 | LOG(WARNING) << "Failed to determine dex file parent directory: " << dex_location_; |
| 133 | } else { |
| 134 | std::string parent = dex_location_.substr(0, pos); |
| 135 | if (access(parent.c_str(), W_OK) == 0) { |
| 136 | dex_parent_writable_ = true; |
| 137 | } else { |
| 138 | VLOG(oat) << "Dex parent of " << dex_location_ << " is not writable: " << strerror(errno); |
Richard Uhler | d684f52 | 2016-04-19 13:24:41 -0700 | [diff] [blame] | 139 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 140 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | OatFileAssistant::~OatFileAssistant() { |
| 144 | // Clean up the lock file. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 145 | if (flock_.get() != nullptr) { |
| 146 | unlink(flock_->GetPath().c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | bool OatFileAssistant::IsInBootClassPath() { |
| 151 | // Note: We check the current boot class path, regardless of the ISA |
| 152 | // specified by the user. This is okay, because the boot class path should |
| 153 | // be the same for all ISAs. |
| 154 | // TODO: Can we verify the boot class path is the same for all ISAs? |
| 155 | Runtime* runtime = Runtime::Current(); |
| 156 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 157 | const auto& boot_class_path = class_linker->GetBootClassPath(); |
| 158 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 159 | if (boot_class_path[i]->GetLocation() == dex_location_) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 160 | VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path"; |
| 161 | return true; |
| 162 | } |
| 163 | } |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | bool OatFileAssistant::Lock(std::string* error_msg) { |
| 168 | CHECK(error_msg != nullptr); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 169 | CHECK(flock_.get() == nullptr) << "OatFileAssistant::Lock already acquired"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 170 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 171 | // Note the lock will only succeed for secondary dex files and in test |
| 172 | // environment. |
| 173 | // |
| 174 | // The lock *will fail* for all primary apks in a production environment. |
| 175 | // The app does not have permissions to create locks next to its dex location |
| 176 | // (be it system, data or vendor parition). We also cannot use the odex or |
| 177 | // oat location for the same reasoning. |
| 178 | // |
| 179 | // This is best effort and if it fails it's unlikely that we will be able |
| 180 | // to generate oat files anyway. |
| 181 | std::string lock_file_name = dex_location_ + "." + GetInstructionSetString(isa_) + ".flock"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 182 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 183 | flock_ = LockedFile::Open(lock_file_name.c_str(), error_msg); |
| 184 | if (flock_.get() == nullptr) { |
Vladimir Marko | 66fdcbd | 2016-04-05 14:19:08 +0100 | [diff] [blame] | 185 | unlink(lock_file_name.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 191 | int OatFileAssistant::GetDexOptNeeded(CompilerFilter::Filter target, |
| 192 | bool profile_changed, |
| 193 | bool downgrade) { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 194 | OatFileInfo& info = GetBestInfo(); |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 195 | DexOptNeeded dexopt_needed = info.GetDexOptNeeded(target, profile_changed, downgrade); |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 196 | if (info.IsOatLocation() || dexopt_needed == kDex2OatFromScratch) { |
| 197 | return dexopt_needed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 198 | } |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 199 | return -dexopt_needed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 202 | // Figure out the currently specified compile filter option in the runtime. |
| 203 | // Returns true on success, false if the compiler filter is invalid, in which |
| 204 | // case error_msg describes the problem. |
| 205 | static bool GetRuntimeCompilerFilterOption(CompilerFilter::Filter* filter, |
| 206 | std::string* error_msg) { |
| 207 | CHECK(filter != nullptr); |
| 208 | CHECK(error_msg != nullptr); |
| 209 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 210 | *filter = OatFileAssistant::kDefaultCompilerFilterForDexLoading; |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 211 | for (StringPiece option : Runtime::Current()->GetCompilerOptions()) { |
| 212 | if (option.starts_with("--compiler-filter=")) { |
| 213 | const char* compiler_filter_string = option.substr(strlen("--compiler-filter=")).data(); |
| 214 | if (!CompilerFilter::ParseCompilerFilter(compiler_filter_string, filter)) { |
| 215 | *error_msg = std::string("Unknown --compiler-filter value: ") |
| 216 | + std::string(compiler_filter_string); |
| 217 | return false; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | return true; |
| 222 | } |
| 223 | |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 224 | bool OatFileAssistant::IsUpToDate() { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 225 | return GetBestInfo().Status() == kOatUpToDate; |
Richard Uhler | 01be681 | 2016-05-17 10:34:52 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 228 | OatFileAssistant::ResultOfAttemptToUpdate |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame^] | 229 | OatFileAssistant::MakeUpToDate(bool profile_changed, |
| 230 | const std::string& class_loader_context, |
| 231 | std::string* error_msg) { |
Richard Uhler | f4b3487 | 2016-04-13 11:03:46 -0700 | [diff] [blame] | 232 | CompilerFilter::Filter target; |
| 233 | if (!GetRuntimeCompilerFilterOption(&target, error_msg)) { |
| 234 | return kUpdateNotAttempted; |
| 235 | } |
| 236 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 237 | OatFileInfo& info = GetBestInfo(); |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame^] | 238 | // TODO(calin, jeffhao): the context should really be passed to GetDexOptNeeded: b/62269291. |
| 239 | // This is actually not trivial in the current logic as it will interact with the collision |
| 240 | // check: |
| 241 | // - currently, if the context does not match but we have no collisions we still accept the |
| 242 | // oat file. |
| 243 | // - if GetDexOptNeeded would return kDex2OatFromScratch for a context mismatch and we make |
| 244 | // the oat code up to date the collision check becomes useless. |
| 245 | // - however, MakeUpToDate will not always succeed (e.g. for primary apks, or for dex files |
| 246 | // loaded in other processes). So it boils down to how far do we want to complicate |
| 247 | // the logic in order to enable the use of oat files. Maybe its time to try simplify it. |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 248 | switch (info.GetDexOptNeeded(target, profile_changed, /*downgrade*/ false)) { |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 249 | case kNoDexOptNeeded: |
| 250 | return kUpdateSucceeded; |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 251 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 252 | // TODO: For now, don't bother with all the different ways we can call |
| 253 | // dex2oat to generate the oat file. Always generate the oat file as if it |
| 254 | // were kDex2OatFromScratch. |
| 255 | case kDex2OatFromScratch: |
| 256 | case kDex2OatForBootImage: |
| 257 | case kDex2OatForRelocation: |
| 258 | case kDex2OatForFilter: |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame^] | 259 | return GenerateOatFileNoChecks(info, target, class_loader_context, error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 260 | } |
| 261 | UNREACHABLE(); |
| 262 | } |
| 263 | |
| 264 | std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() { |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 265 | return GetBestInfo().ReleaseFileForUse(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 268 | std::string OatFileAssistant::GetStatusDump() { |
| 269 | std::ostringstream status; |
| 270 | bool oat_file_exists = false; |
| 271 | bool odex_file_exists = false; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 272 | if (oat_.Status() != kOatCannotOpen) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 273 | // If we can open the file, Filename should not return null. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 274 | CHECK(oat_.Filename() != nullptr); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 275 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 276 | oat_file_exists = true; |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 277 | status << *oat_.Filename() << "[status=" << oat_.Status() << ", "; |
| 278 | const OatFile* file = oat_.GetFile(); |
| 279 | if (file == nullptr) { |
| 280 | // If the file is null even though the status is not kOatCannotOpen, it |
| 281 | // means we must have a vdex file with no corresponding oat file. In |
| 282 | // this case we cannot determine the compilation filter. Indicate that |
| 283 | // we have only the vdex file instead. |
| 284 | status << "vdex-only"; |
| 285 | } else { |
| 286 | status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter()); |
| 287 | } |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 290 | if (odex_.Status() != kOatCannotOpen) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 291 | // If we can open the file, Filename should not return null. |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 292 | CHECK(odex_.Filename() != nullptr); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 293 | |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 294 | odex_file_exists = true; |
| 295 | if (oat_file_exists) { |
| 296 | status << "] "; |
| 297 | } |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 298 | status << *odex_.Filename() << "[status=" << odex_.Status() << ", "; |
| 299 | const OatFile* file = odex_.GetFile(); |
| 300 | if (file == nullptr) { |
| 301 | status << "vdex-only"; |
| 302 | } else { |
| 303 | status << "compilation_filter=" << CompilerFilter::NameOfFilter(file->GetCompilerFilter()); |
| 304 | } |
Richard Uhler | 46cc64f | 2016-11-14 14:53:55 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | if (!oat_file_exists && !odex_file_exists) { |
| 308 | status << "invalid["; |
| 309 | } |
| 310 | |
| 311 | status << "]"; |
| 312 | return status.str(); |
| 313 | } |
| 314 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 315 | std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles( |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 316 | const OatFile &oat_file, const char *dex_location) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 317 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 318 | if (LoadDexFiles(oat_file, dex_location, &dex_files)) { |
| 319 | return dex_files; |
| 320 | } else { |
| 321 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 322 | } |
| 323 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 324 | |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 325 | bool OatFileAssistant::LoadDexFiles( |
| 326 | const OatFile &oat_file, |
| 327 | const std::string& dex_location, |
| 328 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 329 | // Load the main dex file. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 330 | std::string error_msg; |
| 331 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile( |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 332 | dex_location.c_str(), nullptr, &error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 333 | if (oat_dex_file == nullptr) { |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 334 | LOG(WARNING) << error_msg; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 335 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 338 | 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] | 339 | if (dex_file.get() == nullptr) { |
| 340 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 341 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 342 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 343 | out_dex_files->push_back(std::move(dex_file)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 344 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 345 | // Load the rest of the multidex entries |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 346 | for (size_t i = 1;; i++) { |
| 347 | std::string multidex_dex_location = DexFile::GetMultiDexLocation(i, dex_location.c_str()); |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 348 | oat_dex_file = oat_file.GetOatDexFile(multidex_dex_location.c_str(), nullptr); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 349 | if (oat_dex_file == nullptr) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 350 | // There are no more multidex entries to load. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 354 | dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 355 | if (dex_file.get() == nullptr) { |
| 356 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 357 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 358 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 359 | out_dex_files->push_back(std::move(dex_file)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 360 | } |
Calin Juravle | 87e2cb6 | 2017-06-13 21:48:45 -0700 | [diff] [blame] | 361 | return true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 364 | bool OatFileAssistant::HasOriginalDexFiles() { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 365 | // Ensure GetRequiredDexChecksums has been run so that |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 366 | // has_original_dex_files_ is initialized. We don't care about the result of |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 367 | // GetRequiredDexChecksums. |
| 368 | GetRequiredDexChecksums(); |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 369 | return has_original_dex_files_; |
| 370 | } |
| 371 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 372 | OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 373 | return odex_.Status(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 376 | OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 377 | return oat_.Status(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 380 | bool OatFileAssistant::DexChecksumUpToDate(const VdexFile& file, std::string* error_msg) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 381 | const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums(); |
| 382 | if (required_dex_checksums == nullptr) { |
| 383 | LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date."; |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | uint32_t number_of_dex_files = file.GetHeader().GetNumberOfDexFiles(); |
| 388 | if (required_dex_checksums->size() != number_of_dex_files) { |
| 389 | *error_msg = StringPrintf("expected %zu dex files but found %u", |
| 390 | required_dex_checksums->size(), |
| 391 | number_of_dex_files); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 392 | return false; |
Andreas Gampe | f8cd890 | 2017-01-18 16:05:01 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 395 | for (uint32_t i = 0; i < number_of_dex_files; i++) { |
| 396 | uint32_t expected_checksum = (*required_dex_checksums)[i]; |
| 397 | uint32_t actual_checksum = file.GetLocationChecksum(i); |
| 398 | if (expected_checksum != actual_checksum) { |
| 399 | std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str()); |
| 400 | *error_msg = StringPrintf("Dex checksum does not match for dex: %s." |
| 401 | "Expected: %u, actual: %u", |
| 402 | dex.c_str(), |
| 403 | expected_checksum, |
| 404 | actual_checksum); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 405 | return false; |
| 406 | } |
| 407 | } |
| 408 | |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 409 | return true; |
| 410 | } |
| 411 | |
| 412 | bool OatFileAssistant::DexChecksumUpToDate(const OatFile& file, std::string* error_msg) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 413 | const std::vector<uint32_t>* required_dex_checksums = GetRequiredDexChecksums(); |
| 414 | if (required_dex_checksums == nullptr) { |
| 415 | LOG(WARNING) << "Required dex checksums not found. Assuming dex checksums are up to date."; |
| 416 | return true; |
| 417 | } |
| 418 | |
| 419 | uint32_t number_of_dex_files = file.GetOatHeader().GetDexFileCount(); |
| 420 | if (required_dex_checksums->size() != number_of_dex_files) { |
| 421 | *error_msg = StringPrintf("expected %zu dex files but found %u", |
| 422 | required_dex_checksums->size(), |
| 423 | number_of_dex_files); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 424 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 427 | for (uint32_t i = 0; i < number_of_dex_files; i++) { |
| 428 | std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str()); |
| 429 | uint32_t expected_checksum = (*required_dex_checksums)[i]; |
| 430 | const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile(dex.c_str(), nullptr); |
| 431 | if (oat_dex_file == nullptr) { |
| 432 | *error_msg = StringPrintf("failed to find %s in %s", dex.c_str(), file.GetLocation().c_str()); |
| 433 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 434 | } |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 435 | uint32_t actual_checksum = oat_dex_file->GetDexFileLocationChecksum(); |
| 436 | if (expected_checksum != actual_checksum) { |
| 437 | VLOG(oat) << "Dex checksum does not match for dex: " << dex |
| 438 | << ". Expected: " << expected_checksum |
| 439 | << ", Actual: " << actual_checksum; |
| 440 | return false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 443 | return true; |
| 444 | } |
| 445 | |
| 446 | OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) { |
| 447 | // Verify the ART_USE_READ_BARRIER state. |
| 448 | // TODO: Don't fully reject files due to read barrier state. If they contain |
| 449 | // compiled code and are otherwise okay, we should return something like |
| 450 | // kOatRelocationOutOfDate. If they don't contain compiled code, the read |
| 451 | // barrier state doesn't matter. |
| 452 | const bool is_cc = file.GetOatHeader().IsConcurrentCopying(); |
| 453 | constexpr bool kRuntimeIsCC = kUseReadBarrier; |
| 454 | if (is_cc != kRuntimeIsCC) { |
| 455 | return kOatCannotOpen; |
| 456 | } |
| 457 | |
| 458 | // Verify the dex checksum. |
| 459 | std::string error_msg; |
| 460 | if (kIsVdexEnabled) { |
| 461 | VdexFile* vdex = file.GetVdexFile(); |
| 462 | if (!DexChecksumUpToDate(*vdex, &error_msg)) { |
| 463 | LOG(ERROR) << error_msg; |
| 464 | return kOatDexOutOfDate; |
| 465 | } |
| 466 | } else { |
| 467 | if (!DexChecksumUpToDate(file, &error_msg)) { |
| 468 | LOG(ERROR) << error_msg; |
| 469 | return kOatDexOutOfDate; |
| 470 | } |
| 471 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 472 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 473 | CompilerFilter::Filter current_compiler_filter = file.GetCompilerFilter(); |
David Brazdil | ce4b0ba | 2016-01-28 15:05:49 +0000 | [diff] [blame] | 474 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 475 | // Verify the image checksum |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 476 | if (CompilerFilter::DependsOnImageChecksum(current_compiler_filter)) { |
| 477 | const ImageInfo* image_info = GetImageInfo(); |
| 478 | if (image_info == nullptr) { |
| 479 | VLOG(oat) << "No image for oat image checksum to match against."; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 480 | |
Richard Uhler | 76f5cb6 | 2016-04-04 13:30:16 -0700 | [diff] [blame] | 481 | if (HasOriginalDexFiles()) { |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 482 | return kOatBootImageOutOfDate; |
Richard Uhler | 76f5cb6 | 2016-04-04 13:30:16 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // If there is no original dex file to fall back to, grudgingly accept |
| 486 | // the oat file. This could technically lead to crashes, but there's no |
| 487 | // way we could find a better oat file to use for this dex location, |
| 488 | // and it's better than being stuck in a boot loop with no way out. |
| 489 | // The problem will hopefully resolve itself the next time the runtime |
| 490 | // starts up. |
| 491 | LOG(WARNING) << "Dex location " << dex_location_ << " does not seem to include dex file. " |
| 492 | << "Allow oat file use. This is potentially dangerous."; |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 493 | } else if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) { |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 494 | VLOG(oat) << "Oat image checksum does not match image checksum."; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 495 | return kOatBootImageOutOfDate; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 496 | } |
| 497 | } else { |
| 498 | VLOG(oat) << "Image checksum test skipped for compiler filter " << current_compiler_filter; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 501 | if (CompilerFilter::IsAotCompilationEnabled(current_compiler_filter)) { |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 502 | if (!file.IsPic()) { |
| 503 | const ImageInfo* image_info = GetImageInfo(); |
| 504 | if (image_info == nullptr) { |
| 505 | VLOG(oat) << "No image to check oat relocation against."; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 506 | return kOatRelocationOutOfDate; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 507 | } |
Richard Uhler | a62d2f0 | 2016-03-18 15:05:30 -0700 | [diff] [blame] | 508 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 509 | // Verify the oat_data_begin recorded for the image in the oat file matches |
| 510 | // the actual oat_data_begin for boot.oat in the image. |
| 511 | const OatHeader& oat_header = file.GetOatHeader(); |
| 512 | uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin(); |
| 513 | if (oat_data_begin != image_info->oat_data_begin) { |
| 514 | VLOG(oat) << file.GetLocation() << |
| 515 | ": Oat file image oat_data_begin (" << oat_data_begin << ")" |
| 516 | << " does not match actual image oat_data_begin (" |
| 517 | << image_info->oat_data_begin << ")"; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 518 | return kOatRelocationOutOfDate; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 519 | } |
Richard Uhler | a62d2f0 | 2016-03-18 15:05:30 -0700 | [diff] [blame] | 520 | |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 521 | // Verify the oat_patch_delta recorded for the image in the oat file matches |
| 522 | // the actual oat_patch_delta for the image. |
| 523 | int32_t oat_patch_delta = oat_header.GetImagePatchDelta(); |
| 524 | if (oat_patch_delta != image_info->patch_delta) { |
| 525 | VLOG(oat) << file.GetLocation() << |
| 526 | ": Oat file image patch delta (" << oat_patch_delta << ")" |
| 527 | << " does not match actual image patch delta (" |
| 528 | << image_info->patch_delta << ")"; |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 529 | return kOatRelocationOutOfDate; |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 530 | } |
| 531 | } else { |
| 532 | // Oat files compiled in PIC mode do not require relocation. |
| 533 | VLOG(oat) << "Oat relocation test skipped for PIC oat file"; |
| 534 | } |
| 535 | } else { |
| 536 | VLOG(oat) << "Oat relocation test skipped for compiler filter " << current_compiler_filter; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 537 | } |
Richard Uhler | e8109f7 | 2016-04-18 10:40:50 -0700 | [diff] [blame] | 538 | return kOatUpToDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 541 | static bool DexLocationToOdexNames(const std::string& location, |
| 542 | InstructionSet isa, |
| 543 | std::string* odex_filename, |
| 544 | std::string* oat_dir, |
| 545 | std::string* isa_dir, |
| 546 | std::string* error_msg) { |
| 547 | CHECK(odex_filename != nullptr); |
| 548 | CHECK(error_msg != nullptr); |
| 549 | |
| 550 | // The odex file name is formed by replacing the dex_location extension with |
| 551 | // .odex and inserting an oat/<isa> directory. For example: |
| 552 | // location = /foo/bar/baz.jar |
| 553 | // odex_location = /foo/bar/oat/<isa>/baz.odex |
| 554 | |
| 555 | // Find the directory portion of the dex location and add the oat/<isa> |
| 556 | // directory. |
| 557 | size_t pos = location.rfind('/'); |
| 558 | if (pos == std::string::npos) { |
| 559 | *error_msg = "Dex location " + location + " has no directory."; |
| 560 | return false; |
| 561 | } |
| 562 | std::string dir = location.substr(0, pos+1); |
| 563 | // Add the oat directory. |
| 564 | dir += "oat"; |
| 565 | if (oat_dir != nullptr) { |
| 566 | *oat_dir = dir; |
| 567 | } |
| 568 | // Add the isa directory |
| 569 | dir += "/" + std::string(GetInstructionSetString(isa)); |
| 570 | if (isa_dir != nullptr) { |
| 571 | *isa_dir = dir; |
| 572 | } |
| 573 | |
| 574 | // Get the base part of the file without the extension. |
| 575 | std::string file = location.substr(pos+1); |
| 576 | pos = file.rfind('.'); |
| 577 | if (pos == std::string::npos) { |
| 578 | *error_msg = "Dex location " + location + " has no extension."; |
| 579 | return false; |
| 580 | } |
| 581 | std::string base = file.substr(0, pos); |
| 582 | |
| 583 | *odex_filename = dir + "/" + base + ".odex"; |
| 584 | return true; |
| 585 | } |
| 586 | |
| 587 | // Prepare a subcomponent of the odex directory. |
| 588 | // (i.e. create and set the expected permissions on the path `dir`). |
| 589 | static bool PrepareDirectory(const std::string& dir, std::string* error_msg) { |
| 590 | struct stat dir_stat; |
| 591 | if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &dir_stat)) == 0) { |
| 592 | // The directory exists. Check if it is indeed a directory. |
| 593 | if (!S_ISDIR(dir_stat.st_mode)) { |
| 594 | *error_msg = dir + " is not a dir"; |
| 595 | return false; |
| 596 | } else { |
| 597 | // The dir is already on disk. |
| 598 | return true; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | // Failed to stat. We need to create the directory. |
| 603 | if (errno != ENOENT) { |
| 604 | *error_msg = "Could not stat isa dir " + dir + ":" + strerror(errno); |
| 605 | return false; |
| 606 | } |
| 607 | |
| 608 | mode_t mode = S_IRWXU | S_IXGRP | S_IXOTH; |
| 609 | if (mkdir(dir.c_str(), mode) != 0) { |
| 610 | *error_msg = "Could not create dir " + dir + ":" + strerror(errno); |
| 611 | return false; |
| 612 | } |
| 613 | if (chmod(dir.c_str(), mode) != 0) { |
| 614 | *error_msg = "Could not create the oat dir " + dir + ":" + strerror(errno); |
| 615 | return false; |
| 616 | } |
| 617 | return true; |
| 618 | } |
| 619 | |
| 620 | // Prepares the odex directory for the given dex location. |
| 621 | static bool PrepareOdexDirectories(const std::string& dex_location, |
| 622 | const std::string& expected_odex_location, |
| 623 | InstructionSet isa, |
| 624 | std::string* error_msg) { |
| 625 | std::string actual_odex_location; |
| 626 | std::string oat_dir; |
| 627 | std::string isa_dir; |
| 628 | if (!DexLocationToOdexNames( |
| 629 | dex_location, isa, &actual_odex_location, &oat_dir, &isa_dir, error_msg)) { |
| 630 | return false; |
| 631 | } |
| 632 | DCHECK_EQ(expected_odex_location, actual_odex_location); |
| 633 | |
| 634 | if (!PrepareDirectory(oat_dir, error_msg)) { |
| 635 | return false; |
| 636 | } |
| 637 | if (!PrepareDirectory(isa_dir, error_msg)) { |
| 638 | return false; |
| 639 | } |
| 640 | return true; |
| 641 | } |
| 642 | |
| 643 | OatFileAssistant::ResultOfAttemptToUpdate OatFileAssistant::GenerateOatFileNoChecks( |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame^] | 644 | OatFileAssistant::OatFileInfo& info, |
| 645 | CompilerFilter::Filter filter, |
| 646 | const std::string& class_loader_context, |
| 647 | std::string* error_msg) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 648 | CHECK(error_msg != nullptr); |
| 649 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 650 | Runtime* runtime = Runtime::Current(); |
| 651 | if (!runtime->IsDex2OatEnabled()) { |
| 652 | *error_msg = "Generation of oat file for dex location " + dex_location_ |
| 653 | + " not attempted because dex2oat is disabled."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 654 | return kUpdateNotAttempted; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 657 | if (info.Filename() == nullptr) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 658 | *error_msg = "Generation of oat file for dex location " + dex_location_ |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 659 | + " not attempted because the oat file name could not be determined."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 660 | return kUpdateNotAttempted; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 661 | } |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 662 | const std::string& oat_file_name = *info.Filename(); |
Calin Juravle | 367b9d8 | 2017-05-15 18:18:39 -0700 | [diff] [blame] | 663 | const std::string& vdex_file_name = GetVdexFilename(oat_file_name); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 664 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 665 | // dex2oat ignores missing dex files and doesn't report an error. |
| 666 | // Check explicitly here so we can detect the error properly. |
| 667 | // TODO: Why does dex2oat behave that way? |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 668 | struct stat dex_path_stat; |
| 669 | if (TEMP_FAILURE_RETRY(stat(dex_location_.c_str(), &dex_path_stat)) != 0) { |
| 670 | *error_msg = "Could not access dex location " + dex_location_ + ":" + strerror(errno); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 671 | return kUpdateNotAttempted; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 674 | // If this is the odex location, we need to create the odex file layout (../oat/isa/..) |
| 675 | if (!info.IsOatLocation()) { |
| 676 | if (!PrepareOdexDirectories(dex_location_, oat_file_name, isa_, error_msg)) { |
| 677 | return kUpdateNotAttempted; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // Set the permissions for the oat and the vdex files. |
| 682 | // The user always gets read and write while the group and others propagate |
| 683 | // the reading access of the original dex file. |
| 684 | mode_t file_mode = S_IRUSR | S_IWUSR | |
| 685 | (dex_path_stat.st_mode & S_IRGRP) | |
| 686 | (dex_path_stat.st_mode & S_IROTH); |
| 687 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 688 | std::unique_ptr<File> vdex_file(OS::CreateEmptyFile(vdex_file_name.c_str())); |
| 689 | if (vdex_file.get() == nullptr) { |
| 690 | *error_msg = "Generation of oat file " + oat_file_name |
| 691 | + " not attempted because the vdex file " + vdex_file_name |
| 692 | + " could not be opened."; |
| 693 | return kUpdateNotAttempted; |
| 694 | } |
| 695 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 696 | if (fchmod(vdex_file->Fd(), file_mode) != 0) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 697 | *error_msg = "Generation of oat file " + oat_file_name |
| 698 | + " not attempted because the vdex file " + vdex_file_name |
| 699 | + " could not be made world readable."; |
| 700 | return kUpdateNotAttempted; |
| 701 | } |
| 702 | |
| 703 | 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] | 704 | if (oat_file.get() == nullptr) { |
| 705 | *error_msg = "Generation of oat file " + oat_file_name |
| 706 | + " not attempted because the oat file could not be created."; |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 707 | return kUpdateNotAttempted; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 710 | if (fchmod(oat_file->Fd(), file_mode) != 0) { |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 711 | *error_msg = "Generation of oat file " + oat_file_name |
| 712 | + " not attempted because the oat file could not be made world readable."; |
| 713 | oat_file->Erase(); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 714 | return kUpdateNotAttempted; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | std::vector<std::string> args; |
| 718 | args.push_back("--dex-file=" + dex_location_); |
Nicolas Geoffray | a6a448a | 2016-11-10 10:49:40 +0000 | [diff] [blame] | 719 | args.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd())); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 720 | args.push_back("--oat-fd=" + std::to_string(oat_file->Fd())); |
| 721 | args.push_back("--oat-location=" + oat_file_name); |
Calin Juravle | 07c6d72 | 2017-06-07 17:06:12 +0000 | [diff] [blame] | 722 | args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter)); |
Calin Juravle | 27e0d1f | 2017-07-26 00:16:07 -0700 | [diff] [blame^] | 723 | args.push_back("--class-loader-context=" + class_loader_context); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 724 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 725 | if (!Dex2Oat(args, error_msg)) { |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 726 | // Manually delete the oat and vdex files. This ensures there is no garbage |
| 727 | // left over if the process unexpectedly died. |
| 728 | vdex_file->Erase(); |
| 729 | unlink(vdex_file_name.c_str()); |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 730 | oat_file->Erase(); |
Vladimir Marko | 66fdcbd | 2016-04-05 14:19:08 +0100 | [diff] [blame] | 731 | unlink(oat_file_name.c_str()); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 732 | return kUpdateFailed; |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 733 | } |
| 734 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 735 | if (vdex_file->FlushCloseOrErase() != 0) { |
| 736 | *error_msg = "Unable to close vdex file " + vdex_file_name; |
| 737 | unlink(vdex_file_name.c_str()); |
| 738 | return kUpdateFailed; |
| 739 | } |
| 740 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 741 | if (oat_file->FlushCloseOrErase() != 0) { |
| 742 | *error_msg = "Unable to close oat file " + oat_file_name; |
Vladimir Marko | 66fdcbd | 2016-04-05 14:19:08 +0100 | [diff] [blame] | 743 | unlink(oat_file_name.c_str()); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 744 | return kUpdateFailed; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 747 | // Mark that the odex file has changed and we should try to reload. |
| 748 | info.Reset(); |
Richard Uhler | 1e86061 | 2016-03-30 12:17:55 -0700 | [diff] [blame] | 749 | return kUpdateSucceeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args, |
| 753 | std::string* error_msg) { |
| 754 | Runtime* runtime = Runtime::Current(); |
| 755 | std::string image_location = ImageLocation(); |
| 756 | if (image_location.empty()) { |
| 757 | *error_msg = "No image location found for Dex2Oat."; |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | std::vector<std::string> argv; |
| 762 | argv.push_back(runtime->GetCompilerExecutable()); |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 763 | if (runtime->IsJavaDebuggable()) { |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 764 | argv.push_back("--debuggable"); |
| 765 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 766 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 767 | |
| 768 | if (!runtime->IsVerificationEnabled()) { |
| 769 | argv.push_back("--compiler-filter=verify-none"); |
| 770 | } |
| 771 | |
| 772 | if (runtime->MustRelocateIfPossible()) { |
| 773 | argv.push_back("--runtime-arg"); |
| 774 | argv.push_back("-Xrelocate"); |
| 775 | } else { |
| 776 | argv.push_back("--runtime-arg"); |
| 777 | argv.push_back("-Xnorelocate"); |
| 778 | } |
| 779 | |
| 780 | if (!kIsTargetBuild) { |
| 781 | argv.push_back("--host"); |
| 782 | } |
| 783 | |
| 784 | argv.push_back("--boot-image=" + image_location); |
| 785 | |
| 786 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 787 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 788 | |
| 789 | argv.insert(argv.end(), args.begin(), args.end()); |
| 790 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 791 | std::string command_line(android::base::Join(argv, ' ')); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 792 | return Exec(argv, error_msg); |
| 793 | } |
| 794 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 795 | bool OatFileAssistant::DexLocationToOdexFilename(const std::string& location, |
| 796 | InstructionSet isa, |
| 797 | std::string* odex_filename, |
| 798 | std::string* error_msg) { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 799 | return DexLocationToOdexNames(location, isa, odex_filename, nullptr, nullptr, error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 800 | } |
| 801 | |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 802 | bool OatFileAssistant::DexLocationToOatFilename(const std::string& location, |
| 803 | InstructionSet isa, |
| 804 | std::string* oat_filename, |
| 805 | std::string* error_msg) { |
| 806 | CHECK(oat_filename != nullptr); |
| 807 | CHECK(error_msg != nullptr); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 808 | |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame] | 809 | std::string cache_dir = GetDalvikCache(GetInstructionSetString(isa)); |
| 810 | if (cache_dir.empty()) { |
| 811 | *error_msg = "Dalvik cache directory does not exist"; |
| 812 | return false; |
| 813 | } |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 814 | |
| 815 | // TODO: The oat file assistant should be the definitive place for |
| 816 | // determining the oat file name from the dex location, not |
| 817 | // GetDalvikCacheFilename. |
Richard Uhler | b81881d | 2016-04-19 13:08:04 -0700 | [diff] [blame] | 818 | 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] | 819 | } |
| 820 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 821 | std::string OatFileAssistant::ImageLocation() { |
| 822 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 823 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
| 824 | runtime->GetHeap()->GetBootImageSpaces(); |
| 825 | if (image_spaces.empty()) { |
| 826 | return ""; |
| 827 | } |
| 828 | return image_spaces[0]->GetImageLocation(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 829 | } |
| 830 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 831 | const std::vector<uint32_t>* OatFileAssistant::GetRequiredDexChecksums() { |
| 832 | if (!required_dex_checksums_attempted_) { |
| 833 | required_dex_checksums_attempted_ = true; |
| 834 | required_dex_checksums_found_ = false; |
| 835 | cached_required_dex_checksums_.clear(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 836 | std::string error_msg; |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 837 | if (DexFile::GetMultiDexChecksums(dex_location_.c_str(), |
| 838 | &cached_required_dex_checksums_, |
| 839 | &error_msg)) { |
| 840 | required_dex_checksums_found_ = true; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 841 | has_original_dex_files_ = true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 842 | } else { |
| 843 | // This can happen if the original dex file has been stripped from the |
| 844 | // apk. |
| 845 | VLOG(oat) << "OatFileAssistant: " << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 846 | has_original_dex_files_ = false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 847 | |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 848 | // Get the checksums from the odex if we can. |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 849 | const OatFile* odex_file = odex_.GetFile(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 850 | if (odex_file != nullptr) { |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 851 | required_dex_checksums_found_ = true; |
| 852 | for (size_t i = 0; i < odex_file->GetOatHeader().GetDexFileCount(); i++) { |
| 853 | std::string dex = DexFile::GetMultiDexLocation(i, dex_location_.c_str()); |
| 854 | const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile(dex.c_str(), nullptr); |
| 855 | if (odex_dex_file == nullptr) { |
| 856 | required_dex_checksums_found_ = false; |
| 857 | break; |
| 858 | } |
| 859 | cached_required_dex_checksums_.push_back(odex_dex_file->GetDexFileLocationChecksum()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | } |
| 863 | } |
Richard Uhler | 69bcf2c | 2017-01-24 10:25:21 +0000 | [diff] [blame] | 864 | return required_dex_checksums_found_ ? &cached_required_dex_checksums_ : nullptr; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 865 | } |
| 866 | |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 867 | std::unique_ptr<OatFileAssistant::ImageInfo> |
| 868 | OatFileAssistant::ImageInfo::GetRuntimeImageInfo(InstructionSet isa, std::string* error_msg) { |
| 869 | CHECK(error_msg != nullptr); |
| 870 | |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 871 | Runtime* runtime = Runtime::Current(); |
Richard Uhler | 8f10486 | 2017-02-22 12:34:01 +0000 | [diff] [blame] | 872 | std::unique_ptr<ImageInfo> info(new ImageInfo()); |
| 873 | info->location = runtime->GetImageLocation(); |
| 874 | |
| 875 | std::unique_ptr<ImageHeader> image_header( |
| 876 | gc::space::ImageSpace::ReadImageHeader(info->location.c_str(), isa, error_msg)); |
| 877 | if (image_header == nullptr) { |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 878 | return nullptr; |
| 879 | } |
| 880 | |
Richard Uhler | 8f10486 | 2017-02-22 12:34:01 +0000 | [diff] [blame] | 881 | info->oat_checksum = image_header->GetOatChecksum(); |
| 882 | info->oat_data_begin = reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()); |
| 883 | info->patch_delta = image_header->GetPatchDelta(); |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 884 | return info; |
| 885 | } |
| 886 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 887 | const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() { |
| 888 | if (!image_info_load_attempted_) { |
| 889 | image_info_load_attempted_ = true; |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 890 | std::string error_msg; |
| 891 | cached_image_info_ = ImageInfo::GetRuntimeImageInfo(isa_, &error_msg); |
| 892 | if (cached_image_info_ == nullptr) { |
| 893 | LOG(WARNING) << "Unable to get runtime image info: " << error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 894 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 895 | } |
Richard Uhler | 95e0967 | 2017-02-22 11:37:41 +0000 | [diff] [blame] | 896 | return cached_image_info_.get(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 897 | } |
| 898 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 899 | OatFileAssistant::OatFileInfo& OatFileAssistant::GetBestInfo() { |
Calin Juravle | 357c66d | 2017-05-04 01:57:17 +0000 | [diff] [blame] | 900 | // TODO(calin): Document the side effects of class loading when |
| 901 | // running dalvikvm command line. |
| 902 | if (dex_parent_writable_) { |
| 903 | // If the parent of the dex file is writable it means that we can |
| 904 | // create the odex file. In this case we unconditionally pick the odex |
| 905 | // as the best oat file. This corresponds to the regular use case when |
| 906 | // apps gets installed or when they load private, secondary dex file. |
| 907 | // For apps on the system partition the odex location will not be |
| 908 | // writable and thus the oat location might be more up to date. |
| 909 | return odex_; |
| 910 | } |
| 911 | |
| 912 | // We cannot write to the odex location. This must be a system app. |
| 913 | |
| 914 | // If the oat location is usable take it. |
| 915 | if (oat_.IsUseable()) { |
| 916 | return oat_; |
| 917 | } |
| 918 | |
| 919 | // The oat file is not usable but the odex file might be up to date. |
| 920 | // This is an indication that we are dealing with an up to date prebuilt |
| 921 | // (that doesn't need relocation). |
| 922 | if (odex_.Status() == kOatUpToDate) { |
| 923 | return odex_; |
| 924 | } |
| 925 | |
| 926 | // The oat file is not usable and the odex file is not up to date. |
| 927 | // However we have access to the original dex file which means we can make |
| 928 | // the oat location up to date. |
| 929 | if (HasOriginalDexFiles()) { |
| 930 | return oat_; |
| 931 | } |
| 932 | |
| 933 | // We got into the worst situation here: |
| 934 | // - the oat location is not usable |
| 935 | // - the prebuild odex location is not up to date |
| 936 | // - and we don't have the original dex file anymore (stripped). |
| 937 | // Pick the odex if it exists, or the oat if not. |
| 938 | return (odex_.Status() == kOatCannotOpen) ? oat_ : odex_; |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 941 | std::unique_ptr<gc::space::ImageSpace> OatFileAssistant::OpenImageSpace(const OatFile* oat_file) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 942 | DCHECK(oat_file != nullptr); |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 943 | std::string art_file = ReplaceFileExtension(oat_file->GetLocation(), "art"); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 944 | if (art_file.empty()) { |
| 945 | return nullptr; |
| 946 | } |
| 947 | std::string error_msg; |
| 948 | ScopedObjectAccess soa(Thread::Current()); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 949 | std::unique_ptr<gc::space::ImageSpace> ret = |
| 950 | gc::space::ImageSpace::CreateFromAppImage(art_file.c_str(), oat_file, &error_msg); |
Mathieu Chartier | e778fc7 | 2016-01-25 20:11:28 -0800 | [diff] [blame] | 951 | 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] | 952 | LOG(INFO) << "Failed to open app image " << art_file.c_str() << " " << error_msg; |
| 953 | } |
| 954 | return ret; |
| 955 | } |
| 956 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 957 | OatFileAssistant::OatFileInfo::OatFileInfo(OatFileAssistant* oat_file_assistant, |
| 958 | bool is_oat_location) |
| 959 | : oat_file_assistant_(oat_file_assistant), is_oat_location_(is_oat_location) |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 960 | {} |
| 961 | |
Richard Uhler | 88bc673 | 2016-11-14 14:38:03 +0000 | [diff] [blame] | 962 | bool OatFileAssistant::OatFileInfo::IsOatLocation() { |
| 963 | return is_oat_location_; |
| 964 | } |
| 965 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 966 | const std::string* OatFileAssistant::OatFileInfo::Filename() { |
| 967 | return filename_provided_ ? &filename_ : nullptr; |
| 968 | } |
| 969 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 970 | bool OatFileAssistant::OatFileInfo::IsUseable() { |
| 971 | switch (Status()) { |
| 972 | case kOatCannotOpen: |
| 973 | case kOatDexOutOfDate: |
| 974 | case kOatBootImageOutOfDate: return false; |
| 975 | |
| 976 | case kOatRelocationOutOfDate: |
| 977 | case kOatUpToDate: return true; |
| 978 | } |
| 979 | UNREACHABLE(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | OatFileAssistant::OatStatus OatFileAssistant::OatFileInfo::Status() { |
| 983 | if (!status_attempted_) { |
| 984 | status_attempted_ = true; |
| 985 | const OatFile* file = GetFile(); |
| 986 | if (file == nullptr) { |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 987 | // Check to see if there is a vdex file we can make use of. |
| 988 | std::string error_msg; |
Calin Juravle | 367b9d8 | 2017-05-15 18:18:39 -0700 | [diff] [blame] | 989 | std::string vdex_filename = GetVdexFilename(filename_); |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 990 | std::unique_ptr<VdexFile> vdex = VdexFile::Open(vdex_filename, |
| 991 | /*writeable*/false, |
| 992 | /*low_4gb*/false, |
Nicolas Geoffray | 4e868fa | 2017-04-21 17:16:44 +0100 | [diff] [blame] | 993 | /*unquicken*/false, |
Richard Uhler | 2f27abd | 2017-01-31 14:02:34 +0000 | [diff] [blame] | 994 | &error_msg); |
| 995 | if (vdex == nullptr) { |
| 996 | status_ = kOatCannotOpen; |
| 997 | VLOG(oat) << "unable to open vdex file " << vdex_filename << ": " << error_msg; |
| 998 | } else { |
| 999 | if (oat_file_assistant_->DexChecksumUpToDate(*vdex, &error_msg)) { |
| 1000 | // The vdex file does not contain enough information to determine |
| 1001 | // whether it is up to date with respect to the boot image, so we |
| 1002 | // assume it is out of date. |
| 1003 | VLOG(oat) << error_msg; |
| 1004 | status_ = kOatBootImageOutOfDate; |
| 1005 | } else { |
| 1006 | status_ = kOatDexOutOfDate; |
| 1007 | } |
| 1008 | } |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1009 | } else { |
| 1010 | status_ = oat_file_assistant_->GivenOatFileStatus(*file); |
Richard Uhler | 9a37efc | 2016-08-05 16:32:55 -0700 | [diff] [blame] | 1011 | VLOG(oat) << file->GetLocation() << " is " << status_ |
| 1012 | << " with filter " << file->GetCompilerFilter(); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | return status_; |
| 1016 | } |
| 1017 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1018 | OatFileAssistant::DexOptNeeded OatFileAssistant::OatFileInfo::GetDexOptNeeded( |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 1019 | CompilerFilter::Filter target, bool profile_changed, bool downgrade) { |
Nicolas Geoffray | 49cda06 | 2017-04-21 13:08:25 +0100 | [diff] [blame] | 1020 | bool compilation_desired = CompilerFilter::IsAotCompilationEnabled(target); |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 1021 | bool filter_okay = CompilerFilterIsOkay(target, profile_changed, downgrade); |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1022 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1023 | if (filter_okay && Status() == kOatUpToDate) { |
| 1024 | // The oat file is in good shape as is. |
| 1025 | return kNoDexOptNeeded; |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1028 | if (filter_okay && !compilation_desired && Status() == kOatRelocationOutOfDate) { |
| 1029 | // If no compilation is desired, then it doesn't matter if the oat |
| 1030 | // file needs relocation. It's in good shape as is. |
| 1031 | return kNoDexOptNeeded; |
| 1032 | } |
| 1033 | |
Nicolas Geoffray | 08e9eed | 2017-04-25 17:36:51 +0100 | [diff] [blame] | 1034 | if (filter_okay && Status() == kOatRelocationOutOfDate) { |
| 1035 | return kDex2OatForRelocation; |
Richard Uhler | 7225a8d | 2016-11-22 10:12:03 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Nicolas Geoffray | 08e9eed | 2017-04-25 17:36:51 +0100 | [diff] [blame] | 1038 | if (IsUseable()) { |
| 1039 | return kDex2OatForFilter; |
| 1040 | } |
| 1041 | |
| 1042 | if (Status() == kOatBootImageOutOfDate) { |
| 1043 | return kDex2OatForBootImage; |
| 1044 | } |
| 1045 | |
| 1046 | if (oat_file_assistant_->HasOriginalDexFiles()) { |
| 1047 | return kDex2OatFromScratch; |
| 1048 | } else { |
| 1049 | // Otherwise there is nothing we can do, even if we want to. |
| 1050 | return kNoDexOptNeeded; |
| 1051 | } |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1054 | const OatFile* OatFileAssistant::OatFileInfo::GetFile() { |
| 1055 | CHECK(!file_released_) << "GetFile called after oat file released."; |
| 1056 | if (!load_attempted_) { |
| 1057 | load_attempted_ = true; |
| 1058 | if (filename_provided_) { |
| 1059 | std::string error_msg; |
| 1060 | file_.reset(OatFile::Open(filename_.c_str(), |
| 1061 | filename_.c_str(), |
| 1062 | nullptr, |
| 1063 | nullptr, |
| 1064 | oat_file_assistant_->load_executable_, |
| 1065 | /*low_4gb*/false, |
| 1066 | oat_file_assistant_->dex_location_.c_str(), |
| 1067 | &error_msg)); |
| 1068 | if (file_.get() == nullptr) { |
| 1069 | VLOG(oat) << "OatFileAssistant test for existing oat file " |
| 1070 | << filename_ << ": " << error_msg; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | return file_.get(); |
| 1075 | } |
| 1076 | |
| 1077 | bool OatFileAssistant::OatFileInfo::CompilerFilterIsOkay( |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 1078 | CompilerFilter::Filter target, bool profile_changed, bool downgrade) { |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1079 | const OatFile* file = GetFile(); |
| 1080 | if (file == nullptr) { |
| 1081 | return false; |
| 1082 | } |
| 1083 | |
| 1084 | CompilerFilter::Filter current = file->GetCompilerFilter(); |
| 1085 | if (profile_changed && CompilerFilter::DependsOnProfile(current)) { |
| 1086 | VLOG(oat) << "Compiler filter not okay because Profile changed"; |
| 1087 | return false; |
| 1088 | } |
Shubham Ajmera | e4e812a | 2017-05-25 20:09:58 -0700 | [diff] [blame] | 1089 | return downgrade ? !CompilerFilter::IsBetter(current, target) : |
| 1090 | CompilerFilter::IsAsGoodAs(current, target); |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | bool OatFileAssistant::OatFileInfo::IsExecutable() { |
| 1094 | const OatFile* file = GetFile(); |
| 1095 | return (file != nullptr && file->IsExecutable()); |
| 1096 | } |
| 1097 | |
Richard Uhler | 743bf36 | 2016-04-19 15:39:37 -0700 | [diff] [blame] | 1098 | void OatFileAssistant::OatFileInfo::Reset() { |
| 1099 | load_attempted_ = false; |
| 1100 | file_.reset(); |
| 1101 | status_attempted_ = false; |
| 1102 | } |
| 1103 | |
| 1104 | void OatFileAssistant::OatFileInfo::Reset(const std::string& filename) { |
| 1105 | filename_provided_ = true; |
| 1106 | filename_ = filename; |
| 1107 | Reset(); |
| 1108 | } |
| 1109 | |
| 1110 | std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFile() { |
| 1111 | file_released_ = true; |
| 1112 | return std::move(file_); |
| 1113 | } |
| 1114 | |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1115 | std::unique_ptr<OatFile> OatFileAssistant::OatFileInfo::ReleaseFileForUse() { |
Richard Uhler | 3e580bc | 2016-11-08 16:23:07 +0000 | [diff] [blame] | 1116 | if (Status() == kOatUpToDate) { |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1117 | return ReleaseFile(); |
| 1118 | } |
| 1119 | |
| 1120 | VLOG(oat) << "Oat File Assistant: No relocated oat file found," |
| 1121 | << " attempting to fall back to interpreting oat file instead."; |
| 1122 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 1123 | if (Status() == kOatRelocationOutOfDate && !IsExecutable()) { |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1124 | return ReleaseFile(); |
| 1125 | } |
| 1126 | |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 1127 | if (Status() == kOatRelocationOutOfDate) { |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1128 | // We are loading an oat file for runtime use that needs relocation. |
| 1129 | // Reload the file non-executable to ensure that we interpret out of the |
| 1130 | // dex code in the oat file rather than trying to execute the unrelocated |
| 1131 | // compiled code. |
| 1132 | oat_file_assistant_->load_executable_ = false; |
| 1133 | Reset(); |
Richard Uhler | 03bc659 | 2016-11-22 09:42:04 +0000 | [diff] [blame] | 1134 | if (IsUseable()) { |
Richard Uhler | 70a8426 | 2016-11-08 16:51:51 +0000 | [diff] [blame] | 1135 | CHECK(!IsExecutable()); |
| 1136 | return ReleaseFile(); |
| 1137 | } |
| 1138 | } |
| 1139 | return std::unique_ptr<OatFile>(); |
| 1140 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1141 | } // namespace art |