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