blob: c71cc977f53251ea574912590724138d5d464126 [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"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070026#include "lock_word.h"
27#include "mirror/object-inl.h"
Brian Carlstrom51c24672013-07-11 16:00:56 -070028#include "signal_catcher.h"
Ian Rogers10c5b782013-01-10 10:40:53 -080029#include "UniquePtr.h"
buzbeec143c552011-08-20 17:38:58 -070030#include "utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031#include "vector_output_stream.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070032
Brian Carlstromdb4d5402011-08-09 12:18:28 -070033namespace art {
34
Ian Rogers10c5b782013-01-10 10:40:53 -080035class ImageTest : public CommonTest {
Ian Rogers10c5b782013-01-10 10:40:53 -080036 protected:
37 virtual void SetUp() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080038 ReserveImageSpace();
Ian Rogers10c5b782013-01-10 10:40:53 -080039 CommonTest::SetUp();
40 }
Ian Rogers10c5b782013-01-10 10:40:53 -080041};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070042
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070043TEST_F(ImageTest, WriteRead) {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080044 ScratchFile tmp_elf;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045 {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080046 {
Brian Carlstrom96391602013-06-13 19:49:50 -070047 jobject class_loader = NULL;
48 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers5fe9af72013-11-14 00:17:20 -080049 TimingLogger timings("ImageTest::WriteRead", false, false);
Ian Rogersbd2c19f2013-07-25 17:43:46 -070050 timings.StartSplit("CompileAll");
Ian Rogerse9869532013-08-03 14:56:04 -070051#if defined(ART_USE_PORTABLE_COMPILER)
52 // TODO: we disable this for portable so the test executes in a reasonable amount of time.
53 // We shouldn't need to do this.
Ian Rogers36e58b82013-08-09 09:09:41 -070054 runtime_->SetCompilerFilter(Runtime::kInterpretOnly);
Ian Rogerse9869532013-08-03 14:56:04 -070055#endif
Brian Carlstrom5f675322013-09-03 22:45:51 -070056 for (const DexFile* dex_file : class_linker->GetBootClassPath()) {
57 dex_file->EnableWrite();
58 }
Brian Carlstrom45602482013-07-21 22:07:55 -070059 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), timings);
Brian Carlstrom96391602013-06-13 19:49:50 -070060
Brian Carlstrom700c8d32012-11-05 10:42:02 -080061 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromc50d8e12013-07-23 22:35:16 -070062 OatWriter oat_writer(class_linker->GetBootClassPath(),
Ian Rogersca368cb2013-11-15 15:52:08 -080063 0, 0, "", compiler_driver_.get(), &timings);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070064 bool success = compiler_driver_->WriteElf(GetTestAndroidRoot(),
65 !kIsTargetBuild,
66 class_linker->GetBootClassPath(),
67 oat_writer,
68 tmp_elf.GetFile());
69 ASSERT_TRUE(success);
Ian Rogers5fe9af72013-11-14 00:17:20 -080070 timings.EndSplit();
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.
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070074 UniquePtr<File> tmp_oat(OS::OpenFileReadWrite(tmp_elf.GetFilename().c_str()));
Brian Carlstrom700c8d32012-11-05 10:42:02 -080075 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 {
Brian Carlstrom96391602013-06-13 19:49:50 -070080 ImageWriter writer(*compiler_driver_.get());
jeffhao8161c032012-10-31 15:50:00 -070081 bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base,
Brian Carlstrom96391602013-06-13 19:49:50 -070082 tmp_oat->GetPath(), tmp_oat->GetPath());
jeffhao8161c032012-10-31 15:50:00 -070083 ASSERT_TRUE(success_image);
Brian Carlstrom51c24672013-07-11 16:00:56 -070084 bool success_fixup = ElfFixup::Fixup(tmp_oat.get(), writer.GetOatDataBegin());
Brian Carlstrom700c8d32012-11-05 10:42:02 -080085 ASSERT_TRUE(success_fixup);
jeffhao8161c032012-10-31 15:50:00 -070086 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070087
88 {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -070089 UniquePtr<File> file(OS::OpenFileForReading(tmp_image.GetFilename().c_str()));
Elliott Hughes90a33692011-08-30 13:27:07 -070090 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070091 ImageHeader image_header;
92 file->ReadFully(&image_header, sizeof(image_header));
93 ASSERT_TRUE(image_header.IsValid());
Mathieu Chartier31e89252013-08-28 11:29:12 -070094 ASSERT_GE(image_header.GetImageBitmapOffset(), sizeof(image_header));
95 ASSERT_NE(0U, image_header.GetImageBitmapSize());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070096
Ian Rogers1d54e732013-05-02 21:10:01 -070097 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -070098 ASSERT_TRUE(!heap->GetContinuousSpaces().empty());
99 gc::space::ContinuousSpace* space = heap->GetNonMovingSpace();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700100 ASSERT_FALSE(space->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700101 ASSERT_TRUE(space != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700102 ASSERT_TRUE(space->IsMallocSpace());
Elliott Hughes76160052012-12-12 16:31:20 -0800103 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->GetLength()));
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700104 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700105
Brian Carlstrom96391602013-06-13 19:49:50 -0700106 ASSERT_TRUE(compiler_driver_->GetImageClasses() != NULL);
107 CompilerDriver::DescriptorSet image_classes(*compiler_driver_->GetImageClasses());
108
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700109 // Need to delete the compiler since it has worker threads which are attached to runtime.
Ian Rogers1212a022013-03-04 10:48:41 -0800110 compiler_driver_.reset();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700111
Ian Rogers10c5b782013-01-10 10:40:53 -0800112 // Tear down old runtime before making a new one, clearing out misc state.
113 runtime_.reset();
Brian Carlstroma004aa92012-02-08 18:05:09 -0800114 java_lang_dex_file_ = NULL;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700115
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700116 std::string error_msg;
117 UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName().c_str(),
118 GetLibCoreDexFileName().c_str(),
119 &error_msg));
120 ASSERT_TRUE(dex.get() != nullptr) << error_msg;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700121
Ian Rogers10c5b782013-01-10 10:40:53 -0800122 // Remove the reservation of the memory for use to load the image.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800123 UnreserveImageSpace();
Ian Rogers10c5b782013-01-10 10:40:53 -0800124
Brian Carlstrom8a436592011-08-15 21:27:23 -0700125 Runtime::Options options;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700126 std::string image("-Ximage:");
127 image.append(tmp_image.GetFilename());
128 options.push_back(std::make_pair(image.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom8a436592011-08-15 21:27:23 -0700129
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 if (!Runtime::Create(options, false)) {
131 LOG(FATAL) << "Failed to create runtime";
132 return;
133 }
134 runtime_.reset(Runtime::Current());
135 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
136 // give it away now and then switch to a more managable ScopedObjectAccess.
137 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
138 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes90a33692011-08-30 13:27:07 -0700139 ASSERT_TRUE(runtime_.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700140 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700141
Ian Rogers1d54e732013-05-02 21:10:01 -0700142 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700143 ASSERT_TRUE(heap->HasImageSpace());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700144 ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700145
Ian Rogers1d54e732013-05-02 21:10:01 -0700146 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Mathieu Chartier31e89252013-08-28 11:29:12 -0700147 image_space->VerifyImageAllocations();
Ian Rogers30fab402012-01-23 15:43:46 -0800148 byte* image_begin = image_space->Begin();
149 byte* image_end = image_space->End();
150 CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800151 for (size_t i = 0; i < dex->NumClassDefs(); ++i) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700152 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700153 const char* descriptor = dex->GetClassDescriptor(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154 mirror::Class* klass = class_linker_->FindSystemClass(descriptor);
Brian Carlstroma663ea52011-08-19 23:33:41 -0700155 EXPECT_TRUE(klass != NULL) << descriptor;
Ian Rogers30fab402012-01-23 15:43:46 -0800156 EXPECT_LT(image_begin, reinterpret_cast<byte*>(klass)) << descriptor;
Brian Carlstrom96391602013-06-13 19:49:50 -0700157 if (image_classes.find(descriptor) != image_classes.end()) {
158 // image classes should be located before the end of the image.
159 EXPECT_LT(reinterpret_cast<byte*>(klass), image_end) << descriptor;
160 } else {
161 // non image classes should be in a space after the image.
162 EXPECT_GT(reinterpret_cast<byte*>(klass), image_end) << descriptor;
163 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700164 EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord()));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700165 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700166}
167
Brian Carlstrom179486a2013-09-03 11:51:42 -0700168TEST_F(ImageTest, ImageHeaderIsValid) {
169 uint32_t image_begin = ART_BASE_ADDRESS;
170 uint32_t image_size_ = 16 * KB;
171 uint32_t image_bitmap_offset = 0;
172 uint32_t image_bitmap_size = 0;
173 uint32_t image_roots = ART_BASE_ADDRESS + (1 * KB);
174 uint32_t oat_checksum = 0;
175 uint32_t oat_file_begin = ART_BASE_ADDRESS + (4 * KB); // page aligned
176 uint32_t oat_data_begin = ART_BASE_ADDRESS + (8 * KB); // page aligned
177 uint32_t oat_data_end = ART_BASE_ADDRESS + (9 * KB);
178 uint32_t oat_file_end = ART_BASE_ADDRESS + (10 * KB);
179 ImageHeader image_header(image_begin,
180 image_size_,
181 image_bitmap_offset,
182 image_bitmap_size,
183 image_roots,
184 oat_checksum,
185 oat_file_begin,
186 oat_data_begin,
187 oat_data_end,
188 oat_file_end);
189 ASSERT_TRUE(image_header.IsValid());
190
191 char* magic = const_cast<char*>(image_header.GetMagic());
192 strcpy(magic, ""); // bad magic
193 ASSERT_FALSE(image_header.IsValid());
194 strcpy(magic, "art\n000"); // bad version
195 ASSERT_FALSE(image_header.IsValid());
196}
197
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700198} // namespace art