blob: 6e5f19a8c5d3ee5890ef0181a98b6384e055b1fe [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "image_writer.h"
18
19#include <sys/stat.h>
20
Ian Rogers700a4022014-05-19 16:49:03 -070021#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include <vector>
23
24#include "base/logging.h"
25#include "base/unix_file/fd_file.h"
26#include "class_linker.h"
27#include "compiled_method.h"
28#include "dex_file-inl.h"
29#include "driver/compiler_driver.h"
30#include "elf_writer.h"
31#include "gc/accounting/card_table-inl.h"
32#include "gc/accounting/heap_bitmap.h"
Mathieu Chartier31e89252013-08-28 11:29:12 -070033#include "gc/accounting/space_bitmap-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034#include "gc/heap.h"
35#include "gc/space/large_object_space.h"
36#include "gc/space/space-inl.h"
37#include "globals.h"
38#include "image.h"
39#include "intern_table.h"
Mathieu Chartierad2541a2013-10-25 10:05:23 -070040#include "lock_word.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070041#include "mirror/art_field-inl.h"
42#include "mirror/art_method-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070043#include "mirror/array-inl.h"
44#include "mirror/class-inl.h"
45#include "mirror/class_loader.h"
46#include "mirror/dex_cache-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070049#include "mirror/string-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070050#include "oat.h"
51#include "oat_file.h"
52#include "object_utils.h"
53#include "runtime.h"
54#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070055#include "handle_scope-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070056#include "utils.h"
57
Brian Carlstromea46f952013-07-30 01:26:50 -070058using ::art::mirror::ArtField;
59using ::art::mirror::ArtMethod;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070060using ::art::mirror::Class;
61using ::art::mirror::DexCache;
62using ::art::mirror::EntryPointFromInterpreter;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070063using ::art::mirror::Object;
64using ::art::mirror::ObjectArray;
65using ::art::mirror::String;
Brian Carlstrom7940e442013-07-12 13:46:57 -070066
67namespace art {
68
69bool ImageWriter::Write(const std::string& image_filename,
70 uintptr_t image_begin,
71 const std::string& oat_filename,
72 const std::string& oat_location) {
73 CHECK(!image_filename.empty());
74
75 CHECK_NE(image_begin, 0U);
76 image_begin_ = reinterpret_cast<byte*>(image_begin);
77
78 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -070079
Ian Rogers700a4022014-05-19 16:49:03 -070080 std::unique_ptr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str()));
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 if (oat_file.get() == NULL) {
82 LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
83 return false;
84 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070085 std::string error_msg;
86 oat_file_ = OatFile::OpenWritable(oat_file.get(), oat_location, &error_msg);
87 if (oat_file_ == nullptr) {
88 LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location
89 << ": " << error_msg;
Brian Carlstromc50d8e12013-07-23 22:35:16 -070090 return false;
91 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070092 CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070093
Ian Rogers848871b2013-08-05 10:56:33 -070094 interpreter_to_interpreter_bridge_offset_ =
95 oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset();
96 interpreter_to_compiled_code_bridge_offset_ =
97 oat_file_->GetOatHeader().GetInterpreterToCompiledCodeBridgeOffset();
98
99 jni_dlsym_lookup_offset_ = oat_file_->GetOatHeader().GetJniDlsymLookupOffset();
100
Jeff Hao88474b42013-10-23 16:24:40 -0700101 portable_imt_conflict_trampoline_offset_ =
102 oat_file_->GetOatHeader().GetPortableImtConflictTrampolineOffset();
Ian Rogers848871b2013-08-05 10:56:33 -0700103 portable_resolution_trampoline_offset_ =
104 oat_file_->GetOatHeader().GetPortableResolutionTrampolineOffset();
105 portable_to_interpreter_bridge_offset_ =
106 oat_file_->GetOatHeader().GetPortableToInterpreterBridgeOffset();
107
Andreas Gampe2da88232014-02-27 12:26:20 -0800108 quick_generic_jni_trampoline_offset_ =
109 oat_file_->GetOatHeader().GetQuickGenericJniTrampolineOffset();
Jeff Hao88474b42013-10-23 16:24:40 -0700110 quick_imt_conflict_trampoline_offset_ =
111 oat_file_->GetOatHeader().GetQuickImtConflictTrampolineOffset();
Ian Rogers848871b2013-08-05 10:56:33 -0700112 quick_resolution_trampoline_offset_ =
113 oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset();
114 quick_to_interpreter_bridge_offset_ =
115 oat_file_->GetOatHeader().GetQuickToInterpreterBridgeOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 {
117 Thread::Current()->TransitionFromSuspendedToRunnable();
118 PruneNonImageClasses(); // Remove junk
119 ComputeLazyFieldsForImageClasses(); // Add useful information
120 ComputeEagerResolvedStrings();
121 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
122 }
123 gc::Heap* heap = Runtime::Current()->GetHeap();
124 heap->CollectGarbage(false); // Remove garbage.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125
126 if (!AllocMemory()) {
127 return false;
128 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700129
130 if (kIsDebugBuild) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131 ScopedObjectAccess soa(Thread::Current());
132 CheckNonImageClassesRemoved();
133 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700134
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 Thread::Current()->TransitionFromSuspendedToRunnable();
136 size_t oat_loaded_size = 0;
137 size_t oat_data_offset = 0;
138 ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset);
139 CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset);
140 CopyAndFixupObjects();
141 PatchOatCodeAndMethods();
142 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
143
Ian Rogers700a4022014-05-19 16:49:03 -0700144 std::unique_ptr<File> image_file(OS::CreateEmptyFile(image_filename.c_str()));
Mathieu Chartier31e89252013-08-28 11:29:12 -0700145 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 if (image_file.get() == NULL) {
147 LOG(ERROR) << "Failed to open image file " << image_filename;
148 return false;
149 }
150 if (fchmod(image_file->Fd(), 0644) != 0) {
151 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
152 return EXIT_FAILURE;
153 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700154
155 // Write out the image.
156 CHECK_EQ(image_end_, image_header->GetImageSize());
157 if (!image_file->WriteFully(image_->Begin(), image_end_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 PLOG(ERROR) << "Failed to write image file " << image_filename;
159 return false;
160 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700161
162 // Write out the image bitmap at the page aligned start of the image end.
163 CHECK_ALIGNED(image_header->GetImageBitmapOffset(), kPageSize);
164 if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()),
165 image_header->GetImageBitmapSize(),
166 image_header->GetImageBitmapOffset())) {
167 PLOG(ERROR) << "Failed to write image file " << image_filename;
168 return false;
169 }
170
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 return true;
172}
173
Mathieu Chartier590fee92013-09-13 13:46:47 -0700174void ImageWriter::SetImageOffset(mirror::Object* object, size_t offset) {
175 DCHECK(object != nullptr);
176 DCHECK_NE(offset, 0U);
177 DCHECK(!IsImageOffsetAssigned(object));
178 mirror::Object* obj = reinterpret_cast<mirror::Object*>(image_->Begin() + offset);
179 DCHECK_ALIGNED(obj, kObjectAlignment);
180 image_bitmap_->Set(obj);
181 // Before we stomp over the lock word, save the hash code for later.
182 Monitor::Deflate(Thread::Current(), object);;
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700183 LockWord lw(object->GetLockWord(false));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700184 switch (lw.GetState()) {
185 case LockWord::kFatLocked: {
186 LOG(FATAL) << "Fat locked object " << obj << " found during object copy";
187 break;
188 }
189 case LockWord::kThinLocked: {
190 LOG(FATAL) << "Thin locked object " << obj << " found during object copy";
191 break;
192 }
193 case LockWord::kUnlocked:
194 // No hash, don't need to save it.
195 break;
196 case LockWord::kHashCode:
197 saved_hashes_.push_back(std::make_pair(obj, lw.GetHashCode()));
198 break;
199 default:
200 LOG(FATAL) << "Unreachable.";
201 break;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700202 }
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700203 object->SetLockWord(LockWord::FromForwardingAddress(offset), false);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700204 DCHECK(IsImageOffsetAssigned(object));
205}
206
207void ImageWriter::AssignImageOffset(mirror::Object* object) {
208 DCHECK(object != nullptr);
209 SetImageOffset(object, image_end_);
210 image_end_ += RoundUp(object->SizeOf(), 8); // 64-bit alignment
211 DCHECK_LT(image_end_, image_->Size());
212}
213
Ian Rogersef7d42f2014-01-06 12:55:46 -0800214bool ImageWriter::IsImageOffsetAssigned(mirror::Object* object) const {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700215 DCHECK(object != nullptr);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700216 return object->GetLockWord(false).GetState() == LockWord::kForwardingAddress;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700217}
218
Ian Rogersef7d42f2014-01-06 12:55:46 -0800219size_t ImageWriter::GetImageOffset(mirror::Object* object) const {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700220 DCHECK(object != nullptr);
221 DCHECK(IsImageOffsetAssigned(object));
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700222 LockWord lock_word = object->GetLockWord(false);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700223 size_t offset = lock_word.ForwardingAddress();
224 DCHECK_LT(offset, image_end_);
225 return offset;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700226}
227
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228bool ImageWriter::AllocMemory() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700229 size_t length = RoundUp(Runtime::Current()->GetHeap()->GetTotalMemory(), kPageSize);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700230 std::string error_msg;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700231 image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, PROT_READ | PROT_WRITE,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800232 true, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700233 if (UNLIKELY(image_.get() == nullptr)) {
234 LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 return false;
236 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700237
238 // Create the image bitmap.
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700239 image_bitmap_.reset(gc::accounting::ContinuousSpaceBitmap::Create("image bitmap", image_->Begin(),
240 length));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700241 if (image_bitmap_.get() == nullptr) {
242 LOG(ERROR) << "Failed to allocate memory for image bitmap";
243 return false;
244 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 return true;
246}
247
248void ImageWriter::ComputeLazyFieldsForImageClasses() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700249 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
251}
252
253bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700254 Thread* self = Thread::Current();
255 StackHandleScope<1> hs(self);
256 mirror::Class::ComputeName(hs.NewHandle(c));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 return true;
258}
259
260void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
261 if (!obj->GetClass()->IsStringClass()) {
262 return;
263 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700264 mirror::String* string = obj->AsString();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset();
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700266 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
267 ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock());
268 size_t dex_cache_count = class_linker->GetDexCacheCount();
269 for (size_t i = 0; i < dex_cache_count; ++i) {
270 DexCache* dex_cache = class_linker->GetDexCache(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers24c534d2013-11-14 00:15:00 -0800272 const DexFile::StringId* string_id;
273 if (UNLIKELY(string->GetLength() == 0)) {
274 string_id = dex_file.FindStringId("");
275 } else {
276 string_id = dex_file.FindStringId(utf16_string);
277 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700278 if (string_id != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 // This string occurs in this dex file, assign the dex cache entry.
280 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
281 if (dex_cache->GetResolvedString(string_idx) == NULL) {
282 dex_cache->SetResolvedString(string_idx, string);
283 }
284 }
285 }
286}
287
Mathieu Chartier590fee92013-09-13 13:46:47 -0700288void ImageWriter::ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
289 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
290 Runtime::Current()->GetHeap()->VisitObjects(ComputeEagerResolvedStringsCallback, this);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291}
292
Ian Rogersef7d42f2014-01-06 12:55:46 -0800293bool ImageWriter::IsImageClass(Class* klass) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700294 return compiler_driver_.IsImageClass(klass->GetDescriptor().c_str());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295}
296
297struct NonImageClasses {
298 ImageWriter* image_writer;
299 std::set<std::string>* non_image_classes;
300};
301
302void ImageWriter::PruneNonImageClasses() {
303 if (compiler_driver_.GetImageClasses() == NULL) {
304 return;
305 }
306 Runtime* runtime = Runtime::Current();
307 ClassLinker* class_linker = runtime->GetClassLinker();
308
309 // Make a list of classes we would like to prune.
310 std::set<std::string> non_image_classes;
311 NonImageClasses context;
312 context.image_writer = this;
313 context.non_image_classes = &non_image_classes;
314 class_linker->VisitClasses(NonImageClassesVisitor, &context);
315
316 // Remove the undesired classes from the class roots.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700317 for (const std::string& it : non_image_classes) {
318 class_linker->RemoveClass(it.c_str(), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 }
320
321 // Clear references to removed classes from the DexCaches.
Brian Carlstromea46f952013-07-30 01:26:50 -0700322 ArtMethod* resolution_method = runtime->GetResolutionMethod();
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700323 ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock());
324 size_t dex_cache_count = class_linker->GetDexCacheCount();
325 for (size_t idx = 0; idx < dex_cache_count; ++idx) {
326 DexCache* dex_cache = class_linker->GetDexCache(idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
328 Class* klass = dex_cache->GetResolvedType(i);
329 if (klass != NULL && !IsImageClass(klass)) {
330 dex_cache->SetResolvedType(i, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 }
332 }
333 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700334 ArtMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
336 dex_cache->SetResolvedMethod(i, resolution_method);
337 }
338 }
339 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700340 ArtField* field = dex_cache->GetResolvedField(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
342 dex_cache->SetResolvedField(i, NULL);
343 }
344 }
345 }
346}
347
348bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
349 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
350 if (!context->image_writer->IsImageClass(klass)) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700351 context->non_image_classes->insert(klass->GetDescriptor());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 }
353 return true;
354}
355
356void ImageWriter::CheckNonImageClassesRemoved()
357 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700358 if (compiler_driver_.GetImageClasses() != nullptr) {
359 gc::Heap* heap = Runtime::Current()->GetHeap();
360 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
361 heap->VisitObjects(CheckNonImageClassesRemovedCallback, this);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363}
364
365void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
366 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700367 if (obj->IsClass()) {
368 Class* klass = obj->AsClass();
369 if (!image_writer->IsImageClass(klass)) {
370 image_writer->DumpImageClasses();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700371 CHECK(image_writer->IsImageClass(klass)) << klass->GetDescriptor()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700372 << " " << PrettyDescriptor(klass);
373 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 }
375}
376
377void ImageWriter::DumpImageClasses() {
378 CompilerDriver::DescriptorSet* image_classes = compiler_driver_.GetImageClasses();
379 CHECK(image_classes != NULL);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700380 for (const std::string& image_class : *image_classes) {
381 LOG(INFO) << " " << image_class;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 }
383}
384
Mathieu Chartier590fee92013-09-13 13:46:47 -0700385void ImageWriter::CalculateObjectOffsets(Object* obj) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 DCHECK(obj != NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 // if it is a string, we want to intern it if its not interned.
388 if (obj->GetClass()->IsStringClass()) {
389 // we must be an interned string that was forward referenced and already assigned
Mathieu Chartier590fee92013-09-13 13:46:47 -0700390 if (IsImageOffsetAssigned(obj)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 DCHECK_EQ(obj, obj->AsString()->Intern());
392 return;
393 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700394 mirror::String* const interned = obj->AsString()->Intern();
395 if (obj != interned) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700396 if (!IsImageOffsetAssigned(interned)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 // interned obj is after us, allocate its location early
Mathieu Chartier590fee92013-09-13 13:46:47 -0700398 AssignImageOffset(interned);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 }
400 // point those looking for this object to the interned version.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700401 SetImageOffset(obj, GetImageOffset(interned));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700402 return;
403 }
404 // else (obj == interned), nothing to do but fall through to the normal case
405 }
406
Mathieu Chartier590fee92013-09-13 13:46:47 -0700407 AssignImageOffset(obj);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408}
409
410ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
411 Runtime* runtime = Runtime::Current();
412 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700414 StackHandleScope<3> hs(self);
415 Handle<Class> object_array_class(hs.NewHandle(
416 class_linker->FindSystemClass(self, "[Ljava/lang/Object;")));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700418 // build an Object[] of all the DexCaches used in the source_space_.
419 // Since we can't hold the dex lock when allocating the dex_caches
420 // ObjectArray, we lock the dex lock twice, first to get the number
421 // of dex caches first and then lock it again to copy the dex
422 // caches. We check that the number of dex caches does not change.
423 size_t dex_cache_count;
424 {
425 ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock());
426 dex_cache_count = class_linker->GetDexCacheCount();
427 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700428 Handle<ObjectArray<Object>> dex_caches(
429 hs.NewHandle(ObjectArray<Object>::Alloc(self, object_array_class.Get(),
Hiroshi Yamauchie9e3e692014-06-24 14:31:37 -0700430 dex_cache_count)));
431 CHECK(dex_caches.Get() != nullptr) << "Failed to allocate a dex cache array.";
432 {
433 ReaderMutexLock mu(Thread::Current(), *class_linker->DexLock());
434 CHECK_EQ(dex_cache_count, class_linker->GetDexCacheCount())
435 << "The number of dex caches changed.";
436 for (size_t i = 0; i < dex_cache_count; ++i) {
437 dex_caches->Set<false>(i, class_linker->GetDexCache(i));
438 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 }
440
441 // build an Object[] of the roots needed to restore the runtime
Ian Rogers700a4022014-05-19 16:49:03 -0700442 Handle<ObjectArray<Object>> image_roots(hs.NewHandle(
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700443 ObjectArray<Object>::Alloc(self, object_array_class.Get(), ImageHeader::kImageRootsMax)));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100444 image_roots->Set<false>(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
445 image_roots->Set<false>(ImageHeader::kImtConflictMethod, runtime->GetImtConflictMethod());
446 image_roots->Set<false>(ImageHeader::kDefaultImt, runtime->GetDefaultImt());
447 image_roots->Set<false>(ImageHeader::kCalleeSaveMethod,
448 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
449 image_roots->Set<false>(ImageHeader::kRefsOnlySaveMethod,
450 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
451 image_roots->Set<false>(ImageHeader::kRefsAndArgsSaveMethod,
452 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700453 image_roots->Set<false>(ImageHeader::kDexCaches, dex_caches.Get());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100454 image_roots->Set<false>(ImageHeader::kClassRoots, class_linker->GetClassRoots());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
456 CHECK(image_roots->Get(i) != NULL);
457 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700458 return image_roots.Get();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459}
460
Mathieu Chartier590fee92013-09-13 13:46:47 -0700461// Walk instance fields of the given Class. Separate function to allow recursion on the super
462// class.
463void ImageWriter::WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) {
464 // Visit fields of parent classes first.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700465 StackHandleScope<1> hs(Thread::Current());
466 Handle<mirror::Class> h_class(hs.NewHandle(klass));
467 mirror::Class* super = h_class->GetSuperClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700468 if (super != nullptr) {
469 WalkInstanceFields(obj, super);
470 }
471 //
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700472 size_t num_reference_fields = h_class->NumReferenceInstanceFields();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700473 for (size_t i = 0; i < num_reference_fields; ++i) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700474 mirror::ArtField* field = h_class->GetInstanceField(i);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700475 MemberOffset field_offset = field->GetOffset();
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700476 mirror::Object* value = obj->GetFieldObject<mirror::Object>(field_offset);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700477 if (value != nullptr) {
478 WalkFieldsInOrder(value);
479 }
480 }
481}
482
483// For an unvisited object, visit it then all its children found via fields.
484void ImageWriter::WalkFieldsInOrder(mirror::Object* obj) {
485 if (!IsImageOffsetAssigned(obj)) {
486 // Walk instance fields of all objects
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700487 StackHandleScope<2> hs(Thread::Current());
488 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
489 Handle<mirror::Class> klass(hs.NewHandle(obj->GetClass()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700490 // visit the object itself.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700491 CalculateObjectOffsets(h_obj.Get());
492 WalkInstanceFields(h_obj.Get(), klass.Get());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700493 // Walk static fields of a Class.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700494 if (h_obj->IsClass()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700495 size_t num_static_fields = klass->NumReferenceStaticFields();
496 for (size_t i = 0; i < num_static_fields; ++i) {
497 mirror::ArtField* field = klass->GetStaticField(i);
498 MemberOffset field_offset = field->GetOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700499 mirror::Object* value = h_obj->GetFieldObject<mirror::Object>(field_offset);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700500 if (value != nullptr) {
501 WalkFieldsInOrder(value);
502 }
503 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700504 } else if (h_obj->IsObjectArray()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700505 // Walk elements of an object array.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700506 int32_t length = h_obj->AsObjectArray<mirror::Object>()->GetLength();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700507 for (int32_t i = 0; i < length; i++) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700508 mirror::ObjectArray<mirror::Object>* obj_array = h_obj->AsObjectArray<mirror::Object>();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700509 mirror::Object* value = obj_array->Get(i);
510 if (value != nullptr) {
511 WalkFieldsInOrder(value);
512 }
513 }
514 }
515 }
516}
517
518void ImageWriter::WalkFieldsCallback(mirror::Object* obj, void* arg) {
519 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
520 DCHECK(writer != nullptr);
521 writer->WalkFieldsInOrder(obj);
522}
523
Brian Carlstrom7940e442013-07-12 13:46:57 -0700524void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) {
525 CHECK_NE(0U, oat_loaded_size);
526 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700527 StackHandleScope<1> hs(self);
528 Handle<ObjectArray<Object>> image_roots(hs.NewHandle(CreateImageRoots()));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529
530 gc::Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 DCHECK_EQ(0U, image_end_);
532
Mathieu Chartier31e89252013-08-28 11:29:12 -0700533 // Leave space for the header, but do not write it yet, we need to
Brian Carlstrom7940e442013-07-12 13:46:57 -0700534 // know where image_roots is going to end up
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700535 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536
537 {
538 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700539 // TODO: Image spaces only?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700541 DCHECK_LT(image_end_, image_->Size());
542 // Clear any pre-existing monitors which may have been in the monitor words.
543 heap->VisitObjects(WalkFieldsCallback, this);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700544 self->EndAssertNoThreadSuspension(old);
545 }
546
547 const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize);
548 const byte* oat_file_end = oat_file_begin + oat_loaded_size;
549 oat_data_begin_ = oat_file_begin + oat_data_offset;
550 const byte* oat_data_end = oat_data_begin_ + oat_file_->Size();
551
Mathieu Chartier31e89252013-08-28 11:29:12 -0700552 // Return to write header at start of image with future location of image_roots. At this point,
553 // image_end_ is the size of the image (excluding bitmaps).
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700554 const size_t heap_bytes_per_bitmap_byte = kBitsPerByte * kObjectAlignment;
Mathieu Chartier12aeccd2013-11-13 15:52:06 -0800555 const size_t bitmap_bytes = RoundUp(image_end_, heap_bytes_per_bitmap_byte) /
556 heap_bytes_per_bitmap_byte;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800557 ImageHeader image_header(PointerToLowMemUInt32(image_begin_),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700558 static_cast<uint32_t>(image_end_),
559 RoundUp(image_end_, kPageSize),
Mathieu Chartier12aeccd2013-11-13 15:52:06 -0800560 RoundUp(bitmap_bytes, kPageSize),
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700561 PointerToLowMemUInt32(GetImageAddress(image_roots.Get())),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800563 PointerToLowMemUInt32(oat_file_begin),
564 PointerToLowMemUInt32(oat_data_begin_),
565 PointerToLowMemUInt32(oat_data_end),
566 PointerToLowMemUInt32(oat_file_end));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 memcpy(image_->Begin(), &image_header, sizeof(image_header));
568
569 // Note that image_end_ is left at end of used space
570}
571
572void ImageWriter::CopyAndFixupObjects()
573 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
574 Thread* self = Thread::Current();
575 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
576 gc::Heap* heap = Runtime::Current()->GetHeap();
577 // TODO: heap validation can't handle this fix up pass
578 heap->DisableObjectValidation();
579 // TODO: Image spaces only?
580 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700581 heap->VisitObjects(CopyAndFixupObjectsCallback, this);
582 // Fix up the object previously had hash codes.
583 for (const std::pair<mirror::Object*, uint32_t>& hash_pair : saved_hashes_) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700584 hash_pair.first->SetLockWord(LockWord::FromHashCode(hash_pair.second), false);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700585 }
586 saved_hashes_.clear();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 self->EndAssertNoThreadSuspension(old_cause);
588}
589
Mathieu Chartier590fee92013-09-13 13:46:47 -0700590void ImageWriter::CopyAndFixupObjectsCallback(Object* obj, void* arg) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700591 DCHECK(obj != nullptr);
592 DCHECK(arg != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594 // see GetLocalAddress for similar computation
595 size_t offset = image_writer->GetImageOffset(obj);
596 byte* dst = image_writer->image_->Begin() + offset;
597 const byte* src = reinterpret_cast<const byte*>(obj);
598 size_t n = obj->SizeOf();
599 DCHECK_LT(offset + n, image_writer->image_->Size());
600 memcpy(dst, src, n);
601 Object* copy = reinterpret_cast<Object*>(dst);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700602 // Write in a hash code of objects which have inflated monitors or a hash code in their monitor
603 // word.
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700604 copy->SetLockWord(LockWord(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700605 image_writer->FixupObject(obj, copy);
606}
607
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700608class FixupVisitor {
609 public:
610 FixupVisitor(ImageWriter* image_writer, Object* copy) : image_writer_(image_writer), copy_(copy) {
611 }
612
613 void operator()(Object* obj, MemberOffset offset, bool /*is_static*/) const
614 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Hiroshi Yamauchi6e83c172014-05-01 21:25:41 -0700615 Object* ref = obj->GetFieldObject<Object, kVerifyNone>(offset);
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700616 // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the
617 // image.
618 copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700619 offset, image_writer_->GetImageAddress(ref));
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700620 }
621
622 // java.lang.ref.Reference visitor.
623 void operator()(mirror::Class* /*klass*/, mirror::Reference* ref) const
624 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
625 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
626 copy_->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700627 mirror::Reference::ReferentOffset(), image_writer_->GetImageAddress(ref->GetReferent()));
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700628 }
629
630 private:
631 ImageWriter* const image_writer_;
632 mirror::Object* const copy_;
633};
634
Ian Rogersef7d42f2014-01-06 12:55:46 -0800635void ImageWriter::FixupObject(Object* orig, Object* copy) {
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700636 DCHECK(orig != nullptr);
637 DCHECK(copy != nullptr);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700638 if (kUseBakerOrBrooksReadBarrier) {
639 orig->AssertReadBarrierPointer();
640 if (kUseBrooksReadBarrier) {
641 // Note the address 'copy' isn't the same as the image address of 'orig'.
642 copy->SetReadBarrierPointer(GetImageAddress(orig));
643 DCHECK_EQ(copy->GetReadBarrierPointer(), GetImageAddress(orig));
644 }
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800645 }
Mathieu Chartierb7ea3ac2014-03-24 16:54:46 -0700646 FixupVisitor visitor(this, copy);
647 orig->VisitReferences<true /*visit class*/>(visitor, visitor);
648 if (orig->IsArtMethod<kVerifyNone>()) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800649 FixupMethod(orig->AsArtMethod<kVerifyNone>(), down_cast<ArtMethod*>(copy));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 }
651}
652
Ian Rogersef7d42f2014-01-06 12:55:46 -0800653void ImageWriter::FixupMethod(ArtMethod* orig, ArtMethod* copy) {
Ian Rogers848871b2013-08-05 10:56:33 -0700654 // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to
655 // oat_begin_
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656
Ian Rogers848871b2013-08-05 10:56:33 -0700657 // The resolution method has a special trampoline to call.
658 if (UNLIKELY(orig == Runtime::Current()->GetResolutionMethod())) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800659 copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_resolution_trampoline_offset_));
660 copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_resolution_trampoline_offset_));
Jeff Hao88474b42013-10-23 16:24:40 -0700661 } else if (UNLIKELY(orig == Runtime::Current()->GetImtConflictMethod())) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800662 copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_imt_conflict_trampoline_offset_));
663 copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_imt_conflict_trampoline_offset_));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700665 // We assume all methods have code. If they don't currently then we set them to the use the
666 // resolution trampoline. Abstract methods never have code and so we need to make sure their
667 // use results in an AbstractMethodError. We use the interpreter to achieve this.
668 if (UNLIKELY(orig->IsAbstract())) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800669 copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_to_interpreter_bridge_offset_));
670 copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_to_interpreter_bridge_offset_));
671 copy->SetEntryPointFromInterpreter<kVerifyNone>(reinterpret_cast<EntryPointFromInterpreter*>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800672 (const_cast<byte*>(GetOatAddress(interpreter_to_interpreter_bridge_offset_))));
Ian Rogers848871b2013-08-05 10:56:33 -0700673 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700674 // Use original code if it exists. Otherwise, set the code pointer to the resolution
675 // trampoline.
Sebastien Hertze1d07812014-05-21 15:44:09 +0200676
677 // Quick entrypoint:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800678 const byte* quick_code = GetOatAddress(orig->GetQuickOatCodeOffset());
Sebastien Hertze1d07812014-05-21 15:44:09 +0200679 bool quick_is_interpreted = false;
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800680 if (quick_code != nullptr &&
681 (!orig->IsStatic() || orig->IsConstructor() || orig->GetDeclaringClass()->IsInitialized())) {
682 // We have code for a non-static or initialized method, just use the code.
Ian Rogers1a570662014-03-12 01:02:21 -0700683 } else if (quick_code == nullptr && orig->IsNative() &&
684 (!orig->IsStatic() || orig->GetDeclaringClass()->IsInitialized())) {
685 // Non-static or initialized native method missing compiled code, use generic JNI version.
Sebastien Hertze1d07812014-05-21 15:44:09 +0200686 quick_code = GetOatAddress(quick_generic_jni_trampoline_offset_);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800687 } else if (quick_code == nullptr && !orig->IsNative()) {
688 // We don't have code at all for a non-native method, use the interpreter.
Sebastien Hertze1d07812014-05-21 15:44:09 +0200689 quick_code = GetOatAddress(quick_to_interpreter_bridge_offset_);
690 quick_is_interpreted = true;
Ian Rogers848871b2013-08-05 10:56:33 -0700691 } else {
Ian Rogers1a570662014-03-12 01:02:21 -0700692 CHECK(!orig->GetDeclaringClass()->IsInitialized());
693 // We have code for a static method, but need to go through the resolution stub for class
694 // initialization.
Sebastien Hertze1d07812014-05-21 15:44:09 +0200695 quick_code = GetOatAddress(quick_resolution_trampoline_offset_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800696 }
Sebastien Hertze1d07812014-05-21 15:44:09 +0200697 copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(quick_code);
698
699 // Portable entrypoint:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800700 const byte* portable_code = GetOatAddress(orig->GetPortableOatCodeOffset());
Sebastien Hertze1d07812014-05-21 15:44:09 +0200701 bool portable_is_interpreted = false;
702 if (portable_code != nullptr &&
703 (!orig->IsStatic() || orig->IsConstructor() || orig->GetDeclaringClass()->IsInitialized())) {
704 // We have code for a non-static or initialized method, just use the code.
705 } else if (portable_code == nullptr && orig->IsNative() &&
706 (!orig->IsStatic() || orig->GetDeclaringClass()->IsInitialized())) {
707 // Non-static or initialized native method missing compiled code, use generic JNI version.
708 // TODO: generic JNI support for LLVM.
709 portable_code = GetOatAddress(portable_resolution_trampoline_offset_);
710 } else if (portable_code == nullptr && !orig->IsNative()) {
711 // We don't have code at all for a non-native method, use the interpreter.
712 portable_code = GetOatAddress(portable_to_interpreter_bridge_offset_);
713 portable_is_interpreted = true;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800714 } else {
Sebastien Hertze1d07812014-05-21 15:44:09 +0200715 CHECK(!orig->GetDeclaringClass()->IsInitialized());
716 // We have code for a static method, but need to go through the resolution stub for class
717 // initialization.
718 portable_code = GetOatAddress(portable_resolution_trampoline_offset_);
Ian Rogers848871b2013-08-05 10:56:33 -0700719 }
Sebastien Hertze1d07812014-05-21 15:44:09 +0200720 copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(portable_code);
721
722 // JNI entrypoint:
Ian Rogers848871b2013-08-05 10:56:33 -0700723 if (orig->IsNative()) {
724 // The native method's pointer is set to a stub to lookup via dlsym.
725 // Note this is not the code_ pointer, that is handled above.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800726 copy->SetNativeMethod<kVerifyNone>(GetOatAddress(jni_dlsym_lookup_offset_));
Ian Rogers848871b2013-08-05 10:56:33 -0700727 } else {
728 // Normal (non-abstract non-native) methods have various tables to relocate.
Ian Rogers848871b2013-08-05 10:56:33 -0700729 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
730 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800731 copy->SetNativeGcMap<kVerifyNone>(reinterpret_cast<const uint8_t*>(native_gc_map));
Ian Rogers848871b2013-08-05 10:56:33 -0700732 }
Sebastien Hertze1d07812014-05-21 15:44:09 +0200733
734 // Interpreter entrypoint:
735 // Set the interpreter entrypoint depending on whether there is compiled code or not.
736 uint32_t interpreter_code = (quick_is_interpreted && portable_is_interpreted)
737 ? interpreter_to_interpreter_bridge_offset_
738 : interpreter_to_compiled_code_bridge_offset_;
739 copy->SetEntryPointFromInterpreter<kVerifyNone>(
740 reinterpret_cast<EntryPointFromInterpreter*>(
741 const_cast<byte*>(GetOatAddress(interpreter_code))));
Ian Rogers848871b2013-08-05 10:56:33 -0700742 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 }
744}
745
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800746static ArtMethod* GetTargetMethod(const CompilerDriver::CallPatchInformation* patch)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700747 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
748 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700749 StackHandleScope<1> hs(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700750 Handle<mirror::DexCache> dex_cache(
751 hs.NewHandle(class_linker->FindDexCache(*patch->GetTargetDexFile())));
Jeff Hao49161ce2014-03-12 11:05:25 -0700752 ArtMethod* method = class_linker->ResolveMethod(*patch->GetTargetDexFile(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700753 patch->GetTargetMethodIdx(),
754 dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700755 NullHandle<mirror::ClassLoader>(),
756 NullHandle<mirror::ArtMethod>(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700757 patch->GetTargetInvokeType());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700758 CHECK(method != NULL)
Jeff Hao49161ce2014-03-12 11:05:25 -0700759 << patch->GetTargetDexFile()->GetLocation() << " " << patch->GetTargetMethodIdx();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 CHECK(!method->IsRuntimeMethod())
Jeff Hao49161ce2014-03-12 11:05:25 -0700761 << patch->GetTargetDexFile()->GetLocation() << " " << patch->GetTargetMethodIdx();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
Jeff Hao49161ce2014-03-12 11:05:25 -0700763 << patch->GetTargetDexFile()->GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
765 << PrettyMethod(method);
766 return method;
767}
768
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800769static Class* GetTargetType(const CompilerDriver::TypePatchInformation* patch)
770 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
771 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700772 StackHandleScope<2> hs(Thread::Current());
773 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(patch->GetDexFile())));
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700774 Class* klass = class_linker->ResolveType(patch->GetDexFile(), patch->GetTargetTypeIdx(),
775 dex_cache, NullHandle<mirror::ClassLoader>());
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800776 CHECK(klass != NULL)
777 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetTypeIdx();
778 CHECK(dex_cache->GetResolvedTypes()->Get(patch->GetTargetTypeIdx()) == klass)
779 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
780 << PrettyClass(dex_cache->GetResolvedTypes()->Get(patch->GetTargetTypeIdx())) << " "
781 << PrettyClass(klass);
782 return klass;
783}
784
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785void ImageWriter::PatchOatCodeAndMethods() {
786 Thread* self = Thread::Current();
787 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
788 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
789
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800790 typedef std::vector<const CompilerDriver::CallPatchInformation*> CallPatches;
791 const CallPatches& code_to_patch = compiler_driver_.GetCodeToPatch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700792 for (size_t i = 0; i < code_to_patch.size(); i++) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800793 const CompilerDriver::CallPatchInformation* patch = code_to_patch[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700794 ArtMethod* target = GetTargetMethod(patch);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800795 uintptr_t quick_code = reinterpret_cast<uintptr_t>(class_linker->GetQuickOatCodeFor(target));
Mathieu Chartier57d27332014-05-29 10:19:19 -0700796 DCHECK_NE(quick_code, 0U) << PrettyMethod(target);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800797 uintptr_t code_base = reinterpret_cast<uintptr_t>(&oat_file_->GetOatHeader());
798 uintptr_t code_offset = quick_code - code_base;
Mathieu Chartier57d27332014-05-29 10:19:19 -0700799 bool is_quick_offset = false;
800 if (quick_code == reinterpret_cast<uintptr_t>(GetQuickToInterpreterBridge())) {
801 is_quick_offset = true;
Mathieu Chartierd8a737a2014-05-30 16:44:47 +0000802 code_offset = quick_to_interpreter_bridge_offset_;
Mathieu Chartier57d27332014-05-29 10:19:19 -0700803 } else if (quick_code ==
804 reinterpret_cast<uintptr_t>(class_linker->GetQuickGenericJniTrampoline())) {
805 CHECK(target->IsNative());
806 is_quick_offset = true;
Mathieu Chartierd8a737a2014-05-30 16:44:47 +0000807 code_offset = quick_generic_jni_trampoline_offset_;
Mathieu Chartier57d27332014-05-29 10:19:19 -0700808 }
809 uintptr_t value;
Mark Mendell55d0eac2014-02-06 11:02:52 -0800810 if (patch->IsRelative()) {
811 // value to patch is relative to the location being patched
812 const void* quick_oat_code =
813 class_linker->GetQuickOatCodeFor(patch->GetDexFile(),
814 patch->GetReferrerClassDefIdx(),
815 patch->GetReferrerMethodIdx());
Mathieu Chartier57d27332014-05-29 10:19:19 -0700816 if (is_quick_offset) {
Mathieu Chartier57d27332014-05-29 10:19:19 -0700817 // If its a quick offset it means that we are doing a relative patch from the class linker
818 // oat_file to the image writer oat_file so we need to adjust the quick oat code to be the
819 // one in the image writer oat_file.
Mathieu Chartierd8a737a2014-05-30 16:44:47 +0000820 quick_code = PointerToLowMemUInt32(GetOatAddress(code_offset));
Mathieu Chartier57d27332014-05-29 10:19:19 -0700821 quick_oat_code =
822 reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(quick_oat_code) +
823 reinterpret_cast<uintptr_t>(oat_data_begin_) - code_base);
824 }
Mark Mendell55d0eac2014-02-06 11:02:52 -0800825 uintptr_t base = reinterpret_cast<uintptr_t>(quick_oat_code);
826 uintptr_t patch_location = base + patch->GetLiteralOffset();
Mathieu Chartier57d27332014-05-29 10:19:19 -0700827 value = quick_code - patch_location + patch->RelativeOffset();
Mark Mendell55d0eac2014-02-06 11:02:52 -0800828 } else {
Mathieu Chartierd8a737a2014-05-30 16:44:47 +0000829 value = PointerToLowMemUInt32(GetOatAddress(code_offset));
Mark Mendell55d0eac2014-02-06 11:02:52 -0800830 }
Mathieu Chartier57d27332014-05-29 10:19:19 -0700831 SetPatchLocation(patch, value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 }
833
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800834 const CallPatches& methods_to_patch = compiler_driver_.GetMethodsToPatch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835 for (size_t i = 0; i < methods_to_patch.size(); i++) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800836 const CompilerDriver::CallPatchInformation* patch = methods_to_patch[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700837 ArtMethod* target = GetTargetMethod(patch);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800838 SetPatchLocation(patch, PointerToLowMemUInt32(GetImageAddress(target)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839 }
840
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800841 const std::vector<const CompilerDriver::TypePatchInformation*>& classes_to_patch =
842 compiler_driver_.GetClassesToPatch();
843 for (size_t i = 0; i < classes_to_patch.size(); i++) {
844 const CompilerDriver::TypePatchInformation* patch = classes_to_patch[i];
845 Class* target = GetTargetType(patch);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800846 SetPatchLocation(patch, PointerToLowMemUInt32(GetImageAddress(target)));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800847 }
848
Brian Carlstrom7940e442013-07-12 13:46:57 -0700849 // Update the image header with the new checksum after patching
850 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
851 image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
852 self->EndAssertNoThreadSuspension(old_cause);
853}
854
855void ImageWriter::SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value) {
856 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800857 const void* quick_oat_code = class_linker->GetQuickOatCodeFor(patch->GetDexFile(),
858 patch->GetReferrerClassDefIdx(),
859 patch->GetReferrerMethodIdx());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860 OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
861 // TODO: make this Thumb2 specific
Ian Rogersef7d42f2014-01-06 12:55:46 -0800862 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(quick_oat_code) & ~0x1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700863 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700864 if (kIsDebugBuild) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800865 if (patch->IsCall()) {
866 const CompilerDriver::CallPatchInformation* cpatch = patch->AsCall();
Jeff Hao49161ce2014-03-12 11:05:25 -0700867 const DexFile::MethodId& id = cpatch->GetTargetDexFile()->GetMethodId(cpatch->GetTargetMethodIdx());
Andreas Gampe2da88232014-02-27 12:26:20 -0800868 uint32_t expected = reinterpret_cast<uintptr_t>(&id) & 0xFFFFFFFF;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800869 uint32_t actual = *patch_location;
870 CHECK(actual == expected || actual == value) << std::hex
871 << "actual=" << actual
872 << "expected=" << expected
873 << "value=" << value;
874 }
875 if (patch->IsType()) {
876 const CompilerDriver::TypePatchInformation* tpatch = patch->AsType();
877 const DexFile::TypeId& id = tpatch->GetDexFile().GetTypeId(tpatch->GetTargetTypeIdx());
Andreas Gampe2da88232014-02-27 12:26:20 -0800878 uint32_t expected = reinterpret_cast<uintptr_t>(&id) & 0xFFFFFFFF;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800879 uint32_t actual = *patch_location;
880 CHECK(actual == expected || actual == value) << std::hex
881 << "actual=" << actual
882 << "expected=" << expected
883 << "value=" << value;
884 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700885 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700886 *patch_location = value;
887 oat_header.UpdateChecksum(patch_location, sizeof(value));
888}
889
890} // namespace art