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 | |
Andreas Gampe | 70be1fb | 2014-10-31 16:45:19 -0700 | [diff] [blame] | 19 | #include <sys/statvfs.h> |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
Narayan Kamath | 5a2be3f | 2015-02-16 13:51:51 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 22 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include <random> |
| 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 | |
Andreas Gampe | 639b2b1 | 2019-01-08 10:32:50 -0800 | [diff] [blame] | 28 | #include "arch/instruction_set.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 29 | #include "art_field-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 30 | #include "art_method-inl.h" |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 31 | #include "base/array_ref.h" |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 32 | #include "base/bit_memory_region.h" |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 33 | #include "base/callee_save_type.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 34 | #include "base/enums.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 35 | #include "base/file_utils.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 36 | #include "base/macros.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 37 | #include "base/os.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 38 | #include "base/scoped_flock.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 39 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 40 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 41 | #include "base/time_utils.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 42 | #include "base/utils.h" |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 43 | #include "class_root.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 44 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 45 | #include "dex/dex_file_loader.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 46 | #include "exec_utils.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 47 | #include "gc/accounting/space_bitmap-inl.h" |
Mathieu Chartier | 93c21ba | 2018-12-10 13:08:30 -0800 | [diff] [blame] | 48 | #include "gc/task_processor.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 49 | #include "image-inl.h" |
Andreas Gampe | bec6358 | 2015-11-20 19:26:51 -0800 | [diff] [blame] | 50 | #include "image_space_fs.h" |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 51 | #include "intern_table-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 52 | #include "mirror/class-inl.h" |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 53 | #include "mirror/executable.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 54 | #include "mirror/object-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 55 | #include "mirror/object-refvisitor-inl.h" |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 56 | #include "oat_file.h" |
Andreas Gampe | d482e73 | 2017-04-24 17:59:09 -0700 | [diff] [blame] | 57 | #include "runtime.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 58 | #include "space-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 59 | |
| 60 | namespace art { |
| 61 | namespace gc { |
| 62 | namespace space { |
| 63 | |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 64 | using android::base::StringAppendF; |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 65 | using android::base::StringPrintf; |
| 66 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 67 | Atomic<uint32_t> ImageSpace::bitmap_index_(0); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 68 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 69 | ImageSpace::ImageSpace(const std::string& image_filename, |
| 70 | const char* image_location, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 71 | MemMap&& mem_map, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 72 | std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap, |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 73 | uint8_t* end) |
| 74 | : MemMapSpace(image_filename, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 75 | std::move(mem_map), |
| 76 | mem_map.Begin(), |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 77 | end, |
| 78 | end, |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 79 | kGcRetentionPolicyNeverCollect), |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 80 | live_bitmap_(std::move(live_bitmap)), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 81 | oat_file_non_owned_(nullptr), |
Mathieu Chartier | 2d124ec | 2016-01-05 18:03:15 -0800 | [diff] [blame] | 82 | image_location_(image_location) { |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 83 | DCHECK(live_bitmap_ != nullptr); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 86 | static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) { |
| 87 | CHECK_ALIGNED(min_delta, kPageSize); |
| 88 | CHECK_ALIGNED(max_delta, kPageSize); |
| 89 | CHECK_LT(min_delta, max_delta); |
| 90 | |
Alex Light | 1532476 | 2015-11-19 11:03:10 -0800 | [diff] [blame] | 91 | int32_t r = GetRandomNumber<int32_t>(min_delta, max_delta); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 92 | if (r % 2 == 0) { |
| 93 | r = RoundUp(r, kPageSize); |
| 94 | } else { |
| 95 | r = RoundDown(r, kPageSize); |
| 96 | } |
| 97 | CHECK_LE(min_delta, r); |
| 98 | CHECK_GE(max_delta, r); |
| 99 | CHECK_ALIGNED(r, kPageSize); |
| 100 | return r; |
| 101 | } |
| 102 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 103 | static int32_t ChooseRelocationOffsetDelta() { |
| 104 | return ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, ART_BASE_ADDRESS_MAX_DELTA); |
| 105 | } |
| 106 | |
| 107 | static bool GenerateImage(const std::string& image_filename, |
| 108 | InstructionSet image_isa, |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 109 | std::string* error_msg) { |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 110 | Runtime* runtime = Runtime::Current(); |
| 111 | const std::vector<std::string>& boot_class_path = runtime->GetBootClassPath(); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 112 | if (boot_class_path.empty()) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 113 | *error_msg = "Failed to generate image because no boot class path specified"; |
| 114 | return false; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 115 | } |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 116 | // We should clean up so we are more likely to have room for the image. |
| 117 | if (Runtime::Current()->IsZygote()) { |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 118 | 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] | 119 | PruneDalvikCache(image_isa); |
Alex Light | 2539613 | 2014-08-27 15:37:23 -0700 | [diff] [blame] | 120 | } |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 121 | |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 122 | std::vector<std::string> arg_vector; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 123 | |
Tsu Chiang Chuang | 12e6d74 | 2014-05-22 10:22:25 -0700 | [diff] [blame] | 124 | std::string dex2oat(Runtime::Current()->GetCompilerExecutable()); |
Mathieu Chartier | 08d7d44 | 2013-07-31 18:08:51 -0700 | [diff] [blame] | 125 | arg_vector.push_back(dex2oat); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 126 | |
| 127 | std::string image_option_string("--image="); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 128 | image_option_string += image_filename; |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 129 | arg_vector.push_back(image_option_string); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 130 | |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 131 | const std::vector<std::string>& boot_class_path_locations = runtime->GetBootClassPathLocations(); |
| 132 | DCHECK_EQ(boot_class_path.size(), boot_class_path_locations.size()); |
| 133 | for (size_t i = 0u; i < boot_class_path.size(); i++) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 134 | arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]); |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 135 | arg_vector.push_back(std::string("--dex-location=") + boot_class_path_locations[i]); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | std::string oat_file_option_string("--oat-file="); |
Brian Carlstrom | 2f1e15c | 2014-10-27 16:27:06 -0700 | [diff] [blame] | 139 | oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename); |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 140 | arg_vector.push_back(oat_file_option_string); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 141 | |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 142 | // Note: we do not generate a fully debuggable boot image so we do not pass the |
| 143 | // compiler flag --debuggable here. |
| 144 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 145 | Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector); |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 146 | CHECK_EQ(image_isa, kRuntimeISA) |
| 147 | << "We should always be generating an image for the current isa."; |
Ian Rogers | 8afeb85 | 2014-04-02 14:55:49 -0700 | [diff] [blame] | 148 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 149 | int32_t base_offset = ChooseRelocationOffsetDelta(); |
Alex Light | cf4bf38 | 2014-07-24 11:29:14 -0700 | [diff] [blame] | 150 | LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default " |
| 151 | << "art base address of 0x" << std::hex << ART_BASE_ADDRESS; |
| 152 | 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] | 153 | |
Brian Carlstrom | 57309db | 2014-07-30 15:13:25 -0700 | [diff] [blame] | 154 | if (!kIsTargetBuild) { |
Mathieu Chartier | 8bbc8c0 | 2013-07-31 16:27:01 -0700 | [diff] [blame] | 155 | arg_vector.push_back("--host"); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 158 | const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions(); |
Brian Carlstrom | 2ec6520 | 2014-03-03 15:16:37 -0800 | [diff] [blame] | 159 | for (size_t i = 0; i < compiler_options.size(); ++i) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 160 | arg_vector.push_back(compiler_options[i].c_str()); |
| 161 | } |
| 162 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 163 | std::string command_line(android::base::Join(arg_vector, ' ')); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 164 | LOG(INFO) << "GenerateImage: " << command_line; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 165 | return Exec(arg_vector, error_msg); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 168 | static bool FindImageFilenameImpl(const char* image_location, |
| 169 | const InstructionSet image_isa, |
| 170 | bool* has_system, |
| 171 | std::string* system_filename, |
| 172 | bool* dalvik_cache_exists, |
| 173 | std::string* dalvik_cache, |
| 174 | bool* is_global_cache, |
| 175 | bool* has_cache, |
| 176 | std::string* cache_filename) { |
| 177 | DCHECK(dalvik_cache != nullptr); |
| 178 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 179 | *has_system = false; |
| 180 | *has_cache = false; |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 181 | // image_location = /system/framework/boot.art |
| 182 | // system_image_location = /system/framework/<image_isa>/boot.art |
| 183 | std::string system_image_filename(GetSystemImageFilename(image_location, image_isa)); |
| 184 | if (OS::FileExists(system_image_filename.c_str())) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 185 | *system_filename = system_image_filename; |
| 186 | *has_system = true; |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 187 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 188 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 189 | bool have_android_data = false; |
| 190 | *dalvik_cache_exists = false; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 191 | GetDalvikCache(GetInstructionSetString(image_isa), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 192 | /*create_if_absent=*/ true, |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 193 | dalvik_cache, |
| 194 | &have_android_data, |
| 195 | dalvik_cache_exists, |
| 196 | is_global_cache); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 197 | |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 198 | if (*dalvik_cache_exists) { |
| 199 | DCHECK(have_android_data); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 200 | // Always set output location even if it does not exist, |
| 201 | // so that the caller knows where to create the image. |
| 202 | // |
| 203 | // image_location = /system/framework/boot.art |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 204 | // *image_filename = /data/dalvik-cache/<image_isa>/system@framework@boot.art |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 205 | std::string error_msg; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 206 | if (!GetDalvikCacheFilename(image_location, |
| 207 | dalvik_cache->c_str(), |
| 208 | cache_filename, |
| 209 | &error_msg)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 210 | LOG(WARNING) << error_msg; |
| 211 | return *has_system; |
| 212 | } |
| 213 | *has_cache = OS::FileExists(cache_filename->c_str()); |
| 214 | } |
| 215 | return *has_system || *has_cache; |
| 216 | } |
| 217 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 218 | bool ImageSpace::FindImageFilename(const char* image_location, |
| 219 | const InstructionSet image_isa, |
| 220 | std::string* system_filename, |
| 221 | bool* has_system, |
| 222 | std::string* cache_filename, |
| 223 | bool* dalvik_cache_exists, |
| 224 | bool* has_cache, |
| 225 | bool* is_global_cache) { |
| 226 | std::string dalvik_cache_unused; |
| 227 | return FindImageFilenameImpl(image_location, |
| 228 | image_isa, |
| 229 | has_system, |
| 230 | system_filename, |
| 231 | dalvik_cache_exists, |
| 232 | &dalvik_cache_unused, |
| 233 | is_global_cache, |
| 234 | has_cache, |
| 235 | cache_filename); |
| 236 | } |
| 237 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 238 | static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) { |
| 239 | std::unique_ptr<File> image_file(OS::OpenFileForReading(filename)); |
| 240 | if (image_file.get() == nullptr) { |
| 241 | return false; |
| 242 | } |
| 243 | const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader)); |
| 244 | if (!success || !image_header->IsValid()) { |
| 245 | return false; |
| 246 | } |
| 247 | return true; |
| 248 | } |
| 249 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 250 | static std::unique_ptr<ImageHeader> ReadSpecificImageHeader(const char* filename, |
| 251 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 252 | std::unique_ptr<ImageHeader> hdr(new ImageHeader); |
| 253 | if (!ReadSpecificImageHeader(filename, hdr.get())) { |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 254 | *error_msg = StringPrintf("Unable to read image header for %s", filename); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 255 | return nullptr; |
| 256 | } |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 257 | return hdr; |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 258 | } |
| 259 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 260 | std::unique_ptr<ImageHeader> ImageSpace::ReadImageHeader(const char* image_location, |
| 261 | const InstructionSet image_isa, |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 262 | ImageSpaceLoadingOrder order, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 263 | std::string* error_msg) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 264 | std::string system_filename; |
| 265 | bool has_system = false; |
| 266 | std::string cache_filename; |
| 267 | bool has_cache = false; |
| 268 | bool dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 269 | bool is_global_cache = false; |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 270 | if (FindImageFilename(image_location, |
| 271 | image_isa, |
| 272 | &system_filename, |
| 273 | &has_system, |
| 274 | &cache_filename, |
| 275 | &dalvik_cache_exists, |
| 276 | &has_cache, |
| 277 | &is_global_cache)) { |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 278 | if (order == ImageSpaceLoadingOrder::kSystemFirst) { |
| 279 | if (has_system) { |
| 280 | return ReadSpecificImageHeader(system_filename.c_str(), error_msg); |
| 281 | } |
| 282 | if (has_cache) { |
| 283 | return ReadSpecificImageHeader(cache_filename.c_str(), error_msg); |
| 284 | } |
| 285 | } else { |
| 286 | if (has_cache) { |
| 287 | return ReadSpecificImageHeader(cache_filename.c_str(), error_msg); |
| 288 | } |
| 289 | if (has_system) { |
| 290 | return ReadSpecificImageHeader(system_filename.c_str(), error_msg); |
| 291 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 292 | } |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Brian Carlstrom | 31d8f52 | 2014-09-29 11:22:54 -0700 | [diff] [blame] | 295 | *error_msg = StringPrintf("Unable to find image file for %s", image_location); |
Narayan Kamath | 52f8488 | 2014-05-02 10:10:39 +0100 | [diff] [blame] | 296 | return nullptr; |
| 297 | } |
| 298 | |
Robert Sesek | bfa1f8d | 2016-08-15 15:21:09 -0400 | [diff] [blame] | 299 | static bool CanWriteToDalvikCache(const InstructionSet isa) { |
| 300 | const std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(isa)); |
| 301 | if (access(dalvik_cache.c_str(), O_RDWR) == 0) { |
| 302 | return true; |
| 303 | } else if (errno != EACCES) { |
| 304 | PLOG(WARNING) << "CanWriteToDalvikCache returned error other than EACCES"; |
| 305 | } |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | static bool ImageCreationAllowed(bool is_global_cache, |
| 310 | const InstructionSet isa, |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 311 | bool is_zygote, |
Robert Sesek | bfa1f8d | 2016-08-15 15:21:09 -0400 | [diff] [blame] | 312 | std::string* error_msg) { |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 313 | // Anyone can write into a "local" cache. |
| 314 | if (!is_global_cache) { |
| 315 | return true; |
| 316 | } |
| 317 | |
Robert Sesek | bfa1f8d | 2016-08-15 15:21:09 -0400 | [diff] [blame] | 318 | // Only the zygote running as root is allowed to create the global boot image. |
| 319 | // If the zygote is running as non-root (and cannot write to the dalvik-cache), |
| 320 | // then image creation is not allowed.. |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 321 | if (is_zygote) { |
Robert Sesek | bfa1f8d | 2016-08-15 15:21:09 -0400 | [diff] [blame] | 322 | return CanWriteToDalvikCache(isa); |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | *error_msg = "Only the zygote can create the global boot image."; |
| 326 | return false; |
| 327 | } |
| 328 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 329 | void ImageSpace::VerifyImageAllocations() { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 330 | uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 331 | while (current < End()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 332 | CHECK_ALIGNED(current, kObjectAlignment); |
| 333 | auto* obj = reinterpret_cast<mirror::Object*>(current); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 334 | CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class"; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 335 | CHECK(live_bitmap_->Test(obj)) << obj->PrettyTypeOf(); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 336 | if (kUseBakerReadBarrier) { |
| 337 | obj->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 338 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 339 | current += RoundUp(obj->SizeOf(), kObjectAlignment); |
| 340 | } |
| 341 | } |
| 342 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 343 | // Helper class for relocating from one range of memory to another. |
| 344 | class RelocationRange { |
| 345 | public: |
| 346 | RelocationRange() = default; |
| 347 | RelocationRange(const RelocationRange&) = default; |
| 348 | RelocationRange(uintptr_t source, uintptr_t dest, uintptr_t length) |
| 349 | : source_(source), |
| 350 | dest_(dest), |
| 351 | length_(length) {} |
| 352 | |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 353 | bool InSource(uintptr_t address) const { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 354 | return address - source_ < length_; |
| 355 | } |
| 356 | |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 357 | bool InDest(const void* dest) const { |
| 358 | return InDest(reinterpret_cast<uintptr_t>(dest)); |
| 359 | } |
| 360 | |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 361 | bool InDest(uintptr_t address) const { |
| 362 | return address - dest_ < length_; |
| 363 | } |
| 364 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 365 | // Translate a source address to the destination space. |
| 366 | uintptr_t ToDest(uintptr_t address) const { |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 367 | DCHECK(InSource(address)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 368 | return address + Delta(); |
| 369 | } |
| 370 | |
| 371 | // Returns the delta between the dest from the source. |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 372 | uintptr_t Delta() const { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 373 | return dest_ - source_; |
| 374 | } |
| 375 | |
| 376 | uintptr_t Source() const { |
| 377 | return source_; |
| 378 | } |
| 379 | |
| 380 | uintptr_t Dest() const { |
| 381 | return dest_; |
| 382 | } |
| 383 | |
| 384 | uintptr_t Length() const { |
| 385 | return length_; |
| 386 | } |
| 387 | |
| 388 | private: |
| 389 | const uintptr_t source_; |
| 390 | const uintptr_t dest_; |
| 391 | const uintptr_t length_; |
| 392 | }; |
| 393 | |
Mathieu Chartier | 0b4cbd0 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 394 | std::ostream& operator<<(std::ostream& os, const RelocationRange& reloc) { |
| 395 | return os << "(" << reinterpret_cast<const void*>(reloc.Source()) << "-" |
| 396 | << reinterpret_cast<const void*>(reloc.Source() + reloc.Length()) << ")->(" |
| 397 | << reinterpret_cast<const void*>(reloc.Dest()) << "-" |
| 398 | << reinterpret_cast<const void*>(reloc.Dest() + reloc.Length()) << ")"; |
| 399 | } |
| 400 | |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 401 | template <PointerSize kPointerSize, typename HeapVisitor, typename NativeVisitor> |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 402 | class ImageSpace::PatchObjectVisitor final { |
| 403 | public: |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 404 | explicit PatchObjectVisitor(HeapVisitor heap_visitor, NativeVisitor native_visitor) |
| 405 | : heap_visitor_(heap_visitor), native_visitor_(native_visitor) {} |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 406 | |
| 407 | void VisitClass(mirror::Class* klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 408 | // A mirror::Class object consists of |
| 409 | // - instance fields inherited from j.l.Object, |
| 410 | // - instance fields inherited from j.l.Class, |
| 411 | // - embedded tables (vtable, interface method table), |
| 412 | // - static fields of the class itself. |
| 413 | // The reference fields are at the start of each field section (this is how the |
| 414 | // ClassLinker orders fields; except when that would create a gap between superclass |
| 415 | // fields and the first reference of the subclass due to alignment, it can be filled |
| 416 | // with smaller fields - but that's not the case for j.l.Object and j.l.Class). |
| 417 | |
| 418 | DCHECK_ALIGNED(klass, kObjectAlignment); |
| 419 | static_assert(IsAligned<kHeapReferenceSize>(kObjectAlignment), "Object alignment check."); |
| 420 | // First, patch the `klass->klass_`, known to be a reference to the j.l.Class.class. |
| 421 | // This should be the only reference field in j.l.Object and we assert that below. |
| 422 | PatchReferenceField</*kMayBeNull=*/ false>(klass, mirror::Object::ClassOffset()); |
| 423 | // Then patch the reference instance fields described by j.l.Class.class. |
| 424 | // Use the sizeof(Object) to determine where these reference fields start; |
| 425 | // this is the same as `class_class->GetFirstReferenceInstanceFieldOffset()` |
| 426 | // after patching but the j.l.Class may not have been patched yet. |
| 427 | mirror::Class* class_class = klass->GetClass<kVerifyNone, kWithoutReadBarrier>(); |
| 428 | size_t num_reference_instance_fields = class_class->NumReferenceInstanceFields<kVerifyNone>(); |
| 429 | DCHECK_NE(num_reference_instance_fields, 0u); |
| 430 | static_assert(IsAligned<kHeapReferenceSize>(sizeof(mirror::Object)), "Size alignment check."); |
| 431 | MemberOffset instance_field_offset(sizeof(mirror::Object)); |
| 432 | for (size_t i = 0; i != num_reference_instance_fields; ++i) { |
| 433 | PatchReferenceField(klass, instance_field_offset); |
| 434 | static_assert(sizeof(mirror::HeapReference<mirror::Object>) == kHeapReferenceSize, |
| 435 | "Heap reference sizes equality check."); |
| 436 | instance_field_offset = |
| 437 | MemberOffset(instance_field_offset.Uint32Value() + kHeapReferenceSize); |
| 438 | } |
| 439 | // Now that we have patched the `super_class_`, if this is the j.l.Class.class, |
| 440 | // we can get a reference to j.l.Object.class and assert that it has only one |
| 441 | // reference instance field (the `klass_` patched above). |
| 442 | if (kIsDebugBuild && klass == class_class) { |
| 443 | ObjPtr<mirror::Class> object_class = |
| 444 | klass->GetSuperClass<kVerifyNone, kWithoutReadBarrier>(); |
| 445 | CHECK_EQ(object_class->NumReferenceInstanceFields<kVerifyNone>(), 1u); |
| 446 | } |
| 447 | // Then patch static fields. |
| 448 | size_t num_reference_static_fields = klass->NumReferenceStaticFields<kVerifyNone>(); |
| 449 | if (num_reference_static_fields != 0u) { |
| 450 | MemberOffset static_field_offset = |
| 451 | klass->GetFirstReferenceStaticFieldOffset<kVerifyNone>(kPointerSize); |
| 452 | for (size_t i = 0; i != num_reference_static_fields; ++i) { |
| 453 | PatchReferenceField(klass, static_field_offset); |
| 454 | static_assert(sizeof(mirror::HeapReference<mirror::Object>) == kHeapReferenceSize, |
| 455 | "Heap reference sizes equality check."); |
| 456 | static_field_offset = |
| 457 | MemberOffset(static_field_offset.Uint32Value() + kHeapReferenceSize); |
| 458 | } |
| 459 | } |
| 460 | // Then patch native pointers. |
| 461 | klass->FixupNativePointers<kVerifyNone>(klass, kPointerSize, *this); |
| 462 | } |
| 463 | |
| 464 | template <typename T> |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 465 | T* operator()(T* ptr, void** dest_addr ATTRIBUTE_UNUSED) const { |
| 466 | return (ptr != nullptr) ? native_visitor_(ptr) : nullptr; |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | void VisitPointerArray(mirror::PointerArray* pointer_array) |
| 470 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 471 | // Fully patch the pointer array, including the `klass_` field. |
| 472 | PatchReferenceField</*kMayBeNull=*/ false>(pointer_array, mirror::Object::ClassOffset()); |
| 473 | |
| 474 | int32_t length = pointer_array->GetLength<kVerifyNone>(); |
| 475 | for (int32_t i = 0; i != length; ++i) { |
| 476 | ArtMethod** method_entry = reinterpret_cast<ArtMethod**>( |
| 477 | pointer_array->ElementAddress<kVerifyNone>(i, kPointerSize)); |
| 478 | PatchNativePointer</*kMayBeNull=*/ false>(method_entry); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | void VisitObject(mirror::Object* object) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 483 | // Visit all reference fields. |
| 484 | object->VisitReferences</*kVisitNativeRoots=*/ false, |
| 485 | kVerifyNone, |
| 486 | kWithoutReadBarrier>(*this, *this); |
| 487 | // This function should not be called for classes. |
| 488 | DCHECK(!object->IsClass<kVerifyNone>()); |
| 489 | } |
| 490 | |
| 491 | // Visitor for VisitReferences(). |
| 492 | ALWAYS_INLINE void operator()(mirror::Object* object, MemberOffset field_offset, bool is_static) |
| 493 | const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 494 | DCHECK(!is_static); |
| 495 | PatchReferenceField(object, field_offset); |
| 496 | } |
| 497 | // Visitor for VisitReferences(), java.lang.ref.Reference case. |
| 498 | ALWAYS_INLINE void operator()(ObjPtr<mirror::Class> klass, mirror::Reference* ref) const |
| 499 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 500 | DCHECK(klass->IsTypeOfReferenceClass()); |
| 501 | this->operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false); |
| 502 | } |
| 503 | // Ignore class native roots; not called from VisitReferences() for kVisitNativeRoots == false. |
| 504 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 505 | const {} |
| 506 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 507 | |
| 508 | void VisitDexCacheArrays(mirror::DexCache* dex_cache) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 509 | FixupDexCacheArray<mirror::StringDexCacheType>(dex_cache, |
| 510 | mirror::DexCache::StringsOffset(), |
| 511 | dex_cache->NumStrings<kVerifyNone>()); |
| 512 | FixupDexCacheArray<mirror::TypeDexCacheType>(dex_cache, |
| 513 | mirror::DexCache::ResolvedTypesOffset(), |
| 514 | dex_cache->NumResolvedTypes<kVerifyNone>()); |
| 515 | FixupDexCacheArray<mirror::MethodDexCacheType>(dex_cache, |
| 516 | mirror::DexCache::ResolvedMethodsOffset(), |
| 517 | dex_cache->NumResolvedMethods<kVerifyNone>()); |
| 518 | FixupDexCacheArray<mirror::FieldDexCacheType>(dex_cache, |
| 519 | mirror::DexCache::ResolvedFieldsOffset(), |
| 520 | dex_cache->NumResolvedFields<kVerifyNone>()); |
| 521 | FixupDexCacheArray<mirror::MethodTypeDexCacheType>( |
| 522 | dex_cache, |
| 523 | mirror::DexCache::ResolvedMethodTypesOffset(), |
| 524 | dex_cache->NumResolvedMethodTypes<kVerifyNone>()); |
| 525 | FixupDexCacheArray<GcRoot<mirror::CallSite>>( |
| 526 | dex_cache, |
| 527 | mirror::DexCache::ResolvedCallSitesOffset(), |
| 528 | dex_cache->NumResolvedCallSites<kVerifyNone>()); |
| 529 | FixupDexCacheArray<GcRoot<mirror::String>>( |
| 530 | dex_cache, |
| 531 | mirror::DexCache::PreResolvedStringsOffset(), |
| 532 | dex_cache->NumPreResolvedStrings<kVerifyNone>()); |
| 533 | } |
| 534 | |
| 535 | template <bool kMayBeNull = true, typename T> |
| 536 | ALWAYS_INLINE void PatchGcRoot(/*inout*/GcRoot<T>* root) const |
| 537 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 538 | static_assert(sizeof(GcRoot<mirror::Class*>) == sizeof(uint32_t), "GcRoot size check"); |
| 539 | T* old_value = root->template Read<kWithoutReadBarrier>(); |
| 540 | DCHECK(kMayBeNull || old_value != nullptr); |
| 541 | if (!kMayBeNull || old_value != nullptr) { |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 542 | *root = GcRoot<T>(heap_visitor_(old_value)); |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | template <bool kMayBeNull = true, typename T> |
| 547 | ALWAYS_INLINE void PatchNativePointer(/*inout*/T** entry) const { |
| 548 | if (kPointerSize == PointerSize::k64) { |
| 549 | uint64_t* raw_entry = reinterpret_cast<uint64_t*>(entry); |
| 550 | T* old_value = reinterpret_cast64<T*>(*raw_entry); |
| 551 | DCHECK(kMayBeNull || old_value != nullptr); |
| 552 | if (!kMayBeNull || old_value != nullptr) { |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 553 | T* new_value = native_visitor_(old_value); |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 554 | *raw_entry = reinterpret_cast64<uint64_t>(new_value); |
| 555 | } |
| 556 | } else { |
| 557 | uint32_t* raw_entry = reinterpret_cast<uint32_t*>(entry); |
| 558 | T* old_value = reinterpret_cast32<T*>(*raw_entry); |
| 559 | DCHECK(kMayBeNull || old_value != nullptr); |
| 560 | if (!kMayBeNull || old_value != nullptr) { |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 561 | T* new_value = native_visitor_(old_value); |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 562 | *raw_entry = reinterpret_cast32<uint32_t>(new_value); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | template <bool kMayBeNull = true> |
| 568 | ALWAYS_INLINE void PatchReferenceField(mirror::Object* object, MemberOffset offset) const |
| 569 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 570 | mirror::Object* old_value = |
| 571 | object->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset); |
| 572 | DCHECK(kMayBeNull || old_value != nullptr); |
| 573 | if (!kMayBeNull || old_value != nullptr) { |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 574 | mirror::Object* new_value = heap_visitor_(old_value); |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 575 | object->SetFieldObjectWithoutWriteBarrier</*kTransactionActive=*/ false, |
| 576 | /*kCheckTransaction=*/ true, |
| 577 | kVerifyNone>(offset, new_value); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | template <typename T> |
| 582 | void FixupDexCacheArrayEntry(std::atomic<mirror::DexCachePair<T>>* array, uint32_t index) |
| 583 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 584 | static_assert(sizeof(std::atomic<mirror::DexCachePair<T>>) == sizeof(mirror::DexCachePair<T>), |
| 585 | "Size check for removing std::atomic<>."); |
| 586 | PatchGcRoot(&(reinterpret_cast<mirror::DexCachePair<T>*>(array)[index].object)); |
| 587 | } |
| 588 | |
| 589 | template <typename T> |
| 590 | void FixupDexCacheArrayEntry(std::atomic<mirror::NativeDexCachePair<T>>* array, uint32_t index) |
| 591 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 592 | static_assert(sizeof(std::atomic<mirror::NativeDexCachePair<T>>) == |
| 593 | sizeof(mirror::NativeDexCachePair<T>), |
| 594 | "Size check for removing std::atomic<>."); |
| 595 | mirror::NativeDexCachePair<T> pair = |
| 596 | mirror::DexCache::GetNativePairPtrSize(array, index, kPointerSize); |
| 597 | if (pair.object != nullptr) { |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 598 | pair.object = native_visitor_(pair.object); |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 599 | mirror::DexCache::SetNativePairPtrSize(array, index, pair, kPointerSize); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | void FixupDexCacheArrayEntry(GcRoot<mirror::CallSite>* array, uint32_t index) |
| 604 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 605 | PatchGcRoot(&array[index]); |
| 606 | } |
| 607 | |
| 608 | void FixupDexCacheArrayEntry(GcRoot<mirror::String>* array, uint32_t index) |
| 609 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 610 | PatchGcRoot(&array[index]); |
| 611 | } |
| 612 | |
| 613 | template <typename EntryType> |
| 614 | void FixupDexCacheArray(mirror::DexCache* dex_cache, |
| 615 | MemberOffset array_offset, |
| 616 | uint32_t size) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 617 | EntryType* old_array = |
| 618 | reinterpret_cast64<EntryType*>(dex_cache->GetField64<kVerifyNone>(array_offset)); |
| 619 | DCHECK_EQ(old_array != nullptr, size != 0u); |
| 620 | if (old_array != nullptr) { |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 621 | EntryType* new_array = native_visitor_(old_array); |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 622 | dex_cache->SetField64<kVerifyNone>(array_offset, reinterpret_cast64<uint64_t>(new_array)); |
| 623 | for (uint32_t i = 0; i != size; ++i) { |
| 624 | FixupDexCacheArrayEntry(new_array, i); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | private: |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 630 | // Heap objects visitor. |
| 631 | HeapVisitor heap_visitor_; |
| 632 | |
| 633 | // Native objects visitor. |
| 634 | NativeVisitor native_visitor_; |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 635 | }; |
| 636 | |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 637 | template <typename ReferenceVisitor> |
| 638 | class ImageSpace::ClassTableVisitor final { |
| 639 | public: |
| 640 | explicit ClassTableVisitor(const ReferenceVisitor& reference_visitor) |
| 641 | : reference_visitor_(reference_visitor) {} |
| 642 | |
| 643 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 644 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 645 | DCHECK(root->AsMirrorPtr() != nullptr); |
| 646 | root->Assign(reference_visitor_(root->AsMirrorPtr())); |
| 647 | } |
| 648 | |
| 649 | private: |
| 650 | ReferenceVisitor reference_visitor_; |
| 651 | }; |
| 652 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 653 | // Helper class encapsulating loading, so we can access private ImageSpace members (this is a |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 654 | // nested class), but not declare functions in the header. |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 655 | class ImageSpace::Loader { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 656 | public: |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 657 | static std::unique_ptr<ImageSpace> InitAppImage(const char* image_filename, |
| 658 | const char* image_location, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 659 | const OatFile* oat_file, |
| 660 | /*inout*/MemMap* image_reservation, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 661 | /*out*/std::string* error_msg) |
| 662 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 663 | TimingLogger logger(__PRETTY_FUNCTION__, /*precise=*/ true, VLOG_IS_ON(image)); |
Mathieu Chartier | 3ea4322 | 2018-12-08 20:44:50 -0800 | [diff] [blame] | 664 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 665 | std::unique_ptr<ImageSpace> space = Init(image_filename, |
| 666 | image_location, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 667 | oat_file, |
| 668 | &logger, |
| 669 | image_reservation, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 670 | error_msg); |
| 671 | if (space != nullptr) { |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 672 | uint32_t expected_reservation_size = |
| 673 | RoundUp(space->GetImageHeader().GetImageSize(), kPageSize); |
| 674 | if (!CheckImageReservationSize(*space, expected_reservation_size, error_msg) || |
| 675 | !CheckImageComponentCount(*space, /*expected_component_count=*/ 1u, error_msg)) { |
| 676 | return nullptr; |
| 677 | } |
| 678 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 679 | TimingLogger::ScopedTiming timing("RelocateImage", &logger); |
| 680 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(space->GetMemMap()->Begin()); |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 681 | const PointerSize pointer_size = image_header->GetPointerSize(); |
| 682 | bool result; |
| 683 | if (pointer_size == PointerSize::k64) { |
| 684 | result = RelocateInPlace<PointerSize::k64>(*image_header, |
| 685 | space->GetMemMap()->Begin(), |
| 686 | space->GetLiveBitmap(), |
| 687 | oat_file, |
| 688 | error_msg); |
| 689 | } else { |
| 690 | result = RelocateInPlace<PointerSize::k32>(*image_header, |
| 691 | space->GetMemMap()->Begin(), |
| 692 | space->GetLiveBitmap(), |
| 693 | oat_file, |
| 694 | error_msg); |
| 695 | } |
| 696 | if (!result) { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 697 | return nullptr; |
| 698 | } |
| 699 | Runtime* runtime = Runtime::Current(); |
| 700 | CHECK_EQ(runtime->GetResolutionMethod(), |
| 701 | image_header->GetImageMethod(ImageHeader::kResolutionMethod)); |
| 702 | CHECK_EQ(runtime->GetImtConflictMethod(), |
| 703 | image_header->GetImageMethod(ImageHeader::kImtConflictMethod)); |
| 704 | CHECK_EQ(runtime->GetImtUnimplementedMethod(), |
| 705 | image_header->GetImageMethod(ImageHeader::kImtUnimplementedMethod)); |
| 706 | CHECK_EQ(runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves), |
| 707 | image_header->GetImageMethod(ImageHeader::kSaveAllCalleeSavesMethod)); |
| 708 | CHECK_EQ(runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly), |
| 709 | image_header->GetImageMethod(ImageHeader::kSaveRefsOnlyMethod)); |
| 710 | CHECK_EQ(runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs), |
| 711 | image_header->GetImageMethod(ImageHeader::kSaveRefsAndArgsMethod)); |
| 712 | CHECK_EQ(runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything), |
| 713 | image_header->GetImageMethod(ImageHeader::kSaveEverythingMethod)); |
| 714 | CHECK_EQ(runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForClinit), |
| 715 | image_header->GetImageMethod(ImageHeader::kSaveEverythingMethodForClinit)); |
| 716 | CHECK_EQ(runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForSuspendCheck), |
| 717 | image_header->GetImageMethod(ImageHeader::kSaveEverythingMethodForSuspendCheck)); |
| 718 | |
| 719 | VLOG(image) << "ImageSpace::Loader::InitAppImage exiting " << *space.get(); |
Mathieu Chartier | 3f1fec6 | 2018-10-17 09:14:05 -0700 | [diff] [blame] | 720 | } |
| 721 | if (VLOG_IS_ON(image)) { |
| 722 | logger.Dump(LOG_STREAM(INFO)); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 723 | } |
| 724 | return space; |
| 725 | } |
| 726 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 727 | static std::unique_ptr<ImageSpace> Init(const char* image_filename, |
| 728 | const char* image_location, |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 729 | const OatFile* oat_file, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 730 | TimingLogger* logger, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 731 | /*inout*/MemMap* image_reservation, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 732 | /*out*/std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 733 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 734 | CHECK(image_filename != nullptr); |
| 735 | CHECK(image_location != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 736 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 737 | VLOG(image) << "ImageSpace::Init entering image_filename=" << image_filename; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 738 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 739 | std::unique_ptr<File> file; |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 740 | { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 741 | TimingLogger::ScopedTiming timing("OpenImageFile", logger); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 742 | file.reset(OS::OpenFileForReading(image_filename)); |
| 743 | if (file == nullptr) { |
| 744 | *error_msg = StringPrintf("Failed to open '%s'", image_filename); |
| 745 | return nullptr; |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 746 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 747 | } |
| 748 | ImageHeader temp_image_header; |
| 749 | ImageHeader* image_header = &temp_image_header; |
| 750 | { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 751 | TimingLogger::ScopedTiming timing("ReadImageHeader", logger); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 752 | bool success = file->ReadFully(image_header, sizeof(*image_header)); |
| 753 | if (!success || !image_header->IsValid()) { |
| 754 | *error_msg = StringPrintf("Invalid image header in '%s'", image_filename); |
| 755 | return nullptr; |
| 756 | } |
| 757 | } |
| 758 | // Check that the file is larger or equal to the header size + data size. |
| 759 | const uint64_t image_file_size = static_cast<uint64_t>(file->GetLength()); |
| 760 | if (image_file_size < sizeof(ImageHeader) + image_header->GetDataSize()) { |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 761 | *error_msg = StringPrintf( |
| 762 | "Image file truncated: %" PRIu64 " vs. %" PRIu64 ".", |
| 763 | image_file_size, |
| 764 | static_cast<uint64_t>(sizeof(ImageHeader) + image_header->GetDataSize())); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 765 | return nullptr; |
| 766 | } |
| 767 | |
| 768 | if (oat_file != nullptr) { |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 769 | // If we have an oat file (i.e. for app image), check the oat file checksum. |
| 770 | // Otherwise, we open the oat file after the image and check the checksum there. |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 771 | const uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum(); |
| 772 | const uint32_t image_oat_checksum = image_header->GetOatChecksum(); |
| 773 | if (oat_checksum != image_oat_checksum) { |
| 774 | *error_msg = StringPrintf("Oat checksum 0x%x does not match the image one 0x%x in image %s", |
| 775 | oat_checksum, |
| 776 | image_oat_checksum, |
| 777 | image_filename); |
| 778 | return nullptr; |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 782 | if (VLOG_IS_ON(startup)) { |
| 783 | LOG(INFO) << "Dumping image sections"; |
| 784 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 785 | const auto section_idx = static_cast<ImageHeader::ImageSections>(i); |
| 786 | auto& section = image_header->GetImageSection(section_idx); |
| 787 | LOG(INFO) << section_idx << " start=" |
| 788 | << reinterpret_cast<void*>(image_header->GetImageBegin() + section.Offset()) << " " |
| 789 | << section; |
Mathieu Chartier | 92ec594 | 2016-04-11 12:03:48 -0700 | [diff] [blame] | 790 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 793 | const auto& bitmap_section = image_header->GetImageBitmapSection(); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 794 | // The location we want to map from is the first aligned page after the end of the stored |
| 795 | // (possibly compressed) data. |
| 796 | const size_t image_bitmap_offset = RoundUp(sizeof(ImageHeader) + image_header->GetDataSize(), |
| 797 | kPageSize); |
| 798 | const size_t end_of_bitmap = image_bitmap_offset + bitmap_section.Size(); |
Vladimir Marko | d68ab24 | 2018-10-18 16:07:10 +0100 | [diff] [blame] | 799 | if (end_of_bitmap != image_file_size) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 800 | *error_msg = StringPrintf( |
Vladimir Marko | d68ab24 | 2018-10-18 16:07:10 +0100 | [diff] [blame] | 801 | "Image file size does not equal end of bitmap: size=%" PRIu64 " vs. %zu.", |
Vladimir Marko | 6121aa6 | 2018-07-06 10:04:35 +0100 | [diff] [blame] | 802 | image_file_size, |
Vladimir Marko | d68ab24 | 2018-10-18 16:07:10 +0100 | [diff] [blame] | 803 | end_of_bitmap); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 804 | return nullptr; |
| 805 | } |
| 806 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 807 | // GetImageBegin is the preferred address to map the image. If we manage to map the |
| 808 | // image at the image begin, the amount of fixup work required is minimized. |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 809 | // If it is pic we will retry with error_msg for the2 failure case. Pass a null error_msg to |
Mathieu Chartier | 66b1d57 | 2017-02-10 18:41:39 -0800 | [diff] [blame] | 810 | // avoid reading proc maps for a mapping failure and slowing everything down. |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 811 | // For the boot image, we have already reserved the memory and we load the image |
| 812 | // into the `image_reservation`. |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 813 | MemMap map = LoadImageFile( |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 814 | image_filename, |
| 815 | image_location, |
| 816 | *image_header, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 817 | file->Fd(), |
| 818 | logger, |
| 819 | image_reservation, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 820 | error_msg); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 821 | if (!map.IsValid()) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 822 | DCHECK(!error_msg->empty()); |
| 823 | return nullptr; |
| 824 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 825 | DCHECK_EQ(0, memcmp(image_header, map.Begin(), sizeof(ImageHeader))); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 826 | |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 827 | MemMap image_bitmap_map = MemMap::MapFile(bitmap_section.Size(), |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 828 | PROT_READ, |
| 829 | MAP_PRIVATE, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 830 | file->Fd(), |
| 831 | image_bitmap_offset, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 832 | /*low_4gb=*/ false, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 833 | image_filename, |
| 834 | error_msg); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 835 | if (!image_bitmap_map.IsValid()) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 836 | *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str()); |
| 837 | return nullptr; |
| 838 | } |
| 839 | // Loaded the map, use the image header from the file now in case we patch it with |
| 840 | // RelocateInPlace. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 841 | image_header = reinterpret_cast<ImageHeader*>(map.Begin()); |
Hans Boehm | fb8b4e2 | 2018-09-05 16:45:42 -0700 | [diff] [blame] | 842 | const uint32_t bitmap_index = ImageSpace::bitmap_index_.fetch_add(1); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 843 | std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", |
| 844 | image_filename, |
| 845 | bitmap_index)); |
| 846 | // Bitmap only needs to cover until the end of the mirror objects section. |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 847 | const ImageSection& image_objects = image_header->GetObjectsSection(); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 848 | // We only want the mirror object, not the ArtFields and ArtMethods. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 849 | uint8_t* const image_end = map.Begin() + image_objects.End(); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 850 | std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap; |
| 851 | { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 852 | TimingLogger::ScopedTiming timing("CreateImageBitmap", logger); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 853 | bitmap.reset( |
| 854 | accounting::ContinuousSpaceBitmap::CreateFromMemMap( |
| 855 | bitmap_name, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 856 | std::move(image_bitmap_map), |
| 857 | reinterpret_cast<uint8_t*>(map.Begin()), |
Mathieu Chartier | 612ff54 | 2017-05-01 09:59:24 -0700 | [diff] [blame] | 858 | // Make sure the bitmap is aligned to card size instead of just bitmap word size. |
| 859 | RoundUp(image_objects.End(), gc::accounting::CardTable::kCardSize))); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 860 | if (bitmap == nullptr) { |
| 861 | *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str()); |
| 862 | return nullptr; |
| 863 | } |
| 864 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 865 | // We only want the mirror object, not the ArtFields and ArtMethods. |
| 866 | std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, |
| 867 | image_location, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 868 | std::move(map), |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 869 | std::move(bitmap), |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 870 | image_end)); |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 871 | space->oat_file_non_owned_ = oat_file; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 872 | return space; |
| 873 | } |
| 874 | |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 875 | static bool CheckImageComponentCount(const ImageSpace& space, |
| 876 | uint32_t expected_component_count, |
| 877 | /*out*/std::string* error_msg) { |
| 878 | const ImageHeader& header = space.GetImageHeader(); |
| 879 | if (header.GetComponentCount() != expected_component_count) { |
| 880 | *error_msg = StringPrintf("Unexpected component count in %s, received %u, expected %u", |
| 881 | space.GetImageFilename().c_str(), |
| 882 | header.GetComponentCount(), |
| 883 | expected_component_count); |
| 884 | return false; |
| 885 | } |
| 886 | return true; |
| 887 | } |
| 888 | |
| 889 | static bool CheckImageReservationSize(const ImageSpace& space, |
| 890 | uint32_t expected_reservation_size, |
| 891 | /*out*/std::string* error_msg) { |
| 892 | const ImageHeader& header = space.GetImageHeader(); |
| 893 | if (header.GetImageReservationSize() != expected_reservation_size) { |
| 894 | *error_msg = StringPrintf("Unexpected reservation size in %s, received %u, expected %u", |
| 895 | space.GetImageFilename().c_str(), |
| 896 | header.GetImageReservationSize(), |
| 897 | expected_reservation_size); |
| 898 | return false; |
| 899 | } |
| 900 | return true; |
| 901 | } |
| 902 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 903 | private: |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 904 | static MemMap LoadImageFile(const char* image_filename, |
| 905 | const char* image_location, |
| 906 | const ImageHeader& image_header, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 907 | int fd, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 908 | TimingLogger* logger, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 909 | /*inout*/MemMap* image_reservation, |
| 910 | /*out*/std::string* error_msg) { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 911 | TimingLogger::ScopedTiming timing("MapImageFile", logger); |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 912 | std::string temp_error_msg; |
| 913 | const bool is_compressed = image_header.HasCompressedBlock(); |
| 914 | if (!is_compressed) { |
Vladimir Marko | 1130659 | 2018-10-26 14:22:59 +0100 | [diff] [blame] | 915 | uint8_t* address = (image_reservation != nullptr) ? image_reservation->Begin() : nullptr; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 916 | return MemMap::MapFileAtAddress(address, |
| 917 | image_header.GetImageSize(), |
| 918 | PROT_READ | PROT_WRITE, |
| 919 | MAP_PRIVATE, |
| 920 | fd, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 921 | /*start=*/ 0, |
| 922 | /*low_4gb=*/ true, |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 923 | image_filename, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 924 | /*reuse=*/ false, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 925 | image_reservation, |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 926 | error_msg); |
| 927 | } |
| 928 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 929 | // Reserve output and decompress into it. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 930 | MemMap map = MemMap::MapAnonymous(image_location, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 931 | image_header.GetImageSize(), |
| 932 | PROT_READ | PROT_WRITE, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 933 | /*low_4gb=*/ true, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 934 | image_reservation, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 935 | error_msg); |
| 936 | if (map.IsValid()) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 937 | const size_t stored_size = image_header.GetDataSize(); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 938 | MemMap temp_map = MemMap::MapFile(sizeof(ImageHeader) + stored_size, |
| 939 | PROT_READ, |
| 940 | MAP_PRIVATE, |
| 941 | fd, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 942 | /*start=*/ 0, |
| 943 | /*low_4gb=*/ false, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 944 | image_filename, |
| 945 | error_msg); |
| 946 | if (!temp_map.IsValid()) { |
Mathieu Chartier | 66b1d57 | 2017-02-10 18:41:39 -0800 | [diff] [blame] | 947 | DCHECK(error_msg == nullptr || !error_msg->empty()); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 948 | return MemMap::Invalid(); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 949 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 950 | memcpy(map.Begin(), &image_header, sizeof(ImageHeader)); |
Mathieu Chartier | c6068c7 | 2018-11-13 16:00:58 -0800 | [diff] [blame] | 951 | |
Mathieu Chartier | ada33d7 | 2018-12-17 13:17:30 -0800 | [diff] [blame] | 952 | Runtime::ScopedThreadPoolUsage stpu; |
| 953 | ThreadPool* const pool = stpu.GetThreadPool(); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 954 | const uint64_t start = NanoTime(); |
Mathieu Chartier | c6068c7 | 2018-11-13 16:00:58 -0800 | [diff] [blame] | 955 | Thread* const self = Thread::Current(); |
Mathieu Chartier | ada33d7 | 2018-12-17 13:17:30 -0800 | [diff] [blame] | 956 | static constexpr size_t kMinBlocks = 2u; |
| 957 | const bool use_parallel = pool != nullptr && image_header.GetBlockCount() >= kMinBlocks; |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 958 | for (const ImageHeader::Block& block : image_header.GetBlocks(temp_map.Begin())) { |
Mathieu Chartier | c6068c7 | 2018-11-13 16:00:58 -0800 | [diff] [blame] | 959 | auto function = [&](Thread*) { |
| 960 | const uint64_t start2 = NanoTime(); |
| 961 | ScopedTrace trace("LZ4 decompress block"); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 962 | bool result = block.Decompress(/*out_ptr=*/map.Begin(), |
| 963 | /*in_ptr=*/temp_map.Begin(), |
| 964 | error_msg); |
| 965 | if (!result && error_msg != nullptr) { |
| 966 | *error_msg = "Failed to decompress image block " + *error_msg; |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 967 | } |
Mathieu Chartier | c6068c7 | 2018-11-13 16:00:58 -0800 | [diff] [blame] | 968 | VLOG(image) << "Decompress block " << block.GetDataSize() << " -> " |
| 969 | << block.GetImageSize() << " in " << PrettyDuration(NanoTime() - start2); |
| 970 | }; |
| 971 | if (use_parallel) { |
| 972 | pool->AddTask(self, new FunctionTask(std::move(function))); |
| 973 | } else { |
| 974 | function(self); |
Mathieu Chartier | 1a84296 | 2018-11-13 15:09:51 -0800 | [diff] [blame] | 975 | } |
| 976 | } |
Mathieu Chartier | c6068c7 | 2018-11-13 16:00:58 -0800 | [diff] [blame] | 977 | if (use_parallel) { |
| 978 | ScopedTrace trace("Waiting for workers"); |
| 979 | pool->Wait(self, true, false); |
| 980 | } |
Mathieu Chartier | 0d4d291 | 2017-02-10 17:22:41 -0800 | [diff] [blame] | 981 | const uint64_t time = NanoTime() - start; |
| 982 | // Add one 1 ns to prevent possible divide by 0. |
| 983 | VLOG(image) << "Decompressing image took " << PrettyDuration(time) << " (" |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 984 | << PrettySize(static_cast<uint64_t>(map.Size()) * MsToNs(1000) / (time + 1)) |
Mathieu Chartier | 0d4d291 | 2017-02-10 17:22:41 -0800 | [diff] [blame] | 985 | << "/s)"; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 988 | return map; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 989 | } |
| 990 | |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 991 | class EmptyRange { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 992 | public: |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 993 | ALWAYS_INLINE bool InSource(uintptr_t) const { return false; } |
| 994 | ALWAYS_INLINE bool InDest(uintptr_t) const { return false; } |
| 995 | ALWAYS_INLINE uintptr_t ToDest(uintptr_t) const { UNREACHABLE(); } |
| 996 | }; |
| 997 | |
| 998 | template <typename Range0, typename Range1 = EmptyRange, typename Range2 = EmptyRange> |
| 999 | class ForwardAddress { |
| 1000 | public: |
| 1001 | ForwardAddress(const Range0& range0 = Range0(), |
| 1002 | const Range1& range1 = Range1(), |
| 1003 | const Range2& range2 = Range2()) |
| 1004 | : range0_(range0), range1_(range1), range2_(range2) {} |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1005 | |
| 1006 | // Return the relocated address of a heap object. |
Mathieu Chartier | 9a3da9a | 2018-12-21 12:56:55 -0800 | [diff] [blame] | 1007 | // Null checks must be performed in the caller (for performance reasons). |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1008 | template <typename T> |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1009 | ALWAYS_INLINE T* operator()(T* src) const { |
Mathieu Chartier | 9a3da9a | 2018-12-21 12:56:55 -0800 | [diff] [blame] | 1010 | DCHECK(src != nullptr); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1011 | const uintptr_t uint_src = reinterpret_cast<uintptr_t>(src); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1012 | if (range2_.InSource(uint_src)) { |
| 1013 | return reinterpret_cast<T*>(range2_.ToDest(uint_src)); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1014 | } |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1015 | if (range1_.InSource(uint_src)) { |
| 1016 | return reinterpret_cast<T*>(range1_.ToDest(uint_src)); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1017 | } |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1018 | CHECK(range0_.InSource(uint_src)) |
| 1019 | << reinterpret_cast<const void*>(src) << " not in " |
| 1020 | << reinterpret_cast<const void*>(range0_.Source()) << "-" |
| 1021 | << reinterpret_cast<const void*>(range0_.Source() + range0_.Length()); |
| 1022 | return reinterpret_cast<T*>(range0_.ToDest(uint_src)); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1025 | private: |
| 1026 | const Range0 range0_; |
| 1027 | const Range1 range1_; |
| 1028 | const Range2 range2_; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1029 | }; |
| 1030 | |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1031 | template <typename Forward> |
| 1032 | class FixupRootVisitor { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1033 | public: |
| 1034 | template<typename... Args> |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1035 | explicit FixupRootVisitor(Args... args) : forward_(args...) {} |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1036 | |
| 1037 | ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1038 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1039 | if (!root->IsNull()) { |
| 1040 | VisitRoot(root); |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1045 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1046 | mirror::Object* ref = root->AsMirrorPtr(); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1047 | mirror::Object* new_ref = forward_(ref); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1048 | if (ref != new_ref) { |
| 1049 | root->Assign(new_ref); |
| 1050 | } |
| 1051 | } |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1052 | |
| 1053 | private: |
| 1054 | Forward forward_; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1055 | }; |
| 1056 | |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1057 | template <typename Forward> |
| 1058 | class FixupObjectVisitor { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1059 | public: |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1060 | explicit FixupObjectVisitor(gc::accounting::ContinuousSpaceBitmap* visited, |
| 1061 | const Forward& forward) |
| 1062 | : visited_(visited), forward_(forward) {} |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1063 | |
| 1064 | // Fix up separately since we also need to fix up method entrypoints. |
| 1065 | ALWAYS_INLINE void VisitRootIfNonNull( |
| 1066 | mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 1067 | |
| 1068 | ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 1069 | const {} |
| 1070 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1071 | ALWAYS_INLINE void operator()(ObjPtr<mirror::Object> obj, |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1072 | MemberOffset offset, |
| 1073 | bool is_static ATTRIBUTE_UNUSED) const |
| 1074 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1075 | // Space is not yet added to the heap, don't do a read barrier. |
| 1076 | mirror::Object* ref = obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>( |
| 1077 | offset); |
Mathieu Chartier | 9a3da9a | 2018-12-21 12:56:55 -0800 | [diff] [blame] | 1078 | if (ref != nullptr) { |
| 1079 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 1080 | // image. |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1081 | obj->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(offset, forward_(ref)); |
Mathieu Chartier | 9a3da9a | 2018-12-21 12:56:55 -0800 | [diff] [blame] | 1082 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | // java.lang.ref.Reference visitor. |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1086 | void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 1087 | ObjPtr<mirror::Reference> ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1088 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1089 | mirror::Object* obj = ref->GetReferent<kWithoutReadBarrier>(); |
Mathieu Chartier | 9a3da9a | 2018-12-21 12:56:55 -0800 | [diff] [blame] | 1090 | if (obj != nullptr) { |
| 1091 | ref->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
| 1092 | mirror::Reference::ReferentOffset(), |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1093 | forward_(obj)); |
Mathieu Chartier | 9a3da9a | 2018-12-21 12:56:55 -0800 | [diff] [blame] | 1094 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 1097 | void operator()(mirror::Object* obj) const |
| 1098 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1099 | if (!visited_->Set(obj)) { |
| 1100 | // Not already visited. |
| 1101 | obj->VisitReferences</*visit native roots*/false, kVerifyNone, kWithoutReadBarrier>( |
| 1102 | *this, |
| 1103 | *this); |
| 1104 | CHECK(!obj->IsClass()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1105 | } |
| 1106 | } |
Mathieu Chartier | 91edc62 | 2016-02-16 17:16:01 -0800 | [diff] [blame] | 1107 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1108 | private: |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1109 | gc::accounting::ContinuousSpaceBitmap* const visited_; |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1110 | Forward forward_; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1111 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1112 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1113 | // Relocate an image space mapped at target_base which possibly used to be at a different base |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1114 | // address. In place means modifying a single ImageSpace in place rather than relocating from |
| 1115 | // one ImageSpace to another. |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1116 | template <PointerSize kPointerSize> |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1117 | static bool RelocateInPlace(ImageHeader& image_header, |
| 1118 | uint8_t* target_base, |
| 1119 | accounting::ContinuousSpaceBitmap* bitmap, |
| 1120 | const OatFile* app_oat_file, |
| 1121 | std::string* error_msg) { |
| 1122 | DCHECK(error_msg != nullptr); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1123 | // Set up sections. |
| 1124 | uint32_t boot_image_begin = 0; |
| 1125 | uint32_t boot_image_end = 0; |
| 1126 | uint32_t boot_oat_begin = 0; |
| 1127 | uint32_t boot_oat_end = 0; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1128 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 1129 | heap->GetBootImagesSize(&boot_image_begin, &boot_image_end, &boot_oat_begin, &boot_oat_end); |
| 1130 | if (boot_image_begin == boot_image_end) { |
| 1131 | *error_msg = "Can not relocate app image without boot image space"; |
| 1132 | return false; |
| 1133 | } |
| 1134 | if (boot_oat_begin == boot_oat_end) { |
| 1135 | *error_msg = "Can not relocate app image without boot oat file"; |
| 1136 | return false; |
| 1137 | } |
Vladimir Marko | 0c78ef7 | 2018-11-21 14:09:35 +0000 | [diff] [blame] | 1138 | const uint32_t boot_image_size = boot_oat_end - boot_image_begin; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1139 | const uint32_t image_header_boot_image_size = image_header.GetBootImageSize(); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1140 | if (boot_image_size != image_header_boot_image_size) { |
| 1141 | *error_msg = StringPrintf("Boot image size %" PRIu64 " does not match expected size %" |
| 1142 | PRIu64, |
| 1143 | static_cast<uint64_t>(boot_image_size), |
| 1144 | static_cast<uint64_t>(image_header_boot_image_size)); |
| 1145 | return false; |
| 1146 | } |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1147 | const ImageSection& objects_section = image_header.GetObjectsSection(); |
| 1148 | // Where the app image objects are mapped to. |
| 1149 | uint8_t* objects_location = target_base + objects_section.Offset(); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1150 | TimingLogger logger(__FUNCTION__, true, false); |
| 1151 | RelocationRange boot_image(image_header.GetBootImageBegin(), |
| 1152 | boot_image_begin, |
| 1153 | boot_image_size); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1154 | // Metadata is everything after the objects section, use exclusion to be safe. |
| 1155 | RelocationRange app_image_metadata( |
| 1156 | reinterpret_cast<uintptr_t>(image_header.GetImageBegin()) + objects_section.End(), |
| 1157 | reinterpret_cast<uintptr_t>(target_base) + objects_section.End(), |
| 1158 | image_header.GetImageSize() - objects_section.End()); |
| 1159 | // App image heap objects, may be mapped in the heap. |
| 1160 | RelocationRange app_image_objects( |
| 1161 | reinterpret_cast<uintptr_t>(image_header.GetImageBegin()) + objects_section.Offset(), |
| 1162 | reinterpret_cast<uintptr_t>(objects_location), |
| 1163 | objects_section.Size()); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1164 | // Use the oat data section since this is where the OatFile::Begin is. |
| 1165 | RelocationRange app_oat(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()), |
| 1166 | // Not necessarily in low 4GB. |
| 1167 | reinterpret_cast<uintptr_t>(app_oat_file->Begin()), |
| 1168 | image_header.GetOatDataEnd() - image_header.GetOatDataBegin()); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1169 | VLOG(image) << "App image metadata " << app_image_metadata; |
| 1170 | VLOG(image) << "App image objects " << app_image_objects; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1171 | VLOG(image) << "App oat " << app_oat; |
| 1172 | VLOG(image) << "Boot image " << boot_image; |
Vladimir Marko | 0c78ef7 | 2018-11-21 14:09:35 +0000 | [diff] [blame] | 1173 | // True if we need to fixup any heap pointers. |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1174 | const bool fixup_image = boot_image.Delta() != 0 || app_image_metadata.Delta() != 0 || |
| 1175 | app_image_objects.Delta() != 0; |
Vladimir Marko | 0c78ef7 | 2018-11-21 14:09:35 +0000 | [diff] [blame] | 1176 | if (!fixup_image) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1177 | // Nothing to fix up. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1178 | return true; |
| 1179 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1180 | ScopedDebugDisallowReadBarriers sddrb(Thread::Current()); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1181 | |
| 1182 | using ForwardObject = ForwardAddress<RelocationRange, RelocationRange>; |
| 1183 | ForwardObject forward_object(boot_image, app_image_objects); |
| 1184 | ForwardObject forward_metadata(boot_image, app_image_metadata); |
| 1185 | using ForwardCode = ForwardAddress<RelocationRange, RelocationRange>; |
| 1186 | ForwardCode forward_code(boot_image, app_oat); |
| 1187 | PatchObjectVisitor<kPointerSize, ForwardObject, ForwardCode> patch_object_visitor( |
| 1188 | forward_object, |
| 1189 | forward_metadata); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1190 | if (fixup_image) { |
| 1191 | // Two pass approach, fix up all classes first, then fix up non class-objects. |
| 1192 | // The visited bitmap is used to ensure that pointer arrays are not forwarded twice. |
| 1193 | std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> visited_bitmap( |
| 1194 | gc::accounting::ContinuousSpaceBitmap::Create("Relocate bitmap", |
| 1195 | target_base, |
| 1196 | image_header.GetImageSize())); |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1197 | { |
| 1198 | TimingLogger::ScopedTiming timing("Fixup classes", &logger); |
| 1199 | const auto& class_table_section = image_header.GetClassTableSection(); |
| 1200 | if (class_table_section.Size() > 0u) { |
| 1201 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1202 | ClassTableVisitor class_table_visitor(forward_object); |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1203 | size_t read_count = 0u; |
| 1204 | const uint8_t* data = target_base + class_table_section.Offset(); |
| 1205 | // We avoid making a copy of the data since we want modifications to be propagated to the |
| 1206 | // memory map. |
| 1207 | ClassTable::ClassSet temp_set(data, /*make_copy_of_data=*/ false, &read_count); |
| 1208 | for (ClassTable::TableSlot& slot : temp_set) { |
| 1209 | slot.VisitRoot(class_table_visitor); |
| 1210 | mirror::Class* klass = slot.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1211 | if (!app_image_objects.InDest(klass)) { |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1212 | continue; |
| 1213 | } |
| 1214 | const bool already_marked = visited_bitmap->Set(klass); |
| 1215 | CHECK(!already_marked) << "App image class already visited"; |
| 1216 | patch_object_visitor.VisitClass(klass); |
| 1217 | // Then patch the non-embedded vtable and iftable. |
| 1218 | mirror::PointerArray* vtable = klass->GetVTable<kVerifyNone, kWithoutReadBarrier>(); |
| 1219 | if (vtable != nullptr && |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1220 | app_image_objects.InDest(vtable) && |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1221 | !visited_bitmap->Set(vtable)) { |
| 1222 | patch_object_visitor.VisitPointerArray(vtable); |
| 1223 | } |
| 1224 | auto* iftable = klass->GetIfTable<kVerifyNone, kWithoutReadBarrier>(); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1225 | if (iftable != nullptr && app_image_objects.InDest(iftable)) { |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1226 | // Avoid processing the fields of iftable since we will process them later anyways |
| 1227 | // below. |
| 1228 | int32_t ifcount = klass->GetIfTableCount<kVerifyNone>(); |
| 1229 | for (int32_t i = 0; i != ifcount; ++i) { |
| 1230 | mirror::PointerArray* unpatched_ifarray = |
| 1231 | iftable->GetMethodArrayOrNull<kVerifyNone, kWithoutReadBarrier>(i); |
| 1232 | if (unpatched_ifarray != nullptr) { |
| 1233 | // The iftable has not been patched, so we need to explicitly adjust the pointer. |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1234 | mirror::PointerArray* ifarray = forward_object(unpatched_ifarray); |
| 1235 | if (app_image_objects.InDest(ifarray) && !visited_bitmap->Set(ifarray)) { |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1236 | patch_object_visitor.VisitPointerArray(ifarray); |
| 1237 | } |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | // Fixup objects may read fields in the boot image, use the mutator lock here for sanity. |
| 1246 | // Though its probably not required. |
Mathieu Chartier | 2ffc74b | 2019-01-03 19:25:41 -0800 | [diff] [blame] | 1247 | TimingLogger::ScopedTiming timing("Fixup objects", &logger); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1248 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1249 | // Need to update the image to be at the target base. |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1250 | uintptr_t objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset()); |
| 1251 | uintptr_t objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End()); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1252 | FixupObjectVisitor<ForwardObject> fixup_object_visitor(visited_bitmap.get(), forward_object); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1253 | bitmap->VisitMarkedRange(objects_begin, objects_end, fixup_object_visitor); |
| 1254 | // Fixup image roots. |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1255 | CHECK(app_image_objects.InSource(reinterpret_cast<uintptr_t>( |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 1256 | image_header.GetImageRoots<kWithoutReadBarrier>().Ptr()))); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1257 | image_header.RelocateImageObjects(app_image_objects.Delta()); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1258 | CHECK_EQ(image_header.GetImageBegin(), target_base); |
| 1259 | // Fix up dex cache DexFile pointers. |
| 1260 | auto* dex_caches = image_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kDexCaches)-> |
Vladimir Marko | 98db89c | 2018-09-07 11:45:46 +0100 | [diff] [blame] | 1261 | AsObjectArray<mirror::DexCache, kVerifyNone>(); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1262 | for (int32_t i = 0, count = dex_caches->GetLength(); i < count; ++i) { |
| 1263 | mirror::DexCache* dex_cache = dex_caches->Get<kVerifyNone, kWithoutReadBarrier>(i); |
Mathieu Chartier | 25602dc | 2018-12-11 11:31:57 -0800 | [diff] [blame] | 1264 | CHECK(dex_cache != nullptr); |
| 1265 | patch_object_visitor.VisitDexCacheArrays(dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1266 | } |
| 1267 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1268 | { |
| 1269 | // Only touches objects in the app image, no need for mutator lock. |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1270 | TimingLogger::ScopedTiming timing("Fixup methods", &logger); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame^] | 1271 | image_header.VisitPackedArtMethods([&](ArtMethod& method) NO_THREAD_SAFETY_ANALYSIS { |
| 1272 | // TODO: Consider a separate visitor for runtime vs normal methods. |
| 1273 | if (UNLIKELY(method.IsRuntimeMethod())) { |
| 1274 | ImtConflictTable* table = method.GetImtConflictTable(kPointerSize); |
| 1275 | if (table != nullptr) { |
| 1276 | ImtConflictTable* new_table = forward_metadata(table); |
| 1277 | if (table != new_table) { |
| 1278 | method.SetImtConflictTable(new_table, kPointerSize); |
| 1279 | } |
| 1280 | } |
| 1281 | const void* old_code = method.GetEntryPointFromQuickCompiledCodePtrSize(kPointerSize); |
| 1282 | const void* new_code = forward_code(old_code); |
| 1283 | if (old_code != new_code) { |
| 1284 | method.SetEntryPointFromQuickCompiledCodePtrSize(new_code, kPointerSize); |
| 1285 | } |
| 1286 | } else { |
| 1287 | method.UpdateObjectsForImageRelocation(forward_object); |
| 1288 | method.UpdateEntrypoints(forward_code, kPointerSize); |
| 1289 | } |
| 1290 | }, target_base, kPointerSize); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1291 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1292 | if (fixup_image) { |
| 1293 | { |
| 1294 | // Only touches objects in the app image, no need for mutator lock. |
| 1295 | TimingLogger::ScopedTiming timing("Fixup fields", &logger); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame^] | 1296 | image_header.VisitPackedArtFields([&](ArtField& field) NO_THREAD_SAFETY_ANALYSIS { |
| 1297 | field.UpdateObjects(forward_object); |
| 1298 | }, target_base); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1299 | } |
| 1300 | { |
| 1301 | TimingLogger::ScopedTiming timing("Fixup imt", &logger); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1302 | image_header.VisitPackedImTables(forward_metadata, target_base, kPointerSize); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1303 | } |
| 1304 | { |
| 1305 | TimingLogger::ScopedTiming timing("Fixup conflict tables", &logger); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1306 | image_header.VisitPackedImtConflictTables(forward_metadata, target_base, kPointerSize); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1307 | } |
| 1308 | // In the app image case, the image methods are actually in the boot image. |
| 1309 | image_header.RelocateImageMethods(boot_image.Delta()); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1310 | // Fix up the intern table. |
| 1311 | const auto& intern_table_section = image_header.GetInternedStringsSection(); |
| 1312 | if (intern_table_section.Size() > 0u) { |
| 1313 | TimingLogger::ScopedTiming timing("Fixup intern table", &logger); |
| 1314 | ScopedObjectAccess soa(Thread::Current()); |
| 1315 | // Fixup the pointers in the newly written intern table to contain image addresses. |
| 1316 | InternTable temp_intern_table; |
| 1317 | // Note that we require that ReadFromMemory does not make an internal copy of the elements |
| 1318 | // so that the VisitRoots() will update the memory directly rather than the copies. |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1319 | temp_intern_table.AddTableFromMemory(target_base + intern_table_section.Offset(), |
| 1320 | [&](InternTable::UnorderedSet& strings) |
| 1321 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1322 | for (GcRoot<mirror::String>& root : strings) { |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1323 | root = GcRoot<mirror::String>(forward_object(root.Read<kWithoutReadBarrier>())); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1324 | } |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 1325 | }, /*is_boot_image=*/ false); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1326 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1327 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1328 | if (VLOG_IS_ON(image)) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 1329 | logger.Dump(LOG_STREAM(INFO)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1330 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1331 | return true; |
Andreas Gampe | 7fa5578 | 2016-06-15 17:45:01 -0700 | [diff] [blame] | 1332 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1333 | }; |
Hiroshi Yamauchi | bd0fb61 | 2014-05-20 13:46:00 -0700 | [diff] [blame] | 1334 | |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1335 | class ImageSpace::BootImageLoader { |
| 1336 | public: |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 1337 | BootImageLoader(const std::vector<std::string>& boot_class_path, |
| 1338 | const std::vector<std::string>& boot_class_path_locations, |
| 1339 | const std::string& image_location, |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1340 | InstructionSet image_isa, |
| 1341 | bool relocate, |
| 1342 | bool executable, |
| 1343 | bool is_zygote) |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 1344 | : boot_class_path_(boot_class_path), |
| 1345 | boot_class_path_locations_(boot_class_path_locations), |
| 1346 | image_location_(image_location), |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1347 | image_isa_(image_isa), |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1348 | relocate_(relocate), |
| 1349 | executable_(executable), |
| 1350 | is_zygote_(is_zygote), |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1351 | has_system_(false), |
| 1352 | has_cache_(false), |
| 1353 | is_global_cache_(true), |
Vladimir Marko | e307002 | 2018-08-22 09:36:19 +0000 | [diff] [blame] | 1354 | dalvik_cache_exists_(false), |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1355 | dalvik_cache_(), |
| 1356 | cache_filename_() { |
| 1357 | } |
| 1358 | |
| 1359 | bool IsZygote() const { return is_zygote_; } |
| 1360 | |
| 1361 | void FindImageFiles() { |
| 1362 | std::string system_filename; |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1363 | bool found_image = FindImageFilenameImpl(image_location_.c_str(), |
| 1364 | image_isa_, |
| 1365 | &has_system_, |
| 1366 | &system_filename, |
Vladimir Marko | e307002 | 2018-08-22 09:36:19 +0000 | [diff] [blame] | 1367 | &dalvik_cache_exists_, |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1368 | &dalvik_cache_, |
| 1369 | &is_global_cache_, |
| 1370 | &has_cache_, |
| 1371 | &cache_filename_); |
Vladimir Marko | e307002 | 2018-08-22 09:36:19 +0000 | [diff] [blame] | 1372 | DCHECK(!dalvik_cache_exists_ || !dalvik_cache_.empty()); |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1373 | DCHECK_EQ(found_image, has_system_ || has_cache_); |
| 1374 | } |
| 1375 | |
| 1376 | bool HasSystem() const { return has_system_; } |
| 1377 | bool HasCache() const { return has_cache_; } |
| 1378 | |
Vladimir Marko | e307002 | 2018-08-22 09:36:19 +0000 | [diff] [blame] | 1379 | bool DalvikCacheExists() const { return dalvik_cache_exists_; } |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1380 | bool IsGlobalCache() const { return is_global_cache_; } |
| 1381 | |
| 1382 | const std::string& GetDalvikCache() const { |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1383 | return dalvik_cache_; |
| 1384 | } |
| 1385 | |
| 1386 | const std::string& GetCacheFilename() const { |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1387 | return cache_filename_; |
| 1388 | } |
| 1389 | |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 1390 | bool LoadFromSystem(bool validate_oat_file, |
| 1391 | size_t extra_reservation_size, |
Vladimir Marko | d44d703 | 2018-08-30 13:02:31 +0100 | [diff] [blame] | 1392 | /*out*/std::vector<std::unique_ptr<space::ImageSpace>>* boot_image_spaces, |
| 1393 | /*out*/MemMap* extra_reservation, |
| 1394 | /*out*/std::string* error_msg) REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 1395 | TimingLogger logger(__PRETTY_FUNCTION__, /*precise=*/ true, VLOG_IS_ON(image)); |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1396 | std::string filename = GetSystemImageFilename(image_location_.c_str(), image_isa_); |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1397 | |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 1398 | if (!LoadFromFile(filename, |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 1399 | validate_oat_file, |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 1400 | extra_reservation_size, |
| 1401 | &logger, |
| 1402 | boot_image_spaces, |
| 1403 | extra_reservation, |
| 1404 | error_msg)) { |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1405 | return false; |
| 1406 | } |
| 1407 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1408 | if (VLOG_IS_ON(image)) { |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 1409 | LOG(INFO) << "ImageSpace::BootImageLoader::LoadFromSystem exiting " |
| 1410 | << boot_image_spaces->front(); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1411 | logger.Dump(LOG_STREAM(INFO)); |
| 1412 | } |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1413 | return true; |
| 1414 | } |
| 1415 | |
| 1416 | bool LoadFromDalvikCache( |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1417 | bool validate_oat_file, |
Vladimir Marko | d44d703 | 2018-08-30 13:02:31 +0100 | [diff] [blame] | 1418 | size_t extra_reservation_size, |
| 1419 | /*out*/std::vector<std::unique_ptr<space::ImageSpace>>* boot_image_spaces, |
| 1420 | /*out*/MemMap* extra_reservation, |
| 1421 | /*out*/std::string* error_msg) REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 1422 | TimingLogger logger(__PRETTY_FUNCTION__, /*precise=*/ true, VLOG_IS_ON(image)); |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1423 | DCHECK(DalvikCacheExists()); |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1424 | |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 1425 | if (!LoadFromFile(cache_filename_, |
| 1426 | validate_oat_file, |
| 1427 | extra_reservation_size, |
| 1428 | &logger, |
| 1429 | boot_image_spaces, |
| 1430 | extra_reservation, |
| 1431 | error_msg)) { |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1432 | return false; |
| 1433 | } |
| 1434 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1435 | if (VLOG_IS_ON(image)) { |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 1436 | LOG(INFO) << "ImageSpace::BootImageLoader::LoadFromDalvikCache exiting " |
| 1437 | << boot_image_spaces->front(); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1438 | logger.Dump(LOG_STREAM(INFO)); |
| 1439 | } |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1440 | return true; |
| 1441 | } |
| 1442 | |
| 1443 | private: |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 1444 | bool LoadFromFile( |
| 1445 | const std::string& filename, |
| 1446 | bool validate_oat_file, |
| 1447 | size_t extra_reservation_size, |
| 1448 | TimingLogger* logger, |
| 1449 | /*out*/std::vector<std::unique_ptr<space::ImageSpace>>* boot_image_spaces, |
| 1450 | /*out*/MemMap* extra_reservation, |
| 1451 | /*out*/std::string* error_msg) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1452 | ImageHeader system_hdr; |
| 1453 | if (!ReadSpecificImageHeader(filename.c_str(), &system_hdr)) { |
| 1454 | *error_msg = StringPrintf("Cannot read header of %s", filename.c_str()); |
| 1455 | return false; |
| 1456 | } |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 1457 | if (system_hdr.GetComponentCount() == 0u || |
| 1458 | system_hdr.GetComponentCount() > boot_class_path_.size()) { |
| 1459 | *error_msg = StringPrintf("Unexpected component count in %s, received %u, " |
| 1460 | "expected non-zero and <= %zu", |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 1461 | filename.c_str(), |
| 1462 | system_hdr.GetComponentCount(), |
| 1463 | boot_class_path_.size()); |
| 1464 | return false; |
| 1465 | } |
| 1466 | MemMap image_reservation; |
| 1467 | MemMap local_extra_reservation; |
| 1468 | if (!ReserveBootImageMemory(system_hdr.GetImageReservationSize(), |
| 1469 | reinterpret_cast32<uint32_t>(system_hdr.GetImageBegin()), |
| 1470 | extra_reservation_size, |
| 1471 | &image_reservation, |
| 1472 | &local_extra_reservation, |
| 1473 | error_msg)) { |
| 1474 | return false; |
| 1475 | } |
| 1476 | |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 1477 | ArrayRef<const std::string> provided_locations(boot_class_path_locations_.data(), |
| 1478 | system_hdr.GetComponentCount()); |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 1479 | std::vector<std::string> locations = |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 1480 | ExpandMultiImageLocations(provided_locations, image_location_); |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 1481 | std::vector<std::string> filenames = |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 1482 | ExpandMultiImageLocations(provided_locations, filename); |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 1483 | DCHECK_EQ(locations.size(), filenames.size()); |
| 1484 | std::vector<std::unique_ptr<ImageSpace>> spaces; |
| 1485 | spaces.reserve(locations.size()); |
| 1486 | for (std::size_t i = 0u, size = locations.size(); i != size; ++i) { |
| 1487 | spaces.push_back(Load(locations[i], filenames[i], logger, &image_reservation, error_msg)); |
| 1488 | const ImageSpace* space = spaces.back().get(); |
| 1489 | if (space == nullptr) { |
| 1490 | return false; |
| 1491 | } |
| 1492 | uint32_t expected_component_count = (i == 0u) ? system_hdr.GetComponentCount() : 0u; |
| 1493 | uint32_t expected_reservation_size = (i == 0u) ? system_hdr.GetImageReservationSize() : 0u; |
| 1494 | if (!Loader::CheckImageReservationSize(*space, expected_reservation_size, error_msg) || |
| 1495 | !Loader::CheckImageComponentCount(*space, expected_component_count, error_msg)) { |
| 1496 | return false; |
| 1497 | } |
| 1498 | } |
| 1499 | for (size_t i = 0u, size = spaces.size(); i != size; ++i) { |
| 1500 | std::string expected_boot_class_path = |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 1501 | (i == 0u) ? android::base::Join(provided_locations, ':') : std::string(); |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 1502 | if (!OpenOatFile(spaces[i].get(), |
| 1503 | boot_class_path_[i], |
| 1504 | expected_boot_class_path, |
| 1505 | validate_oat_file, |
| 1506 | logger, |
| 1507 | &image_reservation, |
| 1508 | error_msg)) { |
| 1509 | return false; |
| 1510 | } |
| 1511 | } |
| 1512 | if (!CheckReservationExhausted(image_reservation, error_msg)) { |
| 1513 | return false; |
| 1514 | } |
| 1515 | |
| 1516 | MaybeRelocateSpaces(spaces, logger); |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 1517 | boot_image_spaces->swap(spaces); |
| 1518 | *extra_reservation = std::move(local_extra_reservation); |
| 1519 | return true; |
| 1520 | } |
| 1521 | |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 1522 | private: |
| 1523 | class RelocateVisitor { |
| 1524 | public: |
| 1525 | explicit RelocateVisitor(uint32_t diff) : diff_(diff) {} |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1526 | |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 1527 | template <typename T> |
| 1528 | ALWAYS_INLINE T* operator()(T* src) const { |
| 1529 | DCHECK(src != nullptr); |
| 1530 | return reinterpret_cast32<T*>(reinterpret_cast32<uint32_t>(src) + diff_); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1531 | } |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1532 | |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 1533 | private: |
| 1534 | const uint32_t diff_; |
| 1535 | }; |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1536 | |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame^] | 1537 | static void** PointerAddress(ArtMethod* method, MemberOffset offset) { |
| 1538 | return reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(method) + offset.Uint32Value()); |
| 1539 | } |
| 1540 | |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1541 | template <PointerSize kPointerSize> |
| 1542 | static void DoRelocateSpaces(const std::vector<std::unique_ptr<ImageSpace>>& spaces, |
| 1543 | uint32_t diff) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 2ffc74b | 2019-01-03 19:25:41 -0800 | [diff] [blame] | 1544 | std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> patched_objects( |
| 1545 | gc::accounting::ContinuousSpaceBitmap::Create( |
| 1546 | "Marked objects", |
| 1547 | spaces.front()->Begin(), |
| 1548 | spaces.back()->End() - spaces.front()->Begin())); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1549 | using PatchRelocateVisitor = PatchObjectVisitor<kPointerSize, RelocateVisitor, RelocateVisitor>; |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 1550 | RelocateVisitor relocate_visitor(diff); |
Mathieu Chartier | f0a96eb | 2019-01-11 11:06:43 -0800 | [diff] [blame] | 1551 | PatchRelocateVisitor patch_object_visitor(relocate_visitor, relocate_visitor); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1552 | |
| 1553 | mirror::Class* dcheck_class_class = nullptr; // Used only for a DCHECK(). |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame^] | 1554 | for (const std::unique_ptr<ImageSpace>& space : spaces) { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1555 | // First patch the image header. The `diff` is OK for patching 32-bit fields but |
| 1556 | // the 64-bit method fields in the ImageHeader may need a negative `delta`. |
| 1557 | reinterpret_cast<ImageHeader*>(space->Begin())->RelocateImage( |
Vladimir Marko | e37b791 | 2019-03-13 10:52:39 +0000 | [diff] [blame] | 1558 | (reinterpret_cast32<uint32_t>(space->Begin()) >= -diff) // Would `begin+diff` overflow? |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1559 | ? -static_cast<int64_t>(-diff) : static_cast<int64_t>(diff)); |
| 1560 | |
| 1561 | // Patch fields and methods. |
| 1562 | const ImageHeader& image_header = space->GetImageHeader(); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame^] | 1563 | image_header.VisitPackedArtFields([&](ArtField& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1564 | patch_object_visitor.template PatchGcRoot</*kMayBeNull=*/ false>( |
| 1565 | &field.DeclaringClassRoot()); |
| 1566 | }, space->Begin()); |
| 1567 | image_header.VisitPackedArtMethods([&](ArtMethod& method) |
| 1568 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1569 | patch_object_visitor.PatchGcRoot(&method.DeclaringClassRoot()); |
| 1570 | void** data_address = PointerAddress(&method, ArtMethod::DataOffset(kPointerSize)); |
| 1571 | patch_object_visitor.PatchNativePointer(data_address); |
| 1572 | void** entrypoint_address = |
| 1573 | PointerAddress(&method, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kPointerSize)); |
| 1574 | patch_object_visitor.PatchNativePointer(entrypoint_address); |
| 1575 | }, space->Begin(), kPointerSize); |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 1576 | auto method_table_visitor = [&](ArtMethod* method) { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1577 | DCHECK(method != nullptr); |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 1578 | return relocate_visitor(method); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1579 | }; |
| 1580 | image_header.VisitPackedImTables(method_table_visitor, space->Begin(), kPointerSize); |
| 1581 | image_header.VisitPackedImtConflictTables(method_table_visitor, space->Begin(), kPointerSize); |
| 1582 | |
| 1583 | // Patch the intern table. |
| 1584 | if (image_header.GetInternedStringsSection().Size() != 0u) { |
| 1585 | const uint8_t* data = space->Begin() + image_header.GetInternedStringsSection().Offset(); |
| 1586 | size_t read_count; |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 1587 | InternTable::UnorderedSet temp_set(data, /*make_copy_of_data=*/ false, &read_count); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1588 | for (GcRoot<mirror::String>& slot : temp_set) { |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 1589 | patch_object_visitor.template PatchGcRoot</*kMayBeNull=*/ false>(&slot); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | // Patch the class table and classes, so that we can traverse class hierarchy to |
| 1594 | // determine the types of other objects when we visit them later. |
| 1595 | if (image_header.GetClassTableSection().Size() != 0u) { |
| 1596 | uint8_t* data = space->Begin() + image_header.GetClassTableSection().Offset(); |
| 1597 | size_t read_count; |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 1598 | ClassTable::ClassSet temp_set(data, /*make_copy_of_data=*/ false, &read_count); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1599 | DCHECK(!temp_set.empty()); |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 1600 | ClassTableVisitor class_table_visitor(relocate_visitor); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1601 | for (ClassTable::TableSlot& slot : temp_set) { |
| 1602 | slot.VisitRoot(class_table_visitor); |
| 1603 | mirror::Class* klass = slot.Read<kWithoutReadBarrier>(); |
| 1604 | DCHECK(klass != nullptr); |
Mathieu Chartier | 2ffc74b | 2019-01-03 19:25:41 -0800 | [diff] [blame] | 1605 | patched_objects->Set(klass); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1606 | patch_object_visitor.VisitClass(klass); |
| 1607 | if (kIsDebugBuild) { |
| 1608 | mirror::Class* class_class = klass->GetClass<kVerifyNone, kWithoutReadBarrier>(); |
| 1609 | if (dcheck_class_class == nullptr) { |
| 1610 | dcheck_class_class = class_class; |
| 1611 | } else { |
| 1612 | CHECK_EQ(class_class, dcheck_class_class); |
| 1613 | } |
| 1614 | } |
| 1615 | // Then patch the non-embedded vtable and iftable. |
| 1616 | mirror::PointerArray* vtable = klass->GetVTable<kVerifyNone, kWithoutReadBarrier>(); |
Mathieu Chartier | 2ffc74b | 2019-01-03 19:25:41 -0800 | [diff] [blame] | 1617 | if (vtable != nullptr && !patched_objects->Set(vtable)) { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1618 | patch_object_visitor.VisitPointerArray(vtable); |
| 1619 | } |
| 1620 | auto* iftable = klass->GetIfTable<kVerifyNone, kWithoutReadBarrier>(); |
| 1621 | if (iftable != nullptr) { |
Vladimir Marko | dbcb48f | 2018-11-12 11:47:04 +0000 | [diff] [blame] | 1622 | int32_t ifcount = klass->GetIfTableCount<kVerifyNone>(); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1623 | for (int32_t i = 0; i != ifcount; ++i) { |
| 1624 | mirror::PointerArray* unpatched_ifarray = |
| 1625 | iftable->GetMethodArrayOrNull<kVerifyNone, kWithoutReadBarrier>(i); |
| 1626 | if (unpatched_ifarray != nullptr) { |
| 1627 | // The iftable has not been patched, so we need to explicitly adjust the pointer. |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 1628 | mirror::PointerArray* ifarray = relocate_visitor(unpatched_ifarray); |
Mathieu Chartier | 2ffc74b | 2019-01-03 19:25:41 -0800 | [diff] [blame] | 1629 | if (!patched_objects->Set(ifarray)) { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1630 | patch_object_visitor.VisitPointerArray(ifarray); |
| 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | // Patch class roots now, so that we can recognize mirror::Method and mirror::Constructor. |
| 1640 | ObjPtr<mirror::Class> method_class; |
| 1641 | ObjPtr<mirror::Class> constructor_class; |
| 1642 | { |
| 1643 | const ImageSpace* space = spaces.front().get(); |
| 1644 | const ImageHeader& image_header = space->GetImageHeader(); |
| 1645 | |
| 1646 | ObjPtr<mirror::ObjectArray<mirror::Object>> image_roots = |
| 1647 | image_header.GetImageRoots<kWithoutReadBarrier>(); |
Mathieu Chartier | 2ffc74b | 2019-01-03 19:25:41 -0800 | [diff] [blame] | 1648 | patched_objects->Set(image_roots.Ptr()); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1649 | patch_object_visitor.VisitObject(image_roots.Ptr()); |
| 1650 | |
| 1651 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = |
| 1652 | ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast(MakeObjPtr( |
| 1653 | image_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kClassRoots))); |
Mathieu Chartier | 2ffc74b | 2019-01-03 19:25:41 -0800 | [diff] [blame] | 1654 | patched_objects->Set(class_roots.Ptr()); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1655 | patch_object_visitor.VisitObject(class_roots.Ptr()); |
| 1656 | |
| 1657 | method_class = GetClassRoot<mirror::Method, kWithoutReadBarrier>(class_roots); |
| 1658 | constructor_class = GetClassRoot<mirror::Constructor, kWithoutReadBarrier>(class_roots); |
| 1659 | } |
| 1660 | |
Vladimir Marko | afe14eb | 2018-10-01 12:11:14 +0100 | [diff] [blame] | 1661 | for (size_t s = 0u, size = spaces.size(); s != size; ++s) { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1662 | const ImageSpace* space = spaces[s].get(); |
| 1663 | const ImageHeader& image_header = space->GetImageHeader(); |
| 1664 | |
| 1665 | static_assert(IsAligned<kObjectAlignment>(sizeof(ImageHeader)), "Header alignment check"); |
| 1666 | uint32_t objects_end = image_header.GetObjectsSection().Size(); |
| 1667 | DCHECK_ALIGNED(objects_end, kObjectAlignment); |
| 1668 | for (uint32_t pos = sizeof(ImageHeader); pos != objects_end; ) { |
| 1669 | mirror::Object* object = reinterpret_cast<mirror::Object*>(space->Begin() + pos); |
Mathieu Chartier | 2ffc74b | 2019-01-03 19:25:41 -0800 | [diff] [blame] | 1670 | if (!patched_objects->Test(object)) { |
| 1671 | // This is the last pass over objects, so we do not need to Set(). |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1672 | patch_object_visitor.VisitObject(object); |
| 1673 | mirror::Class* klass = object->GetClass<kVerifyNone, kWithoutReadBarrier>(); |
| 1674 | if (klass->IsDexCacheClass<kVerifyNone>()) { |
| 1675 | // Patch dex cache array pointers and elements. |
| 1676 | mirror::DexCache* dex_cache = object->AsDexCache<kVerifyNone, kWithoutReadBarrier>(); |
| 1677 | patch_object_visitor.VisitDexCacheArrays(dex_cache); |
| 1678 | } else if (klass == method_class || klass == constructor_class) { |
| 1679 | // Patch the ArtMethod* in the mirror::Executable subobject. |
| 1680 | ObjPtr<mirror::Executable> as_executable = |
| 1681 | ObjPtr<mirror::Executable>::DownCast(MakeObjPtr(object)); |
| 1682 | ArtMethod* unpatched_method = as_executable->GetArtMethod<kVerifyNone>(); |
Mathieu Chartier | d3f037b | 2018-12-06 23:50:56 -0800 | [diff] [blame] | 1683 | ArtMethod* patched_method = relocate_visitor(unpatched_method); |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 1684 | as_executable->SetArtMethod</*kTransactionActive=*/ false, |
| 1685 | /*kCheckTransaction=*/ true, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1686 | kVerifyNone>(patched_method); |
| 1687 | } |
| 1688 | } |
| 1689 | pos += RoundUp(object->SizeOf<kVerifyNone>(), kObjectAlignment); |
| 1690 | } |
| 1691 | } |
| 1692 | } |
| 1693 | |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1694 | void MaybeRelocateSpaces(const std::vector<std::unique_ptr<ImageSpace>>& spaces, |
| 1695 | TimingLogger* logger) |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1696 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1697 | TimingLogger::ScopedTiming timing("MaybeRelocateSpaces", logger); |
| 1698 | ImageSpace* first_space = spaces.front().get(); |
| 1699 | const ImageHeader& first_space_header = first_space->GetImageHeader(); |
| 1700 | uint32_t diff = |
| 1701 | static_cast<uint32_t>(first_space->Begin() - first_space_header.GetImageBegin()); |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1702 | if (!relocate_) { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1703 | DCHECK_EQ(diff, 0u); |
| 1704 | return; |
| 1705 | } |
| 1706 | |
| 1707 | PointerSize pointer_size = first_space_header.GetPointerSize(); |
| 1708 | if (pointer_size == PointerSize::k64) { |
| 1709 | DoRelocateSpaces<PointerSize::k64>(spaces, diff); |
| 1710 | } else { |
| 1711 | DoRelocateSpaces<PointerSize::k32>(spaces, diff); |
| 1712 | } |
| 1713 | } |
| 1714 | |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1715 | std::unique_ptr<ImageSpace> Load(const std::string& image_location, |
| 1716 | const std::string& image_filename, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1717 | TimingLogger* logger, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1718 | /*inout*/MemMap* image_reservation, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1719 | /*out*/std::string* error_msg) |
| 1720 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1721 | // Should this be a RDWR lock? This is only a defensive measure, as at |
| 1722 | // this point the image should exist. |
| 1723 | // However, only the zygote can write into the global dalvik-cache, so |
| 1724 | // restrict to zygote processes, or any process that isn't using |
| 1725 | // /data/dalvik-cache (which we assume to be allowed to write there). |
| 1726 | const bool rw_lock = is_zygote_ || !is_global_cache_; |
| 1727 | |
| 1728 | // Note that we must not use the file descriptor associated with |
| 1729 | // ScopedFlock::GetFile to Init the image file. We want the file |
| 1730 | // descriptor (and the associated exclusive lock) to be released when |
| 1731 | // we leave Create. |
| 1732 | ScopedFlock image = LockedFile::Open(image_filename.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 1733 | /*flags=*/ rw_lock ? (O_CREAT | O_RDWR) : O_RDONLY, |
| 1734 | /*block=*/ true, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1735 | error_msg); |
| 1736 | |
| 1737 | VLOG(startup) << "Using image file " << image_filename.c_str() << " for image location " |
| 1738 | << image_location; |
| 1739 | // If we are in /system we can assume the image is good. We can also |
| 1740 | // assume this if we are using a relocated image (i.e. image checksum |
| 1741 | // matches) since this is only different by the offset. We need this to |
| 1742 | // make sure that host tests continue to work. |
| 1743 | // Since we are the boot image, pass null since we load the oat file from the boot image oat |
| 1744 | // file name. |
| 1745 | return Loader::Init(image_filename.c_str(), |
| 1746 | image_location.c_str(), |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 1747 | /*oat_file=*/ nullptr, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1748 | logger, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1749 | image_reservation, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1750 | error_msg); |
| 1751 | } |
| 1752 | |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 1753 | bool OpenOatFile(ImageSpace* space, |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 1754 | const std::string& dex_filename, |
| 1755 | const std::string& expected_boot_class_path, |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 1756 | bool validate_oat_file, |
| 1757 | TimingLogger* logger, |
| 1758 | /*inout*/MemMap* image_reservation, |
| 1759 | /*out*/std::string* error_msg) { |
| 1760 | // VerifyImageAllocations() will be called later in Runtime::Init() |
| 1761 | // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_ |
| 1762 | // and ArtField::java_lang_reflect_ArtField_, which are used from |
| 1763 | // Object::SizeOf() which VerifyImageAllocations() calls, are not |
| 1764 | // set yet at this point. |
| 1765 | DCHECK(image_reservation != nullptr); |
| 1766 | std::unique_ptr<OatFile> oat_file; |
| 1767 | { |
| 1768 | TimingLogger::ScopedTiming timing("OpenOatFile", logger); |
| 1769 | std::string oat_filename = |
| 1770 | ImageHeader::GetOatLocationFromImageLocation(space->GetImageFilename()); |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 1771 | std::string oat_location = |
| 1772 | ImageHeader::GetOatLocationFromImageLocation(space->GetImageLocation()); |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 1773 | |
| 1774 | oat_file.reset(OatFile::Open(/*zip_fd=*/ -1, |
| 1775 | oat_filename, |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 1776 | oat_location, |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1777 | executable_, |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 1778 | /*low_4gb=*/ false, |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 1779 | /*abs_dex_location=*/ dex_filename.c_str(), |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 1780 | image_reservation, |
| 1781 | error_msg)); |
| 1782 | if (oat_file == nullptr) { |
| 1783 | *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s", |
| 1784 | oat_filename.c_str(), |
| 1785 | space->GetName(), |
| 1786 | error_msg->c_str()); |
| 1787 | return false; |
| 1788 | } |
| 1789 | const ImageHeader& image_header = space->GetImageHeader(); |
| 1790 | uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum(); |
| 1791 | uint32_t image_oat_checksum = image_header.GetOatChecksum(); |
| 1792 | if (oat_checksum != image_oat_checksum) { |
| 1793 | *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum" |
| 1794 | " 0x%x in image %s", |
| 1795 | oat_checksum, |
| 1796 | image_oat_checksum, |
| 1797 | space->GetName()); |
| 1798 | return false; |
| 1799 | } |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 1800 | const char* oat_boot_class_path = |
| 1801 | oat_file->GetOatHeader().GetStoreValueByKey(OatHeader::kBootClassPathKey); |
| 1802 | oat_boot_class_path = (oat_boot_class_path != nullptr) ? oat_boot_class_path : ""; |
| 1803 | if (expected_boot_class_path != oat_boot_class_path) { |
| 1804 | *error_msg = StringPrintf("Failed to match oat boot class path %s to expected " |
| 1805 | "boot class path %s in image %s", |
| 1806 | oat_boot_class_path, |
| 1807 | expected_boot_class_path.c_str(), |
| 1808 | space->GetName()); |
| 1809 | return false; |
| 1810 | } |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 1811 | ptrdiff_t relocation_diff = space->Begin() - image_header.GetImageBegin(); |
| 1812 | CHECK(image_header.GetOatDataBegin() != nullptr); |
| 1813 | uint8_t* oat_data_begin = image_header.GetOatDataBegin() + relocation_diff; |
| 1814 | if (oat_file->Begin() != oat_data_begin) { |
| 1815 | *error_msg = StringPrintf("Oat file '%s' referenced from image %s has unexpected begin" |
| 1816 | " %p v. %p", |
| 1817 | oat_filename.c_str(), |
| 1818 | space->GetName(), |
| 1819 | oat_file->Begin(), |
| 1820 | oat_data_begin); |
| 1821 | return false; |
| 1822 | } |
| 1823 | } |
| 1824 | if (validate_oat_file) { |
| 1825 | TimingLogger::ScopedTiming timing("ValidateOatFile", logger); |
| 1826 | if (!ImageSpace::ValidateOatFile(*oat_file, error_msg)) { |
| 1827 | DCHECK(!error_msg->empty()); |
| 1828 | return false; |
| 1829 | } |
| 1830 | } |
| 1831 | space->oat_file_ = std::move(oat_file); |
| 1832 | space->oat_file_non_owned_ = space->oat_file_.get(); |
| 1833 | return true; |
| 1834 | } |
| 1835 | |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 1836 | bool ReserveBootImageMemory(uint32_t reservation_size, |
| 1837 | uint32_t image_start, |
Vladimir Marko | d44d703 | 2018-08-30 13:02:31 +0100 | [diff] [blame] | 1838 | size_t extra_reservation_size, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1839 | /*out*/MemMap* image_reservation, |
Vladimir Marko | d44d703 | 2018-08-30 13:02:31 +0100 | [diff] [blame] | 1840 | /*out*/MemMap* extra_reservation, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1841 | /*out*/std::string* error_msg) { |
Vladimir Marko | 7391c8c | 2018-11-21 17:58:44 +0000 | [diff] [blame] | 1842 | DCHECK_ALIGNED(reservation_size, kPageSize); |
| 1843 | DCHECK_ALIGNED(image_start, kPageSize); |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1844 | DCHECK(!image_reservation->IsValid()); |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 1845 | DCHECK_LT(extra_reservation_size, std::numeric_limits<uint32_t>::max() - reservation_size); |
| 1846 | size_t total_size = reservation_size + extra_reservation_size; |
Vladimir Marko | ae581ed | 2018-10-08 09:29:05 +0100 | [diff] [blame] | 1847 | // If relocating, choose a random address for ALSR. |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1848 | uint32_t addr = relocate_ ? ART_BASE_ADDRESS + ChooseRelocationOffsetDelta() : image_start; |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1849 | *image_reservation = |
| 1850 | MemMap::MapAnonymous("Boot image reservation", |
Vladimir Marko | ae581ed | 2018-10-08 09:29:05 +0100 | [diff] [blame] | 1851 | reinterpret_cast32<uint8_t*>(addr), |
| 1852 | total_size, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1853 | PROT_NONE, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 1854 | /*low_4gb=*/ true, |
| 1855 | /*reuse=*/ false, |
| 1856 | /*reservation=*/ nullptr, |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1857 | error_msg); |
| 1858 | if (!image_reservation->IsValid()) { |
| 1859 | return false; |
| 1860 | } |
Vladimir Marko | d44d703 | 2018-08-30 13:02:31 +0100 | [diff] [blame] | 1861 | DCHECK(!extra_reservation->IsValid()); |
| 1862 | if (extra_reservation_size != 0u) { |
| 1863 | DCHECK_ALIGNED(extra_reservation_size, kPageSize); |
| 1864 | DCHECK_LT(extra_reservation_size, image_reservation->Size()); |
| 1865 | uint8_t* split = image_reservation->End() - extra_reservation_size; |
| 1866 | *extra_reservation = image_reservation->RemapAtEnd(split, |
| 1867 | "Boot image extra reservation", |
| 1868 | PROT_NONE, |
| 1869 | error_msg); |
| 1870 | if (!extra_reservation->IsValid()) { |
| 1871 | return false; |
| 1872 | } |
| 1873 | } |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1874 | |
| 1875 | return true; |
| 1876 | } |
| 1877 | |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 1878 | bool CheckReservationExhausted(const MemMap& image_reservation, /*out*/std::string* error_msg) { |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1879 | if (image_reservation.IsValid()) { |
| 1880 | *error_msg = StringPrintf("Excessive image reservation after loading boot image: %p-%p", |
| 1881 | image_reservation.Begin(), |
| 1882 | image_reservation.End()); |
| 1883 | return false; |
| 1884 | } |
Vladimir Marko | c09cd05 | 2018-08-23 16:36:36 +0100 | [diff] [blame] | 1885 | return true; |
| 1886 | } |
| 1887 | |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 1888 | const std::vector<std::string>& boot_class_path_; |
| 1889 | const std::vector<std::string>& boot_class_path_locations_; |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1890 | const std::string& image_location_; |
| 1891 | InstructionSet image_isa_; |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1892 | bool relocate_; |
| 1893 | bool executable_; |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1894 | bool is_zygote_; |
| 1895 | bool has_system_; |
| 1896 | bool has_cache_; |
| 1897 | bool is_global_cache_; |
Vladimir Marko | e307002 | 2018-08-22 09:36:19 +0000 | [diff] [blame] | 1898 | bool dalvik_cache_exists_; |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1899 | std::string dalvik_cache_; |
| 1900 | std::string cache_filename_; |
| 1901 | }; |
| 1902 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1903 | static constexpr uint64_t kLowSpaceValue = 50 * MB; |
| 1904 | static constexpr uint64_t kTmpFsSentinelValue = 384 * MB; |
| 1905 | |
| 1906 | // Read the free space of the cache partition and make a decision whether to keep the generated |
| 1907 | // image. This is to try to mitigate situations where the system might run out of space later. |
| 1908 | static bool CheckSpace(const std::string& cache_filename, std::string* error_msg) { |
| 1909 | // Using statvfs vs statvfs64 because of b/18207376, and it is enough for all practical purposes. |
| 1910 | struct statvfs buf; |
| 1911 | |
| 1912 | int res = TEMP_FAILURE_RETRY(statvfs(cache_filename.c_str(), &buf)); |
| 1913 | if (res != 0) { |
| 1914 | // Could not stat. Conservatively tell the system to delete the image. |
| 1915 | *error_msg = "Could not stat the filesystem, assuming low-memory situation."; |
| 1916 | return false; |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 1917 | } |
Nicolas Geoffray | 1bc977c | 2016-01-23 14:15:49 +0000 | [diff] [blame] | 1918 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1919 | uint64_t fs_overall_size = buf.f_bsize * static_cast<uint64_t>(buf.f_blocks); |
| 1920 | // Zygote is privileged, but other things are not. Use bavail. |
| 1921 | 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] | 1922 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1923 | // Take the overall size as an indicator for a tmpfs, which is being used for the decryption |
| 1924 | // environment. We do not want to fail quickening the boot image there, as it is beneficial |
| 1925 | // for time-to-UI. |
| 1926 | if (fs_overall_size > kTmpFsSentinelValue) { |
| 1927 | if (fs_free_size < kLowSpaceValue) { |
| 1928 | *error_msg = StringPrintf("Low-memory situation: only %4.2f megabytes available, need at " |
| 1929 | "least %" PRIu64 ".", |
| 1930 | static_cast<double>(fs_free_size) / MB, |
| 1931 | kLowSpaceValue / MB); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 1932 | return false; |
| 1933 | } |
| 1934 | } |
| 1935 | return true; |
| 1936 | } |
| 1937 | |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1938 | bool ImageSpace::LoadBootImage( |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 1939 | const std::vector<std::string>& boot_class_path, |
| 1940 | const std::vector<std::string>& boot_class_path_locations, |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1941 | const std::string& image_location, |
| 1942 | const InstructionSet image_isa, |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 1943 | ImageSpaceLoadingOrder order, |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1944 | bool relocate, |
| 1945 | bool executable, |
| 1946 | bool is_zygote, |
Vladimir Marko | d44d703 | 2018-08-30 13:02:31 +0100 | [diff] [blame] | 1947 | size_t extra_reservation_size, |
| 1948 | /*out*/std::vector<std::unique_ptr<space::ImageSpace>>* boot_image_spaces, |
| 1949 | /*out*/MemMap* extra_reservation) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1950 | ScopedTrace trace(__FUNCTION__); |
| 1951 | |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1952 | DCHECK(boot_image_spaces != nullptr); |
| 1953 | DCHECK(boot_image_spaces->empty()); |
Vladimir Marko | d44d703 | 2018-08-30 13:02:31 +0100 | [diff] [blame] | 1954 | DCHECK_ALIGNED(extra_reservation_size, kPageSize); |
| 1955 | DCHECK(extra_reservation != nullptr); |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1956 | DCHECK_NE(image_isa, InstructionSet::kNone); |
| 1957 | |
| 1958 | if (image_location.empty()) { |
| 1959 | return false; |
| 1960 | } |
| 1961 | |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1962 | BootImageLoader loader(boot_class_path, |
| 1963 | boot_class_path_locations, |
| 1964 | image_location, |
| 1965 | image_isa, |
| 1966 | relocate, |
| 1967 | executable, |
| 1968 | is_zygote); |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1969 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1970 | // Step 0: Extra zygote work. |
| 1971 | |
| 1972 | // Step 0.a: If we're the zygote, mark boot. |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1973 | if (loader.IsZygote() && CanWriteToDalvikCache(image_isa)) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1974 | MarkZygoteStart(image_isa, Runtime::Current()->GetZygoteMaxFailedBoots()); |
| 1975 | } |
| 1976 | |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1977 | loader.FindImageFiles(); |
| 1978 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1979 | // Step 0.b: If we're the zygote, check for free space, and prune the cache preemptively, |
| 1980 | // if necessary. While the runtime may be fine (it is pretty tolerant to |
| 1981 | // out-of-disk-space situations), other parts of the platform are not. |
| 1982 | // |
| 1983 | // The advantage of doing this proactively is that the later steps are simplified, |
| 1984 | // i.e., we do not need to code retries. |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1985 | bool low_space = false; |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1986 | if (loader.IsZygote() && loader.DalvikCacheExists()) { |
Andreas Gampe | 6e74abb | 2018-03-01 17:33:19 -0800 | [diff] [blame] | 1987 | // Extra checks for the zygote. These only apply when loading the first image, explained below. |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1988 | const std::string& dalvik_cache = loader.GetDalvikCache(); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1989 | DCHECK(!dalvik_cache.empty()); |
| 1990 | std::string local_error_msg; |
Andreas Gampe | 6e74abb | 2018-03-01 17:33:19 -0800 | [diff] [blame] | 1991 | bool check_space = CheckSpace(dalvik_cache, &local_error_msg); |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 1992 | if (!check_space) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 1993 | LOG(WARNING) << local_error_msg << " Preemptively pruning the dalvik cache."; |
| 1994 | PruneDalvikCache(image_isa); |
| 1995 | |
| 1996 | // Re-evaluate the image. |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 1997 | loader.FindImageFiles(); |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1998 | |
Andreas Gampe | 6e74abb | 2018-03-01 17:33:19 -0800 | [diff] [blame] | 1999 | // Disable compilation/patching - we do not want to fill up the space again. |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 2000 | low_space = true; |
Andreas Gampe | 6e74abb | 2018-03-01 17:33:19 -0800 | [diff] [blame] | 2001 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | // Collect all the errors. |
| 2005 | std::vector<std::string> error_msgs; |
| 2006 | |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 2007 | auto try_load_from = [&](auto has_fn, auto load_fn, bool validate_oat_file) { |
| 2008 | if ((loader.*has_fn)()) { |
| 2009 | std::string local_error_msg; |
| 2010 | if ((loader.*load_fn)(validate_oat_file, |
| 2011 | extra_reservation_size, |
| 2012 | boot_image_spaces, |
| 2013 | extra_reservation, |
| 2014 | &local_error_msg)) { |
| 2015 | return true; |
| 2016 | } |
| 2017 | error_msgs.push_back(local_error_msg); |
| 2018 | } |
| 2019 | return false; |
| 2020 | }; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2021 | |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 2022 | auto try_load_from_system = [&]() { |
| 2023 | return try_load_from(&BootImageLoader::HasSystem, &BootImageLoader::LoadFromSystem, false); |
| 2024 | }; |
| 2025 | auto try_load_from_cache = [&]() { |
| 2026 | return try_load_from(&BootImageLoader::HasCache, &BootImageLoader::LoadFromDalvikCache, true); |
| 2027 | }; |
| 2028 | |
| 2029 | auto invoke_sequentially = [](auto first, auto second) { |
| 2030 | return first() || second(); |
| 2031 | }; |
| 2032 | |
| 2033 | // Step 1+2: Check system and cache images in the asked-for order. |
| 2034 | if (order == ImageSpaceLoadingOrder::kSystemFirst) { |
| 2035 | if (invoke_sequentially(try_load_from_system, try_load_from_cache)) { |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 2036 | return true; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2037 | } |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 2038 | } else { |
| 2039 | if (invoke_sequentially(try_load_from_cache, try_load_from_system)) { |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 2040 | return true; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2041 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2042 | } |
| 2043 | |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 2044 | // Step 3: We do not have an existing image in /system, |
| 2045 | // so generate an image into the dalvik cache. |
Vladimir Marko | e307002 | 2018-08-22 09:36:19 +0000 | [diff] [blame] | 2046 | if (!loader.HasSystem() && loader.DalvikCacheExists()) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2047 | std::string local_error_msg; |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 2048 | if (low_space || !Runtime::Current()->IsImageDex2OatEnabled()) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2049 | local_error_msg = "Image compilation disabled."; |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 2050 | } else if (ImageCreationAllowed(loader.IsGlobalCache(), |
| 2051 | image_isa, |
| 2052 | is_zygote, |
| 2053 | &local_error_msg)) { |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 2054 | bool compilation_success = |
| 2055 | GenerateImage(loader.GetCacheFilename(), image_isa, &local_error_msg); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2056 | if (compilation_success) { |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 2057 | if (loader.LoadFromDalvikCache(/*validate_oat_file=*/ false, |
Vladimir Marko | d44d703 | 2018-08-30 13:02:31 +0100 | [diff] [blame] | 2058 | extra_reservation_size, |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 2059 | boot_image_spaces, |
Vladimir Marko | d44d703 | 2018-08-30 13:02:31 +0100 | [diff] [blame] | 2060 | extra_reservation, |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 2061 | &local_error_msg)) { |
| 2062 | return true; |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2063 | } |
| 2064 | } |
| 2065 | } |
| 2066 | error_msgs.push_back(StringPrintf("Cannot compile image to %s: %s", |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 2067 | loader.GetCacheFilename().c_str(), |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2068 | local_error_msg.c_str())); |
| 2069 | } |
| 2070 | |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 2071 | // We failed. Prune the cache the free up space, create a compound error message |
| 2072 | // and return false. |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 2073 | if (loader.DalvikCacheExists()) { |
| 2074 | PruneDalvikCache(image_isa); |
| 2075 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2076 | |
| 2077 | std::ostringstream oss; |
| 2078 | bool first = true; |
Andreas Gampe | 4c481a4 | 2016-11-03 08:21:59 -0700 | [diff] [blame] | 2079 | for (const auto& msg : error_msgs) { |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2080 | if (!first) { |
| 2081 | oss << "\n "; |
| 2082 | } |
| 2083 | oss << msg; |
| 2084 | } |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2085 | |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 2086 | LOG(ERROR) << "Could not create image space with image file '" << image_location << "'. " |
| 2087 | << "Attempting to fall back to imageless running. Error was: " << oss.str(); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2088 | |
Vladimir Marko | 82e1e27 | 2018-08-20 13:38:06 +0000 | [diff] [blame] | 2089 | return false; |
Andreas Gampe | 2bd8428 | 2016-12-05 12:37:36 -0800 | [diff] [blame] | 2090 | } |
| 2091 | |
Igor Murashkin | 8275fba | 2017-05-02 15:58:02 -0700 | [diff] [blame] | 2092 | ImageSpace::~ImageSpace() { |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 2093 | // Everything done by member destructors. Classes forward-declared in header are now defined. |
Igor Murashkin | 8275fba | 2017-05-02 15:58:02 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2096 | std::unique_ptr<ImageSpace> ImageSpace::CreateFromAppImage(const char* image, |
| 2097 | const OatFile* oat_file, |
| 2098 | std::string* error_msg) { |
Vladimir Marko | 312f10e | 2018-11-21 12:35:24 +0000 | [diff] [blame] | 2099 | // Note: The oat file has already been validated. |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 2100 | return Loader::InitAppImage(image, |
| 2101 | image, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 2102 | oat_file, |
Vladimir Marko | f4efa9e | 2018-10-17 14:12:45 +0100 | [diff] [blame] | 2103 | /*image_reservation=*/ nullptr, |
Vladimir Marko | 4df2d80 | 2018-09-27 16:42:44 +0000 | [diff] [blame] | 2104 | error_msg); |
Andreas Gampe | a463b6a | 2016-08-12 21:53:32 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 2107 | const OatFile* ImageSpace::GetOatFile() const { |
Andreas Gampe | 88da3b0 | 2015-06-12 20:38:49 -0700 | [diff] [blame] | 2108 | return oat_file_non_owned_; |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 2109 | } |
| 2110 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 2111 | std::unique_ptr<const OatFile> ImageSpace::ReleaseOatFile() { |
| 2112 | CHECK(oat_file_ != nullptr); |
| 2113 | return std::move(oat_file_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2116 | void ImageSpace::Dump(std::ostream& os) const { |
| 2117 | os << GetType() |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2118 | << " begin=" << reinterpret_cast<void*>(Begin()) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2119 | << ",end=" << reinterpret_cast<void*>(End()) |
| 2120 | << ",size=" << PrettySize(Size()) |
| 2121 | << ",name=\"" << GetName() << "\"]"; |
| 2122 | } |
| 2123 | |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 2124 | bool ImageSpace::ValidateOatFile(const OatFile& oat_file, std::string* error_msg) { |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 2125 | const ArtDexFileLoader dex_file_loader; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 2126 | for (const OatDexFile* oat_dex_file : oat_file.GetOatDexFiles()) { |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 2127 | const std::string& dex_file_location = oat_dex_file->GetDexFileLocation(); |
| 2128 | |
| 2129 | // Skip multidex locations - These will be checked when we visit their |
| 2130 | // corresponding primary non-multidex location. |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 2131 | if (DexFileLoader::IsMultiDexLocation(dex_file_location.c_str())) { |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 2132 | continue; |
| 2133 | } |
| 2134 | |
| 2135 | std::vector<uint32_t> checksums; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 2136 | if (!dex_file_loader.GetMultiDexChecksums(dex_file_location.c_str(), &checksums, error_msg)) { |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 2137 | *error_msg = StringPrintf("ValidateOatFile failed to get checksums of dex file '%s' " |
| 2138 | "referenced by oat file %s: %s", |
| 2139 | dex_file_location.c_str(), |
| 2140 | oat_file.GetLocation().c_str(), |
| 2141 | error_msg->c_str()); |
| 2142 | return false; |
| 2143 | } |
| 2144 | CHECK(!checksums.empty()); |
| 2145 | if (checksums[0] != oat_dex_file->GetDexFileLocationChecksum()) { |
| 2146 | *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file " |
| 2147 | "'%s' and dex file '%s' (0x%x != 0x%x)", |
| 2148 | oat_file.GetLocation().c_str(), |
| 2149 | dex_file_location.c_str(), |
| 2150 | oat_dex_file->GetDexFileLocationChecksum(), |
| 2151 | checksums[0]); |
| 2152 | return false; |
| 2153 | } |
| 2154 | |
| 2155 | // Verify checksums for any related multidex entries. |
| 2156 | for (size_t i = 1; i < checksums.size(); i++) { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 2157 | std::string multi_dex_location = DexFileLoader::GetMultiDexLocation( |
| 2158 | i, |
| 2159 | dex_file_location.c_str()); |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 2160 | const OatDexFile* multi_dex = oat_file.GetOatDexFile(multi_dex_location.c_str(), |
| 2161 | nullptr, |
| 2162 | error_msg); |
Richard Uhler | 84f50ae | 2017-02-06 15:12:45 +0000 | [diff] [blame] | 2163 | if (multi_dex == nullptr) { |
| 2164 | *error_msg = StringPrintf("ValidateOatFile oat file '%s' is missing entry '%s'", |
| 2165 | oat_file.GetLocation().c_str(), |
| 2166 | multi_dex_location.c_str()); |
| 2167 | return false; |
| 2168 | } |
| 2169 | |
| 2170 | if (checksums[i] != multi_dex->GetDexFileLocationChecksum()) { |
| 2171 | *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file " |
| 2172 | "'%s' and dex file '%s' (0x%x != 0x%x)", |
| 2173 | oat_file.GetLocation().c_str(), |
| 2174 | multi_dex_location.c_str(), |
| 2175 | multi_dex->GetDexFileLocationChecksum(), |
| 2176 | checksums[i]); |
| 2177 | return false; |
| 2178 | } |
| 2179 | } |
| 2180 | } |
| 2181 | return true; |
| 2182 | } |
| 2183 | |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 2184 | std::string ImageSpace::GetBootClassPathChecksums(const std::vector<std::string>& boot_class_path, |
| 2185 | const std::string& image_location, |
| 2186 | InstructionSet image_isa, |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 2187 | ImageSpaceLoadingOrder order, |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 2188 | /*out*/std::string* error_msg) { |
| 2189 | std::string system_filename; |
| 2190 | bool has_system = false; |
| 2191 | std::string cache_filename; |
| 2192 | bool has_cache = false; |
| 2193 | bool dalvik_cache_exists = false; |
| 2194 | bool is_global_cache = false; |
| 2195 | if (!FindImageFilename(image_location.c_str(), |
| 2196 | image_isa, |
| 2197 | &system_filename, |
| 2198 | &has_system, |
| 2199 | &cache_filename, |
| 2200 | &dalvik_cache_exists, |
| 2201 | &has_cache, |
| 2202 | &is_global_cache)) { |
| 2203 | *error_msg = StringPrintf("Unable to find image file for %s and %s", |
| 2204 | image_location.c_str(), |
| 2205 | GetInstructionSetString(image_isa)); |
| 2206 | return std::string(); |
| 2207 | } |
| 2208 | |
| 2209 | DCHECK(has_system || has_cache); |
Andreas Gampe | 8682354 | 2019-02-25 09:38:49 -0800 | [diff] [blame] | 2210 | const std::string& filename = (order == ImageSpaceLoadingOrder::kSystemFirst) |
| 2211 | ? (has_system ? system_filename : cache_filename) |
| 2212 | : (has_cache ? cache_filename : system_filename); |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 2213 | std::unique_ptr<ImageHeader> header = ReadSpecificImageHeader(filename.c_str(), error_msg); |
| 2214 | if (header == nullptr) { |
| 2215 | return std::string(); |
| 2216 | } |
| 2217 | if (header->GetComponentCount() == 0u || header->GetComponentCount() > boot_class_path.size()) { |
| 2218 | *error_msg = StringPrintf("Unexpected component count in %s, received %u, " |
| 2219 | "expected non-zero and <= %zu", |
| 2220 | filename.c_str(), |
| 2221 | header->GetComponentCount(), |
| 2222 | boot_class_path.size()); |
| 2223 | return std::string(); |
| 2224 | } |
| 2225 | |
| 2226 | std::string boot_image_checksum = |
| 2227 | StringPrintf("i;%d/%08x", header->GetComponentCount(), header->GetImageChecksum()); |
| 2228 | ArrayRef<const std::string> boot_class_path_tail = |
| 2229 | ArrayRef<const std::string>(boot_class_path).SubArray(header->GetComponentCount()); |
| 2230 | for (const std::string& bcp_filename : boot_class_path_tail) { |
| 2231 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 2232 | const ArtDexFileLoader dex_file_loader; |
| 2233 | if (!dex_file_loader.Open(bcp_filename.c_str(), |
| 2234 | bcp_filename, // The location does not matter here. |
| 2235 | /*verify=*/ false, |
| 2236 | /*verify_checksum=*/ false, |
| 2237 | error_msg, |
| 2238 | &dex_files)) { |
| 2239 | return std::string(); |
| 2240 | } |
| 2241 | DCHECK(!dex_files.empty()); |
| 2242 | StringAppendF(&boot_image_checksum, ":d"); |
| 2243 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 2244 | StringAppendF(&boot_image_checksum, "/%08x", dex_file->GetLocationChecksum()); |
| 2245 | } |
| 2246 | } |
| 2247 | return boot_image_checksum; |
| 2248 | } |
| 2249 | |
| 2250 | std::string ImageSpace::GetBootClassPathChecksums( |
| 2251 | const std::vector<ImageSpace*>& image_spaces, |
| 2252 | const std::vector<const DexFile*>& boot_class_path) { |
| 2253 | DCHECK(!image_spaces.empty()); |
| 2254 | const ImageHeader& primary_header = image_spaces.front()->GetImageHeader(); |
| 2255 | uint32_t component_count = primary_header.GetComponentCount(); |
| 2256 | DCHECK_EQ(component_count, image_spaces.size()); |
| 2257 | std::string boot_image_checksum = |
| 2258 | StringPrintf("i;%d/%08x", component_count, primary_header.GetImageChecksum()); |
| 2259 | size_t pos = 0u; |
| 2260 | for (const ImageSpace* space : image_spaces) { |
| 2261 | size_t num_dex_files = space->oat_file_non_owned_->GetOatDexFiles().size(); |
| 2262 | if (kIsDebugBuild) { |
| 2263 | CHECK_NE(num_dex_files, 0u); |
| 2264 | CHECK_LE(space->oat_file_non_owned_->GetOatDexFiles().size(), boot_class_path.size() - pos); |
| 2265 | for (size_t i = 0; i != num_dex_files; ++i) { |
| 2266 | CHECK_EQ(space->oat_file_non_owned_->GetOatDexFiles()[i]->GetDexFileLocation(), |
| 2267 | boot_class_path[pos + i]->GetLocation()); |
| 2268 | } |
| 2269 | } |
| 2270 | pos += num_dex_files; |
| 2271 | } |
| 2272 | ArrayRef<const DexFile* const> boot_class_path_tail = |
| 2273 | ArrayRef<const DexFile* const>(boot_class_path).SubArray(pos); |
| 2274 | DCHECK(boot_class_path_tail.empty() || |
| 2275 | !DexFileLoader::IsMultiDexLocation(boot_class_path_tail.front()->GetLocation().c_str())); |
| 2276 | for (const DexFile* dex_file : boot_class_path_tail) { |
| 2277 | if (!DexFileLoader::IsMultiDexLocation(dex_file->GetLocation().c_str())) { |
| 2278 | StringAppendF(&boot_image_checksum, ":d"); |
| 2279 | } |
| 2280 | StringAppendF(&boot_image_checksum, "/%08x", dex_file->GetLocationChecksum()); |
| 2281 | } |
| 2282 | return boot_image_checksum; |
| 2283 | } |
| 2284 | |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 2285 | std::vector<std::string> ImageSpace::ExpandMultiImageLocations( |
| 2286 | const std::vector<std::string>& dex_locations, |
| 2287 | const std::string& image_location) { |
Vladimir Marko | 0ace563 | 2018-12-14 11:11:47 +0000 | [diff] [blame] | 2288 | return ExpandMultiImageLocations(ArrayRef<const std::string>(dex_locations), image_location); |
| 2289 | } |
| 2290 | |
| 2291 | std::vector<std::string> ImageSpace::ExpandMultiImageLocations( |
| 2292 | ArrayRef<const std::string> dex_locations, |
| 2293 | const std::string& image_location) { |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 2294 | DCHECK(!dex_locations.empty()); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 2295 | |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 2296 | // Find the path. |
| 2297 | size_t last_slash = image_location.rfind('/'); |
| 2298 | CHECK_NE(last_slash, std::string::npos); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 2299 | |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 2300 | // We also need to honor path components that were encoded through '@'. Otherwise the loading |
| 2301 | // code won't be able to find the images. |
| 2302 | if (image_location.find('@', last_slash) != std::string::npos) { |
| 2303 | last_slash = image_location.rfind('@'); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 2304 | } |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 2305 | |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 2306 | // Find the dot separating the primary image name from the extension. |
| 2307 | size_t last_dot = image_location.rfind('.'); |
| 2308 | // Extract the extension and base (the path and primary image name). |
| 2309 | std::string extension; |
| 2310 | std::string base = image_location; |
| 2311 | if (last_dot != std::string::npos && last_dot > last_slash) { |
| 2312 | extension = image_location.substr(last_dot); // Including the dot. |
| 2313 | base.resize(last_dot); |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 2314 | } |
Vladimir Marko | 91f1032 | 2018-12-07 18:04:10 +0000 | [diff] [blame] | 2315 | // For non-empty primary image name, add '-' to the `base`. |
| 2316 | if (last_slash + 1u != base.size()) { |
| 2317 | base += '-'; |
| 2318 | } |
| 2319 | |
| 2320 | std::vector<std::string> locations; |
| 2321 | locations.reserve(dex_locations.size()); |
| 2322 | locations.push_back(image_location); |
| 2323 | |
| 2324 | // Now create the other names. Use a counted loop to skip the first one. |
| 2325 | for (size_t i = 1u; i < dex_locations.size(); ++i) { |
| 2326 | // Replace path with `base` (i.e. image path and prefix) and replace the original |
| 2327 | // extension (if any) with `extension`. |
| 2328 | std::string name = dex_locations[i]; |
| 2329 | size_t last_dex_slash = name.rfind('/'); |
| 2330 | if (last_dex_slash != std::string::npos) { |
| 2331 | name = name.substr(last_dex_slash + 1); |
| 2332 | } |
| 2333 | size_t last_dex_dot = name.rfind('.'); |
| 2334 | if (last_dex_dot != std::string::npos) { |
| 2335 | name.resize(last_dex_dot); |
| 2336 | } |
| 2337 | locations.push_back(base + name + extension); |
| 2338 | } |
| 2339 | return locations; |
Andreas Gampe | 8994a04 | 2015-12-30 19:03:17 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Mathieu Chartier | d5f3f32 | 2016-03-21 14:05:56 -0700 | [diff] [blame] | 2342 | void ImageSpace::DumpSections(std::ostream& os) const { |
| 2343 | const uint8_t* base = Begin(); |
| 2344 | const ImageHeader& header = GetImageHeader(); |
| 2345 | for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { |
| 2346 | auto section_type = static_cast<ImageHeader::ImageSections>(i); |
| 2347 | const ImageSection& section = header.GetImageSection(section_type); |
| 2348 | os << section_type << " " << reinterpret_cast<const void*>(base + section.Offset()) |
| 2349 | << "-" << reinterpret_cast<const void*>(base + section.End()) << "\n"; |
| 2350 | } |
| 2351 | } |
| 2352 | |
Mathieu Chartier | 6e7a72c | 2019-03-07 21:40:10 -0800 | [diff] [blame] | 2353 | void ImageSpace::DisablePreResolvedStrings() { |
| 2354 | // Clear dex cache pointers. |
| 2355 | ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches = |
| 2356 | GetImageHeader().GetImageRoot(ImageHeader::kDexCaches)->AsObjectArray<mirror::DexCache>(); |
| 2357 | for (size_t len = dex_caches->GetLength(), i = 0; i < len; ++i) { |
| 2358 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 2359 | dex_cache->ClearPreResolvedStrings(); |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | void ImageSpace::ReleaseMetadata() { |
| 2364 | const ImageSection& metadata = GetImageHeader().GetMetadataSection(); |
| 2365 | VLOG(image) << "Releasing " << metadata.Size() << " image metadata bytes"; |
| 2366 | // In the case where new app images may have been added around the checkpoint, ensure that we |
| 2367 | // don't madvise the cache for these. |
| 2368 | ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches = |
| 2369 | GetImageHeader().GetImageRoot(ImageHeader::kDexCaches)->AsObjectArray<mirror::DexCache>(); |
| 2370 | bool have_startup_cache = false; |
| 2371 | for (size_t len = dex_caches->GetLength(), i = 0; i < len; ++i) { |
| 2372 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 2373 | if (dex_cache->NumPreResolvedStrings() != 0u) { |
| 2374 | have_startup_cache = true; |
| 2375 | } |
| 2376 | } |
| 2377 | // Only safe to do for images that have their preresolved strings caches disabled. This is because |
| 2378 | // uncompressed images madvise to the original unrelocated image contents. |
| 2379 | if (!have_startup_cache) { |
| 2380 | // Avoid using ZeroAndReleasePages since the zero fill might not be word atomic. |
| 2381 | uint8_t* const page_begin = AlignUp(Begin() + metadata.Offset(), kPageSize); |
| 2382 | uint8_t* const page_end = AlignDown(Begin() + metadata.End(), kPageSize); |
| 2383 | if (page_begin < page_end) { |
| 2384 | CHECK_NE(madvise(page_begin, page_end - page_begin, MADV_DONTNEED), -1) << "madvise failed"; |
| 2385 | } |
| 2386 | } |
| 2387 | } |
| 2388 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2389 | } // namespace space |
| 2390 | } // namespace gc |
| 2391 | } // namespace art |