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, |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 50 | uint8_t* end) |
| 51 | : MemMapSpace(image_filename, |
| 52 | mem_map, |
| 53 | mem_map->Begin(), |
| 54 | end, |
| 55 | end, |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 56 | kGcRetentionPolicyNeverCollect), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 57 | oat_file_non_owned_(nullptr), |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 58 | image_location_(image_location) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 59 | DCHECK(live_bitmap != nullptr); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 60 | live_bitmap_.reset(live_bitmap); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 63 | static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) { |
| 64 | CHECK_ALIGNED(min_delta, kPageSize); |
| 65 | CHECK_ALIGNED(max_delta, kPageSize); |
| 66 | CHECK_LT(min_delta, max_delta); |
| 67 | |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 68 | int32_t r = GetRandomNumber<int32_t>(min_delta, max_delta); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 69 | if (r % 2 == 0) { |
| 70 | r = RoundUp(r, kPageSize); |
| 71 | } else { |
| 72 | r = RoundDown(r, kPageSize); |
| 73 | } |
| 74 | CHECK_LE(min_delta, r); |
| 75 | CHECK_GE(max_delta, r); |
| 76 | CHECK_ALIGNED(r, kPageSize); |
| 77 | return r; |
| 78 | } |
| 79 | |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 80 | // 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] | 81 | // 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] | 82 | // Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c |
| 83 | // Note this should only be used during first boot. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 84 | static void RealPruneDalvikCache(const std::string& cache_dir_path); |
Andreas Gampe | 8db9dcd | 2014-11-09 18:14:30 -0800 | [diff] [blame] | 85 | |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 86 | static void PruneDalvikCache(InstructionSet isa) { |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 87 | CHECK_NE(isa, kNone); |
Andreas Gampe | 8db9dcd | 2014-11-09 18:14:30 -0800 | [diff] [blame] | 88 | // Prune the base /data/dalvik-cache. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 89 | RealPruneDalvikCache(GetDalvikCacheOrDie(".", false)); |
Andreas Gampe | 8db9dcd | 2014-11-09 18:14:30 -0800 | [diff] [blame] | 90 | // Prune /data/dalvik-cache/<isa>. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 91 | RealPruneDalvikCache(GetDalvikCacheOrDie(GetInstructionSetString(isa), false)); |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 92 | } |
Andreas Gampe | 8db9dcd | 2014-11-09 18:14:30 -0800 | [diff] [blame] | 93 | |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 94 | static void RealPruneDalvikCache(const std::string& cache_dir_path) { |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 95 | if (!OS::DirectoryExists(cache_dir_path.c_str())) { |
| 96 | return; |
| 97 | } |
| 98 | DIR* cache_dir = opendir(cache_dir_path.c_str()); |
| 99 | if (cache_dir == nullptr) { |
| 100 | PLOG(WARNING) << "Unable to open " << cache_dir_path << " to delete it's contents"; |
| 101 | return; |
| 102 | } |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 103 | |
| 104 | for (struct dirent* de = readdir(cache_dir); de != nullptr; de = readdir(cache_dir)) { |
| 105 | const char* name = de->d_name; |
| 106 | if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) { |
| 107 | continue; |
| 108 | } |
Andreas Gampe | 8db9dcd | 2014-11-09 18:14:30 -0800 | [diff] [blame] | 109 | // We only want to delete regular files and symbolic links. |
| 110 | if (de->d_type != DT_REG && de->d_type != DT_LNK) { |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 111 | if (de->d_type != DT_DIR) { |
| 112 | // We do expect some directories (namely the <isa> for pruning the base dalvik-cache). |
| 113 | LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered."; |
| 114 | } |
| 115 | continue; |
| 116 | } |
Brian Carlstrom | debdda0 | 2014-08-28 22:17:13 -0700 | [diff] [blame] | 117 | std::string cache_file(cache_dir_path); |
| 118 | cache_file += '/'; |
| 119 | cache_file += name; |
| 120 | if (TEMP_FAILURE_RETRY(unlink(cache_file.c_str())) != 0) { |
| 121 | PLOG(ERROR) << "Unable to unlink " << cache_file; |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 122 | continue; |
| 123 | } |
| 124 | } |
| 125 | CHECK_EQ(0, TEMP_FAILURE_RETRY(closedir(cache_dir))) << "Unable to close directory."; |
| 126 | } |
| 127 | |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 128 | // We write out an empty file to the zygote's ISA specific cache dir at the start of |
| 129 | // every zygote boot and delete it when the boot completes. If we find a file already |
| 130 | // present, it usually means the boot didn't complete. We wipe the entire dalvik |
| 131 | // cache if that's the case. |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 132 | static void MarkZygoteStart(const InstructionSet isa, const uint32_t max_failed_boots) { |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 133 | const std::string isa_subdir = GetDalvikCacheOrDie(GetInstructionSetString(isa), false); |
| 134 | const std::string boot_marker = isa_subdir + "/.booting"; |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 135 | const char* file_name = boot_marker.c_str(); |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 136 | |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 137 | uint32_t num_failed_boots = 0; |
| 138 | std::unique_ptr<File> file(OS::OpenFileReadWrite(file_name)); |
| 139 | if (file.get() == nullptr) { |
| 140 | file.reset(OS::CreateEmptyFile(file_name)); |
| 141 | |
| 142 | if (file.get() == nullptr) { |
| 143 | PLOG(WARNING) << "Failed to create boot marker."; |
| 144 | return; |
| 145 | } |
| 146 | } else { |
| 147 | if (!file->ReadFully(&num_failed_boots, sizeof(num_failed_boots))) { |
| 148 | PLOG(WARNING) << "Failed to read boot marker."; |
| 149 | file->Erase(); |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (max_failed_boots != 0 && num_failed_boots > max_failed_boots) { |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 155 | LOG(WARNING) << "Incomplete boot detected. Pruning dalvik cache"; |
| 156 | RealPruneDalvikCache(isa_subdir); |
| 157 | } |
| 158 | |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 159 | ++num_failed_boots; |
| 160 | VLOG(startup) << "Number of failed boots on : " << boot_marker << " = " << num_failed_boots; |
| 161 | |
| 162 | if (lseek(file->Fd(), 0, SEEK_SET) == -1) { |
| 163 | PLOG(WARNING) << "Failed to write boot marker."; |
| 164 | file->Erase(); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | if (!file->WriteFully(&num_failed_boots, sizeof(num_failed_boots))) { |
| 169 | PLOG(WARNING) << "Failed to write boot marker."; |
| 170 | file->Erase(); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | if (file->FlushCloseOrErase() != 0) { |
| 175 | PLOG(WARNING) << "Failed to flush boot marker."; |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 179 | static bool GenerateImage(const std::string& image_filename, InstructionSet image_isa, |
| 180 | std::string* error_msg) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 181 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
| 182 | std::vector<std::string> boot_class_path; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 183 | Split(boot_class_path_string, ':', &boot_class_path); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 184 | if (boot_class_path.empty()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 185 | *error_msg = "Failed to generate image because no boot class path specified"; |
| 186 | return false; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 187 | } |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 188 | // We should clean up so we are more likely to have room for the image. |
| 189 | if (Runtime::Current()->IsZygote()) { |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 190 | 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] | 191 | PruneDalvikCache(image_isa); |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 192 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 193 | |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 194 | std::vector<std::string> arg_vector; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 195 | |
Tsu Chiang Chuang | 12e6d74 | 2014-05-22 10:22:25 -0700 | [diff] [blame] | 196 | std::string dex2oat(Runtime::Current()->GetCompilerExecutable()); |
Mathieu Chartier | 08d7d44 | 2013-07-31 18:08:51 -0700 | [diff] [blame] | 197 | arg_vector.push_back(dex2oat); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 198 | |
| 199 | std::string image_option_string("--image="); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 200 | image_option_string += image_filename; |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 201 | arg_vector.push_back(image_option_string); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 202 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 203 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 204 | arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | std::string oat_file_option_string("--oat-file="); |
Brian Carlstrom | 2f1e15c | 2014-10-27 16:27:06 -0700 | [diff] [blame] | 208 | oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename); |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 209 | arg_vector.push_back(oat_file_option_string); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 210 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 211 | // Note: we do not generate a fully debuggable boot image so we do not pass the |
| 212 | // compiler flag --debuggable here. |
| 213 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 214 | Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector); |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 215 | CHECK_EQ(image_isa, kRuntimeISA) |
| 216 | << "We should always be generating an image for the current isa."; |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 217 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 218 | int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, |
| 219 | ART_BASE_ADDRESS_MAX_DELTA); |
| 220 | LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default " |
| 221 | << "art base address of 0x" << std::hex << ART_BASE_ADDRESS; |
| 222 | 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] | 223 | |
Brian Carlstrom | 57309db | 2014-07-30 15:13:25 -0700 | [diff] [blame] | 224 | if (!kIsTargetBuild) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 225 | arg_vector.push_back("--host"); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 228 | const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 229 | for (size_t i = 0; i < compiler_options.size(); ++i) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 230 | arg_vector.push_back(compiler_options[i].c_str()); |
| 231 | } |
| 232 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 233 | std::string command_line(Join(arg_vector, ' ')); |
| 234 | LOG(INFO) << "GenerateImage: " << command_line; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 235 | return Exec(arg_vector, error_msg); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 238 | bool ImageSpace::FindImageFilename(const char* image_location, |
| 239 | const InstructionSet image_isa, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 240 | std::string* system_filename, |
| 241 | bool* has_system, |
| 242 | std::string* cache_filename, |
| 243 | bool* dalvik_cache_exists, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 244 | bool* has_cache, |
| 245 | bool* is_global_cache) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 246 | *has_system = false; |
| 247 | *has_cache = false; |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 248 | // image_location = /system/framework/boot.art |
| 249 | // system_image_location = /system/framework/<image_isa>/boot.art |
| 250 | std::string system_image_filename(GetSystemImageFilename(image_location, image_isa)); |
| 251 | if (OS::FileExists(system_image_filename.c_str())) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 252 | *system_filename = system_image_filename; |
| 253 | *has_system = true; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 254 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 255 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 256 | bool have_android_data = false; |
| 257 | *dalvik_cache_exists = false; |
| 258 | std::string dalvik_cache; |
| 259 | GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 260 | &have_android_data, dalvik_cache_exists, is_global_cache); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 261 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 262 | if (have_android_data && *dalvik_cache_exists) { |
| 263 | // Always set output location even if it does not exist, |
| 264 | // so that the caller knows where to create the image. |
| 265 | // |
| 266 | // image_location = /system/framework/boot.art |
| 267 | // *image_filename = /data/dalvik-cache/<image_isa>/boot.art |
| 268 | std::string error_msg; |
| 269 | if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) { |
| 270 | LOG(WARNING) << error_msg; |
| 271 | return *has_system; |
| 272 | } |
| 273 | *has_cache = OS::FileExists(cache_filename->c_str()); |
| 274 | } |
| 275 | return *has_system || *has_cache; |
| 276 | } |
| 277 | |
| 278 | static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) { |
| 279 | std::unique_ptr<File> image_file(OS::OpenFileForReading(filename)); |
| 280 | if (image_file.get() == nullptr) { |
| 281 | return false; |
| 282 | } |
| 283 | const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader)); |
| 284 | if (!success || !image_header->IsValid()) { |
| 285 | return false; |
| 286 | } |
| 287 | return true; |
| 288 | } |
| 289 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 290 | // Relocate the image at image_location to dest_filename and relocate it by a random amount. |
| 291 | static bool RelocateImage(const char* image_location, const char* dest_filename, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 292 | InstructionSet isa, std::string* error_msg) { |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 293 | // We should clean up so we are more likely to have room for the image. |
| 294 | if (Runtime::Current()->IsZygote()) { |
| 295 | 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] | 296 | PruneDalvikCache(isa); |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 299 | std::string patchoat(Runtime::Current()->GetPatchoatExecutable()); |
| 300 | |
| 301 | std::string input_image_location_arg("--input-image-location="); |
| 302 | input_image_location_arg += image_location; |
| 303 | |
| 304 | std::string output_image_filename_arg("--output-image-file="); |
| 305 | output_image_filename_arg += dest_filename; |
| 306 | |
| 307 | std::string input_oat_location_arg("--input-oat-location="); |
| 308 | input_oat_location_arg += ImageHeader::GetOatLocationFromImageLocation(image_location); |
| 309 | |
| 310 | std::string output_oat_filename_arg("--output-oat-file="); |
| 311 | output_oat_filename_arg += ImageHeader::GetOatLocationFromImageLocation(dest_filename); |
| 312 | |
| 313 | std::string instruction_set_arg("--instruction-set="); |
| 314 | instruction_set_arg += GetInstructionSetString(isa); |
| 315 | |
| 316 | std::string base_offset_arg("--base-offset-delta="); |
| 317 | StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, |
| 318 | ART_BASE_ADDRESS_MAX_DELTA)); |
| 319 | |
| 320 | std::vector<std::string> argv; |
| 321 | argv.push_back(patchoat); |
| 322 | |
| 323 | argv.push_back(input_image_location_arg); |
| 324 | argv.push_back(output_image_filename_arg); |
| 325 | |
| 326 | argv.push_back(input_oat_location_arg); |
| 327 | argv.push_back(output_oat_filename_arg); |
| 328 | |
| 329 | argv.push_back(instruction_set_arg); |
| 330 | argv.push_back(base_offset_arg); |
| 331 | |
| 332 | std::string command_line(Join(argv, ' ')); |
| 333 | LOG(INFO) << "RelocateImage: " << command_line; |
| 334 | return Exec(argv, error_msg); |
| 335 | } |
| 336 | |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 337 | static ImageHeader* ReadSpecificImageHeader(const char* filename, std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 338 | std::unique_ptr<ImageHeader> hdr(new ImageHeader); |
| 339 | if (!ReadSpecificImageHeader(filename, hdr.get())) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 340 | *error_msg = StringPrintf("Unable to read image header for %s", filename); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 341 | return nullptr; |
| 342 | } |
| 343 | return hdr.release(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location, |
| 347 | const InstructionSet image_isa) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 348 | std::string error_msg; |
| 349 | ImageHeader* image_header = ReadImageHeader(image_location, image_isa, &error_msg); |
| 350 | if (image_header == nullptr) { |
| 351 | LOG(FATAL) << error_msg; |
| 352 | } |
| 353 | return image_header; |
| 354 | } |
| 355 | |
| 356 | ImageHeader* ImageSpace::ReadImageHeader(const char* image_location, |
| 357 | const InstructionSet image_isa, |
| 358 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 359 | std::string system_filename; |
| 360 | bool has_system = false; |
| 361 | std::string cache_filename; |
| 362 | bool has_cache = false; |
| 363 | bool dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 364 | bool is_global_cache = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 365 | if (FindImageFilename(image_location, image_isa, &system_filename, &has_system, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 366 | &cache_filename, &dalvik_cache_exists, &has_cache, &is_global_cache)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 367 | if (Runtime::Current()->ShouldRelocate()) { |
| 368 | if (has_system && has_cache) { |
| 369 | std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader); |
| 370 | std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader); |
| 371 | if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 372 | *error_msg = StringPrintf("Unable to read image header for %s at %s", |
| 373 | image_location, system_filename.c_str()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 374 | return nullptr; |
| 375 | } |
| 376 | if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 377 | *error_msg = StringPrintf("Unable to read image header for %s at %s", |
| 378 | image_location, cache_filename.c_str()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 379 | return nullptr; |
| 380 | } |
| 381 | if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 382 | *error_msg = StringPrintf("Unable to find a relocated version of image file %s", |
| 383 | image_location); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 384 | return nullptr; |
| 385 | } |
| 386 | return cache_hdr.release(); |
| 387 | } else if (!has_cache) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 388 | *error_msg = StringPrintf("Unable to find a relocated version of image file %s", |
| 389 | image_location); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 390 | return nullptr; |
| 391 | } else if (!has_system && has_cache) { |
| 392 | // This can probably just use the cache one. |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 393 | return ReadSpecificImageHeader(cache_filename.c_str(), error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 394 | } |
| 395 | } else { |
| 396 | // We don't want to relocate, Just pick the appropriate one if we have it and return. |
| 397 | if (has_system && has_cache) { |
| 398 | // We want the cache if the checksum matches, otherwise the system. |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 399 | std::unique_ptr<ImageHeader> system(ReadSpecificImageHeader(system_filename.c_str(), |
| 400 | error_msg)); |
| 401 | std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeader(cache_filename.c_str(), |
| 402 | error_msg)); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 403 | if (system.get() == nullptr || |
| 404 | (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) { |
| 405 | return cache.release(); |
| 406 | } else { |
| 407 | return system.release(); |
| 408 | } |
| 409 | } else if (has_system) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 410 | return ReadSpecificImageHeader(system_filename.c_str(), error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 411 | } else if (has_cache) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 412 | return ReadSpecificImageHeader(cache_filename.c_str(), error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 413 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 414 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 415 | } |
| 416 | |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 417 | *error_msg = StringPrintf("Unable to find image file for %s", image_location); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 418 | return nullptr; |
| 419 | } |
| 420 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 421 | static bool ChecksumsMatch(const char* image_a, const char* image_b) { |
| 422 | ImageHeader hdr_a; |
| 423 | ImageHeader hdr_b; |
| 424 | return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b) |
| 425 | && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum(); |
| 426 | } |
| 427 | |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 428 | static bool ImageCreationAllowed(bool is_global_cache, std::string* error_msg) { |
| 429 | // Anyone can write into a "local" cache. |
| 430 | if (!is_global_cache) { |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | // Only the zygote is allowed to create the global boot image. |
| 435 | if (Runtime::Current()->IsZygote()) { |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | *error_msg = "Only the zygote can create the global boot image."; |
| 440 | return false; |
| 441 | } |
| 442 | |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 443 | static constexpr uint64_t kLowSpaceValue = 50 * MB; |
| 444 | static constexpr uint64_t kTmpFsSentinelValue = 384 * MB; |
| 445 | |
| 446 | // Read the free space of the cache partition and make a decision whether to keep the generated |
| 447 | // image. This is to try to mitigate situations where the system might run out of space later. |
| 448 | static bool CheckSpace(const std::string& cache_filename, std::string* error_msg) { |
| 449 | // Using statvfs vs statvfs64 because of b/18207376, and it is enough for all practical purposes. |
| 450 | struct statvfs buf; |
| 451 | |
| 452 | int res = TEMP_FAILURE_RETRY(statvfs(cache_filename.c_str(), &buf)); |
| 453 | if (res != 0) { |
| 454 | // Could not stat. Conservatively tell the system to delete the image. |
| 455 | *error_msg = "Could not stat the filesystem, assuming low-memory situation."; |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | uint64_t fs_overall_size = buf.f_bsize * static_cast<uint64_t>(buf.f_blocks); |
| 460 | // Zygote is privileged, but other things are not. Use bavail. |
| 461 | uint64_t fs_free_size = buf.f_bsize * static_cast<uint64_t>(buf.f_bavail); |
| 462 | |
| 463 | // Take the overall size as an indicator for a tmpfs, which is being used for the decryption |
| 464 | // environment. We do not want to fail quickening the boot image there, as it is beneficial |
| 465 | // for time-to-UI. |
| 466 | if (fs_overall_size > kTmpFsSentinelValue) { |
| 467 | if (fs_free_size < kLowSpaceValue) { |
| 468 | *error_msg = StringPrintf("Low-memory situation: only %4.2f megabytes available after image" |
| 469 | " generation, need at least %" PRIu64 ".", |
| 470 | static_cast<double>(fs_free_size) / MB, |
| 471 | kLowSpaceValue / MB); |
| 472 | return false; |
| 473 | } |
| 474 | } |
| 475 | return true; |
| 476 | } |
| 477 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 478 | ImageSpace* ImageSpace::CreateBootImage(const char* image_location, |
| 479 | const InstructionSet image_isa, |
| 480 | bool secondary_image, |
| 481 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 482 | std::string system_filename; |
| 483 | bool has_system = false; |
| 484 | std::string cache_filename; |
| 485 | bool has_cache = false; |
| 486 | bool dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 487 | bool is_global_cache = true; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 488 | const bool found_image = FindImageFilename(image_location, image_isa, &system_filename, |
| 489 | &has_system, &cache_filename, &dalvik_cache_exists, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 490 | &has_cache, &is_global_cache); |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 491 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 492 | if (Runtime::Current()->IsZygote() && !secondary_image) { |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 493 | MarkZygoteStart(image_isa, Runtime::Current()->GetZygoteMaxFailedBoots()); |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 496 | ImageSpace* space; |
| 497 | bool relocate = Runtime::Current()->ShouldRelocate(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 498 | bool can_compile = Runtime::Current()->IsImageDex2OatEnabled(); |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 499 | if (found_image) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 500 | const std::string* image_filename; |
| 501 | bool is_system = false; |
| 502 | bool relocated_version_used = false; |
| 503 | if (relocate) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 504 | if (!dalvik_cache_exists) { |
| 505 | *error_msg = StringPrintf("Requiring relocation for image '%s' at '%s' but we do not have " |
| 506 | "any dalvik_cache to find/place it in.", |
| 507 | image_location, system_filename.c_str()); |
| 508 | return nullptr; |
| 509 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 510 | if (has_system) { |
| 511 | if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) { |
| 512 | // We already have a relocated version |
| 513 | image_filename = &cache_filename; |
| 514 | relocated_version_used = true; |
| 515 | } else { |
| 516 | // 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] | 517 | |
| 518 | std::string reason; |
| 519 | bool success; |
| 520 | |
| 521 | // Check whether we are allowed to relocate. |
| 522 | if (!can_compile) { |
| 523 | reason = "Image dex2oat disabled by -Xnoimage-dex2oat."; |
| 524 | success = false; |
| 525 | } else if (!ImageCreationAllowed(is_global_cache, &reason)) { |
| 526 | // Whether we can write to the cache. |
| 527 | success = false; |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 528 | } else if (secondary_image) { |
| 529 | reason = "Should not have to patch secondary image."; |
| 530 | success = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 531 | } else { |
| 532 | // Try to relocate. |
| 533 | success = RelocateImage(image_location, cache_filename.c_str(), image_isa, &reason); |
| 534 | } |
| 535 | |
| 536 | if (success) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 537 | relocated_version_used = true; |
| 538 | image_filename = &cache_filename; |
| 539 | } else { |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 540 | *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] | 541 | image_location, system_filename.c_str(), |
| 542 | cache_filename.c_str(), reason.c_str()); |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 543 | // We failed to create files, remove any possibly garbage output. |
| 544 | // Since ImageCreationAllowed was true above, we are the zygote |
| 545 | // and therefore the only process expected to generate these for |
| 546 | // the device. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 547 | PruneDalvikCache(image_isa); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 548 | return nullptr; |
| 549 | } |
| 550 | } |
| 551 | } else { |
| 552 | CHECK(has_cache); |
| 553 | // We can just use cache's since it should be fine. This might or might not be relocated. |
| 554 | image_filename = &cache_filename; |
| 555 | } |
| 556 | } else { |
| 557 | if (has_system && has_cache) { |
| 558 | // Check they have the same cksum. If they do use the cache. Otherwise system. |
| 559 | if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) { |
| 560 | image_filename = &cache_filename; |
| 561 | relocated_version_used = true; |
| 562 | } else { |
| 563 | image_filename = &system_filename; |
Alex Light | 1a76213 | 2014-07-31 09:32:13 -0700 | [diff] [blame] | 564 | is_system = true; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 565 | } |
| 566 | } else if (has_system) { |
| 567 | image_filename = &system_filename; |
Alex Light | 1a76213 | 2014-07-31 09:32:13 -0700 | [diff] [blame] | 568 | is_system = true; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 569 | } else { |
| 570 | CHECK(has_cache); |
| 571 | image_filename = &cache_filename; |
| 572 | } |
| 573 | } |
| 574 | { |
| 575 | // Note that we must not use the file descriptor associated with |
| 576 | // ScopedFlock::GetFile to Init the image file. We want the file |
| 577 | // descriptor (and the associated exclusive lock) to be released when |
| 578 | // we leave Create. |
| 579 | ScopedFlock image_lock; |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 580 | image_lock.Init(image_filename->c_str(), error_msg); |
Alex Light | b6cabc1 | 2014-08-21 09:45:00 -0700 | [diff] [blame] | 581 | VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location " |
| 582 | << image_location; |
Alex Light | b93637a | 2014-07-31 10:48:46 -0700 | [diff] [blame] | 583 | // If we are in /system we can assume the image is good. We can also |
| 584 | // assume this if we are using a relocated image (i.e. image checksum |
| 585 | // matches) since this is only different by the offset. We need this to |
| 586 | // make sure that host tests continue to work. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 587 | // Since we are the boot image, pass null since we load the oat file from the boot image oat |
| 588 | // file name. |
| 589 | space = ImageSpace::Init(image_filename->c_str(), |
| 590 | image_location, |
| 591 | !(is_system || relocated_version_used), |
| 592 | /* oat_file */nullptr, |
| 593 | error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 594 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 595 | if (space != nullptr) { |
| 596 | return space; |
| 597 | } |
| 598 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 599 | if (relocated_version_used) { |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 600 | // Something is wrong with the relocated copy (even though checksums match). Cleanup. |
| 601 | // This can happen if the .oat is corrupt, since the above only checks the .art checksums. |
| 602 | // TODO: Check the oat file validity earlier. |
| 603 | *error_msg = StringPrintf("Attempted to use relocated version of %s at %s generated from %s " |
| 604 | "but image failed to load: %s", |
| 605 | image_location, cache_filename.c_str(), system_filename.c_str(), |
| 606 | error_msg->c_str()); |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 607 | PruneDalvikCache(image_isa); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 608 | return nullptr; |
| 609 | } else if (is_system) { |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 610 | // 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] | 611 | *error_msg = StringPrintf("Failed to load /system image '%s': %s", |
| 612 | image_filename->c_str(), error_msg->c_str()); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 613 | return nullptr; |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 614 | } else { |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 615 | // Otherwise, log a warning and fall through to GenerateImage. |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 616 | LOG(WARNING) << *error_msg; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 619 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 620 | if (!can_compile) { |
| 621 | *error_msg = "Not attempting to compile image because -Xnoimage-dex2oat"; |
| 622 | return nullptr; |
| 623 | } else if (!dalvik_cache_exists) { |
| 624 | *error_msg = StringPrintf("No place to put generated image."); |
| 625 | return nullptr; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 626 | } else if (!ImageCreationAllowed(is_global_cache, error_msg)) { |
| 627 | return nullptr; |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 628 | } else if (secondary_image) { |
| 629 | *error_msg = "Cannot compile a secondary image."; |
| 630 | return nullptr; |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 631 | } else if (!GenerateImage(cache_filename, image_isa, error_msg)) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 632 | *error_msg = StringPrintf("Failed to generate image '%s': %s", |
| 633 | cache_filename.c_str(), error_msg->c_str()); |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 634 | // We failed to create files, remove any possibly garbage output. |
| 635 | // Since ImageCreationAllowed was true above, we are the zygote |
| 636 | // and therefore the only process expected to generate these for |
| 637 | // the device. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 638 | PruneDalvikCache(image_isa); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 639 | return nullptr; |
| 640 | } else { |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 641 | // Check whether there is enough space left over after we have generated the image. |
| 642 | if (!CheckSpace(cache_filename, error_msg)) { |
| 643 | // 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] | 644 | PruneDalvikCache(image_isa); |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 645 | return nullptr; |
| 646 | } |
| 647 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 648 | // Note that we must not use the file descriptor associated with |
| 649 | // ScopedFlock::GetFile to Init the image file. We want the file |
| 650 | // descriptor (and the associated exclusive lock) to be released when |
| 651 | // we leave Create. |
| 652 | ScopedFlock image_lock; |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 653 | image_lock.Init(cache_filename.c_str(), error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 654 | space = ImageSpace::Init(cache_filename.c_str(), image_location, true, nullptr, error_msg); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 655 | if (space == nullptr) { |
| 656 | *error_msg = StringPrintf("Failed to load generated image '%s': %s", |
| 657 | cache_filename.c_str(), error_msg->c_str()); |
| 658 | } |
| 659 | return space; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 660 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 663 | void ImageSpace::VerifyImageAllocations() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 664 | uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 665 | while (current < End()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 666 | CHECK_ALIGNED(current, kObjectAlignment); |
| 667 | auto* obj = reinterpret_cast<mirror::Object*>(current); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 668 | CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class"; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 669 | CHECK(live_bitmap_->Test(obj)) << PrettyTypeOf(obj); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 670 | if (kUseBakerOrBrooksReadBarrier) { |
| 671 | obj->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 672 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 673 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
| 674 | } |
| 675 | } |
| 676 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 677 | // Helper class for relocating from one range of memory to another. |
| 678 | class RelocationRange { |
| 679 | public: |
| 680 | RelocationRange() = default; |
| 681 | RelocationRange(const RelocationRange&) = default; |
| 682 | RelocationRange(uintptr_t source, uintptr_t dest, uintptr_t length) |
| 683 | : source_(source), |
| 684 | dest_(dest), |
| 685 | length_(length) {} |
| 686 | |
| 687 | bool ContainsSource(uintptr_t address) const { |
| 688 | return address - source_ < length_; |
| 689 | } |
| 690 | |
| 691 | // Translate a source address to the destination space. |
| 692 | uintptr_t ToDest(uintptr_t address) const { |
| 693 | DCHECK(ContainsSource(address)); |
| 694 | return address + Delta(); |
| 695 | } |
| 696 | |
| 697 | // Returns the delta between the dest from the source. |
| 698 | off_t Delta() const { |
| 699 | return dest_ - source_; |
| 700 | } |
| 701 | |
| 702 | uintptr_t Source() const { |
| 703 | return source_; |
| 704 | } |
| 705 | |
| 706 | uintptr_t Dest() const { |
| 707 | return dest_; |
| 708 | } |
| 709 | |
| 710 | uintptr_t Length() const { |
| 711 | return length_; |
| 712 | } |
| 713 | |
| 714 | private: |
| 715 | const uintptr_t source_; |
| 716 | const uintptr_t dest_; |
| 717 | const uintptr_t length_; |
| 718 | }; |
| 719 | |
| 720 | class FixupVisitor : public ValueObject { |
| 721 | public: |
| 722 | FixupVisitor(const RelocationRange& boot_image, |
| 723 | const RelocationRange& boot_oat, |
| 724 | const RelocationRange& app_image, |
| 725 | const RelocationRange& app_oat) |
| 726 | : boot_image_(boot_image), |
| 727 | boot_oat_(boot_oat), |
| 728 | app_image_(app_image), |
| 729 | app_oat_(app_oat) {} |
| 730 | |
| 731 | // Return the relocated address of a heap object. |
| 732 | template <typename T> |
| 733 | ALWAYS_INLINE T* ForwardObject(T* src) const { |
| 734 | const uintptr_t uint_src = reinterpret_cast<uintptr_t>(src); |
| 735 | if (boot_image_.ContainsSource(uint_src)) { |
| 736 | return reinterpret_cast<T*>(boot_image_.ToDest(uint_src)); |
| 737 | } |
| 738 | if (app_image_.ContainsSource(uint_src)) { |
| 739 | return reinterpret_cast<T*>(app_image_.ToDest(uint_src)); |
| 740 | } |
| 741 | return src; |
| 742 | } |
| 743 | |
| 744 | // Return the relocated address of a code pointer (contained by an oat file). |
| 745 | ALWAYS_INLINE const void* ForwardCode(const void* src) const { |
| 746 | const uintptr_t uint_src = reinterpret_cast<uintptr_t>(src); |
| 747 | if (boot_oat_.ContainsSource(uint_src)) { |
| 748 | return reinterpret_cast<const void*>(boot_oat_.ToDest(uint_src)); |
| 749 | } |
| 750 | if (app_oat_.ContainsSource(uint_src)) { |
| 751 | return reinterpret_cast<const void*>(app_oat_.ToDest(uint_src)); |
| 752 | } |
| 753 | return src; |
| 754 | } |
| 755 | |
| 756 | protected: |
| 757 | // Source section. |
| 758 | const RelocationRange boot_image_; |
| 759 | const RelocationRange boot_oat_; |
| 760 | const RelocationRange app_image_; |
| 761 | const RelocationRange app_oat_; |
| 762 | }; |
| 763 | |
| 764 | std::ostream& operator<<(std::ostream& os, const RelocationRange& reloc) { |
| 765 | return os << "(" << reinterpret_cast<const void*>(reloc.Source()) << "-" |
| 766 | << reinterpret_cast<const void*>(reloc.Source() + reloc.Length()) << ")->(" |
| 767 | << reinterpret_cast<const void*>(reloc.Dest()) << "-" |
| 768 | << reinterpret_cast<const void*>(reloc.Dest() + reloc.Length()) << ")"; |
| 769 | } |
| 770 | |
| 771 | // Adapt for mirror::Class::FixupNativePointers. |
| 772 | class FixupObjectAdapter : public FixupVisitor { |
| 773 | public: |
| 774 | template<typename... Args> |
| 775 | explicit FixupObjectAdapter(Args... args) : FixupVisitor(args...) {} |
| 776 | |
| 777 | template <typename T> |
| 778 | T* operator()(T* obj) const { |
| 779 | return ForwardObject(obj); |
| 780 | } |
| 781 | }; |
| 782 | |
| 783 | class FixupClassVisitor : public FixupVisitor { |
| 784 | public: |
| 785 | template<typename... Args> |
| 786 | explicit FixupClassVisitor(Args... args) : FixupVisitor(args...) {} |
| 787 | |
| 788 | // The image space is contained so the GC doesn't need to know about it. Avoid requiring mutator |
| 789 | // lock to prevent possible pauses. |
| 790 | ALWAYS_INLINE void operator()(mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
| 791 | mirror::Class* klass = obj->GetClass<kVerifyNone, kWithoutReadBarrier>(); |
| 792 | DCHECK(klass != nullptr) << "Null class in image"; |
| 793 | // No AsClass since our fields aren't quite fixed up yet. |
| 794 | mirror::Class* new_klass = down_cast<mirror::Class*>(ForwardObject(klass)); |
| 795 | // Keep clean if possible. |
| 796 | if (klass != new_klass) { |
| 797 | obj->SetClass<kVerifyNone>(new_klass); |
| 798 | } |
| 799 | } |
| 800 | }; |
| 801 | |
| 802 | class FixupRootVisitor : public FixupVisitor { |
| 803 | public: |
| 804 | template<typename... Args> |
| 805 | explicit FixupRootVisitor(Args... args) : FixupVisitor(args...) {} |
| 806 | |
| 807 | ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 808 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 809 | if (!root->IsNull()) { |
| 810 | VisitRoot(root); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 815 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 816 | mirror::Object* ref = root->AsMirrorPtr(); |
| 817 | mirror::Object* new_ref = ForwardObject(ref); |
| 818 | if (ref != new_ref) { |
| 819 | root->Assign(new_ref); |
| 820 | } |
| 821 | } |
| 822 | }; |
| 823 | |
| 824 | class FixupObjectVisitor : public FixupVisitor { |
| 825 | public: |
| 826 | template<typename... Args> |
| 827 | explicit FixupObjectVisitor(Args... args) : FixupVisitor(args...) {} |
| 828 | |
| 829 | // Fix up separately since we also need to fix up method entrypoints. |
| 830 | ALWAYS_INLINE void VisitRootIfNonNull( |
| 831 | mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 832 | |
| 833 | ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 834 | const {} |
| 835 | |
| 836 | ALWAYS_INLINE void operator()(mirror::Object* obj, |
| 837 | MemberOffset offset, |
| 838 | bool is_static ATTRIBUTE_UNUSED) const |
| 839 | NO_THREAD_SAFETY_ANALYSIS { |
| 840 | // There could be overlap between ranges, we must avoid visiting the same reference twice. |
| 841 | // Avoid the class field since we already fixed it up in FixupClassVisitor. |
| 842 | if (offset.Uint32Value() != mirror::Object::ClassOffset().Uint32Value()) { |
| 843 | // Space is not yet added to the heap, don't do a read barrier. |
| 844 | mirror::Object* ref = obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>( |
| 845 | offset); |
| 846 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 847 | // image. |
| 848 | obj->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(offset, ForwardObject(ref)); |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | // java.lang.ref.Reference visitor. |
| 853 | void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, mirror::Reference* ref) const |
| 854 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
| 855 | mirror::Object* obj = ref->GetReferent<kWithoutReadBarrier>(); |
| 856 | ref->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
| 857 | mirror::Reference::ReferentOffset(), |
| 858 | ForwardObject(obj)); |
| 859 | } |
| 860 | |
| 861 | ALWAYS_INLINE void operator()(mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
| 862 | obj->VisitReferences</*visit native roots*/false, kVerifyNone, kWithoutReadBarrier>( |
| 863 | *this, |
| 864 | *this); |
| 865 | // We want to use our own class loader and not the one in the image. |
| 866 | if (obj->IsClass<kVerifyNone, kWithoutReadBarrier>()) { |
| 867 | mirror::Class* klass = obj->AsClass<kVerifyNone, kWithoutReadBarrier>(); |
| 868 | FixupObjectAdapter visitor(boot_image_, boot_oat_, app_image_, app_oat_); |
| 869 | klass->FixupNativePointers(klass, sizeof(void*), visitor); |
| 870 | // Deal with the arrays. |
| 871 | mirror::PointerArray* vtable = klass->GetVTable<kVerifyNone, kWithoutReadBarrier>(); |
| 872 | if (vtable != nullptr) { |
| 873 | vtable->Fixup(vtable, sizeof(void*), visitor); |
| 874 | } |
| 875 | mirror::IfTable* iftable = klass->GetIfTable<kVerifyNone, kWithoutReadBarrier>(); |
| 876 | if (iftable != nullptr) { |
| 877 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 878 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 879 | mirror::PointerArray* methods = |
| 880 | iftable->GetMethodArray<kVerifyNone, kWithoutReadBarrier>(i); |
| 881 | DCHECK(methods != nullptr); |
| 882 | methods->Fixup(methods, sizeof(void*), visitor); |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | }; |
| 889 | |
| 890 | class ForwardObjectAdapter { |
| 891 | public: |
| 892 | ALWAYS_INLINE ForwardObjectAdapter(const FixupVisitor* visitor) : visitor_(visitor) {} |
| 893 | |
| 894 | template <typename T> |
| 895 | ALWAYS_INLINE T* operator()(T* src) const { |
| 896 | return visitor_->ForwardObject(src); |
| 897 | } |
| 898 | |
| 899 | private: |
| 900 | const FixupVisitor* const visitor_; |
| 901 | }; |
| 902 | |
| 903 | class ForwardCodeAdapter { |
| 904 | public: |
| 905 | ALWAYS_INLINE ForwardCodeAdapter(const FixupVisitor* visitor) : visitor_(visitor) {} |
| 906 | |
| 907 | template <typename T> |
| 908 | ALWAYS_INLINE T* operator()(T* src) const { |
| 909 | return visitor_->ForwardCode(src); |
| 910 | } |
| 911 | |
| 912 | private: |
| 913 | const FixupVisitor* const visitor_; |
| 914 | }; |
| 915 | |
| 916 | class FixupArtMethodVisitor : public FixupVisitor, public ArtMethodVisitor { |
| 917 | public: |
| 918 | template<typename... Args> |
| 919 | explicit FixupArtMethodVisitor(bool fixup_heap_objects, Args... args) |
| 920 | : FixupVisitor(args...), |
| 921 | fixup_heap_objects_(fixup_heap_objects) {} |
| 922 | |
| 923 | virtual void Visit(ArtMethod* method) NO_THREAD_SAFETY_ANALYSIS { |
| 924 | if (fixup_heap_objects_) { |
| 925 | method->UpdateObjectsForImageRelocation(ForwardObjectAdapter(this)); |
| 926 | } |
| 927 | method->UpdateEntrypoints(ForwardCodeAdapter(this)); |
| 928 | } |
| 929 | |
| 930 | private: |
| 931 | const bool fixup_heap_objects_; |
| 932 | }; |
| 933 | |
| 934 | class FixupArtFieldVisitor : public FixupVisitor, public ArtFieldVisitor { |
| 935 | public: |
| 936 | template<typename... Args> |
| 937 | explicit FixupArtFieldVisitor(Args... args) : FixupVisitor(args...) {} |
| 938 | |
| 939 | virtual void Visit(ArtField* field) NO_THREAD_SAFETY_ANALYSIS { |
| 940 | field->UpdateObjects(ForwardObjectAdapter(this)); |
| 941 | } |
| 942 | }; |
| 943 | |
| 944 | // Relocate an image space mapped at target_base which possibly used to be at a different base |
| 945 | // address. Only needs a single image space, not one for both source and destination. |
| 946 | // In place means modifying a single ImageSpace in place rather than relocating from one ImageSpace |
| 947 | // to another. |
| 948 | static bool RelocateInPlace(ImageHeader& image_header, |
| 949 | uint8_t* target_base, |
| 950 | accounting::ContinuousSpaceBitmap* bitmap, |
| 951 | const OatFile* app_oat_file, |
| 952 | std::string* error_msg) { |
| 953 | DCHECK(error_msg != nullptr); |
| 954 | if (!image_header.IsPic()) { |
| 955 | if (image_header.GetImageBegin() == target_base) { |
| 956 | return true; |
| 957 | } |
| 958 | *error_msg = StringPrintf("Cannot relocate non-pic image for oat file %s", |
| 959 | (app_oat_file != nullptr) ? app_oat_file->GetLocation().c_str() : ""); |
| 960 | return false; |
| 961 | } |
| 962 | // Set up sections. |
| 963 | uint32_t boot_image_begin = 0; |
| 964 | uint32_t boot_image_end = 0; |
| 965 | uint32_t boot_oat_begin = 0; |
| 966 | uint32_t boot_oat_end = 0; |
| 967 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 968 | heap->GetBootImagesSize(&boot_image_begin, &boot_image_end, &boot_oat_begin, &boot_oat_end); |
| 969 | CHECK_NE(boot_image_begin, boot_image_end) |
| 970 | << "Can not relocate app image without boot image space"; |
| 971 | CHECK_NE(boot_oat_begin, boot_oat_end) << "Can not relocate app image without boot oat file"; |
| 972 | const uint32_t boot_image_size = boot_image_end - boot_image_begin; |
| 973 | const uint32_t boot_oat_size = boot_oat_end - boot_oat_begin; |
| 974 | const uint32_t image_header_boot_image_size = image_header.GetBootImageSize(); |
| 975 | const uint32_t image_header_boot_oat_size = image_header.GetBootOatSize(); |
| 976 | if (boot_image_size != image_header_boot_image_size) { |
| 977 | *error_msg = StringPrintf("Boot image size %" PRIu64 " does not match expected size %" |
| 978 | PRIu64, |
| 979 | static_cast<uint64_t>(boot_image_size), |
| 980 | static_cast<uint64_t>(image_header_boot_image_size)); |
| 981 | return false; |
| 982 | } |
| 983 | if (boot_oat_size != image_header_boot_oat_size) { |
| 984 | *error_msg = StringPrintf("Boot oat size %" PRIu64 " does not match expected size %" |
| 985 | PRIu64, |
| 986 | static_cast<uint64_t>(boot_oat_size), |
| 987 | static_cast<uint64_t>(image_header_boot_oat_size)); |
| 988 | return false; |
| 989 | } |
| 990 | TimingLogger logger(__FUNCTION__, true, false); |
| 991 | RelocationRange boot_image(image_header.GetBootImageBegin(), |
| 992 | boot_image_begin, |
| 993 | boot_image_size); |
| 994 | RelocationRange boot_oat(image_header.GetBootOatBegin(), |
| 995 | boot_oat_begin, |
| 996 | boot_oat_size); |
| 997 | RelocationRange app_image(reinterpret_cast<uintptr_t>(image_header.GetImageBegin()), |
| 998 | reinterpret_cast<uintptr_t>(target_base), |
| 999 | image_header.GetImageSize()); |
| 1000 | // Use the oat data section since this is where the OatFile::Begin is. |
| 1001 | RelocationRange app_oat(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()), |
| 1002 | // Not necessarily in low 4GB. |
| 1003 | reinterpret_cast<uintptr_t>(app_oat_file->Begin()), |
| 1004 | image_header.GetOatDataEnd() - image_header.GetOatDataBegin()); |
| 1005 | VLOG(image) << "App image " << app_image; |
| 1006 | VLOG(image) << "App oat " << app_oat; |
| 1007 | VLOG(image) << "Boot image " << boot_image; |
| 1008 | VLOG(image) << "Boot oat " << boot_oat; |
| 1009 | // True if we need to fixup any heap pointers, otherwise only code pointers. |
| 1010 | const bool fixup_image = boot_image.Delta() != 0 || app_image.Delta() != 0; |
| 1011 | const bool fixup_code = boot_oat.Delta() != 0 || app_oat.Delta() != 0; |
| 1012 | if (!fixup_image && !fixup_code) { |
| 1013 | // Nothing to fix up. |
| 1014 | return true; |
| 1015 | } |
| 1016 | // Need to update the image to be at the target base. |
| 1017 | const ImageSection& objects_section = image_header.GetImageSection(ImageHeader::kSectionObjects); |
| 1018 | uintptr_t objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset()); |
| 1019 | uintptr_t objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End()); |
| 1020 | // Two pass approach, fix up all classes first, then fix up non class-objects. |
| 1021 | FixupObjectVisitor fixup_object_visitor(boot_image, boot_oat, app_image, app_oat); |
| 1022 | if (fixup_image) { |
| 1023 | TimingLogger::ScopedTiming timing("Fixup classes", &logger); |
| 1024 | // Fixup class only touches app image classes, don't need the mutator lock since the space is |
| 1025 | // not yet visible to the GC. |
| 1026 | FixupClassVisitor fixup_class_visitor(boot_image, boot_oat, app_image, app_oat); |
| 1027 | bitmap->VisitMarkedRange(objects_begin, objects_end, fixup_class_visitor); |
| 1028 | // Fixup objects may read fields in the boot image, use the mutator lock here for sanity. Though |
| 1029 | // its probably not required. |
| 1030 | ScopedObjectAccess soa(Thread::Current()); |
| 1031 | timing.NewTiming("Fixup objects"); |
| 1032 | bitmap->VisitMarkedRange(objects_begin, objects_end, fixup_object_visitor); |
| 1033 | FixupObjectAdapter fixup_adapter(boot_image, boot_oat, app_image, app_oat); |
| 1034 | // Fixup image roots. |
| 1035 | CHECK(app_image.ContainsSource(reinterpret_cast<uintptr_t>(image_header.GetImageRoots()))); |
| 1036 | image_header.RelocateImageObjects(app_image.Delta()); |
| 1037 | CHECK_EQ(image_header.GetImageBegin(), target_base); |
| 1038 | // Fix up dex cache DexFile pointers. |
| 1039 | auto* dex_caches = image_header.GetImageRoot(ImageHeader::kDexCaches)-> |
| 1040 | AsObjectArray<mirror::DexCache>(); |
| 1041 | for (int32_t i = 0, count = dex_caches->GetLength(); i < count; ++i) { |
| 1042 | mirror::DexCache* dex_cache = dex_caches->Get(i); |
| 1043 | // Fix up dex cache pointers. |
| 1044 | GcRoot<mirror::String>* strings = dex_cache->GetStrings(); |
| 1045 | if (strings != nullptr) { |
| 1046 | GcRoot<mirror::String>* new_strings = fixup_adapter.ForwardObject(strings); |
| 1047 | if (strings != new_strings) { |
| 1048 | dex_cache->SetFieldPtr64<false>(mirror::DexCache::StringsOffset(), new_strings); |
| 1049 | } |
| 1050 | dex_cache->FixupStrings(new_strings, fixup_adapter); |
| 1051 | } |
| 1052 | GcRoot<mirror::Class>* types = dex_cache->GetResolvedTypes(); |
| 1053 | if (types != nullptr) { |
| 1054 | GcRoot<mirror::Class>* new_types = fixup_adapter.ForwardObject(types); |
| 1055 | if (types != new_types) { |
| 1056 | dex_cache->SetFieldPtr64<false>(mirror::DexCache::ResolvedTypesOffset(), new_types); |
| 1057 | } |
| 1058 | dex_cache->FixupResolvedTypes(new_types, fixup_adapter); |
| 1059 | } |
| 1060 | ArtMethod** methods = dex_cache->GetResolvedMethods(); |
| 1061 | if (methods != nullptr) { |
| 1062 | ArtMethod** new_methods = fixup_adapter.ForwardObject(methods); |
| 1063 | if (methods != new_methods) { |
| 1064 | dex_cache->SetFieldPtr64<false>(mirror::DexCache::ResolvedMethodsOffset(), new_methods); |
| 1065 | } |
| 1066 | for (size_t j = 0, num = dex_cache->NumResolvedMethods(); j != num; ++j) { |
| 1067 | ArtMethod* orig = mirror::DexCache::GetElementPtrSize(new_methods, j, sizeof(void*)); |
| 1068 | ArtMethod* copy = fixup_adapter.ForwardObject(orig); |
| 1069 | if (orig != copy) { |
| 1070 | mirror::DexCache::SetElementPtrSize(new_methods, j, copy, sizeof(void*)); |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | ArtField** fields = dex_cache->GetResolvedFields(); |
| 1075 | if (fields != nullptr) { |
| 1076 | ArtField** new_fields = fixup_adapter.ForwardObject(fields); |
| 1077 | if (fields != new_fields) { |
| 1078 | dex_cache->SetFieldPtr64<false>(mirror::DexCache::ResolvedFieldsOffset(), new_fields); |
| 1079 | } |
| 1080 | for (size_t j = 0, num = dex_cache->NumResolvedFields(); j != num; ++j) { |
| 1081 | ArtField* orig = mirror::DexCache::GetElementPtrSize(new_fields, j, sizeof(void*)); |
| 1082 | ArtField* copy = fixup_adapter.ForwardObject(orig); |
| 1083 | if (orig != copy) { |
| 1084 | mirror::DexCache::SetElementPtrSize(new_fields, j, copy, sizeof(void*)); |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | { |
| 1091 | // Only touches objects in the app image, no need for mutator lock. |
| 1092 | TimingLogger::ScopedTiming timing("Fixup methods", &logger); |
| 1093 | FixupArtMethodVisitor method_visitor(fixup_image, boot_image, boot_oat, app_image, app_oat); |
| 1094 | image_header.GetImageSection(ImageHeader::kSectionArtMethods).VisitPackedArtMethods( |
| 1095 | &method_visitor, |
| 1096 | target_base, |
| 1097 | sizeof(void*)); |
| 1098 | } |
| 1099 | if (fixup_image) { |
| 1100 | { |
| 1101 | // Only touches objects in the app image, no need for mutator lock. |
| 1102 | TimingLogger::ScopedTiming timing("Fixup fields", &logger); |
| 1103 | FixupArtFieldVisitor field_visitor(boot_image, boot_oat, app_image, app_oat); |
| 1104 | image_header.GetImageSection(ImageHeader::kSectionArtFields).VisitPackedArtFields( |
| 1105 | &field_visitor, |
| 1106 | target_base); |
| 1107 | } |
| 1108 | // In the app image case, the image methods are actually in the boot image. |
| 1109 | image_header.RelocateImageMethods(boot_image.Delta()); |
| 1110 | const auto& class_table_section = image_header.GetImageSection(ImageHeader::kSectionClassTable); |
| 1111 | if (class_table_section.Size() > 0u) { |
| 1112 | // Note that we require that ReadFromMemory does not make an internal copy of the elements. |
| 1113 | // This also relies on visit roots not doing any verification which could fail after we update |
| 1114 | // the roots to be the image addresses. |
| 1115 | ScopedObjectAccess soa(Thread::Current()); |
| 1116 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 1117 | ClassTable temp_table; |
| 1118 | temp_table.ReadFromMemory(target_base + class_table_section.Offset()); |
| 1119 | FixupRootVisitor root_visitor(boot_image, boot_oat, app_image, app_oat); |
| 1120 | temp_table.VisitRoots(root_visitor); |
| 1121 | } |
| 1122 | } |
| 1123 | if (VLOG_IS_ON(image)) { |
| 1124 | logger.Dump(LOG(INFO)); |
| 1125 | } |
| 1126 | return true; |
| 1127 | } |
| 1128 | |
| 1129 | ImageSpace* ImageSpace::Init(const char* image_filename, |
| 1130 | const char* image_location, |
| 1131 | bool validate_oat_file, |
| 1132 | const OatFile* oat_file, |
| 1133 | std::string* error_msg) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1134 | CHECK(image_filename != nullptr); |
| 1135 | CHECK(image_location != nullptr); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1136 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1137 | TimingLogger logger(__FUNCTION__, true, false); |
| 1138 | VLOG(image) << "ImageSpace::Init entering image_filename=" << image_filename; |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 1139 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1140 | std::unique_ptr<File> file; |
| 1141 | { |
| 1142 | TimingLogger::ScopedTiming timing("OpenImageFile", &logger); |
| 1143 | file.reset(OS::OpenFileForReading(image_filename)); |
| 1144 | if (file == nullptr) { |
| 1145 | *error_msg = StringPrintf("Failed to open '%s'", image_filename); |
| 1146 | return nullptr; |
| 1147 | } |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 1148 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1149 | ImageHeader temp_image_header; |
| 1150 | ImageHeader* image_header = &temp_image_header; |
| 1151 | { |
| 1152 | TimingLogger::ScopedTiming timing("ReadImageHeader", &logger); |
| 1153 | bool success = file->ReadFully(image_header, sizeof(*image_header)); |
| 1154 | if (!success || !image_header->IsValid()) { |
| 1155 | *error_msg = StringPrintf("Invalid image header in '%s'", image_filename); |
| 1156 | return nullptr; |
| 1157 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1158 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1159 | // Check that the file is larger or equal to the header size + data size. |
| 1160 | const uint64_t image_file_size = static_cast<uint64_t>(file->GetLength()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1161 | if (image_file_size < sizeof(ImageHeader) + image_header->GetDataSize()) { |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1162 | *error_msg = StringPrintf("Image file truncated: %" PRIu64 " vs. %" PRIu64 ".", |
| 1163 | image_file_size, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1164 | sizeof(ImageHeader) + image_header->GetDataSize()); |
Andreas Gampe | 6c8b49f | 2015-02-19 11:42:36 -0800 | [diff] [blame] | 1165 | return nullptr; |
| 1166 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1167 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1168 | if (VLOG_IS_ON(startup)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1169 | LOG(INFO) << "Dumping image sections"; |
| 1170 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 1171 | const auto section_idx = static_cast<ImageHeader::ImageSections>(i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1172 | auto& section = image_header->GetImageSection(section_idx); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1173 | LOG(INFO) << section_idx << " start=" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1174 | << reinterpret_cast<void*>(image_header->GetImageBegin() + section.Offset()) << " " |
| 1175 | << section; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1179 | const auto& bitmap_section = image_header->GetImageSection(ImageHeader::kSectionImageBitmap); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1180 | // The location we want to map from is the first aligned page after the end of the stored |
| 1181 | // (possibly compressed) data. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1182 | const size_t image_bitmap_offset = RoundUp(sizeof(ImageHeader) + image_header->GetDataSize(), |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1183 | kPageSize); |
| 1184 | const size_t end_of_bitmap = image_bitmap_offset + bitmap_section.Size(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1185 | if (end_of_bitmap != image_file_size) { |
| 1186 | *error_msg = StringPrintf( |
| 1187 | "Image file size does not equal end of bitmap: size=%" PRIu64 " vs. %zu.", image_file_size, |
| 1188 | end_of_bitmap); |
Andreas Gampe | 6c8b49f | 2015-02-19 11:42:36 -0800 | [diff] [blame] | 1189 | return nullptr; |
| 1190 | } |
| 1191 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1192 | // The preferred address to map the image, null specifies any address. If we manage to map the |
| 1193 | // image at the image begin, the amount of fixup work required is minimized. |
| 1194 | std::vector<uint8_t*> addresses(1, image_header->GetImageBegin()); |
| 1195 | if (image_header->IsPic()) { |
| 1196 | // Can also map at a random low_4gb address since we can relocate in-place. |
| 1197 | addresses.push_back(nullptr); |
| 1198 | } |
| 1199 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1200 | // 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] | 1201 | std::unique_ptr<MemMap> map; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1202 | std::string temp_error_msg; |
| 1203 | for (uint8_t* address : addresses) { |
| 1204 | TimingLogger::ScopedTiming timing("MapImageFile", &logger); |
| 1205 | // Only care about the error message for the last address in addresses. We want to avoid the |
| 1206 | // overhead of printing the process maps if we can relocate. |
| 1207 | std::string* out_error_msg = (address == addresses.back()) ? &temp_error_msg : nullptr; |
| 1208 | if (image_header->GetStorageMode() == ImageHeader::kStorageModeUncompressed) { |
| 1209 | map.reset(MemMap::MapFileAtAddress(address, |
| 1210 | image_header->GetImageSize(), |
| 1211 | PROT_READ | PROT_WRITE, |
| 1212 | MAP_PRIVATE, |
| 1213 | file->Fd(), |
| 1214 | 0, |
| 1215 | /*low_4gb*/true, |
| 1216 | /*reuse*/false, |
| 1217 | image_filename, |
| 1218 | /*out*/out_error_msg)); |
| 1219 | } else { |
| 1220 | // Reserve output and decompress into it. |
| 1221 | map.reset(MemMap::MapAnonymous(image_location, |
| 1222 | address, |
| 1223 | image_header->GetImageSize(), |
| 1224 | PROT_READ | PROT_WRITE, |
| 1225 | /*low_4gb*/true, |
| 1226 | /*reuse*/false, |
| 1227 | out_error_msg)); |
| 1228 | if (map != nullptr) { |
| 1229 | const size_t stored_size = image_header->GetDataSize(); |
| 1230 | const size_t write_offset = sizeof(ImageHeader); // Skip the header. |
| 1231 | std::unique_ptr<MemMap> temp_map(MemMap::MapFile(sizeof(ImageHeader) + stored_size, |
| 1232 | PROT_READ, |
| 1233 | MAP_PRIVATE, |
| 1234 | file->Fd(), |
| 1235 | /*offset*/0, |
| 1236 | /*low_4gb*/false, |
| 1237 | image_filename, |
| 1238 | out_error_msg)); |
| 1239 | if (temp_map == nullptr) { |
| 1240 | DCHECK(!out_error_msg->empty()); |
| 1241 | return nullptr; |
| 1242 | } |
| 1243 | memcpy(map->Begin(), image_header, sizeof(ImageHeader)); |
| 1244 | const uint64_t start = NanoTime(); |
| 1245 | const size_t decompressed_size = LZ4_decompress_safe( |
| 1246 | reinterpret_cast<char*>(temp_map->Begin()) + sizeof(ImageHeader), |
| 1247 | reinterpret_cast<char*>(map->Begin()) + write_offset, |
| 1248 | stored_size, |
| 1249 | map->Size()); |
| 1250 | VLOG(image) << "Decompressing image took " << PrettyDuration(NanoTime() - start); |
| 1251 | if (decompressed_size + sizeof(ImageHeader) != image_header->GetImageSize()) { |
| 1252 | *error_msg = StringPrintf("Decompressed size does not match expected image size %zu vs %zu", |
| 1253 | decompressed_size + sizeof(ImageHeader), |
| 1254 | image_header->GetImageSize()); |
| 1255 | return nullptr; |
| 1256 | } |
| 1257 | } |
| 1258 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1259 | if (map != nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1260 | break; |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1264 | if (map == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1265 | DCHECK(!temp_error_msg.empty()); |
| 1266 | *error_msg = temp_error_msg; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1267 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1268 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1269 | DCHECK_EQ(0, memcmp(image_header, map->Begin(), sizeof(ImageHeader))); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1270 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1271 | std::unique_ptr<MemMap> image_bitmap_map(MemMap::MapFileAtAddress(nullptr, |
| 1272 | bitmap_section.Size(), |
| 1273 | PROT_READ, MAP_PRIVATE, |
| 1274 | file->Fd(), |
| 1275 | image_bitmap_offset, |
| 1276 | /*low_4gb*/false, |
| 1277 | /*reuse*/false, |
| 1278 | image_filename, |
| 1279 | error_msg)); |
| 1280 | if (image_bitmap_map == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1281 | *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str()); |
| 1282 | return nullptr; |
| 1283 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1284 | // Loaded the map, use the image header from the file now in case we patch it with |
| 1285 | // RelocateInPlace. |
| 1286 | image_header = reinterpret_cast<ImageHeader*>(map->Begin()); |
| 1287 | const uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1); |
| 1288 | std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", |
| 1289 | image_filename, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1290 | bitmap_index)); |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 1291 | // Bitmap only needs to cover until the end of the mirror objects section. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1292 | const ImageSection& image_objects = image_header->GetImageSection(ImageHeader::kSectionObjects); |
| 1293 | // We only want the mirror object, not the ArtFields and ArtMethods. |
| 1294 | uint8_t* const image_end = map->Begin() + image_objects.End(); |
| 1295 | std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap; |
| 1296 | { |
| 1297 | TimingLogger::ScopedTiming timing("CreateImageBitmap", &logger); |
| 1298 | bitmap.reset( |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1299 | accounting::ContinuousSpaceBitmap::CreateFromMemMap( |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1300 | bitmap_name, |
| 1301 | image_bitmap_map.release(), |
| 1302 | reinterpret_cast<uint8_t*>(map->Begin()), |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 1303 | image_objects.End())); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1304 | if (bitmap == nullptr) { |
| 1305 | *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str()); |
| 1306 | return nullptr; |
| 1307 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1308 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1309 | { |
| 1310 | TimingLogger::ScopedTiming timing("RelocateImage", &logger); |
| 1311 | if (!RelocateInPlace(*image_header, |
| 1312 | map->Begin(), |
| 1313 | bitmap.get(), |
| 1314 | oat_file, |
| 1315 | error_msg)) { |
| 1316 | return nullptr; |
| 1317 | } |
| 1318 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1319 | // We only want the mirror object, not the ArtFields and ArtMethods. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1320 | std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, |
| 1321 | image_location, |
| 1322 | map.release(), |
| 1323 | bitmap.release(), |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 1324 | image_end)); |
Hiroshi Yamauchi | bd0fb61 | 2014-05-20 13:46:00 -0700 | [diff] [blame] | 1325 | |
| 1326 | // VerifyImageAllocations() will be called later in Runtime::Init() |
| 1327 | // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_ |
| 1328 | // and ArtField::java_lang_reflect_ArtField_, which are used from |
| 1329 | // Object::SizeOf() which VerifyImageAllocations() calls, are not |
| 1330 | // set yet at this point. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1331 | if (oat_file == nullptr) { |
| 1332 | TimingLogger::ScopedTiming timing("OpenOatFile", &logger); |
| 1333 | space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg)); |
| 1334 | if (space->oat_file_ == nullptr) { |
| 1335 | DCHECK(!error_msg->empty()); |
| 1336 | return nullptr; |
| 1337 | } |
| 1338 | space->oat_file_non_owned_ = space->oat_file_.get(); |
| 1339 | } else { |
| 1340 | space->oat_file_non_owned_ = oat_file; |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 1341 | } |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 1342 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1343 | if (validate_oat_file) { |
| 1344 | TimingLogger::ScopedTiming timing("ValidateOatFile", &logger); |
| 1345 | if (!space->ValidateOatFile(error_msg)) { |
| 1346 | DCHECK(!error_msg->empty()); |
| 1347 | return nullptr; |
| 1348 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 1351 | Runtime* runtime = Runtime::Current(); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 1352 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1353 | // If oat_file is null, then it is the boot image space. Use oat_file_non_owned_ from the space |
| 1354 | // to set the runtime methods. |
| 1355 | CHECK_EQ(oat_file != nullptr, image_header->IsAppImage()); |
| 1356 | if (image_header->IsAppImage()) { |
| 1357 | CHECK_EQ(runtime->GetResolutionMethod(), |
| 1358 | image_header->GetImageMethod(ImageHeader::kResolutionMethod)); |
| 1359 | CHECK_EQ(runtime->GetImtConflictMethod(), |
| 1360 | image_header->GetImageMethod(ImageHeader::kImtConflictMethod)); |
| 1361 | CHECK_EQ(runtime->GetImtUnimplementedMethod(), |
| 1362 | image_header->GetImageMethod(ImageHeader::kImtUnimplementedMethod)); |
| 1363 | CHECK_EQ(runtime->GetCalleeSaveMethod(Runtime::kSaveAll), |
| 1364 | image_header->GetImageMethod(ImageHeader::kCalleeSaveMethod)); |
| 1365 | CHECK_EQ(runtime->GetCalleeSaveMethod(Runtime::kRefsOnly), |
| 1366 | image_header->GetImageMethod(ImageHeader::kRefsOnlySaveMethod)); |
| 1367 | CHECK_EQ(runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs), |
| 1368 | image_header->GetImageMethod(ImageHeader::kRefsAndArgsSaveMethod)); |
| 1369 | } else if (!runtime->HasResolutionMethod()) { |
| 1370 | runtime->SetInstructionSet(space->oat_file_non_owned_->GetOatHeader().GetInstructionSet()); |
| 1371 | runtime->SetResolutionMethod(image_header->GetImageMethod(ImageHeader::kResolutionMethod)); |
| 1372 | runtime->SetImtConflictMethod(image_header->GetImageMethod(ImageHeader::kImtConflictMethod)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1373 | runtime->SetImtUnimplementedMethod( |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1374 | image_header->GetImageMethod(ImageHeader::kImtUnimplementedMethod)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1375 | runtime->SetCalleeSaveMethod( |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1376 | image_header->GetImageMethod(ImageHeader::kCalleeSaveMethod), Runtime::kSaveAll); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1377 | runtime->SetCalleeSaveMethod( |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1378 | image_header->GetImageMethod(ImageHeader::kRefsOnlySaveMethod), Runtime::kRefsOnly); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1379 | runtime->SetCalleeSaveMethod( |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1380 | image_header->GetImageMethod(ImageHeader::kRefsAndArgsSaveMethod), Runtime::kRefsAndArgs); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1381 | } |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 1382 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1383 | VLOG(image) << "ImageSpace::Init exiting " << *space.get(); |
| 1384 | if (VLOG_IS_ON(image)) { |
| 1385 | logger.Dump(LOG(INFO)); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1386 | } |
| 1387 | return space.release(); |
| 1388 | } |
| 1389 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 1390 | OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1391 | const ImageHeader& image_header = GetImageHeader(); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 1392 | std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path); |
| 1393 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1394 | CHECK(image_header.GetOatDataBegin() != nullptr); |
| 1395 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1396 | OatFile* oat_file = OatFile::Open(oat_filename, |
| 1397 | oat_filename, |
| 1398 | image_header.GetOatDataBegin(), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1399 | image_header.GetOatFileBegin(), |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 1400 | !Runtime::Current()->IsAotCompiler(), |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1401 | nullptr, |
| 1402 | error_msg); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1403 | if (oat_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1404 | *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s", |
| 1405 | oat_filename.c_str(), GetName(), error_msg->c_str()); |
| 1406 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1407 | } |
| 1408 | uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum(); |
| 1409 | uint32_t image_oat_checksum = image_header.GetOatChecksum(); |
| 1410 | if (oat_checksum != image_oat_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1411 | *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x" |
| 1412 | " in image %s", oat_checksum, image_oat_checksum, GetName()); |
| 1413 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1414 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1415 | int32_t image_patch_delta = image_header.GetPatchDelta(); |
| 1416 | int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta(); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1417 | if (oat_patch_delta != image_patch_delta && !image_header.CompilePic()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1418 | // We should have already relocated by this point. Bail out. |
| 1419 | *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d " |
| 1420 | "in image %s", oat_patch_delta, image_patch_delta, GetName()); |
| 1421 | return nullptr; |
| 1422 | } |
| 1423 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1424 | return oat_file; |
| 1425 | } |
| 1426 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1427 | bool ImageSpace::ValidateOatFile(std::string* error_msg) const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1428 | CHECK(oat_file_.get() != nullptr); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1429 | for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1430 | const std::string& dex_file_location = oat_dex_file->GetDexFileLocation(); |
| 1431 | uint32_t dex_file_location_checksum; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1432 | if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) { |
| 1433 | *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: " |
| 1434 | "%s", dex_file_location.c_str(), GetName(), error_msg->c_str()); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1435 | return false; |
| 1436 | } |
| 1437 | if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1438 | *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and " |
| 1439 | "dex file '%s' (0x%x != 0x%x)", |
| 1440 | oat_file_->GetLocation().c_str(), dex_file_location.c_str(), |
| 1441 | oat_dex_file->GetDexFileLocationChecksum(), |
| 1442 | dex_file_location_checksum); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1443 | return false; |
| 1444 | } |
| 1445 | } |
| 1446 | return true; |
| 1447 | } |
| 1448 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1449 | const OatFile* ImageSpace::GetOatFile() const { |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 1450 | return oat_file_non_owned_; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1453 | std::unique_ptr<const OatFile> ImageSpace::ReleaseOatFile() { |
| 1454 | CHECK(oat_file_ != nullptr); |
| 1455 | return std::move(oat_file_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1458 | void ImageSpace::Dump(std::ostream& os) const { |
| 1459 | os << GetType() |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1460 | << " begin=" << reinterpret_cast<void*>(Begin()) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1461 | << ",end=" << reinterpret_cast<void*>(End()) |
| 1462 | << ",size=" << PrettySize(Size()) |
| 1463 | << ",name=\"" << GetName() << "\"]"; |
| 1464 | } |
| 1465 | |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 1466 | void ImageSpace::CreateMultiImageLocations(const std::string& input_image_file_name, |
| 1467 | const std::string& boot_classpath, |
| 1468 | std::vector<std::string>* image_file_names) { |
| 1469 | DCHECK(image_file_names != nullptr); |
| 1470 | |
| 1471 | std::vector<std::string> images; |
| 1472 | Split(boot_classpath, ':', &images); |
| 1473 | |
| 1474 | // Add the rest into the list. We have to adjust locations, possibly: |
| 1475 | // |
| 1476 | // For example, image_file_name is /a/b/c/d/e.art |
| 1477 | // images[0] is f/c/d/e.art |
| 1478 | // ---------------------------------------------- |
| 1479 | // images[1] is g/h/i/j.art -> /a/b/h/i/j.art |
| 1480 | |
| 1481 | // Derive pattern. |
| 1482 | std::vector<std::string> left; |
| 1483 | Split(input_image_file_name, '/', &left); |
| 1484 | std::vector<std::string> right; |
| 1485 | Split(images[0], '/', &right); |
| 1486 | |
| 1487 | size_t common = 1; |
| 1488 | while (common < left.size() && common < right.size()) { |
| 1489 | if (left[left.size() - common - 1] != right[right.size() - common - 1]) { |
| 1490 | break; |
| 1491 | } |
| 1492 | common++; |
| 1493 | } |
| 1494 | |
| 1495 | std::vector<std::string> prefix_vector(left.begin(), left.end() - common); |
| 1496 | std::string common_prefix = Join(prefix_vector, '/'); |
| 1497 | if (!common_prefix.empty() && common_prefix[0] != '/' && input_image_file_name[0] == '/') { |
| 1498 | common_prefix = "/" + common_prefix; |
| 1499 | } |
| 1500 | |
| 1501 | // Apply pattern to images[1] .. images[n]. |
| 1502 | for (size_t i = 1; i < images.size(); ++i) { |
| 1503 | std::string image = images[i]; |
| 1504 | |
| 1505 | size_t rslash = std::string::npos; |
| 1506 | for (size_t j = 0; j < common; ++j) { |
| 1507 | if (rslash != std::string::npos) { |
| 1508 | rslash--; |
| 1509 | } |
| 1510 | |
| 1511 | rslash = image.rfind('/', rslash); |
| 1512 | if (rslash == std::string::npos) { |
| 1513 | rslash = 0; |
| 1514 | } |
| 1515 | if (rslash == 0) { |
| 1516 | break; |
| 1517 | } |
| 1518 | } |
| 1519 | std::string image_part = image.substr(rslash); |
| 1520 | |
| 1521 | std::string new_image = common_prefix + (StartsWith(image_part, "/") ? "" : "/") + |
| 1522 | image_part; |
| 1523 | image_file_names->push_back(new_image); |
| 1524 | } |
| 1525 | } |
| 1526 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame^] | 1527 | ImageSpace* ImageSpace::CreateFromAppImage(const char* image, |
| 1528 | const OatFile* oat_file, |
| 1529 | std::string* error_msg) { |
| 1530 | return gc::space::ImageSpace::Init(image, |
| 1531 | image, |
| 1532 | /*validate_oat_file*/false, |
| 1533 | oat_file, |
| 1534 | /*out*/error_msg); |
| 1535 | } |
| 1536 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1537 | } // namespace space |
| 1538 | } // namespace gc |
| 1539 | } // namespace art |