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" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 57 | #include "utils.h" |
| 58 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 59 | using ::art::mirror::ArtField; |
| 60 | using ::art::mirror::ArtMethod; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 61 | using ::art::mirror::Class; |
| 62 | using ::art::mirror::DexCache; |
| 63 | using ::art::mirror::EntryPointFromInterpreter; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 64 | using ::art::mirror::Object; |
| 65 | using ::art::mirror::ObjectArray; |
| 66 | using ::art::mirror::String; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 67 | |
| 68 | namespace art { |
| 69 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 70 | bool ImageWriter::PrepareImageAddressSpace() { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 71 | target_ptr_size_ = InstructionSetPointerSize(compiler_driver_.GetInstructionSet()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 72 | { |
| 73 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 74 | PruneNonImageClasses(); // Remove junk |
| 75 | ComputeLazyFieldsForImageClasses(); // Add useful information |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 76 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 77 | } |
| 78 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 79 | heap->CollectGarbage(false); // Remove garbage. |
| 80 | |
| 81 | if (!AllocMemory()) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | if (kIsDebugBuild) { |
| 86 | ScopedObjectAccess soa(Thread::Current()); |
| 87 | CheckNonImageClassesRemoved(); |
| 88 | } |
| 89 | |
| 90 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 91 | CalculateNewObjectOffsets(); |
| 92 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 97 | bool ImageWriter::Write(const std::string& image_filename, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 98 | const std::string& oat_filename, |
| 99 | const std::string& oat_location) { |
| 100 | CHECK(!image_filename.empty()); |
| 101 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 102 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 103 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 104 | std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 105 | if (oat_file.get() == NULL) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 106 | PLOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 107 | return false; |
| 108 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 109 | std::string error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 110 | oat_file_ = OatFile::OpenReadable(oat_file.get(), oat_location, &error_msg); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 111 | if (oat_file_ == nullptr) { |
Andreas Gampe | 88ec7f4 | 2014-11-05 10:18:32 -0800 | [diff] [blame] | 112 | 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] | 113 | << ": " << error_msg; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 114 | return false; |
| 115 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 116 | CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 117 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 118 | interpreter_to_interpreter_bridge_offset_ = |
| 119 | oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset(); |
| 120 | interpreter_to_compiled_code_bridge_offset_ = |
| 121 | oat_file_->GetOatHeader().GetInterpreterToCompiledCodeBridgeOffset(); |
| 122 | |
| 123 | jni_dlsym_lookup_offset_ = oat_file_->GetOatHeader().GetJniDlsymLookupOffset(); |
| 124 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 125 | portable_imt_conflict_trampoline_offset_ = |
| 126 | oat_file_->GetOatHeader().GetPortableImtConflictTrampolineOffset(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 127 | portable_resolution_trampoline_offset_ = |
| 128 | oat_file_->GetOatHeader().GetPortableResolutionTrampolineOffset(); |
| 129 | portable_to_interpreter_bridge_offset_ = |
| 130 | oat_file_->GetOatHeader().GetPortableToInterpreterBridgeOffset(); |
| 131 | |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 132 | quick_generic_jni_trampoline_offset_ = |
| 133 | oat_file_->GetOatHeader().GetQuickGenericJniTrampolineOffset(); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 134 | quick_imt_conflict_trampoline_offset_ = |
| 135 | oat_file_->GetOatHeader().GetQuickImtConflictTrampolineOffset(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 136 | quick_resolution_trampoline_offset_ = |
| 137 | oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset(); |
| 138 | quick_to_interpreter_bridge_offset_ = |
| 139 | oat_file_->GetOatHeader().GetQuickToInterpreterBridgeOffset(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 140 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 141 | size_t oat_loaded_size = 0; |
| 142 | size_t oat_data_offset = 0; |
| 143 | ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 144 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 145 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 146 | CreateHeader(oat_loaded_size, oat_data_offset); |
| 147 | CopyAndFixupObjects(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 148 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 149 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 150 | SetOatChecksumFromElfFile(oat_file.get()); |
| 151 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 152 | if (oat_file->FlushCloseOrErase() != 0) { |
| 153 | LOG(ERROR) << "Failed to flush and close oat file " << oat_filename << " for " << oat_location; |
| 154 | return false; |
| 155 | } |
| 156 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 157 | std::unique_ptr<File> image_file(OS::CreateEmptyFile(image_filename.c_str())); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 158 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 159 | if (image_file.get() == NULL) { |
| 160 | LOG(ERROR) << "Failed to open image file " << image_filename; |
| 161 | return false; |
| 162 | } |
| 163 | if (fchmod(image_file->Fd(), 0644) != 0) { |
| 164 | PLOG(ERROR) << "Failed to make image file world readable: " << image_filename; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 165 | image_file->Erase(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 166 | return EXIT_FAILURE; |
| 167 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 168 | |
| 169 | // Write out the image. |
| 170 | CHECK_EQ(image_end_, image_header->GetImageSize()); |
| 171 | if (!image_file->WriteFully(image_->Begin(), image_end_)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 172 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 173 | image_file->Erase(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 174 | return false; |
| 175 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 176 | |
| 177 | // Write out the image bitmap at the page aligned start of the image end. |
| 178 | CHECK_ALIGNED(image_header->GetImageBitmapOffset(), kPageSize); |
| 179 | if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()), |
| 180 | image_header->GetImageBitmapSize(), |
| 181 | image_header->GetImageBitmapOffset())) { |
| 182 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 183 | image_file->Erase(); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame^] | 187 | if (image_file->FlushCloseOrErase() != 0) { |
| 188 | PLOG(ERROR) << "Failed to flush and close image file " << image_filename; |
| 189 | return false; |
| 190 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 194 | void ImageWriter::SetImageOffset(mirror::Object* object, size_t offset) { |
| 195 | DCHECK(object != nullptr); |
| 196 | DCHECK_NE(offset, 0U); |
| 197 | DCHECK(!IsImageOffsetAssigned(object)); |
| 198 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(image_->Begin() + offset); |
| 199 | DCHECK_ALIGNED(obj, kObjectAlignment); |
| 200 | image_bitmap_->Set(obj); |
| 201 | // Before we stomp over the lock word, save the hash code for later. |
| 202 | Monitor::Deflate(Thread::Current(), object);; |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 203 | LockWord lw(object->GetLockWord(false)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 204 | switch (lw.GetState()) { |
| 205 | case LockWord::kFatLocked: { |
| 206 | LOG(FATAL) << "Fat locked object " << obj << " found during object copy"; |
| 207 | break; |
| 208 | } |
| 209 | case LockWord::kThinLocked: { |
| 210 | LOG(FATAL) << "Thin locked object " << obj << " found during object copy"; |
| 211 | break; |
| 212 | } |
| 213 | case LockWord::kUnlocked: |
| 214 | // No hash, don't need to save it. |
| 215 | break; |
| 216 | case LockWord::kHashCode: |
| 217 | saved_hashes_.push_back(std::make_pair(obj, lw.GetHashCode())); |
| 218 | break; |
| 219 | default: |
| 220 | LOG(FATAL) << "Unreachable."; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 221 | UNREACHABLE(); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 222 | } |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 223 | object->SetLockWord(LockWord::FromForwardingAddress(offset), false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 224 | DCHECK(IsImageOffsetAssigned(object)); |
| 225 | } |
| 226 | |
| 227 | void ImageWriter::AssignImageOffset(mirror::Object* object) { |
| 228 | DCHECK(object != nullptr); |
| 229 | SetImageOffset(object, image_end_); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 230 | size_t object_size; |
| 231 | if (object->IsArtMethod()) { |
| 232 | // Methods are sized based on the target pointer size. |
| 233 | object_size = mirror::ArtMethod::InstanceSize(target_ptr_size_); |
| 234 | } else { |
| 235 | object_size = object->SizeOf(); |
| 236 | } |
| 237 | image_end_ += RoundUp(object_size, 8); // 64-bit alignment |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 238 | DCHECK_LT(image_end_, image_->Size()); |
| 239 | } |
| 240 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 241 | bool ImageWriter::IsImageOffsetAssigned(mirror::Object* object) const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 242 | DCHECK(object != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 243 | return object->GetLockWord(false).GetState() == LockWord::kForwardingAddress; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 246 | size_t ImageWriter::GetImageOffset(mirror::Object* object) const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 247 | DCHECK(object != nullptr); |
| 248 | DCHECK(IsImageOffsetAssigned(object)); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 249 | LockWord lock_word = object->GetLockWord(false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 250 | size_t offset = lock_word.ForwardingAddress(); |
| 251 | DCHECK_LT(offset, image_end_); |
| 252 | return offset; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 255 | bool ImageWriter::AllocMemory() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 256 | size_t length = RoundUp(Runtime::Current()->GetHeap()->GetTotalMemory(), kPageSize); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 257 | std::string error_msg; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 258 | image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, PROT_READ | PROT_WRITE, |
Ian Rogers | 3cd86d6 | 2014-08-14 08:53:12 -0700 | [diff] [blame] | 259 | false, &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 260 | if (UNLIKELY(image_.get() == nullptr)) { |
| 261 | LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 262 | return false; |
| 263 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 264 | |
| 265 | // Create the image bitmap. |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 266 | image_bitmap_.reset(gc::accounting::ContinuousSpaceBitmap::Create("image bitmap", image_->Begin(), |
| 267 | length)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 268 | if (image_bitmap_.get() == nullptr) { |
| 269 | LOG(ERROR) << "Failed to allocate memory for image bitmap"; |
| 270 | return false; |
| 271 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 272 | return true; |
| 273 | } |
| 274 | |
| 275 | void ImageWriter::ComputeLazyFieldsForImageClasses() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 276 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 277 | class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL); |
| 278 | } |
| 279 | |
| 280 | bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 281 | Thread* self = Thread::Current(); |
| 282 | StackHandleScope<1> hs(self); |
| 283 | mirror::Class::ComputeName(hs.NewHandle(c)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 284 | return true; |
| 285 | } |
| 286 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 287 | // Count the number of strings in the heap and put the result in arg as a size_t pointer. |
| 288 | static void CountStringsCallback(Object* obj, void* arg) |
| 289 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 290 | if (obj->GetClass()->IsStringClass()) { |
| 291 | ++*reinterpret_cast<size_t*>(arg); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // Collect all the java.lang.String in the heap and put them in the output strings_ array. |
| 296 | class StringCollector { |
| 297 | public: |
| 298 | StringCollector(Handle<mirror::ObjectArray<mirror::String>> strings, size_t index) |
| 299 | : strings_(strings), index_(index) { |
| 300 | } |
| 301 | static void Callback(Object* obj, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 302 | auto* collector = reinterpret_cast<StringCollector*>(arg); |
| 303 | if (obj->GetClass()->IsStringClass()) { |
| 304 | collector->strings_->SetWithoutChecks<false>(collector->index_++, obj->AsString()); |
| 305 | } |
| 306 | } |
| 307 | size_t GetIndex() const { |
| 308 | return index_; |
| 309 | } |
| 310 | |
| 311 | private: |
| 312 | Handle<mirror::ObjectArray<mirror::String>> strings_; |
| 313 | size_t index_; |
| 314 | }; |
| 315 | |
| 316 | // Compare strings based on length, used for sorting strings by length / reverse length. |
| 317 | class StringLengthComparator { |
| 318 | public: |
| 319 | explicit StringLengthComparator(Handle<mirror::ObjectArray<mirror::String>> strings) |
| 320 | : strings_(strings) { |
| 321 | } |
| 322 | bool operator()(size_t a, size_t b) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 323 | return strings_->GetWithoutChecks(a)->GetLength() < strings_->GetWithoutChecks(b)->GetLength(); |
| 324 | } |
| 325 | |
| 326 | private: |
| 327 | Handle<mirror::ObjectArray<mirror::String>> strings_; |
| 328 | }; |
| 329 | |
| 330 | // If string a is a prefix of b or b is a prefix of a then they are considered equal. This |
| 331 | // enables us to find prefixes instead of exact matches. Otherwise we do a normal string |
| 332 | // comparison. The strings compared of the form <position, length> inside of the chars_ array. |
| 333 | class SubstringComparator { |
| 334 | public: |
| 335 | explicit SubstringComparator(const std::vector<uint16_t>* const chars) : chars_(chars) { |
| 336 | } |
| 337 | bool operator()(const std::pair<size_t, size_t>& a, const std::pair<size_t, size_t>& b) { |
| 338 | size_t compare_length = std::min(a.second, b.second); |
| 339 | const uint16_t* ptr_a = &chars_->at(a.first); |
| 340 | const uint16_t* ptr_b = &chars_->at(b.first); |
| 341 | for (size_t i = 0; i < compare_length; ++i) { |
| 342 | if (ptr_a[i] != ptr_b[i]) { |
| 343 | return ptr_a[i] < ptr_b[i]; |
| 344 | } |
| 345 | } |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | private: |
| 350 | const std::vector<uint16_t>* const chars_; |
| 351 | }; |
| 352 | |
| 353 | void ImageWriter::ProcessStrings() { |
| 354 | size_t total_strings = 0; |
| 355 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 356 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 357 | { |
| 358 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 359 | heap->VisitObjects(CountStringsCallback, &total_strings); // Count the strings. |
| 360 | } |
| 361 | Thread* self = Thread::Current(); |
| 362 | StackHandleScope<1> hs(self); |
| 363 | auto strings = hs.NewHandle(cl->AllocStringArray(self, total_strings)); |
| 364 | StringCollector string_collector(strings, 0U); |
| 365 | { |
| 366 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 367 | // Read strings into the array. |
| 368 | heap->VisitObjects(StringCollector::Callback, &string_collector); |
| 369 | } |
| 370 | // Some strings could have gotten freed if AllocStringArray caused a GC. |
| 371 | CHECK_LE(string_collector.GetIndex(), total_strings); |
| 372 | total_strings = string_collector.GetIndex(); |
| 373 | size_t total_length = 0; |
| 374 | std::vector<size_t> reverse_sorted_strings; |
| 375 | for (size_t i = 0; i < total_strings; ++i) { |
| 376 | mirror::String* s = strings->GetWithoutChecks(i); |
| 377 | // Look up the string in the array. |
| 378 | total_length += s->GetLength(); |
| 379 | reverse_sorted_strings.push_back(i); |
| 380 | } |
| 381 | // Sort by reverse length. |
| 382 | StringLengthComparator comparator(strings); |
| 383 | std::sort(reverse_sorted_strings.rbegin(), reverse_sorted_strings.rend(), comparator); |
| 384 | // Deduplicate prefixes and add strings to the char array. |
| 385 | std::vector<uint16_t> combined_chars(total_length, 0U); |
| 386 | size_t num_chars = 0; |
| 387 | // Characters of strings which are non equal prefix of another string (not the same string). |
| 388 | // We don't count the savings from equal strings since these would get interned later anyways. |
| 389 | size_t prefix_saved_chars = 0; |
| 390 | std::set<std::pair<size_t, size_t>, SubstringComparator> existing_strings(( |
| 391 | SubstringComparator(&combined_chars))); |
| 392 | for (size_t i = 0; i < total_strings; ++i) { |
| 393 | mirror::String* s = strings->GetWithoutChecks(reverse_sorted_strings[i]); |
| 394 | // Add the string to the end of the char array. |
| 395 | size_t length = s->GetLength(); |
| 396 | for (size_t j = 0; j < length; ++j) { |
| 397 | combined_chars[num_chars++] = s->CharAt(j); |
| 398 | } |
| 399 | // Try to see if the string exists as a prefix of an existing string. |
| 400 | size_t new_offset = 0; |
| 401 | std::pair<size_t, size_t> new_string(num_chars - length, length); |
| 402 | auto it = existing_strings.find(new_string); |
| 403 | if (it != existing_strings.end()) { |
| 404 | for (size_t j = 0; j < length; ++j) { |
| 405 | DCHECK_EQ(combined_chars[it->first + j], s->CharAt(j)); |
| 406 | } |
| 407 | // Shares a prefix, set the offset to where the new offset will be. |
| 408 | new_offset = it->first; |
| 409 | // Remove the added chars. |
| 410 | num_chars -= length; |
| 411 | if (it->second != length) { |
| 412 | prefix_saved_chars += length; |
| 413 | } |
| 414 | } else { |
| 415 | new_offset = new_string.first; |
| 416 | existing_strings.insert(new_string); |
| 417 | } |
| 418 | s->SetOffset(new_offset); |
| 419 | } |
| 420 | // Allocate and update the char arrays. |
| 421 | auto* array = mirror::CharArray::Alloc(self, num_chars); |
| 422 | for (size_t i = 0; i < num_chars; ++i) { |
| 423 | array->SetWithoutChecks<false>(i, combined_chars[i]); |
| 424 | } |
| 425 | for (size_t i = 0; i < total_strings; ++i) { |
| 426 | strings->GetWithoutChecks(i)->SetArray(array); |
| 427 | } |
| 428 | VLOG(compiler) << "Total # image strings=" << total_strings << " combined length=" |
| 429 | << total_length << " prefix saved chars=" << prefix_saved_chars; |
| 430 | ComputeEagerResolvedStrings(); |
| 431 | } |
| 432 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 433 | void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg ATTRIBUTE_UNUSED) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 434 | if (!obj->GetClass()->IsStringClass()) { |
| 435 | return; |
| 436 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 437 | mirror::String* string = obj->AsString(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 438 | const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 439 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 440 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 441 | size_t dex_cache_count = class_linker->GetDexCacheCount(); |
| 442 | for (size_t i = 0; i < dex_cache_count; ++i) { |
| 443 | DexCache* dex_cache = class_linker->GetDexCache(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 444 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | 24c534d | 2013-11-14 00:15:00 -0800 | [diff] [blame] | 445 | const DexFile::StringId* string_id; |
| 446 | if (UNLIKELY(string->GetLength() == 0)) { |
| 447 | string_id = dex_file.FindStringId(""); |
| 448 | } else { |
| 449 | string_id = dex_file.FindStringId(utf16_string); |
| 450 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 451 | if (string_id != nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 452 | // This string occurs in this dex file, assign the dex cache entry. |
| 453 | uint32_t string_idx = dex_file.GetIndexForStringId(*string_id); |
| 454 | if (dex_cache->GetResolvedString(string_idx) == NULL) { |
| 455 | dex_cache->SetResolvedString(string_idx, string); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 461 | void ImageWriter::ComputeEagerResolvedStrings() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 462 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 463 | Runtime::Current()->GetHeap()->VisitObjects(ComputeEagerResolvedStringsCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 466 | bool ImageWriter::IsImageClass(Class* klass) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 467 | std::string temp; |
| 468 | return compiler_driver_.IsImageClass(klass->GetDescriptor(&temp)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | struct NonImageClasses { |
| 472 | ImageWriter* image_writer; |
| 473 | std::set<std::string>* non_image_classes; |
| 474 | }; |
| 475 | |
| 476 | void ImageWriter::PruneNonImageClasses() { |
| 477 | if (compiler_driver_.GetImageClasses() == NULL) { |
| 478 | return; |
| 479 | } |
| 480 | Runtime* runtime = Runtime::Current(); |
| 481 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 482 | |
| 483 | // Make a list of classes we would like to prune. |
| 484 | std::set<std::string> non_image_classes; |
| 485 | NonImageClasses context; |
| 486 | context.image_writer = this; |
| 487 | context.non_image_classes = &non_image_classes; |
| 488 | class_linker->VisitClasses(NonImageClassesVisitor, &context); |
| 489 | |
| 490 | // Remove the undesired classes from the class roots. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 491 | for (const std::string& it : non_image_classes) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 492 | bool result = class_linker->RemoveClass(it.c_str(), NULL); |
| 493 | DCHECK(result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // Clear references to removed classes from the DexCaches. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 497 | ArtMethod* resolution_method = runtime->GetResolutionMethod(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 498 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 499 | size_t dex_cache_count = class_linker->GetDexCacheCount(); |
| 500 | for (size_t idx = 0; idx < dex_cache_count; ++idx) { |
| 501 | DexCache* dex_cache = class_linker->GetDexCache(idx); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 502 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
| 503 | Class* klass = dex_cache->GetResolvedType(i); |
| 504 | if (klass != NULL && !IsImageClass(klass)) { |
| 505 | dex_cache->SetResolvedType(i, NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 509 | ArtMethod* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 510 | if (method != NULL && !IsImageClass(method->GetDeclaringClass())) { |
| 511 | dex_cache->SetResolvedMethod(i, resolution_method); |
| 512 | } |
| 513 | } |
| 514 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 515 | ArtField* field = dex_cache->GetResolvedField(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 516 | if (field != NULL && !IsImageClass(field->GetDeclaringClass())) { |
| 517 | dex_cache->SetResolvedField(i, NULL); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) { |
| 524 | NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg); |
| 525 | if (!context->image_writer->IsImageClass(klass)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 526 | std::string temp; |
| 527 | context->non_image_classes->insert(klass->GetDescriptor(&temp)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 528 | } |
| 529 | return true; |
| 530 | } |
| 531 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 532 | void ImageWriter::CheckNonImageClassesRemoved() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 533 | if (compiler_driver_.GetImageClasses() != nullptr) { |
| 534 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 535 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 536 | heap->VisitObjects(CheckNonImageClassesRemovedCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 537 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) { |
| 541 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 542 | if (obj->IsClass()) { |
| 543 | Class* klass = obj->AsClass(); |
| 544 | if (!image_writer->IsImageClass(klass)) { |
| 545 | image_writer->DumpImageClasses(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 546 | std::string temp; |
| 547 | CHECK(image_writer->IsImageClass(klass)) << klass->GetDescriptor(&temp) |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 548 | << " " << PrettyDescriptor(klass); |
| 549 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
| 553 | void ImageWriter::DumpImageClasses() { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 554 | const std::set<std::string>* image_classes = compiler_driver_.GetImageClasses(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 555 | CHECK(image_classes != NULL); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 556 | for (const std::string& image_class : *image_classes) { |
| 557 | LOG(INFO) << " " << image_class; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 561 | void ImageWriter::CalculateObjectOffsets(Object* obj) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 562 | DCHECK(obj != NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 563 | // if it is a string, we want to intern it if its not interned. |
| 564 | if (obj->GetClass()->IsStringClass()) { |
| 565 | // we must be an interned string that was forward referenced and already assigned |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 566 | if (IsImageOffsetAssigned(obj)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 567 | DCHECK_EQ(obj, obj->AsString()->Intern()); |
| 568 | return; |
| 569 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 570 | mirror::String* const interned = obj->AsString()->Intern(); |
| 571 | if (obj != interned) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 572 | if (!IsImageOffsetAssigned(interned)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 573 | // interned obj is after us, allocate its location early |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 574 | AssignImageOffset(interned); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 575 | } |
| 576 | // point those looking for this object to the interned version. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 577 | SetImageOffset(obj, GetImageOffset(interned)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 578 | return; |
| 579 | } |
| 580 | // else (obj == interned), nothing to do but fall through to the normal case |
| 581 | } |
| 582 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 583 | AssignImageOffset(obj); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | ObjectArray<Object>* ImageWriter::CreateImageRoots() const { |
| 587 | Runtime* runtime = Runtime::Current(); |
| 588 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 589 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 590 | StackHandleScope<3> hs(self); |
| 591 | Handle<Class> object_array_class(hs.NewHandle( |
| 592 | class_linker->FindSystemClass(self, "[Ljava/lang/Object;"))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 593 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 594 | // build an Object[] of all the DexCaches used in the source_space_. |
| 595 | // Since we can't hold the dex lock when allocating the dex_caches |
| 596 | // ObjectArray, we lock the dex lock twice, first to get the number |
| 597 | // of dex caches first and then lock it again to copy the dex |
| 598 | // caches. We check that the number of dex caches does not change. |
| 599 | size_t dex_cache_count; |
| 600 | { |
| 601 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 602 | dex_cache_count = class_linker->GetDexCacheCount(); |
| 603 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 604 | Handle<ObjectArray<Object>> dex_caches( |
| 605 | hs.NewHandle(ObjectArray<Object>::Alloc(self, object_array_class.Get(), |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 606 | dex_cache_count))); |
| 607 | CHECK(dex_caches.Get() != nullptr) << "Failed to allocate a dex cache array."; |
| 608 | { |
| 609 | ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock()); |
| 610 | CHECK_EQ(dex_cache_count, class_linker->GetDexCacheCount()) |
| 611 | << "The number of dex caches changed."; |
| 612 | for (size_t i = 0; i < dex_cache_count; ++i) { |
| 613 | dex_caches->Set<false>(i, class_linker->GetDexCache(i)); |
| 614 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | // build an Object[] of the roots needed to restore the runtime |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 618 | Handle<ObjectArray<Object>> image_roots(hs.NewHandle( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 619 | ObjectArray<Object>::Alloc(self, object_array_class.Get(), ImageHeader::kImageRootsMax))); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 620 | image_roots->Set<false>(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod()); |
| 621 | image_roots->Set<false>(ImageHeader::kImtConflictMethod, runtime->GetImtConflictMethod()); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 622 | image_roots->Set<false>(ImageHeader::kImtUnimplementedMethod, |
| 623 | runtime->GetImtUnimplementedMethod()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 624 | image_roots->Set<false>(ImageHeader::kDefaultImt, runtime->GetDefaultImt()); |
| 625 | image_roots->Set<false>(ImageHeader::kCalleeSaveMethod, |
| 626 | runtime->GetCalleeSaveMethod(Runtime::kSaveAll)); |
| 627 | image_roots->Set<false>(ImageHeader::kRefsOnlySaveMethod, |
| 628 | runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)); |
| 629 | image_roots->Set<false>(ImageHeader::kRefsAndArgsSaveMethod, |
| 630 | runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 631 | image_roots->Set<false>(ImageHeader::kDexCaches, dex_caches.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 632 | image_roots->Set<false>(ImageHeader::kClassRoots, class_linker->GetClassRoots()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 633 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 634 | CHECK(image_roots->Get(i) != NULL); |
| 635 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 636 | return image_roots.Get(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 639 | // Walk instance fields of the given Class. Separate function to allow recursion on the super |
| 640 | // class. |
| 641 | void ImageWriter::WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) { |
| 642 | // Visit fields of parent classes first. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 643 | StackHandleScope<1> hs(Thread::Current()); |
| 644 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 645 | mirror::Class* super = h_class->GetSuperClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 646 | if (super != nullptr) { |
| 647 | WalkInstanceFields(obj, super); |
| 648 | } |
| 649 | // |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 650 | size_t num_reference_fields = h_class->NumReferenceInstanceFields(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 651 | MemberOffset field_offset = h_class->GetFirstReferenceInstanceFieldOffset(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 652 | for (size_t i = 0; i < num_reference_fields; ++i) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 653 | mirror::Object* value = obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 654 | if (value != nullptr) { |
| 655 | WalkFieldsInOrder(value); |
| 656 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 657 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 658 | sizeof(mirror::HeapReference<mirror::Object>)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | // For an unvisited object, visit it then all its children found via fields. |
| 663 | void ImageWriter::WalkFieldsInOrder(mirror::Object* obj) { |
| 664 | if (!IsImageOffsetAssigned(obj)) { |
| 665 | // Walk instance fields of all objects |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 666 | StackHandleScope<2> hs(Thread::Current()); |
| 667 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
| 668 | Handle<mirror::Class> klass(hs.NewHandle(obj->GetClass())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 669 | // visit the object itself. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 670 | CalculateObjectOffsets(h_obj.Get()); |
| 671 | WalkInstanceFields(h_obj.Get(), klass.Get()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 672 | // Walk static fields of a Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 673 | if (h_obj->IsClass()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 674 | size_t num_static_fields = klass->NumReferenceStaticFields(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 675 | MemberOffset field_offset = klass->GetFirstReferenceStaticFieldOffset(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 676 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 677 | mirror::Object* value = h_obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 678 | if (value != nullptr) { |
| 679 | WalkFieldsInOrder(value); |
| 680 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 681 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 682 | sizeof(mirror::HeapReference<mirror::Object>)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 683 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 684 | } else if (h_obj->IsObjectArray()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 685 | // Walk elements of an object array. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 686 | int32_t length = h_obj->AsObjectArray<mirror::Object>()->GetLength(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 687 | for (int32_t i = 0; i < length; i++) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 688 | mirror::ObjectArray<mirror::Object>* obj_array = h_obj->AsObjectArray<mirror::Object>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 689 | mirror::Object* value = obj_array->Get(i); |
| 690 | if (value != nullptr) { |
| 691 | WalkFieldsInOrder(value); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | void ImageWriter::WalkFieldsCallback(mirror::Object* obj, void* arg) { |
| 699 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
| 700 | DCHECK(writer != nullptr); |
| 701 | writer->WalkFieldsInOrder(obj); |
| 702 | } |
| 703 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 704 | void ImageWriter::CalculateNewObjectOffsets() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 705 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 706 | StackHandleScope<1> hs(self); |
| 707 | Handle<ObjectArray<Object>> image_roots(hs.NewHandle(CreateImageRoots())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 708 | |
| 709 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 710 | DCHECK_EQ(0U, image_end_); |
| 711 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 712 | // 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] | 713 | // know where image_roots is going to end up |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 714 | image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 715 | |
| 716 | { |
| 717 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 718 | // TODO: Image spaces only? |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 719 | DCHECK_LT(image_end_, image_->Size()); |
| 720 | // Clear any pre-existing monitors which may have been in the monitor words. |
| 721 | heap->VisitObjects(WalkFieldsCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 724 | image_roots_address_ = PointerToLowMemUInt32(GetImageAddress(image_roots.Get())); |
| 725 | |
| 726 | // Note that image_end_ is left at end of used space |
| 727 | } |
| 728 | |
| 729 | void ImageWriter::CreateHeader(size_t oat_loaded_size, size_t oat_data_offset) { |
| 730 | CHECK_NE(0U, oat_loaded_size); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 731 | const uint8_t* oat_file_begin = GetOatFileBegin(); |
| 732 | const uint8_t* oat_file_end = oat_file_begin + oat_loaded_size; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 733 | oat_data_begin_ = oat_file_begin + oat_data_offset; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 734 | const uint8_t* oat_data_end = oat_data_begin_ + oat_file_->Size(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 735 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 736 | // Return to write header at start of image with future location of image_roots. At this point, |
| 737 | // image_end_ is the size of the image (excluding bitmaps). |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 738 | const size_t heap_bytes_per_bitmap_byte = kBitsPerByte * kObjectAlignment; |
Mathieu Chartier | 12aeccd | 2013-11-13 15:52:06 -0800 | [diff] [blame] | 739 | const size_t bitmap_bytes = RoundUp(image_end_, heap_bytes_per_bitmap_byte) / |
| 740 | heap_bytes_per_bitmap_byte; |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 741 | new (image_->Begin()) ImageHeader(PointerToLowMemUInt32(image_begin_), |
| 742 | static_cast<uint32_t>(image_end_), |
| 743 | RoundUp(image_end_, kPageSize), |
| 744 | RoundUp(bitmap_bytes, kPageSize), |
| 745 | image_roots_address_, |
| 746 | oat_file_->GetOatHeader().GetChecksum(), |
| 747 | PointerToLowMemUInt32(oat_file_begin), |
| 748 | PointerToLowMemUInt32(oat_data_begin_), |
| 749 | PointerToLowMemUInt32(oat_data_end), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 750 | PointerToLowMemUInt32(oat_file_end), |
| 751 | compile_pic_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 754 | void ImageWriter::CopyAndFixupObjects() { |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 755 | ScopedAssertNoThreadSuspension ants(Thread::Current(), "ImageWriter"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 756 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 757 | // TODO: heap validation can't handle this fix up pass |
| 758 | heap->DisableObjectValidation(); |
| 759 | // TODO: Image spaces only? |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 760 | WriterMutexLock mu(ants.Self(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 761 | heap->VisitObjects(CopyAndFixupObjectsCallback, this); |
| 762 | // Fix up the object previously had hash codes. |
| 763 | for (const std::pair<mirror::Object*, uint32_t>& hash_pair : saved_hashes_) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 764 | hash_pair.first->SetLockWord(LockWord::FromHashCode(hash_pair.second), false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 765 | } |
| 766 | saved_hashes_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 767 | } |
| 768 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 769 | void ImageWriter::CopyAndFixupObjectsCallback(Object* obj, void* arg) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 770 | DCHECK(obj != nullptr); |
| 771 | DCHECK(arg != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 772 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 773 | // see GetLocalAddress for similar computation |
| 774 | size_t offset = image_writer->GetImageOffset(obj); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 775 | uint8_t* dst = image_writer->image_->Begin() + offset; |
| 776 | const uint8_t* src = reinterpret_cast<const uint8_t*>(obj); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 777 | size_t n; |
| 778 | if (obj->IsArtMethod()) { |
| 779 | // Size without pointer fields since we don't want to overrun the buffer if target art method |
| 780 | // is 32 bits but source is 64 bits. |
| 781 | n = mirror::ArtMethod::SizeWithoutPointerFields(); |
| 782 | } else { |
| 783 | n = obj->SizeOf(); |
| 784 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 785 | DCHECK_LT(offset + n, image_writer->image_->Size()); |
| 786 | memcpy(dst, src, n); |
| 787 | Object* copy = reinterpret_cast<Object*>(dst); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 788 | // Write in a hash code of objects which have inflated monitors or a hash code in their monitor |
| 789 | // word. |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 790 | copy->SetLockWord(LockWord(), false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 791 | image_writer->FixupObject(obj, copy); |
| 792 | } |
| 793 | |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 794 | class FixupVisitor { |
| 795 | public: |
| 796 | FixupVisitor(ImageWriter* image_writer, Object* copy) : image_writer_(image_writer), copy_(copy) { |
| 797 | } |
| 798 | |
| 799 | void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const |
| 800 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 801 | Object* ref = obj->GetFieldObject<Object, kVerifyNone>(offset); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 802 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 803 | // image. |
| 804 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 805 | offset, image_writer_->GetImageAddress(ref)); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | // java.lang.ref.Reference visitor. |
| 809 | void operator()(mirror::Class* /*klass*/, mirror::Reference* ref) const |
| 810 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 811 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 812 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 813 | mirror::Reference::ReferentOffset(), image_writer_->GetImageAddress(ref->GetReferent())); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 816 | protected: |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 817 | ImageWriter* const image_writer_; |
| 818 | mirror::Object* const copy_; |
| 819 | }; |
| 820 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 821 | class FixupClassVisitor FINAL : public FixupVisitor { |
| 822 | public: |
| 823 | FixupClassVisitor(ImageWriter* image_writer, Object* copy) : FixupVisitor(image_writer, copy) { |
| 824 | } |
| 825 | |
| 826 | void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const |
| 827 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 828 | DCHECK(obj->IsClass()); |
| 829 | FixupVisitor::operator()(obj, offset, false); |
| 830 | |
| 831 | if (offset.Uint32Value() < mirror::Class::EmbeddedVTableOffset().Uint32Value()) { |
| 832 | return; |
| 833 | } |
| 834 | } |
| 835 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 836 | void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, |
| 837 | mirror::Reference* ref ATTRIBUTE_UNUSED) const |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 838 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 839 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) { |
| 840 | LOG(FATAL) << "Reference not expected here."; |
| 841 | } |
| 842 | }; |
| 843 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 844 | void ImageWriter::FixupObject(Object* orig, Object* copy) { |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 845 | DCHECK(orig != nullptr); |
| 846 | DCHECK(copy != nullptr); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 847 | if (kUseBakerOrBrooksReadBarrier) { |
| 848 | orig->AssertReadBarrierPointer(); |
| 849 | if (kUseBrooksReadBarrier) { |
| 850 | // Note the address 'copy' isn't the same as the image address of 'orig'. |
| 851 | copy->SetReadBarrierPointer(GetImageAddress(orig)); |
| 852 | DCHECK_EQ(copy->GetReadBarrierPointer(), GetImageAddress(orig)); |
| 853 | } |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 854 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 855 | if (orig->IsClass() && orig->AsClass()->ShouldHaveEmbeddedImtAndVTable()) { |
| 856 | FixupClassVisitor visitor(this, copy); |
| 857 | orig->VisitReferences<true /*visit class*/>(visitor, visitor); |
| 858 | } else { |
| 859 | FixupVisitor visitor(this, copy); |
| 860 | orig->VisitReferences<true /*visit class*/>(visitor, visitor); |
| 861 | } |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 862 | if (orig->IsArtMethod<kVerifyNone>()) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 863 | FixupMethod(orig->AsArtMethod<kVerifyNone>(), down_cast<ArtMethod*>(copy)); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 864 | } else if (orig->IsClass() && orig->AsClass()->IsArtMethodClass()) { |
| 865 | // Set the right size for the target. |
| 866 | size_t size = mirror::ArtMethod::InstanceSize(target_ptr_size_); |
| 867 | down_cast<mirror::Class*>(copy)->SetObjectSizeWithoutChecks(size); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 871 | const uint8_t* ImageWriter::GetQuickCode(mirror::ArtMethod* method, bool* quick_is_interpreted) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 872 | DCHECK(!method->IsResolutionMethod() && !method->IsImtConflictMethod() && |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 873 | !method->IsImtUnimplementedMethod() && !method->IsAbstract()) << PrettyMethod(method); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 874 | |
| 875 | // Use original code if it exists. Otherwise, set the code pointer to the resolution |
| 876 | // trampoline. |
| 877 | |
| 878 | // Quick entrypoint: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 879 | const uint8_t* quick_code = GetOatAddress(method->GetQuickOatCodeOffset()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 880 | *quick_is_interpreted = false; |
| 881 | if (quick_code != nullptr && |
| 882 | (!method->IsStatic() || method->IsConstructor() || method->GetDeclaringClass()->IsInitialized())) { |
| 883 | // We have code for a non-static or initialized method, just use the code. |
| 884 | } else if (quick_code == nullptr && method->IsNative() && |
| 885 | (!method->IsStatic() || method->GetDeclaringClass()->IsInitialized())) { |
| 886 | // Non-static or initialized native method missing compiled code, use generic JNI version. |
| 887 | quick_code = GetOatAddress(quick_generic_jni_trampoline_offset_); |
| 888 | } else if (quick_code == nullptr && !method->IsNative()) { |
| 889 | // We don't have code at all for a non-native method, use the interpreter. |
| 890 | quick_code = GetOatAddress(quick_to_interpreter_bridge_offset_); |
| 891 | *quick_is_interpreted = true; |
| 892 | } else { |
| 893 | CHECK(!method->GetDeclaringClass()->IsInitialized()); |
| 894 | // We have code for a static method, but need to go through the resolution stub for class |
| 895 | // initialization. |
| 896 | quick_code = GetOatAddress(quick_resolution_trampoline_offset_); |
| 897 | } |
| 898 | return quick_code; |
| 899 | } |
| 900 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 901 | const uint8_t* ImageWriter::GetQuickEntryPoint(mirror::ArtMethod* method) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 902 | // Calculate the quick entry point following the same logic as FixupMethod() below. |
| 903 | // The resolution method has a special trampoline to call. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 904 | Runtime* runtime = Runtime::Current(); |
| 905 | if (UNLIKELY(method == runtime->GetResolutionMethod())) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 906 | return GetOatAddress(quick_resolution_trampoline_offset_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 907 | } else if (UNLIKELY(method == runtime->GetImtConflictMethod() || |
| 908 | method == runtime->GetImtUnimplementedMethod())) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 909 | return GetOatAddress(quick_imt_conflict_trampoline_offset_); |
| 910 | } else { |
| 911 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 912 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 913 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
| 914 | if (UNLIKELY(method->IsAbstract())) { |
| 915 | return GetOatAddress(quick_to_interpreter_bridge_offset_); |
| 916 | } else { |
| 917 | bool quick_is_interpreted; |
| 918 | return GetQuickCode(method, &quick_is_interpreted); |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 923 | void ImageWriter::FixupMethod(ArtMethod* orig, ArtMethod* copy) { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 924 | // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to |
| 925 | // oat_begin_ |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 926 | // For 64 bit targets we need to repack the current runtime pointer sized fields to the right |
| 927 | // locations. |
| 928 | // Copy all of the fields from the runtime methods to the target methods first since we did a |
| 929 | // bytewise copy earlier. |
| 930 | copy->SetEntryPointFromPortableCompiledCodePtrSize<kVerifyNone>( |
| 931 | orig->GetEntryPointFromPortableCompiledCode(), target_ptr_size_); |
| 932 | copy->SetEntryPointFromInterpreterPtrSize<kVerifyNone>(orig->GetEntryPointFromInterpreter(), |
| 933 | target_ptr_size_); |
| 934 | copy->SetEntryPointFromJniPtrSize<kVerifyNone>(orig->GetEntryPointFromJni(), target_ptr_size_); |
| 935 | copy->SetEntryPointFromQuickCompiledCodePtrSize<kVerifyNone>( |
| 936 | orig->GetEntryPointFromQuickCompiledCode(), target_ptr_size_); |
| 937 | copy->SetNativeGcMapPtrSize<kVerifyNone>(orig->GetNativeGcMap(), target_ptr_size_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 938 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 939 | // The resolution method has a special trampoline to call. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 940 | Runtime* runtime = Runtime::Current(); |
| 941 | if (UNLIKELY(orig == runtime->GetResolutionMethod())) { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 942 | copy->SetEntryPointFromPortableCompiledCodePtrSize<kVerifyNone>( |
| 943 | GetOatAddress(portable_resolution_trampoline_offset_), target_ptr_size_); |
| 944 | copy->SetEntryPointFromQuickCompiledCodePtrSize<kVerifyNone>( |
| 945 | GetOatAddress(quick_resolution_trampoline_offset_), target_ptr_size_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 946 | } else if (UNLIKELY(orig == runtime->GetImtConflictMethod() || |
| 947 | orig == runtime->GetImtUnimplementedMethod())) { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 948 | copy->SetEntryPointFromPortableCompiledCodePtrSize<kVerifyNone>( |
| 949 | GetOatAddress(portable_imt_conflict_trampoline_offset_), target_ptr_size_); |
| 950 | copy->SetEntryPointFromQuickCompiledCodePtrSize<kVerifyNone>( |
| 951 | GetOatAddress(quick_imt_conflict_trampoline_offset_), target_ptr_size_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 952 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 953 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 954 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 955 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
| 956 | if (UNLIKELY(orig->IsAbstract())) { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 957 | copy->SetEntryPointFromPortableCompiledCodePtrSize<kVerifyNone>( |
| 958 | GetOatAddress(portable_to_interpreter_bridge_offset_), target_ptr_size_); |
| 959 | copy->SetEntryPointFromQuickCompiledCodePtrSize<kVerifyNone>( |
| 960 | GetOatAddress(quick_to_interpreter_bridge_offset_), target_ptr_size_); |
| 961 | copy->SetEntryPointFromInterpreterPtrSize<kVerifyNone>( |
| 962 | reinterpret_cast<EntryPointFromInterpreter*>(const_cast<uint8_t*>( |
| 963 | GetOatAddress(interpreter_to_interpreter_bridge_offset_))), target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 964 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 965 | bool quick_is_interpreted; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 966 | const uint8_t* quick_code = GetQuickCode(orig, &quick_is_interpreted); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 967 | copy->SetEntryPointFromQuickCompiledCodePtrSize<kVerifyNone>(quick_code, target_ptr_size_); |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 968 | |
| 969 | // Portable entrypoint: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 970 | const uint8_t* portable_code = GetOatAddress(orig->GetPortableOatCodeOffset()); |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 971 | bool portable_is_interpreted = false; |
| 972 | if (portable_code != nullptr && |
| 973 | (!orig->IsStatic() || orig->IsConstructor() || orig->GetDeclaringClass()->IsInitialized())) { |
| 974 | // We have code for a non-static or initialized method, just use the code. |
| 975 | } else if (portable_code == nullptr && orig->IsNative() && |
| 976 | (!orig->IsStatic() || orig->GetDeclaringClass()->IsInitialized())) { |
| 977 | // Non-static or initialized native method missing compiled code, use generic JNI version. |
| 978 | // TODO: generic JNI support for LLVM. |
| 979 | portable_code = GetOatAddress(portable_resolution_trampoline_offset_); |
| 980 | } else if (portable_code == nullptr && !orig->IsNative()) { |
| 981 | // We don't have code at all for a non-native method, use the interpreter. |
| 982 | portable_code = GetOatAddress(portable_to_interpreter_bridge_offset_); |
| 983 | portable_is_interpreted = true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 984 | } else { |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 985 | CHECK(!orig->GetDeclaringClass()->IsInitialized()); |
| 986 | // We have code for a static method, but need to go through the resolution stub for class |
| 987 | // initialization. |
| 988 | portable_code = GetOatAddress(portable_resolution_trampoline_offset_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 989 | } |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 990 | copy->SetEntryPointFromPortableCompiledCodePtrSize<kVerifyNone>( |
| 991 | portable_code, target_ptr_size_); |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 992 | // JNI entrypoint: |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 993 | if (orig->IsNative()) { |
| 994 | // The native method's pointer is set to a stub to lookup via dlsym. |
| 995 | // Note this is not the code_ pointer, that is handled above. |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 996 | copy->SetEntryPointFromJniPtrSize<kVerifyNone>(GetOatAddress(jni_dlsym_lookup_offset_), |
| 997 | target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 998 | } else { |
| 999 | // Normal (non-abstract non-native) methods have various tables to relocate. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1000 | uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset(); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1001 | const uint8_t* native_gc_map = GetOatAddress(native_gc_map_offset); |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1002 | copy->SetNativeGcMapPtrSize<kVerifyNone>(native_gc_map, target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1003 | } |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 1004 | |
| 1005 | // Interpreter entrypoint: |
| 1006 | // Set the interpreter entrypoint depending on whether there is compiled code or not. |
| 1007 | uint32_t interpreter_code = (quick_is_interpreted && portable_is_interpreted) |
| 1008 | ? interpreter_to_interpreter_bridge_offset_ |
| 1009 | : interpreter_to_compiled_code_bridge_offset_; |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1010 | EntryPointFromInterpreter* interpreter_entrypoint = |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 1011 | reinterpret_cast<EntryPointFromInterpreter*>( |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 1012 | const_cast<uint8_t*>(GetOatAddress(interpreter_code))); |
| 1013 | copy->SetEntryPointFromInterpreterPtrSize<kVerifyNone>( |
| 1014 | interpreter_entrypoint, target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1015 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1019 | static OatHeader* GetOatHeaderFromElf(ElfFile* elf) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1020 | uint64_t data_sec_offset; |
| 1021 | bool has_data_sec = elf->GetSectionOffsetAndSize(".rodata", &data_sec_offset, nullptr); |
| 1022 | if (!has_data_sec) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1023 | return nullptr; |
| 1024 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1025 | return reinterpret_cast<OatHeader*>(elf->Begin() + data_sec_offset); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1028 | void ImageWriter::SetOatChecksumFromElfFile(File* elf_file) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1029 | std::string error_msg; |
| 1030 | std::unique_ptr<ElfFile> elf(ElfFile::Open(elf_file, PROT_READ|PROT_WRITE, |
| 1031 | MAP_SHARED, &error_msg)); |
| 1032 | if (elf.get() == nullptr) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1033 | LOG(FATAL) << "Unable open oat file: " << error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1034 | return; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1035 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1036 | OatHeader* oat_header = GetOatHeaderFromElf(elf.get()); |
| 1037 | CHECK(oat_header != nullptr); |
| 1038 | CHECK(oat_header->IsValid()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1039 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1040 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1041 | image_header->SetOatChecksum(oat_header->GetChecksum()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | } // namespace art |