blob: db090213224aff8bf2221960d0aefe2463a78ec4 [file] [log] [blame]
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07003#include <string>
4#include <vector>
5
Brian Carlstromdb4d5402011-08-09 12:18:28 -07006#include "common_test.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07007#include "file.h"
8#include "image.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07009#include "image_writer.h"
Elliott Hughes42ee1422011-09-06 12:33:32 -070010#include "signal_catcher.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070011#include "space.h"
buzbeec143c552011-08-20 17:38:58 -070012#include "utils.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070013
Brian Carlstromdb4d5402011-08-09 12:18:28 -070014namespace art {
15
Brian Carlstromf734cf52011-08-17 16:28:14 -070016class ImageTest : public CommonTest {};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070017
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070018TEST_F(ImageTest, WriteRead) {
Brian Carlstroma663ea52011-08-19 23:33:41 -070019 // TODO: move the touching of classes and GC to the ImageWriter proper
20 for (size_t i = 0; i < java_lang_dex_file_->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070021 const DexFile::ClassDef& class_def = java_lang_dex_file_->GetClassDef(i);
Brian Carlstroma663ea52011-08-19 23:33:41 -070022 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 Carlstrom9cff8e12011-08-18 16:47:29 -070026 // TODO: Heap::CollectGarbage before writing
Brian Carlstroma663ea52011-08-19 23:33:41 -070027
Brian Carlstromdb4d5402011-08-09 12:18:28 -070028 const std::vector<Space*>& spaces = Heap::GetSpaces();
29 // can't currently deal with writing a space that might have pointers between spaces
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070030 ASSERT_EQ(1U, spaces.size());
31 Space* space = spaces[0];
Brian Carlstromdb4d5402011-08-09 12:18:28 -070032
33 ImageWriter writer;
34 ScratchFile tmp;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070035 const int image_base = 0x50000000;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070036 bool success = writer.Write(space, tmp.GetFilename(), reinterpret_cast<byte*>(image_base));
37 ASSERT_TRUE(success);
38
39 {
Elliott Hughes90a33692011-08-30 13:27:07 -070040 UniquePtr<File> file(OS::OpenFile(tmp.GetFilename(), false));
41 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070042 ImageHeader image_header;
43 file->ReadFully(&image_header, sizeof(image_header));
44 ASSERT_TRUE(image_header.IsValid());
45 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length()));
46 }
Brian Carlstrom8a436592011-08-15 21:27:23 -070047
Brian Carlstroma663ea52011-08-19 23:33:41 -070048 // tear down old runtime before making a new one, clearing out misc state
Brian Carlstrom8a436592011-08-15 21:27:23 -070049 delete runtime_.release();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070050
51 // don't reuse java_lang_dex_file_ so we make sure we don't get
52 // lucky by pointers that happen to work referencing the earlier
53 // dex.
54 delete java_lang_dex_file_.release();
Elliott Hughes90a33692011-08-30 13:27:07 -070055 UniquePtr<const DexFile> dex(GetLibCoreDex());
56 ASSERT_TRUE(dex.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070057
58 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070059 boot_class_path.push_back(dex.get());
Brian Carlstrom8a436592011-08-15 21:27:23 -070060
61 Runtime::Options options;
62 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
63 std::string boot_image("-Xbootimage:");
64 boot_image.append(tmp.GetFilename());
65 options.push_back(std::make_pair(boot_image.c_str(), reinterpret_cast<void*>(NULL)));
66
67 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -070068 ASSERT_TRUE(runtime_.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070069 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070070
71 ASSERT_EQ(2U, Heap::GetSpaces().size());
72 Space* boot_space = Heap::GetSpaces()[0];
73 ASSERT_TRUE(boot_space != NULL);
74
Brian Carlstroma663ea52011-08-19 23:33:41 -070075 // enable to display maps to debug boot_base and boot_limit checking problems below
Elliott Hughes42ee1422011-09-06 12:33:32 -070076 // TODO: why does this dump show two attached threads?
77 if (true) {
78 SignalCatcher::HandleSigQuit();
Brian Carlstroma663ea52011-08-19 23:33:41 -070079 }
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