blob: b03cbe35847bdbcd8ee13f30b2a7b142dc186b70 [file] [log] [blame]
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "common_test.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07004#include "file.h"
5#include "image.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07006#include "image_writer.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07007#include "space.h"
buzbeec143c552011-08-20 17:38:58 -07008#include "utils.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07009
Brian Carlstromdb4d5402011-08-09 12:18:28 -070010namespace art {
11
Brian Carlstromf734cf52011-08-17 16:28:14 -070012class ImageTest : public CommonTest {};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070013
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070014TEST_F(ImageTest, WriteRead) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070015
16 // TODO: move the touching of classes and GC to the ImageWriter proper
17 for (size_t i = 0; i < java_lang_dex_file_->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070018 const DexFile::ClassDef& class_def = java_lang_dex_file_->GetClassDef(i);
Brian Carlstroma663ea52011-08-19 23:33:41 -070019 const char* descriptor = java_lang_dex_file_->GetClassDescriptor(class_def);
20 Class* klass = class_linker_->FindSystemClass(descriptor);
21 ASSERT_TRUE(klass != NULL) << descriptor;
22 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070023 // TODO: Heap::CollectGarbage before writing
Brian Carlstroma663ea52011-08-19 23:33:41 -070024
Brian Carlstromdb4d5402011-08-09 12:18:28 -070025 const std::vector<Space*>& spaces = Heap::GetSpaces();
26 // can't currently deal with writing a space that might have pointers between spaces
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070027 ASSERT_EQ(1U, spaces.size());
28 Space* space = spaces[0];
Brian Carlstromdb4d5402011-08-09 12:18:28 -070029
30 ImageWriter writer;
31 ScratchFile tmp;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070032 const int image_base = 0x50000000;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070033 bool success = writer.Write(space, tmp.GetFilename(), reinterpret_cast<byte*>(image_base));
34 ASSERT_TRUE(success);
35
36 {
Elliott Hughes90a33692011-08-30 13:27:07 -070037 UniquePtr<File> file(OS::OpenFile(tmp.GetFilename(), false));
38 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070039 ImageHeader image_header;
40 file->ReadFully(&image_header, sizeof(image_header));
41 ASSERT_TRUE(image_header.IsValid());
42 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length()));
43 }
Brian Carlstrom8a436592011-08-15 21:27:23 -070044
Brian Carlstroma663ea52011-08-19 23:33:41 -070045 // tear down old runtime before making a new one, clearing out misc state
Brian Carlstrom8a436592011-08-15 21:27:23 -070046 delete runtime_.release();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070047
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 Hughes90a33692011-08-30 13:27:07 -070052 UniquePtr<const DexFile> dex(GetLibCoreDex());
53 ASSERT_TRUE(dex.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070054
55 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070056 boot_class_path.push_back(dex.get());
Brian Carlstrom8a436592011-08-15 21:27:23 -070057
58 Runtime::Options options;
59 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
60 std::string boot_image("-Xbootimage:");
61 boot_image.append(tmp.GetFilename());
62 options.push_back(std::make_pair(boot_image.c_str(), reinterpret_cast<void*>(NULL)));
63
64 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -070065 ASSERT_TRUE(runtime_.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070066 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070067
68 ASSERT_EQ(2U, Heap::GetSpaces().size());
69 Space* boot_space = Heap::GetSpaces()[0];
70 ASSERT_TRUE(boot_space != NULL);
71
Brian Carlstroma663ea52011-08-19 23:33:41 -070072 // enable to display maps to debug boot_base and boot_limit checking problems below
73 if (false) {
74 const char* maps_file = "/proc/self/maps";
75 std::string contents = ReadFileToString(maps_file);
76 LG << maps_file << ":\n" << contents;
77 }
78
79 byte* boot_base = boot_space->GetBase();
80 byte* boot_limit = boot_space->GetLimit();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070081 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070082 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070083 const char* descriptor = dex->GetClassDescriptor(class_def);
84 Class* klass = class_linker_->FindSystemClass(descriptor);
Brian Carlstroma663ea52011-08-19 23:33:41 -070085 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 Carlstrom9cff8e12011-08-18 16:47:29 -070088 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -070089}
90
91} // namespace art