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" |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 10 | #include "signal_catcher.h" |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 11 | #include "space.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 12 | #include "utils.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 13 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 14 | namespace art { |
| 15 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 16 | class ImageTest : public CommonTest {}; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 17 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 18 | TEST_F(ImageTest, WriteRead) { |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 19 | // TODO: remove the touching of classes, call Compiler instead |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 20 | for (size_t i = 0; i < java_lang_dex_file_->NumClassDefs(); i++) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 21 | const DexFile::ClassDef& class_def = java_lang_dex_file_->GetClassDef(i); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 22 | const char* descriptor = java_lang_dex_file_->GetClassDescriptor(class_def); |
| 23 | Class* klass = class_linker_->FindSystemClass(descriptor); |
| 24 | ASSERT_TRUE(klass != NULL) << descriptor; |
| 25 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 26 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 27 | ImageWriter writer; |
| 28 | ScratchFile tmp; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 29 | const uintptr_t image_base = 0x50000000; |
| 30 | bool success = writer.Write(tmp.GetFilename(), image_base); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 31 | ASSERT_TRUE(success); |
| 32 | |
| 33 | { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 34 | UniquePtr<File> file(OS::OpenFile(tmp.GetFilename(), false)); |
| 35 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 36 | ImageHeader image_header; |
| 37 | file->ReadFully(&image_header, sizeof(image_header)); |
| 38 | ASSERT_TRUE(image_header.IsValid()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 39 | |
| 40 | ASSERT_EQ(1U, Heap::GetSpaces().size()); |
| 41 | Space* space = Heap::GetSpaces()[0]; |
| 42 | ASSERT_TRUE(space != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 43 | ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length())); |
| 44 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 45 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 46 | // 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] | 47 | delete runtime_.release(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 48 | |
| 49 | // don't reuse java_lang_dex_file_ so we make sure we don't get |
| 50 | // lucky by pointers that happen to work referencing the earlier |
| 51 | // dex. |
| 52 | delete java_lang_dex_file_.release(); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 53 | UniquePtr<const DexFile> dex(GetLibCoreDex()); |
| 54 | ASSERT_TRUE(dex.get() != NULL); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 55 | |
| 56 | std::vector<const DexFile*> boot_class_path; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 57 | boot_class_path.push_back(dex.get()); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 58 | |
| 59 | Runtime::Options options; |
| 60 | options.push_back(std::make_pair("bootclasspath", &boot_class_path)); |
| 61 | std::string boot_image("-Xbootimage:"); |
| 62 | boot_image.append(tmp.GetFilename()); |
| 63 | options.push_back(std::make_pair(boot_image.c_str(), reinterpret_cast<void*>(NULL))); |
| 64 | |
| 65 | runtime_.reset(Runtime::Create(options, false)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 66 | ASSERT_TRUE(runtime_.get() != NULL); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 67 | class_linker_ = runtime_->GetClassLinker(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 68 | |
| 69 | ASSERT_EQ(2U, Heap::GetSpaces().size()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 70 | Space* boot_space = Heap::GetBootSpace(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 71 | ASSERT_TRUE(boot_space != NULL); |
| 72 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 73 | // enable to display maps to debug boot_base and boot_limit checking problems below |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 74 | // TODO: why does this dump show two attached threads? |
| 75 | if (true) { |
| 76 | SignalCatcher::HandleSigQuit(); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | byte* boot_base = boot_space->GetBase(); |
| 80 | byte* boot_limit = boot_space->GetLimit(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 81 | for (size_t i = 0; i < dex->NumClassDefs(); i++) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 82 | const DexFile::ClassDef& class_def = dex->GetClassDef(i); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 83 | const char* descriptor = dex->GetClassDescriptor(class_def); |
| 84 | Class* klass = class_linker_->FindSystemClass(descriptor); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 85 | EXPECT_TRUE(klass != NULL) << descriptor; |
| 86 | EXPECT_LT(boot_base, reinterpret_cast<byte*>(klass)) << descriptor; |
| 87 | EXPECT_LT(reinterpret_cast<byte*>(klass), boot_limit) << descriptor; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 88 | EXPECT_TRUE(klass->GetMonitor() == NULL); // address should have been removed from monitor |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 89 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | } // namespace art |