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