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