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 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 20 | #include "common_test.h" |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 21 | #include "compiler/elf_fixup.h" |
| 22 | #include "compiler/image_writer.h" |
| 23 | #include "compiler/oat_writer.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 24 | #include "gc/space/image_space.h" |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 25 | #include "image.h" |
| 26 | #include "signal_catcher.h" |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 27 | #include "UniquePtr.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 28 | #include "utils.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 29 | #include "vector_output_stream.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 30 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 33 | class ImageTest : public CommonTest { |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 34 | protected: |
| 35 | virtual void SetUp() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 36 | ReserveImageSpace(); |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 37 | CommonTest::SetUp(); |
| 38 | } |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 39 | }; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 40 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 41 | TEST_F(ImageTest, WriteRead) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 42 | ScratchFile tmp_elf; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 43 | { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 44 | { |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 45 | jobject class_loader = NULL; |
| 46 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame] | 47 | base::TimingLogger timings("ImageTest::WriteRead", false, false); |
Ian Rogers | bd2c19f | 2013-07-25 17:43:46 -0700 | [diff] [blame] | 48 | timings.StartSplit("CompileAll"); |
Ian Rogers | e986953 | 2013-08-03 14:56:04 -0700 | [diff] [blame^] | 49 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 50 | // TODO: we disable this for portable so the test executes in a reasonable amount of time. |
| 51 | // We shouldn't need to do this. |
| 52 | runtime_->SetSmallMode(true); |
| 53 | #endif |
Brian Carlstrom | 4560248 | 2013-07-21 22:07:55 -0700 | [diff] [blame] | 54 | compiler_driver_->CompileAll(class_loader, class_linker->GetBootClassPath(), timings); |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 55 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 56 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | c50d8e1 | 2013-07-23 22:35:16 -0700 | [diff] [blame] | 57 | OatWriter oat_writer(class_linker->GetBootClassPath(), |
| 58 | 0, 0, "", compiler_driver_.get()); |
| 59 | bool success = compiler_driver_->WriteElf(GetTestAndroidRoot(), |
| 60 | !kIsTargetBuild, |
| 61 | class_linker->GetBootClassPath(), |
| 62 | oat_writer, |
| 63 | tmp_elf.GetFile()); |
| 64 | ASSERT_TRUE(success); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 65 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 66 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 67 | // Workound bug that mcld::Linker::emit closes tmp_elf by reopening as tmp_oat. |
| 68 | UniquePtr<File> tmp_oat(OS::OpenFile(tmp_elf.GetFilename().c_str(), true, false)); |
| 69 | ASSERT_TRUE(tmp_oat.get() != NULL); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 70 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 71 | ScratchFile tmp_image; |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 72 | const uintptr_t requested_image_base = ART_BASE_ADDRESS; |
| 73 | { |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 74 | ImageWriter writer(*compiler_driver_.get()); |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 75 | bool success_image = writer.Write(tmp_image.GetFilename(), requested_image_base, |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 76 | tmp_oat->GetPath(), tmp_oat->GetPath()); |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 77 | ASSERT_TRUE(success_image); |
Brian Carlstrom | 51c2467 | 2013-07-11 16:00:56 -0700 | [diff] [blame] | 78 | bool success_fixup = ElfFixup::Fixup(tmp_oat.get(), writer.GetOatDataBegin()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 79 | ASSERT_TRUE(success_fixup); |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 80 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 81 | |
| 82 | { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 83 | UniquePtr<File> file(OS::OpenFile(tmp_image.GetFilename().c_str(), false)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 84 | ASSERT_TRUE(file.get() != NULL); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 85 | ImageHeader image_header; |
| 86 | file->ReadFully(&image_header, sizeof(image_header)); |
| 87 | ASSERT_TRUE(image_header.IsValid()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 88 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 89 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 90 | ASSERT_EQ(1U, heap->GetContinuousSpaces().size()); |
| 91 | gc::space::ContinuousSpace* space = heap->GetContinuousSpaces().front(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 92 | ASSERT_FALSE(space->IsImageSpace()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 93 | ASSERT_TRUE(space != NULL); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 94 | ASSERT_TRUE(space->IsDlMallocSpace()); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 95 | 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] | 96 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 97 | |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 98 | ASSERT_TRUE(compiler_driver_->GetImageClasses() != NULL); |
| 99 | CompilerDriver::DescriptorSet image_classes(*compiler_driver_->GetImageClasses()); |
| 100 | |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 101 | // 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] | 102 | compiler_driver_.reset(); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 103 | |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 104 | // Tear down old runtime before making a new one, clearing out misc state. |
| 105 | runtime_.reset(); |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 106 | java_lang_dex_file_ = NULL; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 107 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 108 | UniquePtr<const DexFile> dex(DexFile::Open(GetLibCoreDexFileName(), GetLibCoreDexFileName())); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 109 | ASSERT_TRUE(dex.get() != NULL); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 110 | |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 111 | // Remove the reservation of the memory for use to load the image. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 112 | UnreserveImageSpace(); |
Ian Rogers | 10c5b78 | 2013-01-10 10:40:53 -0800 | [diff] [blame] | 113 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 114 | Runtime::Options options; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 115 | std::string image("-Ximage:"); |
| 116 | image.append(tmp_image.GetFilename()); |
| 117 | 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] | 118 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 119 | if (!Runtime::Create(options, false)) { |
| 120 | LOG(FATAL) << "Failed to create runtime"; |
| 121 | return; |
| 122 | } |
| 123 | runtime_.reset(Runtime::Current()); |
| 124 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, |
| 125 | // give it away now and then switch to a more managable ScopedObjectAccess. |
| 126 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 127 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 128 | ASSERT_TRUE(runtime_.get() != NULL); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 129 | class_linker_ = runtime_->GetClassLinker(); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 130 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 131 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 132 | ASSERT_EQ(2U, heap->GetContinuousSpaces().size()); |
| 133 | ASSERT_TRUE(heap->GetContinuousSpaces()[0]->IsImageSpace()); |
| 134 | ASSERT_FALSE(heap->GetContinuousSpaces()[0]->IsDlMallocSpace()); |
| 135 | ASSERT_FALSE(heap->GetContinuousSpaces()[1]->IsImageSpace()); |
| 136 | ASSERT_TRUE(heap->GetContinuousSpaces()[1]->IsDlMallocSpace()); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 137 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 138 | gc::space::ImageSpace* image_space = heap->GetImageSpace(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 139 | byte* image_begin = image_space->Begin(); |
| 140 | byte* image_end = image_space->End(); |
| 141 | CHECK_EQ(requested_image_base, reinterpret_cast<uintptr_t>(image_begin)); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 142 | for (size_t i = 0; i < dex->NumClassDefs(); ++i) { |
Brian Carlstrom | d2fbb2b | 2011-08-23 11:57:08 -0700 | [diff] [blame] | 143 | const DexFile::ClassDef& class_def = dex->GetClassDef(i); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 144 | const char* descriptor = dex->GetClassDescriptor(class_def); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 145 | mirror::Class* klass = class_linker_->FindSystemClass(descriptor); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 146 | EXPECT_TRUE(klass != NULL) << descriptor; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 147 | EXPECT_LT(image_begin, reinterpret_cast<byte*>(klass)) << descriptor; |
Brian Carlstrom | 9639160 | 2013-06-13 19:49:50 -0700 | [diff] [blame] | 148 | if (image_classes.find(descriptor) != image_classes.end()) { |
| 149 | // image classes should be located before the end of the image. |
| 150 | EXPECT_LT(reinterpret_cast<byte*>(klass), image_end) << descriptor; |
| 151 | } else { |
| 152 | // non image classes should be in a space after the image. |
| 153 | EXPECT_GT(reinterpret_cast<byte*>(klass), image_end) << descriptor; |
| 154 | } |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 155 | EXPECT_EQ(*klass->GetRawLockWordAddress(), 0); // address should have been removed from monitor |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 156 | } |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | } // namespace art |