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> |
| 20 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 22 | #include <numeric> |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 23 | #include <unordered_set> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 26 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 27 | #include "art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | #include "base/logging.h" |
| 29 | #include "base/unix_file/fd_file.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 30 | #include "class_linker-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 31 | #include "compiled_method.h" |
| 32 | #include "dex_file-inl.h" |
| 33 | #include "driver/compiler_driver.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 34 | #include "elf_file.h" |
| 35 | #include "elf_utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 36 | #include "elf_writer.h" |
| 37 | #include "gc/accounting/card_table-inl.h" |
| 38 | #include "gc/accounting/heap_bitmap.h" |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 39 | #include "gc/accounting/space_bitmap-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 40 | #include "gc/heap.h" |
| 41 | #include "gc/space/large_object_space.h" |
| 42 | #include "gc/space/space-inl.h" |
| 43 | #include "globals.h" |
| 44 | #include "image.h" |
| 45 | #include "intern_table.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 46 | #include "linear_alloc.h" |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 47 | #include "lock_word.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 48 | #include "mirror/abstract_method.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 49 | #include "mirror/array-inl.h" |
| 50 | #include "mirror/class-inl.h" |
| 51 | #include "mirror/class_loader.h" |
| 52 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 53 | #include "mirror/method.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 54 | #include "mirror/object-inl.h" |
| 55 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 56 | #include "mirror/string-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 57 | #include "oat.h" |
| 58 | #include "oat_file.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 59 | #include "oat_file_manager.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 60 | #include "runtime.h" |
| 61 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 62 | #include "handle_scope-inl.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 63 | #include "utils/dex_cache_arrays_layout-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 64 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 65 | using ::art::mirror::Class; |
| 66 | using ::art::mirror::DexCache; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 67 | using ::art::mirror::Object; |
| 68 | using ::art::mirror::ObjectArray; |
| 69 | using ::art::mirror::String; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 70 | |
| 71 | namespace art { |
| 72 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 73 | // Separate objects into multiple bins to optimize dirty memory use. |
| 74 | static constexpr bool kBinObjects = true; |
| 75 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 76 | // Return true if an object is already in an image space. |
| 77 | bool ImageWriter::IsInBootImage(const void* obj) const { |
| 78 | if (!compile_app_image_) { |
| 79 | DCHECK(boot_image_space_ == nullptr); |
| 80 | return false; |
| 81 | } |
| 82 | const uint8_t* image_begin = boot_image_space_->Begin(); |
| 83 | // Real image end including ArtMethods and ArtField sections. |
| 84 | const uint8_t* image_end = image_begin + boot_image_space_->GetImageHeader().GetImageSize(); |
| 85 | return image_begin <= obj && obj < image_end; |
| 86 | } |
| 87 | |
| 88 | bool ImageWriter::IsInBootOatFile(const void* ptr) const { |
| 89 | if (!compile_app_image_) { |
| 90 | DCHECK(boot_image_space_ == nullptr); |
| 91 | return false; |
| 92 | } |
| 93 | const ImageHeader& image_header = boot_image_space_->GetImageHeader(); |
| 94 | return image_header.GetOatFileBegin() <= ptr && ptr < image_header.GetOatFileEnd(); |
| 95 | } |
| 96 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 97 | static void CheckNoDexObjectsCallback(Object* obj, void* arg ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 98 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 99 | Class* klass = obj->GetClass(); |
| 100 | CHECK_NE(PrettyClass(klass), "com.android.dex.Dex"); |
| 101 | } |
| 102 | |
| 103 | static void CheckNoDexObjects() { |
| 104 | ScopedObjectAccess soa(Thread::Current()); |
| 105 | Runtime::Current()->GetHeap()->VisitObjects(CheckNoDexObjectsCallback, nullptr); |
| 106 | } |
| 107 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 108 | bool ImageWriter::PrepareImageAddressSpace() { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 109 | target_ptr_size_ = InstructionSetPointerSize(compiler_driver_.GetInstructionSet()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 110 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 111 | // Cache boot image space. |
| 112 | for (gc::space::ContinuousSpace* space : heap->GetContinuousSpaces()) { |
| 113 | if (space->IsImageSpace()) { |
| 114 | CHECK(compile_app_image_); |
| 115 | CHECK(boot_image_space_ == nullptr) << "Multiple image spaces"; |
| 116 | boot_image_space_ = space->AsImageSpace(); |
| 117 | } |
| 118 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 119 | { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 120 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 121 | PruneNonImageClasses(); // Remove junk |
| 122 | ComputeLazyFieldsForImageClasses(); // Add useful information |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 123 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 124 | heap->CollectGarbage(false); // Remove garbage. |
| 125 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 126 | // Dex caches must not have their dex fields set in the image. These are memory buffers of mapped |
| 127 | // dex files. |
| 128 | // |
| 129 | // We may open them in the unstarted-runtime code for class metadata. Their fields should all be |
| 130 | // reset in PruneNonImageClasses and the objects reclaimed in the GC. Make sure that's actually |
| 131 | // true. |
| 132 | if (kIsDebugBuild) { |
| 133 | CheckNoDexObjects(); |
| 134 | } |
| 135 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 136 | if (kIsDebugBuild) { |
| 137 | ScopedObjectAccess soa(Thread::Current()); |
| 138 | CheckNonImageClassesRemoved(); |
| 139 | } |
| 140 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 141 | { |
| 142 | ScopedObjectAccess soa(Thread::Current()); |
| 143 | CalculateNewObjectOffsets(); |
| 144 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 145 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 146 | // This needs to happen after CalculateNewObjectOffsets since it relies on intern_table_bytes_ and |
| 147 | // bin size sums being calculated. |
| 148 | if (!AllocMemory()) { |
| 149 | return false; |
| 150 | } |
| 151 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 152 | return true; |
| 153 | } |
| 154 | |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 155 | bool ImageWriter::Write(int image_fd, |
| 156 | const std::string& image_filename, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 157 | const std::string& oat_filename, |
| 158 | const std::string& oat_location) { |
| 159 | CHECK(!image_filename.empty()); |
| 160 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 161 | std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str())); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 162 | if (oat_file.get() == nullptr) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 163 | PLOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 164 | return false; |
| 165 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 166 | std::string error_msg; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 167 | oat_file_ = OatFile::OpenReadable(oat_file.get(), oat_location, nullptr, &error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 168 | if (oat_file_ == nullptr) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 169 | PLOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 170 | << ": " << error_msg; |
Andreas Gampe | 0b7fcf9 | 2015-03-13 16:54:54 -0700 | [diff] [blame] | 171 | oat_file->Erase(); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 172 | return false; |
| 173 | } |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 174 | Runtime::Current()->GetOatFileManager().RegisterOatFile( |
| 175 | std::unique_ptr<const OatFile>(oat_file_)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 176 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 177 | const OatHeader& oat_header = oat_file_->GetOatHeader(); |
| 178 | oat_address_offsets_[kOatAddressInterpreterToInterpreterBridge] = |
| 179 | oat_header.GetInterpreterToInterpreterBridgeOffset(); |
| 180 | oat_address_offsets_[kOatAddressInterpreterToCompiledCodeBridge] = |
| 181 | oat_header.GetInterpreterToCompiledCodeBridgeOffset(); |
| 182 | oat_address_offsets_[kOatAddressJNIDlsymLookup] = |
| 183 | oat_header.GetJniDlsymLookupOffset(); |
| 184 | oat_address_offsets_[kOatAddressQuickGenericJNITrampoline] = |
| 185 | oat_header.GetQuickGenericJniTrampolineOffset(); |
| 186 | oat_address_offsets_[kOatAddressQuickIMTConflictTrampoline] = |
| 187 | oat_header.GetQuickImtConflictTrampolineOffset(); |
| 188 | oat_address_offsets_[kOatAddressQuickResolutionTrampoline] = |
| 189 | oat_header.GetQuickResolutionTrampolineOffset(); |
| 190 | oat_address_offsets_[kOatAddressQuickToInterpreterBridge] = |
| 191 | oat_header.GetQuickToInterpreterBridgeOffset(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 192 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 193 | size_t oat_loaded_size = 0; |
| 194 | size_t oat_data_offset = 0; |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 195 | ElfWriter::GetOatElfInformation(oat_file.get(), &oat_loaded_size, &oat_data_offset); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 196 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 197 | { |
| 198 | ScopedObjectAccess soa(Thread::Current()); |
| 199 | CreateHeader(oat_loaded_size, oat_data_offset); |
| 200 | CopyAndFixupNativeData(); |
| 201 | // TODO: heap validation can't handle these fix up passes. |
| 202 | Runtime::Current()->GetHeap()->DisableObjectValidation(); |
| 203 | CopyAndFixupObjects(); |
| 204 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 205 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 206 | SetOatChecksumFromElfFile(oat_file.get()); |
| 207 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 208 | if (oat_file->FlushCloseOrErase() != 0) { |
| 209 | LOG(ERROR) << "Failed to flush and close oat file " << oat_filename << " for " << oat_location; |
| 210 | return false; |
| 211 | } |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 212 | std::unique_ptr<File> image_file; |
| 213 | if (image_fd != kInvalidImageFd) { |
| 214 | image_file.reset(new File(image_fd, image_filename, unix_file::kCheckSafeUsage)); |
| 215 | } else { |
| 216 | image_file.reset(OS::CreateEmptyFile(image_filename.c_str())); |
| 217 | } |
| 218 | if (image_file == nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 219 | LOG(ERROR) << "Failed to open image file " << image_filename; |
| 220 | return false; |
| 221 | } |
| 222 | if (fchmod(image_file->Fd(), 0644) != 0) { |
| 223 | PLOG(ERROR) << "Failed to make image file world readable: " << image_filename; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 224 | image_file->Erase(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 225 | return EXIT_FAILURE; |
| 226 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 227 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 228 | // Write out the image + fields + methods. |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 229 | ImageHeader* const image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 230 | const auto write_count = image_header->GetImageSize(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 231 | if (!image_file->WriteFully(image_->Begin(), write_count)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 232 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 233 | image_file->Erase(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 234 | return false; |
| 235 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 236 | |
| 237 | // Write out the image bitmap at the page aligned start of the image end. |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 238 | const ImageSection& bitmap_section = image_header->GetImageSection( |
| 239 | ImageHeader::kSectionImageBitmap); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 240 | CHECK_ALIGNED(bitmap_section.Offset(), kPageSize); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 241 | if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 242 | bitmap_section.Size(), bitmap_section.Offset())) { |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 243 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 244 | image_file->Erase(); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 245 | return false; |
| 246 | } |
| 247 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 248 | CHECK_EQ(bitmap_section.End(), static_cast<size_t>(image_file->GetLength())); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 249 | if (image_file->FlushCloseOrErase() != 0) { |
| 250 | PLOG(ERROR) << "Failed to flush and close image file " << image_filename; |
| 251 | return false; |
| 252 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 256 | void ImageWriter::SetImageOffset(mirror::Object* object, size_t offset) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 257 | DCHECK(object != nullptr); |
| 258 | DCHECK_NE(offset, 0U); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 259 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 260 | // 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] | 261 | object->SetLockWord(LockWord::FromForwardingAddress(offset), false); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 262 | DCHECK_EQ(object->GetLockWord(false).ReadBarrierState(), 0u); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 263 | DCHECK(IsImageOffsetAssigned(object)); |
| 264 | } |
| 265 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 266 | void ImageWriter::UpdateImageOffset(mirror::Object* obj, uintptr_t offset) { |
| 267 | DCHECK(IsImageOffsetAssigned(obj)) << obj << " " << offset; |
| 268 | obj->SetLockWord(LockWord::FromForwardingAddress(offset), false); |
| 269 | DCHECK_EQ(obj->GetLockWord(false).ReadBarrierState(), 0u); |
| 270 | } |
| 271 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 272 | void ImageWriter::AssignImageOffset(mirror::Object* object, ImageWriter::BinSlot bin_slot) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 273 | DCHECK(object != nullptr); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 274 | DCHECK_NE(image_objects_offset_begin_, 0u); |
| 275 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 276 | size_t bin_slot_offset = bin_slot_offsets_[bin_slot.GetBin()]; |
| 277 | size_t new_offset = bin_slot_offset + bin_slot.GetIndex(); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 278 | DCHECK_ALIGNED(new_offset, kObjectAlignment); |
| 279 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 280 | SetImageOffset(object, new_offset); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 281 | DCHECK_LT(new_offset, image_end_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 284 | bool ImageWriter::IsImageOffsetAssigned(mirror::Object* object) const { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 285 | // 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] | 286 | DCHECK(object != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 287 | return object->GetLockWord(false).GetState() == LockWord::kForwardingAddress; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 290 | size_t ImageWriter::GetImageOffset(mirror::Object* object) const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 291 | DCHECK(object != nullptr); |
| 292 | DCHECK(IsImageOffsetAssigned(object)); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 293 | LockWord lock_word = object->GetLockWord(false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 294 | size_t offset = lock_word.ForwardingAddress(); |
| 295 | DCHECK_LT(offset, image_end_); |
| 296 | return offset; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 299 | void ImageWriter::SetImageBinSlot(mirror::Object* object, BinSlot bin_slot) { |
| 300 | DCHECK(object != nullptr); |
| 301 | DCHECK(!IsImageOffsetAssigned(object)); |
| 302 | DCHECK(!IsImageBinSlotAssigned(object)); |
| 303 | |
| 304 | // Before we stomp over the lock word, save the hash code for later. |
| 305 | Monitor::Deflate(Thread::Current(), object);; |
| 306 | LockWord lw(object->GetLockWord(false)); |
| 307 | switch (lw.GetState()) { |
| 308 | case LockWord::kFatLocked: { |
| 309 | LOG(FATAL) << "Fat locked object " << object << " found during object copy"; |
| 310 | break; |
| 311 | } |
| 312 | case LockWord::kThinLocked: { |
| 313 | LOG(FATAL) << "Thin locked object " << object << " found during object copy"; |
| 314 | break; |
| 315 | } |
| 316 | case LockWord::kUnlocked: |
| 317 | // No hash, don't need to save it. |
| 318 | break; |
| 319 | case LockWord::kHashCode: |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 320 | DCHECK(saved_hashcode_map_.find(object) == saved_hashcode_map_.end()); |
| 321 | saved_hashcode_map_.emplace(object, lw.GetHashCode()); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 322 | break; |
| 323 | default: |
| 324 | LOG(FATAL) << "Unreachable."; |
| 325 | UNREACHABLE(); |
| 326 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 327 | object->SetLockWord(LockWord::FromForwardingAddress(bin_slot.Uint32Value()), false); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 328 | DCHECK_EQ(object->GetLockWord(false).ReadBarrierState(), 0u); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 329 | DCHECK(IsImageBinSlotAssigned(object)); |
| 330 | } |
| 331 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 332 | void ImageWriter::PrepareDexCacheArraySlots() { |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 333 | // Prepare dex cache array starts based on the ordering specified in the CompilerDriver. |
| 334 | uint32_t size = 0u; |
| 335 | for (const DexFile* dex_file : compiler_driver_.GetDexFilesForOatFile()) { |
| 336 | dex_cache_array_starts_.Put(dex_file, size); |
| 337 | DexCacheArraysLayout layout(target_ptr_size_, dex_file); |
| 338 | size += layout.Size(); |
| 339 | } |
| 340 | // Set the slot size early to avoid DCHECK() failures in IsImageBinSlotAssigned() |
| 341 | // when AssignImageBinSlot() assigns their indexes out or order. |
| 342 | bin_slot_sizes_[kBinDexCacheArray] = size; |
| 343 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 344 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 345 | Thread* const self = Thread::Current(); |
| 346 | ReaderMutexLock mu(self, *class_linker->DexLock()); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 347 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 348 | mirror::DexCache* dex_cache = |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 349 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 350 | if (dex_cache == nullptr || IsInBootImage(dex_cache)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 351 | continue; |
| 352 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 353 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 354 | DexCacheArraysLayout layout(target_ptr_size_, dex_file); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 355 | DCHECK(layout.Valid()); |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 356 | uint32_t start = dex_cache_array_starts_.Get(dex_file); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 357 | DCHECK_EQ(dex_file->NumTypeIds() != 0u, dex_cache->GetResolvedTypes() != nullptr); |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 358 | AddDexCacheArrayRelocation(dex_cache->GetResolvedTypes(), start + layout.TypesOffset()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 359 | DCHECK_EQ(dex_file->NumMethodIds() != 0u, dex_cache->GetResolvedMethods() != nullptr); |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 360 | AddDexCacheArrayRelocation(dex_cache->GetResolvedMethods(), start + layout.MethodsOffset()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 361 | DCHECK_EQ(dex_file->NumFieldIds() != 0u, dex_cache->GetResolvedFields() != nullptr); |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 362 | AddDexCacheArrayRelocation(dex_cache->GetResolvedFields(), start + layout.FieldsOffset()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 363 | DCHECK_EQ(dex_file->NumStringIds() != 0u, dex_cache->GetStrings() != nullptr); |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 364 | AddDexCacheArrayRelocation(dex_cache->GetStrings(), start + layout.StringsOffset()); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 365 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 368 | void ImageWriter::AddDexCacheArrayRelocation(void* array, size_t offset) { |
| 369 | if (array != nullptr) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 370 | DCHECK(!IsInBootImage(array)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 371 | native_object_relocations_.emplace( |
| 372 | array, |
| 373 | NativeObjectRelocation { offset, kNativeObjectRelocationTypeDexCacheArray }); |
| 374 | } |
| 375 | } |
| 376 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 377 | void ImageWriter::AddMethodPointerArray(mirror::PointerArray* arr) { |
| 378 | DCHECK(arr != nullptr); |
| 379 | if (kIsDebugBuild) { |
| 380 | for (size_t i = 0, len = arr->GetLength(); i < len; i++) { |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 381 | ArtMethod* method = arr->GetElementPtrSize<ArtMethod*>(i, target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 382 | if (method != nullptr && !method->IsRuntimeMethod()) { |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 383 | mirror::Class* klass = method->GetDeclaringClass(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 384 | CHECK(klass == nullptr || KeepClass(klass)) |
| 385 | << PrettyClass(klass) << " should be a kept class"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | } |
| 389 | // kBinArtMethodClean picked arbitrarily, just required to differentiate between ArtFields and |
| 390 | // ArtMethods. |
| 391 | pointer_arrays_.emplace(arr, kBinArtMethodClean); |
| 392 | } |
| 393 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 394 | void ImageWriter::AssignImageBinSlot(mirror::Object* object) { |
| 395 | DCHECK(object != nullptr); |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 396 | size_t object_size = object->SizeOf(); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 397 | |
| 398 | // The magic happens here. We segregate objects into different bins based |
| 399 | // on how likely they are to get dirty at runtime. |
| 400 | // |
| 401 | // Likely-to-dirty objects get packed together into the same bin so that |
| 402 | // at runtime their page dirtiness ratio (how many dirty objects a page has) is |
| 403 | // maximized. |
| 404 | // |
| 405 | // This means more pages will stay either clean or shared dirty (with zygote) and |
| 406 | // the app will use less of its own (private) memory. |
| 407 | Bin bin = kBinRegular; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 408 | size_t current_offset = 0u; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 409 | |
| 410 | if (kBinObjects) { |
| 411 | // |
| 412 | // Changing the bin of an object is purely a memory-use tuning. |
| 413 | // It has no change on runtime correctness. |
| 414 | // |
| 415 | // Memory analysis has determined that the following types of objects get dirtied |
| 416 | // the most: |
| 417 | // |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 418 | // * Dex cache arrays are stored in a special bin. The arrays for each dex cache have |
| 419 | // a fixed layout which helps improve generated code (using PC-relative addressing), |
| 420 | // so we pre-calculate their offsets separately in PrepareDexCacheArraySlots(). |
| 421 | // Since these arrays are huge, most pages do not overlap other objects and it's not |
| 422 | // 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] | 423 | // special PC-relative addressing, we arbitrarily keep them at the end. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 424 | // * Class'es which are verified [their clinit runs only at runtime] |
| 425 | // - classes in general [because their static fields get overwritten] |
| 426 | // - initialized classes with all-final statics are unlikely to be ever dirty, |
| 427 | // so bin them separately |
| 428 | // * Art Methods that are: |
| 429 | // - native [their native entry point is not looked up until runtime] |
| 430 | // - have declaring classes that aren't initialized |
| 431 | // [their interpreter/quick entry points are trampolines until the class |
| 432 | // becomes initialized] |
| 433 | // |
| 434 | // We also assume the following objects get dirtied either never or extremely rarely: |
| 435 | // * Strings (they are immutable) |
| 436 | // * Art methods that aren't native and have initialized declared classes |
| 437 | // |
| 438 | // We assume that "regular" bin objects are highly unlikely to become dirtied, |
| 439 | // so packing them together will not result in a noticeably tighter dirty-to-clean ratio. |
| 440 | // |
| 441 | if (object->IsClass()) { |
| 442 | bin = kBinClassVerified; |
| 443 | mirror::Class* klass = object->AsClass(); |
| 444 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 445 | // Add non-embedded vtable to the pointer array table if there is one. |
| 446 | auto* vtable = klass->GetVTable(); |
| 447 | if (vtable != nullptr) { |
| 448 | AddMethodPointerArray(vtable); |
| 449 | } |
| 450 | auto* iftable = klass->GetIfTable(); |
| 451 | if (iftable != nullptr) { |
| 452 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 453 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 454 | AddMethodPointerArray(iftable->GetMethodArray(i)); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 459 | if (klass->GetStatus() == Class::kStatusInitialized) { |
| 460 | bin = kBinClassInitialized; |
| 461 | |
| 462 | // If the class's static fields are all final, put it into a separate bin |
| 463 | // since it's very likely it will stay clean. |
| 464 | uint32_t num_static_fields = klass->NumStaticFields(); |
| 465 | if (num_static_fields == 0) { |
| 466 | bin = kBinClassInitializedFinalStatics; |
| 467 | } else { |
| 468 | // Maybe all the statics are final? |
| 469 | bool all_final = true; |
| 470 | for (uint32_t i = 0; i < num_static_fields; ++i) { |
| 471 | ArtField* field = klass->GetStaticField(i); |
| 472 | if (!field->IsFinal()) { |
| 473 | all_final = false; |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if (all_final) { |
| 479 | bin = kBinClassInitializedFinalStatics; |
| 480 | } |
| 481 | } |
| 482 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 483 | } else if (object->GetClass<kVerifyNone>()->IsStringClass()) { |
| 484 | bin = kBinString; // Strings are almost always immutable (except for object header). |
| 485 | } // else bin = kBinRegular |
| 486 | } |
| 487 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 488 | size_t offset_delta = RoundUp(object_size, kObjectAlignment); // 64-bit alignment |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 489 | current_offset = bin_slot_sizes_[bin]; // How many bytes the current bin is at (aligned). |
| 490 | // Move the current bin size up to accomodate the object we just assigned a bin slot. |
| 491 | bin_slot_sizes_[bin] += offset_delta; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 492 | |
| 493 | BinSlot new_bin_slot(bin, current_offset); |
| 494 | SetImageBinSlot(object, new_bin_slot); |
| 495 | |
| 496 | ++bin_slot_count_[bin]; |
| 497 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 498 | // Grow the image closer to the end by the object we just assigned. |
| 499 | image_end_ += offset_delta; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 502 | bool ImageWriter::WillMethodBeDirty(ArtMethod* m) const { |
| 503 | if (m->IsNative()) { |
| 504 | return true; |
| 505 | } |
| 506 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 507 | // Initialized is highly unlikely to dirty since there's no entry points to mutate. |
| 508 | return declaring_class == nullptr || declaring_class->GetStatus() != Class::kStatusInitialized; |
| 509 | } |
| 510 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 511 | bool ImageWriter::IsImageBinSlotAssigned(mirror::Object* object) const { |
| 512 | DCHECK(object != nullptr); |
| 513 | |
| 514 | // We always stash the bin slot into a lockword, in the 'forwarding address' state. |
| 515 | // If it's in some other state, then we haven't yet assigned an image bin slot. |
| 516 | if (object->GetLockWord(false).GetState() != LockWord::kForwardingAddress) { |
| 517 | return false; |
| 518 | } else if (kIsDebugBuild) { |
| 519 | LockWord lock_word = object->GetLockWord(false); |
| 520 | size_t offset = lock_word.ForwardingAddress(); |
| 521 | BinSlot bin_slot(offset); |
| 522 | DCHECK_LT(bin_slot.GetIndex(), bin_slot_sizes_[bin_slot.GetBin()]) |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 523 | << "bin slot offset should not exceed the size of that bin"; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 524 | } |
| 525 | return true; |
| 526 | } |
| 527 | |
| 528 | ImageWriter::BinSlot ImageWriter::GetImageBinSlot(mirror::Object* object) const { |
| 529 | DCHECK(object != nullptr); |
| 530 | DCHECK(IsImageBinSlotAssigned(object)); |
| 531 | |
| 532 | LockWord lock_word = object->GetLockWord(false); |
| 533 | size_t offset = lock_word.ForwardingAddress(); // TODO: ForwardingAddress should be uint32_t |
| 534 | DCHECK_LE(offset, std::numeric_limits<uint32_t>::max()); |
| 535 | |
| 536 | BinSlot bin_slot(static_cast<uint32_t>(offset)); |
| 537 | DCHECK_LT(bin_slot.GetIndex(), bin_slot_sizes_[bin_slot.GetBin()]); |
| 538 | |
| 539 | return bin_slot; |
| 540 | } |
| 541 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 542 | bool ImageWriter::AllocMemory() { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 543 | const size_t length = RoundUp(image_objects_offset_begin_ + GetBinSizeSum() + intern_table_bytes_, |
| 544 | kPageSize); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 545 | std::string error_msg; |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 546 | image_.reset(MemMap::MapAnonymous("image writer image", |
| 547 | nullptr, |
| 548 | length, |
| 549 | PROT_READ | PROT_WRITE, |
| 550 | false, |
| 551 | false, |
| 552 | &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 553 | if (UNLIKELY(image_.get() == nullptr)) { |
| 554 | LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 555 | return false; |
| 556 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 557 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 558 | // Create the image bitmap, only needs to cover mirror object section which is up to image_end_. |
| 559 | CHECK_LE(image_end_, length); |
| 560 | image_bitmap_.reset(gc::accounting::ContinuousSpaceBitmap::Create( |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 561 | "image bitmap", |
| 562 | image_->Begin(), |
| 563 | RoundUp(image_end_, kPageSize))); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 564 | if (image_bitmap_.get() == nullptr) { |
| 565 | LOG(ERROR) << "Failed to allocate memory for image bitmap"; |
| 566 | return false; |
| 567 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 568 | return true; |
| 569 | } |
| 570 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 571 | class ComputeLazyFieldsForClassesVisitor : public ClassVisitor { |
| 572 | public: |
| 573 | bool Visit(Class* c) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 574 | StackHandleScope<1> hs(Thread::Current()); |
| 575 | mirror::Class::ComputeName(hs.NewHandle(c)); |
| 576 | return true; |
| 577 | } |
| 578 | }; |
| 579 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 580 | void ImageWriter::ComputeLazyFieldsForImageClasses() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 581 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 582 | ComputeLazyFieldsForClassesVisitor visitor; |
| 583 | class_linker->VisitClassesWithoutClassesLock(&visitor); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 586 | static bool IsBootClassLoaderClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 587 | return klass->GetClassLoader() == nullptr; |
| 588 | } |
| 589 | |
| 590 | bool ImageWriter::IsBootClassLoaderNonImageClass(mirror::Class* klass) { |
| 591 | return IsBootClassLoaderClass(klass) && !IsInBootImage(klass); |
| 592 | } |
| 593 | |
| 594 | bool ImageWriter::ContainsBootClassLoaderNonImageClass(mirror::Class* klass) { |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 595 | bool early_exit = false; |
| 596 | std::unordered_set<mirror::Class*> visited; |
| 597 | return ContainsBootClassLoaderNonImageClassInternal(klass, &early_exit, &visited); |
| 598 | } |
| 599 | |
| 600 | bool ImageWriter::ContainsBootClassLoaderNonImageClassInternal( |
| 601 | mirror::Class* klass, |
| 602 | bool* early_exit, |
| 603 | std::unordered_set<mirror::Class*>* visited) { |
| 604 | DCHECK(early_exit != nullptr); |
| 605 | DCHECK(visited != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 606 | if (klass == nullptr) { |
| 607 | return false; |
| 608 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 609 | auto found = prune_class_memo_.find(klass); |
| 610 | if (found != prune_class_memo_.end()) { |
| 611 | // Already computed, return the found value. |
| 612 | return found->second; |
| 613 | } |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 614 | // Circular dependencies, return false but do not store the result in the memoization table. |
| 615 | if (visited->find(klass) != visited->end()) { |
| 616 | *early_exit = true; |
| 617 | return false; |
| 618 | } |
| 619 | visited->emplace(klass); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 620 | bool result = IsBootClassLoaderNonImageClass(klass); |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 621 | bool my_early_exit = false; // Only for ourselves, ignore caller. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 622 | if (!result) { |
| 623 | // Check interfaces since these wont be visited through VisitReferences.) |
| 624 | mirror::IfTable* if_table = klass->GetIfTable(); |
| 625 | for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) { |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 626 | result = result || ContainsBootClassLoaderNonImageClassInternal( |
| 627 | if_table->GetInterface(i), |
| 628 | &my_early_exit, |
| 629 | visited); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | // Check static fields and their classes. |
| 633 | size_t num_static_fields = klass->NumReferenceStaticFields(); |
| 634 | if (num_static_fields != 0 && klass->IsResolved()) { |
| 635 | // Presumably GC can happen when we are cross compiling, it should not cause performance |
| 636 | // problems to do pointer size logic. |
| 637 | MemberOffset field_offset = klass->GetFirstReferenceStaticFieldOffset( |
| 638 | Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 639 | for (size_t i = 0u; i < num_static_fields; ++i) { |
| 640 | mirror::Object* ref = klass->GetFieldObject<mirror::Object>(field_offset); |
| 641 | if (ref != nullptr) { |
| 642 | if (ref->IsClass()) { |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 643 | result = result || |
| 644 | ContainsBootClassLoaderNonImageClassInternal( |
| 645 | ref->AsClass(), |
| 646 | &my_early_exit, |
| 647 | visited); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 648 | } |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 649 | result = result || |
| 650 | ContainsBootClassLoaderNonImageClassInternal( |
| 651 | ref->GetClass(), |
| 652 | &my_early_exit, |
| 653 | visited); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 654 | } |
| 655 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 656 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 657 | } |
| 658 | } |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 659 | result = result || |
| 660 | ContainsBootClassLoaderNonImageClassInternal( |
| 661 | klass->GetSuperClass(), |
| 662 | &my_early_exit, |
| 663 | visited); |
| 664 | // Erase the element we stored earlier since we are exiting the function. |
| 665 | auto it = visited->find(klass); |
| 666 | DCHECK(it != visited->end()); |
| 667 | visited->erase(it); |
| 668 | // Only store result if it is true or none of the calls early exited due to circular |
| 669 | // dependencies. If visited is empty then we are the root caller, in this case the cycle was in |
| 670 | // a child call and we can remember the result. |
| 671 | if (result == true || !my_early_exit || visited->empty()) { |
| 672 | prune_class_memo_[klass] = result; |
| 673 | } |
| 674 | *early_exit |= my_early_exit; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 675 | return result; |
| 676 | } |
| 677 | |
| 678 | bool ImageWriter::KeepClass(Class* klass) { |
| 679 | if (klass == nullptr) { |
| 680 | return false; |
| 681 | } |
| 682 | if (compile_app_image_) { |
| 683 | // For app images, we need to prune boot loader classes that are not in the boot image since |
| 684 | // these may have already been loaded when the app image is loaded. |
| 685 | return !ContainsBootClassLoaderNonImageClass(klass); |
| 686 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 687 | std::string temp; |
| 688 | return compiler_driver_.IsImageClass(klass->GetDescriptor(&temp)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 691 | class NonImageClassesVisitor : public ClassVisitor { |
| 692 | public: |
| 693 | explicit NonImageClassesVisitor(ImageWriter* image_writer) : image_writer_(image_writer) {} |
| 694 | |
| 695 | bool Visit(Class* klass) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 696 | if (!image_writer_->KeepClass(klass)) { |
| 697 | classes_to_prune_.insert(klass); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 698 | } |
| 699 | return true; |
| 700 | } |
| 701 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 702 | std::unordered_set<mirror::Class*> classes_to_prune_; |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 703 | ImageWriter* const image_writer_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 704 | }; |
| 705 | |
| 706 | void ImageWriter::PruneNonImageClasses() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 707 | Runtime* runtime = Runtime::Current(); |
| 708 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 709 | Thread* self = Thread::Current(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 710 | |
| 711 | // Make a list of classes we would like to prune. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 712 | NonImageClassesVisitor visitor(this); |
| 713 | class_linker->VisitClasses(&visitor); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 714 | |
| 715 | // Remove the undesired classes from the class roots. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 716 | for (mirror::Class* klass : visitor.classes_to_prune_) { |
| 717 | std::string temp; |
| 718 | const char* name = klass->GetDescriptor(&temp); |
| 719 | VLOG(compiler) << "Pruning class " << name; |
| 720 | if (!compile_app_image_) { |
| 721 | DCHECK(IsBootClassLoaderClass(klass)); |
| 722 | } |
| 723 | bool result = class_linker->RemoveClass(name, klass->GetClassLoader()); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 724 | DCHECK(result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | // Clear references to removed classes from the DexCaches. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 728 | ArtMethod* resolution_method = runtime->GetResolutionMethod(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 729 | |
| 730 | ScopedAssertNoThreadSuspension sa(self, __FUNCTION__); |
| 731 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); // For ClassInClassTable |
| 732 | ReaderMutexLock mu2(self, *class_linker->DexLock()); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 733 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 734 | mirror::DexCache* dex_cache = down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 735 | if (dex_cache == nullptr) { |
| 736 | continue; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 737 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 738 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
| 739 | Class* klass = dex_cache->GetResolvedType(i); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 740 | if (klass != nullptr && !KeepClass(klass)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 741 | dex_cache->SetResolvedType(i, nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 744 | ArtMethod** resolved_methods = dex_cache->GetResolvedMethods(); |
| 745 | for (size_t i = 0, num = dex_cache->NumResolvedMethods(); i != num; ++i) { |
| 746 | ArtMethod* method = |
| 747 | mirror::DexCache::GetElementPtrSize(resolved_methods, i, target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 748 | if (method != nullptr) { |
| 749 | auto* declaring_class = method->GetDeclaringClass(); |
| 750 | // Miranda methods may be held live by a class which was not an image class but have a |
| 751 | // declaring class which is an image class. Set it to the resolution method to be safe and |
| 752 | // prevent dangling pointers. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 753 | if (method->IsMiranda() || !KeepClass(declaring_class)) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 754 | mirror::DexCache::SetElementPtrSize(resolved_methods, |
| 755 | i, |
| 756 | resolution_method, |
| 757 | target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 758 | } else { |
| 759 | // Check that the class is still in the classes table. |
| 760 | DCHECK(class_linker->ClassInClassTable(declaring_class)) << "Class " |
| 761 | << PrettyClass(declaring_class) << " not in class linker table"; |
| 762 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 766 | ArtField* field = dex_cache->GetResolvedField(i, target_ptr_size_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 767 | if (field != nullptr && !KeepClass(field->GetDeclaringClass())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 768 | dex_cache->SetResolvedField(i, nullptr, target_ptr_size_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 771 | // Clean the dex field. It might have been populated during the initialization phase, but |
| 772 | // contains data only valid during a real run. |
| 773 | dex_cache->SetFieldObject<false>(mirror::DexCache::DexOffset(), nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 774 | } |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 775 | |
| 776 | // Drop the array class cache in the ClassLinker, as these are roots holding those classes live. |
| 777 | class_linker->DropFindArrayClassCache(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 778 | |
| 779 | // Clear to save RAM. |
| 780 | prune_class_memo_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 783 | void ImageWriter::CheckNonImageClassesRemoved() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 784 | if (compiler_driver_.GetImageClasses() != nullptr) { |
| 785 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 786 | heap->VisitObjects(CheckNonImageClassesRemovedCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 787 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) { |
| 791 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 792 | if (obj->IsClass() && !image_writer->IsInBootImage(obj)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 793 | Class* klass = obj->AsClass(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 794 | if (!image_writer->KeepClass(klass)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 795 | image_writer->DumpImageClasses(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 796 | std::string temp; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 797 | CHECK(image_writer->KeepClass(klass)) << klass->GetDescriptor(&temp) |
| 798 | << " " << PrettyDescriptor(klass); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 799 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
| 803 | void ImageWriter::DumpImageClasses() { |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 804 | auto image_classes = compiler_driver_.GetImageClasses(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 805 | CHECK(image_classes != nullptr); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 806 | for (const std::string& image_class : *image_classes) { |
| 807 | LOG(INFO) << " " << image_class; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 811 | void ImageWriter::CalculateObjectBinSlots(Object* obj) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 812 | DCHECK(obj != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 813 | // if it is a string, we want to intern it if its not interned. |
| 814 | if (obj->GetClass()->IsStringClass()) { |
| 815 | // we must be an interned string that was forward referenced and already assigned |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 816 | if (IsImageBinSlotAssigned(obj)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 817 | DCHECK_EQ(obj, obj->AsString()->Intern()); |
| 818 | return; |
| 819 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 820 | // InternImageString allows us to intern while holding the heap bitmap lock. This is safe since |
| 821 | // we are guaranteed to not have GC during image writing. |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 822 | mirror::String* const interned = Runtime::Current()->GetInternTable()->InternStrongImageString( |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 823 | obj->AsString()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 824 | if (obj != interned) { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 825 | if (!IsImageBinSlotAssigned(interned)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 826 | // interned obj is after us, allocate its location early |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 827 | AssignImageBinSlot(interned); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 828 | } |
| 829 | // point those looking for this object to the interned version. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 830 | SetImageBinSlot(obj, GetImageBinSlot(interned)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 831 | return; |
| 832 | } |
| 833 | // else (obj == interned), nothing to do but fall through to the normal case |
| 834 | } |
| 835 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 836 | AssignImageBinSlot(obj); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | ObjectArray<Object>* ImageWriter::CreateImageRoots() const { |
| 840 | Runtime* runtime = Runtime::Current(); |
| 841 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 842 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 843 | StackHandleScope<3> hs(self); |
| 844 | Handle<Class> object_array_class(hs.NewHandle( |
| 845 | class_linker->FindSystemClass(self, "[Ljava/lang/Object;"))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 846 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 847 | // build an Object[] of all the DexCaches used in the source_space_. |
| 848 | // Since we can't hold the dex lock when allocating the dex_caches |
| 849 | // ObjectArray, we lock the dex lock twice, first to get the number |
| 850 | // of dex caches first and then lock it again to copy the dex |
| 851 | // caches. We check that the number of dex caches does not change. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 852 | size_t dex_cache_count = 0; |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 853 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 854 | ReaderMutexLock mu(self, *class_linker->DexLock()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 855 | // Count number of dex caches not in the boot image. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 856 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 857 | mirror::DexCache* dex_cache = |
| 858 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 859 | dex_cache_count += IsInBootImage(dex_cache) ? 0u : 1u; |
| 860 | } |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 861 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 862 | Handle<ObjectArray<Object>> dex_caches( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 863 | hs.NewHandle(ObjectArray<Object>::Alloc(self, object_array_class.Get(), dex_cache_count))); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 864 | CHECK(dex_caches.Get() != nullptr) << "Failed to allocate a dex cache array."; |
| 865 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 866 | ReaderMutexLock mu(self, *class_linker->DexLock()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 867 | size_t non_image_dex_caches = 0; |
| 868 | // Re-count number of non image dex caches. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 869 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 870 | mirror::DexCache* dex_cache = |
| 871 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 872 | non_image_dex_caches += IsInBootImage(dex_cache) ? 0u : 1u; |
| 873 | } |
| 874 | CHECK_EQ(dex_cache_count, non_image_dex_caches) |
| 875 | << "The number of non-image dex caches changed."; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 876 | size_t i = 0; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 877 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 878 | mirror::DexCache* dex_cache = |
| 879 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 880 | if (!IsInBootImage(dex_cache)) { |
| 881 | dex_caches->Set<false>(i, dex_cache); |
| 882 | ++i; |
| 883 | } |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 884 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | // build an Object[] of the roots needed to restore the runtime |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 888 | auto image_roots(hs.NewHandle( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 889 | ObjectArray<Object>::Alloc(self, object_array_class.Get(), ImageHeader::kImageRootsMax))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 890 | image_roots->Set<false>(ImageHeader::kDexCaches, dex_caches.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 891 | image_roots->Set<false>(ImageHeader::kClassRoots, class_linker->GetClassRoots()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 892 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 893 | CHECK(image_roots->Get(i) != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 894 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 895 | return image_roots.Get(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 898 | // Walk instance fields of the given Class. Separate function to allow recursion on the super |
| 899 | // class. |
| 900 | void ImageWriter::WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) { |
| 901 | // Visit fields of parent classes first. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 902 | StackHandleScope<1> hs(Thread::Current()); |
| 903 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 904 | mirror::Class* super = h_class->GetSuperClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 905 | if (super != nullptr) { |
| 906 | WalkInstanceFields(obj, super); |
| 907 | } |
| 908 | // |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 909 | size_t num_reference_fields = h_class->NumReferenceInstanceFields(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 910 | MemberOffset field_offset = h_class->GetFirstReferenceInstanceFieldOffset(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 911 | for (size_t i = 0; i < num_reference_fields; ++i) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 912 | mirror::Object* value = obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 913 | if (value != nullptr) { |
| 914 | WalkFieldsInOrder(value); |
| 915 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 916 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 917 | sizeof(mirror::HeapReference<mirror::Object>)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
| 921 | // For an unvisited object, visit it then all its children found via fields. |
| 922 | void ImageWriter::WalkFieldsInOrder(mirror::Object* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 923 | if (IsInBootImage(obj)) { |
| 924 | // Object is in the image, don't need to fix it up. |
| 925 | return; |
| 926 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 927 | // Use our own visitor routine (instead of GC visitor) to get better locality between |
| 928 | // an object and its fields |
| 929 | if (!IsImageBinSlotAssigned(obj)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 930 | // Walk instance fields of all objects |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 931 | StackHandleScope<2> hs(Thread::Current()); |
| 932 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
| 933 | Handle<mirror::Class> klass(hs.NewHandle(obj->GetClass())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 934 | // visit the object itself. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 935 | CalculateObjectBinSlots(h_obj.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 936 | WalkInstanceFields(h_obj.Get(), klass.Get()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 937 | // Walk static fields of a Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 938 | if (h_obj->IsClass()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 939 | size_t num_reference_static_fields = klass->NumReferenceStaticFields(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 940 | MemberOffset field_offset = klass->GetFirstReferenceStaticFieldOffset(target_ptr_size_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 941 | for (size_t i = 0; i < num_reference_static_fields; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 942 | mirror::Object* value = h_obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 943 | if (value != nullptr) { |
| 944 | WalkFieldsInOrder(value); |
| 945 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 946 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 947 | sizeof(mirror::HeapReference<mirror::Object>)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 948 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 949 | // Visit and assign offsets for fields and field arrays. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 950 | auto* as_klass = h_obj->AsClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 951 | LengthPrefixedArray<ArtField>* fields[] = { |
| 952 | as_klass->GetSFieldsPtr(), as_klass->GetIFieldsPtr(), |
| 953 | }; |
| 954 | for (LengthPrefixedArray<ArtField>* cur_fields : fields) { |
| 955 | // Total array length including header. |
| 956 | if (cur_fields != nullptr) { |
| 957 | const size_t header_size = LengthPrefixedArray<ArtField>::ComputeSize(0); |
| 958 | // Forward the entire array at once. |
| 959 | auto it = native_object_relocations_.find(cur_fields); |
| 960 | CHECK(it == native_object_relocations_.end()) << "Field array " << cur_fields |
| 961 | << " already forwarded"; |
| 962 | size_t& offset = bin_slot_sizes_[kBinArtField]; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 963 | DCHECK(!IsInBootImage(cur_fields)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 964 | native_object_relocations_.emplace( |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 965 | cur_fields, |
| 966 | NativeObjectRelocation {offset, kNativeObjectRelocationTypeArtFieldArray }); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 967 | offset += header_size; |
| 968 | // Forward individual fields so that we can quickly find where they belong. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 969 | for (size_t i = 0, count = cur_fields->size(); i < count; ++i) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 970 | // Need to forward arrays separate of fields. |
| 971 | ArtField* field = &cur_fields->At(i); |
| 972 | auto it2 = native_object_relocations_.find(field); |
| 973 | CHECK(it2 == native_object_relocations_.end()) << "Field at index=" << i |
| 974 | << " already assigned " << PrettyField(field) << " static=" << field->IsStatic(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 975 | DCHECK(!IsInBootImage(field)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 976 | native_object_relocations_.emplace( |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 977 | field, |
| 978 | NativeObjectRelocation {offset, kNativeObjectRelocationTypeArtField }); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 979 | offset += sizeof(ArtField); |
| 980 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 981 | } |
| 982 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 983 | // Visit and assign offsets for methods. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 984 | LengthPrefixedArray<ArtMethod>* method_arrays[] = { |
| 985 | as_klass->GetDirectMethodsPtr(), as_klass->GetVirtualMethodsPtr(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 986 | }; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 987 | for (LengthPrefixedArray<ArtMethod>* array : method_arrays) { |
| 988 | if (array == nullptr) { |
| 989 | continue; |
| 990 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 991 | bool any_dirty = false; |
| 992 | size_t count = 0; |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 993 | const size_t method_alignment = ArtMethod::Alignment(target_ptr_size_); |
| 994 | const size_t method_size = ArtMethod::Size(target_ptr_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 995 | auto iteration_range = |
| 996 | MakeIterationRangeFromLengthPrefixedArray(array, method_size, method_alignment); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 997 | for (auto& m : iteration_range) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 998 | any_dirty = any_dirty || WillMethodBeDirty(&m); |
| 999 | ++count; |
| 1000 | } |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1001 | NativeObjectRelocationType type = any_dirty |
| 1002 | ? kNativeObjectRelocationTypeArtMethodDirty |
| 1003 | : kNativeObjectRelocationTypeArtMethodClean; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1004 | Bin bin_type = BinTypeForNativeRelocationType(type); |
| 1005 | // Forward the entire array at once, but header first. |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1006 | const size_t header_size = LengthPrefixedArray<ArtMethod>::ComputeSize(0, |
| 1007 | method_size, |
| 1008 | method_alignment); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1009 | auto it = native_object_relocations_.find(array); |
| 1010 | CHECK(it == native_object_relocations_.end()) << "Method array " << array |
| 1011 | << " already forwarded"; |
| 1012 | size_t& offset = bin_slot_sizes_[bin_type]; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1013 | DCHECK(!IsInBootImage(array)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1014 | native_object_relocations_.emplace(array, NativeObjectRelocation { offset, |
| 1015 | any_dirty ? kNativeObjectRelocationTypeArtMethodArrayDirty : |
| 1016 | kNativeObjectRelocationTypeArtMethodArrayClean }); |
| 1017 | offset += header_size; |
| 1018 | for (auto& m : iteration_range) { |
| 1019 | AssignMethodOffset(&m, type); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1020 | } |
| 1021 | (any_dirty ? dirty_methods_ : clean_methods_) += count; |
| 1022 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1023 | } else if (h_obj->IsObjectArray()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1024 | // Walk elements of an object array. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1025 | int32_t length = h_obj->AsObjectArray<mirror::Object>()->GetLength(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1026 | for (int32_t i = 0; i < length; i++) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1027 | mirror::ObjectArray<mirror::Object>* obj_array = h_obj->AsObjectArray<mirror::Object>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1028 | mirror::Object* value = obj_array->Get(i); |
| 1029 | if (value != nullptr) { |
| 1030 | WalkFieldsInOrder(value); |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1037 | void ImageWriter::AssignMethodOffset(ArtMethod* method, NativeObjectRelocationType type) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1038 | DCHECK(!IsInBootImage(method)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1039 | auto it = native_object_relocations_.find(method); |
| 1040 | CHECK(it == native_object_relocations_.end()) << "Method " << method << " already assigned " |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1041 | << PrettyMethod(method); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1042 | size_t& offset = bin_slot_sizes_[BinTypeForNativeRelocationType(type)]; |
| 1043 | native_object_relocations_.emplace(method, NativeObjectRelocation { offset, type }); |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1044 | offset += ArtMethod::Size(target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1047 | void ImageWriter::WalkFieldsCallback(mirror::Object* obj, void* arg) { |
| 1048 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
| 1049 | DCHECK(writer != nullptr); |
| 1050 | writer->WalkFieldsInOrder(obj); |
| 1051 | } |
| 1052 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1053 | void ImageWriter::UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg) { |
| 1054 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
| 1055 | DCHECK(writer != nullptr); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1056 | if (!writer->IsInBootImage(obj)) { |
| 1057 | writer->UnbinObjectsIntoOffset(obj); |
| 1058 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | void ImageWriter::UnbinObjectsIntoOffset(mirror::Object* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1062 | DCHECK(!IsInBootImage(obj)); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1063 | CHECK(obj != nullptr); |
| 1064 | |
| 1065 | // We know the bin slot, and the total bin sizes for all objects by now, |
| 1066 | // so calculate the object's final image offset. |
| 1067 | |
| 1068 | DCHECK(IsImageBinSlotAssigned(obj)); |
| 1069 | BinSlot bin_slot = GetImageBinSlot(obj); |
| 1070 | // Change the lockword from a bin slot into an offset |
| 1071 | AssignImageOffset(obj, bin_slot); |
| 1072 | } |
| 1073 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1074 | void ImageWriter::CalculateNewObjectOffsets() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1075 | Thread* const self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1076 | StackHandleScope<1> hs(self); |
| 1077 | Handle<ObjectArray<Object>> image_roots(hs.NewHandle(CreateImageRoots())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1078 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1079 | auto* runtime = Runtime::Current(); |
| 1080 | auto* heap = runtime->GetHeap(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1081 | DCHECK_EQ(0U, image_end_); |
| 1082 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1083 | // 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] | 1084 | // know where image_roots is going to end up |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1085 | image_end_ += RoundUp(sizeof(ImageHeader), kObjectAlignment); // 64-bit-alignment |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1086 | |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 1087 | image_objects_offset_begin_ = image_end_; |
| 1088 | // Clear any pre-existing monitors which may have been in the monitor words, assign bin slots. |
| 1089 | heap->VisitObjects(WalkFieldsCallback, this); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1090 | // Write the image runtime methods. |
| 1091 | image_methods_[ImageHeader::kResolutionMethod] = runtime->GetResolutionMethod(); |
| 1092 | image_methods_[ImageHeader::kImtConflictMethod] = runtime->GetImtConflictMethod(); |
| 1093 | image_methods_[ImageHeader::kImtUnimplementedMethod] = runtime->GetImtUnimplementedMethod(); |
| 1094 | image_methods_[ImageHeader::kCalleeSaveMethod] = runtime->GetCalleeSaveMethod(Runtime::kSaveAll); |
| 1095 | image_methods_[ImageHeader::kRefsOnlySaveMethod] = |
| 1096 | runtime->GetCalleeSaveMethod(Runtime::kRefsOnly); |
| 1097 | image_methods_[ImageHeader::kRefsAndArgsSaveMethod] = |
| 1098 | runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1099 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1100 | // Add room for fake length prefixed array for holding the image methods. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1101 | const auto image_method_type = kNativeObjectRelocationTypeArtMethodArrayClean; |
| 1102 | auto it = native_object_relocations_.find(&image_method_array_); |
| 1103 | CHECK(it == native_object_relocations_.end()); |
| 1104 | size_t& offset = bin_slot_sizes_[BinTypeForNativeRelocationType(image_method_type)]; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1105 | if (!compile_app_image_) { |
| 1106 | native_object_relocations_.emplace(&image_method_array_, |
| 1107 | NativeObjectRelocation { offset, image_method_type }); |
| 1108 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1109 | size_t method_alignment = ArtMethod::Alignment(target_ptr_size_); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1110 | const size_t array_size = LengthPrefixedArray<ArtMethod>::ComputeSize( |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1111 | 0, ArtMethod::Size(target_ptr_size_), method_alignment); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1112 | CHECK_ALIGNED_PARAM(array_size, method_alignment); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1113 | offset += array_size; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1114 | for (auto* m : image_methods_) { |
| 1115 | CHECK(m != nullptr); |
| 1116 | CHECK(m->IsRuntimeMethod()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1117 | DCHECK_EQ(compile_app_image_, IsInBootImage(m)) << "Trampolines should be in boot image"; |
| 1118 | if (!IsInBootImage(m)) { |
| 1119 | AssignMethodOffset(m, kNativeObjectRelocationTypeArtMethodClean); |
| 1120 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1121 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1122 | // Calculate size of the dex cache arrays slot and prepare offsets. |
| 1123 | PrepareDexCacheArraySlots(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1124 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1125 | // Calculate bin slot offsets. |
| 1126 | size_t bin_offset = image_objects_offset_begin_; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1127 | for (size_t i = 0; i != kBinSize; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1128 | bin_slot_offsets_[i] = bin_offset; |
| 1129 | bin_offset += bin_slot_sizes_[i]; |
| 1130 | if (i == kBinArtField) { |
| 1131 | static_assert(kBinArtField + 1 == kBinArtMethodClean, "Methods follow fields."); |
| 1132 | static_assert(alignof(ArtField) == 4u, "ArtField alignment is 4."); |
| 1133 | DCHECK_ALIGNED(bin_offset, 4u); |
| 1134 | DCHECK(method_alignment == 4u || method_alignment == 8u); |
| 1135 | bin_offset = RoundUp(bin_offset, method_alignment); |
| 1136 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1137 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1138 | // NOTE: There may be additional padding between the bin slots and the intern table. |
| 1139 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1140 | DCHECK_EQ(image_end_, GetBinSizeSum(kBinMirrorCount) + image_objects_offset_begin_); |
| 1141 | |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 1142 | // Transform each object's bin slot into an offset which will be used to do the final copy. |
| 1143 | heap->VisitObjects(UnbinObjectsIntoOffsetCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1144 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1145 | DCHECK_EQ(image_end_, GetBinSizeSum(kBinMirrorCount) + image_objects_offset_begin_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1146 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1147 | image_roots_address_ = PointerToLowMemUInt32(GetImageAddress(image_roots.Get())); |
| 1148 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1149 | // Update the native relocations by adding their bin sums. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1150 | for (auto& pair : native_object_relocations_) { |
| 1151 | NativeObjectRelocation& relocation = pair.second; |
| 1152 | Bin bin_type = BinTypeForNativeRelocationType(relocation.type); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1153 | relocation.offset += bin_slot_offsets_[bin_type]; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1156 | // Calculate how big the intern table will be after being serialized. |
| 1157 | auto* const intern_table = Runtime::Current()->GetInternTable(); |
| 1158 | CHECK_EQ(intern_table->WeakSize(), 0u) << " should have strong interned all the strings"; |
| 1159 | intern_table_bytes_ = intern_table->WriteToMemory(nullptr); |
| 1160 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1161 | // Note that image_end_ is left at end of used mirror object section. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | void ImageWriter::CreateHeader(size_t oat_loaded_size, size_t oat_data_offset) { |
| 1165 | CHECK_NE(0U, oat_loaded_size); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1166 | const uint8_t* oat_file_begin = GetOatFileBegin(); |
| 1167 | const uint8_t* oat_file_end = oat_file_begin + oat_loaded_size; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1168 | oat_data_begin_ = oat_file_begin + oat_data_offset; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1169 | const uint8_t* oat_data_end = oat_data_begin_ + oat_file_->Size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1170 | |
| 1171 | // Create the image sections. |
| 1172 | ImageSection sections[ImageHeader::kSectionCount]; |
| 1173 | // Objects section |
| 1174 | auto* objects_section = §ions[ImageHeader::kSectionObjects]; |
| 1175 | *objects_section = ImageSection(0u, image_end_); |
| 1176 | size_t cur_pos = objects_section->End(); |
| 1177 | // Add field section. |
| 1178 | auto* field_section = §ions[ImageHeader::kSectionArtFields]; |
| 1179 | *field_section = ImageSection(cur_pos, bin_slot_sizes_[kBinArtField]); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1180 | CHECK_EQ(bin_slot_offsets_[kBinArtField], field_section->Offset()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1181 | cur_pos = field_section->End(); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1182 | // Round up to the alignment the required by the method section. |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1183 | cur_pos = RoundUp(cur_pos, ArtMethod::Alignment(target_ptr_size_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1184 | // Add method section. |
| 1185 | auto* methods_section = §ions[ImageHeader::kSectionArtMethods]; |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1186 | *methods_section = ImageSection(cur_pos, |
| 1187 | bin_slot_sizes_[kBinArtMethodClean] + |
| 1188 | bin_slot_sizes_[kBinArtMethodDirty]); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1189 | CHECK_EQ(bin_slot_offsets_[kBinArtMethodClean], methods_section->Offset()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1190 | cur_pos = methods_section->End(); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1191 | // Add dex cache arrays section. |
| 1192 | auto* dex_cache_arrays_section = §ions[ImageHeader::kSectionDexCacheArrays]; |
| 1193 | *dex_cache_arrays_section = ImageSection(cur_pos, bin_slot_sizes_[kBinDexCacheArray]); |
| 1194 | CHECK_EQ(bin_slot_offsets_[kBinDexCacheArray], dex_cache_arrays_section->Offset()); |
| 1195 | cur_pos = dex_cache_arrays_section->End(); |
Nicolas Geoffray | 7bf2b4f | 2015-07-08 10:11:59 +0000 | [diff] [blame] | 1196 | // Round up to the alignment the string table expects. See HashSet::WriteToMemory. |
| 1197 | cur_pos = RoundUp(cur_pos, sizeof(uint64_t)); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1198 | // Calculate the size of the interned strings. |
| 1199 | auto* interned_strings_section = §ions[ImageHeader::kSectionInternedStrings]; |
| 1200 | *interned_strings_section = ImageSection(cur_pos, intern_table_bytes_); |
| 1201 | cur_pos = interned_strings_section->End(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1202 | // Finally bitmap section. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1203 | const size_t bitmap_bytes = image_bitmap_->Size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1204 | auto* bitmap_section = §ions[ImageHeader::kSectionImageBitmap]; |
| 1205 | *bitmap_section = ImageSection(RoundUp(cur_pos, kPageSize), RoundUp(bitmap_bytes, kPageSize)); |
| 1206 | cur_pos = bitmap_section->End(); |
| 1207 | if (kIsDebugBuild) { |
| 1208 | size_t idx = 0; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1209 | for (const ImageSection& section : sections) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1210 | LOG(INFO) << static_cast<ImageHeader::ImageSections>(idx) << " " << section; |
| 1211 | ++idx; |
| 1212 | } |
| 1213 | LOG(INFO) << "Methods: clean=" << clean_methods_ << " dirty=" << dirty_methods_; |
| 1214 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1215 | const size_t image_end = static_cast<uint32_t>(interned_strings_section->End()); |
| 1216 | CHECK_EQ(AlignUp(image_begin_ + image_end, kPageSize), oat_file_begin) << |
| 1217 | "Oat file should be right after the image."; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1218 | // Create the header. |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1219 | new (image_->Begin()) ImageHeader(PointerToLowMemUInt32(image_begin_), |
| 1220 | image_end, |
| 1221 | sections, |
| 1222 | image_roots_address_, |
| 1223 | oat_file_->GetOatHeader().GetChecksum(), |
| 1224 | PointerToLowMemUInt32(oat_file_begin), |
| 1225 | PointerToLowMemUInt32(oat_data_begin_), |
| 1226 | PointerToLowMemUInt32(oat_data_end), |
| 1227 | PointerToLowMemUInt32(oat_file_end), |
| 1228 | target_ptr_size_, |
| 1229 | compile_pic_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | ArtMethod* ImageWriter::GetImageMethodAddress(ArtMethod* method) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1233 | auto it = native_object_relocations_.find(method); |
| 1234 | CHECK(it != native_object_relocations_.end()) << PrettyMethod(method) << " @ " << method; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1235 | CHECK_GE(it->second.offset, image_end_) << "ArtMethods should be after Objects"; |
| 1236 | return reinterpret_cast<ArtMethod*>(image_begin_ + it->second.offset); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1239 | class FixupRootVisitor : public RootVisitor { |
| 1240 | public: |
| 1241 | explicit FixupRootVisitor(ImageWriter* image_writer) : image_writer_(image_writer) { |
| 1242 | } |
| 1243 | |
| 1244 | void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1245 | OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1246 | for (size_t i = 0; i < count; ++i) { |
| 1247 | *roots[i] = ImageAddress(*roots[i]); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 1252 | const RootInfo& info ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1253 | OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1254 | for (size_t i = 0; i < count; ++i) { |
| 1255 | roots[i]->Assign(ImageAddress(roots[i]->AsMirrorPtr())); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | private: |
| 1260 | ImageWriter* const image_writer_; |
| 1261 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1262 | mirror::Object* ImageAddress(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1263 | const size_t offset = image_writer_->GetImageOffset(obj); |
| 1264 | auto* const dest = reinterpret_cast<Object*>(image_writer_->image_begin_ + offset); |
| 1265 | VLOG(compiler) << "Update root from " << obj << " to " << dest; |
| 1266 | return dest; |
| 1267 | } |
| 1268 | }; |
| 1269 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1270 | void ImageWriter::CopyAndFixupNativeData() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1271 | // 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] | 1272 | for (auto& pair : native_object_relocations_) { |
| 1273 | NativeObjectRelocation& relocation = pair.second; |
| 1274 | auto* dest = image_->Begin() + relocation.offset; |
| 1275 | DCHECK_GE(dest, image_->Begin() + image_end_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1276 | DCHECK(!IsInBootImage(pair.first)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1277 | switch (relocation.type) { |
| 1278 | case kNativeObjectRelocationTypeArtField: { |
| 1279 | memcpy(dest, pair.first, sizeof(ArtField)); |
| 1280 | reinterpret_cast<ArtField*>(dest)->SetDeclaringClass( |
| 1281 | GetImageAddress(reinterpret_cast<ArtField*>(pair.first)->GetDeclaringClass())); |
| 1282 | break; |
| 1283 | } |
| 1284 | case kNativeObjectRelocationTypeArtMethodClean: |
| 1285 | case kNativeObjectRelocationTypeArtMethodDirty: { |
| 1286 | CopyAndFixupMethod(reinterpret_cast<ArtMethod*>(pair.first), |
| 1287 | reinterpret_cast<ArtMethod*>(dest)); |
| 1288 | break; |
| 1289 | } |
| 1290 | // For arrays, copy just the header since the elements will get copied by their corresponding |
| 1291 | // relocations. |
| 1292 | case kNativeObjectRelocationTypeArtFieldArray: { |
| 1293 | memcpy(dest, pair.first, LengthPrefixedArray<ArtField>::ComputeSize(0)); |
| 1294 | break; |
| 1295 | } |
| 1296 | case kNativeObjectRelocationTypeArtMethodArrayClean: |
| 1297 | case kNativeObjectRelocationTypeArtMethodArrayDirty: { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1298 | memcpy(dest, pair.first, LengthPrefixedArray<ArtMethod>::ComputeSize( |
| 1299 | 0, |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1300 | ArtMethod::Size(target_ptr_size_), |
| 1301 | ArtMethod::Alignment(target_ptr_size_))); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1302 | break; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1303 | case kNativeObjectRelocationTypeDexCacheArray: |
| 1304 | // Nothing to copy here, everything is done in FixupDexCache(). |
| 1305 | break; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1306 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1307 | } |
| 1308 | } |
| 1309 | // Fixup the image method roots. |
| 1310 | auto* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1311 | const ImageSection& methods_section = image_header->GetMethodsSection(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1312 | for (size_t i = 0; i < ImageHeader::kImageMethodsCount; ++i) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1313 | ArtMethod* method = image_methods_[i]; |
| 1314 | CHECK(method != nullptr); |
| 1315 | if (!IsInBootImage(method)) { |
| 1316 | auto it = native_object_relocations_.find(method); |
| 1317 | CHECK(it != native_object_relocations_.end()) << "No fowarding for " << PrettyMethod(method); |
| 1318 | NativeObjectRelocation& relocation = it->second; |
| 1319 | CHECK(methods_section.Contains(relocation.offset)) << relocation.offset << " not in " |
| 1320 | << methods_section; |
| 1321 | CHECK(relocation.IsArtMethodRelocation()) << relocation.type; |
| 1322 | method = reinterpret_cast<ArtMethod*>(image_begin_ + it->second.offset); |
| 1323 | } |
| 1324 | image_header->SetImageMethod(static_cast<ImageHeader::ImageMethod>(i), method); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1325 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1326 | // Write the intern table into the image. |
| 1327 | const ImageSection& intern_table_section = image_header->GetImageSection( |
| 1328 | ImageHeader::kSectionInternedStrings); |
| 1329 | InternTable* const intern_table = Runtime::Current()->GetInternTable(); |
| 1330 | uint8_t* const memory_ptr = image_->Begin() + intern_table_section.Offset(); |
| 1331 | const size_t intern_table_bytes = intern_table->WriteToMemory(memory_ptr); |
| 1332 | // Fixup the pointers in the newly written intern table to contain image addresses. |
| 1333 | InternTable temp_table; |
| 1334 | // Note that we require that ReadFromMemory does not make an internal copy of the elements so that |
| 1335 | // the VisitRoots() will update the memory directly rather than the copies. |
| 1336 | // This also relies on visit roots not doing any verification which could fail after we update |
| 1337 | // the roots to be the image addresses. |
| 1338 | temp_table.ReadFromMemory(memory_ptr); |
| 1339 | CHECK_EQ(temp_table.Size(), intern_table->Size()); |
| 1340 | FixupRootVisitor visitor(this); |
| 1341 | temp_table.VisitRoots(&visitor, kVisitRootFlagAllRoots); |
| 1342 | CHECK_EQ(intern_table_bytes, intern_table_bytes_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 1345 | void ImageWriter::CopyAndFixupObjects() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1346 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1347 | heap->VisitObjects(CopyAndFixupObjectsCallback, this); |
| 1348 | // Fix up the object previously had hash codes. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1349 | for (const auto& hash_pair : saved_hashcode_map_) { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1350 | Object* obj = hash_pair.first; |
Andreas Gampe | 3b45ef2 | 2015-05-26 21:34:09 -0700 | [diff] [blame] | 1351 | DCHECK_EQ(obj->GetLockWord<kVerifyNone>(false).ReadBarrierState(), 0U); |
| 1352 | obj->SetLockWord<kVerifyNone>(LockWord::FromHashCode(hash_pair.second, 0U), false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1353 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1354 | saved_hashcode_map_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1357 | void ImageWriter::CopyAndFixupObjectsCallback(Object* obj, void* arg) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1358 | DCHECK(obj != nullptr); |
| 1359 | DCHECK(arg != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1360 | reinterpret_cast<ImageWriter*>(arg)->CopyAndFixupObject(obj); |
| 1361 | } |
| 1362 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1363 | void ImageWriter::FixupPointerArray(mirror::Object* dst, mirror::PointerArray* arr, |
| 1364 | mirror::Class* klass, Bin array_type) { |
| 1365 | CHECK(klass->IsArrayClass()); |
| 1366 | CHECK(arr->IsIntArray() || arr->IsLongArray()) << PrettyClass(klass) << " " << arr; |
| 1367 | // Fixup int and long pointers for the ArtMethod or ArtField arrays. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1368 | const size_t num_elements = arr->GetLength(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1369 | dst->SetClass(GetImageAddress(arr->GetClass())); |
| 1370 | auto* dest_array = down_cast<mirror::PointerArray*>(dst); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1371 | for (size_t i = 0, count = num_elements; i < count; ++i) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1372 | void* elem = arr->GetElementPtrSize<void*>(i, target_ptr_size_); |
| 1373 | if (elem != nullptr && !IsInBootImage(elem)) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1374 | auto it = native_object_relocations_.find(elem); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1375 | if (UNLIKELY(it == native_object_relocations_.end())) { |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1376 | if (it->second.IsArtMethodRelocation()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1377 | auto* method = reinterpret_cast<ArtMethod*>(elem); |
| 1378 | LOG(FATAL) << "No relocation entry for ArtMethod " << PrettyMethod(method) << " @ " |
| 1379 | << method << " idx=" << i << "/" << num_elements << " with declaring class " |
| 1380 | << PrettyClass(method->GetDeclaringClass()); |
| 1381 | } else { |
| 1382 | CHECK_EQ(array_type, kBinArtField); |
| 1383 | auto* field = reinterpret_cast<ArtField*>(elem); |
| 1384 | LOG(FATAL) << "No relocation entry for ArtField " << PrettyField(field) << " @ " |
| 1385 | << field << " idx=" << i << "/" << num_elements << " with declaring class " |
| 1386 | << PrettyClass(field->GetDeclaringClass()); |
| 1387 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1388 | UNREACHABLE(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1389 | } else { |
| 1390 | elem = image_begin_ + it->second.offset; |
| 1391 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1392 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1393 | dest_array->SetElementPtrSize<false, true>(i, elem, target_ptr_size_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1394 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | void ImageWriter::CopyAndFixupObject(Object* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1398 | if (IsInBootImage(obj)) { |
| 1399 | return; |
| 1400 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1401 | size_t offset = GetImageOffset(obj); |
| 1402 | auto* dst = reinterpret_cast<Object*>(image_->Begin() + offset); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1403 | DCHECK_LT(offset, image_end_); |
| 1404 | const auto* src = reinterpret_cast<const uint8_t*>(obj); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1405 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1406 | image_bitmap_->Set(dst); // Mark the obj as live. |
| 1407 | |
| 1408 | const size_t n = obj->SizeOf(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1409 | DCHECK_LE(offset + n, image_->Size()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1410 | memcpy(dst, src, n); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1411 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1412 | // Write in a hash code of objects which have inflated monitors or a hash code in their monitor |
| 1413 | // word. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1414 | const auto it = saved_hashcode_map_.find(obj); |
| 1415 | dst->SetLockWord(it != saved_hashcode_map_.end() ? |
| 1416 | LockWord::FromHashCode(it->second, 0u) : LockWord::Default(), false); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1417 | FixupObject(obj, dst); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1420 | // Rewrite all the references in the copied object to point to their image address equivalent |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1421 | class FixupVisitor { |
| 1422 | public: |
| 1423 | FixupVisitor(ImageWriter* image_writer, Object* copy) : image_writer_(image_writer), copy_(copy) { |
| 1424 | } |
| 1425 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1426 | // Ignore class roots since we don't have a way to map them to the destination. These are handled |
| 1427 | // with other logic. |
| 1428 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 1429 | const {} |
| 1430 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 1431 | |
| 1432 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1433 | void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1434 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 1435 | Object* ref = obj->GetFieldObject<Object, kVerifyNone>(offset); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1436 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 1437 | // image. |
| 1438 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1439 | offset, |
| 1440 | image_writer_->GetImageAddress(ref)); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | // java.lang.ref.Reference visitor. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1444 | void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, mirror::Reference* ref) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1445 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1446 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1447 | mirror::Reference::ReferentOffset(), |
| 1448 | image_writer_->GetImageAddress(ref->GetReferent())); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1451 | protected: |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1452 | ImageWriter* const image_writer_; |
| 1453 | mirror::Object* const copy_; |
| 1454 | }; |
| 1455 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1456 | class FixupClassVisitor FINAL : public FixupVisitor { |
| 1457 | public: |
| 1458 | FixupClassVisitor(ImageWriter* image_writer, Object* copy) : FixupVisitor(image_writer, copy) { |
| 1459 | } |
| 1460 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1461 | void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1462 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1463 | DCHECK(obj->IsClass()); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1464 | FixupVisitor::operator()(obj, offset, /*is_static*/false); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1467 | void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, |
| 1468 | mirror::Reference* ref ATTRIBUTE_UNUSED) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1469 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1470 | LOG(FATAL) << "Reference not expected here."; |
| 1471 | } |
| 1472 | }; |
| 1473 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1474 | uintptr_t ImageWriter::NativeOffsetInImage(void* obj) { |
| 1475 | DCHECK(obj != nullptr); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1476 | DCHECK(!IsInBootImage(obj)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1477 | auto it = native_object_relocations_.find(obj); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1478 | CHECK(it != native_object_relocations_.end()) << obj << " spaces " |
| 1479 | << Runtime::Current()->GetHeap()->DumpSpaces(); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1480 | const NativeObjectRelocation& relocation = it->second; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1481 | return relocation.offset; |
| 1482 | } |
| 1483 | |
| 1484 | template <typename T> |
| 1485 | T* ImageWriter::NativeLocationInImage(T* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1486 | return (obj == nullptr || IsInBootImage(obj)) |
| 1487 | ? obj |
| 1488 | : reinterpret_cast<T*>(image_begin_ + NativeOffsetInImage(obj)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1491 | template <typename T> |
| 1492 | T* ImageWriter::NativeCopyLocation(T* obj) { |
| 1493 | return (obj == nullptr || IsInBootImage(obj)) |
| 1494 | ? obj |
| 1495 | : reinterpret_cast<T*>(image_->Begin() + NativeOffsetInImage(obj)); |
| 1496 | } |
| 1497 | |
| 1498 | class NativeLocationVisitor { |
| 1499 | public: |
| 1500 | explicit NativeLocationVisitor(ImageWriter* image_writer) : image_writer_(image_writer) {} |
| 1501 | |
| 1502 | template <typename T> |
| 1503 | T* operator()(T* ptr) const { |
| 1504 | return image_writer_->NativeLocationInImage(ptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1505 | } |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1506 | |
| 1507 | private: |
| 1508 | ImageWriter* const image_writer_; |
| 1509 | }; |
| 1510 | |
| 1511 | void ImageWriter::FixupClass(mirror::Class* orig, mirror::Class* copy) { |
| 1512 | orig->FixupNativePointers(copy, target_ptr_size_, NativeLocationVisitor(this)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1513 | FixupClassVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1514 | static_cast<mirror::Object*>(orig)->VisitReferences(visitor, visitor); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1517 | void ImageWriter::FixupObject(Object* orig, Object* copy) { |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1518 | DCHECK(orig != nullptr); |
| 1519 | DCHECK(copy != nullptr); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 1520 | if (kUseBakerOrBrooksReadBarrier) { |
| 1521 | orig->AssertReadBarrierPointer(); |
| 1522 | if (kUseBrooksReadBarrier) { |
| 1523 | // Note the address 'copy' isn't the same as the image address of 'orig'. |
| 1524 | copy->SetReadBarrierPointer(GetImageAddress(orig)); |
| 1525 | DCHECK_EQ(copy->GetReadBarrierPointer(), GetImageAddress(orig)); |
| 1526 | } |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 1527 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1528 | auto* klass = orig->GetClass(); |
| 1529 | if (klass->IsIntArrayClass() || klass->IsLongArrayClass()) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1530 | // Is this a native pointer array? |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1531 | auto it = pointer_arrays_.find(down_cast<mirror::PointerArray*>(orig)); |
| 1532 | if (it != pointer_arrays_.end()) { |
| 1533 | // Should only need to fixup every pointer array exactly once. |
| 1534 | FixupPointerArray(copy, down_cast<mirror::PointerArray*>(orig), klass, it->second); |
| 1535 | pointer_arrays_.erase(it); |
| 1536 | return; |
| 1537 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1538 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1539 | if (orig->IsClass()) { |
| 1540 | FixupClass(orig->AsClass<kVerifyNone>(), down_cast<mirror::Class*>(copy)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1541 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1542 | if (klass == mirror::Method::StaticClass() || klass == mirror::Constructor::StaticClass()) { |
| 1543 | // Need to go update the ArtMethod. |
| 1544 | auto* dest = down_cast<mirror::AbstractMethod*>(copy); |
| 1545 | auto* src = down_cast<mirror::AbstractMethod*>(orig); |
| 1546 | ArtMethod* src_method = src->GetArtMethod(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1547 | auto it = native_object_relocations_.find(src_method); |
| 1548 | CHECK(it != native_object_relocations_.end()) |
| 1549 | << "Missing relocation for AbstractMethod.artMethod " << PrettyMethod(src_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1550 | dest->SetArtMethod( |
| 1551 | reinterpret_cast<ArtMethod*>(image_begin_ + it->second.offset)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1552 | } else if (!klass->IsArrayClass()) { |
| 1553 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1554 | if (klass == class_linker->GetClassRoot(ClassLinker::kJavaLangDexCache)) { |
| 1555 | FixupDexCache(down_cast<mirror::DexCache*>(orig), down_cast<mirror::DexCache*>(copy)); |
| 1556 | } else if (klass->IsSubClass(down_cast<mirror::Class*>( |
| 1557 | class_linker->GetClassRoot(ClassLinker::kJavaLangClassLoader)))) { |
| 1558 | // If src is a ClassLoader, set the class table to null so that it gets recreated by the |
| 1559 | // ClassLoader. |
| 1560 | down_cast<mirror::ClassLoader*>(copy)->SetClassTable(nullptr); |
Mathieu Chartier | 5550c56 | 2015-09-22 15:18:04 -0700 | [diff] [blame] | 1561 | // Also set allocator to null to be safe. The allocator is created when we create the class |
| 1562 | // table. We also never expect to unload things in the image since they are held live as |
| 1563 | // roots. |
| 1564 | down_cast<mirror::ClassLoader*>(copy)->SetAllocator(nullptr); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1565 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1566 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1567 | FixupVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1568 | orig->VisitReferences(visitor, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1569 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1572 | |
| 1573 | class ImageAddressVisitor { |
| 1574 | public: |
| 1575 | explicit ImageAddressVisitor(ImageWriter* image_writer) : image_writer_(image_writer) {} |
| 1576 | |
| 1577 | template <typename T> |
| 1578 | T* operator()(T* ptr) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1579 | return image_writer_->GetImageAddress(ptr); |
| 1580 | } |
| 1581 | |
| 1582 | private: |
| 1583 | ImageWriter* const image_writer_; |
| 1584 | }; |
| 1585 | |
| 1586 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1587 | void ImageWriter::FixupDexCache(mirror::DexCache* orig_dex_cache, |
| 1588 | mirror::DexCache* copy_dex_cache) { |
| 1589 | // Though the DexCache array fields are usually treated as native pointers, we set the full |
| 1590 | // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is |
| 1591 | // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e. |
| 1592 | // static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))). |
| 1593 | GcRoot<mirror::String>* orig_strings = orig_dex_cache->GetStrings(); |
| 1594 | if (orig_strings != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1595 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::StringsOffset(), |
| 1596 | NativeLocationInImage(orig_strings), |
| 1597 | /*pointer size*/8u); |
| 1598 | orig_dex_cache->FixupStrings(NativeCopyLocation(orig_strings), ImageAddressVisitor(this)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1599 | } |
| 1600 | GcRoot<mirror::Class>* orig_types = orig_dex_cache->GetResolvedTypes(); |
| 1601 | if (orig_types != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1602 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedTypesOffset(), |
| 1603 | NativeLocationInImage(orig_types), |
| 1604 | /*pointer size*/8u); |
| 1605 | orig_dex_cache->FixupResolvedTypes(NativeCopyLocation(orig_types), ImageAddressVisitor(this)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1606 | } |
| 1607 | ArtMethod** orig_methods = orig_dex_cache->GetResolvedMethods(); |
| 1608 | if (orig_methods != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1609 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedMethodsOffset(), |
| 1610 | NativeLocationInImage(orig_methods), |
| 1611 | /*pointer size*/8u); |
| 1612 | ArtMethod** copy_methods = NativeCopyLocation(orig_methods); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1613 | for (size_t i = 0, num = orig_dex_cache->NumResolvedMethods(); i != num; ++i) { |
| 1614 | ArtMethod* orig = mirror::DexCache::GetElementPtrSize(orig_methods, i, target_ptr_size_); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1615 | ArtMethod* copy = NativeLocationInImage(orig); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1616 | mirror::DexCache::SetElementPtrSize(copy_methods, i, copy, target_ptr_size_); |
| 1617 | } |
| 1618 | } |
| 1619 | ArtField** orig_fields = orig_dex_cache->GetResolvedFields(); |
| 1620 | if (orig_fields != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1621 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedFieldsOffset(), |
| 1622 | NativeLocationInImage(orig_fields), |
| 1623 | /*pointer size*/8u); |
| 1624 | ArtField** copy_fields = NativeCopyLocation(orig_fields); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1625 | for (size_t i = 0, num = orig_dex_cache->NumResolvedFields(); i != num; ++i) { |
| 1626 | ArtField* orig = mirror::DexCache::GetElementPtrSize(orig_fields, i, target_ptr_size_); |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1627 | ArtField* copy = NativeLocationInImage(orig); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1628 | mirror::DexCache::SetElementPtrSize(copy_fields, i, copy, target_ptr_size_); |
| 1629 | } |
| 1630 | } |
| 1631 | } |
| 1632 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1633 | const uint8_t* ImageWriter::GetOatAddress(OatAddress type) const { |
| 1634 | DCHECK_LT(type, kOatAddressCount); |
| 1635 | // If we are compiling an app image, we need to use the stubs of the boot image. |
| 1636 | if (compile_app_image_) { |
| 1637 | // Use the current image pointers. |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame] | 1638 | gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetBootImageSpace(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1639 | DCHECK(image_space != nullptr); |
| 1640 | const OatFile* oat_file = image_space->GetOatFile(); |
| 1641 | CHECK(oat_file != nullptr); |
| 1642 | const OatHeader& header = oat_file->GetOatHeader(); |
| 1643 | switch (type) { |
| 1644 | // TODO: We could maybe clean this up if we stored them in an array in the oat header. |
| 1645 | case kOatAddressQuickGenericJNITrampoline: |
| 1646 | return static_cast<const uint8_t*>(header.GetQuickGenericJniTrampoline()); |
| 1647 | case kOatAddressInterpreterToInterpreterBridge: |
| 1648 | return static_cast<const uint8_t*>(header.GetInterpreterToInterpreterBridge()); |
| 1649 | case kOatAddressInterpreterToCompiledCodeBridge: |
| 1650 | return static_cast<const uint8_t*>(header.GetInterpreterToCompiledCodeBridge()); |
| 1651 | case kOatAddressJNIDlsymLookup: |
| 1652 | return static_cast<const uint8_t*>(header.GetJniDlsymLookup()); |
| 1653 | case kOatAddressQuickIMTConflictTrampoline: |
| 1654 | return static_cast<const uint8_t*>(header.GetQuickImtConflictTrampoline()); |
| 1655 | case kOatAddressQuickResolutionTrampoline: |
| 1656 | return static_cast<const uint8_t*>(header.GetQuickResolutionTrampoline()); |
| 1657 | case kOatAddressQuickToInterpreterBridge: |
| 1658 | return static_cast<const uint8_t*>(header.GetQuickToInterpreterBridge()); |
| 1659 | default: |
| 1660 | UNREACHABLE(); |
| 1661 | } |
| 1662 | } |
| 1663 | return GetOatAddressForOffset(oat_address_offsets_[type]); |
| 1664 | } |
| 1665 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1666 | const uint8_t* ImageWriter::GetQuickCode(ArtMethod* method, bool* quick_is_interpreted) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1667 | DCHECK(!method->IsResolutionMethod()) << PrettyMethod(method); |
| 1668 | DCHECK(!method->IsImtConflictMethod()) << PrettyMethod(method); |
| 1669 | DCHECK(!method->IsImtUnimplementedMethod()) << PrettyMethod(method); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 1670 | DCHECK(method->IsInvokable()) << PrettyMethod(method); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1671 | DCHECK(!IsInBootImage(method)) << PrettyMethod(method); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1672 | |
| 1673 | // Use original code if it exists. Otherwise, set the code pointer to the resolution |
| 1674 | // trampoline. |
| 1675 | |
| 1676 | // Quick entrypoint: |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 1677 | uint32_t quick_oat_code_offset = PointerToLowMemUInt32( |
| 1678 | method->GetEntryPointFromQuickCompiledCodePtrSize(target_ptr_size_)); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1679 | const uint8_t* quick_code = GetOatAddressForOffset(quick_oat_code_offset); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1680 | *quick_is_interpreted = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1681 | if (quick_code != nullptr && (!method->IsStatic() || method->IsConstructor() || |
| 1682 | method->GetDeclaringClass()->IsInitialized())) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1683 | // We have code for a non-static or initialized method, just use the code. |
| 1684 | } else if (quick_code == nullptr && method->IsNative() && |
| 1685 | (!method->IsStatic() || method->GetDeclaringClass()->IsInitialized())) { |
| 1686 | // 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] | 1687 | quick_code = GetOatAddress(kOatAddressQuickGenericJNITrampoline); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1688 | } else if (quick_code == nullptr && !method->IsNative()) { |
| 1689 | // 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] | 1690 | quick_code = GetOatAddress(kOatAddressQuickToInterpreterBridge); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1691 | *quick_is_interpreted = true; |
| 1692 | } else { |
| 1693 | CHECK(!method->GetDeclaringClass()->IsInitialized()); |
| 1694 | // We have code for a static method, but need to go through the resolution stub for class |
| 1695 | // initialization. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1696 | quick_code = GetOatAddress(kOatAddressQuickResolutionTrampoline); |
| 1697 | } |
| 1698 | if (!IsInBootOatFile(quick_code)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1699 | DCHECK_GE(quick_code, oat_data_begin_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1700 | } |
| 1701 | return quick_code; |
| 1702 | } |
| 1703 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1704 | const uint8_t* ImageWriter::GetQuickEntryPoint(ArtMethod* method) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1705 | // Calculate the quick entry point following the same logic as FixupMethod() below. |
| 1706 | // The resolution method has a special trampoline to call. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1707 | Runtime* runtime = Runtime::Current(); |
| 1708 | if (UNLIKELY(method == runtime->GetResolutionMethod())) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1709 | return GetOatAddress(kOatAddressQuickResolutionTrampoline); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1710 | } else if (UNLIKELY(method == runtime->GetImtConflictMethod() || |
| 1711 | method == runtime->GetImtUnimplementedMethod())) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1712 | return GetOatAddress(kOatAddressQuickIMTConflictTrampoline); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1713 | } else { |
| 1714 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 1715 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 1716 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 1717 | if (UNLIKELY(!method->IsInvokable())) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1718 | return GetOatAddress(kOatAddressQuickToInterpreterBridge); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1719 | } else { |
| 1720 | bool quick_is_interpreted; |
| 1721 | return GetQuickCode(method, &quick_is_interpreted); |
| 1722 | } |
| 1723 | } |
| 1724 | } |
| 1725 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1726 | void ImageWriter::CopyAndFixupMethod(ArtMethod* orig, ArtMethod* copy) { |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1727 | memcpy(copy, orig, ArtMethod::Size(target_ptr_size_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1728 | |
| 1729 | copy->SetDeclaringClass(GetImageAddress(orig->GetDeclaringClassUnchecked())); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1730 | |
| 1731 | ArtMethod** orig_resolved_methods = orig->GetDexCacheResolvedMethods(target_ptr_size_); |
| 1732 | copy->SetDexCacheResolvedMethods(NativeLocationInImage(orig_resolved_methods), target_ptr_size_); |
| 1733 | GcRoot<mirror::Class>* orig_resolved_types = orig->GetDexCacheResolvedTypes(target_ptr_size_); |
| 1734 | copy->SetDexCacheResolvedTypes(NativeLocationInImage(orig_resolved_types), target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1735 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1736 | // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to |
| 1737 | // oat_begin_ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1738 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1739 | // The resolution method has a special trampoline to call. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1740 | Runtime* runtime = Runtime::Current(); |
| 1741 | if (UNLIKELY(orig == runtime->GetResolutionMethod())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1742 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1743 | GetOatAddress(kOatAddressQuickResolutionTrampoline), target_ptr_size_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1744 | } else if (UNLIKELY(orig == runtime->GetImtConflictMethod() || |
| 1745 | orig == runtime->GetImtUnimplementedMethod())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1746 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1747 | GetOatAddress(kOatAddressQuickIMTConflictTrampoline), target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1748 | } else if (UNLIKELY(orig->IsRuntimeMethod())) { |
| 1749 | bool found_one = false; |
| 1750 | for (size_t i = 0; i < static_cast<size_t>(Runtime::kLastCalleeSaveType); ++i) { |
| 1751 | auto idx = static_cast<Runtime::CalleeSaveType>(i); |
| 1752 | if (runtime->HasCalleeSaveMethod(idx) && runtime->GetCalleeSaveMethod(idx) == orig) { |
| 1753 | found_one = true; |
| 1754 | break; |
| 1755 | } |
| 1756 | } |
| 1757 | CHECK(found_one) << "Expected to find callee save method but got " << PrettyMethod(orig); |
| 1758 | CHECK(copy->IsRuntimeMethod()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1759 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1760 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 1761 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 1762 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 1763 | if (UNLIKELY(!orig->IsInvokable())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1764 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1765 | GetOatAddress(kOatAddressQuickToInterpreterBridge), target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1766 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1767 | bool quick_is_interpreted; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1768 | const uint8_t* quick_code = GetQuickCode(orig, &quick_is_interpreted); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1769 | copy->SetEntryPointFromQuickCompiledCodePtrSize(quick_code, target_ptr_size_); |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 1770 | |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 1771 | // JNI entrypoint: |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1772 | if (orig->IsNative()) { |
| 1773 | // The native method's pointer is set to a stub to lookup via dlsym. |
| 1774 | // Note this is not the code_ pointer, that is handled above. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1775 | copy->SetEntryPointFromJniPtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1776 | GetOatAddress(kOatAddressJNIDlsymLookup), target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1777 | } |
| 1778 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1779 | } |
| 1780 | } |
| 1781 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1782 | static OatHeader* GetOatHeaderFromElf(ElfFile* elf) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1783 | uint64_t data_sec_offset; |
| 1784 | bool has_data_sec = elf->GetSectionOffsetAndSize(".rodata", &data_sec_offset, nullptr); |
| 1785 | if (!has_data_sec) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1786 | return nullptr; |
| 1787 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1788 | return reinterpret_cast<OatHeader*>(elf->Begin() + data_sec_offset); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1789 | } |
| 1790 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1791 | void ImageWriter::SetOatChecksumFromElfFile(File* elf_file) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1792 | std::string error_msg; |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1793 | std::unique_ptr<ElfFile> elf(ElfFile::Open(elf_file, |
| 1794 | PROT_READ | PROT_WRITE, |
| 1795 | MAP_SHARED, |
| 1796 | &error_msg)); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1797 | if (elf.get() == nullptr) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1798 | LOG(FATAL) << "Unable open oat file: " << error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1799 | return; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1800 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1801 | OatHeader* oat_header = GetOatHeaderFromElf(elf.get()); |
| 1802 | CHECK(oat_header != nullptr); |
| 1803 | CHECK(oat_header->IsValid()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1804 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1805 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1806 | image_header->SetOatChecksum(oat_header->GetChecksum()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1809 | size_t ImageWriter::GetBinSizeSum(ImageWriter::Bin up_to) const { |
| 1810 | DCHECK_LE(up_to, kBinSize); |
| 1811 | return std::accumulate(&bin_slot_sizes_[0], &bin_slot_sizes_[up_to], /*init*/0); |
| 1812 | } |
| 1813 | |
| 1814 | ImageWriter::BinSlot::BinSlot(uint32_t lockword) : lockword_(lockword) { |
| 1815 | // 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] | 1816 | static_assert(kBinBits == 3, "wrong number of bin bits"); |
| 1817 | static_assert(kBinShift == 27, "wrong number of shift"); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1818 | static_assert(sizeof(BinSlot) == sizeof(LockWord), "BinSlot/LockWord must have equal sizes"); |
| 1819 | |
| 1820 | DCHECK_LT(GetBin(), kBinSize); |
| 1821 | DCHECK_ALIGNED(GetIndex(), kObjectAlignment); |
| 1822 | } |
| 1823 | |
| 1824 | ImageWriter::BinSlot::BinSlot(Bin bin, uint32_t index) |
| 1825 | : BinSlot(index | (static_cast<uint32_t>(bin) << kBinShift)) { |
| 1826 | DCHECK_EQ(index, GetIndex()); |
| 1827 | } |
| 1828 | |
| 1829 | ImageWriter::Bin ImageWriter::BinSlot::GetBin() const { |
| 1830 | return static_cast<Bin>((lockword_ & kBinMask) >> kBinShift); |
| 1831 | } |
| 1832 | |
| 1833 | uint32_t ImageWriter::BinSlot::GetIndex() const { |
| 1834 | return lockword_ & ~kBinMask; |
| 1835 | } |
| 1836 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1837 | uint8_t* ImageWriter::GetOatFileBegin() const { |
| 1838 | DCHECK_GT(intern_table_bytes_, 0u); |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1839 | size_t native_sections_size = bin_slot_sizes_[kBinArtField] + |
| 1840 | bin_slot_sizes_[kBinArtMethodDirty] + |
| 1841 | bin_slot_sizes_[kBinArtMethodClean] + |
| 1842 | bin_slot_sizes_[kBinDexCacheArray] + |
| 1843 | intern_table_bytes_; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1844 | return image_begin_ + RoundUp(image_end_ + native_sections_size, kPageSize); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1847 | ImageWriter::Bin ImageWriter::BinTypeForNativeRelocationType(NativeObjectRelocationType type) { |
| 1848 | switch (type) { |
| 1849 | case kNativeObjectRelocationTypeArtField: |
| 1850 | case kNativeObjectRelocationTypeArtFieldArray: |
| 1851 | return kBinArtField; |
| 1852 | case kNativeObjectRelocationTypeArtMethodClean: |
| 1853 | case kNativeObjectRelocationTypeArtMethodArrayClean: |
| 1854 | return kBinArtMethodClean; |
| 1855 | case kNativeObjectRelocationTypeArtMethodDirty: |
| 1856 | case kNativeObjectRelocationTypeArtMethodArrayDirty: |
| 1857 | return kBinArtMethodDirty; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1858 | case kNativeObjectRelocationTypeDexCacheArray: |
| 1859 | return kBinDexCacheArray; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1860 | } |
| 1861 | UNREACHABLE(); |
| 1862 | } |
| 1863 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1864 | } // namespace art |