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 #include <unordered_set>
20 #include <vector>
21 #include <unistd.h>
22
23 #ifndef __APPLE__
24 #include <malloc.h> // For mallinfo
25 #endif
26
27 #include "android-base/strings.h"
28
29 #include "art_field-inl.h"
30 #include "art_method-inl.h"
31 #include "base/arena_allocator.h"
32 #include "base/array_ref.h"
33 #include "base/bit_vector.h"
34 #include "base/enums.h"
35 #include "base/stl_util.h"
36 #include "base/systrace.h"
37 #include "base/time_utils.h"
38 #include "base/timing_logger.h"
39 #include "class_linker-inl.h"
40 #include "compiled_method.h"
41 #include "compiler.h"
42 #include "compiler_callbacks.h"
43 #include "compiler_driver-inl.h"
44 #include "dex/dex_to_dex_compiler.h"
45 #include "dex/verification_results.h"
46 #include "dex/verified_method.h"
47 #include "dex_compilation_unit.h"
48 #include "dex_file-inl.h"
49 #include "dex_instruction-inl.h"
50 #include "driver/compiler_options.h"
51 #include "gc/accounting/card_table-inl.h"
52 #include "gc/accounting/heap_bitmap.h"
53 #include "gc/space/image_space.h"
54 #include "gc/space/space.h"
55 #include "handle_scope-inl.h"
56 #include "intrinsics_enum.h"
57 #include "jni_internal.h"
58 #include "mirror/class-inl.h"
59 #include "mirror/class_loader.h"
60 #include "mirror/dex_cache-inl.h"
61 #include "mirror/object-inl.h"
62 #include "mirror/object-refvisitor-inl.h"
63 #include "mirror/object_array-inl.h"
64 #include "mirror/throwable.h"
65 #include "nativehelper/ScopedLocalRef.h"
66 #include "object_lock.h"
67 #include "runtime.h"
68 #include "scoped_thread_state_change-inl.h"
69 #include "thread.h"
70 #include "thread_list.h"
71 #include "thread_pool.h"
72 #include "trampolines/trampoline_compiler.h"
73 #include "transaction.h"
74 #include "utils/atomic_dex_ref_map-inl.h"
75 #include "utils/dex_cache_arrays_layout-inl.h"
76 #include "utils/swap_space.h"
77 #include "vdex_file.h"
78 #include "verifier/method_verifier-inl.h"
79 #include "verifier/method_verifier.h"
80 #include "verifier/verifier_deps.h"
81 #include "verifier/verifier_enums.h"
82
83 namespace art {
84
85 static constexpr bool kTimeCompileMethod = !kIsDebugBuild;
86
87 // Print additional info during profile guided compilation.
88 static constexpr bool kDebugProfileGuidedCompilation = false;
89
90 // Max encoded fields allowed for initializing app image. Hardcode the number for now
91 // because 5000 should be large enough.
92 static constexpr uint32_t kMaxEncodedFields = 5000;
93
Percentage(size_t x,size_t y)94 static double Percentage(size_t x, size_t y) {
95 return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
96 }
97
DumpStat(size_t x,size_t y,const char * str)98 static void DumpStat(size_t x, size_t y, const char* str) {
99 if (x == 0 && y == 0) {
100 return;
101 }
102 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
103 }
104
105 class CompilerDriver::AOTCompilationStats {
106 public:
AOTCompilationStats()107 AOTCompilationStats()
108 : stats_lock_("AOT compilation statistics lock"),
109 resolved_types_(0), unresolved_types_(0),
110 resolved_instance_fields_(0), unresolved_instance_fields_(0),
111 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0),
112 type_based_devirtualization_(0),
113 safe_casts_(0), not_safe_casts_(0) {
114 for (size_t i = 0; i <= kMaxInvokeType; i++) {
115 resolved_methods_[i] = 0;
116 unresolved_methods_[i] = 0;
117 virtual_made_direct_[i] = 0;
118 direct_calls_to_boot_[i] = 0;
119 direct_methods_to_boot_[i] = 0;
120 }
121 }
122
Dump()123 void Dump() {
124 DumpStat(resolved_types_, unresolved_types_, "types resolved");
125 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
126 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
127 "static fields resolved");
128 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
129 "static fields local to a class");
130 DumpStat(safe_casts_, not_safe_casts_, "check-casts removed based on type information");
131 // Note, the code below subtracts the stat value so that when added to the stat value we have
132 // 100% of samples. TODO: clean this up.
133 DumpStat(type_based_devirtualization_,
134 resolved_methods_[kVirtual] + unresolved_methods_[kVirtual] +
135 resolved_methods_[kInterface] + unresolved_methods_[kInterface] -
136 type_based_devirtualization_,
137 "virtual/interface calls made direct based on type information");
138
139 for (size_t i = 0; i <= kMaxInvokeType; i++) {
140 std::ostringstream oss;
141 oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
142 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
143 if (virtual_made_direct_[i] > 0) {
144 std::ostringstream oss2;
145 oss2 << static_cast<InvokeType>(i) << " methods made direct";
146 DumpStat(virtual_made_direct_[i],
147 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
148 oss2.str().c_str());
149 }
150 if (direct_calls_to_boot_[i] > 0) {
151 std::ostringstream oss2;
152 oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
153 DumpStat(direct_calls_to_boot_[i],
154 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
155 oss2.str().c_str());
156 }
157 if (direct_methods_to_boot_[i] > 0) {
158 std::ostringstream oss2;
159 oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
160 DumpStat(direct_methods_to_boot_[i],
161 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
162 oss2.str().c_str());
163 }
164 }
165 }
166
167 // Allow lossy statistics in non-debug builds.
168 #ifndef NDEBUG
169 #define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_)
170 #else
171 #define STATS_LOCK()
172 #endif
173
TypeDoesntNeedAccessCheck()174 void TypeDoesntNeedAccessCheck() REQUIRES(!stats_lock_) {
175 STATS_LOCK();
176 resolved_types_++;
177 }
178
TypeNeedsAccessCheck()179 void TypeNeedsAccessCheck() REQUIRES(!stats_lock_) {
180 STATS_LOCK();
181 unresolved_types_++;
182 }
183
ResolvedInstanceField()184 void ResolvedInstanceField() REQUIRES(!stats_lock_) {
185 STATS_LOCK();
186 resolved_instance_fields_++;
187 }
188
UnresolvedInstanceField()189 void UnresolvedInstanceField() REQUIRES(!stats_lock_) {
190 STATS_LOCK();
191 unresolved_instance_fields_++;
192 }
193
ResolvedLocalStaticField()194 void ResolvedLocalStaticField() REQUIRES(!stats_lock_) {
195 STATS_LOCK();
196 resolved_local_static_fields_++;
197 }
198
ResolvedStaticField()199 void ResolvedStaticField() REQUIRES(!stats_lock_) {
200 STATS_LOCK();
201 resolved_static_fields_++;
202 }
203
UnresolvedStaticField()204 void UnresolvedStaticField() REQUIRES(!stats_lock_) {
205 STATS_LOCK();
206 unresolved_static_fields_++;
207 }
208
209 // Indicate that type information from the verifier led to devirtualization.
PreciseTypeDevirtualization()210 void PreciseTypeDevirtualization() REQUIRES(!stats_lock_) {
211 STATS_LOCK();
212 type_based_devirtualization_++;
213 }
214
215 // A check-cast could be eliminated due to verifier type analysis.
SafeCast()216 void SafeCast() REQUIRES(!stats_lock_) {
217 STATS_LOCK();
218 safe_casts_++;
219 }
220
221 // A check-cast couldn't be eliminated due to verifier type analysis.
NotASafeCast()222 void NotASafeCast() REQUIRES(!stats_lock_) {
223 STATS_LOCK();
224 not_safe_casts_++;
225 }
226
227 private:
228 Mutex stats_lock_;
229
230 size_t resolved_types_;
231 size_t unresolved_types_;
232
233 size_t resolved_instance_fields_;
234 size_t unresolved_instance_fields_;
235
236 size_t resolved_local_static_fields_;
237 size_t resolved_static_fields_;
238 size_t unresolved_static_fields_;
239 // Type based devirtualization for invoke interface and virtual.
240 size_t type_based_devirtualization_;
241
242 size_t resolved_methods_[kMaxInvokeType + 1];
243 size_t unresolved_methods_[kMaxInvokeType + 1];
244 size_t virtual_made_direct_[kMaxInvokeType + 1];
245 size_t direct_calls_to_boot_[kMaxInvokeType + 1];
246 size_t direct_methods_to_boot_[kMaxInvokeType + 1];
247
248 size_t safe_casts_;
249 size_t not_safe_casts_;
250
251 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
252 };
253
254 class CompilerDriver::DexFileMethodSet {
255 public:
DexFileMethodSet(const DexFile & dex_file)256 explicit DexFileMethodSet(const DexFile& dex_file)
257 : dex_file_(dex_file),
258 method_indexes_(dex_file.NumMethodIds(), false, Allocator::GetMallocAllocator()) {
259 }
260 DexFileMethodSet(DexFileMethodSet&& other) = default;
261
GetDexFile() const262 const DexFile& GetDexFile() const { return dex_file_; }
263
GetMethodIndexes()264 BitVector& GetMethodIndexes() { return method_indexes_; }
GetMethodIndexes() const265 const BitVector& GetMethodIndexes() const { return method_indexes_; }
266
267 private:
268 const DexFile& dex_file_;
269 BitVector method_indexes_;
270 };
271
CompilerDriver(const CompilerOptions * compiler_options,VerificationResults * verification_results,Compiler::Kind compiler_kind,InstructionSet instruction_set,const InstructionSetFeatures * instruction_set_features,std::unordered_set<std::string> * image_classes,std::unordered_set<std::string> * compiled_classes,std::unordered_set<std::string> * compiled_methods,size_t thread_count,bool dump_stats,bool dump_passes,CumulativeLogger * timer,int swap_fd,const ProfileCompilationInfo * profile_compilation_info)272 CompilerDriver::CompilerDriver(
273 const CompilerOptions* compiler_options,
274 VerificationResults* verification_results,
275 Compiler::Kind compiler_kind,
276 InstructionSet instruction_set,
277 const InstructionSetFeatures* instruction_set_features,
278 std::unordered_set<std::string>* image_classes,
279 std::unordered_set<std::string>* compiled_classes,
280 std::unordered_set<std::string>* compiled_methods,
281 size_t thread_count,
282 bool dump_stats,
283 bool dump_passes,
284 CumulativeLogger* timer,
285 int swap_fd,
286 const ProfileCompilationInfo* profile_compilation_info)
287 : compiler_options_(compiler_options),
288 verification_results_(verification_results),
289 compiler_(Compiler::Create(this, compiler_kind)),
290 compiler_kind_(compiler_kind),
291 instruction_set_(instruction_set == kArm ? kThumb2 : instruction_set),
292 instruction_set_features_(instruction_set_features),
293 requires_constructor_barrier_lock_("constructor barrier lock"),
294 non_relative_linker_patch_count_(0u),
295 image_classes_(image_classes),
296 classes_to_compile_(compiled_classes),
297 methods_to_compile_(compiled_methods),
298 had_hard_verifier_failure_(false),
299 parallel_thread_count_(thread_count),
300 stats_(new AOTCompilationStats),
301 dump_stats_(dump_stats),
302 dump_passes_(dump_passes),
303 timings_logger_(timer),
304 compiler_context_(nullptr),
305 support_boot_image_fixup_(true),
306 compiled_method_storage_(swap_fd),
307 profile_compilation_info_(profile_compilation_info),
308 max_arena_alloc_(0),
309 dex_to_dex_references_lock_("dex-to-dex references lock"),
310 dex_to_dex_references_(),
311 current_dex_to_dex_methods_(nullptr) {
312 DCHECK(compiler_options_ != nullptr);
313
314 compiler_->Init();
315
316 if (GetCompilerOptions().IsBootImage()) {
317 CHECK(image_classes_.get() != nullptr) << "Expected image classes for boot image";
318 }
319 }
320
~CompilerDriver()321 CompilerDriver::~CompilerDriver() {
322 compiled_methods_.Visit([this](const DexFileReference& ref ATTRIBUTE_UNUSED,
323 CompiledMethod* method) {
324 if (method != nullptr) {
325 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(this, method);
326 }
327 });
328 compiler_->UnInit();
329 }
330
331
332 #define CREATE_TRAMPOLINE(type, abi, offset) \
333 if (Is64BitInstructionSet(instruction_set_)) { \
334 return CreateTrampoline64(instruction_set_, abi, \
335 type ## _ENTRYPOINT_OFFSET(PointerSize::k64, offset)); \
336 } else { \
337 return CreateTrampoline32(instruction_set_, abi, \
338 type ## _ENTRYPOINT_OFFSET(PointerSize::k32, offset)); \
339 }
340
CreateJniDlsymLookup() const341 std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateJniDlsymLookup() const {
342 CREATE_TRAMPOLINE(JNI, kJniAbi, pDlsymLookup)
343 }
344
CreateQuickGenericJniTrampoline() const345 std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickGenericJniTrampoline()
346 const {
347 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickGenericJniTrampoline)
348 }
349
CreateQuickImtConflictTrampoline() const350 std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickImtConflictTrampoline()
351 const {
352 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickImtConflictTrampoline)
353 }
354
CreateQuickResolutionTrampoline() const355 std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickResolutionTrampoline()
356 const {
357 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickResolutionTrampoline)
358 }
359
CreateQuickToInterpreterBridge() const360 std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickToInterpreterBridge()
361 const {
362 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickToInterpreterBridge)
363 }
364 #undef CREATE_TRAMPOLINE
365
SetupIntrinsic(Thread * self,Intrinsics intrinsic,InvokeType invoke_type,const char * class_name,const char * method_name,const char * signature)366 static void SetupIntrinsic(Thread* self,
367 Intrinsics intrinsic,
368 InvokeType invoke_type,
369 const char* class_name,
370 const char* method_name,
371 const char* signature)
372 REQUIRES_SHARED(Locks::mutator_lock_) {
373 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
374 PointerSize image_size = class_linker->GetImagePointerSize();
375 ObjPtr<mirror::Class> cls = class_linker->FindSystemClass(self, class_name);
376 if (cls == nullptr) {
377 LOG(FATAL) << "Could not find class of intrinsic " << class_name;
378 }
379 ArtMethod* method = cls->FindClassMethod(method_name, signature, image_size);
380 if (method == nullptr || method->GetDeclaringClass() != cls) {
381 LOG(FATAL) << "Could not find method of intrinsic "
382 << class_name << " " << method_name << " " << signature;
383 }
384 DCHECK_EQ(method->GetInvokeType(), invoke_type);
385 method->SetIntrinsic(static_cast<uint32_t>(intrinsic));
386 }
387
CompileAll(jobject class_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)388 void CompilerDriver::CompileAll(jobject class_loader,
389 const std::vector<const DexFile*>& dex_files,
390 TimingLogger* timings) {
391 DCHECK(!Runtime::Current()->IsStarted());
392
393 InitializeThreadPools();
394
395 VLOG(compiler) << "Before precompile " << GetMemoryUsageString(false);
396 // Precompile:
397 // 1) Load image classes
398 // 2) Resolve all classes
399 // 3) Attempt to verify all classes
400 // 4) Attempt to initialize image classes, and trivially initialized classes
401 PreCompile(class_loader, dex_files, timings);
402 if (GetCompilerOptions().IsBootImage()) {
403 // We don't need to setup the intrinsics for non boot image compilation, as
404 // those compilations will pick up a boot image that have the ArtMethod already
405 // set with the intrinsics flag.
406 ScopedObjectAccess soa(Thread::Current());
407 #define SETUP_INTRINSICS(Name, InvokeType, NeedsEnvironmentOrCache, SideEffects, Exceptions, \
408 ClassName, MethodName, Signature) \
409 SetupIntrinsic(soa.Self(), Intrinsics::k##Name, InvokeType, ClassName, MethodName, Signature);
410 #include "intrinsics_list.h"
411 INTRINSICS_LIST(SETUP_INTRINSICS)
412 #undef INTRINSICS_LIST
413 #undef SETUP_INTRINSICS
414 }
415 // Compile:
416 // 1) Compile all classes and methods enabled for compilation. May fall back to dex-to-dex
417 // compilation.
418 if (GetCompilerOptions().IsAnyCompilationEnabled()) {
419 Compile(class_loader, dex_files, timings);
420 }
421 if (dump_stats_) {
422 stats_->Dump();
423 }
424
425 FreeThreadPools();
426 }
427
GetDexToDexCompilationLevel(Thread * self,const CompilerDriver & driver,Handle<mirror::ClassLoader> class_loader,const DexFile & dex_file,const DexFile::ClassDef & class_def)428 static optimizer::DexToDexCompilationLevel GetDexToDexCompilationLevel(
429 Thread* self, const CompilerDriver& driver, Handle<mirror::ClassLoader> class_loader,
430 const DexFile& dex_file, const DexFile::ClassDef& class_def)
431 REQUIRES_SHARED(Locks::mutator_lock_) {
432 auto* const runtime = Runtime::Current();
433 DCHECK(driver.GetCompilerOptions().IsQuickeningCompilationEnabled());
434 const char* descriptor = dex_file.GetClassDescriptor(class_def);
435 ClassLinker* class_linker = runtime->GetClassLinker();
436 mirror::Class* klass = class_linker->FindClass(self, descriptor, class_loader);
437 if (klass == nullptr) {
438 CHECK(self->IsExceptionPending());
439 self->ClearException();
440 return optimizer::DexToDexCompilationLevel::kDontDexToDexCompile;
441 }
442 // DexToDex at the kOptimize level may introduce quickened opcodes, which replace symbolic
443 // references with actual offsets. We cannot re-verify such instructions.
444 //
445 // We store the verification information in the class status in the oat file, which the linker
446 // can validate (checksums) and use to skip load-time verification. It is thus safe to
447 // optimize when a class has been fully verified before.
448 optimizer::DexToDexCompilationLevel max_level = optimizer::DexToDexCompilationLevel::kOptimize;
449 if (driver.GetCompilerOptions().GetDebuggable()) {
450 // We are debuggable so definitions of classes might be changed. We don't want to do any
451 // optimizations that could break that.
452 max_level = optimizer::DexToDexCompilationLevel::kDontDexToDexCompile;
453 }
454 if (klass->IsVerified()) {
455 // Class is verified so we can enable DEX-to-DEX compilation for performance.
456 return max_level;
457 } else {
458 // Class verification has failed: do not run DEX-to-DEX optimizations.
459 return optimizer::DexToDexCompilationLevel::kDontDexToDexCompile;
460 }
461 }
462
GetDexToDexCompilationLevel(Thread * self,const CompilerDriver & driver,jobject jclass_loader,const DexFile & dex_file,const DexFile::ClassDef & class_def)463 static optimizer::DexToDexCompilationLevel GetDexToDexCompilationLevel(
464 Thread* self,
465 const CompilerDriver& driver,
466 jobject jclass_loader,
467 const DexFile& dex_file,
468 const DexFile::ClassDef& class_def) {
469 ScopedObjectAccess soa(self);
470 StackHandleScope<1> hs(soa.Self());
471 Handle<mirror::ClassLoader> class_loader(
472 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
473 return GetDexToDexCompilationLevel(self, driver, class_loader, dex_file, class_def);
474 }
475
476 // Does the runtime for the InstructionSet provide an implementation returned by
477 // GetQuickGenericJniStub allowing down calls that aren't compiled using a JNI compiler?
InstructionSetHasGenericJniStub(InstructionSet isa)478 static bool InstructionSetHasGenericJniStub(InstructionSet isa) {
479 switch (isa) {
480 case kArm:
481 case kArm64:
482 case kThumb2:
483 case kMips:
484 case kMips64:
485 case kX86:
486 case kX86_64: return true;
487 default: return false;
488 }
489 }
490
CompileMethod(Thread * self,CompilerDriver * driver,const DexFile::CodeItem * code_item,uint32_t access_flags,InvokeType invoke_type,uint16_t class_def_idx,uint32_t method_idx,Handle<mirror::ClassLoader> class_loader,const DexFile & dex_file,optimizer::DexToDexCompilationLevel dex_to_dex_compilation_level,bool compilation_enabled,Handle<mirror::DexCache> dex_cache)491 static void CompileMethod(Thread* self,
492 CompilerDriver* driver,
493 const DexFile::CodeItem* code_item,
494 uint32_t access_flags,
495 InvokeType invoke_type,
496 uint16_t class_def_idx,
497 uint32_t method_idx,
498 Handle<mirror::ClassLoader> class_loader,
499 const DexFile& dex_file,
500 optimizer::DexToDexCompilationLevel dex_to_dex_compilation_level,
501 bool compilation_enabled,
502 Handle<mirror::DexCache> dex_cache) {
503 DCHECK(driver != nullptr);
504 CompiledMethod* compiled_method = nullptr;
505 uint64_t start_ns = kTimeCompileMethod ? NanoTime() : 0;
506 MethodReference method_ref(&dex_file, method_idx);
507
508 if (driver->GetCurrentDexToDexMethods() != nullptr) {
509 // This is the second pass when we dex-to-dex compile previously marked methods.
510 // TODO: Refactor the compilation to avoid having to distinguish the two passes
511 // here. That should be done on a higher level. http://b/29089975
512 if (driver->GetCurrentDexToDexMethods()->IsBitSet(method_idx)) {
513 VerificationResults* results = driver->GetVerificationResults();
514 DCHECK(results != nullptr);
515 const VerifiedMethod* verified_method = results->GetVerifiedMethod(method_ref);
516 // Do not optimize if a VerifiedMethod is missing. SafeCast elision,
517 // for example, relies on it.
518 compiled_method = optimizer::ArtCompileDEX(
519 driver,
520 code_item,
521 access_flags,
522 invoke_type,
523 class_def_idx,
524 method_idx,
525 class_loader,
526 dex_file,
527 (verified_method != nullptr)
528 ? dex_to_dex_compilation_level
529 : optimizer::DexToDexCompilationLevel::kDontDexToDexCompile);
530 }
531 } else if ((access_flags & kAccNative) != 0) {
532 // Are we extracting only and have support for generic JNI down calls?
533 if (!driver->GetCompilerOptions().IsJniCompilationEnabled() &&
534 InstructionSetHasGenericJniStub(driver->GetInstructionSet())) {
535 // Leaving this empty will trigger the generic JNI version
536 } else {
537 // Look-up the ArtMethod associated with this code_item (if any)
538 // -- It is later used to lookup any [optimization] annotations for this method.
539 ScopedObjectAccess soa(self);
540
541 // TODO: Lookup annotation from DexFile directly without resolving method.
542 ArtMethod* method =
543 Runtime::Current()->GetClassLinker()->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
544 dex_file,
545 method_idx,
546 dex_cache,
547 class_loader,
548 /* referrer */ nullptr,
549 invoke_type);
550
551 // Query any JNI optimization annotations such as @FastNative or @CriticalNative.
552 Compiler::JniOptimizationFlags optimization_flags = Compiler::kNone;
553 if (UNLIKELY(method == nullptr)) {
554 // Failed method resolutions happen very rarely, e.g. ancestor class cannot be resolved.
555 DCHECK(self->IsExceptionPending());
556 self->ClearException();
557 } else if (method->IsAnnotatedWithFastNative()) {
558 // TODO: Will no longer need this CHECK once we have verifier checking this.
559 CHECK(!method->IsAnnotatedWithCriticalNative());
560 optimization_flags = Compiler::kFastNative;
561 } else if (method->IsAnnotatedWithCriticalNative()) {
562 // TODO: Will no longer need this CHECK once we have verifier checking this.
563 CHECK(!method->IsAnnotatedWithFastNative());
564 optimization_flags = Compiler::kCriticalNative;
565 }
566
567 compiled_method = driver->GetCompiler()->JniCompile(access_flags,
568 method_idx,
569 dex_file,
570 optimization_flags);
571 CHECK(compiled_method != nullptr);
572 }
573 } else if ((access_flags & kAccAbstract) != 0) {
574 // Abstract methods don't have code.
575 } else {
576 VerificationResults* results = driver->GetVerificationResults();
577 DCHECK(results != nullptr);
578 const VerifiedMethod* verified_method = results->GetVerifiedMethod(method_ref);
579 bool compile = compilation_enabled &&
580 // Basic checks, e.g., not <clinit>.
581 results->IsCandidateForCompilation(method_ref, access_flags) &&
582 // Did not fail to create VerifiedMethod metadata.
583 verified_method != nullptr &&
584 // Do not have failures that should punt to the interpreter.
585 !verified_method->HasRuntimeThrow() &&
586 (verified_method->GetEncounteredVerificationFailures() &
587 (verifier::VERIFY_ERROR_FORCE_INTERPRETER | verifier::VERIFY_ERROR_LOCKING)) == 0 &&
588 // Is eligable for compilation by methods-to-compile filter.
589 driver->IsMethodToCompile(method_ref) &&
590 driver->ShouldCompileBasedOnProfile(method_ref);
591
592 if (compile) {
593 // NOTE: if compiler declines to compile this method, it will return null.
594 compiled_method = driver->GetCompiler()->Compile(code_item,
595 access_flags,
596 invoke_type,
597 class_def_idx,
598 method_idx,
599 class_loader,
600 dex_file,
601 dex_cache);
602 }
603 if (compiled_method == nullptr &&
604 dex_to_dex_compilation_level != optimizer::DexToDexCompilationLevel::kDontDexToDexCompile) {
605 DCHECK(!Runtime::Current()->UseJitCompilation());
606 // TODO: add a command-line option to disable DEX-to-DEX compilation ?
607 driver->MarkForDexToDexCompilation(self, method_ref);
608 }
609 }
610 if (kTimeCompileMethod) {
611 uint64_t duration_ns = NanoTime() - start_ns;
612 if (duration_ns > MsToNs(driver->GetCompiler()->GetMaximumCompilationTimeBeforeWarning())) {
613 LOG(WARNING) << "Compilation of " << dex_file.PrettyMethod(method_idx)
614 << " took " << PrettyDuration(duration_ns);
615 }
616 }
617
618 if (compiled_method != nullptr) {
619 // Count non-relative linker patches.
620 size_t non_relative_linker_patch_count = 0u;
621 for (const LinkerPatch& patch : compiled_method->GetPatches()) {
622 if (!patch.IsPcRelative()) {
623 ++non_relative_linker_patch_count;
624 }
625 }
626 bool compile_pic = driver->GetCompilerOptions().GetCompilePic(); // Off by default
627 // When compiling with PIC, there should be zero non-relative linker patches
628 CHECK(!compile_pic || non_relative_linker_patch_count == 0u);
629
630 driver->AddCompiledMethod(method_ref, compiled_method, non_relative_linker_patch_count);
631 }
632
633 if (self->IsExceptionPending()) {
634 ScopedObjectAccess soa(self);
635 LOG(FATAL) << "Unexpected exception compiling: " << dex_file.PrettyMethod(method_idx) << "\n"
636 << self->GetException()->Dump();
637 }
638 }
639
CompileOne(Thread * self,ArtMethod * method,TimingLogger * timings)640 void CompilerDriver::CompileOne(Thread* self, ArtMethod* method, TimingLogger* timings) {
641 DCHECK(!Runtime::Current()->IsStarted());
642 jobject jclass_loader;
643 const DexFile* dex_file;
644 uint16_t class_def_idx;
645 uint32_t method_idx = method->GetDexMethodIndex();
646 uint32_t access_flags = method->GetAccessFlags();
647 InvokeType invoke_type = method->GetInvokeType();
648 StackHandleScope<2> hs(self);
649 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
650 Handle<mirror::ClassLoader> class_loader(
651 hs.NewHandle(method->GetDeclaringClass()->GetClassLoader()));
652 {
653 ScopedObjectAccessUnchecked soa(self);
654 ScopedLocalRef<jobject> local_class_loader(
655 soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get()));
656 jclass_loader = soa.Env()->NewGlobalRef(local_class_loader.get());
657 // Find the dex_file
658 dex_file = method->GetDexFile();
659 class_def_idx = method->GetClassDefIndex();
660 }
661 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
662
663 // Go to native so that we don't block GC during compilation.
664 ScopedThreadSuspension sts(self, kNative);
665
666 std::vector<const DexFile*> dex_files;
667 dex_files.push_back(dex_file);
668
669 InitializeThreadPools();
670
671 PreCompile(jclass_loader, dex_files, timings);
672
673 // Can we run DEX-to-DEX compiler on this class ?
674 optimizer::DexToDexCompilationLevel dex_to_dex_compilation_level =
675 GetDexToDexCompilationLevel(self,
676 *this,
677 jclass_loader,
678 *dex_file,
679 dex_file->GetClassDef(class_def_idx));
680
681 DCHECK(current_dex_to_dex_methods_ == nullptr);
682 CompileMethod(self,
683 this,
684 code_item,
685 access_flags,
686 invoke_type,
687 class_def_idx,
688 method_idx,
689 class_loader,
690 *dex_file,
691 dex_to_dex_compilation_level,
692 true,
693 dex_cache);
694
695 ArrayRef<DexFileMethodSet> dex_to_dex_references;
696 {
697 // From this point on, we shall not modify dex_to_dex_references_, so
698 // just grab a reference to it that we use without holding the mutex.
699 MutexLock lock(Thread::Current(), dex_to_dex_references_lock_);
700 dex_to_dex_references = ArrayRef<DexFileMethodSet>(dex_to_dex_references_);
701 }
702 if (!dex_to_dex_references.empty()) {
703 DCHECK_EQ(dex_to_dex_references.size(), 1u);
704 DCHECK(&dex_to_dex_references[0].GetDexFile() == dex_file);
705 current_dex_to_dex_methods_ = &dex_to_dex_references.front().GetMethodIndexes();
706 DCHECK(current_dex_to_dex_methods_->IsBitSet(method_idx));
707 DCHECK_EQ(current_dex_to_dex_methods_->NumSetBits(), 1u);
708 CompileMethod(self,
709 this,
710 code_item,
711 access_flags,
712 invoke_type,
713 class_def_idx,
714 method_idx,
715 class_loader,
716 *dex_file,
717 dex_to_dex_compilation_level,
718 true,
719 dex_cache);
720 current_dex_to_dex_methods_ = nullptr;
721 }
722
723 FreeThreadPools();
724
725 self->GetJniEnv()->DeleteGlobalRef(jclass_loader);
726 }
727
Resolve(jobject class_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)728 void CompilerDriver::Resolve(jobject class_loader,
729 const std::vector<const DexFile*>& dex_files,
730 TimingLogger* timings) {
731 // Resolution allocates classes and needs to run single-threaded to be deterministic.
732 bool force_determinism = GetCompilerOptions().IsForceDeterminism();
733 ThreadPool* resolve_thread_pool = force_determinism
734 ? single_thread_pool_.get()
735 : parallel_thread_pool_.get();
736 size_t resolve_thread_count = force_determinism ? 1U : parallel_thread_count_;
737
738 for (size_t i = 0; i != dex_files.size(); ++i) {
739 const DexFile* dex_file = dex_files[i];
740 CHECK(dex_file != nullptr);
741 ResolveDexFile(class_loader,
742 *dex_file,
743 dex_files,
744 resolve_thread_pool,
745 resolve_thread_count,
746 timings);
747 }
748 }
749
750 // Resolve const-strings in the code. Done to have deterministic allocation behavior. Right now
751 // this is single-threaded for simplicity.
752 // TODO: Collect the relevant string indices in parallel, then allocate them sequentially in a
753 // stable order.
754
ResolveConstStrings(Handle<mirror::DexCache> dex_cache,const DexFile & dex_file,const DexFile::CodeItem * code_item)755 static void ResolveConstStrings(Handle<mirror::DexCache> dex_cache,
756 const DexFile& dex_file,
757 const DexFile::CodeItem* code_item)
758 REQUIRES_SHARED(Locks::mutator_lock_) {
759 if (code_item == nullptr) {
760 // Abstract or native method.
761 return;
762 }
763
764 const uint16_t* code_ptr = code_item->insns_;
765 const uint16_t* code_end = code_item->insns_ + code_item->insns_size_in_code_units_;
766 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
767
768 while (code_ptr < code_end) {
769 const Instruction* inst = Instruction::At(code_ptr);
770 switch (inst->Opcode()) {
771 case Instruction::CONST_STRING:
772 case Instruction::CONST_STRING_JUMBO: {
773 dex::StringIndex string_index((inst->Opcode() == Instruction::CONST_STRING)
774 ? inst->VRegB_21c()
775 : inst->VRegB_31c());
776 mirror::String* string = class_linker->ResolveString(dex_file, string_index, dex_cache);
777 CHECK(string != nullptr) << "Could not allocate a string when forcing determinism";
778 break;
779 }
780
781 default:
782 break;
783 }
784
785 code_ptr += inst->SizeInCodeUnits();
786 }
787 }
788
ResolveConstStrings(CompilerDriver * driver,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)789 static void ResolveConstStrings(CompilerDriver* driver,
790 const std::vector<const DexFile*>& dex_files,
791 TimingLogger* timings) {
792 ScopedObjectAccess soa(Thread::Current());
793 StackHandleScope<1> hs(soa.Self());
794 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
795 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
796
797 for (const DexFile* dex_file : dex_files) {
798 dex_cache.Assign(class_linker->FindDexCache(soa.Self(), *dex_file));
799 TimingLogger::ScopedTiming t("Resolve const-string Strings", timings);
800
801 size_t class_def_count = dex_file->NumClassDefs();
802 for (size_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
803 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
804
805 const uint8_t* class_data = dex_file->GetClassData(class_def);
806 if (class_data == nullptr) {
807 // empty class, probably a marker interface
808 continue;
809 }
810
811 ClassDataItemIterator it(*dex_file, class_data);
812 it.SkipAllFields();
813
814 bool compilation_enabled = driver->IsClassToCompile(
815 dex_file->StringByTypeIdx(class_def.class_idx_));
816 if (!compilation_enabled) {
817 // Compilation is skipped, do not resolve const-string in code of this class.
818 // TODO: Make sure that inlining honors this.
819 continue;
820 }
821
822 // Direct methods.
823 int64_t previous_direct_method_idx = -1;
824 while (it.HasNextDirectMethod()) {
825 uint32_t method_idx = it.GetMemberIndex();
826 if (method_idx == previous_direct_method_idx) {
827 // smali can create dex files with two encoded_methods sharing the same method_idx
828 // http://code.google.com/p/smali/issues/detail?id=119
829 it.Next();
830 continue;
831 }
832 previous_direct_method_idx = method_idx;
833 ResolveConstStrings(dex_cache, *dex_file, it.GetMethodCodeItem());
834 it.Next();
835 }
836 // Virtual methods.
837 int64_t previous_virtual_method_idx = -1;
838 while (it.HasNextVirtualMethod()) {
839 uint32_t method_idx = it.GetMemberIndex();
840 if (method_idx == previous_virtual_method_idx) {
841 // smali can create dex files with two encoded_methods sharing the same method_idx
842 // http://code.google.com/p/smali/issues/detail?id=119
843 it.Next();
844 continue;
845 }
846 previous_virtual_method_idx = method_idx;
847 ResolveConstStrings(dex_cache, *dex_file, it.GetMethodCodeItem());
848 it.Next();
849 }
850 DCHECK(!it.HasNext());
851 }
852 }
853 }
854
CheckThreadPools()855 inline void CompilerDriver::CheckThreadPools() {
856 DCHECK(parallel_thread_pool_ != nullptr);
857 DCHECK(single_thread_pool_ != nullptr);
858 }
859
EnsureVerifiedOrVerifyAtRuntime(jobject jclass_loader,const std::vector<const DexFile * > & dex_files)860 static void EnsureVerifiedOrVerifyAtRuntime(jobject jclass_loader,
861 const std::vector<const DexFile*>& dex_files) {
862 ScopedObjectAccess soa(Thread::Current());
863 StackHandleScope<2> hs(soa.Self());
864 Handle<mirror::ClassLoader> class_loader(
865 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
866 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr));
867 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
868
869 for (const DexFile* dex_file : dex_files) {
870 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
871 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
872 const char* descriptor = dex_file->GetClassDescriptor(class_def);
873 cls.Assign(class_linker->FindClass(soa.Self(), descriptor, class_loader));
874 if (cls == nullptr) {
875 soa.Self()->ClearException();
876 } else if (&cls->GetDexFile() == dex_file) {
877 DCHECK(cls->IsErroneous() || cls->IsVerified() || cls->ShouldVerifyAtRuntime())
878 << cls->PrettyClass()
879 << " " << cls->GetStatus();
880 }
881 }
882 }
883 }
884
PreCompile(jobject class_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)885 void CompilerDriver::PreCompile(jobject class_loader,
886 const std::vector<const DexFile*>& dex_files,
887 TimingLogger* timings) {
888 CheckThreadPools();
889
890 LoadImageClasses(timings);
891 VLOG(compiler) << "LoadImageClasses: " << GetMemoryUsageString(false);
892
893 if (compiler_options_->IsAnyCompilationEnabled()) {
894 // Avoid adding the dex files in the case where we aren't going to add compiled methods.
895 // This reduces RAM usage for this case.
896 for (const DexFile* dex_file : dex_files) {
897 // Can be already inserted if the caller is CompileOne. This happens for gtests.
898 if (!compiled_methods_.HaveDexFile(dex_file)) {
899 compiled_methods_.AddDexFile(dex_file, dex_file->NumMethodIds());
900 }
901 }
902 // Resolve eagerly to prepare for compilation.
903 Resolve(class_loader, dex_files, timings);
904 VLOG(compiler) << "Resolve: " << GetMemoryUsageString(false);
905 }
906
907 if (compiler_options_->AssumeClassesAreVerified()) {
908 VLOG(compiler) << "Verify none mode specified, skipping verification.";
909 SetVerified(class_loader, dex_files, timings);
910 }
911
912 if (!compiler_options_->IsVerificationEnabled()) {
913 return;
914 }
915
916 if (GetCompilerOptions().IsForceDeterminism() && GetCompilerOptions().IsBootImage()) {
917 // Resolve strings from const-string. Do this now to have a deterministic image.
918 ResolveConstStrings(this, dex_files, timings);
919 VLOG(compiler) << "Resolve const-strings: " << GetMemoryUsageString(false);
920 }
921
922 Verify(class_loader, dex_files, timings);
923 VLOG(compiler) << "Verify: " << GetMemoryUsageString(false);
924
925 if (had_hard_verifier_failure_ && GetCompilerOptions().AbortOnHardVerifierFailure()) {
926 LOG(FATAL) << "Had a hard failure verifying all classes, and was asked to abort in such "
927 << "situations. Please check the log.";
928 }
929
930 if (compiler_options_->IsAnyCompilationEnabled()) {
931 if (kIsDebugBuild) {
932 EnsureVerifiedOrVerifyAtRuntime(class_loader, dex_files);
933 }
934 InitializeClasses(class_loader, dex_files, timings);
935 VLOG(compiler) << "InitializeClasses: " << GetMemoryUsageString(false);
936 }
937
938 UpdateImageClasses(timings);
939 VLOG(compiler) << "UpdateImageClasses: " << GetMemoryUsageString(false);
940 }
941
IsImageClass(const char * descriptor) const942 bool CompilerDriver::IsImageClass(const char* descriptor) const {
943 if (image_classes_ != nullptr) {
944 // If we have a set of image classes, use those.
945 return image_classes_->find(descriptor) != image_classes_->end();
946 }
947 // No set of image classes, assume we include all the classes.
948 // NOTE: Currently only reachable from InitImageMethodVisitor for the app image case.
949 return !GetCompilerOptions().IsBootImage();
950 }
951
IsClassToCompile(const char * descriptor) const952 bool CompilerDriver::IsClassToCompile(const char* descriptor) const {
953 if (classes_to_compile_ == nullptr) {
954 return true;
955 }
956 return classes_to_compile_->find(descriptor) != classes_to_compile_->end();
957 }
958
IsMethodToCompile(const MethodReference & method_ref) const959 bool CompilerDriver::IsMethodToCompile(const MethodReference& method_ref) const {
960 if (methods_to_compile_ == nullptr) {
961 return true;
962 }
963
964 std::string tmp = method_ref.dex_file->PrettyMethod(method_ref.dex_method_index, true);
965 return methods_to_compile_->find(tmp.c_str()) != methods_to_compile_->end();
966 }
967
ShouldCompileBasedOnProfile(const MethodReference & method_ref) const968 bool CompilerDriver::ShouldCompileBasedOnProfile(const MethodReference& method_ref) const {
969 // Profile compilation info may be null if no profile is passed.
970 if (!CompilerFilter::DependsOnProfile(compiler_options_->GetCompilerFilter())) {
971 // Use the compiler filter instead of the presence of profile_compilation_info_ since
972 // we may want to have full speed compilation along with profile based layout optimizations.
973 return true;
974 }
975 // If we are using a profile filter but do not have a profile compilation info, compile nothing.
976 if (profile_compilation_info_ == nullptr) {
977 return false;
978 }
979 // Compile only hot methods, it is the profile saver's job to decide what startup methods to mark
980 // as hot.
981 bool result = profile_compilation_info_->GetMethodHotness(method_ref).IsHot();
982
983 if (kDebugProfileGuidedCompilation) {
984 LOG(INFO) << "[ProfileGuidedCompilation] "
985 << (result ? "Compiled" : "Skipped") << " method:"
986 << method_ref.dex_file->PrettyMethod(method_ref.dex_method_index, true);
987 }
988 return result;
989 }
990
991 class ResolveCatchBlockExceptionsClassVisitor : public ClassVisitor {
992 public:
ResolveCatchBlockExceptionsClassVisitor()993 ResolveCatchBlockExceptionsClassVisitor() : classes_() {}
994
operator ()(ObjPtr<mirror::Class> c)995 virtual bool operator()(ObjPtr<mirror::Class> c) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
996 classes_.push_back(c);
997 return true;
998 }
999
FindExceptionTypesToResolve(std::set<std::pair<dex::TypeIndex,const DexFile * >> * exceptions_to_resolve)1000 void FindExceptionTypesToResolve(
1001 std::set<std::pair<dex::TypeIndex, const DexFile*>>* exceptions_to_resolve)
1002 REQUIRES_SHARED(Locks::mutator_lock_) {
1003 const auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1004 for (ObjPtr<mirror::Class> klass : classes_) {
1005 for (ArtMethod& method : klass->GetMethods(pointer_size)) {
1006 FindExceptionTypesToResolveForMethod(&method, exceptions_to_resolve);
1007 }
1008 }
1009 }
1010
1011 private:
FindExceptionTypesToResolveForMethod(ArtMethod * method,std::set<std::pair<dex::TypeIndex,const DexFile * >> * exceptions_to_resolve)1012 void FindExceptionTypesToResolveForMethod(
1013 ArtMethod* method,
1014 std::set<std::pair<dex::TypeIndex, const DexFile*>>* exceptions_to_resolve)
1015 REQUIRES_SHARED(Locks::mutator_lock_) {
1016 const DexFile::CodeItem* code_item = method->GetCodeItem();
1017 if (code_item == nullptr) {
1018 return; // native or abstract method
1019 }
1020 if (code_item->tries_size_ == 0) {
1021 return; // nothing to process
1022 }
1023 const uint8_t* encoded_catch_handler_list = DexFile::GetCatchHandlerData(*code_item, 0);
1024 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
1025 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
1026 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
1027 bool has_catch_all = false;
1028 if (encoded_catch_handler_size <= 0) {
1029 encoded_catch_handler_size = -encoded_catch_handler_size;
1030 has_catch_all = true;
1031 }
1032 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
1033 dex::TypeIndex encoded_catch_handler_handlers_type_idx =
1034 dex::TypeIndex(DecodeUnsignedLeb128(&encoded_catch_handler_list));
1035 // Add to set of types to resolve if not already in the dex cache resolved types
1036 if (!method->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
1037 exceptions_to_resolve->emplace(encoded_catch_handler_handlers_type_idx,
1038 method->GetDexFile());
1039 }
1040 // ignore address associated with catch handler
1041 DecodeUnsignedLeb128(&encoded_catch_handler_list);
1042 }
1043 if (has_catch_all) {
1044 // ignore catch all address
1045 DecodeUnsignedLeb128(&encoded_catch_handler_list);
1046 }
1047 }
1048 }
1049
1050 std::vector<ObjPtr<mirror::Class>> classes_;
1051 };
1052
1053 class RecordImageClassesVisitor : public ClassVisitor {
1054 public:
RecordImageClassesVisitor(std::unordered_set<std::string> * image_classes)1055 explicit RecordImageClassesVisitor(std::unordered_set<std::string>* image_classes)
1056 : image_classes_(image_classes) {}
1057
operator ()(ObjPtr<mirror::Class> klass)1058 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
1059 std::string temp;
1060 image_classes_->insert(klass->GetDescriptor(&temp));
1061 return true;
1062 }
1063
1064 private:
1065 std::unordered_set<std::string>* const image_classes_;
1066 };
1067
1068 // Make a list of descriptors for classes to include in the image
LoadImageClasses(TimingLogger * timings)1069 void CompilerDriver::LoadImageClasses(TimingLogger* timings) {
1070 CHECK(timings != nullptr);
1071 if (!GetCompilerOptions().IsBootImage()) {
1072 return;
1073 }
1074
1075 TimingLogger::ScopedTiming t("LoadImageClasses", timings);
1076 // Make a first class to load all classes explicitly listed in the file
1077 Thread* self = Thread::Current();
1078 ScopedObjectAccess soa(self);
1079 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1080 CHECK(image_classes_.get() != nullptr);
1081 for (auto it = image_classes_->begin(), end = image_classes_->end(); it != end;) {
1082 const std::string& descriptor(*it);
1083 StackHandleScope<1> hs(self);
1084 Handle<mirror::Class> klass(
1085 hs.NewHandle(class_linker->FindSystemClass(self, descriptor.c_str())));
1086 if (klass == nullptr) {
1087 VLOG(compiler) << "Failed to find class " << descriptor;
1088 image_classes_->erase(it++);
1089 self->ClearException();
1090 } else {
1091 ++it;
1092 }
1093 }
1094
1095 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
1096 // exceptions are resolved by the verifier when there is a catch block in an interested method.
1097 // Do this here so that exception classes appear to have been specified image classes.
1098 std::set<std::pair<dex::TypeIndex, const DexFile*>> unresolved_exception_types;
1099 StackHandleScope<1> hs(self);
1100 Handle<mirror::Class> java_lang_Throwable(
1101 hs.NewHandle(class_linker->FindSystemClass(self, "Ljava/lang/Throwable;")));
1102 do {
1103 unresolved_exception_types.clear();
1104 {
1105 // Thread suspension is not allowed while ResolveCatchBlockExceptionsClassVisitor
1106 // is using a std::vector<ObjPtr<mirror::Class>>.
1107 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
1108 ResolveCatchBlockExceptionsClassVisitor visitor;
1109 class_linker->VisitClasses(&visitor);
1110 visitor.FindExceptionTypesToResolve(&unresolved_exception_types);
1111 }
1112 for (const auto& exception_type : unresolved_exception_types) {
1113 dex::TypeIndex exception_type_idx = exception_type.first;
1114 const DexFile* dex_file = exception_type.second;
1115 StackHandleScope<2> hs2(self);
1116 Handle<mirror::DexCache> dex_cache(hs2.NewHandle(class_linker->RegisterDexFile(*dex_file,
1117 nullptr)));
1118 Handle<mirror::Class> klass(hs2.NewHandle(
1119 (dex_cache != nullptr)
1120 ? class_linker->ResolveType(*dex_file,
1121 exception_type_idx,
1122 dex_cache,
1123 ScopedNullHandle<mirror::ClassLoader>())
1124 : nullptr));
1125 if (klass == nullptr) {
1126 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
1127 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
1128 LOG(FATAL) << "Failed to resolve class " << descriptor;
1129 }
1130 DCHECK(java_lang_Throwable->IsAssignableFrom(klass.Get()));
1131 }
1132 // Resolving exceptions may load classes that reference more exceptions, iterate until no
1133 // more are found
1134 } while (!unresolved_exception_types.empty());
1135
1136 // We walk the roots looking for classes so that we'll pick up the
1137 // above classes plus any classes them depend on such super
1138 // classes, interfaces, and the required ClassLinker roots.
1139 RecordImageClassesVisitor visitor(image_classes_.get());
1140 class_linker->VisitClasses(&visitor);
1141
1142 CHECK_NE(image_classes_->size(), 0U);
1143 }
1144
MaybeAddToImageClasses(Thread * self,ObjPtr<mirror::Class> klass,std::unordered_set<std::string> * image_classes)1145 static void MaybeAddToImageClasses(Thread* self,
1146 ObjPtr<mirror::Class> klass,
1147 std::unordered_set<std::string>* image_classes)
1148 REQUIRES_SHARED(Locks::mutator_lock_) {
1149 DCHECK_EQ(self, Thread::Current());
1150 StackHandleScope<1> hs(self);
1151 std::string temp;
1152 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1153 while (!klass->IsObjectClass()) {
1154 const char* descriptor = klass->GetDescriptor(&temp);
1155 std::pair<std::unordered_set<std::string>::iterator, bool> result =
1156 image_classes->insert(descriptor);
1157 if (!result.second) { // Previously inserted.
1158 break;
1159 }
1160 VLOG(compiler) << "Adding " << descriptor << " to image classes";
1161 for (size_t i = 0, num_interfaces = klass->NumDirectInterfaces(); i != num_interfaces; ++i) {
1162 ObjPtr<mirror::Class> interface = mirror::Class::GetDirectInterface(self, klass, i);
1163 DCHECK(interface != nullptr);
1164 MaybeAddToImageClasses(self, interface, image_classes);
1165 }
1166 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
1167 MaybeAddToImageClasses(self, m.GetDeclaringClass(), image_classes);
1168 }
1169 if (klass->IsArrayClass()) {
1170 MaybeAddToImageClasses(self, klass->GetComponentType(), image_classes);
1171 }
1172 klass.Assign(klass->GetSuperClass());
1173 }
1174 }
1175
1176 // Keeps all the data for the update together. Also doubles as the reference visitor.
1177 // Note: we can use object pointers because we suspend all threads.
1178 class ClinitImageUpdate {
1179 public:
Create(VariableSizedHandleScope & hs,std::unordered_set<std::string> * image_class_descriptors,Thread * self,ClassLinker * linker)1180 static ClinitImageUpdate* Create(VariableSizedHandleScope& hs,
1181 std::unordered_set<std::string>* image_class_descriptors,
1182 Thread* self,
1183 ClassLinker* linker) {
1184 std::unique_ptr<ClinitImageUpdate> res(new ClinitImageUpdate(hs,
1185 image_class_descriptors,
1186 self,
1187 linker));
1188 return res.release();
1189 }
1190
~ClinitImageUpdate()1191 ~ClinitImageUpdate() {
1192 // Allow others to suspend again.
1193 self_->EndAssertNoThreadSuspension(old_cause_);
1194 }
1195
1196 // Visitor for VisitReferences.
operator ()(ObjPtr<mirror::Object> object,MemberOffset field_offset,bool) const1197 void operator()(ObjPtr<mirror::Object> object,
1198 MemberOffset field_offset,
1199 bool /* is_static */) const
1200 REQUIRES_SHARED(Locks::mutator_lock_) {
1201 mirror::Object* ref = object->GetFieldObject<mirror::Object>(field_offset);
1202 if (ref != nullptr) {
1203 VisitClinitClassesObject(ref);
1204 }
1205 }
1206
1207 // java.lang.ref.Reference visitor for VisitReferences.
operator ()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED,ObjPtr<mirror::Reference> ref ATTRIBUTE_UNUSED) const1208 void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED,
1209 ObjPtr<mirror::Reference> ref ATTRIBUTE_UNUSED) const {}
1210
1211 // Ignore class native roots.
VisitRootIfNonNull(mirror::CompressedReference<mirror::Object> * root ATTRIBUTE_UNUSED) const1212 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
1213 const {}
VisitRoot(mirror::CompressedReference<mirror::Object> * root ATTRIBUTE_UNUSED) const1214 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
1215
Walk()1216 void Walk() REQUIRES_SHARED(Locks::mutator_lock_) {
1217 // Use the initial classes as roots for a search.
1218 for (Handle<mirror::Class> klass_root : image_classes_) {
1219 VisitClinitClassesObject(klass_root.Get());
1220 }
1221 Thread* self = Thread::Current();
1222 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
1223 for (Handle<mirror::Class> h_klass : to_insert_) {
1224 MaybeAddToImageClasses(self, h_klass.Get(), image_class_descriptors_);
1225 }
1226 }
1227
1228 private:
1229 class FindImageClassesVisitor : public ClassVisitor {
1230 public:
FindImageClassesVisitor(VariableSizedHandleScope & hs,ClinitImageUpdate * data)1231 explicit FindImageClassesVisitor(VariableSizedHandleScope& hs,
1232 ClinitImageUpdate* data)
1233 : data_(data),
1234 hs_(hs) {}
1235
operator ()(ObjPtr<mirror::Class> klass)1236 bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
1237 std::string temp;
1238 const char* name = klass->GetDescriptor(&temp);
1239 if (data_->image_class_descriptors_->find(name) != data_->image_class_descriptors_->end()) {
1240 data_->image_classes_.push_back(hs_.NewHandle(klass));
1241 } else {
1242 // Check whether it is initialized and has a clinit. They must be kept, too.
1243 if (klass->IsInitialized() && klass->FindClassInitializer(
1244 Runtime::Current()->GetClassLinker()->GetImagePointerSize()) != nullptr) {
1245 data_->image_classes_.push_back(hs_.NewHandle(klass));
1246 }
1247 }
1248 return true;
1249 }
1250
1251 private:
1252 ClinitImageUpdate* const data_;
1253 VariableSizedHandleScope& hs_;
1254 };
1255
ClinitImageUpdate(VariableSizedHandleScope & hs,std::unordered_set<std::string> * image_class_descriptors,Thread * self,ClassLinker * linker)1256 ClinitImageUpdate(VariableSizedHandleScope& hs,
1257 std::unordered_set<std::string>* image_class_descriptors,
1258 Thread* self,
1259 ClassLinker* linker) REQUIRES_SHARED(Locks::mutator_lock_)
1260 : hs_(hs),
1261 image_class_descriptors_(image_class_descriptors),
1262 self_(self) {
1263 CHECK(linker != nullptr);
1264 CHECK(image_class_descriptors != nullptr);
1265
1266 // Make sure nobody interferes with us.
1267 old_cause_ = self->StartAssertNoThreadSuspension("Boot image closure");
1268
1269 // Find all the already-marked classes.
1270 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1271 FindImageClassesVisitor visitor(hs_, this);
1272 linker->VisitClasses(&visitor);
1273 }
1274
VisitClinitClassesObject(mirror::Object * object) const1275 void VisitClinitClassesObject(mirror::Object* object) const
1276 REQUIRES_SHARED(Locks::mutator_lock_) {
1277 DCHECK(object != nullptr);
1278 if (marked_objects_.find(object) != marked_objects_.end()) {
1279 // Already processed.
1280 return;
1281 }
1282
1283 // Mark it.
1284 marked_objects_.insert(object);
1285
1286 if (object->IsClass()) {
1287 // Add to the TODO list since MaybeAddToImageClasses may cause thread suspension. Thread
1288 // suspensionb is not safe to do in VisitObjects or VisitReferences.
1289 to_insert_.push_back(hs_.NewHandle(object->AsClass()));
1290 } else {
1291 // Else visit the object's class.
1292 VisitClinitClassesObject(object->GetClass());
1293 }
1294
1295 // If it is not a DexCache, visit all references.
1296 if (!object->IsDexCache()) {
1297 object->VisitReferences(*this, *this);
1298 }
1299 }
1300
1301 VariableSizedHandleScope& hs_;
1302 mutable std::vector<Handle<mirror::Class>> to_insert_;
1303 mutable std::unordered_set<mirror::Object*> marked_objects_;
1304 std::unordered_set<std::string>* const image_class_descriptors_;
1305 std::vector<Handle<mirror::Class>> image_classes_;
1306 Thread* const self_;
1307 const char* old_cause_;
1308
1309 DISALLOW_COPY_AND_ASSIGN(ClinitImageUpdate);
1310 };
1311
UpdateImageClasses(TimingLogger * timings)1312 void CompilerDriver::UpdateImageClasses(TimingLogger* timings) {
1313 if (GetCompilerOptions().IsBootImage()) {
1314 TimingLogger::ScopedTiming t("UpdateImageClasses", timings);
1315
1316 Runtime* runtime = Runtime::Current();
1317
1318 // Suspend all threads.
1319 ScopedSuspendAll ssa(__FUNCTION__);
1320
1321 VariableSizedHandleScope hs(Thread::Current());
1322 std::string error_msg;
1323 std::unique_ptr<ClinitImageUpdate> update(ClinitImageUpdate::Create(hs,
1324 image_classes_.get(),
1325 Thread::Current(),
1326 runtime->GetClassLinker()));
1327
1328 // Do the marking.
1329 update->Walk();
1330 }
1331 }
1332
CanAssumeClassIsLoaded(mirror::Class * klass)1333 bool CompilerDriver::CanAssumeClassIsLoaded(mirror::Class* klass) {
1334 Runtime* runtime = Runtime::Current();
1335 if (!runtime->IsAotCompiler()) {
1336 DCHECK(runtime->UseJitCompilation());
1337 // Having the klass reference here implies that the klass is already loaded.
1338 return true;
1339 }
1340 if (!GetCompilerOptions().IsBootImage()) {
1341 // Assume loaded only if klass is in the boot image. App classes cannot be assumed
1342 // loaded because we don't even know what class loader will be used to load them.
1343 bool class_in_image = runtime->GetHeap()->FindSpaceFromObject(klass, false)->IsImageSpace();
1344 return class_in_image;
1345 }
1346 std::string temp;
1347 const char* descriptor = klass->GetDescriptor(&temp);
1348 return IsImageClass(descriptor);
1349 }
1350
MarkForDexToDexCompilation(Thread * self,const MethodReference & method_ref)1351 void CompilerDriver::MarkForDexToDexCompilation(Thread* self, const MethodReference& method_ref) {
1352 MutexLock lock(self, dex_to_dex_references_lock_);
1353 // Since we're compiling one dex file at a time, we need to look for the
1354 // current dex file entry only at the end of dex_to_dex_references_.
1355 if (dex_to_dex_references_.empty() ||
1356 &dex_to_dex_references_.back().GetDexFile() != method_ref.dex_file) {
1357 dex_to_dex_references_.emplace_back(*method_ref.dex_file);
1358 }
1359 dex_to_dex_references_.back().GetMethodIndexes().SetBit(method_ref.dex_method_index);
1360 }
1361
CanAccessTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class,ObjPtr<mirror::Class> resolved_class)1362 bool CompilerDriver::CanAccessTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class,
1363 ObjPtr<mirror::Class> resolved_class) {
1364 if (resolved_class == nullptr) {
1365 stats_->TypeNeedsAccessCheck();
1366 return false; // Unknown class needs access checks.
1367 }
1368 bool is_accessible = resolved_class->IsPublic(); // Public classes are always accessible.
1369 if (!is_accessible) {
1370 if (referrer_class == nullptr) {
1371 stats_->TypeNeedsAccessCheck();
1372 return false; // Incomplete referrer knowledge needs access check.
1373 }
1374 // Perform access check, will return true if access is ok or false if we're going to have to
1375 // check this at runtime (for example for class loaders).
1376 is_accessible = referrer_class->CanAccess(resolved_class);
1377 }
1378 if (is_accessible) {
1379 stats_->TypeDoesntNeedAccessCheck();
1380 } else {
1381 stats_->TypeNeedsAccessCheck();
1382 }
1383 return is_accessible;
1384 }
1385
CanAccessInstantiableTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class,ObjPtr<mirror::Class> resolved_class,bool * finalizable)1386 bool CompilerDriver::CanAccessInstantiableTypeWithoutChecks(ObjPtr<mirror::Class> referrer_class,
1387 ObjPtr<mirror::Class> resolved_class,
1388 bool* finalizable) {
1389 if (resolved_class == nullptr) {
1390 stats_->TypeNeedsAccessCheck();
1391 // Be conservative.
1392 *finalizable = true;
1393 return false; // Unknown class needs access checks.
1394 }
1395 *finalizable = resolved_class->IsFinalizable();
1396 bool is_accessible = resolved_class->IsPublic(); // Public classes are always accessible.
1397 if (!is_accessible) {
1398 if (referrer_class == nullptr) {
1399 stats_->TypeNeedsAccessCheck();
1400 return false; // Incomplete referrer knowledge needs access check.
1401 }
1402 // Perform access and instantiable checks, will return true if access is ok or false if we're
1403 // going to have to check this at runtime (for example for class loaders).
1404 is_accessible = referrer_class->CanAccess(resolved_class);
1405 }
1406 bool result = is_accessible && resolved_class->IsInstantiable();
1407 if (result) {
1408 stats_->TypeDoesntNeedAccessCheck();
1409 } else {
1410 stats_->TypeNeedsAccessCheck();
1411 }
1412 return result;
1413 }
1414
ProcessedInstanceField(bool resolved)1415 void CompilerDriver::ProcessedInstanceField(bool resolved) {
1416 if (!resolved) {
1417 stats_->UnresolvedInstanceField();
1418 } else {
1419 stats_->ResolvedInstanceField();
1420 }
1421 }
1422
ProcessedStaticField(bool resolved,bool local)1423 void CompilerDriver::ProcessedStaticField(bool resolved, bool local) {
1424 if (!resolved) {
1425 stats_->UnresolvedStaticField();
1426 } else if (local) {
1427 stats_->ResolvedLocalStaticField();
1428 } else {
1429 stats_->ResolvedStaticField();
1430 }
1431 }
1432
ComputeInstanceFieldInfo(uint32_t field_idx,const DexCompilationUnit * mUnit,bool is_put,const ScopedObjectAccess & soa)1433 ArtField* CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx,
1434 const DexCompilationUnit* mUnit, bool is_put,
1435 const ScopedObjectAccess& soa) {
1436 // Try to resolve the field and compiling method's class.
1437 ArtField* resolved_field;
1438 mirror::Class* referrer_class;
1439 Handle<mirror::DexCache> dex_cache(mUnit->GetDexCache());
1440 {
1441 Handle<mirror::ClassLoader> class_loader_handle = mUnit->GetClassLoader();
1442 resolved_field = ResolveField(soa, dex_cache, class_loader_handle, mUnit, field_idx, false);
1443 referrer_class = resolved_field != nullptr
1444 ? ResolveCompilingMethodsClass(soa, dex_cache, class_loader_handle, mUnit) : nullptr;
1445 }
1446 bool can_link = false;
1447 if (resolved_field != nullptr && referrer_class != nullptr) {
1448 std::pair<bool, bool> fast_path = IsFastInstanceField(
1449 dex_cache.Get(), referrer_class, resolved_field, field_idx);
1450 can_link = is_put ? fast_path.second : fast_path.first;
1451 }
1452 ProcessedInstanceField(can_link);
1453 return can_link ? resolved_field : nullptr;
1454 }
1455
ComputeInstanceFieldInfo(uint32_t field_idx,const DexCompilationUnit * mUnit,bool is_put,MemberOffset * field_offset,bool * is_volatile)1456 bool CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
1457 bool is_put, MemberOffset* field_offset,
1458 bool* is_volatile) {
1459 ScopedObjectAccess soa(Thread::Current());
1460 ArtField* resolved_field = ComputeInstanceFieldInfo(field_idx, mUnit, is_put, soa);
1461
1462 if (resolved_field == nullptr) {
1463 // Conservative defaults.
1464 *is_volatile = true;
1465 *field_offset = MemberOffset(static_cast<size_t>(-1));
1466 return false;
1467 } else {
1468 *is_volatile = resolved_field->IsVolatile();
1469 *field_offset = resolved_field->GetOffset();
1470 return true;
1471 }
1472 }
1473
GetVerifiedMethod(const DexFile * dex_file,uint32_t method_idx) const1474 const VerifiedMethod* CompilerDriver::GetVerifiedMethod(const DexFile* dex_file,
1475 uint32_t method_idx) const {
1476 MethodReference ref(dex_file, method_idx);
1477 return verification_results_->GetVerifiedMethod(ref);
1478 }
1479
IsSafeCast(const DexCompilationUnit * mUnit,uint32_t dex_pc)1480 bool CompilerDriver::IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc) {
1481 if (!compiler_options_->IsVerificationEnabled()) {
1482 // If we didn't verify, every cast has to be treated as non-safe.
1483 return false;
1484 }
1485 DCHECK(mUnit->GetVerifiedMethod() != nullptr);
1486 bool result = mUnit->GetVerifiedMethod()->IsSafeCast(dex_pc);
1487 if (result) {
1488 stats_->SafeCast();
1489 } else {
1490 stats_->NotASafeCast();
1491 }
1492 return result;
1493 }
1494
1495 class CompilationVisitor {
1496 public:
~CompilationVisitor()1497 virtual ~CompilationVisitor() {}
1498 virtual void Visit(size_t index) = 0;
1499 };
1500
1501 class ParallelCompilationManager {
1502 public:
ParallelCompilationManager(ClassLinker * class_linker,jobject class_loader,CompilerDriver * compiler,const DexFile * dex_file,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool)1503 ParallelCompilationManager(ClassLinker* class_linker,
1504 jobject class_loader,
1505 CompilerDriver* compiler,
1506 const DexFile* dex_file,
1507 const std::vector<const DexFile*>& dex_files,
1508 ThreadPool* thread_pool)
1509 : index_(0),
1510 class_linker_(class_linker),
1511 class_loader_(class_loader),
1512 compiler_(compiler),
1513 dex_file_(dex_file),
1514 dex_files_(dex_files),
1515 thread_pool_(thread_pool) {}
1516
GetClassLinker() const1517 ClassLinker* GetClassLinker() const {
1518 CHECK(class_linker_ != nullptr);
1519 return class_linker_;
1520 }
1521
GetClassLoader() const1522 jobject GetClassLoader() const {
1523 return class_loader_;
1524 }
1525
GetCompiler() const1526 CompilerDriver* GetCompiler() const {
1527 CHECK(compiler_ != nullptr);
1528 return compiler_;
1529 }
1530
GetDexFile() const1531 const DexFile* GetDexFile() const {
1532 CHECK(dex_file_ != nullptr);
1533 return dex_file_;
1534 }
1535
GetDexFiles() const1536 const std::vector<const DexFile*>& GetDexFiles() const {
1537 return dex_files_;
1538 }
1539
ForAll(size_t begin,size_t end,CompilationVisitor * visitor,size_t work_units)1540 void ForAll(size_t begin, size_t end, CompilationVisitor* visitor, size_t work_units)
1541 REQUIRES(!*Locks::mutator_lock_) {
1542 Thread* self = Thread::Current();
1543 self->AssertNoPendingException();
1544 CHECK_GT(work_units, 0U);
1545
1546 index_.StoreRelaxed(begin);
1547 for (size_t i = 0; i < work_units; ++i) {
1548 thread_pool_->AddTask(self, new ForAllClosure(this, end, visitor));
1549 }
1550 thread_pool_->StartWorkers(self);
1551
1552 // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker
1553 // thread destructor's called below perform join).
1554 CHECK_NE(self->GetState(), kRunnable);
1555
1556 // Wait for all the worker threads to finish.
1557 thread_pool_->Wait(self, true, false);
1558
1559 // And stop the workers accepting jobs.
1560 thread_pool_->StopWorkers(self);
1561 }
1562
NextIndex()1563 size_t NextIndex() {
1564 return index_.FetchAndAddSequentiallyConsistent(1);
1565 }
1566
1567 private:
1568 class ForAllClosure : public Task {
1569 public:
ForAllClosure(ParallelCompilationManager * manager,size_t end,CompilationVisitor * visitor)1570 ForAllClosure(ParallelCompilationManager* manager, size_t end, CompilationVisitor* visitor)
1571 : manager_(manager),
1572 end_(end),
1573 visitor_(visitor) {}
1574
Run(Thread * self)1575 virtual void Run(Thread* self) {
1576 while (true) {
1577 const size_t index = manager_->NextIndex();
1578 if (UNLIKELY(index >= end_)) {
1579 break;
1580 }
1581 visitor_->Visit(index);
1582 self->AssertNoPendingException();
1583 }
1584 }
1585
Finalize()1586 virtual void Finalize() {
1587 delete this;
1588 }
1589
1590 private:
1591 ParallelCompilationManager* const manager_;
1592 const size_t end_;
1593 CompilationVisitor* const visitor_;
1594 };
1595
1596 AtomicInteger index_;
1597 ClassLinker* const class_linker_;
1598 const jobject class_loader_;
1599 CompilerDriver* const compiler_;
1600 const DexFile* const dex_file_;
1601 const std::vector<const DexFile*>& dex_files_;
1602 ThreadPool* const thread_pool_;
1603
1604 DISALLOW_COPY_AND_ASSIGN(ParallelCompilationManager);
1605 };
1606
1607 // A fast version of SkipClass above if the class pointer is available
1608 // that avoids the expensive FindInClassPath search.
SkipClass(jobject class_loader,const DexFile & dex_file,mirror::Class * klass)1609 static bool SkipClass(jobject class_loader, const DexFile& dex_file, mirror::Class* klass)
1610 REQUIRES_SHARED(Locks::mutator_lock_) {
1611 DCHECK(klass != nullptr);
1612 const DexFile& original_dex_file = *klass->GetDexCache()->GetDexFile();
1613 if (&dex_file != &original_dex_file) {
1614 if (class_loader == nullptr) {
1615 LOG(WARNING) << "Skipping class " << klass->PrettyDescriptor() << " from "
1616 << dex_file.GetLocation() << " previously found in "
1617 << original_dex_file.GetLocation();
1618 }
1619 return true;
1620 }
1621 return false;
1622 }
1623
CheckAndClearResolveException(Thread * self)1624 static void CheckAndClearResolveException(Thread* self)
1625 REQUIRES_SHARED(Locks::mutator_lock_) {
1626 CHECK(self->IsExceptionPending());
1627 mirror::Throwable* exception = self->GetException();
1628 std::string temp;
1629 const char* descriptor = exception->GetClass()->GetDescriptor(&temp);
1630 const char* expected_exceptions[] = {
1631 "Ljava/lang/IllegalAccessError;",
1632 "Ljava/lang/IncompatibleClassChangeError;",
1633 "Ljava/lang/InstantiationError;",
1634 "Ljava/lang/LinkageError;",
1635 "Ljava/lang/NoClassDefFoundError;",
1636 "Ljava/lang/NoSuchFieldError;",
1637 "Ljava/lang/NoSuchMethodError;"
1638 };
1639 bool found = false;
1640 for (size_t i = 0; (found == false) && (i < arraysize(expected_exceptions)); ++i) {
1641 if (strcmp(descriptor, expected_exceptions[i]) == 0) {
1642 found = true;
1643 }
1644 }
1645 if (!found) {
1646 LOG(FATAL) << "Unexpected exception " << exception->Dump();
1647 }
1648 self->ClearException();
1649 }
1650
RequiresConstructorBarrier(const DexFile & dex_file,uint16_t class_def_idx) const1651 bool CompilerDriver::RequiresConstructorBarrier(const DexFile& dex_file,
1652 uint16_t class_def_idx) const {
1653 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx);
1654 const uint8_t* class_data = dex_file.GetClassData(class_def);
1655 if (class_data == nullptr) {
1656 // Empty class such as a marker interface.
1657 return false;
1658 }
1659 ClassDataItemIterator it(dex_file, class_data);
1660 it.SkipStaticFields();
1661 // We require a constructor barrier if there are final instance fields.
1662 while (it.HasNextInstanceField()) {
1663 if (it.MemberIsFinal()) {
1664 return true;
1665 }
1666 it.Next();
1667 }
1668 return false;
1669 }
1670
1671 class ResolveClassFieldsAndMethodsVisitor : public CompilationVisitor {
1672 public:
ResolveClassFieldsAndMethodsVisitor(const ParallelCompilationManager * manager)1673 explicit ResolveClassFieldsAndMethodsVisitor(const ParallelCompilationManager* manager)
1674 : manager_(manager) {}
1675
Visit(size_t class_def_index)1676 void Visit(size_t class_def_index) OVERRIDE REQUIRES(!Locks::mutator_lock_) {
1677 ATRACE_CALL();
1678 Thread* const self = Thread::Current();
1679 jobject jclass_loader = manager_->GetClassLoader();
1680 const DexFile& dex_file = *manager_->GetDexFile();
1681 ClassLinker* class_linker = manager_->GetClassLinker();
1682
1683 // If an instance field is final then we need to have a barrier on the return, static final
1684 // fields are assigned within the lock held for class initialization. Conservatively assume
1685 // constructor barriers are always required.
1686 bool requires_constructor_barrier = true;
1687
1688 // Method and Field are the worst. We can't resolve without either
1689 // context from the code use (to disambiguate virtual vs direct
1690 // method and instance vs static field) or from class
1691 // definitions. While the compiler will resolve what it can as it
1692 // needs it, here we try to resolve fields and methods used in class
1693 // definitions, since many of them many never be referenced by
1694 // generated code.
1695 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1696 ScopedObjectAccess soa(self);
1697 StackHandleScope<2> hs(soa.Self());
1698 Handle<mirror::ClassLoader> class_loader(
1699 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
1700 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(
1701 soa.Self(), dex_file)));
1702 // Resolve the class.
1703 mirror::Class* klass = class_linker->ResolveType(dex_file, class_def.class_idx_, dex_cache,
1704 class_loader);
1705 bool resolve_fields_and_methods;
1706 if (klass == nullptr) {
1707 // Class couldn't be resolved, for example, super-class is in a different dex file. Don't
1708 // attempt to resolve methods and fields when there is no declaring class.
1709 CheckAndClearResolveException(soa.Self());
1710 resolve_fields_and_methods = false;
1711 } else {
1712 // We successfully resolved a class, should we skip it?
1713 if (SkipClass(jclass_loader, dex_file, klass)) {
1714 return;
1715 }
1716 // We want to resolve the methods and fields eagerly.
1717 resolve_fields_and_methods = true;
1718 }
1719 // Note the class_data pointer advances through the headers,
1720 // static fields, instance fields, direct methods, and virtual
1721 // methods.
1722 const uint8_t* class_data = dex_file.GetClassData(class_def);
1723 if (class_data == nullptr) {
1724 // Empty class such as a marker interface.
1725 requires_constructor_barrier = false;
1726 } else {
1727 ClassDataItemIterator it(dex_file, class_data);
1728 while (it.HasNextStaticField()) {
1729 if (resolve_fields_and_methods) {
1730 ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1731 dex_cache, class_loader, true);
1732 if (field == nullptr) {
1733 CheckAndClearResolveException(soa.Self());
1734 }
1735 }
1736 it.Next();
1737 }
1738 // We require a constructor barrier if there are final instance fields.
1739 requires_constructor_barrier = false;
1740 while (it.HasNextInstanceField()) {
1741 if (it.MemberIsFinal()) {
1742 requires_constructor_barrier = true;
1743 }
1744 if (resolve_fields_and_methods) {
1745 ArtField* field = class_linker->ResolveField(dex_file, it.GetMemberIndex(),
1746 dex_cache, class_loader, false);
1747 if (field == nullptr) {
1748 CheckAndClearResolveException(soa.Self());
1749 }
1750 }
1751 it.Next();
1752 }
1753 if (resolve_fields_and_methods) {
1754 while (it.HasNextDirectMethod()) {
1755 ArtMethod* method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
1756 dex_file, it.GetMemberIndex(), dex_cache, class_loader, nullptr,
1757 it.GetMethodInvokeType(class_def));
1758 if (method == nullptr) {
1759 CheckAndClearResolveException(soa.Self());
1760 }
1761 it.Next();
1762 }
1763 while (it.HasNextVirtualMethod()) {
1764 ArtMethod* method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
1765 dex_file, it.GetMemberIndex(), dex_cache, class_loader, nullptr,
1766 it.GetMethodInvokeType(class_def));
1767 if (method == nullptr) {
1768 CheckAndClearResolveException(soa.Self());
1769 }
1770 it.Next();
1771 }
1772 DCHECK(!it.HasNext());
1773 }
1774 }
1775 manager_->GetCompiler()->SetRequiresConstructorBarrier(self,
1776 &dex_file,
1777 class_def_index,
1778 requires_constructor_barrier);
1779 }
1780
1781 private:
1782 const ParallelCompilationManager* const manager_;
1783 };
1784
1785 class ResolveTypeVisitor : public CompilationVisitor {
1786 public:
ResolveTypeVisitor(const ParallelCompilationManager * manager)1787 explicit ResolveTypeVisitor(const ParallelCompilationManager* manager) : manager_(manager) {
1788 }
Visit(size_t type_idx)1789 void Visit(size_t type_idx) OVERRIDE REQUIRES(!Locks::mutator_lock_) {
1790 // Class derived values are more complicated, they require the linker and loader.
1791 ScopedObjectAccess soa(Thread::Current());
1792 ClassLinker* class_linker = manager_->GetClassLinker();
1793 const DexFile& dex_file = *manager_->GetDexFile();
1794 StackHandleScope<2> hs(soa.Self());
1795 Handle<mirror::ClassLoader> class_loader(
1796 hs.NewHandle(soa.Decode<mirror::ClassLoader>(manager_->GetClassLoader())));
1797 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->RegisterDexFile(
1798 dex_file,
1799 class_loader.Get())));
1800 ObjPtr<mirror::Class> klass = (dex_cache != nullptr)
1801 ? class_linker->ResolveType(dex_file, dex::TypeIndex(type_idx), dex_cache, class_loader)
1802 : nullptr;
1803
1804 if (klass == nullptr) {
1805 soa.Self()->AssertPendingException();
1806 mirror::Throwable* exception = soa.Self()->GetException();
1807 VLOG(compiler) << "Exception during type resolution: " << exception->Dump();
1808 if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
1809 // There's little point continuing compilation if the heap is exhausted.
1810 LOG(FATAL) << "Out of memory during type resolution for compilation";
1811 }
1812 soa.Self()->ClearException();
1813 }
1814 }
1815
1816 private:
1817 const ParallelCompilationManager* const manager_;
1818 };
1819
ResolveDexFile(jobject class_loader,const DexFile & dex_file,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,size_t thread_count,TimingLogger * timings)1820 void CompilerDriver::ResolveDexFile(jobject class_loader,
1821 const DexFile& dex_file,
1822 const std::vector<const DexFile*>& dex_files,
1823 ThreadPool* thread_pool,
1824 size_t thread_count,
1825 TimingLogger* timings) {
1826 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1827
1828 // TODO: we could resolve strings here, although the string table is largely filled with class
1829 // and method names.
1830
1831 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
1832 thread_pool);
1833 if (GetCompilerOptions().IsBootImage()) {
1834 // For images we resolve all types, such as array, whereas for applications just those with
1835 // classdefs are resolved by ResolveClassFieldsAndMethods.
1836 TimingLogger::ScopedTiming t("Resolve Types", timings);
1837 ResolveTypeVisitor visitor(&context);
1838 context.ForAll(0, dex_file.NumTypeIds(), &visitor, thread_count);
1839 }
1840
1841 TimingLogger::ScopedTiming t("Resolve MethodsAndFields", timings);
1842 ResolveClassFieldsAndMethodsVisitor visitor(&context);
1843 context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
1844 }
1845
SetVerified(jobject class_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)1846 void CompilerDriver::SetVerified(jobject class_loader,
1847 const std::vector<const DexFile*>& dex_files,
1848 TimingLogger* timings) {
1849 // This can be run in parallel.
1850 for (const DexFile* dex_file : dex_files) {
1851 CHECK(dex_file != nullptr);
1852 SetVerifiedDexFile(class_loader,
1853 *dex_file,
1854 dex_files,
1855 parallel_thread_pool_.get(),
1856 parallel_thread_count_,
1857 timings);
1858 }
1859 }
1860
PopulateVerifiedMethods(const DexFile & dex_file,uint32_t class_def_index,VerificationResults * verification_results)1861 static void PopulateVerifiedMethods(const DexFile& dex_file,
1862 uint32_t class_def_index,
1863 VerificationResults* verification_results) {
1864 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1865 const uint8_t* class_data = dex_file.GetClassData(class_def);
1866 if (class_data == nullptr) {
1867 return;
1868 }
1869 ClassDataItemIterator it(dex_file, class_data);
1870 it.SkipAllFields();
1871
1872 while (it.HasNextDirectMethod()) {
1873 verification_results->CreateVerifiedMethodFor(MethodReference(&dex_file, it.GetMemberIndex()));
1874 it.Next();
1875 }
1876
1877 while (it.HasNextVirtualMethod()) {
1878 verification_results->CreateVerifiedMethodFor(MethodReference(&dex_file, it.GetMemberIndex()));
1879 it.Next();
1880 }
1881 DCHECK(!it.HasNext());
1882 }
1883
LoadAndUpdateStatus(const DexFile & dex_file,const DexFile::ClassDef & class_def,mirror::Class::Status status,Handle<mirror::ClassLoader> class_loader,Thread * self)1884 static void LoadAndUpdateStatus(const DexFile& dex_file,
1885 const DexFile::ClassDef& class_def,
1886 mirror::Class::Status status,
1887 Handle<mirror::ClassLoader> class_loader,
1888 Thread* self)
1889 REQUIRES_SHARED(Locks::mutator_lock_) {
1890 StackHandleScope<1> hs(self);
1891 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1892 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1893 Handle<mirror::Class> cls(hs.NewHandle<mirror::Class>(
1894 class_linker->FindClass(self, descriptor, class_loader)));
1895 if (cls != nullptr) {
1896 // Check that the class is resolved with the current dex file. We might get
1897 // a boot image class, or a class in a different dex file for multidex, and
1898 // we should not update the status in that case.
1899 if (&cls->GetDexFile() == &dex_file) {
1900 ObjectLock<mirror::Class> lock(self, cls);
1901 mirror::Class::SetStatus(cls, status, self);
1902 }
1903 } else {
1904 DCHECK(self->IsExceptionPending());
1905 self->ClearException();
1906 }
1907 }
1908
FastVerify(jobject jclass_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)1909 bool CompilerDriver::FastVerify(jobject jclass_loader,
1910 const std::vector<const DexFile*>& dex_files,
1911 TimingLogger* timings) {
1912 verifier::VerifierDeps* verifier_deps =
1913 Runtime::Current()->GetCompilerCallbacks()->GetVerifierDeps();
1914 // If there exist VerifierDeps that aren't the ones we just created to output, use them to verify.
1915 if (verifier_deps == nullptr || verifier_deps->OutputOnly()) {
1916 return false;
1917 }
1918 TimingLogger::ScopedTiming t("Fast Verify", timings);
1919 ScopedObjectAccess soa(Thread::Current());
1920 StackHandleScope<2> hs(soa.Self());
1921 Handle<mirror::ClassLoader> class_loader(
1922 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
1923 if (!verifier_deps->ValidateDependencies(class_loader, soa.Self())) {
1924 return false;
1925 }
1926
1927 bool compiler_only_verifies = !GetCompilerOptions().IsAnyCompilationEnabled();
1928
1929 // We successfully validated the dependencies, now update class status
1930 // of verified classes. Note that the dependencies also record which classes
1931 // could not be fully verified; we could try again, but that would hurt verification
1932 // time. So instead we assume these classes still need to be verified at
1933 // runtime.
1934 for (const DexFile* dex_file : dex_files) {
1935 // Fetch the list of unverified classes.
1936 const std::set<dex::TypeIndex>& unverified_classes =
1937 verifier_deps->GetUnverifiedClasses(*dex_file);
1938 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
1939 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
1940 if (unverified_classes.find(class_def.class_idx_) == unverified_classes.end()) {
1941 if (compiler_only_verifies) {
1942 // Just update the compiled_classes_ map. The compiler doesn't need to resolve
1943 // the type.
1944 DexFileReference ref(dex_file, i);
1945 mirror::Class::Status existing = mirror::Class::kStatusNotReady;
1946 DCHECK(compiled_classes_.Get(ref, &existing)) << ref.dex_file->GetLocation();
1947 ClassStateTable::InsertResult result =
1948 compiled_classes_.Insert(ref, existing, mirror::Class::kStatusVerified);
1949 CHECK_EQ(result, ClassStateTable::kInsertResultSuccess);
1950 } else {
1951 // Update the class status, so later compilation stages know they don't need to verify
1952 // the class.
1953 LoadAndUpdateStatus(
1954 *dex_file, class_def, mirror::Class::kStatusVerified, class_loader, soa.Self());
1955 // Create `VerifiedMethod`s for each methods, the compiler expects one for
1956 // quickening or compiling.
1957 // Note that this means:
1958 // - We're only going to compile methods that did verify.
1959 // - Quickening will not do checkcast ellision.
1960 // TODO(ngeoffray): Reconsider this once we refactor compiler filters.
1961 PopulateVerifiedMethods(*dex_file, i, verification_results_);
1962 }
1963 } else if (!compiler_only_verifies) {
1964 // Make sure later compilation stages know they should not try to verify
1965 // this class again.
1966 LoadAndUpdateStatus(*dex_file,
1967 class_def,
1968 mirror::Class::kStatusRetryVerificationAtRuntime,
1969 class_loader,
1970 soa.Self());
1971 }
1972 }
1973 }
1974 return true;
1975 }
1976
Verify(jobject jclass_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)1977 void CompilerDriver::Verify(jobject jclass_loader,
1978 const std::vector<const DexFile*>& dex_files,
1979 TimingLogger* timings) {
1980 if (FastVerify(jclass_loader, dex_files, timings)) {
1981 return;
1982 }
1983
1984 // If there is no existing `verifier_deps` (because of non-existing vdex), or
1985 // the existing `verifier_deps` is not valid anymore, create a new one for
1986 // non boot image compilation. The verifier will need it to record the new dependencies.
1987 // Then dex2oat can update the vdex file with these new dependencies.
1988 if (!GetCompilerOptions().IsBootImage()) {
1989 // Dex2oat creates the verifier deps.
1990 // Create the main VerifierDeps, and set it to this thread.
1991 verifier::VerifierDeps* verifier_deps =
1992 Runtime::Current()->GetCompilerCallbacks()->GetVerifierDeps();
1993 CHECK(verifier_deps != nullptr);
1994 Thread::Current()->SetVerifierDeps(verifier_deps);
1995 // Create per-thread VerifierDeps to avoid contention on the main one.
1996 // We will merge them after verification.
1997 for (ThreadPoolWorker* worker : parallel_thread_pool_->GetWorkers()) {
1998 worker->GetThread()->SetVerifierDeps(new verifier::VerifierDeps(dex_files_for_oat_file_));
1999 }
2000 }
2001
2002 // Verification updates VerifierDeps and needs to run single-threaded to be deterministic.
2003 bool force_determinism = GetCompilerOptions().IsForceDeterminism();
2004 ThreadPool* verify_thread_pool =
2005 force_determinism ? single_thread_pool_.get() : parallel_thread_pool_.get();
2006 size_t verify_thread_count = force_determinism ? 1U : parallel_thread_count_;
2007 for (const DexFile* dex_file : dex_files) {
2008 CHECK(dex_file != nullptr);
2009 VerifyDexFile(jclass_loader,
2010 *dex_file,
2011 dex_files,
2012 verify_thread_pool,
2013 verify_thread_count,
2014 timings);
2015 }
2016
2017 if (!GetCompilerOptions().IsBootImage()) {
2018 // Merge all VerifierDeps into the main one.
2019 verifier::VerifierDeps* verifier_deps = Thread::Current()->GetVerifierDeps();
2020 for (ThreadPoolWorker* worker : parallel_thread_pool_->GetWorkers()) {
2021 verifier::VerifierDeps* thread_deps = worker->GetThread()->GetVerifierDeps();
2022 worker->GetThread()->SetVerifierDeps(nullptr);
2023 verifier_deps->MergeWith(*thread_deps, dex_files_for_oat_file_);
2024 delete thread_deps;
2025 }
2026 Thread::Current()->SetVerifierDeps(nullptr);
2027 }
2028 }
2029
2030 class VerifyClassVisitor : public CompilationVisitor {
2031 public:
VerifyClassVisitor(const ParallelCompilationManager * manager,verifier::HardFailLogMode log_level)2032 VerifyClassVisitor(const ParallelCompilationManager* manager, verifier::HardFailLogMode log_level)
2033 : manager_(manager), log_level_(log_level) {}
2034
Visit(size_t class_def_index)2035 virtual void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) OVERRIDE {
2036 ATRACE_CALL();
2037 ScopedObjectAccess soa(Thread::Current());
2038 const DexFile& dex_file = *manager_->GetDexFile();
2039 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
2040 const char* descriptor = dex_file.GetClassDescriptor(class_def);
2041 ClassLinker* class_linker = manager_->GetClassLinker();
2042 jobject jclass_loader = manager_->GetClassLoader();
2043 StackHandleScope<3> hs(soa.Self());
2044 Handle<mirror::ClassLoader> class_loader(
2045 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
2046 Handle<mirror::Class> klass(
2047 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
2048 verifier::FailureKind failure_kind;
2049 if (klass == nullptr) {
2050 CHECK(soa.Self()->IsExceptionPending());
2051 soa.Self()->ClearException();
2052
2053 /*
2054 * At compile time, we can still structurally verify the class even if FindClass fails.
2055 * This is to ensure the class is structurally sound for compilation. An unsound class
2056 * will be rejected by the verifier and later skipped during compilation in the compiler.
2057 */
2058 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(
2059 soa.Self(), dex_file)));
2060 std::string error_msg;
2061 failure_kind =
2062 verifier::MethodVerifier::VerifyClass(soa.Self(),
2063 &dex_file,
2064 dex_cache,
2065 class_loader,
2066 class_def,
2067 Runtime::Current()->GetCompilerCallbacks(),
2068 true /* allow soft failures */,
2069 log_level_,
2070 &error_msg);
2071 if (failure_kind == verifier::FailureKind::kHardFailure) {
2072 LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(descriptor)
2073 << " because: " << error_msg;
2074 manager_->GetCompiler()->SetHadHardVerifierFailure();
2075 } else {
2076 // Force a soft failure for the VerifierDeps. This is a sanity measure, as
2077 // the vdex file already records that the class hasn't been resolved. It avoids
2078 // trying to do future verification optimizations when processing the vdex file.
2079 DCHECK(failure_kind == verifier::FailureKind::kSoftFailure ||
2080 failure_kind == verifier::FailureKind::kNoFailure)
2081 << failure_kind;
2082 failure_kind = verifier::FailureKind::kSoftFailure;
2083 }
2084 } else if (!SkipClass(jclass_loader, dex_file, klass.Get())) {
2085 CHECK(klass->IsResolved()) << klass->PrettyClass();
2086 failure_kind = class_linker->VerifyClass(soa.Self(), klass, log_level_);
2087
2088 if (klass->IsErroneous()) {
2089 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
2090 CHECK(soa.Self()->IsExceptionPending());
2091 soa.Self()->ClearException();
2092 manager_->GetCompiler()->SetHadHardVerifierFailure();
2093 }
2094
2095 CHECK(klass->ShouldVerifyAtRuntime() || klass->IsVerified() || klass->IsErroneous())
2096 << klass->PrettyDescriptor() << ": state=" << klass->GetStatus();
2097
2098 // Class has a meaningful status for the compiler now, record it.
2099 ClassReference ref(manager_->GetDexFile(), class_def_index);
2100 manager_->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
2101
2102 // It is *very* problematic if there are verification errors in the boot classpath. For example,
2103 // we rely on things working OK without verification when the decryption dialog is brought up.
2104 // So abort in a debug build if we find this violated.
2105 if (kIsDebugBuild) {
2106 // TODO(narayan): Remove this special case for signature polymorphic
2107 // invokes once verifier support is fully implemented.
2108 if (manager_->GetCompiler()->GetCompilerOptions().IsBootImage() &&
2109 !android::base::StartsWith(descriptor, "Ljava/lang/invoke/")) {
2110 DCHECK(klass->IsVerified()) << "Boot classpath class " << klass->PrettyClass()
2111 << " failed to fully verify: state= " << klass->GetStatus();
2112 }
2113 if (klass->IsVerified()) {
2114 DCHECK_EQ(failure_kind, verifier::FailureKind::kNoFailure);
2115 } else if (klass->ShouldVerifyAtRuntime()) {
2116 DCHECK_EQ(failure_kind, verifier::FailureKind::kSoftFailure);
2117 } else {
2118 DCHECK_EQ(failure_kind, verifier::FailureKind::kHardFailure);
2119 }
2120 }
2121 } else {
2122 // Make the skip a soft failure, essentially being considered as verify at runtime.
2123 failure_kind = verifier::FailureKind::kSoftFailure;
2124 }
2125 verifier::VerifierDeps::MaybeRecordVerificationStatus(
2126 dex_file, class_def.class_idx_, failure_kind);
2127 soa.Self()->AssertNoPendingException();
2128 }
2129
2130 private:
2131 const ParallelCompilationManager* const manager_;
2132 const verifier::HardFailLogMode log_level_;
2133 };
2134
VerifyDexFile(jobject class_loader,const DexFile & dex_file,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,size_t thread_count,TimingLogger * timings)2135 void CompilerDriver::VerifyDexFile(jobject class_loader,
2136 const DexFile& dex_file,
2137 const std::vector<const DexFile*>& dex_files,
2138 ThreadPool* thread_pool,
2139 size_t thread_count,
2140 TimingLogger* timings) {
2141 TimingLogger::ScopedTiming t("Verify Dex File", timings);
2142 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2143 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
2144 thread_pool);
2145 verifier::HardFailLogMode log_level = GetCompilerOptions().AbortOnHardVerifierFailure()
2146 ? verifier::HardFailLogMode::kLogInternalFatal
2147 : verifier::HardFailLogMode::kLogWarning;
2148 VerifyClassVisitor visitor(&context, log_level);
2149 context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
2150 }
2151
2152 class SetVerifiedClassVisitor : public CompilationVisitor {
2153 public:
SetVerifiedClassVisitor(const ParallelCompilationManager * manager)2154 explicit SetVerifiedClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {}
2155
Visit(size_t class_def_index)2156 virtual void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) OVERRIDE {
2157 ATRACE_CALL();
2158 ScopedObjectAccess soa(Thread::Current());
2159 const DexFile& dex_file = *manager_->GetDexFile();
2160 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
2161 const char* descriptor = dex_file.GetClassDescriptor(class_def);
2162 ClassLinker* class_linker = manager_->GetClassLinker();
2163 jobject jclass_loader = manager_->GetClassLoader();
2164 StackHandleScope<3> hs(soa.Self());
2165 Handle<mirror::ClassLoader> class_loader(
2166 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
2167 Handle<mirror::Class> klass(
2168 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
2169 // Class might have failed resolution. Then don't set it to verified.
2170 if (klass != nullptr) {
2171 // Only do this if the class is resolved. If even resolution fails, quickening will go very,
2172 // very wrong.
2173 if (klass->IsResolved() && !klass->IsErroneousResolved()) {
2174 if (klass->GetStatus() < mirror::Class::kStatusVerified) {
2175 ObjectLock<mirror::Class> lock(soa.Self(), klass);
2176 // Set class status to verified.
2177 mirror::Class::SetStatus(klass, mirror::Class::kStatusVerified, soa.Self());
2178 // Mark methods as pre-verified. If we don't do this, the interpreter will run with
2179 // access checks.
2180 klass->SetSkipAccessChecksFlagOnAllMethods(
2181 GetInstructionSetPointerSize(manager_->GetCompiler()->GetInstructionSet()));
2182 klass->SetVerificationAttempted();
2183 }
2184 // Record the final class status if necessary.
2185 ClassReference ref(manager_->GetDexFile(), class_def_index);
2186 manager_->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
2187 }
2188 } else {
2189 Thread* self = soa.Self();
2190 DCHECK(self->IsExceptionPending());
2191 self->ClearException();
2192 }
2193 }
2194
2195 private:
2196 const ParallelCompilationManager* const manager_;
2197 };
2198
SetVerifiedDexFile(jobject class_loader,const DexFile & dex_file,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,size_t thread_count,TimingLogger * timings)2199 void CompilerDriver::SetVerifiedDexFile(jobject class_loader,
2200 const DexFile& dex_file,
2201 const std::vector<const DexFile*>& dex_files,
2202 ThreadPool* thread_pool,
2203 size_t thread_count,
2204 TimingLogger* timings) {
2205 TimingLogger::ScopedTiming t("Verify Dex File", timings);
2206 if (!compiled_classes_.HaveDexFile(&dex_file)) {
2207 compiled_classes_.AddDexFile(&dex_file, dex_file.NumClassDefs());
2208 }
2209 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2210 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
2211 thread_pool);
2212 SetVerifiedClassVisitor visitor(&context);
2213 context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
2214 }
2215
2216 class InitializeClassVisitor : public CompilationVisitor {
2217 public:
InitializeClassVisitor(const ParallelCompilationManager * manager)2218 explicit InitializeClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {}
2219
Visit(size_t class_def_index)2220 void Visit(size_t class_def_index) OVERRIDE {
2221 ATRACE_CALL();
2222 jobject jclass_loader = manager_->GetClassLoader();
2223 const DexFile& dex_file = *manager_->GetDexFile();
2224 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
2225 const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def.class_idx_);
2226 const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_);
2227
2228 ScopedObjectAccess soa(Thread::Current());
2229 StackHandleScope<3> hs(soa.Self());
2230 Handle<mirror::ClassLoader> class_loader(
2231 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
2232 Handle<mirror::Class> klass(
2233 hs.NewHandle(manager_->GetClassLinker()->FindClass(soa.Self(), descriptor, class_loader)));
2234
2235 if (klass != nullptr && !SkipClass(manager_->GetClassLoader(), dex_file, klass.Get())) {
2236 TryInitializeClass(klass, class_loader);
2237 }
2238 // Clear any class not found or verification exceptions.
2239 soa.Self()->ClearException();
2240 }
2241
2242 // A helper function for initializing klass.
TryInitializeClass(Handle<mirror::Class> klass,Handle<mirror::ClassLoader> & class_loader)2243 void TryInitializeClass(Handle<mirror::Class> klass, Handle<mirror::ClassLoader>& class_loader)
2244 REQUIRES_SHARED(Locks::mutator_lock_) {
2245 const DexFile& dex_file = klass->GetDexFile();
2246 const DexFile::ClassDef* class_def = klass->GetClassDef();
2247 const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def->class_idx_);
2248 const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_);
2249 ScopedObjectAccessUnchecked soa(Thread::Current());
2250 StackHandleScope<3> hs(soa.Self());
2251 const bool is_boot_image = manager_->GetCompiler()->GetCompilerOptions().IsBootImage();
2252 const bool is_app_image = manager_->GetCompiler()->GetCompilerOptions().IsAppImage();
2253
2254 mirror::Class::Status old_status = klass->GetStatus();
2255 // Don't initialize classes in boot space when compiling app image
2256 if (is_app_image && klass->IsBootStrapClassLoaded()) {
2257 // Also return early and don't store the class status in the recorded class status.
2258 return;
2259 }
2260 // Only try to initialize classes that were successfully verified.
2261 if (klass->IsVerified()) {
2262 // Attempt to initialize the class but bail if we either need to initialize the super-class
2263 // or static fields.
2264 manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, false, false);
2265 old_status = klass->GetStatus();
2266 if (!klass->IsInitialized()) {
2267 // We don't want non-trivial class initialization occurring on multiple threads due to
2268 // deadlock problems. For example, a parent class is initialized (holding its lock) that
2269 // refers to a sub-class in its static/class initializer causing it to try to acquire the
2270 // sub-class' lock. While on a second thread the sub-class is initialized (holding its lock)
2271 // after first initializing its parents, whose locks are acquired. This leads to a
2272 // parent-to-child and a child-to-parent lock ordering and consequent potential deadlock.
2273 // We need to use an ObjectLock due to potential suspension in the interpreting code. Rather
2274 // than use a special Object for the purpose we use the Class of java.lang.Class.
2275 Handle<mirror::Class> h_klass(hs.NewHandle(klass->GetClass()));
2276 ObjectLock<mirror::Class> lock(soa.Self(), h_klass);
2277 // Attempt to initialize allowing initialization of parent classes but still not static
2278 // fields.
2279 // Initialize dependencies first only for app image, to make TryInitialize recursive.
2280 bool is_superclass_initialized = !is_app_image ? true :
2281 InitializeDependencies(klass, class_loader, soa.Self());
2282 if (!is_app_image || (is_app_image && is_superclass_initialized)) {
2283 manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, false, true);
2284 }
2285 // Otherwise it's in app image but superclasses can't be initialized, no need to proceed.
2286 old_status = klass->GetStatus();
2287
2288 bool too_many_encoded_fields = false;
2289 if (!is_boot_image && klass->NumStaticFields() > kMaxEncodedFields) {
2290 too_many_encoded_fields = true;
2291 }
2292 // If the class was not initialized, we can proceed to see if we can initialize static
2293 // fields. Limit the max number of encoded fields.
2294 if (!klass->IsInitialized() &&
2295 (is_app_image || is_boot_image) &&
2296 is_superclass_initialized &&
2297 !too_many_encoded_fields &&
2298 manager_->GetCompiler()->IsImageClass(descriptor)) {
2299 bool can_init_static_fields = false;
2300 if (is_boot_image) {
2301 // We need to initialize static fields, we only do this for image classes that aren't
2302 // marked with the $NoPreloadHolder (which implies this should not be initialized
2303 // early).
2304 can_init_static_fields = !StringPiece(descriptor).ends_with("$NoPreloadHolder;");
2305 } else {
2306 CHECK(is_app_image);
2307 // The boot image case doesn't need to recursively initialize the dependencies with
2308 // special logic since the class linker already does this.
2309 can_init_static_fields =
2310 !soa.Self()->IsExceptionPending() &&
2311 is_superclass_initialized &&
2312 NoClinitInDependency(klass, soa.Self(), &class_loader);
2313 // TODO The checking for clinit can be removed since it's already
2314 // checked when init superclass. Currently keep it because it contains
2315 // processing of intern strings. Will be removed later when intern strings
2316 // and clinit are both initialized.
2317 }
2318
2319 if (can_init_static_fields) {
2320 VLOG(compiler) << "Initializing: " << descriptor;
2321 // TODO multithreading support. We should ensure the current compilation thread has
2322 // exclusive access to the runtime and the transaction. To achieve this, we could use
2323 // a ReaderWriterMutex but we're holding the mutator lock so we fail mutex sanity
2324 // checks in Thread::AssertThreadSuspensionIsAllowable.
2325 Runtime* const runtime = Runtime::Current();
2326 Transaction transaction;
2327
2328 // Run the class initializer in transaction mode.
2329 runtime->EnterTransactionMode(&transaction);
2330 bool success = manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, true,
2331 true);
2332 // TODO we detach transaction from runtime to indicate we quit the transactional
2333 // mode which prevents the GC from visiting objects modified during the transaction.
2334 // Ensure GC is not run so don't access freed objects when aborting transaction.
2335
2336 {
2337 ScopedAssertNoThreadSuspension ants("Transaction end");
2338 runtime->ExitTransactionMode();
2339
2340 if (!success) {
2341 CHECK(soa.Self()->IsExceptionPending());
2342 mirror::Throwable* exception = soa.Self()->GetException();
2343 VLOG(compiler) << "Initialization of " << descriptor << " aborted because of "
2344 << exception->Dump();
2345 std::ostream* file_log = manager_->GetCompiler()->
2346 GetCompilerOptions().GetInitFailureOutput();
2347 if (file_log != nullptr) {
2348 *file_log << descriptor << "\n";
2349 *file_log << exception->Dump() << "\n";
2350 }
2351 soa.Self()->ClearException();
2352 transaction.Rollback();
2353 CHECK_EQ(old_status, klass->GetStatus()) << "Previous class status not restored";
2354 } else if (is_boot_image) {
2355 // For boot image, we want to put the updated status in the oat class since we can't
2356 // reject the image anyways.
2357 old_status = klass->GetStatus();
2358 }
2359 }
2360
2361 if (!success) {
2362 // On failure, still intern strings of static fields and seen in <clinit>, as these
2363 // will be created in the zygote. This is separated from the transaction code just
2364 // above as we will allocate strings, so must be allowed to suspend.
2365 if (&klass->GetDexFile() == manager_->GetDexFile()) {
2366 InternStrings(klass, class_loader);
2367 } else {
2368 DCHECK(!is_boot_image) << "Boot image must have equal dex files";
2369 }
2370 }
2371 }
2372 }
2373 // If the class still isn't initialized, at least try some checks that initialization
2374 // would do so they can be skipped at runtime.
2375 if (!klass->IsInitialized() &&
2376 manager_->GetClassLinker()->ValidateSuperClassDescriptors(klass)) {
2377 old_status = mirror::Class::kStatusSuperclassValidated;
2378 } else {
2379 soa.Self()->ClearException();
2380 }
2381 soa.Self()->AssertNoPendingException();
2382 }
2383 }
2384 // Record the final class status if necessary.
2385 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
2386 // Back up the status before doing initialization for static encoded fields,
2387 // because the static encoded branch wants to keep the status to uninitialized.
2388 manager_->GetCompiler()->RecordClassStatus(ref, old_status);
2389 }
2390
2391 private:
InternStrings(Handle<mirror::Class> klass,Handle<mirror::ClassLoader> class_loader)2392 void InternStrings(Handle<mirror::Class> klass, Handle<mirror::ClassLoader> class_loader)
2393 REQUIRES_SHARED(Locks::mutator_lock_) {
2394 DCHECK(manager_->GetCompiler()->GetCompilerOptions().IsBootImage());
2395 DCHECK(klass->IsVerified());
2396 DCHECK(!klass->IsInitialized());
2397
2398 StackHandleScope<1> hs(Thread::Current());
2399 Handle<mirror::DexCache> h_dex_cache = hs.NewHandle(klass->GetDexCache());
2400 const DexFile* dex_file = manager_->GetDexFile();
2401 const DexFile::ClassDef* class_def = klass->GetClassDef();
2402 ClassLinker* class_linker = manager_->GetClassLinker();
2403
2404 // Check encoded final field values for strings and intern.
2405 annotations::RuntimeEncodedStaticFieldValueIterator value_it(*dex_file,
2406 &h_dex_cache,
2407 &class_loader,
2408 manager_->GetClassLinker(),
2409 *class_def);
2410 for ( ; value_it.HasNext(); value_it.Next()) {
2411 if (value_it.GetValueType() == annotations::RuntimeEncodedStaticFieldValueIterator::kString) {
2412 // Resolve the string. This will intern the string.
2413 art::ObjPtr<mirror::String> resolved = class_linker->ResolveString(
2414 *dex_file, dex::StringIndex(value_it.GetJavaValue().i), h_dex_cache);
2415 CHECK(resolved != nullptr);
2416 }
2417 }
2418
2419 // Intern strings seen in <clinit>.
2420 ArtMethod* clinit = klass->FindClassInitializer(class_linker->GetImagePointerSize());
2421 if (clinit != nullptr) {
2422 const DexFile::CodeItem* code_item = clinit->GetCodeItem();
2423 DCHECK(code_item != nullptr);
2424 const Instruction* inst = Instruction::At(code_item->insns_);
2425
2426 const uint32_t insns_size = code_item->insns_size_in_code_units_;
2427 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
2428 if (inst->Opcode() == Instruction::CONST_STRING) {
2429 ObjPtr<mirror::String> s = class_linker->ResolveString(
2430 *dex_file, dex::StringIndex(inst->VRegB_21c()), h_dex_cache);
2431 CHECK(s != nullptr);
2432 } else if (inst->Opcode() == Instruction::CONST_STRING_JUMBO) {
2433 ObjPtr<mirror::String> s = class_linker->ResolveString(
2434 *dex_file, dex::StringIndex(inst->VRegB_31c()), h_dex_cache);
2435 CHECK(s != nullptr);
2436 }
2437 dex_pc += inst->SizeInCodeUnits();
2438 inst = inst->Next();
2439 }
2440 }
2441 }
2442
ResolveTypesOfMethods(Thread * self,ArtMethod * m)2443 bool ResolveTypesOfMethods(Thread* self, ArtMethod* m)
2444 REQUIRES_SHARED(Locks::mutator_lock_) {
2445 auto rtn_type = m->GetReturnType(true); // return value is discarded because resolve will be done internally.
2446 if (rtn_type == nullptr) {
2447 self->ClearException();
2448 return false;
2449 }
2450 const DexFile::TypeList* types = m->GetParameterTypeList();
2451 if (types != nullptr) {
2452 for (uint32_t i = 0; i < types->Size(); ++i) {
2453 dex::TypeIndex param_type_idx = types->GetTypeItem(i).type_idx_;
2454 auto param_type = m->GetClassFromTypeIndex(param_type_idx, true);
2455 if (param_type == nullptr) {
2456 self->ClearException();
2457 return false;
2458 }
2459 }
2460 }
2461 return true;
2462 }
2463
2464 // Pre resolve types mentioned in all method signatures before start a transaction
2465 // since ResolveType doesn't work in transaction mode.
PreResolveTypes(Thread * self,const Handle<mirror::Class> & klass)2466 bool PreResolveTypes(Thread* self, const Handle<mirror::Class>& klass)
2467 REQUIRES_SHARED(Locks::mutator_lock_) {
2468 PointerSize pointer_size = manager_->GetClassLinker()->GetImagePointerSize();
2469 for (ArtMethod& m : klass->GetMethods(pointer_size)) {
2470 if (!ResolveTypesOfMethods(self, &m)) {
2471 return false;
2472 }
2473 }
2474 if (klass->IsInterface()) {
2475 return true;
2476 } else if (klass->HasSuperClass()) {
2477 StackHandleScope<1> hs(self);
2478 MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(klass->GetSuperClass()));
2479 for (int i = super_klass->GetVTableLength() - 1; i >= 0; --i) {
2480 ArtMethod* m = klass->GetVTableEntry(i, pointer_size);
2481 ArtMethod* super_m = super_klass->GetVTableEntry(i, pointer_size);
2482 if (!ResolveTypesOfMethods(self, m) || !ResolveTypesOfMethods(self, super_m)) {
2483 return false;
2484 }
2485 }
2486 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
2487 super_klass.Assign(klass->GetIfTable()->GetInterface(i));
2488 if (klass->GetClassLoader() != super_klass->GetClassLoader()) {
2489 uint32_t num_methods = super_klass->NumVirtualMethods();
2490 for (uint32_t j = 0; j < num_methods; ++j) {
2491 ArtMethod* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>(
2492 j, pointer_size);
2493 ArtMethod* super_m = super_klass->GetVirtualMethod(j, pointer_size);
2494 if (!ResolveTypesOfMethods(self, m) || !ResolveTypesOfMethods(self, super_m)) {
2495 return false;
2496 }
2497 }
2498 }
2499 }
2500 }
2501 return true;
2502 }
2503
2504 // Initialize the klass's dependencies recursively before initializing itself.
2505 // Checking for interfaces is also necessary since interfaces can contain
2506 // both default methods and static encoded fields.
InitializeDependencies(const Handle<mirror::Class> & klass,Handle<mirror::ClassLoader> class_loader,Thread * self)2507 bool InitializeDependencies(const Handle<mirror::Class>& klass,
2508 Handle<mirror::ClassLoader> class_loader,
2509 Thread* self)
2510 REQUIRES_SHARED(Locks::mutator_lock_) {
2511 if (klass->HasSuperClass()) {
2512 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
2513 StackHandleScope<1> hs(self);
2514 Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class));
2515 if (!handle_scope_super->IsInitialized()) {
2516 this->TryInitializeClass(handle_scope_super, class_loader);
2517 if (!handle_scope_super->IsInitialized()) {
2518 return false;
2519 }
2520 }
2521 }
2522
2523 uint32_t num_if = klass->NumDirectInterfaces();
2524 for (size_t i = 0; i < num_if; i++) {
2525 ObjPtr<mirror::Class>
2526 interface = mirror::Class::GetDirectInterface(self, klass.Get(), i);
2527 StackHandleScope<1> hs(self);
2528 Handle<mirror::Class> handle_interface(hs.NewHandle(interface));
2529
2530 TryInitializeClass(handle_interface, class_loader);
2531
2532 if (!handle_interface->IsInitialized()) {
2533 return false;
2534 }
2535 }
2536
2537 return PreResolveTypes(self, klass);
2538 }
2539
2540 // In this phase the classes containing class initializers are ignored. Make sure no
2541 // clinit appears in kalss's super class chain and interfaces.
NoClinitInDependency(const Handle<mirror::Class> & klass,Thread * self,Handle<mirror::ClassLoader> * class_loader)2542 bool NoClinitInDependency(const Handle<mirror::Class>& klass,
2543 Thread* self,
2544 Handle<mirror::ClassLoader>* class_loader)
2545 REQUIRES_SHARED(Locks::mutator_lock_) {
2546 ArtMethod* clinit =
2547 klass->FindClassInitializer(manager_->GetClassLinker()->GetImagePointerSize());
2548 if (clinit != nullptr) {
2549 VLOG(compiler) << klass->PrettyClass() << ' ' << clinit->PrettyMethod(true);
2550 return false;
2551 }
2552 if (klass->HasSuperClass()) {
2553 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
2554 StackHandleScope<1> hs(self);
2555 Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class));
2556 if (!NoClinitInDependency(handle_scope_super, self, class_loader)) {
2557 return false;
2558 }
2559 }
2560
2561 uint32_t num_if = klass->NumDirectInterfaces();
2562 for (size_t i = 0; i < num_if; i++) {
2563 ObjPtr<mirror::Class>
2564 interface = mirror::Class::GetDirectInterface(self, klass.Get(), i);
2565 StackHandleScope<1> hs(self);
2566 Handle<mirror::Class> handle_interface(hs.NewHandle(interface));
2567 if (!NoClinitInDependency(handle_interface, self, class_loader)) {
2568 return false;
2569 }
2570 }
2571
2572 return true;
2573 }
2574
2575 const ParallelCompilationManager* const manager_;
2576 };
2577
InitializeClasses(jobject jni_class_loader,const DexFile & dex_file,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)2578 void CompilerDriver::InitializeClasses(jobject jni_class_loader,
2579 const DexFile& dex_file,
2580 const std::vector<const DexFile*>& dex_files,
2581 TimingLogger* timings) {
2582 TimingLogger::ScopedTiming t("InitializeNoClinit", timings);
2583
2584 // Initialization allocates objects and needs to run single-threaded to be deterministic.
2585 bool force_determinism = GetCompilerOptions().IsForceDeterminism();
2586 ThreadPool* init_thread_pool = force_determinism
2587 ? single_thread_pool_.get()
2588 : parallel_thread_pool_.get();
2589 size_t init_thread_count = force_determinism ? 1U : parallel_thread_count_;
2590
2591 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2592 ParallelCompilationManager context(class_linker, jni_class_loader, this, &dex_file, dex_files,
2593 init_thread_pool);
2594
2595 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsAppImage()) {
2596 // Set the concurrency thread to 1 to support initialization for App Images since transaction
2597 // doesn't support multithreading now.
2598 // TODO: remove this when transactional mode supports multithreading.
2599 init_thread_count = 1U;
2600 }
2601 InitializeClassVisitor visitor(&context);
2602 context.ForAll(0, dex_file.NumClassDefs(), &visitor, init_thread_count);
2603 }
2604
2605 class InitializeArrayClassesAndCreateConflictTablesVisitor : public ClassVisitor {
2606 public:
InitializeArrayClassesAndCreateConflictTablesVisitor(VariableSizedHandleScope & hs)2607 explicit InitializeArrayClassesAndCreateConflictTablesVisitor(VariableSizedHandleScope& hs)
2608 : hs_(hs) {}
2609
operator ()(ObjPtr<mirror::Class> klass)2610 virtual bool operator()(ObjPtr<mirror::Class> klass) OVERRIDE
2611 REQUIRES_SHARED(Locks::mutator_lock_) {
2612 if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) {
2613 return true;
2614 }
2615 if (klass->IsArrayClass()) {
2616 StackHandleScope<1> hs(Thread::Current());
2617 auto h_klass = hs.NewHandleWrapper(&klass);
2618 Runtime::Current()->GetClassLinker()->EnsureInitialized(hs.Self(), h_klass, true, true);
2619 }
2620 // Collect handles since there may be thread suspension in future EnsureInitialized.
2621 to_visit_.push_back(hs_.NewHandle(klass));
2622 return true;
2623 }
2624
FillAllIMTAndConflictTables()2625 void FillAllIMTAndConflictTables() REQUIRES_SHARED(Locks::mutator_lock_) {
2626 for (Handle<mirror::Class> c : to_visit_) {
2627 // Create the conflict tables.
2628 FillIMTAndConflictTables(c.Get());
2629 }
2630 }
2631
2632 private:
FillIMTAndConflictTables(ObjPtr<mirror::Class> klass)2633 void FillIMTAndConflictTables(ObjPtr<mirror::Class> klass)
2634 REQUIRES_SHARED(Locks::mutator_lock_) {
2635 if (!klass->ShouldHaveImt()) {
2636 return;
2637 }
2638 if (visited_classes_.find(klass) != visited_classes_.end()) {
2639 return;
2640 }
2641 if (klass->HasSuperClass()) {
2642 FillIMTAndConflictTables(klass->GetSuperClass());
2643 }
2644 if (!klass->IsTemp()) {
2645 Runtime::Current()->GetClassLinker()->FillIMTAndConflictTables(klass);
2646 }
2647 visited_classes_.insert(klass);
2648 }
2649
2650 VariableSizedHandleScope& hs_;
2651 std::vector<Handle<mirror::Class>> to_visit_;
2652 std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr> visited_classes_;
2653 };
2654
InitializeClasses(jobject class_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)2655 void CompilerDriver::InitializeClasses(jobject class_loader,
2656 const std::vector<const DexFile*>& dex_files,
2657 TimingLogger* timings) {
2658 for (size_t i = 0; i != dex_files.size(); ++i) {
2659 const DexFile* dex_file = dex_files[i];
2660 CHECK(dex_file != nullptr);
2661 InitializeClasses(class_loader, *dex_file, dex_files, timings);
2662 }
2663 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsAppImage()) {
2664 // Make sure that we call EnsureIntiailized on all the array classes to call
2665 // SetVerificationAttempted so that the access flags are set. If we do not do this they get
2666 // changed at runtime resulting in more dirty image pages.
2667 // Also create conflict tables.
2668 // Only useful if we are compiling an image (image_classes_ is not null).
2669 ScopedObjectAccess soa(Thread::Current());
2670 VariableSizedHandleScope hs(soa.Self());
2671 InitializeArrayClassesAndCreateConflictTablesVisitor visitor(hs);
2672 Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(&visitor);
2673 visitor.FillAllIMTAndConflictTables();
2674 }
2675 if (GetCompilerOptions().IsBootImage()) {
2676 // Prune garbage objects created during aborted transactions.
2677 Runtime::Current()->GetHeap()->CollectGarbage(true);
2678 }
2679 }
2680
Compile(jobject class_loader,const std::vector<const DexFile * > & dex_files,TimingLogger * timings)2681 void CompilerDriver::Compile(jobject class_loader,
2682 const std::vector<const DexFile*>& dex_files,
2683 TimingLogger* timings) {
2684 if (kDebugProfileGuidedCompilation) {
2685 LOG(INFO) << "[ProfileGuidedCompilation] " <<
2686 ((profile_compilation_info_ == nullptr)
2687 ? "null"
2688 : profile_compilation_info_->DumpInfo(&dex_files));
2689 }
2690
2691 current_dex_to_dex_methods_ = nullptr;
2692 Thread* const self = Thread::Current();
2693 {
2694 // Clear in case we aren't the first call to Compile.
2695 MutexLock mu(self, dex_to_dex_references_lock_);
2696 dex_to_dex_references_.clear();
2697 }
2698
2699 for (const DexFile* dex_file : dex_files) {
2700 CHECK(dex_file != nullptr);
2701 CompileDexFile(class_loader,
2702 *dex_file,
2703 dex_files,
2704 parallel_thread_pool_.get(),
2705 parallel_thread_count_,
2706 timings);
2707 const ArenaPool* const arena_pool = Runtime::Current()->GetArenaPool();
2708 const size_t arena_alloc = arena_pool->GetBytesAllocated();
2709 max_arena_alloc_ = std::max(arena_alloc, max_arena_alloc_);
2710 Runtime::Current()->ReclaimArenaPoolMemory();
2711 }
2712
2713 ArrayRef<DexFileMethodSet> dex_to_dex_references;
2714 {
2715 // From this point on, we shall not modify dex_to_dex_references_, so
2716 // just grab a reference to it that we use without holding the mutex.
2717 MutexLock lock(self, dex_to_dex_references_lock_);
2718 dex_to_dex_references = ArrayRef<DexFileMethodSet>(dex_to_dex_references_);
2719 }
2720 for (const auto& method_set : dex_to_dex_references) {
2721 current_dex_to_dex_methods_ = &method_set.GetMethodIndexes();
2722 CompileDexFile(class_loader,
2723 method_set.GetDexFile(),
2724 dex_files,
2725 parallel_thread_pool_.get(),
2726 parallel_thread_count_,
2727 timings);
2728 }
2729 current_dex_to_dex_methods_ = nullptr;
2730
2731 VLOG(compiler) << "Compile: " << GetMemoryUsageString(false);
2732 }
2733
2734 class CompileClassVisitor : public CompilationVisitor {
2735 public:
CompileClassVisitor(const ParallelCompilationManager * manager)2736 explicit CompileClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {}
2737
Visit(size_t class_def_index)2738 virtual void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) OVERRIDE {
2739 ATRACE_CALL();
2740 const DexFile& dex_file = *manager_->GetDexFile();
2741 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
2742 ClassLinker* class_linker = manager_->GetClassLinker();
2743 jobject jclass_loader = manager_->GetClassLoader();
2744 ClassReference ref(&dex_file, class_def_index);
2745 // Skip compiling classes with generic verifier failures since they will still fail at runtime
2746 if (manager_->GetCompiler()->verification_results_->IsClassRejected(ref)) {
2747 return;
2748 }
2749 // Use a scoped object access to perform to the quick SkipClass check.
2750 const char* descriptor = dex_file.GetClassDescriptor(class_def);
2751 ScopedObjectAccess soa(Thread::Current());
2752 StackHandleScope<3> hs(soa.Self());
2753 Handle<mirror::ClassLoader> class_loader(
2754 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
2755 Handle<mirror::Class> klass(
2756 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
2757 Handle<mirror::DexCache> dex_cache;
2758 if (klass == nullptr) {
2759 soa.Self()->AssertPendingException();
2760 soa.Self()->ClearException();
2761 dex_cache = hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
2762 } else if (SkipClass(jclass_loader, dex_file, klass.Get())) {
2763 return;
2764 } else {
2765 dex_cache = hs.NewHandle(klass->GetDexCache());
2766 }
2767
2768 const uint8_t* class_data = dex_file.GetClassData(class_def);
2769 if (class_data == nullptr) {
2770 // empty class, probably a marker interface
2771 return;
2772 }
2773
2774 // Go to native so that we don't block GC during compilation.
2775 ScopedThreadSuspension sts(soa.Self(), kNative);
2776
2777 CompilerDriver* const driver = manager_->GetCompiler();
2778
2779 // Can we run DEX-to-DEX compiler on this class ?
2780 optimizer::DexToDexCompilationLevel dex_to_dex_compilation_level =
2781 GetDexToDexCompilationLevel(soa.Self(), *driver, jclass_loader, dex_file, class_def);
2782
2783 ClassDataItemIterator it(dex_file, class_data);
2784 it.SkipAllFields();
2785
2786 bool compilation_enabled = driver->IsClassToCompile(
2787 dex_file.StringByTypeIdx(class_def.class_idx_));
2788
2789 // Compile direct methods
2790 int64_t previous_direct_method_idx = -1;
2791 while (it.HasNextDirectMethod()) {
2792 uint32_t method_idx = it.GetMemberIndex();
2793 if (method_idx == previous_direct_method_idx) {
2794 // smali can create dex files with two encoded_methods sharing the same method_idx
2795 // http://code.google.com/p/smali/issues/detail?id=119
2796 it.Next();
2797 continue;
2798 }
2799 previous_direct_method_idx = method_idx;
2800 CompileMethod(soa.Self(),
2801 driver,
2802 it.GetMethodCodeItem(),
2803 it.GetMethodAccessFlags(),
2804 it.GetMethodInvokeType(class_def),
2805 class_def_index,
2806 method_idx,
2807 class_loader,
2808 dex_file,
2809 dex_to_dex_compilation_level,
2810 compilation_enabled,
2811 dex_cache);
2812 it.Next();
2813 }
2814 // Compile virtual methods
2815 int64_t previous_virtual_method_idx = -1;
2816 while (it.HasNextVirtualMethod()) {
2817 uint32_t method_idx = it.GetMemberIndex();
2818 if (method_idx == previous_virtual_method_idx) {
2819 // smali can create dex files with two encoded_methods sharing the same method_idx
2820 // http://code.google.com/p/smali/issues/detail?id=119
2821 it.Next();
2822 continue;
2823 }
2824 previous_virtual_method_idx = method_idx;
2825 CompileMethod(soa.Self(),
2826 driver, it.GetMethodCodeItem(),
2827 it.GetMethodAccessFlags(),
2828 it.GetMethodInvokeType(class_def),
2829 class_def_index,
2830 method_idx,
2831 class_loader,
2832 dex_file,
2833 dex_to_dex_compilation_level,
2834 compilation_enabled,
2835 dex_cache);
2836 it.Next();
2837 }
2838 DCHECK(!it.HasNext());
2839 }
2840
2841 private:
2842 const ParallelCompilationManager* const manager_;
2843 };
2844
CompileDexFile(jobject class_loader,const DexFile & dex_file,const std::vector<const DexFile * > & dex_files,ThreadPool * thread_pool,size_t thread_count,TimingLogger * timings)2845 void CompilerDriver::CompileDexFile(jobject class_loader,
2846 const DexFile& dex_file,
2847 const std::vector<const DexFile*>& dex_files,
2848 ThreadPool* thread_pool,
2849 size_t thread_count,
2850 TimingLogger* timings) {
2851 TimingLogger::ScopedTiming t("Compile Dex File", timings);
2852 ParallelCompilationManager context(Runtime::Current()->GetClassLinker(), class_loader, this,
2853 &dex_file, dex_files, thread_pool);
2854 CompileClassVisitor visitor(&context);
2855 context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
2856 }
2857
AddCompiledMethod(const MethodReference & method_ref,CompiledMethod * const compiled_method,size_t non_relative_linker_patch_count)2858 void CompilerDriver::AddCompiledMethod(const MethodReference& method_ref,
2859 CompiledMethod* const compiled_method,
2860 size_t non_relative_linker_patch_count) {
2861 DCHECK(GetCompiledMethod(method_ref) == nullptr)
2862 << method_ref.dex_file->PrettyMethod(method_ref.dex_method_index);
2863 MethodTable::InsertResult result = compiled_methods_.Insert(
2864 DexFileReference(method_ref.dex_file, method_ref.dex_method_index),
2865 /*expected*/ nullptr,
2866 compiled_method);
2867 CHECK(result == MethodTable::kInsertResultSuccess);
2868 non_relative_linker_patch_count_.FetchAndAddRelaxed(non_relative_linker_patch_count);
2869 DCHECK(GetCompiledMethod(method_ref) != nullptr)
2870 << method_ref.dex_file->PrettyMethod(method_ref.dex_method_index);
2871 }
2872
GetCompiledClass(ClassReference ref,mirror::Class::Status * status) const2873 bool CompilerDriver::GetCompiledClass(ClassReference ref, mirror::Class::Status* status) const {
2874 DCHECK(status != nullptr);
2875 // The table doesn't know if something wasn't inserted. For this case it will return
2876 // kStatusNotReady. To handle this, just assume anything we didn't try to verify is not compiled.
2877 if (!compiled_classes_.Get(DexFileReference(ref.first, ref.second), status) ||
2878 *status < mirror::Class::kStatusRetryVerificationAtRuntime) {
2879 return false;
2880 }
2881 return true;
2882 }
2883
RecordClassStatus(ClassReference ref,mirror::Class::Status status)2884 void CompilerDriver::RecordClassStatus(ClassReference ref, mirror::Class::Status status) {
2885 switch (status) {
2886 case mirror::Class::kStatusErrorResolved:
2887 case mirror::Class::kStatusErrorUnresolved:
2888 case mirror::Class::kStatusNotReady:
2889 case mirror::Class::kStatusResolved:
2890 case mirror::Class::kStatusRetryVerificationAtRuntime:
2891 case mirror::Class::kStatusVerified:
2892 case mirror::Class::kStatusSuperclassValidated:
2893 case mirror::Class::kStatusInitialized:
2894 break; // Expected states.
2895 default:
2896 LOG(FATAL) << "Unexpected class status for class "
2897 << PrettyDescriptor(ref.first->GetClassDescriptor(ref.first->GetClassDef(ref.second)))
2898 << " of " << status;
2899 }
2900
2901 ClassStateTable::InsertResult result;
2902 do {
2903 DexFileReference dex_ref(ref.first, ref.second);
2904 mirror::Class::Status existing = mirror::Class::kStatusNotReady;
2905 if (!compiled_classes_.Get(dex_ref, &existing)) {
2906 // Probably a uses library class, bail.
2907 if (kIsDebugBuild) {
2908 // Check to make sure it's not a dex file for an oat file we are compiling since these
2909 // should always succeed. These do not include classes in for used libraries.
2910 for (const DexFile* dex_file : GetDexFilesForOatFile()) {
2911 CHECK_NE(dex_ref.dex_file, dex_file) << dex_ref.dex_file->GetLocation();
2912 }
2913 }
2914 return;
2915 }
2916 if (existing >= status) {
2917 // Existing status is already better than we expect, break.
2918 break;
2919 }
2920 // Update the status if we now have a greater one. This happens with vdex,
2921 // which records a class is verified, but does not resolve it.
2922 result = compiled_classes_.Insert(dex_ref, existing, status);
2923 CHECK(result != ClassStateTable::kInsertResultInvalidDexFile);
2924 } while (result != ClassStateTable::kInsertResultSuccess);
2925 }
2926
GetCompiledMethod(MethodReference ref) const2927 CompiledMethod* CompilerDriver::GetCompiledMethod(MethodReference ref) const {
2928 CompiledMethod* compiled_method = nullptr;
2929 compiled_methods_.Get(DexFileReference(ref.dex_file, ref.dex_method_index), &compiled_method);
2930 return compiled_method;
2931 }
2932
IsMethodVerifiedWithoutFailures(uint32_t method_idx,uint16_t class_def_idx,const DexFile & dex_file) const2933 bool CompilerDriver::IsMethodVerifiedWithoutFailures(uint32_t method_idx,
2934 uint16_t class_def_idx,
2935 const DexFile& dex_file) const {
2936 const VerifiedMethod* verified_method = GetVerifiedMethod(&dex_file, method_idx);
2937 if (verified_method != nullptr) {
2938 return !verified_method->HasVerificationFailures();
2939 }
2940
2941 // If we can't find verification metadata, check if this is a system class (we trust that system
2942 // classes have their methods verified). If it's not, be conservative and assume the method
2943 // has not been verified successfully.
2944
2945 // TODO: When compiling the boot image it should be safe to assume that everything is verified,
2946 // even if methods are not found in the verification cache.
2947 const char* descriptor = dex_file.GetClassDescriptor(dex_file.GetClassDef(class_def_idx));
2948 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2949 Thread* self = Thread::Current();
2950 ScopedObjectAccess soa(self);
2951 bool is_system_class = class_linker->FindSystemClass(self, descriptor) != nullptr;
2952 if (!is_system_class) {
2953 self->ClearException();
2954 }
2955 return is_system_class;
2956 }
2957
GetNonRelativeLinkerPatchCount() const2958 size_t CompilerDriver::GetNonRelativeLinkerPatchCount() const {
2959 return non_relative_linker_patch_count_.LoadRelaxed();
2960 }
2961
SetRequiresConstructorBarrier(Thread * self,const DexFile * dex_file,uint16_t class_def_index,bool requires)2962 void CompilerDriver::SetRequiresConstructorBarrier(Thread* self,
2963 const DexFile* dex_file,
2964 uint16_t class_def_index,
2965 bool requires) {
2966 WriterMutexLock mu(self, requires_constructor_barrier_lock_);
2967 requires_constructor_barrier_.emplace(ClassReference(dex_file, class_def_index), requires);
2968 }
2969
RequiresConstructorBarrier(Thread * self,const DexFile * dex_file,uint16_t class_def_index)2970 bool CompilerDriver::RequiresConstructorBarrier(Thread* self,
2971 const DexFile* dex_file,
2972 uint16_t class_def_index) {
2973 ClassReference class_ref(dex_file, class_def_index);
2974 {
2975 ReaderMutexLock mu(self, requires_constructor_barrier_lock_);
2976 auto it = requires_constructor_barrier_.find(class_ref);
2977 if (it != requires_constructor_barrier_.end()) {
2978 return it->second;
2979 }
2980 }
2981 WriterMutexLock mu(self, requires_constructor_barrier_lock_);
2982 const bool requires = RequiresConstructorBarrier(*dex_file, class_def_index);
2983 requires_constructor_barrier_.emplace(class_ref, requires);
2984 return requires;
2985 }
2986
GetMemoryUsageString(bool extended) const2987 std::string CompilerDriver::GetMemoryUsageString(bool extended) const {
2988 std::ostringstream oss;
2989 const gc::Heap* const heap = Runtime::Current()->GetHeap();
2990 const size_t java_alloc = heap->GetBytesAllocated();
2991 oss << "arena alloc=" << PrettySize(max_arena_alloc_) << " (" << max_arena_alloc_ << "B)";
2992 oss << " java alloc=" << PrettySize(java_alloc) << " (" << java_alloc << "B)";
2993 #if defined(__BIONIC__) || defined(__GLIBC__)
2994 const struct mallinfo info = mallinfo();
2995 const size_t allocated_space = static_cast<size_t>(info.uordblks);
2996 const size_t free_space = static_cast<size_t>(info.fordblks);
2997 oss << " native alloc=" << PrettySize(allocated_space) << " (" << allocated_space << "B)"
2998 << " free=" << PrettySize(free_space) << " (" << free_space << "B)";
2999 #endif
3000 compiled_method_storage_.DumpMemoryUsage(oss, extended);
3001 return oss.str();
3002 }
3003
MayInlineInternal(const DexFile * inlined_from,const DexFile * inlined_into) const3004 bool CompilerDriver::MayInlineInternal(const DexFile* inlined_from,
3005 const DexFile* inlined_into) const {
3006 // We're not allowed to inline across dex files if we're the no-inline-from dex file.
3007 if (inlined_from != inlined_into &&
3008 compiler_options_->GetNoInlineFromDexFile() != nullptr &&
3009 ContainsElement(*compiler_options_->GetNoInlineFromDexFile(), inlined_from)) {
3010 return false;
3011 }
3012
3013 return true;
3014 }
3015
InitializeThreadPools()3016 void CompilerDriver::InitializeThreadPools() {
3017 size_t parallel_count = parallel_thread_count_ > 0 ? parallel_thread_count_ - 1 : 0;
3018 parallel_thread_pool_.reset(
3019 new ThreadPool("Compiler driver thread pool", parallel_count));
3020 single_thread_pool_.reset(new ThreadPool("Single-threaded Compiler driver thread pool", 0));
3021 }
3022
FreeThreadPools()3023 void CompilerDriver::FreeThreadPools() {
3024 parallel_thread_pool_.reset();
3025 single_thread_pool_.reset();
3026 }
3027
SetDexFilesForOatFile(const std::vector<const DexFile * > & dex_files)3028 void CompilerDriver::SetDexFilesForOatFile(const std::vector<const DexFile*>& dex_files) {
3029 dex_files_for_oat_file_ = dex_files;
3030 for (const DexFile* dex_file : dex_files) {
3031 if (!compiled_classes_.HaveDexFile(dex_file)) {
3032 compiled_classes_.AddDexFile(dex_file, dex_file->NumClassDefs());
3033 }
3034 }
3035 }
3036
CanAssumeVerified(ClassReference ref) const3037 bool CompilerDriver::CanAssumeVerified(ClassReference ref) const {
3038 mirror::Class::Status existing = mirror::Class::kStatusNotReady;
3039 compiled_classes_.Get(DexFileReference(ref.first, ref.second), &existing);
3040 return existing >= mirror::Class::kStatusVerified;
3041 }
3042
3043 } // namespace art
3044