Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3 | #include <string> |
| 4 | #include <vector> |
| 5 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 6 | #include "common_test.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 7 | #include "file.h" |
| 8 | #include "image.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 9 | #include "image_writer.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 10 | #include "oat_writer.h" |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 11 | #include "signal_catcher.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 12 | #include "space.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 13 | #include "utils.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 14 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 15 | namespace art { |
| 16 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 17 | class ImageTest : public CommonTest {}; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 18 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 19 | TEST_F(ImageTest, WriteRead) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 20 | ScratchFile tmp_oat; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 21 | bool success_oat = OatWriter::Create(tmp_oat.GetFile(), NULL, *compiler_.get()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 22 | ASSERT_TRUE(success_oat); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 23 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 24 | // Force all system classes into memory |
| 25 | for (size_t i = 0; i < java_lang_dex_file_->NumClassDefs(); i++) { |
| 26 | const DexFile::ClassDef& class_def = java_lang_dex_file_->GetClassDef(i); |
| 27 | const char* descriptor = java_lang_dex_file_->GetClassDescriptor(class_def); |
| 28 | Class* klass = class_linker_->FindSystemClass(descriptor); |
| 29 | EXPECT_TRUE(klass != NULL) << descriptor; |
| 30 | } |
| 31 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 32 | ImageWriter writer; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 33 | ScratchFile tmp_image; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 34 | const uintptr_t requested_image_base = 0x60000000; |
| 35 | bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 36 | std::string(tmp_oat.GetFilename()), ""); |
| 37 | ASSERT_TRUE(success_image); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 38 | |
| 39 | { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 40 | UniquePtr<File> file(OS::OpenFile(tmp_image.GetFilename(), false)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 41 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 42 | ImageHeader image_header; |
| 43 | file->ReadFully(&image_header, sizeof(image_header)); |
| 44 | ASSERT_TRUE(image_header.IsValid()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 45 | |
| 46 | ASSERT_EQ(1U, Heap::GetSpaces().size()); |
| 47 | Space* space = Heap::GetSpaces()[0]; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 48 | ASSERT_FALSE(space->IsImageSpace()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 49 | ASSERT_TRUE(space != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 50 | ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length())); |
| 51 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 52 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 53 | // tear down old runtime before making a new one, clearing out misc state |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 54 | delete runtime_.release(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 55 | |
| 56 | // don't reuse java_lang_dex_file_ so we make sure we don't get |
| 57 | // lucky by pointers that happen to work referencing the earlier |
| 58 | // dex. |
| 59 | delete java_lang_dex_file_.release(); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 60 | UniquePtr<const DexFile> dex(GetLibCoreDex()); |
| 61 | ASSERT_TRUE(dex.get() != NULL); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 62 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 63 | Runtime::Options options; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 64 | std::string image("-Ximage:"); |
| 65 | image.append(tmp_image.GetFilename()); |
| 66 | options.push_back(std::make_pair(image.c_str(), reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 67 | |
| 68 | runtime_.reset(Runtime::Create(options, false)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 69 | ASSERT_TRUE(runtime_.get() != NULL); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 70 | class_linker_ = runtime_->GetClassLinker(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 71 | |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame^] | 72 | ASSERT_TRUE(runtime_->GetJniDlsymLookupStub() != NULL); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 73 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 74 | ASSERT_EQ(2U, Heap::GetSpaces().size()); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 75 | ASSERT_TRUE(Heap::GetSpaces()[0]->IsImageSpace()); |
| 76 | ASSERT_FALSE(Heap::GetSpaces()[1]->IsImageSpace()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 77 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 78 | Space* image_space = Heap::GetSpaces()[0]; |
| 79 | byte* image_base = image_space->GetBase(); |
| 80 | byte* image_limit = image_space->GetLimit(); |
| 81 | CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_base)); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 82 | for (size_t i = 0; i < dex->NumClassDefs(); i++) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 83 | const DexFile::ClassDef& class_def = dex->GetClassDef(i); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 84 | const char* descriptor = dex->GetClassDescriptor(class_def); |
| 85 | Class* klass = class_linker_->FindSystemClass(descriptor); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 86 | EXPECT_TRUE(klass != NULL) << descriptor; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 87 | EXPECT_LT(image_base, reinterpret_cast<byte*>(klass)) << descriptor; |
| 88 | EXPECT_LT(reinterpret_cast<byte*>(klass), image_limit) << descriptor; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 89 | EXPECT_EQ(*klass->GetRawLockWordAddress(), 0); // address should have been removed from monitor |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 90 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | } // namespace art |