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