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