blob: bbb20165664da5645c459f47a568cfe004d8adc8 [file] [log] [blame]
Ian Rogerse63db272014-07-15 15:36:11 -07001/*
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
Vladimir Marko37fd8002021-02-02 14:29:04 +000019#include <android-base/unique_fd.h>
Andreas Gampeb68ed2c2018-06-20 10:39:31 -070020#include <type_traits>
21
Ian Rogersd582fa42014-11-05 23:46:43 -080022#include "arch/instruction_set_features.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_field-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080024#include "art_method-inl.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070025#include "base/callee_save_type.h"
Vladimir Marko2afaff72018-11-30 17:01:50 +000026#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070027#include "base/enums.h"
Vladimir Marko37fd8002021-02-02 14:29:04 +000028#include "base/memfd.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/utils.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "class_linker.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010031#include "compiled_method-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080032#include "dex/descriptors_names.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080033#include "dex/verification_results.h"
Vladimir Marko815d5e52019-03-04 10:18:31 +000034#include "driver/compiled_method_storage.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000035#include "driver/compiler_options.h"
Vladimir Markodc4bcce2018-06-21 16:15:42 +010036#include "jni/java_vm_ext.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "interpreter/interpreter.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070038#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include "mirror/class_loader.h"
Ian Rogerse63db272014-07-15 15:36:11 -070040#include "mirror/dex_cache.h"
41#include "mirror/object-inl.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010042#include "oat_quick_method_header.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070043#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070044#include "thread-current-inl.h"
Vladimir Marko213ee2d2018-06-22 11:56:34 +010045#include "utils/atomic_dex_ref_map-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070046
47namespace art {
48
Vladimir Marko37fd8002021-02-02 14:29:04 +000049class CommonCompilerTestImpl::CodeAndMetadata {
50 public:
51 CodeAndMetadata(CodeAndMetadata&& other) = default;
52
53 CodeAndMetadata(ArrayRef<const uint8_t> code,
54 ArrayRef<const uint8_t> vmap_table,
55 InstructionSet instruction_set) {
56 const uint32_t code_size = code.size();
57 CHECK_NE(code_size, 0u);
58 const uint32_t vmap_table_offset = vmap_table.empty() ? 0u
59 : sizeof(OatQuickMethodHeader) + vmap_table.size();
David Srbecky113d6ea2021-03-02 22:49:46 +000060 OatQuickMethodHeader method_header(vmap_table_offset);
Vladimir Marko37fd8002021-02-02 14:29:04 +000061 const size_t code_alignment = GetInstructionSetAlignment(instruction_set);
62 DCHECK_ALIGNED_PARAM(kPageSize, code_alignment);
63 code_offset_ = RoundUp(vmap_table.size() + sizeof(method_header), code_alignment);
64 const uint32_t capacity = RoundUp(code_offset_ + code_size, kPageSize);
65
66 // Create a memfd handle with sufficient capacity.
Vladimir Marko26bf47a2021-02-04 17:17:27 +000067 android::base::unique_fd mem_fd(art::memfd_create_compat("test code", /*flags=*/ 0));
Vladimir Marko37fd8002021-02-02 14:29:04 +000068 CHECK_GE(mem_fd.get(), 0);
69 int err = ftruncate(mem_fd, capacity);
70 CHECK_EQ(err, 0);
71
72 // Map the memfd contents for read/write.
73 std::string error_msg;
74 rw_map_ = MemMap::MapFile(capacity,
75 PROT_READ | PROT_WRITE,
76 MAP_SHARED,
77 mem_fd,
78 /*start=*/ 0,
79 /*low_4gb=*/ false,
80 /*filename=*/ "test code",
81 &error_msg);
82 CHECK(rw_map_.IsValid()) << error_msg;
83
84 // Store data.
85 uint8_t* code_addr = rw_map_.Begin() + code_offset_;
86 CHECK_ALIGNED_PARAM(code_addr, code_alignment);
87 CHECK_LE(vmap_table_offset, code_offset_);
88 memcpy(code_addr - vmap_table_offset, vmap_table.data(), vmap_table.size());
89 static_assert(std::is_trivially_copyable<OatQuickMethodHeader>::value, "Cannot use memcpy");
90 CHECK_LE(sizeof(method_header), code_offset_);
91 memcpy(code_addr - sizeof(method_header), &method_header, sizeof(method_header));
92 CHECK_LE(code_size, static_cast<size_t>(rw_map_.End() - code_addr));
93 memcpy(code_addr, code.data(), code_size);
94
95 // Sync data.
96 bool success = rw_map_.Sync();
97 CHECK(success);
98 success = FlushCpuCaches(rw_map_.Begin(), rw_map_.End());
99 CHECK(success);
100
101 // Map the data as read/executable.
102 rx_map_ = MemMap::MapFile(capacity,
103 PROT_READ | PROT_EXEC,
104 MAP_SHARED,
105 mem_fd,
106 /*start=*/ 0,
107 /*low_4gb=*/ false,
108 /*filename=*/ "test code",
109 &error_msg);
110 CHECK(rx_map_.IsValid()) << error_msg;
111 }
112
113 const void* GetCodePointer() const {
114 DCHECK(rx_map_.IsValid());
115 DCHECK_LE(code_offset_, rx_map_.Size());
116 return rx_map_.Begin() + code_offset_;
117 }
118
119 private:
120 MemMap rw_map_;
121 MemMap rx_map_;
122 uint32_t code_offset_;
123
124 DISALLOW_COPY_AND_ASSIGN(CodeAndMetadata);
125};
126
Alex Light3ac2f5a2020-11-18 12:23:48 -0800127std::unique_ptr<CompilerOptions> CommonCompilerTestImpl::CreateCompilerOptions(
Vladimir Markof91fc122020-05-13 09:21:00 +0100128 InstructionSet instruction_set, const std::string& variant) {
129 std::unique_ptr<CompilerOptions> compiler_options = std::make_unique<CompilerOptions>();
130 compiler_options->instruction_set_ = instruction_set;
131 std::string error_msg;
132 compiler_options->instruction_set_features_ =
133 InstructionSetFeatures::FromVariant(instruction_set, variant, &error_msg);
134 CHECK(compiler_options->instruction_set_features_ != nullptr) << error_msg;
135 return compiler_options;
136}
137
Alex Light3ac2f5a2020-11-18 12:23:48 -0800138CommonCompilerTestImpl::CommonCompilerTestImpl() {}
139CommonCompilerTestImpl::~CommonCompilerTestImpl() {}
Ian Rogerse63db272014-07-15 15:36:11 -0700140
Vladimir Marko37fd8002021-02-02 14:29:04 +0000141const void* CommonCompilerTestImpl::MakeExecutable(ArrayRef<const uint8_t> code,
142 ArrayRef<const uint8_t> vmap_table,
143 InstructionSet instruction_set) {
144 CHECK_NE(code.size(), 0u);
145 code_and_metadata_.emplace_back(code, vmap_table, instruction_set);
146 return code_and_metadata_.back().GetCodePointer();
147}
148
Alex Light3ac2f5a2020-11-18 12:23:48 -0800149void CommonCompilerTestImpl::MakeExecutable(ArtMethod* method,
150 const CompiledMethod* compiled_method) {
Ian Rogerse63db272014-07-15 15:36:11 -0700151 CHECK(method != nullptr);
Nicolas Geoffrayc8a694d2022-01-17 17:12:38 +0000152 const void* method_code = nullptr;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800153 // If the code size is 0 it means the method was skipped due to profile guided compilation.
154 if (compiled_method != nullptr && compiled_method->GetQuickCode().size() != 0u) {
Vladimir Marko37fd8002021-02-02 14:29:04 +0000155 const void* code_ptr = MakeExecutable(compiled_method->GetQuickCode(),
156 compiled_method->GetVmapTable(),
157 compiled_method->GetInstructionSet());
Nicolas Geoffrayc8a694d2022-01-17 17:12:38 +0000158 method_code =
Vladimir Marko37fd8002021-02-02 14:29:04 +0000159 CompiledMethod::CodePointer(code_ptr, compiled_method->GetInstructionSet());
David Sehr709b0702016-10-13 09:12:37 -0700160 LOG(INFO) << "MakeExecutable " << method->PrettyMethod() << " code=" << method_code;
Ian Rogerse63db272014-07-15 15:36:11 -0700161 }
Nicolas Geoffrayc8a694d2022-01-17 17:12:38 +0000162 Runtime::Current()->GetInstrumentation()->InitializeMethodsCode(
163 method, /*aot_code=*/ method_code);
Ian Rogerse63db272014-07-15 15:36:11 -0700164}
165
Alex Light3ac2f5a2020-11-18 12:23:48 -0800166void CommonCompilerTestImpl::SetUp() {
Ian Rogerse63db272014-07-15 15:36:11 -0700167 {
168 ScopedObjectAccess soa(Thread::Current());
169
Alex Light3ac2f5a2020-11-18 12:23:48 -0800170 Runtime* runtime = GetRuntime();
171 runtime->SetInstructionSet(instruction_set_);
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700172 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); ++i) {
173 CalleeSaveType type = CalleeSaveType(i);
Alex Light3ac2f5a2020-11-18 12:23:48 -0800174 if (!runtime->HasCalleeSaveMethod(type)) {
175 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(), type);
Ian Rogerse63db272014-07-15 15:36:11 -0700176 }
177 }
Ian Rogerse63db272014-07-15 15:36:11 -0700178 }
Mathieu Chartierceb07b32015-12-10 09:33:21 -0800179}
180
Alex Light3ac2f5a2020-11-18 12:23:48 -0800181void CommonCompilerTestImpl::ApplyInstructionSet() {
Vladimir Markoa0431112018-06-25 09:32:54 +0100182 // Copy local instruction_set_ and instruction_set_features_ to *compiler_options_;
183 CHECK(instruction_set_features_ != nullptr);
184 if (instruction_set_ == InstructionSet::kThumb2) {
185 CHECK_EQ(InstructionSet::kArm, instruction_set_features_->GetInstructionSet());
186 } else {
187 CHECK_EQ(instruction_set_, instruction_set_features_->GetInstructionSet());
188 }
189 compiler_options_->instruction_set_ = instruction_set_;
190 compiler_options_->instruction_set_features_ =
191 InstructionSetFeatures::FromBitmap(instruction_set_, instruction_set_features_->AsBitmap());
192 CHECK(compiler_options_->instruction_set_features_->Equals(instruction_set_features_.get()));
193}
194
Alex Light3ac2f5a2020-11-18 12:23:48 -0800195void CommonCompilerTestImpl::OverrideInstructionSetFeatures(InstructionSet instruction_set,
196 const std::string& variant) {
Vladimir Markoa0431112018-06-25 09:32:54 +0100197 instruction_set_ = instruction_set;
198 std::string error_msg;
199 instruction_set_features_ =
200 InstructionSetFeatures::FromVariant(instruction_set, variant, &error_msg);
201 CHECK(instruction_set_features_ != nullptr) << error_msg;
202
203 if (compiler_options_ != nullptr) {
204 ApplyInstructionSet();
205 }
206}
207
Alex Light3ac2f5a2020-11-18 12:23:48 -0800208void CommonCompilerTestImpl::SetUpRuntimeOptionsImpl() {
Ian Rogerse63db272014-07-15 15:36:11 -0700209 compiler_options_.reset(new CompilerOptions);
Nicolas Geoffray9e050ab2021-07-14 14:59:25 +0100210 verification_results_.reset(new VerificationResults());
Vladimir Marko815d5e52019-03-04 10:18:31 +0000211 ApplyInstructionSet();
Ian Rogerse63db272014-07-15 15:36:11 -0700212}
213
Alex Light3ac2f5a2020-11-18 12:23:48 -0800214Compiler::Kind CommonCompilerTestImpl::GetCompilerKind() const {
Roland Levillainbbcc01a2015-06-30 14:16:48 +0100215 return compiler_kind_;
216}
217
Alex Light3ac2f5a2020-11-18 12:23:48 -0800218void CommonCompilerTestImpl::SetCompilerKind(Compiler::Kind compiler_kind) {
Roland Levillainbbcc01a2015-06-30 14:16:48 +0100219 compiler_kind_ = compiler_kind;
220}
221
Alex Light3ac2f5a2020-11-18 12:23:48 -0800222void CommonCompilerTestImpl::TearDown() {
Vladimir Marko37fd8002021-02-02 14:29:04 +0000223 code_and_metadata_.clear();
Ian Rogerse63db272014-07-15 15:36:11 -0700224 verification_results_.reset();
225 compiler_options_.reset();
Ian Rogerse63db272014-07-15 15:36:11 -0700226}
227
Alex Light3ac2f5a2020-11-18 12:23:48 -0800228void CommonCompilerTestImpl::CompileMethod(ArtMethod* method) {
Ian Rogerse63db272014-07-15 15:36:11 -0700229 CHECK(method != nullptr);
Alex Light3ac2f5a2020-11-18 12:23:48 -0800230 TimingLogger timings("CommonCompilerTestImpl::CompileMethod", false, false);
Ian Rogerse63db272014-07-15 15:36:11 -0700231 TimingLogger::ScopedTiming t(__FUNCTION__, &timings);
Vladimir Marko815d5e52019-03-04 10:18:31 +0000232 CompiledMethodStorage storage(/*swap_fd=*/ -1);
Vladimir Marko4b2d1c52019-03-06 11:21:11 +0000233 CompiledMethod* compiled_method = nullptr;
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100234 {
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100235 DCHECK(!Runtime::Current()->IsStarted());
Vladimir Marko815d5e52019-03-04 10:18:31 +0000236 Thread* self = Thread::Current();
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100237 StackHandleScope<2> hs(self);
Vladimir Marko815d5e52019-03-04 10:18:31 +0000238 std::unique_ptr<Compiler> compiler(
239 Compiler::Create(*compiler_options_, &storage, compiler_kind_));
240 const DexFile& dex_file = *method->GetDexFile();
Alex Light3ac2f5a2020-11-18 12:23:48 -0800241 Handle<mirror::DexCache> dex_cache =
242 hs.NewHandle(GetClassLinker()->FindDexCache(self, dex_file));
Vladimir Marko815d5e52019-03-04 10:18:31 +0000243 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(method->GetClassLoader());
Vladimir Marko2afaff72018-11-30 17:01:50 +0000244 compiler_options_->verification_results_ = verification_results_.get();
Vladimir Marko815d5e52019-03-04 10:18:31 +0000245 if (method->IsNative()) {
246 compiled_method = compiler->JniCompile(method->GetAccessFlags(),
247 method->GetDexMethodIndex(),
248 dex_file,
249 dex_cache);
250 } else {
Vladimir Marko815d5e52019-03-04 10:18:31 +0000251 compiled_method = compiler->Compile(method->GetCodeItem(),
252 method->GetAccessFlags(),
253 method->GetInvokeType(),
254 method->GetClassDefIndex(),
255 method->GetDexMethodIndex(),
256 class_loader,
257 dex_file,
258 dex_cache);
259 }
Vladimir Marko2afaff72018-11-30 17:01:50 +0000260 compiler_options_->verification_results_ = nullptr;
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100261 }
Vladimir Marko4b2d1c52019-03-06 11:21:11 +0000262 CHECK(method != nullptr);
263 {
264 TimingLogger::ScopedTiming t2("MakeExecutable", &timings);
265 MakeExecutable(method, compiled_method);
266 }
267 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(&storage, compiled_method);
Ian Rogerse63db272014-07-15 15:36:11 -0700268}
269
Alex Light3ac2f5a2020-11-18 12:23:48 -0800270void CommonCompilerTestImpl::CompileDirectMethod(Handle<mirror::ClassLoader> class_loader,
271 const char* class_name,
272 const char* method_name,
273 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700274 std::string class_descriptor(DotToDescriptor(class_name));
275 Thread* self = Thread::Current();
Alex Light3ac2f5a2020-11-18 12:23:48 -0800276 ClassLinker* class_linker = GetClassLinker();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100277 ObjPtr<mirror::Class> klass =
Alex Light3ac2f5a2020-11-18 12:23:48 -0800278 class_linker->FindClass(self, class_descriptor.c_str(), class_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700279 CHECK(klass != nullptr) << "Class not found " << class_name;
Alex Light3ac2f5a2020-11-18 12:23:48 -0800280 auto pointer_size = class_linker->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100281 ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size);
282 CHECK(method != nullptr && method->IsDirect()) << "Direct method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700283 << class_name << "." << method_name << signature;
284 CompileMethod(method);
285}
286
Alex Light3ac2f5a2020-11-18 12:23:48 -0800287void CommonCompilerTestImpl::CompileVirtualMethod(Handle<mirror::ClassLoader> class_loader,
288 const char* class_name,
289 const char* method_name,
290 const char* signature) {
Ian Rogerse63db272014-07-15 15:36:11 -0700291 std::string class_descriptor(DotToDescriptor(class_name));
292 Thread* self = Thread::Current();
Alex Light3ac2f5a2020-11-18 12:23:48 -0800293 ClassLinker* class_linker = GetClassLinker();
Vladimir Markoe9987b02018-05-22 16:26:43 +0100294 ObjPtr<mirror::Class> klass =
Alex Light3ac2f5a2020-11-18 12:23:48 -0800295 class_linker->FindClass(self, class_descriptor.c_str(), class_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700296 CHECK(klass != nullptr) << "Class not found " << class_name;
Alex Light3ac2f5a2020-11-18 12:23:48 -0800297 auto pointer_size = class_linker->GetImagePointerSize();
Vladimir Markoba118822017-06-12 15:41:56 +0100298 ArtMethod* method = klass->FindClassMethod(method_name, signature, pointer_size);
299 CHECK(method != nullptr && !method->IsDirect()) << "Virtual method not found: "
Ian Rogerse63db272014-07-15 15:36:11 -0700300 << class_name << "." << method_name << signature;
301 CompileMethod(method);
302}
303
Alex Light3ac2f5a2020-11-18 12:23:48 -0800304void CommonCompilerTestImpl::ClearBootImageOption() {
Vladimir Marko9c4b9702018-11-14 15:09:02 +0000305 compiler_options_->image_type_ = CompilerOptions::ImageType::kNone;
Vladimir Markoa0431112018-06-25 09:32:54 +0100306}
307
Ian Rogerse63db272014-07-15 15:36:11 -0700308} // namespace art