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