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