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