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