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