blob: f8e2dff25ac264caca812c9ca96a41e4911d3dd5 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070019#include <unistd.h>
Andreas Gampeb0f370e2014-09-25 22:51:40 -070020#include <unordered_set>
Anwar Ghuloum67f99412013-08-12 14:19:48 -070021#include <vector>
Brian Carlstrom7940e442013-07-12 13:46:57 -070022
Mathieu Chartierab972ef2014-12-03 17:38:22 -080023#ifndef __APPLE__
24#include <malloc.h> // For mallinfo
25#endif
26
Narayan Kamathe3eae5e2016-10-27 11:47:30 +010027#include "android-base/strings.h"
28
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Andreas Gampef0446e92017-05-16 13:51:57 -070031#include "base/arena_allocator.h"
David Brazdild9c90372016-09-14 16:53:55 +010032#include "base/array_ref.h"
Vladimir Marko492a7fa2016-06-01 18:38:43 +010033#include "base/bit_vector.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070034#include "base/enums.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080035#include "base/logging.h" // For VLOG
Brian Carlstrom7940e442013-07-12 13:46:57 -070036#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080037#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010038#include "base/time_utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070039#include "base/timing_logger.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010040#include "class_linker-inl.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010041#include "compiled_method-inl.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000042#include "compiler.h"
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +000043#include "compiler_callbacks.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000044#include "compiler_driver-inl.h"
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -070045#include "dex/class_accessor-inl.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080046#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080047#include "dex/dex_file-inl.h"
48#include "dex/dex_file_annotations.h"
49#include "dex/dex_instruction-inl.h"
Andreas Gampe5eb0d382015-07-23 01:19:26 -070050#include "dex/dex_to_dex_compiler.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000051#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000052#include "dex/verified_method.h"
Steven Morelande431e272017-07-18 16:53:49 -070053#include "dex_compilation_unit.h"
Mark Mendellae9fd932014-02-10 16:14:35 -080054#include "driver/compiler_options.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070055#include "gc/accounting/card_table-inl.h"
56#include "gc/accounting/heap_bitmap.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070057#include "gc/space/image_space.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070058#include "gc/space/space.h"
Steven Morelande431e272017-07-18 16:53:49 -070059#include "handle_scope-inl.h"
60#include "intrinsics_enum.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010061#include "jni/jni_internal.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010062#include "linker/linker_patch.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070063#include "mirror/class-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070064#include "mirror/class_loader.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070065#include "mirror/dex_cache-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070066#include "mirror/object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080067#include "mirror/object-refvisitor-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070068#include "mirror/object_array-inl.h"
69#include "mirror/throwable.h"
Steven Morelande431e272017-07-18 16:53:49 -070070#include "object_lock.h"
David Sehr82d046e2018-04-23 08:14:19 -070071#include "profile/profile_compilation_info.h"
Steven Morelande431e272017-07-18 16:53:49 -070072#include "runtime.h"
Orion Hodson26ef34c2017-11-01 13:32:41 +000073#include "runtime_intrinsics.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070074#include "scoped_thread_state_change-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070075#include "thread.h"
Andreas Gampeb0f370e2014-09-25 22:51:40 -070076#include "thread_list.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070077#include "thread_pool.h"
Ian Rogers848871b2013-08-05 10:56:33 -070078#include "trampolines/trampoline_compiler.h"
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010079#include "transaction.h"
Mathieu Chartier93764b82017-07-17 14:51:53 -070080#include "utils/atomic_dex_ref_map-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000081#include "utils/dex_cache_arrays_layout-inl.h"
Andreas Gampee21dc3d2014-12-08 16:59:43 -080082#include "utils/swap_space.h"
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +000083#include "vdex_file.h"
Vladimir Marko2bc47802014-02-10 09:43:07 +000084#include "verifier/method_verifier-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070085#include "verifier/method_verifier.h"
Nicolas Geoffray08025182016-10-25 17:20:18 +010086#include "verifier/verifier_deps.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070087#include "verifier/verifier_enums.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070088
Brian Carlstrom7940e442013-07-12 13:46:57 -070089namespace art {
90
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070091static constexpr bool kTimeCompileMethod = !kIsDebugBuild;
92
Calin Juravle226501b2015-12-11 14:41:31 +000093// Print additional info during profile guided compilation.
94static constexpr bool kDebugProfileGuidedCompilation = false;
95
Chang Xing70f689d2017-06-08 17:16:12 -070096// Max encoded fields allowed for initializing app image. Hardcode the number for now
97// because 5000 should be large enough.
98static constexpr uint32_t kMaxEncodedFields = 5000;
99
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100static double Percentage(size_t x, size_t y) {
101 return 100.0 * (static_cast<double>(x)) / (static_cast<double>(x + y));
102}
103
104static void DumpStat(size_t x, size_t y, const char* str) {
105 if (x == 0 && y == 0) {
106 return;
107 }
Ian Rogerse732ef12013-10-09 15:22:24 -0700108 LOG(INFO) << Percentage(x, y) << "% of " << str << " for " << (x + y) << " cases";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109}
110
Vladimir Markof096aad2014-01-23 15:51:58 +0000111class CompilerDriver::AOTCompilationStats {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 public:
113 AOTCompilationStats()
114 : stats_lock_("AOT compilation statistics lock"),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 resolved_instance_fields_(0), unresolved_instance_fields_(0),
116 resolved_local_static_fields_(0), resolved_static_fields_(0), unresolved_static_fields_(0),
117 type_based_devirtualization_(0),
118 safe_casts_(0), not_safe_casts_(0) {
119 for (size_t i = 0; i <= kMaxInvokeType; i++) {
120 resolved_methods_[i] = 0;
121 unresolved_methods_[i] = 0;
122 virtual_made_direct_[i] = 0;
123 direct_calls_to_boot_[i] = 0;
124 direct_methods_to_boot_[i] = 0;
125 }
126 }
127
128 void Dump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 DumpStat(resolved_instance_fields_, unresolved_instance_fields_, "instance fields resolved");
130 DumpStat(resolved_local_static_fields_ + resolved_static_fields_, unresolved_static_fields_,
131 "static fields resolved");
132 DumpStat(resolved_local_static_fields_, resolved_static_fields_ + unresolved_static_fields_,
133 "static fields local to a class");
134 DumpStat(safe_casts_, not_safe_casts_, "check-casts removed based on type information");
135 // Note, the code below subtracts the stat value so that when added to the stat value we have
136 // 100% of samples. TODO: clean this up.
137 DumpStat(type_based_devirtualization_,
138 resolved_methods_[kVirtual] + unresolved_methods_[kVirtual] +
139 resolved_methods_[kInterface] + unresolved_methods_[kInterface] -
140 type_based_devirtualization_,
141 "virtual/interface calls made direct based on type information");
142
143 for (size_t i = 0; i <= kMaxInvokeType; i++) {
144 std::ostringstream oss;
145 oss << static_cast<InvokeType>(i) << " methods were AOT resolved";
146 DumpStat(resolved_methods_[i], unresolved_methods_[i], oss.str().c_str());
147 if (virtual_made_direct_[i] > 0) {
148 std::ostringstream oss2;
149 oss2 << static_cast<InvokeType>(i) << " methods made direct";
150 DumpStat(virtual_made_direct_[i],
151 resolved_methods_[i] + unresolved_methods_[i] - virtual_made_direct_[i],
152 oss2.str().c_str());
153 }
154 if (direct_calls_to_boot_[i] > 0) {
155 std::ostringstream oss2;
156 oss2 << static_cast<InvokeType>(i) << " method calls are direct into boot";
157 DumpStat(direct_calls_to_boot_[i],
158 resolved_methods_[i] + unresolved_methods_[i] - direct_calls_to_boot_[i],
159 oss2.str().c_str());
160 }
161 if (direct_methods_to_boot_[i] > 0) {
162 std::ostringstream oss2;
163 oss2 << static_cast<InvokeType>(i) << " method calls have methods in boot";
164 DumpStat(direct_methods_to_boot_[i],
165 resolved_methods_[i] + unresolved_methods_[i] - direct_methods_to_boot_[i],
166 oss2.str().c_str());
167 }
168 }
169 }
170
171// Allow lossy statistics in non-debug builds.
172#ifndef NDEBUG
173#define STATS_LOCK() MutexLock mu(Thread::Current(), stats_lock_)
174#else
175#define STATS_LOCK()
176#endif
177
Mathieu Chartier90443472015-07-16 20:32:27 -0700178 void ResolvedInstanceField() REQUIRES(!stats_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 STATS_LOCK();
180 resolved_instance_fields_++;
181 }
182
Mathieu Chartier90443472015-07-16 20:32:27 -0700183 void UnresolvedInstanceField() REQUIRES(!stats_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 STATS_LOCK();
185 unresolved_instance_fields_++;
186 }
187
Mathieu Chartier90443472015-07-16 20:32:27 -0700188 void ResolvedLocalStaticField() REQUIRES(!stats_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189 STATS_LOCK();
190 resolved_local_static_fields_++;
191 }
192
Mathieu Chartier90443472015-07-16 20:32:27 -0700193 void ResolvedStaticField() REQUIRES(!stats_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194 STATS_LOCK();
195 resolved_static_fields_++;
196 }
197
Mathieu Chartier90443472015-07-16 20:32:27 -0700198 void UnresolvedStaticField() REQUIRES(!stats_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 STATS_LOCK();
200 unresolved_static_fields_++;
201 }
202
203 // Indicate that type information from the verifier led to devirtualization.
Mathieu Chartier90443472015-07-16 20:32:27 -0700204 void PreciseTypeDevirtualization() REQUIRES(!stats_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 STATS_LOCK();
206 type_based_devirtualization_++;
207 }
208
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 // A check-cast could be eliminated due to verifier type analysis.
Mathieu Chartier90443472015-07-16 20:32:27 -0700210 void SafeCast() REQUIRES(!stats_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 STATS_LOCK();
212 safe_casts_++;
213 }
214
215 // A check-cast couldn't be eliminated due to verifier type analysis.
Mathieu Chartier90443472015-07-16 20:32:27 -0700216 void NotASafeCast() REQUIRES(!stats_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 STATS_LOCK();
218 not_safe_casts_++;
219 }
220
221 private:
222 Mutex stats_lock_;
223
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 size_t resolved_instance_fields_;
225 size_t unresolved_instance_fields_;
226
227 size_t resolved_local_static_fields_;
228 size_t resolved_static_fields_;
229 size_t unresolved_static_fields_;
230 // Type based devirtualization for invoke interface and virtual.
231 size_t type_based_devirtualization_;
232
233 size_t resolved_methods_[kMaxInvokeType + 1];
234 size_t unresolved_methods_[kMaxInvokeType + 1];
235 size_t virtual_made_direct_[kMaxInvokeType + 1];
236 size_t direct_calls_to_boot_[kMaxInvokeType + 1];
237 size_t direct_methods_to_boot_[kMaxInvokeType + 1];
238
239 size_t safe_casts_;
240 size_t not_safe_casts_;
241
242 DISALLOW_COPY_AND_ASSIGN(AOTCompilationStats);
243};
244
Jeff Haodcdc85b2015-12-04 14:06:18 -0800245CompilerDriver::CompilerDriver(
246 const CompilerOptions* compiler_options,
247 VerificationResults* verification_results,
Jeff Haodcdc85b2015-12-04 14:06:18 -0800248 Compiler::Kind compiler_kind,
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100249 HashSet<std::string>* image_classes,
Vladimir Marko944da602016-02-19 12:27:55 +0000250 size_t thread_count,
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +0000251 int swap_fd)
Calin Juravle226501b2015-12-11 14:41:31 +0000252 : compiler_options_(compiler_options),
Brian Carlstrom6449c622014-02-10 23:48:36 -0800253 verification_results_(verification_results),
Ian Rogers72d32622014-05-06 16:20:11 -0700254 compiler_(Compiler::Create(this, compiler_kind)),
Jeff Hao48699fb2015-04-06 14:21:37 -0700255 compiler_kind_(compiler_kind),
Vladimir Marko54159c62018-06-20 14:30:08 +0100256 image_classes_(std::move(image_classes)),
Andreas Gampef39208f2017-10-19 15:06:59 -0700257 number_of_soft_verifier_failures_(0),
Andreas Gampe6cf49e52015-03-05 13:08:45 -0800258 had_hard_verifier_failure_(false),
Andreas Gampeace0dc12016-01-20 13:33:13 -0800259 parallel_thread_count_(thread_count),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 stats_(new AOTCompilationStats),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700261 compiler_context_(nullptr),
Calin Juravle998c2162015-12-21 15:39:33 +0200262 compiled_method_storage_(swap_fd),
Vladimir Marko492a7fa2016-06-01 18:38:43 +0100263 max_arena_alloc_(0),
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800264 dex_to_dex_compiler_(this) {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800265 DCHECK(compiler_options_ != nullptr);
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700266
Ian Rogers72d32622014-05-06 16:20:11 -0700267 compiler_->Init();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268
Vladimir Markoaad75c62016-10-03 08:46:48 +0000269 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100270 CHECK(image_classes_ != nullptr) << "Expected image classes for boot image";
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800271 }
Andreas Gampecac31ad2017-11-06 20:01:17 -0800272
273 compiled_method_storage_.SetDedupeEnabled(compiler_options_->DeduplicateCode());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274}
275
276CompilerDriver::~CompilerDriver() {
Mathieu Chartier93764b82017-07-17 14:51:53 -0700277 compiled_methods_.Visit([this](const DexFileReference& ref ATTRIBUTE_UNUSED,
Mathieu Chartieracab8d42016-11-23 13:45:58 -0800278 CompiledMethod* method) {
279 if (method != nullptr) {
280 CompiledMethod::ReleaseSwapAllocatedCompiledMethod(this, method);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800281 }
Mathieu Chartieracab8d42016-11-23 13:45:58 -0800282 });
Ian Rogers72d32622014-05-06 16:20:11 -0700283 compiler_->UnInit();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284}
285
Vladimir Marko35831e82015-09-11 11:59:18 +0100286
Vladimir Markoa0431112018-06-25 09:32:54 +0100287#define CREATE_TRAMPOLINE(type, abi, offset) \
288 if (Is64BitInstructionSet(GetCompilerOptions().GetInstructionSet())) { \
289 return CreateTrampoline64(GetCompilerOptions().GetInstructionSet(), \
290 abi, \
291 type ## _ENTRYPOINT_OFFSET(PointerSize::k64, offset)); \
292 } else { \
293 return CreateTrampoline32(GetCompilerOptions().GetInstructionSet(), \
294 abi, \
295 type ## _ENTRYPOINT_OFFSET(PointerSize::k32, offset)); \
Ian Rogersdd7624d2014-03-14 17:43:00 -0700296 }
297
Vladimir Marko93205e32016-04-13 11:59:46 +0100298std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateJniDlsymLookup() const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700299 CREATE_TRAMPOLINE(JNI, kJniAbi, pDlsymLookup)
Ian Rogers848871b2013-08-05 10:56:33 -0700300}
301
Vladimir Marko93205e32016-04-13 11:59:46 +0100302std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickGenericJniTrampoline()
303 const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700304 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickGenericJniTrampoline)
Andreas Gampe2da88232014-02-27 12:26:20 -0800305}
306
Vladimir Marko93205e32016-04-13 11:59:46 +0100307std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickImtConflictTrampoline()
308 const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700309 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickImtConflictTrampoline)
Jeff Hao88474b42013-10-23 16:24:40 -0700310}
311
Vladimir Marko93205e32016-04-13 11:59:46 +0100312std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickResolutionTrampoline()
313 const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700314 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickResolutionTrampoline)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315}
316
Vladimir Marko93205e32016-04-13 11:59:46 +0100317std::unique_ptr<const std::vector<uint8_t>> CompilerDriver::CreateQuickToInterpreterBridge()
318 const {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700319 CREATE_TRAMPOLINE(QUICK, kQuickAbi, pQuickToInterpreterBridge)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320}
Ian Rogersdd7624d2014-03-14 17:43:00 -0700321#undef CREATE_TRAMPOLINE
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322
323void CompilerDriver::CompileAll(jobject class_loader,
Brian Carlstrom45602482013-07-21 22:07:55 -0700324 const std::vector<const DexFile*>& dex_files,
Ian Rogers3d504072014-03-01 09:16:49 -0800325 TimingLogger* timings) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 DCHECK(!Runtime::Current()->IsStarted());
Andreas Gampeace0dc12016-01-20 13:33:13 -0800327
328 InitializeThreadPools();
329
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000330 PreCompile(class_loader, dex_files, timings);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000331 if (GetCompilerOptions().IsBootImage()) {
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100332 // We don't need to setup the intrinsics for non boot image compilation, as
333 // those compilations will pick up a boot image that have the ArtMethod already
334 // set with the intrinsics flag.
Orion Hodson26ef34c2017-11-01 13:32:41 +0000335 InitializeIntrinsics();
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100336 }
Andreas Gampe740667a2015-09-15 17:55:06 -0700337 // Compile:
338 // 1) Compile all classes and methods enabled for compilation. May fall back to dex-to-dex
339 // compilation.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100340 if (GetCompilerOptions().IsAnyCompilationEnabled()) {
Andreas Gampeace0dc12016-01-20 13:33:13 -0800341 Compile(class_loader, dex_files, timings);
Andreas Gampe740667a2015-09-15 17:55:06 -0700342 }
Nicolas Geoffray2d8801f2017-11-28 15:50:07 +0000343 if (GetCompilerOptions().GetDumpStats()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 stats_->Dump();
345 }
Andreas Gampeace0dc12016-01-20 13:33:13 -0800346
347 FreeThreadPools();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348}
349
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800350static optimizer::DexToDexCompiler::CompilationLevel GetDexToDexCompilationLevel(
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700351 Thread* self, const CompilerDriver& driver, Handle<mirror::ClassLoader> class_loader,
352 const DexFile& dex_file, const DexFile::ClassDef& class_def)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700353 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffrayf3075272018-01-08 12:41:19 +0000354 // When the dex file is uncompressed in the APK, we do not generate a copy in the .vdex
355 // file. As a result, dex2oat will map the dex file read-only, and we only need to check
356 // that to know if we can do quickening.
357 if (dex_file.GetContainer() != nullptr && dex_file.GetContainer()->IsReadOnly()) {
358 return optimizer::DexToDexCompiler::CompilationLevel::kDontDexToDexCompile;
359 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800360 auto* const runtime = Runtime::Current();
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100361 DCHECK(driver.GetCompilerOptions().IsQuickeningCompilationEnabled());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 const char* descriptor = dex_file.GetClassDescriptor(class_def);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800363 ClassLinker* class_linker = runtime->GetClassLinker();
Vladimir Markoa8bba7d2018-05-30 15:18:48 +0100364 ObjPtr<mirror::Class> klass = class_linker->FindClass(self, descriptor, class_loader);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700365 if (klass == nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 CHECK(self->IsExceptionPending());
367 self->ClearException();
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800368 return optimizer::DexToDexCompiler::CompilationLevel::kDontDexToDexCompile;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700370 // DexToDex at the kOptimize level may introduce quickened opcodes, which replace symbolic
371 // references with actual offsets. We cannot re-verify such instructions.
372 //
373 // We store the verification information in the class status in the oat file, which the linker
374 // can validate (checksums) and use to skip load-time verification. It is thus safe to
375 // optimize when a class has been fully verified before.
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800376 optimizer::DexToDexCompiler::CompilationLevel max_level =
377 optimizer::DexToDexCompiler::CompilationLevel::kOptimize;
Alex Lighte40dd382017-01-25 17:44:53 -0800378 if (driver.GetCompilerOptions().GetDebuggable()) {
379 // We are debuggable so definitions of classes might be changed. We don't want to do any
380 // optimizations that could break that.
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800381 max_level = optimizer::DexToDexCompiler::CompilationLevel::kDontDexToDexCompile;
Alex Lighte40dd382017-01-25 17:44:53 -0800382 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700383 if (klass->IsVerified()) {
Sebastien Hertz75021222013-07-16 18:34:50 +0200384 // Class is verified so we can enable DEX-to-DEX compilation for performance.
Alex Lighte40dd382017-01-25 17:44:53 -0800385 return max_level;
Sebastien Hertz75021222013-07-16 18:34:50 +0200386 } else {
Andreas Gampe1a4bc7f2017-03-27 14:57:30 -0700387 // Class verification has failed: do not run DEX-to-DEX optimizations.
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800388 return optimizer::DexToDexCompiler::CompilationLevel::kDontDexToDexCompile;
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700389 }
390}
391
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800392static optimizer::DexToDexCompiler::CompilationLevel GetDexToDexCompilationLevel(
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700393 Thread* self,
394 const CompilerDriver& driver,
395 jobject jclass_loader,
396 const DexFile& dex_file,
397 const DexFile::ClassDef& class_def) {
398 ScopedObjectAccess soa(self);
399 StackHandleScope<1> hs(soa.Self());
400 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700401 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700402 return GetDexToDexCompilationLevel(self, driver, class_loader, dex_file, class_def);
403}
404
405// Does the runtime for the InstructionSet provide an implementation returned by
406// GetQuickGenericJniStub allowing down calls that aren't compiled using a JNI compiler?
407static bool InstructionSetHasGenericJniStub(InstructionSet isa) {
408 switch (isa) {
Vladimir Marko33bff252017-11-01 14:35:42 +0000409 case InstructionSet::kArm:
410 case InstructionSet::kArm64:
411 case InstructionSet::kThumb2:
412 case InstructionSet::kMips:
413 case InstructionSet::kMips64:
414 case InstructionSet::kX86:
415 case InstructionSet::kX86_64: return true;
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700416 default: return false;
417 }
418}
419
Andreas Gampe3c060ad2018-01-22 13:07:49 -0800420template <typename CompileFn>
421static void CompileMethodHarness(
422 Thread* self,
423 CompilerDriver* driver,
424 const DexFile::CodeItem* code_item,
425 uint32_t access_flags,
426 InvokeType invoke_type,
427 uint16_t class_def_idx,
428 uint32_t method_idx,
429 Handle<mirror::ClassLoader> class_loader,
430 const DexFile& dex_file,
431 optimizer::DexToDexCompiler::CompilationLevel dex_to_dex_compilation_level,
432 bool compilation_enabled,
433 Handle<mirror::DexCache> dex_cache,
434 CompileFn compile_fn) {
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700435 DCHECK(driver != nullptr);
Andreas Gampe3c060ad2018-01-22 13:07:49 -0800436 CompiledMethod* compiled_method;
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700437 uint64_t start_ns = kTimeCompileMethod ? NanoTime() : 0;
438 MethodReference method_ref(&dex_file, method_idx);
439
Andreas Gampe3c060ad2018-01-22 13:07:49 -0800440 compiled_method = compile_fn(self,
441 driver,
442 code_item,
443 access_flags,
444 invoke_type,
445 class_def_idx,
446 method_idx,
447 class_loader,
448 dex_file,
449 dex_to_dex_compilation_level,
450 compilation_enabled,
451 dex_cache);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700452
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700453 if (kTimeCompileMethod) {
454 uint64_t duration_ns = NanoTime() - start_ns;
455 if (duration_ns > MsToNs(driver->GetCompiler()->GetMaximumCompilationTimeBeforeWarning())) {
David Sehr709b0702016-10-13 09:12:37 -0700456 LOG(WARNING) << "Compilation of " << dex_file.PrettyMethod(method_idx)
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700457 << " took " << PrettyDuration(duration_ns);
458 }
459 }
460
461 if (compiled_method != nullptr) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100462 driver->AddCompiledMethod(method_ref, compiled_method);
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700463 }
464
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700465 if (self->IsExceptionPending()) {
466 ScopedObjectAccess soa(self);
David Sehr709b0702016-10-13 09:12:37 -0700467 LOG(FATAL) << "Unexpected exception compiling: " << dex_file.PrettyMethod(method_idx) << "\n"
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700468 << self->GetException()->Dump();
Sebastien Hertz75021222013-07-16 18:34:50 +0200469 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470}
471
Andreas Gampe3c060ad2018-01-22 13:07:49 -0800472static void CompileMethodDex2Dex(
473 Thread* self,
474 CompilerDriver* driver,
475 const DexFile::CodeItem* code_item,
476 uint32_t access_flags,
477 InvokeType invoke_type,
478 uint16_t class_def_idx,
479 uint32_t method_idx,
480 Handle<mirror::ClassLoader> class_loader,
481 const DexFile& dex_file,
482 optimizer::DexToDexCompiler::CompilationLevel dex_to_dex_compilation_level,
483 bool compilation_enabled,
484 Handle<mirror::DexCache> dex_cache) {
485 auto dex_2_dex_fn = [](Thread* self ATTRIBUTE_UNUSED,
486 CompilerDriver* driver,
487 const DexFile::CodeItem* code_item,
488 uint32_t access_flags,
489 InvokeType invoke_type,
490 uint16_t class_def_idx,
491 uint32_t method_idx,
492 Handle<mirror::ClassLoader> class_loader,
493 const DexFile& dex_file,
494 optimizer::DexToDexCompiler::CompilationLevel dex_to_dex_compilation_level,
495 bool compilation_enabled ATTRIBUTE_UNUSED,
496 Handle<mirror::DexCache> dex_cache ATTRIBUTE_UNUSED) -> CompiledMethod* {
497 DCHECK(driver != nullptr);
498 MethodReference method_ref(&dex_file, method_idx);
499
500 optimizer::DexToDexCompiler* const compiler = &driver->GetDexToDexCompiler();
501
502 if (compiler->ShouldCompileMethod(method_ref)) {
503 VerificationResults* results = driver->GetVerificationResults();
504 DCHECK(results != nullptr);
505 const VerifiedMethod* verified_method = results->GetVerifiedMethod(method_ref);
506 // Do not optimize if a VerifiedMethod is missing. SafeCast elision,
507 // for example, relies on it.
508 return compiler->CompileMethod(
509 code_item,
510 access_flags,
511 invoke_type,
512 class_def_idx,
513 method_idx,
514 class_loader,
515 dex_file,
516 (verified_method != nullptr)
517 ? dex_to_dex_compilation_level
518 : optimizer::DexToDexCompiler::CompilationLevel::kDontDexToDexCompile);
519 }
520 return nullptr;
521 };
522 CompileMethodHarness(self,
523 driver,
524 code_item,
525 access_flags,
526 invoke_type,
527 class_def_idx,
528 method_idx,
529 class_loader,
530 dex_file,
531 dex_to_dex_compilation_level,
532 compilation_enabled,
533 dex_cache,
534 dex_2_dex_fn);
535}
536
537static void CompileMethodQuick(
538 Thread* self,
539 CompilerDriver* driver,
540 const DexFile::CodeItem* code_item,
541 uint32_t access_flags,
542 InvokeType invoke_type,
543 uint16_t class_def_idx,
544 uint32_t method_idx,
545 Handle<mirror::ClassLoader> class_loader,
546 const DexFile& dex_file,
547 optimizer::DexToDexCompiler::CompilationLevel dex_to_dex_compilation_level,
548 bool compilation_enabled,
549 Handle<mirror::DexCache> dex_cache) {
550 auto quick_fn = [](
551 Thread* self,
552 CompilerDriver* driver,
553 const DexFile::CodeItem* code_item,
554 uint32_t access_flags,
555 InvokeType invoke_type,
556 uint16_t class_def_idx,
557 uint32_t method_idx,
558 Handle<mirror::ClassLoader> class_loader,
559 const DexFile& dex_file,
560 optimizer::DexToDexCompiler::CompilationLevel dex_to_dex_compilation_level,
561 bool compilation_enabled,
562 Handle<mirror::DexCache> dex_cache) {
563 DCHECK(driver != nullptr);
564 CompiledMethod* compiled_method = nullptr;
565 MethodReference method_ref(&dex_file, method_idx);
566
567 if ((access_flags & kAccNative) != 0) {
568 // Are we extracting only and have support for generic JNI down calls?
569 if (!driver->GetCompilerOptions().IsJniCompilationEnabled() &&
Vladimir Markoa0431112018-06-25 09:32:54 +0100570 InstructionSetHasGenericJniStub(driver->GetCompilerOptions().GetInstructionSet())) {
Andreas Gampe3c060ad2018-01-22 13:07:49 -0800571 // Leaving this empty will trigger the generic JNI version
572 } else {
573 // Query any JNI optimization annotations such as @FastNative or @CriticalNative.
574 access_flags |= annotations::GetNativeMethodAnnotationAccessFlags(
575 dex_file, dex_file.GetClassDef(class_def_idx), method_idx);
576
577 compiled_method = driver->GetCompiler()->JniCompile(
578 access_flags, method_idx, dex_file, dex_cache);
579 CHECK(compiled_method != nullptr);
580 }
581 } else if ((access_flags & kAccAbstract) != 0) {
582 // Abstract methods don't have code.
583 } else {
584 VerificationResults* results = driver->GetVerificationResults();
585 DCHECK(results != nullptr);
586 const VerifiedMethod* verified_method = results->GetVerifiedMethod(method_ref);
587 bool compile = compilation_enabled &&
588 // Basic checks, e.g., not <clinit>.
589 results->IsCandidateForCompilation(method_ref, access_flags) &&
590 // Did not fail to create VerifiedMethod metadata.
591 verified_method != nullptr &&
592 // Do not have failures that should punt to the interpreter.
593 !verified_method->HasRuntimeThrow() &&
594 (verified_method->GetEncounteredVerificationFailures() &
595 (verifier::VERIFY_ERROR_FORCE_INTERPRETER | verifier::VERIFY_ERROR_LOCKING)) == 0 &&
596 // Is eligable for compilation by methods-to-compile filter.
Andreas Gampe3c060ad2018-01-22 13:07:49 -0800597 driver->ShouldCompileBasedOnProfile(method_ref);
598
599 if (compile) {
600 // NOTE: if compiler declines to compile this method, it will return null.
601 compiled_method = driver->GetCompiler()->Compile(code_item,
602 access_flags,
603 invoke_type,
604 class_def_idx,
605 method_idx,
606 class_loader,
607 dex_file,
608 dex_cache);
609 }
610 if (compiled_method == nullptr &&
611 dex_to_dex_compilation_level !=
612 optimizer::DexToDexCompiler::CompilationLevel::kDontDexToDexCompile) {
613 DCHECK(!Runtime::Current()->UseJitCompilation());
614 // TODO: add a command-line option to disable DEX-to-DEX compilation ?
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800615 driver->GetDexToDexCompiler().MarkForCompilation(self, method_ref);
Andreas Gampe3c060ad2018-01-22 13:07:49 -0800616 }
617 }
618 return compiled_method;
619 };
620 CompileMethodHarness(self,
621 driver,
622 code_item,
623 access_flags,
624 invoke_type,
625 class_def_idx,
626 method_idx,
627 class_loader,
628 dex_file,
629 dex_to_dex_compilation_level,
630 compilation_enabled,
631 dex_cache,
632 quick_fn);
633}
634
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100635// Compile a single Method. (For testing only.)
636void CompilerDriver::CompileOne(Thread* self,
637 jobject class_loader,
638 const DexFile& dex_file,
639 uint16_t class_def_idx,
640 uint32_t method_idx,
641 uint32_t access_flags,
642 InvokeType invoke_type,
643 const DexFile::CodeItem* code_item,
644 Handle<mirror::DexCache> dex_cache,
645 Handle<mirror::ClassLoader> h_class_loader) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646 // Can we run DEX-to-DEX compiler on this class ?
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800647 optimizer::DexToDexCompiler::CompilationLevel dex_to_dex_compilation_level =
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700648 GetDexToDexCompilationLevel(self,
649 *this,
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100650 class_loader,
651 dex_file,
652 dex_file.GetClassDef(class_def_idx));
Andreas Gampe5eb0d382015-07-23 01:19:26 -0700653
Andreas Gampe3c060ad2018-01-22 13:07:49 -0800654 CompileMethodQuick(self,
655 this,
656 code_item,
657 access_flags,
658 invoke_type,
659 class_def_idx,
660 method_idx,
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100661 h_class_loader,
662 dex_file,
Andreas Gampe3c060ad2018-01-22 13:07:49 -0800663 dex_to_dex_compilation_level,
664 true,
665 dex_cache);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800667 const size_t num_methods = dex_to_dex_compiler_.NumCodeItemsToQuicken(self);
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800668 if (num_methods != 0) {
669 DCHECK_EQ(num_methods, 1u);
Andreas Gampe3c060ad2018-01-22 13:07:49 -0800670 CompileMethodDex2Dex(self,
671 this,
672 code_item,
673 access_flags,
674 invoke_type,
675 class_def_idx,
676 method_idx,
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100677 h_class_loader,
678 dex_file,
Andreas Gampe3c060ad2018-01-22 13:07:49 -0800679 dex_to_dex_compilation_level,
680 true,
681 dex_cache);
Mathieu Chartiera79efdb2018-01-18 16:31:01 -0800682 dex_to_dex_compiler_.ClearState();
Vladimir Marko492a7fa2016-06-01 18:38:43 +0100683 }
Mathieu Chartier2535abe2015-02-17 10:38:49 -0800684}
685
Andreas Gampeace0dc12016-01-20 13:33:13 -0800686void CompilerDriver::Resolve(jobject class_loader,
687 const std::vector<const DexFile*>& dex_files,
688 TimingLogger* timings) {
689 // Resolution allocates classes and needs to run single-threaded to be deterministic.
690 bool force_determinism = GetCompilerOptions().IsForceDeterminism();
691 ThreadPool* resolve_thread_pool = force_determinism
692 ? single_thread_pool_.get()
693 : parallel_thread_pool_.get();
694 size_t resolve_thread_count = force_determinism ? 1U : parallel_thread_count_;
695
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 for (size_t i = 0; i != dex_files.size(); ++i) {
697 const DexFile* dex_file = dex_files[i];
Kenny Rootd5185342014-05-13 14:47:05 -0700698 CHECK(dex_file != nullptr);
Andreas Gampeace0dc12016-01-20 13:33:13 -0800699 ResolveDexFile(class_loader,
700 *dex_file,
701 dex_files,
702 resolve_thread_pool,
703 resolve_thread_count,
704 timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700705 }
706}
707
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -0700708void CompilerDriver::ResolveConstStrings(const std::vector<const DexFile*>& dex_files,
709 bool only_startup_strings,
710 TimingLogger* timings) {
Nicolas Geoffray571d2342016-10-12 13:03:15 +0000711 ScopedObjectAccess soa(Thread::Current());
712 StackHandleScope<1> hs(soa.Self());
713 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
714 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
Mathieu Chartier22752772018-10-18 14:18:59 -0700715 size_t num_instructions = 0u;
Nicolas Geoffray571d2342016-10-12 13:03:15 +0000716
Andreas Gampeace0dc12016-01-20 13:33:13 -0800717 for (const DexFile* dex_file : dex_files) {
Vladimir Markocd556b02017-02-03 11:47:34 +0000718 dex_cache.Assign(class_linker->FindDexCache(soa.Self(), *dex_file));
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700719 if (only_startup_strings) {
720 // When resolving startup strings, create the preresolved strings array.
721 dex_cache->AddPreResolvedStringsArray();
722 }
Andreas Gampeace0dc12016-01-20 13:33:13 -0800723 TimingLogger::ScopedTiming t("Resolve const-string Strings", timings);
724
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700725 for (ClassAccessor accessor : dex_file->GetClasses()) {
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -0700726 if (!IsClassToCompile(accessor.GetDescriptor())) {
Andreas Gampeace0dc12016-01-20 13:33:13 -0800727 // Compilation is skipped, do not resolve const-string in code of this class.
Vladimir Marko175e7862018-03-27 09:03:13 +0000728 // FIXME: Make sure that inlining honors this. b/26687569
Andreas Gampeace0dc12016-01-20 13:33:13 -0800729 continue;
730 }
Mathieu Chartier22752772018-10-18 14:18:59 -0700731
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +0000732 const ProfileCompilationInfo* profile_compilation_info =
733 GetCompilerOptions().GetProfileCompilationInfo();
Mathieu Chartier22752772018-10-18 14:18:59 -0700734 const bool is_startup_class =
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +0000735 profile_compilation_info != nullptr &&
736 profile_compilation_info->ContainsClass(*dex_file, accessor.GetClassIdx());
Mathieu Chartier22752772018-10-18 14:18:59 -0700737
Mathieu Chartier0d896bd2018-05-25 00:20:27 -0700738 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
Mathieu Chartier22752772018-10-18 14:18:59 -0700739 const bool is_clinit = (method.GetAccessFlags() & kAccConstructor) != 0 &&
740 (method.GetAccessFlags() & kAccStatic) != 0;
741 const bool is_startup_clinit = is_startup_class && is_clinit;
742
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -0700743 if (only_startup_strings &&
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +0000744 profile_compilation_info != nullptr &&
745 (!profile_compilation_info->GetMethodHotness(method.GetReference()).IsStartup() &&
Mathieu Chartier22752772018-10-18 14:18:59 -0700746 !is_startup_clinit)) {
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -0700747 continue;
748 }
749
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700750 // Resolve const-strings in the code. Done to have deterministic allocation behavior. Right
751 // now this is single-threaded for simplicity.
752 // TODO: Collect the relevant string indices in parallel, then allocate them sequentially
753 // in a stable order.
754 for (const DexInstructionPcPair& inst : method.GetInstructions()) {
755 switch (inst->Opcode()) {
756 case Instruction::CONST_STRING:
757 case Instruction::CONST_STRING_JUMBO: {
758 dex::StringIndex string_index((inst->Opcode() == Instruction::CONST_STRING)
759 ? inst->VRegB_21c()
760 : inst->VRegB_31c());
761 ObjPtr<mirror::String> string = class_linker->ResolveString(string_index, dex_cache);
762 CHECK(string != nullptr) << "Could not allocate a string when forcing determinism";
Mathieu Chartier1ca718e2018-10-23 12:55:34 -0700763 if (only_startup_strings) {
764 dex_cache->GetPreResolvedStrings()[string_index.index_] =
765 GcRoot<mirror::String>(string);
766 }
Mathieu Chartier22752772018-10-18 14:18:59 -0700767 ++num_instructions;
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700768 break;
769 }
Andreas Gampeace0dc12016-01-20 13:33:13 -0800770
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700771 default:
772 break;
773 }
774 }
Mathieu Chartier0d896bd2018-05-25 00:20:27 -0700775 }
Vladimir Marko175e7862018-03-27 09:03:13 +0000776 }
777 }
Mathieu Chartier22752772018-10-18 14:18:59 -0700778 VLOG(compiler) << "Resolved " << num_instructions << " const string instructions";
Vladimir Marko175e7862018-03-27 09:03:13 +0000779}
780
781// Initialize type check bit strings for check-cast and instance-of in the code. Done to have
782// deterministic allocation behavior. Right now this is single-threaded for simplicity.
783// TODO: Collect the relevant type indices in parallel, then process them sequentially in a
784// stable order.
785
786static void InitializeTypeCheckBitstrings(CompilerDriver* driver,
787 ClassLinker* class_linker,
788 Handle<mirror::DexCache> dex_cache,
789 const DexFile& dex_file,
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700790 const ClassAccessor::Method& method)
Vladimir Marko175e7862018-03-27 09:03:13 +0000791 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700792 for (const DexInstructionPcPair& inst : method.GetInstructions()) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000793 switch (inst->Opcode()) {
794 case Instruction::CHECK_CAST:
795 case Instruction::INSTANCE_OF: {
796 dex::TypeIndex type_index(
797 (inst->Opcode() == Instruction::CHECK_CAST) ? inst->VRegB_21c() : inst->VRegC_22c());
798 const char* descriptor = dex_file.StringByTypeIdx(type_index);
799 // We currently do not use the bitstring type check for array or final (including
800 // primitive) classes. We may reconsider this in future if it's deemed to be beneficial.
801 // And we cannot use it for classes outside the boot image as we do not know the runtime
802 // value of their bitstring when compiling (it may not even get assigned at runtime).
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100803 if (descriptor[0] == 'L' && driver->GetCompilerOptions().IsImageClass(descriptor)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000804 ObjPtr<mirror::Class> klass =
805 class_linker->LookupResolvedType(type_index,
806 dex_cache.Get(),
807 /* class_loader */ nullptr);
808 CHECK(klass != nullptr) << descriptor << " should have been previously resolved.";
809 // Now assign the bitstring if the class is not final. Keep this in sync with sharpening.
810 if (!klass->IsFinal()) {
811 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
812 SubtypeCheck<ObjPtr<mirror::Class>>::EnsureAssigned(klass);
813 }
Andreas Gampeace0dc12016-01-20 13:33:13 -0800814 }
Vladimir Marko175e7862018-03-27 09:03:13 +0000815 break;
816 }
817
818 default:
819 break;
820 }
821 }
822}
823
824static void InitializeTypeCheckBitstrings(CompilerDriver* driver,
825 const std::vector<const DexFile*>& dex_files,
826 TimingLogger* timings) {
827 ScopedObjectAccess soa(Thread::Current());
828 StackHandleScope<1> hs(soa.Self());
829 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
830 MutableHandle<mirror::DexCache> dex_cache(hs.NewHandle<mirror::DexCache>(nullptr));
831
832 for (const DexFile* dex_file : dex_files) {
833 dex_cache.Assign(class_linker->FindDexCache(soa.Self(), *dex_file));
834 TimingLogger::ScopedTiming t("Initialize type check bitstrings", timings);
835
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700836 for (ClassAccessor accessor : dex_file->GetClasses()) {
837 if (!driver->IsClassToCompile(accessor.GetDescriptor())) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000838 // Compilation is skipped, do not look for type checks in code of this class.
839 // FIXME: Make sure that inlining honors this. b/26687569
840 continue;
841 }
842
843 // Direct and virtual methods.
Mathieu Chartier0d896bd2018-05-25 00:20:27 -0700844 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700845 InitializeTypeCheckBitstrings(driver, class_linker, dex_cache, *dex_file, method);
Mathieu Chartier0d896bd2018-05-25 00:20:27 -0700846 }
Andreas Gampeace0dc12016-01-20 13:33:13 -0800847 }
848 }
849}
850
851inline void CompilerDriver::CheckThreadPools() {
852 DCHECK(parallel_thread_pool_ != nullptr);
853 DCHECK(single_thread_pool_ != nullptr);
854}
855
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +0000856static void EnsureVerifiedOrVerifyAtRuntime(jobject jclass_loader,
857 const std::vector<const DexFile*>& dex_files) {
858 ScopedObjectAccess soa(Thread::Current());
859 StackHandleScope<2> hs(soa.Self());
860 Handle<mirror::ClassLoader> class_loader(
861 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
862 MutableHandle<mirror::Class> cls(hs.NewHandle<mirror::Class>(nullptr));
863 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
864
865 for (const DexFile* dex_file : dex_files) {
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -0700866 for (ClassAccessor accessor : dex_file->GetClasses()) {
867 cls.Assign(class_linker->FindClass(soa.Self(), accessor.GetDescriptor(), class_loader));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800868 if (cls == nullptr) {
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +0000869 soa.Self()->ClearException();
870 } else if (&cls->GetDexFile() == dex_file) {
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +0000871 DCHECK(cls->IsErroneous() || cls->IsVerified() || cls->ShouldVerifyAtRuntime())
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +0000872 << cls->PrettyClass()
873 << " " << cls->GetStatus();
874 }
875 }
876 }
877}
878
Andreas Gampeace0dc12016-01-20 13:33:13 -0800879void CompilerDriver::PreCompile(jobject class_loader,
880 const std::vector<const DexFile*>& dex_files,
881 TimingLogger* timings) {
882 CheckThreadPools();
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100883
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100884 VLOG(compiler) << "Before precompile " << GetMemoryUsageString(false);
885
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100886 compiled_classes_.AddDexFiles(GetCompilerOptions().GetDexFilesForOatFile());
887 dex_to_dex_compiler_.SetDexFiles(GetCompilerOptions().GetDexFilesForOatFile());
888
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100889 // Precompile:
890 // 1) Load image classes.
891 // 2) Resolve all classes.
892 // 3) For deterministic boot image, resolve strings for const-string instructions.
893 // 4) Attempt to verify all classes.
894 // 5) Attempt to initialize image classes, and trivially initialized classes.
895 // 6) Update the set of image classes.
896 // 7) For deterministic boot image, initialize bitstrings for type checking.
Andreas Gampeace0dc12016-01-20 13:33:13 -0800897
Brian Carlstrom7940e442013-07-12 13:46:57 -0700898 LoadImageClasses(timings);
Andreas Gampe8d295f82015-01-20 14:50:21 -0800899 VLOG(compiler) << "LoadImageClasses: " << GetMemoryUsageString(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100901 if (compiler_options_->IsAnyCompilationEnabled()) {
Mathieu Chartier88b74b62017-07-16 17:08:19 -0700902 // Avoid adding the dex files in the case where we aren't going to add compiled methods.
903 // This reduces RAM usage for this case.
904 for (const DexFile* dex_file : dex_files) {
905 // Can be already inserted if the caller is CompileOne. This happens for gtests.
906 if (!compiled_methods_.HaveDexFile(dex_file)) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700907 compiled_methods_.AddDexFile(dex_file);
Mathieu Chartier88b74b62017-07-16 17:08:19 -0700908 }
909 }
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100910 // Resolve eagerly to prepare for compilation.
Andreas Gampeace0dc12016-01-20 13:33:13 -0800911 Resolve(class_loader, dex_files, timings);
Mathieu Chartiere86deef2015-03-19 13:43:37 -0700912 VLOG(compiler) << "Resolve: " << GetMemoryUsageString(false);
913 }
914
Nicolas Geoffray60ca9492016-12-20 21:15:00 +0000915 if (compiler_options_->AssumeClassesAreVerified()) {
Mathieu Chartierab972ef2014-12-03 17:38:22 -0800916 VLOG(compiler) << "Verify none mode specified, skipping verification.";
Andreas Gampeace0dc12016-01-20 13:33:13 -0800917 SetVerified(class_loader, dex_files, timings);
Mathieu Chartiere86deef2015-03-19 13:43:37 -0700918 }
919
Nicolas Geoffray60ca9492016-12-20 21:15:00 +0000920 if (!compiler_options_->IsVerificationEnabled()) {
Jeff Hao4a200f52014-04-01 14:58:49 -0700921 return;
922 }
923
Vladimir Markoaad75c62016-10-03 08:46:48 +0000924 if (GetCompilerOptions().IsForceDeterminism() && GetCompilerOptions().IsBootImage()) {
Andreas Gampeace0dc12016-01-20 13:33:13 -0800925 // Resolve strings from const-string. Do this now to have a deterministic image.
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -0700926 ResolveConstStrings(dex_files, /*only_startup_strings=*/ false, timings);
Andreas Gampeace0dc12016-01-20 13:33:13 -0800927 VLOG(compiler) << "Resolve const-strings: " << GetMemoryUsageString(false);
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -0700928 } else if (GetCompilerOptions().ResolveStartupConstStrings()) {
929 ResolveConstStrings(dex_files, /*only_startup_strings=*/ true, timings);
Andreas Gampeace0dc12016-01-20 13:33:13 -0800930 }
931
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +0000932 Verify(class_loader, dex_files, timings);
Andreas Gampe8d295f82015-01-20 14:50:21 -0800933 VLOG(compiler) << "Verify: " << GetMemoryUsageString(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700934
Andreas Gampe6cf49e52015-03-05 13:08:45 -0800935 if (had_hard_verifier_failure_ && GetCompilerOptions().AbortOnHardVerifierFailure()) {
Andreas Gampefdfb4d42017-07-27 12:12:22 -0700936 // Avoid dumping threads. Even if we shut down the thread pools, there will still be three
937 // instances of this thread's stack.
938 LOG(FATAL_WITHOUT_ABORT) << "Had a hard failure verifying all classes, and was asked to abort "
939 << "in such situations. Please check the log.";
Andreas Gampe56c9b7c2018-02-27 18:49:26 -0800940 _exit(1);
Andreas Gampef39208f2017-10-19 15:06:59 -0700941 } else if (number_of_soft_verifier_failures_ > 0 &&
942 GetCompilerOptions().AbortOnSoftVerifierFailure()) {
943 LOG(FATAL_WITHOUT_ABORT) << "Had " << number_of_soft_verifier_failures_ << " soft failure(s) "
944 << "verifying all classes, and was asked to abort in such situations. "
945 << "Please check the log.";
Andreas Gampe56c9b7c2018-02-27 18:49:26 -0800946 _exit(1);
Andreas Gampe6cf49e52015-03-05 13:08:45 -0800947 }
948
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100949 if (compiler_options_->IsAnyCompilationEnabled()) {
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +0000950 if (kIsDebugBuild) {
951 EnsureVerifiedOrVerifyAtRuntime(class_loader, dex_files);
952 }
Nicolas Geoffrayade72d62016-12-15 13:20:02 +0000953 InitializeClasses(class_loader, dex_files, timings);
954 VLOG(compiler) << "InitializeClasses: " << GetMemoryUsageString(false);
955 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700956
957 UpdateImageClasses(timings);
Andreas Gampe8d295f82015-01-20 14:50:21 -0800958 VLOG(compiler) << "UpdateImageClasses: " << GetMemoryUsageString(false);
Vladimir Marko175e7862018-03-27 09:03:13 +0000959
960 if (kBitstringSubtypeCheckEnabled &&
961 GetCompilerOptions().IsForceDeterminism() && GetCompilerOptions().IsBootImage()) {
962 // Initialize type check bit string used by check-cast and instanceof.
963 // Do this now to have a deterministic image.
964 // Note: This is done after UpdateImageClasses() at it relies on the image classes to be final.
965 InitializeTypeCheckBitstrings(this, dex_files, timings);
966 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700967}
968
Andreas Gampe4bf3ae92014-11-11 13:28:29 -0800969bool CompilerDriver::IsClassToCompile(const char* descriptor) const {
Andreas Gampeb1fcead2015-04-20 18:53:51 -0700970 if (classes_to_compile_ == nullptr) {
971 return true;
972 }
Vladimir Marko54159c62018-06-20 14:30:08 +0100973 return classes_to_compile_->find(StringPiece(descriptor)) != classes_to_compile_->end();
Andreas Gampe4bf3ae92014-11-11 13:28:29 -0800974}
975
Calin Juravle226501b2015-12-11 14:41:31 +0000976bool CompilerDriver::ShouldCompileBasedOnProfile(const MethodReference& method_ref) const {
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800977 // Profile compilation info may be null if no profile is passed.
Mathieu Chartierd0af56c2017-02-17 12:56:25 -0800978 if (!CompilerFilter::DependsOnProfile(compiler_options_->GetCompilerFilter())) {
979 // Use the compiler filter instead of the presence of profile_compilation_info_ since
980 // we may want to have full speed compilation along with profile based layout optimizations.
Calin Juravle226501b2015-12-11 14:41:31 +0000981 return true;
982 }
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800983 // If we are using a profile filter but do not have a profile compilation info, compile nothing.
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +0000984 const ProfileCompilationInfo* profile_compilation_info =
985 GetCompilerOptions().GetProfileCompilationInfo();
986 if (profile_compilation_info == nullptr) {
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800987 return false;
988 }
Mathieu Chartier7b135c82017-06-05 12:54:01 -0700989 // Compile only hot methods, it is the profile saver's job to decide what startup methods to mark
990 // as hot.
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +0000991 bool result = profile_compilation_info->GetMethodHotness(method_ref).IsHot();
Calin Juravle226501b2015-12-11 14:41:31 +0000992
993 if (kDebugProfileGuidedCompilation) {
994 LOG(INFO) << "[ProfileGuidedCompilation] "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700995 << (result ? "Compiled" : "Skipped") << " method:" << method_ref.PrettyMethod(true);
Calin Juravle226501b2015-12-11 14:41:31 +0000996 }
997 return result;
998}
999
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001000class ResolveCatchBlockExceptionsClassVisitor : public ClassVisitor {
1001 public:
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001002 ResolveCatchBlockExceptionsClassVisitor() : classes_() {}
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001003
Roland Levillainf73caca2018-08-24 17:19:07 +01001004 bool operator()(ObjPtr<mirror::Class> c) override REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001005 classes_.push_back(c);
Mathieu Chartiere4275c02015-08-06 15:34:15 -07001006 return true;
1007 }
1008
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001009 void FindExceptionTypesToResolve(
1010 std::set<std::pair<dex::TypeIndex, const DexFile*>>* exceptions_to_resolve)
Vladimir Markobfb80d22017-02-14 14:08:12 +00001011 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001012 const auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1013 for (ObjPtr<mirror::Class> klass : classes_) {
1014 for (ArtMethod& method : klass->GetMethods(pointer_size)) {
1015 FindExceptionTypesToResolveForMethod(&method, exceptions_to_resolve);
1016 }
1017 }
1018 }
1019
1020 private:
1021 void FindExceptionTypesToResolveForMethod(
1022 ArtMethod* method,
1023 std::set<std::pair<dex::TypeIndex, const DexFile*>>* exceptions_to_resolve)
1024 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001025 if (method->GetCodeItem() == nullptr) {
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001026 return; // native or abstract method
Brian Carlstrom7940e442013-07-12 13:46:57 -07001027 }
David Sehr0225f8e2018-01-31 08:52:24 +00001028 CodeItemDataAccessor accessor(method->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001029 if (accessor.TriesSize() == 0) {
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001030 return; // nothing to process
1031 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001032 const uint8_t* encoded_catch_handler_list = accessor.GetCatchHandlerData();
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001033 size_t num_encoded_catch_handlers = DecodeUnsignedLeb128(&encoded_catch_handler_list);
1034 for (size_t i = 0; i < num_encoded_catch_handlers; i++) {
1035 int32_t encoded_catch_handler_size = DecodeSignedLeb128(&encoded_catch_handler_list);
1036 bool has_catch_all = false;
1037 if (encoded_catch_handler_size <= 0) {
1038 encoded_catch_handler_size = -encoded_catch_handler_size;
1039 has_catch_all = true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001040 }
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001041 for (int32_t j = 0; j < encoded_catch_handler_size; j++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001042 dex::TypeIndex encoded_catch_handler_handlers_type_idx =
1043 dex::TypeIndex(DecodeUnsignedLeb128(&encoded_catch_handler_list));
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001044 // Add to set of types to resolve if not already in the dex cache resolved types
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001045 if (!method->IsResolvedTypeIdx(encoded_catch_handler_handlers_type_idx)) {
1046 exceptions_to_resolve->emplace(encoded_catch_handler_handlers_type_idx,
1047 method->GetDexFile());
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001048 }
1049 // ignore address associated with catch handler
1050 DecodeUnsignedLeb128(&encoded_catch_handler_list);
1051 }
1052 if (has_catch_all) {
1053 // ignore catch all address
1054 DecodeUnsignedLeb128(&encoded_catch_handler_list);
1055 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001056 }
1057 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001058
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001059 std::vector<ObjPtr<mirror::Class>> classes_;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001060};
1061
1062class RecordImageClassesVisitor : public ClassVisitor {
1063 public:
Vladimir Marko54159c62018-06-20 14:30:08 +01001064 explicit RecordImageClassesVisitor(HashSet<std::string>* image_classes)
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001065 : image_classes_(image_classes) {}
1066
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001067 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001068 std::string temp;
1069 image_classes_->insert(klass->GetDescriptor(&temp));
1070 return true;
1071 }
1072
1073 private:
Vladimir Marko54159c62018-06-20 14:30:08 +01001074 HashSet<std::string>* const image_classes_;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001075};
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076
1077// Make a list of descriptors for classes to include in the image
Mathieu Chartier90443472015-07-16 20:32:27 -07001078void CompilerDriver::LoadImageClasses(TimingLogger* timings) {
Kenny Rootd5185342014-05-13 14:47:05 -07001079 CHECK(timings != nullptr);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001080 if (!GetCompilerOptions().IsBootImage()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001081 return;
1082 }
1083
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001084 TimingLogger::ScopedTiming t("LoadImageClasses", timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 // Make a first class to load all classes explicitly listed in the file
1086 Thread* self = Thread::Current();
1087 ScopedObjectAccess soa(self);
1088 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Markodc4bcce2018-06-21 16:15:42 +01001089 CHECK(image_classes_ != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001090 for (auto it = image_classes_->begin(), end = image_classes_->end(); it != end;) {
Vladimir Markoe9c36b32013-11-21 15:49:16 +00001091 const std::string& descriptor(*it);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001092 StackHandleScope<1> hs(self);
1093 Handle<mirror::Class> klass(
1094 hs.NewHandle(class_linker->FindSystemClass(self, descriptor.c_str())));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001095 if (klass == nullptr) {
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001096 VLOG(compiler) << "Failed to find class " << descriptor;
Vladimir Marko54159c62018-06-20 14:30:08 +01001097 it = image_classes_->erase(it);
Ian Rogersa436fde2013-08-27 23:34:06 -07001098 self->ClearException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001099 } else {
1100 ++it;
1101 }
1102 }
1103
1104 // Resolve exception classes referenced by the loaded classes. The catch logic assumes
1105 // exceptions are resolved by the verifier when there is a catch block in an interested method.
1106 // Do this here so that exception classes appear to have been specified image classes.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001107 std::set<std::pair<dex::TypeIndex, const DexFile*>> unresolved_exception_types;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001108 StackHandleScope<1> hs(self);
1109 Handle<mirror::Class> java_lang_Throwable(
1110 hs.NewHandle(class_linker->FindSystemClass(self, "Ljava/lang/Throwable;")));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001111 do {
1112 unresolved_exception_types.clear();
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001113 {
1114 // Thread suspension is not allowed while ResolveCatchBlockExceptionsClassVisitor
1115 // is using a std::vector<ObjPtr<mirror::Class>>.
1116 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
1117 ResolveCatchBlockExceptionsClassVisitor visitor;
1118 class_linker->VisitClasses(&visitor);
1119 visitor.FindExceptionTypesToResolve(&unresolved_exception_types);
1120 }
Andreas Gampea5b09a62016-11-17 15:21:22 -08001121 for (const auto& exception_type : unresolved_exception_types) {
1122 dex::TypeIndex exception_type_idx = exception_type.first;
Mathieu Chartier02e25112013-08-14 16:14:24 -07001123 const DexFile* dex_file = exception_type.second;
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001124 StackHandleScope<1> hs2(self);
Mathieu Chartierf284d442016-06-02 11:48:30 -07001125 Handle<mirror::DexCache> dex_cache(hs2.NewHandle(class_linker->RegisterDexFile(*dex_file,
1126 nullptr)));
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001127 ObjPtr<mirror::Class> klass =
Andreas Gampefa4333d2017-02-14 11:10:34 -08001128 (dex_cache != nullptr)
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001129 ? class_linker->ResolveType(exception_type_idx,
Vladimir Markocd556b02017-02-03 11:47:34 +00001130 dex_cache,
1131 ScopedNullHandle<mirror::ClassLoader>())
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001132 : nullptr;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001133 if (klass == nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001134 const DexFile::TypeId& type_id = dex_file->GetTypeId(exception_type_idx);
1135 const char* descriptor = dex_file->GetTypeDescriptor(type_id);
1136 LOG(FATAL) << "Failed to resolve class " << descriptor;
1137 }
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001138 DCHECK(java_lang_Throwable->IsAssignableFrom(klass));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001139 }
1140 // Resolving exceptions may load classes that reference more exceptions, iterate until no
1141 // more are found
1142 } while (!unresolved_exception_types.empty());
1143
1144 // We walk the roots looking for classes so that we'll pick up the
1145 // above classes plus any classes them depend on such super
1146 // classes, interfaces, and the required ClassLinker roots.
Vladimir Markodc4bcce2018-06-21 16:15:42 +01001147 RecordImageClassesVisitor visitor(image_classes_);
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001148 class_linker->VisitClasses(&visitor);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001149
Vladimir Marko54159c62018-06-20 14:30:08 +01001150 CHECK(!image_classes_->empty());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001151}
1152
Vladimir Marko19a4d372016-12-08 14:41:46 +00001153static void MaybeAddToImageClasses(Thread* self,
1154 ObjPtr<mirror::Class> klass,
Vladimir Marko54159c62018-06-20 14:30:08 +01001155 HashSet<std::string>* image_classes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001156 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001157 DCHECK_EQ(self, Thread::Current());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001158 StackHandleScope<1> hs(self);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001159 std::string temp;
Andreas Gampe542451c2016-07-26 09:02:02 -07001160 const PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001161 while (!klass->IsObjectClass()) {
Ian Rogers1ff3c982014-08-12 02:30:58 -07001162 const char* descriptor = klass->GetDescriptor(&temp);
Vladimir Marko54159c62018-06-20 14:30:08 +01001163 if (image_classes->find(StringPiece(descriptor)) != image_classes->end()) {
1164 break; // Previously inserted.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001165 }
Vladimir Marko54159c62018-06-20 14:30:08 +01001166 image_classes->insert(descriptor);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001167 VLOG(compiler) << "Adding " << descriptor << " to image classes";
Vladimir Marko19a4d372016-12-08 14:41:46 +00001168 for (size_t i = 0, num_interfaces = klass->NumDirectInterfaces(); i != num_interfaces; ++i) {
1169 ObjPtr<mirror::Class> interface = mirror::Class::GetDirectInterface(self, klass, i);
1170 DCHECK(interface != nullptr);
1171 MaybeAddToImageClasses(self, interface, image_classes);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001172 }
Vladimir Marko19a4d372016-12-08 14:41:46 +00001173 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
1174 MaybeAddToImageClasses(self, m.GetDeclaringClass(), image_classes);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001175 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001176 if (klass->IsArrayClass()) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001177 MaybeAddToImageClasses(self, klass->GetComponentType(), image_classes);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001178 }
Andreas Gampe98104992018-10-16 12:49:47 -07001179 klass = klass->GetSuperClass();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001180 }
1181}
1182
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001183// Keeps all the data for the update together. Also doubles as the reference visitor.
1184// Note: we can use object pointers because we suspend all threads.
1185class ClinitImageUpdate {
1186 public:
Mathieu Chartier31e88222016-10-14 18:43:19 -07001187 static ClinitImageUpdate* Create(VariableSizedHandleScope& hs,
Vladimir Marko54159c62018-06-20 14:30:08 +01001188 HashSet<std::string>* image_class_descriptors,
Mathieu Chartier31e88222016-10-14 18:43:19 -07001189 Thread* self,
1190 ClassLinker* linker) {
1191 std::unique_ptr<ClinitImageUpdate> res(new ClinitImageUpdate(hs,
1192 image_class_descriptors,
1193 self,
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001194 linker));
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001195 return res.release();
1196 }
1197
1198 ~ClinitImageUpdate() {
1199 // Allow others to suspend again.
1200 self_->EndAssertNoThreadSuspension(old_cause_);
1201 }
1202
1203 // Visitor for VisitReferences.
Mathieu Chartier31e88222016-10-14 18:43:19 -07001204 void operator()(ObjPtr<mirror::Object> object,
1205 MemberOffset field_offset,
1206 bool /* is_static */) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001207 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001208 mirror::Object* ref = object->GetFieldObject<mirror::Object>(field_offset);
1209 if (ref != nullptr) {
1210 VisitClinitClassesObject(ref);
1211 }
1212 }
1213
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001214 // java.lang.ref.Reference visitor for VisitReferences.
Mathieu Chartier31e88222016-10-14 18:43:19 -07001215 void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED,
1216 ObjPtr<mirror::Reference> ref ATTRIBUTE_UNUSED) const {}
Mathieu Chartierda7c6502015-07-23 16:01:26 -07001217
1218 // Ignore class native roots.
1219 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
1220 const {}
1221 void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001222
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001223 void Walk() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001224 // Use the initial classes as roots for a search.
Mathieu Chartier28357fa2016-10-18 16:27:40 -07001225 for (Handle<mirror::Class> klass_root : image_classes_) {
1226 VisitClinitClassesObject(klass_root.Get());
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001227 }
Vladimir Marko19a4d372016-12-08 14:41:46 +00001228 Thread* self = Thread::Current();
1229 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier31e88222016-10-14 18:43:19 -07001230 for (Handle<mirror::Class> h_klass : to_insert_) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001231 MaybeAddToImageClasses(self, h_klass.Get(), image_class_descriptors_);
Mathieu Chartier31e88222016-10-14 18:43:19 -07001232 }
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001233 }
1234
1235 private:
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001236 class FindImageClassesVisitor : public ClassVisitor {
1237 public:
Mathieu Chartier28357fa2016-10-18 16:27:40 -07001238 explicit FindImageClassesVisitor(VariableSizedHandleScope& hs,
1239 ClinitImageUpdate* data)
1240 : data_(data),
1241 hs_(hs) {}
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001242
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001243 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001244 std::string temp;
Vladimir Marko54159c62018-06-20 14:30:08 +01001245 StringPiece name(klass->GetDescriptor(&temp));
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001246 if (data_->image_class_descriptors_->find(name) != data_->image_class_descriptors_->end()) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -07001247 data_->image_classes_.push_back(hs_.NewHandle(klass));
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001248 } else {
1249 // Check whether it is initialized and has a clinit. They must be kept, too.
1250 if (klass->IsInitialized() && klass->FindClassInitializer(
1251 Runtime::Current()->GetClassLinker()->GetImagePointerSize()) != nullptr) {
Mathieu Chartier28357fa2016-10-18 16:27:40 -07001252 data_->image_classes_.push_back(hs_.NewHandle(klass));
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001253 }
1254 }
1255 return true;
1256 }
1257
1258 private:
1259 ClinitImageUpdate* const data_;
Mathieu Chartier28357fa2016-10-18 16:27:40 -07001260 VariableSizedHandleScope& hs_;
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001261 };
1262
Mathieu Chartier31e88222016-10-14 18:43:19 -07001263 ClinitImageUpdate(VariableSizedHandleScope& hs,
Vladimir Marko54159c62018-06-20 14:30:08 +01001264 HashSet<std::string>* image_class_descriptors,
Mathieu Chartier31e88222016-10-14 18:43:19 -07001265 Thread* self,
1266 ClassLinker* linker) REQUIRES_SHARED(Locks::mutator_lock_)
1267 : hs_(hs),
1268 image_class_descriptors_(image_class_descriptors),
1269 self_(self) {
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001270 CHECK(linker != nullptr);
1271 CHECK(image_class_descriptors != nullptr);
1272
1273 // Make sure nobody interferes with us.
1274 old_cause_ = self->StartAssertNoThreadSuspension("Boot image closure");
1275
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001276 // Find all the already-marked classes.
1277 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier28357fa2016-10-18 16:27:40 -07001278 FindImageClassesVisitor visitor(hs_, this);
Mathieu Chartiere0671ce2015-07-28 17:23:28 -07001279 linker->VisitClasses(&visitor);
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001280 }
1281
1282 void VisitClinitClassesObject(mirror::Object* object) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001283 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001284 DCHECK(object != nullptr);
1285 if (marked_objects_.find(object) != marked_objects_.end()) {
1286 // Already processed.
1287 return;
1288 }
1289
1290 // Mark it.
1291 marked_objects_.insert(object);
1292
1293 if (object->IsClass()) {
Mathieu Chartier31e88222016-10-14 18:43:19 -07001294 // Add to the TODO list since MaybeAddToImageClasses may cause thread suspension. Thread
1295 // suspensionb is not safe to do in VisitObjects or VisitReferences.
1296 to_insert_.push_back(hs_.NewHandle(object->AsClass()));
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001297 } else {
1298 // Else visit the object's class.
1299 VisitClinitClassesObject(object->GetClass());
1300 }
1301
Mathieu Chartiere401d142015-04-22 13:56:20 -07001302 // If it is not a DexCache, visit all references.
Mathieu Chartier31e88222016-10-14 18:43:19 -07001303 if (!object->IsDexCache()) {
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001304 object->VisitReferences(*this, *this);
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001305 }
1306 }
1307
Mathieu Chartier31e88222016-10-14 18:43:19 -07001308 VariableSizedHandleScope& hs_;
1309 mutable std::vector<Handle<mirror::Class>> to_insert_;
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001310 mutable std::unordered_set<mirror::Object*> marked_objects_;
Vladimir Marko54159c62018-06-20 14:30:08 +01001311 HashSet<std::string>* const image_class_descriptors_;
Mathieu Chartier28357fa2016-10-18 16:27:40 -07001312 std::vector<Handle<mirror::Class>> image_classes_;
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001313 Thread* const self_;
1314 const char* old_cause_;
1315
1316 DISALLOW_COPY_AND_ASSIGN(ClinitImageUpdate);
1317};
Brian Carlstrom7940e442013-07-12 13:46:57 -07001318
Ian Rogers3d504072014-03-01 09:16:49 -08001319void CompilerDriver::UpdateImageClasses(TimingLogger* timings) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001320 if (GetCompilerOptions().IsBootImage()) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001321 TimingLogger::ScopedTiming t("UpdateImageClasses", timings);
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001322
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001323 Runtime* runtime = Runtime::Current();
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001324
1325 // Suspend all threads.
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001326 ScopedSuspendAll ssa(__FUNCTION__);
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001327
Mathieu Chartier31e88222016-10-14 18:43:19 -07001328 VariableSizedHandleScope hs(Thread::Current());
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001329 std::string error_msg;
Mathieu Chartier31e88222016-10-14 18:43:19 -07001330 std::unique_ptr<ClinitImageUpdate> update(ClinitImageUpdate::Create(hs,
Vladimir Markodc4bcce2018-06-21 16:15:42 +01001331 image_classes_,
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001332 Thread::Current(),
Mathieu Chartier31e88222016-10-14 18:43:19 -07001333 runtime->GetClassLinker()));
Andreas Gampeb0f370e2014-09-25 22:51:40 -07001334
1335 // Do the marking.
1336 update->Walk();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001337 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001338}
1339
Vladimir Markobe0e5462014-02-26 11:24:15 +00001340void CompilerDriver::ProcessedInstanceField(bool resolved) {
1341 if (!resolved) {
1342 stats_->UnresolvedInstanceField();
1343 } else {
1344 stats_->ResolvedInstanceField();
1345 }
1346}
1347
1348void CompilerDriver::ProcessedStaticField(bool resolved, bool local) {
1349 if (!resolved) {
1350 stats_->UnresolvedStaticField();
1351 } else if (local) {
1352 stats_->ResolvedLocalStaticField();
1353 } else {
1354 stats_->ResolvedStaticField();
1355 }
1356}
1357
Mathieu Chartierc7853442015-03-27 14:35:38 -07001358ArtField* CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx,
Vladimir Markoe11dd502017-12-08 14:09:45 +00001359 const DexCompilationUnit* mUnit,
1360 bool is_put,
Mathieu Chartierc7853442015-03-27 14:35:38 -07001361 const ScopedObjectAccess& soa) {
Vladimir Markobe0e5462014-02-26 11:24:15 +00001362 // Try to resolve the field and compiling method's class.
Mathieu Chartierc7853442015-03-27 14:35:38 -07001363 ArtField* resolved_field;
Vladimir Marko28e012a2017-12-07 11:22:59 +00001364 ObjPtr<mirror::Class> referrer_class;
Mathieu Chartier736b5602015-09-02 14:54:11 -07001365 Handle<mirror::DexCache> dex_cache(mUnit->GetDexCache());
Vladimir Markobe0e5462014-02-26 11:24:15 +00001366 {
Vladimir Markoe11dd502017-12-08 14:09:45 +00001367 Handle<mirror::ClassLoader> class_loader = mUnit->GetClassLoader();
1368 resolved_field = ResolveField(soa, dex_cache, class_loader, field_idx, /* is_static */ false);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001369 referrer_class = resolved_field != nullptr
Vladimir Markoe11dd502017-12-08 14:09:45 +00001370 ? ResolveCompilingMethodsClass(soa, dex_cache, class_loader, mUnit) : nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001371 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001372 bool can_link = false;
Vladimir Markobe0e5462014-02-26 11:24:15 +00001373 if (resolved_field != nullptr && referrer_class != nullptr) {
Vladimir Markobe0e5462014-02-26 11:24:15 +00001374 std::pair<bool, bool> fast_path = IsFastInstanceField(
Mathieu Chartier736b5602015-09-02 14:54:11 -07001375 dex_cache.Get(), referrer_class, resolved_field, field_idx);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001376 can_link = is_put ? fast_path.second : fast_path.first;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001377 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001378 ProcessedInstanceField(can_link);
1379 return can_link ? resolved_field : nullptr;
1380}
1381
1382bool CompilerDriver::ComputeInstanceFieldInfo(uint32_t field_idx, const DexCompilationUnit* mUnit,
1383 bool is_put, MemberOffset* field_offset,
1384 bool* is_volatile) {
1385 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierc7853442015-03-27 14:35:38 -07001386 ArtField* resolved_field = ComputeInstanceFieldInfo(field_idx, mUnit, is_put, soa);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001387
Mathieu Chartierc7853442015-03-27 14:35:38 -07001388 if (resolved_field == nullptr) {
Vladimir Markobe0e5462014-02-26 11:24:15 +00001389 // Conservative defaults.
1390 *is_volatile = true;
1391 *field_offset = MemberOffset(static_cast<size_t>(-1));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001392 return false;
1393 } else {
1394 *is_volatile = resolved_field->IsVolatile();
1395 *field_offset = resolved_field->GetOffset();
1396 return true;
Vladimir Markobe0e5462014-02-26 11:24:15 +00001397 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001398}
1399
Vladimir Marko2730db02014-01-27 11:15:17 +00001400const VerifiedMethod* CompilerDriver::GetVerifiedMethod(const DexFile* dex_file,
1401 uint32_t method_idx) const {
1402 MethodReference ref(dex_file, method_idx);
1403 return verification_results_->GetVerifiedMethod(ref);
1404}
1405
1406bool CompilerDriver::IsSafeCast(const DexCompilationUnit* mUnit, uint32_t dex_pc) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001407 if (!compiler_options_->IsVerificationEnabled()) {
1408 // If we didn't verify, every cast has to be treated as non-safe.
1409 return false;
1410 }
Vladimir Marko2730db02014-01-27 11:15:17 +00001411 DCHECK(mUnit->GetVerifiedMethod() != nullptr);
1412 bool result = mUnit->GetVerifiedMethod()->IsSafeCast(dex_pc);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001413 if (result) {
1414 stats_->SafeCast();
1415 } else {
1416 stats_->NotASafeCast();
1417 }
1418 return result;
1419}
1420
Mathieu Chartier90443472015-07-16 20:32:27 -07001421class CompilationVisitor {
1422 public:
1423 virtual ~CompilationVisitor() {}
1424 virtual void Visit(size_t index) = 0;
1425};
1426
Brian Carlstrom7940e442013-07-12 13:46:57 -07001427class ParallelCompilationManager {
1428 public:
Brian Carlstrom7940e442013-07-12 13:46:57 -07001429 ParallelCompilationManager(ClassLinker* class_linker,
1430 jobject class_loader,
1431 CompilerDriver* compiler,
1432 const DexFile* dex_file,
Andreas Gampede7b4362014-07-28 18:38:57 -07001433 const std::vector<const DexFile*>& dex_files,
Ian Rogers3d504072014-03-01 09:16:49 -08001434 ThreadPool* thread_pool)
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001435 : index_(0),
1436 class_linker_(class_linker),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001437 class_loader_(class_loader),
1438 compiler_(compiler),
1439 dex_file_(dex_file),
Andreas Gampede7b4362014-07-28 18:38:57 -07001440 dex_files_(dex_files),
Ian Rogers3d504072014-03-01 09:16:49 -08001441 thread_pool_(thread_pool) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -07001442
1443 ClassLinker* GetClassLinker() const {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001444 CHECK(class_linker_ != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001445 return class_linker_;
1446 }
1447
1448 jobject GetClassLoader() const {
1449 return class_loader_;
1450 }
1451
1452 CompilerDriver* GetCompiler() const {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001453 CHECK(compiler_ != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001454 return compiler_;
1455 }
1456
1457 const DexFile* GetDexFile() const {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001458 CHECK(dex_file_ != nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001459 return dex_file_;
1460 }
1461
Andreas Gampede7b4362014-07-28 18:38:57 -07001462 const std::vector<const DexFile*>& GetDexFiles() const {
1463 return dex_files_;
1464 }
1465
Mathieu Chartier90443472015-07-16 20:32:27 -07001466 void ForAll(size_t begin, size_t end, CompilationVisitor* visitor, size_t work_units)
1467 REQUIRES(!*Locks::mutator_lock_) {
Andreas Gampe3c060ad2018-01-22 13:07:49 -08001468 ForAllLambda(begin, end, [visitor](size_t index) { visitor->Visit(index); }, work_units);
1469 }
1470
1471 template <typename Fn>
1472 void ForAllLambda(size_t begin, size_t end, Fn fn, size_t work_units)
1473 REQUIRES(!*Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001474 Thread* self = Thread::Current();
1475 self->AssertNoPendingException();
1476 CHECK_GT(work_units, 0U);
1477
Orion Hodson88591fe2018-03-06 13:35:43 +00001478 index_.store(begin, std::memory_order_relaxed);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001479 for (size_t i = 0; i < work_units; ++i) {
Andreas Gampe3c060ad2018-01-22 13:07:49 -08001480 thread_pool_->AddTask(self, new ForAllClosureLambda<Fn>(this, end, fn));
Brian Carlstrom7940e442013-07-12 13:46:57 -07001481 }
1482 thread_pool_->StartWorkers(self);
1483
1484 // Ensure we're suspended while we're blocked waiting for the other threads to finish (worker
1485 // thread destructor's called below perform join).
1486 CHECK_NE(self->GetState(), kRunnable);
1487
1488 // Wait for all the worker threads to finish.
1489 thread_pool_->Wait(self, true, false);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001490
1491 // And stop the workers accepting jobs.
1492 thread_pool_->StopWorkers(self);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001493 }
1494
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001495 size_t NextIndex() {
Orion Hodson88591fe2018-03-06 13:35:43 +00001496 return index_.fetch_add(1, std::memory_order_seq_cst);
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001497 }
1498
Brian Carlstrom7940e442013-07-12 13:46:57 -07001499 private:
Andreas Gampe3c060ad2018-01-22 13:07:49 -08001500 template <typename Fn>
1501 class ForAllClosureLambda : public Task {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001502 public:
Andreas Gampe3c060ad2018-01-22 13:07:49 -08001503 ForAllClosureLambda(ParallelCompilationManager* manager, size_t end, Fn fn)
Brian Carlstrom7940e442013-07-12 13:46:57 -07001504 : manager_(manager),
Brian Carlstrom7940e442013-07-12 13:46:57 -07001505 end_(end),
Andreas Gampe3c060ad2018-01-22 13:07:49 -08001506 fn_(fn) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -07001507
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001508 void Run(Thread* self) override {
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001509 while (true) {
1510 const size_t index = manager_->NextIndex();
1511 if (UNLIKELY(index >= end_)) {
1512 break;
1513 }
Andreas Gampe3c060ad2018-01-22 13:07:49 -08001514 fn_(index);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001515 self->AssertNoPendingException();
1516 }
1517 }
1518
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001519 void Finalize() override {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001520 delete this;
1521 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -07001522
Brian Carlstrom7940e442013-07-12 13:46:57 -07001523 private:
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001524 ParallelCompilationManager* const manager_;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001525 const size_t end_;
Andreas Gampe3c060ad2018-01-22 13:07:49 -08001526 Fn fn_;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001527 };
1528
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001529 AtomicInteger index_;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001530 ClassLinker* const class_linker_;
1531 const jobject class_loader_;
1532 CompilerDriver* const compiler_;
1533 const DexFile* const dex_file_;
Andreas Gampede7b4362014-07-28 18:38:57 -07001534 const std::vector<const DexFile*>& dex_files_;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001535 ThreadPool* const thread_pool_;
Mathieu Chartier0b3eb392013-08-23 14:56:59 -07001536
1537 DISALLOW_COPY_AND_ASSIGN(ParallelCompilationManager);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001538};
1539
Jeff Hao0e49b422013-11-08 12:16:56 -08001540// A fast version of SkipClass above if the class pointer is available
1541// that avoids the expensive FindInClassPath search.
Vladimir Marko28e012a2017-12-07 11:22:59 +00001542static bool SkipClass(jobject class_loader, const DexFile& dex_file, ObjPtr<mirror::Class> klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001543 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001544 DCHECK(klass != nullptr);
Jeff Hao0e49b422013-11-08 12:16:56 -08001545 const DexFile& original_dex_file = *klass->GetDexCache()->GetDexFile();
1546 if (&dex_file != &original_dex_file) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001547 if (class_loader == nullptr) {
David Sehr709b0702016-10-13 09:12:37 -07001548 LOG(WARNING) << "Skipping class " << klass->PrettyDescriptor() << " from "
Jeff Hao0e49b422013-11-08 12:16:56 -08001549 << dex_file.GetLocation() << " previously found in "
1550 << original_dex_file.GetLocation();
1551 }
1552 return true;
1553 }
1554 return false;
1555}
1556
Mathieu Chartier70b63482014-06-27 17:19:04 -07001557static void CheckAndClearResolveException(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001558 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier70b63482014-06-27 17:19:04 -07001559 CHECK(self->IsExceptionPending());
Nicolas Geoffray14691c52015-03-05 10:40:17 +00001560 mirror::Throwable* exception = self->GetException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07001561 std::string temp;
1562 const char* descriptor = exception->GetClass()->GetDescriptor(&temp);
1563 const char* expected_exceptions[] = {
1564 "Ljava/lang/IllegalAccessError;",
1565 "Ljava/lang/IncompatibleClassChangeError;",
1566 "Ljava/lang/InstantiationError;",
Brian Carlstrom898fcb52014-08-25 23:07:30 -07001567 "Ljava/lang/LinkageError;",
Ian Rogers1ff3c982014-08-12 02:30:58 -07001568 "Ljava/lang/NoClassDefFoundError;",
1569 "Ljava/lang/NoSuchFieldError;",
1570 "Ljava/lang/NoSuchMethodError;"
1571 };
1572 bool found = false;
1573 for (size_t i = 0; (found == false) && (i < arraysize(expected_exceptions)); ++i) {
1574 if (strcmp(descriptor, expected_exceptions[i]) == 0) {
1575 found = true;
1576 }
1577 }
1578 if (!found) {
Brian Carlstrom898fcb52014-08-25 23:07:30 -07001579 LOG(FATAL) << "Unexpected exception " << exception->Dump();
Mathieu Chartier70b63482014-06-27 17:19:04 -07001580 }
1581 self->ClearException();
1582}
1583
Mathieu Chartier90443472015-07-16 20:32:27 -07001584class ResolveClassFieldsAndMethodsVisitor : public CompilationVisitor {
1585 public:
1586 explicit ResolveClassFieldsAndMethodsVisitor(const ParallelCompilationManager* manager)
1587 : manager_(manager) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -07001588
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001589 void Visit(size_t class_def_index) override REQUIRES(!Locks::mutator_lock_) {
Andreas Gampe9b827ab2017-12-07 19:32:48 -08001590 ScopedTrace trace(__FUNCTION__);
Mathieu Chartier90443472015-07-16 20:32:27 -07001591 Thread* const self = Thread::Current();
1592 jobject jclass_loader = manager_->GetClassLoader();
1593 const DexFile& dex_file = *manager_->GetDexFile();
1594 ClassLinker* class_linker = manager_->GetClassLinker();
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001595
Mathieu Chartier90443472015-07-16 20:32:27 -07001596 // Method and Field are the worst. We can't resolve without either
1597 // context from the code use (to disambiguate virtual vs direct
1598 // method and instance vs static field) or from class
1599 // definitions. While the compiler will resolve what it can as it
1600 // needs it, here we try to resolve fields and methods used in class
1601 // definitions, since many of them many never be referenced by
1602 // generated code.
1603 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1604 ScopedObjectAccess soa(self);
1605 StackHandleScope<2> hs(soa.Self());
1606 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001607 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001608 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(
Vladimir Markocd556b02017-02-03 11:47:34 +00001609 soa.Self(), dex_file)));
Mathieu Chartier90443472015-07-16 20:32:27 -07001610 // Resolve the class.
Vladimir Marko28e012a2017-12-07 11:22:59 +00001611 ObjPtr<mirror::Class> klass =
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001612 class_linker->ResolveType(class_def.class_idx_, dex_cache, class_loader);
Mathieu Chartier90443472015-07-16 20:32:27 -07001613 bool resolve_fields_and_methods;
1614 if (klass == nullptr) {
1615 // Class couldn't be resolved, for example, super-class is in a different dex file. Don't
1616 // attempt to resolve methods and fields when there is no declaring class.
1617 CheckAndClearResolveException(soa.Self());
1618 resolve_fields_and_methods = false;
1619 } else {
1620 // We successfully resolved a class, should we skip it?
1621 if (SkipClass(jclass_loader, dex_file, klass)) {
1622 return;
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001623 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001624 // We want to resolve the methods and fields eagerly.
1625 resolve_fields_and_methods = true;
Ian Rogers68b56852014-08-29 20:19:11 -07001626 }
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001627
Vladimir Markoc1c34522018-10-31 13:56:49 +00001628 if (resolve_fields_and_methods) {
1629 ClassAccessor accessor(dex_file, class_def_index);
1630 // Optionally resolve fields and methods and figure out if we need a constructor barrier.
1631 auto method_visitor = [&](const ClassAccessor::Method& method)
1632 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001633 ArtMethod* resolved = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
1634 method.GetIndex(),
1635 dex_cache,
1636 class_loader,
Vladimir Markoc1c34522018-10-31 13:56:49 +00001637 /*referrer=*/ nullptr,
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001638 method.GetInvokeType(class_def.access_flags_));
1639 if (resolved == nullptr) {
1640 CheckAndClearResolveException(soa.Self());
Mathieu Chartier90443472015-07-16 20:32:27 -07001641 }
Vladimir Markoc1c34522018-10-31 13:56:49 +00001642 };
1643 accessor.VisitFieldsAndMethods(
1644 // static fields
1645 [&](ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001646 ArtField* resolved = class_linker->ResolveField(
Vladimir Markoc1c34522018-10-31 13:56:49 +00001647 field.GetIndex(), dex_cache, class_loader, /*is_static=*/ true);
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001648 if (resolved == nullptr) {
1649 CheckAndClearResolveException(soa.Self());
1650 }
Vladimir Markoc1c34522018-10-31 13:56:49 +00001651 },
1652 // instance fields
1653 [&](ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001654 ArtField* resolved = class_linker->ResolveField(
Vladimir Markoc1c34522018-10-31 13:56:49 +00001655 field.GetIndex(), dex_cache, class_loader, /*is_static=*/ false);
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001656 if (resolved == nullptr) {
1657 CheckAndClearResolveException(soa.Self());
1658 }
Vladimir Markoc1c34522018-10-31 13:56:49 +00001659 },
1660 /*direct_method_visitor=*/ method_visitor,
1661 /*virtual_method_visitor=*/ method_visitor);
1662 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001663 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001664
Mathieu Chartier90443472015-07-16 20:32:27 -07001665 private:
1666 const ParallelCompilationManager* const manager_;
1667};
1668
1669class ResolveTypeVisitor : public CompilationVisitor {
1670 public:
1671 explicit ResolveTypeVisitor(const ParallelCompilationManager* manager) : manager_(manager) {
1672 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001673 void Visit(size_t type_idx) override REQUIRES(!Locks::mutator_lock_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001674 // Class derived values are more complicated, they require the linker and loader.
Mathieu Chartier90443472015-07-16 20:32:27 -07001675 ScopedObjectAccess soa(Thread::Current());
1676 ClassLinker* class_linker = manager_->GetClassLinker();
1677 const DexFile& dex_file = *manager_->GetDexFile();
1678 StackHandleScope<2> hs(soa.Self());
Mathieu Chartier90443472015-07-16 20:32:27 -07001679 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001680 hs.NewHandle(soa.Decode<mirror::ClassLoader>(manager_->GetClassLoader())));
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001681 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->RegisterDexFile(
1682 dex_file,
Mathieu Chartierf284d442016-06-02 11:48:30 -07001683 class_loader.Get())));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001684 ObjPtr<mirror::Class> klass = (dex_cache != nullptr)
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001685 ? class_linker->ResolveType(dex::TypeIndex(type_idx), dex_cache, class_loader)
Vladimir Markocd556b02017-02-03 11:47:34 +00001686 : nullptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001687
Mathieu Chartier90443472015-07-16 20:32:27 -07001688 if (klass == nullptr) {
1689 soa.Self()->AssertPendingException();
1690 mirror::Throwable* exception = soa.Self()->GetException();
1691 VLOG(compiler) << "Exception during type resolution: " << exception->Dump();
1692 if (exception->GetClass()->DescriptorEquals("Ljava/lang/OutOfMemoryError;")) {
1693 // There's little point continuing compilation if the heap is exhausted.
1694 LOG(FATAL) << "Out of memory during type resolution for compilation";
1695 }
1696 soa.Self()->ClearException();
Ian Rogersa436fde2013-08-27 23:34:06 -07001697 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001698 }
Mathieu Chartier90443472015-07-16 20:32:27 -07001699
1700 private:
1701 const ParallelCompilationManager* const manager_;
1702};
Brian Carlstrom7940e442013-07-12 13:46:57 -07001703
Andreas Gampeace0dc12016-01-20 13:33:13 -08001704void CompilerDriver::ResolveDexFile(jobject class_loader,
1705 const DexFile& dex_file,
Andreas Gampede7b4362014-07-28 18:38:57 -07001706 const std::vector<const DexFile*>& dex_files,
Andreas Gampeace0dc12016-01-20 13:33:13 -08001707 ThreadPool* thread_pool,
1708 size_t thread_count,
1709 TimingLogger* timings) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001710 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1711
1712 // TODO: we could resolve strings here, although the string table is largely filled with class
1713 // and method names.
1714
Andreas Gampede7b4362014-07-28 18:38:57 -07001715 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
1716 thread_pool);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001717 if (GetCompilerOptions().IsBootImage()) {
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001718 // For images we resolve all types, such as array, whereas for applications just those with
1719 // classdefs are resolved by ResolveClassFieldsAndMethods.
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001720 TimingLogger::ScopedTiming t("Resolve Types", timings);
Mathieu Chartier90443472015-07-16 20:32:27 -07001721 ResolveTypeVisitor visitor(&context);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001722 context.ForAll(0, dex_file.NumTypeIds(), &visitor, thread_count);
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001723 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001724
Mathieu Chartierf5997b42014-06-20 10:37:54 -07001725 TimingLogger::ScopedTiming t("Resolve MethodsAndFields", timings);
Mathieu Chartier90443472015-07-16 20:32:27 -07001726 ResolveClassFieldsAndMethodsVisitor visitor(&context);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001727 context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001728}
1729
Andreas Gampeace0dc12016-01-20 13:33:13 -08001730void CompilerDriver::SetVerified(jobject class_loader,
1731 const std::vector<const DexFile*>& dex_files,
1732 TimingLogger* timings) {
1733 // This can be run in parallel.
Mathieu Chartier90443472015-07-16 20:32:27 -07001734 for (const DexFile* dex_file : dex_files) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001735 CHECK(dex_file != nullptr);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001736 SetVerifiedDexFile(class_loader,
1737 *dex_file,
1738 dex_files,
1739 parallel_thread_pool_.get(),
1740 parallel_thread_count_,
1741 timings);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001742 }
1743}
1744
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001745static void LoadAndUpdateStatus(const ClassAccessor& accessor,
Vladimir Marko2c64a832018-01-04 11:31:56 +00001746 ClassStatus status,
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +00001747 Handle<mirror::ClassLoader> class_loader,
1748 Thread* self)
1749 REQUIRES_SHARED(Locks::mutator_lock_) {
1750 StackHandleScope<1> hs(self);
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001751 const char* descriptor = accessor.GetDescriptor();
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +00001752 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1753 Handle<mirror::Class> cls(hs.NewHandle<mirror::Class>(
1754 class_linker->FindClass(self, descriptor, class_loader)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001755 if (cls != nullptr) {
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +00001756 // Check that the class is resolved with the current dex file. We might get
1757 // a boot image class, or a class in a different dex file for multidex, and
1758 // we should not update the status in that case.
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001759 if (&cls->GetDexFile() == &accessor.GetDexFile()) {
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +00001760 ObjectLock<mirror::Class> lock(self, cls);
1761 mirror::Class::SetStatus(cls, status, self);
1762 }
1763 } else {
1764 DCHECK(self->IsExceptionPending());
1765 self->ClearException();
1766 }
1767}
1768
Nicolas Geoffray74981052017-01-16 17:54:09 +00001769bool CompilerDriver::FastVerify(jobject jclass_loader,
1770 const std::vector<const DexFile*>& dex_files,
1771 TimingLogger* timings) {
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +00001772 verifier::VerifierDeps* verifier_deps =
1773 Runtime::Current()->GetCompilerCallbacks()->GetVerifierDeps();
Mathieu Chartier72041a02017-07-14 18:23:25 -07001774 // If there exist VerifierDeps that aren't the ones we just created to output, use them to verify.
1775 if (verifier_deps == nullptr || verifier_deps->OutputOnly()) {
Nicolas Geoffray74981052017-01-16 17:54:09 +00001776 return false;
1777 }
1778 TimingLogger::ScopedTiming t("Fast Verify", timings);
1779 ScopedObjectAccess soa(Thread::Current());
1780 StackHandleScope<2> hs(soa.Self());
1781 Handle<mirror::ClassLoader> class_loader(
1782 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Nicolas Geoffray74981052017-01-16 17:54:09 +00001783 if (!verifier_deps->ValidateDependencies(class_loader, soa.Self())) {
1784 return false;
1785 }
1786
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001787 bool compiler_only_verifies = !GetCompilerOptions().IsAnyCompilationEnabled();
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +00001788
Nicolas Geoffray74981052017-01-16 17:54:09 +00001789 // We successfully validated the dependencies, now update class status
1790 // of verified classes. Note that the dependencies also record which classes
1791 // could not be fully verified; we could try again, but that would hurt verification
1792 // time. So instead we assume these classes still need to be verified at
1793 // runtime.
1794 for (const DexFile* dex_file : dex_files) {
Mathieu Chartierbf755fe2017-08-01 13:42:56 -07001795 // Fetch the list of unverified classes.
1796 const std::set<dex::TypeIndex>& unverified_classes =
Nicolas Geoffray74981052017-01-16 17:54:09 +00001797 verifier_deps->GetUnverifiedClasses(*dex_file);
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001798 for (ClassAccessor accessor : dex_file->GetClasses()) {
1799 if (unverified_classes.find(accessor.GetClassIdx()) == unverified_classes.end()) {
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +00001800 if (compiler_only_verifies) {
Nicolas Geoffray74981052017-01-16 17:54:09 +00001801 // Just update the compiled_classes_ map. The compiler doesn't need to resolve
1802 // the type.
Mathieu Chartier18e26872018-06-04 17:19:02 -07001803 ClassReference ref(dex_file, accessor.GetClassDefIndex());
Mathieu Chartier2eabc612018-05-25 14:31:16 -07001804 const ClassStatus existing = ClassStatus::kNotReady;
Mathieu Chartier1a088d42017-07-18 11:43:57 -07001805 ClassStateTable::InsertResult result =
Vladimir Marko2c64a832018-01-04 11:31:56 +00001806 compiled_classes_.Insert(ref, existing, ClassStatus::kVerified);
Mathieu Chartier2eabc612018-05-25 14:31:16 -07001807 CHECK_EQ(result, ClassStateTable::kInsertResultSuccess) << ref.dex_file->GetLocation();
Nicolas Geoffray74981052017-01-16 17:54:09 +00001808 } else {
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +00001809 // Update the class status, so later compilation stages know they don't need to verify
Nicolas Geoffray74981052017-01-16 17:54:09 +00001810 // the class.
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001811 LoadAndUpdateStatus(accessor, ClassStatus::kVerified, class_loader, soa.Self());
Nicolas Geoffray74981052017-01-16 17:54:09 +00001812 // Create `VerifiedMethod`s for each methods, the compiler expects one for
1813 // quickening or compiling.
1814 // Note that this means:
1815 // - We're only going to compile methods that did verify.
1816 // - Quickening will not do checkcast ellision.
1817 // TODO(ngeoffray): Reconsider this once we refactor compiler filters.
Mathieu Chartier0d896bd2018-05-25 00:20:27 -07001818 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001819 verification_results_->CreateVerifiedMethodFor(method.GetReference());
Mathieu Chartier0d896bd2018-05-25 00:20:27 -07001820 }
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001821 }
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +00001822 } else if (!compiler_only_verifies) {
1823 // Make sure later compilation stages know they should not try to verify
1824 // this class again.
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07001825 LoadAndUpdateStatus(accessor,
Vladimir Marko2c64a832018-01-04 11:31:56 +00001826 ClassStatus::kRetryVerificationAtRuntime,
Nicolas Geoffray0a27fd02017-01-25 16:18:54 +00001827 class_loader,
1828 soa.Self());
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001829 }
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001830 }
1831 }
Nicolas Geoffray74981052017-01-16 17:54:09 +00001832 return true;
1833}
1834
1835void CompilerDriver::Verify(jobject jclass_loader,
1836 const std::vector<const DexFile*>& dex_files,
1837 TimingLogger* timings) {
1838 if (FastVerify(jclass_loader, dex_files, timings)) {
1839 return;
1840 }
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001841
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +00001842 // If there is no existing `verifier_deps` (because of non-existing vdex), or
1843 // the existing `verifier_deps` is not valid anymore, create a new one for
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001844 // non boot image compilation. The verifier will need it to record the new dependencies.
1845 // Then dex2oat can update the vdex file with these new dependencies.
1846 if (!GetCompilerOptions().IsBootImage()) {
Mathieu Chartier72041a02017-07-14 18:23:25 -07001847 // Dex2oat creates the verifier deps.
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001848 // Create the main VerifierDeps, and set it to this thread.
Mathieu Chartier72041a02017-07-14 18:23:25 -07001849 verifier::VerifierDeps* verifier_deps =
1850 Runtime::Current()->GetCompilerCallbacks()->GetVerifierDeps();
1851 CHECK(verifier_deps != nullptr);
Nicolas Geoffrayb0bbe8e2016-11-19 10:42:37 +00001852 Thread::Current()->SetVerifierDeps(verifier_deps);
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001853 // Create per-thread VerifierDeps to avoid contention on the main one.
1854 // We will merge them after verification.
1855 for (ThreadPoolWorker* worker : parallel_thread_pool_->GetWorkers()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +01001856 worker->GetThread()->SetVerifierDeps(
1857 new verifier::VerifierDeps(GetCompilerOptions().GetDexFilesForOatFile()));
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001858 }
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001859 }
Nicolas Geoffray74981052017-01-16 17:54:09 +00001860
Nicolas Geoffray46847392017-04-28 14:56:39 +01001861 // Verification updates VerifierDeps and needs to run single-threaded to be deterministic.
1862 bool force_determinism = GetCompilerOptions().IsForceDeterminism();
1863 ThreadPool* verify_thread_pool =
1864 force_determinism ? single_thread_pool_.get() : parallel_thread_pool_.get();
1865 size_t verify_thread_count = force_determinism ? 1U : parallel_thread_count_;
Mathieu Chartier90443472015-07-16 20:32:27 -07001866 for (const DexFile* dex_file : dex_files) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001867 CHECK(dex_file != nullptr);
Nicolas Geoffray6bb7f1b2016-11-03 10:52:49 +00001868 VerifyDexFile(jclass_loader,
Andreas Gampeace0dc12016-01-20 13:33:13 -08001869 *dex_file,
1870 dex_files,
Nicolas Geoffray46847392017-04-28 14:56:39 +01001871 verify_thread_pool,
1872 verify_thread_count,
Andreas Gampeace0dc12016-01-20 13:33:13 -08001873 timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001874 }
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001875
1876 if (!GetCompilerOptions().IsBootImage()) {
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001877 // Merge all VerifierDeps into the main one.
Nicolas Geoffray74981052017-01-16 17:54:09 +00001878 verifier::VerifierDeps* verifier_deps = Thread::Current()->GetVerifierDeps();
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001879 for (ThreadPoolWorker* worker : parallel_thread_pool_->GetWorkers()) {
1880 verifier::VerifierDeps* thread_deps = worker->GetThread()->GetVerifierDeps();
1881 worker->GetThread()->SetVerifierDeps(nullptr);
Vladimir Marko213ee2d2018-06-22 11:56:34 +01001882 verifier_deps->MergeWith(*thread_deps, GetCompilerOptions().GetDexFilesForOatFile());
Nicolas Geoffray340dafa2016-11-18 16:03:10 +00001883 delete thread_deps;
1884 }
1885 Thread::Current()->SetVerifierDeps(nullptr);
1886 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001887}
1888
Mathieu Chartier90443472015-07-16 20:32:27 -07001889class VerifyClassVisitor : public CompilationVisitor {
1890 public:
Andreas Gampe5fd66d02016-09-12 20:22:19 -07001891 VerifyClassVisitor(const ParallelCompilationManager* manager, verifier::HardFailLogMode log_level)
Andreas Gampe6cc23ac2018-08-24 15:22:43 -07001892 : manager_(manager),
1893 log_level_(log_level),
1894 sdk_version_(Runtime::Current()->GetTargetSdkVersion()) {}
Brian Carlstrom7940e442013-07-12 13:46:57 -07001895
Roland Levillainf73caca2018-08-24 17:19:07 +01001896 void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) override {
Andreas Gampe9b827ab2017-12-07 19:32:48 -08001897 ScopedTrace trace(__FUNCTION__);
Mathieu Chartier90443472015-07-16 20:32:27 -07001898 ScopedObjectAccess soa(Thread::Current());
1899 const DexFile& dex_file = *manager_->GetDexFile();
1900 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
1901 const char* descriptor = dex_file.GetClassDescriptor(class_def);
1902 ClassLinker* class_linker = manager_->GetClassLinker();
1903 jobject jclass_loader = manager_->GetClassLoader();
1904 StackHandleScope<3> hs(soa.Self());
1905 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07001906 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Mathieu Chartier90443472015-07-16 20:32:27 -07001907 Handle<mirror::Class> klass(
1908 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
Andreas Gampe6d7abbd2017-04-24 13:19:09 -07001909 verifier::FailureKind failure_kind;
Andreas Gampefa4333d2017-02-14 11:10:34 -08001910 if (klass == nullptr) {
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001911 CHECK(soa.Self()->IsExceptionPending());
1912 soa.Self()->ClearException();
Mathieu Chartier90443472015-07-16 20:32:27 -07001913
1914 /*
1915 * At compile time, we can still structurally verify the class even if FindClass fails.
1916 * This is to ensure the class is structurally sound for compilation. An unsound class
1917 * will be rejected by the verifier and later skipped during compilation in the compiler.
1918 */
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001919 Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(
Vladimir Markocd556b02017-02-03 11:47:34 +00001920 soa.Self(), dex_file)));
Mathieu Chartier90443472015-07-16 20:32:27 -07001921 std::string error_msg;
Nicolas Geoffray08025182016-10-25 17:20:18 +01001922 failure_kind =
1923 verifier::MethodVerifier::VerifyClass(soa.Self(),
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001924 &dex_file,
1925 dex_cache,
1926 class_loader,
David Brazdil15fc7292016-09-02 14:13:18 +01001927 class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -08001928 Runtime::Current()->GetCompilerCallbacks(),
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001929 true /* allow soft failures */,
Andreas Gampe7fe30232016-03-25 16:58:00 -07001930 log_level_,
Andreas Gampe6cc23ac2018-08-24 15:22:43 -07001931 sdk_version_,
Nicolas Geoffray08025182016-10-25 17:20:18 +01001932 &error_msg);
Andreas Gampe6d7abbd2017-04-24 13:19:09 -07001933 if (failure_kind == verifier::FailureKind::kHardFailure) {
Mathieu Chartier90443472015-07-16 20:32:27 -07001934 LOG(ERROR) << "Verification failed on class " << PrettyDescriptor(descriptor)
1935 << " because: " << error_msg;
1936 manager_->GetCompiler()->SetHadHardVerifierFailure();
Andreas Gampef39208f2017-10-19 15:06:59 -07001937 } else if (failure_kind == verifier::FailureKind::kSoftFailure) {
1938 manager_->GetCompiler()->AddSoftVerifierFailure();
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001939 } else {
1940 // Force a soft failure for the VerifierDeps. This is a sanity measure, as
1941 // the vdex file already records that the class hasn't been resolved. It avoids
1942 // trying to do future verification optimizations when processing the vdex file.
Andreas Gampef39208f2017-10-19 15:06:59 -07001943 DCHECK(failure_kind == verifier::FailureKind::kNoFailure) << failure_kind;
Andreas Gampe6d7abbd2017-04-24 13:19:09 -07001944 failure_kind = verifier::FailureKind::kSoftFailure;
Mathieu Chartier90443472015-07-16 20:32:27 -07001945 }
Andreas Gampebb30d5d2018-04-23 09:59:25 -07001946 } else if (&klass->GetDexFile() != &dex_file) {
1947 // Skip a duplicate class (as the resolved class is from another, earlier dex file).
1948 return; // Do not update state.
Mathieu Chartier90443472015-07-16 20:32:27 -07001949 } else if (!SkipClass(jclass_loader, dex_file, klass.Get())) {
David Sehr709b0702016-10-13 09:12:37 -07001950 CHECK(klass->IsResolved()) << klass->PrettyClass();
Nicolas Geoffray08025182016-10-25 17:20:18 +01001951 failure_kind = class_linker->VerifyClass(soa.Self(), klass, log_level_);
Mathieu Chartier90443472015-07-16 20:32:27 -07001952
1953 if (klass->IsErroneous()) {
1954 // ClassLinker::VerifyClass throws, which isn't useful in the compiler.
1955 CHECK(soa.Self()->IsExceptionPending());
1956 soa.Self()->ClearException();
1957 manager_->GetCompiler()->SetHadHardVerifierFailure();
Andreas Gampef39208f2017-10-19 15:06:59 -07001958 } else if (failure_kind == verifier::FailureKind::kSoftFailure) {
1959 manager_->GetCompiler()->AddSoftVerifierFailure();
Mathieu Chartier90443472015-07-16 20:32:27 -07001960 }
1961
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001962 CHECK(klass->ShouldVerifyAtRuntime() || klass->IsVerified() || klass->IsErroneous())
David Sehr709b0702016-10-13 09:12:37 -07001963 << klass->PrettyDescriptor() << ": state=" << klass->GetStatus();
Mathieu Chartier90443472015-07-16 20:32:27 -07001964
Nicolas Geoffrayc7da1d62017-04-19 09:36:24 +01001965 // Class has a meaningful status for the compiler now, record it.
1966 ClassReference ref(manager_->GetDexFile(), class_def_index);
1967 manager_->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
1968
Andreas Gamped278cb42017-11-22 14:13:00 -08001969 // It is *very* problematic if there are resolution errors in the boot classpath.
1970 //
1971 // It is also bad if classes fail verification. For example, we rely on things working
1972 // OK without verification when the decryption dialog is brought up. It is thus highly
1973 // recommended to compile the boot classpath with
1974 // --abort-on-hard-verifier-error --abort-on-soft-verifier-error
1975 // which is the default build system configuration.
Narayan Kamathe3eae5e2016-10-27 11:47:30 +01001976 if (kIsDebugBuild) {
Orion Hodsonc5e0d3f2017-08-22 19:00:04 +01001977 if (manager_->GetCompiler()->GetCompilerOptions().IsBootImage()) {
Andreas Gamped278cb42017-11-22 14:13:00 -08001978 if (!klass->IsResolved() || klass->IsErroneous()) {
Andreas Gampe12fadcd2017-08-03 05:06:28 -07001979 LOG(FATAL) << "Boot classpath class " << klass->PrettyClass()
Andreas Gamped278cb42017-11-22 14:13:00 -08001980 << " failed to resolve/is erroneous: state= " << klass->GetStatus();
1981 UNREACHABLE();
Andreas Gampe12fadcd2017-08-03 05:06:28 -07001982 }
Narayan Kamathe3eae5e2016-10-27 11:47:30 +01001983 }
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001984 if (klass->IsVerified()) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -07001985 DCHECK_EQ(failure_kind, verifier::FailureKind::kNoFailure);
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001986 } else if (klass->ShouldVerifyAtRuntime()) {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -07001987 DCHECK_EQ(failure_kind, verifier::FailureKind::kSoftFailure);
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001988 } else {
Andreas Gampe6d7abbd2017-04-24 13:19:09 -07001989 DCHECK_EQ(failure_kind, verifier::FailureKind::kHardFailure);
Nicolas Geoffray7cc3ae52017-03-07 14:33:37 +00001990 }
Narayan Kamathe3eae5e2016-10-27 11:47:30 +01001991 }
Nicolas Geoffray08025182016-10-25 17:20:18 +01001992 } else {
1993 // Make the skip a soft failure, essentially being considered as verify at runtime.
Andreas Gampe6d7abbd2017-04-24 13:19:09 -07001994 failure_kind = verifier::FailureKind::kSoftFailure;
Ian Rogerse6bb3b22013-08-19 21:51:45 -07001995 }
Nicolas Geoffray08025182016-10-25 17:20:18 +01001996 verifier::VerifierDeps::MaybeRecordVerificationStatus(
1997 dex_file, class_def.class_idx_, failure_kind);
Mathieu Chartier90443472015-07-16 20:32:27 -07001998 soa.Self()->AssertNoPendingException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001999 }
Mathieu Chartier90443472015-07-16 20:32:27 -07002000
2001 private:
2002 const ParallelCompilationManager* const manager_;
Andreas Gampe5fd66d02016-09-12 20:22:19 -07002003 const verifier::HardFailLogMode log_level_;
Andreas Gampe6cc23ac2018-08-24 15:22:43 -07002004 const uint32_t sdk_version_;
Mathieu Chartier90443472015-07-16 20:32:27 -07002005};
Brian Carlstrom7940e442013-07-12 13:46:57 -07002006
Andreas Gampeace0dc12016-01-20 13:33:13 -08002007void CompilerDriver::VerifyDexFile(jobject class_loader,
2008 const DexFile& dex_file,
Andreas Gampede7b4362014-07-28 18:38:57 -07002009 const std::vector<const DexFile*>& dex_files,
Andreas Gampeace0dc12016-01-20 13:33:13 -08002010 ThreadPool* thread_pool,
2011 size_t thread_count,
2012 TimingLogger* timings) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -07002013 TimingLogger::ScopedTiming t("Verify Dex File", timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002014 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampede7b4362014-07-28 18:38:57 -07002015 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
2016 thread_pool);
Andreas Gampef39208f2017-10-19 15:06:59 -07002017 bool abort_on_verifier_failures = GetCompilerOptions().AbortOnHardVerifierFailure()
2018 || GetCompilerOptions().AbortOnSoftVerifierFailure();
2019 verifier::HardFailLogMode log_level = abort_on_verifier_failures
Andreas Gampe5fd66d02016-09-12 20:22:19 -07002020 ? verifier::HardFailLogMode::kLogInternalFatal
2021 : verifier::HardFailLogMode::kLogWarning;
Andreas Gampe7fe30232016-03-25 16:58:00 -07002022 VerifyClassVisitor visitor(&context, log_level);
Andreas Gampeace0dc12016-01-20 13:33:13 -08002023 context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002024}
2025
Mathieu Chartier90443472015-07-16 20:32:27 -07002026class SetVerifiedClassVisitor : public CompilationVisitor {
2027 public:
2028 explicit SetVerifiedClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {}
2029
Roland Levillainf73caca2018-08-24 17:19:07 +01002030 void Visit(size_t class_def_index) REQUIRES(!Locks::mutator_lock_) override {
Andreas Gampe9b827ab2017-12-07 19:32:48 -08002031 ScopedTrace trace(__FUNCTION__);
Mathieu Chartier90443472015-07-16 20:32:27 -07002032 ScopedObjectAccess soa(Thread::Current());
2033 const DexFile& dex_file = *manager_->GetDexFile();
2034 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
2035 const char* descriptor = dex_file.GetClassDescriptor(class_def);
2036 ClassLinker* class_linker = manager_->GetClassLinker();
2037 jobject jclass_loader = manager_->GetClassLoader();
2038 StackHandleScope<3> hs(soa.Self());
2039 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002040 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Mathieu Chartier90443472015-07-16 20:32:27 -07002041 Handle<mirror::Class> klass(
2042 hs.NewHandle(class_linker->FindClass(soa.Self(), descriptor, class_loader)));
2043 // Class might have failed resolution. Then don't set it to verified.
Andreas Gampefa4333d2017-02-14 11:10:34 -08002044 if (klass != nullptr) {
Mathieu Chartier90443472015-07-16 20:32:27 -07002045 // Only do this if the class is resolved. If even resolution fails, quickening will go very,
2046 // very wrong.
Vladimir Marko72ab6842017-01-20 19:32:50 +00002047 if (klass->IsResolved() && !klass->IsErroneousResolved()) {
Vladimir Marko2c64a832018-01-04 11:31:56 +00002048 if (klass->GetStatus() < ClassStatus::kVerified) {
Mathieu Chartier90443472015-07-16 20:32:27 -07002049 ObjectLock<mirror::Class> lock(soa.Self(), klass);
2050 // Set class status to verified.
Vladimir Marko2c64a832018-01-04 11:31:56 +00002051 mirror::Class::SetStatus(klass, ClassStatus::kVerified, soa.Self());
Mathieu Chartier90443472015-07-16 20:32:27 -07002052 // Mark methods as pre-verified. If we don't do this, the interpreter will run with
2053 // access checks.
Vladimir Markoa0431112018-06-25 09:32:54 +01002054 InstructionSet instruction_set =
2055 manager_->GetCompiler()->GetCompilerOptions().GetInstructionSet();
2056 klass->SetSkipAccessChecksFlagOnAllMethods(GetInstructionSetPointerSize(instruction_set));
Igor Murashkindf707e42016-02-02 16:56:50 -08002057 klass->SetVerificationAttempted();
Mathieu Chartier90443472015-07-16 20:32:27 -07002058 }
2059 // Record the final class status if necessary.
2060 ClassReference ref(manager_->GetDexFile(), class_def_index);
2061 manager_->GetCompiler()->RecordClassStatus(ref, klass->GetStatus());
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002062 }
Mathieu Chartier90443472015-07-16 20:32:27 -07002063 } else {
2064 Thread* self = soa.Self();
2065 DCHECK(self->IsExceptionPending());
2066 self->ClearException();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002067 }
2068 }
Mathieu Chartier90443472015-07-16 20:32:27 -07002069
2070 private:
2071 const ParallelCompilationManager* const manager_;
2072};
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002073
Andreas Gampeace0dc12016-01-20 13:33:13 -08002074void CompilerDriver::SetVerifiedDexFile(jobject class_loader,
2075 const DexFile& dex_file,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002076 const std::vector<const DexFile*>& dex_files,
Andreas Gampeace0dc12016-01-20 13:33:13 -08002077 ThreadPool* thread_pool,
2078 size_t thread_count,
2079 TimingLogger* timings) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002080 TimingLogger::ScopedTiming t("Verify Dex File", timings);
Mathieu Chartier1a088d42017-07-18 11:43:57 -07002081 if (!compiled_classes_.HaveDexFile(&dex_file)) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002082 compiled_classes_.AddDexFile(&dex_file);
Mathieu Chartier1a088d42017-07-18 11:43:57 -07002083 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002084 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2085 ParallelCompilationManager context(class_linker, class_loader, this, &dex_file, dex_files,
2086 thread_pool);
Mathieu Chartier90443472015-07-16 20:32:27 -07002087 SetVerifiedClassVisitor visitor(&context);
Andreas Gampeace0dc12016-01-20 13:33:13 -08002088 context.ForAll(0, dex_file.NumClassDefs(), &visitor, thread_count);
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002089}
2090
Mathieu Chartier90443472015-07-16 20:32:27 -07002091class InitializeClassVisitor : public CompilationVisitor {
2092 public:
2093 explicit InitializeClassVisitor(const ParallelCompilationManager* manager) : manager_(manager) {}
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002094
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002095 void Visit(size_t class_def_index) override {
Andreas Gampe9b827ab2017-12-07 19:32:48 -08002096 ScopedTrace trace(__FUNCTION__);
Mathieu Chartier90443472015-07-16 20:32:27 -07002097 jobject jclass_loader = manager_->GetClassLoader();
2098 const DexFile& dex_file = *manager_->GetDexFile();
2099 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
2100 const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def.class_idx_);
2101 const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_);
Jeff Hao0e49b422013-11-08 12:16:56 -08002102
Mathieu Chartier90443472015-07-16 20:32:27 -07002103 ScopedObjectAccess soa(Thread::Current());
2104 StackHandleScope<3> hs(soa.Self());
2105 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002106 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Mathieu Chartier90443472015-07-16 20:32:27 -07002107 Handle<mirror::Class> klass(
2108 hs.NewHandle(manager_->GetClassLinker()->FindClass(soa.Self(), descriptor, class_loader)));
2109
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002110 if (klass != nullptr && !SkipClass(manager_->GetClassLoader(), dex_file, klass.Get())) {
2111 TryInitializeClass(klass, class_loader);
2112 }
2113 // Clear any class not found or verification exceptions.
2114 soa.Self()->ClearException();
2115 }
2116
2117 // A helper function for initializing klass.
2118 void TryInitializeClass(Handle<mirror::Class> klass, Handle<mirror::ClassLoader>& class_loader)
2119 REQUIRES_SHARED(Locks::mutator_lock_) {
2120 const DexFile& dex_file = klass->GetDexFile();
2121 const DexFile::ClassDef* class_def = klass->GetClassDef();
2122 const DexFile::TypeId& class_type_id = dex_file.GetTypeId(class_def->class_idx_);
2123 const char* descriptor = dex_file.StringDataByIdx(class_type_id.descriptor_idx_);
2124 ScopedObjectAccessUnchecked soa(Thread::Current());
2125 StackHandleScope<3> hs(soa.Self());
Mathieu Chartierdabeb3a2017-06-12 17:10:07 -07002126 const bool is_boot_image = manager_->GetCompiler()->GetCompilerOptions().IsBootImage();
Mathieu Chartier17498e52017-06-13 11:35:04 -07002127 const bool is_app_image = manager_->GetCompiler()->GetCompilerOptions().IsAppImage();
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002128
Vladimir Marko2c64a832018-01-04 11:31:56 +00002129 ClassStatus old_status = klass->GetStatus();
Mathieu Chartier1a088d42017-07-18 11:43:57 -07002130 // Don't initialize classes in boot space when compiling app image
2131 if (is_app_image && klass->IsBootStrapClassLoaded()) {
2132 // Also return early and don't store the class status in the recorded class status.
2133 return;
2134 }
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002135 // Only try to initialize classes that were successfully verified.
2136 if (klass->IsVerified()) {
2137 // Attempt to initialize the class but bail if we either need to initialize the super-class
2138 // or static fields.
Nicolas Geoffrayabadf022017-08-03 08:25:41 +00002139 manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, false, false);
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002140 old_status = klass->GetStatus();
2141 if (!klass->IsInitialized()) {
2142 // We don't want non-trivial class initialization occurring on multiple threads due to
2143 // deadlock problems. For example, a parent class is initialized (holding its lock) that
2144 // refers to a sub-class in its static/class initializer causing it to try to acquire the
2145 // sub-class' lock. While on a second thread the sub-class is initialized (holding its lock)
2146 // after first initializing its parents, whose locks are acquired. This leads to a
2147 // parent-to-child and a child-to-parent lock ordering and consequent potential deadlock.
2148 // We need to use an ObjectLock due to potential suspension in the interpreting code. Rather
2149 // than use a special Object for the purpose we use the Class of java.lang.Class.
2150 Handle<mirror::Class> h_klass(hs.NewHandle(klass->GetClass()));
2151 ObjectLock<mirror::Class> lock(soa.Self(), h_klass);
2152 // Attempt to initialize allowing initialization of parent classes but still not static
2153 // fields.
Chang Xinge602b1c2017-06-30 11:55:01 -07002154 // Initialize dependencies first only for app image, to make TryInitialize recursive.
2155 bool is_superclass_initialized = !is_app_image ? true :
2156 InitializeDependencies(klass, class_loader, soa.Self());
2157 if (!is_app_image || (is_app_image && is_superclass_initialized)) {
Nicolas Geoffrayabadf022017-08-03 08:25:41 +00002158 manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, false, true);
Chang Xinge602b1c2017-06-30 11:55:01 -07002159 }
2160 // Otherwise it's in app image but superclasses can't be initialized, no need to proceed.
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002161 old_status = klass->GetStatus();
Chang Xing70f689d2017-06-08 17:16:12 -07002162
Chris Wailes0c61be42018-09-26 17:27:34 -07002163 bool too_many_encoded_fields = !is_boot_image &&
2164 klass->NumStaticFields() > kMaxEncodedFields;
2165
Mathieu Chartierdabeb3a2017-06-12 17:10:07 -07002166 // If the class was not initialized, we can proceed to see if we can initialize static
Chang Xing70f689d2017-06-08 17:16:12 -07002167 // fields. Limit the max number of encoded fields.
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002168 if (!klass->IsInitialized() &&
Mathieu Chartier17498e52017-06-13 11:35:04 -07002169 (is_app_image || is_boot_image) &&
Chang Xinge602b1c2017-06-30 11:55:01 -07002170 is_superclass_initialized &&
Chang Xing70f689d2017-06-08 17:16:12 -07002171 !too_many_encoded_fields &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +01002172 manager_->GetCompiler()->GetCompilerOptions().IsImageClass(descriptor)) {
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002173 bool can_init_static_fields = false;
Mathieu Chartierdabeb3a2017-06-12 17:10:07 -07002174 if (is_boot_image) {
Mathieu Chartier90443472015-07-16 20:32:27 -07002175 // We need to initialize static fields, we only do this for image classes that aren't
Mathieu Chartierdabeb3a2017-06-12 17:10:07 -07002176 // marked with the $NoPreloadHolder (which implies this should not be initialized
2177 // early).
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002178 can_init_static_fields = !StringPiece(descriptor).ends_with("$NoPreloadHolder;");
2179 } else {
Mathieu Chartier17498e52017-06-13 11:35:04 -07002180 CHECK(is_app_image);
Mathieu Chartierdabeb3a2017-06-12 17:10:07 -07002181 // The boot image case doesn't need to recursively initialize the dependencies with
2182 // special logic since the class linker already does this.
Mathieu Chartierdabeb3a2017-06-12 17:10:07 -07002183 can_init_static_fields =
Mathieu Chartier0933cc52018-03-23 14:25:08 -07002184 ClassLinker::kAppImageMayContainStrings &&
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002185 !soa.Self()->IsExceptionPending() &&
Nicolas Geoffrayabadf022017-08-03 08:25:41 +00002186 is_superclass_initialized &&
2187 NoClinitInDependency(klass, soa.Self(), &class_loader);
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002188 // TODO The checking for clinit can be removed since it's already
2189 // checked when init superclass. Currently keep it because it contains
2190 // processing of intern strings. Will be removed later when intern strings
2191 // and clinit are both initialized.
2192 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002193
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002194 if (can_init_static_fields) {
2195 VLOG(compiler) << "Initializing: " << descriptor;
2196 // TODO multithreading support. We should ensure the current compilation thread has
2197 // exclusive access to the runtime and the transaction. To achieve this, we could use
2198 // a ReaderWriterMutex but we're holding the mutator lock so we fail mutex sanity
2199 // checks in Thread::AssertThreadSuspensionIsAllowable.
2200 Runtime* const runtime = Runtime::Current();
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002201 // Run the class initializer in transaction mode.
Chang Xing5a906fc2017-07-26 15:01:16 -07002202 runtime->EnterTransactionMode(is_app_image, klass.Get());
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002203 bool success = manager_->GetClassLinker()->EnsureInitialized(soa.Self(), klass, true,
2204 true);
2205 // TODO we detach transaction from runtime to indicate we quit the transactional
2206 // mode which prevents the GC from visiting objects modified during the transaction.
2207 // Ensure GC is not run so don't access freed objects when aborting transaction.
Andreas Gampe9e7078b2017-03-02 13:50:36 -08002208
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002209 {
2210 ScopedAssertNoThreadSuspension ants("Transaction end");
Chang Xing16d1dd82017-07-20 17:56:26 -07002211
2212 if (success) {
2213 runtime->ExitTransactionMode();
2214 DCHECK(!runtime->IsActiveTransaction());
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002215
Chris Wailes0c61be42018-09-26 17:27:34 -07002216 if (is_boot_image) {
2217 // For boot image, we want to put the updated status in the oat class since we
2218 // can't reject the image anyways.
2219 old_status = klass->GetStatus();
2220 }
2221 } else {
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002222 CHECK(soa.Self()->IsExceptionPending());
2223 mirror::Throwable* exception = soa.Self()->GetException();
2224 VLOG(compiler) << "Initialization of " << descriptor << " aborted because of "
2225 << exception->Dump();
2226 std::ostream* file_log = manager_->GetCompiler()->
2227 GetCompilerOptions().GetInitFailureOutput();
2228 if (file_log != nullptr) {
2229 *file_log << descriptor << "\n";
2230 *file_log << exception->Dump() << "\n";
2231 }
2232 soa.Self()->ClearException();
Chang Xing605fe242017-07-20 15:57:21 -07002233 runtime->RollbackAllTransactions();
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002234 CHECK_EQ(old_status, klass->GetStatus()) << "Previous class status not restored";
2235 }
2236 }
2237
Nicolas Geoffrayabadf022017-08-03 08:25:41 +00002238 if (!success) {
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002239 // On failure, still intern strings of static fields and seen in <clinit>, as these
2240 // will be created in the zygote. This is separated from the transaction code just
2241 // above as we will allocate strings, so must be allowed to suspend.
2242 if (&klass->GetDexFile() == manager_->GetDexFile()) {
Andreas Gampe9e7078b2017-03-02 13:50:36 -08002243 InternStrings(klass, class_loader);
Mathieu Chartierdabeb3a2017-06-12 17:10:07 -07002244 } else {
2245 DCHECK(!is_boot_image) << "Boot image must have equal dex files";
Andreas Gampedbfe2542014-11-25 22:21:42 -08002246 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002247 }
2248 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002249 }
Jeff Hao0cb17282017-07-12 14:51:49 -07002250 // If the class still isn't initialized, at least try some checks that initialization
2251 // would do so they can be skipped at runtime.
2252 if (!klass->IsInitialized() &&
2253 manager_->GetClassLinker()->ValidateSuperClassDescriptors(klass)) {
Vladimir Marko2c64a832018-01-04 11:31:56 +00002254 old_status = ClassStatus::kSuperclassValidated;
Jeff Hao0cb17282017-07-12 14:51:49 -07002255 } else {
2256 soa.Self()->ClearException();
2257 }
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002258 soa.Self()->AssertNoPendingException();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002259 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002260 }
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002261 // Record the final class status if necessary.
2262 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
2263 // Back up the status before doing initialization for static encoded fields,
2264 // because the static encoded branch wants to keep the status to uninitialized.
2265 manager_->GetCompiler()->RecordClassStatus(ref, old_status);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002266 }
Mathieu Chartier90443472015-07-16 20:32:27 -07002267
2268 private:
Andreas Gampe9e7078b2017-03-02 13:50:36 -08002269 void InternStrings(Handle<mirror::Class> klass, Handle<mirror::ClassLoader> class_loader)
2270 REQUIRES_SHARED(Locks::mutator_lock_) {
2271 DCHECK(manager_->GetCompiler()->GetCompilerOptions().IsBootImage());
2272 DCHECK(klass->IsVerified());
2273 DCHECK(!klass->IsInitialized());
2274
2275 StackHandleScope<1> hs(Thread::Current());
Vladimir Markoe11dd502017-12-08 14:09:45 +00002276 Handle<mirror::DexCache> dex_cache = hs.NewHandle(klass->GetDexCache());
Andreas Gampe9e7078b2017-03-02 13:50:36 -08002277 const DexFile::ClassDef* class_def = klass->GetClassDef();
2278 ClassLinker* class_linker = manager_->GetClassLinker();
2279
Andreas Gampe7bf90482017-03-02 16:41:35 -08002280 // Check encoded final field values for strings and intern.
Vladimir Markoe11dd502017-12-08 14:09:45 +00002281 annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache,
2282 class_loader,
Andreas Gampe9e7078b2017-03-02 13:50:36 -08002283 manager_->GetClassLinker(),
2284 *class_def);
2285 for ( ; value_it.HasNext(); value_it.Next()) {
2286 if (value_it.GetValueType() == annotations::RuntimeEncodedStaticFieldValueIterator::kString) {
2287 // Resolve the string. This will intern the string.
2288 art::ObjPtr<mirror::String> resolved = class_linker->ResolveString(
Vladimir Markoa64b52d2017-12-08 16:27:49 +00002289 dex::StringIndex(value_it.GetJavaValue().i), dex_cache);
Andreas Gampe9e7078b2017-03-02 13:50:36 -08002290 CHECK(resolved != nullptr);
2291 }
2292 }
Andreas Gampe7bf90482017-03-02 16:41:35 -08002293
2294 // Intern strings seen in <clinit>.
2295 ArtMethod* clinit = klass->FindClassInitializer(class_linker->GetImagePointerSize());
2296 if (clinit != nullptr) {
Mathieu Chartier73f21d42018-01-02 14:26:50 -08002297 for (const DexInstructionPcPair& inst : clinit->DexInstructions()) {
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07002298 if (inst->Opcode() == Instruction::CONST_STRING) {
Andreas Gampe7bf90482017-03-02 16:41:35 -08002299 ObjPtr<mirror::String> s = class_linker->ResolveString(
Vladimir Markoa64b52d2017-12-08 16:27:49 +00002300 dex::StringIndex(inst->VRegB_21c()), dex_cache);
Andreas Gampe7bf90482017-03-02 16:41:35 -08002301 CHECK(s != nullptr);
Mathieu Chartier2b2bef22017-10-26 17:10:19 -07002302 } else if (inst->Opcode() == Instruction::CONST_STRING_JUMBO) {
Andreas Gampe7bf90482017-03-02 16:41:35 -08002303 ObjPtr<mirror::String> s = class_linker->ResolveString(
Vladimir Markoa64b52d2017-12-08 16:27:49 +00002304 dex::StringIndex(inst->VRegB_31c()), dex_cache);
Andreas Gampe7bf90482017-03-02 16:41:35 -08002305 CHECK(s != nullptr);
2306 }
Andreas Gampe7bf90482017-03-02 16:41:35 -08002307 }
2308 }
Andreas Gampe9e7078b2017-03-02 13:50:36 -08002309 }
2310
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002311 bool ResolveTypesOfMethods(Thread* self, ArtMethod* m)
2312 REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Markob45528c2017-07-27 14:14:28 +01002313 // Return value of ResolveReturnType() is discarded because resolve will be done internally.
2314 ObjPtr<mirror::Class> rtn_type = m->ResolveReturnType();
Mathieu Chartier7a8bcfd2017-06-13 13:40:43 -07002315 if (rtn_type == nullptr) {
2316 self->ClearException();
2317 return false;
2318 }
2319 const DexFile::TypeList* types = m->GetParameterTypeList();
2320 if (types != nullptr) {
2321 for (uint32_t i = 0; i < types->Size(); ++i) {
2322 dex::TypeIndex param_type_idx = types->GetTypeItem(i).type_idx_;
Vladimir Markob45528c2017-07-27 14:14:28 +01002323 ObjPtr<mirror::Class> param_type = m->ResolveClassFromTypeIndex(param_type_idx);
Mathieu Chartier7a8bcfd2017-06-13 13:40:43 -07002324 if (param_type == nullptr) {
2325 self->ClearException();
2326 return false;
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002327 }
2328 }
Mathieu Chartier7a8bcfd2017-06-13 13:40:43 -07002329 }
2330 return true;
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002331 }
2332
2333 // Pre resolve types mentioned in all method signatures before start a transaction
2334 // since ResolveType doesn't work in transaction mode.
2335 bool PreResolveTypes(Thread* self, const Handle<mirror::Class>& klass)
2336 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier7a8bcfd2017-06-13 13:40:43 -07002337 PointerSize pointer_size = manager_->GetClassLinker()->GetImagePointerSize();
2338 for (ArtMethod& m : klass->GetMethods(pointer_size)) {
2339 if (!ResolveTypesOfMethods(self, &m)) {
2340 return false;
2341 }
2342 }
2343 if (klass->IsInterface()) {
2344 return true;
2345 } else if (klass->HasSuperClass()) {
2346 StackHandleScope<1> hs(self);
2347 MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(klass->GetSuperClass()));
2348 for (int i = super_klass->GetVTableLength() - 1; i >= 0; --i) {
2349 ArtMethod* m = klass->GetVTableEntry(i, pointer_size);
2350 ArtMethod* super_m = super_klass->GetVTableEntry(i, pointer_size);
2351 if (!ResolveTypesOfMethods(self, m) || !ResolveTypesOfMethods(self, super_m)) {
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002352 return false;
2353 }
2354 }
Mathieu Chartier7a8bcfd2017-06-13 13:40:43 -07002355 for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) {
2356 super_klass.Assign(klass->GetIfTable()->GetInterface(i));
2357 if (klass->GetClassLoader() != super_klass->GetClassLoader()) {
2358 uint32_t num_methods = super_klass->NumVirtualMethods();
2359 for (uint32_t j = 0; j < num_methods; ++j) {
2360 ArtMethod* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>(
2361 j, pointer_size);
2362 ArtMethod* super_m = super_klass->GetVirtualMethod(j, pointer_size);
2363 if (!ResolveTypesOfMethods(self, m) || !ResolveTypesOfMethods(self, super_m)) {
2364 return false;
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002365 }
2366 }
2367 }
2368 }
Mathieu Chartier7a8bcfd2017-06-13 13:40:43 -07002369 }
2370 return true;
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002371 }
2372
2373 // Initialize the klass's dependencies recursively before initializing itself.
2374 // Checking for interfaces is also necessary since interfaces can contain
2375 // both default methods and static encoded fields.
2376 bool InitializeDependencies(const Handle<mirror::Class>& klass,
2377 Handle<mirror::ClassLoader> class_loader,
2378 Thread* self)
2379 REQUIRES_SHARED(Locks::mutator_lock_) {
2380 if (klass->HasSuperClass()) {
2381 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
2382 StackHandleScope<1> hs(self);
2383 Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class));
2384 if (!handle_scope_super->IsInitialized()) {
2385 this->TryInitializeClass(handle_scope_super, class_loader);
2386 if (!handle_scope_super->IsInitialized()) {
2387 return false;
2388 }
2389 }
2390 }
2391
2392 uint32_t num_if = klass->NumDirectInterfaces();
2393 for (size_t i = 0; i < num_if; i++) {
2394 ObjPtr<mirror::Class>
2395 interface = mirror::Class::GetDirectInterface(self, klass.Get(), i);
2396 StackHandleScope<1> hs(self);
2397 Handle<mirror::Class> handle_interface(hs.NewHandle(interface));
2398
2399 TryInitializeClass(handle_interface, class_loader);
2400
2401 if (!handle_interface->IsInitialized()) {
2402 return false;
2403 }
2404 }
2405
2406 return PreResolveTypes(self, klass);
2407 }
2408
2409 // In this phase the classes containing class initializers are ignored. Make sure no
2410 // clinit appears in kalss's super class chain and interfaces.
2411 bool NoClinitInDependency(const Handle<mirror::Class>& klass,
2412 Thread* self,
2413 Handle<mirror::ClassLoader>* class_loader)
2414 REQUIRES_SHARED(Locks::mutator_lock_) {
2415 ArtMethod* clinit =
2416 klass->FindClassInitializer(manager_->GetClassLinker()->GetImagePointerSize());
2417 if (clinit != nullptr) {
2418 VLOG(compiler) << klass->PrettyClass() << ' ' << clinit->PrettyMethod(true);
2419 return false;
2420 }
2421 if (klass->HasSuperClass()) {
2422 ObjPtr<mirror::Class> super_class = klass->GetSuperClass();
2423 StackHandleScope<1> hs(self);
2424 Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class));
2425 if (!NoClinitInDependency(handle_scope_super, self, class_loader)) {
2426 return false;
2427 }
2428 }
2429
2430 uint32_t num_if = klass->NumDirectInterfaces();
2431 for (size_t i = 0; i < num_if; i++) {
2432 ObjPtr<mirror::Class>
2433 interface = mirror::Class::GetDirectInterface(self, klass.Get(), i);
2434 StackHandleScope<1> hs(self);
2435 Handle<mirror::Class> handle_interface(hs.NewHandle(interface));
2436 if (!NoClinitInDependency(handle_interface, self, class_loader)) {
2437 return false;
2438 }
2439 }
2440
Chang Xingba17dbd2017-06-28 21:27:56 +00002441 return true;
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002442 }
2443
Mathieu Chartier90443472015-07-16 20:32:27 -07002444 const ParallelCompilationManager* const manager_;
2445};
Brian Carlstrom7940e442013-07-12 13:46:57 -07002446
Andreas Gampeace0dc12016-01-20 13:33:13 -08002447void CompilerDriver::InitializeClasses(jobject jni_class_loader,
2448 const DexFile& dex_file,
Andreas Gampede7b4362014-07-28 18:38:57 -07002449 const std::vector<const DexFile*>& dex_files,
Andreas Gampeace0dc12016-01-20 13:33:13 -08002450 TimingLogger* timings) {
Mathieu Chartierf5997b42014-06-20 10:37:54 -07002451 TimingLogger::ScopedTiming t("InitializeNoClinit", timings);
Andreas Gampeace0dc12016-01-20 13:33:13 -08002452
2453 // Initialization allocates objects and needs to run single-threaded to be deterministic.
2454 bool force_determinism = GetCompilerOptions().IsForceDeterminism();
2455 ThreadPool* init_thread_pool = force_determinism
2456 ? single_thread_pool_.get()
2457 : parallel_thread_pool_.get();
2458 size_t init_thread_count = force_determinism ? 1U : parallel_thread_count_;
2459
Brian Carlstrom7940e442013-07-12 13:46:57 -07002460 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampede7b4362014-07-28 18:38:57 -07002461 ParallelCompilationManager context(class_linker, jni_class_loader, this, &dex_file, dex_files,
Andreas Gampeace0dc12016-01-20 13:33:13 -08002462 init_thread_pool);
Mathieu Chartierf1dd69a2017-06-08 23:30:15 +00002463
2464 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsAppImage()) {
2465 // Set the concurrency thread to 1 to support initialization for App Images since transaction
2466 // doesn't support multithreading now.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002467 // TODO: remove this when transactional mode supports multithreading.
Andreas Gampeace0dc12016-01-20 13:33:13 -08002468 init_thread_count = 1U;
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +01002469 }
Mathieu Chartier90443472015-07-16 20:32:27 -07002470 InitializeClassVisitor visitor(&context);
Andreas Gampeace0dc12016-01-20 13:33:13 -08002471 context.ForAll(0, dex_file.NumClassDefs(), &visitor, init_thread_count);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002472}
2473
Mathieu Chartier91288d82016-04-28 09:44:54 -07002474class InitializeArrayClassesAndCreateConflictTablesVisitor : public ClassVisitor {
Mathieu Chartier085a0722016-04-01 17:33:31 -07002475 public:
Mathieu Chartier28357fa2016-10-18 16:27:40 -07002476 explicit InitializeArrayClassesAndCreateConflictTablesVisitor(VariableSizedHandleScope& hs)
2477 : hs_(hs) {}
2478
Roland Levillainf73caca2018-08-24 17:19:07 +01002479 bool operator()(ObjPtr<mirror::Class> klass) override
Mathieu Chartier28357fa2016-10-18 16:27:40 -07002480 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier91288d82016-04-28 09:44:54 -07002481 if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) {
2482 return true;
2483 }
Mathieu Chartier085a0722016-04-01 17:33:31 -07002484 if (klass->IsArrayClass()) {
2485 StackHandleScope<1> hs(Thread::Current());
Mathieu Chartier28357fa2016-10-18 16:27:40 -07002486 auto h_klass = hs.NewHandleWrapper(&klass);
2487 Runtime::Current()->GetClassLinker()->EnsureInitialized(hs.Self(), h_klass, true, true);
Mathieu Chartier085a0722016-04-01 17:33:31 -07002488 }
Mathieu Chartier28357fa2016-10-18 16:27:40 -07002489 // Collect handles since there may be thread suspension in future EnsureInitialized.
2490 to_visit_.push_back(hs_.NewHandle(klass));
Nicolas Geoffray88f288e2016-06-29 08:17:52 +00002491 return true;
Nelli Kimbadee982016-05-13 13:08:53 +03002492 }
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002493
Mathieu Chartier28357fa2016-10-18 16:27:40 -07002494 void FillAllIMTAndConflictTables() REQUIRES_SHARED(Locks::mutator_lock_) {
2495 for (Handle<mirror::Class> c : to_visit_) {
2496 // Create the conflict tables.
2497 FillIMTAndConflictTables(c.Get());
2498 }
2499 }
2500
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002501 private:
Mathieu Chartier28357fa2016-10-18 16:27:40 -07002502 void FillIMTAndConflictTables(ObjPtr<mirror::Class> klass)
2503 REQUIRES_SHARED(Locks::mutator_lock_) {
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002504 if (!klass->ShouldHaveImt()) {
2505 return;
2506 }
2507 if (visited_classes_.find(klass) != visited_classes_.end()) {
2508 return;
2509 }
2510 if (klass->HasSuperClass()) {
2511 FillIMTAndConflictTables(klass->GetSuperClass());
2512 }
2513 if (!klass->IsTemp()) {
2514 Runtime::Current()->GetClassLinker()->FillIMTAndConflictTables(klass);
2515 }
2516 visited_classes_.insert(klass);
2517 }
2518
Mathieu Chartier28357fa2016-10-18 16:27:40 -07002519 VariableSizedHandleScope& hs_;
2520 std::vector<Handle<mirror::Class>> to_visit_;
2521 std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr> visited_classes_;
Mathieu Chartier085a0722016-04-01 17:33:31 -07002522};
2523
Brian Carlstrom7940e442013-07-12 13:46:57 -07002524void CompilerDriver::InitializeClasses(jobject class_loader,
2525 const std::vector<const DexFile*>& dex_files,
Andreas Gampeace0dc12016-01-20 13:33:13 -08002526 TimingLogger* timings) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07002527 for (size_t i = 0; i != dex_files.size(); ++i) {
2528 const DexFile* dex_file = dex_files[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002529 CHECK(dex_file != nullptr);
Andreas Gampeace0dc12016-01-20 13:33:13 -08002530 InitializeClasses(class_loader, *dex_file, dex_files, timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002531 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00002532 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsAppImage()) {
Mathieu Chartier085a0722016-04-01 17:33:31 -07002533 // Make sure that we call EnsureIntiailized on all the array classes to call
2534 // SetVerificationAttempted so that the access flags are set. If we do not do this they get
2535 // changed at runtime resulting in more dirty image pages.
Mathieu Chartier91288d82016-04-28 09:44:54 -07002536 // Also create conflict tables.
2537 // Only useful if we are compiling an image (image_classes_ is not null).
Mathieu Chartier085a0722016-04-01 17:33:31 -07002538 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier28357fa2016-10-18 16:27:40 -07002539 VariableSizedHandleScope hs(soa.Self());
2540 InitializeArrayClassesAndCreateConflictTablesVisitor visitor(hs);
Mathieu Chartier91288d82016-04-28 09:44:54 -07002541 Runtime::Current()->GetClassLinker()->VisitClassesWithoutClassesLock(&visitor);
Mathieu Chartier28357fa2016-10-18 16:27:40 -07002542 visitor.FillAllIMTAndConflictTables();
Mathieu Chartier085a0722016-04-01 17:33:31 -07002543 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00002544 if (GetCompilerOptions().IsBootImage()) {
Mathieu Chartier093ef212014-08-11 13:52:12 -07002545 // Prune garbage objects created during aborted transactions.
Roland Levillainaf290312018-02-27 20:02:17 +00002546 Runtime::Current()->GetHeap()->CollectGarbage(/* clear_soft_references */ true);
Mathieu Chartier093ef212014-08-11 13:52:12 -07002547 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002548}
2549
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002550template <typename CompileFn>
2551static void CompileDexFile(CompilerDriver* driver,
2552 jobject class_loader,
2553 const DexFile& dex_file,
2554 const std::vector<const DexFile*>& dex_files,
2555 ThreadPool* thread_pool,
2556 size_t thread_count,
2557 TimingLogger* timings,
2558 const char* timing_name,
2559 CompileFn compile_fn) {
2560 TimingLogger::ScopedTiming t(timing_name, timings);
2561 ParallelCompilationManager context(Runtime::Current()->GetClassLinker(),
2562 class_loader,
2563 driver,
2564 &dex_file,
2565 dex_files,
2566 thread_pool);
Vladimir Marko492a7fa2016-06-01 18:38:43 +01002567
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002568 auto compile = [&context, &compile_fn](size_t class_def_index) {
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002569 const DexFile& dex_file = *context.GetDexFile();
Andreas Gampe3e7f66b2018-09-06 10:08:18 -07002570 SCOPED_TRACE << "compile " << dex_file.GetLocation() << "@" << class_def_index;
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002571 ClassLinker* class_linker = context.GetClassLinker();
2572 jobject jclass_loader = context.GetClassLoader();
Mathieu Chartier90443472015-07-16 20:32:27 -07002573 ClassReference ref(&dex_file, class_def_index);
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07002574 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Mathieu Chartier18e26872018-06-04 17:19:02 -07002575 ClassAccessor accessor(dex_file, class_def_index);
Mathieu Chartier90443472015-07-16 20:32:27 -07002576 // Skip compiling classes with generic verifier failures since they will still fail at runtime
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002577 if (context.GetCompiler()->GetVerificationResults()->IsClassRejected(ref)) {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002578 return;
2579 }
Mathieu Chartier736b5602015-09-02 14:54:11 -07002580 // Use a scoped object access to perform to the quick SkipClass check.
Mathieu Chartier736b5602015-09-02 14:54:11 -07002581 ScopedObjectAccess soa(Thread::Current());
2582 StackHandleScope<3> hs(soa.Self());
2583 Handle<mirror::ClassLoader> class_loader(
Mathieu Chartier0795f232016-09-27 18:43:30 -07002584 hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
Mathieu Chartier736b5602015-09-02 14:54:11 -07002585 Handle<mirror::Class> klass(
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07002586 hs.NewHandle(class_linker->FindClass(soa.Self(), accessor.GetDescriptor(), class_loader)));
Mathieu Chartier736b5602015-09-02 14:54:11 -07002587 Handle<mirror::DexCache> dex_cache;
Andreas Gampefa4333d2017-02-14 11:10:34 -08002588 if (klass == nullptr) {
Mathieu Chartier736b5602015-09-02 14:54:11 -07002589 soa.Self()->AssertPendingException();
2590 soa.Self()->ClearException();
2591 dex_cache = hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
2592 } else if (SkipClass(jclass_loader, dex_file, klass.Get())) {
2593 return;
Andreas Gampebb30d5d2018-04-23 09:59:25 -07002594 } else if (&klass->GetDexFile() != &dex_file) {
2595 // Skip a duplicate class (as the resolved class is from another, earlier dex file).
2596 return; // Do not update state.
Mathieu Chartier736b5602015-09-02 14:54:11 -07002597 } else {
2598 dex_cache = hs.NewHandle(klass->GetDexCache());
2599 }
2600
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07002601 // Avoid suspension if there are no methods to compile.
2602 if (accessor.NumDirectMethods() + accessor.NumVirtualMethods() == 0) {
Mathieu Chartier90443472015-07-16 20:32:27 -07002603 return;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002604 }
Mathieu Chartier90443472015-07-16 20:32:27 -07002605
Mathieu Chartier736b5602015-09-02 14:54:11 -07002606 // Go to native so that we don't block GC during compilation.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07002607 ScopedThreadSuspension sts(soa.Self(), kNative);
Mathieu Chartier736b5602015-09-02 14:54:11 -07002608
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002609 CompilerDriver* const driver = context.GetCompiler();
Mathieu Chartier90443472015-07-16 20:32:27 -07002610
2611 // Can we run DEX-to-DEX compiler on this class ?
Mathieu Chartiera79efdb2018-01-18 16:31:01 -08002612 optimizer::DexToDexCompiler::CompilationLevel dex_to_dex_compilation_level =
Mathieu Chartier736b5602015-09-02 14:54:11 -07002613 GetDexToDexCompilationLevel(soa.Self(), *driver, jclass_loader, dex_file, class_def);
Andreas Gampe5eb0d382015-07-23 01:19:26 -07002614
Mathieu Chartier90443472015-07-16 20:32:27 -07002615
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07002616 const bool compilation_enabled = driver->IsClassToCompile(accessor.GetDescriptor());
Mathieu Chartier90443472015-07-16 20:32:27 -07002617
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08002618 // Compile direct and virtual methods.
2619 int64_t previous_method_idx = -1;
Mathieu Chartier0d896bd2018-05-25 00:20:27 -07002620 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07002621 const uint32_t method_idx = method.GetIndex();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08002622 if (method_idx == previous_method_idx) {
Mathieu Chartier90443472015-07-16 20:32:27 -07002623 // smali can create dex files with two encoded_methods sharing the same method_idx
2624 // http://code.google.com/p/smali/issues/detail?id=119
Mathieu Chartier0d896bd2018-05-25 00:20:27 -07002625 continue;
Mathieu Chartier90443472015-07-16 20:32:27 -07002626 }
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08002627 previous_method_idx = method_idx;
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002628 compile_fn(soa.Self(),
2629 driver,
Mathieu Chartierc8c8d5f2018-05-22 11:56:14 -07002630 method.GetCodeItem(),
2631 method.GetAccessFlags(),
2632 method.GetInvokeType(class_def.access_flags_),
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002633 class_def_index,
2634 method_idx,
2635 class_loader,
2636 dex_file,
2637 dex_to_dex_compilation_level,
2638 compilation_enabled,
2639 dex_cache);
Mathieu Chartier0d896bd2018-05-25 00:20:27 -07002640 }
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002641 };
2642 context.ForAllLambda(0, dex_file.NumClassDefs(), compile, thread_count);
2643}
2644
2645void CompilerDriver::Compile(jobject class_loader,
2646 const std::vector<const DexFile*>& dex_files,
2647 TimingLogger* timings) {
2648 if (kDebugProfileGuidedCompilation) {
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +00002649 const ProfileCompilationInfo* profile_compilation_info =
2650 GetCompilerOptions().GetProfileCompilationInfo();
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002651 LOG(INFO) << "[ProfileGuidedCompilation] " <<
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +00002652 ((profile_compilation_info == nullptr)
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002653 ? "null"
Vladimir Marko1a2a5cd2018-11-07 15:39:48 +00002654 : profile_compilation_info->DumpInfo(dex_files));
Brian Carlstrom7940e442013-07-12 13:46:57 -07002655 }
Mathieu Chartier90443472015-07-16 20:32:27 -07002656
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002657 dex_to_dex_compiler_.ClearState();
2658 for (const DexFile* dex_file : dex_files) {
2659 CHECK(dex_file != nullptr);
2660 CompileDexFile(this,
2661 class_loader,
2662 *dex_file,
2663 dex_files,
2664 parallel_thread_pool_.get(),
2665 parallel_thread_count_,
2666 timings,
2667 "Compile Dex File Quick",
2668 CompileMethodQuick);
2669 const ArenaPool* const arena_pool = Runtime::Current()->GetArenaPool();
2670 const size_t arena_alloc = arena_pool->GetBytesAllocated();
2671 max_arena_alloc_ = std::max(arena_alloc, max_arena_alloc_);
2672 Runtime::Current()->ReclaimArenaPoolMemory();
2673 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07002674
Mathieu Chartier279e3a32018-01-24 18:17:55 -08002675 if (dex_to_dex_compiler_.NumCodeItemsToQuicken(Thread::Current()) > 0u) {
Andreas Gampe3c060ad2018-01-22 13:07:49 -08002676 // TODO: Not visit all of the dex files, its probably rare that only one would have quickened
2677 // methods though.
2678 for (const DexFile* dex_file : dex_files) {
2679 CompileDexFile(this,
2680 class_loader,
2681 *dex_file,
2682 dex_files,
2683 parallel_thread_pool_.get(),
2684 parallel_thread_count_,
2685 timings,
2686 "Compile Dex File Dex2Dex",
2687 CompileMethodDex2Dex);
2688 }
2689 dex_to_dex_compiler_.ClearState();
2690 }
2691
2692 VLOG(compiler) << "Compile: " << GetMemoryUsageString(false);
Brian Carlstrom7940e442013-07-12 13:46:57 -07002693}
2694
Andreas Gampe5eb0d382015-07-23 01:19:26 -07002695void CompilerDriver::AddCompiledMethod(const MethodReference& method_ref,
Vladimir Marko8e524ad2018-07-13 10:27:43 +01002696 CompiledMethod* const compiled_method) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002697 DCHECK(GetCompiledMethod(method_ref) == nullptr) << method_ref.PrettyMethod();
2698 MethodTable::InsertResult result = compiled_methods_.Insert(method_ref,
2699 /*expected*/ nullptr,
2700 compiled_method);
Mathieu Chartieracab8d42016-11-23 13:45:58 -08002701 CHECK(result == MethodTable::kInsertResultSuccess);
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002702 DCHECK(GetCompiledMethod(method_ref) != nullptr) << method_ref.PrettyMethod();
Brian Carlstrom7940e442013-07-12 13:46:57 -07002703}
2704
Mathieu Chartier279e3a32018-01-24 18:17:55 -08002705CompiledMethod* CompilerDriver::RemoveCompiledMethod(const MethodReference& method_ref) {
2706 CompiledMethod* ret = nullptr;
2707 CHECK(compiled_methods_.Remove(method_ref, &ret));
2708 return ret;
2709}
2710
Vladimir Marko2c64a832018-01-04 11:31:56 +00002711bool CompilerDriver::GetCompiledClass(const ClassReference& ref, ClassStatus* status) const {
Andreas Gampebb846102017-05-11 21:03:35 -07002712 DCHECK(status != nullptr);
Mathieu Chartier1a088d42017-07-18 11:43:57 -07002713 // The table doesn't know if something wasn't inserted. For this case it will return
Vladimir Marko2c64a832018-01-04 11:31:56 +00002714 // ClassStatus::kNotReady. To handle this, just assume anything we didn't try to verify
2715 // is not compiled.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002716 if (!compiled_classes_.Get(ref, status) ||
Vladimir Marko2c64a832018-01-04 11:31:56 +00002717 *status < ClassStatus::kRetryVerificationAtRuntime) {
Andreas Gampebb846102017-05-11 21:03:35 -07002718 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002719 }
Andreas Gampebb846102017-05-11 21:03:35 -07002720 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002721}
2722
Vladimir Marko2c64a832018-01-04 11:31:56 +00002723ClassStatus CompilerDriver::GetClassStatus(const ClassReference& ref) const {
2724 ClassStatus status = ClassStatus::kNotReady;
Nicolas Geoffray486dda02017-09-11 14:15:52 +01002725 if (!GetCompiledClass(ref, &status)) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002726 classpath_classes_.Get(ref, &status);
Nicolas Geoffray486dda02017-09-11 14:15:52 +01002727 }
2728 return status;
2729}
2730
Vladimir Marko2c64a832018-01-04 11:31:56 +00002731void CompilerDriver::RecordClassStatus(const ClassReference& ref, ClassStatus status) {
Nicolas Geoffrayade72d62016-12-15 13:20:02 +00002732 switch (status) {
Vladimir Marko2c64a832018-01-04 11:31:56 +00002733 case ClassStatus::kErrorResolved:
2734 case ClassStatus::kErrorUnresolved:
2735 case ClassStatus::kNotReady:
2736 case ClassStatus::kResolved:
2737 case ClassStatus::kRetryVerificationAtRuntime:
2738 case ClassStatus::kVerified:
2739 case ClassStatus::kSuperclassValidated:
2740 case ClassStatus::kInitialized:
Nicolas Geoffrayade72d62016-12-15 13:20:02 +00002741 break; // Expected states.
2742 default:
2743 LOG(FATAL) << "Unexpected class status for class "
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002744 << PrettyDescriptor(
2745 ref.dex_file->GetClassDescriptor(ref.dex_file->GetClassDef(ref.index)))
Nicolas Geoffrayade72d62016-12-15 13:20:02 +00002746 << " of " << status;
2747 }
2748
Mathieu Chartier1a088d42017-07-18 11:43:57 -07002749 ClassStateTable::InsertResult result;
Nicolas Geoffray486dda02017-09-11 14:15:52 +01002750 ClassStateTable* table = &compiled_classes_;
Mathieu Chartier1a088d42017-07-18 11:43:57 -07002751 do {
Vladimir Marko2c64a832018-01-04 11:31:56 +00002752 ClassStatus existing = ClassStatus::kNotReady;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002753 if (!table->Get(ref, &existing)) {
Nicolas Geoffray486dda02017-09-11 14:15:52 +01002754 // A classpath class.
Mathieu Chartier010f5cc2017-07-24 15:53:46 -07002755 if (kIsDebugBuild) {
2756 // Check to make sure it's not a dex file for an oat file we are compiling since these
2757 // should always succeed. These do not include classes in for used libraries.
Vladimir Marko213ee2d2018-06-22 11:56:34 +01002758 for (const DexFile* dex_file : GetCompilerOptions().GetDexFilesForOatFile()) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002759 CHECK_NE(ref.dex_file, dex_file) << ref.dex_file->GetLocation();
Mathieu Chartier010f5cc2017-07-24 15:53:46 -07002760 }
2761 }
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002762 if (!classpath_classes_.HaveDexFile(ref.dex_file)) {
Nicolas Geoffray486dda02017-09-11 14:15:52 +01002763 // Boot classpath dex file.
2764 return;
2765 }
2766 table = &classpath_classes_;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002767 table->Get(ref, &existing);
Mathieu Chartier010f5cc2017-07-24 15:53:46 -07002768 }
Mathieu Chartier1a088d42017-07-18 11:43:57 -07002769 if (existing >= status) {
2770 // Existing status is already better than we expect, break.
2771 break;
2772 }
Nicolas Geoffrayade72d62016-12-15 13:20:02 +00002773 // Update the status if we now have a greater one. This happens with vdex,
2774 // which records a class is verified, but does not resolve it.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002775 result = table->Insert(ref, existing, status);
2776 CHECK(result != ClassStateTable::kInsertResultInvalidDexFile) << ref.dex_file->GetLocation();
Mathieu Chartier1a088d42017-07-18 11:43:57 -07002777 } while (result != ClassStateTable::kInsertResultSuccess);
Ian Rogers8f3c9ae2013-08-20 17:26:41 -07002778}
2779
Brian Carlstrom7940e442013-07-12 13:46:57 -07002780CompiledMethod* CompilerDriver::GetCompiledMethod(MethodReference ref) const {
Mathieu Chartieracab8d42016-11-23 13:45:58 -08002781 CompiledMethod* compiled_method = nullptr;
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07002782 compiled_methods_.Get(ref, &compiled_method);
Mathieu Chartieracab8d42016-11-23 13:45:58 -08002783 return compiled_method;
Brian Carlstrom7940e442013-07-12 13:46:57 -07002784}
2785
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01002786bool CompilerDriver::IsMethodVerifiedWithoutFailures(uint32_t method_idx,
2787 uint16_t class_def_idx,
2788 const DexFile& dex_file) const {
2789 const VerifiedMethod* verified_method = GetVerifiedMethod(&dex_file, method_idx);
2790 if (verified_method != nullptr) {
2791 return !verified_method->HasVerificationFailures();
2792 }
2793
2794 // If we can't find verification metadata, check if this is a system class (we trust that system
2795 // classes have their methods verified). If it's not, be conservative and assume the method
2796 // has not been verified successfully.
2797
2798 // TODO: When compiling the boot image it should be safe to assume that everything is verified,
2799 // even if methods are not found in the verification cache.
2800 const char* descriptor = dex_file.GetClassDescriptor(dex_file.GetClassDef(class_def_idx));
2801 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2802 Thread* self = Thread::Current();
2803 ScopedObjectAccess soa(self);
2804 bool is_system_class = class_linker->FindSystemClass(self, descriptor) != nullptr;
2805 if (!is_system_class) {
2806 self->ClearException();
2807 }
2808 return is_system_class;
2809}
2810
Andreas Gampe8d295f82015-01-20 14:50:21 -08002811std::string CompilerDriver::GetMemoryUsageString(bool extended) const {
Mathieu Chartierab972ef2014-12-03 17:38:22 -08002812 std::ostringstream oss;
Calin Juravle69158982016-03-16 11:53:41 +00002813 const gc::Heap* const heap = Runtime::Current()->GetHeap();
Anton Kirilovdd9473b2016-01-28 15:08:01 +00002814 const size_t java_alloc = heap->GetBytesAllocated();
Calin Juravle69158982016-03-16 11:53:41 +00002815 oss << "arena alloc=" << PrettySize(max_arena_alloc_) << " (" << max_arena_alloc_ << "B)";
Anton Kirilovdd9473b2016-01-28 15:08:01 +00002816 oss << " java alloc=" << PrettySize(java_alloc) << " (" << java_alloc << "B)";
Elliott Hughes7bf5a262015-04-02 20:55:07 -07002817#if defined(__BIONIC__) || defined(__GLIBC__)
Anton Kirilovdd9473b2016-01-28 15:08:01 +00002818 const struct mallinfo info = mallinfo();
Mathieu Chartierab972ef2014-12-03 17:38:22 -08002819 const size_t allocated_space = static_cast<size_t>(info.uordblks);
2820 const size_t free_space = static_cast<size_t>(info.fordblks);
Anton Kirilovdd9473b2016-01-28 15:08:01 +00002821 oss << " native alloc=" << PrettySize(allocated_space) << " (" << allocated_space << "B)"
2822 << " free=" << PrettySize(free_space) << " (" << free_space << "B)";
Mathieu Chartierab972ef2014-12-03 17:38:22 -08002823#endif
Vladimir Marko35831e82015-09-11 11:59:18 +01002824 compiled_method_storage_.DumpMemoryUsage(oss, extended);
Mathieu Chartierab972ef2014-12-03 17:38:22 -08002825 return oss.str();
2826}
2827
Andreas Gampeace0dc12016-01-20 13:33:13 -08002828void CompilerDriver::InitializeThreadPools() {
2829 size_t parallel_count = parallel_thread_count_ > 0 ? parallel_thread_count_ - 1 : 0;
2830 parallel_thread_pool_.reset(
2831 new ThreadPool("Compiler driver thread pool", parallel_count));
2832 single_thread_pool_.reset(new ThreadPool("Single-threaded Compiler driver thread pool", 0));
2833}
2834
2835void CompilerDriver::FreeThreadPools() {
2836 parallel_thread_pool_.reset();
2837 single_thread_pool_.reset();
2838}
2839
Nicolas Geoffray486dda02017-09-11 14:15:52 +01002840void CompilerDriver::SetClasspathDexFiles(const std::vector<const DexFile*>& dex_files) {
2841 classpath_classes_.AddDexFiles(dex_files);
Mathieu Chartier72041a02017-07-14 18:23:25 -07002842}
2843
Brian Carlstrom7940e442013-07-12 13:46:57 -07002844} // namespace art