blob: 5f4a92299bb7327cfaf82159c12a0d683388a2ad [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 Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020#include <string>
21#include <vector>
22
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "base/unix_file/fd_file.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010024#include "class_linker-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080025#include "common_compiler_test.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000026#include "dwarf/method_debug_info.h"
Tong Shen62d1ca32014-09-03 17:24:56 -070027#include "elf_writer.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000028#include "elf_writer_quick.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/space/image_space.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "image_writer.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070031#include "lock_word.h"
32#include "mirror/object-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070033#include "oat_writer.h"
34#include "scoped_thread_state_change.h"
Brian Carlstrom51c24672013-07-11 16:00:56 -070035#include "signal_catcher.h"
buzbeec143c552011-08-20 17:38:58 -070036#include "utils.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080037#include "vector_output_stream.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -070038
Brian Carlstromdb4d5402011-08-09 12:18:28 -070039namespace art {
40
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080041class ImageTest : public CommonCompilerTest {
Ian Rogers10c5b782013-01-10 10:40:53 -080042 protected:
43 virtual void SetUp() {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080044 ReserveImageSpace();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080045 CommonCompilerTest::SetUp();
Ian Rogers10c5b782013-01-10 10:40:53 -080046 }
Ian Rogers10c5b782013-01-10 10:40:53 -080047};
Brian Carlstromdb4d5402011-08-09 12:18:28 -070048
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070049TEST_F(ImageTest, WriteRead) {
Roland Levillainbbcc01a2015-06-30 14:16:48 +010050 TEST_DISABLED_FOR_NON_PIC_COMPILING_WITH_OPTIMIZING();
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070051 // Create a generic location tmp file, to be the base of the .art and .oat temporary files.
52 ScratchFile location;
53 ScratchFile image_location(location, ".art");
54
55 std::string image_filename(GetSystemImageFilename(image_location.GetFilename().c_str(),
56 kRuntimeISA));
57 size_t pos = image_filename.rfind('/');
58 CHECK_NE(pos, std::string::npos) << image_filename;
59 std::string image_dir(image_filename, 0, pos);
60 int mkdir_result = mkdir(image_dir.c_str(), 0700);
61 CHECK_EQ(0, mkdir_result) << image_dir;
62 ScratchFile image_file(OS::CreateEmptyFile(image_filename.c_str()));
63
64 std::string oat_filename(image_filename, 0, image_filename.size() - 3);
65 oat_filename += "oat";
66 ScratchFile oat_file(OS::CreateEmptyFile(oat_filename.c_str()));
67
Vladimir Markof4da6752014-08-01 19:04:18 +010068 const uintptr_t requested_image_base = ART_BASE_ADDRESS;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080069 std::unique_ptr<ImageWriter> writer(new ImageWriter(*compiler_driver_,
70 requested_image_base,
71 /*compile_pic*/false,
72 /*compile_app_image*/false));
Igor Murashkin46774762014-10-22 11:37:02 -070073 // TODO: compile_pic should be a test argument.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 {
Brian Carlstrom700c8d32012-11-05 10:42:02 -080075 {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070076 jobject class_loader = nullptr;
Brian Carlstrom96391602013-06-13 19:49:50 -070077 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers5fe9af72013-11-14 00:17:20 -080078 TimingLogger timings("ImageTest::WriteRead", false, false);
Mathieu Chartierf5997b42014-06-20 10:37:54 -070079 TimingLogger::ScopedTiming t("CompileAll", &timings);
Brian Carlstrom5f675322013-09-03 22:45:51 -070080 for (const DexFile* dex_file : class_linker->GetBootClassPath()) {
81 dex_file->EnableWrite();
82 }
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000083 compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath());
Ian Rogers3d504072014-03-01 09:16:49 -080084 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings);
Brian Carlstrom96391602013-06-13 19:49:50 -070085
Mathieu Chartierf5997b42014-06-20 10:37:54 -070086 t.NewTiming("WriteElf");
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070087 SafeMap<std::string, std::string> key_value_store;
Mathieu Chartierda5b28a2015-11-05 08:03:47 -080088 OatWriter oat_writer(class_linker->GetBootClassPath(),
89 0,
90 0,
91 0,
92 compiler_driver_.get(),
93 writer.get(),
94 /*compiling_boot_image*/true,
95 &timings,
96 &key_value_store);
Vladimir Marko10c13562015-11-25 14:33:36 +000097 std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick(
98 compiler_driver_->GetInstructionSet(),
99 &compiler_driver_->GetCompilerOptions(),
100 oat_file.GetFile());
101 bool success = writer->PrepareImageAddressSpace();
102 ASSERT_TRUE(success);
103
104 elf_writer->Start();
105
106 OutputStream* rodata = elf_writer->StartRoData();
107 bool rodata_ok = oat_writer.WriteRodata(rodata);
108 ASSERT_TRUE(rodata_ok);
109 elf_writer->EndRoData(rodata);
110
111 OutputStream* text = elf_writer->StartText();
112 bool text_ok = oat_writer.WriteCode(text);
113 ASSERT_TRUE(text_ok);
114 elf_writer->EndText(text);
115
116 elf_writer->SetBssSize(oat_writer.GetBssSize());
117
118 elf_writer->WriteDynamicSection();
119
120 ArrayRef<const dwarf::MethodDebugInfo> method_infos(oat_writer.GetMethodDebugInfo());
121 elf_writer->WriteDebugInfo(method_infos);
122
123 ArrayRef<const uintptr_t> patch_locations(oat_writer.GetAbsolutePatchLocations());
124 elf_writer->WritePatchLocations(patch_locations);
125
126 success = elf_writer->End();
127
Brian Carlstromc50d8e12013-07-23 22:35:16 -0700128 ASSERT_TRUE(success);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 }
Ian Rogers0571d352011-11-03 19:51:38 -0700130 }
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700131 // Workound bug that mcld::Linker::emit closes oat_file by reopening as dup_oat.
Ian Rogers700a4022014-05-19 16:49:03 -0700132 std::unique_ptr<File> dup_oat(OS::OpenFileReadWrite(oat_file.GetFilename().c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700133 ASSERT_TRUE(dup_oat.get() != nullptr);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700134
jeffhao8161c032012-10-31 15:50:00 -0700135 {
Mathieu Chartiera90c7722015-10-29 15:41:36 -0700136 bool success_image = writer->Write(kInvalidImageFd,
137 image_file.GetFilename(),
138 dup_oat->GetPath(),
139 dup_oat->GetPath());
jeffhao8161c032012-10-31 15:50:00 -0700140 ASSERT_TRUE(success_image);
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700141 bool success_fixup = ElfWriter::Fixup(dup_oat.get(), writer->GetOatDataBegin());
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800142 ASSERT_TRUE(success_fixup);
Andreas Gampe4303ba92014-11-06 01:00:46 -0800143
144 ASSERT_EQ(dup_oat->FlushCloseOrErase(), 0) << "Could not flush and close oat file "
145 << oat_file.GetFilename();
jeffhao8161c032012-10-31 15:50:00 -0700146 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700147
Mathieu Chartier68c868e2015-06-01 16:33:53 -0700148 uint64_t image_file_size;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700149 {
Ian Rogers700a4022014-05-19 16:49:03 -0700150 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700151 ASSERT_TRUE(file.get() != nullptr);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700152 ImageHeader image_header;
Andreas Gampe4303ba92014-11-06 01:00:46 -0800153 ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700154 ASSERT_TRUE(image_header.IsValid());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700155 const auto& bitmap_section = image_header.GetImageSection(ImageHeader::kSectionImageBitmap);
156 ASSERT_GE(bitmap_section.Offset(), sizeof(image_header));
157 ASSERT_NE(0U, bitmap_section.Size());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700158
Ian Rogers1d54e732013-05-02 21:10:01 -0700159 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700160 ASSERT_TRUE(!heap->GetContinuousSpaces().empty());
161 gc::space::ContinuousSpace* space = heap->GetNonMovingSpace();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700162 ASSERT_FALSE(space->IsImageSpace());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700163 ASSERT_TRUE(space != nullptr);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700164 ASSERT_TRUE(space->IsMallocSpace());
Mathieu Chartier68c868e2015-06-01 16:33:53 -0700165
166 image_file_size = file->GetLength();
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700167 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700168
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700169 ASSERT_TRUE(compiler_driver_->GetImageClasses() != nullptr);
Andreas Gampeb1fcead2015-04-20 18:53:51 -0700170 std::unordered_set<std::string> image_classes(*compiler_driver_->GetImageClasses());
Brian Carlstrom96391602013-06-13 19:49:50 -0700171
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700172 // Need to delete the compiler since it has worker threads which are attached to runtime.
Ian Rogers1212a022013-03-04 10:48:41 -0800173 compiler_driver_.reset();
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700174
Ian Rogers10c5b782013-01-10 10:40:53 -0800175 // Tear down old runtime before making a new one, clearing out misc state.
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700176
177 // Remove the reservation of the memory for use to load the image.
178 // Need to do this before we reset the runtime.
179 UnreserveImageSpace();
180 writer.reset(nullptr);
181
Ian Rogers10c5b782013-01-10 10:40:53 -0800182 runtime_.reset();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700183 java_lang_dex_file_ = nullptr;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700184
Mathieu Chartier6e88ef62014-10-14 15:01:24 -0700185 MemMap::Init();
Andreas Gampe833a4852014-05-21 18:46:59 -0700186 std::unique_ptr<const DexFile> dex(LoadExpectSingleDexFile(GetLibCoreDexFileName().c_str()));
Brian Carlstrom8a436592011-08-15 21:27:23 -0700187
Ian Rogerse63db272014-07-15 15:36:11 -0700188 RuntimeOptions options;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700189 std::string image("-Ximage:");
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700190 image.append(image_location.GetFilename());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700191 options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr)));
Alex Light6e183f22014-07-18 14:57:04 -0700192 // By default the compiler this creates will not include patch information.
193 options.push_back(std::make_pair("-Xnorelocate", nullptr));
Brian Carlstrom8a436592011-08-15 21:27:23 -0700194
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700195 if (!Runtime::Create(options, false)) {
196 LOG(FATAL) << "Failed to create runtime";
197 return;
198 }
199 runtime_.reset(Runtime::Current());
200 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
201 // give it away now and then switch to a more managable ScopedObjectAccess.
202 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
203 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700204 ASSERT_TRUE(runtime_.get() != nullptr);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700205 class_linker_ = runtime_->GetClassLinker();
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700206
Ian Rogers1d54e732013-05-02 21:10:01 -0700207 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700208 ASSERT_TRUE(heap->HasImageSpace());
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700209 ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700210
Mathieu Chartier073b16c2015-11-10 14:13:23 -0800211 gc::space::ImageSpace* image_space = heap->GetBootImageSpace();
Mathieu Chartier68c868e2015-06-01 16:33:53 -0700212 ASSERT_TRUE(image_space != nullptr);
213 ASSERT_LE(image_space->Size(), image_file_size);
214
Mathieu Chartier31e89252013-08-28 11:29:12 -0700215 image_space->VerifyImageAllocations();
Ian Rogers13735952014-10-08 12:43:28 -0700216 uint8_t* image_begin = image_space->Begin();
217 uint8_t* image_end = image_space->End();
Ian Rogers30fab402012-01-23 15:43:46 -0800218 CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800219 for (size_t i = 0; i < dex->NumClassDefs(); ++i) {
Brian Carlstromd2fbb2b2011-08-23 11:57:08 -0700220 const DexFile::ClassDef& class_def = dex->GetClassDef(i);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700221 const char* descriptor = dex->GetClassDescriptor(class_def);
Ian Rogers98379392014-02-24 16:53:16 -0800222 mirror::Class* klass = class_linker_->FindSystemClass(soa.Self(), descriptor);
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800223 EXPECT_TRUE(klass != nullptr) << descriptor;
Brian Carlstrom96391602013-06-13 19:49:50 -0700224 if (image_classes.find(descriptor) != image_classes.end()) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800225 // Image classes should be located inside the image.
Ian Rogers13735952014-10-08 12:43:28 -0700226 EXPECT_LT(image_begin, reinterpret_cast<uint8_t*>(klass)) << descriptor;
227 EXPECT_LT(reinterpret_cast<uint8_t*>(klass), image_end) << descriptor;
Brian Carlstrom96391602013-06-13 19:49:50 -0700228 } else {
Ian Rogers13735952014-10-08 12:43:28 -0700229 EXPECT_TRUE(reinterpret_cast<uint8_t*>(klass) >= image_end ||
230 reinterpret_cast<uint8_t*>(klass) < image_begin) << descriptor;
Brian Carlstrom96391602013-06-13 19:49:50 -0700231 }
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700232 EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false)));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700233 }
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700234
235 image_file.Unlink();
236 oat_file.Unlink();
237 int rmdir_result = rmdir(image_dir.c_str());
238 CHECK_EQ(0, rmdir_result);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700239}
240
Brian Carlstrom179486a2013-09-03 11:51:42 -0700241TEST_F(ImageTest, ImageHeaderIsValid) {
242 uint32_t image_begin = ART_BASE_ADDRESS;
243 uint32_t image_size_ = 16 * KB;
Brian Carlstrom179486a2013-09-03 11:51:42 -0700244 uint32_t image_roots = ART_BASE_ADDRESS + (1 * KB);
245 uint32_t oat_checksum = 0;
246 uint32_t oat_file_begin = ART_BASE_ADDRESS + (4 * KB); // page aligned
247 uint32_t oat_data_begin = ART_BASE_ADDRESS + (8 * KB); // page aligned
248 uint32_t oat_data_end = ART_BASE_ADDRESS + (9 * KB);
249 uint32_t oat_file_end = ART_BASE_ADDRESS + (10 * KB);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 ImageSection sections[ImageHeader::kSectionCount];
Brian Carlstrom179486a2013-09-03 11:51:42 -0700251 ImageHeader image_header(image_begin,
252 image_size_,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700253 sections,
Brian Carlstrom179486a2013-09-03 11:51:42 -0700254 image_roots,
255 oat_checksum,
256 oat_file_begin,
257 oat_data_begin,
258 oat_data_end,
Igor Murashkin46774762014-10-22 11:37:02 -0700259 oat_file_end,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700260 sizeof(void*),
Igor Murashkin46774762014-10-22 11:37:02 -0700261 /*compile_pic*/false);
Brian Carlstrom179486a2013-09-03 11:51:42 -0700262 ASSERT_TRUE(image_header.IsValid());
263
264 char* magic = const_cast<char*>(image_header.GetMagic());
265 strcpy(magic, ""); // bad magic
266 ASSERT_FALSE(image_header.IsValid());
267 strcpy(magic, "art\n000"); // bad version
268 ASSERT_FALSE(image_header.IsValid());
269}
270
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700271} // namespace art