blob: fb87a493322bd4a16bc23c7768064f7ba4ab026f [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstromdb4d5402011-08-09 12:18:28 -070016
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070017#include <string>
18#include <vector>
19
Brian Carlstromdb4d5402011-08-09 12:18:28 -070020#include "common_test.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070021#include "file.h"
22#include "image.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070023#include "image_writer.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070024#include "oat_writer.h"
Elliott Hughes42ee1422011-09-06 12:33:32 -070025#include "signal_catcher.h"
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070026#include "space.h"
buzbeec143c552011-08-20 17:38:58 -070027#include "utils.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070028
Brian Carlstromdb4d5402011-08-09 12:18:28 -070029namespace art {
30
Brian Carlstromf734cf52011-08-17 16:28:14 -070031class ImageTest : public CommonTest {};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070032
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070033TEST_F(ImageTest, WriteRead) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070034 ScratchFile tmp_oat;
jeffhao10037c82012-01-23 15:06:23 -080035 std::vector<const DexFile*> dex_files;
Brian Carlstroma004aa92012-02-08 18:05:09 -080036 dex_files.push_back(java_lang_dex_file_);
jeffhao10037c82012-01-23 15:06:23 -080037 bool success_oat = OatWriter::Create(tmp_oat.GetFile(), NULL, dex_files, *compiler_.get());
Brian Carlstrome24fa612011-09-29 00:53:55 -070038 ASSERT_TRUE(success_oat);
Brian Carlstroma663ea52011-08-19 23:33:41 -070039
Ian Rogers0571d352011-11-03 19:51:38 -070040 // Force all system classes into memory
41 for (size_t i = 0; i < java_lang_dex_file_->NumClassDefs(); i++) {
42 const DexFile::ClassDef& class_def = java_lang_dex_file_->GetClassDef(i);
43 const char* descriptor = java_lang_dex_file_->GetClassDescriptor(class_def);
44 Class* klass = class_linker_->FindSystemClass(descriptor);
45 EXPECT_TRUE(klass != NULL) << descriptor;
46 }
47
Brian Carlstromae826982011-11-09 01:33:42 -080048 ImageWriter writer(NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -070049 ScratchFile tmp_image;
Brian Carlstrom58ae9412011-10-04 00:56:06 -070050 const uintptr_t requested_image_base = 0x60000000;
51 bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base,
Brian Carlstroma004aa92012-02-08 18:05:09 -080052 tmp_oat.GetFilename(), tmp_oat.GetFilename());
Brian Carlstrome24fa612011-09-29 00:53:55 -070053 ASSERT_TRUE(success_image);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070054
55 {
Brian Carlstroma004aa92012-02-08 18:05:09 -080056 UniquePtr<File> file(OS::OpenFile(tmp_image.GetFilename().c_str(), false));
Elliott Hughes90a33692011-08-30 13:27:07 -070057 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070058 ImageHeader image_header;
59 file->ReadFully(&image_header, sizeof(image_header));
60 ASSERT_TRUE(image_header.IsValid());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070061
62 ASSERT_EQ(1U, Heap::GetSpaces().size());
63 Space* space = Heap::GetSpaces()[0];
Brian Carlstrom3320cf42011-10-04 14:58:28 -070064 ASSERT_FALSE(space->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070065 ASSERT_TRUE(space != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070066 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->Length()));
67 }
Brian Carlstrom8a436592011-08-15 21:27:23 -070068
Brian Carlstroma663ea52011-08-19 23:33:41 -070069 // tear down old runtime before making a new one, clearing out misc state
Brian Carlstrom8a436592011-08-15 21:27:23 -070070 delete runtime_.release();
Brian Carlstroma004aa92012-02-08 18:05:09 -080071 java_lang_dex_file_ = NULL;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070072
Brian Carlstroma004aa92012-02-08 18:05:09 -080073 UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName(), GetLibCoreDexFileName()));
Elliott Hughes90a33692011-08-30 13:27:07 -070074 ASSERT_TRUE(dex.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070075
Brian Carlstrom8a436592011-08-15 21:27:23 -070076 Runtime::Options options;
Brian Carlstrom58ae9412011-10-04 00:56:06 -070077 std::string image("-Ximage:");
78 image.append(tmp_image.GetFilename());
79 options.push_back(std::make_pair(image.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom8a436592011-08-15 21:27:23 -070080
81 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -070082 ASSERT_TRUE(runtime_.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -070083 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070084
Ian Rogers169c9a72011-11-13 20:13:17 -080085 ASSERT_TRUE(runtime_->GetJniDlsymLookupStub() != NULL);
Brian Carlstrom16192862011-09-12 17:50:06 -070086
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070087 ASSERT_EQ(2U, Heap::GetSpaces().size());
Brian Carlstrom58ae9412011-10-04 00:56:06 -070088 ASSERT_TRUE(Heap::GetSpaces()[0]->IsImageSpace());
89 ASSERT_FALSE(Heap::GetSpaces()[1]->IsImageSpace());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070090
Ian Rogers30fab402012-01-23 15:43:46 -080091 ImageSpace* image_space = Heap::GetSpaces()[0]->AsImageSpace();
92 byte* image_begin = image_space->Begin();
93 byte* image_end = image_space->End();
94 CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070095 for (size_t i = 0; i < dex->NumClassDefs(); i++) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -070096 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070097 const char* descriptor = dex->GetClassDescriptor(class_def);
98 Class* klass = class_linker_->FindSystemClass(descriptor);
Brian Carlstroma663ea52011-08-19 23:33:41 -070099 EXPECT_TRUE(klass != NULL) << descriptor;
Ian Rogers30fab402012-01-23 15:43:46 -0800100 EXPECT_LT(image_begin, reinterpret_cast<byte*>(klass)) << descriptor;
101 EXPECT_LT(reinterpret_cast<byte*>(klass), image_end) << descriptor;
Elliott Hughes5f791332011-09-15 17:45:30 -0700102 EXPECT_EQ(*klass->GetRawLockWordAddress(), 0); // address should have been removed from monitor
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700103 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700104}
105
106} // namespace art