blob: cd1a34fa56ecf9ea8da20e701ead5d9ae668ca6d [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 "image.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070022#include "image_writer.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070023#include "oat_writer.h"
Elliott Hughes42ee1422011-09-06 12:33:32 -070024#include "signal_catcher.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070025#include "gc/space.h"
Ian Rogers10c5b782013-01-10 10:40:53 -080026#include "UniquePtr.h"
buzbeec143c552011-08-20 17:38:58 -070027#include "utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080028#include "vector_output_stream.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070029
Brian Carlstromdb4d5402011-08-09 12:18:28 -070030namespace art {
31
Ian Rogers10c5b782013-01-10 10:40:53 -080032class ImageTest : public CommonTest {
33
34 protected:
35 virtual void SetUp() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080036 ReserveImageSpace();
Ian Rogers10c5b782013-01-10 10:40:53 -080037 CommonTest::SetUp();
38 }
Ian Rogers10c5b782013-01-10 10:40:53 -080039};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070040
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070041TEST_F(ImageTest, WriteRead) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080042 ScratchFile tmp_elf;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043 {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080044 std::vector<uint8_t> oat_contents;
45 {
46 ScopedObjectAccess soa(Thread::Current());
47 std::vector<const DexFile*> dex_files;
48 dex_files.push_back(java_lang_dex_file_);
Brian Carlstrom654d9192013-04-30 18:35:32 -070049 dex_files.push_back(conscrypt_file_);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080050 VectorOutputStream output_stream(tmp_elf.GetFilename(), oat_contents);
Jeff Hao0aba0ba2013-06-03 14:49:28 -070051 bool success_oat = OatWriter::Create(output_stream, dex_files, 0, 0, "",
52 *compiler_driver_.get());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053 ASSERT_TRUE(success_oat);
Brian Carlstroma663ea52011-08-19 23:33:41 -070054
Brian Carlstrom700c8d32012-11-05 10:42:02 -080055 // Force all system classes into memory
Brian Carlstrom654d9192013-04-30 18:35:32 -070056 for (size_t dex_file_index = 0; dex_file_index < dex_files.size(); ++dex_file_index) {
57 const DexFile* dex_file = dex_files[dex_file_index];
58 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); ++class_def_index) {
59 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
60 const char* descriptor = dex_file->GetClassDescriptor(class_def);
61 mirror::Class* klass = class_linker_->FindSystemClass(descriptor);
62 EXPECT_TRUE(klass != NULL) << descriptor;
63 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -080064 }
Brian Carlstrom3f47c122013-03-07 00:02:40 -080065 bool success_elf = compiler_driver_->WriteElf(GetTestAndroidRoot(),
Brian Carlstrom265091e2013-01-30 14:08:26 -080066 !kIsTargetBuild,
67 dex_files,
68 oat_contents,
69 tmp_elf.GetFile());
70 ASSERT_TRUE(success_elf);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071 }
Ian Rogers0571d352011-11-03 19:51:38 -070072 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -080073 // Workound bug that mcld::Linker::emit closes tmp_elf by reopening as tmp_oat.
74 UniquePtr<File> tmp_oat(OS::OpenFile(tmp_elf.GetFilename().c_str(), true, false));
75 ASSERT_TRUE(tmp_oat.get() != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070076
Brian Carlstrome24fa612011-09-29 00:53:55 -070077 ScratchFile tmp_image;
jeffhao8161c032012-10-31 15:50:00 -070078 const uintptr_t requested_image_base = ART_BASE_ADDRESS;
79 {
80 ImageWriter writer(NULL);
81 bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base,
Brian Carlstrom700c8d32012-11-05 10:42:02 -080082 tmp_oat->GetPath(), tmp_oat->GetPath(),
Ian Rogers1212a022013-03-04 10:48:41 -080083 *compiler_driver_.get());
jeffhao8161c032012-10-31 15:50:00 -070084 ASSERT_TRUE(success_image);
Ian Rogers1212a022013-03-04 10:48:41 -080085 bool success_fixup = compiler_driver_->FixupElf(tmp_oat.get(), writer.GetOatDataBegin());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080086 ASSERT_TRUE(success_fixup);
jeffhao8161c032012-10-31 15:50:00 -070087 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070088
89 {
Brian Carlstroma004aa92012-02-08 18:05:09 -080090 UniquePtr<File> file(OS::OpenFile(tmp_image.GetFilename().c_str(), false));
Elliott Hughes90a33692011-08-30 13:27:07 -070091 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070092 ImageHeader image_header;
93 file->ReadFully(&image_header, sizeof(image_header));
94 ASSERT_TRUE(image_header.IsValid());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070095
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080096 Heap* heap = Runtime::Current()->GetHeap();
97 ASSERT_EQ(1U, heap->GetSpaces().size());
Mathieu Chartier2fde5332012-09-14 14:51:54 -070098 ContinuousSpace* space = heap->GetSpaces().front();
Brian Carlstrom3320cf42011-10-04 14:58:28 -070099 ASSERT_FALSE(space->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700100 ASSERT_TRUE(space != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700101 ASSERT_TRUE(space->IsAllocSpace());
Elliott Hughes76160052012-12-12 16:31:20 -0800102 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->GetLength()));
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700103 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700104
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700105 // Need to delete the compiler since it has worker threads which are attached to runtime.
Ian Rogers1212a022013-03-04 10:48:41 -0800106 compiler_driver_.reset();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700107
Ian Rogers10c5b782013-01-10 10:40:53 -0800108 // Tear down old runtime before making a new one, clearing out misc state.
109 runtime_.reset();
Brian Carlstroma004aa92012-02-08 18:05:09 -0800110 java_lang_dex_file_ = NULL;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700111
Brian Carlstroma004aa92012-02-08 18:05:09 -0800112 UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName(), GetLibCoreDexFileName()));
Elliott Hughes90a33692011-08-30 13:27:07 -0700113 ASSERT_TRUE(dex.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700114
Ian Rogers10c5b782013-01-10 10:40:53 -0800115 // Remove the reservation of the memory for use to load the image.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800116 UnreserveImageSpace();
Ian Rogers10c5b782013-01-10 10:40:53 -0800117
Brian Carlstrom8a436592011-08-15 21:27:23 -0700118 Runtime::Options options;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700119 std::string image("-Ximage:");
120 image.append(tmp_image.GetFilename());
121 options.push_back(std::make_pair(image.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom8a436592011-08-15 21:27:23 -0700122
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700123 if (!Runtime::Create(options, false)) {
124 LOG(FATAL) << "Failed to create runtime";
125 return;
126 }
127 runtime_.reset(Runtime::Current());
128 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
129 // give it away now and then switch to a more managable ScopedObjectAccess.
130 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
131 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes90a33692011-08-30 13:27:07 -0700132 ASSERT_TRUE(runtime_.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700133 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700134
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800135 Heap* heap = Runtime::Current()->GetHeap();
136 ASSERT_EQ(2U, heap->GetSpaces().size());
137 ASSERT_TRUE(heap->GetSpaces()[0]->IsImageSpace());
Brian Carlstromfddf6f62012-03-15 16:56:45 -0700138 ASSERT_FALSE(heap->GetSpaces()[0]->IsAllocSpace());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800139 ASSERT_FALSE(heap->GetSpaces()[1]->IsImageSpace());
Brian Carlstromfddf6f62012-03-15 16:56:45 -0700140 ASSERT_TRUE(heap->GetSpaces()[1]->IsAllocSpace());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700141
Brian Carlstromfddf6f62012-03-15 16:56:45 -0700142 ImageSpace* image_space = heap->GetImageSpace();
Ian Rogers30fab402012-01-23 15:43:46 -0800143 byte* image_begin = image_space->Begin();
144 byte* image_end = image_space->End();
145 CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800146 for (size_t i = 0; i < dex->NumClassDefs(); ++i) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700147 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700148 const char* descriptor = dex->GetClassDescriptor(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 mirror::Class* klass = class_linker_->FindSystemClass(descriptor);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700150 EXPECT_TRUE(klass != NULL) << descriptor;
Ian Rogers30fab402012-01-23 15:43:46 -0800151 EXPECT_LT(image_begin, reinterpret_cast<byte*>(klass)) << descriptor;
152 EXPECT_LT(reinterpret_cast<byte*>(klass), image_end) << descriptor;
Elliott Hughes5f791332011-09-15 17:45:30 -0700153 EXPECT_EQ(*klass->GetRawLockWordAddress(), 0); // address should have been removed from monitor
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700154 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700155}
156
157} // namespace art