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