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