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