blob: 08c32c28aeccaaeaad58faf7a7a28e101623d9bc [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 Carlstrom51c24672013-07-11 16:00:56 -070021#include "compiler/elf_fixup.h"
22#include "compiler/image_writer.h"
23#include "compiler/oat_writer.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "gc/space/image_space.h"
Brian Carlstrom51c24672013-07-11 16:00:56 -070025#include "image.h"
26#include "signal_catcher.h"
Ian Rogers10c5b782013-01-10 10:40:53 -080027#include "UniquePtr.h"
buzbeec143c552011-08-20 17:38:58 -070028#include "utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080029#include "vector_output_stream.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070030
Brian Carlstromdb4d5402011-08-09 12:18:28 -070031namespace art {
32
Ian Rogers10c5b782013-01-10 10:40:53 -080033class ImageTest : public CommonTest {
Ian Rogers10c5b782013-01-10 10:40:53 -080034 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 {
Brian Carlstrom96391602013-06-13 19:49:50 -070045 jobject class_loader = NULL;
46 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Anwar Ghuloum6f28d912013-07-24 15:02:53 -070047 base::TimingLogger timings("ImageTest::WriteRead", false, false);
Ian Rogersbd2c19f2013-07-25 17:43:46 -070048 timings.StartSplit("CompileAll");
Ian Rogerse9869532013-08-03 14:56:04 -070049#if defined(ART_USE_PORTABLE_COMPILER)
50 // TODO: we disable this for portable so the test executes in a reasonable amount of time.
51 // We shouldn't need to do this.
Ian Rogers36e58b82013-08-09 09:09:41 -070052 runtime_->SetCompilerFilter(Runtime::kInterpretOnly);
Ian Rogerse9869532013-08-03 14:56:04 -070053#endif
Brian Carlstrom45602482013-07-21 22:07:55 -070054 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), timings);
Brian Carlstrom96391602013-06-13 19:49:50 -070055
Brian Carlstrom700c8d32012-11-05 10:42:02 -080056 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromc50d8e12013-07-23 22:35:16 -070057 OatWriter oat_writer(class_linker->GetBootClassPath(),
58 0, 0, "", compiler_driver_.get());
59 bool success = compiler_driver_->WriteElf(GetTestAndroidRoot(),
60 !kIsTargetBuild,
61 class_linker->GetBootClassPath(),
62 oat_writer,
63 tmp_elf.GetFile());
64 ASSERT_TRUE(success);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065 }
Ian Rogers0571d352011-11-03 19:51:38 -070066 }
Brian Carlstrom700c8d32012-11-05 10:42:02 -080067 // Workound bug that mcld::Linker::emit closes tmp_elf by reopening as tmp_oat.
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070068 UniquePtr<File> tmp_oat(OS::OpenFileReadWrite(tmp_elf.GetFilename().c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080069 ASSERT_TRUE(tmp_oat.get() != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070070
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 ScratchFile tmp_image;
jeffhao8161c032012-10-31 15:50:00 -070072 const uintptr_t requested_image_base = ART_BASE_ADDRESS;
73 {
Brian Carlstrom96391602013-06-13 19:49:50 -070074 ImageWriter writer(*compiler_driver_.get());
jeffhao8161c032012-10-31 15:50:00 -070075 bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base,
Brian Carlstrom96391602013-06-13 19:49:50 -070076 tmp_oat->GetPath(), tmp_oat->GetPath());
jeffhao8161c032012-10-31 15:50:00 -070077 ASSERT_TRUE(success_image);
Brian Carlstrom51c24672013-07-11 16:00:56 -070078 bool success_fixup = ElfFixup::Fixup(tmp_oat.get(), writer.GetOatDataBegin());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080079 ASSERT_TRUE(success_fixup);
jeffhao8161c032012-10-31 15:50:00 -070080 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070081
82 {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070083 UniquePtr<File> file(OS::OpenFileForReading(tmp_image.GetFilename().c_str()));
Elliott Hughes90a33692011-08-30 13:27:07 -070084 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070085 ImageHeader image_header;
86 file->ReadFully(&image_header, sizeof(image_header));
87 ASSERT_TRUE(image_header.IsValid());
Mathieu Chartier31e89252013-08-28 11:29:12 -070088 ASSERT_GE(image_header.GetImageBitmapOffset(), sizeof(image_header));
89 ASSERT_NE(0U, image_header.GetImageBitmapSize());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070090
Ian Rogers1d54e732013-05-02 21:10:01 -070091 gc::Heap* heap = Runtime::Current()->GetHeap();
92 ASSERT_EQ(1U, heap->GetContinuousSpaces().size());
93 gc::space::ContinuousSpace* space = heap->GetContinuousSpaces().front();
Brian Carlstrom3320cf42011-10-04 14:58:28 -070094 ASSERT_FALSE(space->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070095 ASSERT_TRUE(space != NULL);
Ian Rogers1d54e732013-05-02 21:10:01 -070096 ASSERT_TRUE(space->IsDlMallocSpace());
Elliott Hughes76160052012-12-12 16:31:20 -080097 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->GetLength()));
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070098 }
Brian Carlstrom8a436592011-08-15 21:27:23 -070099
Brian Carlstrom96391602013-06-13 19:49:50 -0700100 ASSERT_TRUE(compiler_driver_->GetImageClasses() != NULL);
101 CompilerDriver::DescriptorSet image_classes(*compiler_driver_->GetImageClasses());
102
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700103 // Need to delete the compiler since it has worker threads which are attached to runtime.
Ian Rogers1212a022013-03-04 10:48:41 -0800104 compiler_driver_.reset();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700105
Ian Rogers10c5b782013-01-10 10:40:53 -0800106 // Tear down old runtime before making a new one, clearing out misc state.
107 runtime_.reset();
Brian Carlstroma004aa92012-02-08 18:05:09 -0800108 java_lang_dex_file_ = NULL;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700109
Brian Carlstroma004aa92012-02-08 18:05:09 -0800110 UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName(), GetLibCoreDexFileName()));
Elliott Hughes90a33692011-08-30 13:27:07 -0700111 ASSERT_TRUE(dex.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700112
Ian Rogers10c5b782013-01-10 10:40:53 -0800113 // Remove the reservation of the memory for use to load the image.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800114 UnreserveImageSpace();
Ian Rogers10c5b782013-01-10 10:40:53 -0800115
Brian Carlstrom8a436592011-08-15 21:27:23 -0700116 Runtime::Options options;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700117 std::string image("-Ximage:");
118 image.append(tmp_image.GetFilename());
119 options.push_back(std::make_pair(image.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom8a436592011-08-15 21:27:23 -0700120
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 if (!Runtime::Create(options, false)) {
122 LOG(FATAL) << "Failed to create runtime";
123 return;
124 }
125 runtime_.reset(Runtime::Current());
126 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
127 // give it away now and then switch to a more managable ScopedObjectAccess.
128 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
129 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes90a33692011-08-30 13:27:07 -0700130 ASSERT_TRUE(runtime_.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700131 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700132
Ian Rogers1d54e732013-05-02 21:10:01 -0700133 gc::Heap* heap = Runtime::Current()->GetHeap();
134 ASSERT_EQ(2U, heap->GetContinuousSpaces().size());
135 ASSERT_TRUE(heap->GetContinuousSpaces()[0]->IsImageSpace());
136 ASSERT_FALSE(heap->GetContinuousSpaces()[0]->IsDlMallocSpace());
137 ASSERT_FALSE(heap->GetContinuousSpaces()[1]->IsImageSpace());
138 ASSERT_TRUE(heap->GetContinuousSpaces()[1]->IsDlMallocSpace());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700139
Ian Rogers1d54e732013-05-02 21:10:01 -0700140 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Mathieu Chartier31e89252013-08-28 11:29:12 -0700141 image_space->VerifyImageAllocations();
Ian Rogers30fab402012-01-23 15:43:46 -0800142 byte* image_begin = image_space->Begin();
143 byte* image_end = image_space->End();
144 CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800145 for (size_t i = 0; i < dex->NumClassDefs(); ++i) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700146 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700147 const char* descriptor = dex->GetClassDescriptor(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148 mirror::Class* klass = class_linker_->FindSystemClass(descriptor);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700149 EXPECT_TRUE(klass != NULL) << descriptor;
Ian Rogers30fab402012-01-23 15:43:46 -0800150 EXPECT_LT(image_begin, reinterpret_cast<byte*>(klass)) << descriptor;
Brian Carlstrom96391602013-06-13 19:49:50 -0700151 if (image_classes.find(descriptor) != image_classes.end()) {
152 // image classes should be located before the end of the image.
153 EXPECT_LT(reinterpret_cast<byte*>(klass), image_end) << descriptor;
154 } else {
155 // non image classes should be in a space after the image.
156 EXPECT_GT(reinterpret_cast<byte*>(klass), image_end) << descriptor;
157 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700158 EXPECT_TRUE(Monitor::IsValidLockWord(*klass->GetRawLockWordAddress()));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700159 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700160}
161
Brian Carlstrom179486a2013-09-03 11:51:42 -0700162TEST_F(ImageTest, ImageHeaderIsValid) {
163 uint32_t image_begin = ART_BASE_ADDRESS;
164 uint32_t image_size_ = 16 * KB;
165 uint32_t image_bitmap_offset = 0;
166 uint32_t image_bitmap_size = 0;
167 uint32_t image_roots = ART_BASE_ADDRESS + (1 * KB);
168 uint32_t oat_checksum = 0;
169 uint32_t oat_file_begin = ART_BASE_ADDRESS + (4 * KB); // page aligned
170 uint32_t oat_data_begin = ART_BASE_ADDRESS + (8 * KB); // page aligned
171 uint32_t oat_data_end = ART_BASE_ADDRESS + (9 * KB);
172 uint32_t oat_file_end = ART_BASE_ADDRESS + (10 * KB);
173 ImageHeader image_header(image_begin,
174 image_size_,
175 image_bitmap_offset,
176 image_bitmap_size,
177 image_roots,
178 oat_checksum,
179 oat_file_begin,
180 oat_data_begin,
181 oat_data_end,
182 oat_file_end);
183 ASSERT_TRUE(image_header.IsValid());
184
185 char* magic = const_cast<char*>(image_header.GetMagic());
186 strcpy(magic, ""); // bad magic
187 ASSERT_FALSE(image_header.IsValid());
188 strcpy(magic, "art\n000"); // bad version
189 ASSERT_FALSE(image_header.IsValid());
190}
191
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700192} // namespace art