xref: /aosp_15_r20/art/dex2oat/linker/image_write_read_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "image_test.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker namespace art {
20*795d594fSAndroid Build Coastguard Worker namespace linker {
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker class ImageWriteReadTest : public ImageTest {
23*795d594fSAndroid Build Coastguard Worker  protected:
24*795d594fSAndroid Build Coastguard Worker   void TestWriteRead(ImageHeader::StorageMode storage_mode, uint32_t max_image_block_size);
25*795d594fSAndroid Build Coastguard Worker };
26*795d594fSAndroid Build Coastguard Worker 
TestWriteRead(ImageHeader::StorageMode storage_mode,uint32_t max_image_block_size)27*795d594fSAndroid Build Coastguard Worker void ImageWriteReadTest::TestWriteRead(ImageHeader::StorageMode storage_mode,
28*795d594fSAndroid Build Coastguard Worker                                        uint32_t max_image_block_size) {
29*795d594fSAndroid Build Coastguard Worker   CompilationHelper helper;
30*795d594fSAndroid Build Coastguard Worker   Compile(storage_mode, max_image_block_size, /*out*/ helper);
31*795d594fSAndroid Build Coastguard Worker   std::vector<uint64_t> image_file_sizes;
32*795d594fSAndroid Build Coastguard Worker   for (ScratchFile& image_file : helper.image_files) {
33*795d594fSAndroid Build Coastguard Worker     std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
34*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(file.get() != nullptr);
35*795d594fSAndroid Build Coastguard Worker     ImageHeader image_header;
36*795d594fSAndroid Build Coastguard Worker     ASSERT_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
37*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(image_header.IsValid());
38*795d594fSAndroid Build Coastguard Worker     const auto& bitmap_section = image_header.GetImageBitmapSection();
39*795d594fSAndroid Build Coastguard Worker     ASSERT_GE(bitmap_section.Offset(), sizeof(image_header));
40*795d594fSAndroid Build Coastguard Worker     ASSERT_NE(0U, bitmap_section.Size());
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker     gc::Heap* heap = Runtime::Current()->GetHeap();
43*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(heap->HaveContinuousSpaces());
44*795d594fSAndroid Build Coastguard Worker     gc::space::ContinuousSpace* space = heap->GetNonMovingSpace();
45*795d594fSAndroid Build Coastguard Worker     ASSERT_FALSE(space->IsImageSpace());
46*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(space != nullptr);
47*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(space->IsMallocSpace());
48*795d594fSAndroid Build Coastguard Worker     image_file_sizes.push_back(file->GetLength());
49*795d594fSAndroid Build Coastguard Worker   }
50*795d594fSAndroid Build Coastguard Worker 
51*795d594fSAndroid Build Coastguard Worker   // Need to delete the compiler since it has worker threads which are attached to runtime.
52*795d594fSAndroid Build Coastguard Worker   compiler_driver_.reset();
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker   // Tear down old runtime before making a new one, clearing out misc state.
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker   // Remove the reservation of the memory for use to load the image.
57*795d594fSAndroid Build Coastguard Worker   // Need to do this before we reset the runtime.
58*795d594fSAndroid Build Coastguard Worker   UnreserveImageSpace();
59*795d594fSAndroid Build Coastguard Worker 
60*795d594fSAndroid Build Coastguard Worker   helper.extra_dex_files.clear();
61*795d594fSAndroid Build Coastguard Worker   runtime_.reset();
62*795d594fSAndroid Build Coastguard Worker   java_lang_dex_file_ = nullptr;
63*795d594fSAndroid Build Coastguard Worker 
64*795d594fSAndroid Build Coastguard Worker   MemMap::Init();
65*795d594fSAndroid Build Coastguard Worker 
66*795d594fSAndroid Build Coastguard Worker   RuntimeOptions options;
67*795d594fSAndroid Build Coastguard Worker   options.emplace_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()), nullptr);
68*795d594fSAndroid Build Coastguard Worker   options.emplace_back(
69*795d594fSAndroid Build Coastguard Worker       GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()), nullptr);
70*795d594fSAndroid Build Coastguard Worker   std::string image("-Ximage:");
71*795d594fSAndroid Build Coastguard Worker   image.append(helper.image_locations[0].GetFilename());
72*795d594fSAndroid Build Coastguard Worker   options.push_back(std::make_pair(image.c_str(), static_cast<void*>(nullptr)));
73*795d594fSAndroid Build Coastguard Worker   // By default the compiler this creates will not include patch information.
74*795d594fSAndroid Build Coastguard Worker   options.push_back(std::make_pair("-Xnorelocate", nullptr));
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker   if (!Runtime::Create(options, false)) {
77*795d594fSAndroid Build Coastguard Worker     LOG(FATAL) << "Failed to create runtime";
78*795d594fSAndroid Build Coastguard Worker     return;
79*795d594fSAndroid Build Coastguard Worker   }
80*795d594fSAndroid Build Coastguard Worker   runtime_.reset(Runtime::Current());
81*795d594fSAndroid Build Coastguard Worker   // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
82*795d594fSAndroid Build Coastguard Worker   // give it away now and then switch to a more managable ScopedObjectAccess.
83*795d594fSAndroid Build Coastguard Worker   Thread::Current()->TransitionFromRunnableToSuspended(ThreadState::kNative);
84*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
85*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(runtime_.get() != nullptr);
86*795d594fSAndroid Build Coastguard Worker   class_linker_ = runtime_->GetClassLinker();
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker   gc::Heap* heap = Runtime::Current()->GetHeap();
89*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(heap->HasBootImageSpace());
90*795d594fSAndroid Build Coastguard Worker   ASSERT_TRUE(heap->GetNonMovingSpace()->IsMallocSpace());
91*795d594fSAndroid Build Coastguard Worker 
92*795d594fSAndroid Build Coastguard Worker   // We loaded the runtime with an explicit image, so it must exist.
93*795d594fSAndroid Build Coastguard Worker   ASSERT_EQ(heap->GetBootImageSpaces().size(), image_file_sizes.size());
94*795d594fSAndroid Build Coastguard Worker   const HashSet<std::string>& image_classes = compiler_options_->GetImageClasses();
95*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i < helper.dex_file_locations.size(); ++i) {
96*795d594fSAndroid Build Coastguard Worker     std::unique_ptr<const DexFile> dex(
97*795d594fSAndroid Build Coastguard Worker         LoadExpectSingleDexFile(helper.dex_file_locations[i].c_str()));
98*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(dex != nullptr);
99*795d594fSAndroid Build Coastguard Worker     uint64_t image_file_size = image_file_sizes[i];
100*795d594fSAndroid Build Coastguard Worker     gc::space::ImageSpace* image_space = heap->GetBootImageSpaces()[i];
101*795d594fSAndroid Build Coastguard Worker     ASSERT_TRUE(image_space != nullptr);
102*795d594fSAndroid Build Coastguard Worker     if (storage_mode == ImageHeader::kStorageModeUncompressed) {
103*795d594fSAndroid Build Coastguard Worker       // Uncompressed, image should be smaller than file.
104*795d594fSAndroid Build Coastguard Worker       ASSERT_LE(image_space->GetImageHeader().GetImageSize(), image_file_size);
105*795d594fSAndroid Build Coastguard Worker     } else if (image_file_size > 4 * kElfSegmentAlignment) {
106*795d594fSAndroid Build Coastguard Worker       // Compressed, file should be smaller than image. Not really valid for small images.
107*795d594fSAndroid Build Coastguard Worker       ASSERT_LE(image_file_size, image_space->GetImageHeader().GetImageSize());
108*795d594fSAndroid Build Coastguard Worker       // TODO: Actually validate the blocks, this is hard since the blocks are not copied over for
109*795d594fSAndroid Build Coastguard Worker       // compressed images. Add kElfSegmentAlignment since image_size is rounded up to this.
110*795d594fSAndroid Build Coastguard Worker       ASSERT_GT(image_space->GetImageHeader().GetBlockCount() * max_image_block_size,
111*795d594fSAndroid Build Coastguard Worker                 image_space->GetImageHeader().GetImageSize() - kElfSegmentAlignment);
112*795d594fSAndroid Build Coastguard Worker     }
113*795d594fSAndroid Build Coastguard Worker 
114*795d594fSAndroid Build Coastguard Worker     image_space->VerifyImageAllocations();
115*795d594fSAndroid Build Coastguard Worker     uint8_t* image_begin = image_space->Begin();
116*795d594fSAndroid Build Coastguard Worker     uint8_t* image_end = image_space->End();
117*795d594fSAndroid Build Coastguard Worker     if (i == 0) {
118*795d594fSAndroid Build Coastguard Worker       // This check is only valid for image 0.
119*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(kRequestedImageBase, reinterpret_cast<uintptr_t>(image_begin));
120*795d594fSAndroid Build Coastguard Worker     }
121*795d594fSAndroid Build Coastguard Worker     for (size_t j = 0; j < dex->NumClassDefs(); ++j) {
122*795d594fSAndroid Build Coastguard Worker       const dex::ClassDef& class_def = dex->GetClassDef(j);
123*795d594fSAndroid Build Coastguard Worker       const char* descriptor = dex->GetClassDescriptor(class_def);
124*795d594fSAndroid Build Coastguard Worker       ObjPtr<mirror::Class> klass = class_linker_->FindSystemClass(soa.Self(), descriptor);
125*795d594fSAndroid Build Coastguard Worker       EXPECT_TRUE(klass != nullptr) << descriptor;
126*795d594fSAndroid Build Coastguard Worker       uint8_t* raw_klass = reinterpret_cast<uint8_t*>(klass.Ptr());
127*795d594fSAndroid Build Coastguard Worker       if (image_classes.find(std::string_view(descriptor)) == image_classes.end()) {
128*795d594fSAndroid Build Coastguard Worker         EXPECT_TRUE(raw_klass >= image_end || raw_klass < image_begin) << descriptor;
129*795d594fSAndroid Build Coastguard Worker       } else {
130*795d594fSAndroid Build Coastguard Worker         // Image classes should be located inside the image.
131*795d594fSAndroid Build Coastguard Worker         EXPECT_LT(image_begin, raw_klass) << descriptor;
132*795d594fSAndroid Build Coastguard Worker         EXPECT_LT(raw_klass, image_end) << descriptor;
133*795d594fSAndroid Build Coastguard Worker       }
134*795d594fSAndroid Build Coastguard Worker       EXPECT_TRUE(Monitor::IsValidLockWord(klass->GetLockWord(false)));
135*795d594fSAndroid Build Coastguard Worker     }
136*795d594fSAndroid Build Coastguard Worker   }
137*795d594fSAndroid Build Coastguard Worker }
138*795d594fSAndroid Build Coastguard Worker 
TEST_F(ImageWriteReadTest,WriteReadUncompressed)139*795d594fSAndroid Build Coastguard Worker TEST_F(ImageWriteReadTest, WriteReadUncompressed) {
140*795d594fSAndroid Build Coastguard Worker   TestWriteRead(ImageHeader::kStorageModeUncompressed,
141*795d594fSAndroid Build Coastguard Worker                 /*max_image_block_size=*/std::numeric_limits<uint32_t>::max());
142*795d594fSAndroid Build Coastguard Worker }
143*795d594fSAndroid Build Coastguard Worker 
TEST_F(ImageWriteReadTest,WriteReadLZ4)144*795d594fSAndroid Build Coastguard Worker TEST_F(ImageWriteReadTest, WriteReadLZ4) {
145*795d594fSAndroid Build Coastguard Worker   TestWriteRead(ImageHeader::kStorageModeLZ4,
146*795d594fSAndroid Build Coastguard Worker                 /*max_image_block_size=*/std::numeric_limits<uint32_t>::max());
147*795d594fSAndroid Build Coastguard Worker }
148*795d594fSAndroid Build Coastguard Worker 
TEST_F(ImageWriteReadTest,WriteReadLZ4HC)149*795d594fSAndroid Build Coastguard Worker TEST_F(ImageWriteReadTest, WriteReadLZ4HC) {
150*795d594fSAndroid Build Coastguard Worker   TestWriteRead(ImageHeader::kStorageModeLZ4HC,
151*795d594fSAndroid Build Coastguard Worker                 /*max_image_block_size=*/std::numeric_limits<uint32_t>::max());
152*795d594fSAndroid Build Coastguard Worker }
153*795d594fSAndroid Build Coastguard Worker 
154*795d594fSAndroid Build Coastguard Worker 
TEST_F(ImageWriteReadTest,WriteReadLZ4HCKBBlock)155*795d594fSAndroid Build Coastguard Worker TEST_F(ImageWriteReadTest, WriteReadLZ4HCKBBlock) {
156*795d594fSAndroid Build Coastguard Worker   TestWriteRead(ImageHeader::kStorageModeLZ4HC, /*max_image_block_size=*/KB);
157*795d594fSAndroid Build Coastguard Worker }
158*795d594fSAndroid Build Coastguard Worker 
159*795d594fSAndroid Build Coastguard Worker }  // namespace linker
160*795d594fSAndroid Build Coastguard Worker }  // namespace art
161