blob: dc81587d6ba6d35e5b883f91775f473b33446db4 [file] [log] [blame]
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
#include <vector>
#include "common_test.h"
#include "image.h"
#include "image_writer.h"
#include "oat_writer.h"
#include "signal_catcher.h"
#include "gc/space.h"
#include "UniquePtr.h"
#include "utils.h"
namespace art {
class ImageTest : public CommonTest {
protected:
virtual void SetUp() {
// Reserve where the image will be loaded up front so that other parts of test set up don't
// accidentally end up colliding with the fixed memory address when we need to load the image.
image_reservation_.reset(MemMap::MapAnonymous("Image reservation", (byte*)ART_BASE_ADDRESS,
(size_t)100 * 1024 *1024, // 100MB
PROT_NONE));
CommonTest::SetUp();
}
UniquePtr<MemMap> image_reservation_;
};
TEST_F(ImageTest, WriteRead) {
ScratchFile tmp_oat;
{
ScopedObjectAccess soa(Thread::Current());
std::vector<const DexFile*> dex_files;
dex_files.push_back(java_lang_dex_file_);
bool success_oat = OatWriter::Create(tmp_oat.GetFile(), dex_files, 0, 0, "", *compiler_.get());
ASSERT_TRUE(success_oat);
// Force all system classes into memory
for (size_t i = 0; i < java_lang_dex_file_->NumClassDefs(); ++i) {
const DexFile::ClassDef& class_def = java_lang_dex_file_->GetClassDef(i);
const char* descriptor = java_lang_dex_file_->GetClassDescriptor(class_def);
Class* klass = class_linker_->FindSystemClass(descriptor);
EXPECT_TRUE(klass != NULL) << descriptor;
}
}
ScratchFile tmp_image;
const uintptr_t requested_image_base = ART_BASE_ADDRESS;
{
ImageWriter writer(NULL);
bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base,
tmp_oat.GetFilename(), tmp_oat.GetFilename(),
*compiler_.get());
ASSERT_TRUE(success_image);
}
{
UniquePtr<File> file(OS::OpenFile(tmp_image.GetFilename().c_str(), false));
ASSERT_TRUE(file.get() != NULL);
ImageHeader image_header;
file->ReadFully(&image_header, sizeof(image_header));
ASSERT_TRUE(image_header.IsValid());
Heap* heap = Runtime::Current()->GetHeap();
ASSERT_EQ(1U, heap->GetSpaces().size());
ContinuousSpace* space = heap->GetSpaces().front();
ASSERT_FALSE(space->IsImageSpace());
ASSERT_TRUE(space != NULL);
ASSERT_TRUE(space->IsAllocSpace());
ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->GetLength()));
}
// Need to delete the compiler since it has worker threads which are attached to runtime.
compiler_.reset();
// Tear down old runtime before making a new one, clearing out misc state.
runtime_.reset();
java_lang_dex_file_ = NULL;
UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName(), GetLibCoreDexFileName()));
ASSERT_TRUE(dex.get() != NULL);
// Remove the reservation of the memory for use to load the image.
image_reservation_.reset();
Runtime::Options options;
std::string image("-Ximage:");
image.append(tmp_image.GetFilename());
options.push_back(std::make_pair(image.c_str(), reinterpret_cast<void*>(NULL)));
if (!Runtime::Create(options, false)) {
LOG(FATAL) << "Failed to create runtime";
return;
}
runtime_.reset(Runtime::Current());
// Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
// give it away now and then switch to a more managable ScopedObjectAccess.
Thread::Current()->TransitionFromRunnableToSuspended(kNative);
ScopedObjectAccess soa(Thread::Current());
ASSERT_TRUE(runtime_.get() != NULL);
class_linker_ = runtime_->GetClassLinker();
ASSERT_TRUE(runtime_->GetJniDlsymLookupStub() != NULL);
Heap* heap = Runtime::Current()->GetHeap();
ASSERT_EQ(2U, heap->GetSpaces().size());
ASSERT_TRUE(heap->GetSpaces()[0]->IsImageSpace());
ASSERT_FALSE(heap->GetSpaces()[0]->IsAllocSpace());
ASSERT_FALSE(heap->GetSpaces()[1]->IsImageSpace());
ASSERT_TRUE(heap->GetSpaces()[1]->IsAllocSpace());
ImageSpace* image_space = heap->GetImageSpace();
byte* image_begin = image_space->Begin();
byte* image_end = image_space->End();
CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin));
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) << descriptor;
EXPECT_LT(image_begin, reinterpret_cast<byte*>(klass)) << descriptor;
EXPECT_LT(reinterpret_cast<byte*>(klass), image_end) << descriptor;
EXPECT_EQ(*klass->GetRawLockWordAddress(), 0); // address should have been removed from monitor
}
}
} // namespace art