blob: c68b6003893d7f55d9a6a499b6e66bea6bf0b735 [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 "os.h"
8#include "space.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07009
10#include "gtest/gtest.h"
11
12namespace art {
13
Brian Carlstromf734cf52011-08-17 16:28:14 -070014class ImageTest : public CommonTest {};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070015
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070016std::string ReadFileToString(const char* file_name) {
17 scoped_ptr<File> file(OS::OpenFile(file_name, false));
18 CHECK(file != NULL);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070019
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070020 std::string contents;
21 char buf[8 * KB];
22 while (true) {
23 int64_t n = file->Read(buf, sizeof(buf));
24 CHECK_NE(-1, n);
25 if (n == 0) {
26 break;
27 }
28 contents.append(buf, n);
29 }
30 return contents;
31}
32
33TEST_F(ImageTest, WriteRead) {
34 // TODO: Heap::CollectGarbage before writing
Brian Carlstromdb4d5402011-08-09 12:18:28 -070035 const std::vector<Space*>& spaces = Heap::GetSpaces();
36 // can't currently deal with writing a space that might have pointers between spaces
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070037 ASSERT_EQ(1U, spaces.size());
38 Space* space = spaces[0];
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039
40 ImageWriter writer;
41 ScratchFile tmp;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070042 const int image_base = 0x50000000;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070043 bool success = writer.Write(space, tmp.GetFilename(), reinterpret_cast<byte*>(image_base));
44 ASSERT_TRUE(success);
45
46 {
47 scoped_ptr<File> file(OS::OpenFile(tmp.GetFilename(), false));
48 ASSERT_TRUE(file != NULL);
49 ImageHeader image_header;
50 file->ReadFully(&image_header, sizeof(image_header));
51 ASSERT_TRUE(image_header.IsValid());
52 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length()));
53 }
Brian Carlstrom8a436592011-08-15 21:27:23 -070054
55 // tear down old runtime and make a new one
56 delete runtime_.release();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070057
58 // don't reuse java_lang_dex_file_ so we make sure we don't get
59 // lucky by pointers that happen to work referencing the earlier
60 // dex.
61 delete java_lang_dex_file_.release();
62 scoped_ptr<DexFile> dex(GetLibCoreDex());
63 ASSERT_TRUE(dex != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070064
65 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070066 boot_class_path.push_back(dex.get());
Brian Carlstrom8a436592011-08-15 21:27:23 -070067
68 Runtime::Options options;
69 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
70 std::string boot_image("-Xbootimage:");
71 boot_image.append(tmp.GetFilename());
72 options.push_back(std::make_pair(boot_image.c_str(), reinterpret_cast<void*>(NULL)));
73
74 runtime_.reset(Runtime::Create(options, false));
75 ASSERT_TRUE(runtime_ != NULL);
76 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070077
78 if (true) {
79 const char* maps_file = "/proc/self/maps";
80 std::string contents = ReadFileToString(maps_file);
81 LG << maps_file << ":\n" << contents;
82 }
83
84 ASSERT_EQ(2U, Heap::GetSpaces().size());
85 Space* boot_space = Heap::GetSpaces()[0];
86 ASSERT_TRUE(boot_space != NULL);
87
88 // TODO: need to rebuild ClassLinker::classes_ and ::intern_table_
89 // byte* boot_base = boot_space->GetBase();
90 // byte* boot_limit = boot_space->GetLimit();
91 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
92 const DexFile::ClassDef class_def = dex->GetClassDef(i);
93 const char* descriptor = dex->GetClassDescriptor(class_def);
94 Class* klass = class_linker_->FindSystemClass(descriptor);
95 EXPECT_TRUE(klass != NULL);
96 // EXPECT_LT(boot_base, reinterpret_cast<byte*>(klass));
97 // EXPECT_LT(reinterpret_cast<byte*>(klass), boot_limit);
98 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -070099}
100
101} // namespace art