blob: 7ecdf3cb1f4f14714c8841b6a51e57df156f7d87 [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 Carlstrom693267a2011-09-06 09:25:34 -070019 // TODO: remove the touching of classes, call Compiler instead
Brian Carlstroma663ea52011-08-19 23:33:41 -070020 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 Carlstroma663ea52011-08-19 23:33:41 -070026
Brian Carlstromdb4d5402011-08-09 12:18:28 -070027 ImageWriter writer;
28 ScratchFile tmp;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070029 const uintptr_t image_base = 0x50000000;
30 bool success = writer.Write(tmp.GetFilename(), image_base);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070031 ASSERT_TRUE(success);
32
33 {
Elliott Hughes90a33692011-08-30 13:27:07 -070034 UniquePtr<File> file(OS::OpenFile(tmp.GetFilename(), false));
35 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070036 ImageHeader image_header;
37 file->ReadFully(&image_header, sizeof(image_header));
38 ASSERT_TRUE(image_header.IsValid());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070039
40 ASSERT_EQ(1U, Heap::GetSpaces().size());
41 Space* space = Heap::GetSpaces()[0];
42 ASSERT_TRUE(space != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070043 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length()));
44 }
Brian Carlstrom8a436592011-08-15 21:27:23 -070045
Brian Carlstroma663ea52011-08-19 23:33:41 -070046 // tear down old runtime before making a new one, clearing out misc state
Brian Carlstrom8a436592011-08-15 21:27:23 -070047 delete runtime_.release();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070048
49 // don't reuse java_lang_dex_file_ so we make sure we don't get
50 // lucky by pointers that happen to work referencing the earlier
51 // dex.
52 delete java_lang_dex_file_.release();
Elliott Hughes90a33692011-08-30 13:27:07 -070053 UniquePtr<const DexFile> dex(GetLibCoreDex());
54 ASSERT_TRUE(dex.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070055
56 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070057 boot_class_path.push_back(dex.get());
Brian Carlstrom8a436592011-08-15 21:27:23 -070058
59 Runtime::Options options;
60 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
61 std::string boot_image("-Xbootimage:");
62 boot_image.append(tmp.GetFilename());
63 options.push_back(std::make_pair(boot_image.c_str(), reinterpret_cast<void*>(NULL)));
64
65 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -070066 ASSERT_TRUE(runtime_.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070067 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070068
Brian Carlstrom16192862011-09-12 17:50:06 -070069 ASSERT_TRUE(runtime_->GetJniStubArray() != NULL);
70
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070071 ASSERT_EQ(2U, Heap::GetSpaces().size());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070072 Space* boot_space = Heap::GetBootSpace();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070073 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 Hughesd369bb72011-09-12 14:41:14 -070076 if (false) {
Elliott Hughes42ee1422011-09-06 12:33:32 -070077 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;
Elliott Hughes5f791332011-09-15 17:45:30 -070089 EXPECT_EQ(*klass->GetRawLockWordAddress(), 0); // 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