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 #ifndef ART_DEX2OAT_LINKER_IMAGE_TEST_H_
18 #define ART_DEX2OAT_LINKER_IMAGE_TEST_H_
19
20 #include "image.h"
21
22 #include <memory>
23 #include <string>
24 #include <string_view>
25 #include <vector>
26
27 #include "android-base/stringprintf.h"
28 #include "android-base/strings.h"
29
30 #include "art_method-inl.h"
31 #include "base/file_utils.h"
32 #include "base/hash_set.h"
33 #include "base/stl_util.h"
34 #include "base/unix_file/fd_file.h"
35 #include "base/utils.h"
36 #include "class_linker-inl.h"
37 #include "common_compiler_driver_test.h"
38 #include "compiler_callbacks.h"
39 #include "debug/method_debug_info.h"
40 #include "dex/quick_compiler_callbacks.h"
41 #include "dex/signature-inl.h"
42 #include "driver/compiler_driver.h"
43 #include "driver/compiler_options.h"
44 #include "gc/space/image_space.h"
45 #include "image_writer.h"
46 #include "linker/elf_writer.h"
47 #include "linker/elf_writer_quick.h"
48 #include "linker/multi_oat_relative_patcher.h"
49 #include "lock_word.h"
50 #include "mirror/object-inl.h"
51 #include "oat.h"
52 #include "oat_writer.h"
53 #include "read_barrier_config.h"
54 #include "scoped_thread_state_change-inl.h"
55 #include "signal_catcher.h"
56 #include "stream/buffered_output_stream.h"
57 #include "stream/file_output_stream.h"
58
59 namespace art {
60 namespace linker {
61
62 static const uintptr_t kRequestedImageBase = ART_BASE_ADDRESS;
63
64 struct CompilationHelper {
65 std::vector<std::string> dex_file_locations;
66 std::vector<ScratchFile> image_locations;
67 std::string extra_dex;
68 std::vector<std::unique_ptr<const DexFile>> extra_dex_files;
69 std::vector<ScratchFile> image_files;
70 std::vector<ScratchFile> oat_files;
71 std::vector<ScratchFile> vdex_files;
72 std::string image_dir;
73
74 std::vector<size_t> GetImageObjectSectionSizes();
75
76 ~CompilationHelper();
77 };
78
79 class ImageTest : public CommonCompilerDriverTest {
80 protected:
SetUp()81 void SetUp() override {
82 ReserveImageSpace();
83 CommonCompilerDriverTest::SetUp();
84 }
85
86 void Compile(ImageHeader::StorageMode storage_mode,
87 uint32_t max_image_block_size,
88 /*out*/ CompilationHelper& out_helper,
89 const std::string& extra_dex = "",
90 const std::initializer_list<std::string>& image_classes = {},
91 const std::initializer_list<std::string>& image_classes_failing_aot_clinit = {},
92 const std::initializer_list<std::string>& image_classes_failing_resolution = {});
93
SetUpRuntimeOptions(RuntimeOptions * options)94 void SetUpRuntimeOptions(RuntimeOptions* options) override {
95 CommonCompilerDriverTest::SetUpRuntimeOptions(options);
96 QuickCompilerCallbacks* new_callbacks =
97 new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileBootImage);
98 new_callbacks->SetVerificationResults(verification_results_.get());
99 callbacks_.reset(new_callbacks);
100 options->push_back(std::make_pair("compilercallbacks", callbacks_.get()));
101 }
102
GetImageClasses()103 std::unique_ptr<HashSet<std::string>> GetImageClasses() override {
104 return std::make_unique<HashSet<std::string>>(image_classes_);
105 }
106
FindCopiedMethod(ArtMethod * origin,ObjPtr<mirror::Class> klass)107 ArtMethod* FindCopiedMethod(ArtMethod* origin, ObjPtr<mirror::Class> klass)
108 REQUIRES_SHARED(Locks::mutator_lock_) {
109 PointerSize pointer_size = class_linker_->GetImagePointerSize();
110 for (ArtMethod& m : klass->GetCopiedMethods(pointer_size)) {
111 if (strcmp(origin->GetName(), m.GetName()) == 0 &&
112 origin->GetSignature() == m.GetSignature()) {
113 return &m;
114 }
115 }
116 return nullptr;
117 }
118
119 private:
120 void DoCompile(ImageHeader::StorageMode storage_mode, /*out*/ CompilationHelper& out_helper);
121
122 HashSet<std::string> image_classes_;
123 };
124
~CompilationHelper()125 inline CompilationHelper::~CompilationHelper() {
126 for (ScratchFile& image_file : image_files) {
127 image_file.Unlink();
128 }
129 for (ScratchFile& oat_file : oat_files) {
130 oat_file.Unlink();
131 }
132 for (ScratchFile& vdex_file : vdex_files) {
133 vdex_file.Unlink();
134 }
135 const int rmdir_result = rmdir(image_dir.c_str());
136 CHECK_EQ(0, rmdir_result);
137 }
138
GetImageObjectSectionSizes()139 inline std::vector<size_t> CompilationHelper::GetImageObjectSectionSizes() {
140 std::vector<size_t> ret;
141 for (ScratchFile& image_file : image_files) {
142 std::unique_ptr<File> file(OS::OpenFileForReading(image_file.GetFilename().c_str()));
143 CHECK(file.get() != nullptr);
144 ImageHeader image_header;
145 CHECK_EQ(file->ReadFully(&image_header, sizeof(image_header)), true);
146 CHECK(image_header.IsValid());
147 ret.push_back(image_header.GetObjectsSection().Size());
148 }
149 return ret;
150 }
151
DoCompile(ImageHeader::StorageMode storage_mode,CompilationHelper & out_helper)152 inline void ImageTest::DoCompile(ImageHeader::StorageMode storage_mode,
153 /*out*/ CompilationHelper& out_helper) {
154 CompilerDriver* driver = compiler_driver_.get();
155 Runtime::Current()->AppendToBootClassPath(
156 out_helper.extra_dex, out_helper.extra_dex, out_helper.extra_dex_files);
157 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
158 std::vector<const DexFile*> class_path = class_linker->GetBootClassPath();
159
160 // Enable write for dex2dex.
161 for (const DexFile* dex_file : class_path) {
162 out_helper.dex_file_locations.push_back(dex_file->GetLocation());
163 if (dex_file->IsReadOnly()) {
164 dex_file->EnableWrite();
165 }
166 }
167 {
168 // Create a generic tmp file, to be the base of the .art and .oat temporary files.
169 ScratchFile location;
170 std::vector<std::string> image_locations =
171 gc::space::ImageSpace::ExpandMultiImageLocations(
172 ArrayRef<const std::string>(out_helper.dex_file_locations),
173 location.GetFilename() + ".art");
174 for (size_t i = 0u; i != class_path.size(); ++i) {
175 out_helper.image_locations.push_back(ScratchFile(image_locations[i]));
176 }
177 }
178 std::vector<std::string> image_filenames;
179 for (ScratchFile& file : out_helper.image_locations) {
180 std::string image_filename(GetSystemImageFilename(file.GetFilename().c_str(), kRuntimeISA));
181 image_filenames.push_back(image_filename);
182 size_t pos = image_filename.rfind('/');
183 CHECK_NE(pos, std::string::npos) << image_filename;
184 if (out_helper.image_dir.empty()) {
185 out_helper.image_dir = image_filename.substr(0, pos);
186 int mkdir_result = mkdir(out_helper.image_dir.c_str(), 0700);
187 CHECK_EQ(0, mkdir_result) << out_helper.image_dir;
188 }
189 out_helper.image_files.push_back(ScratchFile(OS::CreateEmptyFile(image_filename.c_str())));
190 }
191
192 std::vector<std::string> oat_filenames;
193 std::vector<std::string> vdex_filenames;
194 for (const std::string& image_filename : image_filenames) {
195 std::string oat_filename = ReplaceFileExtension(image_filename, "oat");
196 out_helper.oat_files.push_back(ScratchFile(OS::CreateEmptyFile(oat_filename.c_str())));
197 oat_filenames.push_back(oat_filename);
198 std::string vdex_filename = ReplaceFileExtension(image_filename, "vdex");
199 out_helper.vdex_files.push_back(ScratchFile(OS::CreateEmptyFile(vdex_filename.c_str())));
200 vdex_filenames.push_back(vdex_filename);
201 }
202
203 HashMap<const DexFile*, size_t> dex_file_to_oat_index_map;
204 size_t image_idx = 0;
205 for (const DexFile* dex_file : class_path) {
206 dex_file_to_oat_index_map.insert(std::make_pair(dex_file, image_idx));
207 ++image_idx;
208 }
209 std::unique_ptr<ImageWriter> writer(new ImageWriter(*compiler_options_,
210 kRequestedImageBase,
211 storage_mode,
212 oat_filenames,
213 dex_file_to_oat_index_map,
214 /*class_loader=*/ nullptr,
215 /*dirty_image_objects=*/ nullptr));
216 {
217 {
218 jobject class_loader = nullptr;
219 TimingLogger timings("ImageTest::WriteRead", false, false);
220 CompileAll(class_loader, class_path, &timings);
221
222 TimingLogger::ScopedTiming t("WriteElf", &timings);
223 SafeMap<std::string, std::string> key_value_store;
224 key_value_store.Put(OatHeader::kBootClassPathKey,
225 android::base::Join(out_helper.dex_file_locations, ':'));
226 key_value_store.Put(OatHeader::kApexVersionsKey, Runtime::Current()->GetApexVersions());
227 key_value_store.Put(OatHeader::kConcurrentCopying,
228 gUseReadBarrier ? OatHeader::kTrueValue : OatHeader::kFalseValue);
229
230 std::vector<std::unique_ptr<ElfWriter>> elf_writers;
231 std::vector<std::unique_ptr<OatWriter>> oat_writers;
232 for (ScratchFile& oat_file : out_helper.oat_files) {
233 elf_writers.emplace_back(CreateElfWriterQuick(*compiler_options_, oat_file.GetFile()));
234 elf_writers.back()->Start();
235 oat_writers.emplace_back(new OatWriter(*compiler_options_,
236 verification_results_.get(),
237 &timings,
238 /*profile_compilation_info*/nullptr,
239 CompactDexLevel::kCompactDexLevelNone));
240 }
241
242 std::vector<OutputStream*> rodata;
243 std::vector<MemMap> opened_dex_files_maps;
244 std::vector<std::unique_ptr<const DexFile>> opened_dex_files;
245 // Now that we have finalized key_value_store_, start writing the oat file.
246 for (size_t i = 0, size = oat_writers.size(); i != size; ++i) {
247 const DexFile* dex_file = class_path[i];
248 rodata.push_back(elf_writers[i]->StartRoData());
249 ArrayRef<const uint8_t> raw_dex_file(
250 reinterpret_cast<const uint8_t*>(&dex_file->GetHeader()),
251 dex_file->GetHeader().file_size_);
252 oat_writers[i]->AddRawDexFileSource(raw_dex_file,
253 dex_file->GetLocation().c_str(),
254 dex_file->GetLocationChecksum());
255
256 std::vector<MemMap> cur_opened_dex_files_maps;
257 std::vector<std::unique_ptr<const DexFile>> cur_opened_dex_files;
258 bool dex_files_ok = oat_writers[i]->WriteAndOpenDexFiles(
259 out_helper.vdex_files[i].GetFile(),
260 /* verify */ false, // Dex files may be dex-to-dex-ed, don't verify.
261 /* update_input_vdex */ false,
262 /* copy_dex_files */ CopyOption::kOnlyIfCompressed,
263 &cur_opened_dex_files_maps,
264 &cur_opened_dex_files);
265 ASSERT_TRUE(dex_files_ok);
266
267 if (!cur_opened_dex_files_maps.empty()) {
268 for (MemMap& cur_map : cur_opened_dex_files_maps) {
269 opened_dex_files_maps.push_back(std::move(cur_map));
270 }
271 for (std::unique_ptr<const DexFile>& cur_dex_file : cur_opened_dex_files) {
272 // dex_file_oat_index_map_.emplace(dex_file.get(), i);
273 opened_dex_files.push_back(std::move(cur_dex_file));
274 }
275 } else {
276 ASSERT_TRUE(cur_opened_dex_files.empty());
277 }
278 }
279 bool image_space_ok = writer->PrepareImageAddressSpace(&timings);
280 ASSERT_TRUE(image_space_ok);
281
282 DCHECK_EQ(out_helper.vdex_files.size(), out_helper.oat_files.size());
283 for (size_t i = 0, size = out_helper.oat_files.size(); i != size; ++i) {
284 MultiOatRelativePatcher patcher(compiler_options_->GetInstructionSet(),
285 compiler_options_->GetInstructionSetFeatures(),
286 driver->GetCompiledMethodStorage());
287 OatWriter* const oat_writer = oat_writers[i].get();
288 ElfWriter* const elf_writer = elf_writers[i].get();
289 std::vector<const DexFile*> cur_dex_files(1u, class_path[i]);
290 bool start_rodata_ok = oat_writer->StartRoData(cur_dex_files,
291 rodata[i],
292 (i == 0u) ? &key_value_store : nullptr);
293 ASSERT_TRUE(start_rodata_ok);
294 oat_writer->Initialize(driver, writer.get(), cur_dex_files);
295
296 oat_writer->FinishVdexFile(out_helper.vdex_files[i].GetFile(), /*verifier_deps=*/ nullptr);
297
298 oat_writer->PrepareLayout(&patcher);
299 elf_writer->PrepareDynamicSection(oat_writer->GetOatHeader().GetExecutableOffset(),
300 oat_writer->GetCodeSize(),
301 oat_writer->GetDataBimgRelRoSize(),
302 oat_writer->GetBssSize(),
303 oat_writer->GetBssMethodsOffset(),
304 oat_writer->GetBssRootsOffset(),
305 oat_writer->GetVdexSize());
306
307 writer->UpdateOatFileLayout(i,
308 elf_writer->GetLoadedSize(),
309 oat_writer->GetOatDataOffset(),
310 oat_writer->GetOatSize());
311
312 bool rodata_ok = oat_writer->WriteRodata(rodata[i]);
313 ASSERT_TRUE(rodata_ok);
314 elf_writer->EndRoData(rodata[i]);
315
316 OutputStream* text = elf_writer->StartText();
317 bool text_ok = oat_writer->WriteCode(text);
318 ASSERT_TRUE(text_ok);
319 elf_writer->EndText(text);
320
321 if (oat_writer->GetDataBimgRelRoSize() != 0u) {
322 OutputStream* data_bimg_rel_ro = elf_writer->StartDataBimgRelRo();
323 bool data_bimg_rel_ro_ok = oat_writer->WriteDataBimgRelRo(data_bimg_rel_ro);
324 ASSERT_TRUE(data_bimg_rel_ro_ok);
325 elf_writer->EndDataBimgRelRo(data_bimg_rel_ro);
326 }
327
328 bool header_ok = oat_writer->WriteHeader(elf_writer->GetStream());
329 ASSERT_TRUE(header_ok);
330
331 writer->UpdateOatFileHeader(i, oat_writer->GetOatHeader());
332
333 elf_writer->WriteDynamicSection();
334 elf_writer->WriteDebugInfo(oat_writer->GetDebugInfo());
335
336 bool success = elf_writer->End();
337 ASSERT_TRUE(success);
338 }
339 }
340
341 bool success_image = writer->Write(File::kInvalidFd,
342 image_filenames,
343 image_filenames.size());
344 ASSERT_TRUE(success_image);
345 }
346 }
347
Compile(ImageHeader::StorageMode storage_mode,uint32_t max_image_block_size,CompilationHelper & helper,const std::string & extra_dex,const std::initializer_list<std::string> & image_classes,const std::initializer_list<std::string> & image_classes_failing_aot_clinit,const std::initializer_list<std::string> & image_classes_failing_resolution)348 inline void ImageTest::Compile(
349 ImageHeader::StorageMode storage_mode,
350 uint32_t max_image_block_size,
351 CompilationHelper& helper,
352 const std::string& extra_dex,
353 const std::initializer_list<std::string>& image_classes,
354 const std::initializer_list<std::string>& image_classes_failing_aot_clinit,
355 const std::initializer_list<std::string>& image_classes_failing_resolution) {
356 for (const std::string& image_class : image_classes_failing_aot_clinit) {
357 ASSERT_TRUE(ContainsElement(image_classes, image_class));
358 }
359 for (const std::string& image_class : image_classes) {
360 image_classes_.insert(image_class);
361 }
362 number_of_threads_ = kIsTargetBuild ? 2U : 16U;
363 CreateCompilerDriver();
364 // Set inline filter values.
365 compiler_options_->SetInlineMaxCodeUnits(CompilerOptions::kDefaultInlineMaxCodeUnits);
366 compiler_options_->SetMaxImageBlockSize(max_image_block_size);
367 image_classes_.clear();
368 if (!extra_dex.empty()) {
369 helper.extra_dex = extra_dex;
370 helper.extra_dex_files = OpenTestDexFiles(extra_dex.c_str());
371 }
372 DoCompile(storage_mode, helper);
373 if (image_classes.begin() != image_classes.end()) {
374 // Make sure the class got initialized.
375 ScopedObjectAccess soa(Thread::Current());
376 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
377 for (const std::string& image_class : image_classes) {
378 ObjPtr<mirror::Class> klass =
379 class_linker->LookupClass(Thread::Current(), image_class.c_str(), nullptr);
380 if (ContainsElement(image_classes_failing_resolution, image_class)) {
381 EXPECT_TRUE(klass == nullptr || klass->IsErroneousUnresolved());
382 } else if (ContainsElement(image_classes_failing_aot_clinit, image_class)) {
383 ASSERT_TRUE(klass != nullptr) << image_class;
384 EXPECT_FALSE(klass->IsInitialized());
385 } else {
386 ASSERT_TRUE(klass != nullptr) << image_class;
387 EXPECT_TRUE(klass->IsInitialized());
388 }
389 }
390 }
391 }
392
393 } // namespace linker
394 } // namespace art
395
396 #endif // ART_DEX2OAT_LINKER_IMAGE_TEST_H_
397