1 /*
2  * Copyright (C) 2019 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 <sys/mman.h>
18 
19 #include "common_compiler_driver_test.h"
20 
21 #include "base/casts.h"
22 #include "base/timing_logger.h"
23 #include "dex/quick_compiler_callbacks.h"
24 #include "dex/verification_results.h"
25 #include "driver/compiler_driver.h"
26 #include "driver/compiler_options.h"
27 #include "utils/atomic_dex_ref_map-inl.h"
28 
29 namespace art {
30 
CommonCompilerDriverTest()31 CommonCompilerDriverTest::CommonCompilerDriverTest() : inaccessible_page_(nullptr) {}
~CommonCompilerDriverTest()32 CommonCompilerDriverTest::~CommonCompilerDriverTest() {}
33 
CompileAll(jobject class_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)34 void CommonCompilerDriverTest::CompileAll(jobject class_loader,
35                                           const std::vector<const DexFile*>& dex_files,
36                                           TimingLogger* timings) {
37   TimingLogger::ScopedTiming t(__FUNCTION__, timings);
38   SetDexFilesForOatFile(dex_files);
39 
40   compiler_driver_->InitializeThreadPools();
41 
42   compiler_driver_->PreCompile(class_loader,
43                                dex_files,
44                                timings,
45                                &compiler_options_->image_classes_);
46 
47   // Verification results in the `callback_` should not be used during compilation.
48   down_cast<QuickCompilerCallbacks*>(callbacks_.get())->SetVerificationResults(
49       reinterpret_cast<VerificationResults*>(inaccessible_page_));
50   compiler_driver_->CompileAll(class_loader, dex_files, timings);
51   down_cast<QuickCompilerCallbacks*>(callbacks_.get())->SetVerificationResults(
52       verification_results_.get());
53 
54   compiler_driver_->FreeThreadPools();
55 }
56 
SetDexFilesForOatFile(const std::vector<const DexFile * > & dex_files)57 void CommonCompilerDriverTest::SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files) {
58   compiler_options_->dex_files_for_oat_file_ = dex_files;
59   compiler_driver_->compiled_classes_.AddDexFiles(dex_files);
60 }
61 
ReserveImageSpace()62 void CommonCompilerDriverTest::ReserveImageSpace() {
63   // Reserve where the image will be loaded up front so that other parts of test set up don't
64   // accidentally end up colliding with the fixed memory address when we need to load the image.
65   std::string error_msg;
66   MemMap::Init();
67   image_reservation_ = MemMap::MapAnonymous("image reservation",
68                                             reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
69                                             static_cast<size_t>(120 * 1024 * 1024),  // 120MB
70                                             PROT_NONE,
71                                             false /* no need for 4gb flag with fixed mmap */,
72                                             /*reuse=*/ false,
73                                             /*reservation=*/ nullptr,
74                                             &error_msg);
75   CHECK(image_reservation_.IsValid()) << error_msg;
76 }
77 
UnreserveImageSpace()78 void CommonCompilerDriverTest::UnreserveImageSpace() {
79   image_reservation_.Reset();
80 }
81 
CreateCompilerDriver()82 void CommonCompilerDriverTest::CreateCompilerDriver() {
83   ApplyInstructionSet();
84 
85   compiler_options_->image_type_ = CompilerOptions::ImageType::kBootImage;
86   compiler_options_->compile_pic_ = false;  // Non-PIC boot image is a test configuration.
87   compiler_options_->SetCompilerFilter(GetCompilerFilter());
88   compiler_options_->image_classes_.swap(*GetImageClasses());
89   compiler_options_->profile_compilation_info_ = GetProfileCompilationInfo();
90   compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
91                                             verification_results_.get(),
92                                             number_of_threads_,
93                                             /* swap_fd= */ -1));
94 }
95 
SetUpRuntimeOptions(RuntimeOptions * options)96 void CommonCompilerDriverTest::SetUpRuntimeOptions(RuntimeOptions* options) {
97   CommonCompilerTest::SetUpRuntimeOptions(options);
98 
99   verification_results_.reset(new VerificationResults());
100   QuickCompilerCallbacks* callbacks =
101       new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp);
102   callbacks->SetVerificationResults(verification_results_.get());
103   callbacks_.reset(callbacks);
104 }
105 
SetUp()106 void CommonCompilerDriverTest::SetUp() {
107   CommonCompilerTest::SetUp();
108 
109   CreateCompilerDriver();
110 
111   // Note: We cannot use MemMap because some tests tear down the Runtime and destroy
112   // the gMaps, so when destroying the MemMap, the test would crash.
113   const size_t page_size = MemMap::GetPageSize();
114   inaccessible_page_ = mmap(nullptr, page_size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
115   CHECK(inaccessible_page_ != MAP_FAILED) << strerror(errno);
116 }
117 
TearDown()118 void CommonCompilerDriverTest::TearDown() {
119   if (inaccessible_page_ != nullptr) {
120     munmap(inaccessible_page_, MemMap::GetPageSize());
121     inaccessible_page_ = nullptr;
122   }
123   image_reservation_.Reset();
124   compiler_driver_.reset();
125   verification_results_.reset();
126 
127   CommonCompilerTest::TearDown();
128 }
129 
130 // Get the set of image classes given to the compiler options in CreateCompilerDriver().
GetImageClasses()131 std::unique_ptr<HashSet<std::string>> CommonCompilerDriverTest::GetImageClasses() {
132   // Empty set: by default no classes are retained in the image.
133   return std::make_unique<HashSet<std::string>>();
134 }
135 
136 // Get ProfileCompilationInfo that should be passed to the driver.
GetProfileCompilationInfo()137 ProfileCompilationInfo* CommonCompilerDriverTest::GetProfileCompilationInfo() {
138   // Null, profile information will not be taken into account.
139   return nullptr;
140 }
141 
142 }  // namespace art
143