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 | |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "base/logging.h" |
| 24 | #include "base/unix_file/fd_file.h" |
| 25 | #include "class_linker.h" |
| 26 | #include "compiled_method.h" |
| 27 | #include "dex_file-inl.h" |
| 28 | #include "driver/compiler_driver.h" |
| 29 | #include "elf_writer.h" |
| 30 | #include "gc/accounting/card_table-inl.h" |
| 31 | #include "gc/accounting/heap_bitmap.h" |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 32 | #include "gc/accounting/space_bitmap-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 33 | #include "gc/heap.h" |
| 34 | #include "gc/space/large_object_space.h" |
| 35 | #include "gc/space/space-inl.h" |
| 36 | #include "globals.h" |
| 37 | #include "image.h" |
| 38 | #include "intern_table.h" |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 39 | #include "lock_word.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 40 | #include "mirror/art_field-inl.h" |
| 41 | #include "mirror/art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 42 | #include "mirror/array-inl.h" |
| 43 | #include "mirror/class-inl.h" |
| 44 | #include "mirror/class_loader.h" |
| 45 | #include "mirror/dex_cache-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 46 | #include "mirror/object-inl.h" |
| 47 | #include "mirror/object_array-inl.h" |
| 48 | #include "oat.h" |
| 49 | #include "oat_file.h" |
| 50 | #include "object_utils.h" |
| 51 | #include "runtime.h" |
| 52 | #include "scoped_thread_state_change.h" |
| 53 | #include "sirt_ref.h" |
| 54 | #include "UniquePtr.h" |
| 55 | #include "utils.h" |
| 56 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 57 | using ::art::mirror::ArtField; |
| 58 | using ::art::mirror::ArtMethod; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 59 | using ::art::mirror::Class; |
| 60 | using ::art::mirror::DexCache; |
| 61 | using ::art::mirror::EntryPointFromInterpreter; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 62 | using ::art::mirror::Object; |
| 63 | using ::art::mirror::ObjectArray; |
| 64 | using ::art::mirror::String; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 65 | |
| 66 | namespace art { |
| 67 | |
| 68 | bool ImageWriter::Write(const std::string& image_filename, |
| 69 | uintptr_t image_begin, |
| 70 | const std::string& oat_filename, |
| 71 | const std::string& oat_location) { |
| 72 | CHECK(!image_filename.empty()); |
| 73 | |
| 74 | CHECK_NE(image_begin, 0U); |
| 75 | image_begin_ = reinterpret_cast<byte*>(image_begin); |
| 76 | |
| 77 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 78 | |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 79 | UniquePtr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 80 | if (oat_file.get() == NULL) { |
| 81 | LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location; |
| 82 | return false; |
| 83 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 84 | std::string error_msg; |
| 85 | oat_file_ = OatFile::OpenWritable(oat_file.get(), oat_location, &error_msg); |
| 86 | if (oat_file_ == nullptr) { |
| 87 | LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location |
| 88 | << ": " << error_msg; |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 89 | return false; |
| 90 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 91 | CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 92 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 93 | interpreter_to_interpreter_bridge_offset_ = |
| 94 | oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset(); |
| 95 | interpreter_to_compiled_code_bridge_offset_ = |
| 96 | oat_file_->GetOatHeader().GetInterpreterToCompiledCodeBridgeOffset(); |
| 97 | |
| 98 | jni_dlsym_lookup_offset_ = oat_file_->GetOatHeader().GetJniDlsymLookupOffset(); |
| 99 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 100 | portable_imt_conflict_trampoline_offset_ = |
| 101 | oat_file_->GetOatHeader().GetPortableImtConflictTrampolineOffset(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 102 | portable_resolution_trampoline_offset_ = |
| 103 | oat_file_->GetOatHeader().GetPortableResolutionTrampolineOffset(); |
| 104 | portable_to_interpreter_bridge_offset_ = |
| 105 | oat_file_->GetOatHeader().GetPortableToInterpreterBridgeOffset(); |
| 106 | |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 107 | quick_imt_conflict_trampoline_offset_ = |
| 108 | oat_file_->GetOatHeader().GetQuickImtConflictTrampolineOffset(); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 109 | quick_resolution_trampoline_offset_ = |
| 110 | oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset(); |
| 111 | quick_to_interpreter_bridge_offset_ = |
| 112 | oat_file_->GetOatHeader().GetQuickToInterpreterBridgeOffset(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 113 | { |
| 114 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 115 | PruneNonImageClasses(); // Remove junk |
| 116 | ComputeLazyFieldsForImageClasses(); // Add useful information |
| 117 | ComputeEagerResolvedStrings(); |
| 118 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 119 | } |
| 120 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 121 | heap->CollectGarbage(false); // Remove garbage. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 122 | |
| 123 | if (!AllocMemory()) { |
| 124 | return false; |
| 125 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 126 | |
| 127 | if (kIsDebugBuild) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 128 | ScopedObjectAccess soa(Thread::Current()); |
| 129 | CheckNonImageClassesRemoved(); |
| 130 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 131 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 132 | Thread::Current()->TransitionFromSuspendedToRunnable(); |
| 133 | size_t oat_loaded_size = 0; |
| 134 | size_t oat_data_offset = 0; |
| 135 | ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset); |
| 136 | CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset); |
| 137 | CopyAndFixupObjects(); |
| 138 | PatchOatCodeAndMethods(); |
| 139 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 140 | |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 141 | UniquePtr<File> image_file(OS::CreateEmptyFile(image_filename.c_str())); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 142 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 143 | if (image_file.get() == NULL) { |
| 144 | LOG(ERROR) << "Failed to open image file " << image_filename; |
| 145 | return false; |
| 146 | } |
| 147 | if (fchmod(image_file->Fd(), 0644) != 0) { |
| 148 | PLOG(ERROR) << "Failed to make image file world readable: " << image_filename; |
| 149 | return EXIT_FAILURE; |
| 150 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 151 | |
| 152 | // Write out the image. |
| 153 | CHECK_EQ(image_end_, image_header->GetImageSize()); |
| 154 | if (!image_file->WriteFully(image_->Begin(), image_end_)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 155 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
| 156 | return false; |
| 157 | } |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 158 | |
| 159 | // Write out the image bitmap at the page aligned start of the image end. |
| 160 | CHECK_ALIGNED(image_header->GetImageBitmapOffset(), kPageSize); |
| 161 | if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()), |
| 162 | image_header->GetImageBitmapSize(), |
| 163 | image_header->GetImageBitmapOffset())) { |
| 164 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
| 165 | return false; |
| 166 | } |
| 167 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 171 | void ImageWriter::SetImageOffset(mirror::Object* object, size_t offset) { |
| 172 | DCHECK(object != nullptr); |
| 173 | DCHECK_NE(offset, 0U); |
| 174 | DCHECK(!IsImageOffsetAssigned(object)); |
| 175 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(image_->Begin() + offset); |
| 176 | DCHECK_ALIGNED(obj, kObjectAlignment); |
| 177 | image_bitmap_->Set(obj); |
| 178 | // Before we stomp over the lock word, save the hash code for later. |
| 179 | Monitor::Deflate(Thread::Current(), object);; |
| 180 | LockWord lw(object->GetLockWord()); |
| 181 | switch (lw.GetState()) { |
| 182 | case LockWord::kFatLocked: { |
| 183 | LOG(FATAL) << "Fat locked object " << obj << " found during object copy"; |
| 184 | break; |
| 185 | } |
| 186 | case LockWord::kThinLocked: { |
| 187 | LOG(FATAL) << "Thin locked object " << obj << " found during object copy"; |
| 188 | break; |
| 189 | } |
| 190 | case LockWord::kUnlocked: |
| 191 | // No hash, don't need to save it. |
| 192 | break; |
| 193 | case LockWord::kHashCode: |
| 194 | saved_hashes_.push_back(std::make_pair(obj, lw.GetHashCode())); |
| 195 | break; |
| 196 | default: |
| 197 | LOG(FATAL) << "Unreachable."; |
| 198 | break; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 199 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 200 | object->SetLockWord(LockWord::FromForwardingAddress(offset)); |
| 201 | DCHECK(IsImageOffsetAssigned(object)); |
| 202 | } |
| 203 | |
| 204 | void ImageWriter::AssignImageOffset(mirror::Object* object) { |
| 205 | DCHECK(object != nullptr); |
| 206 | SetImageOffset(object, image_end_); |
| 207 | image_end_ += RoundUp(object->SizeOf(), 8); // 64-bit alignment |
| 208 | DCHECK_LT(image_end_, image_->Size()); |
| 209 | } |
| 210 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 211 | bool ImageWriter::IsImageOffsetAssigned(mirror::Object* object) const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 212 | DCHECK(object != nullptr); |
| 213 | return object->GetLockWord().GetState() == LockWord::kForwardingAddress; |
| 214 | } |
| 215 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 216 | size_t ImageWriter::GetImageOffset(mirror::Object* object) const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 217 | DCHECK(object != nullptr); |
| 218 | DCHECK(IsImageOffsetAssigned(object)); |
| 219 | LockWord lock_word = object->GetLockWord(); |
| 220 | size_t offset = lock_word.ForwardingAddress(); |
| 221 | DCHECK_LT(offset, image_end_); |
| 222 | return offset; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 225 | bool ImageWriter::AllocMemory() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 226 | size_t length = RoundUp(Runtime::Current()->GetHeap()->GetTotalMemory(), kPageSize); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 227 | std::string error_msg; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 228 | image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, PROT_READ | PROT_WRITE, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 229 | true, &error_msg)); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 230 | if (UNLIKELY(image_.get() == nullptr)) { |
| 231 | LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 232 | return false; |
| 233 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 234 | |
| 235 | // Create the image bitmap. |
| 236 | image_bitmap_.reset(gc::accounting::SpaceBitmap::Create("image bitmap", image_->Begin(), |
| 237 | length)); |
| 238 | if (image_bitmap_.get() == nullptr) { |
| 239 | LOG(ERROR) << "Failed to allocate memory for image bitmap"; |
| 240 | return false; |
| 241 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 242 | return true; |
| 243 | } |
| 244 | |
| 245 | void ImageWriter::ComputeLazyFieldsForImageClasses() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 246 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 247 | class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL); |
| 248 | } |
| 249 | |
| 250 | bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) { |
| 251 | c->ComputeName(); |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) { |
| 256 | if (!obj->GetClass()->IsStringClass()) { |
| 257 | return; |
| 258 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 259 | mirror::String* string = obj->AsString(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 260 | const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 261 | for (DexCache* dex_cache : Runtime::Current()->GetClassLinker()->GetDexCaches()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 262 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | 24c534d | 2013-11-14 00:15:00 -0800 | [diff] [blame] | 263 | const DexFile::StringId* string_id; |
| 264 | if (UNLIKELY(string->GetLength() == 0)) { |
| 265 | string_id = dex_file.FindStringId(""); |
| 266 | } else { |
| 267 | string_id = dex_file.FindStringId(utf16_string); |
| 268 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 269 | if (string_id != nullptr) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 270 | // This string occurs in this dex file, assign the dex cache entry. |
| 271 | uint32_t string_idx = dex_file.GetIndexForStringId(*string_id); |
| 272 | if (dex_cache->GetResolvedString(string_idx) == NULL) { |
| 273 | dex_cache->SetResolvedString(string_idx, string); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 279 | void ImageWriter::ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 280 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 281 | Runtime::Current()->GetHeap()->VisitObjects(ComputeEagerResolvedStringsCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 284 | bool ImageWriter::IsImageClass(Class* klass) { |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 285 | return compiler_driver_.IsImageClass(ClassHelper(klass).GetDescriptor()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | struct NonImageClasses { |
| 289 | ImageWriter* image_writer; |
| 290 | std::set<std::string>* non_image_classes; |
| 291 | }; |
| 292 | |
| 293 | void ImageWriter::PruneNonImageClasses() { |
| 294 | if (compiler_driver_.GetImageClasses() == NULL) { |
| 295 | return; |
| 296 | } |
| 297 | Runtime* runtime = Runtime::Current(); |
| 298 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 299 | |
| 300 | // Make a list of classes we would like to prune. |
| 301 | std::set<std::string> non_image_classes; |
| 302 | NonImageClasses context; |
| 303 | context.image_writer = this; |
| 304 | context.non_image_classes = &non_image_classes; |
| 305 | class_linker->VisitClasses(NonImageClassesVisitor, &context); |
| 306 | |
| 307 | // Remove the undesired classes from the class roots. |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 308 | for (const std::string& it : non_image_classes) { |
| 309 | class_linker->RemoveClass(it.c_str(), NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | // Clear references to removed classes from the DexCaches. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 313 | ArtMethod* resolution_method = runtime->GetResolutionMethod(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 314 | for (DexCache* dex_cache : class_linker->GetDexCaches()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 315 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
| 316 | Class* klass = dex_cache->GetResolvedType(i); |
| 317 | if (klass != NULL && !IsImageClass(klass)) { |
| 318 | dex_cache->SetResolvedType(i, NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 322 | ArtMethod* method = dex_cache->GetResolvedMethod(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 323 | if (method != NULL && !IsImageClass(method->GetDeclaringClass())) { |
| 324 | dex_cache->SetResolvedMethod(i, resolution_method); |
| 325 | } |
| 326 | } |
| 327 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 328 | ArtField* field = dex_cache->GetResolvedField(i); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 329 | if (field != NULL && !IsImageClass(field->GetDeclaringClass())) { |
| 330 | dex_cache->SetResolvedField(i, NULL); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) { |
| 337 | NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg); |
| 338 | if (!context->image_writer->IsImageClass(klass)) { |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 339 | context->non_image_classes->insert(ClassHelper(klass).GetDescriptor()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 340 | } |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | void ImageWriter::CheckNonImageClassesRemoved() |
| 345 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 346 | if (compiler_driver_.GetImageClasses() != nullptr) { |
| 347 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 348 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 349 | heap->VisitObjects(CheckNonImageClassesRemovedCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 350 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) { |
| 354 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 355 | if (obj->IsClass()) { |
| 356 | Class* klass = obj->AsClass(); |
| 357 | if (!image_writer->IsImageClass(klass)) { |
| 358 | image_writer->DumpImageClasses(); |
| 359 | CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor() |
| 360 | << " " << PrettyDescriptor(klass); |
| 361 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | void ImageWriter::DumpImageClasses() { |
| 366 | CompilerDriver::DescriptorSet* image_classes = compiler_driver_.GetImageClasses(); |
| 367 | CHECK(image_classes != NULL); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 368 | for (const std::string& image_class : *image_classes) { |
| 369 | LOG(INFO) << " " << image_class; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 373 | void ImageWriter::CalculateObjectOffsets(Object* obj) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 374 | DCHECK(obj != NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 375 | // if it is a string, we want to intern it if its not interned. |
| 376 | if (obj->GetClass()->IsStringClass()) { |
| 377 | // 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] | 378 | if (IsImageOffsetAssigned(obj)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 379 | DCHECK_EQ(obj, obj->AsString()->Intern()); |
| 380 | return; |
| 381 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 382 | Thread* self = Thread::Current(); |
| 383 | SirtRef<Object> sirt_obj(self, obj); |
| 384 | mirror::String* interned = obj->AsString()->Intern(); |
| 385 | if (sirt_obj.get() != interned) { |
| 386 | if (!IsImageOffsetAssigned(interned)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 387 | // interned obj is after us, allocate its location early |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 388 | AssignImageOffset(interned); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 389 | } |
| 390 | // point those looking for this object to the interned version. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 391 | SetImageOffset(sirt_obj.get(), GetImageOffset(interned)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 392 | return; |
| 393 | } |
| 394 | // else (obj == interned), nothing to do but fall through to the normal case |
| 395 | } |
| 396 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 397 | AssignImageOffset(obj); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | ObjectArray<Object>* ImageWriter::CreateImageRoots() const { |
| 401 | Runtime* runtime = Runtime::Current(); |
| 402 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 403 | Thread* self = Thread::Current(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 404 | SirtRef<Class> object_array_class(self, class_linker->FindSystemClass("[Ljava/lang/Object;")); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 405 | |
| 406 | // build an Object[] of all the DexCaches used in the source_space_ |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 407 | ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class.get(), |
| 408 | class_linker->GetDexCaches().size()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 409 | int i = 0; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 410 | for (DexCache* dex_cache : class_linker->GetDexCaches()) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 411 | dex_caches->Set<false>(i++, dex_cache); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | // build an Object[] of the roots needed to restore the runtime |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 415 | SirtRef<ObjectArray<Object> > image_roots( |
| 416 | self, ObjectArray<Object>::Alloc(self, object_array_class.get(), ImageHeader::kImageRootsMax)); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 417 | image_roots->Set<false>(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod()); |
| 418 | image_roots->Set<false>(ImageHeader::kImtConflictMethod, runtime->GetImtConflictMethod()); |
| 419 | image_roots->Set<false>(ImageHeader::kDefaultImt, runtime->GetDefaultImt()); |
| 420 | image_roots->Set<false>(ImageHeader::kCalleeSaveMethod, |
| 421 | runtime->GetCalleeSaveMethod(Runtime::kSaveAll)); |
| 422 | image_roots->Set<false>(ImageHeader::kRefsOnlySaveMethod, |
| 423 | runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)); |
| 424 | image_roots->Set<false>(ImageHeader::kRefsAndArgsSaveMethod, |
| 425 | runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)); |
| 426 | image_roots->Set<false>(ImageHeader::kOatLocation, |
| 427 | String::AllocFromModifiedUtf8(self, oat_file_->GetLocation().c_str())); |
| 428 | image_roots->Set<false>(ImageHeader::kDexCaches, dex_caches); |
| 429 | image_roots->Set<false>(ImageHeader::kClassRoots, class_linker->GetClassRoots()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 430 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 431 | CHECK(image_roots->Get(i) != NULL); |
| 432 | } |
| 433 | return image_roots.get(); |
| 434 | } |
| 435 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 436 | // Walk instance fields of the given Class. Separate function to allow recursion on the super |
| 437 | // class. |
| 438 | void ImageWriter::WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) { |
| 439 | // Visit fields of parent classes first. |
| 440 | SirtRef<mirror::Class> sirt_class(Thread::Current(), klass); |
| 441 | mirror::Class* super = sirt_class->GetSuperClass(); |
| 442 | if (super != nullptr) { |
| 443 | WalkInstanceFields(obj, super); |
| 444 | } |
| 445 | // |
| 446 | size_t num_reference_fields = sirt_class->NumReferenceInstanceFields(); |
| 447 | for (size_t i = 0; i < num_reference_fields; ++i) { |
| 448 | mirror::ArtField* field = sirt_class->GetInstanceField(i); |
| 449 | MemberOffset field_offset = field->GetOffset(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 450 | mirror::Object* value = obj->GetFieldObject<mirror::Object>(field_offset, false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 451 | if (value != nullptr) { |
| 452 | WalkFieldsInOrder(value); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // For an unvisited object, visit it then all its children found via fields. |
| 458 | void ImageWriter::WalkFieldsInOrder(mirror::Object* obj) { |
| 459 | if (!IsImageOffsetAssigned(obj)) { |
| 460 | // Walk instance fields of all objects |
| 461 | Thread* self = Thread::Current(); |
| 462 | SirtRef<mirror::Object> sirt_obj(self, obj); |
| 463 | SirtRef<mirror::Class> klass(self, obj->GetClass()); |
| 464 | // visit the object itself. |
| 465 | CalculateObjectOffsets(sirt_obj.get()); |
| 466 | WalkInstanceFields(sirt_obj.get(), klass.get()); |
| 467 | // Walk static fields of a Class. |
| 468 | if (sirt_obj->IsClass()) { |
| 469 | size_t num_static_fields = klass->NumReferenceStaticFields(); |
| 470 | for (size_t i = 0; i < num_static_fields; ++i) { |
| 471 | mirror::ArtField* field = klass->GetStaticField(i); |
| 472 | MemberOffset field_offset = field->GetOffset(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 473 | mirror::Object* value = sirt_obj->GetFieldObject<mirror::Object>(field_offset, false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 474 | if (value != nullptr) { |
| 475 | WalkFieldsInOrder(value); |
| 476 | } |
| 477 | } |
| 478 | } else if (sirt_obj->IsObjectArray()) { |
| 479 | // Walk elements of an object array. |
| 480 | int32_t length = sirt_obj->AsObjectArray<mirror::Object>()->GetLength(); |
| 481 | for (int32_t i = 0; i < length; i++) { |
| 482 | mirror::ObjectArray<mirror::Object>* obj_array = sirt_obj->AsObjectArray<mirror::Object>(); |
| 483 | mirror::Object* value = obj_array->Get(i); |
| 484 | if (value != nullptr) { |
| 485 | WalkFieldsInOrder(value); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | void ImageWriter::WalkFieldsCallback(mirror::Object* obj, void* arg) { |
| 493 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
| 494 | DCHECK(writer != nullptr); |
| 495 | writer->WalkFieldsInOrder(obj); |
| 496 | } |
| 497 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 498 | void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) { |
| 499 | CHECK_NE(0U, oat_loaded_size); |
| 500 | Thread* self = Thread::Current(); |
| 501 | SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots()); |
| 502 | |
| 503 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 504 | DCHECK_EQ(0U, image_end_); |
| 505 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 506 | // 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] | 507 | // know where image_roots is going to end up |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 508 | image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 509 | |
| 510 | { |
| 511 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 512 | // TODO: Image spaces only? |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 513 | const char* old = self->StartAssertNoThreadSuspension("ImageWriter"); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 514 | DCHECK_LT(image_end_, image_->Size()); |
| 515 | // Clear any pre-existing monitors which may have been in the monitor words. |
| 516 | heap->VisitObjects(WalkFieldsCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 517 | self->EndAssertNoThreadSuspension(old); |
| 518 | } |
| 519 | |
| 520 | const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize); |
| 521 | const byte* oat_file_end = oat_file_begin + oat_loaded_size; |
| 522 | oat_data_begin_ = oat_file_begin + oat_data_offset; |
| 523 | const byte* oat_data_end = oat_data_begin_ + oat_file_->Size(); |
| 524 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 525 | // Return to write header at start of image with future location of image_roots. At this point, |
| 526 | // image_end_ is the size of the image (excluding bitmaps). |
Mathieu Chartier | 5048223 | 2013-11-21 11:48:14 -0800 | [diff] [blame] | 527 | const size_t heap_bytes_per_bitmap_byte = kBitsPerByte * gc::accounting::SpaceBitmap::kAlignment; |
Mathieu Chartier | 12aeccd | 2013-11-13 15:52:06 -0800 | [diff] [blame] | 528 | const size_t bitmap_bytes = RoundUp(image_end_, heap_bytes_per_bitmap_byte) / |
| 529 | heap_bytes_per_bitmap_byte; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 530 | ImageHeader image_header(PointerToLowMemUInt32(image_begin_), |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 531 | static_cast<uint32_t>(image_end_), |
| 532 | RoundUp(image_end_, kPageSize), |
Mathieu Chartier | 12aeccd | 2013-11-13 15:52:06 -0800 | [diff] [blame] | 533 | RoundUp(bitmap_bytes, kPageSize), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 534 | PointerToLowMemUInt32(GetImageAddress(image_roots.get())), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 535 | oat_file_->GetOatHeader().GetChecksum(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 536 | PointerToLowMemUInt32(oat_file_begin), |
| 537 | PointerToLowMemUInt32(oat_data_begin_), |
| 538 | PointerToLowMemUInt32(oat_data_end), |
| 539 | PointerToLowMemUInt32(oat_file_end)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 540 | memcpy(image_->Begin(), &image_header, sizeof(image_header)); |
| 541 | |
| 542 | // Note that image_end_ is left at end of used space |
| 543 | } |
| 544 | |
| 545 | void ImageWriter::CopyAndFixupObjects() |
| 546 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 547 | Thread* self = Thread::Current(); |
| 548 | const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter"); |
| 549 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 550 | // TODO: heap validation can't handle this fix up pass |
| 551 | heap->DisableObjectValidation(); |
| 552 | // TODO: Image spaces only? |
| 553 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 554 | heap->VisitObjects(CopyAndFixupObjectsCallback, this); |
| 555 | // Fix up the object previously had hash codes. |
| 556 | for (const std::pair<mirror::Object*, uint32_t>& hash_pair : saved_hashes_) { |
| 557 | hash_pair.first->SetLockWord(LockWord::FromHashCode(hash_pair.second)); |
| 558 | } |
| 559 | saved_hashes_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 560 | self->EndAssertNoThreadSuspension(old_cause); |
| 561 | } |
| 562 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 563 | void ImageWriter::CopyAndFixupObjectsCallback(Object* obj, void* arg) { |
| 564 | DCHECK(obj != NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 565 | DCHECK(arg != NULL); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 566 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 567 | // see GetLocalAddress for similar computation |
| 568 | size_t offset = image_writer->GetImageOffset(obj); |
| 569 | byte* dst = image_writer->image_->Begin() + offset; |
| 570 | const byte* src = reinterpret_cast<const byte*>(obj); |
| 571 | size_t n = obj->SizeOf(); |
| 572 | DCHECK_LT(offset + n, image_writer->image_->Size()); |
| 573 | memcpy(dst, src, n); |
| 574 | Object* copy = reinterpret_cast<Object*>(dst); |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 575 | // Write in a hash code of objects which have inflated monitors or a hash code in their monitor |
| 576 | // word. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 577 | copy->SetLockWord(LockWord()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 578 | image_writer->FixupObject(obj, copy); |
| 579 | } |
| 580 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 581 | void ImageWriter::FixupObject(Object* orig, Object* copy) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 582 | DCHECK(orig != NULL); |
| 583 | DCHECK(copy != NULL); |
| 584 | copy->SetClass(down_cast<Class*>(GetImageAddress(orig->GetClass()))); |
| 585 | // TODO: special case init of pointers to malloc data (or removal of these pointers) |
| 586 | if (orig->IsClass()) { |
| 587 | FixupClass(orig->AsClass(), down_cast<Class*>(copy)); |
| 588 | } else if (orig->IsObjectArray()) { |
| 589 | FixupObjectArray(orig->AsObjectArray<Object>(), down_cast<ObjectArray<Object>*>(copy)); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 590 | } else if (orig->IsArtMethod()) { |
| 591 | FixupMethod(orig->AsArtMethod(), down_cast<ArtMethod*>(copy)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 592 | } else { |
| 593 | FixupInstanceFields(orig, copy); |
| 594 | } |
| 595 | } |
| 596 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 597 | void ImageWriter::FixupClass(Class* orig, Class* copy) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 598 | FixupInstanceFields(orig, copy); |
| 599 | FixupStaticFields(orig, copy); |
| 600 | } |
| 601 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 602 | void ImageWriter::FixupMethod(ArtMethod* orig, ArtMethod* copy) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 603 | FixupInstanceFields(orig, copy); |
| 604 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 605 | // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to |
| 606 | // oat_begin_ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 607 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 608 | // The resolution method has a special trampoline to call. |
| 609 | if (UNLIKELY(orig == Runtime::Current()->GetResolutionMethod())) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 610 | copy->SetEntryPointFromPortableCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_)); |
| 611 | copy->SetEntryPointFromQuickCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_)); |
Jeff Hao | 88474b4 | 2013-10-23 16:24:40 -0700 | [diff] [blame] | 612 | } else if (UNLIKELY(orig == Runtime::Current()->GetImtConflictMethod())) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 613 | copy->SetEntryPointFromPortableCompiledCode(GetOatAddress(portable_imt_conflict_trampoline_offset_)); |
| 614 | copy->SetEntryPointFromQuickCompiledCode(GetOatAddress(quick_imt_conflict_trampoline_offset_)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 615 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 616 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 617 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 618 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
| 619 | if (UNLIKELY(orig->IsAbstract())) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 620 | copy->SetEntryPointFromPortableCompiledCode(GetOatAddress(portable_to_interpreter_bridge_offset_)); |
| 621 | copy->SetEntryPointFromQuickCompiledCode(GetOatAddress(quick_to_interpreter_bridge_offset_)); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 622 | copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 623 | (const_cast<byte*>(GetOatAddress(interpreter_to_interpreter_bridge_offset_)))); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 624 | } else { |
| 625 | copy->SetEntryPointFromInterpreter(reinterpret_cast<EntryPointFromInterpreter*> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 626 | (const_cast<byte*>(GetOatAddress(interpreter_to_compiled_code_bridge_offset_)))); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 627 | // Use original code if it exists. Otherwise, set the code pointer to the resolution |
| 628 | // trampoline. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 629 | const byte* quick_code = GetOatAddress(orig->GetQuickOatCodeOffset()); |
| 630 | if (quick_code != nullptr) { |
| 631 | copy->SetEntryPointFromQuickCompiledCode(quick_code); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 632 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 633 | copy->SetEntryPointFromQuickCompiledCode(GetOatAddress(quick_resolution_trampoline_offset_)); |
| 634 | } |
| 635 | const byte* portable_code = GetOatAddress(orig->GetPortableOatCodeOffset()); |
| 636 | if (portable_code != nullptr) { |
| 637 | copy->SetEntryPointFromPortableCompiledCode(portable_code); |
| 638 | } else { |
| 639 | copy->SetEntryPointFromPortableCompiledCode(GetOatAddress(portable_resolution_trampoline_offset_)); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 640 | } |
| 641 | if (orig->IsNative()) { |
| 642 | // The native method's pointer is set to a stub to lookup via dlsym. |
| 643 | // Note this is not the code_ pointer, that is handled above. |
| 644 | copy->SetNativeMethod(GetOatAddress(jni_dlsym_lookup_offset_)); |
| 645 | } else { |
| 646 | // Normal (non-abstract non-native) methods have various tables to relocate. |
| 647 | uint32_t mapping_table_off = orig->GetOatMappingTableOffset(); |
| 648 | const byte* mapping_table = GetOatAddress(mapping_table_off); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 649 | copy->SetMappingTable(mapping_table); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 650 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 651 | uint32_t vmap_table_offset = orig->GetOatVmapTableOffset(); |
| 652 | const byte* vmap_table = GetOatAddress(vmap_table_offset); |
Ian Rogers | 1809a72 | 2013-08-09 22:05:32 -0700 | [diff] [blame] | 653 | copy->SetVmapTable(vmap_table); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 654 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 655 | uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset(); |
| 656 | const byte* native_gc_map = GetOatAddress(native_gc_map_offset); |
| 657 | copy->SetNativeGcMap(reinterpret_cast<const uint8_t*>(native_gc_map)); |
| 658 | } |
| 659 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 663 | void ImageWriter::FixupObjectArray(ObjectArray<Object>* orig, ObjectArray<Object>* copy) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 664 | for (int32_t i = 0; i < orig->GetLength(); ++i) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 665 | Object* element = orig->Get(i); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 666 | copy->SetWithoutChecksAndWriteBarrier<false>(i, GetImageAddress(element)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 670 | void ImageWriter::FixupInstanceFields(Object* orig, Object* copy) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 671 | DCHECK(orig != NULL); |
| 672 | DCHECK(copy != NULL); |
| 673 | Class* klass = orig->GetClass(); |
| 674 | DCHECK(klass != NULL); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 675 | FixupFields(orig, copy, klass->GetReferenceInstanceOffsets(), false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 678 | void ImageWriter::FixupStaticFields(Class* orig, Class* copy) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 679 | DCHECK(orig != NULL); |
| 680 | DCHECK(copy != NULL); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 681 | FixupFields(orig, copy, orig->GetReferenceStaticOffsets(), true); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 684 | void ImageWriter::FixupFields(Object* orig, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 685 | Object* copy, |
| 686 | uint32_t ref_offsets, |
| 687 | bool is_static) { |
| 688 | if (ref_offsets != CLASS_WALK_SUPER) { |
| 689 | // Found a reference offset bitmap. Fixup the specified offsets. |
| 690 | while (ref_offsets != 0) { |
| 691 | size_t right_shift = CLZ(ref_offsets); |
| 692 | MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 693 | Object* ref = orig->GetFieldObject<Object>(byte_offset, false); |
| 694 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 695 | // image. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 696 | copy->SetFieldObjectWithoutWriteBarrier<false>(byte_offset, GetImageAddress(ref), false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 697 | ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift); |
| 698 | } |
| 699 | } else { |
| 700 | // There is no reference offset bitmap. In the non-static case, |
| 701 | // walk up the class inheritance hierarchy and find reference |
| 702 | // offsets the hard way. In the static case, just consider this |
| 703 | // class. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 704 | for (Class *klass = is_static ? orig->AsClass() : orig->GetClass(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 705 | klass != NULL; |
| 706 | klass = is_static ? NULL : klass->GetSuperClass()) { |
| 707 | size_t num_reference_fields = (is_static |
| 708 | ? klass->NumReferenceStaticFields() |
| 709 | : klass->NumReferenceInstanceFields()); |
| 710 | for (size_t i = 0; i < num_reference_fields; ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 711 | ArtField* field = (is_static |
| 712 | ? klass->GetStaticField(i) |
| 713 | : klass->GetInstanceField(i)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 714 | MemberOffset field_offset = field->GetOffset(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 715 | Object* ref = orig->GetFieldObject<Object>(field_offset, false); |
| 716 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 717 | // image. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 718 | copy->SetFieldObjectWithoutWriteBarrier<false>(field_offset, GetImageAddress(ref), false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | } |
| 722 | if (!is_static && orig->IsReferenceInstance()) { |
| 723 | // Fix-up referent, that isn't marked as an object field, for References. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 724 | ArtField* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;"); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 725 | MemberOffset field_offset = field->GetOffset(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 726 | Object* ref = orig->GetFieldObject<Object>(field_offset, false); |
| 727 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 728 | // image. |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame^] | 729 | copy->SetFieldObjectWithoutWriteBarrier<false>(field_offset, GetImageAddress(ref), false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 733 | static ArtMethod* GetTargetMethod(const CompilerDriver::CallPatchInformation* patch) |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 734 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 735 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 736 | Thread* self = Thread::Current(); |
| 737 | SirtRef<mirror::DexCache> dex_cache(self, class_linker->FindDexCache(patch->GetDexFile())); |
| 738 | SirtRef<mirror::ClassLoader> class_loader(self, nullptr); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 739 | ArtMethod* method = class_linker->ResolveMethod(patch->GetDexFile(), |
| 740 | patch->GetTargetMethodIdx(), |
| 741 | dex_cache, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 742 | class_loader, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 743 | NULL, |
| 744 | patch->GetTargetInvokeType()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 745 | CHECK(method != NULL) |
| 746 | << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx(); |
| 747 | CHECK(!method->IsRuntimeMethod()) |
| 748 | << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx(); |
| 749 | CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method) |
| 750 | << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " " |
| 751 | << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " " |
| 752 | << PrettyMethod(method); |
| 753 | return method; |
| 754 | } |
| 755 | |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 756 | static Class* GetTargetType(const CompilerDriver::TypePatchInformation* patch) |
| 757 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 758 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 759 | Thread* self = Thread::Current(); |
| 760 | SirtRef<mirror::DexCache> dex_cache(self, class_linker->FindDexCache(patch->GetDexFile())); |
| 761 | SirtRef<mirror::ClassLoader> class_loader(self, nullptr); |
| 762 | Class* klass = class_linker->ResolveType(patch->GetDexFile(), |
| 763 | patch->GetTargetTypeIdx(), |
| 764 | dex_cache, |
| 765 | class_loader); |
| 766 | CHECK(klass != NULL) |
| 767 | << patch->GetDexFile().GetLocation() << " " << patch->GetTargetTypeIdx(); |
| 768 | CHECK(dex_cache->GetResolvedTypes()->Get(patch->GetTargetTypeIdx()) == klass) |
| 769 | << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " " |
| 770 | << PrettyClass(dex_cache->GetResolvedTypes()->Get(patch->GetTargetTypeIdx())) << " " |
| 771 | << PrettyClass(klass); |
| 772 | return klass; |
| 773 | } |
| 774 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 775 | void ImageWriter::PatchOatCodeAndMethods() { |
| 776 | Thread* self = Thread::Current(); |
| 777 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 778 | const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter"); |
| 779 | |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 780 | typedef std::vector<const CompilerDriver::CallPatchInformation*> CallPatches; |
| 781 | const CallPatches& code_to_patch = compiler_driver_.GetCodeToPatch(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 782 | for (size_t i = 0; i < code_to_patch.size(); i++) { |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 783 | const CompilerDriver::CallPatchInformation* patch = code_to_patch[i]; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 784 | ArtMethod* target = GetTargetMethod(patch); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 785 | uintptr_t quick_code = reinterpret_cast<uintptr_t>(class_linker->GetQuickOatCodeFor(target)); |
| 786 | uintptr_t code_base = reinterpret_cast<uintptr_t>(&oat_file_->GetOatHeader()); |
| 787 | uintptr_t code_offset = quick_code - code_base; |
Mark Mendell | 55d0eac | 2014-02-06 11:02:52 -0800 | [diff] [blame] | 788 | if (patch->IsRelative()) { |
| 789 | // value to patch is relative to the location being patched |
| 790 | const void* quick_oat_code = |
| 791 | class_linker->GetQuickOatCodeFor(patch->GetDexFile(), |
| 792 | patch->GetReferrerClassDefIdx(), |
| 793 | patch->GetReferrerMethodIdx()); |
| 794 | uintptr_t base = reinterpret_cast<uintptr_t>(quick_oat_code); |
| 795 | uintptr_t patch_location = base + patch->GetLiteralOffset(); |
| 796 | uintptr_t value = quick_code - patch_location + patch->RelativeOffset(); |
| 797 | SetPatchLocation(patch, value); |
| 798 | } else { |
| 799 | SetPatchLocation(patch, PointerToLowMemUInt32(GetOatAddress(code_offset))); |
| 800 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 803 | const CallPatches& methods_to_patch = compiler_driver_.GetMethodsToPatch(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 804 | for (size_t i = 0; i < methods_to_patch.size(); i++) { |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 805 | const CompilerDriver::CallPatchInformation* patch = methods_to_patch[i]; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 806 | ArtMethod* target = GetTargetMethod(patch); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 807 | SetPatchLocation(patch, PointerToLowMemUInt32(GetImageAddress(target))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 810 | const std::vector<const CompilerDriver::TypePatchInformation*>& classes_to_patch = |
| 811 | compiler_driver_.GetClassesToPatch(); |
| 812 | for (size_t i = 0; i < classes_to_patch.size(); i++) { |
| 813 | const CompilerDriver::TypePatchInformation* patch = classes_to_patch[i]; |
| 814 | Class* target = GetTargetType(patch); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 815 | SetPatchLocation(patch, PointerToLowMemUInt32(GetImageAddress(target))); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 818 | // Update the image header with the new checksum after patching |
| 819 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin()); |
| 820 | image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum()); |
| 821 | self->EndAssertNoThreadSuspension(old_cause); |
| 822 | } |
| 823 | |
| 824 | void ImageWriter::SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value) { |
| 825 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 826 | const void* quick_oat_code = class_linker->GetQuickOatCodeFor(patch->GetDexFile(), |
| 827 | patch->GetReferrerClassDefIdx(), |
| 828 | patch->GetReferrerMethodIdx()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 829 | OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader()); |
| 830 | // TODO: make this Thumb2 specific |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 831 | uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(quick_oat_code) & ~0x1); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 832 | uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 833 | if (kIsDebugBuild) { |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 834 | if (patch->IsCall()) { |
| 835 | const CompilerDriver::CallPatchInformation* cpatch = patch->AsCall(); |
| 836 | const DexFile::MethodId& id = cpatch->GetDexFile().GetMethodId(cpatch->GetTargetMethodIdx()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 837 | uintptr_t expected = reinterpret_cast<uintptr_t>(&id); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 838 | uint32_t actual = *patch_location; |
| 839 | CHECK(actual == expected || actual == value) << std::hex |
| 840 | << "actual=" << actual |
| 841 | << "expected=" << expected |
| 842 | << "value=" << value; |
| 843 | } |
| 844 | if (patch->IsType()) { |
| 845 | const CompilerDriver::TypePatchInformation* tpatch = patch->AsType(); |
| 846 | const DexFile::TypeId& id = tpatch->GetDexFile().GetTypeId(tpatch->GetTargetTypeIdx()); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 847 | uintptr_t expected = reinterpret_cast<uintptr_t>(&id); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 848 | uint32_t actual = *patch_location; |
| 849 | CHECK(actual == expected || actual == value) << std::hex |
| 850 | << "actual=" << actual |
| 851 | << "expected=" << expected |
| 852 | << "value=" << value; |
| 853 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 854 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 855 | *patch_location = value; |
| 856 | oat_header.UpdateChecksum(patch_location, sizeof(value)); |
| 857 | } |
| 858 | |
| 859 | } // namespace art |