blob: 2fd5c34169fee1b3d76f42b1cd09215d4c513b21 [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"
Brian Carlstrome24fa612011-09-29 00:53:55 -070010#include "oat_writer.h"
Elliott Hughes42ee1422011-09-06 12:33:32 -070011#include "signal_catcher.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070012#include "space.h"
buzbeec143c552011-08-20 17:38:58 -070013#include "utils.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070014
Brian Carlstromdb4d5402011-08-09 12:18:28 -070015namespace art {
16
Brian Carlstromf734cf52011-08-17 16:28:14 -070017class ImageTest : public CommonTest {};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070018
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070019TEST_F(ImageTest, WriteRead) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070020 ScratchFile tmp_oat;
21 bool success_oat = OatWriter::Create(tmp_oat.GetFilename(), NULL);
22 ASSERT_TRUE(success_oat);
Brian Carlstroma663ea52011-08-19 23:33:41 -070023
Brian Carlstromdb4d5402011-08-09 12:18:28 -070024 ImageWriter writer;
Brian Carlstrome24fa612011-09-29 00:53:55 -070025 ScratchFile tmp_image;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070026 const uintptr_t image_base = 0x50000000;
Brian Carlstrome24fa612011-09-29 00:53:55 -070027 bool success_image = writer.Write(tmp_image.GetFilename(), image_base,
28 std::string(tmp_oat.GetFilename()), "");
29 ASSERT_TRUE(success_image);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070030
31 {
Brian Carlstrome24fa612011-09-29 00:53:55 -070032 UniquePtr<File> file(OS::OpenFile(tmp_image.GetFilename(), false));
Elliott Hughes90a33692011-08-30 13:27:07 -070033 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070034 ImageHeader image_header;
35 file->ReadFully(&image_header, sizeof(image_header));
36 ASSERT_TRUE(image_header.IsValid());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070037
38 ASSERT_EQ(1U, Heap::GetSpaces().size());
39 Space* space = Heap::GetSpaces()[0];
40 ASSERT_TRUE(space != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070041 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length()));
42 }
Brian Carlstrom8a436592011-08-15 21:27:23 -070043
Brian Carlstroma663ea52011-08-19 23:33:41 -070044 // tear down old runtime before making a new one, clearing out misc state
Brian Carlstrom8a436592011-08-15 21:27:23 -070045 delete runtime_.release();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070046
47 // don't reuse java_lang_dex_file_ so we make sure we don't get
48 // lucky by pointers that happen to work referencing the earlier
49 // dex.
50 delete java_lang_dex_file_.release();
Elliott Hughes90a33692011-08-30 13:27:07 -070051 UniquePtr<const DexFile> dex(GetLibCoreDex());
52 ASSERT_TRUE(dex.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070053
54 std::vector<const DexFile*> boot_class_path;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070055 boot_class_path.push_back(dex.get());
Brian Carlstrom8a436592011-08-15 21:27:23 -070056
57 Runtime::Options options;
58 options.push_back(std::make_pair("bootclasspath", &boot_class_path));
Brian Carlstrome24fa612011-09-29 00:53:55 -070059 std::string boot_oat("-Xbootoat:");
60 boot_oat.append(tmp_oat.GetFilename());
61 options.push_back(std::make_pair(boot_oat.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom8a436592011-08-15 21:27:23 -070062 std::string boot_image("-Xbootimage:");
Brian Carlstrome24fa612011-09-29 00:53:55 -070063 boot_image.append(tmp_image.GetFilename());
Brian Carlstrom8a436592011-08-15 21:27:23 -070064 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
Brian Carlstrom16192862011-09-12 17:50:06 -070070 ASSERT_TRUE(runtime_->GetJniStubArray() != NULL);
71
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070072 ASSERT_EQ(2U, Heap::GetSpaces().size());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070073 Space* boot_space = Heap::GetBootSpace();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070074 ASSERT_TRUE(boot_space != NULL);
75
Brian Carlstroma663ea52011-08-19 23:33:41 -070076 // enable to display maps to debug boot_base and boot_limit checking problems below
Elliott Hughesd369bb72011-09-12 14:41:14 -070077 if (false) {
Elliott Hughes42ee1422011-09-06 12:33:32 -070078 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;
Elliott Hughes5f791332011-09-15 17:45:30 -070090 EXPECT_EQ(*klass->GetRawLockWordAddress(), 0); // address should have been removed from monitor
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070091 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -070092}
93
94} // namespace art