blob: f07588a78a520bd1ff2c60d8e6c2c810d0342596 [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 ImageWriter writer;
29 ScratchFile tmp;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070030 const uintptr_t image_base = 0x50000000;
31 bool success = writer.Write(tmp.GetFilename(), image_base);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070032 ASSERT_TRUE(success);
33
34 {
Elliott Hughes90a33692011-08-30 13:27:07 -070035 UniquePtr<File> file(OS::OpenFile(tmp.GetFilename(), false));
36 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070037 ImageHeader image_header;
38 file->ReadFully(&image_header, sizeof(image_header));
39 ASSERT_TRUE(image_header.IsValid());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070040
41 ASSERT_EQ(1U, Heap::GetSpaces().size());
42 Space* space = Heap::GetSpaces()[0];
43 ASSERT_TRUE(space != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070044 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());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070071 Space* boot_space = Heap::GetBootSpace();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070072 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
Elliott Hughes42ee1422011-09-06 12:33:32 -070075 // TODO: why does this dump show two attached threads?
76 if (true) {
77 SignalCatcher::HandleSigQuit();
Brian Carlstroma663ea52011-08-19 23:33:41 -070078 }
79
80 byte* boot_base = boot_space->GetBase();
81 byte* boot_limit = boot_space->GetLimit();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070082 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070083 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070084 const char* descriptor = dex->GetClassDescriptor(class_def);
85 Class* klass = class_linker_->FindSystemClass(descriptor);
Brian Carlstroma663ea52011-08-19 23:33:41 -070086 EXPECT_TRUE(klass != NULL) << descriptor;
87 EXPECT_LT(boot_base, reinterpret_cast<byte*>(klass)) << descriptor;
88 EXPECT_LT(reinterpret_cast<byte*>(klass), boot_limit) << descriptor;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070089 EXPECT_TRUE(klass->GetMonitor() == NULL); // address should have been removed from monitor
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070090 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -070091}
92
93} // namespace art