Convert Class::descriptor_ from StringPiece to String (as part of image loading)
Change-Id: Iab5ffa353661a7c06ee79af1f40d399a53777174
diff --git a/src/image_test.cc b/src/image_test.cc
index 6cfa62d..c68b600 100644
--- a/src/image_test.cc
+++ b/src/image_test.cc
@@ -13,11 +13,25 @@
class ImageTest : public CommonTest {};
-TEST_F(ImageTest, WriteRead) {
- scoped_ptr<DexFile> libcore_dex_file(GetLibCoreDex());
- EXPECT_TRUE(libcore_dex_file.get() != NULL);
+std::string ReadFileToString(const char* file_name) {
+ scoped_ptr<File> file(OS::OpenFile(file_name, false));
+ CHECK(file != NULL);
- // TODO: garbage collect before writing
+ std::string contents;
+ char buf[8 * KB];
+ while (true) {
+ int64_t n = file->Read(buf, sizeof(buf));
+ CHECK_NE(-1, n);
+ if (n == 0) {
+ break;
+ }
+ contents.append(buf, n);
+ }
+ return contents;
+}
+
+TEST_F(ImageTest, WriteRead) {
+ // TODO: Heap::CollectGarbage before writing
const std::vector<Space*>& spaces = Heap::GetSpaces();
// can't currently deal with writing a space that might have pointers between spaces
ASSERT_EQ(1U, spaces.size());
@@ -25,7 +39,7 @@
ImageWriter writer;
ScratchFile tmp;
- const int image_base = 0x5000000;
+ const int image_base = 0x50000000;
bool success = writer.Write(space, tmp.GetFilename(), reinterpret_cast<byte*>(image_base));
ASSERT_TRUE(success);
@@ -40,10 +54,16 @@
// tear down old runtime and make a new one
delete runtime_.release();
- java_lang_dex_file_.reset(GetLibCoreDex());
+
+ // don't reuse java_lang_dex_file_ so we make sure we don't get
+ // lucky by pointers that happen to work referencing the earlier
+ // dex.
+ delete java_lang_dex_file_.release();
+ scoped_ptr<DexFile> dex(GetLibCoreDex());
+ ASSERT_TRUE(dex != NULL);
std::vector<const DexFile*> boot_class_path;
- boot_class_path.push_back(java_lang_dex_file_.get());
+ boot_class_path.push_back(dex.get());
Runtime::Options options;
options.push_back(std::make_pair("bootclasspath", &boot_class_path));
@@ -54,6 +74,28 @@
runtime_.reset(Runtime::Create(options, false));
ASSERT_TRUE(runtime_ != NULL);
class_linker_ = runtime_->GetClassLinker();
+
+ if (true) {
+ const char* maps_file = "/proc/self/maps";
+ std::string contents = ReadFileToString(maps_file);
+ LG << maps_file << ":\n" << contents;
+ }
+
+ ASSERT_EQ(2U, Heap::GetSpaces().size());
+ Space* boot_space = Heap::GetSpaces()[0];
+ ASSERT_TRUE(boot_space != NULL);
+
+ // TODO: need to rebuild ClassLinker::classes_ and ::intern_table_
+ // byte* boot_base = boot_space->GetBase();
+ // byte* boot_limit = boot_space->GetLimit();
+ for (size_t i = 0; i < dex->NumClassDefs(); i++) {
+ const DexFile::ClassDef class_def = dex->GetClassDef(i);
+ const char* descriptor = dex->GetClassDescriptor(class_def);
+ Class* klass = class_linker_->FindSystemClass(descriptor);
+ EXPECT_TRUE(klass != NULL);
+ // EXPECT_LT(boot_base, reinterpret_cast<byte*>(klass));
+ // EXPECT_LT(reinterpret_cast<byte*>(klass), boot_limit);
+ }
}
} // namespace art