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