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