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