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 #include "common_compiler_test.h"
18 
19 #include <android-base/unique_fd.h>
20 #include <type_traits>
21 
22 #include "arch/instruction_set_features.h"
23 #include "art_field-inl.h"
24 #include "art_method-inl.h"
25 #include "base/callee_save_type.h"
26 #include "base/casts.h"
27 #include "base/memfd.h"
28 #include "base/pointer_size.h"
29 #include "base/utils.h"
30 #include "class_linker.h"
31 #include "dex/descriptors_names.h"
32 #include "driver/compiled_code_storage.h"
33 #include "driver/compiler_options.h"
34 #include "jni/java_vm_ext.h"
35 #include "interpreter/interpreter.h"
36 #include "mirror/class-inl.h"
37 #include "mirror/class_loader.h"
38 #include "mirror/dex_cache.h"
39 #include "mirror/object-inl.h"
40 #include "oat/oat_quick_method_header.h"
41 #include "scoped_thread_state_change-inl.h"
42 #include "thread-current-inl.h"
43 #include "utils/atomic_dex_ref_map-inl.h"
44 
45 namespace art HIDDEN {
46 
47 class CommonCompilerTestImpl::CodeAndMetadata {
48  public:
49   CodeAndMetadata(CodeAndMetadata&& other) = default;
50 
CodeAndMetadata(ArrayRef<const uint8_t> code,ArrayRef<const uint8_t> vmap_table,InstructionSet instruction_set)51   CodeAndMetadata(ArrayRef<const uint8_t> code,
52                   ArrayRef<const uint8_t> vmap_table,
53                   InstructionSet instruction_set) {
54     const size_t page_size = MemMap::GetPageSize();
55     const uint32_t code_size = code.size();
56     CHECK_NE(code_size, 0u);
57     const uint32_t vmap_table_offset = vmap_table.empty() ? 0u
58         : sizeof(OatQuickMethodHeader) + vmap_table.size();
59     OatQuickMethodHeader method_header(vmap_table_offset);
60     const size_t code_alignment = GetInstructionSetCodeAlignment(instruction_set);
61     DCHECK_ALIGNED_PARAM(page_size, code_alignment);
62     const uint32_t code_offset = RoundUp(vmap_table.size() + sizeof(method_header), code_alignment);
63     const uint32_t capacity = RoundUp(code_offset + code_size, page_size);
64 
65     // Create a memfd handle with sufficient capacity.
66     android::base::unique_fd mem_fd(art::memfd_create_compat("test code", /*flags=*/ 0));
67     CHECK_GE(mem_fd.get(), 0);
68     int err = ftruncate(mem_fd, capacity);
69     CHECK_EQ(err, 0);
70 
71     // Map the memfd contents for read/write.
72     std::string error_msg;
73     rw_map_ = MemMap::MapFile(capacity,
74                               PROT_READ | PROT_WRITE,
75                               MAP_SHARED,
76                               mem_fd,
77                               /*start=*/ 0,
78                               /*low_4gb=*/ false,
79                               /*filename=*/ "test code",
80                               &error_msg);
81     CHECK(rw_map_.IsValid()) << error_msg;
82 
83     // Store data.
84     uint8_t* code_addr = rw_map_.Begin() + code_offset;
85     CHECK_ALIGNED_PARAM(code_addr, code_alignment);
86     CHECK_LE(vmap_table_offset, code_offset);
87     memcpy(code_addr - vmap_table_offset, vmap_table.data(), vmap_table.size());
88     static_assert(std::is_trivially_copyable<OatQuickMethodHeader>::value, "Cannot use memcpy");
89     CHECK_LE(sizeof(method_header), code_offset);
90     memcpy(code_addr - sizeof(method_header), &method_header, sizeof(method_header));
91     CHECK_LE(code_size, static_cast<size_t>(rw_map_.End() - code_addr));
92     memcpy(code_addr, code.data(), code_size);
93 
94     // Sync data.
95     bool success = rw_map_.Sync();
96     CHECK(success);
97     success = FlushCpuCaches(rw_map_.Begin(), rw_map_.End());
98     CHECK(success);
99 
100     // Map the data as read/executable.
101     rx_map_ = MemMap::MapFile(capacity,
102                               PROT_READ | PROT_EXEC,
103                               MAP_SHARED,
104                               mem_fd,
105                               /*start=*/ 0,
106                               /*low_4gb=*/ false,
107                               /*filename=*/ "test code",
108                               &error_msg);
109     CHECK(rx_map_.IsValid()) << error_msg;
110 
111     DCHECK_LT(code_offset, rx_map_.Size());
112     size_t adjustment = GetInstructionSetEntryPointAdjustment(instruction_set);
113     entry_point_ = rx_map_.Begin() + code_offset + adjustment;
114   }
115 
GetEntryPoint() const116   const void* GetEntryPoint() const {
117     DCHECK(rx_map_.IsValid());
118     return entry_point_;
119   }
120 
121  private:
122   MemMap rw_map_;
123   MemMap rx_map_;
124   const void* entry_point_;
125 
126   DISALLOW_COPY_AND_ASSIGN(CodeAndMetadata);
127 };
128 
129 class CommonCompilerTestImpl::OneCompiledMethodStorage final : public CompiledCodeStorage {
130  public:
OneCompiledMethodStorage()131   OneCompiledMethodStorage() {}
~OneCompiledMethodStorage()132   ~OneCompiledMethodStorage() {}
133 
CreateCompiledMethod(InstructionSet instruction_set,ArrayRef<const uint8_t> code,ArrayRef<const uint8_t> stack_map,ArrayRef<const uint8_t> cfi,ArrayRef<const linker::LinkerPatch> patches,bool is_intrinsic)134   CompiledMethod* CreateCompiledMethod(InstructionSet instruction_set,
135                                        ArrayRef<const uint8_t> code,
136                                        ArrayRef<const uint8_t> stack_map,
137                                        [[maybe_unused]] ArrayRef<const uint8_t> cfi,
138                                        ArrayRef<const linker::LinkerPatch> patches,
139                                        [[maybe_unused]] bool is_intrinsic) override {
140     // Supports only one method at a time.
141     CHECK_EQ(instruction_set_, InstructionSet::kNone);
142     CHECK_NE(instruction_set, InstructionSet::kNone);
143     instruction_set_ = instruction_set;
144     CHECK(code_.empty());
145     CHECK(!code.empty());
146     code_.assign(code.begin(), code.end());
147     CHECK(stack_map_.empty());
148     CHECK(!stack_map.empty());
149     stack_map_.assign(stack_map.begin(), stack_map.end());
150     CHECK(patches.empty()) << "Linker patches are unsupported for compiler gtests.";
151     return reinterpret_cast<CompiledMethod*>(this);
152   }
153 
GetThunkCode(const linker::LinkerPatch & patch,std::string * debug_name)154   ArrayRef<const uint8_t> GetThunkCode([[maybe_unused]] const linker::LinkerPatch& patch,
155                                        [[maybe_unused]] /*out*/ std::string* debug_name) override {
156     LOG(FATAL) << "Unsupported.";
157     UNREACHABLE();
158   }
159 
SetThunkCode(const linker::LinkerPatch & patch,ArrayRef<const uint8_t> code,const std::string & debug_name)160   void SetThunkCode([[maybe_unused]] const linker::LinkerPatch& patch,
161                     [[maybe_unused]] ArrayRef<const uint8_t> code,
162                     [[maybe_unused]] const std::string& debug_name) override {
163     LOG(FATAL) << "Unsupported.";
164     UNREACHABLE();
165   }
166 
GetInstructionSet() const167   InstructionSet GetInstructionSet() const {
168     CHECK_NE(instruction_set_, InstructionSet::kNone);
169     return instruction_set_;
170   }
171 
GetCode() const172   ArrayRef<const uint8_t> GetCode() const {
173     CHECK(!code_.empty());
174     return ArrayRef<const uint8_t>(code_);
175   }
176 
GetStackMap() const177   ArrayRef<const uint8_t> GetStackMap() const {
178     CHECK(!stack_map_.empty());
179     return ArrayRef<const uint8_t>(stack_map_);
180   }
181 
182  private:
183   InstructionSet instruction_set_ = InstructionSet::kNone;
184   std::vector<uint8_t> code_;
185   std::vector<uint8_t> stack_map_;
186 };
187 
CreateCompilerOptions(InstructionSet instruction_set,const std::string & variant)188 std::unique_ptr<CompilerOptions> CommonCompilerTestImpl::CreateCompilerOptions(
189     InstructionSet instruction_set, const std::string& variant) {
190   std::unique_ptr<CompilerOptions> compiler_options = std::make_unique<CompilerOptions>();
191   compiler_options->emit_read_barrier_ = gUseReadBarrier;
192   compiler_options->instruction_set_ = instruction_set;
193   std::string error_msg;
194   compiler_options->instruction_set_features_ =
195       InstructionSetFeatures::FromVariant(instruction_set, variant, &error_msg);
196   CHECK(compiler_options->instruction_set_features_ != nullptr) << error_msg;
197   return compiler_options;
198 }
199 
CommonCompilerTestImpl()200 CommonCompilerTestImpl::CommonCompilerTestImpl() {}
~CommonCompilerTestImpl()201 CommonCompilerTestImpl::~CommonCompilerTestImpl() {}
202 
MakeExecutable(ArrayRef<const uint8_t> code,ArrayRef<const uint8_t> vmap_table,InstructionSet instruction_set)203 const void* CommonCompilerTestImpl::MakeExecutable(ArrayRef<const uint8_t> code,
204                                                    ArrayRef<const uint8_t> vmap_table,
205                                                    InstructionSet instruction_set) {
206   CHECK_NE(code.size(), 0u);
207   code_and_metadata_.emplace_back(code, vmap_table, instruction_set);
208   return code_and_metadata_.back().GetEntryPoint();
209 }
210 
SetUp()211 void CommonCompilerTestImpl::SetUp() {
212   {
213     ScopedObjectAccess soa(Thread::Current());
214 
215     Runtime* runtime = GetRuntime();
216     runtime->SetInstructionSet(instruction_set_);
217     for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); ++i) {
218       CalleeSaveType type = CalleeSaveType(i);
219       if (!runtime->HasCalleeSaveMethod(type)) {
220         runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(), type);
221       }
222     }
223   }
224 }
225 
ApplyInstructionSet()226 void CommonCompilerTestImpl::ApplyInstructionSet() {
227   // Copy local instruction_set_ and instruction_set_features_ to *compiler_options_;
228   CHECK(instruction_set_features_ != nullptr);
229   if (instruction_set_ == InstructionSet::kThumb2) {
230     CHECK_EQ(InstructionSet::kArm, instruction_set_features_->GetInstructionSet());
231   } else {
232     CHECK_EQ(instruction_set_, instruction_set_features_->GetInstructionSet());
233   }
234   compiler_options_->instruction_set_ = instruction_set_;
235   compiler_options_->instruction_set_features_ =
236       InstructionSetFeatures::FromBitmap(instruction_set_, instruction_set_features_->AsBitmap());
237   CHECK(compiler_options_->instruction_set_features_->Equals(instruction_set_features_.get()));
238 }
239 
OverrideInstructionSetFeatures(InstructionSet instruction_set,const std::string & variant)240 void CommonCompilerTestImpl::OverrideInstructionSetFeatures(InstructionSet instruction_set,
241                                                             const std::string& variant) {
242   instruction_set_ = instruction_set;
243   std::string error_msg;
244   instruction_set_features_ =
245       InstructionSetFeatures::FromVariant(instruction_set, variant, &error_msg);
246   CHECK(instruction_set_features_ != nullptr) << error_msg;
247 
248   if (compiler_options_ != nullptr) {
249     ApplyInstructionSet();
250   }
251 }
252 
SetUpRuntimeOptionsImpl()253 void CommonCompilerTestImpl::SetUpRuntimeOptionsImpl() {
254   compiler_options_ = CreateCompilerOptions(instruction_set_, "default");
255   ApplyInstructionSet();
256 }
257 
TearDown()258 void CommonCompilerTestImpl::TearDown() {
259   code_and_metadata_.clear();
260   compiler_options_.reset();
261 }
262 
CompileMethod(ArtMethod * method)263 void CommonCompilerTestImpl::CompileMethod(ArtMethod* method) {
264   CHECK(method != nullptr);
265   TimingLogger timings("CommonCompilerTestImpl::CompileMethod", false, false);
266   TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
267   OneCompiledMethodStorage storage;
268   CompiledMethod* compiled_method = nullptr;
269   {
270     DCHECK(!Runtime::Current()->IsStarted());
271     Thread* self = Thread::Current();
272     StackHandleScope<2> hs(self);
273     std::unique_ptr<Compiler> compiler(Compiler::Create(*compiler_options_, &storage));
274     const DexFile& dex_file = *method->GetDexFile();
275     Handle<mirror::DexCache> dex_cache =
276         hs.NewHandle(GetClassLinker()->FindDexCache(self, dex_file));
277     Handle<mirror::ClassLoader> class_loader = hs.NewHandle(method->GetClassLoader());
278     if (method->IsNative()) {
279       compiled_method = compiler->JniCompile(method->GetAccessFlags(),
280                                              method->GetDexMethodIndex(),
281                                              dex_file,
282                                              dex_cache);
283     } else {
284       compiled_method = compiler->Compile(method->GetCodeItem(),
285                                           method->GetAccessFlags(),
286                                           method->GetInvokeType(),
287                                           method->GetClassDefIndex(),
288                                           method->GetDexMethodIndex(),
289                                           class_loader,
290                                           dex_file,
291                                           dex_cache);
292     }
293     CHECK(compiled_method != nullptr) << "Failed to compile " << method->PrettyMethod();
294     CHECK_EQ(reinterpret_cast<OneCompiledMethodStorage*>(compiled_method), &storage);
295   }
296   {
297     TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
298     const void* method_code = MakeExecutable(storage.GetCode(),
299                                              storage.GetStackMap(),
300                                              storage.GetInstructionSet());
301     LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code;
302     GetRuntime()->GetInstrumentation()->InitializeMethodsCode(method, /*aot_code=*/ method_code);
303   }
304 }
305 
ClearBootImageOption()306 void CommonCompilerTestImpl::ClearBootImageOption() {
307   compiler_options_->image_type_ = CompilerOptions::ImageType::kNone;
308 }
309 
310 }  // namespace art
311