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