Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -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_writer.h" |
| 18 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 19 | #include <lz4.h> |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 20 | #include <lz4hc.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 23 | #include <memory> |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 24 | #include <numeric> |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 25 | #include <unordered_set> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 28 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 29 | #include "art_method-inl.h" |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 30 | #include "base/callee_save_type.h" |
| 31 | #include "base/enums.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 32 | #include "base/logging.h" |
| 33 | #include "base/unix_file/fd_file.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 34 | #include "class_linker-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 35 | #include "compiled_method.h" |
| 36 | #include "dex_file-inl.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 37 | #include "dex_file_types.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 38 | #include "driver/compiler_driver.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 39 | #include "elf_file.h" |
| 40 | #include "elf_utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 41 | #include "elf_writer.h" |
| 42 | #include "gc/accounting/card_table-inl.h" |
| 43 | #include "gc/accounting/heap_bitmap.h" |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 44 | #include "gc/accounting/space_bitmap-inl.h" |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 45 | #include "gc/collector/concurrent_copying.h" |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 46 | #include "gc/heap-visit-objects-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 47 | #include "gc/heap.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 48 | #include "gc/space/large_object_space.h" |
| 49 | #include "gc/space/space-inl.h" |
Mathieu Chartier | 4f5e3cb | 2017-06-12 13:10:01 -0700 | [diff] [blame] | 50 | #include "gc/verification.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 51 | #include "globals.h" |
Mathieu Chartier | 4f5e3cb | 2017-06-12 13:10:01 -0700 | [diff] [blame] | 52 | #include "handle_scope-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 53 | #include "image.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 54 | #include "imt_conflict_table.h" |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 55 | #include "jni_internal.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 56 | #include "linear_alloc.h" |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 57 | #include "lock_word.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 58 | #include "mirror/array-inl.h" |
| 59 | #include "mirror/class-inl.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 60 | #include "mirror/class_ext.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 61 | #include "mirror/class_loader.h" |
| 62 | #include "mirror/dex_cache-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 63 | #include "mirror/dex_cache.h" |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 64 | #include "mirror/executable.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 65 | #include "mirror/method.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 66 | #include "mirror/object-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 67 | #include "mirror/object-refvisitor-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 68 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 69 | #include "mirror/string-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 70 | #include "oat.h" |
| 71 | #include "oat_file.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 72 | #include "oat_file_manager.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 73 | #include "runtime.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 74 | #include "scoped_thread_state_change-inl.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 75 | #include "utils/dex_cache_arrays_layout-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 76 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 77 | using ::art::mirror::Class; |
| 78 | using ::art::mirror::DexCache; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 79 | using ::art::mirror::Object; |
| 80 | using ::art::mirror::ObjectArray; |
| 81 | using ::art::mirror::String; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 82 | |
| 83 | namespace art { |
| 84 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 85 | // Separate objects into multiple bins to optimize dirty memory use. |
| 86 | static constexpr bool kBinObjects = true; |
| 87 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 88 | // Return true if an object is already in an image space. |
| 89 | bool ImageWriter::IsInBootImage(const void* obj) const { |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 90 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 91 | if (!compile_app_image_) { |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 92 | DCHECK(heap->GetBootImageSpaces().empty()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 93 | return false; |
| 94 | } |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 95 | for (gc::space::ImageSpace* boot_image_space : heap->GetBootImageSpaces()) { |
| 96 | const uint8_t* image_begin = boot_image_space->Begin(); |
| 97 | // Real image end including ArtMethods and ArtField sections. |
| 98 | const uint8_t* image_end = image_begin + boot_image_space->GetImageHeader().GetImageSize(); |
| 99 | if (image_begin <= obj && obj < image_end) { |
| 100 | return true; |
| 101 | } |
| 102 | } |
| 103 | return false; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | bool ImageWriter::IsInBootOatFile(const void* ptr) const { |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 107 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 108 | if (!compile_app_image_) { |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 109 | DCHECK(heap->GetBootImageSpaces().empty()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 110 | return false; |
| 111 | } |
Mathieu Chartier | e467cea | 2016-01-07 18:36:19 -0800 | [diff] [blame] | 112 | for (gc::space::ImageSpace* boot_image_space : heap->GetBootImageSpaces()) { |
| 113 | const ImageHeader& image_header = boot_image_space->GetImageHeader(); |
| 114 | if (image_header.GetOatFileBegin() <= ptr && ptr < image_header.GetOatFileEnd()) { |
| 115 | return true; |
| 116 | } |
| 117 | } |
| 118 | return false; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 121 | static void ClearDexFileCookies() REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 122 | auto visitor = [](Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 123 | DCHECK(obj != nullptr); |
| 124 | Class* klass = obj->GetClass(); |
| 125 | if (klass == WellKnownClasses::ToClass(WellKnownClasses::dalvik_system_DexFile)) { |
| 126 | ArtField* field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
| 127 | // Null out the cookie to enable determinism. b/34090128 |
| 128 | field->SetObject</*kTransactionActive*/false>(obj, nullptr); |
| 129 | } |
| 130 | }; |
| 131 | Runtime::Current()->GetHeap()->VisitObjects(visitor); |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 134 | bool ImageWriter::PrepareImageAddressSpace() { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 135 | target_ptr_size_ = InstructionSetPointerSize(compiler_driver_.GetInstructionSet()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 136 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 137 | { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 138 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 139 | PruneNonImageClasses(); // Remove junk |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 140 | if (compile_app_image_) { |
| 141 | // Clear dex file cookies for app images to enable app image determinism. This is required |
| 142 | // since the cookie field contains long pointers to DexFiles which are not deterministic. |
| 143 | // b/34090128 |
| 144 | ClearDexFileCookies(); |
| 145 | } else { |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 146 | // Avoid for app image since this may increase RAM and image size. |
| 147 | ComputeLazyFieldsForImageClasses(); // Add useful information |
| 148 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 149 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 150 | heap->CollectGarbage(false); // Remove garbage. |
| 151 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 152 | if (kIsDebugBuild) { |
| 153 | ScopedObjectAccess soa(Thread::Current()); |
| 154 | CheckNonImageClassesRemoved(); |
| 155 | } |
| 156 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 157 | { |
| 158 | ScopedObjectAccess soa(Thread::Current()); |
| 159 | CalculateNewObjectOffsets(); |
| 160 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 161 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 162 | // This needs to happen after CalculateNewObjectOffsets since it relies on intern_table_bytes_ and |
| 163 | // bin size sums being calculated. |
| 164 | if (!AllocMemory()) { |
| 165 | return false; |
| 166 | } |
| 167 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 171 | bool ImageWriter::Write(int image_fd, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 172 | const std::vector<const char*>& image_filenames, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 173 | const std::vector<const char*>& oat_filenames) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 174 | // If image_fd or oat_fd are not kInvalidFd then we may have empty strings in image_filenames or |
| 175 | // oat_filenames. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 176 | CHECK(!image_filenames.empty()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 177 | if (image_fd != kInvalidFd) { |
| 178 | CHECK_EQ(image_filenames.size(), 1u); |
| 179 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 180 | CHECK(!oat_filenames.empty()); |
| 181 | CHECK_EQ(image_filenames.size(), oat_filenames.size()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 182 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 183 | { |
| 184 | ScopedObjectAccess soa(Thread::Current()); |
| 185 | for (size_t i = 0; i < oat_filenames.size(); ++i) { |
| 186 | CreateHeader(i); |
| 187 | CopyAndFixupNativeData(i); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 190 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 191 | { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 192 | // TODO: heap validation can't handle these fix up passes. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 193 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 194 | Runtime::Current()->GetHeap()->DisableObjectValidation(); |
| 195 | CopyAndFixupObjects(); |
| 196 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 197 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 198 | for (size_t i = 0; i < image_filenames.size(); ++i) { |
| 199 | const char* image_filename = image_filenames[i]; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 200 | ImageInfo& image_info = GetImageInfo(i); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 201 | std::unique_ptr<File> image_file; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 202 | if (image_fd != kInvalidFd) { |
| 203 | if (strlen(image_filename) == 0u) { |
| 204 | image_file.reset(new File(image_fd, unix_file::kCheckSafeUsage)); |
Mathieu Chartier | 784bb09 | 2016-01-28 12:02:00 -0800 | [diff] [blame] | 205 | // Empty the file in case it already exists. |
| 206 | if (image_file != nullptr) { |
| 207 | TEMP_FAILURE_RETRY(image_file->SetLength(0)); |
| 208 | TEMP_FAILURE_RETRY(image_file->Flush()); |
| 209 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 210 | } else { |
| 211 | LOG(ERROR) << "image fd " << image_fd << " name " << image_filename; |
| 212 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 213 | } else { |
| 214 | image_file.reset(OS::CreateEmptyFile(image_filename)); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 215 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 216 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 217 | if (image_file == nullptr) { |
| 218 | LOG(ERROR) << "Failed to open image file " << image_filename; |
| 219 | return false; |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 220 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 221 | |
| 222 | if (!compile_app_image_ && fchmod(image_file->Fd(), 0644) != 0) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 223 | PLOG(ERROR) << "Failed to make image file world readable: " << image_filename; |
| 224 | image_file->Erase(); |
| 225 | return EXIT_FAILURE; |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 226 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 227 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 228 | std::unique_ptr<char[]> compressed_data; |
| 229 | // Image data size excludes the bitmap and the header. |
| 230 | ImageHeader* const image_header = reinterpret_cast<ImageHeader*>(image_info.image_->Begin()); |
| 231 | const size_t image_data_size = image_header->GetImageSize() - sizeof(ImageHeader); |
| 232 | char* image_data = reinterpret_cast<char*>(image_info.image_->Begin()) + sizeof(ImageHeader); |
| 233 | size_t data_size; |
| 234 | const char* image_data_to_write; |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 235 | const uint64_t compress_start_time = NanoTime(); |
Nicolas Geoffray | 83d4d72 | 2015-12-10 08:26:32 +0000 | [diff] [blame] | 236 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 237 | CHECK_EQ(image_header->storage_mode_, image_storage_mode_); |
| 238 | switch (image_storage_mode_) { |
Mathieu Chartier | 9894fc8 | 2016-03-17 19:19:15 -0700 | [diff] [blame] | 239 | case ImageHeader::kStorageModeLZ4HC: // Fall-through. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 240 | case ImageHeader::kStorageModeLZ4: { |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 241 | const size_t compressed_max_size = LZ4_compressBound(image_data_size); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 242 | compressed_data.reset(new char[compressed_max_size]); |
David Lin | 915ec55 | 2017-02-23 15:36:06 -0800 | [diff] [blame] | 243 | data_size = LZ4_compress_default( |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 244 | reinterpret_cast<char*>(image_info.image_->Begin()) + sizeof(ImageHeader), |
| 245 | &compressed_data[0], |
David Lin | 915ec55 | 2017-02-23 15:36:06 -0800 | [diff] [blame] | 246 | image_data_size, |
| 247 | compressed_max_size); |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 248 | |
| 249 | break; |
| 250 | } |
Mathieu Chartier | 9894fc8 | 2016-03-17 19:19:15 -0700 | [diff] [blame] | 251 | /* |
| 252 | * Disabled due to image_test64 flakyness. Both use same decompression. b/27560444 |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 253 | case ImageHeader::kStorageModeLZ4HC: { |
| 254 | // Bound is same as non HC. |
| 255 | const size_t compressed_max_size = LZ4_compressBound(image_data_size); |
| 256 | compressed_data.reset(new char[compressed_max_size]); |
| 257 | data_size = LZ4_compressHC( |
| 258 | reinterpret_cast<char*>(image_info.image_->Begin()) + sizeof(ImageHeader), |
| 259 | &compressed_data[0], |
| 260 | image_data_size); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 261 | break; |
| 262 | } |
Mathieu Chartier | 9894fc8 | 2016-03-17 19:19:15 -0700 | [diff] [blame] | 263 | */ |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 264 | case ImageHeader::kStorageModeUncompressed: { |
| 265 | data_size = image_data_size; |
| 266 | image_data_to_write = image_data; |
| 267 | break; |
| 268 | } |
| 269 | default: { |
| 270 | LOG(FATAL) << "Unsupported"; |
| 271 | UNREACHABLE(); |
| 272 | } |
| 273 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 274 | |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 275 | if (compressed_data != nullptr) { |
| 276 | image_data_to_write = &compressed_data[0]; |
| 277 | VLOG(compiler) << "Compressed from " << image_data_size << " to " << data_size << " in " |
| 278 | << PrettyDuration(NanoTime() - compress_start_time); |
Mathieu Chartier | 9894fc8 | 2016-03-17 19:19:15 -0700 | [diff] [blame] | 279 | if (kIsDebugBuild) { |
| 280 | std::unique_ptr<uint8_t[]> temp(new uint8_t[image_data_size]); |
| 281 | const size_t decompressed_size = LZ4_decompress_safe( |
| 282 | reinterpret_cast<char*>(&compressed_data[0]), |
| 283 | reinterpret_cast<char*>(&temp[0]), |
| 284 | data_size, |
| 285 | image_data_size); |
| 286 | CHECK_EQ(decompressed_size, image_data_size); |
| 287 | CHECK_EQ(memcmp(image_data, &temp[0], image_data_size), 0) << image_storage_mode_; |
| 288 | } |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 291 | // Write out the image + fields + methods. |
| 292 | const bool is_compressed = compressed_data != nullptr; |
Mathieu Chartier | 6f6b134 | 2016-03-09 11:14:50 -0800 | [diff] [blame] | 293 | if (!image_file->PwriteFully(image_data_to_write, data_size, sizeof(ImageHeader))) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 294 | PLOG(ERROR) << "Failed to write image file data " << image_filename; |
| 295 | image_file->Erase(); |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | // Write out the image bitmap at the page aligned start of the image end, also uncompressed for |
| 300 | // convenience. |
| 301 | const ImageSection& bitmap_section = image_header->GetImageSection( |
| 302 | ImageHeader::kSectionImageBitmap); |
| 303 | // Align up since data size may be unaligned if the image is compressed. |
| 304 | size_t bitmap_position_in_file = RoundUp(sizeof(ImageHeader) + data_size, kPageSize); |
| 305 | if (!is_compressed) { |
| 306 | CHECK_EQ(bitmap_position_in_file, bitmap_section.Offset()); |
| 307 | } |
Mathieu Chartier | 6f6b134 | 2016-03-09 11:14:50 -0800 | [diff] [blame] | 308 | if (!image_file->PwriteFully(reinterpret_cast<char*>(image_info.image_bitmap_->Begin()), |
| 309 | bitmap_section.Size(), |
| 310 | bitmap_position_in_file)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 311 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
| 312 | image_file->Erase(); |
| 313 | return false; |
| 314 | } |
Mathieu Chartier | 6f6b134 | 2016-03-09 11:14:50 -0800 | [diff] [blame] | 315 | |
| 316 | int err = image_file->Flush(); |
| 317 | if (err < 0) { |
| 318 | PLOG(ERROR) << "Failed to flush image file " << image_filename << " with result " << err; |
| 319 | image_file->Erase(); |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | // Write header last in case the compiler gets killed in the middle of image writing. |
| 324 | // We do not want to have a corrupted image with a valid header. |
| 325 | // The header is uncompressed since it contains whether the image is compressed or not. |
| 326 | image_header->data_size_ = data_size; |
| 327 | if (!image_file->PwriteFully(reinterpret_cast<char*>(image_info.image_->Begin()), |
| 328 | sizeof(ImageHeader), |
| 329 | 0)) { |
| 330 | PLOG(ERROR) << "Failed to write image file header " << image_filename; |
| 331 | image_file->Erase(); |
| 332 | return false; |
| 333 | } |
| 334 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 335 | CHECK_EQ(bitmap_position_in_file + bitmap_section.Size(), |
| 336 | static_cast<size_t>(image_file->GetLength())); |
| 337 | if (image_file->FlushCloseOrErase() != 0) { |
| 338 | PLOG(ERROR) << "Failed to flush and close image file " << image_filename; |
| 339 | return false; |
| 340 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 341 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 342 | return true; |
| 343 | } |
| 344 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 345 | void ImageWriter::SetImageOffset(mirror::Object* object, size_t offset) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 346 | DCHECK(object != nullptr); |
| 347 | DCHECK_NE(offset, 0U); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 348 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 349 | // The object is already deflated from when we set the bin slot. Just overwrite the lock word. |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 350 | object->SetLockWord(LockWord::FromForwardingAddress(offset), false); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 351 | DCHECK_EQ(object->GetLockWord(false).ReadBarrierState(), 0u); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 352 | DCHECK(IsImageOffsetAssigned(object)); |
| 353 | } |
| 354 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 355 | void ImageWriter::UpdateImageOffset(mirror::Object* obj, uintptr_t offset) { |
| 356 | DCHECK(IsImageOffsetAssigned(obj)) << obj << " " << offset; |
| 357 | obj->SetLockWord(LockWord::FromForwardingAddress(offset), false); |
| 358 | DCHECK_EQ(obj->GetLockWord(false).ReadBarrierState(), 0u); |
| 359 | } |
| 360 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 361 | void ImageWriter::AssignImageOffset(mirror::Object* object, ImageWriter::BinSlot bin_slot) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 362 | DCHECK(object != nullptr); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 363 | DCHECK_NE(image_objects_offset_begin_, 0u); |
| 364 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 365 | size_t oat_index = GetOatIndex(object); |
| 366 | ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 367 | size_t bin_slot_offset = image_info.bin_slot_offsets_[bin_slot.GetBin()]; |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 368 | size_t new_offset = bin_slot_offset + bin_slot.GetIndex(); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 369 | DCHECK_ALIGNED(new_offset, kObjectAlignment); |
| 370 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 371 | SetImageOffset(object, new_offset); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 372 | DCHECK_LT(new_offset, image_info.image_end_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 375 | bool ImageWriter::IsImageOffsetAssigned(mirror::Object* object) const { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 376 | // Will also return true if the bin slot was assigned since we are reusing the lock word. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 377 | DCHECK(object != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 378 | return object->GetLockWord(false).GetState() == LockWord::kForwardingAddress; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 381 | size_t ImageWriter::GetImageOffset(mirror::Object* object) const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 382 | DCHECK(object != nullptr); |
| 383 | DCHECK(IsImageOffsetAssigned(object)); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 384 | LockWord lock_word = object->GetLockWord(false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 385 | size_t offset = lock_word.ForwardingAddress(); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 386 | size_t oat_index = GetOatIndex(object); |
| 387 | const ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 388 | DCHECK_LT(offset, image_info.image_end_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 389 | return offset; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 392 | void ImageWriter::SetImageBinSlot(mirror::Object* object, BinSlot bin_slot) { |
| 393 | DCHECK(object != nullptr); |
| 394 | DCHECK(!IsImageOffsetAssigned(object)); |
| 395 | DCHECK(!IsImageBinSlotAssigned(object)); |
| 396 | |
| 397 | // Before we stomp over the lock word, save the hash code for later. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 398 | LockWord lw(object->GetLockWord(false)); |
| 399 | switch (lw.GetState()) { |
Ian Rogers | 23e81a1 | 2017-07-05 09:36:27 -0700 | [diff] [blame] | 400 | case LockWord::kFatLocked: |
| 401 | FALLTHROUGH_INTENDED; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 402 | case LockWord::kThinLocked: { |
Ian Rogers | 23e81a1 | 2017-07-05 09:36:27 -0700 | [diff] [blame] | 403 | std::ostringstream oss; |
| 404 | bool thin = (lw.GetState() == LockWord::kThinLocked); |
| 405 | oss << (thin ? "Thin" : "Fat") |
| 406 | << " locked object " << object << "(" << object->PrettyTypeOf() |
| 407 | << ") found during object copy"; |
| 408 | if (thin) { |
| 409 | oss << ". Lock owner:" << lw.ThinLockOwner(); |
| 410 | } |
| 411 | LOG(FATAL) << oss.str(); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 412 | break; |
| 413 | } |
| 414 | case LockWord::kUnlocked: |
| 415 | // No hash, don't need to save it. |
| 416 | break; |
| 417 | case LockWord::kHashCode: |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 418 | DCHECK(saved_hashcode_map_.find(object) == saved_hashcode_map_.end()); |
| 419 | saved_hashcode_map_.emplace(object, lw.GetHashCode()); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 420 | break; |
| 421 | default: |
| 422 | LOG(FATAL) << "Unreachable."; |
| 423 | UNREACHABLE(); |
| 424 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 425 | object->SetLockWord(LockWord::FromForwardingAddress(bin_slot.Uint32Value()), false); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 426 | DCHECK_EQ(object->GetLockWord(false).ReadBarrierState(), 0u); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 427 | DCHECK(IsImageBinSlotAssigned(object)); |
| 428 | } |
| 429 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 430 | void ImageWriter::PrepareDexCacheArraySlots() { |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 431 | // Prepare dex cache array starts based on the ordering specified in the CompilerDriver. |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 432 | // Set the slot size early to avoid DCHECK() failures in IsImageBinSlotAssigned() |
| 433 | // when AssignImageBinSlot() assigns their indexes out or order. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 434 | for (const DexFile* dex_file : compiler_driver_.GetDexFilesForOatFile()) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 435 | auto it = dex_file_oat_index_map_.find(dex_file); |
| 436 | DCHECK(it != dex_file_oat_index_map_.end()) << dex_file->GetLocation(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 437 | ImageInfo& image_info = GetImageInfo(it->second); |
| 438 | image_info.dex_cache_array_starts_.Put(dex_file, image_info.bin_slot_sizes_[kBinDexCacheArray]); |
| 439 | DexCacheArraysLayout layout(target_ptr_size_, dex_file); |
| 440 | image_info.bin_slot_sizes_[kBinDexCacheArray] += layout.Size(); |
| 441 | } |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 442 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 443 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 444 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 445 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 446 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 447 | ObjPtr<mirror::DexCache> dex_cache = |
| 448 | ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)); |
| 449 | if (dex_cache == nullptr || IsInBootImage(dex_cache.Ptr())) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 450 | continue; |
| 451 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 452 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | 0b49084 | 2016-05-25 15:05:59 -0700 | [diff] [blame] | 453 | CHECK(dex_file_oat_index_map_.find(dex_file) != dex_file_oat_index_map_.end()) |
| 454 | << "Dex cache should have been pruned " << dex_file->GetLocation() |
| 455 | << "; possibly in class path"; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 456 | DexCacheArraysLayout layout(target_ptr_size_, dex_file); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 457 | DCHECK(layout.Valid()); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 458 | size_t oat_index = GetOatIndexForDexCache(dex_cache); |
| 459 | ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 460 | uint32_t start = image_info.dex_cache_array_starts_.Get(dex_file); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 461 | DCHECK_EQ(dex_file->NumTypeIds() != 0u, dex_cache->GetResolvedTypes() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 462 | AddDexCacheArrayRelocation(dex_cache->GetResolvedTypes(), |
| 463 | start + layout.TypesOffset(), |
| 464 | dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 465 | DCHECK_EQ(dex_file->NumMethodIds() != 0u, dex_cache->GetResolvedMethods() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 466 | AddDexCacheArrayRelocation(dex_cache->GetResolvedMethods(), |
| 467 | start + layout.MethodsOffset(), |
| 468 | dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 469 | DCHECK_EQ(dex_file->NumFieldIds() != 0u, dex_cache->GetResolvedFields() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 470 | AddDexCacheArrayRelocation(dex_cache->GetResolvedFields(), |
| 471 | start + layout.FieldsOffset(), |
| 472 | dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 473 | DCHECK_EQ(dex_file->NumStringIds() != 0u, dex_cache->GetStrings() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 474 | AddDexCacheArrayRelocation(dex_cache->GetStrings(), start + layout.StringsOffset(), dex_cache); |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 475 | |
| 476 | if (dex_cache->GetResolvedMethodTypes() != nullptr) { |
| 477 | AddDexCacheArrayRelocation(dex_cache->GetResolvedMethodTypes(), |
| 478 | start + layout.MethodTypesOffset(), |
| 479 | dex_cache); |
| 480 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 481 | if (dex_cache->GetResolvedCallSites() != nullptr) { |
| 482 | AddDexCacheArrayRelocation(dex_cache->GetResolvedCallSites(), |
| 483 | start + layout.CallSitesOffset(), |
| 484 | dex_cache); |
| 485 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 486 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 489 | void ImageWriter::AddDexCacheArrayRelocation(void* array, |
| 490 | size_t offset, |
| 491 | ObjPtr<mirror::DexCache> dex_cache) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 492 | if (array != nullptr) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 493 | DCHECK(!IsInBootImage(array)); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 494 | size_t oat_index = GetOatIndexForDexCache(dex_cache); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 495 | native_object_relocations_.emplace(array, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 496 | NativeObjectRelocation { oat_index, offset, kNativeObjectRelocationTypeDexCacheArray }); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 500 | void ImageWriter::AddMethodPointerArray(mirror::PointerArray* arr) { |
| 501 | DCHECK(arr != nullptr); |
| 502 | if (kIsDebugBuild) { |
| 503 | for (size_t i = 0, len = arr->GetLength(); i < len; i++) { |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 504 | ArtMethod* method = arr->GetElementPtrSize<ArtMethod*>(i, target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 505 | if (method != nullptr && !method->IsRuntimeMethod()) { |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 506 | mirror::Class* klass = method->GetDeclaringClass(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 507 | CHECK(klass == nullptr || KeepClass(klass)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 508 | << Class::PrettyClass(klass) << " should be a kept class"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | } |
| 512 | // kBinArtMethodClean picked arbitrarily, just required to differentiate between ArtFields and |
| 513 | // ArtMethods. |
| 514 | pointer_arrays_.emplace(arr, kBinArtMethodClean); |
| 515 | } |
| 516 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 517 | void ImageWriter::AssignImageBinSlot(mirror::Object* object, size_t oat_index) { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 518 | DCHECK(object != nullptr); |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 519 | size_t object_size = object->SizeOf(); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 520 | |
| 521 | // The magic happens here. We segregate objects into different bins based |
| 522 | // on how likely they are to get dirty at runtime. |
| 523 | // |
| 524 | // Likely-to-dirty objects get packed together into the same bin so that |
| 525 | // at runtime their page dirtiness ratio (how many dirty objects a page has) is |
| 526 | // maximized. |
| 527 | // |
| 528 | // This means more pages will stay either clean or shared dirty (with zygote) and |
| 529 | // the app will use less of its own (private) memory. |
| 530 | Bin bin = kBinRegular; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 531 | size_t current_offset = 0u; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 532 | |
| 533 | if (kBinObjects) { |
| 534 | // |
| 535 | // Changing the bin of an object is purely a memory-use tuning. |
| 536 | // It has no change on runtime correctness. |
| 537 | // |
| 538 | // Memory analysis has determined that the following types of objects get dirtied |
| 539 | // the most: |
| 540 | // |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 541 | // * Dex cache arrays are stored in a special bin. The arrays for each dex cache have |
| 542 | // a fixed layout which helps improve generated code (using PC-relative addressing), |
| 543 | // so we pre-calculate their offsets separately in PrepareDexCacheArraySlots(). |
| 544 | // Since these arrays are huge, most pages do not overlap other objects and it's not |
| 545 | // really important where they are for the clean/dirty separation. Due to their |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 546 | // special PC-relative addressing, we arbitrarily keep them at the end. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 547 | // * Class'es which are verified [their clinit runs only at runtime] |
| 548 | // - classes in general [because their static fields get overwritten] |
| 549 | // - initialized classes with all-final statics are unlikely to be ever dirty, |
| 550 | // so bin them separately |
| 551 | // * Art Methods that are: |
| 552 | // - native [their native entry point is not looked up until runtime] |
| 553 | // - have declaring classes that aren't initialized |
| 554 | // [their interpreter/quick entry points are trampolines until the class |
| 555 | // becomes initialized] |
| 556 | // |
| 557 | // We also assume the following objects get dirtied either never or extremely rarely: |
| 558 | // * Strings (they are immutable) |
| 559 | // * Art methods that aren't native and have initialized declared classes |
| 560 | // |
| 561 | // We assume that "regular" bin objects are highly unlikely to become dirtied, |
| 562 | // so packing them together will not result in a noticeably tighter dirty-to-clean ratio. |
| 563 | // |
| 564 | if (object->IsClass()) { |
| 565 | bin = kBinClassVerified; |
| 566 | mirror::Class* klass = object->AsClass(); |
| 567 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 568 | // Add non-embedded vtable to the pointer array table if there is one. |
| 569 | auto* vtable = klass->GetVTable(); |
| 570 | if (vtable != nullptr) { |
| 571 | AddMethodPointerArray(vtable); |
| 572 | } |
| 573 | auto* iftable = klass->GetIfTable(); |
| 574 | if (iftable != nullptr) { |
| 575 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 576 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 577 | AddMethodPointerArray(iftable->GetMethodArray(i)); |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 582 | // Move known dirty objects into their own sections. This includes: |
| 583 | // - classes with dirty static fields. |
| 584 | if (dirty_image_objects_ != nullptr && |
| 585 | dirty_image_objects_->find(klass->PrettyDescriptor()) != dirty_image_objects_->end()) { |
| 586 | bin = kBinKnownDirty; |
| 587 | } else if (klass->GetStatus() == Class::kStatusInitialized) { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 588 | bin = kBinClassInitialized; |
| 589 | |
| 590 | // If the class's static fields are all final, put it into a separate bin |
| 591 | // since it's very likely it will stay clean. |
| 592 | uint32_t num_static_fields = klass->NumStaticFields(); |
| 593 | if (num_static_fields == 0) { |
| 594 | bin = kBinClassInitializedFinalStatics; |
| 595 | } else { |
| 596 | // Maybe all the statics are final? |
| 597 | bool all_final = true; |
| 598 | for (uint32_t i = 0; i < num_static_fields; ++i) { |
| 599 | ArtField* field = klass->GetStaticField(i); |
| 600 | if (!field->IsFinal()) { |
| 601 | all_final = false; |
| 602 | break; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (all_final) { |
| 607 | bin = kBinClassInitializedFinalStatics; |
| 608 | } |
| 609 | } |
| 610 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 611 | } else if (object->GetClass<kVerifyNone>()->IsStringClass()) { |
| 612 | bin = kBinString; // Strings are almost always immutable (except for object header). |
Mathieu Chartier | 2ba04ea | 2016-04-08 19:01:05 -0700 | [diff] [blame] | 613 | } else if (object->GetClass<kVerifyNone>() == |
| 614 | Runtime::Current()->GetClassLinker()->GetClassRoot(ClassLinker::kJavaLangObject)) { |
| 615 | // Instance of java lang object, probably a lock object. This means it will be dirty when we |
| 616 | // synchronize on it. |
| 617 | bin = kBinMiscDirty; |
| 618 | } else if (object->IsDexCache()) { |
| 619 | // Dex file field becomes dirty when the image is loaded. |
| 620 | bin = kBinMiscDirty; |
| 621 | } |
| 622 | // else bin = kBinRegular |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 625 | // Assign the oat index too. |
| 626 | DCHECK(oat_index_map_.find(object) == oat_index_map_.end()); |
| 627 | oat_index_map_.emplace(object, oat_index); |
| 628 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 629 | ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 630 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 631 | size_t offset_delta = RoundUp(object_size, kObjectAlignment); // 64-bit alignment |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 632 | current_offset = image_info.bin_slot_sizes_[bin]; // How many bytes the current bin is at (aligned). |
| 633 | // Move the current bin size up to accommodate the object we just assigned a bin slot. |
| 634 | image_info.bin_slot_sizes_[bin] += offset_delta; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 635 | |
| 636 | BinSlot new_bin_slot(bin, current_offset); |
| 637 | SetImageBinSlot(object, new_bin_slot); |
| 638 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 639 | ++image_info.bin_slot_count_[bin]; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 640 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 641 | // Grow the image closer to the end by the object we just assigned. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 642 | image_info.image_end_ += offset_delta; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 645 | bool ImageWriter::WillMethodBeDirty(ArtMethod* m) const { |
| 646 | if (m->IsNative()) { |
| 647 | return true; |
| 648 | } |
| 649 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 650 | // Initialized is highly unlikely to dirty since there's no entry points to mutate. |
| 651 | return declaring_class == nullptr || declaring_class->GetStatus() != Class::kStatusInitialized; |
| 652 | } |
| 653 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 654 | bool ImageWriter::IsImageBinSlotAssigned(mirror::Object* object) const { |
| 655 | DCHECK(object != nullptr); |
| 656 | |
| 657 | // We always stash the bin slot into a lockword, in the 'forwarding address' state. |
| 658 | // If it's in some other state, then we haven't yet assigned an image bin slot. |
| 659 | if (object->GetLockWord(false).GetState() != LockWord::kForwardingAddress) { |
| 660 | return false; |
| 661 | } else if (kIsDebugBuild) { |
| 662 | LockWord lock_word = object->GetLockWord(false); |
| 663 | size_t offset = lock_word.ForwardingAddress(); |
| 664 | BinSlot bin_slot(offset); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 665 | size_t oat_index = GetOatIndex(object); |
| 666 | const ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 667 | DCHECK_LT(bin_slot.GetIndex(), image_info.bin_slot_sizes_[bin_slot.GetBin()]) |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 668 | << "bin slot offset should not exceed the size of that bin"; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 669 | } |
| 670 | return true; |
| 671 | } |
| 672 | |
| 673 | ImageWriter::BinSlot ImageWriter::GetImageBinSlot(mirror::Object* object) const { |
| 674 | DCHECK(object != nullptr); |
| 675 | DCHECK(IsImageBinSlotAssigned(object)); |
| 676 | |
| 677 | LockWord lock_word = object->GetLockWord(false); |
| 678 | size_t offset = lock_word.ForwardingAddress(); // TODO: ForwardingAddress should be uint32_t |
| 679 | DCHECK_LE(offset, std::numeric_limits<uint32_t>::max()); |
| 680 | |
| 681 | BinSlot bin_slot(static_cast<uint32_t>(offset)); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 682 | size_t oat_index = GetOatIndex(object); |
| 683 | const ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 684 | DCHECK_LT(bin_slot.GetIndex(), image_info.bin_slot_sizes_[bin_slot.GetBin()]); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 685 | |
| 686 | return bin_slot; |
| 687 | } |
| 688 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 689 | bool ImageWriter::AllocMemory() { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 690 | for (ImageInfo& image_info : image_infos_) { |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 691 | ImageSection unused_sections[ImageHeader::kSectionCount]; |
| 692 | const size_t length = RoundUp( |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 693 | image_info.CreateImageSections(unused_sections), kPageSize); |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 694 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 695 | std::string error_msg; |
| 696 | image_info.image_.reset(MemMap::MapAnonymous("image writer image", |
| 697 | nullptr, |
| 698 | length, |
| 699 | PROT_READ | PROT_WRITE, |
| 700 | false, |
| 701 | false, |
| 702 | &error_msg)); |
| 703 | if (UNLIKELY(image_info.image_.get() == nullptr)) { |
| 704 | LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg; |
| 705 | return false; |
| 706 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 707 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 708 | // Create the image bitmap, only needs to cover mirror object section which is up to image_end_. |
| 709 | CHECK_LE(image_info.image_end_, length); |
| 710 | image_info.image_bitmap_.reset(gc::accounting::ContinuousSpaceBitmap::Create( |
| 711 | "image bitmap", image_info.image_->Begin(), RoundUp(image_info.image_end_, kPageSize))); |
| 712 | if (image_info.image_bitmap_.get() == nullptr) { |
| 713 | LOG(ERROR) << "Failed to allocate memory for image bitmap"; |
| 714 | return false; |
| 715 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 716 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 717 | return true; |
| 718 | } |
| 719 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 720 | class ImageWriter::ComputeLazyFieldsForClassesVisitor : public ClassVisitor { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 721 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 722 | bool operator()(ObjPtr<Class> c) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 723 | StackHandleScope<1> hs(Thread::Current()); |
| 724 | mirror::Class::ComputeName(hs.NewHandle(c)); |
| 725 | return true; |
| 726 | } |
| 727 | }; |
| 728 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 729 | void ImageWriter::ComputeLazyFieldsForImageClasses() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 730 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 731 | ComputeLazyFieldsForClassesVisitor visitor; |
| 732 | class_linker->VisitClassesWithoutClassesLock(&visitor); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 735 | static bool IsBootClassLoaderClass(ObjPtr<mirror::Class> klass) |
| 736 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 737 | return klass->GetClassLoader() == nullptr; |
| 738 | } |
| 739 | |
| 740 | bool ImageWriter::IsBootClassLoaderNonImageClass(mirror::Class* klass) { |
| 741 | return IsBootClassLoaderClass(klass) && !IsInBootImage(klass); |
| 742 | } |
| 743 | |
Chang Xing | b2e8adc | 2017-07-07 17:35:37 -0700 | [diff] [blame] | 744 | // This visitor follows the references of an instance, recursively then prune this class |
| 745 | // if a type of any field is pruned. |
| 746 | class ImageWriter::PruneObjectReferenceVisitor { |
| 747 | public: |
| 748 | PruneObjectReferenceVisitor(ImageWriter* image_writer, |
| 749 | bool* early_exit, |
| 750 | std::unordered_set<mirror::Object*>* visited, |
| 751 | bool* result) |
| 752 | : image_writer_(image_writer), early_exit_(early_exit), visited_(visited), result_(result) {} |
| 753 | |
| 754 | ALWAYS_INLINE void VisitRootIfNonNull( |
| 755 | mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const |
| 756 | REQUIRES_SHARED(Locks::mutator_lock_) { } |
| 757 | |
| 758 | ALWAYS_INLINE void VisitRoot( |
| 759 | mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const |
| 760 | REQUIRES_SHARED(Locks::mutator_lock_) { } |
| 761 | |
| 762 | ALWAYS_INLINE void operator() (ObjPtr<mirror::Object> obj, |
| 763 | MemberOffset offset, |
| 764 | bool is_static ATTRIBUTE_UNUSED) const |
| 765 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 766 | mirror::Object* ref = |
| 767 | obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset); |
| 768 | if (ref == nullptr || visited_->find(ref) != visited_->end()) { |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | ObjPtr<mirror::Class> klass = ref->IsClass() ? ref->AsClass() : ref->GetClass(); |
| 773 | if (klass == mirror::Method::StaticClass() || klass == mirror::Constructor::StaticClass()) { |
| 774 | // Prune all classes using reflection because the content they held will not be fixup. |
| 775 | *result_ = true; |
| 776 | } |
| 777 | |
Chang Xing | b2e8adc | 2017-07-07 17:35:37 -0700 | [diff] [blame] | 778 | if (ref->IsClass()) { |
| 779 | *result_ = *result_ || |
| 780 | image_writer_->PruneAppImageClassInternal(ref->AsClass(), early_exit_, visited_); |
| 781 | } else { |
Chang Xing | ec37325 | 2017-07-13 17:51:52 -0700 | [diff] [blame] | 782 | // Record the object visited in case of circular reference. |
| 783 | visited_->emplace(ref); |
Chang Xing | b2e8adc | 2017-07-07 17:35:37 -0700 | [diff] [blame] | 784 | *result_ = *result_ || |
| 785 | image_writer_->PruneAppImageClassInternal(klass, early_exit_, visited_); |
| 786 | ref->VisitReferences(*this, *this); |
Chang Xing | ec37325 | 2017-07-13 17:51:52 -0700 | [diff] [blame] | 787 | // Clean up before exit for next call of this function. |
| 788 | visited_->erase(ref); |
Chang Xing | b2e8adc | 2017-07-07 17:35:37 -0700 | [diff] [blame] | 789 | } |
Chang Xing | b2e8adc | 2017-07-07 17:35:37 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | ALWAYS_INLINE void operator() (ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 793 | ObjPtr<mirror::Reference> ref) const |
| 794 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 795 | operator()(ref, mirror::Reference::ReferentOffset(), /* is_static */ false); |
| 796 | } |
| 797 | |
| 798 | ALWAYS_INLINE bool GetResult() const { |
| 799 | return result_; |
| 800 | } |
| 801 | |
| 802 | private: |
| 803 | ImageWriter* image_writer_; |
| 804 | bool* early_exit_; |
| 805 | std::unordered_set<mirror::Object*>* visited_; |
| 806 | bool* const result_; |
| 807 | }; |
| 808 | |
| 809 | |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 810 | bool ImageWriter::PruneAppImageClass(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 811 | bool early_exit = false; |
Chang Xing | b2e8adc | 2017-07-07 17:35:37 -0700 | [diff] [blame] | 812 | std::unordered_set<mirror::Object*> visited; |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 813 | return PruneAppImageClassInternal(klass, &early_exit, &visited); |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 816 | bool ImageWriter::PruneAppImageClassInternal( |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 817 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 818 | bool* early_exit, |
Chang Xing | b2e8adc | 2017-07-07 17:35:37 -0700 | [diff] [blame] | 819 | std::unordered_set<mirror::Object*>* visited) { |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 820 | DCHECK(early_exit != nullptr); |
| 821 | DCHECK(visited != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 822 | DCHECK(compile_app_image_); |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 823 | if (klass == nullptr || IsInBootImage(klass.Ptr())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 824 | return false; |
| 825 | } |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 826 | auto found = prune_class_memo_.find(klass.Ptr()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 827 | if (found != prune_class_memo_.end()) { |
| 828 | // Already computed, return the found value. |
| 829 | return found->second; |
| 830 | } |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 831 | // Circular dependencies, return false but do not store the result in the memoization table. |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 832 | if (visited->find(klass.Ptr()) != visited->end()) { |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 833 | *early_exit = true; |
| 834 | return false; |
| 835 | } |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 836 | visited->emplace(klass.Ptr()); |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 837 | bool result = IsBootClassLoaderClass(klass); |
| 838 | std::string temp; |
| 839 | // Prune if not an image class, this handles any broken sets of image classes such as having a |
| 840 | // class in the set but not it's superclass. |
| 841 | result = result || !compiler_driver_.IsImageClass(klass->GetDescriptor(&temp)); |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 842 | bool my_early_exit = false; // Only for ourselves, ignore caller. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 843 | // Remove classes that failed to verify since we don't want to have java.lang.VerifyError in the |
| 844 | // app image. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 845 | if (klass->IsErroneous()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 846 | result = true; |
| 847 | } else { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 848 | ObjPtr<mirror::ClassExt> ext(klass->GetExtData()); |
| 849 | CHECK(ext.IsNull() || ext->GetVerifyError() == nullptr) << klass->PrettyClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 850 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 851 | if (!result) { |
| 852 | // Check interfaces since these wont be visited through VisitReferences.) |
| 853 | mirror::IfTable* if_table = klass->GetIfTable(); |
| 854 | for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) { |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 855 | result = result || PruneAppImageClassInternal(if_table->GetInterface(i), |
| 856 | &my_early_exit, |
| 857 | visited); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 858 | } |
| 859 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 860 | if (klass->IsObjectArrayClass()) { |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 861 | result = result || PruneAppImageClassInternal(klass->GetComponentType(), |
| 862 | &my_early_exit, |
| 863 | visited); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 864 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 865 | // Check static fields and their classes. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 866 | if (klass->IsResolved() && klass->NumReferenceStaticFields() != 0) { |
| 867 | size_t num_static_fields = klass->NumReferenceStaticFields(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 868 | // Presumably GC can happen when we are cross compiling, it should not cause performance |
| 869 | // problems to do pointer size logic. |
| 870 | MemberOffset field_offset = klass->GetFirstReferenceStaticFieldOffset( |
| 871 | Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 872 | for (size_t i = 0u; i < num_static_fields; ++i) { |
| 873 | mirror::Object* ref = klass->GetFieldObject<mirror::Object>(field_offset); |
| 874 | if (ref != nullptr) { |
| 875 | if (ref->IsClass()) { |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 876 | result = result || PruneAppImageClassInternal(ref->AsClass(), |
| 877 | &my_early_exit, |
| 878 | visited); |
| 879 | } else { |
Chang Xing | b2e8adc | 2017-07-07 17:35:37 -0700 | [diff] [blame] | 880 | mirror::Class* type = ref->GetClass(); |
| 881 | result = result || PruneAppImageClassInternal(type, |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 882 | &my_early_exit, |
| 883 | visited); |
Chang Xing | b2e8adc | 2017-07-07 17:35:37 -0700 | [diff] [blame] | 884 | if (!result) { |
| 885 | // For non-class case, also go through all the types mentioned by it's fields' |
| 886 | // references recursively to decide whether to keep this class. |
| 887 | bool tmp = false; |
| 888 | PruneObjectReferenceVisitor visitor(this, &my_early_exit, visited, &tmp); |
| 889 | ref->VisitReferences(visitor, visitor); |
| 890 | result = result || tmp; |
| 891 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 892 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 893 | } |
| 894 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 895 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 896 | } |
| 897 | } |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 898 | result = result || PruneAppImageClassInternal(klass->GetSuperClass(), |
| 899 | &my_early_exit, |
| 900 | visited); |
Mathieu Chartier | c27bc40 | 2016-08-05 16:09:09 -0700 | [diff] [blame] | 901 | // Remove the class if the dex file is not in the set of dex files. This happens for classes that |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 902 | // are from uses-library if there is no profile. b/30688277 |
Mathieu Chartier | c27bc40 | 2016-08-05 16:09:09 -0700 | [diff] [blame] | 903 | mirror::DexCache* dex_cache = klass->GetDexCache(); |
| 904 | if (dex_cache != nullptr) { |
| 905 | result = result || |
| 906 | dex_file_oat_index_map_.find(dex_cache->GetDexFile()) == dex_file_oat_index_map_.end(); |
| 907 | } |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 908 | // Erase the element we stored earlier since we are exiting the function. |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 909 | auto it = visited->find(klass.Ptr()); |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 910 | DCHECK(it != visited->end()); |
| 911 | visited->erase(it); |
| 912 | // Only store result if it is true or none of the calls early exited due to circular |
| 913 | // dependencies. If visited is empty then we are the root caller, in this case the cycle was in |
| 914 | // a child call and we can remember the result. |
| 915 | if (result == true || !my_early_exit || visited->empty()) { |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 916 | prune_class_memo_[klass.Ptr()] = result; |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 917 | } |
| 918 | *early_exit |= my_early_exit; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 919 | return result; |
| 920 | } |
| 921 | |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 922 | bool ImageWriter::KeepClass(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 923 | if (klass == nullptr) { |
| 924 | return false; |
| 925 | } |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 926 | if (compile_app_image_ && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 927 | // Already in boot image, return true. |
| 928 | return true; |
| 929 | } |
| 930 | std::string temp; |
| 931 | if (!compiler_driver_.IsImageClass(klass->GetDescriptor(&temp))) { |
| 932 | return false; |
| 933 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 934 | if (compile_app_image_) { |
| 935 | // For app images, we need to prune boot loader classes that are not in the boot image since |
| 936 | // these may have already been loaded when the app image is loaded. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 937 | // Keep classes in the boot image space since we don't want to re-resolve these. |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 938 | return !PruneAppImageClass(klass); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 939 | } |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 940 | return true; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 943 | class ImageWriter::PruneClassesVisitor : public ClassVisitor { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 944 | public: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 945 | PruneClassesVisitor(ImageWriter* image_writer, ObjPtr<mirror::ClassLoader> class_loader) |
| 946 | : image_writer_(image_writer), |
| 947 | class_loader_(class_loader), |
| 948 | classes_to_prune_(), |
| 949 | defined_class_count_(0u) { } |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 950 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 951 | bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 952 | if (!image_writer_->KeepClass(klass.Ptr())) { |
| 953 | classes_to_prune_.insert(klass.Ptr()); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 954 | if (klass->GetClassLoader() == class_loader_) { |
| 955 | ++defined_class_count_; |
| 956 | } |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 957 | } |
| 958 | return true; |
| 959 | } |
| 960 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 961 | size_t Prune() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 962 | ClassTable* class_table = |
| 963 | Runtime::Current()->GetClassLinker()->ClassTableForClassLoader(class_loader_); |
| 964 | for (mirror::Class* klass : classes_to_prune_) { |
| 965 | std::string storage; |
| 966 | const char* descriptor = klass->GetDescriptor(&storage); |
| 967 | bool result = class_table->Remove(descriptor); |
| 968 | DCHECK(result); |
| 969 | DCHECK(!class_table->Remove(descriptor)) << descriptor; |
| 970 | } |
| 971 | return defined_class_count_; |
| 972 | } |
| 973 | |
| 974 | private: |
Vladimir Marko | cb5ab35 | 2016-11-30 15:31:13 +0000 | [diff] [blame] | 975 | ImageWriter* const image_writer_; |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 976 | const ObjPtr<mirror::ClassLoader> class_loader_; |
| 977 | std::unordered_set<mirror::Class*> classes_to_prune_; |
| 978 | size_t defined_class_count_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 979 | }; |
| 980 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 981 | class ImageWriter::PruneClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 982 | public: |
| 983 | explicit PruneClassLoaderClassesVisitor(ImageWriter* image_writer) |
| 984 | : image_writer_(image_writer), removed_class_count_(0) {} |
| 985 | |
| 986 | virtual void Visit(ObjPtr<mirror::ClassLoader> class_loader) OVERRIDE |
| 987 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 988 | PruneClassesVisitor classes_visitor(image_writer_, class_loader); |
| 989 | ClassTable* class_table = |
| 990 | Runtime::Current()->GetClassLinker()->ClassTableForClassLoader(class_loader); |
| 991 | class_table->Visit(classes_visitor); |
| 992 | removed_class_count_ += classes_visitor.Prune(); |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 993 | |
| 994 | // Record app image class loader. The fake boot class loader should not get registered |
| 995 | // and we should end up with only one class loader for an app and none for boot image. |
| 996 | if (class_loader != nullptr && class_table != nullptr) { |
| 997 | DCHECK(class_loader_ == nullptr); |
| 998 | class_loader_ = class_loader; |
| 999 | } |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | size_t GetRemovedClassCount() const { |
| 1003 | return removed_class_count_; |
| 1004 | } |
| 1005 | |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1006 | ObjPtr<mirror::ClassLoader> GetClassLoader() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1007 | return class_loader_; |
| 1008 | } |
| 1009 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 1010 | private: |
| 1011 | ImageWriter* const image_writer_; |
| 1012 | size_t removed_class_count_; |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1013 | ObjPtr<mirror::ClassLoader> class_loader_; |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 1014 | }; |
| 1015 | |
| 1016 | void ImageWriter::VisitClassLoaders(ClassLoaderVisitor* visitor) { |
| 1017 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 1018 | visitor->Visit(nullptr); // Visit boot class loader. |
| 1019 | Runtime::Current()->GetClassLinker()->VisitClassLoaders(visitor); |
| 1020 | } |
| 1021 | |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1022 | void ImageWriter::PruneAndPreloadDexCache(ObjPtr<mirror::DexCache> dex_cache, |
| 1023 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 1024 | // To ensure deterministic contents of the hash-based arrays, each slot shall contain |
| 1025 | // the candidate with the lowest index. As we're processing entries in increasing index |
| 1026 | // order, this means trying to look up the entry for the current index if the slot is |
| 1027 | // empty or if it contains a higher index. |
| 1028 | |
| 1029 | Runtime* runtime = Runtime::Current(); |
| 1030 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1031 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 1032 | // Prune methods. |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1033 | mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods(); |
| 1034 | dex::TypeIndex last_class_idx; // Initialized to invalid index. |
| 1035 | ObjPtr<mirror::Class> last_class = nullptr; |
| 1036 | for (size_t i = 0, num = dex_cache->GetDexFile()->NumMethodIds(); i != num; ++i) { |
| 1037 | uint32_t slot_idx = dex_cache->MethodSlotIndex(i); |
| 1038 | auto pair = |
| 1039 | mirror::DexCache::GetNativePairPtrSize(resolved_methods, slot_idx, target_ptr_size_); |
| 1040 | uint32_t stored_index = pair.index; |
| 1041 | ArtMethod* method = pair.object; |
| 1042 | if (method != nullptr && i > stored_index) { |
| 1043 | continue; // Already checked. |
| 1044 | } |
Vladimir Marko | 3b15545 | 2017-07-05 11:41:33 +0100 | [diff] [blame] | 1045 | // Check if the referenced class is in the image. Note that we want to check the referenced |
| 1046 | // class rather than the declaring class to preserve the semantics, i.e. using a MethodId |
| 1047 | // results in resolving the referenced class and that can for example throw OOME. |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1048 | const DexFile::MethodId& method_id = dex_file.GetMethodId(i); |
| 1049 | if (method_id.class_idx_ != last_class_idx) { |
| 1050 | last_class_idx = method_id.class_idx_; |
| 1051 | last_class = class_linker->LookupResolvedType( |
| 1052 | dex_file, last_class_idx, dex_cache, class_loader); |
| 1053 | if (last_class != nullptr && !KeepClass(last_class)) { |
| 1054 | last_class = nullptr; |
| 1055 | } |
| 1056 | } |
| 1057 | if (method == nullptr || i < stored_index) { |
| 1058 | if (last_class != nullptr) { |
| 1059 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 1060 | Signature signature = dex_file.GetMethodSignature(method_id); |
| 1061 | if (last_class->IsInterface()) { |
| 1062 | method = last_class->FindInterfaceMethod(name, signature, target_ptr_size_); |
| 1063 | } else { |
| 1064 | method = last_class->FindClassMethod(name, signature, target_ptr_size_); |
| 1065 | } |
| 1066 | if (method != nullptr) { |
| 1067 | // If the referenced class is in the image, the defining class must also be there. |
| 1068 | DCHECK(KeepClass(method->GetDeclaringClass())); |
| 1069 | dex_cache->SetResolvedMethod(i, method, target_ptr_size_); |
| 1070 | } |
| 1071 | } |
| 1072 | } else { |
| 1073 | DCHECK_EQ(i, stored_index); |
| 1074 | if (last_class == nullptr) { |
| 1075 | dex_cache->ClearResolvedMethod(stored_index, target_ptr_size_); |
| 1076 | } |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | // Prune fields and make the contents of the field array deterministic. |
| 1080 | mirror::FieldDexCacheType* resolved_fields = dex_cache->GetResolvedFields(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1081 | last_class_idx = dex::TypeIndex(); // Initialized to invalid index. |
| 1082 | last_class = nullptr; |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1083 | for (size_t i = 0, end = dex_file.NumFieldIds(); i < end; ++i) { |
| 1084 | uint32_t slot_idx = dex_cache->FieldSlotIndex(i); |
| 1085 | auto pair = mirror::DexCache::GetNativePairPtrSize(resolved_fields, slot_idx, target_ptr_size_); |
| 1086 | uint32_t stored_index = pair.index; |
| 1087 | ArtField* field = pair.object; |
| 1088 | if (field != nullptr && i > stored_index) { |
| 1089 | continue; // Already checked. |
| 1090 | } |
| 1091 | // Check if the referenced class is in the image. Note that we want to check the referenced |
| 1092 | // class rather than the declaring class to preserve the semantics, i.e. using a FieldId |
| 1093 | // results in resolving the referenced class and that can for example throw OOME. |
| 1094 | const DexFile::FieldId& field_id = dex_file.GetFieldId(i); |
| 1095 | if (field_id.class_idx_ != last_class_idx) { |
| 1096 | last_class_idx = field_id.class_idx_; |
| 1097 | last_class = class_linker->LookupResolvedType( |
| 1098 | dex_file, last_class_idx, dex_cache, class_loader); |
| 1099 | if (last_class != nullptr && !KeepClass(last_class)) { |
| 1100 | last_class = nullptr; |
| 1101 | } |
| 1102 | } |
| 1103 | if (field == nullptr || i < stored_index) { |
| 1104 | if (last_class != nullptr) { |
| 1105 | const char* name = dex_file.StringDataByIdx(field_id.name_idx_); |
| 1106 | const char* type = dex_file.StringByTypeIdx(field_id.type_idx_); |
| 1107 | field = mirror::Class::FindField(Thread::Current(), last_class, name, type); |
| 1108 | if (field != nullptr) { |
| 1109 | // If the referenced class is in the image, the defining class must also be there. |
| 1110 | DCHECK(KeepClass(field->GetDeclaringClass())); |
| 1111 | dex_cache->SetResolvedField(i, field, target_ptr_size_); |
| 1112 | } |
| 1113 | } |
| 1114 | } else { |
| 1115 | DCHECK_EQ(i, stored_index); |
| 1116 | if (last_class == nullptr) { |
| 1117 | dex_cache->ClearResolvedField(stored_index, target_ptr_size_); |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | // Prune types and make the contents of the type array deterministic. |
| 1122 | // This is done after fields and methods as their lookup can touch the types array. |
| 1123 | for (size_t i = 0, end = dex_cache->GetDexFile()->NumTypeIds(); i < end; ++i) { |
| 1124 | dex::TypeIndex type_idx(i); |
| 1125 | uint32_t slot_idx = dex_cache->TypeSlotIndex(type_idx); |
| 1126 | mirror::TypeDexCachePair pair = |
| 1127 | dex_cache->GetResolvedTypes()[slot_idx].load(std::memory_order_relaxed); |
| 1128 | uint32_t stored_index = pair.index; |
| 1129 | ObjPtr<mirror::Class> klass = pair.object.Read(); |
| 1130 | if (klass == nullptr || i < stored_index) { |
| 1131 | klass = class_linker->LookupResolvedType(dex_file, type_idx, dex_cache, class_loader); |
| 1132 | if (klass != nullptr) { |
| 1133 | DCHECK_EQ(dex_cache->GetResolvedType(type_idx), klass); |
| 1134 | stored_index = i; // For correct clearing below if not keeping the `klass`. |
| 1135 | } |
| 1136 | } else if (i == stored_index && !KeepClass(klass)) { |
| 1137 | dex_cache->ClearResolvedType(dex::TypeIndex(stored_index)); |
| 1138 | } |
| 1139 | } |
| 1140 | // Strings do not need pruning, but the contents of the string array must be deterministic. |
| 1141 | for (size_t i = 0, end = dex_cache->GetDexFile()->NumStringIds(); i < end; ++i) { |
| 1142 | dex::StringIndex string_idx(i); |
| 1143 | uint32_t slot_idx = dex_cache->StringSlotIndex(string_idx); |
| 1144 | mirror::StringDexCachePair pair = |
| 1145 | dex_cache->GetStrings()[slot_idx].load(std::memory_order_relaxed); |
| 1146 | uint32_t stored_index = pair.index; |
| 1147 | ObjPtr<mirror::String> string = pair.object.Read(); |
| 1148 | if (string == nullptr || i < stored_index) { |
| 1149 | string = class_linker->LookupString(dex_file, string_idx, dex_cache); |
| 1150 | DCHECK(string == nullptr || dex_cache->GetResolvedString(string_idx) == string); |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1155 | void ImageWriter::PruneNonImageClasses() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1156 | Runtime* runtime = Runtime::Current(); |
| 1157 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1158 | Thread* self = Thread::Current(); |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1159 | ScopedAssertNoThreadSuspension sa(__FUNCTION__); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1160 | |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 1161 | // Prune uses-library dex caches. Only prune the uses-library dex caches since we want to make |
| 1162 | // sure the other ones don't get unloaded before the OatWriter runs. |
| 1163 | class_linker->VisitClassTables( |
| 1164 | [&](ClassTable* table) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1165 | table->RemoveStrongRoots( |
| 1166 | [&](GcRoot<mirror::Object> root) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1167 | ObjPtr<mirror::Object> obj = root.Read(); |
| 1168 | if (obj->IsDexCache()) { |
| 1169 | // Return true if the dex file is not one of the ones in the map. |
| 1170 | return dex_file_oat_index_map_.find(obj->AsDexCache()->GetDexFile()) == |
| 1171 | dex_file_oat_index_map_.end(); |
| 1172 | } |
| 1173 | // Return false to avoid removing. |
| 1174 | return false; |
| 1175 | }); |
| 1176 | }); |
Mathieu Chartier | 696632e | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 1177 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1178 | // Remove the undesired classes from the class roots. |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1179 | ObjPtr<mirror::ClassLoader> class_loader; |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 1180 | { |
| 1181 | PruneClassLoaderClassesVisitor class_loader_visitor(this); |
| 1182 | VisitClassLoaders(&class_loader_visitor); |
| 1183 | VLOG(compiler) << "Pruned " << class_loader_visitor.GetRemovedClassCount() << " classes"; |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1184 | class_loader = class_loader_visitor.GetClassLoader(); |
| 1185 | DCHECK_EQ(class_loader != nullptr, compile_app_image_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | // Clear references to removed classes from the DexCaches. |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1189 | std::vector<ObjPtr<mirror::DexCache>> dex_caches; |
| 1190 | { |
| 1191 | ReaderMutexLock mu2(self, *Locks::dex_lock_); |
| 1192 | dex_caches.reserve(class_linker->GetDexCachesData().size()); |
| 1193 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 1194 | if (self->IsJWeakCleared(data.weak_root)) { |
| 1195 | continue; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1196 | } |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1197 | dex_caches.push_back(self->DecodeJObject(data.weak_root)->AsDexCache()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1198 | } |
Vladimir Marko | f25cc73 | 2017-03-16 16:18:15 +0000 | [diff] [blame] | 1199 | } |
| 1200 | for (ObjPtr<mirror::DexCache> dex_cache : dex_caches) { |
| 1201 | PruneAndPreloadDexCache(dex_cache, class_loader); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1202 | } |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 1203 | |
| 1204 | // Drop the array class cache in the ClassLinker, as these are roots holding those classes live. |
| 1205 | class_linker->DropFindArrayClassCache(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1206 | |
| 1207 | // Clear to save RAM. |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 1208 | prune_class_memo_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 1211 | void ImageWriter::CheckNonImageClassesRemoved() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1212 | if (compiler_driver_.GetImageClasses() != nullptr) { |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1213 | auto visitor = [&](Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1214 | if (obj->IsClass() && !IsInBootImage(obj)) { |
| 1215 | Class* klass = obj->AsClass(); |
| 1216 | if (!KeepClass(klass)) { |
| 1217 | DumpImageClasses(); |
| 1218 | std::string temp; |
| 1219 | CHECK(KeepClass(klass)) |
| 1220 | << Runtime::Current()->GetHeap()->GetVerification()->FirstPathFromRootSet(klass); |
| 1221 | } |
| 1222 | } |
| 1223 | }; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1224 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1225 | heap->VisitObjects(visitor); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | void ImageWriter::DumpImageClasses() { |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 1230 | auto image_classes = compiler_driver_.GetImageClasses(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1231 | CHECK(image_classes != nullptr); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 1232 | for (const std::string& image_class : *image_classes) { |
| 1233 | LOG(INFO) << " " << image_class; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1237 | mirror::String* ImageWriter::FindInternedString(mirror::String* string) { |
| 1238 | Thread* const self = Thread::Current(); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1239 | for (const ImageInfo& image_info : image_infos_) { |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 1240 | ObjPtr<mirror::String> const found = image_info.intern_table_->LookupStrong(self, string); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1241 | DCHECK(image_info.intern_table_->LookupWeak(self, string) == nullptr) |
| 1242 | << string->ToModifiedUtf8(); |
| 1243 | if (found != nullptr) { |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 1244 | return found.Ptr(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1245 | } |
| 1246 | } |
| 1247 | if (compile_app_image_) { |
| 1248 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 1249 | ObjPtr<mirror::String> found = runtime->GetInternTable()->LookupStrong(self, string); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1250 | // If we found it in the runtime intern table it could either be in the boot image or interned |
| 1251 | // during app image compilation. If it was in the boot image return that, otherwise return null |
| 1252 | // since it belongs to another image space. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 1253 | if (found != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(found.Ptr())) { |
| 1254 | return found.Ptr(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1255 | } |
| 1256 | DCHECK(runtime->GetInternTable()->LookupWeak(self, string) == nullptr) |
| 1257 | << string->ToModifiedUtf8(); |
| 1258 | } |
| 1259 | return nullptr; |
| 1260 | } |
| 1261 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1262 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1263 | ObjectArray<Object>* ImageWriter::CreateImageRoots(size_t oat_index) const { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1264 | Runtime* runtime = Runtime::Current(); |
| 1265 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1266 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1267 | StackHandleScope<3> hs(self); |
| 1268 | Handle<Class> object_array_class(hs.NewHandle( |
| 1269 | class_linker->FindSystemClass(self, "[Ljava/lang/Object;"))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1270 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1271 | std::unordered_set<const DexFile*> image_dex_files; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1272 | for (auto& pair : dex_file_oat_index_map_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1273 | const DexFile* image_dex_file = pair.first; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1274 | size_t image_oat_index = pair.second; |
| 1275 | if (oat_index == image_oat_index) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1276 | image_dex_files.insert(image_dex_file); |
| 1277 | } |
| 1278 | } |
| 1279 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 1280 | // build an Object[] of all the DexCaches used in the source_space_. |
| 1281 | // Since we can't hold the dex lock when allocating the dex_caches |
| 1282 | // ObjectArray, we lock the dex lock twice, first to get the number |
| 1283 | // of dex caches first and then lock it again to copy the dex |
| 1284 | // caches. We check that the number of dex caches does not change. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1285 | size_t dex_cache_count = 0; |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 1286 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1287 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1288 | // Count number of dex caches not in the boot image. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 1289 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 1290 | ObjPtr<mirror::DexCache> dex_cache = |
| 1291 | ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)); |
Brian Carlstrom | 0c050a1 | 2016-04-29 10:28:34 -0700 | [diff] [blame] | 1292 | if (dex_cache == nullptr) { |
| 1293 | continue; |
| 1294 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1295 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 1296 | if (!IsInBootImage(dex_cache.Ptr())) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1297 | dex_cache_count += image_dex_files.find(dex_file) != image_dex_files.end() ? 1u : 0u; |
| 1298 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1299 | } |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 1300 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1301 | Handle<ObjectArray<Object>> dex_caches( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1302 | hs.NewHandle(ObjectArray<Object>::Alloc(self, object_array_class.Get(), dex_cache_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1303 | CHECK(dex_caches != nullptr) << "Failed to allocate a dex cache array."; |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 1304 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1305 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1306 | size_t non_image_dex_caches = 0; |
| 1307 | // Re-count number of non image dex caches. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 1308 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 1309 | ObjPtr<mirror::DexCache> dex_cache = |
| 1310 | ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)); |
Brian Carlstrom | 0c050a1 | 2016-04-29 10:28:34 -0700 | [diff] [blame] | 1311 | if (dex_cache == nullptr) { |
| 1312 | continue; |
| 1313 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1314 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 1315 | if (!IsInBootImage(dex_cache.Ptr())) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1316 | non_image_dex_caches += image_dex_files.find(dex_file) != image_dex_files.end() ? 1u : 0u; |
| 1317 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1318 | } |
| 1319 | CHECK_EQ(dex_cache_count, non_image_dex_caches) |
| 1320 | << "The number of non-image dex caches changed."; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1321 | size_t i = 0; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 1322 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 1323 | ObjPtr<mirror::DexCache> dex_cache = |
| 1324 | ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)); |
Brian Carlstrom | 0c050a1 | 2016-04-29 10:28:34 -0700 | [diff] [blame] | 1325 | if (dex_cache == nullptr) { |
| 1326 | continue; |
| 1327 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1328 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 1329 | if (!IsInBootImage(dex_cache.Ptr()) && |
| 1330 | image_dex_files.find(dex_file) != image_dex_files.end()) { |
| 1331 | dex_caches->Set<false>(i, dex_cache.Ptr()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1332 | ++i; |
| 1333 | } |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 1334 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | // build an Object[] of the roots needed to restore the runtime |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1338 | int32_t image_roots_size = ImageHeader::NumberOfImageRoots(compile_app_image_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1339 | auto image_roots(hs.NewHandle( |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1340 | ObjectArray<Object>::Alloc(self, object_array_class.Get(), image_roots_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1341 | image_roots->Set<false>(ImageHeader::kDexCaches, dex_caches.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1342 | image_roots->Set<false>(ImageHeader::kClassRoots, class_linker->GetClassRoots()); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1343 | // image_roots[ImageHeader::kClassLoader] will be set later for app image. |
| 1344 | static_assert(ImageHeader::kClassLoader + 1u == ImageHeader::kImageRootsMax, |
| 1345 | "Class loader should be the last image root."); |
| 1346 | for (int32_t i = 0; i < ImageHeader::kImageRootsMax - 1; ++i) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1347 | CHECK(image_roots->Get(i) != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1348 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1349 | return image_roots.Get(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1352 | mirror::Object* ImageWriter::TryAssignBinSlot(WorkStack& work_stack, |
| 1353 | mirror::Object* obj, |
| 1354 | size_t oat_index) { |
| 1355 | if (obj == nullptr || IsInBootImage(obj)) { |
| 1356 | // Object is null or already in the image, there is no work to do. |
| 1357 | return obj; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1358 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1359 | if (!IsImageBinSlotAssigned(obj)) { |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1360 | // We want to intern all strings but also assign offsets for the source string. Since the |
| 1361 | // pruning phase has already happened, if we intern a string to one in the image we still |
| 1362 | // end up copying an unreachable string. |
| 1363 | if (obj->IsString()) { |
| 1364 | // Need to check if the string is already interned in another image info so that we don't have |
| 1365 | // the intern tables of two different images contain the same string. |
| 1366 | mirror::String* interned = FindInternedString(obj->AsString()); |
| 1367 | if (interned == nullptr) { |
| 1368 | // Not in another image space, insert to our table. |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 1369 | interned = |
| 1370 | GetImageInfo(oat_index).intern_table_->InternStrongImageString(obj->AsString()).Ptr(); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1371 | DCHECK_EQ(interned, obj); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1372 | } |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1373 | } else if (obj->IsDexCache()) { |
| 1374 | oat_index = GetOatIndexForDexCache(obj->AsDexCache()); |
| 1375 | } else if (obj->IsClass()) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1376 | // Visit and assign offsets for fields and field arrays. |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1377 | mirror::Class* as_klass = obj->AsClass(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1378 | mirror::DexCache* dex_cache = as_klass->GetDexCache(); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1379 | DCHECK(!as_klass->IsErroneous()) << as_klass->GetStatus(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1380 | if (compile_app_image_) { |
| 1381 | // Extra sanity, no boot loader classes should be left! |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 1382 | CHECK(!IsBootClassLoaderClass(as_klass)) << as_klass->PrettyClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1383 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1384 | LengthPrefixedArray<ArtField>* fields[] = { |
| 1385 | as_klass->GetSFieldsPtr(), as_klass->GetIFieldsPtr(), |
| 1386 | }; |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1387 | // Overwrite the oat index value since the class' dex cache is more accurate of where it |
| 1388 | // belongs. |
| 1389 | oat_index = GetOatIndexForDexCache(dex_cache); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1390 | ImageInfo& image_info = GetImageInfo(oat_index); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1391 | if (!compile_app_image_) { |
| 1392 | // Note: Avoid locking to prevent lock order violations from root visiting; |
| 1393 | // image_info.class_table_ is only accessed from the image writer. |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1394 | image_info.class_table_->InsertWithoutLocks(as_klass); |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 1395 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1396 | for (LengthPrefixedArray<ArtField>* cur_fields : fields) { |
| 1397 | // Total array length including header. |
| 1398 | if (cur_fields != nullptr) { |
| 1399 | const size_t header_size = LengthPrefixedArray<ArtField>::ComputeSize(0); |
| 1400 | // Forward the entire array at once. |
| 1401 | auto it = native_object_relocations_.find(cur_fields); |
| 1402 | CHECK(it == native_object_relocations_.end()) << "Field array " << cur_fields |
| 1403 | << " already forwarded"; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1404 | size_t& offset = image_info.bin_slot_sizes_[kBinArtField]; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1405 | DCHECK(!IsInBootImage(cur_fields)); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1406 | native_object_relocations_.emplace( |
| 1407 | cur_fields, |
| 1408 | NativeObjectRelocation { |
| 1409 | oat_index, offset, kNativeObjectRelocationTypeArtFieldArray |
| 1410 | }); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1411 | offset += header_size; |
| 1412 | // Forward individual fields so that we can quickly find where they belong. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1413 | for (size_t i = 0, count = cur_fields->size(); i < count; ++i) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1414 | // Need to forward arrays separate of fields. |
| 1415 | ArtField* field = &cur_fields->At(i); |
| 1416 | auto it2 = native_object_relocations_.find(field); |
| 1417 | CHECK(it2 == native_object_relocations_.end()) << "Field at index=" << i |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1418 | << " already assigned " << field->PrettyField() << " static=" << field->IsStatic(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1419 | DCHECK(!IsInBootImage(field)); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1420 | native_object_relocations_.emplace( |
| 1421 | field, |
| 1422 | NativeObjectRelocation { oat_index, offset, kNativeObjectRelocationTypeArtField }); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1423 | offset += sizeof(ArtField); |
| 1424 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1425 | } |
| 1426 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1427 | // Visit and assign offsets for methods. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1428 | size_t num_methods = as_klass->NumMethods(); |
| 1429 | if (num_methods != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1430 | bool any_dirty = false; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1431 | for (auto& m : as_klass->GetMethods(target_ptr_size_)) { |
| 1432 | if (WillMethodBeDirty(&m)) { |
| 1433 | any_dirty = true; |
| 1434 | break; |
| 1435 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1436 | } |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1437 | NativeObjectRelocationType type = any_dirty |
| 1438 | ? kNativeObjectRelocationTypeArtMethodDirty |
| 1439 | : kNativeObjectRelocationTypeArtMethodClean; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1440 | Bin bin_type = BinTypeForNativeRelocationType(type); |
| 1441 | // Forward the entire array at once, but header first. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1442 | const size_t method_alignment = ArtMethod::Alignment(target_ptr_size_); |
| 1443 | const size_t method_size = ArtMethod::Size(target_ptr_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1444 | const size_t header_size = LengthPrefixedArray<ArtMethod>::ComputeSize(0, |
| 1445 | method_size, |
| 1446 | method_alignment); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1447 | LengthPrefixedArray<ArtMethod>* array = as_klass->GetMethodsPtr(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1448 | auto it = native_object_relocations_.find(array); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1449 | CHECK(it == native_object_relocations_.end()) |
| 1450 | << "Method array " << array << " already forwarded"; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1451 | size_t& offset = image_info.bin_slot_sizes_[bin_type]; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1452 | DCHECK(!IsInBootImage(array)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1453 | native_object_relocations_.emplace(array, |
| 1454 | NativeObjectRelocation { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1455 | oat_index, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1456 | offset, |
| 1457 | any_dirty ? kNativeObjectRelocationTypeArtMethodArrayDirty |
| 1458 | : kNativeObjectRelocationTypeArtMethodArrayClean }); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1459 | offset += header_size; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1460 | for (auto& m : as_klass->GetMethods(target_ptr_size_)) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1461 | AssignMethodOffset(&m, type, oat_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1462 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1463 | (any_dirty ? dirty_methods_ : clean_methods_) += num_methods; |
Mathieu Chartier | 97bad1b | 2016-05-16 14:58:01 -0700 | [diff] [blame] | 1464 | } |
| 1465 | // Assign offsets for all runtime methods in the IMT since these may hold conflict tables |
| 1466 | // live. |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1467 | if (as_klass->ShouldHaveImt()) { |
| 1468 | ImTable* imt = as_klass->GetImt(target_ptr_size_); |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 1469 | if (TryAssignImTableOffset(imt, oat_index)) { |
| 1470 | // Since imt's can be shared only do this the first time to not double count imt method |
| 1471 | // fixups. |
| 1472 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 1473 | ArtMethod* imt_method = imt->Get(i, target_ptr_size_); |
| 1474 | DCHECK(imt_method != nullptr); |
| 1475 | if (imt_method->IsRuntimeMethod() && |
| 1476 | !IsInBootImage(imt_method) && |
| 1477 | !NativeRelocationAssigned(imt_method)) { |
| 1478 | AssignMethodOffset(imt_method, kNativeObjectRelocationTypeRuntimeMethod, oat_index); |
| 1479 | } |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1480 | } |
| 1481 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1482 | } |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1483 | } else if (obj->IsClassLoader()) { |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1484 | // Register the class loader if it has a class table. |
| 1485 | // The fake boot class loader should not get registered and we should end up with only one |
| 1486 | // class loader. |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1487 | mirror::ClassLoader* class_loader = obj->AsClassLoader(); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1488 | if (class_loader->GetClassTable() != nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1489 | DCHECK(compile_app_image_); |
| 1490 | DCHECK(class_loaders_.empty()); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1491 | class_loaders_.insert(class_loader); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1492 | ImageInfo& image_info = GetImageInfo(oat_index); |
| 1493 | // Note: Avoid locking to prevent lock order violations from root visiting; |
| 1494 | // image_info.class_table_ table is only accessed from the image writer |
| 1495 | // and class_loader->GetClassTable() is iterated but not modified. |
| 1496 | image_info.class_table_->CopyWithoutLocks(*class_loader->GetClassTable()); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1497 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1498 | } |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1499 | AssignImageBinSlot(obj, oat_index); |
| 1500 | work_stack.emplace(obj, oat_index); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1501 | } |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1502 | if (obj->IsString()) { |
| 1503 | // Always return the interned string if there exists one. |
| 1504 | mirror::String* interned = FindInternedString(obj->AsString()); |
| 1505 | if (interned != nullptr) { |
| 1506 | return interned; |
| 1507 | } |
| 1508 | } |
| 1509 | return obj; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1512 | bool ImageWriter::NativeRelocationAssigned(void* ptr) const { |
| 1513 | return native_object_relocations_.find(ptr) != native_object_relocations_.end(); |
| 1514 | } |
| 1515 | |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 1516 | bool ImageWriter::TryAssignImTableOffset(ImTable* imt, size_t oat_index) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1517 | // No offset, or already assigned. |
| 1518 | if (imt == nullptr || IsInBootImage(imt) || NativeRelocationAssigned(imt)) { |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 1519 | return false; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1520 | } |
| 1521 | // If the method is a conflict method we also want to assign the conflict table offset. |
| 1522 | ImageInfo& image_info = GetImageInfo(oat_index); |
| 1523 | const size_t size = ImTable::SizeInBytes(target_ptr_size_); |
| 1524 | native_object_relocations_.emplace( |
| 1525 | imt, |
| 1526 | NativeObjectRelocation { |
| 1527 | oat_index, |
| 1528 | image_info.bin_slot_sizes_[kBinImTable], |
| 1529 | kNativeObjectRelocationTypeIMTable}); |
| 1530 | image_info.bin_slot_sizes_[kBinImTable] += size; |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 1531 | return true; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1534 | void ImageWriter::TryAssignConflictTableOffset(ImtConflictTable* table, size_t oat_index) { |
| 1535 | // No offset, or already assigned. |
| 1536 | if (table == nullptr || NativeRelocationAssigned(table)) { |
| 1537 | return; |
| 1538 | } |
| 1539 | CHECK(!IsInBootImage(table)); |
| 1540 | // If the method is a conflict method we also want to assign the conflict table offset. |
| 1541 | ImageInfo& image_info = GetImageInfo(oat_index); |
| 1542 | const size_t size = table->ComputeSize(target_ptr_size_); |
| 1543 | native_object_relocations_.emplace( |
| 1544 | table, |
| 1545 | NativeObjectRelocation { |
| 1546 | oat_index, |
| 1547 | image_info.bin_slot_sizes_[kBinIMTConflictTable], |
| 1548 | kNativeObjectRelocationTypeIMTConflictTable}); |
| 1549 | image_info.bin_slot_sizes_[kBinIMTConflictTable] += size; |
| 1550 | } |
| 1551 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1552 | void ImageWriter::AssignMethodOffset(ArtMethod* method, |
| 1553 | NativeObjectRelocationType type, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1554 | size_t oat_index) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1555 | DCHECK(!IsInBootImage(method)); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1556 | CHECK(!NativeRelocationAssigned(method)) << "Method " << method << " already assigned " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1557 | << ArtMethod::PrettyMethod(method); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1558 | if (method->IsRuntimeMethod()) { |
| 1559 | TryAssignConflictTableOffset(method->GetImtConflictTable(target_ptr_size_), oat_index); |
| 1560 | } |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1561 | ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1562 | size_t& offset = image_info.bin_slot_sizes_[BinTypeForNativeRelocationType(type)]; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1563 | native_object_relocations_.emplace(method, NativeObjectRelocation { oat_index, offset, type }); |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1564 | offset += ArtMethod::Size(target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1567 | void ImageWriter::UnbinObjectsIntoOffset(mirror::Object* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1568 | DCHECK(!IsInBootImage(obj)); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1569 | CHECK(obj != nullptr); |
| 1570 | |
| 1571 | // We know the bin slot, and the total bin sizes for all objects by now, |
| 1572 | // so calculate the object's final image offset. |
| 1573 | |
| 1574 | DCHECK(IsImageBinSlotAssigned(obj)); |
| 1575 | BinSlot bin_slot = GetImageBinSlot(obj); |
| 1576 | // Change the lockword from a bin slot into an offset |
| 1577 | AssignImageOffset(obj, bin_slot); |
| 1578 | } |
| 1579 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1580 | class ImageWriter::VisitReferencesVisitor { |
| 1581 | public: |
| 1582 | VisitReferencesVisitor(ImageWriter* image_writer, WorkStack* work_stack, size_t oat_index) |
| 1583 | : image_writer_(image_writer), work_stack_(work_stack), oat_index_(oat_index) {} |
| 1584 | |
| 1585 | // Fix up separately since we also need to fix up method entrypoints. |
| 1586 | ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 1587 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1588 | if (!root->IsNull()) { |
| 1589 | VisitRoot(root); |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 1594 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1595 | root->Assign(VisitReference(root->AsMirrorPtr())); |
| 1596 | } |
| 1597 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1598 | ALWAYS_INLINE void operator() (ObjPtr<mirror::Object> obj, |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1599 | MemberOffset offset, |
| 1600 | bool is_static ATTRIBUTE_UNUSED) const |
| 1601 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1602 | mirror::Object* ref = |
| 1603 | obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset); |
| 1604 | obj->SetFieldObject</*kTransactionActive*/false>(offset, VisitReference(ref)); |
| 1605 | } |
| 1606 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 1607 | ALWAYS_INLINE void operator() (ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 1608 | ObjPtr<mirror::Reference> ref) const |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1609 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 1610 | operator()(ref, mirror::Reference::ReferentOffset(), /* is_static */ false); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | private: |
| 1614 | mirror::Object* VisitReference(mirror::Object* ref) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1615 | return image_writer_->TryAssignBinSlot(*work_stack_, ref, oat_index_); |
| 1616 | } |
| 1617 | |
| 1618 | ImageWriter* const image_writer_; |
| 1619 | WorkStack* const work_stack_; |
| 1620 | const size_t oat_index_; |
| 1621 | }; |
| 1622 | |
| 1623 | class ImageWriter::GetRootsVisitor : public RootVisitor { |
| 1624 | public: |
| 1625 | explicit GetRootsVisitor(std::vector<mirror::Object*>* roots) : roots_(roots) {} |
| 1626 | |
| 1627 | void VisitRoots(mirror::Object*** roots, |
| 1628 | size_t count, |
| 1629 | const RootInfo& info ATTRIBUTE_UNUSED) OVERRIDE |
| 1630 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1631 | for (size_t i = 0; i < count; ++i) { |
| 1632 | roots_->push_back(*roots[i]); |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, |
| 1637 | size_t count, |
| 1638 | const RootInfo& info ATTRIBUTE_UNUSED) OVERRIDE |
| 1639 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1640 | for (size_t i = 0; i < count; ++i) { |
| 1641 | roots_->push_back(roots[i]->AsMirrorPtr()); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | private: |
| 1646 | std::vector<mirror::Object*>* const roots_; |
| 1647 | }; |
| 1648 | |
| 1649 | void ImageWriter::ProcessWorkStack(WorkStack* work_stack) { |
| 1650 | while (!work_stack->empty()) { |
| 1651 | std::pair<mirror::Object*, size_t> pair(work_stack->top()); |
| 1652 | work_stack->pop(); |
| 1653 | VisitReferencesVisitor visitor(this, work_stack, /*oat_index*/ pair.second); |
| 1654 | // Walk references and assign bin slots for them. |
| 1655 | pair.first->VisitReferences</*kVisitNativeRoots*/true, kVerifyNone, kWithoutReadBarrier>( |
| 1656 | visitor, |
| 1657 | visitor); |
| 1658 | } |
| 1659 | } |
| 1660 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1661 | void ImageWriter::CalculateNewObjectOffsets() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1662 | Thread* const self = Thread::Current(); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 1663 | VariableSizedHandleScope handles(self); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1664 | std::vector<Handle<ObjectArray<Object>>> image_roots; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1665 | for (size_t i = 0, size = oat_filenames_.size(); i != size; ++i) { |
| 1666 | image_roots.push_back(handles.NewHandle(CreateImageRoots(i))); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1667 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1668 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1669 | Runtime* const runtime = Runtime::Current(); |
| 1670 | gc::Heap* const heap = runtime->GetHeap(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1671 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1672 | // Leave space for the header, but do not write it yet, we need to |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1673 | // know where image_roots is going to end up |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1674 | image_objects_offset_begin_ = RoundUp(sizeof(ImageHeader), kObjectAlignment); // 64-bit-alignment |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1675 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1676 | const size_t method_alignment = ArtMethod::Alignment(target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1677 | // Write the image runtime methods. |
| 1678 | image_methods_[ImageHeader::kResolutionMethod] = runtime->GetResolutionMethod(); |
| 1679 | image_methods_[ImageHeader::kImtConflictMethod] = runtime->GetImtConflictMethod(); |
| 1680 | image_methods_[ImageHeader::kImtUnimplementedMethod] = runtime->GetImtUnimplementedMethod(); |
Vladimir Marko | fd36f1f | 2016-08-03 18:49:58 +0100 | [diff] [blame] | 1681 | image_methods_[ImageHeader::kSaveAllCalleeSavesMethod] = |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 1682 | runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves); |
Vladimir Marko | fd36f1f | 2016-08-03 18:49:58 +0100 | [diff] [blame] | 1683 | image_methods_[ImageHeader::kSaveRefsOnlyMethod] = |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 1684 | runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly); |
Vladimir Marko | fd36f1f | 2016-08-03 18:49:58 +0100 | [diff] [blame] | 1685 | image_methods_[ImageHeader::kSaveRefsAndArgsMethod] = |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 1686 | runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs); |
Vladimir Marko | 952dbb1 | 2016-07-28 12:01:51 +0100 | [diff] [blame] | 1687 | image_methods_[ImageHeader::kSaveEverythingMethod] = |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 1688 | runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything); |
Mingyao Yang | 0a87a65 | 2017-04-12 13:43:15 -0700 | [diff] [blame] | 1689 | image_methods_[ImageHeader::kSaveEverythingMethodForClinit] = |
| 1690 | runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForClinit); |
| 1691 | image_methods_[ImageHeader::kSaveEverythingMethodForSuspendCheck] = |
| 1692 | runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForSuspendCheck); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1693 | // Visit image methods first to have the main runtime methods in the first image. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1694 | for (auto* m : image_methods_) { |
| 1695 | CHECK(m != nullptr); |
| 1696 | CHECK(m->IsRuntimeMethod()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1697 | DCHECK_EQ(compile_app_image_, IsInBootImage(m)) << "Trampolines should be in boot image"; |
| 1698 | if (!IsInBootImage(m)) { |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1699 | AssignMethodOffset(m, kNativeObjectRelocationTypeRuntimeMethod, GetDefaultOatIndex()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1700 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1701 | } |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1702 | |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1703 | // Deflate monitors before we visit roots since deflating acquires the monitor lock. Acquiring |
| 1704 | // this lock while holding other locks may cause lock order violations. |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1705 | { |
| 1706 | auto deflate_monitor = [](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1707 | Monitor::Deflate(Thread::Current(), obj); |
| 1708 | }; |
| 1709 | heap->VisitObjects(deflate_monitor); |
| 1710 | } |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1711 | |
| 1712 | // Work list of <object, oat_index> for objects. Everything on the stack must already be |
| 1713 | // assigned a bin slot. |
| 1714 | WorkStack work_stack; |
| 1715 | |
| 1716 | // Special case interned strings to put them in the image they are likely to be resolved from. |
| 1717 | for (const DexFile* dex_file : compiler_driver_.GetDexFilesForOatFile()) { |
| 1718 | auto it = dex_file_oat_index_map_.find(dex_file); |
| 1719 | DCHECK(it != dex_file_oat_index_map_.end()) << dex_file->GetLocation(); |
| 1720 | const size_t oat_index = it->second; |
| 1721 | InternTable* const intern_table = runtime->GetInternTable(); |
| 1722 | for (size_t i = 0, count = dex_file->NumStringIds(); i < count; ++i) { |
| 1723 | uint32_t utf16_length; |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 1724 | const char* utf8_data = dex_file->StringDataAndUtf16LengthByIdx(dex::StringIndex(i), |
| 1725 | &utf16_length); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 1726 | mirror::String* string = intern_table->LookupStrong(self, utf16_length, utf8_data).Ptr(); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1727 | TryAssignBinSlot(work_stack, string, oat_index); |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | // Get the GC roots and then visit them separately to avoid lock violations since the root visitor |
| 1732 | // visits roots while holding various locks. |
| 1733 | { |
| 1734 | std::vector<mirror::Object*> roots; |
| 1735 | GetRootsVisitor root_visitor(&roots); |
| 1736 | runtime->VisitRoots(&root_visitor); |
| 1737 | for (mirror::Object* obj : roots) { |
| 1738 | TryAssignBinSlot(work_stack, obj, GetDefaultOatIndex()); |
| 1739 | } |
| 1740 | } |
| 1741 | ProcessWorkStack(&work_stack); |
| 1742 | |
| 1743 | // For app images, there may be objects that are only held live by the by the boot image. One |
| 1744 | // example is finalizer references. Forward these objects so that EnsureBinSlotAssignedCallback |
| 1745 | // does not fail any checks. TODO: We should probably avoid copying these objects. |
| 1746 | if (compile_app_image_) { |
| 1747 | for (gc::space::ImageSpace* space : heap->GetBootImageSpaces()) { |
| 1748 | DCHECK(space->IsImageSpace()); |
| 1749 | gc::accounting::ContinuousSpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 1750 | live_bitmap->VisitMarkedRange(reinterpret_cast<uintptr_t>(space->Begin()), |
| 1751 | reinterpret_cast<uintptr_t>(space->Limit()), |
| 1752 | [this, &work_stack](mirror::Object* obj) |
| 1753 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1754 | VisitReferencesVisitor visitor(this, &work_stack, GetDefaultOatIndex()); |
| 1755 | // Visit all references and try to assign bin slots for them (calls TryAssignBinSlot). |
| 1756 | obj->VisitReferences</*kVisitNativeRoots*/true, kVerifyNone, kWithoutReadBarrier>( |
| 1757 | visitor, |
| 1758 | visitor); |
| 1759 | }); |
| 1760 | } |
| 1761 | // Process the work stack in case anything was added by TryAssignBinSlot. |
| 1762 | ProcessWorkStack(&work_stack); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1763 | |
| 1764 | // Store the class loader in the class roots. |
| 1765 | CHECK_EQ(class_loaders_.size(), 1u); |
| 1766 | CHECK_EQ(image_roots.size(), 1u); |
| 1767 | CHECK(*class_loaders_.begin() != nullptr); |
| 1768 | image_roots[0]->Set<false>(ImageHeader::kClassLoader, *class_loaders_.begin()); |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | // Verify that all objects have assigned image bin slots. |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1772 | { |
| 1773 | auto ensure_bin_slots_assigned = [&](mirror::Object* obj) |
| 1774 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1775 | if (!Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(obj)) { |
| 1776 | CHECK(IsImageBinSlotAssigned(obj)) << mirror::Object::PrettyTypeOf(obj) << " " << obj; |
| 1777 | } |
| 1778 | }; |
| 1779 | heap->VisitObjects(ensure_bin_slots_assigned); |
| 1780 | } |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1781 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1782 | // Calculate size of the dex cache arrays slot and prepare offsets. |
| 1783 | PrepareDexCacheArraySlots(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1784 | |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 1785 | // Calculate the sizes of the intern tables, class tables, and fixup tables. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1786 | for (ImageInfo& image_info : image_infos_) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1787 | // Calculate how big the intern table will be after being serialized. |
| 1788 | InternTable* const intern_table = image_info.intern_table_.get(); |
| 1789 | CHECK_EQ(intern_table->WeakSize(), 0u) << " should have strong interned all the strings"; |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1790 | if (intern_table->StrongSize() != 0u) { |
| 1791 | image_info.intern_table_bytes_ = intern_table->WriteToMemory(nullptr); |
| 1792 | } |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 1793 | |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 1794 | // Calculate the size of the class table. |
| 1795 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1796 | DCHECK_EQ(image_info.class_table_->NumReferencedZygoteClasses(), 0u); |
| 1797 | if (image_info.class_table_->NumReferencedNonZygoteClasses() != 0u) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1798 | image_info.class_table_bytes_ += image_info.class_table_->WriteToMemory(nullptr); |
| 1799 | } |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1800 | } |
| 1801 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1802 | // Calculate bin slot offsets. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1803 | for (ImageInfo& image_info : image_infos_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1804 | size_t bin_offset = image_objects_offset_begin_; |
| 1805 | for (size_t i = 0; i != kBinSize; ++i) { |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1806 | switch (i) { |
| 1807 | case kBinArtMethodClean: |
| 1808 | case kBinArtMethodDirty: { |
| 1809 | bin_offset = RoundUp(bin_offset, method_alignment); |
| 1810 | break; |
| 1811 | } |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 1812 | case kBinDexCacheArray: |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1813 | bin_offset = RoundUp(bin_offset, DexCacheArraysLayout::Alignment(target_ptr_size_)); |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 1814 | break; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1815 | case kBinImTable: |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1816 | case kBinIMTConflictTable: { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1817 | bin_offset = RoundUp(bin_offset, static_cast<size_t>(target_ptr_size_)); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1818 | break; |
| 1819 | } |
| 1820 | default: { |
| 1821 | // Normal alignment. |
| 1822 | } |
| 1823 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1824 | image_info.bin_slot_offsets_[i] = bin_offset; |
| 1825 | bin_offset += image_info.bin_slot_sizes_[i]; |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1826 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1827 | // NOTE: There may be additional padding between the bin slots and the intern table. |
| 1828 | DCHECK_EQ(image_info.image_end_, |
| 1829 | GetBinSizeSum(image_info, kBinMirrorCount) + image_objects_offset_begin_); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1830 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1831 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1832 | // Calculate image offsets. |
| 1833 | size_t image_offset = 0; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1834 | for (ImageInfo& image_info : image_infos_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1835 | image_info.image_begin_ = global_image_begin_ + image_offset; |
| 1836 | image_info.image_offset_ = image_offset; |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1837 | ImageSection unused_sections[ImageHeader::kSectionCount]; |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1838 | image_info.image_size_ = RoundUp(image_info.CreateImageSections(unused_sections), kPageSize); |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1839 | // There should be no gaps until the next image. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1840 | image_offset += image_info.image_size_; |
| 1841 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1842 | |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 1843 | // Transform each object's bin slot into an offset which will be used to do the final copy. |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1844 | { |
| 1845 | auto unbin_objects_into_offset = [&](mirror::Object* obj) |
| 1846 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1847 | if (!IsInBootImage(obj)) { |
| 1848 | UnbinObjectsIntoOffset(obj); |
| 1849 | } |
| 1850 | }; |
| 1851 | heap->VisitObjects(unbin_objects_into_offset); |
| 1852 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1853 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1854 | size_t i = 0; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1855 | for (ImageInfo& image_info : image_infos_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1856 | image_info.image_roots_address_ = PointerToLowMemUInt32(GetImageAddress(image_roots[i].Get())); |
| 1857 | i++; |
| 1858 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1859 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1860 | // Update the native relocations by adding their bin sums. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1861 | for (auto& pair : native_object_relocations_) { |
| 1862 | NativeObjectRelocation& relocation = pair.second; |
| 1863 | Bin bin_type = BinTypeForNativeRelocationType(relocation.type); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1864 | ImageInfo& image_info = GetImageInfo(relocation.oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1865 | relocation.offset += image_info.bin_slot_offsets_[bin_type]; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1866 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1867 | } |
| 1868 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1869 | size_t ImageWriter::ImageInfo::CreateImageSections(ImageSection* out_sections) const { |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1870 | DCHECK(out_sections != nullptr); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1871 | |
| 1872 | // Do not round up any sections here that are represented by the bins since it will break |
| 1873 | // offsets. |
| 1874 | |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1875 | // Objects section |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1876 | ImageSection* objects_section = &out_sections[ImageHeader::kSectionObjects]; |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1877 | *objects_section = ImageSection(0u, image_end_); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1878 | |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1879 | // Add field section. |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1880 | ImageSection* field_section = &out_sections[ImageHeader::kSectionArtFields]; |
| 1881 | *field_section = ImageSection(bin_slot_offsets_[kBinArtField], bin_slot_sizes_[kBinArtField]); |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1882 | CHECK_EQ(bin_slot_offsets_[kBinArtField], field_section->Offset()); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1883 | |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1884 | // Add method section. |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1885 | ImageSection* methods_section = &out_sections[ImageHeader::kSectionArtMethods]; |
| 1886 | *methods_section = ImageSection( |
| 1887 | bin_slot_offsets_[kBinArtMethodClean], |
| 1888 | bin_slot_sizes_[kBinArtMethodClean] + bin_slot_sizes_[kBinArtMethodDirty]); |
| 1889 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1890 | // IMT section. |
| 1891 | ImageSection* imt_section = &out_sections[ImageHeader::kSectionImTables]; |
| 1892 | *imt_section = ImageSection(bin_slot_offsets_[kBinImTable], bin_slot_sizes_[kBinImTable]); |
| 1893 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1894 | // Conflict tables section. |
| 1895 | ImageSection* imt_conflict_tables_section = &out_sections[ImageHeader::kSectionIMTConflictTables]; |
| 1896 | *imt_conflict_tables_section = ImageSection(bin_slot_offsets_[kBinIMTConflictTable], |
| 1897 | bin_slot_sizes_[kBinIMTConflictTable]); |
| 1898 | |
| 1899 | // Runtime methods section. |
| 1900 | ImageSection* runtime_methods_section = &out_sections[ImageHeader::kSectionRuntimeMethods]; |
| 1901 | *runtime_methods_section = ImageSection(bin_slot_offsets_[kBinRuntimeMethod], |
| 1902 | bin_slot_sizes_[kBinRuntimeMethod]); |
| 1903 | |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1904 | // Add dex cache arrays section. |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1905 | ImageSection* dex_cache_arrays_section = &out_sections[ImageHeader::kSectionDexCacheArrays]; |
| 1906 | *dex_cache_arrays_section = ImageSection(bin_slot_offsets_[kBinDexCacheArray], |
| 1907 | bin_slot_sizes_[kBinDexCacheArray]); |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1908 | // Round up to the alignment the string table expects. See HashSet::WriteToMemory. |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1909 | size_t cur_pos = RoundUp(dex_cache_arrays_section->End(), sizeof(uint64_t)); |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1910 | // Calculate the size of the interned strings. |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1911 | ImageSection* interned_strings_section = &out_sections[ImageHeader::kSectionInternedStrings]; |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1912 | *interned_strings_section = ImageSection(cur_pos, intern_table_bytes_); |
| 1913 | cur_pos = interned_strings_section->End(); |
| 1914 | // Round up to the alignment the class table expects. See HashSet::WriteToMemory. |
| 1915 | cur_pos = RoundUp(cur_pos, sizeof(uint64_t)); |
| 1916 | // Calculate the size of the class table section. |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1917 | ImageSection* class_table_section = &out_sections[ImageHeader::kSectionClassTable]; |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 1918 | *class_table_section = ImageSection(cur_pos, class_table_bytes_); |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1919 | cur_pos = class_table_section->End(); |
| 1920 | // Image end goes right before the start of the image bitmap. |
| 1921 | return cur_pos; |
| 1922 | } |
| 1923 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1924 | void ImageWriter::CreateHeader(size_t oat_index) { |
| 1925 | ImageInfo& image_info = GetImageInfo(oat_index); |
| 1926 | const uint8_t* oat_file_begin = image_info.oat_file_begin_; |
| 1927 | const uint8_t* oat_file_end = oat_file_begin + image_info.oat_loaded_size_; |
| 1928 | const uint8_t* oat_data_end = image_info.oat_data_begin_ + image_info.oat_size_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1929 | |
| 1930 | // Create the image sections. |
| 1931 | ImageSection sections[ImageHeader::kSectionCount]; |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 1932 | const size_t image_end = image_info.CreateImageSections(sections); |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1933 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1934 | // Finally bitmap section. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1935 | const size_t bitmap_bytes = image_info.image_bitmap_->Size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1936 | auto* bitmap_section = §ions[ImageHeader::kSectionImageBitmap]; |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1937 | *bitmap_section = ImageSection(RoundUp(image_end, kPageSize), RoundUp(bitmap_bytes, kPageSize)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1938 | if (VLOG_IS_ON(compiler)) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1939 | LOG(INFO) << "Creating header for " << oat_filenames_[oat_index]; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1940 | size_t idx = 0; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1941 | for (const ImageSection& section : sections) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1942 | LOG(INFO) << static_cast<ImageHeader::ImageSections>(idx) << " " << section; |
| 1943 | ++idx; |
| 1944 | } |
| 1945 | LOG(INFO) << "Methods: clean=" << clean_methods_ << " dirty=" << dirty_methods_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1946 | LOG(INFO) << "Image roots address=" << std::hex << image_info.image_roots_address_ << std::dec; |
| 1947 | LOG(INFO) << "Image begin=" << std::hex << reinterpret_cast<uintptr_t>(global_image_begin_) |
| 1948 | << " Image offset=" << image_info.image_offset_ << std::dec; |
| 1949 | LOG(INFO) << "Oat file begin=" << std::hex << reinterpret_cast<uintptr_t>(oat_file_begin) |
| 1950 | << " Oat data begin=" << reinterpret_cast<uintptr_t>(image_info.oat_data_begin_) |
| 1951 | << " Oat data end=" << reinterpret_cast<uintptr_t>(oat_data_end) |
| 1952 | << " Oat file end=" << reinterpret_cast<uintptr_t>(oat_file_end); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1953 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1954 | // Store boot image info for app image so that we can relocate. |
| 1955 | uint32_t boot_image_begin = 0; |
| 1956 | uint32_t boot_image_end = 0; |
| 1957 | uint32_t boot_oat_begin = 0; |
| 1958 | uint32_t boot_oat_end = 0; |
| 1959 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 1960 | heap->GetBootImagesSize(&boot_image_begin, &boot_image_end, &boot_oat_begin, &boot_oat_end); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1961 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1962 | // Create the header, leave 0 for data size since we will fill this in as we are writing the |
| 1963 | // image. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1964 | new (image_info.image_->Begin()) ImageHeader(PointerToLowMemUInt32(image_info.image_begin_), |
| 1965 | image_end, |
| 1966 | sections, |
| 1967 | image_info.image_roots_address_, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1968 | image_info.oat_checksum_, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1969 | PointerToLowMemUInt32(oat_file_begin), |
| 1970 | PointerToLowMemUInt32(image_info.oat_data_begin_), |
| 1971 | PointerToLowMemUInt32(oat_data_end), |
| 1972 | PointerToLowMemUInt32(oat_file_end), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1973 | boot_image_begin, |
| 1974 | boot_image_end - boot_image_begin, |
| 1975 | boot_oat_begin, |
| 1976 | boot_oat_end - boot_oat_begin, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1977 | static_cast<uint32_t>(target_ptr_size_), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1978 | compile_pic_, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1979 | /*is_pic*/compile_app_image_, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1980 | image_storage_mode_, |
| 1981 | /*data_size*/0u); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | ArtMethod* ImageWriter::GetImageMethodAddress(ArtMethod* method) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1985 | auto it = native_object_relocations_.find(method); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1986 | CHECK(it != native_object_relocations_.end()) << ArtMethod::PrettyMethod(method) << " @ " |
| 1987 | << method; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 1988 | size_t oat_index = GetOatIndex(method->GetDexCache()); |
| 1989 | ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1990 | CHECK_GE(it->second.offset, image_info.image_end_) << "ArtMethods should be after Objects"; |
| 1991 | return reinterpret_cast<ArtMethod*>(image_info.image_begin_ + it->second.offset); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1992 | } |
| 1993 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 1994 | class ImageWriter::FixupRootVisitor : public RootVisitor { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1995 | public: |
| 1996 | explicit FixupRootVisitor(ImageWriter* image_writer) : image_writer_(image_writer) { |
| 1997 | } |
| 1998 | |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 1999 | void VisitRoots(mirror::Object*** roots ATTRIBUTE_UNUSED, |
| 2000 | size_t count ATTRIBUTE_UNUSED, |
| 2001 | const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2002 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2003 | LOG(FATAL) << "Unsupported"; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 2007 | const RootInfo& info ATTRIBUTE_UNUSED) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2008 | OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2009 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2010 | image_writer_->CopyReference(roots[i], roots[i]->AsMirrorPtr()); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | private: |
| 2015 | ImageWriter* const image_writer_; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2016 | }; |
| 2017 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 2018 | void ImageWriter::CopyAndFixupImTable(ImTable* orig, ImTable* copy) { |
| 2019 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 2020 | ArtMethod* method = orig->Get(i, target_ptr_size_); |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2021 | void** address = reinterpret_cast<void**>(copy->AddressOfElement(i, target_ptr_size_)); |
| 2022 | CopyAndFixupPointer(address, method); |
| 2023 | DCHECK_EQ(copy->Get(i, target_ptr_size_), NativeLocationInImage(method)); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 2024 | } |
| 2025 | } |
| 2026 | |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2027 | void ImageWriter::CopyAndFixupImtConflictTable(ImtConflictTable* orig, ImtConflictTable* copy) { |
| 2028 | const size_t count = orig->NumEntries(target_ptr_size_); |
| 2029 | for (size_t i = 0; i < count; ++i) { |
| 2030 | ArtMethod* interface_method = orig->GetInterfaceMethod(i, target_ptr_size_); |
| 2031 | ArtMethod* implementation_method = orig->GetImplementationMethod(i, target_ptr_size_); |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2032 | CopyAndFixupPointer(copy->AddressOfInterfaceMethod(i, target_ptr_size_), interface_method); |
| 2033 | CopyAndFixupPointer(copy->AddressOfImplementationMethod(i, target_ptr_size_), |
| 2034 | implementation_method); |
| 2035 | DCHECK_EQ(copy->GetInterfaceMethod(i, target_ptr_size_), |
| 2036 | NativeLocationInImage(interface_method)); |
| 2037 | DCHECK_EQ(copy->GetImplementationMethod(i, target_ptr_size_), |
| 2038 | NativeLocationInImage(implementation_method)); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2039 | } |
| 2040 | } |
| 2041 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2042 | void ImageWriter::CopyAndFixupNativeData(size_t oat_index) { |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2043 | const ImageInfo& image_info = GetImageInfo(oat_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2044 | // Copy ArtFields and methods to their locations and update the array for convenience. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2045 | for (auto& pair : native_object_relocations_) { |
| 2046 | NativeObjectRelocation& relocation = pair.second; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2047 | // Only work with fields and methods that are in the current oat file. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2048 | if (relocation.oat_index != oat_index) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2049 | continue; |
| 2050 | } |
| 2051 | auto* dest = image_info.image_->Begin() + relocation.offset; |
| 2052 | DCHECK_GE(dest, image_info.image_->Begin() + image_info.image_end_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2053 | DCHECK(!IsInBootImage(pair.first)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2054 | switch (relocation.type) { |
| 2055 | case kNativeObjectRelocationTypeArtField: { |
| 2056 | memcpy(dest, pair.first, sizeof(ArtField)); |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2057 | CopyReference( |
| 2058 | reinterpret_cast<ArtField*>(dest)->GetDeclaringClassAddressWithoutBarrier(), |
| 2059 | reinterpret_cast<ArtField*>(pair.first)->GetDeclaringClass().Ptr()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2060 | break; |
| 2061 | } |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2062 | case kNativeObjectRelocationTypeRuntimeMethod: |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2063 | case kNativeObjectRelocationTypeArtMethodClean: |
| 2064 | case kNativeObjectRelocationTypeArtMethodDirty: { |
| 2065 | CopyAndFixupMethod(reinterpret_cast<ArtMethod*>(pair.first), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2066 | reinterpret_cast<ArtMethod*>(dest), |
| 2067 | image_info); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2068 | break; |
| 2069 | } |
| 2070 | // For arrays, copy just the header since the elements will get copied by their corresponding |
| 2071 | // relocations. |
| 2072 | case kNativeObjectRelocationTypeArtFieldArray: { |
| 2073 | memcpy(dest, pair.first, LengthPrefixedArray<ArtField>::ComputeSize(0)); |
| 2074 | break; |
| 2075 | } |
| 2076 | case kNativeObjectRelocationTypeArtMethodArrayClean: |
| 2077 | case kNativeObjectRelocationTypeArtMethodArrayDirty: { |
Vladimir Marko | d9813cb | 2016-03-15 12:41:27 +0000 | [diff] [blame] | 2078 | size_t size = ArtMethod::Size(target_ptr_size_); |
| 2079 | size_t alignment = ArtMethod::Alignment(target_ptr_size_); |
| 2080 | memcpy(dest, pair.first, LengthPrefixedArray<ArtMethod>::ComputeSize(0, size, alignment)); |
| 2081 | // Clear padding to avoid non-deterministic data in the image (and placate valgrind). |
| 2082 | reinterpret_cast<LengthPrefixedArray<ArtMethod>*>(dest)->ClearPadding(size, alignment); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2083 | break; |
Vladimir Marko | d9813cb | 2016-03-15 12:41:27 +0000 | [diff] [blame] | 2084 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2085 | case kNativeObjectRelocationTypeDexCacheArray: |
| 2086 | // Nothing to copy here, everything is done in FixupDexCache(). |
| 2087 | break; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 2088 | case kNativeObjectRelocationTypeIMTable: { |
| 2089 | ImTable* orig_imt = reinterpret_cast<ImTable*>(pair.first); |
| 2090 | ImTable* dest_imt = reinterpret_cast<ImTable*>(dest); |
| 2091 | CopyAndFixupImTable(orig_imt, dest_imt); |
| 2092 | break; |
| 2093 | } |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2094 | case kNativeObjectRelocationTypeIMTConflictTable: { |
| 2095 | auto* orig_table = reinterpret_cast<ImtConflictTable*>(pair.first); |
| 2096 | CopyAndFixupImtConflictTable( |
| 2097 | orig_table, |
| 2098 | new(dest)ImtConflictTable(orig_table->NumEntries(target_ptr_size_), target_ptr_size_)); |
| 2099 | break; |
| 2100 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | // Fixup the image method roots. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2104 | auto* image_header = reinterpret_cast<ImageHeader*>(image_info.image_->Begin()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2105 | for (size_t i = 0; i < ImageHeader::kImageMethodsCount; ++i) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2106 | ArtMethod* method = image_methods_[i]; |
| 2107 | CHECK(method != nullptr); |
| 2108 | if (!IsInBootImage(method)) { |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2109 | method = NativeLocationInImage(method); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2110 | } |
| 2111 | image_header->SetImageMethod(static_cast<ImageHeader::ImageMethod>(i), method); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2112 | } |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 2113 | FixupRootVisitor root_visitor(this); |
| 2114 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2115 | // Write the intern table into the image. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2116 | if (image_info.intern_table_bytes_ > 0) { |
| 2117 | const ImageSection& intern_table_section = image_header->GetImageSection( |
| 2118 | ImageHeader::kSectionInternedStrings); |
| 2119 | InternTable* const intern_table = image_info.intern_table_.get(); |
| 2120 | uint8_t* const intern_table_memory_ptr = |
| 2121 | image_info.image_->Begin() + intern_table_section.Offset(); |
| 2122 | const size_t intern_table_bytes = intern_table->WriteToMemory(intern_table_memory_ptr); |
| 2123 | CHECK_EQ(intern_table_bytes, image_info.intern_table_bytes_); |
| 2124 | // Fixup the pointers in the newly written intern table to contain image addresses. |
| 2125 | InternTable temp_intern_table; |
| 2126 | // Note that we require that ReadFromMemory does not make an internal copy of the elements so that |
| 2127 | // the VisitRoots() will update the memory directly rather than the copies. |
| 2128 | // This also relies on visit roots not doing any verification which could fail after we update |
| 2129 | // the roots to be the image addresses. |
| 2130 | temp_intern_table.AddTableFromMemory(intern_table_memory_ptr); |
| 2131 | CHECK_EQ(temp_intern_table.Size(), intern_table->Size()); |
| 2132 | temp_intern_table.VisitRoots(&root_visitor, kVisitRootFlagAllRoots); |
| 2133 | } |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 2134 | // Write the class table(s) into the image. class_table_bytes_ may be 0 if there are multiple |
| 2135 | // class loaders. Writing multiple class tables into the image is currently unsupported. |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 2136 | if (image_info.class_table_bytes_ > 0u) { |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 2137 | const ImageSection& class_table_section = image_header->GetImageSection( |
| 2138 | ImageHeader::kSectionClassTable); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2139 | uint8_t* const class_table_memory_ptr = |
| 2140 | image_info.image_->Begin() + class_table_section.Offset(); |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 2141 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 2142 | |
| 2143 | ClassTable* table = image_info.class_table_.get(); |
| 2144 | CHECK(table != nullptr); |
| 2145 | const size_t class_table_bytes = table->WriteToMemory(class_table_memory_ptr); |
| 2146 | CHECK_EQ(class_table_bytes, image_info.class_table_bytes_); |
| 2147 | // Fixup the pointers in the newly written class table to contain image addresses. See |
| 2148 | // above comment for intern tables. |
| 2149 | ClassTable temp_class_table; |
| 2150 | temp_class_table.ReadFromMemory(class_table_memory_ptr); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 2151 | CHECK_EQ(temp_class_table.NumReferencedZygoteClasses(), |
| 2152 | table->NumReferencedNonZygoteClasses() + table->NumReferencedZygoteClasses()); |
Mathieu Chartier | 58c3f6a | 2016-12-01 14:21:11 -0800 | [diff] [blame] | 2153 | UnbufferedRootVisitor visitor(&root_visitor, RootInfo(kRootUnknown)); |
| 2154 | temp_class_table.VisitRoots(visitor); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 2155 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 2158 | void ImageWriter::CopyAndFixupObjects() { |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 2159 | auto visitor = [&](Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2160 | DCHECK(obj != nullptr); |
| 2161 | CopyAndFixupObject(obj); |
| 2162 | }; |
| 2163 | Runtime::Current()->GetHeap()->VisitObjects(visitor); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2164 | // Fix up the object previously had hash codes. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2165 | for (const auto& hash_pair : saved_hashcode_map_) { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 2166 | Object* obj = hash_pair.first; |
Andreas Gampe | 3b45ef2 | 2015-05-26 21:34:09 -0700 | [diff] [blame] | 2167 | DCHECK_EQ(obj->GetLockWord<kVerifyNone>(false).ReadBarrierState(), 0U); |
| 2168 | obj->SetLockWord<kVerifyNone>(LockWord::FromHashCode(hash_pair.second, 0U), false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2169 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2170 | saved_hashcode_map_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2171 | } |
| 2172 | |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2173 | void ImageWriter::FixupPointerArray(mirror::Object* dst, |
| 2174 | mirror::PointerArray* arr, |
| 2175 | mirror::Class* klass, |
| 2176 | Bin array_type) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2177 | CHECK(klass->IsArrayClass()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2178 | CHECK(arr->IsIntArray() || arr->IsLongArray()) << klass->PrettyClass() << " " << arr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2179 | // Fixup int and long pointers for the ArtMethod or ArtField arrays. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2180 | const size_t num_elements = arr->GetLength(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2181 | dst->SetClass(GetImageAddress(arr->GetClass())); |
| 2182 | auto* dest_array = down_cast<mirror::PointerArray*>(dst); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2183 | for (size_t i = 0, count = num_elements; i < count; ++i) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2184 | void* elem = arr->GetElementPtrSize<void*>(i, target_ptr_size_); |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2185 | if (kIsDebugBuild && elem != nullptr && !IsInBootImage(elem)) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2186 | auto it = native_object_relocations_.find(elem); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2187 | if (UNLIKELY(it == native_object_relocations_.end())) { |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 2188 | if (it->second.IsArtMethodRelocation()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2189 | auto* method = reinterpret_cast<ArtMethod*>(elem); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2190 | LOG(FATAL) << "No relocation entry for ArtMethod " << method->PrettyMethod() << " @ " |
| 2191 | << method << " idx=" << i << "/" << num_elements << " with declaring class " |
| 2192 | << Class::PrettyClass(method->GetDeclaringClass()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2193 | } else { |
| 2194 | CHECK_EQ(array_type, kBinArtField); |
| 2195 | auto* field = reinterpret_cast<ArtField*>(elem); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2196 | LOG(FATAL) << "No relocation entry for ArtField " << field->PrettyField() << " @ " |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2197 | << field << " idx=" << i << "/" << num_elements << " with declaring class " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2198 | << Class::PrettyClass(field->GetDeclaringClass()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2199 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2200 | UNREACHABLE(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2201 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2202 | } |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2203 | CopyAndFixupPointer(dest_array->ElementAddress(i, target_ptr_size_), elem); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2204 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | void ImageWriter::CopyAndFixupObject(Object* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2208 | if (IsInBootImage(obj)) { |
| 2209 | return; |
| 2210 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2211 | size_t offset = GetImageOffset(obj); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2212 | size_t oat_index = GetOatIndex(obj); |
| 2213 | ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2214 | auto* dst = reinterpret_cast<Object*>(image_info.image_->Begin() + offset); |
| 2215 | DCHECK_LT(offset, image_info.image_end_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2216 | const auto* src = reinterpret_cast<const uint8_t*>(obj); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2217 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2218 | image_info.image_bitmap_->Set(dst); // Mark the obj as live. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2219 | |
| 2220 | const size_t n = obj->SizeOf(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2221 | DCHECK_LE(offset + n, image_info.image_->Size()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2222 | memcpy(dst, src, n); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2223 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 2224 | // Write in a hash code of objects which have inflated monitors or a hash code in their monitor |
| 2225 | // word. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2226 | const auto it = saved_hashcode_map_.find(obj); |
| 2227 | dst->SetLockWord(it != saved_hashcode_map_.end() ? |
| 2228 | LockWord::FromHashCode(it->second, 0u) : LockWord::Default(), false); |
Mathieu Chartier | 36a270a | 2016-07-28 18:08:51 -0700 | [diff] [blame] | 2229 | if (kUseBakerReadBarrier && gc::collector::ConcurrentCopying::kGrayDirtyImmuneObjects) { |
| 2230 | // Treat all of the objects in the image as marked to avoid unnecessary dirty pages. This is |
| 2231 | // safe since we mark all of the objects that may reference non immune objects as gray. |
| 2232 | CHECK(dst->AtomicSetMarkBit(0, 1)); |
| 2233 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2234 | FixupObject(obj, dst); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 2237 | // Rewrite all the references in the copied object to point to their image address equivalent |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 2238 | class ImageWriter::FixupVisitor { |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 2239 | public: |
| 2240 | FixupVisitor(ImageWriter* image_writer, Object* copy) : image_writer_(image_writer), copy_(copy) { |
| 2241 | } |
| 2242 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 2243 | // Ignore class roots since we don't have a way to map them to the destination. These are handled |
| 2244 | // with other logic. |
| 2245 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 2246 | const {} |
| 2247 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 2248 | |
| 2249 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 2250 | void operator()(ObjPtr<Object> obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2251 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 2252 | ObjPtr<Object> ref = obj->GetFieldObject<Object, kVerifyNone>(offset); |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2253 | // Copy the reference and record the fixup if necessary. |
| 2254 | image_writer_->CopyReference( |
| 2255 | copy_->GetFieldObjectReferenceAddr<kVerifyNone>(offset), |
| 2256 | ref.Ptr()); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | // java.lang.ref.Reference visitor. |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 2260 | void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 2261 | ObjPtr<mirror::Reference> ref) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2262 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2263 | operator()(ref, mirror::Reference::ReferentOffset(), /* is_static */ false); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 2264 | } |
| 2265 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2266 | protected: |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 2267 | ImageWriter* const image_writer_; |
| 2268 | mirror::Object* const copy_; |
| 2269 | }; |
| 2270 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 2271 | class ImageWriter::FixupClassVisitor FINAL : public FixupVisitor { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2272 | public: |
| 2273 | FixupClassVisitor(ImageWriter* image_writer, Object* copy) : FixupVisitor(image_writer, copy) { |
| 2274 | } |
| 2275 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 2276 | void operator()(ObjPtr<Object> obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 2277 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2278 | DCHECK(obj->IsClass()); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 2279 | FixupVisitor::operator()(obj, offset, /*is_static*/false); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 2282 | void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 2283 | ObjPtr<mirror::Reference> ref ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2284 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2285 | LOG(FATAL) << "Reference not expected here."; |
| 2286 | } |
| 2287 | }; |
| 2288 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2289 | uintptr_t ImageWriter::NativeOffsetInImage(void* obj) { |
| 2290 | DCHECK(obj != nullptr); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2291 | DCHECK(!IsInBootImage(obj)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2292 | auto it = native_object_relocations_.find(obj); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2293 | CHECK(it != native_object_relocations_.end()) << obj << " spaces " |
| 2294 | << Runtime::Current()->GetHeap()->DumpSpaces(); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 2295 | const NativeObjectRelocation& relocation = it->second; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2296 | return relocation.offset; |
| 2297 | } |
| 2298 | |
| 2299 | template <typename T> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2300 | std::string PrettyPrint(T* ptr) REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 2301 | std::ostringstream oss; |
| 2302 | oss << ptr; |
| 2303 | return oss.str(); |
| 2304 | } |
| 2305 | |
| 2306 | template <> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2307 | std::string PrettyPrint(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2308 | return ArtMethod::PrettyMethod(method); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
| 2311 | template <typename T> |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2312 | T* ImageWriter::NativeLocationInImage(T* obj) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2313 | if (obj == nullptr || IsInBootImage(obj)) { |
| 2314 | return obj; |
| 2315 | } else { |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2316 | auto it = native_object_relocations_.find(obj); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 2317 | CHECK(it != native_object_relocations_.end()) << obj << " " << PrettyPrint(obj) |
| 2318 | << " spaces " << Runtime::Current()->GetHeap()->DumpSpaces(); |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2319 | const NativeObjectRelocation& relocation = it->second; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2320 | ImageInfo& image_info = GetImageInfo(relocation.oat_index); |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2321 | return reinterpret_cast<T*>(image_info.image_begin_ + relocation.offset); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2322 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2325 | template <typename T> |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2326 | T* ImageWriter::NativeCopyLocation(T* obj, mirror::DexCache* dex_cache) { |
| 2327 | if (obj == nullptr || IsInBootImage(obj)) { |
| 2328 | return obj; |
| 2329 | } else { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2330 | size_t oat_index = GetOatIndexForDexCache(dex_cache); |
| 2331 | ImageInfo& image_info = GetImageInfo(oat_index); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2332 | return reinterpret_cast<T*>(image_info.image_->Begin() + NativeOffsetInImage(obj)); |
| 2333 | } |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2334 | } |
| 2335 | |
Vladimir Marko | ad06b98 | 2016-11-17 16:38:59 +0000 | [diff] [blame] | 2336 | class ImageWriter::NativeLocationVisitor { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2337 | public: |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2338 | explicit NativeLocationVisitor(ImageWriter* image_writer) : image_writer_(image_writer) {} |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2339 | |
| 2340 | template <typename T> |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2341 | T* operator()(T* ptr, void** dest_addr = nullptr) const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2342 | if (dest_addr != nullptr) { |
| 2343 | image_writer_->CopyAndFixupPointer(dest_addr, ptr); |
| 2344 | } |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2345 | return image_writer_->NativeLocationInImage(ptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2346 | } |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2347 | |
| 2348 | private: |
| 2349 | ImageWriter* const image_writer_; |
| 2350 | }; |
| 2351 | |
| 2352 | void ImageWriter::FixupClass(mirror::Class* orig, mirror::Class* copy) { |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2353 | orig->FixupNativePointers(copy, target_ptr_size_, NativeLocationVisitor(this)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2354 | FixupClassVisitor visitor(this, copy); |
Mathieu Chartier | 31e8822 | 2016-10-14 18:43:19 -0700 | [diff] [blame] | 2355 | ObjPtr<mirror::Object>(orig)->VisitReferences(visitor, visitor); |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2356 | |
| 2357 | // Remove the clinitThreadId. This is required for image determinism. |
| 2358 | copy->SetClinitThreadId(static_cast<pid_t>(0)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2359 | } |
| 2360 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2361 | void ImageWriter::FixupObject(Object* orig, Object* copy) { |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 2362 | DCHECK(orig != nullptr); |
| 2363 | DCHECK(copy != nullptr); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 2364 | if (kUseBakerReadBarrier) { |
| 2365 | orig->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 2366 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2367 | auto* klass = orig->GetClass(); |
| 2368 | if (klass->IsIntArrayClass() || klass->IsLongArrayClass()) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2369 | // Is this a native pointer array? |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2370 | auto it = pointer_arrays_.find(down_cast<mirror::PointerArray*>(orig)); |
| 2371 | if (it != pointer_arrays_.end()) { |
| 2372 | // Should only need to fixup every pointer array exactly once. |
| 2373 | FixupPointerArray(copy, down_cast<mirror::PointerArray*>(orig), klass, it->second); |
| 2374 | pointer_arrays_.erase(it); |
| 2375 | return; |
| 2376 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2377 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2378 | if (orig->IsClass()) { |
| 2379 | FixupClass(orig->AsClass<kVerifyNone>(), down_cast<mirror::Class*>(copy)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2380 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2381 | if (klass == mirror::Method::StaticClass() || klass == mirror::Constructor::StaticClass()) { |
| 2382 | // Need to go update the ArtMethod. |
Neil Fuller | 0e84439 | 2016-09-08 13:43:31 +0100 | [diff] [blame] | 2383 | auto* dest = down_cast<mirror::Executable*>(copy); |
| 2384 | auto* src = down_cast<mirror::Executable*>(orig); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2385 | ArtMethod* src_method = src->GetArtMethod(); |
Jing Ji | 96e640c | 2016-08-31 21:21:37 -0500 | [diff] [blame] | 2386 | dest->SetArtMethod(GetImageMethodAddress(src_method)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2387 | } else if (!klass->IsArrayClass()) { |
| 2388 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 2389 | if (klass == class_linker->GetClassRoot(ClassLinker::kJavaLangDexCache)) { |
| 2390 | FixupDexCache(down_cast<mirror::DexCache*>(orig), down_cast<mirror::DexCache*>(copy)); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 2391 | } else if (klass->IsClassLoaderClass()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2392 | mirror::ClassLoader* copy_loader = down_cast<mirror::ClassLoader*>(copy); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2393 | // If src is a ClassLoader, set the class table to null so that it gets recreated by the |
| 2394 | // ClassLoader. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2395 | copy_loader->SetClassTable(nullptr); |
Mathieu Chartier | 5550c56 | 2015-09-22 15:18:04 -0700 | [diff] [blame] | 2396 | // Also set allocator to null to be safe. The allocator is created when we create the class |
| 2397 | // table. We also never expect to unload things in the image since they are held live as |
| 2398 | // roots. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2399 | copy_loader->SetAllocator(nullptr); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2400 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2401 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2402 | FixupVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 2403 | orig->VisitReferences(visitor, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2404 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2405 | } |
| 2406 | |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2407 | class ImageWriter::ImageAddressVisitorForDexCacheArray { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2408 | public: |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2409 | explicit ImageAddressVisitorForDexCacheArray(ImageWriter* image_writer) |
| 2410 | : image_writer_(image_writer) {} |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2411 | |
| 2412 | template <typename T> |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2413 | T* operator()(T* ptr) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2414 | return image_writer_->GetImageAddress(ptr); |
| 2415 | } |
| 2416 | |
| 2417 | private: |
| 2418 | ImageWriter* const image_writer_; |
| 2419 | }; |
| 2420 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2421 | void ImageWriter::FixupDexCache(mirror::DexCache* orig_dex_cache, |
| 2422 | mirror::DexCache* copy_dex_cache) { |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2423 | ImageAddressVisitorForDexCacheArray fixup_visitor(this); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2424 | // Though the DexCache array fields are usually treated as native pointers, we set the full |
| 2425 | // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is |
| 2426 | // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e. |
| 2427 | // static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))). |
Christina Wadsworth | bf44e0e | 2016-08-18 10:37:42 -0700 | [diff] [blame] | 2428 | mirror::StringDexCacheType* orig_strings = orig_dex_cache->GetStrings(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2429 | if (orig_strings != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2430 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::StringsOffset(), |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2431 | NativeLocationInImage(orig_strings), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2432 | PointerSize::k64); |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2433 | orig_dex_cache->FixupStrings(NativeCopyLocation(orig_strings, orig_dex_cache), fixup_visitor); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2434 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 2435 | mirror::TypeDexCacheType* orig_types = orig_dex_cache->GetResolvedTypes(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2436 | if (orig_types != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2437 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedTypesOffset(), |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2438 | NativeLocationInImage(orig_types), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2439 | PointerSize::k64); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2440 | orig_dex_cache->FixupResolvedTypes(NativeCopyLocation(orig_types, orig_dex_cache), |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2441 | fixup_visitor); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2442 | } |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2443 | mirror::MethodDexCacheType* orig_methods = orig_dex_cache->GetResolvedMethods(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2444 | if (orig_methods != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2445 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedMethodsOffset(), |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2446 | NativeLocationInImage(orig_methods), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2447 | PointerSize::k64); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2448 | mirror::MethodDexCacheType* copy_methods = NativeCopyLocation(orig_methods, orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2449 | for (size_t i = 0, num = orig_dex_cache->NumResolvedMethods(); i != num; ++i) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2450 | mirror::MethodDexCachePair orig_pair = |
| 2451 | mirror::DexCache::GetNativePairPtrSize(orig_methods, i, target_ptr_size_); |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2452 | // NativeLocationInImage also handles runtime methods since these have relocation info. |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2453 | mirror::MethodDexCachePair copy_pair(NativeLocationInImage(orig_pair.object), |
| 2454 | orig_pair.index); |
| 2455 | mirror::DexCache::SetNativePairPtrSize(copy_methods, i, copy_pair, target_ptr_size_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2456 | } |
| 2457 | } |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 2458 | mirror::FieldDexCacheType* orig_fields = orig_dex_cache->GetResolvedFields(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2459 | if (orig_fields != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 2460 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedFieldsOffset(), |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2461 | NativeLocationInImage(orig_fields), |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2462 | PointerSize::k64); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 2463 | mirror::FieldDexCacheType* copy_fields = NativeCopyLocation(orig_fields, orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2464 | for (size_t i = 0, num = orig_dex_cache->NumResolvedFields(); i != num; ++i) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 2465 | mirror::FieldDexCachePair orig = |
| 2466 | mirror::DexCache::GetNativePairPtrSize(orig_fields, i, target_ptr_size_); |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2467 | mirror::FieldDexCachePair copy = orig; |
| 2468 | copy.object = NativeLocationInImage(orig.object); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 2469 | mirror::DexCache::SetNativePairPtrSize(copy_fields, i, copy, target_ptr_size_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2470 | } |
| 2471 | } |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 2472 | mirror::MethodTypeDexCacheType* orig_method_types = orig_dex_cache->GetResolvedMethodTypes(); |
| 2473 | if (orig_method_types != nullptr) { |
| 2474 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedMethodTypesOffset(), |
| 2475 | NativeLocationInImage(orig_method_types), |
| 2476 | PointerSize::k64); |
| 2477 | orig_dex_cache->FixupResolvedMethodTypes(NativeCopyLocation(orig_method_types, orig_dex_cache), |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2478 | fixup_visitor); |
Narayan Kamath | 7fe5658 | 2016-10-14 18:49:12 +0100 | [diff] [blame] | 2479 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 2480 | GcRoot<mirror::CallSite>* orig_call_sites = orig_dex_cache->GetResolvedCallSites(); |
| 2481 | if (orig_call_sites != nullptr) { |
| 2482 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedCallSitesOffset(), |
| 2483 | NativeLocationInImage(orig_call_sites), |
| 2484 | PointerSize::k64); |
| 2485 | orig_dex_cache->FixupResolvedCallSites(NativeCopyLocation(orig_call_sites, orig_dex_cache), |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2486 | fixup_visitor); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 2487 | } |
Andreas Gampe | ace0dc1 | 2016-01-20 13:33:13 -0800 | [diff] [blame] | 2488 | |
| 2489 | // Remove the DexFile pointers. They will be fixed up when the runtime loads the oat file. Leaving |
| 2490 | // compiler pointers in here will make the output non-deterministic. |
| 2491 | copy_dex_cache->SetDexFile(nullptr); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2492 | } |
| 2493 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2494 | const uint8_t* ImageWriter::GetOatAddress(OatAddress type) const { |
| 2495 | DCHECK_LT(type, kOatAddressCount); |
| 2496 | // If we are compiling an app image, we need to use the stubs of the boot image. |
| 2497 | if (compile_app_image_) { |
| 2498 | // Use the current image pointers. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2499 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2500 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 2501 | DCHECK(!image_spaces.empty()); |
| 2502 | const OatFile* oat_file = image_spaces[0]->GetOatFile(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2503 | CHECK(oat_file != nullptr); |
| 2504 | const OatHeader& header = oat_file->GetOatHeader(); |
| 2505 | switch (type) { |
| 2506 | // TODO: We could maybe clean this up if we stored them in an array in the oat header. |
| 2507 | case kOatAddressQuickGenericJNITrampoline: |
| 2508 | return static_cast<const uint8_t*>(header.GetQuickGenericJniTrampoline()); |
| 2509 | case kOatAddressInterpreterToInterpreterBridge: |
| 2510 | return static_cast<const uint8_t*>(header.GetInterpreterToInterpreterBridge()); |
| 2511 | case kOatAddressInterpreterToCompiledCodeBridge: |
| 2512 | return static_cast<const uint8_t*>(header.GetInterpreterToCompiledCodeBridge()); |
| 2513 | case kOatAddressJNIDlsymLookup: |
| 2514 | return static_cast<const uint8_t*>(header.GetJniDlsymLookup()); |
| 2515 | case kOatAddressQuickIMTConflictTrampoline: |
| 2516 | return static_cast<const uint8_t*>(header.GetQuickImtConflictTrampoline()); |
| 2517 | case kOatAddressQuickResolutionTrampoline: |
| 2518 | return static_cast<const uint8_t*>(header.GetQuickResolutionTrampoline()); |
| 2519 | case kOatAddressQuickToInterpreterBridge: |
| 2520 | return static_cast<const uint8_t*>(header.GetQuickToInterpreterBridge()); |
| 2521 | default: |
| 2522 | UNREACHABLE(); |
| 2523 | } |
| 2524 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2525 | const ImageInfo& primary_image_info = GetImageInfo(0); |
| 2526 | return GetOatAddressForOffset(primary_image_info.oat_address_offsets_[type], primary_image_info); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2527 | } |
| 2528 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2529 | const uint8_t* ImageWriter::GetQuickCode(ArtMethod* method, |
| 2530 | const ImageInfo& image_info, |
| 2531 | bool* quick_is_interpreted) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2532 | DCHECK(!method->IsResolutionMethod()) << method->PrettyMethod(); |
| 2533 | DCHECK_NE(method, Runtime::Current()->GetImtConflictMethod()) << method->PrettyMethod(); |
| 2534 | DCHECK(!method->IsImtUnimplementedMethod()) << method->PrettyMethod(); |
| 2535 | DCHECK(method->IsInvokable()) << method->PrettyMethod(); |
| 2536 | DCHECK(!IsInBootImage(method)) << method->PrettyMethod(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2537 | |
| 2538 | // Use original code if it exists. Otherwise, set the code pointer to the resolution |
| 2539 | // trampoline. |
| 2540 | |
| 2541 | // Quick entrypoint: |
Igor Murashkin | 0ccfe2c | 2016-02-19 16:41:44 -0800 | [diff] [blame] | 2542 | const void* quick_oat_entry_point = |
| 2543 | method->GetEntryPointFromQuickCompiledCodePtrSize(target_ptr_size_); |
| 2544 | const uint8_t* quick_code; |
| 2545 | |
| 2546 | if (UNLIKELY(IsInBootImage(method->GetDeclaringClass()))) { |
| 2547 | DCHECK(method->IsCopied()); |
| 2548 | // If the code is not in the oat file corresponding to this image (e.g. default methods) |
| 2549 | quick_code = reinterpret_cast<const uint8_t*>(quick_oat_entry_point); |
| 2550 | } else { |
| 2551 | uint32_t quick_oat_code_offset = PointerToLowMemUInt32(quick_oat_entry_point); |
| 2552 | quick_code = GetOatAddressForOffset(quick_oat_code_offset, image_info); |
| 2553 | } |
| 2554 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2555 | *quick_is_interpreted = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2556 | if (quick_code != nullptr && (!method->IsStatic() || method->IsConstructor() || |
| 2557 | method->GetDeclaringClass()->IsInitialized())) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2558 | // We have code for a non-static or initialized method, just use the code. |
| 2559 | } else if (quick_code == nullptr && method->IsNative() && |
| 2560 | (!method->IsStatic() || method->GetDeclaringClass()->IsInitialized())) { |
| 2561 | // Non-static or initialized native method missing compiled code, use generic JNI version. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2562 | quick_code = GetOatAddress(kOatAddressQuickGenericJNITrampoline); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2563 | } else if (quick_code == nullptr && !method->IsNative()) { |
| 2564 | // We don't have code at all for a non-native method, use the interpreter. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2565 | quick_code = GetOatAddress(kOatAddressQuickToInterpreterBridge); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2566 | *quick_is_interpreted = true; |
| 2567 | } else { |
| 2568 | CHECK(!method->GetDeclaringClass()->IsInitialized()); |
| 2569 | // We have code for a static method, but need to go through the resolution stub for class |
| 2570 | // initialization. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2571 | quick_code = GetOatAddress(kOatAddressQuickResolutionTrampoline); |
| 2572 | } |
| 2573 | if (!IsInBootOatFile(quick_code)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2574 | // DCHECK_GE(quick_code, oat_data_begin_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2575 | } |
| 2576 | return quick_code; |
| 2577 | } |
| 2578 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2579 | void ImageWriter::CopyAndFixupMethod(ArtMethod* orig, |
| 2580 | ArtMethod* copy, |
| 2581 | const ImageInfo& image_info) { |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 2582 | if (orig->IsAbstract()) { |
| 2583 | // Ignore the single-implementation info for abstract method. |
| 2584 | // Do this on orig instead of copy, otherwise there is a crash due to methods |
| 2585 | // are copied before classes. |
| 2586 | // TODO: handle fixup of single-implementation method for abstract method. |
| 2587 | orig->SetHasSingleImplementation(false); |
| 2588 | orig->SetSingleImplementation( |
| 2589 | nullptr, Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 2590 | } |
| 2591 | |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 2592 | memcpy(copy, orig, ArtMethod::Size(target_ptr_size_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2593 | |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2594 | CopyReference(copy->GetDeclaringClassAddressWithoutBarrier(), orig->GetDeclaringClassUnchecked()); |
| 2595 | |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2596 | mirror::MethodDexCacheType* orig_resolved_methods = |
| 2597 | orig->GetDexCacheResolvedMethods(target_ptr_size_); |
Mathieu Chartier | e8bf134 | 2016-02-17 18:02:40 -0800 | [diff] [blame] | 2598 | copy->SetDexCacheResolvedMethods(NativeLocationInImage(orig_resolved_methods), target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2599 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2600 | // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to |
| 2601 | // oat_begin_ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2602 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2603 | // The resolution method has a special trampoline to call. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 2604 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2605 | if (orig->IsRuntimeMethod()) { |
| 2606 | ImtConflictTable* orig_table = orig->GetImtConflictTable(target_ptr_size_); |
| 2607 | if (orig_table != nullptr) { |
| 2608 | // Special IMT conflict method, normal IMT conflict method or unimplemented IMT method. |
| 2609 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
| 2610 | GetOatAddress(kOatAddressQuickIMTConflictTrampoline), target_ptr_size_); |
| 2611 | copy->SetImtConflictTable(NativeLocationInImage(orig_table), target_ptr_size_); |
| 2612 | } else if (UNLIKELY(orig == runtime->GetResolutionMethod())) { |
| 2613 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
| 2614 | GetOatAddress(kOatAddressQuickResolutionTrampoline), target_ptr_size_); |
| 2615 | } else { |
| 2616 | bool found_one = false; |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 2617 | for (size_t i = 0; i < static_cast<size_t>(CalleeSaveType::kLastCalleeSaveType); ++i) { |
| 2618 | auto idx = static_cast<CalleeSaveType>(i); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2619 | if (runtime->HasCalleeSaveMethod(idx) && runtime->GetCalleeSaveMethod(idx) == orig) { |
| 2620 | found_one = true; |
| 2621 | break; |
| 2622 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2623 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2624 | CHECK(found_one) << "Expected to find callee save method but got " << orig->PrettyMethod(); |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2625 | CHECK(copy->IsRuntimeMethod()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2626 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2627 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2628 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 2629 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 2630 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2631 | if (UNLIKELY(!orig->IsInvokable())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2632 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2633 | GetOatAddress(kOatAddressQuickToInterpreterBridge), target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2634 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2635 | bool quick_is_interpreted; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2636 | const uint8_t* quick_code = GetQuickCode(orig, image_info, &quick_is_interpreted); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2637 | copy->SetEntryPointFromQuickCompiledCodePtrSize(quick_code, target_ptr_size_); |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 2638 | |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 2639 | // JNI entrypoint: |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2640 | if (orig->IsNative()) { |
| 2641 | // The native method's pointer is set to a stub to lookup via dlsym. |
| 2642 | // Note this is not the code_ pointer, that is handled above. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2643 | copy->SetEntryPointFromJniPtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2644 | GetOatAddress(kOatAddressJNIDlsymLookup), target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2645 | } |
| 2646 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2647 | } |
| 2648 | } |
| 2649 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2650 | size_t ImageWriter::GetBinSizeSum(ImageWriter::ImageInfo& image_info, ImageWriter::Bin up_to) const { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 2651 | DCHECK_LE(up_to, kBinSize); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2652 | return std::accumulate(&image_info.bin_slot_sizes_[0], |
| 2653 | &image_info.bin_slot_sizes_[up_to], |
| 2654 | /*init*/0); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | ImageWriter::BinSlot::BinSlot(uint32_t lockword) : lockword_(lockword) { |
| 2658 | // These values may need to get updated if more bins are added to the enum Bin |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2659 | static_assert(kBinBits == 3, "wrong number of bin bits"); |
| 2660 | static_assert(kBinShift == 27, "wrong number of shift"); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 2661 | static_assert(sizeof(BinSlot) == sizeof(LockWord), "BinSlot/LockWord must have equal sizes"); |
| 2662 | |
| 2663 | DCHECK_LT(GetBin(), kBinSize); |
| 2664 | DCHECK_ALIGNED(GetIndex(), kObjectAlignment); |
| 2665 | } |
| 2666 | |
| 2667 | ImageWriter::BinSlot::BinSlot(Bin bin, uint32_t index) |
| 2668 | : BinSlot(index | (static_cast<uint32_t>(bin) << kBinShift)) { |
| 2669 | DCHECK_EQ(index, GetIndex()); |
| 2670 | } |
| 2671 | |
| 2672 | ImageWriter::Bin ImageWriter::BinSlot::GetBin() const { |
| 2673 | return static_cast<Bin>((lockword_ & kBinMask) >> kBinShift); |
| 2674 | } |
| 2675 | |
| 2676 | uint32_t ImageWriter::BinSlot::GetIndex() const { |
| 2677 | return lockword_ & ~kBinMask; |
| 2678 | } |
| 2679 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2680 | ImageWriter::Bin ImageWriter::BinTypeForNativeRelocationType(NativeObjectRelocationType type) { |
| 2681 | switch (type) { |
| 2682 | case kNativeObjectRelocationTypeArtField: |
| 2683 | case kNativeObjectRelocationTypeArtFieldArray: |
| 2684 | return kBinArtField; |
| 2685 | case kNativeObjectRelocationTypeArtMethodClean: |
| 2686 | case kNativeObjectRelocationTypeArtMethodArrayClean: |
| 2687 | return kBinArtMethodClean; |
| 2688 | case kNativeObjectRelocationTypeArtMethodDirty: |
| 2689 | case kNativeObjectRelocationTypeArtMethodArrayDirty: |
| 2690 | return kBinArtMethodDirty; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2691 | case kNativeObjectRelocationTypeDexCacheArray: |
| 2692 | return kBinDexCacheArray; |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2693 | case kNativeObjectRelocationTypeRuntimeMethod: |
| 2694 | return kBinRuntimeMethod; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 2695 | case kNativeObjectRelocationTypeIMTable: |
| 2696 | return kBinImTable; |
Mathieu Chartier | e42888f | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 2697 | case kNativeObjectRelocationTypeIMTConflictTable: |
| 2698 | return kBinIMTConflictTable; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2699 | } |
| 2700 | UNREACHABLE(); |
| 2701 | } |
| 2702 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2703 | size_t ImageWriter::GetOatIndex(mirror::Object* obj) const { |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 2704 | if (!IsMultiImage()) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2705 | return GetDefaultOatIndex(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2706 | } |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 2707 | auto it = oat_index_map_.find(obj); |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2708 | DCHECK(it != oat_index_map_.end()) << obj; |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 2709 | return it->second; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2710 | } |
| 2711 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2712 | size_t ImageWriter::GetOatIndexForDexFile(const DexFile* dex_file) const { |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 2713 | if (!IsMultiImage()) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2714 | return GetDefaultOatIndex(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2715 | } |
Mathieu Chartier | 496577f | 2016-09-20 15:33:31 -0700 | [diff] [blame] | 2716 | auto it = dex_file_oat_index_map_.find(dex_file); |
| 2717 | DCHECK(it != dex_file_oat_index_map_.end()) << dex_file->GetLocation(); |
| 2718 | return it->second; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2719 | } |
| 2720 | |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 2721 | size_t ImageWriter::GetOatIndexForDexCache(ObjPtr<mirror::DexCache> dex_cache) const { |
| 2722 | return (dex_cache == nullptr) |
| 2723 | ? GetDefaultOatIndex() |
| 2724 | : GetOatIndexForDexFile(dex_cache->GetDexFile()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2725 | } |
| 2726 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2727 | void ImageWriter::UpdateOatFileLayout(size_t oat_index, |
| 2728 | size_t oat_loaded_size, |
| 2729 | size_t oat_data_offset, |
| 2730 | size_t oat_data_size) { |
| 2731 | const uint8_t* images_end = image_infos_.back().image_begin_ + image_infos_.back().image_size_; |
| 2732 | for (const ImageInfo& info : image_infos_) { |
| 2733 | DCHECK_LE(info.image_begin_ + info.image_size_, images_end); |
| 2734 | } |
| 2735 | DCHECK(images_end != nullptr); // Image space must be ready. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2736 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2737 | ImageInfo& cur_image_info = GetImageInfo(oat_index); |
| 2738 | cur_image_info.oat_file_begin_ = images_end + cur_image_info.oat_offset_; |
| 2739 | cur_image_info.oat_loaded_size_ = oat_loaded_size; |
| 2740 | cur_image_info.oat_data_begin_ = cur_image_info.oat_file_begin_ + oat_data_offset; |
| 2741 | cur_image_info.oat_size_ = oat_data_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2742 | |
Mathieu Chartier | 14567fd | 2016-01-28 20:33:36 -0800 | [diff] [blame] | 2743 | if (compile_app_image_) { |
| 2744 | CHECK_EQ(oat_filenames_.size(), 1u) << "App image should have no next image."; |
| 2745 | return; |
| 2746 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2747 | |
| 2748 | // Update the oat_offset of the next image info. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2749 | if (oat_index + 1u != oat_filenames_.size()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2750 | // There is a following one. |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2751 | ImageInfo& next_image_info = GetImageInfo(oat_index + 1u); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2752 | next_image_info.oat_offset_ = cur_image_info.oat_offset_ + oat_loaded_size; |
| 2753 | } |
| 2754 | } |
| 2755 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2756 | void ImageWriter::UpdateOatFileHeader(size_t oat_index, const OatHeader& oat_header) { |
| 2757 | ImageInfo& cur_image_info = GetImageInfo(oat_index); |
| 2758 | cur_image_info.oat_checksum_ = oat_header.GetChecksum(); |
| 2759 | |
| 2760 | if (oat_index == GetDefaultOatIndex()) { |
| 2761 | // Primary oat file, read the trampolines. |
| 2762 | cur_image_info.oat_address_offsets_[kOatAddressInterpreterToInterpreterBridge] = |
| 2763 | oat_header.GetInterpreterToInterpreterBridgeOffset(); |
| 2764 | cur_image_info.oat_address_offsets_[kOatAddressInterpreterToCompiledCodeBridge] = |
| 2765 | oat_header.GetInterpreterToCompiledCodeBridgeOffset(); |
| 2766 | cur_image_info.oat_address_offsets_[kOatAddressJNIDlsymLookup] = |
| 2767 | oat_header.GetJniDlsymLookupOffset(); |
| 2768 | cur_image_info.oat_address_offsets_[kOatAddressQuickGenericJNITrampoline] = |
| 2769 | oat_header.GetQuickGenericJniTrampolineOffset(); |
| 2770 | cur_image_info.oat_address_offsets_[kOatAddressQuickIMTConflictTrampoline] = |
| 2771 | oat_header.GetQuickImtConflictTrampolineOffset(); |
| 2772 | cur_image_info.oat_address_offsets_[kOatAddressQuickResolutionTrampoline] = |
| 2773 | oat_header.GetQuickResolutionTrampolineOffset(); |
| 2774 | cur_image_info.oat_address_offsets_[kOatAddressQuickToInterpreterBridge] = |
| 2775 | oat_header.GetQuickToInterpreterBridgeOffset(); |
| 2776 | } |
| 2777 | } |
| 2778 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2779 | ImageWriter::ImageWriter( |
| 2780 | const CompilerDriver& compiler_driver, |
| 2781 | uintptr_t image_begin, |
| 2782 | bool compile_pic, |
| 2783 | bool compile_app_image, |
| 2784 | ImageHeader::StorageMode image_storage_mode, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2785 | const std::vector<const char*>& oat_filenames, |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 2786 | const std::unordered_map<const DexFile*, size_t>& dex_file_oat_index_map, |
| 2787 | const std::unordered_set<std::string>* dirty_image_objects) |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2788 | : compiler_driver_(compiler_driver), |
| 2789 | global_image_begin_(reinterpret_cast<uint8_t*>(image_begin)), |
| 2790 | image_objects_offset_begin_(0), |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2791 | compile_pic_(compile_pic), |
| 2792 | compile_app_image_(compile_app_image), |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2793 | target_ptr_size_(InstructionSetPointerSize(compiler_driver_.GetInstructionSet())), |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 2794 | image_infos_(oat_filenames.size()), |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2795 | dirty_methods_(0u), |
| 2796 | clean_methods_(0u), |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2797 | image_storage_mode_(image_storage_mode), |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2798 | oat_filenames_(oat_filenames), |
Jeff Hao | c23b0c0 | 2017-07-27 18:19:38 -0700 | [diff] [blame] | 2799 | dex_file_oat_index_map_(dex_file_oat_index_map), |
| 2800 | dirty_image_objects_(dirty_image_objects) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2801 | CHECK_NE(image_begin, 0U); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2802 | std::fill_n(image_methods_, arraysize(image_methods_), nullptr); |
Mathieu Chartier | 901e070 | 2016-02-19 13:42:48 -0800 | [diff] [blame] | 2803 | CHECK_EQ(compile_app_image, !Runtime::Current()->GetHeap()->GetBootImageSpaces().empty()) |
| 2804 | << "Compiling a boot image should occur iff there are no boot image spaces loaded"; |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2805 | } |
| 2806 | |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame] | 2807 | ImageWriter::ImageInfo::ImageInfo() |
| 2808 | : intern_table_(new InternTable), |
| 2809 | class_table_(new ClassTable) {} |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2810 | |
Mathieu Chartier | 8c19d24 | 2017-03-06 12:35:10 -0800 | [diff] [blame] | 2811 | void ImageWriter::CopyReference(mirror::HeapReference<mirror::Object>* dest, |
| 2812 | ObjPtr<mirror::Object> src) { |
| 2813 | dest->Assign(GetImageAddress(src.Ptr())); |
| 2814 | } |
| 2815 | |
| 2816 | void ImageWriter::CopyReference(mirror::CompressedReference<mirror::Object>* dest, |
| 2817 | ObjPtr<mirror::Object> src) { |
| 2818 | dest->Assign(GetImageAddress(src.Ptr())); |
| 2819 | } |
| 2820 | |
| 2821 | void ImageWriter::CopyAndFixupPointer(void** target, void* value) { |
| 2822 | void* new_value = value; |
| 2823 | if (value != nullptr && !IsInBootImage(value)) { |
| 2824 | auto it = native_object_relocations_.find(value); |
| 2825 | CHECK(it != native_object_relocations_.end()) << value; |
| 2826 | const NativeObjectRelocation& relocation = it->second; |
| 2827 | ImageInfo& image_info = GetImageInfo(relocation.oat_index); |
| 2828 | new_value = reinterpret_cast<void*>(image_info.image_begin_ + relocation.offset); |
| 2829 | } |
| 2830 | if (target_ptr_size_ == PointerSize::k32) { |
| 2831 | *reinterpret_cast<uint32_t*>(target) = PointerToLowMemUInt32(new_value); |
| 2832 | } else { |
| 2833 | *reinterpret_cast<uint64_t*>(target) = reinterpret_cast<uintptr_t>(new_value); |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2838 | } // namespace art |