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   compiler_driver_->dex_to_dex_compiler_.SetDexFiles(dex_files);
63 }
64 
ReserveImageSpace()65 void CommonCompilerDriverTest::ReserveImageSpace() {
66   // Reserve where the image will be loaded up front so that other parts of test set up don't
67   // accidentally end up colliding with the fixed memory address when we need to load the image.
68   std::string error_msg;
69   MemMap::Init();
70   image_reservation_ = MemMap::MapAnonymous("image reservation",
71                                             reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
72                                             (size_t)120 * 1024 * 1024,  // 120MB
73                                             PROT_NONE,
74                                             false /* no need for 4gb flag with fixed mmap */,
75                                             /*reuse=*/ false,
76                                             /*reservation=*/ nullptr,
77                                             &error_msg);
78   CHECK(image_reservation_.IsValid()) << error_msg;
79 }
80 
UnreserveImageSpace()81 void CommonCompilerDriverTest::UnreserveImageSpace() {
82   image_reservation_.Reset();
83 }
84 
CreateCompilerDriver()85 void CommonCompilerDriverTest::CreateCompilerDriver() {
86   ApplyInstructionSet();
87 
88   compiler_options_->image_type_ = CompilerOptions::ImageType::kBootImage;
89   compiler_options_->compile_pic_ = false;  // Non-PIC boot image is a test configuration.
90   compiler_options_->SetCompilerFilter(GetCompilerFilter());
91   compiler_options_->image_classes_.swap(*GetImageClasses());
92   compiler_options_->profile_compilation_info_ = GetProfileCompilationInfo();
93   compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
94                                             compiler_kind_,
95                                             number_of_threads_,
96                                             /* swap_fd= */ -1));
97 }
98 
SetUpRuntimeOptions(RuntimeOptions * options)99 void CommonCompilerDriverTest::SetUpRuntimeOptions(RuntimeOptions* options) {
100   CommonCompilerTest::SetUpRuntimeOptions(options);
101 
102   QuickCompilerCallbacks* callbacks =
103       new QuickCompilerCallbacks(CompilerCallbacks::CallbackMode::kCompileApp);
104   callbacks->SetVerificationResults(verification_results_.get());
105   callbacks_.reset(callbacks);
106 }
107 
SetUp()108 void CommonCompilerDriverTest::SetUp() {
109   CommonCompilerTest::SetUp();
110 
111   CreateCompilerDriver();
112 
113   // Note: We cannot use MemMap because some tests tear down the Runtime and destroy
114   // the gMaps, so when destroying the MemMap, the test would crash.
115   inaccessible_page_ = mmap(nullptr, kPageSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
116   CHECK(inaccessible_page_ != MAP_FAILED) << strerror(errno);
117 }
118 
TearDown()119 void CommonCompilerDriverTest::TearDown() {
120   if (inaccessible_page_ != nullptr) {
121     munmap(inaccessible_page_, kPageSize);
122     inaccessible_page_ = nullptr;
123   }
124   image_reservation_.Reset();
125   compiler_driver_.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