• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
46   // Verification results in the `callback_` should not be used during compilation.
47   down_cast<QuickCompilerCallbacks*>(callbacks_.get())->SetVerificationResults(
48       reinterpret_cast<VerificationResults*>(inaccessible_page_));
49   compiler_options_->verification_results_ = verification_results_.get();
50   compiler_driver_->CompileAll(class_loader, dex_files, timings);
51   compiler_options_->verification_results_ = nullptr;
52   down_cast<QuickCompilerCallbacks*>(callbacks_.get())->SetVerificationResults(
53       verification_results_.get());
54 
55   compiler_driver_->FreeThreadPools();
56 }
57 
SetDexFilesForOatFile(const std::vector<const DexFile * > & dex_files)58 void CommonCompilerDriverTest::SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files) {
59   compiler_options_->dex_files_for_oat_file_ = dex_files;
60   compiler_driver_->compiled_classes_.AddDexFiles(dex_files);
61 }
62 
ReserveImageSpace()63 void CommonCompilerDriverTest::ReserveImageSpace() {
64   // Reserve where the image will be loaded up front so that other parts of test set up don't
65   // accidentally end up colliding with the fixed memory address when we need to load the image.
66   std::string error_msg;
67   MemMap::Init();
68   image_reservation_ = MemMap::MapAnonymous("image reservation",
69                                             reinterpret_cast<uint8_t*>(ART_BASE_ADDRESS),
70                                             static_cast<size_t>(120 * 1024 * 1024),  // 120MB
71                                             PROT_NONE,
72                                             false /* no need for 4gb flag with fixed mmap */,
73                                             /*reuse=*/ false,
74                                             /*reservation=*/ nullptr,
75                                             &error_msg);
76   CHECK(image_reservation_.IsValid()) << error_msg;
77 }
78 
UnreserveImageSpace()79 void CommonCompilerDriverTest::UnreserveImageSpace() {
80   image_reservation_.Reset();
81 }
82 
CreateCompilerDriver()83 void CommonCompilerDriverTest::CreateCompilerDriver() {
84   ApplyInstructionSet();
85 
86   compiler_options_->image_type_ = CompilerOptions::ImageType::kBootImage;
87   compiler_options_->compile_pic_ = false;  // Non-PIC boot image is a test configuration.
88   compiler_options_->SetCompilerFilter(GetCompilerFilter());
89   compiler_options_->image_classes_.swap(*GetImageClasses());
90   compiler_options_->profile_compilation_info_ = GetProfileCompilationInfo();
91   compiler_driver_.reset(new CompilerDriver(compiler_options_.get(),
92                                             compiler_kind_,
93                                             number_of_threads_,
94                                             /* swap_fd= */ -1));
95 }
96 
SetUpRuntimeOptions(RuntimeOptions * options)97 void CommonCompilerDriverTest::SetUpRuntimeOptions(RuntimeOptions* options) {
98   CommonCompilerTest::SetUpRuntimeOptions(options);
99 
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   inaccessible_page_ = mmap(nullptr, kPageSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
114   CHECK(inaccessible_page_ != MAP_FAILED) << strerror(errno);
115 }
116 
TearDown()117 void CommonCompilerDriverTest::TearDown() {
118   if (inaccessible_page_ != nullptr) {
119     munmap(inaccessible_page_, kPageSize);
120     inaccessible_page_ = nullptr;
121   }
122   image_reservation_.Reset();
123   compiler_driver_.reset();
124 
125   CommonCompilerTest::TearDown();
126 }
127 
128 // Get the set of image classes given to the compiler options in CreateCompilerDriver().
GetImageClasses()129 std::unique_ptr<HashSet<std::string>> CommonCompilerDriverTest::GetImageClasses() {
130   // Empty set: by default no classes are retained in the image.
131   return std::make_unique<HashSet<std::string>>();
132 }
133 
134 // Get ProfileCompilationInfo that should be passed to the driver.
GetProfileCompilationInfo()135 ProfileCompilationInfo* CommonCompilerDriverTest::GetProfileCompilationInfo() {
136   // Null, profile information will not be taken into account.
137   return nullptr;
138 }
139 
140 }  // namespace art
141