xref: /aosp_15_r20/art/compiler/common_compiler_test.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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,const std::optional<std::string> & extra_features)188 std::unique_ptr<CompilerOptions> CommonCompilerTestImpl::CreateCompilerOptions(
189     InstructionSet instruction_set,
190     const std::string& variant,
191     const std::optional<std::string>& extra_features) {
192   std::unique_ptr<CompilerOptions> compiler_options = std::make_unique<CompilerOptions>();
193   compiler_options->emit_read_barrier_ = gUseReadBarrier;
194   compiler_options->instruction_set_ = instruction_set;
195   std::string error_msg;
196   compiler_options->instruction_set_features_ =
197       InstructionSetFeatures::FromVariant(instruction_set, variant, &error_msg);
198   CHECK(compiler_options->instruction_set_features_ != nullptr) << error_msg;
199   if (extra_features) {
200     compiler_options->instruction_set_features_ =
201         compiler_options->instruction_set_features_->AddFeaturesFromString(*extra_features,
202                                                                            &error_msg);
203     CHECK_NE(compiler_options->instruction_set_features_, nullptr) << error_msg;
204   }
205   return compiler_options;
206 }
207 
CommonCompilerTestImpl()208 CommonCompilerTestImpl::CommonCompilerTestImpl() {}
~CommonCompilerTestImpl()209 CommonCompilerTestImpl::~CommonCompilerTestImpl() {}
210 
MakeExecutable(ArrayRef<const uint8_t> code,ArrayRef<const uint8_t> vmap_table,InstructionSet instruction_set)211 const void* CommonCompilerTestImpl::MakeExecutable(ArrayRef<const uint8_t> code,
212                                                    ArrayRef<const uint8_t> vmap_table,
213                                                    InstructionSet instruction_set) {
214   CHECK_NE(code.size(), 0u);
215   code_and_metadata_.emplace_back(code, vmap_table, instruction_set);
216   return code_and_metadata_.back().GetEntryPoint();
217 }
218 
SetUp()219 void CommonCompilerTestImpl::SetUp() {
220   {
221     ScopedObjectAccess soa(Thread::Current());
222 
223     Runtime* runtime = GetRuntime();
224     runtime->SetInstructionSet(instruction_set_);
225     for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); ++i) {
226       CalleeSaveType type = CalleeSaveType(i);
227       if (!runtime->HasCalleeSaveMethod(type)) {
228         runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(), type);
229       }
230     }
231   }
232 }
233 
ApplyInstructionSet()234 void CommonCompilerTestImpl::ApplyInstructionSet() {
235   // Copy local instruction_set_ and instruction_set_features_ to *compiler_options_;
236   CHECK(instruction_set_features_ != nullptr);
237   if (instruction_set_ == InstructionSet::kThumb2) {
238     CHECK_EQ(InstructionSet::kArm, instruction_set_features_->GetInstructionSet());
239   } else {
240     CHECK_EQ(instruction_set_, instruction_set_features_->GetInstructionSet());
241   }
242   compiler_options_->instruction_set_ = instruction_set_;
243   compiler_options_->instruction_set_features_ =
244       InstructionSetFeatures::FromBitmap(instruction_set_, instruction_set_features_->AsBitmap());
245   CHECK(compiler_options_->instruction_set_features_->Equals(instruction_set_features_.get()));
246 }
247 
OverrideInstructionSetFeatures(InstructionSet instruction_set,const std::string & variant)248 void CommonCompilerTestImpl::OverrideInstructionSetFeatures(InstructionSet instruction_set,
249                                                             const std::string& variant) {
250   instruction_set_ = instruction_set;
251   std::string error_msg;
252   instruction_set_features_ =
253       InstructionSetFeatures::FromVariant(instruction_set, variant, &error_msg);
254   CHECK(instruction_set_features_ != nullptr) << error_msg;
255 
256   if (compiler_options_ != nullptr) {
257     ApplyInstructionSet();
258   }
259 }
260 
SetUpRuntimeOptionsImpl()261 void CommonCompilerTestImpl::SetUpRuntimeOptionsImpl() {
262   compiler_options_ = CreateCompilerOptions(instruction_set_, "default");
263   ApplyInstructionSet();
264 }
265 
TearDown()266 void CommonCompilerTestImpl::TearDown() {
267   code_and_metadata_.clear();
268   compiler_options_.reset();
269 }
270 
CompileMethod(ArtMethod * method)271 void CommonCompilerTestImpl::CompileMethod(ArtMethod* method) {
272   CHECK(method != nullptr);
273   TimingLogger timings("CommonCompilerTestImpl::CompileMethod", false, false);
274   TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
275   OneCompiledMethodStorage storage;
276   CompiledMethod* compiled_method = nullptr;
277   {
278     DCHECK(!Runtime::Current()->IsStarted());
279     Thread* self = Thread::Current();
280     StackHandleScope<2> hs(self);
281     std::unique_ptr<Compiler> compiler(Compiler::Create(*compiler_options_, &storage));
282     const DexFile& dex_file = *method->GetDexFile();
283     Handle<mirror::DexCache> dex_cache =
284         hs.NewHandle(GetClassLinker()->FindDexCache(self, dex_file));
285     Handle<mirror::ClassLoader> class_loader = hs.NewHandle(method->GetClassLoader());
286     if (method->IsNative()) {
287       compiled_method = compiler->JniCompile(method->GetAccessFlags(),
288                                              method->GetDexMethodIndex(),
289                                              dex_file,
290                                              dex_cache);
291     } else {
292       compiled_method = compiler->Compile(method->GetCodeItem(),
293                                           method->GetAccessFlags(),
294                                           method->GetClassDefIndex(),
295                                           method->GetDexMethodIndex(),
296                                           class_loader,
297                                           dex_file,
298                                           dex_cache);
299     }
300     CHECK(compiled_method != nullptr) << "Failed to compile " << method->PrettyMethod();
301     CHECK_EQ(reinterpret_cast<OneCompiledMethodStorage*>(compiled_method), &storage);
302   }
303   {
304     TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
305     const void* method_code = MakeExecutable(storage.GetCode(),
306                                              storage.GetStackMap(),
307                                              storage.GetInstructionSet());
308     LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code;
309     GetRuntime()->GetInstrumentation()->InitializeMethodsCode(method, /*aot_code=*/ method_code);
310   }
311 }
312 
JniCompileCode(ArtMethod * method)313 std::vector<uint8_t> CommonCompilerTestImpl::JniCompileCode(ArtMethod* method) {
314   CHECK(method->IsNative());
315   Thread* self = Thread::Current();
316   StackHandleScope<1> hs(self);
317   const DexFile& dex_file = *method->GetDexFile();
318   Handle<mirror::DexCache> dex_cache =
319       hs.NewHandle(GetClassLinker()->FindDexCache(self, dex_file));
320   OneCompiledMethodStorage storage;
321   std::unique_ptr<Compiler> compiler(Compiler::Create(*compiler_options_, &storage));
322   compiler->JniCompile(method->GetAccessFlags(),
323                        method->GetDexMethodIndex(),
324                        dex_file,
325                        dex_cache);
326   ArrayRef<const uint8_t> code = storage.GetCode();
327   return std::vector<uint8_t>(code.begin(), code.end());
328 }
329 
ClearBootImageOption()330 void CommonCompilerTestImpl::ClearBootImageOption() {
331   compiler_options_->image_type_ = CompilerOptions::ImageType::kNone;
332 }
333 
334 }  // namespace art
335