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 | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 22 | #include "common_compiler_test.h" |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 23 | #include "compiler/elf_fixup.h" |
| 24 | #include "compiler/image_writer.h" |
| 25 | #include "compiler/oat_writer.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 26 | #include "gc/space/image_space.h" |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 27 | #include "lock_word.h" |
| 28 | #include "mirror/object-inl.h" |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 29 | #include "signal_catcher.h" |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 30 | #include "UniquePtr.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 31 | #include "utils.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 32 | #include "vector_output_stream.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 33 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 34 | namespace art { |
| 35 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 36 | class ImageTest : public CommonCompilerTest { |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 37 | protected: |
| 38 | virtual void SetUp() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 39 | ReserveImageSpace(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 40 | CommonCompilerTest::SetUp(); |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 41 | } |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 42 | }; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 43 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 44 | TEST_F(ImageTest, WriteRead) { |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 45 | // Create a root tmp file, to be the base of the .art and .oat temporary files. |
| 46 | ScratchFile tmp; |
| 47 | ScratchFile tmp_elf(tmp, "oat"); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 48 | { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 49 | { |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 50 | jobject class_loader = NULL; |
| 51 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 52 | TimingLogger timings("ImageTest::WriteRead", false, false); |
Ian Rogers | bd2c19f | 2013-07-25 17:43:46 -0700 | [diff] [blame] | 53 | timings.StartSplit("CompileAll"); |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 54 | if (kUsePortableCompiler) { |
| 55 | // TODO: we disable this for portable so the test executes in a reasonable amount of time. |
| 56 | // We shouldn't need to do this. |
| 57 | compiler_options_->SetCompilerFilter(CompilerOptions::kInterpretOnly); |
| 58 | } |
Brian Carlstrom | 5f67532 | 2013-09-03 22:45:51 -0700 | [diff] [blame] | 59 | for (const DexFile* dex_file : class_linker->GetBootClassPath()) { |
| 60 | dex_file->EnableWrite(); |
| 61 | } |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 62 | compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), &timings); |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 63 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 64 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 65 | OatWriter oat_writer(class_linker->GetBootClassPath(), |
Ian Rogers | ca368cb | 2013-11-15 15:52:08 -0800 | [diff] [blame] | 66 | 0, 0, "", compiler_driver_.get(), &timings); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 67 | bool success = compiler_driver_->WriteElf(GetTestAndroidRoot(), |
| 68 | !kIsTargetBuild, |
| 69 | class_linker->GetBootClassPath(), |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 70 | &oat_writer, |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 71 | tmp_elf.GetFile()); |
| 72 | ASSERT_TRUE(success); |
Ian Rogers | 5fe9af7 | 2013-11-14 00:17:20 -0800 | [diff] [blame] | 73 | timings.EndSplit(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 74 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 75 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 76 | // Workound bug that mcld::Linker::emit closes tmp_elf by reopening as tmp_oat. |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 77 | UniquePtr<File> tmp_oat(OS::OpenFileReadWrite(tmp_elf.GetFilename().c_str())); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 78 | ASSERT_TRUE(tmp_oat.get() != NULL); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 79 | |
Nicolas Geoffray | 9583fbc | 2014-02-28 15:21:07 +0000 | [diff] [blame] | 80 | ScratchFile tmp_image(tmp, "art"); |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 81 | const uintptr_t requested_image_base = ART_BASE_ADDRESS; |
| 82 | { |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 83 | ImageWriter writer(*compiler_driver_.get()); |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 84 | bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base, |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 85 | tmp_oat->GetPath(), tmp_oat->GetPath()); |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 86 | ASSERT_TRUE(success_image); |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 87 | bool success_fixup = ElfFixup::Fixup(tmp_oat.get(), writer.GetOatDataBegin()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 88 | ASSERT_TRUE(success_fixup); |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 89 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 90 | |
| 91 | { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 92 | UniquePtr<File> file(OS::OpenFileForReading(tmp_image.GetFilename().c_str())); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 93 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 94 | ImageHeader image_header; |
| 95 | file->ReadFully(&image_header, sizeof(image_header)); |
| 96 | ASSERT_TRUE(image_header.IsValid()); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 97 | ASSERT_GE(image_header.GetImageBitmapOffset(), sizeof(image_header)); |
| 98 | ASSERT_NE(0U, image_header.GetImageBitmapSize()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 99 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 100 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 101 | ASSERT_TRUE(!heap->GetContinuousSpaces().empty()); |
| 102 | gc::space::ContinuousSpace* space = heap->GetNonMovingSpace(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 103 | ASSERT_FALSE(space->IsImageSpace()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 104 | ASSERT_TRUE(space != NULL); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 105 | ASSERT_TRUE(space->IsMallocSpace()); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 106 | ASSERT_GE(sizeof(image_header) + space->Size(), static_cast<size_t>(file->GetLength())); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 107 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 108 | |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 109 | ASSERT_TRUE(compiler_driver_->GetImageClasses() != NULL); |
| 110 | CompilerDriver::DescriptorSet image_classes(*compiler_driver_->GetImageClasses()); |
| 111 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 112 | // 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] | 113 | compiler_driver_.reset(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 114 | |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 115 | // Tear down old runtime before making a new one, clearing out misc state. |
| 116 | runtime_.reset(); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 117 | java_lang_dex_file_ = NULL; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 118 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 119 | std::string error_msg; |
| 120 | UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName().c_str(), |
| 121 | GetLibCoreDexFileName().c_str(), |
| 122 | &error_msg)); |
| 123 | ASSERT_TRUE(dex.get() != nullptr) << error_msg; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 124 | |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 125 | // Remove the reservation of the memory for use to load the image. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 126 | UnreserveImageSpace(); |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 127 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 128 | Runtime::Options options; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 129 | std::string image("-Ximage:"); |
| 130 | image.append(tmp_image.GetFilename()); |
| 131 | options.push_back(std::make_pair(image.c_str(), reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 132 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 133 | if (!Runtime::Create(options, false)) { |
| 134 | LOG(FATAL) << "Failed to create runtime"; |
| 135 | return; |
| 136 | } |
| 137 | runtime_.reset(Runtime::Current()); |
| 138 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 139 | // give it away now and then switch to a more managable ScopedObjectAccess. |
| 140 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 141 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 142 | ASSERT_TRUE(runtime_.get() != NULL); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 143 | class_linker_ = runtime_->GetClassLinker(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 144 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 145 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 146 | ASSERT_TRUE(heap->HasImageSpace()); |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 147 | ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 148 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 149 | gc::space::ImageSpace* image_space = heap->GetImageSpace(); |
Mathieu Chartier | 31e8925 | 2013-08-28 11:29:12 -0700 | [diff] [blame] | 150 | image_space->VerifyImageAllocations(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 151 | byte* image_begin = image_space->Begin(); |
| 152 | byte* image_end = image_space->End(); |
| 153 | CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin)); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 154 | for (size_t i = 0; i < dex->NumClassDefs(); ++i) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 155 | const DexFile::ClassDef& class_def = dex->GetClassDef(i); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 156 | const char* descriptor = dex->GetClassDescriptor(class_def); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 157 | mirror::Class* klass = class_linker_->FindSystemClass(soa.Self(), descriptor); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 158 | EXPECT_TRUE(klass != nullptr) << descriptor; |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 159 | if (image_classes.find(descriptor) != image_classes.end()) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 160 | // Image classes should be located inside the image. |
| 161 | EXPECT_LT(image_begin, reinterpret_cast<byte*>(klass)) << descriptor; |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 162 | EXPECT_LT(reinterpret_cast<byte*>(klass), image_end) << descriptor; |
| 163 | } else { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 164 | EXPECT_TRUE(reinterpret_cast<byte*>(klass) >= image_end || |
| 165 | reinterpret_cast<byte*>(klass) < image_begin) << descriptor; |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 166 | } |
Mathieu Chartier | 4d7f61d | 2014-04-17 14:43:39 -0700 | [diff] [blame^] | 167 | EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false))); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 168 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Brian Carlstrom | 179486a | 2013-09-03 11:51:42 -0700 | [diff] [blame] | 171 | TEST_F(ImageTest, ImageHeaderIsValid) { |
| 172 | uint32_t image_begin = ART_BASE_ADDRESS; |
| 173 | uint32_t image_size_ = 16 * KB; |
| 174 | uint32_t image_bitmap_offset = 0; |
| 175 | uint32_t image_bitmap_size = 0; |
| 176 | uint32_t image_roots = ART_BASE_ADDRESS + (1 * KB); |
| 177 | uint32_t oat_checksum = 0; |
| 178 | uint32_t oat_file_begin = ART_BASE_ADDRESS + (4 * KB); // page aligned |
| 179 | uint32_t oat_data_begin = ART_BASE_ADDRESS + (8 * KB); // page aligned |
| 180 | uint32_t oat_data_end = ART_BASE_ADDRESS + (9 * KB); |
| 181 | uint32_t oat_file_end = ART_BASE_ADDRESS + (10 * KB); |
| 182 | ImageHeader image_header(image_begin, |
| 183 | image_size_, |
| 184 | image_bitmap_offset, |
| 185 | image_bitmap_size, |
| 186 | image_roots, |
| 187 | oat_checksum, |
| 188 | oat_file_begin, |
| 189 | oat_data_begin, |
| 190 | oat_data_end, |
| 191 | oat_file_end); |
| 192 | ASSERT_TRUE(image_header.IsValid()); |
| 193 | |
| 194 | char* magic = const_cast<char*>(image_header.GetMagic()); |
| 195 | strcpy(magic, ""); // bad magic |
| 196 | ASSERT_FALSE(image_header.IsValid()); |
| 197 | strcpy(magic, "art\n000"); // bad version |
| 198 | ASSERT_FALSE(image_header.IsValid()); |
| 199 | } |
| 200 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 201 | } // namespace art |