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 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 19 | #include <lz4.h> |
| 20 | #include <random> |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 21 | #include <sys/statvfs.h> |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 23 | #include <unistd.h> |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 24 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "art_method.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 26 | #include "base/macros.h" |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 27 | #include "base/stl_util.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 28 | #include "base/scoped_flock.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 29 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 30 | #include "base/time_utils.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 31 | #include "gc/accounting/space_bitmap-inl.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 32 | #include "image-inl.h" |
Andreas Gampe | bec6358 | 2015-11-20 19:26:51 -0800 | [diff] [blame] | 33 | #include "image_space_fs.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 34 | #include "mirror/class-inl.h" |
| 35 | #include "mirror/object-inl.h" |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 36 | #include "oat_file.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 37 | #include "os.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 38 | #include "space-inl.h" |
| 39 | #include "utils.h" |
| 40 | |
| 41 | namespace art { |
| 42 | namespace gc { |
| 43 | namespace space { |
| 44 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 45 | Atomic<uint32_t> ImageSpace::bitmap_index_(0); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 46 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 47 | ImageSpace::ImageSpace(const std::string& image_filename, |
| 48 | const char* image_location, |
| 49 | MemMap* mem_map, |
| 50 | accounting::ContinuousSpaceBitmap* live_bitmap, |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 51 | uint8_t* end) |
| 52 | : MemMapSpace(image_filename, |
| 53 | mem_map, |
| 54 | mem_map->Begin(), |
| 55 | end, |
| 56 | end, |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 57 | kGcRetentionPolicyNeverCollect), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 58 | oat_file_non_owned_(nullptr), |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 59 | image_location_(image_location) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 60 | DCHECK(live_bitmap != nullptr); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 61 | live_bitmap_.reset(live_bitmap); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 64 | static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) { |
| 65 | CHECK_ALIGNED(min_delta, kPageSize); |
| 66 | CHECK_ALIGNED(max_delta, kPageSize); |
| 67 | CHECK_LT(min_delta, max_delta); |
| 68 | |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 69 | int32_t r = GetRandomNumber<int32_t>(min_delta, max_delta); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 70 | if (r % 2 == 0) { |
| 71 | r = RoundUp(r, kPageSize); |
| 72 | } else { |
| 73 | r = RoundDown(r, kPageSize); |
| 74 | } |
| 75 | CHECK_LE(min_delta, r); |
| 76 | CHECK_GE(max_delta, r); |
| 77 | CHECK_ALIGNED(r, kPageSize); |
| 78 | return r; |
| 79 | } |
| 80 | |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 81 | static bool GenerateImage(const std::string& image_filename, InstructionSet image_isa, |
| 82 | std::string* error_msg) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 83 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
| 84 | std::vector<std::string> boot_class_path; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 85 | Split(boot_class_path_string, ':', &boot_class_path); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 86 | if (boot_class_path.empty()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 87 | *error_msg = "Failed to generate image because no boot class path specified"; |
| 88 | return false; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 89 | } |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 90 | // We should clean up so we are more likely to have room for the image. |
| 91 | if (Runtime::Current()->IsZygote()) { |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 92 | LOG(INFO) << "Pruning dalvik-cache since we are generating an image and will need to recompile"; |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 93 | PruneDalvikCache(image_isa); |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 94 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 95 | |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 96 | std::vector<std::string> arg_vector; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 97 | |
Tsu Chiang Chuang | 12e6d74 | 2014-05-22 10:22:25 -0700 | [diff] [blame] | 98 | std::string dex2oat(Runtime::Current()->GetCompilerExecutable()); |
Mathieu Chartier | 08d7d44 | 2013-07-31 18:08:51 -0700 | [diff] [blame] | 99 | arg_vector.push_back(dex2oat); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 100 | |
| 101 | std::string image_option_string("--image="); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 102 | image_option_string += image_filename; |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 103 | arg_vector.push_back(image_option_string); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 104 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 105 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 106 | arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | std::string oat_file_option_string("--oat-file="); |
Brian Carlstrom | 2f1e15c | 2014-10-27 16:27:06 -0700 | [diff] [blame] | 110 | oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename); |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 111 | arg_vector.push_back(oat_file_option_string); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 112 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 113 | // Note: we do not generate a fully debuggable boot image so we do not pass the |
| 114 | // compiler flag --debuggable here. |
| 115 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 116 | Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector); |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 117 | CHECK_EQ(image_isa, kRuntimeISA) |
| 118 | << "We should always be generating an image for the current isa."; |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 119 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 120 | int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, |
| 121 | ART_BASE_ADDRESS_MAX_DELTA); |
| 122 | LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default " |
| 123 | << "art base address of 0x" << std::hex << ART_BASE_ADDRESS; |
| 124 | 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] | 125 | |
Brian Carlstrom | 57309db | 2014-07-30 15:13:25 -0700 | [diff] [blame] | 126 | if (!kIsTargetBuild) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 127 | arg_vector.push_back("--host"); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 130 | const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 131 | for (size_t i = 0; i < compiler_options.size(); ++i) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 132 | arg_vector.push_back(compiler_options[i].c_str()); |
| 133 | } |
| 134 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 135 | std::string command_line(Join(arg_vector, ' ')); |
| 136 | LOG(INFO) << "GenerateImage: " << command_line; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 137 | return Exec(arg_vector, error_msg); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 140 | bool ImageSpace::FindImageFilename(const char* image_location, |
| 141 | const InstructionSet image_isa, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 142 | std::string* system_filename, |
| 143 | bool* has_system, |
| 144 | std::string* cache_filename, |
| 145 | bool* dalvik_cache_exists, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 146 | bool* has_cache, |
| 147 | bool* is_global_cache) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 148 | *has_system = false; |
| 149 | *has_cache = false; |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 150 | // image_location = /system/framework/boot.art |
| 151 | // system_image_location = /system/framework/<image_isa>/boot.art |
| 152 | std::string system_image_filename(GetSystemImageFilename(image_location, image_isa)); |
| 153 | if (OS::FileExists(system_image_filename.c_str())) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 154 | *system_filename = system_image_filename; |
| 155 | *has_system = true; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 156 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 157 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 158 | bool have_android_data = false; |
| 159 | *dalvik_cache_exists = false; |
| 160 | std::string dalvik_cache; |
| 161 | GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 162 | &have_android_data, dalvik_cache_exists, is_global_cache); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 163 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 164 | if (have_android_data && *dalvik_cache_exists) { |
| 165 | // Always set output location even if it does not exist, |
| 166 | // so that the caller knows where to create the image. |
| 167 | // |
| 168 | // image_location = /system/framework/boot.art |
| 169 | // *image_filename = /data/dalvik-cache/<image_isa>/boot.art |
| 170 | std::string error_msg; |
| 171 | if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) { |
| 172 | LOG(WARNING) << error_msg; |
| 173 | return *has_system; |
| 174 | } |
| 175 | *has_cache = OS::FileExists(cache_filename->c_str()); |
| 176 | } |
| 177 | return *has_system || *has_cache; |
| 178 | } |
| 179 | |
| 180 | static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) { |
| 181 | std::unique_ptr<File> image_file(OS::OpenFileForReading(filename)); |
| 182 | if (image_file.get() == nullptr) { |
| 183 | return false; |
| 184 | } |
| 185 | const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader)); |
| 186 | if (!success || !image_header->IsValid()) { |
| 187 | return false; |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 192 | // Relocate the image at image_location to dest_filename and relocate it by a random amount. |
| 193 | static bool RelocateImage(const char* image_location, const char* dest_filename, |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 194 | InstructionSet isa, std::string* error_msg) { |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 195 | // We should clean up so we are more likely to have room for the image. |
| 196 | if (Runtime::Current()->IsZygote()) { |
| 197 | LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile"; |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 198 | PruneDalvikCache(isa); |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 201 | std::string patchoat(Runtime::Current()->GetPatchoatExecutable()); |
| 202 | |
| 203 | std::string input_image_location_arg("--input-image-location="); |
| 204 | input_image_location_arg += image_location; |
| 205 | |
| 206 | std::string output_image_filename_arg("--output-image-file="); |
| 207 | output_image_filename_arg += dest_filename; |
| 208 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 209 | std::string instruction_set_arg("--instruction-set="); |
| 210 | instruction_set_arg += GetInstructionSetString(isa); |
| 211 | |
| 212 | std::string base_offset_arg("--base-offset-delta="); |
| 213 | StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, |
| 214 | ART_BASE_ADDRESS_MAX_DELTA)); |
| 215 | |
| 216 | std::vector<std::string> argv; |
| 217 | argv.push_back(patchoat); |
| 218 | |
| 219 | argv.push_back(input_image_location_arg); |
| 220 | argv.push_back(output_image_filename_arg); |
| 221 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 222 | argv.push_back(instruction_set_arg); |
| 223 | argv.push_back(base_offset_arg); |
| 224 | |
| 225 | std::string command_line(Join(argv, ' ')); |
| 226 | LOG(INFO) << "RelocateImage: " << command_line; |
| 227 | return Exec(argv, error_msg); |
| 228 | } |
| 229 | |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 230 | static ImageHeader* ReadSpecificImageHeader(const char* filename, std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 231 | std::unique_ptr<ImageHeader> hdr(new ImageHeader); |
| 232 | if (!ReadSpecificImageHeader(filename, hdr.get())) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 233 | *error_msg = StringPrintf("Unable to read image header for %s", filename); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 234 | return nullptr; |
| 235 | } |
| 236 | return hdr.release(); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location, |
| 240 | const InstructionSet image_isa) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 241 | std::string error_msg; |
| 242 | ImageHeader* image_header = ReadImageHeader(image_location, image_isa, &error_msg); |
| 243 | if (image_header == nullptr) { |
| 244 | LOG(FATAL) << error_msg; |
| 245 | } |
| 246 | return image_header; |
| 247 | } |
| 248 | |
| 249 | ImageHeader* ImageSpace::ReadImageHeader(const char* image_location, |
| 250 | const InstructionSet image_isa, |
| 251 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 252 | std::string system_filename; |
| 253 | bool has_system = false; |
| 254 | std::string cache_filename; |
| 255 | bool has_cache = false; |
| 256 | bool dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 257 | bool is_global_cache = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 258 | if (FindImageFilename(image_location, image_isa, &system_filename, &has_system, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 259 | &cache_filename, &dalvik_cache_exists, &has_cache, &is_global_cache)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 260 | if (Runtime::Current()->ShouldRelocate()) { |
| 261 | if (has_system && has_cache) { |
| 262 | std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader); |
| 263 | std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader); |
| 264 | if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 265 | *error_msg = StringPrintf("Unable to read image header for %s at %s", |
| 266 | image_location, system_filename.c_str()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 267 | return nullptr; |
| 268 | } |
| 269 | if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 270 | *error_msg = StringPrintf("Unable to read image header for %s at %s", |
| 271 | image_location, cache_filename.c_str()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 272 | return nullptr; |
| 273 | } |
| 274 | if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 275 | *error_msg = StringPrintf("Unable to find a relocated version of image file %s", |
| 276 | image_location); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 277 | return nullptr; |
| 278 | } |
| 279 | return cache_hdr.release(); |
| 280 | } else if (!has_cache) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 281 | *error_msg = StringPrintf("Unable to find a relocated version of image file %s", |
| 282 | image_location); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 283 | return nullptr; |
| 284 | } else if (!has_system && has_cache) { |
| 285 | // This can probably just use the cache one. |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 286 | return ReadSpecificImageHeader(cache_filename.c_str(), error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 287 | } |
| 288 | } else { |
| 289 | // We don't want to relocate, Just pick the appropriate one if we have it and return. |
| 290 | if (has_system && has_cache) { |
| 291 | // We want the cache if the checksum matches, otherwise the system. |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 292 | std::unique_ptr<ImageHeader> system(ReadSpecificImageHeader(system_filename.c_str(), |
| 293 | error_msg)); |
| 294 | std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeader(cache_filename.c_str(), |
| 295 | error_msg)); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 296 | if (system.get() == nullptr || |
| 297 | (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) { |
| 298 | return cache.release(); |
| 299 | } else { |
| 300 | return system.release(); |
| 301 | } |
| 302 | } else if (has_system) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 303 | return ReadSpecificImageHeader(system_filename.c_str(), error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 304 | } else if (has_cache) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 305 | return ReadSpecificImageHeader(cache_filename.c_str(), error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 306 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 307 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 310 | *error_msg = StringPrintf("Unable to find image file for %s", image_location); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 311 | return nullptr; |
| 312 | } |
| 313 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 314 | static bool ChecksumsMatch(const char* image_a, const char* image_b) { |
| 315 | ImageHeader hdr_a; |
| 316 | ImageHeader hdr_b; |
| 317 | return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b) |
| 318 | && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum(); |
| 319 | } |
| 320 | |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 321 | static bool ImageCreationAllowed(bool is_global_cache, std::string* error_msg) { |
| 322 | // Anyone can write into a "local" cache. |
| 323 | if (!is_global_cache) { |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | // Only the zygote is allowed to create the global boot image. |
| 328 | if (Runtime::Current()->IsZygote()) { |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | *error_msg = "Only the zygote can create the global boot image."; |
| 333 | return false; |
| 334 | } |
| 335 | |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 336 | static constexpr uint64_t kLowSpaceValue = 50 * MB; |
| 337 | static constexpr uint64_t kTmpFsSentinelValue = 384 * MB; |
| 338 | |
| 339 | // Read the free space of the cache partition and make a decision whether to keep the generated |
| 340 | // image. This is to try to mitigate situations where the system might run out of space later. |
| 341 | static bool CheckSpace(const std::string& cache_filename, std::string* error_msg) { |
| 342 | // Using statvfs vs statvfs64 because of b/18207376, and it is enough for all practical purposes. |
| 343 | struct statvfs buf; |
| 344 | |
| 345 | int res = TEMP_FAILURE_RETRY(statvfs(cache_filename.c_str(), &buf)); |
| 346 | if (res != 0) { |
| 347 | // Could not stat. Conservatively tell the system to delete the image. |
| 348 | *error_msg = "Could not stat the filesystem, assuming low-memory situation."; |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | uint64_t fs_overall_size = buf.f_bsize * static_cast<uint64_t>(buf.f_blocks); |
| 353 | // Zygote is privileged, but other things are not. Use bavail. |
| 354 | uint64_t fs_free_size = buf.f_bsize * static_cast<uint64_t>(buf.f_bavail); |
| 355 | |
| 356 | // Take the overall size as an indicator for a tmpfs, which is being used for the decryption |
| 357 | // environment. We do not want to fail quickening the boot image there, as it is beneficial |
| 358 | // for time-to-UI. |
| 359 | if (fs_overall_size > kTmpFsSentinelValue) { |
| 360 | if (fs_free_size < kLowSpaceValue) { |
| 361 | *error_msg = StringPrintf("Low-memory situation: only %4.2f megabytes available after image" |
| 362 | " generation, need at least %" PRIu64 ".", |
| 363 | static_cast<double>(fs_free_size) / MB, |
| 364 | kLowSpaceValue / MB); |
| 365 | return false; |
| 366 | } |
| 367 | } |
| 368 | return true; |
| 369 | } |
| 370 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 371 | ImageSpace* ImageSpace::CreateBootImage(const char* image_location, |
| 372 | const InstructionSet image_isa, |
| 373 | bool secondary_image, |
| 374 | std::string* error_msg) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 375 | ScopedTrace trace(__FUNCTION__); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 376 | std::string system_filename; |
| 377 | bool has_system = false; |
| 378 | std::string cache_filename; |
| 379 | bool has_cache = false; |
| 380 | bool dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 381 | bool is_global_cache = true; |
Andreas Gampe | bec6358 | 2015-11-20 19:26:51 -0800 | [diff] [blame] | 382 | bool found_image = FindImageFilename(image_location, image_isa, &system_filename, |
| 383 | &has_system, &cache_filename, &dalvik_cache_exists, |
| 384 | &has_cache, &is_global_cache); |
| 385 | |
| 386 | // If we're starting with the global cache, and we're the zygote, try to see whether there are |
| 387 | // OTA artifacts from the A/B OTA preopting to move over. |
| 388 | // (It is structurally simpler to check this here, instead of complicating the compile/relocate |
| 389 | // logic below.) |
| 390 | const bool is_zygote = Runtime::Current()->IsZygote(); |
| 391 | if (is_global_cache && is_zygote) { |
| 392 | VLOG(startup) << "Checking for A/B OTA data."; |
| 393 | TryMoveOTAArtifacts(cache_filename, dalvik_cache_exists); |
| 394 | |
| 395 | // Retry. There are two cases where the old info is outdated: |
| 396 | // * There wasn't a boot image before (e.g., some failure on boot), but now the OTA preopted |
| 397 | // image has been moved in-place. |
| 398 | // * There was a boot image before, and we tried to move the OTA preopted image, but a failure |
| 399 | // happened and there is no file anymore. |
| 400 | found_image = FindImageFilename(image_location, |
| 401 | image_isa, |
| 402 | &system_filename, |
| 403 | &has_system, |
| 404 | &cache_filename, |
| 405 | &dalvik_cache_exists, |
| 406 | &has_cache, |
| 407 | &is_global_cache); |
| 408 | } |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 409 | |
Andreas Gampe | acc1be3 | 2016-04-05 10:26:42 -0700 | [diff] [blame] | 410 | if (is_zygote && !secondary_image) { |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 411 | MarkZygoteStart(image_isa, Runtime::Current()->GetZygoteMaxFailedBoots()); |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 414 | ImageSpace* space; |
| 415 | bool relocate = Runtime::Current()->ShouldRelocate(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 416 | bool can_compile = Runtime::Current()->IsImageDex2OatEnabled(); |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 417 | if (found_image) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 418 | const std::string* image_filename; |
| 419 | bool is_system = false; |
| 420 | bool relocated_version_used = false; |
| 421 | if (relocate) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 422 | if (!dalvik_cache_exists) { |
| 423 | *error_msg = StringPrintf("Requiring relocation for image '%s' at '%s' but we do not have " |
| 424 | "any dalvik_cache to find/place it in.", |
| 425 | image_location, system_filename.c_str()); |
| 426 | return nullptr; |
| 427 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 428 | if (has_system) { |
| 429 | if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) { |
| 430 | // We already have a relocated version |
| 431 | image_filename = &cache_filename; |
| 432 | relocated_version_used = true; |
| 433 | } else { |
| 434 | // 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] | 435 | |
| 436 | std::string reason; |
| 437 | bool success; |
| 438 | |
| 439 | // Check whether we are allowed to relocate. |
| 440 | if (!can_compile) { |
| 441 | reason = "Image dex2oat disabled by -Xnoimage-dex2oat."; |
| 442 | success = false; |
| 443 | } else if (!ImageCreationAllowed(is_global_cache, &reason)) { |
| 444 | // Whether we can write to the cache. |
| 445 | success = false; |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 446 | } else if (secondary_image) { |
Andreas Gampe | acc1be3 | 2016-04-05 10:26:42 -0700 | [diff] [blame] | 447 | if (is_zygote) { |
Jeff Hao | ab4a4d2 | 2016-03-14 18:50:49 -0700 | [diff] [blame] | 448 | // Secondary image is out of date. Clear cache and exit to let it retry from scratch. |
| 449 | LOG(ERROR) << "Cannot patch secondary image '" << image_location |
| 450 | << "', clearing dalvik_cache and restarting zygote."; |
| 451 | PruneDalvikCache(image_isa); |
| 452 | _exit(1); |
| 453 | } else { |
| 454 | reason = "Should not have to patch secondary image."; |
| 455 | success = false; |
| 456 | } |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 457 | } else { |
| 458 | // Try to relocate. |
| 459 | success = RelocateImage(image_location, cache_filename.c_str(), image_isa, &reason); |
| 460 | } |
| 461 | |
| 462 | if (success) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 463 | relocated_version_used = true; |
| 464 | image_filename = &cache_filename; |
| 465 | } else { |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 466 | *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] | 467 | image_location, system_filename.c_str(), |
| 468 | cache_filename.c_str(), reason.c_str()); |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 469 | // We failed to create files, remove any possibly garbage output. |
| 470 | // Since ImageCreationAllowed was true above, we are the zygote |
| 471 | // and therefore the only process expected to generate these for |
| 472 | // the device. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 473 | PruneDalvikCache(image_isa); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 474 | return nullptr; |
| 475 | } |
| 476 | } |
| 477 | } else { |
| 478 | CHECK(has_cache); |
| 479 | // We can just use cache's since it should be fine. This might or might not be relocated. |
| 480 | image_filename = &cache_filename; |
| 481 | } |
| 482 | } else { |
| 483 | if (has_system && has_cache) { |
| 484 | // Check they have the same cksum. If they do use the cache. Otherwise system. |
| 485 | if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) { |
| 486 | image_filename = &cache_filename; |
| 487 | relocated_version_used = true; |
| 488 | } else { |
| 489 | image_filename = &system_filename; |
Alex Light | 1a76213 | 2014-07-31 09:32:13 -0700 | [diff] [blame] | 490 | is_system = true; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 491 | } |
| 492 | } else if (has_system) { |
| 493 | image_filename = &system_filename; |
Alex Light | 1a76213 | 2014-07-31 09:32:13 -0700 | [diff] [blame] | 494 | is_system = true; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 495 | } else { |
| 496 | CHECK(has_cache); |
| 497 | image_filename = &cache_filename; |
| 498 | } |
| 499 | } |
| 500 | { |
| 501 | // Note that we must not use the file descriptor associated with |
| 502 | // ScopedFlock::GetFile to Init the image file. We want the file |
| 503 | // descriptor (and the associated exclusive lock) to be released when |
| 504 | // we leave Create. |
| 505 | ScopedFlock image_lock; |
Andreas Gampe | acc1be3 | 2016-04-05 10:26:42 -0700 | [diff] [blame] | 506 | // Should this be a RDWR lock? This is only a defensive measure, as at |
| 507 | // this point the image should exist. |
| 508 | // However, only the zygote can write into the global dalvik-cache, so |
| 509 | // restrict to zygote processes, or any process that isn't using |
| 510 | // /data/dalvik-cache (which we assume to be allowed to write there). |
| 511 | const bool rw_lock = is_zygote || !is_global_cache; |
| 512 | image_lock.Init(image_filename->c_str(), |
| 513 | rw_lock ? (O_CREAT | O_RDWR) : O_RDONLY /* flags */, |
| 514 | true /* block */, |
| 515 | error_msg); |
Alex Light | b6cabc1 | 2014-08-21 09:45:00 -0700 | [diff] [blame] | 516 | VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location " |
| 517 | << image_location; |
Alex Light | b93637a | 2014-07-31 10:48:46 -0700 | [diff] [blame] | 518 | // If we are in /system we can assume the image is good. We can also |
| 519 | // assume this if we are using a relocated image (i.e. image checksum |
| 520 | // matches) since this is only different by the offset. We need this to |
| 521 | // make sure that host tests continue to work. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 522 | // Since we are the boot image, pass null since we load the oat file from the boot image oat |
| 523 | // file name. |
| 524 | space = ImageSpace::Init(image_filename->c_str(), |
| 525 | image_location, |
| 526 | !(is_system || relocated_version_used), |
| 527 | /* oat_file */nullptr, |
| 528 | error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 529 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 530 | if (space != nullptr) { |
| 531 | return space; |
| 532 | } |
| 533 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 534 | if (relocated_version_used) { |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 535 | // Something is wrong with the relocated copy (even though checksums match). Cleanup. |
| 536 | // This can happen if the .oat is corrupt, since the above only checks the .art checksums. |
| 537 | // TODO: Check the oat file validity earlier. |
| 538 | *error_msg = StringPrintf("Attempted to use relocated version of %s at %s generated from %s " |
| 539 | "but image failed to load: %s", |
| 540 | image_location, cache_filename.c_str(), system_filename.c_str(), |
| 541 | error_msg->c_str()); |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 542 | PruneDalvikCache(image_isa); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 543 | return nullptr; |
| 544 | } else if (is_system) { |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 545 | // If the /system file exists, it should be up-to-date, don't try to generate it. |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 546 | *error_msg = StringPrintf("Failed to load /system image '%s': %s", |
| 547 | image_filename->c_str(), error_msg->c_str()); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 548 | return nullptr; |
Mathieu Chartier | c7cb190 | 2014-03-05 14:41:03 -0800 | [diff] [blame] | 549 | } else { |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 550 | // Otherwise, log a warning and fall through to GenerateImage. |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 551 | LOG(WARNING) << *error_msg; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 554 | |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 555 | if (!can_compile) { |
| 556 | *error_msg = "Not attempting to compile image because -Xnoimage-dex2oat"; |
| 557 | return nullptr; |
| 558 | } else if (!dalvik_cache_exists) { |
| 559 | *error_msg = StringPrintf("No place to put generated image."); |
| 560 | return nullptr; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 561 | } else if (!ImageCreationAllowed(is_global_cache, error_msg)) { |
| 562 | return nullptr; |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 563 | } else if (secondary_image) { |
| 564 | *error_msg = "Cannot compile a secondary image."; |
| 565 | return nullptr; |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 566 | } else if (!GenerateImage(cache_filename, image_isa, error_msg)) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 567 | *error_msg = StringPrintf("Failed to generate image '%s': %s", |
| 568 | cache_filename.c_str(), error_msg->c_str()); |
Brian Carlstrom | e9105f7 | 2014-10-28 15:53:43 -0700 | [diff] [blame] | 569 | // We failed to create files, remove any possibly garbage output. |
| 570 | // Since ImageCreationAllowed was true above, we are the zygote |
| 571 | // and therefore the only process expected to generate these for |
| 572 | // the device. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 573 | PruneDalvikCache(image_isa); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 574 | return nullptr; |
| 575 | } else { |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 576 | // Check whether there is enough space left over after we have generated the image. |
| 577 | if (!CheckSpace(cache_filename, error_msg)) { |
| 578 | // No. Delete the generated image and try to run out of the dex files. |
Narayan Kamath | 28bc987 | 2014-11-07 17:46:28 +0000 | [diff] [blame] | 579 | PruneDalvikCache(image_isa); |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 580 | return nullptr; |
| 581 | } |
| 582 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 583 | // Note that we must not use the file descriptor associated with |
| 584 | // ScopedFlock::GetFile to Init the image file. We want the file |
| 585 | // descriptor (and the associated exclusive lock) to be released when |
| 586 | // we leave Create. |
| 587 | ScopedFlock image_lock; |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 588 | image_lock.Init(cache_filename.c_str(), error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 589 | space = ImageSpace::Init(cache_filename.c_str(), image_location, true, nullptr, error_msg); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 590 | if (space == nullptr) { |
| 591 | *error_msg = StringPrintf("Failed to load generated image '%s': %s", |
| 592 | cache_filename.c_str(), error_msg->c_str()); |
| 593 | } |
| 594 | return space; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 595 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 598 | void ImageSpace::VerifyImageAllocations() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 599 | uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 600 | while (current < End()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 601 | CHECK_ALIGNED(current, kObjectAlignment); |
| 602 | auto* obj = reinterpret_cast<mirror::Object*>(current); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 603 | CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class"; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 604 | CHECK(live_bitmap_->Test(obj)) << PrettyTypeOf(obj); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 605 | if (kUseBakerOrBrooksReadBarrier) { |
| 606 | obj->AssertReadBarrierPointer(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 607 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 608 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
| 609 | } |
| 610 | } |
| 611 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 612 | // Helper class for relocating from one range of memory to another. |
| 613 | class RelocationRange { |
| 614 | public: |
| 615 | RelocationRange() = default; |
| 616 | RelocationRange(const RelocationRange&) = default; |
| 617 | RelocationRange(uintptr_t source, uintptr_t dest, uintptr_t length) |
| 618 | : source_(source), |
| 619 | dest_(dest), |
| 620 | length_(length) {} |
| 621 | |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 622 | bool InSource(uintptr_t address) const { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 623 | return address - source_ < length_; |
| 624 | } |
| 625 | |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 626 | bool InDest(uintptr_t address) const { |
| 627 | return address - dest_ < length_; |
| 628 | } |
| 629 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 630 | // Translate a source address to the destination space. |
| 631 | uintptr_t ToDest(uintptr_t address) const { |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 632 | DCHECK(InSource(address)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 633 | return address + Delta(); |
| 634 | } |
| 635 | |
| 636 | // Returns the delta between the dest from the source. |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 637 | uintptr_t Delta() const { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 638 | return dest_ - source_; |
| 639 | } |
| 640 | |
| 641 | uintptr_t Source() const { |
| 642 | return source_; |
| 643 | } |
| 644 | |
| 645 | uintptr_t Dest() const { |
| 646 | return dest_; |
| 647 | } |
| 648 | |
| 649 | uintptr_t Length() const { |
| 650 | return length_; |
| 651 | } |
| 652 | |
| 653 | private: |
| 654 | const uintptr_t source_; |
| 655 | const uintptr_t dest_; |
| 656 | const uintptr_t length_; |
| 657 | }; |
| 658 | |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 659 | std::ostream& operator<<(std::ostream& os, const RelocationRange& reloc) { |
| 660 | return os << "(" << reinterpret_cast<const void*>(reloc.Source()) << "-" |
| 661 | << reinterpret_cast<const void*>(reloc.Source() + reloc.Length()) << ")->(" |
| 662 | << reinterpret_cast<const void*>(reloc.Dest()) << "-" |
| 663 | << reinterpret_cast<const void*>(reloc.Dest() + reloc.Length()) << ")"; |
| 664 | } |
| 665 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 666 | class FixupVisitor : public ValueObject { |
| 667 | public: |
| 668 | FixupVisitor(const RelocationRange& boot_image, |
| 669 | const RelocationRange& boot_oat, |
| 670 | const RelocationRange& app_image, |
| 671 | const RelocationRange& app_oat) |
| 672 | : boot_image_(boot_image), |
| 673 | boot_oat_(boot_oat), |
| 674 | app_image_(app_image), |
| 675 | app_oat_(app_oat) {} |
| 676 | |
| 677 | // Return the relocated address of a heap object. |
| 678 | template <typename T> |
| 679 | ALWAYS_INLINE T* ForwardObject(T* src) const { |
| 680 | const uintptr_t uint_src = reinterpret_cast<uintptr_t>(src); |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 681 | if (boot_image_.InSource(uint_src)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 682 | return reinterpret_cast<T*>(boot_image_.ToDest(uint_src)); |
| 683 | } |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 684 | if (app_image_.InSource(uint_src)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 685 | return reinterpret_cast<T*>(app_image_.ToDest(uint_src)); |
| 686 | } |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 687 | // Since we are fixing up the app image, there should only be pointers to the app image and |
| 688 | // boot image. |
| 689 | DCHECK(src == nullptr) << reinterpret_cast<const void*>(src); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 690 | return src; |
| 691 | } |
| 692 | |
| 693 | // Return the relocated address of a code pointer (contained by an oat file). |
| 694 | ALWAYS_INLINE const void* ForwardCode(const void* src) const { |
| 695 | const uintptr_t uint_src = reinterpret_cast<uintptr_t>(src); |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 696 | if (boot_oat_.InSource(uint_src)) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 697 | return reinterpret_cast<const void*>(boot_oat_.ToDest(uint_src)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 698 | } |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 699 | if (app_oat_.InSource(uint_src)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 700 | return reinterpret_cast<const void*>(app_oat_.ToDest(uint_src)); |
| 701 | } |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 702 | DCHECK(src == nullptr) << src; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 703 | return src; |
| 704 | } |
| 705 | |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 706 | // Must be called on pointers that already have been relocated to the destination relocation. |
| 707 | ALWAYS_INLINE bool IsInAppImage(mirror::Object* object) const { |
| 708 | return app_image_.InDest(reinterpret_cast<uintptr_t>(object)); |
| 709 | } |
| 710 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 711 | protected: |
| 712 | // Source section. |
| 713 | const RelocationRange boot_image_; |
| 714 | const RelocationRange boot_oat_; |
| 715 | const RelocationRange app_image_; |
| 716 | const RelocationRange app_oat_; |
| 717 | }; |
| 718 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 719 | // Adapt for mirror::Class::FixupNativePointers. |
| 720 | class FixupObjectAdapter : public FixupVisitor { |
| 721 | public: |
| 722 | template<typename... Args> |
| 723 | explicit FixupObjectAdapter(Args... args) : FixupVisitor(args...) {} |
| 724 | |
| 725 | template <typename T> |
| 726 | T* operator()(T* obj) const { |
| 727 | return ForwardObject(obj); |
| 728 | } |
| 729 | }; |
| 730 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 731 | class FixupRootVisitor : public FixupVisitor { |
| 732 | public: |
| 733 | template<typename... Args> |
| 734 | explicit FixupRootVisitor(Args... args) : FixupVisitor(args...) {} |
| 735 | |
| 736 | ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 737 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 738 | if (!root->IsNull()) { |
| 739 | VisitRoot(root); |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 744 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 745 | mirror::Object* ref = root->AsMirrorPtr(); |
| 746 | mirror::Object* new_ref = ForwardObject(ref); |
| 747 | if (ref != new_ref) { |
| 748 | root->Assign(new_ref); |
| 749 | } |
| 750 | } |
| 751 | }; |
| 752 | |
| 753 | class FixupObjectVisitor : public FixupVisitor { |
| 754 | public: |
| 755 | template<typename... Args> |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 756 | explicit FixupObjectVisitor(gc::accounting::ContinuousSpaceBitmap* visited, |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 757 | const size_t pointer_size, |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 758 | Args... args) |
| 759 | : FixupVisitor(args...), |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 760 | pointer_size_(pointer_size), |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 761 | visited_(visited) {} |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 762 | |
| 763 | // Fix up separately since we also need to fix up method entrypoints. |
| 764 | ALWAYS_INLINE void VisitRootIfNonNull( |
| 765 | mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 766 | |
| 767 | ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 768 | const {} |
| 769 | |
| 770 | ALWAYS_INLINE void operator()(mirror::Object* obj, |
| 771 | MemberOffset offset, |
| 772 | bool is_static ATTRIBUTE_UNUSED) const |
| 773 | NO_THREAD_SAFETY_ANALYSIS { |
| 774 | // There could be overlap between ranges, we must avoid visiting the same reference twice. |
| 775 | // Avoid the class field since we already fixed it up in FixupClassVisitor. |
| 776 | if (offset.Uint32Value() != mirror::Object::ClassOffset().Uint32Value()) { |
| 777 | // Space is not yet added to the heap, don't do a read barrier. |
| 778 | mirror::Object* ref = obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>( |
| 779 | offset); |
| 780 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 781 | // image. |
| 782 | obj->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(offset, ForwardObject(ref)); |
| 783 | } |
| 784 | } |
| 785 | |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 786 | // Visit a pointer array and forward corresponding native data. Ignores pointer arrays in the |
| 787 | // boot image. Uses the bitmap to ensure the same array is not visited multiple times. |
| 788 | template <typename Visitor> |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 789 | void UpdatePointerArrayContents(mirror::PointerArray* array, const Visitor& visitor) const |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 790 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 791 | DCHECK(array != nullptr); |
| 792 | DCHECK(visitor.IsInAppImage(array)); |
| 793 | // The bit for the array contents is different than the bit for the array. Since we may have |
| 794 | // already visited the array as a long / int array from walking the bitmap without knowing it |
| 795 | // was a pointer array. |
| 796 | static_assert(kObjectAlignment == 8u, "array bit may be in another object"); |
| 797 | mirror::Object* const contents_bit = reinterpret_cast<mirror::Object*>( |
| 798 | reinterpret_cast<uintptr_t>(array) + kObjectAlignment); |
| 799 | // If the bit is not set then the contents have not yet been updated. |
| 800 | if (!visited_->Test(contents_bit)) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 801 | array->Fixup<kVerifyNone, kWithoutReadBarrier>(array, pointer_size_, visitor); |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 802 | visited_->Set(contents_bit); |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 806 | // java.lang.ref.Reference visitor. |
| 807 | void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, mirror::Reference* ref) const |
| 808 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
| 809 | mirror::Object* obj = ref->GetReferent<kWithoutReadBarrier>(); |
| 810 | ref->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
| 811 | mirror::Reference::ReferentOffset(), |
| 812 | ForwardObject(obj)); |
| 813 | } |
| 814 | |
Goran Jakovljevic | 0dfb30d | 2016-04-12 13:11:16 +0200 | [diff] [blame] | 815 | void operator()(mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 816 | if (visited_->Test(obj)) { |
| 817 | // Already visited. |
| 818 | return; |
| 819 | } |
| 820 | visited_->Set(obj); |
| 821 | |
| 822 | // Handle class specially first since we need it to be updated to properly visit the rest of |
| 823 | // the instance fields. |
| 824 | { |
| 825 | mirror::Class* klass = obj->GetClass<kVerifyNone, kWithoutReadBarrier>(); |
| 826 | DCHECK(klass != nullptr) << "Null class in image"; |
| 827 | // No AsClass since our fields aren't quite fixed up yet. |
| 828 | mirror::Class* new_klass = down_cast<mirror::Class*>(ForwardObject(klass)); |
| 829 | if (klass != new_klass) { |
| 830 | obj->SetClass<kVerifyNone>(new_klass); |
| 831 | } |
| 832 | if (new_klass != klass && IsInAppImage(new_klass)) { |
| 833 | // Make sure the klass contents are fixed up since we depend on it to walk the fields. |
| 834 | operator()(new_klass); |
| 835 | } |
| 836 | } |
| 837 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 838 | obj->VisitReferences</*visit native roots*/false, kVerifyNone, kWithoutReadBarrier>( |
| 839 | *this, |
| 840 | *this); |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 841 | // Note that this code relies on no circular dependencies. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 842 | // We want to use our own class loader and not the one in the image. |
| 843 | if (obj->IsClass<kVerifyNone, kWithoutReadBarrier>()) { |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 844 | mirror::Class* as_klass = obj->AsClass<kVerifyNone, kWithoutReadBarrier>(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 845 | FixupObjectAdapter visitor(boot_image_, boot_oat_, app_image_, app_oat_); |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 846 | as_klass->FixupNativePointers<kVerifyNone, kWithoutReadBarrier>(as_klass, |
| 847 | pointer_size_, |
| 848 | visitor); |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 849 | // Deal with the pointer arrays. Use the helper function since multiple classes can reference |
| 850 | // the same arrays. |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 851 | mirror::PointerArray* const vtable = as_klass->GetVTable<kVerifyNone, kWithoutReadBarrier>(); |
| 852 | if (vtable != nullptr && IsInAppImage(vtable)) { |
| 853 | operator()(vtable); |
| 854 | UpdatePointerArrayContents(vtable, visitor); |
| 855 | } |
| 856 | mirror::IfTable* iftable = as_klass->GetIfTable<kVerifyNone, kWithoutReadBarrier>(); |
| 857 | // Ensure iftable arrays are fixed up since we need GetMethodArray to return the valid |
| 858 | // contents. |
| 859 | if (iftable != nullptr && IsInAppImage(iftable)) { |
| 860 | operator()(iftable); |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 861 | for (int32_t i = 0, count = iftable->Count(); i < count; ++i) { |
| 862 | if (iftable->GetMethodArrayCount<kVerifyNone, kWithoutReadBarrier>(i) > 0) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 863 | mirror::PointerArray* methods = |
| 864 | iftable->GetMethodArray<kVerifyNone, kWithoutReadBarrier>(i); |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 865 | if (visitor.IsInAppImage(methods)) { |
| 866 | operator()(methods); |
| 867 | DCHECK(methods != nullptr); |
| 868 | UpdatePointerArrayContents(methods, visitor); |
| 869 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | } |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 875 | |
| 876 | private: |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 877 | const size_t pointer_size_; |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 878 | gc::accounting::ContinuousSpaceBitmap* const visited_; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 879 | }; |
| 880 | |
| 881 | class ForwardObjectAdapter { |
| 882 | public: |
Chih-Hung Hsieh | 471118e | 2016-04-29 14:27:41 -0700 | [diff] [blame] | 883 | ALWAYS_INLINE explicit ForwardObjectAdapter(const FixupVisitor* visitor) : visitor_(visitor) {} |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 884 | |
| 885 | template <typename T> |
| 886 | ALWAYS_INLINE T* operator()(T* src) const { |
| 887 | return visitor_->ForwardObject(src); |
| 888 | } |
| 889 | |
| 890 | private: |
| 891 | const FixupVisitor* const visitor_; |
| 892 | }; |
| 893 | |
| 894 | class ForwardCodeAdapter { |
| 895 | public: |
Chih-Hung Hsieh | 471118e | 2016-04-29 14:27:41 -0700 | [diff] [blame] | 896 | ALWAYS_INLINE explicit ForwardCodeAdapter(const FixupVisitor* visitor) |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 897 | : visitor_(visitor) {} |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 898 | |
| 899 | template <typename T> |
| 900 | ALWAYS_INLINE T* operator()(T* src) const { |
| 901 | return visitor_->ForwardCode(src); |
| 902 | } |
| 903 | |
| 904 | private: |
| 905 | const FixupVisitor* const visitor_; |
| 906 | }; |
| 907 | |
| 908 | class FixupArtMethodVisitor : public FixupVisitor, public ArtMethodVisitor { |
| 909 | public: |
| 910 | template<typename... Args> |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 911 | explicit FixupArtMethodVisitor(bool fixup_heap_objects, size_t pointer_size, Args... args) |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 912 | : FixupVisitor(args...), |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 913 | fixup_heap_objects_(fixup_heap_objects), |
| 914 | pointer_size_(pointer_size) {} |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 915 | |
| 916 | virtual void Visit(ArtMethod* method) NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 917 | // TODO: Separate visitor for runtime vs normal methods. |
| 918 | if (UNLIKELY(method->IsRuntimeMethod())) { |
| 919 | ImtConflictTable* table = method->GetImtConflictTable(pointer_size_); |
| 920 | if (table != nullptr) { |
| 921 | ImtConflictTable* new_table = ForwardObject(table); |
| 922 | if (table != new_table) { |
| 923 | method->SetImtConflictTable(new_table, pointer_size_); |
| 924 | } |
| 925 | } |
| 926 | const void* old_code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size_); |
| 927 | const void* new_code = ForwardCode(old_code); |
| 928 | if (old_code != new_code) { |
| 929 | method->SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size_); |
| 930 | } |
| 931 | } else { |
| 932 | if (fixup_heap_objects_) { |
| 933 | method->UpdateObjectsForImageRelocation(ForwardObjectAdapter(this), pointer_size_); |
| 934 | } |
| 935 | method->UpdateEntrypoints<kWithoutReadBarrier>(ForwardCodeAdapter(this), pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 936 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | private: |
| 940 | const bool fixup_heap_objects_; |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 941 | const size_t pointer_size_; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 942 | }; |
| 943 | |
| 944 | class FixupArtFieldVisitor : public FixupVisitor, public ArtFieldVisitor { |
| 945 | public: |
| 946 | template<typename... Args> |
| 947 | explicit FixupArtFieldVisitor(Args... args) : FixupVisitor(args...) {} |
| 948 | |
| 949 | virtual void Visit(ArtField* field) NO_THREAD_SAFETY_ANALYSIS { |
| 950 | field->UpdateObjects(ForwardObjectAdapter(this)); |
| 951 | } |
| 952 | }; |
| 953 | |
| 954 | // Relocate an image space mapped at target_base which possibly used to be at a different base |
| 955 | // address. Only needs a single image space, not one for both source and destination. |
| 956 | // In place means modifying a single ImageSpace in place rather than relocating from one ImageSpace |
| 957 | // to another. |
| 958 | static bool RelocateInPlace(ImageHeader& image_header, |
| 959 | uint8_t* target_base, |
| 960 | accounting::ContinuousSpaceBitmap* bitmap, |
| 961 | const OatFile* app_oat_file, |
| 962 | std::string* error_msg) { |
| 963 | DCHECK(error_msg != nullptr); |
| 964 | if (!image_header.IsPic()) { |
| 965 | if (image_header.GetImageBegin() == target_base) { |
| 966 | return true; |
| 967 | } |
| 968 | *error_msg = StringPrintf("Cannot relocate non-pic image for oat file %s", |
| 969 | (app_oat_file != nullptr) ? app_oat_file->GetLocation().c_str() : ""); |
| 970 | return false; |
| 971 | } |
| 972 | // Set up sections. |
| 973 | uint32_t boot_image_begin = 0; |
| 974 | uint32_t boot_image_end = 0; |
| 975 | uint32_t boot_oat_begin = 0; |
| 976 | uint32_t boot_oat_end = 0; |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 977 | const size_t pointer_size = image_header.GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 978 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 979 | heap->GetBootImagesSize(&boot_image_begin, &boot_image_end, &boot_oat_begin, &boot_oat_end); |
Mathieu Chartier | e719926 | 2016-04-11 13:56:45 -0700 | [diff] [blame] | 980 | if (boot_image_begin == boot_image_end) { |
| 981 | *error_msg = "Can not relocate app image without boot image space"; |
| 982 | return false; |
| 983 | } |
| 984 | if (boot_oat_begin == boot_oat_end) { |
| 985 | *error_msg = "Can not relocate app image without boot oat file"; |
| 986 | return false; |
| 987 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 988 | const uint32_t boot_image_size = boot_image_end - boot_image_begin; |
| 989 | const uint32_t boot_oat_size = boot_oat_end - boot_oat_begin; |
| 990 | const uint32_t image_header_boot_image_size = image_header.GetBootImageSize(); |
| 991 | const uint32_t image_header_boot_oat_size = image_header.GetBootOatSize(); |
| 992 | if (boot_image_size != image_header_boot_image_size) { |
| 993 | *error_msg = StringPrintf("Boot image size %" PRIu64 " does not match expected size %" |
| 994 | PRIu64, |
| 995 | static_cast<uint64_t>(boot_image_size), |
| 996 | static_cast<uint64_t>(image_header_boot_image_size)); |
| 997 | return false; |
| 998 | } |
| 999 | if (boot_oat_size != image_header_boot_oat_size) { |
| 1000 | *error_msg = StringPrintf("Boot oat size %" PRIu64 " does not match expected size %" |
| 1001 | PRIu64, |
| 1002 | static_cast<uint64_t>(boot_oat_size), |
| 1003 | static_cast<uint64_t>(image_header_boot_oat_size)); |
| 1004 | return false; |
| 1005 | } |
| 1006 | TimingLogger logger(__FUNCTION__, true, false); |
| 1007 | RelocationRange boot_image(image_header.GetBootImageBegin(), |
| 1008 | boot_image_begin, |
| 1009 | boot_image_size); |
| 1010 | RelocationRange boot_oat(image_header.GetBootOatBegin(), |
| 1011 | boot_oat_begin, |
| 1012 | boot_oat_size); |
| 1013 | RelocationRange app_image(reinterpret_cast<uintptr_t>(image_header.GetImageBegin()), |
| 1014 | reinterpret_cast<uintptr_t>(target_base), |
| 1015 | image_header.GetImageSize()); |
| 1016 | // Use the oat data section since this is where the OatFile::Begin is. |
| 1017 | RelocationRange app_oat(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()), |
| 1018 | // Not necessarily in low 4GB. |
| 1019 | reinterpret_cast<uintptr_t>(app_oat_file->Begin()), |
| 1020 | image_header.GetOatDataEnd() - image_header.GetOatDataBegin()); |
| 1021 | VLOG(image) << "App image " << app_image; |
| 1022 | VLOG(image) << "App oat " << app_oat; |
| 1023 | VLOG(image) << "Boot image " << boot_image; |
| 1024 | VLOG(image) << "Boot oat " << boot_oat; |
| 1025 | // True if we need to fixup any heap pointers, otherwise only code pointers. |
| 1026 | const bool fixup_image = boot_image.Delta() != 0 || app_image.Delta() != 0; |
| 1027 | const bool fixup_code = boot_oat.Delta() != 0 || app_oat.Delta() != 0; |
| 1028 | if (!fixup_image && !fixup_code) { |
| 1029 | // Nothing to fix up. |
| 1030 | return true; |
| 1031 | } |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 1032 | ScopedDebugDisallowReadBarriers sddrb(Thread::Current()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1033 | // Need to update the image to be at the target base. |
| 1034 | const ImageSection& objects_section = image_header.GetImageSection(ImageHeader::kSectionObjects); |
| 1035 | uintptr_t objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset()); |
| 1036 | uintptr_t objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End()); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1037 | FixupObjectAdapter fixup_adapter(boot_image, boot_oat, app_image, app_oat); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1038 | if (fixup_image) { |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 1039 | // Two pass approach, fix up all classes first, then fix up non class-objects. |
| 1040 | // The visited bitmap is used to ensure that pointer arrays are not forwarded twice. |
| 1041 | std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> visited_bitmap( |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 1042 | gc::accounting::ContinuousSpaceBitmap::Create("Relocate bitmap", |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 1043 | target_base, |
| 1044 | image_header.GetImageSize())); |
| 1045 | FixupObjectVisitor fixup_object_visitor(visited_bitmap.get(), |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1046 | pointer_size, |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 1047 | boot_image, |
| 1048 | boot_oat, |
| 1049 | app_image, |
| 1050 | app_oat); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1051 | TimingLogger::ScopedTiming timing("Fixup classes", &logger); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1052 | // Fixup objects may read fields in the boot image, use the mutator lock here for sanity. Though |
| 1053 | // its probably not required. |
| 1054 | ScopedObjectAccess soa(Thread::Current()); |
| 1055 | timing.NewTiming("Fixup objects"); |
| 1056 | bitmap->VisitMarkedRange(objects_begin, objects_end, fixup_object_visitor); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1057 | // Fixup image roots. |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 1058 | CHECK(app_image.InSource(reinterpret_cast<uintptr_t>( |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 1059 | image_header.GetImageRoots<kWithoutReadBarrier>()))); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1060 | image_header.RelocateImageObjects(app_image.Delta()); |
| 1061 | CHECK_EQ(image_header.GetImageBegin(), target_base); |
| 1062 | // Fix up dex cache DexFile pointers. |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 1063 | auto* dex_caches = image_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kDexCaches)-> |
Mathieu Chartier | dfe02f6 | 2016-02-01 20:15:11 -0800 | [diff] [blame] | 1064 | AsObjectArray<mirror::DexCache, kVerifyNone, kWithoutReadBarrier>(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1065 | for (int32_t i = 0, count = dex_caches->GetLength(); i < count; ++i) { |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 1066 | mirror::DexCache* dex_cache = dex_caches->Get<kVerifyNone, kWithoutReadBarrier>(i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1067 | // Fix up dex cache pointers. |
| 1068 | GcRoot<mirror::String>* strings = dex_cache->GetStrings(); |
| 1069 | if (strings != nullptr) { |
| 1070 | GcRoot<mirror::String>* new_strings = fixup_adapter.ForwardObject(strings); |
| 1071 | if (strings != new_strings) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1072 | dex_cache->SetStrings(new_strings); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1073 | } |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 1074 | dex_cache->FixupStrings<kWithoutReadBarrier>(new_strings, fixup_adapter); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1075 | } |
| 1076 | GcRoot<mirror::Class>* types = dex_cache->GetResolvedTypes(); |
| 1077 | if (types != nullptr) { |
| 1078 | GcRoot<mirror::Class>* new_types = fixup_adapter.ForwardObject(types); |
| 1079 | if (types != new_types) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1080 | dex_cache->SetResolvedTypes(new_types); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1081 | } |
Mathieu Chartier | 60bc39c | 2016-01-27 18:37:48 -0800 | [diff] [blame] | 1082 | dex_cache->FixupResolvedTypes<kWithoutReadBarrier>(new_types, fixup_adapter); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1083 | } |
| 1084 | ArtMethod** methods = dex_cache->GetResolvedMethods(); |
| 1085 | if (methods != nullptr) { |
| 1086 | ArtMethod** new_methods = fixup_adapter.ForwardObject(methods); |
| 1087 | if (methods != new_methods) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1088 | dex_cache->SetResolvedMethods(new_methods); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1089 | } |
| 1090 | for (size_t j = 0, num = dex_cache->NumResolvedMethods(); j != num; ++j) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1091 | ArtMethod* orig = mirror::DexCache::GetElementPtrSize(new_methods, j, pointer_size); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1092 | ArtMethod* copy = fixup_adapter.ForwardObject(orig); |
| 1093 | if (orig != copy) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1094 | mirror::DexCache::SetElementPtrSize(new_methods, j, copy, pointer_size); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | ArtField** fields = dex_cache->GetResolvedFields(); |
| 1099 | if (fields != nullptr) { |
| 1100 | ArtField** new_fields = fixup_adapter.ForwardObject(fields); |
| 1101 | if (fields != new_fields) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1102 | dex_cache->SetResolvedFields(new_fields); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1103 | } |
| 1104 | for (size_t j = 0, num = dex_cache->NumResolvedFields(); j != num; ++j) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1105 | ArtField* orig = mirror::DexCache::GetElementPtrSize(new_fields, j, pointer_size); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1106 | ArtField* copy = fixup_adapter.ForwardObject(orig); |
| 1107 | if (orig != copy) { |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1108 | mirror::DexCache::SetElementPtrSize(new_fields, j, copy, pointer_size); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | { |
| 1115 | // Only touches objects in the app image, no need for mutator lock. |
| 1116 | TimingLogger::ScopedTiming timing("Fixup methods", &logger); |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1117 | FixupArtMethodVisitor method_visitor(fixup_image, |
| 1118 | pointer_size, |
| 1119 | boot_image, |
| 1120 | boot_oat, |
| 1121 | app_image, |
| 1122 | app_oat); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1123 | image_header.VisitPackedArtMethods(&method_visitor, target_base, pointer_size); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1124 | } |
| 1125 | if (fixup_image) { |
| 1126 | { |
| 1127 | // Only touches objects in the app image, no need for mutator lock. |
| 1128 | TimingLogger::ScopedTiming timing("Fixup fields", &logger); |
| 1129 | FixupArtFieldVisitor field_visitor(boot_image, boot_oat, app_image, app_oat); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1130 | image_header.VisitPackedArtFields(&field_visitor, target_base); |
| 1131 | } |
| 1132 | { |
Nelli Kim | badee98 | 2016-05-13 13:08:53 +0300 | [diff] [blame] | 1133 | TimingLogger::ScopedTiming timing("Fixup imt", &logger); |
| 1134 | image_header.VisitPackedImTables(fixup_adapter, target_base, pointer_size); |
| 1135 | } |
| 1136 | { |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1137 | TimingLogger::ScopedTiming timing("Fixup conflict tables", &logger); |
| 1138 | image_header.VisitPackedImtConflictTables(fixup_adapter, target_base, pointer_size); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1139 | } |
| 1140 | // In the app image case, the image methods are actually in the boot image. |
| 1141 | image_header.RelocateImageMethods(boot_image.Delta()); |
| 1142 | const auto& class_table_section = image_header.GetImageSection(ImageHeader::kSectionClassTable); |
| 1143 | if (class_table_section.Size() > 0u) { |
| 1144 | // Note that we require that ReadFromMemory does not make an internal copy of the elements. |
| 1145 | // This also relies on visit roots not doing any verification which could fail after we update |
| 1146 | // the roots to be the image addresses. |
| 1147 | ScopedObjectAccess soa(Thread::Current()); |
| 1148 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 1149 | ClassTable temp_table; |
| 1150 | temp_table.ReadFromMemory(target_base + class_table_section.Offset()); |
| 1151 | FixupRootVisitor root_visitor(boot_image, boot_oat, app_image, app_oat); |
| 1152 | temp_table.VisitRoots(root_visitor); |
| 1153 | } |
| 1154 | } |
| 1155 | if (VLOG_IS_ON(image)) { |
| 1156 | logger.Dump(LOG(INFO)); |
| 1157 | } |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
| 1161 | ImageSpace* ImageSpace::Init(const char* image_filename, |
| 1162 | const char* image_location, |
| 1163 | bool validate_oat_file, |
| 1164 | const OatFile* oat_file, |
| 1165 | std::string* error_msg) { |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 1166 | CHECK(image_filename != nullptr); |
| 1167 | CHECK(image_location != nullptr); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1168 | |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 1169 | TimingLogger logger(__PRETTY_FUNCTION__, true, VLOG_IS_ON(image)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1170 | VLOG(image) << "ImageSpace::Init entering image_filename=" << image_filename; |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 1171 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1172 | std::unique_ptr<File> file; |
| 1173 | { |
| 1174 | TimingLogger::ScopedTiming timing("OpenImageFile", &logger); |
| 1175 | file.reset(OS::OpenFileForReading(image_filename)); |
| 1176 | if (file == nullptr) { |
| 1177 | *error_msg = StringPrintf("Failed to open '%s'", image_filename); |
| 1178 | return nullptr; |
| 1179 | } |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 1180 | } |
Mathieu Chartier | a67a49a | 2016-06-15 13:11:40 -0700 | [diff] [blame^] | 1181 | // unique_ptr to reduce frame size. |
| 1182 | std::unique_ptr<ImageHeader> temp_image_header(new ImageHeader); |
| 1183 | ImageHeader* image_header = temp_image_header.get(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1184 | { |
| 1185 | TimingLogger::ScopedTiming timing("ReadImageHeader", &logger); |
| 1186 | bool success = file->ReadFully(image_header, sizeof(*image_header)); |
| 1187 | if (!success || !image_header->IsValid()) { |
| 1188 | *error_msg = StringPrintf("Invalid image header in '%s'", image_filename); |
| 1189 | return nullptr; |
| 1190 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1191 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1192 | // Check that the file is larger or equal to the header size + data size. |
| 1193 | const uint64_t image_file_size = static_cast<uint64_t>(file->GetLength()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1194 | if (image_file_size < sizeof(ImageHeader) + image_header->GetDataSize()) { |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1195 | *error_msg = StringPrintf("Image file truncated: %" PRIu64 " vs. %" PRIu64 ".", |
| 1196 | image_file_size, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1197 | sizeof(ImageHeader) + image_header->GetDataSize()); |
Andreas Gampe | 6c8b49f | 2015-02-19 11:42:36 -0800 | [diff] [blame] | 1198 | return nullptr; |
| 1199 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1200 | |
Mathieu Chartier | 9ff8460 | 2016-01-29 12:22:17 -0800 | [diff] [blame] | 1201 | if (oat_file != nullptr) { |
| 1202 | // If we have an oat file, check the oat file checksum. The oat file is only non-null for the |
| 1203 | // app image case. Otherwise, we open the oat file after the image and check the checksum there. |
| 1204 | const uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum(); |
| 1205 | const uint32_t image_oat_checksum = image_header->GetOatChecksum(); |
| 1206 | if (oat_checksum != image_oat_checksum) { |
| 1207 | *error_msg = StringPrintf("Oat checksum 0x%x does not match the image one 0x%x in image %s", |
| 1208 | oat_checksum, |
| 1209 | image_oat_checksum, |
| 1210 | image_filename); |
| 1211 | return nullptr; |
| 1212 | } |
| 1213 | } |
| 1214 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1215 | if (VLOG_IS_ON(startup)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1216 | LOG(INFO) << "Dumping image sections"; |
| 1217 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 1218 | const auto section_idx = static_cast<ImageHeader::ImageSections>(i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1219 | auto& section = image_header->GetImageSection(section_idx); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1220 | LOG(INFO) << section_idx << " start=" |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1221 | << reinterpret_cast<void*>(image_header->GetImageBegin() + section.Offset()) << " " |
| 1222 | << section; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1226 | const auto& bitmap_section = image_header->GetImageSection(ImageHeader::kSectionImageBitmap); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1227 | // The location we want to map from is the first aligned page after the end of the stored |
| 1228 | // (possibly compressed) data. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1229 | const size_t image_bitmap_offset = RoundUp(sizeof(ImageHeader) + image_header->GetDataSize(), |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1230 | kPageSize); |
| 1231 | const size_t end_of_bitmap = image_bitmap_offset + bitmap_section.Size(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1232 | if (end_of_bitmap != image_file_size) { |
| 1233 | *error_msg = StringPrintf( |
| 1234 | "Image file size does not equal end of bitmap: size=%" PRIu64 " vs. %zu.", image_file_size, |
| 1235 | end_of_bitmap); |
Andreas Gampe | 6c8b49f | 2015-02-19 11:42:36 -0800 | [diff] [blame] | 1236 | return nullptr; |
| 1237 | } |
| 1238 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1239 | // The preferred address to map the image, null specifies any address. If we manage to map the |
| 1240 | // image at the image begin, the amount of fixup work required is minimized. |
| 1241 | std::vector<uint8_t*> addresses(1, image_header->GetImageBegin()); |
| 1242 | if (image_header->IsPic()) { |
| 1243 | // Can also map at a random low_4gb address since we can relocate in-place. |
| 1244 | addresses.push_back(nullptr); |
| 1245 | } |
| 1246 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1247 | // Note: The image header is part of the image due to mmap page alignment required of offset. |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1248 | std::unique_ptr<MemMap> map; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1249 | std::string temp_error_msg; |
| 1250 | for (uint8_t* address : addresses) { |
| 1251 | TimingLogger::ScopedTiming timing("MapImageFile", &logger); |
| 1252 | // Only care about the error message for the last address in addresses. We want to avoid the |
| 1253 | // overhead of printing the process maps if we can relocate. |
| 1254 | std::string* out_error_msg = (address == addresses.back()) ? &temp_error_msg : nullptr; |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 1255 | const ImageHeader::StorageMode storage_mode = image_header->GetStorageMode(); |
| 1256 | if (storage_mode == ImageHeader::kStorageModeUncompressed) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1257 | map.reset(MemMap::MapFileAtAddress(address, |
| 1258 | image_header->GetImageSize(), |
| 1259 | PROT_READ | PROT_WRITE, |
| 1260 | MAP_PRIVATE, |
| 1261 | file->Fd(), |
| 1262 | 0, |
| 1263 | /*low_4gb*/true, |
| 1264 | /*reuse*/false, |
| 1265 | image_filename, |
| 1266 | /*out*/out_error_msg)); |
| 1267 | } else { |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 1268 | if (storage_mode != ImageHeader::kStorageModeLZ4 && |
| 1269 | storage_mode != ImageHeader::kStorageModeLZ4HC) { |
| 1270 | *error_msg = StringPrintf("Invalid storage mode in image header %d", |
| 1271 | static_cast<int>(storage_mode)); |
| 1272 | return nullptr; |
| 1273 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1274 | // Reserve output and decompress into it. |
| 1275 | map.reset(MemMap::MapAnonymous(image_location, |
| 1276 | address, |
| 1277 | image_header->GetImageSize(), |
| 1278 | PROT_READ | PROT_WRITE, |
| 1279 | /*low_4gb*/true, |
| 1280 | /*reuse*/false, |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 1281 | /*out*/out_error_msg)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1282 | if (map != nullptr) { |
| 1283 | const size_t stored_size = image_header->GetDataSize(); |
Mathieu Chartier | 8e864bf | 2016-03-14 11:02:59 -0700 | [diff] [blame] | 1284 | const size_t decompress_offset = sizeof(ImageHeader); // Skip the header. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1285 | std::unique_ptr<MemMap> temp_map(MemMap::MapFile(sizeof(ImageHeader) + stored_size, |
| 1286 | PROT_READ, |
| 1287 | MAP_PRIVATE, |
| 1288 | file->Fd(), |
| 1289 | /*offset*/0, |
| 1290 | /*low_4gb*/false, |
| 1291 | image_filename, |
| 1292 | out_error_msg)); |
| 1293 | if (temp_map == nullptr) { |
| 1294 | DCHECK(!out_error_msg->empty()); |
| 1295 | return nullptr; |
| 1296 | } |
| 1297 | memcpy(map->Begin(), image_header, sizeof(ImageHeader)); |
| 1298 | const uint64_t start = NanoTime(); |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 1299 | // LZ4HC and LZ4 have same internal format, both use LZ4_decompress. |
Mathieu Chartier | 31317c3 | 2016-02-25 12:28:40 -0800 | [diff] [blame] | 1300 | TimingLogger::ScopedTiming timing2("LZ4 decompress image", &logger); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1301 | const size_t decompressed_size = LZ4_decompress_safe( |
| 1302 | reinterpret_cast<char*>(temp_map->Begin()) + sizeof(ImageHeader), |
Mathieu Chartier | 8e864bf | 2016-03-14 11:02:59 -0700 | [diff] [blame] | 1303 | reinterpret_cast<char*>(map->Begin()) + decompress_offset, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1304 | stored_size, |
Mathieu Chartier | 8e864bf | 2016-03-14 11:02:59 -0700 | [diff] [blame] | 1305 | map->Size() - decompress_offset); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1306 | VLOG(image) << "Decompressing image took " << PrettyDuration(NanoTime() - start); |
| 1307 | if (decompressed_size + sizeof(ImageHeader) != image_header->GetImageSize()) { |
Mathieu Chartier | 8e864bf | 2016-03-14 11:02:59 -0700 | [diff] [blame] | 1308 | *error_msg = StringPrintf( |
| 1309 | "Decompressed size does not match expected image size %zu vs %zu", |
| 1310 | decompressed_size + sizeof(ImageHeader), |
| 1311 | image_header->GetImageSize()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1312 | return nullptr; |
| 1313 | } |
| 1314 | } |
| 1315 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1316 | if (map != nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1317 | break; |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1318 | } |
| 1319 | } |
| 1320 | |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1321 | if (map == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1322 | DCHECK(!temp_error_msg.empty()); |
| 1323 | *error_msg = temp_error_msg; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1324 | return nullptr; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1325 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1326 | DCHECK_EQ(0, memcmp(image_header, map->Begin(), sizeof(ImageHeader))); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1327 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1328 | std::unique_ptr<MemMap> image_bitmap_map(MemMap::MapFileAtAddress(nullptr, |
| 1329 | bitmap_section.Size(), |
| 1330 | PROT_READ, MAP_PRIVATE, |
| 1331 | file->Fd(), |
| 1332 | image_bitmap_offset, |
| 1333 | /*low_4gb*/false, |
| 1334 | /*reuse*/false, |
| 1335 | image_filename, |
| 1336 | error_msg)); |
| 1337 | if (image_bitmap_map == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1338 | *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str()); |
| 1339 | return nullptr; |
| 1340 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1341 | // Loaded the map, use the image header from the file now in case we patch it with |
| 1342 | // RelocateInPlace. |
| 1343 | image_header = reinterpret_cast<ImageHeader*>(map->Begin()); |
| 1344 | const uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1); |
| 1345 | std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", |
| 1346 | image_filename, |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1347 | bitmap_index)); |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 1348 | // Bitmap only needs to cover until the end of the mirror objects section. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1349 | const ImageSection& image_objects = image_header->GetImageSection(ImageHeader::kSectionObjects); |
| 1350 | // We only want the mirror object, not the ArtFields and ArtMethods. |
| 1351 | uint8_t* const image_end = map->Begin() + image_objects.End(); |
| 1352 | std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap; |
| 1353 | { |
| 1354 | TimingLogger::ScopedTiming timing("CreateImageBitmap", &logger); |
| 1355 | bitmap.reset( |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1356 | accounting::ContinuousSpaceBitmap::CreateFromMemMap( |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1357 | bitmap_name, |
| 1358 | image_bitmap_map.release(), |
| 1359 | reinterpret_cast<uint8_t*>(map->Begin()), |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 1360 | image_objects.End())); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1361 | if (bitmap == nullptr) { |
| 1362 | *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str()); |
| 1363 | return nullptr; |
| 1364 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1365 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1366 | { |
| 1367 | TimingLogger::ScopedTiming timing("RelocateImage", &logger); |
| 1368 | if (!RelocateInPlace(*image_header, |
| 1369 | map->Begin(), |
| 1370 | bitmap.get(), |
| 1371 | oat_file, |
| 1372 | error_msg)) { |
| 1373 | return nullptr; |
| 1374 | } |
| 1375 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1376 | // We only want the mirror object, not the ArtFields and ArtMethods. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1377 | std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, |
| 1378 | image_location, |
| 1379 | map.release(), |
| 1380 | bitmap.release(), |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 1381 | image_end)); |
Hiroshi Yamauchi | bd0fb61 | 2014-05-20 13:46:00 -0700 | [diff] [blame] | 1382 | |
| 1383 | // VerifyImageAllocations() will be called later in Runtime::Init() |
| 1384 | // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_ |
| 1385 | // and ArtField::java_lang_reflect_ArtField_, which are used from |
| 1386 | // Object::SizeOf() which VerifyImageAllocations() calls, are not |
| 1387 | // set yet at this point. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1388 | if (oat_file == nullptr) { |
| 1389 | TimingLogger::ScopedTiming timing("OpenOatFile", &logger); |
| 1390 | space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg)); |
| 1391 | if (space->oat_file_ == nullptr) { |
| 1392 | DCHECK(!error_msg->empty()); |
| 1393 | return nullptr; |
| 1394 | } |
| 1395 | space->oat_file_non_owned_ = space->oat_file_.get(); |
| 1396 | } else { |
| 1397 | space->oat_file_non_owned_ = oat_file; |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 1398 | } |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 1399 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1400 | if (validate_oat_file) { |
| 1401 | TimingLogger::ScopedTiming timing("ValidateOatFile", &logger); |
| 1402 | if (!space->ValidateOatFile(error_msg)) { |
| 1403 | DCHECK(!error_msg->empty()); |
| 1404 | return nullptr; |
| 1405 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 1408 | Runtime* runtime = Runtime::Current(); |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 1409 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1410 | // If oat_file is null, then it is the boot image space. Use oat_file_non_owned_ from the space |
| 1411 | // to set the runtime methods. |
| 1412 | CHECK_EQ(oat_file != nullptr, image_header->IsAppImage()); |
| 1413 | if (image_header->IsAppImage()) { |
| 1414 | CHECK_EQ(runtime->GetResolutionMethod(), |
| 1415 | image_header->GetImageMethod(ImageHeader::kResolutionMethod)); |
| 1416 | CHECK_EQ(runtime->GetImtConflictMethod(), |
| 1417 | image_header->GetImageMethod(ImageHeader::kImtConflictMethod)); |
| 1418 | CHECK_EQ(runtime->GetImtUnimplementedMethod(), |
| 1419 | image_header->GetImageMethod(ImageHeader::kImtUnimplementedMethod)); |
| 1420 | CHECK_EQ(runtime->GetCalleeSaveMethod(Runtime::kSaveAll), |
| 1421 | image_header->GetImageMethod(ImageHeader::kCalleeSaveMethod)); |
| 1422 | CHECK_EQ(runtime->GetCalleeSaveMethod(Runtime::kRefsOnly), |
| 1423 | image_header->GetImageMethod(ImageHeader::kRefsOnlySaveMethod)); |
| 1424 | CHECK_EQ(runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs), |
| 1425 | image_header->GetImageMethod(ImageHeader::kRefsAndArgsSaveMethod)); |
| 1426 | } else if (!runtime->HasResolutionMethod()) { |
| 1427 | runtime->SetInstructionSet(space->oat_file_non_owned_->GetOatHeader().GetInstructionSet()); |
| 1428 | runtime->SetResolutionMethod(image_header->GetImageMethod(ImageHeader::kResolutionMethod)); |
| 1429 | runtime->SetImtConflictMethod(image_header->GetImageMethod(ImageHeader::kImtConflictMethod)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1430 | runtime->SetImtUnimplementedMethod( |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1431 | image_header->GetImageMethod(ImageHeader::kImtUnimplementedMethod)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1432 | runtime->SetCalleeSaveMethod( |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1433 | image_header->GetImageMethod(ImageHeader::kCalleeSaveMethod), Runtime::kSaveAll); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1434 | runtime->SetCalleeSaveMethod( |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1435 | image_header->GetImageMethod(ImageHeader::kRefsOnlySaveMethod), Runtime::kRefsOnly); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1436 | runtime->SetCalleeSaveMethod( |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1437 | image_header->GetImageMethod(ImageHeader::kRefsAndArgsSaveMethod), Runtime::kRefsAndArgs); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1438 | } |
Vladimir Marko | 7624d25 | 2014-05-02 14:40:15 +0100 | [diff] [blame] | 1439 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1440 | VLOG(image) << "ImageSpace::Init exiting " << *space.get(); |
| 1441 | if (VLOG_IS_ON(image)) { |
| 1442 | logger.Dump(LOG(INFO)); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1443 | } |
| 1444 | return space.release(); |
| 1445 | } |
| 1446 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 1447 | OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1448 | const ImageHeader& image_header = GetImageHeader(); |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 1449 | std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path); |
| 1450 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1451 | CHECK(image_header.GetOatDataBegin() != nullptr); |
| 1452 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1453 | OatFile* oat_file = OatFile::Open(oat_filename, |
| 1454 | oat_filename, |
| 1455 | image_header.GetOatDataBegin(), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1456 | image_header.GetOatFileBegin(), |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 1457 | !Runtime::Current()->IsAotCompiler(), |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1458 | /*low_4gb*/false, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1459 | nullptr, |
| 1460 | error_msg); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1461 | if (oat_file == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1462 | *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s", |
| 1463 | oat_filename.c_str(), GetName(), error_msg->c_str()); |
| 1464 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1465 | } |
| 1466 | uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum(); |
| 1467 | uint32_t image_oat_checksum = image_header.GetOatChecksum(); |
| 1468 | if (oat_checksum != image_oat_checksum) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1469 | *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x" |
| 1470 | " in image %s", oat_checksum, image_oat_checksum, GetName()); |
| 1471 | return nullptr; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1472 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1473 | int32_t image_patch_delta = image_header.GetPatchDelta(); |
| 1474 | int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta(); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1475 | if (oat_patch_delta != image_patch_delta && !image_header.CompilePic()) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1476 | // We should have already relocated by this point. Bail out. |
| 1477 | *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d " |
| 1478 | "in image %s", oat_patch_delta, image_patch_delta, GetName()); |
| 1479 | return nullptr; |
| 1480 | } |
| 1481 | |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1482 | return oat_file; |
| 1483 | } |
| 1484 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1485 | bool ImageSpace::ValidateOatFile(std::string* error_msg) const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1486 | CHECK(oat_file_.get() != nullptr); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1487 | for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1488 | const std::string& dex_file_location = oat_dex_file->GetDexFileLocation(); |
| 1489 | uint32_t dex_file_location_checksum; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1490 | if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) { |
| 1491 | *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: " |
| 1492 | "%s", dex_file_location.c_str(), GetName(), error_msg->c_str()); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1493 | return false; |
| 1494 | } |
| 1495 | if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1496 | *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and " |
| 1497 | "dex file '%s' (0x%x != 0x%x)", |
| 1498 | oat_file_->GetLocation().c_str(), dex_file_location.c_str(), |
| 1499 | oat_dex_file->GetDexFileLocationChecksum(), |
| 1500 | dex_file_location_checksum); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1501 | return false; |
| 1502 | } |
| 1503 | } |
| 1504 | return true; |
| 1505 | } |
| 1506 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1507 | const OatFile* ImageSpace::GetOatFile() const { |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 1508 | return oat_file_non_owned_; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 1511 | std::unique_ptr<const OatFile> ImageSpace::ReleaseOatFile() { |
| 1512 | CHECK(oat_file_ != nullptr); |
| 1513 | return std::move(oat_file_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1514 | } |
| 1515 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1516 | void ImageSpace::Dump(std::ostream& os) const { |
| 1517 | os << GetType() |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1518 | << " begin=" << reinterpret_cast<void*>(Begin()) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1519 | << ",end=" << reinterpret_cast<void*>(End()) |
| 1520 | << ",size=" << PrettySize(Size()) |
| 1521 | << ",name=\"" << GetName() << "\"]"; |
| 1522 | } |
| 1523 | |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 1524 | void ImageSpace::CreateMultiImageLocations(const std::string& input_image_file_name, |
| 1525 | const std::string& boot_classpath, |
| 1526 | std::vector<std::string>* image_file_names) { |
| 1527 | DCHECK(image_file_names != nullptr); |
| 1528 | |
| 1529 | std::vector<std::string> images; |
| 1530 | Split(boot_classpath, ':', &images); |
| 1531 | |
| 1532 | // Add the rest into the list. We have to adjust locations, possibly: |
| 1533 | // |
| 1534 | // For example, image_file_name is /a/b/c/d/e.art |
| 1535 | // images[0] is f/c/d/e.art |
| 1536 | // ---------------------------------------------- |
| 1537 | // images[1] is g/h/i/j.art -> /a/b/h/i/j.art |
Mathieu Chartier | 8b8f6d6 | 2016-03-08 16:50:20 -0800 | [diff] [blame] | 1538 | const std::string& first_image = images[0]; |
| 1539 | // Length of common suffix. |
| 1540 | size_t common = 0; |
| 1541 | while (common < input_image_file_name.size() && |
| 1542 | common < first_image.size() && |
| 1543 | *(input_image_file_name.end() - common - 1) == *(first_image.end() - common - 1)) { |
| 1544 | ++common; |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 1545 | } |
Mathieu Chartier | 8b8f6d6 | 2016-03-08 16:50:20 -0800 | [diff] [blame] | 1546 | // We want to replace the prefix of the input image with the prefix of the boot class path. |
| 1547 | // This handles the case where the image file contains @ separators. |
| 1548 | // Example image_file_name is oats/system@framework@boot.art |
| 1549 | // images[0] is .../arm/boot.art |
| 1550 | // means that the image name prefix will be oats/system@framework@ |
| 1551 | // so that the other images are openable. |
| 1552 | const size_t old_prefix_length = first_image.size() - common; |
| 1553 | const std::string new_prefix = input_image_file_name.substr( |
| 1554 | 0, |
| 1555 | input_image_file_name.size() - common); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 1556 | |
| 1557 | // Apply pattern to images[1] .. images[n]. |
| 1558 | for (size_t i = 1; i < images.size(); ++i) { |
Mathieu Chartier | 8b8f6d6 | 2016-03-08 16:50:20 -0800 | [diff] [blame] | 1559 | const std::string& image = images[i]; |
| 1560 | CHECK_GT(image.length(), old_prefix_length); |
| 1561 | std::string suffix = image.substr(old_prefix_length); |
| 1562 | image_file_names->push_back(new_prefix + suffix); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1566 | ImageSpace* ImageSpace::CreateFromAppImage(const char* image, |
| 1567 | const OatFile* oat_file, |
| 1568 | std::string* error_msg) { |
| 1569 | return gc::space::ImageSpace::Init(image, |
| 1570 | image, |
| 1571 | /*validate_oat_file*/false, |
| 1572 | oat_file, |
| 1573 | /*out*/error_msg); |
| 1574 | } |
| 1575 | |
Mathieu Chartier | d5f3f32 | 2016-03-21 14:05:56 -0700 | [diff] [blame] | 1576 | void ImageSpace::DumpSections(std::ostream& os) const { |
| 1577 | const uint8_t* base = Begin(); |
| 1578 | const ImageHeader& header = GetImageHeader(); |
| 1579 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 1580 | auto section_type = static_cast<ImageHeader::ImageSections>(i); |
| 1581 | const ImageSection& section = header.GetImageSection(section_type); |
| 1582 | os << section_type << " " << reinterpret_cast<const void*>(base + section.Offset()) |
| 1583 | << "-" << reinterpret_cast<const void*>(base + section.End()) << "\n"; |
| 1584 | } |
| 1585 | } |
| 1586 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1587 | } // namespace space |
| 1588 | } // namespace gc |
| 1589 | } // namespace art |