blob: 8c00e895ada6dec47eec0efa1beca65fd9b76821 [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) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070034
35 // TODO: move the touching of classes and GC to the ImageWriter proper
36 for (size_t i = 0; i < java_lang_dex_file_->NumClassDefs(); i++) {
37 const DexFile::ClassDef class_def = java_lang_dex_file_->GetClassDef(i);
38 const char* descriptor = java_lang_dex_file_->GetClassDescriptor(class_def);
39 Class* klass = class_linker_->FindSystemClass(descriptor);
40 ASSERT_TRUE(klass != NULL) << descriptor;
41 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070042 // TODO: Heap::CollectGarbage before writing
Brian Carlstroma663ea52011-08-19 23:33:41 -070043
Brian Carlstromdb4d5402011-08-09 12:18:28 -070044 const std::vector<Space*>& spaces = Heap::GetSpaces();
45 // can't currently deal with writing a space that might have pointers between spaces
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070046 ASSERT_EQ(1U, spaces.size());
47 Space* space = spaces[0];
Brian Carlstromdb4d5402011-08-09 12:18:28 -070048
49 ImageWriter writer;
50 ScratchFile tmp;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070051 const int image_base = 0x50000000;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070052 bool success = writer.Write(space, tmp.GetFilename(), reinterpret_cast<byte*>(image_base));
53 ASSERT_TRUE(success);
54
55 {
56 scoped_ptr<File> file(OS::OpenFile(tmp.GetFilename(), false));
57 ASSERT_TRUE(file != NULL);
58 ImageHeader image_header;
59 file->ReadFully(&image_header, sizeof(image_header));
60 ASSERT_TRUE(image_header.IsValid());
61 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length()));
62 }
Brian Carlstrom8a436592011-08-15 21:27:23 -070063
Brian Carlstroma663ea52011-08-19 23:33:41 -070064 // tear down old runtime before making a new one, clearing out misc state
Brian Carlstrom8a436592011-08-15 21:27:23 -070065 delete runtime_.release();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070066
67 // don't reuse java_lang_dex_file_ so we make sure we don't get
68 // lucky by pointers that happen to work referencing the earlier
69 // dex.
70 delete java_lang_dex_file_.release();
71 scoped_ptr<DexFile> dex(GetLibCoreDex());
72 ASSERT_TRUE(dex != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070073
74 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070075 boot_class_path.push_back(dex.get());
Brian Carlstrom8a436592011-08-15 21:27:23 -070076
77 Runtime::Options options;
78 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
79 std::string boot_image("-Xbootimage:");
80 boot_image.append(tmp.GetFilename());
81 options.push_back(std::make_pair(boot_image.c_str(), reinterpret_cast<void*>(NULL)));
82
83 runtime_.reset(Runtime::Create(options, false));
84 ASSERT_TRUE(runtime_ != NULL);
85 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070086
87 ASSERT_EQ(2U, Heap::GetSpaces().size());
88 Space* boot_space = Heap::GetSpaces()[0];
89 ASSERT_TRUE(boot_space != NULL);
90
Brian Carlstroma663ea52011-08-19 23:33:41 -070091 // enable to display maps to debug boot_base and boot_limit checking problems below
92 if (false) {
93 const char* maps_file = "/proc/self/maps";
94 std::string contents = ReadFileToString(maps_file);
95 LG << maps_file << ":\n" << contents;
96 }
97
98 byte* boot_base = boot_space->GetBase();
99 byte* boot_limit = boot_space->GetLimit();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700100 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
101 const DexFile::ClassDef class_def = dex->GetClassDef(i);
102 const char* descriptor = dex->GetClassDescriptor(class_def);
103 Class* klass = class_linker_->FindSystemClass(descriptor);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700104 EXPECT_TRUE(klass != NULL) << descriptor;
105 EXPECT_LT(boot_base, reinterpret_cast<byte*>(klass)) << descriptor;
106 EXPECT_LT(reinterpret_cast<byte*>(klass), boot_limit) << descriptor;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700107 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700108}
109
110} // namespace art