blob: 3e64d956661ceeec81a536054b5b31596e769d54 [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;
Elliott Hughes234da572011-11-03 22:13:06 -070021 bool success_oat = OatWriter::Create(tmp_oat.GetFile(), NULL, *compiler_.get());
Brian Carlstrome24fa612011-09-29 00:53:55 -070022 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 Carlstrom58ae9412011-10-04 00:56:06 -070026 const uintptr_t requested_image_base = 0x60000000;
27 bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base,
Brian Carlstrome24fa612011-09-29 00:53:55 -070028 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];
Brian Carlstrom3320cf42011-10-04 14:58:28 -070040 ASSERT_FALSE(space->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070041 ASSERT_TRUE(space != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070042 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length()));
43 }
Brian Carlstrom8a436592011-08-15 21:27:23 -070044
Brian Carlstroma663ea52011-08-19 23:33:41 -070045 // tear down old runtime before making a new one, clearing out misc state
Brian Carlstrom8a436592011-08-15 21:27:23 -070046 delete runtime_.release();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070047
48 // don't reuse java_lang_dex_file_ so we make sure we don't get
49 // lucky by pointers that happen to work referencing the earlier
50 // dex.
51 delete java_lang_dex_file_.release();
Elliott Hughes90a33692011-08-30 13:27:07 -070052 UniquePtr<const DexFile> dex(GetLibCoreDex());
53 ASSERT_TRUE(dex.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070054
Brian Carlstrom8a436592011-08-15 21:27:23 -070055 Runtime::Options options;
Brian Carlstrom58ae9412011-10-04 00:56:06 -070056 std::string image("-Ximage:");
57 image.append(tmp_image.GetFilename());
58 options.push_back(std::make_pair(image.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom8a436592011-08-15 21:27:23 -070059
60 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -070061 ASSERT_TRUE(runtime_.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070062 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070063
Brian Carlstrom16192862011-09-12 17:50:06 -070064 ASSERT_TRUE(runtime_->GetJniStubArray() != NULL);
65
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070066 ASSERT_EQ(2U, Heap::GetSpaces().size());
Brian Carlstrom58ae9412011-10-04 00:56:06 -070067 ASSERT_TRUE(Heap::GetSpaces()[0]->IsImageSpace());
68 ASSERT_FALSE(Heap::GetSpaces()[1]->IsImageSpace());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070069
Brian Carlstrom58ae9412011-10-04 00:56:06 -070070 Space* image_space = Heap::GetSpaces()[0];
71 byte* image_base = image_space->GetBase();
72 byte* image_limit = image_space->GetLimit();
73 CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_base));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070074 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070075 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070076 const char* descriptor = dex->GetClassDescriptor(class_def);
77 Class* klass = class_linker_->FindSystemClass(descriptor);
Brian Carlstroma663ea52011-08-19 23:33:41 -070078 EXPECT_TRUE(klass != NULL) << descriptor;
Brian Carlstrom58ae9412011-10-04 00:56:06 -070079 EXPECT_LT(image_base, reinterpret_cast<byte*>(klass)) << descriptor;
80 EXPECT_LT(reinterpret_cast<byte*>(klass), image_limit) << descriptor;
Elliott Hughes5f791332011-09-15 17:45:30 -070081 EXPECT_EQ(*klass->GetRawLockWordAddress(), 0); // address should have been removed from monitor
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070082 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -070083}
84
85} // namespace art