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 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 24 | ImageWriter writer; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 25 | ScratchFile tmp_image; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 26 | const uintptr_t requested_image_base = 0x60000000; |
| 27 | bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 28 | std::string(tmp_oat.GetFilename()), ""); |
| 29 | ASSERT_TRUE(success_image); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 30 | |
| 31 | { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 32 | UniquePtr<File> file(OS::OpenFile(tmp_image.GetFilename(), false)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 33 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 34 | ImageHeader image_header; |
| 35 | file->ReadFully(&image_header, sizeof(image_header)); |
| 36 | ASSERT_TRUE(image_header.IsValid()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 37 | |
| 38 | ASSERT_EQ(1U, Heap::GetSpaces().size()); |
| 39 | Space* space = Heap::GetSpaces()[0]; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 40 | ASSERT_FALSE(space->IsImageSpace()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 41 | ASSERT_TRUE(space != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 42 | ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length())); |
| 43 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 44 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 45 | // 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] | 46 | delete runtime_.release(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 47 | |
| 48 | // don't reuse java_lang_dex_file_ so we make sure we don't get |
| 49 | // lucky by pointers that happen to work referencing the earlier |
| 50 | // dex. |
| 51 | delete java_lang_dex_file_.release(); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 52 | UniquePtr<const DexFile> dex(GetLibCoreDex()); |
| 53 | ASSERT_TRUE(dex.get() != NULL); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 54 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 55 | Runtime::Options options; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 56 | std::string image("-Ximage:"); |
| 57 | image.append(tmp_image.GetFilename()); |
| 58 | 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] | 59 | |
| 60 | runtime_.reset(Runtime::Create(options, false)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 61 | ASSERT_TRUE(runtime_.get() != NULL); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 62 | class_linker_ = runtime_->GetClassLinker(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 63 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 64 | ASSERT_TRUE(runtime_->GetJniStubArray() != NULL); |
| 65 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 66 | ASSERT_EQ(2U, Heap::GetSpaces().size()); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 67 | ASSERT_TRUE(Heap::GetSpaces()[0]->IsImageSpace()); |
| 68 | ASSERT_FALSE(Heap::GetSpaces()[1]->IsImageSpace()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 69 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 70 | Space* image_space = Heap::GetSpaces()[0]; |
| 71 | byte* image_base = image_space->GetBase(); |
| 72 | byte* image_limit = image_space->GetLimit(); |
| 73 | CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_base)); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 74 | for (size_t i = 0; i < dex->NumClassDefs(); i++) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 75 | const DexFile::ClassDef& class_def = dex->GetClassDef(i); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 76 | const char* descriptor = dex->GetClassDescriptor(class_def); |
| 77 | Class* klass = class_linker_->FindSystemClass(descriptor); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 78 | EXPECT_TRUE(klass != NULL) << descriptor; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 79 | EXPECT_LT(image_base, reinterpret_cast<byte*>(klass)) << descriptor; |
| 80 | EXPECT_LT(reinterpret_cast<byte*>(klass), image_limit) << descriptor; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 81 | EXPECT_EQ(*klass->GetRawLockWordAddress(), 0); // address should have been removed from monitor |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 82 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace art |