blob: f4b507a64aa1a1f1b79b2d872dd28a43521b9c56 [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
21#include <vector>
22
23#include "base/logging.h"
24#include "base/unix_file/fd_file.h"
25#include "class_linker.h"
26#include "compiled_method.h"
27#include "dex_file-inl.h"
28#include "driver/compiler_driver.h"
29#include "elf_writer.h"
30#include "gc/accounting/card_table-inl.h"
31#include "gc/accounting/heap_bitmap.h"
Mathieu Chartier31e89252013-08-28 11:29:12 -070032#include "gc/accounting/space_bitmap-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033#include "gc/heap.h"
34#include "gc/space/large_object_space.h"
35#include "gc/space/space-inl.h"
36#include "globals.h"
37#include "image.h"
38#include "intern_table.h"
Mathieu Chartierad2541a2013-10-25 10:05:23 -070039#include "lock_word.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070040#include "mirror/art_field-inl.h"
41#include "mirror/art_method-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070042#include "mirror/array-inl.h"
43#include "mirror/class-inl.h"
44#include "mirror/class_loader.h"
45#include "mirror/dex_cache-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
48#include "oat.h"
49#include "oat_file.h"
50#include "object_utils.h"
51#include "runtime.h"
52#include "scoped_thread_state_change.h"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080053#include "sirt_ref-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070054#include "UniquePtr.h"
55#include "utils.h"
56
Brian Carlstromea46f952013-07-30 01:26:50 -070057using ::art::mirror::ArtField;
58using ::art::mirror::ArtMethod;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070059using ::art::mirror::Class;
60using ::art::mirror::DexCache;
61using ::art::mirror::EntryPointFromInterpreter;
Brian Carlstrom3e3d5912013-07-18 00:19:45 -070062using ::art::mirror::Object;
63using ::art::mirror::ObjectArray;
64using ::art::mirror::String;
Brian Carlstrom7940e442013-07-12 13:46:57 -070065
66namespace art {
67
68bool ImageWriter::Write(const std::string& image_filename,
69 uintptr_t image_begin,
70 const std::string& oat_filename,
71 const std::string& oat_location) {
72 CHECK(!image_filename.empty());
73
74 CHECK_NE(image_begin, 0U);
75 image_begin_ = reinterpret_cast<byte*>(image_begin);
76
77 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -070078
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070079 UniquePtr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str()));
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 if (oat_file.get() == NULL) {
81 LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
82 return false;
83 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070084 std::string error_msg;
85 oat_file_ = OatFile::OpenWritable(oat_file.get(), oat_location, &error_msg);
86 if (oat_file_ == nullptr) {
87 LOG(ERROR) << "Failed to open writable oat file " << oat_filename << " for " << oat_location
88 << ": " << error_msg;
Brian Carlstromc50d8e12013-07-23 22:35:16 -070089 return false;
90 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -070091 CHECK_EQ(class_linker->RegisterOatFile(oat_file_), oat_file_);
Brian Carlstrom7940e442013-07-12 13:46:57 -070092
Ian Rogers848871b2013-08-05 10:56:33 -070093 interpreter_to_interpreter_bridge_offset_ =
94 oat_file_->GetOatHeader().GetInterpreterToInterpreterBridgeOffset();
95 interpreter_to_compiled_code_bridge_offset_ =
96 oat_file_->GetOatHeader().GetInterpreterToCompiledCodeBridgeOffset();
97
98 jni_dlsym_lookup_offset_ = oat_file_->GetOatHeader().GetJniDlsymLookupOffset();
99
Jeff Hao88474b42013-10-23 16:24:40 -0700100 portable_imt_conflict_trampoline_offset_ =
101 oat_file_->GetOatHeader().GetPortableImtConflictTrampolineOffset();
Ian Rogers848871b2013-08-05 10:56:33 -0700102 portable_resolution_trampoline_offset_ =
103 oat_file_->GetOatHeader().GetPortableResolutionTrampolineOffset();
104 portable_to_interpreter_bridge_offset_ =
105 oat_file_->GetOatHeader().GetPortableToInterpreterBridgeOffset();
106
Andreas Gampe2da88232014-02-27 12:26:20 -0800107 quick_generic_jni_trampoline_offset_ =
108 oat_file_->GetOatHeader().GetQuickGenericJniTrampolineOffset();
Jeff Hao88474b42013-10-23 16:24:40 -0700109 quick_imt_conflict_trampoline_offset_ =
110 oat_file_->GetOatHeader().GetQuickImtConflictTrampolineOffset();
Ian Rogers848871b2013-08-05 10:56:33 -0700111 quick_resolution_trampoline_offset_ =
112 oat_file_->GetOatHeader().GetQuickResolutionTrampolineOffset();
113 quick_to_interpreter_bridge_offset_ =
114 oat_file_->GetOatHeader().GetQuickToInterpreterBridgeOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 {
116 Thread::Current()->TransitionFromSuspendedToRunnable();
117 PruneNonImageClasses(); // Remove junk
118 ComputeLazyFieldsForImageClasses(); // Add useful information
119 ComputeEagerResolvedStrings();
120 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
121 }
122 gc::Heap* heap = Runtime::Current()->GetHeap();
123 heap->CollectGarbage(false); // Remove garbage.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124
125 if (!AllocMemory()) {
126 return false;
127 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700128
129 if (kIsDebugBuild) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 ScopedObjectAccess soa(Thread::Current());
131 CheckNonImageClassesRemoved();
132 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700133
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 Thread::Current()->TransitionFromSuspendedToRunnable();
135 size_t oat_loaded_size = 0;
136 size_t oat_data_offset = 0;
137 ElfWriter::GetOatElfInformation(oat_file.get(), oat_loaded_size, oat_data_offset);
138 CalculateNewObjectOffsets(oat_loaded_size, oat_data_offset);
139 CopyAndFixupObjects();
140 PatchOatCodeAndMethods();
141 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
142
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700143 UniquePtr<File> image_file(OS::CreateEmptyFile(image_filename.c_str()));
Mathieu Chartier31e89252013-08-28 11:29:12 -0700144 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 if (image_file.get() == NULL) {
146 LOG(ERROR) << "Failed to open image file " << image_filename;
147 return false;
148 }
149 if (fchmod(image_file->Fd(), 0644) != 0) {
150 PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
151 return EXIT_FAILURE;
152 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700153
154 // Write out the image.
155 CHECK_EQ(image_end_, image_header->GetImageSize());
156 if (!image_file->WriteFully(image_->Begin(), image_end_)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 PLOG(ERROR) << "Failed to write image file " << image_filename;
158 return false;
159 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700160
161 // Write out the image bitmap at the page aligned start of the image end.
162 CHECK_ALIGNED(image_header->GetImageBitmapOffset(), kPageSize);
163 if (!image_file->Write(reinterpret_cast<char*>(image_bitmap_->Begin()),
164 image_header->GetImageBitmapSize(),
165 image_header->GetImageBitmapOffset())) {
166 PLOG(ERROR) << "Failed to write image file " << image_filename;
167 return false;
168 }
169
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 return true;
171}
172
Mathieu Chartier590fee92013-09-13 13:46:47 -0700173void ImageWriter::SetImageOffset(mirror::Object* object, size_t offset) {
174 DCHECK(object != nullptr);
175 DCHECK_NE(offset, 0U);
176 DCHECK(!IsImageOffsetAssigned(object));
177 mirror::Object* obj = reinterpret_cast<mirror::Object*>(image_->Begin() + offset);
178 DCHECK_ALIGNED(obj, kObjectAlignment);
179 image_bitmap_->Set(obj);
180 // Before we stomp over the lock word, save the hash code for later.
181 Monitor::Deflate(Thread::Current(), object);;
182 LockWord lw(object->GetLockWord());
183 switch (lw.GetState()) {
184 case LockWord::kFatLocked: {
185 LOG(FATAL) << "Fat locked object " << obj << " found during object copy";
186 break;
187 }
188 case LockWord::kThinLocked: {
189 LOG(FATAL) << "Thin locked object " << obj << " found during object copy";
190 break;
191 }
192 case LockWord::kUnlocked:
193 // No hash, don't need to save it.
194 break;
195 case LockWord::kHashCode:
196 saved_hashes_.push_back(std::make_pair(obj, lw.GetHashCode()));
197 break;
198 default:
199 LOG(FATAL) << "Unreachable.";
200 break;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700201 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700202 object->SetLockWord(LockWord::FromForwardingAddress(offset));
203 DCHECK(IsImageOffsetAssigned(object));
204}
205
206void ImageWriter::AssignImageOffset(mirror::Object* object) {
207 DCHECK(object != nullptr);
208 SetImageOffset(object, image_end_);
209 image_end_ += RoundUp(object->SizeOf(), 8); // 64-bit alignment
210 DCHECK_LT(image_end_, image_->Size());
211}
212
Ian Rogersef7d42f2014-01-06 12:55:46 -0800213bool ImageWriter::IsImageOffsetAssigned(mirror::Object* object) const {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700214 DCHECK(object != nullptr);
215 return object->GetLockWord().GetState() == LockWord::kForwardingAddress;
216}
217
Ian Rogersef7d42f2014-01-06 12:55:46 -0800218size_t ImageWriter::GetImageOffset(mirror::Object* object) const {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700219 DCHECK(object != nullptr);
220 DCHECK(IsImageOffsetAssigned(object));
221 LockWord lock_word = object->GetLockWord();
222 size_t offset = lock_word.ForwardingAddress();
223 DCHECK_LT(offset, image_end_);
224 return offset;
Mathieu Chartier31e89252013-08-28 11:29:12 -0700225}
226
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227bool ImageWriter::AllocMemory() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700228 size_t length = RoundUp(Runtime::Current()->GetHeap()->GetTotalMemory(), kPageSize);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700229 std::string error_msg;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700230 image_.reset(MemMap::MapAnonymous("image writer image", NULL, length, PROT_READ | PROT_WRITE,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800231 true, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700232 if (UNLIKELY(image_.get() == nullptr)) {
233 LOG(ERROR) << "Failed to allocate memory for image file generation: " << error_msg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 return false;
235 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700236
237 // Create the image bitmap.
238 image_bitmap_.reset(gc::accounting::SpaceBitmap::Create("image bitmap", image_->Begin(),
239 length));
240 if (image_bitmap_.get() == nullptr) {
241 LOG(ERROR) << "Failed to allocate memory for image bitmap";
242 return false;
243 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 return true;
245}
246
247void ImageWriter::ComputeLazyFieldsForImageClasses() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700248 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 class_linker->VisitClassesWithoutClassesLock(ComputeLazyFieldsForClassesVisitor, NULL);
250}
251
252bool ImageWriter::ComputeLazyFieldsForClassesVisitor(Class* c, void* /*arg*/) {
253 c->ComputeName();
254 return true;
255}
256
257void ImageWriter::ComputeEagerResolvedStringsCallback(Object* obj, void* arg) {
258 if (!obj->GetClass()->IsStringClass()) {
259 return;
260 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700261 mirror::String* string = obj->AsString();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 const uint16_t* utf16_string = string->GetCharArray()->GetData() + string->GetOffset();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700263 for (DexCache* dex_cache : Runtime::Current()->GetClassLinker()->GetDexCaches()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700264 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers24c534d2013-11-14 00:15:00 -0800265 const DexFile::StringId* string_id;
266 if (UNLIKELY(string->GetLength() == 0)) {
267 string_id = dex_file.FindStringId("");
268 } else {
269 string_id = dex_file.FindStringId(utf16_string);
270 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700271 if (string_id != nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 // This string occurs in this dex file, assign the dex cache entry.
273 uint32_t string_idx = dex_file.GetIndexForStringId(*string_id);
274 if (dex_cache->GetResolvedString(string_idx) == NULL) {
275 dex_cache->SetResolvedString(string_idx, string);
276 }
277 }
278 }
279}
280
Mathieu Chartier590fee92013-09-13 13:46:47 -0700281void ImageWriter::ComputeEagerResolvedStrings() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
282 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
283 Runtime::Current()->GetHeap()->VisitObjects(ComputeEagerResolvedStringsCallback, this);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284}
285
Ian Rogersef7d42f2014-01-06 12:55:46 -0800286bool ImageWriter::IsImageClass(Class* klass) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700287 return compiler_driver_.IsImageClass(ClassHelper(klass).GetDescriptor());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288}
289
290struct NonImageClasses {
291 ImageWriter* image_writer;
292 std::set<std::string>* non_image_classes;
293};
294
295void ImageWriter::PruneNonImageClasses() {
296 if (compiler_driver_.GetImageClasses() == NULL) {
297 return;
298 }
299 Runtime* runtime = Runtime::Current();
300 ClassLinker* class_linker = runtime->GetClassLinker();
301
302 // Make a list of classes we would like to prune.
303 std::set<std::string> non_image_classes;
304 NonImageClasses context;
305 context.image_writer = this;
306 context.non_image_classes = &non_image_classes;
307 class_linker->VisitClasses(NonImageClassesVisitor, &context);
308
309 // Remove the undesired classes from the class roots.
Mathieu Chartier02e25112013-08-14 16:14:24 -0700310 for (const std::string& it : non_image_classes) {
311 class_linker->RemoveClass(it.c_str(), NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 }
313
314 // Clear references to removed classes from the DexCaches.
Brian Carlstromea46f952013-07-30 01:26:50 -0700315 ArtMethod* resolution_method = runtime->GetResolutionMethod();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700316 for (DexCache* dex_cache : class_linker->GetDexCaches()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 for (size_t i = 0; i < dex_cache->NumResolvedTypes(); i++) {
318 Class* klass = dex_cache->GetResolvedType(i);
319 if (klass != NULL && !IsImageClass(klass)) {
320 dex_cache->SetResolvedType(i, NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 }
322 }
323 for (size_t i = 0; i < dex_cache->NumResolvedMethods(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700324 ArtMethod* method = dex_cache->GetResolvedMethod(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 if (method != NULL && !IsImageClass(method->GetDeclaringClass())) {
326 dex_cache->SetResolvedMethod(i, resolution_method);
327 }
328 }
329 for (size_t i = 0; i < dex_cache->NumResolvedFields(); i++) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700330 ArtField* field = dex_cache->GetResolvedField(i);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 if (field != NULL && !IsImageClass(field->GetDeclaringClass())) {
332 dex_cache->SetResolvedField(i, NULL);
333 }
334 }
335 }
336}
337
338bool ImageWriter::NonImageClassesVisitor(Class* klass, void* arg) {
339 NonImageClasses* context = reinterpret_cast<NonImageClasses*>(arg);
340 if (!context->image_writer->IsImageClass(klass)) {
Ian Rogersdfb325e2013-10-30 01:00:44 -0700341 context->non_image_classes->insert(ClassHelper(klass).GetDescriptor());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 }
343 return true;
344}
345
346void ImageWriter::CheckNonImageClassesRemoved()
347 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700348 if (compiler_driver_.GetImageClasses() != nullptr) {
349 gc::Heap* heap = Runtime::Current()->GetHeap();
350 ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_);
351 heap->VisitObjects(CheckNonImageClassesRemovedCallback, this);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353}
354
355void ImageWriter::CheckNonImageClassesRemovedCallback(Object* obj, void* arg) {
356 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700357 if (obj->IsClass()) {
358 Class* klass = obj->AsClass();
359 if (!image_writer->IsImageClass(klass)) {
360 image_writer->DumpImageClasses();
361 CHECK(image_writer->IsImageClass(klass)) << ClassHelper(klass).GetDescriptor()
362 << " " << PrettyDescriptor(klass);
363 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364 }
365}
366
367void ImageWriter::DumpImageClasses() {
368 CompilerDriver::DescriptorSet* image_classes = compiler_driver_.GetImageClasses();
369 CHECK(image_classes != NULL);
Mathieu Chartier02e25112013-08-14 16:14:24 -0700370 for (const std::string& image_class : *image_classes) {
371 LOG(INFO) << " " << image_class;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 }
373}
374
Mathieu Chartier590fee92013-09-13 13:46:47 -0700375void ImageWriter::CalculateObjectOffsets(Object* obj) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 DCHECK(obj != NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700377 // if it is a string, we want to intern it if its not interned.
378 if (obj->GetClass()->IsStringClass()) {
379 // we must be an interned string that was forward referenced and already assigned
Mathieu Chartier590fee92013-09-13 13:46:47 -0700380 if (IsImageOffsetAssigned(obj)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 DCHECK_EQ(obj, obj->AsString()->Intern());
382 return;
383 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700384 Thread* self = Thread::Current();
385 SirtRef<Object> sirt_obj(self, obj);
386 mirror::String* interned = obj->AsString()->Intern();
387 if (sirt_obj.get() != interned) {
388 if (!IsImageOffsetAssigned(interned)) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 // interned obj is after us, allocate its location early
Mathieu Chartier590fee92013-09-13 13:46:47 -0700390 AssignImageOffset(interned);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 }
392 // point those looking for this object to the interned version.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700393 SetImageOffset(sirt_obj.get(), GetImageOffset(interned));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 return;
395 }
396 // else (obj == interned), nothing to do but fall through to the normal case
397 }
398
Mathieu Chartier590fee92013-09-13 13:46:47 -0700399 AssignImageOffset(obj);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400}
401
402ObjectArray<Object>* ImageWriter::CreateImageRoots() const {
403 Runtime* runtime = Runtime::Current();
404 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700405 Thread* self = Thread::Current();
Ian Rogers98379392014-02-24 16:53:16 -0800406 SirtRef<Class> object_array_class(self, class_linker->FindSystemClass(self,
407 "[Ljava/lang/Object;"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408
409 // build an Object[] of all the DexCaches used in the source_space_
Mathieu Chartier590fee92013-09-13 13:46:47 -0700410 ObjectArray<Object>* dex_caches = ObjectArray<Object>::Alloc(self, object_array_class.get(),
411 class_linker->GetDexCaches().size());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412 int i = 0;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700413 for (DexCache* dex_cache : class_linker->GetDexCaches()) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100414 dex_caches->Set<false>(i++, dex_cache);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 }
416
417 // build an Object[] of the roots needed to restore the runtime
Mathieu Chartier590fee92013-09-13 13:46:47 -0700418 SirtRef<ObjectArray<Object> > image_roots(
419 self, ObjectArray<Object>::Alloc(self, object_array_class.get(), ImageHeader::kImageRootsMax));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100420 image_roots->Set<false>(ImageHeader::kResolutionMethod, runtime->GetResolutionMethod());
421 image_roots->Set<false>(ImageHeader::kImtConflictMethod, runtime->GetImtConflictMethod());
422 image_roots->Set<false>(ImageHeader::kDefaultImt, runtime->GetDefaultImt());
423 image_roots->Set<false>(ImageHeader::kCalleeSaveMethod,
424 runtime->GetCalleeSaveMethod(Runtime::kSaveAll));
425 image_roots->Set<false>(ImageHeader::kRefsOnlySaveMethod,
426 runtime->GetCalleeSaveMethod(Runtime::kRefsOnly));
427 image_roots->Set<false>(ImageHeader::kRefsAndArgsSaveMethod,
428 runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100429 image_roots->Set<false>(ImageHeader::kDexCaches, dex_caches);
430 image_roots->Set<false>(ImageHeader::kClassRoots, class_linker->GetClassRoots());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
432 CHECK(image_roots->Get(i) != NULL);
433 }
434 return image_roots.get();
435}
436
Mathieu Chartier590fee92013-09-13 13:46:47 -0700437// Walk instance fields of the given Class. Separate function to allow recursion on the super
438// class.
439void ImageWriter::WalkInstanceFields(mirror::Object* obj, mirror::Class* klass) {
440 // Visit fields of parent classes first.
441 SirtRef<mirror::Class> sirt_class(Thread::Current(), klass);
442 mirror::Class* super = sirt_class->GetSuperClass();
443 if (super != nullptr) {
444 WalkInstanceFields(obj, super);
445 }
446 //
447 size_t num_reference_fields = sirt_class->NumReferenceInstanceFields();
448 for (size_t i = 0; i < num_reference_fields; ++i) {
449 mirror::ArtField* field = sirt_class->GetInstanceField(i);
450 MemberOffset field_offset = field->GetOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800451 mirror::Object* value = obj->GetFieldObject<mirror::Object>(field_offset, false);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700452 if (value != nullptr) {
453 WalkFieldsInOrder(value);
454 }
455 }
456}
457
458// For an unvisited object, visit it then all its children found via fields.
459void ImageWriter::WalkFieldsInOrder(mirror::Object* obj) {
460 if (!IsImageOffsetAssigned(obj)) {
461 // Walk instance fields of all objects
462 Thread* self = Thread::Current();
463 SirtRef<mirror::Object> sirt_obj(self, obj);
464 SirtRef<mirror::Class> klass(self, obj->GetClass());
465 // visit the object itself.
466 CalculateObjectOffsets(sirt_obj.get());
467 WalkInstanceFields(sirt_obj.get(), klass.get());
468 // Walk static fields of a Class.
469 if (sirt_obj->IsClass()) {
470 size_t num_static_fields = klass->NumReferenceStaticFields();
471 for (size_t i = 0; i < num_static_fields; ++i) {
472 mirror::ArtField* field = klass->GetStaticField(i);
473 MemberOffset field_offset = field->GetOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800474 mirror::Object* value = sirt_obj->GetFieldObject<mirror::Object>(field_offset, false);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700475 if (value != nullptr) {
476 WalkFieldsInOrder(value);
477 }
478 }
479 } else if (sirt_obj->IsObjectArray()) {
480 // Walk elements of an object array.
481 int32_t length = sirt_obj->AsObjectArray<mirror::Object>()->GetLength();
482 for (int32_t i = 0; i < length; i++) {
483 mirror::ObjectArray<mirror::Object>* obj_array = sirt_obj->AsObjectArray<mirror::Object>();
484 mirror::Object* value = obj_array->Get(i);
485 if (value != nullptr) {
486 WalkFieldsInOrder(value);
487 }
488 }
489 }
490 }
491}
492
493void ImageWriter::WalkFieldsCallback(mirror::Object* obj, void* arg) {
494 ImageWriter* writer = reinterpret_cast<ImageWriter*>(arg);
495 DCHECK(writer != nullptr);
496 writer->WalkFieldsInOrder(obj);
497}
498
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499void ImageWriter::CalculateNewObjectOffsets(size_t oat_loaded_size, size_t oat_data_offset) {
500 CHECK_NE(0U, oat_loaded_size);
501 Thread* self = Thread::Current();
502 SirtRef<ObjectArray<Object> > image_roots(self, CreateImageRoots());
503
504 gc::Heap* heap = Runtime::Current()->GetHeap();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 DCHECK_EQ(0U, image_end_);
506
Mathieu Chartier31e89252013-08-28 11:29:12 -0700507 // Leave space for the header, but do not write it yet, we need to
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 // know where image_roots is going to end up
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700509 image_end_ += RoundUp(sizeof(ImageHeader), 8); // 64-bit-alignment
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510
511 {
512 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 // TODO: Image spaces only?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 const char* old = self->StartAssertNoThreadSuspension("ImageWriter");
Mathieu Chartier590fee92013-09-13 13:46:47 -0700515 DCHECK_LT(image_end_, image_->Size());
516 // Clear any pre-existing monitors which may have been in the monitor words.
517 heap->VisitObjects(WalkFieldsCallback, this);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700518 self->EndAssertNoThreadSuspension(old);
519 }
520
521 const byte* oat_file_begin = image_begin_ + RoundUp(image_end_, kPageSize);
522 const byte* oat_file_end = oat_file_begin + oat_loaded_size;
523 oat_data_begin_ = oat_file_begin + oat_data_offset;
524 const byte* oat_data_end = oat_data_begin_ + oat_file_->Size();
525
Mathieu Chartier31e89252013-08-28 11:29:12 -0700526 // Return to write header at start of image with future location of image_roots. At this point,
527 // image_end_ is the size of the image (excluding bitmaps).
Mathieu Chartier50482232013-11-21 11:48:14 -0800528 const size_t heap_bytes_per_bitmap_byte = kBitsPerByte * gc::accounting::SpaceBitmap::kAlignment;
Mathieu Chartier12aeccd2013-11-13 15:52:06 -0800529 const size_t bitmap_bytes = RoundUp(image_end_, heap_bytes_per_bitmap_byte) /
530 heap_bytes_per_bitmap_byte;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800531 ImageHeader image_header(PointerToLowMemUInt32(image_begin_),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700532 static_cast<uint32_t>(image_end_),
533 RoundUp(image_end_, kPageSize),
Mathieu Chartier12aeccd2013-11-13 15:52:06 -0800534 RoundUp(bitmap_bytes, kPageSize),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800535 PointerToLowMemUInt32(GetImageAddress(image_roots.get())),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 oat_file_->GetOatHeader().GetChecksum(),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800537 PointerToLowMemUInt32(oat_file_begin),
538 PointerToLowMemUInt32(oat_data_begin_),
539 PointerToLowMemUInt32(oat_data_end),
540 PointerToLowMemUInt32(oat_file_end));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 memcpy(image_->Begin(), &image_header, sizeof(image_header));
542
543 // Note that image_end_ is left at end of used space
544}
545
546void ImageWriter::CopyAndFixupObjects()
547 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
548 Thread* self = Thread::Current();
549 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
550 gc::Heap* heap = Runtime::Current()->GetHeap();
551 // TODO: heap validation can't handle this fix up pass
552 heap->DisableObjectValidation();
553 // TODO: Image spaces only?
554 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700555 heap->VisitObjects(CopyAndFixupObjectsCallback, this);
556 // Fix up the object previously had hash codes.
557 for (const std::pair<mirror::Object*, uint32_t>& hash_pair : saved_hashes_) {
558 hash_pair.first->SetLockWord(LockWord::FromHashCode(hash_pair.second));
559 }
560 saved_hashes_.clear();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700561 self->EndAssertNoThreadSuspension(old_cause);
562}
563
Mathieu Chartier590fee92013-09-13 13:46:47 -0700564void ImageWriter::CopyAndFixupObjectsCallback(Object* obj, void* arg) {
565 DCHECK(obj != NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 DCHECK(arg != NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 ImageWriter* image_writer = reinterpret_cast<ImageWriter*>(arg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 // see GetLocalAddress for similar computation
569 size_t offset = image_writer->GetImageOffset(obj);
570 byte* dst = image_writer->image_->Begin() + offset;
571 const byte* src = reinterpret_cast<const byte*>(obj);
572 size_t n = obj->SizeOf();
573 DCHECK_LT(offset + n, image_writer->image_->Size());
574 memcpy(dst, src, n);
575 Object* copy = reinterpret_cast<Object*>(dst);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700576 // Write in a hash code of objects which have inflated monitors or a hash code in their monitor
577 // word.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700578 copy->SetLockWord(LockWord());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700579 image_writer->FixupObject(obj, copy);
580}
581
Ian Rogersef7d42f2014-01-06 12:55:46 -0800582void ImageWriter::FixupObject(Object* orig, Object* copy) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 DCHECK(orig != NULL);
584 DCHECK(copy != NULL);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800585 copy->SetClass<kVerifyNone>(down_cast<Class*>(GetImageAddress(orig->GetClass())));
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800586 if (kUseBrooksPointer) {
587 orig->AssertSelfBrooksPointer();
588 // Note the address 'copy' isn't the same as the image address of 'orig'.
589 copy->SetBrooksPointer(GetImageAddress(orig));
590 DCHECK(copy->GetBrooksPointer() == GetImageAddress(orig));
591 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 // TODO: special case init of pointers to malloc data (or removal of these pointers)
Mathieu Chartier4e305412014-02-19 10:54:44 -0800593 if (orig->IsClass<kVerifyNone>()) {
594 FixupClass(orig->AsClass<kVerifyNone>(), down_cast<Class*>(copy));
595 } else if (orig->IsObjectArray<kVerifyNone>()) {
596 FixupObjectArray(orig->AsObjectArray<Object, kVerifyNone>(),
597 down_cast<ObjectArray<Object>*>(copy));
598 } else if (orig->IsArtMethod<kVerifyNone>()) {
599 FixupMethod(orig->AsArtMethod<kVerifyNone>(), down_cast<ArtMethod*>(copy));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700600 } else {
601 FixupInstanceFields(orig, copy);
602 }
603}
604
Ian Rogersef7d42f2014-01-06 12:55:46 -0800605void ImageWriter::FixupClass(Class* orig, Class* copy) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 FixupInstanceFields(orig, copy);
607 FixupStaticFields(orig, copy);
608}
609
Ian Rogersef7d42f2014-01-06 12:55:46 -0800610void ImageWriter::FixupMethod(ArtMethod* orig, ArtMethod* copy) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 FixupInstanceFields(orig, copy);
612
Ian Rogers848871b2013-08-05 10:56:33 -0700613 // OatWriter replaces the code_ with an offset value. Here we re-adjust to a pointer relative to
614 // oat_begin_
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615
Ian Rogers848871b2013-08-05 10:56:33 -0700616 // The resolution method has a special trampoline to call.
617 if (UNLIKELY(orig == Runtime::Current()->GetResolutionMethod())) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800618 copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_resolution_trampoline_offset_));
619 copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_resolution_trampoline_offset_));
Jeff Hao88474b42013-10-23 16:24:40 -0700620 } else if (UNLIKELY(orig == Runtime::Current()->GetImtConflictMethod())) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800621 copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_imt_conflict_trampoline_offset_));
622 copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_imt_conflict_trampoline_offset_));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 } else {
Ian Rogers848871b2013-08-05 10:56:33 -0700624 // We assume all methods have code. If they don't currently then we set them to the use the
625 // resolution trampoline. Abstract methods never have code and so we need to make sure their
626 // use results in an AbstractMethodError. We use the interpreter to achieve this.
627 if (UNLIKELY(orig->IsAbstract())) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800628 copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_to_interpreter_bridge_offset_));
629 copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_to_interpreter_bridge_offset_));
630 copy->SetEntryPointFromInterpreter<kVerifyNone>(reinterpret_cast<EntryPointFromInterpreter*>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800631 (const_cast<byte*>(GetOatAddress(interpreter_to_interpreter_bridge_offset_))));
Ian Rogers848871b2013-08-05 10:56:33 -0700632 } else {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800633 copy->SetEntryPointFromInterpreter<kVerifyNone>(reinterpret_cast<EntryPointFromInterpreter*>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800634 (const_cast<byte*>(GetOatAddress(interpreter_to_compiled_code_bridge_offset_))));
Ian Rogers848871b2013-08-05 10:56:33 -0700635 // Use original code if it exists. Otherwise, set the code pointer to the resolution
636 // trampoline.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800637 const byte* quick_code = GetOatAddress(orig->GetQuickOatCodeOffset());
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800638 if (quick_code != nullptr &&
639 (!orig->IsStatic() || orig->IsConstructor() || orig->GetDeclaringClass()->IsInitialized())) {
640 // We have code for a non-static or initialized method, just use the code.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800641 copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(quick_code);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800642 } else if (quick_code == nullptr && orig->IsNative() && !orig->IsStatic()) {
643 // Non-static native method missing compiled code, use generic JNI version.
644 copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_generic_jni_trampoline_offset_));
645 } else if (quick_code == nullptr && !orig->IsNative()) {
646 // We don't have code at all for a non-native method, use the interpreter.
647 copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_to_interpreter_bridge_offset_));
Ian Rogers848871b2013-08-05 10:56:33 -0700648 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800649 // We have code for a static method, but need to go through the resolution stub for class initialization.
650 copy->SetEntryPointFromQuickCompiledCode<kVerifyNone>(GetOatAddress(quick_resolution_trampoline_offset_));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800651 }
652 const byte* portable_code = GetOatAddress(orig->GetPortableOatCodeOffset());
653 if (portable_code != nullptr) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800654 copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(portable_code);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800655 } else {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800656 copy->SetEntryPointFromPortableCompiledCode<kVerifyNone>(GetOatAddress(portable_resolution_trampoline_offset_));
Ian Rogers848871b2013-08-05 10:56:33 -0700657 }
658 if (orig->IsNative()) {
659 // The native method's pointer is set to a stub to lookup via dlsym.
660 // Note this is not the code_ pointer, that is handled above.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800661 copy->SetNativeMethod<kVerifyNone>(GetOatAddress(jni_dlsym_lookup_offset_));
Ian Rogers848871b2013-08-05 10:56:33 -0700662 } else {
663 // Normal (non-abstract non-native) methods have various tables to relocate.
664 uint32_t mapping_table_off = orig->GetOatMappingTableOffset();
665 const byte* mapping_table = GetOatAddress(mapping_table_off);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800666 copy->SetMappingTable<kVerifyNone>(mapping_table);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700667
Ian Rogers848871b2013-08-05 10:56:33 -0700668 uint32_t vmap_table_offset = orig->GetOatVmapTableOffset();
669 const byte* vmap_table = GetOatAddress(vmap_table_offset);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800670 copy->SetVmapTable<kVerifyNone>(vmap_table);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671
Ian Rogers848871b2013-08-05 10:56:33 -0700672 uint32_t native_gc_map_offset = orig->GetOatNativeGcMapOffset();
673 const byte* native_gc_map = GetOatAddress(native_gc_map_offset);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800674 copy->SetNativeGcMap<kVerifyNone>(reinterpret_cast<const uint8_t*>(native_gc_map));
Ian Rogers848871b2013-08-05 10:56:33 -0700675 }
676 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700677 }
678}
679
Ian Rogersef7d42f2014-01-06 12:55:46 -0800680void ImageWriter::FixupObjectArray(ObjectArray<Object>* orig, ObjectArray<Object>* copy) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 for (int32_t i = 0; i < orig->GetLength(); ++i) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800682 Object* element = orig->Get(i);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800683 copy->SetWithoutChecksAndWriteBarrier<false, true, kVerifyNone>(i, GetImageAddress(element));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684 }
685}
686
Ian Rogersef7d42f2014-01-06 12:55:46 -0800687void ImageWriter::FixupInstanceFields(Object* orig, Object* copy) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700688 DCHECK(orig != NULL);
689 DCHECK(copy != NULL);
690 Class* klass = orig->GetClass();
691 DCHECK(klass != NULL);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700692 FixupFields(orig, copy, klass->GetReferenceInstanceOffsets(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693}
694
Ian Rogersef7d42f2014-01-06 12:55:46 -0800695void ImageWriter::FixupStaticFields(Class* orig, Class* copy) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 DCHECK(orig != NULL);
697 DCHECK(copy != NULL);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700698 FixupFields(orig, copy, orig->GetReferenceStaticOffsets(), true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699}
700
Ian Rogersef7d42f2014-01-06 12:55:46 -0800701void ImageWriter::FixupFields(Object* orig,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700702 Object* copy,
703 uint32_t ref_offsets,
704 bool is_static) {
705 if (ref_offsets != CLASS_WALK_SUPER) {
706 // Found a reference offset bitmap. Fixup the specified offsets.
707 while (ref_offsets != 0) {
708 size_t right_shift = CLZ(ref_offsets);
709 MemberOffset byte_offset = CLASS_OFFSET_FROM_CLZ(right_shift);
Mathieu Chartier4e305412014-02-19 10:54:44 -0800710 Object* ref = orig->GetFieldObject<Object, kVerifyNone>(byte_offset, false);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800711 // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the
712 // image.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800713 copy->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(
714 byte_offset, GetImageAddress(ref), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 ref_offsets &= ~(CLASS_HIGH_BIT >> right_shift);
716 }
717 } else {
718 // There is no reference offset bitmap. In the non-static case,
719 // walk up the class inheritance hierarchy and find reference
720 // offsets the hard way. In the static case, just consider this
721 // class.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800722 for (Class *klass = is_static ? orig->AsClass() : orig->GetClass();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700723 klass != NULL;
724 klass = is_static ? NULL : klass->GetSuperClass()) {
725 size_t num_reference_fields = (is_static
726 ? klass->NumReferenceStaticFields()
727 : klass->NumReferenceInstanceFields());
728 for (size_t i = 0; i < num_reference_fields; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700729 ArtField* field = (is_static
730 ? klass->GetStaticField(i)
731 : klass->GetInstanceField(i));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 MemberOffset field_offset = field->GetOffset();
Mathieu Chartier4e305412014-02-19 10:54:44 -0800733 Object* ref = orig->GetFieldObject<Object, kVerifyNone>(field_offset, false);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800734 // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the
735 // image.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800736 copy->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(
737 field_offset, GetImageAddress(ref), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700738 }
739 }
740 }
741 if (!is_static && orig->IsReferenceInstance()) {
742 // Fix-up referent, that isn't marked as an object field, for References.
Brian Carlstromea46f952013-07-30 01:26:50 -0700743 ArtField* field = orig->GetClass()->FindInstanceField("referent", "Ljava/lang/Object;");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700744 MemberOffset field_offset = field->GetOffset();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800745 Object* ref = orig->GetFieldObject<Object>(field_offset, false);
746 // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the
747 // image.
Mathieu Chartier4e305412014-02-19 10:54:44 -0800748 copy->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(
749 field_offset, GetImageAddress(ref), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 }
751}
752
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800753static ArtMethod* GetTargetMethod(const CompilerDriver::CallPatchInformation* patch)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
755 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700756 Thread* self = Thread::Current();
757 SirtRef<mirror::DexCache> dex_cache(self, class_linker->FindDexCache(patch->GetDexFile()));
758 SirtRef<mirror::ClassLoader> class_loader(self, nullptr);
Brian Carlstromea46f952013-07-30 01:26:50 -0700759 ArtMethod* method = class_linker->ResolveMethod(patch->GetDexFile(),
760 patch->GetTargetMethodIdx(),
761 dex_cache,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700762 class_loader,
Brian Carlstromea46f952013-07-30 01:26:50 -0700763 NULL,
764 patch->GetTargetInvokeType());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 CHECK(method != NULL)
766 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
767 CHECK(!method->IsRuntimeMethod())
768 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetMethodIdx();
769 CHECK(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx()) == method)
770 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
771 << PrettyMethod(dex_cache->GetResolvedMethods()->Get(patch->GetTargetMethodIdx())) << " "
772 << PrettyMethod(method);
773 return method;
774}
775
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800776static Class* GetTargetType(const CompilerDriver::TypePatchInformation* patch)
777 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
778 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
779 Thread* self = Thread::Current();
780 SirtRef<mirror::DexCache> dex_cache(self, class_linker->FindDexCache(patch->GetDexFile()));
781 SirtRef<mirror::ClassLoader> class_loader(self, nullptr);
782 Class* klass = class_linker->ResolveType(patch->GetDexFile(),
783 patch->GetTargetTypeIdx(),
784 dex_cache,
785 class_loader);
786 CHECK(klass != NULL)
787 << patch->GetDexFile().GetLocation() << " " << patch->GetTargetTypeIdx();
788 CHECK(dex_cache->GetResolvedTypes()->Get(patch->GetTargetTypeIdx()) == klass)
789 << patch->GetDexFile().GetLocation() << " " << patch->GetReferrerMethodIdx() << " "
790 << PrettyClass(dex_cache->GetResolvedTypes()->Get(patch->GetTargetTypeIdx())) << " "
791 << PrettyClass(klass);
792 return klass;
793}
794
Brian Carlstrom7940e442013-07-12 13:46:57 -0700795void ImageWriter::PatchOatCodeAndMethods() {
796 Thread* self = Thread::Current();
797 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
798 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
799
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800800 typedef std::vector<const CompilerDriver::CallPatchInformation*> CallPatches;
801 const CallPatches& code_to_patch = compiler_driver_.GetCodeToPatch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 for (size_t i = 0; i < code_to_patch.size(); i++) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800803 const CompilerDriver::CallPatchInformation* patch = code_to_patch[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700804 ArtMethod* target = GetTargetMethod(patch);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800805 uintptr_t quick_code = reinterpret_cast<uintptr_t>(class_linker->GetQuickOatCodeFor(target));
806 uintptr_t code_base = reinterpret_cast<uintptr_t>(&oat_file_->GetOatHeader());
807 uintptr_t code_offset = quick_code - code_base;
Mark Mendell55d0eac2014-02-06 11:02:52 -0800808 if (patch->IsRelative()) {
809 // value to patch is relative to the location being patched
810 const void* quick_oat_code =
811 class_linker->GetQuickOatCodeFor(patch->GetDexFile(),
812 patch->GetReferrerClassDefIdx(),
813 patch->GetReferrerMethodIdx());
814 uintptr_t base = reinterpret_cast<uintptr_t>(quick_oat_code);
815 uintptr_t patch_location = base + patch->GetLiteralOffset();
816 uintptr_t value = quick_code - patch_location + patch->RelativeOffset();
817 SetPatchLocation(patch, value);
818 } else {
Brian Carlstrom5e754d82014-03-05 10:59:04 -0800819 if (quick_code == reinterpret_cast<uintptr_t>(GetQuickToInterpreterBridge())) {
820 if (target->IsNative()) {
821 // generic JNI, not interpreter bridge from GetQuickOatCodeFor().
822 code_offset = quick_generic_jni_trampoline_offset_;
823 } else {
824 code_offset = quick_to_interpreter_bridge_offset_;
825 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800826 }
Mark Mendell55d0eac2014-02-06 11:02:52 -0800827 SetPatchLocation(patch, PointerToLowMemUInt32(GetOatAddress(code_offset)));
828 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 }
830
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800831 const CallPatches& methods_to_patch = compiler_driver_.GetMethodsToPatch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 for (size_t i = 0; i < methods_to_patch.size(); i++) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800833 const CompilerDriver::CallPatchInformation* patch = methods_to_patch[i];
Brian Carlstromea46f952013-07-30 01:26:50 -0700834 ArtMethod* target = GetTargetMethod(patch);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800835 SetPatchLocation(patch, PointerToLowMemUInt32(GetImageAddress(target)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836 }
837
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800838 const std::vector<const CompilerDriver::TypePatchInformation*>& classes_to_patch =
839 compiler_driver_.GetClassesToPatch();
840 for (size_t i = 0; i < classes_to_patch.size(); i++) {
841 const CompilerDriver::TypePatchInformation* patch = classes_to_patch[i];
842 Class* target = GetTargetType(patch);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800843 SetPatchLocation(patch, PointerToLowMemUInt32(GetImageAddress(target)));
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800844 }
845
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846 // Update the image header with the new checksum after patching
847 ImageHeader* image_header = reinterpret_cast<ImageHeader*>(image_->Begin());
848 image_header->SetOatChecksum(oat_file_->GetOatHeader().GetChecksum());
849 self->EndAssertNoThreadSuspension(old_cause);
850}
851
852void ImageWriter::SetPatchLocation(const CompilerDriver::PatchInformation* patch, uint32_t value) {
853 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800854 const void* quick_oat_code = class_linker->GetQuickOatCodeFor(patch->GetDexFile(),
855 patch->GetReferrerClassDefIdx(),
856 patch->GetReferrerMethodIdx());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700857 OatHeader& oat_header = const_cast<OatHeader&>(oat_file_->GetOatHeader());
858 // TODO: make this Thumb2 specific
Ian Rogersef7d42f2014-01-06 12:55:46 -0800859 uint8_t* base = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(quick_oat_code) & ~0x1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700860 uint32_t* patch_location = reinterpret_cast<uint32_t*>(base + patch->GetLiteralOffset());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700861 if (kIsDebugBuild) {
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800862 if (patch->IsCall()) {
863 const CompilerDriver::CallPatchInformation* cpatch = patch->AsCall();
864 const DexFile::MethodId& id = cpatch->GetDexFile().GetMethodId(cpatch->GetTargetMethodIdx());
Andreas Gampe2da88232014-02-27 12:26:20 -0800865 uint32_t expected = reinterpret_cast<uintptr_t>(&id) & 0xFFFFFFFF;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800866 uint32_t actual = *patch_location;
867 CHECK(actual == expected || actual == value) << std::hex
868 << "actual=" << actual
869 << "expected=" << expected
870 << "value=" << value;
871 }
872 if (patch->IsType()) {
873 const CompilerDriver::TypePatchInformation* tpatch = patch->AsType();
874 const DexFile::TypeId& id = tpatch->GetDexFile().GetTypeId(tpatch->GetTargetTypeIdx());
Andreas Gampe2da88232014-02-27 12:26:20 -0800875 uint32_t expected = reinterpret_cast<uintptr_t>(&id) & 0xFFFFFFFF;
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800876 uint32_t actual = *patch_location;
877 CHECK(actual == expected || actual == value) << std::hex
878 << "actual=" << actual
879 << "expected=" << expected
880 << "value=" << value;
881 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700882 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700883 *patch_location = value;
884 oat_header.UpdateChecksum(patch_location, sizeof(value));
885}
886
887} // namespace art