blob: 34e243d78dac59cefaef3e1e38f2f61bd78a8f94 [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
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 -070016TEST_F(ImageTest, WriteRead) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070017
18 // TODO: move the touching of classes and GC to the ImageWriter proper
19 for (size_t i = 0; i < java_lang_dex_file_->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070020 const DexFile::ClassDef& class_def = java_lang_dex_file_->GetClassDef(i);
Brian Carlstroma663ea52011-08-19 23:33:41 -070021 const char* descriptor = java_lang_dex_file_->GetClassDescriptor(class_def);
22 Class* klass = class_linker_->FindSystemClass(descriptor);
23 ASSERT_TRUE(klass != NULL) << descriptor;
24 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070025 // TODO: Heap::CollectGarbage before writing
Brian Carlstroma663ea52011-08-19 23:33:41 -070026
Brian Carlstromdb4d5402011-08-09 12:18:28 -070027 const std::vector<Space*>& spaces = Heap::GetSpaces();
28 // can't currently deal with writing a space that might have pointers between spaces
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070029 ASSERT_EQ(1U, spaces.size());
30 Space* space = spaces[0];
Brian Carlstromdb4d5402011-08-09 12:18:28 -070031
32 ImageWriter writer;
33 ScratchFile tmp;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070034 const int image_base = 0x50000000;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070035 bool success = writer.Write(space, tmp.GetFilename(), reinterpret_cast<byte*>(image_base));
36 ASSERT_TRUE(success);
37
38 {
Elliott Hughes90a33692011-08-30 13:27:07 -070039 UniquePtr<File> file(OS::OpenFile(tmp.GetFilename(), false));
40 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070041 ImageHeader image_header;
42 file->ReadFully(&image_header, sizeof(image_header));
43 ASSERT_TRUE(image_header.IsValid());
44 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length()));
45 }
Brian Carlstrom8a436592011-08-15 21:27:23 -070046
Brian Carlstroma663ea52011-08-19 23:33:41 -070047 // tear down old runtime before making a new one, clearing out misc state
Brian Carlstrom8a436592011-08-15 21:27:23 -070048 delete runtime_.release();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070049
50 // don't reuse java_lang_dex_file_ so we make sure we don't get
51 // lucky by pointers that happen to work referencing the earlier
52 // dex.
53 delete java_lang_dex_file_.release();
Elliott Hughes90a33692011-08-30 13:27:07 -070054 UniquePtr<const DexFile> dex(GetLibCoreDex());
55 ASSERT_TRUE(dex.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070056
57 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070058 boot_class_path.push_back(dex.get());
Brian Carlstrom8a436592011-08-15 21:27:23 -070059
60 Runtime::Options options;
61 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
62 std::string boot_image("-Xbootimage:");
63 boot_image.append(tmp.GetFilename());
64 options.push_back(std::make_pair(boot_image.c_str(), reinterpret_cast<void*>(NULL)));
65
66 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -070067 ASSERT_TRUE(runtime_.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070068 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070069
70 ASSERT_EQ(2U, Heap::GetSpaces().size());
71 Space* boot_space = Heap::GetSpaces()[0];
72 ASSERT_TRUE(boot_space != NULL);
73
Brian Carlstroma663ea52011-08-19 23:33:41 -070074 // enable to display maps to debug boot_base and boot_limit checking problems below
75 if (false) {
76 const char* maps_file = "/proc/self/maps";
77 std::string contents = ReadFileToString(maps_file);
78 LG << maps_file << ":\n" << contents;
79 }
80
81 byte* boot_base = boot_space->GetBase();
82 byte* boot_limit = boot_space->GetLimit();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070083 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070084 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070085 const char* descriptor = dex->GetClassDescriptor(class_def);
86 Class* klass = class_linker_->FindSystemClass(descriptor);
Brian Carlstroma663ea52011-08-19 23:33:41 -070087 EXPECT_TRUE(klass != NULL) << descriptor;
88 EXPECT_LT(boot_base, reinterpret_cast<byte*>(klass)) << descriptor;
89 EXPECT_LT(reinterpret_cast<byte*>(klass), boot_limit) << descriptor;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070090 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -070091}
92
93} // namespace art