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