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 */ 16 17#include "image.h" 18 19#include <memory> 20#include <string> 21#include <vector> 22 23#include "base/unix_file/fd_file.h" 24#include "class_linker-inl.h" 25#include "common_compiler_test.h" 26#include "elf_writer.h" 27#include "gc/space/image_space.h" 28#include "image_writer.h" 29#include "lock_word.h" 30#include "mirror/object-inl.h" 31#include "oat_writer.h" 32#include "scoped_thread_state_change.h" 33#include "signal_catcher.h" 34#include "utils.h" 35#include "vector_output_stream.h" 36 37namespace art { 38 39class ImageTest : public CommonCompilerTest { 40 protected: 41 virtual void SetUp() { 42 ReserveImageSpace(); 43 CommonCompilerTest::SetUp(); 44 } 45}; 46 47TEST_F(ImageTest, WriteRead) { 48 // Create a generic location tmp file, to be the base of the .art and .oat temporary files. 49 ScratchFile location; 50 ScratchFile image_location(location, ".art"); 51 52 std::string image_filename(GetSystemImageFilename(image_location.GetFilename().c_str(), 53 kRuntimeISA)); 54 size_t pos = image_filename.rfind('/'); 55 CHECK_NE(pos, std::string::npos) << image_filename; 56 std::string image_dir(image_filename, 0, pos); 57 int mkdir_result = mkdir(image_dir.c_str(), 0700); 58 CHECK_EQ(0, mkdir_result) << image_dir; 59 ScratchFile image_file(OS::CreateEmptyFile(image_filename.c_str())); 60 61 std::string oat_filename(image_filename, 0, image_filename.size() - 3); 62 oat_filename += "oat"; 63 ScratchFile oat_file(OS::CreateEmptyFile(oat_filename.c_str())); 64 65 const uintptr_t requested_image_base = ART_BASE_ADDRESS; 66 std::unique_ptr<ImageWriter> writer(new ImageWriter(*compiler_driver_, requested_image_base, 67 /*compile_pic*/false)); 68 // TODO: compile_pic should be a test argument. 69 { 70 { 71 jobject class_loader = nullptr; 72 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 73 TimingLogger timings("ImageTest::WriteRead", false, false); 74 TimingLogger::ScopedTiming t("CompileAll", &timings); 75 for (const DexFile* dex_file : class_linker->GetBootClassPath()) { 76 dex_file->EnableWrite(); 77 } 78 compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings); 79 80 t.NewTiming("WriteElf"); 81 SafeMap<std::string, std::string> key_value_store; 82 OatWriter oat_writer(class_linker->GetBootClassPath(), 0, 0, 0, compiler_driver_.get(), 83 writer.get(), &timings, &key_value_store); 84 bool success = writer->PrepareImageAddressSpace() && 85 compiler_driver_->WriteElf(GetTestAndroidRoot(), 86 !kIsTargetBuild, 87 class_linker->GetBootClassPath(), 88 &oat_writer, 89 oat_file.GetFile()); 90 ASSERT_TRUE(success); 91 } 92 } 93 // Workound bug that mcld::Linker::emit closes oat_file by reopening as dup_oat. 94 std::unique_ptr<File> dup_oat(OS::OpenFileReadWrite(oat_file.GetFilename().c_str())); 95 ASSERT_TRUE(dup_oat.get() != nullptr); 96 97 { 98 bool success_image = 99 writer->Write(image_file.GetFilename(), dup_oat->GetPath(), dup_oat->GetPath()); 100 ASSERT_TRUE(success_image); 101 bool success_fixup = ElfWriter::Fixup(dup_oat.get(), writer->GetOatDataBegin()); 102 ASSERT_TRUE(success_fixup); 103 104 ASSERT_EQ(dup_oat->FlushCloseOrErase(), 0) << "Could not flush and close oat file " 105 << oat_file.GetFilename(); 106 } 107 108 uint64_t image_file_size; 109 { 110 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str())); 111 ASSERT_TRUE(file.get() != nullptr); 112 ImageHeader image_header; 113 ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true); 114 ASSERT_TRUE(image_header.IsValid()); 115 const auto& bitmap_section = image_header.GetImageSection(ImageHeader::kSectionImageBitmap); 116 ASSERT_GE(bitmap_section.Offset(), sizeof(image_header)); 117 ASSERT_NE(0U, bitmap_section.Size()); 118 119 gc::Heap* heap = Runtime::Current()->GetHeap(); 120 ASSERT_TRUE(!heap->GetContinuousSpaces().empty()); 121 gc::space::ContinuousSpace* space = heap->GetNonMovingSpace(); 122 ASSERT_FALSE(space->IsImageSpace()); 123 ASSERT_TRUE(space != nullptr); 124 ASSERT_TRUE(space->IsMallocSpace()); 125 126 image_file_size = file->GetLength(); 127 } 128 129 ASSERT_TRUE(compiler_driver_->GetImageClasses() != nullptr); 130 std::unordered_set<std::string> image_classes(*compiler_driver_->GetImageClasses()); 131 132 // Need to delete the compiler since it has worker threads which are attached to runtime. 133 compiler_driver_.reset(); 134 135 // Tear down old runtime before making a new one, clearing out misc state. 136 137 // Remove the reservation of the memory for use to load the image. 138 // Need to do this before we reset the runtime. 139 UnreserveImageSpace(); 140 writer.reset(nullptr); 141 142 runtime_.reset(); 143 java_lang_dex_file_ = nullptr; 144 145 MemMap::Init(); 146 std::unique_ptr<const DexFile> dex(LoadExpectSingleDexFile(GetLibCoreDexFileName().c_str())); 147 148 RuntimeOptions options; 149 std::string image("-Ximage:"); 150 image.append(image_location.GetFilename()); 151 options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr))); 152 // By default the compiler this creates will not include patch information. 153 options.push_back(std::make_pair("-Xnorelocate", nullptr)); 154 155 if (!Runtime::Create(options, false)) { 156 LOG(FATAL) << "Failed to create runtime"; 157 return; 158 } 159 runtime_.reset(Runtime::Current()); 160 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, 161 // give it away now and then switch to a more managable ScopedObjectAccess. 162 Thread::Current()->TransitionFromRunnableToSuspended(kNative); 163 ScopedObjectAccess soa(Thread::Current()); 164 ASSERT_TRUE(runtime_.get() != nullptr); 165 class_linker_ = runtime_->GetClassLinker(); 166 167 gc::Heap* heap = Runtime::Current()->GetHeap(); 168 ASSERT_TRUE(heap->HasImageSpace()); 169 ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace()); 170 171 gc::space::ImageSpace* image_space = heap->GetImageSpace(); 172 ASSERT_TRUE(image_space != nullptr); 173 ASSERT_LE(image_space->Size(), image_file_size); 174 175 image_space->VerifyImageAllocations(); 176 uint8_t* image_begin = image_space->Begin(); 177 uint8_t* image_end = image_space->End(); 178 CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin)); 179 for (size_t i = 0; i < dex->NumClassDefs(); ++i) { 180 const DexFile::ClassDef& class_def = dex->GetClassDef(i); 181 const char* descriptor = dex->GetClassDescriptor(class_def); 182 mirror::Class* klass = class_linker_->FindSystemClass(soa.Self(), descriptor); 183 EXPECT_TRUE(klass != nullptr) << descriptor; 184 if (image_classes.find(descriptor) != image_classes.end()) { 185 // Image classes should be located inside the image. 186 EXPECT_LT(image_begin, reinterpret_cast<uint8_t*>(klass)) << descriptor; 187 EXPECT_LT(reinterpret_cast<uint8_t*>(klass), image_end) << descriptor; 188 } else { 189 EXPECT_TRUE(reinterpret_cast<uint8_t*>(klass) >= image_end || 190 reinterpret_cast<uint8_t*>(klass) < image_begin) << descriptor; 191 } 192 EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false))); 193 } 194 195 image_file.Unlink(); 196 oat_file.Unlink(); 197 int rmdir_result = rmdir(image_dir.c_str()); 198 CHECK_EQ(0, rmdir_result); 199} 200 201TEST_F(ImageTest, ImageHeaderIsValid) { 202 uint32_t image_begin = ART_BASE_ADDRESS; 203 uint32_t image_size_ = 16 * KB; 204 uint32_t image_roots = ART_BASE_ADDRESS + (1 * KB); 205 uint32_t oat_checksum = 0; 206 uint32_t oat_file_begin = ART_BASE_ADDRESS + (4 * KB); // page aligned 207 uint32_t oat_data_begin = ART_BASE_ADDRESS + (8 * KB); // page aligned 208 uint32_t oat_data_end = ART_BASE_ADDRESS + (9 * KB); 209 uint32_t oat_file_end = ART_BASE_ADDRESS + (10 * KB); 210 ImageSection sections[ImageHeader::kSectionCount]; 211 ImageHeader image_header(image_begin, 212 image_size_, 213 sections, 214 image_roots, 215 oat_checksum, 216 oat_file_begin, 217 oat_data_begin, 218 oat_data_end, 219 oat_file_end, 220 sizeof(void*), 221 /*compile_pic*/false); 222 ASSERT_TRUE(image_header.IsValid()); 223 224 char* magic = const_cast<char*>(image_header.GetMagic()); 225 strcpy(magic, ""); // bad magic 226 ASSERT_FALSE(image_header.IsValid()); 227 strcpy(magic, "art\n000"); // bad version 228 ASSERT_FALSE(image_header.IsValid()); 229} 230 231} // namespace art 232