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