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 "compiler_driver.h"
18
19 #define ATRACE_TAG ATRACE_TAG_DALVIK
20 #include <utils/Trace.h>
21
22 #include <vector>
23 #include <unistd.h>
24
25 #ifndef __APPLE__
26 #include <malloc.h> // For mallinfo
27 #endif
28
29 #include "base/stl_util.h"
30 #include "base/timing_logger.h"
31 #include "class_linker.h"
32 #include "compiled_class.h"
33 #include "compiler.h"
34 #include "compiler_driver-inl.h"
35 #include "dex_compilation_unit.h"
36 #include "dex_file-inl.h"
37 #include "dex/verification_results.h"
38 #include "dex/verified_method.h"
39 #include "dex/quick/dex_file_method_inliner.h"
40 #include "driver/compiler_options.h"
41 #include "jni_internal.h"
42 #include "object_lock.h"
43 #include "profiler.h"
44 #include "runtime.h"
45 #include "gc/accounting/card_table-inl.h"
46 #include "gc/accounting/heap_bitmap.h"
47 #include "gc/space/space.h"
48 #include "mirror/art_field-inl.h"
49 #include "mirror/art_method-inl.h"
50 #include "mirror/class_loader.h"
51 #include "mirror/class-inl.h"
52 #include "mirror/dex_cache-inl.h"
53 #include "mirror/object-inl.h"
54 #include "mirror/object_array-inl.h"
55 #include "mirror/throwable.h"
56 #include "scoped_thread_state_change.h"
57 #include "ScopedLocalRef.h"
58 #include "handle_scope-inl.h"
59 #include "thread.h"
60 #include "thread_pool.h"
61 #include "trampolines/trampoline_compiler.h"
62 #include "transaction.h"
63 #include "utils/swap_space.h"
64 #include "verifier/method_verifier.h"
65 #include "verifier/method_verifier-inl.h"
66
67 namespace art {
68
69 static constexpr bool kTimeCompileMethod = !kIsDebugBuild;
70
Percentage(size_t x,size_t y)71 static double Percentage(size_t x, size_t y) {
72 return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
73 }
74
DumpStat(size_t x,size_t y,const char * str)75 static void DumpStat(size_t x, size_t y, const char* str) {
76 if (x == 0 && y == 0) {
77 return;
78 }
79 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
80 }
81
82 class CompilerDriver::AOTCompilationStats {
83 public:
AOTCompilationStats()84 AOTCompilationStats()
85 : stats_lock_("AOT compilation statistics lock"),
86 types_in_dex_cache_(0), types_not_in_dex_cache_(0),
87 strings_in_dex_cache_(0), strings_not_in_dex_cache_(0),
88 resolved_types_(0), unresolved_types_(0),
89 resolved_instance_fields_(0), unresolved_instance_fields_(0),
90 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0),
91 type_based_devirtualization_(0),
92 safe_casts_(0), not_safe_casts_(0) {
93 for (size_t i = 0; i <= kMaxInvokeType; i++) {
94 resolved_methods_[i] = 0;
95 unresolved_methods_[i] = 0;
96 virtual_made_direct_[i] = 0;
97 direct_calls_to_boot_[i] = 0;
98 direct_methods_to_boot_[i] = 0;
99 }
100 }
101
Dump()102 void Dump() {
103 DumpStat(types_in_dex_cache_, types_not_in_dex_cache_, "types known to be in dex cache");
104 DumpStat(strings_in_dex_cache_, strings_not_in_dex_cache_, "strings known to be in dex cache");
105 DumpStat(resolved_types_, unresolved_types_, "types resolved");
106 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
107 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
108 "static fields resolved");
109 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
110 "static fields local to a class");
111 DumpStat(safe_casts_, not_safe_casts_, "check-casts removed based on type information");
112 // Note, the code below subtracts the stat value so that when added to the stat value we have
113 // 100% of samples. TODO: clean this up.
114 DumpStat(type_based_devirtualization_,
115 resolved_methods_[kVirtual] + unresolved_methods_[kVirtual] +
116 resolved_methods_[kInterface] + unresolved_methods_[kInterface] -
117 type_based_devirtualization_,
118 "virtual/interface calls made direct based on type information");
119
120 for (size_t i = 0; i <= kMaxInvokeType; i++) {
121 std::ostringstream oss;
122 oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
123 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
124 if (virtual_made_direct_[i] > 0) {
125 std::ostringstream oss2;
126 oss2 << static_cast<InvokeType>(i) << " methods made direct";
127 DumpStat(virtual_made_direct_[i],
128 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
129 oss2.str().c_str());
130 }
131 if (direct_calls_to_boot_[i] > 0) {
132 std::ostringstream oss2;
133 oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
134 DumpStat(direct_calls_to_boot_[i],
135 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
136 oss2.str().c_str());
137 }
138 if (direct_methods_to_boot_[i] > 0) {
139 std::ostringstream oss2;
140 oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
141 DumpStat(direct_methods_to_boot_[i],
142 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
143 oss2.str().c_str());
144 }
145 }
146 }
147
148 // Allow lossy statistics in non-debug builds.
149 #ifndef NDEBUG
150 #define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_)
151 #else
152 #define STATS_LOCK()
153 #endif
154
TypeInDexCache()155 void TypeInDexCache() {
156 STATS_LOCK();
157 types_in_dex_cache_++;
158 }
159
TypeNotInDexCache()160 void TypeNotInDexCache() {
161 STATS_LOCK();
162 types_not_in_dex_cache_++;
163 }
164
StringInDexCache()165 void StringInDexCache() {
166 STATS_LOCK();
167 strings_in_dex_cache_++;
168 }
169
StringNotInDexCache()170 void StringNotInDexCache() {
171 STATS_LOCK();
172 strings_not_in_dex_cache_++;
173 }
174
TypeDoesntNeedAccessCheck()175 void TypeDoesntNeedAccessCheck() {
176 STATS_LOCK();
177 resolved_types_++;
178 }
179
TypeNeedsAccessCheck()180 void TypeNeedsAccessCheck() {
181 STATS_LOCK();
182 unresolved_types_++;
183 }
184
ResolvedInstanceField()185 void ResolvedInstanceField() {
186 STATS_LOCK();
187 resolved_instance_fields_++;
188 }
189
UnresolvedInstanceField()190 void UnresolvedInstanceField() {
191 STATS_LOCK();
192 unresolved_instance_fields_++;
193 }
194
ResolvedLocalStaticField()195 void ResolvedLocalStaticField() {
196 STATS_LOCK();
197 resolved_local_static_fields_++;
198 }
199
ResolvedStaticField()200 void ResolvedStaticField() {
201 STATS_LOCK();
202 resolved_static_fields_++;
203 }
204
UnresolvedStaticField()205 void UnresolvedStaticField() {
206 STATS_LOCK();
207 unresolved_static_fields_++;
208 }
209
210 // Indicate that type information from the verifier led to devirtualization.
PreciseTypeDevirtualization()211 void PreciseTypeDevirtualization() {
212 STATS_LOCK();
213 type_based_devirtualization_++;
214 }
215
216 // Indicate that a method of the given type was resolved at compile time.
ResolvedMethod(InvokeType type)217 void ResolvedMethod(InvokeType type) {
218 DCHECK_LE(type, kMaxInvokeType);
219 STATS_LOCK();
220 resolved_methods_[type]++;
221 }
222
223 // Indicate that a method of the given type was unresolved at compile time as it was in an
224 // unknown dex file.
UnresolvedMethod(InvokeType type)225 void UnresolvedMethod(InvokeType type) {
226 DCHECK_LE(type, kMaxInvokeType);
227 STATS_LOCK();
228 unresolved_methods_[type]++;
229 }
230
231 // Indicate that a type of virtual method dispatch has been converted into a direct method
232 // dispatch.
VirtualMadeDirect(InvokeType type)233 void VirtualMadeDirect(InvokeType type) {
234 DCHECK(type == kVirtual || type == kInterface || type == kSuper);
235 STATS_LOCK();
236 virtual_made_direct_[type]++;
237 }
238
239 // Indicate that a method of the given type was able to call directly into boot.
DirectCallsToBoot(InvokeType type)240 void DirectCallsToBoot(InvokeType type) {
241 DCHECK_LE(type, kMaxInvokeType);
242 STATS_LOCK();
243 direct_calls_to_boot_[type]++;
244 }
245
246 // Indicate that a method of the given type was able to be resolved directly from boot.
DirectMethodsToBoot(InvokeType type)247 void DirectMethodsToBoot(InvokeType type) {
248 DCHECK_LE(type, kMaxInvokeType);
249 STATS_LOCK();
250 direct_methods_to_boot_[type]++;
251 }
252
ProcessedInvoke(InvokeType type,int flags)253 void ProcessedInvoke(InvokeType type, int flags) {
254 STATS_LOCK();
255 if (flags == 0) {
256 unresolved_methods_[type]++;
257 } else {
258 DCHECK_NE((flags & kFlagMethodResolved), 0);
259 resolved_methods_[type]++;
260 if ((flags & kFlagVirtualMadeDirect) != 0) {
261 virtual_made_direct_[type]++;
262 if ((flags & kFlagPreciseTypeDevirtualization) != 0) {
263 type_based_devirtualization_++;
264 }
265 } else {
266 DCHECK_EQ((flags & kFlagPreciseTypeDevirtualization), 0);
267 }
268 if ((flags & kFlagDirectCallToBoot) != 0) {
269 direct_calls_to_boot_[type]++;
270 }
271 if ((flags & kFlagDirectMethodToBoot) != 0) {
272 direct_methods_to_boot_[type]++;
273 }
274 }
275 }
276
277 // A check-cast could be eliminated due to verifier type analysis.
SafeCast()278 void SafeCast() {
279 STATS_LOCK();
280 safe_casts_++;
281 }
282
283 // A check-cast couldn't be eliminated due to verifier type analysis.
NotASafeCast()284 void NotASafeCast() {
285 STATS_LOCK();
286 not_safe_casts_++;
287 }
288
289 private:
290 Mutex stats_lock_;
291
292 size_t types_in_dex_cache_;
293 size_t types_not_in_dex_cache_;
294
295 size_t strings_in_dex_cache_;
296 size_t strings_not_in_dex_cache_;
297
298 size_t resolved_types_;
299 size_t unresolved_types_;
300
301 size_t resolved_instance_fields_;
302 size_t unresolved_instance_fields_;
303
304 size_t resolved_local_static_fields_;
305 size_t resolved_static_fields_;
306 size_t unresolved_static_fields_;
307 // Type based devirtualization for invoke interface and virtual.
308 size_t type_based_devirtualization_;
309
310 size_t resolved_methods_[kMaxInvokeType + 1];
311 size_t unresolved_methods_[kMaxInvokeType + 1];
312 size_t virtual_made_direct_[kMaxInvokeType + 1];
313 size_t direct_calls_to_boot_[kMaxInvokeType + 1];
314 size_t direct_methods_to_boot_[kMaxInvokeType + 1];
315
316 size_t safe_casts_;
317 size_t not_safe_casts_;
318
319 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
320 };
321
322
323 extern "C" art::CompiledMethod* ArtCompileDEX(art::CompilerDriver& compiler,
324 const art::DexFile::CodeItem* code_item,
325 uint32_t access_flags,
326 art::InvokeType invoke_type,
327 uint16_t class_def_idx,
328 uint32_t method_idx,
329 jobject class_loader,
330 const art::DexFile& dex_file);
331
CompilerDriver(const CompilerOptions * compiler_options,VerificationResults * verification_results,DexFileToMethodInlinerMap * method_inliner_map,Compiler::Kind compiler_kind,InstructionSet instruction_set,InstructionSetFeatures instruction_set_features,bool image,std::set<std::string> * image_classes,std::set<std::string> * compiled_classes,size_t thread_count,bool dump_stats,bool dump_passes,CumulativeLogger * timer,int swap_fd,std::string profile_file)332 CompilerDriver::CompilerDriver(const CompilerOptions* compiler_options,
333 VerificationResults* verification_results,
334 DexFileToMethodInlinerMap* method_inliner_map,
335 Compiler::Kind compiler_kind,
336 InstructionSet instruction_set,
337 InstructionSetFeatures instruction_set_features,
338 bool image, std::set<std::string>* image_classes,
339 std::set<std::string>* compiled_classes, size_t thread_count,
340 bool dump_stats, bool dump_passes, CumulativeLogger* timer,
341 int swap_fd, std::string profile_file)
342 : swap_space_(swap_fd == -1 ? nullptr : new SwapSpace(swap_fd, 10 * MB)),
343 swap_space_allocator_(new SwapAllocator<void>(swap_space_.get())),
344 profile_present_(false), compiler_options_(compiler_options),
345 verification_results_(verification_results),
346 method_inliner_map_(method_inliner_map),
347 compiler_(Compiler::Create(this, compiler_kind)),
348 instruction_set_(instruction_set),
349 instruction_set_features_(instruction_set_features),
350 freezing_constructor_lock_("freezing constructor lock"),
351 compiled_classes_lock_("compiled classes lock"),
352 compiled_methods_lock_("compiled method lock"),
353 compiled_methods_(MethodTable::key_compare()),
354 image_(image),
355 image_classes_(image_classes),
356 classes_to_compile_(compiled_classes),
357 thread_count_(thread_count),
358 start_ns_(0),
359 stats_(new AOTCompilationStats),
360 dump_stats_(dump_stats),
361 dump_passes_(dump_passes),
362 timings_logger_(timer),
363 compiler_library_(nullptr),
364 compiler_context_(nullptr),
365 compiler_enable_auto_elf_loading_(nullptr),
366 compiler_get_method_code_addr_(nullptr),
367 support_boot_image_fixup_(instruction_set != kMips),
368 cfi_info_(nullptr),
369 // Use actual deduping only if we don't use swap.
370 dedupe_code_("dedupe code", *swap_space_allocator_),
371 dedupe_mapping_table_("dedupe mapping table", *swap_space_allocator_),
372 dedupe_vmap_table_("dedupe vmap table", *swap_space_allocator_),
373 dedupe_gc_map_("dedupe gc map", *swap_space_allocator_),
374 dedupe_cfi_info_("dedupe cfi info", *swap_space_allocator_) {
375 DCHECK(compiler_options_ != nullptr);
376 DCHECK(verification_results_ != nullptr);
377 DCHECK(method_inliner_map_ != nullptr);
378
379 CHECK_PTHREAD_CALL(pthread_key_create, (&tls_key_, nullptr), "compiler tls key");
380
381 dex_to_dex_compiler_ = reinterpret_cast<DexToDexCompilerFn>(ArtCompileDEX);
382
383 compiler_->Init();
384
385 CHECK(!Runtime::Current()->IsStarted());
386 if (image_) {
387 CHECK(image_classes_.get() != nullptr);
388 } else {
389 CHECK(image_classes_.get() == nullptr);
390 }
391
392 // Are we generating CFI information?
393 if (compiler_options->GetGenerateGDBInformation()) {
394 cfi_info_.reset(compiler_->GetCallFrameInformationInitialization(*this));
395 }
396
397 // Read the profile file if one is provided.
398 if (!profile_file.empty()) {
399 profile_present_ = profile_file_.LoadFile(profile_file);
400 if (profile_present_) {
401 LOG(INFO) << "Using profile data form file " << profile_file;
402 } else {
403 LOG(INFO) << "Failed to load profile file " << profile_file;
404 }
405 }
406 }
407
DeduplicateCode(const ArrayRef<const uint8_t> & code)408 SwapVector<uint8_t>* CompilerDriver::DeduplicateCode(const ArrayRef<const uint8_t>& code) {
409 return dedupe_code_.Add(Thread::Current(), code);
410 }
411
DeduplicateMappingTable(const ArrayRef<const uint8_t> & code)412 SwapVector<uint8_t>* CompilerDriver::DeduplicateMappingTable(const ArrayRef<const uint8_t>& code) {
413 return dedupe_mapping_table_.Add(Thread::Current(), code);
414 }
415
DeduplicateVMapTable(const ArrayRef<const uint8_t> & code)416 SwapVector<uint8_t>* CompilerDriver::DeduplicateVMapTable(const ArrayRef<const uint8_t>& code) {
417 return dedupe_vmap_table_.Add(Thread::Current(), code);
418 }
419
DeduplicateGCMap(const ArrayRef<const uint8_t> & code)420 SwapVector<uint8_t>* CompilerDriver::DeduplicateGCMap(const ArrayRef<const uint8_t>& code) {
421 return dedupe_gc_map_.Add(Thread::Current(), code);
422 }
423
DeduplicateCFIInfo(const ArrayRef<const uint8_t> & cfi_info)424 SwapVector<uint8_t>* CompilerDriver::DeduplicateCFIInfo(const ArrayRef<const uint8_t>& cfi_info) {
425 return dedupe_cfi_info_.Add(Thread::Current(), cfi_info);
426 }
427
~CompilerDriver()428 CompilerDriver::~CompilerDriver() {
429 Thread* self = Thread::Current();
430 {
431 MutexLock mu(self, compiled_classes_lock_);
432 STLDeleteValues(&compiled_classes_);
433 STLDeleteElements(&code_to_patch_);
434 STLDeleteElements(&methods_to_patch_);
435 STLDeleteElements(&classes_to_patch_);
436 STLDeleteElements(&strings_to_patch_);
437
438 for (auto& pair : compiled_methods_) {
439 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(this, pair.second);
440 }
441 }
442 CHECK_PTHREAD_CALL(pthread_key_delete, (tls_key_), "delete tls key");
443 compiler_->UnInit();
444 }
445
GetTls()446 CompilerTls* CompilerDriver::GetTls() {
447 // Lazily create thread-local storage
448 CompilerTls* res = static_cast<CompilerTls*>(pthread_getspecific(tls_key_));
449 if (res == nullptr) {
450 res = new CompilerTls();
451 CHECK_PTHREAD_CALL(pthread_setspecific, (tls_key_, res), "compiler tls");
452 }
453 return res;
454 }
455
456 #define CREATE_TRAMPOLINE(type, abi, offset) \
457 if (Is64BitInstructionSet(instruction_set_)) { \
458 return CreateTrampoline64(instruction_set_, abi, \
459 type ## _ENTRYPOINT_OFFSET(8, offset)); \
460 } else { \
461 return CreateTrampoline32(instruction_set_, abi, \
462 type ## _ENTRYPOINT_OFFSET(4, offset)); \
463 }
464
CreateInterpreterToInterpreterBridge() const465 const std::vector<uint8_t>* CompilerDriver::CreateInterpreterToInterpreterBridge() const {
466 CREATE_TRAMPOLINE(INTERPRETER, kInterpreterAbi, pInterpreterToInterpreterBridge)
467 }
468
CreateInterpreterToCompiledCodeBridge() const469 const std::vector<uint8_t>* CompilerDriver::CreateInterpreterToCompiledCodeBridge() const {
470 CREATE_TRAMPOLINE(INTERPRETER, kInterpreterAbi, pInterpreterToCompiledCodeBridge)
471 }
472
CreateJniDlsymLookup() const473 const std::vector<uint8_t>* CompilerDriver::CreateJniDlsymLookup() const {
474 CREATE_TRAMPOLINE(JNI, kJniAbi, pDlsymLookup)
475 }
476
CreatePortableImtConflictTrampoline() const477 const std::vector<uint8_t>* CompilerDriver::CreatePortableImtConflictTrampoline() const {
478 CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableImtConflictTrampoline)
479 }
480
CreatePortableResolutionTrampoline() const481 const std::vector<uint8_t>* CompilerDriver::CreatePortableResolutionTrampoline() const {
482 CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableResolutionTrampoline)
483 }
484
CreatePortableToInterpreterBridge() const485 const std::vector<uint8_t>* CompilerDriver::CreatePortableToInterpreterBridge() const {
486 CREATE_TRAMPOLINE(PORTABLE, kPortableAbi, pPortableToInterpreterBridge)
487 }
488
CreateQuickGenericJniTrampoline() const489 const std::vector<uint8_t>* CompilerDriver::CreateQuickGenericJniTrampoline() const {
490 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickGenericJniTrampoline)
491 }
492
CreateQuickImtConflictTrampoline() const493 const std::vector<uint8_t>* CompilerDriver::CreateQuickImtConflictTrampoline() const {
494 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickImtConflictTrampoline)
495 }
496
CreateQuickResolutionTrampoline() const497 const std::vector<uint8_t>* CompilerDriver::CreateQuickResolutionTrampoline() const {
498 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickResolutionTrampoline)
499 }
500
CreateQuickToInterpreterBridge() const501 const std::vector<uint8_t>* CompilerDriver::CreateQuickToInterpreterBridge() const {
502 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickToInterpreterBridge)
503 }
504 #undef CREATE_TRAMPOLINE
505
CompileAll(jobject class_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)506 void CompilerDriver::CompileAll(jobject class_loader,
507 const std::vector<const DexFile*>& dex_files,
508 TimingLogger* timings) {
509 DCHECK(!Runtime::Current()->IsStarted());
510 std::unique_ptr<ThreadPool> thread_pool(new ThreadPool("Compiler driver thread pool", thread_count_ - 1));
511 VLOG(compiler) << "Before precompile " << GetMemoryUsageString(false);
512 PreCompile(class_loader, dex_files, thread_pool.get(), timings);
513 Compile(class_loader, dex_files, thread_pool.get(), timings);
514 if (dump_stats_) {
515 stats_->Dump();
516 }
517 }
518
GetDexToDexCompilationlevel(Thread * self,Handle<mirror::ClassLoader> class_loader,const DexFile & dex_file,const DexFile::ClassDef & class_def)519 static DexToDexCompilationLevel GetDexToDexCompilationlevel(
520 Thread* self, Handle<mirror::ClassLoader> class_loader, const DexFile& dex_file,
521 const DexFile::ClassDef& class_def) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
522 const char* descriptor = dex_file.GetClassDescriptor(class_def);
523 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
524 mirror::Class* klass = class_linker->FindClass(self, descriptor, class_loader);
525 if (klass == nullptr) {
526 CHECK(self->IsExceptionPending());
527 self->ClearException();
528 return kDontDexToDexCompile;
529 }
530 // DexToDex at the kOptimize level may introduce quickened opcodes, which replace symbolic
531 // references with actual offsets. We cannot re-verify such instructions.
532 //
533 // We store the verification information in the class status in the oat file, which the linker
534 // can validate (checksums) and use to skip load-time verification. It is thus safe to
535 // optimize when a class has been fully verified before.
536 if (klass->IsVerified()) {
537 // Class is verified so we can enable DEX-to-DEX compilation for performance.
538 return kOptimize;
539 } else if (klass->IsCompileTimeVerified()) {
540 // Class verification has soft-failed. Anyway, ensure at least correctness.
541 DCHECK_EQ(klass->GetStatus(), mirror::Class::kStatusRetryVerificationAtRuntime);
542 return kRequired;
543 } else {
544 // Class verification has failed: do not run DEX-to-DEX compilation.
545 return kDontDexToDexCompile;
546 }
547 }
548
CompileOne(mirror::ArtMethod * method,TimingLogger * timings)549 void CompilerDriver::CompileOne(mirror::ArtMethod* method, TimingLogger* timings) {
550 DCHECK(!Runtime::Current()->IsStarted());
551 Thread* self = Thread::Current();
552 jobject jclass_loader;
553 const DexFile* dex_file;
554 uint16_t class_def_idx;
555 uint32_t method_idx = method->GetDexMethodIndex();
556 uint32_t access_flags = method->GetAccessFlags();
557 InvokeType invoke_type = method->GetInvokeType();
558 {
559 ScopedObjectAccessUnchecked soa(self);
560 ScopedLocalRef<jobject>
561 local_class_loader(soa.Env(),
562 soa.AddLocalReference<jobject>(method->GetDeclaringClass()->GetClassLoader()));
563 jclass_loader = soa.Env()->NewGlobalRef(local_class_loader.get());
564 // Find the dex_file
565 dex_file = method->GetDexFile();
566 class_def_idx = method->GetClassDefIndex();
567 }
568 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
569 self->TransitionFromRunnableToSuspended(kNative);
570
571 std::vector<const DexFile*> dex_files;
572 dex_files.push_back(dex_file);
573
574 std::unique_ptr<ThreadPool> thread_pool(new ThreadPool("Compiler driver thread pool", 0U));
575 PreCompile(jclass_loader, dex_files, thread_pool.get(), timings);
576
577 // Can we run DEX-to-DEX compiler on this class ?
578 DexToDexCompilationLevel dex_to_dex_compilation_level = kDontDexToDexCompile;
579 {
580 ScopedObjectAccess soa(Thread::Current());
581 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
582 StackHandleScope<1> hs(soa.Self());
583 Handle<mirror::ClassLoader> class_loader(
584 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
585 dex_to_dex_compilation_level = GetDexToDexCompilationlevel(self, class_loader, *dex_file,
586 class_def);
587 }
588 CompileMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx, jclass_loader,
589 *dex_file, dex_to_dex_compilation_level, true);
590
591 self->GetJniEnv()->DeleteGlobalRef(jclass_loader);
592
593 self->TransitionFromSuspendedToRunnable();
594 }
595
Resolve(jobject class_loader,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,TimingLogger * timings)596 void CompilerDriver::Resolve(jobject class_loader, const std::vector<const DexFile*>& dex_files,
597 ThreadPool* thread_pool, TimingLogger* timings) {
598 for (size_t i = 0; i != dex_files.size(); ++i) {
599 const DexFile* dex_file = dex_files[i];
600 CHECK(dex_file != nullptr);
601 ResolveDexFile(class_loader, *dex_file, dex_files, thread_pool, timings);
602 }
603 }
604
PreCompile(jobject class_loader,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,TimingLogger * timings)605 void CompilerDriver::PreCompile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
606 ThreadPool* thread_pool, TimingLogger* timings) {
607 LoadImageClasses(timings);
608 VLOG(compiler) << "LoadImageClasses: " << GetMemoryUsageString(false);
609
610 Resolve(class_loader, dex_files, thread_pool, timings);
611 VLOG(compiler) << "Resolve: " << GetMemoryUsageString(false);
612
613 if (!compiler_options_->IsVerificationEnabled()) {
614 VLOG(compiler) << "Verify none mode specified, skipping verification.";
615 SetVerified(class_loader, dex_files, thread_pool, timings);
616 return;
617 }
618
619 Verify(class_loader, dex_files, thread_pool, timings);
620 VLOG(compiler) << "Verify: " << GetMemoryUsageString(false);
621
622 InitializeClasses(class_loader, dex_files, thread_pool, timings);
623 VLOG(compiler) << "InitializeClasses: " << GetMemoryUsageString(false);
624
625 UpdateImageClasses(timings);
626 VLOG(compiler) << "UpdateImageClasses: " << GetMemoryUsageString(false);
627 }
628
IsImageClass(const char * descriptor) const629 bool CompilerDriver::IsImageClass(const char* descriptor) const {
630 if (!IsImage()) {
631 return true;
632 } else {
633 return image_classes_->find(descriptor) != image_classes_->end();
634 }
635 }
636
IsClassToCompile(const char * descriptor) const637 bool CompilerDriver::IsClassToCompile(const char* descriptor) const {
638 if (!IsImage()) {
639 return true;
640 } else {
641 if (classes_to_compile_ == nullptr) {
642 return true;
643 }
644 return classes_to_compile_->find(descriptor) != classes_to_compile_->end();
645 }
646 }
647
ResolveExceptionsForMethod(MethodHelper * mh,std::set<std::pair<uint16_t,const DexFile * >> & exceptions_to_resolve)648 static void ResolveExceptionsForMethod(MethodHelper* mh,
649 std::set<std::pair<uint16_t, const DexFile*>>& exceptions_to_resolve)
650 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
651 const DexFile::CodeItem* code_item = mh->GetMethod()->GetCodeItem();
652 if (code_item == nullptr) {
653 return; // native or abstract method
654 }
655 if (code_item->tries_size_ == 0) {
656 return; // nothing to process
657 }
658 const byte* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
659 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
660 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
661 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
662 bool has_catch_all = false;
663 if (encoded_catch_handler_size <= 0) {
664 encoded_catch_handler_size = -encoded_catch_handler_size;
665 has_catch_all = true;
666 }
667 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
668 uint16_t encoded_catch_handler_handlers_type_idx =
669 DecodeUnsignedLeb128(&encoded_catch_handler_list);
670 // Add to set of types to resolve if not already in the dex cache resolved types
671 if (!mh->GetMethod()->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
672 exceptions_to_resolve.insert(
673 std::pair<uint16_t, const DexFile*>(encoded_catch_handler_handlers_type_idx,
674 mh->GetMethod()->GetDexFile()));
675 }
676 // ignore address associated with catch handler
677 DecodeUnsignedLeb128(&encoded_catch_handler_list);
678 }
679 if (has_catch_all) {
680 // ignore catch all address
681 DecodeUnsignedLeb128(&encoded_catch_handler_list);
682 }
683 }
684 }
685
ResolveCatchBlockExceptionsClassVisitor(mirror::Class * c,void * arg)686 static bool ResolveCatchBlockExceptionsClassVisitor(mirror::Class* c, void* arg)
687 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
688 std::set<std::pair<uint16_t, const DexFile*>>* exceptions_to_resolve =
689 reinterpret_cast<std::set<std::pair<uint16_t, const DexFile*>>*>(arg);
690 StackHandleScope<1> hs(Thread::Current());
691 MethodHelper mh(hs.NewHandle<mirror::ArtMethod>(nullptr));
692 for (size_t i = 0; i < c->NumVirtualMethods(); ++i) {
693 mh.ChangeMethod(c->GetVirtualMethod(i));
694 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
695 }
696 for (size_t i = 0; i < c->NumDirectMethods(); ++i) {
697 mh.ChangeMethod(c->GetDirectMethod(i));
698 ResolveExceptionsForMethod(&mh, *exceptions_to_resolve);
699 }
700 return true;
701 }
702
RecordImageClassesVisitor(mirror::Class * klass,void * arg)703 static bool RecordImageClassesVisitor(mirror::Class* klass, void* arg)
704 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
705 std::set<std::string>* image_classes = reinterpret_cast<std::set<std::string>*>(arg);
706 std::string temp;
707 image_classes->insert(klass->GetDescriptor(&temp));
708 return true;
709 }
710
711 // Make a list of descriptors for classes to include in the image
LoadImageClasses(TimingLogger * timings)712 void CompilerDriver::LoadImageClasses(TimingLogger* timings)
713 LOCKS_EXCLUDED(Locks::mutator_lock_) {
714 CHECK(timings != nullptr);
715 if (!IsImage()) {
716 return;
717 }
718
719 TimingLogger::ScopedTiming t("LoadImageClasses", timings);
720 // Make a first class to load all classes explicitly listed in the file
721 Thread* self = Thread::Current();
722 ScopedObjectAccess soa(self);
723 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
724 CHECK(image_classes_.get() != nullptr);
725 for (auto it = image_classes_->begin(), end = image_classes_->end(); it != end;) {
726 const std::string& descriptor(*it);
727 StackHandleScope<1> hs(self);
728 Handle<mirror::Class> klass(
729 hs.NewHandle(class_linker->FindSystemClass(self, descriptor.c_str())));
730 if (klass.Get() == nullptr) {
731 VLOG(compiler) << "Failed to find class " << descriptor;
732 image_classes_->erase(it++);
733 self->ClearException();
734 } else {
735 ++it;
736 }
737 }
738
739 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
740 // exceptions are resolved by the verifier when there is a catch block in an interested method.
741 // Do this here so that exception classes appear to have been specified image classes.
742 std::set<std::pair<uint16_t, const DexFile*>> unresolved_exception_types;
743 StackHandleScope<1> hs(self);
744 Handle<mirror::Class> java_lang_Throwable(
745 hs.NewHandle(class_linker->FindSystemClass(self, "Ljava/lang/Throwable;")));
746 do {
747 unresolved_exception_types.clear();
748 class_linker->VisitClasses(ResolveCatchBlockExceptionsClassVisitor,
749 &unresolved_exception_types);
750 for (const std::pair<uint16_t, const DexFile*>& exception_type : unresolved_exception_types) {
751 uint16_t exception_type_idx = exception_type.first;
752 const DexFile* dex_file = exception_type.second;
753 StackHandleScope<2> hs(self);
754 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(*dex_file)));
755 Handle<mirror::Class> klass(hs.NewHandle(
756 class_linker->ResolveType(*dex_file, exception_type_idx, dex_cache,
757 NullHandle<mirror::ClassLoader>())));
758 if (klass.Get() == nullptr) {
759 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
760 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
761 LOG(FATAL) << "Failed to resolve class " << descriptor;
762 }
763 DCHECK(java_lang_Throwable->IsAssignableFrom(klass.Get()));
764 }
765 // Resolving exceptions may load classes that reference more exceptions, iterate until no
766 // more are found
767 } while (!unresolved_exception_types.empty());
768
769 // We walk the roots looking for classes so that we'll pick up the
770 // above classes plus any classes them depend on such super
771 // classes, interfaces, and the required ClassLinker roots.
772 class_linker->VisitClasses(RecordImageClassesVisitor, image_classes_.get());
773
774 CHECK_NE(image_classes_->size(), 0U);
775 }
776
MaybeAddToImageClasses(Handle<mirror::Class> c,std::set<std::string> * image_classes)777 static void MaybeAddToImageClasses(Handle<mirror::Class> c, std::set<std::string>* image_classes)
778 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
779 Thread* self = Thread::Current();
780 StackHandleScope<1> hs(self);
781 // Make a copy of the handle so that we don't clobber it doing Assign.
782 Handle<mirror::Class> klass(hs.NewHandle(c.Get()));
783 std::string temp;
784 while (!klass->IsObjectClass()) {
785 const char* descriptor = klass->GetDescriptor(&temp);
786 std::pair<std::set<std::string>::iterator, bool> result = image_classes->insert(descriptor);
787 if (!result.second) { // Previously inserted.
788 break;
789 }
790 VLOG(compiler) << "Adding " << descriptor << " to image classes";
791 for (size_t i = 0; i < klass->NumDirectInterfaces(); ++i) {
792 StackHandleScope<1> hs(self);
793 MaybeAddToImageClasses(hs.NewHandle(mirror::Class::GetDirectInterface(self, klass, i)),
794 image_classes);
795 }
796 if (klass->IsArrayClass()) {
797 StackHandleScope<1> hs(self);
798 MaybeAddToImageClasses(hs.NewHandle(klass->GetComponentType()), image_classes);
799 }
800 klass.Assign(klass->GetSuperClass());
801 }
802 }
803
FindClinitImageClassesCallback(mirror::Object * object,void * arg)804 void CompilerDriver::FindClinitImageClassesCallback(mirror::Object* object, void* arg) {
805 DCHECK(object != nullptr);
806 DCHECK(arg != nullptr);
807 CompilerDriver* compiler_driver = reinterpret_cast<CompilerDriver*>(arg);
808 StackHandleScope<1> hs(Thread::Current());
809 MaybeAddToImageClasses(hs.NewHandle(object->GetClass()), compiler_driver->image_classes_.get());
810 }
811
UpdateImageClasses(TimingLogger * timings)812 void CompilerDriver::UpdateImageClasses(TimingLogger* timings) {
813 if (IsImage()) {
814 TimingLogger::ScopedTiming t("UpdateImageClasses", timings);
815 // Update image_classes_ with classes for objects created by <clinit> methods.
816 Thread* self = Thread::Current();
817 const char* old_cause = self->StartAssertNoThreadSuspension("ImageWriter");
818 gc::Heap* heap = Runtime::Current()->GetHeap();
819 // TODO: Image spaces only?
820 ScopedObjectAccess soa(Thread::Current());
821 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
822 heap->VisitObjects(FindClinitImageClassesCallback, this);
823 self->EndAssertNoThreadSuspension(old_cause);
824 }
825 }
826
CanAssumeTypeIsPresentInDexCache(const DexFile & dex_file,uint32_t type_idx)827 bool CompilerDriver::CanAssumeTypeIsPresentInDexCache(const DexFile& dex_file, uint32_t type_idx) {
828 if (IsImage() &&
829 IsImageClass(dex_file.StringDataByIdx(dex_file.GetTypeId(type_idx).descriptor_idx_))) {
830 {
831 ScopedObjectAccess soa(Thread::Current());
832 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
833 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
834 if (resolved_class == nullptr) {
835 // Erroneous class.
836 stats_->TypeNotInDexCache();
837 return false;
838 }
839 }
840 stats_->TypeInDexCache();
841 return true;
842 } else {
843 stats_->TypeNotInDexCache();
844 return false;
845 }
846 }
847
CanAssumeStringIsPresentInDexCache(const DexFile & dex_file,uint32_t string_idx)848 bool CompilerDriver::CanAssumeStringIsPresentInDexCache(const DexFile& dex_file,
849 uint32_t string_idx) {
850 // See also Compiler::ResolveDexFile
851
852 bool result = false;
853 if (IsImage()) {
854 // We resolve all const-string strings when building for the image.
855 ScopedObjectAccess soa(Thread::Current());
856 StackHandleScope<1> hs(soa.Self());
857 Handle<mirror::DexCache> dex_cache(
858 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(dex_file)));
859 Runtime::Current()->GetClassLinker()->ResolveString(dex_file, string_idx, dex_cache);
860 result = true;
861 }
862 if (result) {
863 stats_->StringInDexCache();
864 } else {
865 stats_->StringNotInDexCache();
866 }
867 return result;
868 }
869
CanAccessTypeWithoutChecks(uint32_t referrer_idx,const DexFile & dex_file,uint32_t type_idx,bool * type_known_final,bool * type_known_abstract,bool * equals_referrers_class)870 bool CompilerDriver::CanAccessTypeWithoutChecks(uint32_t referrer_idx, const DexFile& dex_file,
871 uint32_t type_idx,
872 bool* type_known_final, bool* type_known_abstract,
873 bool* equals_referrers_class) {
874 if (type_known_final != nullptr) {
875 *type_known_final = false;
876 }
877 if (type_known_abstract != nullptr) {
878 *type_known_abstract = false;
879 }
880 if (equals_referrers_class != nullptr) {
881 *equals_referrers_class = false;
882 }
883 ScopedObjectAccess soa(Thread::Current());
884 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
885 // Get type from dex cache assuming it was populated by the verifier
886 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
887 if (resolved_class == nullptr) {
888 stats_->TypeNeedsAccessCheck();
889 return false; // Unknown class needs access checks.
890 }
891 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
892 if (equals_referrers_class != nullptr) {
893 *equals_referrers_class = (method_id.class_idx_ == type_idx);
894 }
895 mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
896 if (referrer_class == nullptr) {
897 stats_->TypeNeedsAccessCheck();
898 return false; // Incomplete referrer knowledge needs access check.
899 }
900 // Perform access check, will return true if access is ok or false if we're going to have to
901 // check this at runtime (for example for class loaders).
902 bool result = referrer_class->CanAccess(resolved_class);
903 if (result) {
904 stats_->TypeDoesntNeedAccessCheck();
905 if (type_known_final != nullptr) {
906 *type_known_final = resolved_class->IsFinal() && !resolved_class->IsArrayClass();
907 }
908 if (type_known_abstract != nullptr) {
909 *type_known_abstract = resolved_class->IsAbstract() && !resolved_class->IsArrayClass();
910 }
911 } else {
912 stats_->TypeNeedsAccessCheck();
913 }
914 return result;
915 }
916
CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,const DexFile & dex_file,uint32_t type_idx)917 bool CompilerDriver::CanAccessInstantiableTypeWithoutChecks(uint32_t referrer_idx,
918 const DexFile& dex_file,
919 uint32_t type_idx) {
920 ScopedObjectAccess soa(Thread::Current());
921 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
922 // Get type from dex cache assuming it was populated by the verifier.
923 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
924 if (resolved_class == nullptr) {
925 stats_->TypeNeedsAccessCheck();
926 return false; // Unknown class needs access checks.
927 }
928 const DexFile::MethodId& method_id = dex_file.GetMethodId(referrer_idx);
929 mirror::Class* referrer_class = dex_cache->GetResolvedType(method_id.class_idx_);
930 if (referrer_class == nullptr) {
931 stats_->TypeNeedsAccessCheck();
932 return false; // Incomplete referrer knowledge needs access check.
933 }
934 // Perform access and instantiable checks, will return true if access is ok or false if we're
935 // going to have to check this at runtime (for example for class loaders).
936 bool result = referrer_class->CanAccess(resolved_class) && resolved_class->IsInstantiable();
937 if (result) {
938 stats_->TypeDoesntNeedAccessCheck();
939 } else {
940 stats_->TypeNeedsAccessCheck();
941 }
942 return result;
943 }
944
CanEmbedTypeInCode(const DexFile & dex_file,uint32_t type_idx,bool * is_type_initialized,bool * use_direct_type_ptr,uintptr_t * direct_type_ptr,bool * out_is_finalizable)945 bool CompilerDriver::CanEmbedTypeInCode(const DexFile& dex_file, uint32_t type_idx,
946 bool* is_type_initialized, bool* use_direct_type_ptr,
947 uintptr_t* direct_type_ptr, bool* out_is_finalizable) {
948 if (GetCompilerOptions().GetCompilePic()) {
949 // Do not allow a direct class pointer to be used when compiling for position-independent
950 return false;
951 }
952 ScopedObjectAccess soa(Thread::Current());
953 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
954 mirror::Class* resolved_class = dex_cache->GetResolvedType(type_idx);
955 if (resolved_class == nullptr) {
956 return false;
957 }
958 *out_is_finalizable = resolved_class->IsFinalizable();
959 const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot();
960 const bool support_boot_image_fixup = GetSupportBootImageFixup();
961 if (compiling_boot) {
962 // boot -> boot class pointers.
963 // True if the class is in the image at boot compiling time.
964 const bool is_image_class = IsImage() && IsImageClass(
965 dex_file.StringDataByIdx(dex_file.GetTypeId(type_idx).descriptor_idx_));
966 // True if pc relative load works.
967 if (is_image_class && support_boot_image_fixup) {
968 *is_type_initialized = resolved_class->IsInitialized();
969 *use_direct_type_ptr = false;
970 *direct_type_ptr = 0;
971 return true;
972 } else {
973 return false;
974 }
975 } else {
976 // True if the class is in the image at app compiling time.
977 const bool class_in_image =
978 Runtime::Current()->GetHeap()->FindSpaceFromObject(resolved_class, false)->IsImageSpace();
979 if (class_in_image && support_boot_image_fixup) {
980 // boot -> app class pointers.
981 *is_type_initialized = resolved_class->IsInitialized();
982 // TODO This is somewhat hacky. We should refactor all of this invoke codepath.
983 *use_direct_type_ptr = !GetCompilerOptions().GetIncludePatchInformation();
984 *direct_type_ptr = reinterpret_cast<uintptr_t>(resolved_class);
985 return true;
986 } else {
987 // app -> app class pointers.
988 // Give up because app does not have an image and class
989 // isn't created at compile time. TODO: implement this
990 // if/when each app gets an image.
991 return false;
992 }
993 }
994 }
995
CanEmbedStringInCode(const DexFile & dex_file,uint32_t string_idx,bool * use_direct_type_ptr,uintptr_t * direct_type_ptr)996 bool CompilerDriver::CanEmbedStringInCode(const DexFile& dex_file, uint32_t string_idx,
997 bool* use_direct_type_ptr, uintptr_t* direct_type_ptr) {
998 if (GetCompilerOptions().GetCompilePic()) {
999 // Do not allow a direct class pointer to be used when compiling for position-independent
1000 return false;
1001 }
1002 ScopedObjectAccess soa(Thread::Current());
1003 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(dex_file);
1004 mirror::String* resolved_string = dex_cache->GetResolvedString(string_idx);
1005 if (resolved_string == nullptr) {
1006 return false;
1007 }
1008 const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot();
1009 const bool support_boot_image_fixup = GetSupportBootImageFixup();
1010 if (compiling_boot) {
1011 // boot -> boot class pointers.
1012 // True if the class is in the image at boot compiling time.
1013 const bool is_image_string = IsImage();
1014 // True if pc relative load works.
1015 if (is_image_string && support_boot_image_fixup) {
1016 *use_direct_type_ptr = false;
1017 *direct_type_ptr = 0;
1018 return true;
1019 }
1020 return false;
1021 } else {
1022 // True if the class is in the image at app compiling time.
1023 const bool obj_in_image =
1024 false && Runtime::Current()->GetHeap()->FindSpaceFromObject(resolved_string, false)->IsImageSpace();
1025 if (obj_in_image && support_boot_image_fixup) {
1026 // boot -> app class pointers.
1027 // TODO This is somewhat hacky. We should refactor all of this invoke codepath.
1028 *use_direct_type_ptr = !GetCompilerOptions().GetIncludePatchInformation();
1029 *direct_type_ptr = reinterpret_cast<uintptr_t>(resolved_string);
1030 return true;
1031 }
1032
1033 // app -> app class pointers.
1034 // Give up because app does not have an image and class
1035 // isn't created at compile time. TODO: implement this
1036 // if/when each app gets an image.
1037 return false;
1038 }
1039 }
1040
ProcessedInstanceField(bool resolved)1041 void CompilerDriver::ProcessedInstanceField(bool resolved) {
1042 if (!resolved) {
1043 stats_->UnresolvedInstanceField();
1044 } else {
1045 stats_->ResolvedInstanceField();
1046 }
1047 }
1048
ProcessedStaticField(bool resolved,bool local)1049 void CompilerDriver::ProcessedStaticField(bool resolved, bool local) {
1050 if (!resolved) {
1051 stats_->UnresolvedStaticField();
1052 } else if (local) {
1053 stats_->ResolvedLocalStaticField();
1054 } else {
1055 stats_->ResolvedStaticField();
1056 }
1057 }
1058
ProcessedInvoke(InvokeType invoke_type,int flags)1059 void CompilerDriver::ProcessedInvoke(InvokeType invoke_type, int flags) {
1060 stats_->ProcessedInvoke(invoke_type, flags);
1061 }
1062
ComputeInstanceFieldInfo(uint32_t field_idx,const DexCompilationUnit * mUnit,bool is_put,const ScopedObjectAccess & soa)1063 mirror::ArtField* CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx,
1064 const DexCompilationUnit* mUnit,
1065 bool is_put,
1066 const ScopedObjectAccess& soa) {
1067 // Try to resolve the field and compiling method's class.
1068 mirror::ArtField* resolved_field;
1069 mirror::Class* referrer_class;
1070 mirror::DexCache* dex_cache;
1071 {
1072 StackHandleScope<3> hs(soa.Self());
1073 Handle<mirror::DexCache> dex_cache_handle(
1074 hs.NewHandle(mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())));
1075 Handle<mirror::ClassLoader> class_loader_handle(
1076 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1077 Handle<mirror::ArtField> resolved_field_handle(hs.NewHandle(
1078 ResolveField(soa, dex_cache_handle, class_loader_handle, mUnit, field_idx, false)));
1079 referrer_class = (resolved_field_handle.Get() != nullptr)
1080 ? ResolveCompilingMethodsClass(soa, dex_cache_handle, class_loader_handle, mUnit) : nullptr;
1081 resolved_field = resolved_field_handle.Get();
1082 dex_cache = dex_cache_handle.Get();
1083 }
1084 bool can_link = false;
1085 if (resolved_field != nullptr && referrer_class != nullptr) {
1086 std::pair<bool, bool> fast_path = IsFastInstanceField(
1087 dex_cache, referrer_class, resolved_field, field_idx);
1088 can_link = is_put ? fast_path.second : fast_path.first;
1089 }
1090 ProcessedInstanceField(can_link);
1091 return can_link ? resolved_field : nullptr;
1092 }
1093
ComputeInstanceFieldInfo(uint32_t field_idx,const DexCompilationUnit * mUnit,bool is_put,MemberOffset * field_offset,bool * is_volatile)1094 bool CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
1095 bool is_put, MemberOffset* field_offset,
1096 bool* is_volatile) {
1097 ScopedObjectAccess soa(Thread::Current());
1098 StackHandleScope<1> hs(soa.Self());
1099 Handle<mirror::ArtField> resolved_field =
1100 hs.NewHandle(ComputeInstanceFieldInfo(field_idx, mUnit, is_put, soa));
1101
1102 if (resolved_field.Get() == nullptr) {
1103 // Conservative defaults.
1104 *is_volatile = true;
1105 *field_offset = MemberOffset(static_cast<size_t>(-1));
1106 return false;
1107 } else {
1108 *is_volatile = resolved_field->IsVolatile();
1109 *field_offset = resolved_field->GetOffset();
1110 return true;
1111 }
1112 }
1113
ComputeStaticFieldInfo(uint32_t field_idx,const DexCompilationUnit * mUnit,bool is_put,MemberOffset * field_offset,uint32_t * storage_index,bool * is_referrers_class,bool * is_volatile,bool * is_initialized)1114 bool CompilerDriver::ComputeStaticFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
1115 bool is_put, MemberOffset* field_offset,
1116 uint32_t* storage_index, bool* is_referrers_class,
1117 bool* is_volatile, bool* is_initialized) {
1118 ScopedObjectAccess soa(Thread::Current());
1119 // Try to resolve the field and compiling method's class.
1120 mirror::ArtField* resolved_field;
1121 mirror::Class* referrer_class;
1122 mirror::DexCache* dex_cache;
1123 {
1124 StackHandleScope<3> hs(soa.Self());
1125 Handle<mirror::DexCache> dex_cache_handle(
1126 hs.NewHandle(mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())));
1127 Handle<mirror::ClassLoader> class_loader_handle(
1128 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1129 Handle<mirror::ArtField> resolved_field_handle(hs.NewHandle(
1130 ResolveField(soa, dex_cache_handle, class_loader_handle, mUnit, field_idx, true)));
1131 referrer_class = (resolved_field_handle.Get() != nullptr)
1132 ? ResolveCompilingMethodsClass(soa, dex_cache_handle, class_loader_handle, mUnit) : nullptr;
1133 resolved_field = resolved_field_handle.Get();
1134 dex_cache = dex_cache_handle.Get();
1135 }
1136 bool result = false;
1137 if (resolved_field != nullptr && referrer_class != nullptr) {
1138 *is_volatile = IsFieldVolatile(resolved_field);
1139 std::pair<bool, bool> fast_path = IsFastStaticField(
1140 dex_cache, referrer_class, resolved_field, field_idx, field_offset,
1141 storage_index, is_referrers_class, is_initialized);
1142 result = is_put ? fast_path.second : fast_path.first;
1143 }
1144 if (!result) {
1145 // Conservative defaults.
1146 *is_volatile = true;
1147 *field_offset = MemberOffset(static_cast<size_t>(-1));
1148 *storage_index = -1;
1149 *is_referrers_class = false;
1150 *is_initialized = false;
1151 }
1152 ProcessedStaticField(result, *is_referrers_class);
1153 return result;
1154 }
1155
GetCodeAndMethodForDirectCall(InvokeType * type,InvokeType sharp_type,bool no_guarantee_of_dex_cache_entry,const mirror::Class * referrer_class,mirror::ArtMethod * method,int * stats_flags,MethodReference * target_method,uintptr_t * direct_code,uintptr_t * direct_method)1156 void CompilerDriver::GetCodeAndMethodForDirectCall(InvokeType* type, InvokeType sharp_type,
1157 bool no_guarantee_of_dex_cache_entry,
1158 const mirror::Class* referrer_class,
1159 mirror::ArtMethod* method,
1160 int* stats_flags,
1161 MethodReference* target_method,
1162 uintptr_t* direct_code,
1163 uintptr_t* direct_method) {
1164 // For direct and static methods compute possible direct_code and direct_method values, ie
1165 // an address for the Method* being invoked and an address of the code for that Method*.
1166 // For interface calls compute a value for direct_method that is the interface method being
1167 // invoked, so this can be passed to the out-of-line runtime support code.
1168 *direct_code = 0;
1169 *direct_method = 0;
1170 bool use_dex_cache = GetCompilerOptions().GetCompilePic(); // Off by default
1171 const bool compiling_boot = Runtime::Current()->GetHeap()->IsCompilingBoot();
1172 // TODO This is somewhat hacky. We should refactor all of this invoke codepath.
1173 const bool force_relocations = (compiling_boot ||
1174 GetCompilerOptions().GetIncludePatchInformation());
1175 if (compiler_->IsPortable()) {
1176 if (sharp_type != kStatic && sharp_type != kDirect) {
1177 return;
1178 }
1179 use_dex_cache = true;
1180 } else {
1181 if (sharp_type != kStatic && sharp_type != kDirect) {
1182 return;
1183 }
1184 // TODO: support patching on all architectures.
1185 use_dex_cache = use_dex_cache || (force_relocations && !support_boot_image_fixup_);
1186 }
1187 mirror::Class* declaring_class = method->GetDeclaringClass();
1188 bool method_code_in_boot = (declaring_class->GetClassLoader() == nullptr);
1189 if (!use_dex_cache) {
1190 if (!method_code_in_boot) {
1191 use_dex_cache = true;
1192 } else {
1193 bool has_clinit_trampoline =
1194 method->IsStatic() && !declaring_class->IsInitialized();
1195 if (has_clinit_trampoline && (declaring_class != referrer_class)) {
1196 // Ensure we run the clinit trampoline unless we are invoking a static method in the same
1197 // class.
1198 use_dex_cache = true;
1199 }
1200 }
1201 }
1202 if (method_code_in_boot) {
1203 *stats_flags |= kFlagDirectCallToBoot | kFlagDirectMethodToBoot;
1204 }
1205 if (!use_dex_cache && force_relocations) {
1206 bool is_in_image;
1207 if (IsImage()) {
1208 is_in_image = IsImageClass(method->GetDeclaringClassDescriptor());
1209 } else {
1210 is_in_image = instruction_set_ != kX86 && instruction_set_ != kX86_64 &&
1211 Runtime::Current()->GetHeap()->FindSpaceFromObject(declaring_class,
1212 false)->IsImageSpace();
1213 }
1214 if (!is_in_image) {
1215 // We can only branch directly to Methods that are resolved in the DexCache.
1216 // Otherwise we won't invoke the resolution trampoline.
1217 use_dex_cache = true;
1218 }
1219 }
1220 // The method is defined not within this dex file. We need a dex cache slot within the current
1221 // dex file or direct pointers.
1222 bool must_use_direct_pointers = false;
1223 if (target_method->dex_file == declaring_class->GetDexCache()->GetDexFile()) {
1224 target_method->dex_method_index = method->GetDexMethodIndex();
1225 } else {
1226 if (no_guarantee_of_dex_cache_entry) {
1227 StackHandleScope<1> hs(Thread::Current());
1228 MethodHelper mh(hs.NewHandle(method));
1229 // See if the method is also declared in this dex cache.
1230 uint32_t dex_method_idx = mh.FindDexMethodIndexInOtherDexFile(
1231 *target_method->dex_file, target_method->dex_method_index);
1232 if (dex_method_idx != DexFile::kDexNoIndex) {
1233 target_method->dex_method_index = dex_method_idx;
1234 } else {
1235 if (force_relocations && !use_dex_cache) {
1236 target_method->dex_method_index = method->GetDexMethodIndex();
1237 target_method->dex_file = declaring_class->GetDexCache()->GetDexFile();
1238 }
1239 must_use_direct_pointers = true;
1240 }
1241 }
1242 }
1243 if (use_dex_cache) {
1244 if (must_use_direct_pointers) {
1245 // Fail. Test above showed the only safe dispatch was via the dex cache, however, the direct
1246 // pointers are required as the dex cache lacks an appropriate entry.
1247 VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
1248 } else {
1249 *type = sharp_type;
1250 }
1251 } else {
1252 bool method_in_image =
1253 Runtime::Current()->GetHeap()->FindSpaceFromObject(method, false)->IsImageSpace();
1254 if (method_in_image || compiling_boot) {
1255 // We know we must be able to get to the method in the image, so use that pointer.
1256 CHECK(!method->IsAbstract());
1257 *type = sharp_type;
1258 *direct_method = force_relocations ? -1 : reinterpret_cast<uintptr_t>(method);
1259 *direct_code = force_relocations ? -1 : compiler_->GetEntryPointOf(method);
1260 target_method->dex_file = declaring_class->GetDexCache()->GetDexFile();
1261 target_method->dex_method_index = method->GetDexMethodIndex();
1262 } else if (!must_use_direct_pointers) {
1263 // Set the code and rely on the dex cache for the method.
1264 *type = sharp_type;
1265 if (force_relocations) {
1266 *direct_code = -1;
1267 target_method->dex_file = declaring_class->GetDexCache()->GetDexFile();
1268 target_method->dex_method_index = method->GetDexMethodIndex();
1269 } else {
1270 *direct_code = compiler_->GetEntryPointOf(method);
1271 }
1272 } else {
1273 // Direct pointers were required but none were available.
1274 VLOG(compiler) << "Dex cache devirtualization failed for: " << PrettyMethod(method);
1275 }
1276 }
1277 }
1278
ComputeInvokeInfo(const DexCompilationUnit * mUnit,const uint32_t dex_pc,bool update_stats,bool enable_devirtualization,InvokeType * invoke_type,MethodReference * target_method,int * vtable_idx,uintptr_t * direct_code,uintptr_t * direct_method)1279 bool CompilerDriver::ComputeInvokeInfo(const DexCompilationUnit* mUnit, const uint32_t dex_pc,
1280 bool update_stats, bool enable_devirtualization,
1281 InvokeType* invoke_type, MethodReference* target_method,
1282 int* vtable_idx, uintptr_t* direct_code,
1283 uintptr_t* direct_method) {
1284 InvokeType orig_invoke_type = *invoke_type;
1285 int stats_flags = 0;
1286 ScopedObjectAccess soa(Thread::Current());
1287 // Try to resolve the method and compiling method's class.
1288 mirror::ArtMethod* resolved_method;
1289 mirror::Class* referrer_class;
1290 StackHandleScope<3> hs(soa.Self());
1291 Handle<mirror::DexCache> dex_cache(
1292 hs.NewHandle(mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile())));
1293 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1294 soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())));
1295 {
1296 uint32_t method_idx = target_method->dex_method_index;
1297 Handle<mirror::ArtMethod> resolved_method_handle(hs.NewHandle(
1298 ResolveMethod(soa, dex_cache, class_loader, mUnit, method_idx, orig_invoke_type)));
1299 referrer_class = (resolved_method_handle.Get() != nullptr)
1300 ? ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit) : nullptr;
1301 resolved_method = resolved_method_handle.Get();
1302 }
1303 bool result = false;
1304 if (resolved_method != nullptr) {
1305 *vtable_idx = GetResolvedMethodVTableIndex(resolved_method, orig_invoke_type);
1306
1307 if (enable_devirtualization) {
1308 DCHECK(mUnit->GetVerifiedMethod() != nullptr);
1309 const MethodReference* devirt_target = mUnit->GetVerifiedMethod()->GetDevirtTarget(dex_pc);
1310
1311 stats_flags = IsFastInvoke(
1312 soa, dex_cache, class_loader, mUnit, referrer_class, resolved_method,
1313 invoke_type, target_method, devirt_target, direct_code, direct_method);
1314 result = stats_flags != 0;
1315 } else {
1316 // Devirtualization not enabled. Inline IsFastInvoke(), dropping the devirtualization parts.
1317 if (UNLIKELY(referrer_class == nullptr) ||
1318 UNLIKELY(!referrer_class->CanAccessResolvedMethod(resolved_method->GetDeclaringClass(),
1319 resolved_method, dex_cache.Get(),
1320 target_method->dex_method_index)) ||
1321 *invoke_type == kSuper) {
1322 // Slow path. (Without devirtualization, all super calls go slow path as well.)
1323 } else {
1324 // Sharpening failed so generate a regular resolved method dispatch.
1325 stats_flags = kFlagMethodResolved;
1326 GetCodeAndMethodForDirectCall(invoke_type, *invoke_type, false, referrer_class, resolved_method,
1327 &stats_flags, target_method, direct_code, direct_method);
1328 result = true;
1329 }
1330 }
1331 }
1332 if (!result) {
1333 // Conservative defaults.
1334 *vtable_idx = -1;
1335 *direct_code = 0u;
1336 *direct_method = 0u;
1337 }
1338 if (update_stats) {
1339 ProcessedInvoke(orig_invoke_type, stats_flags);
1340 }
1341 return result;
1342 }
1343
GetVerifiedMethod(const DexFile * dex_file,uint32_t method_idx) const1344 const VerifiedMethod* CompilerDriver::GetVerifiedMethod(const DexFile* dex_file,
1345 uint32_t method_idx) const {
1346 MethodReference ref(dex_file, method_idx);
1347 return verification_results_->GetVerifiedMethod(ref);
1348 }
1349
IsSafeCast(const DexCompilationUnit * mUnit,uint32_t dex_pc)1350 bool CompilerDriver::IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc) {
1351 if (!compiler_options_->IsVerificationEnabled()) {
1352 // If we didn't verify, every cast has to be treated as non-safe.
1353 return false;
1354 }
1355 DCHECK(mUnit->GetVerifiedMethod() != nullptr);
1356 bool result = mUnit->GetVerifiedMethod()->IsSafeCast(dex_pc);
1357 if (result) {
1358 stats_->SafeCast();
1359 } else {
1360 stats_->NotASafeCast();
1361 }
1362 return result;
1363 }
1364
AddCodePatch(const DexFile * dex_file,uint16_t referrer_class_def_idx,uint32_t referrer_method_idx,InvokeType referrer_invoke_type,uint32_t target_method_idx,const DexFile * target_dex_file,InvokeType target_invoke_type,size_t literal_offset)1365 void CompilerDriver::AddCodePatch(const DexFile* dex_file,
1366 uint16_t referrer_class_def_idx,
1367 uint32_t referrer_method_idx,
1368 InvokeType referrer_invoke_type,
1369 uint32_t target_method_idx,
1370 const DexFile* target_dex_file,
1371 InvokeType target_invoke_type,
1372 size_t literal_offset) {
1373 MutexLock mu(Thread::Current(), compiled_methods_lock_);
1374 code_to_patch_.push_back(new CallPatchInformation(dex_file,
1375 referrer_class_def_idx,
1376 referrer_method_idx,
1377 referrer_invoke_type,
1378 target_method_idx,
1379 target_dex_file,
1380 target_invoke_type,
1381 literal_offset));
1382 }
AddRelativeCodePatch(const DexFile * dex_file,uint16_t referrer_class_def_idx,uint32_t referrer_method_idx,InvokeType referrer_invoke_type,uint32_t target_method_idx,const DexFile * target_dex_file,InvokeType target_invoke_type,size_t literal_offset,int32_t pc_relative_offset)1383 void CompilerDriver::AddRelativeCodePatch(const DexFile* dex_file,
1384 uint16_t referrer_class_def_idx,
1385 uint32_t referrer_method_idx,
1386 InvokeType referrer_invoke_type,
1387 uint32_t target_method_idx,
1388 const DexFile* target_dex_file,
1389 InvokeType target_invoke_type,
1390 size_t literal_offset,
1391 int32_t pc_relative_offset) {
1392 MutexLock mu(Thread::Current(), compiled_methods_lock_);
1393 code_to_patch_.push_back(new RelativeCallPatchInformation(dex_file,
1394 referrer_class_def_idx,
1395 referrer_method_idx,
1396 referrer_invoke_type,
1397 target_method_idx,
1398 target_dex_file,
1399 target_invoke_type,
1400 literal_offset,
1401 pc_relative_offset));
1402 }
AddMethodPatch(const DexFile * dex_file,uint16_t referrer_class_def_idx,uint32_t referrer_method_idx,InvokeType referrer_invoke_type,uint32_t target_method_idx,const DexFile * target_dex_file,InvokeType target_invoke_type,size_t literal_offset)1403 void CompilerDriver::AddMethodPatch(const DexFile* dex_file,
1404 uint16_t referrer_class_def_idx,
1405 uint32_t referrer_method_idx,
1406 InvokeType referrer_invoke_type,
1407 uint32_t target_method_idx,
1408 const DexFile* target_dex_file,
1409 InvokeType target_invoke_type,
1410 size_t literal_offset) {
1411 MutexLock mu(Thread::Current(), compiled_methods_lock_);
1412 methods_to_patch_.push_back(new CallPatchInformation(dex_file,
1413 referrer_class_def_idx,
1414 referrer_method_idx,
1415 referrer_invoke_type,
1416 target_method_idx,
1417 target_dex_file,
1418 target_invoke_type,
1419 literal_offset));
1420 }
AddClassPatch(const DexFile * dex_file,uint16_t referrer_class_def_idx,uint32_t referrer_method_idx,uint32_t target_type_idx,size_t literal_offset)1421 void CompilerDriver::AddClassPatch(const DexFile* dex_file,
1422 uint16_t referrer_class_def_idx,
1423 uint32_t referrer_method_idx,
1424 uint32_t target_type_idx,
1425 size_t literal_offset) {
1426 MutexLock mu(Thread::Current(), compiled_methods_lock_);
1427 classes_to_patch_.push_back(new TypePatchInformation(dex_file,
1428 referrer_class_def_idx,
1429 referrer_method_idx,
1430 target_type_idx,
1431 literal_offset));
1432 }
AddStringPatch(const DexFile * dex_file,uint16_t referrer_class_def_idx,uint32_t referrer_method_idx,uint32_t string_idx,size_t literal_offset)1433 void CompilerDriver::AddStringPatch(const DexFile* dex_file,
1434 uint16_t referrer_class_def_idx,
1435 uint32_t referrer_method_idx,
1436 uint32_t string_idx,
1437 size_t literal_offset) {
1438 MutexLock mu(Thread::Current(), compiled_methods_lock_);
1439 strings_to_patch_.push_back(new StringPatchInformation(dex_file,
1440 referrer_class_def_idx,
1441 referrer_method_idx,
1442 string_idx,
1443 literal_offset));
1444 }
1445
1446 class ParallelCompilationManager {
1447 public:
1448 typedef void Callback(const ParallelCompilationManager* manager, size_t index);
1449
ParallelCompilationManager(ClassLinker * class_linker,jobject class_loader,CompilerDriver * compiler,const DexFile * dex_file,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool)1450 ParallelCompilationManager(ClassLinker* class_linker,
1451 jobject class_loader,
1452 CompilerDriver* compiler,
1453 const DexFile* dex_file,
1454 const std::vector<const DexFile*>& dex_files,
1455 ThreadPool* thread_pool)
1456 : index_(0),
1457 class_linker_(class_linker),
1458 class_loader_(class_loader),
1459 compiler_(compiler),
1460 dex_file_(dex_file),
1461 dex_files_(dex_files),
1462 thread_pool_(thread_pool) {}
1463
GetClassLinker() const1464 ClassLinker* GetClassLinker() const {
1465 CHECK(class_linker_ != nullptr);
1466 return class_linker_;
1467 }
1468
GetClassLoader() const1469 jobject GetClassLoader() const {
1470 return class_loader_;
1471 }
1472
GetCompiler() const1473 CompilerDriver* GetCompiler() const {
1474 CHECK(compiler_ != nullptr);
1475 return compiler_;
1476 }
1477
GetDexFile() const1478 const DexFile* GetDexFile() const {
1479 CHECK(dex_file_ != nullptr);
1480 return dex_file_;
1481 }
1482
GetDexFiles() const1483 const std::vector<const DexFile*>& GetDexFiles() const {
1484 return dex_files_;
1485 }
1486
ForAll(size_t begin,size_t end,Callback callback,size_t work_units)1487 void ForAll(size_t begin, size_t end, Callback callback, size_t work_units) {
1488 Thread* self = Thread::Current();
1489 self->AssertNoPendingException();
1490 CHECK_GT(work_units, 0U);
1491
1492 index_.StoreRelaxed(begin);
1493 for (size_t i = 0; i < work_units; ++i) {
1494 thread_pool_->AddTask(self, new ForAllClosure(this, end, callback));
1495 }
1496 thread_pool_->StartWorkers(self);
1497
1498 // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker
1499 // thread destructor's called below perform join).
1500 CHECK_NE(self->GetState(), kRunnable);
1501
1502 // Wait for all the worker threads to finish.
1503 thread_pool_->Wait(self, true, false);
1504 }
1505
NextIndex()1506 size_t NextIndex() {
1507 return index_.FetchAndAddSequentiallyConsistent(1);
1508 }
1509
1510 private:
1511 class ForAllClosure : public Task {
1512 public:
ForAllClosure(ParallelCompilationManager * manager,size_t end,Callback * callback)1513 ForAllClosure(ParallelCompilationManager* manager, size_t end, Callback* callback)
1514 : manager_(manager),
1515 end_(end),
1516 callback_(callback) {}
1517
Run(Thread * self)1518 virtual void Run(Thread* self) {
1519 while (true) {
1520 const size_t index = manager_->NextIndex();
1521 if (UNLIKELY(index >= end_)) {
1522 break;
1523 }
1524 callback_(manager_, index);
1525 self->AssertNoPendingException();
1526 }
1527 }
1528
Finalize()1529 virtual void Finalize() {
1530 delete this;
1531 }
1532
1533 private:
1534 ParallelCompilationManager* const manager_;
1535 const size_t end_;
1536 Callback* const callback_;
1537 };
1538
1539 AtomicInteger index_;
1540 ClassLinker* const class_linker_;
1541 const jobject class_loader_;
1542 CompilerDriver* const compiler_;
1543 const DexFile* const dex_file_;
1544 const std::vector<const DexFile*>& dex_files_;
1545 ThreadPool* const thread_pool_;
1546
1547 DISALLOW_COPY_AND_ASSIGN(ParallelCompilationManager);
1548 };
1549
1550 // A fast version of SkipClass above if the class pointer is available
1551 // that avoids the expensive FindInClassPath search.
SkipClass(jobject class_loader,const DexFile & dex_file,mirror::Class * klass)1552 static bool SkipClass(jobject class_loader, const DexFile& dex_file, mirror::Class* klass)
1553 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1554 DCHECK(klass != nullptr);
1555 const DexFile& original_dex_file = *klass->GetDexCache()->GetDexFile();
1556 if (&dex_file != &original_dex_file) {
1557 if (class_loader == nullptr) {
1558 LOG(WARNING) << "Skipping class " << PrettyDescriptor(klass) << " from "
1559 << dex_file.GetLocation() << " previously found in "
1560 << original_dex_file.GetLocation();
1561 }
1562 return true;
1563 }
1564 return false;
1565 }
1566
CheckAndClearResolveException(Thread * self)1567 static void CheckAndClearResolveException(Thread* self)
1568 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1569 CHECK(self->IsExceptionPending());
1570 mirror::Throwable* exception = self->GetException(nullptr);
1571 std::string temp;
1572 const char* descriptor = exception->GetClass()->GetDescriptor(&temp);
1573 const char* expected_exceptions[] = {
1574 "Ljava/lang/IllegalAccessError;",
1575 "Ljava/lang/IncompatibleClassChangeError;",
1576 "Ljava/lang/InstantiationError;",
1577 "Ljava/lang/LinkageError;",
1578 "Ljava/lang/NoClassDefFoundError;",
1579 "Ljava/lang/NoSuchFieldError;",
1580 "Ljava/lang/NoSuchMethodError;"
1581 };
1582 bool found = false;
1583 for (size_t i = 0; (found == false) && (i < arraysize(expected_exceptions)); ++i) {
1584 if (strcmp(descriptor, expected_exceptions[i]) == 0) {
1585 found = true;
1586 }
1587 }
1588 if (!found) {
1589 LOG(FATAL) << "Unexpected exception " << exception->Dump();
1590 }
1591 self->ClearException();
1592 }
1593
ResolveClassFieldsAndMethods(const ParallelCompilationManager * manager,size_t class_def_index)1594 static void ResolveClassFieldsAndMethods(const ParallelCompilationManager* manager,
1595 size_t class_def_index)
1596 LOCKS_EXCLUDED(Locks::mutator_lock_) {
1597 ATRACE_CALL();
1598 Thread* self = Thread::Current();
1599 jobject jclass_loader = manager->GetClassLoader();
1600 const DexFile& dex_file = *manager->GetDexFile();
1601 ClassLinker* class_linker = manager->GetClassLinker();
1602
1603 // If an instance field is final then we need to have a barrier on the return, static final
1604 // fields are assigned within the lock held for class initialization. Conservatively assume
1605 // constructor barriers are always required.
1606 bool requires_constructor_barrier = true;
1607
1608 // Method and Field are the worst. We can't resolve without either
1609 // context from the code use (to disambiguate virtual vs direct
1610 // method and instance vs static field) or from class
1611 // definitions. While the compiler will resolve what it can as it
1612 // needs it, here we try to resolve fields and methods used in class
1613 // definitions, since many of them many never be referenced by
1614 // generated code.
1615 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1616 ScopedObjectAccess soa(self);
1617 StackHandleScope<2> hs(soa.Self());
1618 Handle<mirror::ClassLoader> class_loader(
1619 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1620 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1621 // Resolve the class.
1622 mirror::Class* klass = class_linker->ResolveType(dex_file, class_def.class_idx_, dex_cache,
1623 class_loader);
1624 bool resolve_fields_and_methods;
1625 if (klass == nullptr) {
1626 // Class couldn't be resolved, for example, super-class is in a different dex file. Don't
1627 // attempt to resolve methods and fields when there is no declaring class.
1628 CheckAndClearResolveException(soa.Self());
1629 resolve_fields_and_methods = false;
1630 } else {
1631 // We successfully resolved a class, should we skip it?
1632 if (SkipClass(jclass_loader, dex_file, klass)) {
1633 return;
1634 }
1635 // We want to resolve the methods and fields eagerly.
1636 resolve_fields_and_methods = true;
1637 }
1638 // Note the class_data pointer advances through the headers,
1639 // static fields, instance fields, direct methods, and virtual
1640 // methods.
1641 const byte* class_data = dex_file.GetClassData(class_def);
1642 if (class_data == nullptr) {
1643 // Empty class such as a marker interface.
1644 requires_constructor_barrier = false;
1645 } else {
1646 ClassDataItemIterator it(dex_file, class_data);
1647 while (it.HasNextStaticField()) {
1648 if (resolve_fields_and_methods) {
1649 mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1650 dex_cache, class_loader, true);
1651 if (field == nullptr) {
1652 CheckAndClearResolveException(soa.Self());
1653 }
1654 }
1655 it.Next();
1656 }
1657 // We require a constructor barrier if there are final instance fields.
1658 requires_constructor_barrier = false;
1659 while (it.HasNextInstanceField()) {
1660 if (it.MemberIsFinal()) {
1661 requires_constructor_barrier = true;
1662 }
1663 if (resolve_fields_and_methods) {
1664 mirror::ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1665 dex_cache, class_loader, false);
1666 if (field == nullptr) {
1667 CheckAndClearResolveException(soa.Self());
1668 }
1669 }
1670 it.Next();
1671 }
1672 if (resolve_fields_and_methods) {
1673 while (it.HasNextDirectMethod()) {
1674 mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1675 dex_cache, class_loader,
1676 NullHandle<mirror::ArtMethod>(),
1677 it.GetMethodInvokeType(class_def));
1678 if (method == nullptr) {
1679 CheckAndClearResolveException(soa.Self());
1680 }
1681 it.Next();
1682 }
1683 while (it.HasNextVirtualMethod()) {
1684 mirror::ArtMethod* method = class_linker->ResolveMethod(dex_file, it.GetMemberIndex(),
1685 dex_cache, class_loader,
1686 NullHandle<mirror::ArtMethod>(),
1687 it.GetMethodInvokeType(class_def));
1688 if (method == nullptr) {
1689 CheckAndClearResolveException(soa.Self());
1690 }
1691 it.Next();
1692 }
1693 DCHECK(!it.HasNext());
1694 }
1695 }
1696 if (requires_constructor_barrier) {
1697 manager->GetCompiler()->AddRequiresConstructorBarrier(self, &dex_file, class_def_index);
1698 }
1699 }
1700
ResolveType(const ParallelCompilationManager * manager,size_t type_idx)1701 static void ResolveType(const ParallelCompilationManager* manager, size_t type_idx)
1702 LOCKS_EXCLUDED(Locks::mutator_lock_) {
1703 // Class derived values are more complicated, they require the linker and loader.
1704 ScopedObjectAccess soa(Thread::Current());
1705 ClassLinker* class_linker = manager->GetClassLinker();
1706 const DexFile& dex_file = *manager->GetDexFile();
1707 StackHandleScope<2> hs(soa.Self());
1708 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1709 Handle<mirror::ClassLoader> class_loader(
1710 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(manager->GetClassLoader())));
1711 mirror::Class* klass = class_linker->ResolveType(dex_file, type_idx, dex_cache, class_loader);
1712
1713 if (klass == nullptr) {
1714 CHECK(soa.Self()->IsExceptionPending());
1715 mirror::Throwable* exception = soa.Self()->GetException(nullptr);
1716 VLOG(compiler) << "Exception during type resolution: " << exception->Dump();
1717 if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
1718 // There's little point continuing compilation if the heap is exhausted.
1719 LOG(FATAL) << "Out of memory during type resolution for compilation";
1720 }
1721 soa.Self()->ClearException();
1722 }
1723 }
1724
ResolveDexFile(jobject class_loader,const DexFile & dex_file,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,TimingLogger * timings)1725 void CompilerDriver::ResolveDexFile(jobject class_loader, const DexFile& dex_file,
1726 const std::vector<const DexFile*>& dex_files,
1727 ThreadPool* thread_pool, TimingLogger* timings) {
1728 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1729
1730 // TODO: we could resolve strings here, although the string table is largely filled with class
1731 // and method names.
1732
1733 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
1734 thread_pool);
1735 if (IsImage()) {
1736 // For images we resolve all types, such as array, whereas for applications just those with
1737 // classdefs are resolved by ResolveClassFieldsAndMethods.
1738 TimingLogger::ScopedTiming t("Resolve Types", timings);
1739 context.ForAll(0, dex_file.NumTypeIds(), ResolveType, thread_count_);
1740 }
1741
1742 TimingLogger::ScopedTiming t("Resolve MethodsAndFields", timings);
1743 context.ForAll(0, dex_file.NumClassDefs(), ResolveClassFieldsAndMethods, thread_count_);
1744 }
1745
SetVerified(jobject class_loader,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,TimingLogger * timings)1746 void CompilerDriver::SetVerified(jobject class_loader, const std::vector<const DexFile*>& dex_files,
1747 ThreadPool* thread_pool, TimingLogger* timings) {
1748 for (size_t i = 0; i != dex_files.size(); ++i) {
1749 const DexFile* dex_file = dex_files[i];
1750 CHECK(dex_file != nullptr);
1751 SetVerifiedDexFile(class_loader, *dex_file, dex_files, thread_pool, timings);
1752 }
1753 }
1754
Verify(jobject class_loader,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,TimingLogger * timings)1755 void CompilerDriver::Verify(jobject class_loader, const std::vector<const DexFile*>& dex_files,
1756 ThreadPool* thread_pool, TimingLogger* timings) {
1757 for (size_t i = 0; i != dex_files.size(); ++i) {
1758 const DexFile* dex_file = dex_files[i];
1759 CHECK(dex_file != nullptr);
1760 VerifyDexFile(class_loader, *dex_file, dex_files, thread_pool, timings);
1761 }
1762 }
1763
VerifyClass(const ParallelCompilationManager * manager,size_t class_def_index)1764 static void VerifyClass(const ParallelCompilationManager* manager, size_t class_def_index)
1765 LOCKS_EXCLUDED(Locks::mutator_lock_) {
1766 ATRACE_CALL();
1767 ScopedObjectAccess soa(Thread::Current());
1768 const DexFile& dex_file = *manager->GetDexFile();
1769 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1770 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1771 ClassLinker* class_linker = manager->GetClassLinker();
1772 jobject jclass_loader = manager->GetClassLoader();
1773 StackHandleScope<3> hs(soa.Self());
1774 Handle<mirror::ClassLoader> class_loader(
1775 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1776 Handle<mirror::Class> klass(
1777 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
1778 if (klass.Get() == nullptr) {
1779 CHECK(soa.Self()->IsExceptionPending());
1780 soa.Self()->ClearException();
1781
1782 /*
1783 * At compile time, we can still structurally verify the class even if FindClass fails.
1784 * This is to ensure the class is structurally sound for compilation. An unsound class
1785 * will be rejected by the verifier and later skipped during compilation in the compiler.
1786 */
1787 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
1788 std::string error_msg;
1789 if (verifier::MethodVerifier::VerifyClass(&dex_file, dex_cache, class_loader, &class_def, true,
1790 &error_msg) ==
1791 verifier::MethodVerifier::kHardFailure) {
1792 LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(descriptor)
1793 << " because: " << error_msg;
1794 }
1795 } else if (!SkipClass(jclass_loader, dex_file, klass.Get())) {
1796 CHECK(klass->IsResolved()) << PrettyClass(klass.Get());
1797 class_linker->VerifyClass(klass);
1798
1799 if (klass->IsErroneous()) {
1800 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
1801 CHECK(soa.Self()->IsExceptionPending());
1802 soa.Self()->ClearException();
1803 }
1804
1805 CHECK(klass->IsCompileTimeVerified() || klass->IsErroneous())
1806 << PrettyDescriptor(klass.Get()) << ": state=" << klass->GetStatus();
1807 }
1808 soa.Self()->AssertNoPendingException();
1809 }
1810
VerifyDexFile(jobject class_loader,const DexFile & dex_file,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,TimingLogger * timings)1811 void CompilerDriver::VerifyDexFile(jobject class_loader, const DexFile& dex_file,
1812 const std::vector<const DexFile*>& dex_files,
1813 ThreadPool* thread_pool, TimingLogger* timings) {
1814 TimingLogger::ScopedTiming t("Verify Dex File", timings);
1815 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1816 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
1817 thread_pool);
1818 context.ForAll(0, dex_file.NumClassDefs(), VerifyClass, thread_count_);
1819 }
1820
SetVerifiedClass(const ParallelCompilationManager * manager,size_t class_def_index)1821 static void SetVerifiedClass(const ParallelCompilationManager* manager, size_t class_def_index)
1822 LOCKS_EXCLUDED(Locks::mutator_lock_) {
1823 ATRACE_CALL();
1824 ScopedObjectAccess soa(Thread::Current());
1825 const DexFile& dex_file = *manager->GetDexFile();
1826 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1827 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1828 ClassLinker* class_linker = manager->GetClassLinker();
1829 jobject jclass_loader = manager->GetClassLoader();
1830 StackHandleScope<3> hs(soa.Self());
1831 Handle<mirror::ClassLoader> class_loader(
1832 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1833 Handle<mirror::Class> klass(
1834 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
1835 // Class might have failed resolution. Then don't set it to verified.
1836 if (klass.Get() != nullptr) {
1837 // Only do this if the class is resolved. If even resolution fails, quickening will go very,
1838 // very wrong.
1839 if (klass->IsResolved()) {
1840 if (klass->GetStatus() < mirror::Class::kStatusVerified) {
1841 ObjectLock<mirror::Class> lock(soa.Self(), klass);
1842 klass->SetStatus(mirror::Class::kStatusVerified, soa.Self());
1843 }
1844 // Record the final class status if necessary.
1845 ClassReference ref(manager->GetDexFile(), class_def_index);
1846 manager->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
1847 }
1848 } else {
1849 Thread* self = soa.Self();
1850 DCHECK(self->IsExceptionPending());
1851 self->ClearException();
1852 }
1853 }
1854
SetVerifiedDexFile(jobject class_loader,const DexFile & dex_file,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,TimingLogger * timings)1855 void CompilerDriver::SetVerifiedDexFile(jobject class_loader, const DexFile& dex_file,
1856 const std::vector<const DexFile*>& dex_files,
1857 ThreadPool* thread_pool, TimingLogger* timings) {
1858 TimingLogger::ScopedTiming t("Verify Dex File", timings);
1859 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1860 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
1861 thread_pool);
1862 context.ForAll(0, dex_file.NumClassDefs(), SetVerifiedClass, thread_count_);
1863 }
1864
InitializeClass(const ParallelCompilationManager * manager,size_t class_def_index)1865 static void InitializeClass(const ParallelCompilationManager* manager, size_t class_def_index)
1866 LOCKS_EXCLUDED(Locks::mutator_lock_) {
1867 ATRACE_CALL();
1868 jobject jclass_loader = manager->GetClassLoader();
1869 const DexFile& dex_file = *manager->GetDexFile();
1870 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1871 const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def.class_idx_);
1872 const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_);
1873
1874 ScopedObjectAccess soa(Thread::Current());
1875 StackHandleScope<3> hs(soa.Self());
1876 Handle<mirror::ClassLoader> class_loader(
1877 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
1878 Handle<mirror::Class> klass(
1879 hs.NewHandle(manager->GetClassLinker()->FindClass(soa.Self(), descriptor, class_loader)));
1880
1881 if (klass.Get() != nullptr && !SkipClass(jclass_loader, dex_file, klass.Get())) {
1882 // Only try to initialize classes that were successfully verified.
1883 if (klass->IsVerified()) {
1884 // Attempt to initialize the class but bail if we either need to initialize the super-class
1885 // or static fields.
1886 manager->GetClassLinker()->EnsureInitialized(klass, false, false);
1887 if (!klass->IsInitialized()) {
1888 // We don't want non-trivial class initialization occurring on multiple threads due to
1889 // deadlock problems. For example, a parent class is initialized (holding its lock) that
1890 // refers to a sub-class in its static/class initializer causing it to try to acquire the
1891 // sub-class' lock. While on a second thread the sub-class is initialized (holding its lock)
1892 // after first initializing its parents, whose locks are acquired. This leads to a
1893 // parent-to-child and a child-to-parent lock ordering and consequent potential deadlock.
1894 // We need to use an ObjectLock due to potential suspension in the interpreting code. Rather
1895 // than use a special Object for the purpose we use the Class of java.lang.Class.
1896 Handle<mirror::Class> h_klass(hs.NewHandle(klass->GetClass()));
1897 ObjectLock<mirror::Class> lock(soa.Self(), h_klass);
1898 // Attempt to initialize allowing initialization of parent classes but still not static
1899 // fields.
1900 manager->GetClassLinker()->EnsureInitialized(klass, false, true);
1901 if (!klass->IsInitialized()) {
1902 // We need to initialize static fields, we only do this for image classes that aren't
1903 // marked with the $NoPreloadHolder (which implies this should not be initialized early).
1904 bool can_init_static_fields = manager->GetCompiler()->IsImage() &&
1905 manager->GetCompiler()->IsImageClass(descriptor) &&
1906 !StringPiece(descriptor).ends_with("$NoPreloadHolder;");
1907 if (can_init_static_fields) {
1908 VLOG(compiler) << "Initializing: " << descriptor;
1909 // TODO multithreading support. We should ensure the current compilation thread has
1910 // exclusive access to the runtime and the transaction. To achieve this, we could use
1911 // a ReaderWriterMutex but we're holding the mutator lock so we fail mutex sanity
1912 // checks in Thread::AssertThreadSuspensionIsAllowable.
1913 Runtime* const runtime = Runtime::Current();
1914 Transaction transaction;
1915
1916 // Run the class initializer in transaction mode.
1917 runtime->EnterTransactionMode(&transaction);
1918 const mirror::Class::Status old_status = klass->GetStatus();
1919 bool success = manager->GetClassLinker()->EnsureInitialized(klass, true, true);
1920 // TODO we detach transaction from runtime to indicate we quit the transactional
1921 // mode which prevents the GC from visiting objects modified during the transaction.
1922 // Ensure GC is not run so don't access freed objects when aborting transaction.
1923 const char* old_casue = soa.Self()->StartAssertNoThreadSuspension("Transaction end");
1924 runtime->ExitTransactionMode();
1925
1926 if (!success) {
1927 CHECK(soa.Self()->IsExceptionPending());
1928 ThrowLocation throw_location;
1929 mirror::Throwable* exception = soa.Self()->GetException(&throw_location);
1930 VLOG(compiler) << "Initialization of " << descriptor << " aborted because of "
1931 << exception->Dump();
1932 soa.Self()->ClearException();
1933 transaction.Abort();
1934 CHECK_EQ(old_status, klass->GetStatus()) << "Previous class status not restored";
1935 }
1936 soa.Self()->EndAssertNoThreadSuspension(old_casue);
1937 }
1938 }
1939 soa.Self()->AssertNoPendingException();
1940 }
1941 }
1942 // Record the final class status if necessary.
1943 ClassReference ref(manager->GetDexFile(), class_def_index);
1944 manager->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
1945 }
1946 // Clear any class not found or verification exceptions.
1947 soa.Self()->ClearException();
1948 }
1949
InitializeClasses(jobject jni_class_loader,const DexFile & dex_file,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,TimingLogger * timings)1950 void CompilerDriver::InitializeClasses(jobject jni_class_loader, const DexFile& dex_file,
1951 const std::vector<const DexFile*>& dex_files,
1952 ThreadPool* thread_pool, TimingLogger* timings) {
1953 TimingLogger::ScopedTiming t("InitializeNoClinit", timings);
1954 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1955 ParallelCompilationManager context(class_linker, jni_class_loader, this, &dex_file, dex_files,
1956 thread_pool);
1957 size_t thread_count;
1958 if (IsImage()) {
1959 // TODO: remove this when transactional mode supports multithreading.
1960 thread_count = 1U;
1961 } else {
1962 thread_count = thread_count_;
1963 }
1964 context.ForAll(0, dex_file.NumClassDefs(), InitializeClass, thread_count);
1965 }
1966
InitializeClasses(jobject class_loader,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,TimingLogger * timings)1967 void CompilerDriver::InitializeClasses(jobject class_loader,
1968 const std::vector<const DexFile*>& dex_files,
1969 ThreadPool* thread_pool, TimingLogger* timings) {
1970 for (size_t i = 0; i != dex_files.size(); ++i) {
1971 const DexFile* dex_file = dex_files[i];
1972 CHECK(dex_file != nullptr);
1973 InitializeClasses(class_loader, *dex_file, dex_files, thread_pool, timings);
1974 }
1975 if (IsImage()) {
1976 // Prune garbage objects created during aborted transactions.
1977 Runtime::Current()->GetHeap()->CollectGarbage(true);
1978 }
1979 }
1980
Compile(jobject class_loader,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,TimingLogger * timings)1981 void CompilerDriver::Compile(jobject class_loader, const std::vector<const DexFile*>& dex_files,
1982 ThreadPool* thread_pool, TimingLogger* timings) {
1983 for (size_t i = 0; i != dex_files.size(); ++i) {
1984 const DexFile* dex_file = dex_files[i];
1985 CHECK(dex_file != nullptr);
1986 CompileDexFile(class_loader, *dex_file, dex_files, thread_pool, timings);
1987 }
1988 VLOG(compiler) << "Compile: " << GetMemoryUsageString(false);
1989 }
1990
CompileClass(const ParallelCompilationManager * manager,size_t class_def_index)1991 void CompilerDriver::CompileClass(const ParallelCompilationManager* manager, size_t class_def_index) {
1992 ATRACE_CALL();
1993 const DexFile& dex_file = *manager->GetDexFile();
1994 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1995 ClassLinker* class_linker = manager->GetClassLinker();
1996 jobject jclass_loader = manager->GetClassLoader();
1997 {
1998 // Use a scoped object access to perform to the quick SkipClass check.
1999 const char* descriptor = dex_file.GetClassDescriptor(class_def);
2000 ScopedObjectAccess soa(Thread::Current());
2001 StackHandleScope<3> hs(soa.Self());
2002 Handle<mirror::ClassLoader> class_loader(
2003 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
2004 Handle<mirror::Class> klass(
2005 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
2006 if (klass.Get() == nullptr) {
2007 CHECK(soa.Self()->IsExceptionPending());
2008 soa.Self()->ClearException();
2009 } else if (SkipClass(jclass_loader, dex_file, klass.Get())) {
2010 return;
2011 }
2012 }
2013 ClassReference ref(&dex_file, class_def_index);
2014 // Skip compiling classes with generic verifier failures since they will still fail at runtime
2015 if (manager->GetCompiler()->verification_results_->IsClassRejected(ref)) {
2016 return;
2017 }
2018 const byte* class_data = dex_file.GetClassData(class_def);
2019 if (class_data == nullptr) {
2020 // empty class, probably a marker interface
2021 return;
2022 }
2023
2024 // Can we run DEX-to-DEX compiler on this class ?
2025 DexToDexCompilationLevel dex_to_dex_compilation_level = kDontDexToDexCompile;
2026 {
2027 ScopedObjectAccess soa(Thread::Current());
2028 StackHandleScope<1> hs(soa.Self());
2029 Handle<mirror::ClassLoader> class_loader(
2030 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(jclass_loader)));
2031 dex_to_dex_compilation_level = GetDexToDexCompilationlevel(soa.Self(), class_loader, dex_file,
2032 class_def);
2033 }
2034 ClassDataItemIterator it(dex_file, class_data);
2035 // Skip fields
2036 while (it.HasNextStaticField()) {
2037 it.Next();
2038 }
2039 while (it.HasNextInstanceField()) {
2040 it.Next();
2041 }
2042 CompilerDriver* driver = manager->GetCompiler();
2043
2044 bool compilation_enabled = driver->IsClassToCompile(
2045 dex_file.StringByTypeIdx(class_def.class_idx_));
2046
2047 // Compile direct methods
2048 int64_t previous_direct_method_idx = -1;
2049 while (it.HasNextDirectMethod()) {
2050 uint32_t method_idx = it.GetMemberIndex();
2051 if (method_idx == previous_direct_method_idx) {
2052 // smali can create dex files with two encoded_methods sharing the same method_idx
2053 // http://code.google.com/p/smali/issues/detail?id=119
2054 it.Next();
2055 continue;
2056 }
2057 previous_direct_method_idx = method_idx;
2058 driver->CompileMethod(it.GetMethodCodeItem(), it.GetMethodAccessFlags(),
2059 it.GetMethodInvokeType(class_def), class_def_index,
2060 method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level,
2061 compilation_enabled);
2062 it.Next();
2063 }
2064 // Compile virtual methods
2065 int64_t previous_virtual_method_idx = -1;
2066 while (it.HasNextVirtualMethod()) {
2067 uint32_t method_idx = it.GetMemberIndex();
2068 if (method_idx == previous_virtual_method_idx) {
2069 // smali can create dex files with two encoded_methods sharing the same method_idx
2070 // http://code.google.com/p/smali/issues/detail?id=119
2071 it.Next();
2072 continue;
2073 }
2074 previous_virtual_method_idx = method_idx;
2075 driver->CompileMethod(it.GetMethodCodeItem(), it.GetMethodAccessFlags(),
2076 it.GetMethodInvokeType(class_def), class_def_index,
2077 method_idx, jclass_loader, dex_file, dex_to_dex_compilation_level,
2078 compilation_enabled);
2079 it.Next();
2080 }
2081 DCHECK(!it.HasNext());
2082 }
2083
CompileDexFile(jobject class_loader,const DexFile & dex_file,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,TimingLogger * timings)2084 void CompilerDriver::CompileDexFile(jobject class_loader, const DexFile& dex_file,
2085 const std::vector<const DexFile*>& dex_files,
2086 ThreadPool* thread_pool, TimingLogger* timings) {
2087 TimingLogger::ScopedTiming t("Compile Dex File", timings);
2088 ParallelCompilationManager context(Runtime::Current()->GetClassLinker(), class_loader, this,
2089 &dex_file, dex_files, thread_pool);
2090 context.ForAll(0, dex_file.NumClassDefs(), CompilerDriver::CompileClass, thread_count_);
2091 }
2092
CompileMethod(const DexFile::CodeItem * code_item,uint32_t access_flags,InvokeType invoke_type,uint16_t class_def_idx,uint32_t method_idx,jobject class_loader,const DexFile & dex_file,DexToDexCompilationLevel dex_to_dex_compilation_level,bool compilation_enabled)2093 void CompilerDriver::CompileMethod(const DexFile::CodeItem* code_item, uint32_t access_flags,
2094 InvokeType invoke_type, uint16_t class_def_idx,
2095 uint32_t method_idx, jobject class_loader,
2096 const DexFile& dex_file,
2097 DexToDexCompilationLevel dex_to_dex_compilation_level,
2098 bool compilation_enabled) {
2099 CompiledMethod* compiled_method = nullptr;
2100 uint64_t start_ns = kTimeCompileMethod ? NanoTime() : 0;
2101 MethodReference method_ref(&dex_file, method_idx);
2102
2103 if ((access_flags & kAccNative) != 0) {
2104 // Are we interpreting only and have support for generic JNI down calls?
2105 if (!compiler_options_->IsCompilationEnabled() &&
2106 (instruction_set_ == kX86_64 || instruction_set_ == kArm64)) {
2107 // Leaving this empty will trigger the generic JNI version
2108 } else {
2109 compiled_method = compiler_->JniCompile(access_flags, method_idx, dex_file);
2110 CHECK(compiled_method != nullptr);
2111 }
2112 } else if ((access_flags & kAccAbstract) != 0) {
2113 } else {
2114 bool has_verified_method = verification_results_->GetVerifiedMethod(method_ref) != nullptr;
2115 bool compile = compilation_enabled &&
2116 // Basic checks, e.g., not <clinit>.
2117 verification_results_->IsCandidateForCompilation(method_ref, access_flags) &&
2118 // Did not fail to create VerifiedMethod metadata.
2119 has_verified_method;
2120 if (compile) {
2121 // NOTE: if compiler declines to compile this method, it will return nullptr.
2122 compiled_method = compiler_->Compile(code_item, access_flags, invoke_type, class_def_idx,
2123 method_idx, class_loader, dex_file);
2124 }
2125 if (compiled_method == nullptr && dex_to_dex_compilation_level != kDontDexToDexCompile) {
2126 // TODO: add a command-line option to disable DEX-to-DEX compilation ?
2127 // Do not optimize if a VerifiedMethod is missing. SafeCast elision, for example, relies on
2128 // it.
2129 (*dex_to_dex_compiler_)(*this, code_item, access_flags,
2130 invoke_type, class_def_idx,
2131 method_idx, class_loader, dex_file,
2132 has_verified_method ? dex_to_dex_compilation_level : kRequired);
2133 }
2134 }
2135 if (kTimeCompileMethod) {
2136 uint64_t duration_ns = NanoTime() - start_ns;
2137 if (duration_ns > MsToNs(compiler_->GetMaximumCompilationTimeBeforeWarning())) {
2138 LOG(WARNING) << "Compilation of " << PrettyMethod(method_idx, dex_file)
2139 << " took " << PrettyDuration(duration_ns);
2140 }
2141 }
2142
2143 Thread* self = Thread::Current();
2144 if (compiled_method != nullptr) {
2145 DCHECK(GetCompiledMethod(method_ref) == nullptr) << PrettyMethod(method_idx, dex_file);
2146 {
2147 MutexLock mu(self, compiled_methods_lock_);
2148 compiled_methods_.Put(method_ref, compiled_method);
2149 }
2150 DCHECK(GetCompiledMethod(method_ref) != nullptr) << PrettyMethod(method_idx, dex_file);
2151 }
2152
2153 // Done compiling, delete the verified method to reduce native memory usage.
2154 verification_results_->RemoveVerifiedMethod(method_ref);
2155
2156 if (self->IsExceptionPending()) {
2157 ScopedObjectAccess soa(self);
2158 LOG(FATAL) << "Unexpected exception compiling: " << PrettyMethod(method_idx, dex_file) << "\n"
2159 << self->GetException(nullptr)->Dump();
2160 }
2161 }
2162
GetCompiledClass(ClassReference ref) const2163 CompiledClass* CompilerDriver::GetCompiledClass(ClassReference ref) const {
2164 MutexLock mu(Thread::Current(), compiled_classes_lock_);
2165 ClassTable::const_iterator it = compiled_classes_.find(ref);
2166 if (it == compiled_classes_.end()) {
2167 return nullptr;
2168 }
2169 CHECK(it->second != nullptr);
2170 return it->second;
2171 }
2172
RecordClassStatus(ClassReference ref,mirror::Class::Status status)2173 void CompilerDriver::RecordClassStatus(ClassReference ref, mirror::Class::Status status) {
2174 MutexLock mu(Thread::Current(), compiled_classes_lock_);
2175 auto it = compiled_classes_.find(ref);
2176 if (it == compiled_classes_.end() || it->second->GetStatus() != status) {
2177 // An entry doesn't exist or the status is lower than the new status.
2178 if (it != compiled_classes_.end()) {
2179 CHECK_GT(status, it->second->GetStatus());
2180 delete it->second;
2181 }
2182 switch (status) {
2183 case mirror::Class::kStatusNotReady:
2184 case mirror::Class::kStatusError:
2185 case mirror::Class::kStatusRetryVerificationAtRuntime:
2186 case mirror::Class::kStatusVerified:
2187 case mirror::Class::kStatusInitialized:
2188 break; // Expected states.
2189 default:
2190 LOG(FATAL) << "Unexpected class status for class "
2191 << PrettyDescriptor(ref.first->GetClassDescriptor(ref.first->GetClassDef(ref.second)))
2192 << " of " << status;
2193 }
2194 CompiledClass* compiled_class = new CompiledClass(status);
2195 compiled_classes_.Overwrite(ref, compiled_class);
2196 }
2197 }
2198
GetCompiledMethod(MethodReference ref) const2199 CompiledMethod* CompilerDriver::GetCompiledMethod(MethodReference ref) const {
2200 MutexLock mu(Thread::Current(), compiled_methods_lock_);
2201 MethodTable::const_iterator it = compiled_methods_.find(ref);
2202 if (it == compiled_methods_.end()) {
2203 return nullptr;
2204 }
2205 CHECK(it->second != nullptr);
2206 return it->second;
2207 }
2208
AddRequiresConstructorBarrier(Thread * self,const DexFile * dex_file,uint16_t class_def_index)2209 void CompilerDriver::AddRequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
2210 uint16_t class_def_index) {
2211 WriterMutexLock mu(self, freezing_constructor_lock_);
2212 freezing_constructor_classes_.insert(ClassReference(dex_file, class_def_index));
2213 }
2214
RequiresConstructorBarrier(Thread * self,const DexFile * dex_file,uint16_t class_def_index)2215 bool CompilerDriver::RequiresConstructorBarrier(Thread* self, const DexFile* dex_file,
2216 uint16_t class_def_index) {
2217 ReaderMutexLock mu(self, freezing_constructor_lock_);
2218 return freezing_constructor_classes_.count(ClassReference(dex_file, class_def_index)) != 0;
2219 }
2220
WriteElf(const std::string & android_root,bool is_host,const std::vector<const art::DexFile * > & dex_files,OatWriter * oat_writer,art::File * file)2221 bool CompilerDriver::WriteElf(const std::string& android_root,
2222 bool is_host,
2223 const std::vector<const art::DexFile*>& dex_files,
2224 OatWriter* oat_writer,
2225 art::File* file)
2226 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2227 return compiler_->WriteElf(file, oat_writer, dex_files, android_root, is_host);
2228 }
InstructionSetToLLVMTarget(InstructionSet instruction_set,std::string * target_triple,std::string * target_cpu,std::string * target_attr)2229 void CompilerDriver::InstructionSetToLLVMTarget(InstructionSet instruction_set,
2230 std::string* target_triple,
2231 std::string* target_cpu,
2232 std::string* target_attr) {
2233 switch (instruction_set) {
2234 case kThumb2:
2235 *target_triple = "thumb-none-linux-gnueabi";
2236 *target_cpu = "cortex-a9";
2237 *target_attr = "+thumb2,+neon,+neonfp,+vfp3,+db";
2238 break;
2239
2240 case kArm:
2241 *target_triple = "armv7-none-linux-gnueabi";
2242 // TODO: Fix for Nexus S.
2243 *target_cpu = "cortex-a9";
2244 // TODO: Fix for Xoom.
2245 *target_attr = "+v7,+neon,+neonfp,+vfp3,+db";
2246 break;
2247
2248 case kX86:
2249 *target_triple = "i386-pc-linux-gnu";
2250 *target_attr = "";
2251 break;
2252
2253 case kX86_64:
2254 *target_triple = "x86_64-pc-linux-gnu";
2255 *target_attr = "";
2256 break;
2257
2258 case kMips:
2259 *target_triple = "mipsel-unknown-linux";
2260 *target_attr = "mips32r2";
2261 break;
2262
2263 default:
2264 LOG(FATAL) << "Unknown instruction set: " << instruction_set;
2265 }
2266 }
2267
SkipCompilation(const std::string & method_name)2268 bool CompilerDriver::SkipCompilation(const std::string& method_name) {
2269 if (!profile_present_) {
2270 return false;
2271 }
2272 // First find the method in the profile file.
2273 ProfileFile::ProfileData data;
2274 if (!profile_file_.GetProfileData(&data, method_name)) {
2275 // Not in profile, no information can be determined.
2276 if (kIsDebugBuild) {
2277 VLOG(compiler) << "not compiling " << method_name << " because it's not in the profile";
2278 }
2279 return true;
2280 }
2281
2282 // Methods that comprise top_k_threshold % of the total samples will be compiled.
2283 // Compare against the start of the topK percentage bucket just in case the threshold
2284 // falls inside a bucket.
2285 bool compile = data.GetTopKUsedPercentage() - data.GetUsedPercent()
2286 <= compiler_options_->GetTopKProfileThreshold();
2287 if (kIsDebugBuild) {
2288 if (compile) {
2289 LOG(INFO) << "compiling method " << method_name << " because its usage is part of top "
2290 << data.GetTopKUsedPercentage() << "% with a percent of " << data.GetUsedPercent() << "%"
2291 << " (topKThreshold=" << compiler_options_->GetTopKProfileThreshold() << ")";
2292 } else {
2293 VLOG(compiler) << "not compiling method " << method_name
2294 << " because it's not part of leading " << compiler_options_->GetTopKProfileThreshold()
2295 << "% samples)";
2296 }
2297 }
2298 return !compile;
2299 }
2300
GetMemoryUsageString(bool extended) const2301 std::string CompilerDriver::GetMemoryUsageString(bool extended) const {
2302 std::ostringstream oss;
2303 const ArenaPool* arena_pool = GetArenaPool();
2304 gc::Heap* heap = Runtime::Current()->GetHeap();
2305 oss << "arena alloc=" << PrettySize(arena_pool->GetBytesAllocated());
2306 oss << " java alloc=" << PrettySize(heap->GetBytesAllocated());
2307 #ifdef HAVE_MALLOC_H
2308 struct mallinfo info = mallinfo();
2309 const size_t allocated_space = static_cast<size_t>(info.uordblks);
2310 const size_t free_space = static_cast<size_t>(info.fordblks);
2311 oss << " native alloc=" << PrettySize(allocated_space) << " free="
2312 << PrettySize(free_space);
2313 #endif
2314 if (swap_space_.get() != nullptr) {
2315 oss << " swap=" << PrettySize(swap_space_->GetSize());
2316 }
2317 if (extended) {
2318 oss << "\nCode dedupe: " << dedupe_code_.DumpStats();
2319 oss << "\nMapping table dedupe: " << dedupe_mapping_table_.DumpStats();
2320 oss << "\nVmap table dedupe: " << dedupe_vmap_table_.DumpStats();
2321 oss << "\nGC map dedupe: " << dedupe_gc_map_.DumpStats();
2322 oss << "\nCFI info dedupe: " << dedupe_cfi_info_.DumpStats();
2323 }
2324 return oss.str();
2325 }
2326
2327 } // namespace art
2328