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