Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 17 | #include "image.h" |
| 18 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 19 | #include <memory> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include "base/unix_file/fd_file.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 24 | #include "class_linker-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 25 | #include "common_compiler_test.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 26 | #include "debug/method_debug_info.h" |
Andreas Gampe | 3f41a01 | 2016-02-18 16:53:41 -0800 | [diff] [blame] | 27 | #include "driver/compiler_options.h" |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 28 | #include "elf_writer.h" |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 29 | #include "elf_writer_quick.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 30 | #include "gc/space/image_space.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 31 | #include "image_writer.h" |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 32 | #include "linker/multi_oat_relative_patcher.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 33 | #include "lock_word.h" |
| 34 | #include "mirror/object-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 35 | #include "oat_writer.h" |
| 36 | #include "scoped_thread_state_change.h" |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 37 | #include "signal_catcher.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 38 | #include "utils.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 39 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 40 | namespace art { |
| 41 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 42 | class ImageTest : public CommonCompilerTest { |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 43 | protected: |
| 44 | virtual void SetUp() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 45 | ReserveImageSpace(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 46 | CommonCompilerTest::SetUp(); |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 47 | } |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 48 | void TestWriteRead(ImageHeader::StorageMode storage_mode); |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 49 | }; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 50 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 51 | void ImageTest::TestWriteRead(ImageHeader::StorageMode storage_mode) { |
Andreas Gampe | 3f41a01 | 2016-02-18 16:53:41 -0800 | [diff] [blame] | 52 | CreateCompilerDriver(Compiler::kOptimizing, kRuntimeISA, kIsTargetBuild ? 2U : 16U); |
| 53 | |
| 54 | // Set inline filter values. |
| 55 | compiler_options_->SetInlineDepthLimit(CompilerOptions::kDefaultInlineDepthLimit); |
| 56 | compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits); |
| 57 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 58 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 59 | // Enable write for dex2dex. |
| 60 | for (const DexFile* dex_file : class_linker->GetBootClassPath()) { |
| 61 | dex_file->EnableWrite(); |
| 62 | } |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 63 | // Create a generic location tmp file, to be the base of the .art and .oat temporary files. |
| 64 | ScratchFile location; |
| 65 | ScratchFile image_location(location, ".art"); |
| 66 | |
| 67 | std::string image_filename(GetSystemImageFilename(image_location.GetFilename().c_str(), |
| 68 | kRuntimeISA)); |
| 69 | size_t pos = image_filename.rfind('/'); |
| 70 | CHECK_NE(pos, std::string::npos) << image_filename; |
| 71 | std::string image_dir(image_filename, 0, pos); |
| 72 | int mkdir_result = mkdir(image_dir.c_str(), 0700); |
| 73 | CHECK_EQ(0, mkdir_result) << image_dir; |
| 74 | ScratchFile image_file(OS::CreateEmptyFile(image_filename.c_str())); |
| 75 | |
| 76 | std::string oat_filename(image_filename, 0, image_filename.size() - 3); |
| 77 | oat_filename += "oat"; |
| 78 | ScratchFile oat_file(OS::CreateEmptyFile(oat_filename.c_str())); |
| 79 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 80 | const uintptr_t requested_image_base = ART_BASE_ADDRESS; |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 81 | std::unordered_map<const DexFile*, size_t> dex_file_to_oat_index_map; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 82 | std::vector<const char*> oat_filename_vector(1, oat_filename.c_str()); |
| 83 | for (const DexFile* dex_file : class_linker->GetBootClassPath()) { |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 84 | dex_file_to_oat_index_map.emplace(dex_file, 0); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 85 | } |
Mathieu Chartier | da5b28a | 2015-11-05 08:03:47 -0800 | [diff] [blame] | 86 | std::unique_ptr<ImageWriter> writer(new ImageWriter(*compiler_driver_, |
| 87 | requested_image_base, |
| 88 | /*compile_pic*/false, |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 89 | /*compile_app_image*/false, |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 90 | storage_mode, |
| 91 | oat_filename_vector, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 92 | dex_file_to_oat_index_map)); |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 93 | // TODO: compile_pic should be a test argument. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 94 | { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 95 | { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 96 | jobject class_loader = nullptr; |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 97 | TimingLogger timings("ImageTest::WriteRead", false, false); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 98 | TimingLogger::ScopedTiming t("CompileAll", &timings); |
Vladimir Marko | d1eaf0d | 2015-10-29 12:18:29 +0000 | [diff] [blame] | 99 | compiler_driver_->SetDexFilesForOatFile(class_linker->GetBootClassPath()); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 100 | compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings); |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 101 | |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 102 | t.NewTiming("WriteElf"); |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 103 | SafeMap<std::string, std::string> key_value_store; |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 104 | const std::vector<const DexFile*>& dex_files = class_linker->GetBootClassPath(); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 105 | std::unique_ptr<ElfWriter> elf_writer = CreateElfWriterQuick( |
| 106 | compiler_driver_->GetInstructionSet(), |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 107 | compiler_driver_->GetInstructionSetFeatures(), |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 108 | &compiler_driver_->GetCompilerOptions(), |
| 109 | oat_file.GetFile()); |
Vladimir Marko | 919f553 | 2016-01-20 19:13:01 +0000 | [diff] [blame] | 110 | elf_writer->Start(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 111 | OatWriter oat_writer(/*compiling_boot_image*/true, &timings); |
Vladimir Marko | 919f553 | 2016-01-20 19:13:01 +0000 | [diff] [blame] | 112 | OutputStream* rodata = elf_writer->StartRoData(); |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 113 | for (const DexFile* dex_file : dex_files) { |
| 114 | ArrayRef<const uint8_t> raw_dex_file( |
| 115 | reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()), |
| 116 | dex_file->GetHeader().file_size_); |
| 117 | oat_writer.AddRawDexFileSource(raw_dex_file, |
| 118 | dex_file->GetLocation().c_str(), |
| 119 | dex_file->GetLocationChecksum()); |
| 120 | } |
| 121 | std::unique_ptr<MemMap> opened_dex_files_map; |
| 122 | std::vector<std::unique_ptr<const DexFile>> opened_dex_files; |
| 123 | bool dex_files_ok = oat_writer.WriteAndOpenDexFiles( |
| 124 | rodata, |
| 125 | oat_file.GetFile(), |
| 126 | compiler_driver_->GetInstructionSet(), |
| 127 | compiler_driver_->GetInstructionSetFeatures(), |
| 128 | &key_value_store, |
Andreas Gampe | 3a2bd29 | 2016-01-26 17:23:47 -0800 | [diff] [blame] | 129 | /* verify */ false, // Dex files may be dex-to-dex-ed, don't verify. |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 130 | &opened_dex_files_map, |
| 131 | &opened_dex_files); |
| 132 | ASSERT_TRUE(dex_files_ok); |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 133 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 134 | bool image_space_ok = writer->PrepareImageAddressSpace(); |
| 135 | ASSERT_TRUE(image_space_ok); |
| 136 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 137 | linker::MultiOatRelativePatcher patcher(compiler_driver_->GetInstructionSet(), |
| 138 | instruction_set_features_.get()); |
| 139 | oat_writer.PrepareLayout(compiler_driver_.get(), writer.get(), dex_files, &patcher); |
| 140 | size_t rodata_size = oat_writer.GetOatHeader().GetExecutableOffset(); |
| 141 | size_t text_size = oat_writer.GetSize() - rodata_size; |
| 142 | elf_writer->SetLoadedSectionSizes(rodata_size, text_size, oat_writer.GetBssSize()); |
| 143 | |
| 144 | writer->UpdateOatFileLayout(/* oat_index */ 0u, |
| 145 | elf_writer->GetLoadedSize(), |
| 146 | oat_writer.GetOatDataOffset(), |
| 147 | oat_writer.GetSize()); |
| 148 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 149 | bool rodata_ok = oat_writer.WriteRodata(rodata); |
| 150 | ASSERT_TRUE(rodata_ok); |
| 151 | elf_writer->EndRoData(rodata); |
| 152 | |
| 153 | OutputStream* text = elf_writer->StartText(); |
| 154 | bool text_ok = oat_writer.WriteCode(text); |
| 155 | ASSERT_TRUE(text_ok); |
| 156 | elf_writer->EndText(text); |
| 157 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 158 | bool header_ok = oat_writer.WriteHeader(elf_writer->GetStream(), 0u, 0u, 0u); |
| 159 | ASSERT_TRUE(header_ok); |
| 160 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 161 | writer->UpdateOatFileHeader(/* oat_index */ 0u, oat_writer.GetOatHeader()); |
| 162 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 163 | elf_writer->WriteDynamicSection(); |
Vladimir Marko | 49b0f45 | 2015-12-10 13:49:19 +0000 | [diff] [blame] | 164 | elf_writer->WriteDebugInfo(oat_writer.GetMethodDebugInfo()); |
| 165 | elf_writer->WritePatchLocations(oat_writer.GetAbsolutePatchLocations()); |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 166 | |
Vladimir Marko | 9bdf108 | 2016-01-21 12:15:52 +0000 | [diff] [blame] | 167 | bool success = elf_writer->End(); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 168 | ASSERT_TRUE(success); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 169 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 170 | } |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 171 | // Workound bug that mcld::Linker::emit closes oat_file by reopening as dup_oat. |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 172 | std::unique_ptr<File> dup_oat(OS::OpenFileReadWrite(oat_file.GetFilename().c_str())); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 173 | ASSERT_TRUE(dup_oat.get() != nullptr); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 174 | |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 175 | { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 176 | std::vector<const char*> dup_oat_filename(1, dup_oat->GetPath().c_str()); |
| 177 | std::vector<const char*> dup_image_filename(1, image_file.GetFilename().c_str()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 178 | bool success_image = writer->Write(kInvalidFd, |
| 179 | dup_image_filename, |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 180 | dup_oat_filename); |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 181 | ASSERT_TRUE(success_image); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 182 | bool success_fixup = ElfWriter::Fixup(dup_oat.get(), |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 183 | writer->GetOatDataBegin(0)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 184 | ASSERT_TRUE(success_fixup); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 185 | |
| 186 | ASSERT_EQ(dup_oat->FlushCloseOrErase(), 0) << "Could not flush and close oat file " |
| 187 | << oat_file.GetFilename(); |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 188 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 189 | |
Mathieu Chartier | 68c868e | 2015-06-01 16:33:53 -0700 | [diff] [blame] | 190 | uint64_t image_file_size; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 191 | { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 192 | std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str())); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 193 | ASSERT_TRUE(file.get() != nullptr); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 194 | ImageHeader image_header; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 195 | ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 196 | ASSERT_TRUE(image_header.IsValid()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 197 | const auto& bitmap_section = image_header.GetImageSection(ImageHeader::kSectionImageBitmap); |
| 198 | ASSERT_GE(bitmap_section.Offset(), sizeof(image_header)); |
| 199 | ASSERT_NE(0U, bitmap_section.Size()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 200 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 201 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | a9d82fe | 2016-01-25 20:06:11 -0800 | [diff] [blame] | 202 | ASSERT_TRUE(heap->HaveContinuousSpaces()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 203 | gc::space::ContinuousSpace* space = heap->GetNonMovingSpace(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 204 | ASSERT_FALSE(space->IsImageSpace()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 205 | ASSERT_TRUE(space != nullptr); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 206 | ASSERT_TRUE(space->IsMallocSpace()); |
Mathieu Chartier | 68c868e | 2015-06-01 16:33:53 -0700 | [diff] [blame] | 207 | |
| 208 | image_file_size = file->GetLength(); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 209 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 210 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 211 | ASSERT_TRUE(compiler_driver_->GetImageClasses() != nullptr); |
Andreas Gampe | b1fcead | 2015-04-20 18:53:51 -0700 | [diff] [blame] | 212 | std::unordered_set<std::string> image_classes(*compiler_driver_->GetImageClasses()); |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 213 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 214 | // Need to delete the compiler since it has worker threads which are attached to runtime. |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 215 | compiler_driver_.reset(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 216 | |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 217 | // Tear down old runtime before making a new one, clearing out misc state. |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 218 | |
| 219 | // Remove the reservation of the memory for use to load the image. |
| 220 | // Need to do this before we reset the runtime. |
| 221 | UnreserveImageSpace(); |
| 222 | writer.reset(nullptr); |
| 223 | |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 224 | runtime_.reset(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 225 | java_lang_dex_file_ = nullptr; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 226 | |
Mathieu Chartier | 6e88ef6 | 2014-10-14 15:01:24 -0700 | [diff] [blame] | 227 | MemMap::Init(); |
Przemyslaw Szczepaniak | 5c0cf12 | 2015-09-30 17:41:38 +0100 | [diff] [blame] | 228 | std::unique_ptr<const DexFile> dex(LoadExpectSingleDexFile(GetLibCoreDexFileNames()[0].c_str())); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 229 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 230 | RuntimeOptions options; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 231 | std::string image("-Ximage:"); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 232 | image.append(image_location.GetFilename()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 233 | options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr))); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 234 | // By default the compiler this creates will not include patch information. |
| 235 | options.push_back(std::make_pair("-Xnorelocate", nullptr)); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 236 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 237 | if (!Runtime::Create(options, false)) { |
| 238 | LOG(FATAL) << "Failed to create runtime"; |
| 239 | return; |
| 240 | } |
| 241 | runtime_.reset(Runtime::Current()); |
| 242 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 243 | // give it away now and then switch to a more managable ScopedObjectAccess. |
| 244 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 245 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 246 | ASSERT_TRUE(runtime_.get() != nullptr); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 247 | class_linker_ = runtime_->GetClassLinker(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 248 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 249 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 250 | ASSERT_TRUE(heap->HasBootImageSpace()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 251 | ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 252 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 253 | // We loaded the runtime with an explicit image, so it must exist. |
| 254 | gc::space::ImageSpace* image_space = heap->GetBootImageSpaces()[0]; |
Mathieu Chartier | 68c868e | 2015-06-01 16:33:53 -0700 | [diff] [blame] | 255 | ASSERT_TRUE(image_space != nullptr); |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 256 | if (storage_mode == ImageHeader::kStorageModeUncompressed) { |
| 257 | // Uncompressed, image should be smaller than file. |
| 258 | ASSERT_LE(image_space->Size(), image_file_size); |
| 259 | } else { |
| 260 | // Compressed, file should be smaller than image. |
| 261 | ASSERT_LE(image_file_size, image_space->Size()); |
| 262 | } |
Mathieu Chartier | 68c868e | 2015-06-01 16:33:53 -0700 | [diff] [blame] | 263 | |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 264 | image_space->VerifyImageAllocations(); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 265 | uint8_t* image_begin = image_space->Begin(); |
| 266 | uint8_t* image_end = image_space->End(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 267 | CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin)); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 268 | for (size_t i = 0; i < dex->NumClassDefs(); ++i) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 269 | const DexFile::ClassDef& class_def = dex->GetClassDef(i); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 270 | const char* descriptor = dex->GetClassDescriptor(class_def); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 271 | mirror::Class* klass = class_linker_->FindSystemClass(soa.Self(), descriptor); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 272 | EXPECT_TRUE(klass != nullptr) << descriptor; |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 273 | if (image_classes.find(descriptor) != image_classes.end()) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 274 | // Image classes should be located inside the image. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 275 | EXPECT_LT(image_begin, reinterpret_cast<uint8_t*>(klass)) << descriptor; |
| 276 | EXPECT_LT(reinterpret_cast<uint8_t*>(klass), image_end) << descriptor; |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 277 | } else { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 278 | EXPECT_TRUE(reinterpret_cast<uint8_t*>(klass) >= image_end || |
| 279 | reinterpret_cast<uint8_t*>(klass) < image_begin) << descriptor; |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 280 | } |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame] | 281 | EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false))); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 282 | } |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 283 | |
| 284 | image_file.Unlink(); |
| 285 | oat_file.Unlink(); |
| 286 | int rmdir_result = rmdir(image_dir.c_str()); |
| 287 | CHECK_EQ(0, rmdir_result); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 290 | TEST_F(ImageTest, WriteReadUncompressed) { |
| 291 | TestWriteRead(ImageHeader::kStorageModeUncompressed); |
| 292 | } |
| 293 | |
| 294 | TEST_F(ImageTest, WriteReadLZ4) { |
| 295 | TestWriteRead(ImageHeader::kStorageModeLZ4); |
| 296 | } |
| 297 | |
Mathieu Chartier | a6e81ed | 2016-02-25 13:52:10 -0800 | [diff] [blame] | 298 | TEST_F(ImageTest, WriteReadLZ4HC) { |
| 299 | TestWriteRead(ImageHeader::kStorageModeLZ4HC); |
| 300 | } |
| 301 | |
| 302 | |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 303 | TEST_F(ImageTest, ImageHeaderIsValid) { |
| 304 | uint32_t image_begin = ART_BASE_ADDRESS; |
| 305 | uint32_t image_size_ = 16 * KB; |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 306 | uint32_t image_roots = ART_BASE_ADDRESS + (1 * KB); |
| 307 | uint32_t oat_checksum = 0; |
| 308 | uint32_t oat_file_begin = ART_BASE_ADDRESS + (4 * KB); // page aligned |
| 309 | uint32_t oat_data_begin = ART_BASE_ADDRESS + (8 * KB); // page aligned |
| 310 | uint32_t oat_data_end = ART_BASE_ADDRESS + (9 * KB); |
| 311 | uint32_t oat_file_end = ART_BASE_ADDRESS + (10 * KB); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 312 | ImageSection sections[ImageHeader::kSectionCount]; |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 313 | ImageHeader image_header(image_begin, |
| 314 | image_size_, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 315 | sections, |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 316 | image_roots, |
| 317 | oat_checksum, |
| 318 | oat_file_begin, |
| 319 | oat_data_begin, |
| 320 | oat_data_end, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 321 | oat_file_end, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 322 | /*boot_image_begin*/0U, |
| 323 | /*boot_image_size*/0U, |
| 324 | /*boot_oat_begin*/0U, |
| 325 | /*boot_oat_size_*/0U, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 326 | sizeof(void*), |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 327 | /*compile_pic*/false, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 328 | /*is_pic*/false, |
Mathieu Chartier | ceb07b3 | 2015-12-10 09:33:21 -0800 | [diff] [blame] | 329 | ImageHeader::kDefaultStorageMode, |
| 330 | /*data_size*/0u); |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 331 | ASSERT_TRUE(image_header.IsValid()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 332 | ASSERT_TRUE(!image_header.IsAppImage()); |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 333 | |
| 334 | char* magic = const_cast<char*>(image_header.GetMagic()); |
| 335 | strcpy(magic, ""); // bad magic |
| 336 | ASSERT_FALSE(image_header.IsValid()); |
| 337 | strcpy(magic, "art\n000"); // bad version |
| 338 | ASSERT_FALSE(image_header.IsValid()); |
| 339 | } |
| 340 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 341 | } // namespace art |