Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 "image_space.h" |
| 18 | |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 20 | #include <lz4.h> |
| 21 | #include <random> |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 22 | #include <sys/statvfs.h> |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 23 | #include <sys/types.h> |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 25 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 26 | #include "art_method.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 27 | #include "base/macros.h" |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 28 | #include "base/stl_util.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 29 | #include "base/scoped_flock.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 30 | #include "base/time_utils.h" |
| 31 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 32 | #include "gc/accounting/space_bitmap-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 33 | #include "mirror/class-inl.h" |
| 34 | #include "mirror/object-inl.h" |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 35 | #include "oat_file.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 36 | #include "os.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 37 | #include "space-inl.h" |
| 38 | #include "utils.h" |
| 39 | |
| 40 | namespace art { |
| 41 | namespace gc { |
| 42 | namespace space { |
| 43 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 44 | Atomic<uint32_t> ImageSpace::bitmap_index_(0); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 45 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 46 | ImageSpace::ImageSpace(const std::string& image_filename, |
| 47 | const char* image_location, |
| 48 | MemMap* mem_map, |
| 49 | accounting::ContinuousSpaceBitmap* live_bitmap, |
| 50 | uint8_t* end, |
| 51 | MemMap* shadow_map) |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 52 | : MemMapSpace(image_filename, mem_map, mem_map->Begin(), end, end, |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 53 | kGcRetentionPolicyNeverCollect), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 54 | oat_file_non_owned_(nullptr), |
| 55 | image_location_(image_location), |
| 56 | shadow_map_(shadow_map) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 57 | DCHECK(live_bitmap != nullptr); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 58 | live_bitmap_.reset(live_bitmap); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 61 | static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) { |
| 62 | CHECK_ALIGNED(min_delta, kPageSize); |
| 63 | CHECK_ALIGNED(max_delta, kPageSize); |
| 64 | CHECK_LT(min_delta, max_delta); |
| 65 | |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 66 | int32_t r = GetRandomNumber<int32_t>(min_delta, max_delta); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 67 | if (r % 2 == 0) { |
| 68 | r = RoundUp(r, kPageSize); |
| 69 | } else { |
| 70 | r = RoundDown(r, kPageSize); |
| 71 | } |
| 72 | CHECK_LE(min_delta, r); |
| 73 | CHECK_GE(max_delta, r); |
| 74 | CHECK_ALIGNED(r, kPageSize); |
| 75 | return r; |
| 76 | } |
| 77 | |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 78 | // We are relocating or generating the core image. We should get rid of everything. It is all |
Andreas Gampe | 8db9dcd | 2014-11-09 18:14:30 -0800 | [diff] [blame] | 79 | // out-of-date. We also don't really care if this fails since it is just a convenience. |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 80 | // Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c |
| 81 | // Note this should only be used during first boot. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 82 | static void RealPruneDalvikCache(const std::string& cache_dir_path); |
Andreas Gampe | 8db9dcd | 2014-11-09 18:14:30 -0800 | [diff] [blame] | 83 | |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 84 | static void PruneDalvikCache(InstructionSet isa) { |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 85 | CHECK_NE(isa, kNone); |
Andreas Gampe | 8db9dcd | 2014-11-09 18:14:30 -0800 | [diff] [blame] | 86 | // Prune the base /data/dalvik-cache. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 87 | RealPruneDalvikCache(GetDalvikCacheOrDie(".", false)); |
Andreas Gampe | 8db9dcd | 2014-11-09 18:14:30 -0800 | [diff] [blame] | 88 | // Prune /data/dalvik-cache/<isa>. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 89 | RealPruneDalvikCache(GetDalvikCacheOrDie(GetInstructionSetString(isa), false)); |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 90 | } |
Andreas Gampe | 8db9dcd | 2014-11-09 18:14:30 -0800 | [diff] [blame] | 91 | |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 92 | static void RealPruneDalvikCache(const std::string& cache_dir_path) { |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 93 | if (!OS::DirectoryExists(cache_dir_path.c_str())) { |
| 94 | return; |
| 95 | } |
| 96 | DIR* cache_dir = opendir(cache_dir_path.c_str()); |
| 97 | if (cache_dir == nullptr) { |
| 98 | PLOG(WARNING) << "Unable to open " << cache_dir_path << " to delete it's contents"; |
| 99 | return; |
| 100 | } |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 101 | |
| 102 | for (struct dirent* de = readdir(cache_dir); de != nullptr; de = readdir(cache_dir)) { |
| 103 | const char* name = de->d_name; |
| 104 | if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { |
| 105 | continue; |
| 106 | } |
Andreas Gampe | 8db9dcd | 2014-11-09 18:14:30 -0800 | [diff] [blame] | 107 | // We only want to delete regular files and symbolic links. |
| 108 | if (de->d_type != DT_REG && de->d_type != DT_LNK) { |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 109 | if (de->d_type != DT_DIR) { |
| 110 | // We do expect some directories (namely the <isa> for pruning the base dalvik-cache). |
| 111 | LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered."; |
| 112 | } |
| 113 | continue; |
| 114 | } |
Brian Carlstrom | debdda0 | 2014-08-28 22:17:13 -0700 | [diff] [blame] | 115 | std::string cache_file(cache_dir_path); |
| 116 | cache_file += '/'; |
| 117 | cache_file += name; |
| 118 | if (TEMP_FAILURE_RETRY(unlink(cache_file.c_str())) != 0) { |
| 119 | PLOG(ERROR) << "Unable to unlink " << cache_file; |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 120 | continue; |
| 121 | } |
| 122 | } |
| 123 | CHECK_EQ(0, TEMP_FAILURE_RETRY(closedir(cache_dir))) << "Unable to close directory."; |
| 124 | } |
| 125 | |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 126 | // We write out an empty file to the zygote's ISA specific cache dir at the start of |
| 127 | // every zygote boot and delete it when the boot completes. If we find a file already |
| 128 | // present, it usually means the boot didn't complete. We wipe the entire dalvik |
| 129 | // cache if that's the case. |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 130 | static void MarkZygoteStart(const InstructionSet isa, const uint32_t max_failed_boots) { |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 131 | const std::string isa_subdir = GetDalvikCacheOrDie(GetInstructionSetString(isa), false); |
| 132 | const std::string boot_marker = isa_subdir + "/.booting"; |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 133 | const char* file_name = boot_marker.c_str(); |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 134 | |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 135 | uint32_t num_failed_boots = 0; |
| 136 | std::unique_ptr<File> file(OS::OpenFileReadWrite(file_name)); |
| 137 | if (file.get() == nullptr) { |
| 138 | file.reset(OS::CreateEmptyFile(file_name)); |
| 139 | |
| 140 | if (file.get() == nullptr) { |
| 141 | PLOG(WARNING) << "Failed to create boot marker."; |
| 142 | return; |
| 143 | } |
| 144 | } else { |
| 145 | if (!file->ReadFully(&num_failed_boots, sizeof(num_failed_boots))) { |
| 146 | PLOG(WARNING) << "Failed to read boot marker."; |
| 147 | file->Erase(); |
| 148 | return; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (max_failed_boots != 0 && num_failed_boots > max_failed_boots) { |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 153 | LOG(WARNING) << "Incomplete boot detected. Pruning dalvik cache"; |
| 154 | RealPruneDalvikCache(isa_subdir); |
| 155 | } |
| 156 | |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 157 | ++num_failed_boots; |
| 158 | VLOG(startup) << "Number of failed boots on : " << boot_marker << " = " << num_failed_boots; |
| 159 | |
| 160 | if (lseek(file->Fd(), 0, SEEK_SET) == -1) { |
| 161 | PLOG(WARNING) << "Failed to write boot marker."; |
| 162 | file->Erase(); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | if (!file->WriteFully(&num_failed_boots, sizeof(num_failed_boots))) { |
| 167 | PLOG(WARNING) << "Failed to write boot marker."; |
| 168 | file->Erase(); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (file->FlushCloseOrErase() != 0) { |
| 173 | PLOG(WARNING) << "Failed to flush boot marker."; |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 177 | static bool GenerateImage(const std::string& image_filename, InstructionSet image_isa, |
| 178 | std::string* error_msg) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 179 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
| 180 | std::vector<std::string> boot_class_path; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 181 | Split(boot_class_path_string, ':', &boot_class_path); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 182 | if (boot_class_path.empty()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 183 | *error_msg = "Failed to generate image because no boot class path specified"; |
| 184 | return false; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 185 | } |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 186 | // We should clean up so we are more likely to have room for the image. |
| 187 | if (Runtime::Current()->IsZygote()) { |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 188 | LOG(INFO) << "Pruning dalvik-cache since we are generating an image and will need to recompile"; |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 189 | PruneDalvikCache(image_isa); |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 190 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 191 | |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 192 | std::vector<std::string> arg_vector; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 193 | |
Tsu Chiang Chuang | 12e6d74 | 2014-05-22 10:22:25 -0700 | [diff] [blame] | 194 | std::string dex2oat(Runtime::Current()->GetCompilerExecutable()); |
Mathieu Chartier | 08d7d44 | 2013-07-31 18:08:51 -0700 | [diff] [blame] | 195 | arg_vector.push_back(dex2oat); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 196 | |
| 197 | std::string image_option_string("--image="); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 198 | image_option_string += image_filename; |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 199 | arg_vector.push_back(image_option_string); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 200 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 201 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 202 | arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | std::string oat_file_option_string("--oat-file="); |
Brian Carlstrom | 2f1e15c | 2014-10-27 16:27:06 -0700 | [diff] [blame] | 206 | oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename); |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 207 | arg_vector.push_back(oat_file_option_string); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 208 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 209 | // Note: we do not generate a fully debuggable boot image so we do not pass the |
| 210 | // compiler flag --debuggable here. |
| 211 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 212 | Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector); |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 213 | CHECK_EQ(image_isa, kRuntimeISA) |
| 214 | << "We should always be generating an image for the current isa."; |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 215 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 216 | int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, |
| 217 | ART_BASE_ADDRESS_MAX_DELTA); |
| 218 | LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default " |
| 219 | << "art base address of 0x" << std::hex << ART_BASE_ADDRESS; |
| 220 | arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset)); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 221 | |
Brian Carlstrom | 57309db | 2014-07-30 15:13:25 -0700 | [diff] [blame] | 222 | if (!kIsTargetBuild) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 223 | arg_vector.push_back("--host"); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 226 | const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 227 | for (size_t i = 0; i < compiler_options.size(); ++i) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 228 | arg_vector.push_back(compiler_options[i].c_str()); |
| 229 | } |
| 230 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 231 | std::string command_line(Join(arg_vector, ' ')); |
| 232 | LOG(INFO) << "GenerateImage: " << command_line; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 233 | return Exec(arg_vector, error_msg); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 236 | bool ImageSpace::FindImageFilename(const char* image_location, |
| 237 | const InstructionSet image_isa, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 238 | std::string* system_filename, |
| 239 | bool* has_system, |
| 240 | std::string* cache_filename, |
| 241 | bool* dalvik_cache_exists, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 242 | bool* has_cache, |
| 243 | bool* is_global_cache) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 244 | *has_system = false; |
| 245 | *has_cache = false; |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 246 | // image_location = /system/framework/boot.art |
| 247 | // system_image_location = /system/framework/<image_isa>/boot.art |
| 248 | std::string system_image_filename(GetSystemImageFilename(image_location, image_isa)); |
| 249 | if (OS::FileExists(system_image_filename.c_str())) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 250 | *system_filename = system_image_filename; |
| 251 | *has_system = true; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 252 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 253 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 254 | bool have_android_data = false; |
| 255 | *dalvik_cache_exists = false; |
| 256 | std::string dalvik_cache; |
| 257 | GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 258 | &have_android_data, dalvik_cache_exists, is_global_cache); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 259 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 260 | if (have_android_data && *dalvik_cache_exists) { |
| 261 | // Always set output location even if it does not exist, |
| 262 | // so that the caller knows where to create the image. |
| 263 | // |
| 264 | // image_location = /system/framework/boot.art |
| 265 | // *image_filename = /data/dalvik-cache/<image_isa>/boot.art |
| 266 | std::string error_msg; |
| 267 | if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) { |
| 268 | LOG(WARNING) << error_msg; |
| 269 | return *has_system; |
| 270 | } |
| 271 | *has_cache = OS::FileExists(cache_filename->c_str()); |
| 272 | } |
| 273 | return *has_system || *has_cache; |
| 274 | } |
| 275 | |
| 276 | static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) { |
| 277 | std::unique_ptr<File> image_file(OS::OpenFileForReading(filename)); |
| 278 | if (image_file.get() == nullptr) { |
| 279 | return false; |
| 280 | } |
| 281 | const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader)); |
| 282 | if (!success || !image_header->IsValid()) { |
| 283 | return false; |
| 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 288 | // Relocate the image at image_location to dest_filename and relocate it by a random amount. |
| 289 | static bool RelocateImage(const char* image_location, const char* dest_filename, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 290 | InstructionSet isa, std::string* error_msg) { |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 291 | // We should clean up so we are more likely to have room for the image. |
| 292 | if (Runtime::Current()->IsZygote()) { |
| 293 | LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile"; |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 294 | PruneDalvikCache(isa); |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 297 | std::string patchoat(Runtime::Current()->GetPatchoatExecutable()); |
| 298 | |
| 299 | std::string input_image_location_arg("--input-image-location="); |
| 300 | input_image_location_arg += image_location; |
| 301 | |
| 302 | std::string output_image_filename_arg("--output-image-file="); |
| 303 | output_image_filename_arg += dest_filename; |
| 304 | |
| 305 | std::string input_oat_location_arg("--input-oat-location="); |
| 306 | input_oat_location_arg += ImageHeader::GetOatLocationFromImageLocation(image_location); |
| 307 | |
| 308 | std::string output_oat_filename_arg("--output-oat-file="); |
| 309 | output_oat_filename_arg += ImageHeader::GetOatLocationFromImageLocation(dest_filename); |
| 310 | |
| 311 | std::string instruction_set_arg("--instruction-set="); |
| 312 | instruction_set_arg += GetInstructionSetString(isa); |
| 313 | |
| 314 | std::string base_offset_arg("--base-offset-delta="); |
| 315 | StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, |
| 316 | ART_BASE_ADDRESS_MAX_DELTA)); |
| 317 | |
| 318 | std::vector<std::string> argv; |
| 319 | argv.push_back(patchoat); |
| 320 | |
| 321 | argv.push_back(input_image_location_arg); |
| 322 | argv.push_back(output_image_filename_arg); |
| 323 | |
| 324 | argv.push_back(input_oat_location_arg); |
| 325 | argv.push_back(output_oat_filename_arg); |
| 326 | |
| 327 | argv.push_back(instruction_set_arg); |
| 328 | argv.push_back(base_offset_arg); |
| 329 | |
| 330 | std::string command_line(Join(argv, ' ')); |
| 331 | LOG(INFO) << "RelocateImage: " << command_line; |
| 332 | return Exec(argv, error_msg); |
| 333 | } |
| 334 | |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 335 | static ImageHeader* ReadSpecificImageHeader(const char* filename, std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 336 | std::unique_ptr<ImageHeader> hdr(new ImageHeader); |
| 337 | if (!ReadSpecificImageHeader(filename, hdr.get())) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 338 | *error_msg = StringPrintf("Unable to read image header for %s", filename); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 339 | return nullptr; |
| 340 | } |
| 341 | return hdr.release(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location, |
| 345 | const InstructionSet image_isa) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 346 | std::string error_msg; |
| 347 | ImageHeader* image_header = ReadImageHeader(image_location, image_isa, &error_msg); |
| 348 | if (image_header == nullptr) { |
| 349 | LOG(FATAL) << error_msg; |
| 350 | } |
| 351 | return image_header; |
| 352 | } |
| 353 | |
| 354 | ImageHeader* ImageSpace::ReadImageHeader(const char* image_location, |
| 355 | const InstructionSet image_isa, |
| 356 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 357 | std::string system_filename; |
| 358 | bool has_system = false; |
| 359 | std::string cache_filename; |
| 360 | bool has_cache = false; |
| 361 | bool dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 362 | bool is_global_cache = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 363 | if (FindImageFilename(image_location, image_isa, &system_filename, &has_system, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 364 | &cache_filename, &dalvik_cache_exists, &has_cache, &is_global_cache)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 365 | if (Runtime::Current()->ShouldRelocate()) { |
| 366 | if (has_system && has_cache) { |
| 367 | std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader); |
| 368 | std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader); |
| 369 | if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 370 | *error_msg = StringPrintf("Unable to read image header for %s at %s", |
| 371 | image_location, system_filename.c_str()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 372 | return nullptr; |
| 373 | } |
| 374 | if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 375 | *error_msg = StringPrintf("Unable to read image header for %s at %s", |
| 376 | image_location, cache_filename.c_str()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 377 | return nullptr; |
| 378 | } |
| 379 | if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 380 | *error_msg = StringPrintf("Unable to find a relocated version of image file %s", |
| 381 | image_location); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 382 | return nullptr; |
| 383 | } |
| 384 | return cache_hdr.release(); |
| 385 | } else if (!has_cache) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 386 | *error_msg = StringPrintf("Unable to find a relocated version of image file %s", |
| 387 | image_location); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 388 | return nullptr; |
| 389 | } else if (!has_system && has_cache) { |
| 390 | // This can probably just use the cache one. |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 391 | return ReadSpecificImageHeader(cache_filename.c_str(), error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 392 | } |
| 393 | } else { |
| 394 | // We don't want to relocate, Just pick the appropriate one if we have it and return. |
| 395 | if (has_system && has_cache) { |
| 396 | // We want the cache if the checksum matches, otherwise the system. |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 397 | std::unique_ptr<ImageHeader> system(ReadSpecificImageHeader(system_filename.c_str(), |
| 398 | error_msg)); |
| 399 | std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeader(cache_filename.c_str(), |
| 400 | error_msg)); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 401 | if (system.get() == nullptr || |
| 402 | (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) { |
| 403 | return cache.release(); |
| 404 | } else { |
| 405 | return system.release(); |
| 406 | } |
| 407 | } else if (has_system) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 408 | return ReadSpecificImageHeader(system_filename.c_str(), error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 409 | } else if (has_cache) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 410 | return ReadSpecificImageHeader(cache_filename.c_str(), error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 411 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 412 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 415 | *error_msg = StringPrintf("Unable to find image file for %s", image_location); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 416 | return nullptr; |
| 417 | } |
| 418 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 419 | static bool ChecksumsMatch(const char* image_a, const char* image_b) { |
| 420 | ImageHeader hdr_a; |
| 421 | ImageHeader hdr_b; |
| 422 | return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b) |
| 423 | && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum(); |
| 424 | } |
| 425 | |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 426 | static bool ImageCreationAllowed(bool is_global_cache, std::string* error_msg) { |
| 427 | // Anyone can write into a "local" cache. |
| 428 | if (!is_global_cache) { |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | // Only the zygote is allowed to create the global boot image. |
| 433 | if (Runtime::Current()->IsZygote()) { |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | *error_msg = "Only the zygote can create the global boot image."; |
| 438 | return false; |
| 439 | } |
| 440 | |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 441 | static constexpr uint64_t kLowSpaceValue = 50 * MB; |
| 442 | static constexpr uint64_t kTmpFsSentinelValue = 384 * MB; |
| 443 | |
| 444 | // Read the free space of the cache partition and make a decision whether to keep the generated |
| 445 | // image. This is to try to mitigate situations where the system might run out of space later. |
| 446 | static bool CheckSpace(const std::string& cache_filename, std::string* error_msg) { |
| 447 | // Using statvfs vs statvfs64 because of b/18207376, and it is enough for all practical purposes. |
| 448 | struct statvfs buf; |
| 449 | |
| 450 | int res = TEMP_FAILURE_RETRY(statvfs(cache_filename.c_str(), &buf)); |
| 451 | if (res != 0) { |
| 452 | // Could not stat. Conservatively tell the system to delete the image. |
| 453 | *error_msg = "Could not stat the filesystem, assuming low-memory situation."; |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | uint64_t fs_overall_size = buf.f_bsize * static_cast<uint64_t>(buf.f_blocks); |
| 458 | // Zygote is privileged, but other things are not. Use bavail. |
| 459 | uint64_t fs_free_size = buf.f_bsize * static_cast<uint64_t>(buf.f_bavail); |
| 460 | |
| 461 | // Take the overall size as an indicator for a tmpfs, which is being used for the decryption |
| 462 | // environment. We do not want to fail quickening the boot image there, as it is beneficial |
| 463 | // for time-to-UI. |
| 464 | if (fs_overall_size > kTmpFsSentinelValue) { |
| 465 | if (fs_free_size < kLowSpaceValue) { |
| 466 | *error_msg = StringPrintf("Low-memory situation: only %4.2f megabytes available after image" |
| 467 | " generation, need at least %" PRIu64 ".", |
| 468 | static_cast<double>(fs_free_size) / MB, |
| 469 | kLowSpaceValue / MB); |
| 470 | return false; |
| 471 | } |
| 472 | } |
| 473 | return true; |
| 474 | } |
| 475 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 476 | ImageSpace* ImageSpace::Create(const char* image_location, |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 477 | const InstructionSet image_isa, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 478 | bool secondary_image, |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 479 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 480 | std::string system_filename; |
| 481 | bool has_system = false; |
| 482 | std::string cache_filename; |
| 483 | bool has_cache = false; |
| 484 | bool dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 485 | bool is_global_cache = true; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 486 | const bool found_image = FindImageFilename(image_location, image_isa, &system_filename, |
| 487 | &has_system, &cache_filename, &dalvik_cache_exists, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 488 | &has_cache, &is_global_cache); |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 489 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 490 | if (Runtime::Current()->IsZygote() && !secondary_image) { |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 491 | MarkZygoteStart(image_isa, Runtime::Current()->GetZygoteMaxFailedBoots()); |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 494 | ImageSpace* space; |
| 495 | bool relocate = Runtime::Current()->ShouldRelocate(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 496 | bool can_compile = Runtime::Current()->IsImageDex2OatEnabled(); |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 497 | if (found_image) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 498 | const std::string* image_filename; |
| 499 | bool is_system = false; |
| 500 | bool relocated_version_used = false; |
| 501 | if (relocate) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 502 | if (!dalvik_cache_exists) { |
| 503 | *error_msg = StringPrintf("Requiring relocation for image '%s' at '%s' but we do not have " |
| 504 | "any dalvik_cache to find/place it in.", |
| 505 | image_location, system_filename.c_str()); |
| 506 | return nullptr; |
| 507 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 508 | if (has_system) { |
| 509 | if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) { |
| 510 | // We already have a relocated version |
| 511 | image_filename = &cache_filename; |
| 512 | relocated_version_used = true; |
| 513 | } else { |
| 514 | // We cannot have a relocated version, Relocate the system one and use it. |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 515 | |
| 516 | std::string reason; |
| 517 | bool success; |
| 518 | |
| 519 | // Check whether we are allowed to relocate. |
| 520 | if (!can_compile) { |
| 521 | reason = "Image dex2oat disabled by -Xnoimage-dex2oat."; |
| 522 | success = false; |
| 523 | } else if (!ImageCreationAllowed(is_global_cache, &reason)) { |
| 524 | // Whether we can write to the cache. |
| 525 | success = false; |
| 526 | } else { |
| 527 | // Try to relocate. |
| 528 | success = RelocateImage(image_location, cache_filename.c_str(), image_isa, &reason); |
| 529 | } |
| 530 | |
| 531 | if (success) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 532 | relocated_version_used = true; |
| 533 | image_filename = &cache_filename; |
| 534 | } else { |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 535 | *error_msg = StringPrintf("Unable to relocate image '%s' from '%s' to '%s': %s", |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 536 | image_location, system_filename.c_str(), |
| 537 | cache_filename.c_str(), reason.c_str()); |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 538 | // We failed to create files, remove any possibly garbage output. |
| 539 | // Since ImageCreationAllowed was true above, we are the zygote |
| 540 | // and therefore the only process expected to generate these for |
| 541 | // the device. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 542 | PruneDalvikCache(image_isa); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 543 | return nullptr; |
| 544 | } |
| 545 | } |
| 546 | } else { |
| 547 | CHECK(has_cache); |
| 548 | // We can just use cache's since it should be fine. This might or might not be relocated. |
| 549 | image_filename = &cache_filename; |
| 550 | } |
| 551 | } else { |
| 552 | if (has_system && has_cache) { |
| 553 | // Check they have the same cksum. If they do use the cache. Otherwise system. |
| 554 | if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) { |
| 555 | image_filename = &cache_filename; |
| 556 | relocated_version_used = true; |
| 557 | } else { |
| 558 | image_filename = &system_filename; |
Alex Light | 1a76213 | 2014-07-31 09:32:13 -0700 | [diff] [blame] | 559 | is_system = true; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 560 | } |
| 561 | } else if (has_system) { |
| 562 | image_filename = &system_filename; |
Alex Light | 1a76213 | 2014-07-31 09:32:13 -0700 | [diff] [blame] | 563 | is_system = true; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 564 | } else { |
| 565 | CHECK(has_cache); |
| 566 | image_filename = &cache_filename; |
| 567 | } |
| 568 | } |
| 569 | { |
| 570 | // Note that we must not use the file descriptor associated with |
| 571 | // ScopedFlock::GetFile to Init the image file. We want the file |
| 572 | // descriptor (and the associated exclusive lock) to be released when |
| 573 | // we leave Create. |
| 574 | ScopedFlock image_lock; |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 575 | image_lock.Init(image_filename->c_str(), error_msg); |
Alex Light | b6cabc1 | 2014-08-21 09:45:00 -0700 | [diff] [blame] | 576 | VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location " |
| 577 | << image_location; |
Alex Light | b93637a | 2014-07-31 10:48:46 -0700 | [diff] [blame] | 578 | // If we are in /system we can assume the image is good. We can also |
| 579 | // assume this if we are using a relocated image (i.e. image checksum |
| 580 | // matches) since this is only different by the offset. We need this to |
| 581 | // make sure that host tests continue to work. |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 582 | space = ImageSpace::Init(image_filename->c_str(), image_location, |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 583 | !(is_system || relocated_version_used), error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 584 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 585 | if (space != nullptr) { |
| 586 | return space; |
| 587 | } |
| 588 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 589 | if (relocated_version_used) { |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 590 | // Something is wrong with the relocated copy (even though checksums match). Cleanup. |
| 591 | // This can happen if the .oat is corrupt, since the above only checks the .art checksums. |
| 592 | // TODO: Check the oat file validity earlier. |
| 593 | *error_msg = StringPrintf("Attempted to use relocated version of %s at %s generated from %s " |
| 594 | "but image failed to load: %s", |
| 595 | image_location, cache_filename.c_str(), system_filename.c_str(), |
| 596 | error_msg->c_str()); |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 597 | PruneDalvikCache(image_isa); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 598 | return nullptr; |
| 599 | } else if (is_system) { |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 600 | // If the /system file exists, it should be up-to-date, don't try to generate it. |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 601 | *error_msg = StringPrintf("Failed to load /system image '%s': %s", |
| 602 | image_filename->c_str(), error_msg->c_str()); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 603 | return nullptr; |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 604 | } else { |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 605 | // Otherwise, log a warning and fall through to GenerateImage. |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 606 | LOG(WARNING) << *error_msg; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 609 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 610 | if (!can_compile) { |
| 611 | *error_msg = "Not attempting to compile image because -Xnoimage-dex2oat"; |
| 612 | return nullptr; |
| 613 | } else if (!dalvik_cache_exists) { |
| 614 | *error_msg = StringPrintf("No place to put generated image."); |
| 615 | return nullptr; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 616 | } else if (!ImageCreationAllowed(is_global_cache, error_msg)) { |
| 617 | return nullptr; |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 618 | } else if (!GenerateImage(cache_filename, image_isa, error_msg)) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 619 | *error_msg = StringPrintf("Failed to generate image '%s': %s", |
| 620 | cache_filename.c_str(), error_msg->c_str()); |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 621 | // We failed to create files, remove any possibly garbage output. |
| 622 | // Since ImageCreationAllowed was true above, we are the zygote |
| 623 | // and therefore the only process expected to generate these for |
| 624 | // the device. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 625 | PruneDalvikCache(image_isa); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 626 | return nullptr; |
| 627 | } else { |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 628 | // Check whether there is enough space left over after we have generated the image. |
| 629 | if (!CheckSpace(cache_filename, error_msg)) { |
| 630 | // No. Delete the generated image and try to run out of the dex files. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 631 | PruneDalvikCache(image_isa); |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 632 | return nullptr; |
| 633 | } |
| 634 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 635 | // Note that we must not use the file descriptor associated with |
| 636 | // ScopedFlock::GetFile to Init the image file. We want the file |
| 637 | // descriptor (and the associated exclusive lock) to be released when |
| 638 | // we leave Create. |
| 639 | ScopedFlock image_lock; |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 640 | image_lock.Init(cache_filename.c_str(), error_msg); |
| 641 | space = ImageSpace::Init(cache_filename.c_str(), image_location, true, error_msg); |
| 642 | if (space == nullptr) { |
| 643 | *error_msg = StringPrintf("Failed to load generated image '%s': %s", |
| 644 | cache_filename.c_str(), error_msg->c_str()); |
| 645 | } |
| 646 | return space; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 647 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 650 | void ImageSpace::VerifyImageAllocations() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 651 | uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 652 | while (current < End()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 653 | CHECK_ALIGNED(current, kObjectAlignment); |
| 654 | auto* obj = reinterpret_cast<mirror::Object*>(current); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 655 | CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class"; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 656 | CHECK(live_bitmap_->Test(obj)) << PrettyTypeOf(obj); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 657 | if (kUseBakerOrBrooksReadBarrier) { |
| 658 | obj->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 659 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 660 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
| 661 | } |
| 662 | } |
| 663 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 664 | ImageSpace* ImageSpace::Init(const char* image_filename, const char* image_location, |
| 665 | bool validate_oat_file, std::string* error_msg) { |
| 666 | CHECK(image_filename != nullptr); |
| 667 | CHECK(image_location != nullptr); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 668 | |
| 669 | uint64_t start_time = 0; |
| 670 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 671 | start_time = NanoTime(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 672 | LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 675 | std::unique_ptr<File> file(OS::OpenFileForReading(image_filename)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 676 | if (file.get() == nullptr) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 677 | *error_msg = StringPrintf("Failed to open '%s'", image_filename); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 678 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 679 | } |
| 680 | ImageHeader image_header; |
| 681 | bool success = file->ReadFully(&image_header, sizeof(image_header)); |
| 682 | if (!success || !image_header.IsValid()) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 683 | *error_msg = StringPrintf("Invalid image header in '%s'", image_filename); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 684 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 685 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 686 | // Check that the file is larger or equal to the header size + data size. |
| 687 | const uint64_t image_file_size = static_cast<uint64_t>(file->GetLength()); |
| 688 | if (image_file_size < sizeof(ImageHeader) + image_header.GetDataSize()) { |
| 689 | *error_msg = StringPrintf("Image file truncated: %" PRIu64 " vs. %" PRIu64 ".", |
| 690 | image_file_size, |
| 691 | image_header.GetDataSize()); |
Andreas Gampe | 6c8b49f | 2015-02-19 11:42:36 -0800 | [diff] [blame] | 692 | return nullptr; |
| 693 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 694 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 695 | if (VLOG_IS_ON(startup)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 696 | LOG(INFO) << "Dumping image sections"; |
| 697 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 698 | const auto section_idx = static_cast<ImageHeader::ImageSections>(i); |
| 699 | auto& section = image_header.GetImageSection(section_idx); |
| 700 | LOG(INFO) << section_idx << " start=" |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 701 | << reinterpret_cast<void*>(image_header.GetImageBegin() + section.Offset()) << " " |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 702 | << section; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | const auto& bitmap_section = image_header.GetImageSection(ImageHeader::kSectionImageBitmap); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 707 | // The location we want to map from is the first aligned page after the end of the stored |
| 708 | // (possibly compressed) data. |
| 709 | const size_t image_bitmap_offset = RoundUp(sizeof(image_header) + image_header.GetDataSize(), |
| 710 | kPageSize); |
| 711 | const size_t end_of_bitmap = image_bitmap_offset + bitmap_section.Size(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 712 | if (end_of_bitmap != image_file_size) { |
| 713 | *error_msg = StringPrintf( |
| 714 | "Image file size does not equal end of bitmap: size=%" PRIu64 " vs. %zu.", image_file_size, |
| 715 | end_of_bitmap); |
Andreas Gampe | 6c8b49f | 2015-02-19 11:42:36 -0800 | [diff] [blame] | 716 | return nullptr; |
| 717 | } |
| 718 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 719 | // Note: The image header is part of the image due to mmap page alignment required of offset. |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 720 | std::unique_ptr<MemMap> map; |
| 721 | if (image_header.GetStorageMode() == ImageHeader::kStorageModeUncompressed) { |
| 722 | map.reset(MemMap::MapFileAtAddress(image_header.GetImageBegin(), |
| 723 | image_header.GetImageSize(), |
| 724 | PROT_READ | PROT_WRITE, |
| 725 | MAP_PRIVATE, |
| 726 | file->Fd(), |
| 727 | 0, |
| 728 | /*low_4gb*/false, |
| 729 | /*reuse*/false, |
| 730 | image_filename, |
| 731 | error_msg)); |
| 732 | } else { |
| 733 | // Reserve output and decompress into it. |
| 734 | map.reset(MemMap::MapAnonymous(image_location, |
| 735 | image_header.GetImageBegin(), |
| 736 | image_header.GetImageSize(), |
| 737 | PROT_READ | PROT_WRITE, |
| 738 | /*low_4gb*/false, |
| 739 | /*reuse*/false, |
| 740 | error_msg)); |
| 741 | if (map != nullptr) { |
| 742 | const size_t stored_size = image_header.GetDataSize(); |
| 743 | const size_t write_offset = sizeof(image_header); // Skip the header. |
| 744 | std::unique_ptr<MemMap> temp_map(MemMap::MapFile(sizeof(ImageHeader) + stored_size, |
| 745 | PROT_READ, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 746 | MAP_PRIVATE, |
| 747 | file->Fd(), |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 748 | /*offset*/0, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 749 | /*low_4gb*/false, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 750 | image_filename, |
| 751 | error_msg)); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 752 | if (temp_map == nullptr) { |
| 753 | DCHECK(!error_msg->empty()); |
| 754 | return nullptr; |
| 755 | } |
| 756 | memcpy(map->Begin(), &image_header, sizeof(image_header)); |
| 757 | const uint64_t start = NanoTime(); |
| 758 | const size_t decompressed_size = LZ4_decompress_safe( |
| 759 | reinterpret_cast<char*>(temp_map->Begin()) + sizeof(ImageHeader), |
| 760 | reinterpret_cast<char*>(map->Begin()) + write_offset, |
| 761 | stored_size, |
| 762 | map->Size()); |
| 763 | // TODO: VLOG(image) |
| 764 | VLOG(class_linker) << "Decompressing image took " << PrettyDuration(NanoTime() - start); |
| 765 | if (decompressed_size + sizeof(ImageHeader) != image_header.GetImageSize()) { |
| 766 | *error_msg = StringPrintf("Decompressed size does not match expected image size %zu vs %zu", |
| 767 | decompressed_size + sizeof(ImageHeader), |
| 768 | image_header.GetImageSize()); |
| 769 | return nullptr; |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 774 | if (map == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 775 | DCHECK(!error_msg->empty()); |
| 776 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 777 | } |
| 778 | CHECK_EQ(image_header.GetImageBegin(), map->Begin()); |
| 779 | DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader))); |
| 780 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 781 | std::unique_ptr<MemMap> image_bitmap_map(MemMap::MapFileAtAddress(nullptr, |
| 782 | bitmap_section.Size(), |
| 783 | PROT_READ, MAP_PRIVATE, |
| 784 | file->Fd(), |
| 785 | image_bitmap_offset, |
| 786 | /*low_4gb*/false, |
| 787 | /*reuse*/false, |
| 788 | image_filename, |
| 789 | error_msg)); |
| 790 | if (image_bitmap_map == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 791 | *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str()); |
| 792 | return nullptr; |
| 793 | } |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 794 | uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 795 | std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 796 | bitmap_index)); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 797 | std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap( |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 798 | accounting::ContinuousSpaceBitmap::CreateFromMemMap( |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 799 | bitmap_name, |
| 800 | image_bitmap_map.release(), |
| 801 | reinterpret_cast<uint8_t*>(map->Begin()), |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 802 | accounting::ContinuousSpaceBitmap::ComputeHeapSize(bitmap_section.Size()))); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 803 | if (bitmap == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 804 | *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str()); |
| 805 | return nullptr; |
| 806 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 807 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 808 | // In case of multi-images, the images are spaced apart so that the bitmaps don't overlap. We |
| 809 | // need to reserve the slack, as otherwise the large object space might allocate in there. |
| 810 | // TODO: Reconsider the multi-image layout. b/26317072 |
| 811 | std::unique_ptr<MemMap> shadow_map; |
| 812 | { |
| 813 | uintptr_t image_begin = reinterpret_cast<uintptr_t>(image_header.GetImageBegin()); |
| 814 | uintptr_t image_end = RoundUp(image_begin + image_header.GetImageSize(), kPageSize); |
| 815 | uintptr_t oat_begin = reinterpret_cast<uintptr_t>(image_header.GetOatFileBegin()); |
| 816 | if (image_end < oat_begin) { |
| 817 | // There's a gap. Could be multi-image, could be the oat file spaced apart. Go ahead and |
| 818 | // dummy-reserve the space covered by the bitmap (which will be a shadow that introduces |
| 819 | // a gap to the next image). |
| 820 | uintptr_t heap_size = bitmap->HeapSize(); |
| 821 | uintptr_t bitmap_coverage_end = RoundUp(image_begin + heap_size, kPageSize); |
| 822 | if (bitmap_coverage_end > image_end) { |
| 823 | VLOG(startup) << "Reserving bitmap shadow [" |
| 824 | << std::hex << image_end << ";" |
| 825 | << std::hex << bitmap_coverage_end << ";] (oat file begins at " |
| 826 | << std::hex << oat_begin; |
| 827 | // Note: we cannot use MemMap::Dummy here, as that won't reserve the space in 32-bit mode. |
| 828 | shadow_map.reset(MemMap::MapAnonymous("Image bitmap shadow", |
| 829 | reinterpret_cast<uint8_t*>(image_end), |
| 830 | bitmap_coverage_end - image_end, |
| 831 | PROT_NONE, |
| 832 | false, |
| 833 | false, |
| 834 | error_msg)); |
| 835 | if (shadow_map == nullptr) { |
| 836 | return nullptr; |
| 837 | } |
| 838 | // madvise it away, we don't really want it, just reserve the address space. |
| 839 | // TODO: Should we use MadviseDontNeedAndZero? b/26317072 |
| 840 | madvise(shadow_map->BaseBegin(), shadow_map->BaseSize(), MADV_DONTNEED); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 845 | // We only want the mirror object, not the ArtFields and ArtMethods. |
| 846 | uint8_t* const image_end = |
| 847 | map->Begin() + image_header.GetImageSection(ImageHeader::kSectionObjects).End(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 848 | std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, |
| 849 | image_location, |
| 850 | map.release(), |
| 851 | bitmap.release(), |
| 852 | image_end, |
| 853 | shadow_map.release())); |
Hiroshi Yamauchi | bd0fb61 | 2014-05-20 13:46:00 -0700 | [diff] [blame] | 854 | |
| 855 | // VerifyImageAllocations() will be called later in Runtime::Init() |
| 856 | // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_ |
| 857 | // and ArtField::java_lang_reflect_ArtField_, which are used from |
| 858 | // Object::SizeOf() which VerifyImageAllocations() calls, are not |
| 859 | // set yet at this point. |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 860 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 861 | space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 862 | if (space->oat_file_.get() == nullptr) { |
| 863 | DCHECK(!error_msg->empty()); |
| 864 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 865 | } |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 866 | space->oat_file_non_owned_ = space->oat_file_.get(); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 867 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 868 | if (validate_oat_file && !space->ValidateOatFile(error_msg)) { |
| 869 | DCHECK(!error_msg->empty()); |
| 870 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 873 | Runtime* runtime = Runtime::Current(); |
| 874 | runtime->SetInstructionSet(space->oat_file_->GetOatHeader().GetInstructionSet()); |
| 875 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 876 | if (!runtime->HasResolutionMethod()) { |
| 877 | runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod)); |
| 878 | runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod)); |
| 879 | runtime->SetImtUnimplementedMethod( |
| 880 | image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod)); |
| 881 | runtime->SetCalleeSaveMethod( |
| 882 | image_header.GetImageMethod(ImageHeader::kCalleeSaveMethod), Runtime::kSaveAll); |
| 883 | runtime->SetCalleeSaveMethod( |
| 884 | image_header.GetImageMethod(ImageHeader::kRefsOnlySaveMethod), Runtime::kRefsOnly); |
| 885 | runtime->SetCalleeSaveMethod( |
| 886 | image_header.GetImageMethod(ImageHeader::kRefsAndArgsSaveMethod), Runtime::kRefsAndArgs); |
| 887 | } |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 888 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 889 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
| 890 | LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time) |
| 891 | << ") " << *space.get(); |
| 892 | } |
| 893 | return space.release(); |
| 894 | } |
| 895 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 896 | OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 897 | const ImageHeader& image_header = GetImageHeader(); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 898 | std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path); |
| 899 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 900 | CHECK(image_header.GetOatDataBegin() != nullptr); |
| 901 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 902 | OatFile* oat_file = OatFile::Open(oat_filename, |
| 903 | oat_filename, |
| 904 | image_header.GetOatDataBegin(), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 905 | image_header.GetOatFileBegin(), |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 906 | !Runtime::Current()->IsAotCompiler(), |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 907 | nullptr, |
| 908 | error_msg); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 909 | if (oat_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 910 | *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s", |
| 911 | oat_filename.c_str(), GetName(), error_msg->c_str()); |
| 912 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 913 | } |
| 914 | uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum(); |
| 915 | uint32_t image_oat_checksum = image_header.GetOatChecksum(); |
| 916 | if (oat_checksum != image_oat_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 917 | *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x" |
| 918 | " in image %s", oat_checksum, image_oat_checksum, GetName()); |
| 919 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 920 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 921 | int32_t image_patch_delta = image_header.GetPatchDelta(); |
| 922 | int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta(); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 923 | if (oat_patch_delta != image_patch_delta && !image_header.CompilePic()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 924 | // We should have already relocated by this point. Bail out. |
| 925 | *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d " |
| 926 | "in image %s", oat_patch_delta, image_patch_delta, GetName()); |
| 927 | return nullptr; |
| 928 | } |
| 929 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 930 | return oat_file; |
| 931 | } |
| 932 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 933 | bool ImageSpace::ValidateOatFile(std::string* error_msg) const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 934 | CHECK(oat_file_.get() != nullptr); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 935 | for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 936 | const std::string& dex_file_location = oat_dex_file->GetDexFileLocation(); |
| 937 | uint32_t dex_file_location_checksum; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 938 | if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) { |
| 939 | *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: " |
| 940 | "%s", dex_file_location.c_str(), GetName(), error_msg->c_str()); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 941 | return false; |
| 942 | } |
| 943 | if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 944 | *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and " |
| 945 | "dex file '%s' (0x%x != 0x%x)", |
| 946 | oat_file_->GetLocation().c_str(), dex_file_location.c_str(), |
| 947 | oat_dex_file->GetDexFileLocationChecksum(), |
| 948 | dex_file_location_checksum); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 949 | return false; |
| 950 | } |
| 951 | } |
| 952 | return true; |
| 953 | } |
| 954 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 955 | const OatFile* ImageSpace::GetOatFile() const { |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 956 | return oat_file_non_owned_; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 959 | std::unique_ptr<const OatFile> ImageSpace::ReleaseOatFile() { |
| 960 | CHECK(oat_file_ != nullptr); |
| 961 | return std::move(oat_file_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 964 | void ImageSpace::Dump(std::ostream& os) const { |
| 965 | os << GetType() |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 966 | << " begin=" << reinterpret_cast<void*>(Begin()) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 967 | << ",end=" << reinterpret_cast<void*>(End()) |
| 968 | << ",size=" << PrettySize(Size()) |
| 969 | << ",name=\"" << GetName() << "\"]"; |
| 970 | } |
| 971 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 972 | } // namespace space |
| 973 | } // namespace gc |
| 974 | } // namespace art |