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