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> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | #include "base/logging.h" |
| 25 | #include "base/unix_file/fd_file.h" |
| 26 | #include "class_linker.h" |
| 27 | #include "compiled_method.h" |
| 28 | #include "dex_file-inl.h" |
| 29 | #include "driver/compiler_driver.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 30 | #include "elf_file.h" |
| 31 | #include "elf_utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 32 | #include "elf_writer.h" |
| 33 | #include "gc/accounting/card_table-inl.h" |
| 34 | #include "gc/accounting/heap_bitmap.h" |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 35 | #include "gc/accounting/space_bitmap-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 36 | #include "gc/heap.h" |
| 37 | #include "gc/space/large_object_space.h" |
| 38 | #include "gc/space/space-inl.h" |
| 39 | #include "globals.h" |
| 40 | #include "image.h" |
| 41 | #include "intern_table.h" |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 42 | #include "lock_word.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 43 | #include "mirror/art_field-inl.h" |
| 44 | #include "mirror/art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 45 | #include "mirror/array-inl.h" |
| 46 | #include "mirror/class-inl.h" |
| 47 | #include "mirror/class_loader.h" |
| 48 | #include "mirror/dex_cache-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 49 | #include "mirror/object-inl.h" |
| 50 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 51 | #include "mirror/string-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 52 | #include "oat.h" |
| 53 | #include "oat_file.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 54 | #include "runtime.h" |
| 55 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 56 | #include "handle_scope-inl.h" |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 57 | |
| 58 | #include <numeric> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 59 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 60 | using ::art::mirror::ArtField; |
| 61 | using ::art::mirror::ArtMethod; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 62 | using ::art::mirror::Class; |
| 63 | using ::art::mirror::DexCache; |
| 64 | using ::art::mirror::EntryPointFromInterpreter; |
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 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 74 | bool ImageWriter::PrepareImageAddressSpace() { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 75 | target_ptr_size_ = InstructionSetPointerSize(compiler_driver_.GetInstructionSet()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 76 | { |
| 77 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 78 | PruneNonImageClasses(); // Remove junk |
| 79 | ComputeLazyFieldsForImageClasses(); // Add useful information |
Vladimir Marko | 3389ca7 | 2014-12-03 14:35:54 +0000 | [diff] [blame] | 80 | ProcessStrings(); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 81 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 82 | } |
| 83 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 84 | heap->CollectGarbage(false); // Remove garbage. |
| 85 | |
| 86 | if (!AllocMemory()) { |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | if (kIsDebugBuild) { |
| 91 | ScopedObjectAccess soa(Thread::Current()); |
| 92 | CheckNonImageClassesRemoved(); |
| 93 | } |
| 94 | |
| 95 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 96 | CalculateNewObjectOffsets(); |
| 97 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 102 | bool ImageWriter::Write(const std::string& image_filename, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 103 | const std::string& oat_filename, |
| 104 | const std::string& oat_location) { |
| 105 | CHECK(!image_filename.empty()); |
| 106 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 107 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 108 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 109 | std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 110 | if (oat_file.get() == NULL) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 111 | PLOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 112 | return false; |
| 113 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 114 | std::string error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 115 | oat_file_ = OatFile::OpenReadable(oat_file.get(), oat_location, &error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 116 | if (oat_file_ == nullptr) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 117 | 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] | 118 | << ": " << error_msg; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 121 | CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 122 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 123 | interpreter_to_interpreter_bridge_offset_ = |
| 124 | oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset(); |
| 125 | interpreter_to_compiled_code_bridge_offset_ = |
| 126 | oat_file_->GetOatHeader().GetInterpreterToCompiledCodeBridgeOffset(); |
| 127 | |
| 128 | jni_dlsym_lookup_offset_ = oat_file_->GetOatHeader().GetJniDlsymLookupOffset(); |
| 129 | |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 130 | quick_generic_jni_trampoline_offset_ = |
| 131 | oat_file_->GetOatHeader().GetQuickGenericJniTrampolineOffset(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 132 | quick_imt_conflict_trampoline_offset_ = |
| 133 | oat_file_->GetOatHeader().GetQuickImtConflictTrampolineOffset(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 134 | quick_resolution_trampoline_offset_ = |
| 135 | oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset(); |
| 136 | quick_to_interpreter_bridge_offset_ = |
| 137 | oat_file_->GetOatHeader().GetQuickToInterpreterBridgeOffset(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 138 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 139 | size_t oat_loaded_size = 0; |
| 140 | size_t oat_data_offset = 0; |
| 141 | ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 142 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 143 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 144 | CreateHeader(oat_loaded_size, oat_data_offset); |
| 145 | CopyAndFixupObjects(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 146 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 147 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 148 | SetOatChecksumFromElfFile(oat_file.get()); |
| 149 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 150 | if (oat_file->FlushCloseOrErase() != 0) { |
| 151 | LOG(ERROR) << "Failed to flush and close oat file " << oat_filename << " for " << oat_location; |
| 152 | return false; |
| 153 | } |
| 154 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 155 | std::unique_ptr<File> image_file(OS::CreateEmptyFile(image_filename.c_str())); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 156 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 157 | if (image_file.get() == NULL) { |
| 158 | LOG(ERROR) << "Failed to open image file " << image_filename; |
| 159 | return false; |
| 160 | } |
| 161 | if (fchmod(image_file->Fd(), 0644) != 0) { |
| 162 | PLOG(ERROR) << "Failed to make image file world readable: " << image_filename; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 163 | image_file->Erase(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 164 | return EXIT_FAILURE; |
| 165 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 166 | |
| 167 | // Write out the image. |
| 168 | CHECK_EQ(image_end_, image_header->GetImageSize()); |
| 169 | if (!image_file->WriteFully(image_->Begin(), image_end_)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 170 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 171 | image_file->Erase(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 172 | return false; |
| 173 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 174 | |
| 175 | // Write out the image bitmap at the page aligned start of the image end. |
| 176 | CHECK_ALIGNED(image_header->GetImageBitmapOffset(), kPageSize); |
| 177 | if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()), |
| 178 | image_header->GetImageBitmapSize(), |
| 179 | image_header->GetImageBitmapOffset())) { |
| 180 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 181 | image_file->Erase(); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 185 | if (image_file->FlushCloseOrErase() != 0) { |
| 186 | PLOG(ERROR) << "Failed to flush and close image file " << image_filename; |
| 187 | return false; |
| 188 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 192 | void ImageWriter::SetImageOffset(mirror::Object* object, |
| 193 | ImageWriter::BinSlot bin_slot, |
| 194 | size_t offset) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 195 | DCHECK(object != nullptr); |
| 196 | DCHECK_NE(offset, 0U); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 197 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(image_->Begin() + offset); |
| 198 | DCHECK_ALIGNED(obj, kObjectAlignment); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 199 | |
| 200 | image_bitmap_->Set(obj); // Mark the obj as mutated, since we will end up changing it. |
| 201 | { |
| 202 | // Remember the object-inside-of-the-image's hash code so we can restore it after the copy. |
| 203 | auto hash_it = saved_hashes_map_.find(bin_slot); |
| 204 | if (hash_it != saved_hashes_map_.end()) { |
| 205 | std::pair<BinSlot, uint32_t> slot_hash = *hash_it; |
| 206 | saved_hashes_.push_back(std::make_pair(obj, slot_hash.second)); |
| 207 | saved_hashes_map_.erase(hash_it); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 208 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 209 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 210 | // 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] | 211 | object->SetLockWord(LockWord::FromForwardingAddress(offset), false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 212 | DCHECK(IsImageOffsetAssigned(object)); |
| 213 | } |
| 214 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 215 | void ImageWriter::AssignImageOffset(mirror::Object* object, ImageWriter::BinSlot bin_slot) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 216 | DCHECK(object != nullptr); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 217 | DCHECK_NE(image_objects_offset_begin_, 0u); |
| 218 | |
| 219 | size_t previous_bin_sizes = GetBinSizeSum(bin_slot.GetBin()); // sum sizes in [0..bin#) |
| 220 | size_t new_offset = image_objects_offset_begin_ + previous_bin_sizes + bin_slot.GetIndex(); |
| 221 | DCHECK_ALIGNED(new_offset, kObjectAlignment); |
| 222 | |
| 223 | SetImageOffset(object, bin_slot, new_offset); |
| 224 | DCHECK_LT(new_offset, image_end_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 227 | bool ImageWriter::IsImageOffsetAssigned(mirror::Object* object) const { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 228 | // 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] | 229 | DCHECK(object != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 230 | return object->GetLockWord(false).GetState() == LockWord::kForwardingAddress; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 233 | size_t ImageWriter::GetImageOffset(mirror::Object* object) const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 234 | DCHECK(object != nullptr); |
| 235 | DCHECK(IsImageOffsetAssigned(object)); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 236 | LockWord lock_word = object->GetLockWord(false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 237 | size_t offset = lock_word.ForwardingAddress(); |
| 238 | DCHECK_LT(offset, image_end_); |
| 239 | return offset; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 242 | void ImageWriter::SetImageBinSlot(mirror::Object* object, BinSlot bin_slot) { |
| 243 | DCHECK(object != nullptr); |
| 244 | DCHECK(!IsImageOffsetAssigned(object)); |
| 245 | DCHECK(!IsImageBinSlotAssigned(object)); |
| 246 | |
| 247 | // Before we stomp over the lock word, save the hash code for later. |
| 248 | Monitor::Deflate(Thread::Current(), object);; |
| 249 | LockWord lw(object->GetLockWord(false)); |
| 250 | switch (lw.GetState()) { |
| 251 | case LockWord::kFatLocked: { |
| 252 | LOG(FATAL) << "Fat locked object " << object << " found during object copy"; |
| 253 | break; |
| 254 | } |
| 255 | case LockWord::kThinLocked: { |
| 256 | LOG(FATAL) << "Thin locked object " << object << " found during object copy"; |
| 257 | break; |
| 258 | } |
| 259 | case LockWord::kUnlocked: |
| 260 | // No hash, don't need to save it. |
| 261 | break; |
| 262 | case LockWord::kHashCode: |
| 263 | saved_hashes_map_[bin_slot] = lw.GetHashCode(); |
| 264 | break; |
| 265 | default: |
| 266 | LOG(FATAL) << "Unreachable."; |
| 267 | UNREACHABLE(); |
| 268 | } |
| 269 | object->SetLockWord(LockWord::FromForwardingAddress(static_cast<uint32_t>(bin_slot)), |
| 270 | false); |
| 271 | DCHECK(IsImageBinSlotAssigned(object)); |
| 272 | } |
| 273 | |
| 274 | void ImageWriter::AssignImageBinSlot(mirror::Object* object) { |
| 275 | DCHECK(object != nullptr); |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 276 | size_t object_size = object->SizeOf(); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 277 | |
| 278 | // The magic happens here. We segregate objects into different bins based |
| 279 | // on how likely they are to get dirty at runtime. |
| 280 | // |
| 281 | // Likely-to-dirty objects get packed together into the same bin so that |
| 282 | // at runtime their page dirtiness ratio (how many dirty objects a page has) is |
| 283 | // maximized. |
| 284 | // |
| 285 | // This means more pages will stay either clean or shared dirty (with zygote) and |
| 286 | // the app will use less of its own (private) memory. |
| 287 | Bin bin = kBinRegular; |
| 288 | |
| 289 | if (kBinObjects) { |
| 290 | // |
| 291 | // Changing the bin of an object is purely a memory-use tuning. |
| 292 | // It has no change on runtime correctness. |
| 293 | // |
| 294 | // Memory analysis has determined that the following types of objects get dirtied |
| 295 | // the most: |
| 296 | // |
| 297 | // * Class'es which are verified [their clinit runs only at runtime] |
| 298 | // - classes in general [because their static fields get overwritten] |
| 299 | // - initialized classes with all-final statics are unlikely to be ever dirty, |
| 300 | // so bin them separately |
| 301 | // * Art Methods that are: |
| 302 | // - native [their native entry point is not looked up until runtime] |
| 303 | // - have declaring classes that aren't initialized |
| 304 | // [their interpreter/quick entry points are trampolines until the class |
| 305 | // becomes initialized] |
| 306 | // |
| 307 | // We also assume the following objects get dirtied either never or extremely rarely: |
| 308 | // * Strings (they are immutable) |
| 309 | // * Art methods that aren't native and have initialized declared classes |
| 310 | // |
| 311 | // We assume that "regular" bin objects are highly unlikely to become dirtied, |
| 312 | // so packing them together will not result in a noticeably tighter dirty-to-clean ratio. |
| 313 | // |
| 314 | if (object->IsClass()) { |
| 315 | bin = kBinClassVerified; |
| 316 | mirror::Class* klass = object->AsClass(); |
| 317 | |
| 318 | if (klass->GetStatus() == Class::kStatusInitialized) { |
| 319 | bin = kBinClassInitialized; |
| 320 | |
| 321 | // If the class's static fields are all final, put it into a separate bin |
| 322 | // since it's very likely it will stay clean. |
| 323 | uint32_t num_static_fields = klass->NumStaticFields(); |
| 324 | if (num_static_fields == 0) { |
| 325 | bin = kBinClassInitializedFinalStatics; |
| 326 | } else { |
| 327 | // Maybe all the statics are final? |
| 328 | bool all_final = true; |
| 329 | for (uint32_t i = 0; i < num_static_fields; ++i) { |
| 330 | ArtField* field = klass->GetStaticField(i); |
| 331 | if (!field->IsFinal()) { |
| 332 | all_final = false; |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | if (all_final) { |
| 338 | bin = kBinClassInitializedFinalStatics; |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | } else if (object->IsArtMethod<kVerifyNone>()) { |
| 343 | mirror::ArtMethod* art_method = down_cast<ArtMethod*>(object); |
| 344 | if (art_method->IsNative()) { |
| 345 | bin = kBinArtMethodNative; |
| 346 | } else { |
| 347 | mirror::Class* declaring_class = art_method->GetDeclaringClass(); |
| 348 | if (declaring_class->GetStatus() != Class::kStatusInitialized) { |
| 349 | bin = kBinArtMethodNotInitialized; |
| 350 | } else { |
| 351 | // This is highly unlikely to dirty since there's no entry points to mutate. |
| 352 | bin = kBinArtMethodsManagedInitialized; |
| 353 | } |
| 354 | } |
| 355 | } else if (object->GetClass<kVerifyNone>()->IsStringClass()) { |
| 356 | bin = kBinString; // Strings are almost always immutable (except for object header). |
| 357 | } // else bin = kBinRegular |
| 358 | } |
| 359 | |
| 360 | size_t current_offset = bin_slot_sizes_[bin]; // How many bytes the current bin is at (aligned). |
| 361 | // Move the current bin size up to accomodate the object we just assigned a bin slot. |
| 362 | size_t offset_delta = RoundUp(object_size, kObjectAlignment); // 64-bit alignment |
| 363 | bin_slot_sizes_[bin] += offset_delta; |
| 364 | |
| 365 | BinSlot new_bin_slot(bin, current_offset); |
| 366 | SetImageBinSlot(object, new_bin_slot); |
| 367 | |
| 368 | ++bin_slot_count_[bin]; |
| 369 | |
| 370 | DCHECK_LT(GetBinSizeSum(), image_->Size()); |
| 371 | |
| 372 | // Grow the image closer to the end by the object we just assigned. |
| 373 | image_end_ += offset_delta; |
| 374 | DCHECK_LT(image_end_, image_->Size()); |
| 375 | } |
| 376 | |
| 377 | bool ImageWriter::IsImageBinSlotAssigned(mirror::Object* object) const { |
| 378 | DCHECK(object != nullptr); |
| 379 | |
| 380 | // We always stash the bin slot into a lockword, in the 'forwarding address' state. |
| 381 | // If it's in some other state, then we haven't yet assigned an image bin slot. |
| 382 | if (object->GetLockWord(false).GetState() != LockWord::kForwardingAddress) { |
| 383 | return false; |
| 384 | } else if (kIsDebugBuild) { |
| 385 | LockWord lock_word = object->GetLockWord(false); |
| 386 | size_t offset = lock_word.ForwardingAddress(); |
| 387 | BinSlot bin_slot(offset); |
| 388 | DCHECK_LT(bin_slot.GetIndex(), bin_slot_sizes_[bin_slot.GetBin()]) |
| 389 | << "bin slot offset should not exceed the size of that bin"; |
| 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | ImageWriter::BinSlot ImageWriter::GetImageBinSlot(mirror::Object* object) const { |
| 395 | DCHECK(object != nullptr); |
| 396 | DCHECK(IsImageBinSlotAssigned(object)); |
| 397 | |
| 398 | LockWord lock_word = object->GetLockWord(false); |
| 399 | size_t offset = lock_word.ForwardingAddress(); // TODO: ForwardingAddress should be uint32_t |
| 400 | DCHECK_LE(offset, std::numeric_limits<uint32_t>::max()); |
| 401 | |
| 402 | BinSlot bin_slot(static_cast<uint32_t>(offset)); |
| 403 | DCHECK_LT(bin_slot.GetIndex(), bin_slot_sizes_[bin_slot.GetBin()]); |
| 404 | |
| 405 | return bin_slot; |
| 406 | } |
| 407 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 408 | bool ImageWriter::AllocMemory() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 409 | size_t length = RoundUp(Runtime::Current()->GetHeap()->GetTotalMemory(), kPageSize); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 410 | std::string error_msg; |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 411 | image_.reset(MemMap::MapAnonymous("image writer image", nullptr, length, PROT_READ | PROT_WRITE, |
| 412 | false, false, &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 413 | if (UNLIKELY(image_.get() == nullptr)) { |
| 414 | LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 415 | return false; |
| 416 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 417 | |
| 418 | // Create the image bitmap. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 419 | image_bitmap_.reset(gc::accounting::ContinuousSpaceBitmap::Create("image bitmap", image_->Begin(), |
| 420 | length)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 421 | if (image_bitmap_.get() == nullptr) { |
| 422 | LOG(ERROR) << "Failed to allocate memory for image bitmap"; |
| 423 | return false; |
| 424 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 425 | return true; |
| 426 | } |
| 427 | |
| 428 | void ImageWriter::ComputeLazyFieldsForImageClasses() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 429 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 430 | class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL); |
| 431 | } |
| 432 | |
| 433 | bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 434 | Thread* self = Thread::Current(); |
| 435 | StackHandleScope<1> hs(self); |
| 436 | mirror::Class::ComputeName(hs.NewHandle(c)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 437 | return true; |
| 438 | } |
| 439 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 440 | // Count the number of strings in the heap and put the result in arg as a size_t pointer. |
| 441 | static void CountStringsCallback(Object* obj, void* arg) |
| 442 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 443 | if (obj->GetClass()->IsStringClass()) { |
| 444 | ++*reinterpret_cast<size_t*>(arg); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // Collect all the java.lang.String in the heap and put them in the output strings_ array. |
| 449 | class StringCollector { |
| 450 | public: |
| 451 | StringCollector(Handle<mirror::ObjectArray<mirror::String>> strings, size_t index) |
| 452 | : strings_(strings), index_(index) { |
| 453 | } |
| 454 | static void Callback(Object* obj, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 455 | auto* collector = reinterpret_cast<StringCollector*>(arg); |
| 456 | if (obj->GetClass()->IsStringClass()) { |
| 457 | collector->strings_->SetWithoutChecks<false>(collector->index_++, obj->AsString()); |
| 458 | } |
| 459 | } |
| 460 | size_t GetIndex() const { |
| 461 | return index_; |
| 462 | } |
| 463 | |
| 464 | private: |
| 465 | Handle<mirror::ObjectArray<mirror::String>> strings_; |
| 466 | size_t index_; |
| 467 | }; |
| 468 | |
| 469 | // Compare strings based on length, used for sorting strings by length / reverse length. |
Vladimir Marko | faeda18 | 2014-12-04 14:52:25 +0000 | [diff] [blame] | 470 | class LexicographicalStringComparator { |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 471 | public: |
Vladimir Marko | faeda18 | 2014-12-04 14:52:25 +0000 | [diff] [blame] | 472 | bool operator()(const mirror::HeapReference<mirror::String>& lhs, |
| 473 | const mirror::HeapReference<mirror::String>& rhs) const |
| 474 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 475 | mirror::String* lhs_s = lhs.AsMirrorPtr(); |
| 476 | mirror::String* rhs_s = rhs.AsMirrorPtr(); |
| 477 | uint16_t* lhs_begin = lhs_s->GetCharArray()->GetData() + lhs_s->GetOffset(); |
| 478 | uint16_t* rhs_begin = rhs_s->GetCharArray()->GetData() + rhs_s->GetOffset(); |
| 479 | return std::lexicographical_compare(lhs_begin, lhs_begin + lhs_s->GetLength(), |
| 480 | rhs_begin, rhs_begin + rhs_s->GetLength()); |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 481 | } |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 482 | }; |
| 483 | |
Vladimir Marko | faeda18 | 2014-12-04 14:52:25 +0000 | [diff] [blame] | 484 | static bool IsPrefix(mirror::String* pref, mirror::String* full) |
| 485 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 486 | if (pref->GetLength() > full->GetLength()) { |
| 487 | return false; |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 488 | } |
Vladimir Marko | faeda18 | 2014-12-04 14:52:25 +0000 | [diff] [blame] | 489 | uint16_t* pref_begin = pref->GetCharArray()->GetData() + pref->GetOffset(); |
| 490 | uint16_t* full_begin = full->GetCharArray()->GetData() + full->GetOffset(); |
| 491 | return std::equal(pref_begin, pref_begin + pref->GetLength(), full_begin); |
| 492 | } |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 493 | |
| 494 | void ImageWriter::ProcessStrings() { |
| 495 | size_t total_strings = 0; |
| 496 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 497 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 498 | // Count the strings. |
| 499 | heap->VisitObjects(CountStringsCallback, &total_strings); |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 500 | Thread* self = Thread::Current(); |
| 501 | StackHandleScope<1> hs(self); |
| 502 | auto strings = hs.NewHandle(cl->AllocStringArray(self, total_strings)); |
| 503 | StringCollector string_collector(strings, 0U); |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 504 | // Read strings into the array. |
| 505 | heap->VisitObjects(StringCollector::Callback, &string_collector); |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 506 | // Some strings could have gotten freed if AllocStringArray caused a GC. |
| 507 | CHECK_LE(string_collector.GetIndex(), total_strings); |
| 508 | total_strings = string_collector.GetIndex(); |
Vladimir Marko | faeda18 | 2014-12-04 14:52:25 +0000 | [diff] [blame] | 509 | auto* strings_begin = reinterpret_cast<mirror::HeapReference<mirror::String>*>( |
| 510 | strings->GetRawData(sizeof(mirror::HeapReference<mirror::String>), 0)); |
| 511 | std::sort(strings_begin, strings_begin + total_strings, LexicographicalStringComparator()); |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 512 | // Characters of strings which are non equal prefix of another string (not the same string). |
| 513 | // We don't count the savings from equal strings since these would get interned later anyways. |
| 514 | size_t prefix_saved_chars = 0; |
Vladimir Marko | faeda18 | 2014-12-04 14:52:25 +0000 | [diff] [blame] | 515 | // Count characters needed for the strings. |
| 516 | size_t num_chars = 0u; |
| 517 | mirror::String* prev_s = nullptr; |
| 518 | for (size_t idx = 0; idx != total_strings; ++idx) { |
| 519 | mirror::String* s = strings->GetWithoutChecks(idx); |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 520 | size_t length = s->GetLength(); |
Vladimir Marko | faeda18 | 2014-12-04 14:52:25 +0000 | [diff] [blame] | 521 | num_chars += length; |
| 522 | if (prev_s != nullptr && IsPrefix(prev_s, s)) { |
| 523 | size_t prev_length = prev_s->GetLength(); |
| 524 | num_chars -= prev_length; |
| 525 | if (prev_length != length) { |
| 526 | prefix_saved_chars += prev_length; |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 527 | } |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 528 | } |
Vladimir Marko | faeda18 | 2014-12-04 14:52:25 +0000 | [diff] [blame] | 529 | prev_s = s; |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 530 | } |
Vladimir Marko | faeda18 | 2014-12-04 14:52:25 +0000 | [diff] [blame] | 531 | // Create character array, copy characters and point the strings there. |
| 532 | mirror::CharArray* array = mirror::CharArray::Alloc(self, num_chars); |
Andreas Gampe | 245ee00 | 2014-12-04 21:25:04 -0800 | [diff] [blame] | 533 | string_data_array_ = array; |
Vladimir Marko | faeda18 | 2014-12-04 14:52:25 +0000 | [diff] [blame] | 534 | uint16_t* array_data = array->GetData(); |
| 535 | size_t pos = 0u; |
| 536 | prev_s = nullptr; |
| 537 | for (size_t idx = 0; idx != total_strings; ++idx) { |
| 538 | mirror::String* s = strings->GetWithoutChecks(idx); |
| 539 | uint16_t* s_data = s->GetCharArray()->GetData() + s->GetOffset(); |
| 540 | int32_t s_length = s->GetLength(); |
| 541 | int32_t prefix_length = 0u; |
| 542 | if (idx != 0u && IsPrefix(prev_s, s)) { |
| 543 | prefix_length = prev_s->GetLength(); |
| 544 | } |
| 545 | memcpy(array_data + pos, s_data + prefix_length, (s_length - prefix_length) * sizeof(*s_data)); |
| 546 | s->SetOffset(pos - prefix_length); |
| 547 | s->SetArray(array); |
| 548 | pos += s_length - prefix_length; |
| 549 | prev_s = s; |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 550 | } |
Vladimir Marko | faeda18 | 2014-12-04 14:52:25 +0000 | [diff] [blame] | 551 | CHECK_EQ(pos, num_chars); |
| 552 | |
Andreas Gampe | dc84301 | 2015-01-20 16:17:19 -0800 | [diff] [blame] | 553 | if (kIsDebugBuild || VLOG_IS_ON(compiler)) { |
| 554 | LOG(INFO) << "Total # image strings=" << total_strings << " combined length=" |
| 555 | << num_chars << " prefix saved chars=" << prefix_saved_chars; |
| 556 | } |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 557 | ComputeEagerResolvedStrings(); |
| 558 | } |
| 559 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 560 | void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg ATTRIBUTE_UNUSED) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 561 | if (!obj->GetClass()->IsStringClass()) { |
| 562 | return; |
| 563 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 564 | mirror::String* string = obj->AsString(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 565 | const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset(); |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 566 | size_t utf16_length = static_cast<size_t>(string->GetLength()); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 567 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 568 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 569 | size_t dex_cache_count = class_linker->GetDexCacheCount(); |
| 570 | for (size_t i = 0; i < dex_cache_count; ++i) { |
| 571 | DexCache* dex_cache = class_linker->GetDexCache(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 572 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | 24c534d | 2013-11-14 00:15:00 -0800 | [diff] [blame] | 573 | const DexFile::StringId* string_id; |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 574 | if (UNLIKELY(utf16_length == 0)) { |
Ian Rogers | 24c534d | 2013-11-14 00:15:00 -0800 | [diff] [blame] | 575 | string_id = dex_file.FindStringId(""); |
| 576 | } else { |
Vladimir Marko | a48aef4 | 2014-12-03 17:53:53 +0000 | [diff] [blame] | 577 | string_id = dex_file.FindStringId(utf16_string, utf16_length); |
Ian Rogers | 24c534d | 2013-11-14 00:15:00 -0800 | [diff] [blame] | 578 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 579 | if (string_id != nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 580 | // This string occurs in this dex file, assign the dex cache entry. |
| 581 | uint32_t string_idx = dex_file.GetIndexForStringId(*string_id); |
| 582 | if (dex_cache->GetResolvedString(string_idx) == NULL) { |
| 583 | dex_cache->SetResolvedString(string_idx, string); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 589 | void ImageWriter::ComputeEagerResolvedStrings() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 590 | Runtime::Current()->GetHeap()->VisitObjects(ComputeEagerResolvedStringsCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 593 | bool ImageWriter::IsImageClass(Class* klass) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 594 | std::string temp; |
| 595 | return compiler_driver_.IsImageClass(klass->GetDescriptor(&temp)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | struct NonImageClasses { |
| 599 | ImageWriter* image_writer; |
| 600 | std::set<std::string>* non_image_classes; |
| 601 | }; |
| 602 | |
| 603 | void ImageWriter::PruneNonImageClasses() { |
| 604 | if (compiler_driver_.GetImageClasses() == NULL) { |
| 605 | return; |
| 606 | } |
| 607 | Runtime* runtime = Runtime::Current(); |
| 608 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 609 | |
| 610 | // Make a list of classes we would like to prune. |
| 611 | std::set<std::string> non_image_classes; |
| 612 | NonImageClasses context; |
| 613 | context.image_writer = this; |
| 614 | context.non_image_classes = &non_image_classes; |
| 615 | class_linker->VisitClasses(NonImageClassesVisitor, &context); |
| 616 | |
| 617 | // Remove the undesired classes from the class roots. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 618 | for (const std::string& it : non_image_classes) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 619 | bool result = class_linker->RemoveClass(it.c_str(), NULL); |
| 620 | DCHECK(result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // Clear references to removed classes from the DexCaches. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 624 | ArtMethod* resolution_method = runtime->GetResolutionMethod(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 625 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 626 | size_t dex_cache_count = class_linker->GetDexCacheCount(); |
| 627 | for (size_t idx = 0; idx < dex_cache_count; ++idx) { |
| 628 | DexCache* dex_cache = class_linker->GetDexCache(idx); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 629 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
| 630 | Class* klass = dex_cache->GetResolvedType(i); |
| 631 | if (klass != NULL && !IsImageClass(klass)) { |
| 632 | dex_cache->SetResolvedType(i, NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 636 | ArtMethod* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 637 | if (method != NULL && !IsImageClass(method->GetDeclaringClass())) { |
| 638 | dex_cache->SetResolvedMethod(i, resolution_method); |
| 639 | } |
| 640 | } |
| 641 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 642 | ArtField* field = dex_cache->GetResolvedField(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 643 | if (field != NULL && !IsImageClass(field->GetDeclaringClass())) { |
| 644 | dex_cache->SetResolvedField(i, NULL); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) { |
| 651 | NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg); |
| 652 | if (!context->image_writer->IsImageClass(klass)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 653 | std::string temp; |
| 654 | context->non_image_classes->insert(klass->GetDescriptor(&temp)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 655 | } |
| 656 | return true; |
| 657 | } |
| 658 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 659 | void ImageWriter::CheckNonImageClassesRemoved() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 660 | if (compiler_driver_.GetImageClasses() != nullptr) { |
| 661 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 662 | heap->VisitObjects(CheckNonImageClassesRemovedCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 663 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) { |
| 667 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 668 | if (obj->IsClass()) { |
| 669 | Class* klass = obj->AsClass(); |
| 670 | if (!image_writer->IsImageClass(klass)) { |
| 671 | image_writer->DumpImageClasses(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 672 | std::string temp; |
| 673 | CHECK(image_writer->IsImageClass(klass)) << klass->GetDescriptor(&temp) |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 674 | << " " << PrettyDescriptor(klass); |
| 675 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
| 679 | void ImageWriter::DumpImageClasses() { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 680 | const std::set<std::string>* image_classes = compiler_driver_.GetImageClasses(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 681 | CHECK(image_classes != NULL); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 682 | for (const std::string& image_class : *image_classes) { |
| 683 | LOG(INFO) << " " << image_class; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 687 | void ImageWriter::CalculateObjectBinSlots(Object* obj) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 688 | DCHECK(obj != NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 689 | // if it is a string, we want to intern it if its not interned. |
| 690 | if (obj->GetClass()->IsStringClass()) { |
| 691 | // 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] | 692 | if (IsImageBinSlotAssigned(obj)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 693 | DCHECK_EQ(obj, obj->AsString()->Intern()); |
| 694 | return; |
| 695 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 696 | mirror::String* const interned = obj->AsString()->Intern(); |
| 697 | if (obj != interned) { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 698 | if (!IsImageBinSlotAssigned(interned)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 699 | // interned obj is after us, allocate its location early |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 700 | AssignImageBinSlot(interned); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 701 | } |
| 702 | // point those looking for this object to the interned version. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 703 | SetImageBinSlot(obj, GetImageBinSlot(interned)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 704 | return; |
| 705 | } |
| 706 | // else (obj == interned), nothing to do but fall through to the normal case |
| 707 | } |
| 708 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 709 | AssignImageBinSlot(obj); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | ObjectArray<Object>* ImageWriter::CreateImageRoots() const { |
| 713 | Runtime* runtime = Runtime::Current(); |
| 714 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 715 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 716 | StackHandleScope<3> hs(self); |
| 717 | Handle<Class> object_array_class(hs.NewHandle( |
| 718 | class_linker->FindSystemClass(self, "[Ljava/lang/Object;"))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 719 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 720 | // build an Object[] of all the DexCaches used in the source_space_. |
| 721 | // Since we can't hold the dex lock when allocating the dex_caches |
| 722 | // ObjectArray, we lock the dex lock twice, first to get the number |
| 723 | // of dex caches first and then lock it again to copy the dex |
| 724 | // caches. We check that the number of dex caches does not change. |
| 725 | size_t dex_cache_count; |
| 726 | { |
| 727 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 728 | dex_cache_count = class_linker->GetDexCacheCount(); |
| 729 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 730 | Handle<ObjectArray<Object>> dex_caches( |
| 731 | hs.NewHandle(ObjectArray<Object>::Alloc(self, object_array_class.Get(), |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 732 | dex_cache_count))); |
| 733 | CHECK(dex_caches.Get() != nullptr) << "Failed to allocate a dex cache array."; |
| 734 | { |
| 735 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 736 | CHECK_EQ(dex_cache_count, class_linker->GetDexCacheCount()) |
| 737 | << "The number of dex caches changed."; |
| 738 | for (size_t i = 0; i < dex_cache_count; ++i) { |
| 739 | dex_caches->Set<false>(i, class_linker->GetDexCache(i)); |
| 740 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | // build an Object[] of the roots needed to restore the runtime |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 744 | Handle<ObjectArray<Object>> image_roots(hs.NewHandle( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 745 | ObjectArray<Object>::Alloc(self, object_array_class.Get(), ImageHeader::kImageRootsMax))); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 746 | image_roots->Set<false>(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod()); |
| 747 | image_roots->Set<false>(ImageHeader::kImtConflictMethod, runtime->GetImtConflictMethod()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 748 | image_roots->Set<false>(ImageHeader::kImtUnimplementedMethod, |
| 749 | runtime->GetImtUnimplementedMethod()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 750 | image_roots->Set<false>(ImageHeader::kDefaultImt, runtime->GetDefaultImt()); |
| 751 | image_roots->Set<false>(ImageHeader::kCalleeSaveMethod, |
| 752 | runtime->GetCalleeSaveMethod(Runtime::kSaveAll)); |
| 753 | image_roots->Set<false>(ImageHeader::kRefsOnlySaveMethod, |
| 754 | runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)); |
| 755 | image_roots->Set<false>(ImageHeader::kRefsAndArgsSaveMethod, |
| 756 | runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 757 | image_roots->Set<false>(ImageHeader::kDexCaches, dex_caches.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 758 | image_roots->Set<false>(ImageHeader::kClassRoots, class_linker->GetClassRoots()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 759 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 760 | CHECK(image_roots->Get(i) != NULL); |
| 761 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 762 | return image_roots.Get(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 765 | // Walk instance fields of the given Class. Separate function to allow recursion on the super |
| 766 | // class. |
| 767 | void ImageWriter::WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) { |
| 768 | // Visit fields of parent classes first. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 769 | StackHandleScope<1> hs(Thread::Current()); |
| 770 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 771 | mirror::Class* super = h_class->GetSuperClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 772 | if (super != nullptr) { |
| 773 | WalkInstanceFields(obj, super); |
| 774 | } |
| 775 | // |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 776 | size_t num_reference_fields = h_class->NumReferenceInstanceFields(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 777 | MemberOffset field_offset = h_class->GetFirstReferenceInstanceFieldOffset(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 778 | for (size_t i = 0; i < num_reference_fields; ++i) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 779 | mirror::Object* value = obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 780 | if (value != nullptr) { |
| 781 | WalkFieldsInOrder(value); |
| 782 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 783 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 784 | sizeof(mirror::HeapReference<mirror::Object>)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | // For an unvisited object, visit it then all its children found via fields. |
| 789 | void ImageWriter::WalkFieldsInOrder(mirror::Object* obj) { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 790 | // Use our own visitor routine (instead of GC visitor) to get better locality between |
| 791 | // an object and its fields |
| 792 | if (!IsImageBinSlotAssigned(obj)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 793 | // Walk instance fields of all objects |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 794 | StackHandleScope<2> hs(Thread::Current()); |
| 795 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
| 796 | Handle<mirror::Class> klass(hs.NewHandle(obj->GetClass())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 797 | // visit the object itself. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 798 | CalculateObjectBinSlots(h_obj.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 799 | WalkInstanceFields(h_obj.Get(), klass.Get()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 800 | // Walk static fields of a Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 801 | if (h_obj->IsClass()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 802 | size_t num_static_fields = klass->NumReferenceStaticFields(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 803 | MemberOffset field_offset = klass->GetFirstReferenceStaticFieldOffset(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 804 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 805 | mirror::Object* value = h_obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 806 | if (value != nullptr) { |
| 807 | WalkFieldsInOrder(value); |
| 808 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 809 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 810 | sizeof(mirror::HeapReference<mirror::Object>)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 811 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 812 | } else if (h_obj->IsObjectArray()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 813 | // Walk elements of an object array. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 814 | int32_t length = h_obj->AsObjectArray<mirror::Object>()->GetLength(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 815 | for (int32_t i = 0; i < length; i++) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 816 | mirror::ObjectArray<mirror::Object>* obj_array = h_obj->AsObjectArray<mirror::Object>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 817 | mirror::Object* value = obj_array->Get(i); |
| 818 | if (value != nullptr) { |
| 819 | WalkFieldsInOrder(value); |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | void ImageWriter::WalkFieldsCallback(mirror::Object* obj, void* arg) { |
| 827 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
| 828 | DCHECK(writer != nullptr); |
| 829 | writer->WalkFieldsInOrder(obj); |
| 830 | } |
| 831 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 832 | void ImageWriter::UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg) { |
| 833 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
| 834 | DCHECK(writer != nullptr); |
| 835 | writer->UnbinObjectsIntoOffset(obj); |
| 836 | } |
| 837 | |
| 838 | void ImageWriter::UnbinObjectsIntoOffset(mirror::Object* obj) { |
| 839 | CHECK(obj != nullptr); |
| 840 | |
| 841 | // We know the bin slot, and the total bin sizes for all objects by now, |
| 842 | // so calculate the object's final image offset. |
| 843 | |
| 844 | DCHECK(IsImageBinSlotAssigned(obj)); |
| 845 | BinSlot bin_slot = GetImageBinSlot(obj); |
| 846 | // Change the lockword from a bin slot into an offset |
| 847 | AssignImageOffset(obj, bin_slot); |
| 848 | } |
| 849 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 850 | void ImageWriter::CalculateNewObjectOffsets() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 851 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 852 | StackHandleScope<1> hs(self); |
| 853 | Handle<ObjectArray<Object>> image_roots(hs.NewHandle(CreateImageRoots())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 854 | |
| 855 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 856 | DCHECK_EQ(0U, image_end_); |
| 857 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 858 | // 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] | 859 | // know where image_roots is going to end up |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 860 | image_end_ += RoundUp(sizeof(ImageHeader), kObjectAlignment); // 64-bit-alignment |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 861 | |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 862 | // TODO: Image spaces only? |
| 863 | DCHECK_LT(image_end_, image_->Size()); |
| 864 | image_objects_offset_begin_ = image_end_; |
| 865 | // Clear any pre-existing monitors which may have been in the monitor words, assign bin slots. |
| 866 | heap->VisitObjects(WalkFieldsCallback, this); |
| 867 | // Transform each object's bin slot into an offset which will be used to do the final copy. |
| 868 | heap->VisitObjects(UnbinObjectsIntoOffsetCallback, this); |
| 869 | DCHECK(saved_hashes_map_.empty()); // All binslot hashes should've been put into vector by now. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 870 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 871 | DCHECK_GT(image_end_, GetBinSizeSum()); |
| 872 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 873 | image_roots_address_ = PointerToLowMemUInt32(GetImageAddress(image_roots.Get())); |
| 874 | |
| 875 | // Note that image_end_ is left at end of used space |
| 876 | } |
| 877 | |
| 878 | void ImageWriter::CreateHeader(size_t oat_loaded_size, size_t oat_data_offset) { |
| 879 | CHECK_NE(0U, oat_loaded_size); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 880 | const uint8_t* oat_file_begin = GetOatFileBegin(); |
| 881 | const uint8_t* oat_file_end = oat_file_begin + oat_loaded_size; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 882 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 883 | oat_data_begin_ = oat_file_begin + oat_data_offset; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 884 | const uint8_t* oat_data_end = oat_data_begin_ + oat_file_->Size(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 885 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 886 | // Return to write header at start of image with future location of image_roots. At this point, |
| 887 | // image_end_ is the size of the image (excluding bitmaps). |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 888 | const size_t heap_bytes_per_bitmap_byte = kBitsPerByte * kObjectAlignment; |
Mathieu Chartier | 12aeccd | 2013-11-13 15:52:06 -0800 | [diff] [blame] | 889 | const size_t bitmap_bytes = RoundUp(image_end_, heap_bytes_per_bitmap_byte) / |
| 890 | heap_bytes_per_bitmap_byte; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 891 | new (image_->Begin()) ImageHeader(PointerToLowMemUInt32(image_begin_), |
| 892 | static_cast<uint32_t>(image_end_), |
| 893 | RoundUp(image_end_, kPageSize), |
| 894 | RoundUp(bitmap_bytes, kPageSize), |
| 895 | image_roots_address_, |
| 896 | oat_file_->GetOatHeader().GetChecksum(), |
| 897 | PointerToLowMemUInt32(oat_file_begin), |
| 898 | PointerToLowMemUInt32(oat_data_begin_), |
| 899 | PointerToLowMemUInt32(oat_data_end), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 900 | PointerToLowMemUInt32(oat_file_end), |
| 901 | compile_pic_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 904 | void ImageWriter::CopyAndFixupObjects() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 905 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 906 | // TODO: heap validation can't handle this fix up pass |
| 907 | heap->DisableObjectValidation(); |
| 908 | // TODO: Image spaces only? |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 909 | heap->VisitObjects(CopyAndFixupObjectsCallback, this); |
| 910 | // Fix up the object previously had hash codes. |
| 911 | for (const std::pair<mirror::Object*, uint32_t>& hash_pair : saved_hashes_) { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 912 | Object* obj = hash_pair.first; |
| 913 | DCHECK_EQ(obj->GetLockWord(false).ReadBarrierState(), 0U); |
| 914 | obj->SetLockWord(LockWord::FromHashCode(hash_pair.second, 0U), false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 915 | } |
| 916 | saved_hashes_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 919 | void ImageWriter::CopyAndFixupObjectsCallback(Object* obj, void* arg) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 920 | DCHECK(obj != nullptr); |
| 921 | DCHECK(arg != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 922 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 923 | // see GetLocalAddress for similar computation |
| 924 | size_t offset = image_writer->GetImageOffset(obj); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 925 | uint8_t* dst = image_writer->image_->Begin() + offset; |
| 926 | const uint8_t* src = reinterpret_cast<const uint8_t*>(obj); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 927 | size_t n; |
| 928 | if (obj->IsArtMethod()) { |
| 929 | // Size without pointer fields since we don't want to overrun the buffer if target art method |
| 930 | // is 32 bits but source is 64 bits. |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 931 | n = mirror::ArtMethod::SizeWithoutPointerFields(image_writer->target_ptr_size_); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 932 | } else { |
| 933 | n = obj->SizeOf(); |
| 934 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 935 | DCHECK_LT(offset + n, image_writer->image_->Size()); |
| 936 | memcpy(dst, src, n); |
| 937 | Object* copy = reinterpret_cast<Object*>(dst); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 938 | // Write in a hash code of objects which have inflated monitors or a hash code in their monitor |
| 939 | // word. |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 940 | copy->SetLockWord(LockWord::Default(), false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 941 | image_writer->FixupObject(obj, copy); |
| 942 | } |
| 943 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 944 | // 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] | 945 | class FixupVisitor { |
| 946 | public: |
| 947 | FixupVisitor(ImageWriter* image_writer, Object* copy) : image_writer_(image_writer), copy_(copy) { |
| 948 | } |
| 949 | |
| 950 | void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const |
| 951 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 952 | Object* ref = obj->GetFieldObject<Object, kVerifyNone>(offset); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 953 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 954 | // image. |
| 955 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 956 | offset, image_writer_->GetImageAddress(ref)); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | // java.lang.ref.Reference visitor. |
| 960 | void operator()(mirror::Class* /*klass*/, mirror::Reference* ref) const |
| 961 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 962 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 963 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 964 | mirror::Reference::ReferentOffset(), image_writer_->GetImageAddress(ref->GetReferent())); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 967 | protected: |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 968 | ImageWriter* const image_writer_; |
| 969 | mirror::Object* const copy_; |
| 970 | }; |
| 971 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 972 | class FixupClassVisitor FINAL : public FixupVisitor { |
| 973 | public: |
| 974 | FixupClassVisitor(ImageWriter* image_writer, Object* copy) : FixupVisitor(image_writer, copy) { |
| 975 | } |
| 976 | |
| 977 | void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const |
| 978 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 979 | DCHECK(obj->IsClass()); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 980 | FixupVisitor::operator()(obj, offset, /*is_static*/false); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 981 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 982 | // TODO: Remove dead code |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 983 | if (offset.Uint32Value() < mirror::Class::EmbeddedVTableOffset().Uint32Value()) { |
| 984 | return; |
| 985 | } |
| 986 | } |
| 987 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 988 | void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, |
| 989 | mirror::Reference* ref ATTRIBUTE_UNUSED) const |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 990 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 991 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 992 | LOG(FATAL) << "Reference not expected here."; |
| 993 | } |
| 994 | }; |
| 995 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 996 | void ImageWriter::FixupObject(Object* orig, Object* copy) { |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 997 | DCHECK(orig != nullptr); |
| 998 | DCHECK(copy != nullptr); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 999 | if (kUseBakerOrBrooksReadBarrier) { |
| 1000 | orig->AssertReadBarrierPointer(); |
| 1001 | if (kUseBrooksReadBarrier) { |
| 1002 | // Note the address 'copy' isn't the same as the image address of 'orig'. |
| 1003 | copy->SetReadBarrierPointer(GetImageAddress(orig)); |
| 1004 | DCHECK_EQ(copy->GetReadBarrierPointer(), GetImageAddress(orig)); |
| 1005 | } |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 1006 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1007 | if (orig->IsClass() && orig->AsClass()->ShouldHaveEmbeddedImtAndVTable()) { |
| 1008 | FixupClassVisitor visitor(this, copy); |
| 1009 | orig->VisitReferences<true /*visit class*/>(visitor, visitor); |
| 1010 | } else { |
| 1011 | FixupVisitor visitor(this, copy); |
| 1012 | orig->VisitReferences<true /*visit class*/>(visitor, visitor); |
| 1013 | } |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1014 | if (orig->IsArtMethod<kVerifyNone>()) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 1015 | FixupMethod(orig->AsArtMethod<kVerifyNone>(), down_cast<ArtMethod*>(copy)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1019 | const uint8_t* ImageWriter::GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1020 | DCHECK(!method->IsResolutionMethod() && !method->IsImtConflictMethod() && |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1021 | !method->IsImtUnimplementedMethod() && !method->IsAbstract()) << PrettyMethod(method); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1022 | |
| 1023 | // Use original code if it exists. Otherwise, set the code pointer to the resolution |
| 1024 | // trampoline. |
| 1025 | |
| 1026 | // Quick entrypoint: |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 1027 | uint32_t quick_oat_code_offset = PointerToLowMemUInt32( |
| 1028 | method->GetEntryPointFromQuickCompiledCodePtrSize(target_ptr_size_)); |
| 1029 | const uint8_t* quick_code = GetOatAddress(quick_oat_code_offset); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1030 | *quick_is_interpreted = false; |
| 1031 | if (quick_code != nullptr && |
| 1032 | (!method->IsStatic() || method->IsConstructor() || method->GetDeclaringClass()->IsInitialized())) { |
| 1033 | // We have code for a non-static or initialized method, just use the code. |
| 1034 | } else if (quick_code == nullptr && method->IsNative() && |
| 1035 | (!method->IsStatic() || method->GetDeclaringClass()->IsInitialized())) { |
| 1036 | // Non-static or initialized native method missing compiled code, use generic JNI version. |
| 1037 | quick_code = GetOatAddress(quick_generic_jni_trampoline_offset_); |
| 1038 | } else if (quick_code == nullptr && !method->IsNative()) { |
| 1039 | // We don't have code at all for a non-native method, use the interpreter. |
| 1040 | quick_code = GetOatAddress(quick_to_interpreter_bridge_offset_); |
| 1041 | *quick_is_interpreted = true; |
| 1042 | } else { |
| 1043 | CHECK(!method->GetDeclaringClass()->IsInitialized()); |
| 1044 | // We have code for a static method, but need to go through the resolution stub for class |
| 1045 | // initialization. |
| 1046 | quick_code = GetOatAddress(quick_resolution_trampoline_offset_); |
| 1047 | } |
| 1048 | return quick_code; |
| 1049 | } |
| 1050 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1051 | const uint8_t* ImageWriter::GetQuickEntryPoint(mirror::ArtMethod* method) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1052 | // Calculate the quick entry point following the same logic as FixupMethod() below. |
| 1053 | // The resolution method has a special trampoline to call. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1054 | Runtime* runtime = Runtime::Current(); |
| 1055 | if (UNLIKELY(method == runtime->GetResolutionMethod())) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1056 | return GetOatAddress(quick_resolution_trampoline_offset_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1057 | } else if (UNLIKELY(method == runtime->GetImtConflictMethod() || |
| 1058 | method == runtime->GetImtUnimplementedMethod())) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1059 | return GetOatAddress(quick_imt_conflict_trampoline_offset_); |
| 1060 | } else { |
| 1061 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 1062 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 1063 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
| 1064 | if (UNLIKELY(method->IsAbstract())) { |
| 1065 | return GetOatAddress(quick_to_interpreter_bridge_offset_); |
| 1066 | } else { |
| 1067 | bool quick_is_interpreted; |
| 1068 | return GetQuickCode(method, &quick_is_interpreted); |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1073 | void ImageWriter::FixupMethod(ArtMethod* orig, ArtMethod* copy) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1074 | // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to |
| 1075 | // oat_begin_ |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1076 | // For 64 bit targets we need to repack the current runtime pointer sized fields to the right |
| 1077 | // locations. |
| 1078 | // Copy all of the fields from the runtime methods to the target methods first since we did a |
| 1079 | // bytewise copy earlier. |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 1080 | copy->SetEntryPointFromInterpreterPtrSize<kVerifyNone>( |
| 1081 | orig->GetEntryPointFromInterpreterPtrSize(target_ptr_size_), target_ptr_size_); |
| 1082 | copy->SetEntryPointFromJniPtrSize<kVerifyNone>( |
| 1083 | orig->GetEntryPointFromJniPtrSize(target_ptr_size_), target_ptr_size_); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1084 | copy->SetEntryPointFromQuickCompiledCodePtrSize<kVerifyNone>( |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 1085 | orig->GetEntryPointFromQuickCompiledCodePtrSize(target_ptr_size_), target_ptr_size_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1086 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1087 | // The resolution method has a special trampoline to call. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1088 | Runtime* runtime = Runtime::Current(); |
| 1089 | if (UNLIKELY(orig == runtime->GetResolutionMethod())) { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1090 | copy->SetEntryPointFromQuickCompiledCodePtrSize<kVerifyNone>( |
| 1091 | GetOatAddress(quick_resolution_trampoline_offset_), target_ptr_size_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1092 | } else if (UNLIKELY(orig == runtime->GetImtConflictMethod() || |
| 1093 | orig == runtime->GetImtUnimplementedMethod())) { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1094 | copy->SetEntryPointFromQuickCompiledCodePtrSize<kVerifyNone>( |
| 1095 | GetOatAddress(quick_imt_conflict_trampoline_offset_), target_ptr_size_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1096 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1097 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 1098 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 1099 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
| 1100 | if (UNLIKELY(orig->IsAbstract())) { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1101 | copy->SetEntryPointFromQuickCompiledCodePtrSize<kVerifyNone>( |
| 1102 | GetOatAddress(quick_to_interpreter_bridge_offset_), target_ptr_size_); |
| 1103 | copy->SetEntryPointFromInterpreterPtrSize<kVerifyNone>( |
| 1104 | reinterpret_cast<EntryPointFromInterpreter*>(const_cast<uint8_t*>( |
| 1105 | GetOatAddress(interpreter_to_interpreter_bridge_offset_))), target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1106 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1107 | bool quick_is_interpreted; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1108 | const uint8_t* quick_code = GetQuickCode(orig, &quick_is_interpreted); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1109 | copy->SetEntryPointFromQuickCompiledCodePtrSize<kVerifyNone>(quick_code, target_ptr_size_); |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 1110 | |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 1111 | // JNI entrypoint: |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1112 | if (orig->IsNative()) { |
| 1113 | // The native method's pointer is set to a stub to lookup via dlsym. |
| 1114 | // Note this is not the code_ pointer, that is handled above. |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1115 | copy->SetEntryPointFromJniPtrSize<kVerifyNone>(GetOatAddress(jni_dlsym_lookup_offset_), |
| 1116 | target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1117 | } |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 1118 | |
| 1119 | // Interpreter entrypoint: |
| 1120 | // Set the interpreter entrypoint depending on whether there is compiled code or not. |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 1121 | uint32_t interpreter_code = (quick_is_interpreted) |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 1122 | ? interpreter_to_interpreter_bridge_offset_ |
| 1123 | : interpreter_to_compiled_code_bridge_offset_; |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1124 | EntryPointFromInterpreter* interpreter_entrypoint = |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 1125 | reinterpret_cast<EntryPointFromInterpreter*>( |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1126 | const_cast<uint8_t*>(GetOatAddress(interpreter_code))); |
| 1127 | copy->SetEntryPointFromInterpreterPtrSize<kVerifyNone>( |
| 1128 | interpreter_entrypoint, target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1129 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1133 | static OatHeader* GetOatHeaderFromElf(ElfFile* elf) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1134 | uint64_t data_sec_offset; |
| 1135 | bool has_data_sec = elf->GetSectionOffsetAndSize(".rodata", &data_sec_offset, nullptr); |
| 1136 | if (!has_data_sec) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1137 | return nullptr; |
| 1138 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1139 | return reinterpret_cast<OatHeader*>(elf->Begin() + data_sec_offset); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1142 | void ImageWriter::SetOatChecksumFromElfFile(File* elf_file) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1143 | std::string error_msg; |
| 1144 | std::unique_ptr<ElfFile> elf(ElfFile::Open(elf_file, PROT_READ|PROT_WRITE, |
| 1145 | MAP_SHARED, &error_msg)); |
| 1146 | if (elf.get() == nullptr) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1147 | LOG(FATAL) << "Unable open oat file: " << error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1148 | return; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1149 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1150 | OatHeader* oat_header = GetOatHeaderFromElf(elf.get()); |
| 1151 | CHECK(oat_header != nullptr); |
| 1152 | CHECK(oat_header->IsValid()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1153 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1154 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1155 | image_header->SetOatChecksum(oat_header->GetChecksum()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1158 | size_t ImageWriter::GetBinSizeSum(ImageWriter::Bin up_to) const { |
| 1159 | DCHECK_LE(up_to, kBinSize); |
| 1160 | return std::accumulate(&bin_slot_sizes_[0], &bin_slot_sizes_[up_to], /*init*/0); |
| 1161 | } |
| 1162 | |
| 1163 | ImageWriter::BinSlot::BinSlot(uint32_t lockword) : lockword_(lockword) { |
| 1164 | // These values may need to get updated if more bins are added to the enum Bin |
| 1165 | static_assert(kBinBits == 3, "wrong number of bin bits"); |
| 1166 | static_assert(kBinShift == 29, "wrong number of shift"); |
| 1167 | static_assert(sizeof(BinSlot) == sizeof(LockWord), "BinSlot/LockWord must have equal sizes"); |
| 1168 | |
| 1169 | DCHECK_LT(GetBin(), kBinSize); |
| 1170 | DCHECK_ALIGNED(GetIndex(), kObjectAlignment); |
| 1171 | } |
| 1172 | |
| 1173 | ImageWriter::BinSlot::BinSlot(Bin bin, uint32_t index) |
| 1174 | : BinSlot(index | (static_cast<uint32_t>(bin) << kBinShift)) { |
| 1175 | DCHECK_EQ(index, GetIndex()); |
| 1176 | } |
| 1177 | |
| 1178 | ImageWriter::Bin ImageWriter::BinSlot::GetBin() const { |
| 1179 | return static_cast<Bin>((lockword_ & kBinMask) >> kBinShift); |
| 1180 | } |
| 1181 | |
| 1182 | uint32_t ImageWriter::BinSlot::GetIndex() const { |
| 1183 | return lockword_ & ~kBinMask; |
| 1184 | } |
| 1185 | |
Andreas Gampe | 245ee00 | 2014-12-04 21:25:04 -0800 | [diff] [blame] | 1186 | void ImageWriter::FreeStringDataArray() { |
| 1187 | if (string_data_array_ != nullptr) { |
| 1188 | gc::space::LargeObjectSpace* los = Runtime::Current()->GetHeap()->GetLargeObjectsSpace(); |
| 1189 | if (los != nullptr) { |
| 1190 | los->Free(Thread::Current(), reinterpret_cast<mirror::Object*>(string_data_array_)); |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1195 | } // namespace art |