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