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