blob: 0babfe3a56dd2afc58697bf7d0510cba49de42d7 [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
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080017#include "image.h"
18
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070019#include <string>
20#include <vector>
21
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022#include "common_compiler_test.h"
Brian Carlstrom51c24672013-07-11 16:00:56 -070023#include "compiler/elf_fixup.h"
24#include "compiler/image_writer.h"
25#include "compiler/oat_writer.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/space/image_space.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070027#include "lock_word.h"
28#include "mirror/object-inl.h"
Brian Carlstrom51c24672013-07-11 16:00:56 -070029#include "signal_catcher.h"
Ian Rogers507dfdd2014-05-15 16:42:40 -070030#include "UniquePtrCompat.h"
buzbeec143c552011-08-20 17:38:58 -070031#include "utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080032#include "vector_output_stream.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070033
Brian Carlstromdb4d5402011-08-09 12:18:28 -070034namespace art {
35
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080036class ImageTest : public CommonCompilerTest {
Ian Rogers10c5b782013-01-10 10:40:53 -080037 protected:
38 virtual void SetUp() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080039 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080040 CommonCompilerTest::SetUp();
Ian Rogers10c5b782013-01-10 10:40:53 -080041 }
Ian Rogers10c5b782013-01-10 10:40:53 -080042};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070043
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070044TEST_F(ImageTest, WriteRead) {
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070045 // Create a generic location tmp file, to be the base of the .art and .oat temporary files.
46 ScratchFile location;
47 ScratchFile image_location(location, ".art");
48
49 std::string image_filename(GetSystemImageFilename(image_location.GetFilename().c_str(),
50 kRuntimeISA));
51 size_t pos = image_filename.rfind('/');
52 CHECK_NE(pos, std::string::npos) << image_filename;
53 std::string image_dir(image_filename, 0, pos);
54 int mkdir_result = mkdir(image_dir.c_str(), 0700);
55 CHECK_EQ(0, mkdir_result) << image_dir;
56 ScratchFile image_file(OS::CreateEmptyFile(image_filename.c_str()));
57
58 std::string oat_filename(image_filename, 0, image_filename.size() - 3);
59 oat_filename += "oat";
60 ScratchFile oat_file(OS::CreateEmptyFile(oat_filename.c_str()));
61
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080063 {
Brian Carlstrom96391602013-06-13 19:49:50 -070064 jobject class_loader = NULL;
65 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers5fe9af72013-11-14 00:17:20 -080066 TimingLogger timings("ImageTest::WriteRead", false, false);
Ian Rogersbd2c19f2013-07-25 17:43:46 -070067 timings.StartSplit("CompileAll");
Ian Rogers3d504072014-03-01 09:16:49 -080068 if (kUsePortableCompiler) {
69 // TODO: we disable this for portable so the test executes in a reasonable amount of time.
70 // We shouldn't need to do this.
71 compiler_options_->SetCompilerFilter(CompilerOptions::kInterpretOnly);
72 }
Brian Carlstrom5f675322013-09-03 22:45:51 -070073 for (const DexFile* dex_file : class_linker->GetBootClassPath()) {
74 dex_file->EnableWrite();
75 }
Ian Rogers3d504072014-03-01 09:16:49 -080076 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
Brian Carlstrom96391602013-06-13 19:49:50 -070077
Brian Carlstrom700c8d32012-11-05 10:42:02 -080078 ScopedObjectAccess soa(Thread::Current());
Brian Carlstromc50d8e12013-07-23 22:35:16 -070079 OatWriter oat_writer(class_linker->GetBootClassPath(),
Ian Rogersca368cb2013-11-15 15:52:08 -080080 0, 0, "", compiler_driver_.get(), &timings);
Brian Carlstromc50d8e12013-07-23 22:35:16 -070081 bool success = compiler_driver_->WriteElf(GetTestAndroidRoot(),
82 !kIsTargetBuild,
83 class_linker->GetBootClassPath(),
Ian Rogers3d504072014-03-01 09:16:49 -080084 &oat_writer,
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070085 oat_file.GetFile());
Brian Carlstromc50d8e12013-07-23 22:35:16 -070086 ASSERT_TRUE(success);
Ian Rogers5fe9af72013-11-14 00:17:20 -080087 timings.EndSplit();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 }
Ian Rogers0571d352011-11-03 19:51:38 -070089 }
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070090 // Workound bug that mcld::Linker::emit closes oat_file by reopening as dup_oat.
91 UniquePtr<File> dup_oat(OS::OpenFileReadWrite(oat_file.GetFilename().c_str()));
92 ASSERT_TRUE(dup_oat.get() != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070093
jeffhao8161c032012-10-31 15:50:00 -070094 const uintptr_t requested_image_base = ART_BASE_ADDRESS;
95 {
Brian Carlstrom96391602013-06-13 19:49:50 -070096 ImageWriter writer(*compiler_driver_.get());
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070097 bool success_image = writer.Write(image_file.GetFilename(), requested_image_base,
98 dup_oat->GetPath(), dup_oat->GetPath());
jeffhao8161c032012-10-31 15:50:00 -070099 ASSERT_TRUE(success_image);
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700100 bool success_fixup = ElfFixup::Fixup(dup_oat.get(), writer.GetOatDataBegin());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800101 ASSERT_TRUE(success_fixup);
jeffhao8161c032012-10-31 15:50:00 -0700102 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700103
104 {
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700105 UniquePtr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
Elliott Hughes90a33692011-08-30 13:27:07 -0700106 ASSERT_TRUE(file.get() != NULL);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700107 ImageHeader image_header;
108 file->ReadFully(&image_header, sizeof(image_header));
109 ASSERT_TRUE(image_header.IsValid());
Mathieu Chartier31e89252013-08-28 11:29:12 -0700110 ASSERT_GE(image_header.GetImageBitmapOffset(), sizeof(image_header));
111 ASSERT_NE(0U, image_header.GetImageBitmapSize());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700112
Ian Rogers1d54e732013-05-02 21:10:01 -0700113 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700114 ASSERT_TRUE(!heap->GetContinuousSpaces().empty());
115 gc::space::ContinuousSpace* space = heap->GetNonMovingSpace();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700116 ASSERT_FALSE(space->IsImageSpace());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700117 ASSERT_TRUE(space != NULL);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700118 ASSERT_TRUE(space->IsMallocSpace());
Elliott Hughes76160052012-12-12 16:31:20 -0800119 ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->GetLength()));
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700120 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700121
Brian Carlstrom96391602013-06-13 19:49:50 -0700122 ASSERT_TRUE(compiler_driver_->GetImageClasses() != NULL);
123 CompilerDriver::DescriptorSet image_classes(*compiler_driver_->GetImageClasses());
124
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700125 // Need to delete the compiler since it has worker threads which are attached to runtime.
Ian Rogers1212a022013-03-04 10:48:41 -0800126 compiler_driver_.reset();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700127
Ian Rogers10c5b782013-01-10 10:40:53 -0800128 // Tear down old runtime before making a new one, clearing out misc state.
129 runtime_.reset();
Brian Carlstroma004aa92012-02-08 18:05:09 -0800130 java_lang_dex_file_ = NULL;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700131
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700132 std::string error_msg;
133 UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName().c_str(),
134 GetLibCoreDexFileName().c_str(),
135 &error_msg));
136 ASSERT_TRUE(dex.get() != nullptr) << error_msg;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700137
Ian Rogers10c5b782013-01-10 10:40:53 -0800138 // Remove the reservation of the memory for use to load the image.
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800139 UnreserveImageSpace();
Ian Rogers10c5b782013-01-10 10:40:53 -0800140
Brian Carlstrom8a436592011-08-15 21:27:23 -0700141 Runtime::Options options;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700142 std::string image("-Ximage:");
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700143 image.append(image_location.GetFilename());
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700144 options.push_back(std::make_pair(image.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom8a436592011-08-15 21:27:23 -0700145
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 if (!Runtime::Create(options, false)) {
147 LOG(FATAL) << "Failed to create runtime";
148 return;
149 }
150 runtime_.reset(Runtime::Current());
151 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
152 // give it away now and then switch to a more managable ScopedObjectAccess.
153 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
154 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes90a33692011-08-30 13:27:07 -0700155 ASSERT_TRUE(runtime_.get() != NULL);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700156 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700157
Ian Rogers1d54e732013-05-02 21:10:01 -0700158 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700159 ASSERT_TRUE(heap->HasImageSpace());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700160 ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700161
Ian Rogers1d54e732013-05-02 21:10:01 -0700162 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Mathieu Chartier31e89252013-08-28 11:29:12 -0700163 image_space->VerifyImageAllocations();
Ian Rogers30fab402012-01-23 15:43:46 -0800164 byte* image_begin = image_space->Begin();
165 byte* image_end = image_space->End();
166 CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800167 for (size_t i = 0; i < dex->NumClassDefs(); ++i) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700168 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700169 const char* descriptor = dex->GetClassDescriptor(class_def);
Ian Rogers98379392014-02-24 16:53:16 -0800170 mirror::Class* klass = class_linker_->FindSystemClass(soa.Self(), descriptor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800171 EXPECT_TRUE(klass != nullptr) << descriptor;
Brian Carlstrom96391602013-06-13 19:49:50 -0700172 if (image_classes.find(descriptor) != image_classes.end()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800173 // Image classes should be located inside the image.
174 EXPECT_LT(image_begin, reinterpret_cast<byte*>(klass)) << descriptor;
Brian Carlstrom96391602013-06-13 19:49:50 -0700175 EXPECT_LT(reinterpret_cast<byte*>(klass), image_end) << descriptor;
176 } else {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800177 EXPECT_TRUE(reinterpret_cast<byte*>(klass) >= image_end ||
178 reinterpret_cast<byte*>(klass) < image_begin) << descriptor;
Brian Carlstrom96391602013-06-13 19:49:50 -0700179 }
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700180 EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false)));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700181 }
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700182
183 image_file.Unlink();
184 oat_file.Unlink();
185 int rmdir_result = rmdir(image_dir.c_str());
186 CHECK_EQ(0, rmdir_result);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700187}
188
Brian Carlstrom179486a2013-09-03 11:51:42 -0700189TEST_F(ImageTest, ImageHeaderIsValid) {
190 uint32_t image_begin = ART_BASE_ADDRESS;
191 uint32_t image_size_ = 16 * KB;
192 uint32_t image_bitmap_offset = 0;
193 uint32_t image_bitmap_size = 0;
194 uint32_t image_roots = ART_BASE_ADDRESS + (1 * KB);
195 uint32_t oat_checksum = 0;
196 uint32_t oat_file_begin = ART_BASE_ADDRESS + (4 * KB); // page aligned
197 uint32_t oat_data_begin = ART_BASE_ADDRESS + (8 * KB); // page aligned
198 uint32_t oat_data_end = ART_BASE_ADDRESS + (9 * KB);
199 uint32_t oat_file_end = ART_BASE_ADDRESS + (10 * KB);
200 ImageHeader image_header(image_begin,
201 image_size_,
202 image_bitmap_offset,
203 image_bitmap_size,
204 image_roots,
205 oat_checksum,
206 oat_file_begin,
207 oat_data_begin,
208 oat_data_end,
209 oat_file_end);
210 ASSERT_TRUE(image_header.IsValid());
211
212 char* magic = const_cast<char*>(image_header.GetMagic());
213 strcpy(magic, ""); // bad magic
214 ASSERT_FALSE(image_header.IsValid());
215 strcpy(magic, "art\n000"); // bad version
216 ASSERT_FALSE(image_header.IsValid());
217}
218
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700219} // namespace art