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> |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 20 | #include <lz4.h> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 21 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 22 | #include <memory> |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 23 | #include <numeric> |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 24 | #include <unordered_set> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 27 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 28 | #include "art_method-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 29 | #include "base/logging.h" |
| 30 | #include "base/unix_file/fd_file.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 31 | #include "class_linker-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 32 | #include "compiled_method.h" |
| 33 | #include "dex_file-inl.h" |
| 34 | #include "driver/compiler_driver.h" |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 35 | #include "elf_file.h" |
| 36 | #include "elf_utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 37 | #include "elf_writer.h" |
| 38 | #include "gc/accounting/card_table-inl.h" |
| 39 | #include "gc/accounting/heap_bitmap.h" |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 40 | #include "gc/accounting/space_bitmap-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 41 | #include "gc/heap.h" |
| 42 | #include "gc/space/large_object_space.h" |
| 43 | #include "gc/space/space-inl.h" |
| 44 | #include "globals.h" |
| 45 | #include "image.h" |
| 46 | #include "intern_table.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 47 | #include "linear_alloc.h" |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 48 | #include "lock_word.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 49 | #include "mirror/abstract_method.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 50 | #include "mirror/array-inl.h" |
| 51 | #include "mirror/class-inl.h" |
| 52 | #include "mirror/class_loader.h" |
| 53 | #include "mirror/dex_cache-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 54 | #include "mirror/method.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 55 | #include "mirror/object-inl.h" |
| 56 | #include "mirror/object_array-inl.h" |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 57 | #include "mirror/string-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 58 | #include "oat.h" |
| 59 | #include "oat_file.h" |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 60 | #include "oat_file_manager.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 61 | #include "runtime.h" |
| 62 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 63 | #include "handle_scope-inl.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 64 | #include "utils/dex_cache_arrays_layout-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 65 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 66 | using ::art::mirror::Class; |
| 67 | using ::art::mirror::DexCache; |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 68 | using ::art::mirror::Object; |
| 69 | using ::art::mirror::ObjectArray; |
| 70 | using ::art::mirror::String; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 71 | |
| 72 | namespace art { |
| 73 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 74 | // Separate objects into multiple bins to optimize dirty memory use. |
| 75 | static constexpr bool kBinObjects = true; |
| 76 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 77 | // Return true if an object is already in an image space. |
| 78 | bool ImageWriter::IsInBootImage(const void* obj) const { |
| 79 | if (!compile_app_image_) { |
| 80 | DCHECK(boot_image_space_ == nullptr); |
| 81 | return false; |
| 82 | } |
| 83 | const uint8_t* image_begin = boot_image_space_->Begin(); |
| 84 | // Real image end including ArtMethods and ArtField sections. |
| 85 | const uint8_t* image_end = image_begin + boot_image_space_->GetImageHeader().GetImageSize(); |
| 86 | return image_begin <= obj && obj < image_end; |
| 87 | } |
| 88 | |
| 89 | bool ImageWriter::IsInBootOatFile(const void* ptr) const { |
| 90 | if (!compile_app_image_) { |
| 91 | DCHECK(boot_image_space_ == nullptr); |
| 92 | return false; |
| 93 | } |
| 94 | const ImageHeader& image_header = boot_image_space_->GetImageHeader(); |
| 95 | return image_header.GetOatFileBegin() <= ptr && ptr < image_header.GetOatFileEnd(); |
| 96 | } |
| 97 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 98 | static void CheckNoDexObjectsCallback(Object* obj, void* arg ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 99 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 100 | Class* klass = obj->GetClass(); |
| 101 | CHECK_NE(PrettyClass(klass), "com.android.dex.Dex"); |
| 102 | } |
| 103 | |
| 104 | static void CheckNoDexObjects() { |
| 105 | ScopedObjectAccess soa(Thread::Current()); |
| 106 | Runtime::Current()->GetHeap()->VisitObjects(CheckNoDexObjectsCallback, nullptr); |
| 107 | } |
| 108 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 109 | bool ImageWriter::PrepareImageAddressSpace() { |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 110 | target_ptr_size_ = InstructionSetPointerSize(compiler_driver_.GetInstructionSet()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 111 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 112 | // Cache boot image space. |
| 113 | for (gc::space::ContinuousSpace* space : heap->GetContinuousSpaces()) { |
| 114 | if (space->IsImageSpace()) { |
| 115 | CHECK(compile_app_image_); |
| 116 | CHECK(boot_image_space_ == nullptr) << "Multiple image spaces"; |
| 117 | boot_image_space_ = space->AsImageSpace(); |
| 118 | } |
| 119 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 120 | { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 121 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 122 | PruneNonImageClasses(); // Remove junk |
| 123 | ComputeLazyFieldsForImageClasses(); // Add useful information |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 124 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 125 | heap->CollectGarbage(false); // Remove garbage. |
| 126 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 127 | // Dex caches must not have their dex fields set in the image. These are memory buffers of mapped |
| 128 | // dex files. |
| 129 | // |
| 130 | // We may open them in the unstarted-runtime code for class metadata. Their fields should all be |
| 131 | // reset in PruneNonImageClasses and the objects reclaimed in the GC. Make sure that's actually |
| 132 | // true. |
| 133 | if (kIsDebugBuild) { |
| 134 | CheckNoDexObjects(); |
| 135 | } |
| 136 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 137 | if (kIsDebugBuild) { |
| 138 | ScopedObjectAccess soa(Thread::Current()); |
| 139 | CheckNonImageClassesRemoved(); |
| 140 | } |
| 141 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 142 | { |
| 143 | ScopedObjectAccess soa(Thread::Current()); |
| 144 | CalculateNewObjectOffsets(); |
| 145 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 146 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 147 | // This needs to happen after CalculateNewObjectOffsets since it relies on intern_table_bytes_ and |
| 148 | // bin size sums being calculated. |
| 149 | if (!AllocMemory()) { |
| 150 | return false; |
| 151 | } |
| 152 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
Mathieu Chartier | a90c772 | 2015-10-29 15:41:36 -0700 | [diff] [blame] | 156 | bool ImageWriter::Write(int image_fd, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 157 | const std::vector<const char*>& image_filenames, |
| 158 | const std::vector<const char*>& oat_filenames) { |
| 159 | CHECK(!image_filenames.empty()); |
| 160 | CHECK(!oat_filenames.empty()); |
| 161 | CHECK_EQ(image_filenames.size(), oat_filenames.size()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 162 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 163 | size_t oat_file_offset = 0; |
| 164 | |
| 165 | for (size_t i = 0; i < oat_filenames.size(); ++i) { |
| 166 | const char* oat_filename = oat_filenames[i]; |
| 167 | std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename)); |
| 168 | if (oat_file.get() == nullptr) { |
| 169 | PLOG(ERROR) << "Failed to open oat file " << oat_filename; |
| 170 | return false; |
| 171 | } |
| 172 | std::string error_msg; |
| 173 | oat_file_ = OatFile::OpenReadable(oat_file.get(), oat_filename, nullptr, &error_msg); |
| 174 | if (oat_file_ == nullptr) { |
| 175 | PLOG(ERROR) << "Failed to open writable oat file " << oat_filename; |
| 176 | oat_file->Erase(); |
| 177 | return false; |
| 178 | } |
| 179 | Runtime::Current()->GetOatFileManager().RegisterOatFile( |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 180 | std::unique_ptr<const OatFile>(oat_file_)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 181 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 182 | const OatHeader& oat_header = oat_file_->GetOatHeader(); |
| 183 | ImageInfo& image_info = GetImageInfo(oat_filename); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 184 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 185 | size_t oat_loaded_size = 0; |
| 186 | size_t oat_data_offset = 0; |
| 187 | ElfWriter::GetOatElfInformation(oat_file.get(), &oat_loaded_size, &oat_data_offset); |
| 188 | |
| 189 | DCHECK_EQ(image_info.oat_offset_, oat_file_offset); |
| 190 | oat_file_offset += oat_loaded_size; |
| 191 | |
| 192 | if (i == 0) { |
| 193 | // Primary oat file, read the trampolines. |
| 194 | image_info.oat_address_offsets_[kOatAddressInterpreterToInterpreterBridge] = |
| 195 | oat_header.GetInterpreterToInterpreterBridgeOffset(); |
| 196 | image_info.oat_address_offsets_[kOatAddressInterpreterToCompiledCodeBridge] = |
| 197 | oat_header.GetInterpreterToCompiledCodeBridgeOffset(); |
| 198 | image_info.oat_address_offsets_[kOatAddressJNIDlsymLookup] = |
| 199 | oat_header.GetJniDlsymLookupOffset(); |
| 200 | image_info.oat_address_offsets_[kOatAddressQuickGenericJNITrampoline] = |
| 201 | oat_header.GetQuickGenericJniTrampolineOffset(); |
| 202 | image_info.oat_address_offsets_[kOatAddressQuickIMTConflictTrampoline] = |
| 203 | oat_header.GetQuickImtConflictTrampolineOffset(); |
| 204 | image_info.oat_address_offsets_[kOatAddressQuickResolutionTrampoline] = |
| 205 | oat_header.GetQuickResolutionTrampolineOffset(); |
| 206 | image_info.oat_address_offsets_[kOatAddressQuickToInterpreterBridge] = |
| 207 | oat_header.GetQuickToInterpreterBridgeOffset(); |
| 208 | } else { |
| 209 | // Other oat files use the primary trampolines. |
| 210 | // TODO: Dummy values to protect usage? b/26317072 |
| 211 | } |
| 212 | |
| 213 | |
| 214 | { |
| 215 | ScopedObjectAccess soa(Thread::Current()); |
| 216 | CreateHeader(oat_loaded_size, oat_data_offset); |
| 217 | CopyAndFixupNativeData(); |
| 218 | } |
| 219 | |
| 220 | SetOatChecksumFromElfFile(oat_file.get()); |
| 221 | |
| 222 | if (oat_file->FlushCloseOrErase() != 0) { |
| 223 | LOG(ERROR) << "Failed to flush and close oat file " << oat_filename; |
| 224 | return false; |
| 225 | } |
| 226 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 227 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 228 | { |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 229 | // TODO: heap validation can't handle these fix up passes. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 230 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 231 | Runtime::Current()->GetHeap()->DisableObjectValidation(); |
| 232 | CopyAndFixupObjects(); |
| 233 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 234 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 235 | for (size_t i = 0; i < image_filenames.size(); ++i) { |
| 236 | const char* image_filename = image_filenames[i]; |
| 237 | const char* oat_filename = oat_filenames[i]; |
| 238 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 239 | std::unique_ptr<File> image_file; |
| 240 | if (image_fd != kInvalidImageFd) { |
| 241 | image_file.reset(new File(image_fd, image_filename, unix_file::kCheckSafeUsage)); |
| 242 | } else { |
| 243 | image_file.reset(OS::CreateEmptyFile(image_filename)); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 244 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 245 | if (image_file == nullptr) { |
| 246 | LOG(ERROR) << "Failed to open image file " << image_filename; |
| 247 | return false; |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 248 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 249 | if (fchmod(image_file->Fd(), 0644) != 0) { |
| 250 | PLOG(ERROR) << "Failed to make image file world readable: " << image_filename; |
| 251 | image_file->Erase(); |
| 252 | return EXIT_FAILURE; |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 253 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 254 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 255 | std::unique_ptr<char[]> compressed_data; |
| 256 | // Image data size excludes the bitmap and the header. |
| 257 | ImageHeader* const image_header = reinterpret_cast<ImageHeader*>(image_info.image_->Begin()); |
| 258 | const size_t image_data_size = image_header->GetImageSize() - sizeof(ImageHeader); |
| 259 | char* image_data = reinterpret_cast<char*>(image_info.image_->Begin()) + sizeof(ImageHeader); |
| 260 | size_t data_size; |
| 261 | const char* image_data_to_write; |
Nicolas Geoffray | 83d4d72 | 2015-12-10 08:26:32 +0000 | [diff] [blame] | 262 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 263 | CHECK_EQ(image_header->storage_mode_, image_storage_mode_); |
| 264 | switch (image_storage_mode_) { |
| 265 | case ImageHeader::kStorageModeLZ4: { |
| 266 | size_t compressed_max_size = LZ4_compressBound(image_data_size); |
| 267 | compressed_data.reset(new char[compressed_max_size]); |
| 268 | data_size = LZ4_compress( |
| 269 | reinterpret_cast<char*>(image_info.image_->Begin()) + sizeof(ImageHeader), |
| 270 | &compressed_data[0], |
| 271 | image_data_size); |
| 272 | image_data_to_write = &compressed_data[0]; |
| 273 | VLOG(compiler) << "Compressed from " << image_data_size << " to " << data_size; |
| 274 | break; |
| 275 | } |
| 276 | case ImageHeader::kStorageModeUncompressed: { |
| 277 | data_size = image_data_size; |
| 278 | image_data_to_write = image_data; |
| 279 | break; |
| 280 | } |
| 281 | default: { |
| 282 | LOG(FATAL) << "Unsupported"; |
| 283 | UNREACHABLE(); |
| 284 | } |
| 285 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 286 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 287 | // Write header first, as uncompressed. |
| 288 | image_header->data_size_ = data_size; |
| 289 | if (!image_file->WriteFully(image_info.image_->Begin(), sizeof(ImageHeader))) { |
| 290 | PLOG(ERROR) << "Failed to write image file header " << image_filename; |
| 291 | image_file->Erase(); |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | // Write out the image + fields + methods. |
| 296 | const bool is_compressed = compressed_data != nullptr; |
| 297 | if (!image_file->WriteFully(image_data_to_write, data_size)) { |
| 298 | PLOG(ERROR) << "Failed to write image file data " << image_filename; |
| 299 | image_file->Erase(); |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | // Write out the image bitmap at the page aligned start of the image end, also uncompressed for |
| 304 | // convenience. |
| 305 | const ImageSection& bitmap_section = image_header->GetImageSection( |
| 306 | ImageHeader::kSectionImageBitmap); |
| 307 | // Align up since data size may be unaligned if the image is compressed. |
| 308 | size_t bitmap_position_in_file = RoundUp(sizeof(ImageHeader) + data_size, kPageSize); |
| 309 | if (!is_compressed) { |
| 310 | CHECK_EQ(bitmap_position_in_file, bitmap_section.Offset()); |
| 311 | } |
| 312 | if (!image_file->Write(reinterpret_cast<char*>(image_info.image_bitmap_->Begin()), |
| 313 | bitmap_section.Size(), |
| 314 | bitmap_position_in_file)) { |
| 315 | PLOG(ERROR) << "Failed to write image file " << image_filename; |
| 316 | image_file->Erase(); |
| 317 | return false; |
| 318 | } |
| 319 | CHECK_EQ(bitmap_position_in_file + bitmap_section.Size(), |
| 320 | static_cast<size_t>(image_file->GetLength())); |
| 321 | if (image_file->FlushCloseOrErase() != 0) { |
| 322 | PLOG(ERROR) << "Failed to flush and close image file " << image_filename; |
| 323 | return false; |
| 324 | } |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 325 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 326 | return true; |
| 327 | } |
| 328 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 329 | void ImageWriter::SetImageOffset(mirror::Object* object, size_t offset) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 330 | DCHECK(object != nullptr); |
| 331 | DCHECK_NE(offset, 0U); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 332 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 333 | // 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] | 334 | object->SetLockWord(LockWord::FromForwardingAddress(offset), false); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 335 | DCHECK_EQ(object->GetLockWord(false).ReadBarrierState(), 0u); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 336 | DCHECK(IsImageOffsetAssigned(object)); |
| 337 | } |
| 338 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 339 | void ImageWriter::UpdateImageOffset(mirror::Object* obj, uintptr_t offset) { |
| 340 | DCHECK(IsImageOffsetAssigned(obj)) << obj << " " << offset; |
| 341 | obj->SetLockWord(LockWord::FromForwardingAddress(offset), false); |
| 342 | DCHECK_EQ(obj->GetLockWord(false).ReadBarrierState(), 0u); |
| 343 | } |
| 344 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 345 | void ImageWriter::AssignImageOffset(mirror::Object* object, ImageWriter::BinSlot bin_slot) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 346 | DCHECK(object != nullptr); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 347 | DCHECK_NE(image_objects_offset_begin_, 0u); |
| 348 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 349 | const char* oat_filename = GetOatFilename(object); |
| 350 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 351 | size_t bin_slot_offset = image_info.bin_slot_offsets_[bin_slot.GetBin()]; |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 352 | size_t new_offset = bin_slot_offset + bin_slot.GetIndex(); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 353 | DCHECK_ALIGNED(new_offset, kObjectAlignment); |
| 354 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 355 | SetImageOffset(object, new_offset); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 356 | DCHECK_LT(new_offset, image_info.image_end_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 359 | bool ImageWriter::IsImageOffsetAssigned(mirror::Object* object) const { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 360 | // 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] | 361 | DCHECK(object != nullptr); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 362 | return object->GetLockWord(false).GetState() == LockWord::kForwardingAddress; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 365 | size_t ImageWriter::GetImageOffset(mirror::Object* object) const { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 366 | DCHECK(object != nullptr); |
| 367 | DCHECK(IsImageOffsetAssigned(object)); |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 368 | LockWord lock_word = object->GetLockWord(false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 369 | size_t offset = lock_word.ForwardingAddress(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 370 | const char* oat_filename = GetOatFilename(object); |
| 371 | const ImageInfo& image_info = GetConstImageInfo(oat_filename); |
| 372 | DCHECK_LT(offset, image_info.image_end_); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 373 | return offset; |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 376 | void ImageWriter::SetImageBinSlot(mirror::Object* object, BinSlot bin_slot) { |
| 377 | DCHECK(object != nullptr); |
| 378 | DCHECK(!IsImageOffsetAssigned(object)); |
| 379 | DCHECK(!IsImageBinSlotAssigned(object)); |
| 380 | |
| 381 | // Before we stomp over the lock word, save the hash code for later. |
| 382 | Monitor::Deflate(Thread::Current(), object);; |
| 383 | LockWord lw(object->GetLockWord(false)); |
| 384 | switch (lw.GetState()) { |
| 385 | case LockWord::kFatLocked: { |
| 386 | LOG(FATAL) << "Fat locked object " << object << " found during object copy"; |
| 387 | break; |
| 388 | } |
| 389 | case LockWord::kThinLocked: { |
| 390 | LOG(FATAL) << "Thin locked object " << object << " found during object copy"; |
| 391 | break; |
| 392 | } |
| 393 | case LockWord::kUnlocked: |
| 394 | // No hash, don't need to save it. |
| 395 | break; |
| 396 | case LockWord::kHashCode: |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 397 | DCHECK(saved_hashcode_map_.find(object) == saved_hashcode_map_.end()); |
| 398 | saved_hashcode_map_.emplace(object, lw.GetHashCode()); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 399 | break; |
| 400 | default: |
| 401 | LOG(FATAL) << "Unreachable."; |
| 402 | UNREACHABLE(); |
| 403 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 404 | object->SetLockWord(LockWord::FromForwardingAddress(bin_slot.Uint32Value()), false); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 405 | DCHECK_EQ(object->GetLockWord(false).ReadBarrierState(), 0u); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 406 | DCHECK(IsImageBinSlotAssigned(object)); |
| 407 | } |
| 408 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 409 | void ImageWriter::PrepareDexCacheArraySlots() { |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 410 | // Prepare dex cache array starts based on the ordering specified in the CompilerDriver. |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 411 | // Set the slot size early to avoid DCHECK() failures in IsImageBinSlotAssigned() |
| 412 | // when AssignImageBinSlot() assigns their indexes out or order. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 413 | for (const DexFile* dex_file : compiler_driver_.GetDexFilesForOatFile()) { |
| 414 | auto it = dex_file_oat_filename_map_.find(dex_file); |
| 415 | DCHECK(it != dex_file_oat_filename_map_.end()) << dex_file->GetLocation(); |
| 416 | ImageInfo& image_info = GetImageInfo(it->second); |
| 417 | image_info.dex_cache_array_starts_.Put(dex_file, image_info.bin_slot_sizes_[kBinDexCacheArray]); |
| 418 | DexCacheArraysLayout layout(target_ptr_size_, dex_file); |
| 419 | image_info.bin_slot_sizes_[kBinDexCacheArray] += layout.Size(); |
| 420 | } |
Vladimir Marko | f60c7e2 | 2015-11-23 18:05:08 +0000 | [diff] [blame] | 421 | |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 422 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 423 | Thread* const self = Thread::Current(); |
| 424 | ReaderMutexLock mu(self, *class_linker->DexLock()); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 425 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 426 | mirror::DexCache* dex_cache = |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 427 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 428 | if (dex_cache == nullptr || IsInBootImage(dex_cache)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 429 | continue; |
| 430 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 431 | const DexFile* dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 432 | DexCacheArraysLayout layout(target_ptr_size_, dex_file); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 433 | DCHECK(layout.Valid()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 434 | const char* oat_filename = GetOatFilenameForDexCache(dex_cache); |
| 435 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 436 | uint32_t start = image_info.dex_cache_array_starts_.Get(dex_file); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 437 | DCHECK_EQ(dex_file->NumTypeIds() != 0u, dex_cache->GetResolvedTypes() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 438 | AddDexCacheArrayRelocation(dex_cache->GetResolvedTypes(), |
| 439 | start + layout.TypesOffset(), |
| 440 | dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 441 | DCHECK_EQ(dex_file->NumMethodIds() != 0u, dex_cache->GetResolvedMethods() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 442 | AddDexCacheArrayRelocation(dex_cache->GetResolvedMethods(), |
| 443 | start + layout.MethodsOffset(), |
| 444 | dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 445 | DCHECK_EQ(dex_file->NumFieldIds() != 0u, dex_cache->GetResolvedFields() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 446 | AddDexCacheArrayRelocation(dex_cache->GetResolvedFields(), |
| 447 | start + layout.FieldsOffset(), |
| 448 | dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 449 | DCHECK_EQ(dex_file->NumStringIds() != 0u, dex_cache->GetStrings() != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 450 | AddDexCacheArrayRelocation(dex_cache->GetStrings(), start + layout.StringsOffset(), dex_cache); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 451 | } |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 454 | void ImageWriter::AddDexCacheArrayRelocation(void* array, size_t offset, DexCache* dex_cache) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 455 | if (array != nullptr) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 456 | DCHECK(!IsInBootImage(array)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 457 | const char* oat_filename = GetOatFilenameForDexCache(dex_cache); |
| 458 | native_object_relocations_.emplace(array, |
| 459 | NativeObjectRelocation { oat_filename, offset, kNativeObjectRelocationTypeDexCacheArray }); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 463 | void ImageWriter::AddMethodPointerArray(mirror::PointerArray* arr) { |
| 464 | DCHECK(arr != nullptr); |
| 465 | if (kIsDebugBuild) { |
| 466 | for (size_t i = 0, len = arr->GetLength(); i < len; i++) { |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 467 | ArtMethod* method = arr->GetElementPtrSize<ArtMethod*>(i, target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 468 | if (method != nullptr && !method->IsRuntimeMethod()) { |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 469 | mirror::Class* klass = method->GetDeclaringClass(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 470 | CHECK(klass == nullptr || KeepClass(klass)) |
| 471 | << PrettyClass(klass) << " should be a kept class"; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | } |
| 475 | // kBinArtMethodClean picked arbitrarily, just required to differentiate between ArtFields and |
| 476 | // ArtMethods. |
| 477 | pointer_arrays_.emplace(arr, kBinArtMethodClean); |
| 478 | } |
| 479 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 480 | void ImageWriter::AssignImageBinSlot(mirror::Object* object) { |
| 481 | DCHECK(object != nullptr); |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 482 | size_t object_size = object->SizeOf(); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 483 | |
| 484 | // The magic happens here. We segregate objects into different bins based |
| 485 | // on how likely they are to get dirty at runtime. |
| 486 | // |
| 487 | // Likely-to-dirty objects get packed together into the same bin so that |
| 488 | // at runtime their page dirtiness ratio (how many dirty objects a page has) is |
| 489 | // maximized. |
| 490 | // |
| 491 | // This means more pages will stay either clean or shared dirty (with zygote) and |
| 492 | // the app will use less of its own (private) memory. |
| 493 | Bin bin = kBinRegular; |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 494 | size_t current_offset = 0u; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 495 | |
| 496 | if (kBinObjects) { |
| 497 | // |
| 498 | // Changing the bin of an object is purely a memory-use tuning. |
| 499 | // It has no change on runtime correctness. |
| 500 | // |
| 501 | // Memory analysis has determined that the following types of objects get dirtied |
| 502 | // the most: |
| 503 | // |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 504 | // * Dex cache arrays are stored in a special bin. The arrays for each dex cache have |
| 505 | // a fixed layout which helps improve generated code (using PC-relative addressing), |
| 506 | // so we pre-calculate their offsets separately in PrepareDexCacheArraySlots(). |
| 507 | // Since these arrays are huge, most pages do not overlap other objects and it's not |
| 508 | // really important where they are for the clean/dirty separation. Due to their |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 509 | // special PC-relative addressing, we arbitrarily keep them at the end. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 510 | // * Class'es which are verified [their clinit runs only at runtime] |
| 511 | // - classes in general [because their static fields get overwritten] |
| 512 | // - initialized classes with all-final statics are unlikely to be ever dirty, |
| 513 | // so bin them separately |
| 514 | // * Art Methods that are: |
| 515 | // - native [their native entry point is not looked up until runtime] |
| 516 | // - have declaring classes that aren't initialized |
| 517 | // [their interpreter/quick entry points are trampolines until the class |
| 518 | // becomes initialized] |
| 519 | // |
| 520 | // We also assume the following objects get dirtied either never or extremely rarely: |
| 521 | // * Strings (they are immutable) |
| 522 | // * Art methods that aren't native and have initialized declared classes |
| 523 | // |
| 524 | // We assume that "regular" bin objects are highly unlikely to become dirtied, |
| 525 | // so packing them together will not result in a noticeably tighter dirty-to-clean ratio. |
| 526 | // |
| 527 | if (object->IsClass()) { |
| 528 | bin = kBinClassVerified; |
| 529 | mirror::Class* klass = object->AsClass(); |
| 530 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 531 | // Add non-embedded vtable to the pointer array table if there is one. |
| 532 | auto* vtable = klass->GetVTable(); |
| 533 | if (vtable != nullptr) { |
| 534 | AddMethodPointerArray(vtable); |
| 535 | } |
| 536 | auto* iftable = klass->GetIfTable(); |
| 537 | if (iftable != nullptr) { |
| 538 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 539 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 540 | AddMethodPointerArray(iftable->GetMethodArray(i)); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 545 | if (klass->GetStatus() == Class::kStatusInitialized) { |
| 546 | bin = kBinClassInitialized; |
| 547 | |
| 548 | // If the class's static fields are all final, put it into a separate bin |
| 549 | // since it's very likely it will stay clean. |
| 550 | uint32_t num_static_fields = klass->NumStaticFields(); |
| 551 | if (num_static_fields == 0) { |
| 552 | bin = kBinClassInitializedFinalStatics; |
| 553 | } else { |
| 554 | // Maybe all the statics are final? |
| 555 | bool all_final = true; |
| 556 | for (uint32_t i = 0; i < num_static_fields; ++i) { |
| 557 | ArtField* field = klass->GetStaticField(i); |
| 558 | if (!field->IsFinal()) { |
| 559 | all_final = false; |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | if (all_final) { |
| 565 | bin = kBinClassInitializedFinalStatics; |
| 566 | } |
| 567 | } |
| 568 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 569 | } else if (object->GetClass<kVerifyNone>()->IsStringClass()) { |
| 570 | bin = kBinString; // Strings are almost always immutable (except for object header). |
| 571 | } // else bin = kBinRegular |
| 572 | } |
| 573 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 574 | const char* oat_filename = GetOatFilename(object); |
| 575 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 576 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 577 | size_t offset_delta = RoundUp(object_size, kObjectAlignment); // 64-bit alignment |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 578 | current_offset = image_info.bin_slot_sizes_[bin]; // How many bytes the current bin is at (aligned). |
| 579 | // Move the current bin size up to accommodate the object we just assigned a bin slot. |
| 580 | image_info.bin_slot_sizes_[bin] += offset_delta; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 581 | |
| 582 | BinSlot new_bin_slot(bin, current_offset); |
| 583 | SetImageBinSlot(object, new_bin_slot); |
| 584 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 585 | ++image_info.bin_slot_count_[bin]; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 586 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 587 | // Grow the image closer to the end by the object we just assigned. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 588 | image_info.image_end_ += offset_delta; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 591 | bool ImageWriter::WillMethodBeDirty(ArtMethod* m) const { |
| 592 | if (m->IsNative()) { |
| 593 | return true; |
| 594 | } |
| 595 | mirror::Class* declaring_class = m->GetDeclaringClass(); |
| 596 | // Initialized is highly unlikely to dirty since there's no entry points to mutate. |
| 597 | return declaring_class == nullptr || declaring_class->GetStatus() != Class::kStatusInitialized; |
| 598 | } |
| 599 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 600 | bool ImageWriter::IsImageBinSlotAssigned(mirror::Object* object) const { |
| 601 | DCHECK(object != nullptr); |
| 602 | |
| 603 | // We always stash the bin slot into a lockword, in the 'forwarding address' state. |
| 604 | // If it's in some other state, then we haven't yet assigned an image bin slot. |
| 605 | if (object->GetLockWord(false).GetState() != LockWord::kForwardingAddress) { |
| 606 | return false; |
| 607 | } else if (kIsDebugBuild) { |
| 608 | LockWord lock_word = object->GetLockWord(false); |
| 609 | size_t offset = lock_word.ForwardingAddress(); |
| 610 | BinSlot bin_slot(offset); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 611 | const char* oat_filename = GetOatFilename(object); |
| 612 | const ImageInfo& image_info = GetConstImageInfo(oat_filename); |
| 613 | DCHECK_LT(bin_slot.GetIndex(), image_info.bin_slot_sizes_[bin_slot.GetBin()]) |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 614 | << "bin slot offset should not exceed the size of that bin"; |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 615 | } |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | ImageWriter::BinSlot ImageWriter::GetImageBinSlot(mirror::Object* object) const { |
| 620 | DCHECK(object != nullptr); |
| 621 | DCHECK(IsImageBinSlotAssigned(object)); |
| 622 | |
| 623 | LockWord lock_word = object->GetLockWord(false); |
| 624 | size_t offset = lock_word.ForwardingAddress(); // TODO: ForwardingAddress should be uint32_t |
| 625 | DCHECK_LE(offset, std::numeric_limits<uint32_t>::max()); |
| 626 | |
| 627 | BinSlot bin_slot(static_cast<uint32_t>(offset)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 628 | const char* oat_filename = GetOatFilename(object); |
| 629 | const ImageInfo& image_info = GetConstImageInfo(oat_filename); |
| 630 | DCHECK_LT(bin_slot.GetIndex(), image_info.bin_slot_sizes_[bin_slot.GetBin()]); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 631 | |
| 632 | return bin_slot; |
| 633 | } |
| 634 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 635 | bool ImageWriter::AllocMemory() { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 636 | for (const char* oat_filename : oat_filenames_) { |
| 637 | ImageInfo& image_info = GetImageInfo(oat_filename); |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 638 | ImageSection unused_sections[ImageHeader::kSectionCount]; |
| 639 | const size_t length = RoundUp( |
| 640 | image_info.CreateImageSections(target_ptr_size_, unused_sections), |
| 641 | kPageSize); |
| 642 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 643 | std::string error_msg; |
| 644 | image_info.image_.reset(MemMap::MapAnonymous("image writer image", |
| 645 | nullptr, |
| 646 | length, |
| 647 | PROT_READ | PROT_WRITE, |
| 648 | false, |
| 649 | false, |
| 650 | &error_msg)); |
| 651 | if (UNLIKELY(image_info.image_.get() == nullptr)) { |
| 652 | LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg; |
| 653 | return false; |
| 654 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 655 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 656 | // Create the image bitmap, only needs to cover mirror object section which is up to image_end_. |
| 657 | CHECK_LE(image_info.image_end_, length); |
| 658 | image_info.image_bitmap_.reset(gc::accounting::ContinuousSpaceBitmap::Create( |
| 659 | "image bitmap", image_info.image_->Begin(), RoundUp(image_info.image_end_, kPageSize))); |
| 660 | if (image_info.image_bitmap_.get() == nullptr) { |
| 661 | LOG(ERROR) << "Failed to allocate memory for image bitmap"; |
| 662 | return false; |
| 663 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 664 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 665 | return true; |
| 666 | } |
| 667 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 668 | class ComputeLazyFieldsForClassesVisitor : public ClassVisitor { |
| 669 | public: |
| 670 | bool Visit(Class* c) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
| 671 | StackHandleScope<1> hs(Thread::Current()); |
| 672 | mirror::Class::ComputeName(hs.NewHandle(c)); |
| 673 | return true; |
| 674 | } |
| 675 | }; |
| 676 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 677 | void ImageWriter::ComputeLazyFieldsForImageClasses() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 678 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 679 | ComputeLazyFieldsForClassesVisitor visitor; |
| 680 | class_linker->VisitClassesWithoutClassesLock(&visitor); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 683 | static bool IsBootClassLoaderClass(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 684 | return klass->GetClassLoader() == nullptr; |
| 685 | } |
| 686 | |
| 687 | bool ImageWriter::IsBootClassLoaderNonImageClass(mirror::Class* klass) { |
| 688 | return IsBootClassLoaderClass(klass) && !IsInBootImage(klass); |
| 689 | } |
| 690 | |
| 691 | bool ImageWriter::ContainsBootClassLoaderNonImageClass(mirror::Class* klass) { |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 692 | bool early_exit = false; |
| 693 | std::unordered_set<mirror::Class*> visited; |
| 694 | return ContainsBootClassLoaderNonImageClassInternal(klass, &early_exit, &visited); |
| 695 | } |
| 696 | |
| 697 | bool ImageWriter::ContainsBootClassLoaderNonImageClassInternal( |
| 698 | mirror::Class* klass, |
| 699 | bool* early_exit, |
| 700 | std::unordered_set<mirror::Class*>* visited) { |
| 701 | DCHECK(early_exit != nullptr); |
| 702 | DCHECK(visited != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 703 | if (klass == nullptr) { |
| 704 | return false; |
| 705 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 706 | auto found = prune_class_memo_.find(klass); |
| 707 | if (found != prune_class_memo_.end()) { |
| 708 | // Already computed, return the found value. |
| 709 | return found->second; |
| 710 | } |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 711 | // Circular dependencies, return false but do not store the result in the memoization table. |
| 712 | if (visited->find(klass) != visited->end()) { |
| 713 | *early_exit = true; |
| 714 | return false; |
| 715 | } |
| 716 | visited->emplace(klass); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 717 | bool result = IsBootClassLoaderNonImageClass(klass); |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 718 | bool my_early_exit = false; // Only for ourselves, ignore caller. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 719 | if (!result) { |
| 720 | // Check interfaces since these wont be visited through VisitReferences.) |
| 721 | mirror::IfTable* if_table = klass->GetIfTable(); |
| 722 | for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) { |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 723 | result = result || ContainsBootClassLoaderNonImageClassInternal( |
| 724 | if_table->GetInterface(i), |
| 725 | &my_early_exit, |
| 726 | visited); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | // Check static fields and their classes. |
| 730 | size_t num_static_fields = klass->NumReferenceStaticFields(); |
| 731 | if (num_static_fields != 0 && klass->IsResolved()) { |
| 732 | // Presumably GC can happen when we are cross compiling, it should not cause performance |
| 733 | // problems to do pointer size logic. |
| 734 | MemberOffset field_offset = klass->GetFirstReferenceStaticFieldOffset( |
| 735 | Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 736 | for (size_t i = 0u; i < num_static_fields; ++i) { |
| 737 | mirror::Object* ref = klass->GetFieldObject<mirror::Object>(field_offset); |
| 738 | if (ref != nullptr) { |
| 739 | if (ref->IsClass()) { |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 740 | result = result || |
| 741 | ContainsBootClassLoaderNonImageClassInternal( |
| 742 | ref->AsClass(), |
| 743 | &my_early_exit, |
| 744 | visited); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 745 | } |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 746 | result = result || |
| 747 | ContainsBootClassLoaderNonImageClassInternal( |
| 748 | ref->GetClass(), |
| 749 | &my_early_exit, |
| 750 | visited); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 751 | } |
| 752 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 753 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 754 | } |
| 755 | } |
Mathieu Chartier | 945c1c1 | 2015-11-24 15:37:12 -0800 | [diff] [blame] | 756 | result = result || |
| 757 | ContainsBootClassLoaderNonImageClassInternal( |
| 758 | klass->GetSuperClass(), |
| 759 | &my_early_exit, |
| 760 | visited); |
| 761 | // Erase the element we stored earlier since we are exiting the function. |
| 762 | auto it = visited->find(klass); |
| 763 | DCHECK(it != visited->end()); |
| 764 | visited->erase(it); |
| 765 | // Only store result if it is true or none of the calls early exited due to circular |
| 766 | // dependencies. If visited is empty then we are the root caller, in this case the cycle was in |
| 767 | // a child call and we can remember the result. |
| 768 | if (result == true || !my_early_exit || visited->empty()) { |
| 769 | prune_class_memo_[klass] = result; |
| 770 | } |
| 771 | *early_exit |= my_early_exit; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 772 | return result; |
| 773 | } |
| 774 | |
| 775 | bool ImageWriter::KeepClass(Class* klass) { |
| 776 | if (klass == nullptr) { |
| 777 | return false; |
| 778 | } |
| 779 | if (compile_app_image_) { |
| 780 | // For app images, we need to prune boot loader classes that are not in the boot image since |
| 781 | // these may have already been loaded when the app image is loaded. |
| 782 | return !ContainsBootClassLoaderNonImageClass(klass); |
| 783 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 784 | std::string temp; |
| 785 | return compiler_driver_.IsImageClass(klass->GetDescriptor(&temp)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 788 | class NonImageClassesVisitor : public ClassVisitor { |
| 789 | public: |
| 790 | explicit NonImageClassesVisitor(ImageWriter* image_writer) : image_writer_(image_writer) {} |
| 791 | |
| 792 | bool Visit(Class* klass) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 793 | if (!image_writer_->KeepClass(klass)) { |
| 794 | classes_to_prune_.insert(klass); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 795 | } |
| 796 | return true; |
| 797 | } |
| 798 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 799 | std::unordered_set<mirror::Class*> classes_to_prune_; |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 800 | ImageWriter* const image_writer_; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 801 | }; |
| 802 | |
| 803 | void ImageWriter::PruneNonImageClasses() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 804 | Runtime* runtime = Runtime::Current(); |
| 805 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 806 | Thread* self = Thread::Current(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 807 | |
| 808 | // Make a list of classes we would like to prune. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 809 | NonImageClassesVisitor visitor(this); |
| 810 | class_linker->VisitClasses(&visitor); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 811 | |
| 812 | // Remove the undesired classes from the class roots. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 813 | for (mirror::Class* klass : visitor.classes_to_prune_) { |
| 814 | std::string temp; |
| 815 | const char* name = klass->GetDescriptor(&temp); |
| 816 | VLOG(compiler) << "Pruning class " << name; |
| 817 | if (!compile_app_image_) { |
| 818 | DCHECK(IsBootClassLoaderClass(klass)); |
| 819 | } |
| 820 | bool result = class_linker->RemoveClass(name, klass->GetClassLoader()); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 821 | DCHECK(result); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | // Clear references to removed classes from the DexCaches. |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 825 | ArtMethod* resolution_method = runtime->GetResolutionMethod(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 826 | |
| 827 | ScopedAssertNoThreadSuspension sa(self, __FUNCTION__); |
| 828 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); // For ClassInClassTable |
| 829 | ReaderMutexLock mu2(self, *class_linker->DexLock()); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 830 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 831 | mirror::DexCache* dex_cache = down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 832 | if (dex_cache == nullptr) { |
| 833 | continue; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 834 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 835 | for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) { |
| 836 | Class* klass = dex_cache->GetResolvedType(i); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 837 | if (klass != nullptr && !KeepClass(klass)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 838 | dex_cache->SetResolvedType(i, nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 839 | } |
| 840 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 841 | ArtMethod** resolved_methods = dex_cache->GetResolvedMethods(); |
| 842 | for (size_t i = 0, num = dex_cache->NumResolvedMethods(); i != num; ++i) { |
| 843 | ArtMethod* method = |
| 844 | mirror::DexCache::GetElementPtrSize(resolved_methods, i, target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 845 | if (method != nullptr) { |
| 846 | auto* declaring_class = method->GetDeclaringClass(); |
| 847 | // Miranda methods may be held live by a class which was not an image class but have a |
| 848 | // declaring class which is an image class. Set it to the resolution method to be safe and |
| 849 | // prevent dangling pointers. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 850 | if (method->IsMiranda() || !KeepClass(declaring_class)) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 851 | mirror::DexCache::SetElementPtrSize(resolved_methods, |
| 852 | i, |
| 853 | resolution_method, |
| 854 | target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 855 | } else { |
| 856 | // Check that the class is still in the classes table. |
| 857 | DCHECK(class_linker->ClassInClassTable(declaring_class)) << "Class " |
| 858 | << PrettyClass(declaring_class) << " not in class linker table"; |
| 859 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 863 | ArtField* field = dex_cache->GetResolvedField(i, target_ptr_size_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 864 | if (field != nullptr && !KeepClass(field->GetDeclaringClass())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 865 | dex_cache->SetResolvedField(i, nullptr, target_ptr_size_); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 866 | } |
| 867 | } |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 868 | // Clean the dex field. It might have been populated during the initialization phase, but |
| 869 | // contains data only valid during a real run. |
| 870 | dex_cache->SetFieldObject<false>(mirror::DexCache::DexOffset(), nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 871 | } |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 872 | |
| 873 | // Drop the array class cache in the ClassLinker, as these are roots holding those classes live. |
| 874 | class_linker->DropFindArrayClassCache(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 875 | |
| 876 | // Clear to save RAM. |
| 877 | prune_class_memo_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 880 | void ImageWriter::CheckNonImageClassesRemoved() { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 881 | if (compiler_driver_.GetImageClasses() != nullptr) { |
| 882 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 883 | heap->VisitObjects(CheckNonImageClassesRemovedCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 884 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) { |
| 888 | ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 889 | if (obj->IsClass() && !image_writer->IsInBootImage(obj)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 890 | Class* klass = obj->AsClass(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 891 | if (!image_writer->KeepClass(klass)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 892 | image_writer->DumpImageClasses(); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 893 | std::string temp; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 894 | CHECK(image_writer->KeepClass(klass)) << klass->GetDescriptor(&temp) |
| 895 | << " " << PrettyDescriptor(klass); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 896 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
| 900 | void ImageWriter::DumpImageClasses() { |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 901 | auto image_classes = compiler_driver_.GetImageClasses(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 902 | CHECK(image_classes != nullptr); |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 903 | for (const std::string& image_class : *image_classes) { |
| 904 | LOG(INFO) << " " << image_class; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 908 | void ImageWriter::CalculateObjectBinSlots(Object* obj) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 909 | DCHECK(obj != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 910 | // if it is a string, we want to intern it if its not interned. |
| 911 | if (obj->GetClass()->IsStringClass()) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 912 | const char* oat_filename = GetOatFilename(obj); |
| 913 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 914 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 915 | // 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] | 916 | if (IsImageBinSlotAssigned(obj)) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 917 | DCHECK_EQ(obj, image_info.intern_table_->InternStrongImageString(obj->AsString())); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 918 | return; |
| 919 | } |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 920 | // InternImageString allows us to intern while holding the heap bitmap lock. This is safe since |
| 921 | // we are guaranteed to not have GC during image writing. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 922 | mirror::String* const interned = image_info.intern_table_->InternStrongImageString( |
Mathieu Chartier | 14c3bf9 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 923 | obj->AsString()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 924 | if (obj != interned) { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 925 | if (!IsImageBinSlotAssigned(interned)) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 926 | // interned obj is after us, allocate its location early |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 927 | AssignImageBinSlot(interned); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 928 | } |
| 929 | // point those looking for this object to the interned version. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 930 | SetImageBinSlot(obj, GetImageBinSlot(interned)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 931 | return; |
| 932 | } |
| 933 | // else (obj == interned), nothing to do but fall through to the normal case |
| 934 | } |
| 935 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 936 | AssignImageBinSlot(obj); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 939 | ObjectArray<Object>* ImageWriter::CreateImageRoots(const char* oat_filename) const { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 940 | Runtime* runtime = Runtime::Current(); |
| 941 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 942 | Thread* self = Thread::Current(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 943 | StackHandleScope<3> hs(self); |
| 944 | Handle<Class> object_array_class(hs.NewHandle( |
| 945 | class_linker->FindSystemClass(self, "[Ljava/lang/Object;"))); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 946 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 947 | std::unordered_set<const DexFile*> image_dex_files; |
| 948 | for (auto& pair : dex_file_oat_filename_map_) { |
| 949 | const DexFile* image_dex_file = pair.first; |
| 950 | const char* image_oat_filename = pair.second; |
| 951 | if (strcmp(oat_filename, image_oat_filename) == 0) { |
| 952 | image_dex_files.insert(image_dex_file); |
| 953 | } |
| 954 | } |
| 955 | |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 956 | // build an Object[] of all the DexCaches used in the source_space_. |
| 957 | // Since we can't hold the dex lock when allocating the dex_caches |
| 958 | // ObjectArray, we lock the dex lock twice, first to get the number |
| 959 | // of dex caches first and then lock it again to copy the dex |
| 960 | // caches. We check that the number of dex caches does not change. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 961 | size_t dex_cache_count = 0; |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 962 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 963 | ReaderMutexLock mu(self, *class_linker->DexLock()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 964 | // Count number of dex caches not in the boot image. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 965 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 966 | mirror::DexCache* dex_cache = |
| 967 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 968 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 969 | if (!IsInBootImage(dex_cache)) { |
| 970 | dex_cache_count += image_dex_files.find(dex_file) != image_dex_files.end() ? 1u : 0u; |
| 971 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 972 | } |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 973 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 974 | Handle<ObjectArray<Object>> dex_caches( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 975 | hs.NewHandle(ObjectArray<Object>::Alloc(self, object_array_class.Get(), dex_cache_count))); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 976 | CHECK(dex_caches.Get() != nullptr) << "Failed to allocate a dex cache array."; |
| 977 | { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 978 | ReaderMutexLock mu(self, *class_linker->DexLock()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 979 | size_t non_image_dex_caches = 0; |
| 980 | // Re-count number of non image dex caches. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 981 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 982 | mirror::DexCache* dex_cache = |
| 983 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 984 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 985 | if (!IsInBootImage(dex_cache)) { |
| 986 | non_image_dex_caches += image_dex_files.find(dex_file) != image_dex_files.end() ? 1u : 0u; |
| 987 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 988 | } |
| 989 | CHECK_EQ(dex_cache_count, non_image_dex_caches) |
| 990 | << "The number of non-image dex caches changed."; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 991 | size_t i = 0; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 992 | for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { |
| 993 | mirror::DexCache* dex_cache = |
| 994 | down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 995 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 996 | if (!IsInBootImage(dex_cache) && image_dex_files.find(dex_file) != image_dex_files.end()) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 997 | dex_caches->Set<false>(i, dex_cache); |
| 998 | ++i; |
| 999 | } |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 1000 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | // build an Object[] of the roots needed to restore the runtime |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1004 | auto image_roots(hs.NewHandle( |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1005 | ObjectArray<Object>::Alloc(self, object_array_class.Get(), ImageHeader::kImageRootsMax))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1006 | image_roots->Set<false>(ImageHeader::kDexCaches, dex_caches.Get()); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 1007 | image_roots->Set<false>(ImageHeader::kClassRoots, class_linker->GetClassRoots()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1008 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1009 | CHECK(image_roots->Get(i) != nullptr); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1010 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1011 | return image_roots.Get(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1014 | // Walk instance fields of the given Class. Separate function to allow recursion on the super |
| 1015 | // class. |
| 1016 | void ImageWriter::WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) { |
| 1017 | // Visit fields of parent classes first. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1018 | StackHandleScope<1> hs(Thread::Current()); |
| 1019 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 1020 | mirror::Class* super = h_class->GetSuperClass(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1021 | if (super != nullptr) { |
| 1022 | WalkInstanceFields(obj, super); |
| 1023 | } |
| 1024 | // |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1025 | size_t num_reference_fields = h_class->NumReferenceInstanceFields(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 1026 | MemberOffset field_offset = h_class->GetFirstReferenceInstanceFieldOffset(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1027 | for (size_t i = 0; i < num_reference_fields; ++i) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 1028 | mirror::Object* value = obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1029 | if (value != nullptr) { |
| 1030 | WalkFieldsInOrder(value); |
| 1031 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 1032 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 1033 | sizeof(mirror::HeapReference<mirror::Object>)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | // For an unvisited object, visit it then all its children found via fields. |
| 1038 | void ImageWriter::WalkFieldsInOrder(mirror::Object* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1039 | if (IsInBootImage(obj)) { |
| 1040 | // Object is in the image, don't need to fix it up. |
| 1041 | return; |
| 1042 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1043 | // Use our own visitor routine (instead of GC visitor) to get better locality between |
| 1044 | // an object and its fields |
| 1045 | if (!IsImageBinSlotAssigned(obj)) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1046 | // Walk instance fields of all objects |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1047 | StackHandleScope<2> hs(Thread::Current()); |
| 1048 | Handle<mirror::Object> h_obj(hs.NewHandle(obj)); |
| 1049 | Handle<mirror::Class> klass(hs.NewHandle(obj->GetClass())); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1050 | // visit the object itself. |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1051 | CalculateObjectBinSlots(h_obj.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1052 | WalkInstanceFields(h_obj.Get(), klass.Get()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1053 | // Walk static fields of a Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1054 | if (h_obj->IsClass()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1055 | size_t num_reference_static_fields = klass->NumReferenceStaticFields(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1056 | MemberOffset field_offset = klass->GetFirstReferenceStaticFieldOffset(target_ptr_size_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1057 | for (size_t i = 0; i < num_reference_static_fields; ++i) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1058 | mirror::Object* value = h_obj->GetFieldObject<mirror::Object>(field_offset); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1059 | if (value != nullptr) { |
| 1060 | WalkFieldsInOrder(value); |
| 1061 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 1062 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 1063 | sizeof(mirror::HeapReference<mirror::Object>)); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1064 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1065 | // Visit and assign offsets for fields and field arrays. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1066 | auto* as_klass = h_obj->AsClass(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1067 | mirror::DexCache* dex_cache = as_klass->GetDexCache(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1068 | LengthPrefixedArray<ArtField>* fields[] = { |
| 1069 | as_klass->GetSFieldsPtr(), as_klass->GetIFieldsPtr(), |
| 1070 | }; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1071 | const char* oat_file = GetOatFilenameForDexCache(dex_cache); |
| 1072 | ImageInfo& image_info = GetImageInfo(oat_file); |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame^] | 1073 | { |
| 1074 | // Note: This table is only accessed from the image writer, so the lock is technically |
| 1075 | // unnecessary. |
| 1076 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 1077 | // Insert in the class table for this iamge. |
| 1078 | image_info.class_table_->Insert(as_klass); |
| 1079 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1080 | for (LengthPrefixedArray<ArtField>* cur_fields : fields) { |
| 1081 | // Total array length including header. |
| 1082 | if (cur_fields != nullptr) { |
| 1083 | const size_t header_size = LengthPrefixedArray<ArtField>::ComputeSize(0); |
| 1084 | // Forward the entire array at once. |
| 1085 | auto it = native_object_relocations_.find(cur_fields); |
| 1086 | CHECK(it == native_object_relocations_.end()) << "Field array " << cur_fields |
| 1087 | << " already forwarded"; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1088 | size_t& offset = image_info.bin_slot_sizes_[kBinArtField]; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1089 | DCHECK(!IsInBootImage(cur_fields)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1090 | native_object_relocations_.emplace(cur_fields, |
| 1091 | NativeObjectRelocation {oat_file, offset, kNativeObjectRelocationTypeArtFieldArray }); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1092 | offset += header_size; |
| 1093 | // Forward individual fields so that we can quickly find where they belong. |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 1094 | for (size_t i = 0, count = cur_fields->size(); i < count; ++i) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1095 | // Need to forward arrays separate of fields. |
| 1096 | ArtField* field = &cur_fields->At(i); |
| 1097 | auto it2 = native_object_relocations_.find(field); |
| 1098 | CHECK(it2 == native_object_relocations_.end()) << "Field at index=" << i |
| 1099 | << " already assigned " << PrettyField(field) << " static=" << field->IsStatic(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1100 | DCHECK(!IsInBootImage(field)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1101 | native_object_relocations_.emplace(field, |
| 1102 | NativeObjectRelocation {oat_file, offset, kNativeObjectRelocationTypeArtField }); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1103 | offset += sizeof(ArtField); |
| 1104 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1105 | } |
| 1106 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1107 | // Visit and assign offsets for methods. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1108 | size_t num_methods = as_klass->NumMethods(); |
| 1109 | if (num_methods != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1110 | bool any_dirty = false; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1111 | for (auto& m : as_klass->GetMethods(target_ptr_size_)) { |
| 1112 | if (WillMethodBeDirty(&m)) { |
| 1113 | any_dirty = true; |
| 1114 | break; |
| 1115 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1116 | } |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1117 | NativeObjectRelocationType type = any_dirty |
| 1118 | ? kNativeObjectRelocationTypeArtMethodDirty |
| 1119 | : kNativeObjectRelocationTypeArtMethodClean; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1120 | Bin bin_type = BinTypeForNativeRelocationType(type); |
| 1121 | // Forward the entire array at once, but header first. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1122 | const size_t method_alignment = ArtMethod::Alignment(target_ptr_size_); |
| 1123 | const size_t method_size = ArtMethod::Size(target_ptr_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1124 | const size_t header_size = LengthPrefixedArray<ArtMethod>::ComputeSize(0, |
| 1125 | method_size, |
| 1126 | method_alignment); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1127 | LengthPrefixedArray<ArtMethod>* array = as_klass->GetMethodsPtr(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1128 | auto it = native_object_relocations_.find(array); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1129 | CHECK(it == native_object_relocations_.end()) |
| 1130 | << "Method array " << array << " already forwarded"; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1131 | size_t& offset = image_info.bin_slot_sizes_[bin_type]; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1132 | DCHECK(!IsInBootImage(array)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1133 | native_object_relocations_.emplace(array, |
| 1134 | NativeObjectRelocation { |
| 1135 | oat_file, |
| 1136 | offset, |
| 1137 | any_dirty ? kNativeObjectRelocationTypeArtMethodArrayDirty |
| 1138 | : kNativeObjectRelocationTypeArtMethodArrayClean }); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1139 | offset += header_size; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1140 | for (auto& m : as_klass->GetMethods(target_ptr_size_)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1141 | AssignMethodOffset(&m, type, oat_file); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1142 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 1143 | (any_dirty ? dirty_methods_ : clean_methods_) += num_methods; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1144 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1145 | } else if (h_obj->IsObjectArray()) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1146 | // Walk elements of an object array. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1147 | int32_t length = h_obj->AsObjectArray<mirror::Object>()->GetLength(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1148 | for (int32_t i = 0; i < length; i++) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1149 | mirror::ObjectArray<mirror::Object>* obj_array = h_obj->AsObjectArray<mirror::Object>(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1150 | mirror::Object* value = obj_array->Get(i); |
| 1151 | if (value != nullptr) { |
| 1152 | WalkFieldsInOrder(value); |
| 1153 | } |
| 1154 | } |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1155 | } else if (h_obj->IsClassLoader()) { |
| 1156 | // Register the class loader if it has a class table. |
| 1157 | // The fake boot class loader should not get registered and we should end up with only one |
| 1158 | // class loader. |
| 1159 | mirror::ClassLoader* class_loader = h_obj->AsClassLoader(); |
| 1160 | if (class_loader->GetClassTable() != nullptr) { |
| 1161 | class_loaders_.insert(class_loader); |
| 1162 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1167 | void ImageWriter::AssignMethodOffset(ArtMethod* method, |
| 1168 | NativeObjectRelocationType type, |
| 1169 | const char* oat_filename) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1170 | DCHECK(!IsInBootImage(method)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1171 | auto it = native_object_relocations_.find(method); |
| 1172 | CHECK(it == native_object_relocations_.end()) << "Method " << method << " already assigned " |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1173 | << PrettyMethod(method); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1174 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1175 | size_t& offset = image_info.bin_slot_sizes_[BinTypeForNativeRelocationType(type)]; |
| 1176 | native_object_relocations_.emplace(method, NativeObjectRelocation { oat_filename, offset, type }); |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1177 | offset += ArtMethod::Size(target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1180 | void ImageWriter::WalkFieldsCallback(mirror::Object* obj, void* arg) { |
| 1181 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
| 1182 | DCHECK(writer != nullptr); |
| 1183 | writer->WalkFieldsInOrder(obj); |
| 1184 | } |
| 1185 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1186 | void ImageWriter::UnbinObjectsIntoOffsetCallback(mirror::Object* obj, void* arg) { |
| 1187 | ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg); |
| 1188 | DCHECK(writer != nullptr); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1189 | if (!writer->IsInBootImage(obj)) { |
| 1190 | writer->UnbinObjectsIntoOffset(obj); |
| 1191 | } |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | void ImageWriter::UnbinObjectsIntoOffset(mirror::Object* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1195 | DCHECK(!IsInBootImage(obj)); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1196 | CHECK(obj != nullptr); |
| 1197 | |
| 1198 | // We know the bin slot, and the total bin sizes for all objects by now, |
| 1199 | // so calculate the object's final image offset. |
| 1200 | |
| 1201 | DCHECK(IsImageBinSlotAssigned(obj)); |
| 1202 | BinSlot bin_slot = GetImageBinSlot(obj); |
| 1203 | // Change the lockword from a bin slot into an offset |
| 1204 | AssignImageOffset(obj, bin_slot); |
| 1205 | } |
| 1206 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1207 | void ImageWriter::CalculateNewObjectOffsets() { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1208 | Thread* const self = Thread::Current(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1209 | StackHandleScopeCollection handles(self); |
| 1210 | std::vector<Handle<ObjectArray<Object>>> image_roots; |
| 1211 | for (const char* oat_filename : oat_filenames_) { |
| 1212 | std::string image_filename = oat_filename; |
| 1213 | image_roots.push_back(handles.NewHandle(CreateImageRoots(image_filename.c_str()))); |
| 1214 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1215 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1216 | auto* runtime = Runtime::Current(); |
| 1217 | auto* heap = runtime->GetHeap(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1218 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 1219 | // 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] | 1220 | // know where image_roots is going to end up |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1221 | image_objects_offset_begin_ = RoundUp(sizeof(ImageHeader), kObjectAlignment); // 64-bit-alignment |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1222 | |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 1223 | // Clear any pre-existing monitors which may have been in the monitor words, assign bin slots. |
| 1224 | heap->VisitObjects(WalkFieldsCallback, this); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1225 | // Write the image runtime methods. |
| 1226 | image_methods_[ImageHeader::kResolutionMethod] = runtime->GetResolutionMethod(); |
| 1227 | image_methods_[ImageHeader::kImtConflictMethod] = runtime->GetImtConflictMethod(); |
| 1228 | image_methods_[ImageHeader::kImtUnimplementedMethod] = runtime->GetImtUnimplementedMethod(); |
| 1229 | image_methods_[ImageHeader::kCalleeSaveMethod] = runtime->GetCalleeSaveMethod(Runtime::kSaveAll); |
| 1230 | image_methods_[ImageHeader::kRefsOnlySaveMethod] = |
| 1231 | runtime->GetCalleeSaveMethod(Runtime::kRefsOnly); |
| 1232 | image_methods_[ImageHeader::kRefsAndArgsSaveMethod] = |
| 1233 | runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1234 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1235 | // Add room for fake length prefixed array for holding the image methods. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1236 | const auto image_method_type = kNativeObjectRelocationTypeArtMethodArrayClean; |
| 1237 | auto it = native_object_relocations_.find(&image_method_array_); |
| 1238 | CHECK(it == native_object_relocations_.end()); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1239 | ImageInfo& default_image_info = GetImageInfo(default_oat_filename_); |
| 1240 | size_t& offset = |
| 1241 | default_image_info.bin_slot_sizes_[BinTypeForNativeRelocationType(image_method_type)]; |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1242 | if (!compile_app_image_) { |
| 1243 | native_object_relocations_.emplace(&image_method_array_, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1244 | NativeObjectRelocation { default_oat_filename_, offset, image_method_type }); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1245 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1246 | size_t method_alignment = ArtMethod::Alignment(target_ptr_size_); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1247 | const size_t array_size = LengthPrefixedArray<ArtMethod>::ComputeSize( |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1248 | 0, ArtMethod::Size(target_ptr_size_), method_alignment); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1249 | CHECK_ALIGNED_PARAM(array_size, method_alignment); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1250 | offset += array_size; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1251 | for (auto* m : image_methods_) { |
| 1252 | CHECK(m != nullptr); |
| 1253 | CHECK(m->IsRuntimeMethod()); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1254 | DCHECK_EQ(compile_app_image_, IsInBootImage(m)) << "Trampolines should be in boot image"; |
| 1255 | if (!IsInBootImage(m)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1256 | AssignMethodOffset(m, kNativeObjectRelocationTypeArtMethodClean, default_oat_filename_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1257 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1258 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1259 | // Calculate size of the dex cache arrays slot and prepare offsets. |
| 1260 | PrepareDexCacheArraySlots(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1261 | |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame^] | 1262 | // Calculate the sizes of the intern tables and class tables. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1263 | for (const char* oat_filename : oat_filenames_) { |
| 1264 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1265 | // Calculate how big the intern table will be after being serialized. |
| 1266 | InternTable* const intern_table = image_info.intern_table_.get(); |
| 1267 | CHECK_EQ(intern_table->WeakSize(), 0u) << " should have strong interned all the strings"; |
| 1268 | image_info.intern_table_bytes_ = intern_table->WriteToMemory(nullptr); |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame^] | 1269 | // Calculate the size of the class table. |
| 1270 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1271 | image_info.class_table_bytes_ += image_info.class_table_->WriteToMemory(nullptr); |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1274 | // Calculate bin slot offsets. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1275 | for (const char* oat_filename : oat_filenames_) { |
| 1276 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1277 | size_t bin_offset = image_objects_offset_begin_; |
| 1278 | for (size_t i = 0; i != kBinSize; ++i) { |
| 1279 | image_info.bin_slot_offsets_[i] = bin_offset; |
| 1280 | bin_offset += image_info.bin_slot_sizes_[i]; |
| 1281 | if (i == kBinArtField) { |
| 1282 | static_assert(kBinArtField + 1 == kBinArtMethodClean, "Methods follow fields."); |
| 1283 | static_assert(alignof(ArtField) == 4u, "ArtField alignment is 4."); |
| 1284 | DCHECK_ALIGNED(bin_offset, 4u); |
| 1285 | DCHECK(method_alignment == 4u || method_alignment == 8u); |
| 1286 | bin_offset = RoundUp(bin_offset, method_alignment); |
| 1287 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1288 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1289 | // NOTE: There may be additional padding between the bin slots and the intern table. |
| 1290 | DCHECK_EQ(image_info.image_end_, |
| 1291 | GetBinSizeSum(image_info, kBinMirrorCount) + image_objects_offset_begin_); |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 1292 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1293 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1294 | // Calculate image offsets. |
| 1295 | size_t image_offset = 0; |
| 1296 | for (const char* oat_filename : oat_filenames_) { |
| 1297 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1298 | image_info.image_begin_ = global_image_begin_ + image_offset; |
| 1299 | image_info.image_offset_ = image_offset; |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1300 | ImageSection unused_sections[ImageHeader::kSectionCount]; |
| 1301 | image_info.image_size_ = RoundUp( |
| 1302 | image_info.CreateImageSections(target_ptr_size_, unused_sections), |
| 1303 | kPageSize); |
| 1304 | // There should be no gaps until the next image. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1305 | image_offset += image_info.image_size_; |
| 1306 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1307 | |
Hiroshi Yamauchi | 0c8c303 | 2015-01-16 16:54:35 -0800 | [diff] [blame] | 1308 | // Transform each object's bin slot into an offset which will be used to do the final copy. |
| 1309 | heap->VisitObjects(UnbinObjectsIntoOffsetCallback, this); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1310 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1311 | // DCHECK_EQ(image_end_, GetBinSizeSum(kBinMirrorCount) + image_objects_offset_begin_); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1312 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1313 | size_t i = 0; |
| 1314 | for (const char* oat_filename : oat_filenames_) { |
| 1315 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1316 | image_info.image_roots_address_ = PointerToLowMemUInt32(GetImageAddress(image_roots[i].Get())); |
| 1317 | i++; |
| 1318 | } |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1319 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1320 | // Update the native relocations by adding their bin sums. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1321 | for (auto& pair : native_object_relocations_) { |
| 1322 | NativeObjectRelocation& relocation = pair.second; |
| 1323 | Bin bin_type = BinTypeForNativeRelocationType(relocation.type); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1324 | ImageInfo& image_info = GetImageInfo(relocation.oat_filename); |
| 1325 | relocation.offset += image_info.bin_slot_offsets_[bin_type]; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1328 | // Note that image_info.image_end_ is left at end of used mirror object section. |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1331 | size_t ImageWriter::ImageInfo::CreateImageSections(size_t target_ptr_size, |
| 1332 | ImageSection* out_sections) const { |
| 1333 | DCHECK(out_sections != nullptr); |
| 1334 | // Objects section |
| 1335 | auto* objects_section = &out_sections[ImageHeader::kSectionObjects]; |
| 1336 | *objects_section = ImageSection(0u, image_end_); |
| 1337 | size_t cur_pos = objects_section->End(); |
| 1338 | // Add field section. |
| 1339 | auto* field_section = &out_sections[ImageHeader::kSectionArtFields]; |
| 1340 | *field_section = ImageSection(cur_pos, bin_slot_sizes_[kBinArtField]); |
| 1341 | CHECK_EQ(bin_slot_offsets_[kBinArtField], field_section->Offset()); |
| 1342 | cur_pos = field_section->End(); |
| 1343 | // Round up to the alignment the required by the method section. |
| 1344 | cur_pos = RoundUp(cur_pos, ArtMethod::Alignment(target_ptr_size)); |
| 1345 | // Add method section. |
| 1346 | auto* methods_section = &out_sections[ImageHeader::kSectionArtMethods]; |
| 1347 | *methods_section = ImageSection(cur_pos, |
| 1348 | bin_slot_sizes_[kBinArtMethodClean] + |
| 1349 | bin_slot_sizes_[kBinArtMethodDirty]); |
| 1350 | CHECK_EQ(bin_slot_offsets_[kBinArtMethodClean], methods_section->Offset()); |
| 1351 | cur_pos = methods_section->End(); |
| 1352 | // Add dex cache arrays section. |
| 1353 | auto* dex_cache_arrays_section = &out_sections[ImageHeader::kSectionDexCacheArrays]; |
| 1354 | *dex_cache_arrays_section = ImageSection(cur_pos, bin_slot_sizes_[kBinDexCacheArray]); |
| 1355 | CHECK_EQ(bin_slot_offsets_[kBinDexCacheArray], dex_cache_arrays_section->Offset()); |
| 1356 | cur_pos = dex_cache_arrays_section->End(); |
| 1357 | // Round up to the alignment the string table expects. See HashSet::WriteToMemory. |
| 1358 | cur_pos = RoundUp(cur_pos, sizeof(uint64_t)); |
| 1359 | // Calculate the size of the interned strings. |
| 1360 | auto* interned_strings_section = &out_sections[ImageHeader::kSectionInternedStrings]; |
| 1361 | *interned_strings_section = ImageSection(cur_pos, intern_table_bytes_); |
| 1362 | cur_pos = interned_strings_section->End(); |
| 1363 | // Round up to the alignment the class table expects. See HashSet::WriteToMemory. |
| 1364 | cur_pos = RoundUp(cur_pos, sizeof(uint64_t)); |
| 1365 | // Calculate the size of the class table section. |
| 1366 | auto* class_table_section = &out_sections[ImageHeader::kSectionClassTable]; |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame^] | 1367 | *class_table_section = ImageSection(cur_pos, class_table_bytes_); |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1368 | cur_pos = class_table_section->End(); |
| 1369 | // Image end goes right before the start of the image bitmap. |
| 1370 | return cur_pos; |
| 1371 | } |
| 1372 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 1373 | void ImageWriter::CreateHeader(size_t oat_loaded_size, size_t oat_data_offset) { |
| 1374 | CHECK_NE(0U, oat_loaded_size); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1375 | const char* oat_filename = oat_file_->GetLocation().c_str(); |
| 1376 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1377 | const uint8_t* oat_file_begin = GetOatFileBegin(oat_filename); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1378 | const uint8_t* oat_file_end = oat_file_begin + oat_loaded_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1379 | image_info.oat_data_begin_ = const_cast<uint8_t*>(oat_file_begin) + oat_data_offset; |
| 1380 | const uint8_t* oat_data_end = image_info.oat_data_begin_ + oat_file_->Size(); |
| 1381 | image_info.oat_size_ = oat_file_->Size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1382 | |
| 1383 | // Create the image sections. |
| 1384 | ImageSection sections[ImageHeader::kSectionCount]; |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1385 | const size_t image_end = image_info.CreateImageSections(target_ptr_size_, sections); |
| 1386 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1387 | // Finally bitmap section. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1388 | const size_t bitmap_bytes = image_info.image_bitmap_->Size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1389 | auto* bitmap_section = §ions[ImageHeader::kSectionImageBitmap]; |
Mathieu Chartier | a06ba05 | 2016-01-06 13:51:52 -0800 | [diff] [blame] | 1390 | *bitmap_section = ImageSection(RoundUp(image_end, kPageSize), RoundUp(bitmap_bytes, kPageSize)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1391 | if (VLOG_IS_ON(compiler)) { |
| 1392 | LOG(INFO) << "Creating header for " << oat_filename; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1393 | size_t idx = 0; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1394 | for (const ImageSection& section : sections) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1395 | LOG(INFO) << static_cast<ImageHeader::ImageSections>(idx) << " " << section; |
| 1396 | ++idx; |
| 1397 | } |
| 1398 | LOG(INFO) << "Methods: clean=" << clean_methods_ << " dirty=" << dirty_methods_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1399 | LOG(INFO) << "Image roots address=" << std::hex << image_info.image_roots_address_ << std::dec; |
| 1400 | LOG(INFO) << "Image begin=" << std::hex << reinterpret_cast<uintptr_t>(global_image_begin_) |
| 1401 | << " Image offset=" << image_info.image_offset_ << std::dec; |
| 1402 | LOG(INFO) << "Oat file begin=" << std::hex << reinterpret_cast<uintptr_t>(oat_file_begin) |
| 1403 | << " Oat data begin=" << reinterpret_cast<uintptr_t>(image_info.oat_data_begin_) |
| 1404 | << " Oat data end=" << reinterpret_cast<uintptr_t>(oat_data_end) |
| 1405 | << " Oat file end=" << reinterpret_cast<uintptr_t>(oat_file_end); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1406 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1407 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 1408 | // Create the header, leave 0 for data size since we will fill this in as we are writing the |
| 1409 | // image. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1410 | new (image_info.image_->Begin()) ImageHeader(PointerToLowMemUInt32(image_info.image_begin_), |
| 1411 | image_end, |
| 1412 | sections, |
| 1413 | image_info.image_roots_address_, |
| 1414 | oat_file_->GetOatHeader().GetChecksum(), |
| 1415 | PointerToLowMemUInt32(oat_file_begin), |
| 1416 | PointerToLowMemUInt32(image_info.oat_data_begin_), |
| 1417 | PointerToLowMemUInt32(oat_data_end), |
| 1418 | PointerToLowMemUInt32(oat_file_end), |
| 1419 | target_ptr_size_, |
| 1420 | compile_pic_, |
| 1421 | image_storage_mode_, |
| 1422 | /*data_size*/0u); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | ArtMethod* ImageWriter::GetImageMethodAddress(ArtMethod* method) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1426 | auto it = native_object_relocations_.find(method); |
| 1427 | CHECK(it != native_object_relocations_.end()) << PrettyMethod(method) << " @ " << method; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1428 | const char* oat_filename = GetOatFilename(method->GetDexCache()); |
| 1429 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1430 | CHECK_GE(it->second.offset, image_info.image_end_) << "ArtMethods should be after Objects"; |
| 1431 | return reinterpret_cast<ArtMethod*>(image_info.image_begin_ + it->second.offset); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1434 | class FixupRootVisitor : public RootVisitor { |
| 1435 | public: |
| 1436 | explicit FixupRootVisitor(ImageWriter* image_writer) : image_writer_(image_writer) { |
| 1437 | } |
| 1438 | |
| 1439 | void VisitRoots(mirror::Object*** roots, size_t count, const RootInfo& info ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1440 | OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1441 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1442 | *roots[i] = image_writer_->GetImageAddress(*roots[i]); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | void VisitRoots(mirror::CompressedReference<mirror::Object>** roots, size_t count, |
| 1447 | const RootInfo& info ATTRIBUTE_UNUSED) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1448 | OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1449 | for (size_t i = 0; i < count; ++i) { |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1450 | roots[i]->Assign(image_writer_->GetImageAddress(roots[i]->AsMirrorPtr())); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | private: |
| 1455 | ImageWriter* const image_writer_; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1456 | }; |
| 1457 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1458 | void ImageWriter::CopyAndFixupNativeData() { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1459 | const char* oat_filename = oat_file_->GetLocation().c_str(); |
| 1460 | ImageInfo& image_info = GetImageInfo(oat_filename); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1461 | // Copy ArtFields and methods to their locations and update the array for convenience. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1462 | for (auto& pair : native_object_relocations_) { |
| 1463 | NativeObjectRelocation& relocation = pair.second; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1464 | // Only work with fields and methods that are in the current oat file. |
| 1465 | if (strcmp(relocation.oat_filename, oat_filename) != 0) { |
| 1466 | continue; |
| 1467 | } |
| 1468 | auto* dest = image_info.image_->Begin() + relocation.offset; |
| 1469 | DCHECK_GE(dest, image_info.image_->Begin() + image_info.image_end_); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1470 | DCHECK(!IsInBootImage(pair.first)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1471 | switch (relocation.type) { |
| 1472 | case kNativeObjectRelocationTypeArtField: { |
| 1473 | memcpy(dest, pair.first, sizeof(ArtField)); |
| 1474 | reinterpret_cast<ArtField*>(dest)->SetDeclaringClass( |
| 1475 | GetImageAddress(reinterpret_cast<ArtField*>(pair.first)->GetDeclaringClass())); |
| 1476 | break; |
| 1477 | } |
| 1478 | case kNativeObjectRelocationTypeArtMethodClean: |
| 1479 | case kNativeObjectRelocationTypeArtMethodDirty: { |
| 1480 | CopyAndFixupMethod(reinterpret_cast<ArtMethod*>(pair.first), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1481 | reinterpret_cast<ArtMethod*>(dest), |
| 1482 | image_info); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1483 | break; |
| 1484 | } |
| 1485 | // For arrays, copy just the header since the elements will get copied by their corresponding |
| 1486 | // relocations. |
| 1487 | case kNativeObjectRelocationTypeArtFieldArray: { |
| 1488 | memcpy(dest, pair.first, LengthPrefixedArray<ArtField>::ComputeSize(0)); |
| 1489 | break; |
| 1490 | } |
| 1491 | case kNativeObjectRelocationTypeArtMethodArrayClean: |
| 1492 | case kNativeObjectRelocationTypeArtMethodArrayDirty: { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 1493 | memcpy(dest, pair.first, LengthPrefixedArray<ArtMethod>::ComputeSize( |
| 1494 | 0, |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1495 | ArtMethod::Size(target_ptr_size_), |
| 1496 | ArtMethod::Alignment(target_ptr_size_))); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1497 | break; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1498 | case kNativeObjectRelocationTypeDexCacheArray: |
| 1499 | // Nothing to copy here, everything is done in FixupDexCache(). |
| 1500 | break; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1501 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | // Fixup the image method roots. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1505 | auto* image_header = reinterpret_cast<ImageHeader*>(image_info.image_->Begin()); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1506 | const ImageSection& methods_section = image_header->GetMethodsSection(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1507 | for (size_t i = 0; i < ImageHeader::kImageMethodsCount; ++i) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1508 | ArtMethod* method = image_methods_[i]; |
| 1509 | CHECK(method != nullptr); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1510 | // Only place runtime methods in the image of the default oat file. |
| 1511 | if (method->IsRuntimeMethod() && strcmp(default_oat_filename_, oat_filename) != 0) { |
| 1512 | continue; |
| 1513 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1514 | if (!IsInBootImage(method)) { |
| 1515 | auto it = native_object_relocations_.find(method); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1516 | CHECK(it != native_object_relocations_.end()) << "No forwarding for " << PrettyMethod(method); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1517 | NativeObjectRelocation& relocation = it->second; |
| 1518 | CHECK(methods_section.Contains(relocation.offset)) << relocation.offset << " not in " |
| 1519 | << methods_section; |
| 1520 | CHECK(relocation.IsArtMethodRelocation()) << relocation.type; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1521 | method = reinterpret_cast<ArtMethod*>(global_image_begin_ + it->second.offset); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1522 | } |
| 1523 | image_header->SetImageMethod(static_cast<ImageHeader::ImageMethod>(i), method); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1524 | } |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1525 | FixupRootVisitor root_visitor(this); |
| 1526 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1527 | // Write the intern table into the image. |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 1528 | if (image_info.intern_table_bytes_ > 0) { |
| 1529 | const ImageSection& intern_table_section = image_header->GetImageSection( |
| 1530 | ImageHeader::kSectionInternedStrings); |
| 1531 | InternTable* const intern_table = image_info.intern_table_.get(); |
| 1532 | uint8_t* const intern_table_memory_ptr = |
| 1533 | image_info.image_->Begin() + intern_table_section.Offset(); |
| 1534 | const size_t intern_table_bytes = intern_table->WriteToMemory(intern_table_memory_ptr); |
| 1535 | CHECK_EQ(intern_table_bytes, image_info.intern_table_bytes_); |
| 1536 | // Fixup the pointers in the newly written intern table to contain image addresses. |
| 1537 | InternTable temp_intern_table; |
| 1538 | // Note that we require that ReadFromMemory does not make an internal copy of the elements so that |
| 1539 | // the VisitRoots() will update the memory directly rather than the copies. |
| 1540 | // This also relies on visit roots not doing any verification which could fail after we update |
| 1541 | // the roots to be the image addresses. |
| 1542 | temp_intern_table.AddTableFromMemory(intern_table_memory_ptr); |
| 1543 | CHECK_EQ(temp_intern_table.Size(), intern_table->Size()); |
| 1544 | temp_intern_table.VisitRoots(&root_visitor, kVisitRootFlagAllRoots); |
| 1545 | } |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 1546 | // Write the class table(s) into the image. class_table_bytes_ may be 0 if there are multiple |
| 1547 | // class loaders. Writing multiple class tables into the image is currently unsupported. |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame^] | 1548 | if (image_info.class_table_bytes_ > 0u) { |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 1549 | const ImageSection& class_table_section = image_header->GetImageSection( |
| 1550 | ImageHeader::kSectionClassTable); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1551 | uint8_t* const class_table_memory_ptr = |
| 1552 | image_info.image_->Begin() + class_table_section.Offset(); |
Mathieu Chartier | 67ad20e | 2015-12-09 15:41:09 -0800 | [diff] [blame] | 1553 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame^] | 1554 | |
| 1555 | ClassTable* table = image_info.class_table_.get(); |
| 1556 | CHECK(table != nullptr); |
| 1557 | const size_t class_table_bytes = table->WriteToMemory(class_table_memory_ptr); |
| 1558 | CHECK_EQ(class_table_bytes, image_info.class_table_bytes_); |
| 1559 | // Fixup the pointers in the newly written class table to contain image addresses. See |
| 1560 | // above comment for intern tables. |
| 1561 | ClassTable temp_class_table; |
| 1562 | temp_class_table.ReadFromMemory(class_table_memory_ptr); |
| 1563 | CHECK_EQ(temp_class_table.NumZygoteClasses(), table->NumNonZygoteClasses() + |
| 1564 | table->NumZygoteClasses()); |
| 1565 | BufferedRootVisitor<kDefaultBufferedRootCount> buffered_visitor(&root_visitor, |
| 1566 | RootInfo(kRootUnknown)); |
| 1567 | temp_class_table.VisitRoots(buffered_visitor); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1568 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
Mathieu Chartier | fd04b6f | 2014-11-14 19:34:18 -0800 | [diff] [blame] | 1571 | void ImageWriter::CopyAndFixupObjects() { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1572 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1573 | heap->VisitObjects(CopyAndFixupObjectsCallback, this); |
| 1574 | // Fix up the object previously had hash codes. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1575 | for (const auto& hash_pair : saved_hashcode_map_) { |
Hiroshi Yamauchi | e15ea08 | 2015-02-09 17:11:42 -0800 | [diff] [blame] | 1576 | Object* obj = hash_pair.first; |
Andreas Gampe | 3b45ef2 | 2015-05-26 21:34:09 -0700 | [diff] [blame] | 1577 | DCHECK_EQ(obj->GetLockWord<kVerifyNone>(false).ReadBarrierState(), 0U); |
| 1578 | obj->SetLockWord<kVerifyNone>(LockWord::FromHashCode(hash_pair.second, 0U), false); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1579 | } |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1580 | saved_hashcode_map_.clear(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 1583 | void ImageWriter::CopyAndFixupObjectsCallback(Object* obj, void* arg) { |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 1584 | DCHECK(obj != nullptr); |
| 1585 | DCHECK(arg != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1586 | reinterpret_cast<ImageWriter*>(arg)->CopyAndFixupObject(obj); |
| 1587 | } |
| 1588 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1589 | void ImageWriter::FixupPointerArray(mirror::Object* dst, mirror::PointerArray* arr, |
| 1590 | mirror::Class* klass, Bin array_type) { |
| 1591 | CHECK(klass->IsArrayClass()); |
| 1592 | CHECK(arr->IsIntArray() || arr->IsLongArray()) << PrettyClass(klass) << " " << arr; |
| 1593 | // Fixup int and long pointers for the ArtMethod or ArtField arrays. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1594 | const size_t num_elements = arr->GetLength(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1595 | dst->SetClass(GetImageAddress(arr->GetClass())); |
| 1596 | auto* dest_array = down_cast<mirror::PointerArray*>(dst); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1597 | for (size_t i = 0, count = num_elements; i < count; ++i) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1598 | void* elem = arr->GetElementPtrSize<void*>(i, target_ptr_size_); |
| 1599 | if (elem != nullptr && !IsInBootImage(elem)) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1600 | auto it = native_object_relocations_.find(elem); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1601 | if (UNLIKELY(it == native_object_relocations_.end())) { |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1602 | if (it->second.IsArtMethodRelocation()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1603 | auto* method = reinterpret_cast<ArtMethod*>(elem); |
| 1604 | LOG(FATAL) << "No relocation entry for ArtMethod " << PrettyMethod(method) << " @ " |
| 1605 | << method << " idx=" << i << "/" << num_elements << " with declaring class " |
| 1606 | << PrettyClass(method->GetDeclaringClass()); |
| 1607 | } else { |
| 1608 | CHECK_EQ(array_type, kBinArtField); |
| 1609 | auto* field = reinterpret_cast<ArtField*>(elem); |
| 1610 | LOG(FATAL) << "No relocation entry for ArtField " << PrettyField(field) << " @ " |
| 1611 | << field << " idx=" << i << "/" << num_elements << " with declaring class " |
| 1612 | << PrettyClass(field->GetDeclaringClass()); |
| 1613 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1614 | UNREACHABLE(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1615 | } else { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1616 | ImageInfo& image_info = GetImageInfo(it->second.oat_filename); |
| 1617 | elem = image_info.image_begin_ + it->second.offset; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1618 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1619 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1620 | dest_array->SetElementPtrSize<false, true>(i, elem, target_ptr_size_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1621 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | void ImageWriter::CopyAndFixupObject(Object* obj) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1625 | if (IsInBootImage(obj)) { |
| 1626 | return; |
| 1627 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1628 | size_t offset = GetImageOffset(obj); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1629 | const char* oat_filename = GetOatFilename(obj); |
| 1630 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1631 | auto* dst = reinterpret_cast<Object*>(image_info.image_->Begin() + offset); |
| 1632 | DCHECK_LT(offset, image_info.image_end_); |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1633 | const auto* src = reinterpret_cast<const uint8_t*>(obj); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1634 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1635 | image_info.image_bitmap_->Set(dst); // Mark the obj as live. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1636 | |
| 1637 | const size_t n = obj->SizeOf(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1638 | DCHECK_LE(offset + n, image_info.image_->Size()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1639 | memcpy(dst, src, n); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1640 | |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 1641 | // Write in a hash code of objects which have inflated monitors or a hash code in their monitor |
| 1642 | // word. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1643 | const auto it = saved_hashcode_map_.find(obj); |
| 1644 | dst->SetLockWord(it != saved_hashcode_map_.end() ? |
| 1645 | LockWord::FromHashCode(it->second, 0u) : LockWord::Default(), false); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1646 | FixupObject(obj, dst); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1647 | } |
| 1648 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1649 | // 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] | 1650 | class FixupVisitor { |
| 1651 | public: |
| 1652 | FixupVisitor(ImageWriter* image_writer, Object* copy) : image_writer_(image_writer), copy_(copy) { |
| 1653 | } |
| 1654 | |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1655 | // Ignore class roots since we don't have a way to map them to the destination. These are handled |
| 1656 | // with other logic. |
| 1657 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) |
| 1658 | const {} |
| 1659 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {} |
| 1660 | |
| 1661 | |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1662 | void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1663 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Hiroshi Yamauchi | 6e83c17 | 2014-05-01 21:25:41 -0700 | [diff] [blame] | 1664 | Object* ref = obj->GetFieldObject<Object, kVerifyNone>(offset); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1665 | // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the |
| 1666 | // image. |
| 1667 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1668 | offset, |
| 1669 | image_writer_->GetImageAddress(ref)); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | // java.lang.ref.Reference visitor. |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 1673 | void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, mirror::Reference* ref) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1674 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1675 | copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>( |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 1676 | mirror::Reference::ReferentOffset(), |
| 1677 | image_writer_->GetImageAddress(ref->GetReferent())); |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1680 | protected: |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1681 | ImageWriter* const image_writer_; |
| 1682 | mirror::Object* const copy_; |
| 1683 | }; |
| 1684 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1685 | class FixupClassVisitor FINAL : public FixupVisitor { |
| 1686 | public: |
| 1687 | FixupClassVisitor(ImageWriter* image_writer, Object* copy) : FixupVisitor(image_writer, copy) { |
| 1688 | } |
| 1689 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1690 | void operator()(Object* obj, MemberOffset offset, bool is_static ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 1691 | REQUIRES(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1692 | DCHECK(obj->IsClass()); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 1693 | FixupVisitor::operator()(obj, offset, /*is_static*/false); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 1696 | void operator()(mirror::Class* klass ATTRIBUTE_UNUSED, |
| 1697 | mirror::Reference* ref ATTRIBUTE_UNUSED) const |
Mathieu Chartier | da7c650 | 2015-07-23 16:01:26 -0700 | [diff] [blame] | 1698 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1699 | LOG(FATAL) << "Reference not expected here."; |
| 1700 | } |
| 1701 | }; |
| 1702 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1703 | uintptr_t ImageWriter::NativeOffsetInImage(void* obj) { |
| 1704 | DCHECK(obj != nullptr); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1705 | DCHECK(!IsInBootImage(obj)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1706 | auto it = native_object_relocations_.find(obj); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1707 | CHECK(it != native_object_relocations_.end()) << obj << " spaces " |
| 1708 | << Runtime::Current()->GetHeap()->DumpSpaces(); |
Mathieu Chartier | c0fe56a | 2015-08-11 13:01:23 -0700 | [diff] [blame] | 1709 | const NativeObjectRelocation& relocation = it->second; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1710 | return relocation.offset; |
| 1711 | } |
| 1712 | |
| 1713 | template <typename T> |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1714 | T* ImageWriter::NativeLocationInImage(T* obj, const char* oat_filename) { |
| 1715 | if (obj == nullptr || IsInBootImage(obj)) { |
| 1716 | return obj; |
| 1717 | } else { |
| 1718 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1719 | return reinterpret_cast<T*>(image_info.image_begin_ + NativeOffsetInImage(obj)); |
| 1720 | } |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1723 | template <typename T> |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1724 | T* ImageWriter::NativeCopyLocation(T* obj, mirror::DexCache* dex_cache) { |
| 1725 | if (obj == nullptr || IsInBootImage(obj)) { |
| 1726 | return obj; |
| 1727 | } else { |
| 1728 | const char* oat_filename = GetOatFilenameForDexCache(dex_cache); |
| 1729 | ImageInfo& image_info = GetImageInfo(oat_filename); |
| 1730 | return reinterpret_cast<T*>(image_info.image_->Begin() + NativeOffsetInImage(obj)); |
| 1731 | } |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | class NativeLocationVisitor { |
| 1735 | public: |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1736 | explicit NativeLocationVisitor(ImageWriter* image_writer, const char* oat_filename) |
| 1737 | : image_writer_(image_writer), oat_filename_(oat_filename) {} |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1738 | |
| 1739 | template <typename T> |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1740 | T* operator()(T* ptr) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1741 | return image_writer_->NativeLocationInImage(ptr, oat_filename_); |
| 1742 | } |
| 1743 | |
| 1744 | ArtMethod* operator()(ArtMethod* method) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1745 | const char* oat_filename = method->IsRuntimeMethod() ? image_writer_->GetDefaultOatFilename() : |
| 1746 | image_writer_->GetOatFilenameForDexCache(method->GetDexCache()); |
| 1747 | return image_writer_->NativeLocationInImage(method, oat_filename); |
| 1748 | } |
| 1749 | |
| 1750 | ArtField* operator()(ArtField* field) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1751 | const char* oat_filename = image_writer_->GetOatFilenameForDexCache(field->GetDexCache()); |
| 1752 | return image_writer_->NativeLocationInImage(field, oat_filename); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1753 | } |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1754 | |
| 1755 | private: |
| 1756 | ImageWriter* const image_writer_; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1757 | const char* oat_filename_; |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1758 | }; |
| 1759 | |
| 1760 | void ImageWriter::FixupClass(mirror::Class* orig, mirror::Class* copy) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1761 | const char* oat_filename = GetOatFilename(orig); |
| 1762 | orig->FixupNativePointers(copy, target_ptr_size_, NativeLocationVisitor(this, oat_filename)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1763 | FixupClassVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1764 | static_cast<mirror::Object*>(orig)->VisitReferences(visitor, visitor); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 1767 | void ImageWriter::FixupObject(Object* orig, Object* copy) { |
Mathieu Chartier | b7ea3ac | 2014-03-24 16:54:46 -0700 | [diff] [blame] | 1768 | DCHECK(orig != nullptr); |
| 1769 | DCHECK(copy != nullptr); |
Hiroshi Yamauchi | 624468c | 2014-03-31 15:14:47 -0700 | [diff] [blame] | 1770 | if (kUseBakerOrBrooksReadBarrier) { |
| 1771 | orig->AssertReadBarrierPointer(); |
| 1772 | if (kUseBrooksReadBarrier) { |
| 1773 | // Note the address 'copy' isn't the same as the image address of 'orig'. |
| 1774 | copy->SetReadBarrierPointer(GetImageAddress(orig)); |
| 1775 | DCHECK_EQ(copy->GetReadBarrierPointer(), GetImageAddress(orig)); |
| 1776 | } |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 1777 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1778 | auto* klass = orig->GetClass(); |
| 1779 | if (klass->IsIntArrayClass() || klass->IsLongArrayClass()) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1780 | // Is this a native pointer array? |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1781 | auto it = pointer_arrays_.find(down_cast<mirror::PointerArray*>(orig)); |
| 1782 | if (it != pointer_arrays_.end()) { |
| 1783 | // Should only need to fixup every pointer array exactly once. |
| 1784 | FixupPointerArray(copy, down_cast<mirror::PointerArray*>(orig), klass, it->second); |
| 1785 | pointer_arrays_.erase(it); |
| 1786 | return; |
| 1787 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1788 | } |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1789 | if (orig->IsClass()) { |
| 1790 | FixupClass(orig->AsClass<kVerifyNone>(), down_cast<mirror::Class*>(copy)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1791 | } else { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1792 | if (klass == mirror::Method::StaticClass() || klass == mirror::Constructor::StaticClass()) { |
| 1793 | // Need to go update the ArtMethod. |
| 1794 | auto* dest = down_cast<mirror::AbstractMethod*>(copy); |
| 1795 | auto* src = down_cast<mirror::AbstractMethod*>(orig); |
| 1796 | ArtMethod* src_method = src->GetArtMethod(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 1797 | auto it = native_object_relocations_.find(src_method); |
| 1798 | CHECK(it != native_object_relocations_.end()) |
| 1799 | << "Missing relocation for AbstractMethod.artMethod " << PrettyMethod(src_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1800 | dest->SetArtMethod( |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1801 | reinterpret_cast<ArtMethod*>(global_image_begin_ + it->second.offset)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1802 | } else if (!klass->IsArrayClass()) { |
| 1803 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1804 | if (klass == class_linker->GetClassRoot(ClassLinker::kJavaLangDexCache)) { |
| 1805 | FixupDexCache(down_cast<mirror::DexCache*>(orig), down_cast<mirror::DexCache*>(copy)); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1806 | } else if (klass->IsClassLoaderClass()) { |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1807 | // If src is a ClassLoader, set the class table to null so that it gets recreated by the |
| 1808 | // ClassLoader. |
| 1809 | down_cast<mirror::ClassLoader*>(copy)->SetClassTable(nullptr); |
Mathieu Chartier | 5550c56 | 2015-09-22 15:18:04 -0700 | [diff] [blame] | 1810 | // Also set allocator to null to be safe. The allocator is created when we create the class |
| 1811 | // table. We also never expect to unload things in the image since they are held live as |
| 1812 | // roots. |
| 1813 | down_cast<mirror::ClassLoader*>(copy)->SetAllocator(nullptr); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1814 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1815 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1816 | FixupVisitor visitor(this, copy); |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1817 | orig->VisitReferences(visitor, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1818 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1821 | |
| 1822 | class ImageAddressVisitor { |
| 1823 | public: |
| 1824 | explicit ImageAddressVisitor(ImageWriter* image_writer) : image_writer_(image_writer) {} |
| 1825 | |
| 1826 | template <typename T> |
| 1827 | T* operator()(T* ptr) const SHARED_REQUIRES(Locks::mutator_lock_) { |
| 1828 | return image_writer_->GetImageAddress(ptr); |
| 1829 | } |
| 1830 | |
| 1831 | private: |
| 1832 | ImageWriter* const image_writer_; |
| 1833 | }; |
| 1834 | |
| 1835 | |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1836 | void ImageWriter::FixupDexCache(mirror::DexCache* orig_dex_cache, |
| 1837 | mirror::DexCache* copy_dex_cache) { |
| 1838 | // Though the DexCache array fields are usually treated as native pointers, we set the full |
| 1839 | // 64-bit values here, clearing the top 32 bits for 32-bit targets. The zero-extension is |
| 1840 | // done by casting to the unsigned type uintptr_t before casting to int64_t, i.e. |
| 1841 | // static_cast<int64_t>(reinterpret_cast<uintptr_t>(image_begin_ + offset))). |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1842 | const char* oat_filename = GetOatFilenameForDexCache(orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1843 | GcRoot<mirror::String>* orig_strings = orig_dex_cache->GetStrings(); |
| 1844 | if (orig_strings != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1845 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::StringsOffset(), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1846 | NativeLocationInImage(orig_strings, oat_filename), |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1847 | /*pointer size*/8u); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1848 | orig_dex_cache->FixupStrings(NativeCopyLocation(orig_strings, orig_dex_cache), |
| 1849 | ImageAddressVisitor(this)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1850 | } |
| 1851 | GcRoot<mirror::Class>* orig_types = orig_dex_cache->GetResolvedTypes(); |
| 1852 | if (orig_types != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1853 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedTypesOffset(), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1854 | NativeLocationInImage(orig_types, oat_filename), |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1855 | /*pointer size*/8u); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1856 | orig_dex_cache->FixupResolvedTypes(NativeCopyLocation(orig_types, orig_dex_cache), |
| 1857 | ImageAddressVisitor(this)); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1858 | } |
| 1859 | ArtMethod** orig_methods = orig_dex_cache->GetResolvedMethods(); |
| 1860 | if (orig_methods != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1861 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedMethodsOffset(), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1862 | NativeLocationInImage(orig_methods, oat_filename), |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1863 | /*pointer size*/8u); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1864 | ArtMethod** copy_methods = NativeCopyLocation(orig_methods, orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1865 | for (size_t i = 0, num = orig_dex_cache->NumResolvedMethods(); i != num; ++i) { |
| 1866 | ArtMethod* orig = mirror::DexCache::GetElementPtrSize(orig_methods, i, target_ptr_size_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1867 | const char* method_oat_filename; |
| 1868 | if (orig == nullptr || orig->IsRuntimeMethod()) { |
| 1869 | method_oat_filename = default_oat_filename_; |
| 1870 | } else { |
| 1871 | method_oat_filename = GetOatFilenameForDexCache(orig->GetDexCache()); |
| 1872 | } |
| 1873 | ArtMethod* copy = NativeLocationInImage(orig, method_oat_filename); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1874 | mirror::DexCache::SetElementPtrSize(copy_methods, i, copy, target_ptr_size_); |
| 1875 | } |
| 1876 | } |
| 1877 | ArtField** orig_fields = orig_dex_cache->GetResolvedFields(); |
| 1878 | if (orig_fields != nullptr) { |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1879 | copy_dex_cache->SetFieldPtrWithSize<false>(mirror::DexCache::ResolvedFieldsOffset(), |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1880 | NativeLocationInImage(orig_fields, oat_filename), |
Mathieu Chartier | 4b00d34 | 2015-11-13 10:42:08 -0800 | [diff] [blame] | 1881 | /*pointer size*/8u); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1882 | ArtField** copy_fields = NativeCopyLocation(orig_fields, orig_dex_cache); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1883 | for (size_t i = 0, num = orig_dex_cache->NumResolvedFields(); i != num; ++i) { |
| 1884 | ArtField* orig = mirror::DexCache::GetElementPtrSize(orig_fields, i, target_ptr_size_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1885 | const char* field_oat_filename = |
| 1886 | orig == nullptr ? default_oat_filename_ : GetOatFilenameForDexCache(orig->GetDexCache()); |
| 1887 | ArtField* copy = NativeLocationInImage(orig, field_oat_filename); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1888 | mirror::DexCache::SetElementPtrSize(copy_fields, i, copy, target_ptr_size_); |
| 1889 | } |
| 1890 | } |
| 1891 | } |
| 1892 | |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1893 | const uint8_t* ImageWriter::GetOatAddress(OatAddress type) const { |
| 1894 | DCHECK_LT(type, kOatAddressCount); |
| 1895 | // If we are compiling an app image, we need to use the stubs of the boot image. |
| 1896 | if (compile_app_image_) { |
| 1897 | // Use the current image pointers. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1898 | std::vector<gc::space::ImageSpace*> image_spaces = |
| 1899 | Runtime::Current()->GetHeap()->GetBootImageSpaces(); |
| 1900 | DCHECK(!image_spaces.empty()); |
| 1901 | const OatFile* oat_file = image_spaces[0]->GetOatFile(); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1902 | CHECK(oat_file != nullptr); |
| 1903 | const OatHeader& header = oat_file->GetOatHeader(); |
| 1904 | switch (type) { |
| 1905 | // TODO: We could maybe clean this up if we stored them in an array in the oat header. |
| 1906 | case kOatAddressQuickGenericJNITrampoline: |
| 1907 | return static_cast<const uint8_t*>(header.GetQuickGenericJniTrampoline()); |
| 1908 | case kOatAddressInterpreterToInterpreterBridge: |
| 1909 | return static_cast<const uint8_t*>(header.GetInterpreterToInterpreterBridge()); |
| 1910 | case kOatAddressInterpreterToCompiledCodeBridge: |
| 1911 | return static_cast<const uint8_t*>(header.GetInterpreterToCompiledCodeBridge()); |
| 1912 | case kOatAddressJNIDlsymLookup: |
| 1913 | return static_cast<const uint8_t*>(header.GetJniDlsymLookup()); |
| 1914 | case kOatAddressQuickIMTConflictTrampoline: |
| 1915 | return static_cast<const uint8_t*>(header.GetQuickImtConflictTrampoline()); |
| 1916 | case kOatAddressQuickResolutionTrampoline: |
| 1917 | return static_cast<const uint8_t*>(header.GetQuickResolutionTrampoline()); |
| 1918 | case kOatAddressQuickToInterpreterBridge: |
| 1919 | return static_cast<const uint8_t*>(header.GetQuickToInterpreterBridge()); |
| 1920 | default: |
| 1921 | UNREACHABLE(); |
| 1922 | } |
| 1923 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1924 | const ImageInfo& primary_image_info = GetImageInfo(0); |
| 1925 | return GetOatAddressForOffset(primary_image_info.oat_address_offsets_[type], primary_image_info); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1926 | } |
| 1927 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1928 | const uint8_t* ImageWriter::GetQuickCode(ArtMethod* method, |
| 1929 | const ImageInfo& image_info, |
| 1930 | bool* quick_is_interpreted) { |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1931 | DCHECK(!method->IsResolutionMethod()) << PrettyMethod(method); |
| 1932 | DCHECK(!method->IsImtConflictMethod()) << PrettyMethod(method); |
| 1933 | DCHECK(!method->IsImtUnimplementedMethod()) << PrettyMethod(method); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 1934 | DCHECK(method->IsInvokable()) << PrettyMethod(method); |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1935 | DCHECK(!IsInBootImage(method)) << PrettyMethod(method); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1936 | |
| 1937 | // Use original code if it exists. Otherwise, set the code pointer to the resolution |
| 1938 | // trampoline. |
| 1939 | |
| 1940 | // Quick entrypoint: |
Jeff Hao | c7d1188 | 2015-02-03 15:08:39 -0800 | [diff] [blame] | 1941 | uint32_t quick_oat_code_offset = PointerToLowMemUInt32( |
| 1942 | method->GetEntryPointFromQuickCompiledCodePtrSize(target_ptr_size_)); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1943 | const uint8_t* quick_code = GetOatAddressForOffset(quick_oat_code_offset, image_info); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1944 | *quick_is_interpreted = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1945 | if (quick_code != nullptr && (!method->IsStatic() || method->IsConstructor() || |
| 1946 | method->GetDeclaringClass()->IsInitialized())) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1947 | // We have code for a non-static or initialized method, just use the code. |
| 1948 | } else if (quick_code == nullptr && method->IsNative() && |
| 1949 | (!method->IsStatic() || method->GetDeclaringClass()->IsInitialized())) { |
| 1950 | // Non-static or initialized native method missing compiled code, use generic JNI version. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1951 | quick_code = GetOatAddress(kOatAddressQuickGenericJNITrampoline); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1952 | } else if (quick_code == nullptr && !method->IsNative()) { |
| 1953 | // We don't have code at all for a non-native method, use the interpreter. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1954 | quick_code = GetOatAddress(kOatAddressQuickToInterpreterBridge); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1955 | *quick_is_interpreted = true; |
| 1956 | } else { |
| 1957 | CHECK(!method->GetDeclaringClass()->IsInitialized()); |
| 1958 | // We have code for a static method, but need to go through the resolution stub for class |
| 1959 | // initialization. |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1960 | quick_code = GetOatAddress(kOatAddressQuickResolutionTrampoline); |
| 1961 | } |
| 1962 | if (!IsInBootOatFile(quick_code)) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1963 | // DCHECK_GE(quick_code, oat_data_begin_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1964 | } |
| 1965 | return quick_code; |
| 1966 | } |
| 1967 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1968 | void ImageWriter::CopyAndFixupMethod(ArtMethod* orig, |
| 1969 | ArtMethod* copy, |
| 1970 | const ImageInfo& image_info) { |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 1971 | memcpy(copy, orig, ArtMethod::Size(target_ptr_size_)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1972 | |
| 1973 | copy->SetDeclaringClass(GetImageAddress(orig->GetDeclaringClassUnchecked())); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1974 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1975 | const char* oat_filename; |
| 1976 | if (orig->IsRuntimeMethod()) { |
| 1977 | oat_filename = default_oat_filename_; |
| 1978 | } else { |
| 1979 | auto it = dex_file_oat_filename_map_.find(orig->GetDexFile()); |
| 1980 | DCHECK(it != dex_file_oat_filename_map_.end()) << orig->GetDexFile()->GetLocation(); |
| 1981 | oat_filename = it->second; |
| 1982 | } |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1983 | ArtMethod** orig_resolved_methods = orig->GetDexCacheResolvedMethods(target_ptr_size_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1984 | copy->SetDexCacheResolvedMethods(NativeLocationInImage(orig_resolved_methods, oat_filename), |
| 1985 | target_ptr_size_); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 1986 | GcRoot<mirror::Class>* orig_resolved_types = orig->GetDexCacheResolvedTypes(target_ptr_size_); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1987 | copy->SetDexCacheResolvedTypes(NativeLocationInImage(orig_resolved_types, oat_filename), |
| 1988 | target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1989 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1990 | // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to |
| 1991 | // oat_begin_ |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1992 | |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 1993 | // The resolution method has a special trampoline to call. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1994 | Runtime* runtime = Runtime::Current(); |
| 1995 | if (UNLIKELY(orig == runtime->GetResolutionMethod())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1996 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 1997 | GetOatAddress(kOatAddressQuickResolutionTrampoline), target_ptr_size_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1998 | } else if (UNLIKELY(orig == runtime->GetImtConflictMethod() || |
| 1999 | orig == runtime->GetImtUnimplementedMethod())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2000 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2001 | GetOatAddress(kOatAddressQuickIMTConflictTrampoline), target_ptr_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2002 | } else if (UNLIKELY(orig->IsRuntimeMethod())) { |
| 2003 | bool found_one = false; |
| 2004 | for (size_t i = 0; i < static_cast<size_t>(Runtime::kLastCalleeSaveType); ++i) { |
| 2005 | auto idx = static_cast<Runtime::CalleeSaveType>(i); |
| 2006 | if (runtime->HasCalleeSaveMethod(idx) && runtime->GetCalleeSaveMethod(idx) == orig) { |
| 2007 | found_one = true; |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
| 2011 | CHECK(found_one) << "Expected to find callee save method but got " << PrettyMethod(orig); |
| 2012 | CHECK(copy->IsRuntimeMethod()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2013 | } else { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2014 | // We assume all methods have code. If they don't currently then we set them to the use the |
| 2015 | // resolution trampoline. Abstract methods never have code and so we need to make sure their |
| 2016 | // use results in an AbstractMethodError. We use the interpreter to achieve this. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2017 | if (UNLIKELY(!orig->IsInvokable())) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2018 | copy->SetEntryPointFromQuickCompiledCodePtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2019 | GetOatAddress(kOatAddressQuickToInterpreterBridge), target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2020 | } else { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2021 | bool quick_is_interpreted; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2022 | const uint8_t* quick_code = GetQuickCode(orig, image_info, &quick_is_interpreted); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2023 | copy->SetEntryPointFromQuickCompiledCodePtrSize(quick_code, target_ptr_size_); |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 2024 | |
Sebastien Hertz | e1d0781 | 2014-05-21 15:44:09 +0200 | [diff] [blame] | 2025 | // JNI entrypoint: |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2026 | if (orig->IsNative()) { |
| 2027 | // The native method's pointer is set to a stub to lookup via dlsym. |
| 2028 | // Note this is not the code_ pointer, that is handled above. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2029 | copy->SetEntryPointFromJniPtrSize( |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 2030 | GetOatAddress(kOatAddressJNIDlsymLookup), target_ptr_size_); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 2031 | } |
| 2032 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2033 | } |
| 2034 | } |
| 2035 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2036 | static OatHeader* GetOatHeaderFromElf(ElfFile* elf) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2037 | uint64_t data_sec_offset; |
| 2038 | bool has_data_sec = elf->GetSectionOffsetAndSize(".rodata", &data_sec_offset, nullptr); |
| 2039 | if (!has_data_sec) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2040 | return nullptr; |
| 2041 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2042 | return reinterpret_cast<OatHeader*>(elf->Begin() + data_sec_offset); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 2043 | } |
| 2044 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2045 | void ImageWriter::SetOatChecksumFromElfFile(File* elf_file) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2046 | std::string error_msg; |
Mathieu Chartier | a808bac | 2015-11-05 16:33:15 -0800 | [diff] [blame] | 2047 | std::unique_ptr<ElfFile> elf(ElfFile::Open(elf_file, |
| 2048 | PROT_READ | PROT_WRITE, |
| 2049 | MAP_SHARED, |
| 2050 | &error_msg)); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2051 | if (elf.get() == nullptr) { |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 2052 | LOG(FATAL) << "Unable open oat file: " << error_msg; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2053 | return; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2054 | } |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2055 | OatHeader* oat_header = GetOatHeaderFromElf(elf.get()); |
| 2056 | CHECK(oat_header != nullptr); |
| 2057 | CHECK(oat_header->IsValid()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2058 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2059 | ImageInfo& image_info = GetImageInfo(oat_file_->GetLocation().c_str()); |
| 2060 | ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_info.image_->Begin()); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 2061 | image_header->SetOatChecksum(oat_header->GetChecksum()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2062 | } |
| 2063 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2064 | size_t ImageWriter::GetBinSizeSum(ImageWriter::ImageInfo& image_info, ImageWriter::Bin up_to) const { |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 2065 | DCHECK_LE(up_to, kBinSize); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2066 | return std::accumulate(&image_info.bin_slot_sizes_[0], |
| 2067 | &image_info.bin_slot_sizes_[up_to], |
| 2068 | /*init*/0); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | ImageWriter::BinSlot::BinSlot(uint32_t lockword) : lockword_(lockword) { |
| 2072 | // 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] | 2073 | static_assert(kBinBits == 3, "wrong number of bin bits"); |
| 2074 | static_assert(kBinShift == 27, "wrong number of shift"); |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 2075 | static_assert(sizeof(BinSlot) == sizeof(LockWord), "BinSlot/LockWord must have equal sizes"); |
| 2076 | |
| 2077 | DCHECK_LT(GetBin(), kBinSize); |
| 2078 | DCHECK_ALIGNED(GetIndex(), kObjectAlignment); |
| 2079 | } |
| 2080 | |
| 2081 | ImageWriter::BinSlot::BinSlot(Bin bin, uint32_t index) |
| 2082 | : BinSlot(index | (static_cast<uint32_t>(bin) << kBinShift)) { |
| 2083 | DCHECK_EQ(index, GetIndex()); |
| 2084 | } |
| 2085 | |
| 2086 | ImageWriter::Bin ImageWriter::BinSlot::GetBin() const { |
| 2087 | return static_cast<Bin>((lockword_ & kBinMask) >> kBinShift); |
| 2088 | } |
| 2089 | |
| 2090 | uint32_t ImageWriter::BinSlot::GetIndex() const { |
| 2091 | return lockword_ & ~kBinMask; |
| 2092 | } |
| 2093 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2094 | uint8_t* ImageWriter::GetOatFileBegin(const char* oat_filename) const { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2095 | uintptr_t last_image_end = 0; |
| 2096 | for (const char* oat_fn : oat_filenames_) { |
| 2097 | const ImageInfo& image_info = GetConstImageInfo(oat_fn); |
| 2098 | DCHECK(image_info.image_begin_ != nullptr); |
| 2099 | uintptr_t this_end = reinterpret_cast<uintptr_t>(image_info.image_begin_) + |
| 2100 | image_info.image_size_; |
| 2101 | last_image_end = std::max(this_end, last_image_end); |
| 2102 | } |
| 2103 | const ImageInfo& image_info = GetConstImageInfo(oat_filename); |
| 2104 | return reinterpret_cast<uint8_t*>(last_image_end) + image_info.oat_offset_; |
Mathieu Chartier | d39645e | 2015-06-09 17:50:29 -0700 | [diff] [blame] | 2105 | } |
| 2106 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2107 | ImageWriter::Bin ImageWriter::BinTypeForNativeRelocationType(NativeObjectRelocationType type) { |
| 2108 | switch (type) { |
| 2109 | case kNativeObjectRelocationTypeArtField: |
| 2110 | case kNativeObjectRelocationTypeArtFieldArray: |
| 2111 | return kBinArtField; |
| 2112 | case kNativeObjectRelocationTypeArtMethodClean: |
| 2113 | case kNativeObjectRelocationTypeArtMethodArrayClean: |
| 2114 | return kBinArtMethodClean; |
| 2115 | case kNativeObjectRelocationTypeArtMethodDirty: |
| 2116 | case kNativeObjectRelocationTypeArtMethodArrayDirty: |
| 2117 | return kBinArtMethodDirty; |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 2118 | case kNativeObjectRelocationTypeDexCacheArray: |
| 2119 | return kBinDexCacheArray; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 2120 | } |
| 2121 | UNREACHABLE(); |
| 2122 | } |
| 2123 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2124 | const char* ImageWriter::GetOatFilename(mirror::Object* obj) const { |
| 2125 | if (compile_app_image_) { |
| 2126 | return default_oat_filename_; |
| 2127 | } else { |
| 2128 | return GetOatFilenameForDexCache(obj->IsDexCache() ? obj->AsDexCache() : |
| 2129 | obj->IsClass() ? obj->AsClass()->GetDexCache() : obj->GetClass()->GetDexCache()); |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | const char* ImageWriter::GetOatFilenameForDexCache(mirror::DexCache* dex_cache) const { |
| 2134 | if (compile_app_image_ || dex_cache == nullptr) { |
| 2135 | return default_oat_filename_; |
| 2136 | } else { |
| 2137 | auto it = dex_file_oat_filename_map_.find(dex_cache->GetDexFile()); |
| 2138 | DCHECK(it != dex_file_oat_filename_map_.end()) << dex_cache->GetDexFile()->GetLocation(); |
| 2139 | return it->second; |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | ImageWriter::ImageInfo& ImageWriter::GetImageInfo(const char* oat_filename) { |
| 2144 | auto it = image_info_map_.find(oat_filename); |
| 2145 | DCHECK(it != image_info_map_.end()); |
| 2146 | return it->second; |
| 2147 | } |
| 2148 | |
| 2149 | const ImageWriter::ImageInfo& ImageWriter::GetConstImageInfo(const char* oat_filename) const { |
| 2150 | auto it = image_info_map_.find(oat_filename); |
| 2151 | DCHECK(it != image_info_map_.end()); |
| 2152 | return it->second; |
| 2153 | } |
| 2154 | |
| 2155 | const ImageWriter::ImageInfo& ImageWriter::GetImageInfo(size_t index) const { |
| 2156 | DCHECK_LT(index, oat_filenames_.size()); |
| 2157 | return GetConstImageInfo(oat_filenames_[index]); |
| 2158 | } |
| 2159 | |
| 2160 | void ImageWriter::UpdateOatFile(const char* oat_filename) { |
| 2161 | std::unique_ptr<File> oat_file(OS::OpenFileForReading(oat_filename)); |
| 2162 | DCHECK(oat_file != nullptr); |
| 2163 | size_t oat_loaded_size = 0; |
| 2164 | size_t oat_data_offset = 0; |
| 2165 | ElfWriter::GetOatElfInformation(oat_file.get(), &oat_loaded_size, &oat_data_offset); |
| 2166 | |
| 2167 | ImageInfo& cur_image_info = GetImageInfo(oat_filename); |
| 2168 | |
| 2169 | // Update the oat_offset of the next image info. |
| 2170 | auto it = std::find(oat_filenames_.begin(), oat_filenames_.end(), oat_filename); |
| 2171 | DCHECK(it != oat_filenames_.end()); |
| 2172 | |
| 2173 | it++; |
| 2174 | if (it != oat_filenames_.end()) { |
| 2175 | // There is a following one. |
| 2176 | ImageInfo& next_image_info = GetImageInfo(*it); |
| 2177 | next_image_info.oat_offset_ = cur_image_info.oat_offset_ + oat_loaded_size; |
| 2178 | } |
| 2179 | } |
| 2180 | |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2181 | ImageWriter::ImageWriter( |
| 2182 | const CompilerDriver& compiler_driver, |
| 2183 | uintptr_t image_begin, |
| 2184 | bool compile_pic, |
| 2185 | bool compile_app_image, |
| 2186 | ImageHeader::StorageMode image_storage_mode, |
| 2187 | const std::vector<const char*> oat_filenames, |
| 2188 | const std::unordered_map<const DexFile*, const char*>& dex_file_oat_filename_map) |
| 2189 | : compiler_driver_(compiler_driver), |
| 2190 | global_image_begin_(reinterpret_cast<uint8_t*>(image_begin)), |
| 2191 | image_objects_offset_begin_(0), |
| 2192 | oat_file_(nullptr), |
| 2193 | compile_pic_(compile_pic), |
| 2194 | compile_app_image_(compile_app_image), |
| 2195 | boot_image_space_(nullptr), |
| 2196 | target_ptr_size_(InstructionSetPointerSize(compiler_driver_.GetInstructionSet())), |
| 2197 | image_method_array_(ImageHeader::kImageMethodsCount), |
| 2198 | dirty_methods_(0u), |
| 2199 | clean_methods_(0u), |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2200 | image_storage_mode_(image_storage_mode), |
| 2201 | dex_file_oat_filename_map_(dex_file_oat_filename_map), |
| 2202 | oat_filenames_(oat_filenames), |
| 2203 | default_oat_filename_(oat_filenames[0]) { |
| 2204 | CHECK_NE(image_begin, 0U); |
| 2205 | for (const char* oat_filename : oat_filenames) { |
| 2206 | image_info_map_.emplace(oat_filename, ImageInfo()); |
| 2207 | } |
| 2208 | std::fill_n(image_methods_, arraysize(image_methods_), nullptr); |
| 2209 | } |
| 2210 | |
Mathieu Chartier | 1f47b67 | 2016-01-07 16:29:01 -0800 | [diff] [blame^] | 2211 | ImageWriter::ImageInfo::ImageInfo() |
| 2212 | : intern_table_(new InternTable), |
| 2213 | class_table_(new ClassTable) {} |
Mathieu Chartier | ea0831f | 2015-12-29 13:17:37 -0800 | [diff] [blame] | 2214 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 2215 | } // namespace art |