1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "runtime.h"
18
19 #include <utility>
20
21 #ifdef __linux__
22 #include <sys/prctl.h>
23 #endif
24
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <sys/mount.h>
28 #include <sys/syscall.h>
29
30 #if defined(__APPLE__)
31 #include <crt_externs.h> // for _NSGetEnviron
32 #endif
33
34 #include <cstdio>
35 #include <cstdlib>
36 #include <limits>
37 #include <string.h>
38 #include <thread>
39 #include <unordered_set>
40 #include <vector>
41
42 #include "android-base/strings.h"
43
44 #include "aot_class_linker.h"
45 #include "arch/arm/registers_arm.h"
46 #include "arch/arm64/registers_arm64.h"
47 #include "arch/context.h"
48 #include "arch/instruction_set_features.h"
49 #include "arch/x86/registers_x86.h"
50 #include "arch/x86_64/registers_x86_64.h"
51 #include "art_field-inl.h"
52 #include "art_method-inl.h"
53 #include "asm_support.h"
54 #include "base/aborting.h"
55 #include "base/arena_allocator.h"
56 #include "base/atomic.h"
57 #include "base/dumpable.h"
58 #include "base/enums.h"
59 #include "base/file_utils.h"
60 #include "base/flags.h"
61 #include "base/malloc_arena_pool.h"
62 #include "base/mem_map_arena_pool.h"
63 #include "base/memory_tool.h"
64 #include "base/mutex.h"
65 #include "base/os.h"
66 #include "base/quasi_atomic.h"
67 #include "base/sdk_version.h"
68 #include "base/stl_util.h"
69 #include "base/systrace.h"
70 #include "base/unix_file/fd_file.h"
71 #include "base/utils.h"
72 #include "class_linker-inl.h"
73 #include "class_root-inl.h"
74 #include "compiler_callbacks.h"
75 #include "debugger.h"
76 #include "dex/art_dex_file_loader.h"
77 #include "dex/dex_file_loader.h"
78 #include "elf_file.h"
79 #include "entrypoints/runtime_asm_entrypoints.h"
80 #include "entrypoints/entrypoint_utils-inl.h"
81 #include "experimental_flags.h"
82 #include "fault_handler.h"
83 #include "gc/accounting/card_table-inl.h"
84 #include "gc/heap.h"
85 #include "gc/scoped_gc_critical_section.h"
86 #include "gc/space/image_space.h"
87 #include "gc/space/space-inl.h"
88 #include "gc/system_weak.h"
89 #include "gc/task_processor.h"
90 #include "handle_scope-inl.h"
91 #include "hidden_api.h"
92 #include "image-inl.h"
93 #include "indirect_reference_table.h"
94 #include "instrumentation.h"
95 #include "intern_table-inl.h"
96 #include "interpreter/interpreter.h"
97 #include "jit/jit.h"
98 #include "jit/jit_code_cache.h"
99 #include "jit/profile_saver.h"
100 #include "jni/java_vm_ext.h"
101 #include "jni/jni_id_manager.h"
102 #include "jni_id_type.h"
103 #include "linear_alloc.h"
104 #include "memory_representation.h"
105 #include "metrics/statsd.h"
106 #include "mirror/array.h"
107 #include "mirror/class-alloc-inl.h"
108 #include "mirror/class-inl.h"
109 #include "mirror/class_ext.h"
110 #include "mirror/class_loader-inl.h"
111 #include "mirror/emulated_stack_frame.h"
112 #include "mirror/field.h"
113 #include "mirror/method.h"
114 #include "mirror/method_handle_impl.h"
115 #include "mirror/method_handles_lookup.h"
116 #include "mirror/method_type.h"
117 #include "mirror/stack_trace_element.h"
118 #include "mirror/throwable.h"
119 #include "mirror/var_handle.h"
120 #include "monitor.h"
121 #include "native/dalvik_system_DexFile.h"
122 #include "native/dalvik_system_BaseDexClassLoader.h"
123 #include "native/dalvik_system_VMDebug.h"
124 #include "native/dalvik_system_VMRuntime.h"
125 #include "native/dalvik_system_VMStack.h"
126 #include "native/dalvik_system_ZygoteHooks.h"
127 #include "native/java_lang_Class.h"
128 #include "native/java_lang_Object.h"
129 #include "native/java_lang_StackStreamFactory.h"
130 #include "native/java_lang_String.h"
131 #include "native/java_lang_StringFactory.h"
132 #include "native/java_lang_System.h"
133 #include "native/java_lang_Thread.h"
134 #include "native/java_lang_Throwable.h"
135 #include "native/java_lang_VMClassLoader.h"
136 #include "native/java_lang_invoke_MethodHandle.h"
137 #include "native/java_lang_invoke_MethodHandleImpl.h"
138 #include "native/java_lang_ref_FinalizerReference.h"
139 #include "native/java_lang_ref_Reference.h"
140 #include "native/java_lang_reflect_Array.h"
141 #include "native/java_lang_reflect_Constructor.h"
142 #include "native/java_lang_reflect_Executable.h"
143 #include "native/java_lang_reflect_Field.h"
144 #include "native/java_lang_reflect_Method.h"
145 #include "native/java_lang_reflect_Parameter.h"
146 #include "native/java_lang_reflect_Proxy.h"
147 #include "native/java_util_concurrent_atomic_AtomicLong.h"
148 #include "native/libcore_io_Memory.h"
149 #include "native/libcore_util_CharsetUtils.h"
150 #include "native/org_apache_harmony_dalvik_ddmc_DdmServer.h"
151 #include "native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.h"
152 #include "native/sun_misc_Unsafe.h"
153 #include "native/jdk_internal_misc_Unsafe.h"
154 #include "native_bridge_art_interface.h"
155 #include "native_stack_dump.h"
156 #include "nativehelper/scoped_local_ref.h"
157 #include "nterp_helpers.h"
158 #include "oat.h"
159 #include "oat_file_manager.h"
160 #include "oat_quick_method_header.h"
161 #include "object_callbacks.h"
162 #include "odr_statslog/odr_statslog.h"
163 #include "parsed_options.h"
164 #include "quick/quick_method_frame_info.h"
165 #include "reflection.h"
166 #include "runtime_callbacks.h"
167 #include "runtime_common.h"
168 #include "runtime_image.h"
169 #include "runtime_intrinsics.h"
170 #include "runtime_options.h"
171 #include "scoped_thread_state_change-inl.h"
172 #include "sigchain.h"
173 #include "signal_catcher.h"
174 #include "signal_set.h"
175 #include "thread.h"
176 #include "thread_list.h"
177 #include "ti/agent.h"
178 #include "trace.h"
179 #include "transaction.h"
180 #include "vdex_file.h"
181 #include "verifier/class_verifier.h"
182 #include "well_known_classes-inl.h"
183
184 #ifdef ART_TARGET_ANDROID
185 #include <android/api-level.h>
186 #include <android/set_abort_message.h>
187 #include "com_android_apex.h"
188 namespace apex = com::android::apex;
189
190 #endif
191
192 // Static asserts to check the values of generated assembly-support macros.
193 #define ASM_DEFINE(NAME, EXPR) static_assert((NAME) == (EXPR), "Unexpected value of " #NAME);
194 #include "asm_defines.def"
195 #undef ASM_DEFINE
196
197 namespace art {
198
199 // If a signal isn't handled properly, enable a handler that attempts to dump the Java stack.
200 static constexpr bool kEnableJavaStackTraceHandler = false;
201 // Tuned by compiling GmsCore under perf and measuring time spent in DescriptorEquals for class
202 // linking.
203 static constexpr double kLowMemoryMinLoadFactor = 0.5;
204 static constexpr double kLowMemoryMaxLoadFactor = 0.8;
205 static constexpr double kNormalMinLoadFactor = 0.4;
206 static constexpr double kNormalMaxLoadFactor = 0.7;
207
208 Runtime* Runtime::instance_ = nullptr;
209
210 struct TraceConfig {
211 Trace::TraceMode trace_mode;
212 Trace::TraceOutputMode trace_output_mode;
213 std::string trace_file;
214 size_t trace_file_size;
215 TraceClockSource clock_source;
216 };
217
218 namespace {
219
220 #ifdef __APPLE__
GetEnviron()221 inline char** GetEnviron() {
222 // When Google Test is built as a framework on MacOS X, the environ variable
223 // is unavailable. Apple's documentation (man environ) recommends using
224 // _NSGetEnviron() instead.
225 return *_NSGetEnviron();
226 }
227 #else
228 // Some POSIX platforms expect you to declare environ. extern "C" makes
229 // it reside in the global namespace.
230 extern "C" char** environ;
231 inline char** GetEnviron() { return environ; }
232 #endif
233
CheckConstants()234 void CheckConstants() {
235 CHECK_EQ(mirror::Array::kFirstElementOffset, mirror::Array::FirstElementOffset());
236 }
237
238 } // namespace
239
Runtime()240 Runtime::Runtime()
241 : resolution_method_(nullptr),
242 imt_conflict_method_(nullptr),
243 imt_unimplemented_method_(nullptr),
244 instruction_set_(InstructionSet::kNone),
245 compiler_callbacks_(nullptr),
246 is_zygote_(false),
247 is_primary_zygote_(false),
248 is_system_server_(false),
249 must_relocate_(false),
250 is_concurrent_gc_enabled_(true),
251 is_explicit_gc_disabled_(false),
252 image_dex2oat_enabled_(true),
253 default_stack_size_(0),
254 heap_(nullptr),
255 max_spins_before_thin_lock_inflation_(Monitor::kDefaultMaxSpinsBeforeThinLockInflation),
256 monitor_list_(nullptr),
257 monitor_pool_(nullptr),
258 thread_list_(nullptr),
259 intern_table_(nullptr),
260 class_linker_(nullptr),
261 signal_catcher_(nullptr),
262 java_vm_(nullptr),
263 thread_pool_ref_count_(0u),
264 fault_message_(nullptr),
265 threads_being_born_(0),
266 shutdown_cond_(new ConditionVariable("Runtime shutdown", *Locks::runtime_shutdown_lock_)),
267 shutting_down_(false),
268 shutting_down_started_(false),
269 started_(false),
270 finished_starting_(false),
271 vfprintf_(nullptr),
272 exit_(nullptr),
273 abort_(nullptr),
274 stats_enabled_(false),
275 is_running_on_memory_tool_(kRunningOnMemoryTool),
276 instrumentation_(),
277 main_thread_group_(nullptr),
278 system_thread_group_(nullptr),
279 system_class_loader_(nullptr),
280 dump_gc_performance_on_shutdown_(false),
281 preinitialization_transactions_(),
282 verify_(verifier::VerifyMode::kNone),
283 target_sdk_version_(static_cast<uint32_t>(SdkVersion::kUnset)),
284 compat_framework_(),
285 implicit_null_checks_(false),
286 implicit_so_checks_(false),
287 implicit_suspend_checks_(false),
288 no_sig_chain_(false),
289 force_native_bridge_(false),
290 is_native_bridge_loaded_(false),
291 is_native_debuggable_(false),
292 async_exceptions_thrown_(false),
293 non_standard_exits_enabled_(false),
294 runtime_debug_state_(RuntimeDebugState::kNonJavaDebuggable),
295 monitor_timeout_enable_(false),
296 monitor_timeout_ns_(0),
297 zygote_max_failed_boots_(0),
298 experimental_flags_(ExperimentalFlags::kNone),
299 oat_file_manager_(nullptr),
300 is_low_memory_mode_(false),
301 madvise_willneed_total_dex_size_(0),
302 madvise_willneed_odex_filesize_(0),
303 madvise_willneed_art_filesize_(0),
304 safe_mode_(false),
305 hidden_api_policy_(hiddenapi::EnforcementPolicy::kDisabled),
306 core_platform_api_policy_(hiddenapi::EnforcementPolicy::kDisabled),
307 test_api_policy_(hiddenapi::EnforcementPolicy::kDisabled),
308 dedupe_hidden_api_warnings_(true),
309 hidden_api_access_event_log_rate_(0),
310 dump_native_stack_on_sig_quit_(true),
311 // Initially assume we perceive jank in case the process state is never updated.
312 process_state_(kProcessStateJankPerceptible),
313 zygote_no_threads_(false),
314 verifier_logging_threshold_ms_(100),
315 verifier_missing_kthrow_fatal_(false),
316 perfetto_hprof_enabled_(false),
317 perfetto_javaheapprof_enabled_(false),
318 out_of_memory_error_hook_(nullptr) {
319 static_assert(Runtime::kCalleeSaveSize ==
320 static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType), "Unexpected size");
321 CheckConstants();
322
323 std::fill(callee_save_methods_, callee_save_methods_ + arraysize(callee_save_methods_), 0u);
324 interpreter::CheckInterpreterAsmConstants();
325 callbacks_.reset(new RuntimeCallbacks());
326 for (size_t i = 0; i <= static_cast<size_t>(DeoptimizationKind::kLast); ++i) {
327 deoptimization_counts_[i] = 0u;
328 }
329 }
330
~Runtime()331 Runtime::~Runtime() {
332 ScopedTrace trace("Runtime shutdown");
333 if (is_native_bridge_loaded_) {
334 UnloadNativeBridge();
335 }
336
337 Thread* self = Thread::Current();
338 const bool attach_shutdown_thread = self == nullptr;
339 if (attach_shutdown_thread) {
340 // We can only create a peer if the runtime is actually started. This is only not true during
341 // some tests. If there is extreme memory pressure the allocation of the thread peer can fail.
342 // In this case we will just try again without allocating a peer so that shutdown can continue.
343 // Very few things are actually capable of distinguishing between the peer & peerless states so
344 // this should be fine.
345 // Running callbacks is prone to deadlocks in libjdwp tests that need an event handler lock to
346 // process any event. We also need to enter a GCCriticalSection when processing certain events
347 // (for ex: removing the last breakpoint). These two restrictions together make the tear down
348 // of the jdwp tests deadlock prone if we fail to finish Thread::Attach callback.
349 // (TODO:b/251163712) Remove this once we update deopt manager to not use GCCriticalSection.
350 bool thread_attached = AttachCurrentThread("Shutdown thread",
351 /* as_daemon= */ false,
352 GetSystemThreadGroup(),
353 /* create_peer= */ IsStarted(),
354 /* should_run_callbacks= */ false);
355 if (UNLIKELY(!thread_attached)) {
356 LOG(WARNING) << "Failed to attach shutdown thread. Trying again without a peer.";
357 CHECK(AttachCurrentThread("Shutdown thread (no java peer)",
358 /* as_daemon= */ false,
359 /* thread_group=*/ nullptr,
360 /* create_peer= */ false));
361 }
362 self = Thread::Current();
363 } else {
364 LOG(WARNING) << "Current thread not detached in Runtime shutdown";
365 }
366
367 if (dump_gc_performance_on_shutdown_) {
368 heap_->CalculatePreGcWeightedAllocatedBytes();
369 uint64_t process_cpu_end_time = ProcessCpuNanoTime();
370 ScopedLogSeverity sls(LogSeverity::INFO);
371 // This can't be called from the Heap destructor below because it
372 // could call RosAlloc::InspectAll() which needs the thread_list
373 // to be still alive.
374 heap_->DumpGcPerformanceInfo(LOG_STREAM(INFO));
375
376 uint64_t process_cpu_time = process_cpu_end_time - heap_->GetProcessCpuStartTime();
377 uint64_t gc_cpu_time = heap_->GetTotalGcCpuTime();
378 float ratio = static_cast<float>(gc_cpu_time) / process_cpu_time;
379 LOG_STREAM(INFO) << "GC CPU time " << PrettyDuration(gc_cpu_time)
380 << " out of process CPU time " << PrettyDuration(process_cpu_time)
381 << " (" << ratio << ")"
382 << "\n";
383 double pre_gc_weighted_allocated_bytes =
384 heap_->GetPreGcWeightedAllocatedBytes() / process_cpu_time;
385 // Here we don't use process_cpu_time for normalization, because VM shutdown is not a real
386 // GC. Both numerator and denominator take into account until the end of the last GC,
387 // instead of the whole process life time like pre_gc_weighted_allocated_bytes.
388 double post_gc_weighted_allocated_bytes =
389 heap_->GetPostGcWeightedAllocatedBytes() /
390 (heap_->GetPostGCLastProcessCpuTime() - heap_->GetProcessCpuStartTime());
391
392 LOG_STREAM(INFO) << "Average bytes allocated at GC start, weighted by CPU time between GCs: "
393 << static_cast<uint64_t>(pre_gc_weighted_allocated_bytes)
394 << " (" << PrettySize(pre_gc_weighted_allocated_bytes) << ")";
395 LOG_STREAM(INFO) << "Average bytes allocated at GC end, weighted by CPU time between GCs: "
396 << static_cast<uint64_t>(post_gc_weighted_allocated_bytes)
397 << " (" << PrettySize(post_gc_weighted_allocated_bytes) << ")"
398 << "\n";
399 }
400
401 // Wait for the workers of thread pools to be created since there can't be any
402 // threads attaching during shutdown.
403 WaitForThreadPoolWorkersToStart();
404 if (jit_ != nullptr) {
405 jit_->WaitForWorkersToBeCreated();
406 // Stop the profile saver thread before marking the runtime as shutting down.
407 // The saver will try to dump the profiles before being sopped and that
408 // requires holding the mutator lock.
409 jit_->StopProfileSaver();
410 // Delete thread pool before the thread list since we don't want to wait forever on the
411 // JIT compiler threads. Also this should be run before marking the runtime
412 // as shutting down as some tasks may require mutator access.
413 jit_->DeleteThreadPool();
414 }
415 if (oat_file_manager_ != nullptr) {
416 oat_file_manager_->WaitForWorkersToBeCreated();
417 }
418 // Disable GC before deleting the thread-pool and shutting down runtime as it
419 // restricts attaching new threads.
420 heap_->DisableGCForShutdown();
421 heap_->WaitForWorkersToBeCreated();
422 // Make sure to let the GC complete if it is running.
423 heap_->WaitForGcToComplete(gc::kGcCauseBackground, self);
424
425 {
426 ScopedTrace trace2("Wait for shutdown cond");
427 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
428 shutting_down_started_ = true;
429 while (threads_being_born_ > 0) {
430 shutdown_cond_->Wait(self);
431 }
432 SetShuttingDown();
433 }
434 // Shutdown and wait for the daemons.
435 CHECK(self != nullptr);
436 if (IsFinishedStarting()) {
437 ScopedTrace trace2("Waiting for Daemons");
438 self->ClearException();
439 ScopedObjectAccess soa(self);
440 WellKnownClasses::java_lang_Daemons_stop->InvokeStatic<'V'>(self);
441 }
442
443 // Shutdown any trace running.
444 Trace::Shutdown();
445
446 // Report death. Clients may require a working thread, still, so do it before GC completes and
447 // all non-daemon threads are done.
448 {
449 ScopedObjectAccess soa(self);
450 callbacks_->NextRuntimePhase(RuntimePhaseCallback::RuntimePhase::kDeath);
451 }
452
453 // Delete thread pools before detaching the current thread in case tasks
454 // getting deleted need to have access to Thread::Current.
455 heap_->DeleteThreadPool();
456 if (oat_file_manager_ != nullptr) {
457 oat_file_manager_->DeleteThreadPool();
458 }
459 DeleteThreadPool();
460 CHECK(thread_pool_ == nullptr);
461
462 if (attach_shutdown_thread) {
463 DetachCurrentThread(/* should_run_callbacks= */ false);
464 self = nullptr;
465 }
466
467 // Make sure our internal threads are dead before we start tearing down things they're using.
468 GetRuntimeCallbacks()->StopDebugger();
469 // Deletion ordering is tricky. Null out everything we've deleted.
470 delete signal_catcher_;
471 signal_catcher_ = nullptr;
472
473 // Shutdown metrics reporting.
474 metrics_reporter_.reset();
475
476 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
477 // Also wait for daemon threads to quiesce, so that in addition to being "suspended", they
478 // no longer access monitor and thread list data structures. We leak user daemon threads
479 // themselves, since we have no mechanism for shutting them down.
480 {
481 ScopedTrace trace2("Delete thread list");
482 thread_list_->ShutDown();
483 }
484
485 // TODO Maybe do some locking.
486 for (auto& agent : agents_) {
487 agent->Unload();
488 }
489
490 // TODO Maybe do some locking
491 for (auto& plugin : plugins_) {
492 plugin.Unload();
493 }
494
495 // Finally delete the thread list.
496 // Thread_list_ can be accessed by "suspended" threads, e.g. in InflateThinLocked.
497 // We assume that by this point, we've waited long enough for things to quiesce.
498 delete thread_list_;
499 thread_list_ = nullptr;
500
501 // Delete the JIT after thread list to ensure that there is no remaining threads which could be
502 // accessing the instrumentation when we delete it.
503 if (jit_ != nullptr) {
504 VLOG(jit) << "Deleting jit";
505 jit_.reset(nullptr);
506 jit_code_cache_.reset(nullptr);
507 }
508
509 // Shutdown the fault manager if it was initialized.
510 fault_manager.Shutdown();
511
512 ScopedTrace trace2("Delete state");
513 delete monitor_list_;
514 monitor_list_ = nullptr;
515 delete monitor_pool_;
516 monitor_pool_ = nullptr;
517 delete class_linker_;
518 class_linker_ = nullptr;
519 delete small_lrt_allocator_;
520 small_lrt_allocator_ = nullptr;
521 delete heap_;
522 heap_ = nullptr;
523 delete intern_table_;
524 intern_table_ = nullptr;
525 delete oat_file_manager_;
526 oat_file_manager_ = nullptr;
527 Thread::Shutdown();
528 QuasiAtomic::Shutdown();
529 verifier::ClassVerifier::Shutdown();
530
531 // Destroy allocators before shutting down the MemMap because they may use it.
532 java_vm_.reset();
533 linear_alloc_.reset();
534 delete ReleaseStartupLinearAlloc();
535 linear_alloc_arena_pool_.reset();
536 arena_pool_.reset();
537 jit_arena_pool_.reset();
538 protected_fault_page_.Reset();
539 MemMap::Shutdown();
540
541 // TODO: acquire a static mutex on Runtime to avoid racing.
542 CHECK(instance_ == nullptr || instance_ == this);
543 instance_ = nullptr;
544
545 // Well-known classes must be deleted or it is impossible to successfully start another Runtime
546 // instance. We rely on a small initialization order issue in Runtime::Start() that requires
547 // elements of WellKnownClasses to be null, see b/65500943.
548 WellKnownClasses::Clear();
549 }
550
551 struct AbortState {
Dumpart::AbortState552 void Dump(std::ostream& os) const {
553 if (gAborting > 1) {
554 os << "Runtime aborting --- recursively, so no thread-specific detail!\n";
555 DumpRecursiveAbort(os);
556 return;
557 }
558 gAborting++;
559 os << "Runtime aborting...\n";
560 if (Runtime::Current() == nullptr) {
561 os << "(Runtime does not yet exist!)\n";
562 DumpNativeStack(os, GetTid(), " native: ", nullptr);
563 return;
564 }
565 Thread* self = Thread::Current();
566
567 // Dump all threads first and then the aborting thread. While this is counter the logical flow,
568 // it improves the chance of relevant data surviving in the Android logs.
569
570 DumpAllThreads(os, self);
571
572 if (self == nullptr) {
573 os << "(Aborting thread was not attached to runtime!)\n";
574 DumpNativeStack(os, GetTid(), " native: ", nullptr);
575 } else {
576 os << "Aborting thread:\n";
577 if (Locks::mutator_lock_->IsExclusiveHeld(self) || Locks::mutator_lock_->IsSharedHeld(self)) {
578 DumpThread(os, self);
579 } else {
580 if (Locks::mutator_lock_->SharedTryLock(self)) {
581 DumpThread(os, self);
582 Locks::mutator_lock_->SharedUnlock(self);
583 }
584 }
585 }
586 }
587
588 // No thread-safety analysis as we do explicitly test for holding the mutator lock.
DumpThreadart::AbortState589 void DumpThread(std::ostream& os, Thread* self) const NO_THREAD_SAFETY_ANALYSIS {
590 DCHECK(Locks::mutator_lock_->IsExclusiveHeld(self) || Locks::mutator_lock_->IsSharedHeld(self));
591 self->Dump(os);
592 if (self->IsExceptionPending()) {
593 mirror::Throwable* exception = self->GetException();
594 os << "Pending exception " << exception->Dump();
595 }
596 }
597
DumpAllThreadsart::AbortState598 void DumpAllThreads(std::ostream& os, Thread* self) const {
599 Runtime* runtime = Runtime::Current();
600 if (runtime != nullptr) {
601 ThreadList* thread_list = runtime->GetThreadList();
602 if (thread_list != nullptr) {
603 // Dump requires ThreadListLock and ThreadSuspendCountLock to not be held (they will be
604 // grabbed).
605 // TODO(b/134167395): Change Dump to work with the locks held, and have a loop with timeout
606 // acquiring the locks.
607 bool tll_already_held = Locks::thread_list_lock_->IsExclusiveHeld(self);
608 bool tscl_already_held = Locks::thread_suspend_count_lock_->IsExclusiveHeld(self);
609 if (tll_already_held || tscl_already_held) {
610 os << "Skipping all-threads dump as locks are held:"
611 << (tll_already_held ? "" : " thread_list_lock")
612 << (tscl_already_held ? "" : " thread_suspend_count_lock")
613 << "\n";
614 return;
615 }
616 bool ml_already_exlusively_held = Locks::mutator_lock_->IsExclusiveHeld(self);
617 if (ml_already_exlusively_held) {
618 os << "Skipping all-threads dump as mutator lock is exclusively held.";
619 return;
620 }
621 bool ml_already_held = Locks::mutator_lock_->IsSharedHeld(self);
622 if (!ml_already_held) {
623 os << "Dumping all threads without mutator lock held\n";
624 }
625 os << "All threads:\n";
626 thread_list->Dump(os);
627 }
628 }
629 }
630
631 // For recursive aborts.
DumpRecursiveAbortart::AbortState632 void DumpRecursiveAbort(std::ostream& os) const NO_THREAD_SAFETY_ANALYSIS {
633 // The only thing we'll attempt is dumping the native stack of the current thread. We will only
634 // try this if we haven't exceeded an arbitrary amount of recursions, to recover and actually
635 // die.
636 // Note: as we're using a global counter for the recursive abort detection, there is a potential
637 // race here and it is not OK to just print when the counter is "2" (one from
638 // Runtime::Abort(), one from previous Dump() call). Use a number that seems large enough.
639 static constexpr size_t kOnlyPrintWhenRecursionLessThan = 100u;
640 if (gAborting < kOnlyPrintWhenRecursionLessThan) {
641 gAborting++;
642 DumpNativeStack(os, GetTid());
643 }
644 }
645 };
646
Abort(const char * msg)647 void Runtime::Abort(const char* msg) {
648 auto old_value = gAborting.fetch_add(1); // set before taking any locks
649
650 // Only set the first abort message.
651 if (old_value == 0) {
652 #ifdef ART_TARGET_ANDROID
653 android_set_abort_message(msg);
654 #else
655 // Set the runtime fault message in case our unexpected-signal code will run.
656 Runtime* current = Runtime::Current();
657 if (current != nullptr) {
658 current->SetFaultMessage(msg);
659 }
660 #endif
661 }
662
663 // May be coming from an unattached thread.
664 if (Thread::Current() == nullptr) {
665 Runtime* current = Runtime::Current();
666 if (current != nullptr && current->IsStarted() && !current->IsShuttingDownUnsafe()) {
667 // We do not flag this to the unexpected-signal handler so that that may dump the stack.
668 abort();
669 UNREACHABLE();
670 }
671 }
672
673 {
674 // Ensure that we don't have multiple threads trying to abort at once,
675 // which would result in significantly worse diagnostics.
676 ScopedThreadStateChange tsc(Thread::Current(), ThreadState::kNativeForAbort);
677 Locks::abort_lock_->ExclusiveLock(Thread::Current());
678 }
679
680 // Get any pending output out of the way.
681 fflush(nullptr);
682
683 // Many people have difficulty distinguish aborts from crashes,
684 // so be explicit.
685 // Note: use cerr on the host to print log lines immediately, so we get at least some output
686 // in case of recursive aborts. We lose annotation with the source file and line number
687 // here, which is a minor issue. The same is significantly more complicated on device,
688 // which is why we ignore the issue there.
689 AbortState state;
690 if (kIsTargetBuild) {
691 LOG(FATAL_WITHOUT_ABORT) << Dumpable<AbortState>(state);
692 } else {
693 std::cerr << Dumpable<AbortState>(state);
694 }
695
696 // Sometimes we dump long messages, and the Android abort message only retains the first line.
697 // In those cases, just log the message again, to avoid logcat limits.
698 if (msg != nullptr && strchr(msg, '\n') != nullptr) {
699 LOG(FATAL_WITHOUT_ABORT) << msg;
700 }
701
702 FlagRuntimeAbort();
703
704 // Call the abort hook if we have one.
705 if (Runtime::Current() != nullptr && Runtime::Current()->abort_ != nullptr) {
706 LOG(FATAL_WITHOUT_ABORT) << "Calling abort hook...";
707 Runtime::Current()->abort_();
708 // notreached
709 LOG(FATAL_WITHOUT_ABORT) << "Unexpectedly returned from abort hook!";
710 }
711
712 abort();
713 // notreached
714 }
715
716 /**
717 * Update entrypoints of methods before the first fork. This
718 * helps sharing pages where ArtMethods are allocated between the zygote and
719 * forked apps.
720 */
721 class UpdateMethodsPreFirstForkVisitor : public ClassVisitor {
722 public:
UpdateMethodsPreFirstForkVisitor(ClassLinker * class_linker)723 explicit UpdateMethodsPreFirstForkVisitor(ClassLinker* class_linker)
724 : class_linker_(class_linker),
725 can_use_nterp_(interpreter::CanRuntimeUseNterp()) {}
726
operator ()(ObjPtr<mirror::Class> klass)727 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) {
728 bool is_initialized = klass->IsVisiblyInitialized();
729 for (ArtMethod& method : klass->GetDeclaredMethods(kRuntimePointerSize)) {
730 if (!is_initialized && method.NeedsClinitCheckBeforeCall() && can_use_nterp_) {
731 const void* existing = method.GetEntryPointFromQuickCompiledCode();
732 if (class_linker_->IsQuickResolutionStub(existing) && CanMethodUseNterp(&method)) {
733 method.SetEntryPointFromQuickCompiledCode(interpreter::GetNterpWithClinitEntryPoint());
734 }
735 }
736 }
737 return true;
738 }
739
740 private:
741 ClassLinker* const class_linker_;
742 const bool can_use_nterp_;
743
744 DISALLOW_COPY_AND_ASSIGN(UpdateMethodsPreFirstForkVisitor);
745 };
746
PreZygoteFork()747 void Runtime::PreZygoteFork() {
748 if (GetJit() != nullptr) {
749 GetJit()->PreZygoteFork();
750 }
751 if (!heap_->HasZygoteSpace()) {
752 Thread* self = Thread::Current();
753 // This is the first fork. Update ArtMethods in the boot classpath now to
754 // avoid having forked apps dirty the memory.
755
756 // Ensure we call FixupStaticTrampolines on all methods that are
757 // initialized.
758 class_linker_->MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ true);
759
760 ScopedObjectAccess soa(self);
761 UpdateMethodsPreFirstForkVisitor visitor(class_linker_);
762 class_linker_->VisitClasses(&visitor);
763 }
764 heap_->PreZygoteFork();
765 PreZygoteForkNativeBridge();
766 }
767
PostZygoteFork()768 void Runtime::PostZygoteFork() {
769 jit::Jit* jit = GetJit();
770 if (jit != nullptr) {
771 jit->PostZygoteFork();
772 // Ensure that the threads in the JIT pool have been created with the right
773 // priority.
774 if (kIsDebugBuild && jit->GetThreadPool() != nullptr) {
775 jit->GetThreadPool()->CheckPthreadPriority(
776 IsZygote() ? jit->GetZygoteThreadPoolPthreadPriority()
777 : jit->GetThreadPoolPthreadPriority());
778 }
779 }
780 // Reset all stats.
781 ResetStats(0xFFFFFFFF);
782 }
783
CallExitHook(jint status)784 void Runtime::CallExitHook(jint status) {
785 if (exit_ != nullptr) {
786 ScopedThreadStateChange tsc(Thread::Current(), ThreadState::kNative);
787 exit_(status);
788 LOG(WARNING) << "Exit hook returned instead of exiting!";
789 }
790 }
791
SweepSystemWeaks(IsMarkedVisitor * visitor)792 void Runtime::SweepSystemWeaks(IsMarkedVisitor* visitor) {
793 GetInternTable()->SweepInternTableWeaks(visitor);
794 GetMonitorList()->SweepMonitorList(visitor);
795 GetJavaVM()->SweepJniWeakGlobals(visitor);
796 GetHeap()->SweepAllocationRecords(visitor);
797 // Sweep JIT tables only if the GC is moving as in other cases the entries are
798 // not updated.
799 if (GetJit() != nullptr && GetHeap()->IsMovingGc()) {
800 // Visit JIT literal tables. Objects in these tables are classes and strings
801 // and only classes can be affected by class unloading. The strings always
802 // stay alive as they are strongly interned.
803 // TODO: Move this closer to CleanupClassLoaders, to avoid blocking weak accesses
804 // from mutators. See b/32167580.
805 GetJit()->GetCodeCache()->SweepRootTables(visitor);
806 }
807
808 // All other generic system-weak holders.
809 for (gc::AbstractSystemWeakHolder* holder : system_weak_holders_) {
810 holder->Sweep(visitor);
811 }
812 }
813
ParseOptions(const RuntimeOptions & raw_options,bool ignore_unrecognized,RuntimeArgumentMap * runtime_options)814 bool Runtime::ParseOptions(const RuntimeOptions& raw_options,
815 bool ignore_unrecognized,
816 RuntimeArgumentMap* runtime_options) {
817 Locks::Init();
818 InitLogging(/* argv= */ nullptr, Abort); // Calls Locks::Init() as a side effect.
819 bool parsed = ParsedOptions::Parse(raw_options, ignore_unrecognized, runtime_options);
820 if (!parsed) {
821 LOG(ERROR) << "Failed to parse options";
822 return false;
823 }
824 return true;
825 }
826
827 // Callback to check whether it is safe to call Abort (e.g., to use a call to
828 // LOG(FATAL)). It is only safe to call Abort if the runtime has been created,
829 // properly initialized, and has not shut down.
IsSafeToCallAbort()830 static bool IsSafeToCallAbort() NO_THREAD_SAFETY_ANALYSIS {
831 Runtime* runtime = Runtime::Current();
832 return runtime != nullptr && runtime->IsStarted() && !runtime->IsShuttingDownLocked();
833 }
834
AddGeneratedCodeRange(const void * start,size_t size)835 void Runtime::AddGeneratedCodeRange(const void* start, size_t size) {
836 if (HandlesSignalsInCompiledCode()) {
837 fault_manager.AddGeneratedCodeRange(start, size);
838 }
839 }
840
RemoveGeneratedCodeRange(const void * start,size_t size)841 void Runtime::RemoveGeneratedCodeRange(const void* start, size_t size) {
842 if (HandlesSignalsInCompiledCode()) {
843 fault_manager.RemoveGeneratedCodeRange(start, size);
844 }
845 }
846
Create(RuntimeArgumentMap && runtime_options)847 bool Runtime::Create(RuntimeArgumentMap&& runtime_options) {
848 // TODO: acquire a static mutex on Runtime to avoid racing.
849 if (Runtime::instance_ != nullptr) {
850 return false;
851 }
852 instance_ = new Runtime;
853 Locks::SetClientCallback(IsSafeToCallAbort);
854 if (!instance_->Init(std::move(runtime_options))) {
855 // TODO: Currently deleting the instance will abort the runtime on destruction. Now This will
856 // leak memory, instead. Fix the destructor. b/19100793.
857 // delete instance_;
858 instance_ = nullptr;
859 return false;
860 }
861 return true;
862 }
863
Create(const RuntimeOptions & raw_options,bool ignore_unrecognized)864 bool Runtime::Create(const RuntimeOptions& raw_options, bool ignore_unrecognized) {
865 RuntimeArgumentMap runtime_options;
866 return ParseOptions(raw_options, ignore_unrecognized, &runtime_options) &&
867 Create(std::move(runtime_options));
868 }
869
CreateSystemClassLoader(Runtime * runtime)870 static jobject CreateSystemClassLoader(Runtime* runtime) {
871 if (runtime->IsAotCompiler() && !runtime->GetCompilerCallbacks()->IsBootImage()) {
872 return nullptr;
873 }
874
875 ScopedObjectAccess soa(Thread::Current());
876 ClassLinker* cl = runtime->GetClassLinker();
877 auto pointer_size = cl->GetImagePointerSize();
878
879 ObjPtr<mirror::Class> class_loader_class = GetClassRoot<mirror::ClassLoader>(cl);
880 DCHECK(class_loader_class->IsInitialized()); // Class roots have been initialized.
881
882 ArtMethod* getSystemClassLoader = class_loader_class->FindClassMethod(
883 "getSystemClassLoader", "()Ljava/lang/ClassLoader;", pointer_size);
884 CHECK(getSystemClassLoader != nullptr);
885 CHECK(getSystemClassLoader->IsStatic());
886
887 ObjPtr<mirror::Object> system_class_loader = getSystemClassLoader->InvokeStatic<'L'>(soa.Self());
888 CHECK(system_class_loader != nullptr);
889
890 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
891 jobject g_system_class_loader =
892 runtime->GetJavaVM()->AddGlobalRef(soa.Self(), system_class_loader);
893 soa.Self()->SetClassLoaderOverride(g_system_class_loader);
894
895 ObjPtr<mirror::Class> thread_class = WellKnownClasses::java_lang_Thread.Get();
896 ArtField* contextClassLoader =
897 thread_class->FindDeclaredInstanceField("contextClassLoader", "Ljava/lang/ClassLoader;");
898 CHECK(contextClassLoader != nullptr);
899
900 // We can't run in a transaction yet.
901 contextClassLoader->SetObject<false>(soa.Self()->GetPeer(), system_class_loader);
902
903 return g_system_class_loader;
904 }
905
GetCompilerExecutable() const906 std::string Runtime::GetCompilerExecutable() const {
907 if (!compiler_executable_.empty()) {
908 return compiler_executable_;
909 }
910 std::string compiler_executable = GetArtBinDir() + "/dex2oat";
911 if (kIsDebugBuild) {
912 compiler_executable += 'd';
913 }
914 if (kIsTargetBuild) {
915 compiler_executable += Is64BitInstructionSet(kRuntimeISA) ? "64" : "32";
916 }
917 return compiler_executable;
918 }
919
RunRootClinits(Thread * self)920 void Runtime::RunRootClinits(Thread* self) {
921 class_linker_->RunRootClinits(self);
922
923 GcRoot<mirror::Throwable>* exceptions[] = {
924 &pre_allocated_OutOfMemoryError_when_throwing_exception_,
925 // &pre_allocated_OutOfMemoryError_when_throwing_oome_, // Same class as above.
926 // &pre_allocated_OutOfMemoryError_when_handling_stack_overflow_, // Same class as above.
927 &pre_allocated_NoClassDefFoundError_,
928 };
929 for (GcRoot<mirror::Throwable>* exception : exceptions) {
930 StackHandleScope<1> hs(self);
931 Handle<mirror::Class> klass = hs.NewHandle<mirror::Class>(exception->Read()->GetClass());
932 class_linker_->EnsureInitialized(self, klass, true, true);
933 self->AssertNoPendingException();
934 }
935 }
936
Start()937 bool Runtime::Start() {
938 VLOG(startup) << "Runtime::Start entering";
939
940 CHECK(!no_sig_chain_) << "A started runtime should have sig chain enabled";
941
942 // If a debug host build, disable ptrace restriction for debugging and test timeout thread dump.
943 // Only 64-bit as prctl() may fail in 32 bit userspace on a 64-bit kernel.
944 #if defined(__linux__) && !defined(ART_TARGET_ANDROID) && defined(__x86_64__)
945 if (kIsDebugBuild) {
946 if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) != 0) {
947 PLOG(WARNING) << "Failed setting PR_SET_PTRACER to PR_SET_PTRACER_ANY";
948 }
949 }
950 #endif
951
952 // Restore main thread state to kNative as expected by native code.
953 Thread* self = Thread::Current();
954
955 started_ = true;
956
957 // Before running any clinit, set up the native methods provided by the runtime itself.
958 RegisterRuntimeNativeMethods(self->GetJniEnv());
959
960 class_linker_->RunEarlyRootClinits(self);
961 InitializeIntrinsics();
962
963 self->TransitionFromRunnableToSuspended(ThreadState::kNative);
964
965 // InitNativeMethods needs to be after started_ so that the classes
966 // it touches will have methods linked to the oat file if necessary.
967 {
968 ScopedTrace trace2("InitNativeMethods");
969 InitNativeMethods();
970 }
971
972 // InitializeCorePlatformApiPrivateFields() needs to be called after well known class
973 // initializtion in InitNativeMethods().
974 art::hiddenapi::InitializeCorePlatformApiPrivateFields();
975
976 // Initialize well known thread group values that may be accessed threads while attaching.
977 InitThreadGroups(self);
978
979 Thread::FinishStartup();
980
981 // Create the JIT either if we have to use JIT compilation or save profiling info. This is
982 // done after FinishStartup as the JIT pool needs Java thread peers, which require the main
983 // ThreadGroup to exist.
984 //
985 // TODO(calin): We use the JIT class as a proxy for JIT compilation and for
986 // recoding profiles. Maybe we should consider changing the name to be more clear it's
987 // not only about compiling. b/28295073.
988 if (jit_options_->UseJitCompilation() || jit_options_->GetSaveProfilingInfo()) {
989 // Try to load compiler pre zygote to reduce PSS. b/27744947
990 std::string error_msg;
991 if (!jit::Jit::LoadCompilerLibrary(&error_msg)) {
992 LOG(WARNING) << "Failed to load JIT compiler with error " << error_msg;
993 }
994 CreateJit();
995 #ifdef ADDRESS_SANITIZER
996 // (b/238730394): In older implementations of sanitizer + glibc there is a race between
997 // pthread_create and dlopen that could cause a deadlock. pthread_create interceptor in ASAN
998 // uses dl_pthread_iterator with a callback that could request a dl_load_lock via call to
999 // __tls_get_addr [1]. dl_pthread_iterate would already hold dl_load_lock so this could cause a
1000 // deadlock. __tls_get_addr needs a dl_load_lock only when there is a dlopen happening in
1001 // parallel. As a workaround we wait for the pthread_create (i.e JIT thread pool creation) to
1002 // finish before going to the next phase. Creating a system class loader could need a dlopen so
1003 // we wait here till threads are initialized.
1004 // [1] https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp#L408
1005 // See this for more context: https://reviews.llvm.org/D98926
1006 // TODO(b/238730394): Revisit this workaround once we migrate to musl libc.
1007 if (jit_ != nullptr) {
1008 jit_->GetThreadPool()->WaitForWorkersToBeCreated();
1009 }
1010 #endif
1011 }
1012
1013 // Send the start phase event. We have to wait till here as this is when the main thread peer
1014 // has just been generated, important root clinits have been run and JNI is completely functional.
1015 {
1016 ScopedObjectAccess soa(self);
1017 callbacks_->NextRuntimePhase(RuntimePhaseCallback::RuntimePhase::kStart);
1018 }
1019
1020 system_class_loader_ = CreateSystemClassLoader(this);
1021
1022 if (!is_zygote_) {
1023 if (is_native_bridge_loaded_) {
1024 PreInitializeNativeBridge(".");
1025 }
1026 NativeBridgeAction action = force_native_bridge_
1027 ? NativeBridgeAction::kInitialize
1028 : NativeBridgeAction::kUnload;
1029 InitNonZygoteOrPostFork(self->GetJniEnv(),
1030 /* is_system_server= */ false,
1031 /* is_child_zygote= */ false,
1032 action,
1033 GetInstructionSetString(kRuntimeISA));
1034 }
1035
1036 {
1037 ScopedObjectAccess soa(self);
1038 StartDaemonThreads();
1039 self->GetJniEnv()->AssertLocalsEmpty();
1040
1041 // Send the initialized phase event. Send it after starting the Daemon threads so that agents
1042 // cannot delay the daemon threads from starting forever.
1043 callbacks_->NextRuntimePhase(RuntimePhaseCallback::RuntimePhase::kInit);
1044 self->GetJniEnv()->AssertLocalsEmpty();
1045 }
1046
1047 VLOG(startup) << "Runtime::Start exiting";
1048 finished_starting_ = true;
1049
1050 if (trace_config_.get() != nullptr && trace_config_->trace_file != "") {
1051 ScopedThreadStateChange tsc(self, ThreadState::kWaitingForMethodTracingStart);
1052 int flags = 0;
1053 if (trace_config_->clock_source == TraceClockSource::kDual) {
1054 flags = Trace::TraceFlag::kTraceClockSourceWallClock |
1055 Trace::TraceFlag::kTraceClockSourceThreadCpu;
1056 } else if (trace_config_->clock_source == TraceClockSource::kWall) {
1057 flags = Trace::TraceFlag::kTraceClockSourceWallClock;
1058 } else if (TraceClockSource::kThreadCpu == trace_config_->clock_source) {
1059 flags = Trace::TraceFlag::kTraceClockSourceThreadCpu;
1060 } else {
1061 LOG(ERROR) << "Unexpected clock source";
1062 }
1063 Trace::Start(trace_config_->trace_file.c_str(),
1064 static_cast<int>(trace_config_->trace_file_size),
1065 flags,
1066 trace_config_->trace_output_mode,
1067 trace_config_->trace_mode,
1068 0);
1069 }
1070
1071 // In case we have a profile path passed as a command line argument,
1072 // register the current class path for profiling now. Note that we cannot do
1073 // this before we create the JIT and having it here is the most convenient way.
1074 // This is used when testing profiles with dalvikvm command as there is no
1075 // framework to register the dex files for profiling.
1076 if (jit_.get() != nullptr && jit_options_->GetSaveProfilingInfo() &&
1077 !jit_options_->GetProfileSaverOptions().GetProfilePath().empty()) {
1078 std::vector<std::string> dex_filenames;
1079 Split(class_path_string_, ':', &dex_filenames);
1080
1081 // We pass "" as the package name because at this point we don't know it. It could be the
1082 // Zygote or it could be a dalvikvm cmd line execution. The package name will be re-set during
1083 // post-fork or during RegisterAppInfo.
1084 //
1085 // Also, it's ok to pass "" to the ref profile filename. It indicates we don't have
1086 // a reference profile.
1087 RegisterAppInfo(
1088 /*package_name=*/ "",
1089 dex_filenames,
1090 jit_options_->GetProfileSaverOptions().GetProfilePath(),
1091 /*ref_profile_filename=*/ "",
1092 kVMRuntimePrimaryApk);
1093 }
1094
1095 return true;
1096 }
1097
EndThreadBirth()1098 void Runtime::EndThreadBirth() REQUIRES(Locks::runtime_shutdown_lock_) {
1099 DCHECK_GT(threads_being_born_, 0U);
1100 threads_being_born_--;
1101 if (shutting_down_started_ && threads_being_born_ == 0) {
1102 shutdown_cond_->Broadcast(Thread::Current());
1103 }
1104 }
1105
InitNonZygoteOrPostFork(JNIEnv * env,bool is_system_server,bool is_child_zygote,NativeBridgeAction action,const char * isa,bool profile_system_server)1106 void Runtime::InitNonZygoteOrPostFork(
1107 JNIEnv* env,
1108 bool is_system_server,
1109 // This is true when we are initializing a child-zygote. It requires
1110 // native bridge initialization to be able to run guest native code in
1111 // doPreload().
1112 bool is_child_zygote,
1113 NativeBridgeAction action,
1114 const char* isa,
1115 bool profile_system_server) {
1116 if (is_native_bridge_loaded_) {
1117 switch (action) {
1118 case NativeBridgeAction::kUnload:
1119 UnloadNativeBridge();
1120 is_native_bridge_loaded_ = false;
1121 break;
1122 case NativeBridgeAction::kInitialize:
1123 InitializeNativeBridge(env, isa);
1124 break;
1125 }
1126 }
1127
1128 if (is_child_zygote) {
1129 // If creating a child-zygote we only initialize native bridge. The rest of
1130 // runtime post-fork logic would spin up threads for Binder and JDWP.
1131 // Instead, the Java side of the child process will call a static main in a
1132 // class specified by the parent.
1133 return;
1134 }
1135
1136 DCHECK(!IsZygote());
1137
1138 if (is_system_server) {
1139 // Register the system server code paths.
1140 // TODO: Ideally this should be done by the VMRuntime#RegisterAppInfo. However, right now
1141 // the method is only called when we set up the profile. It should be called all the time
1142 // (simillar to the apps). Once that's done this manual registration can be removed.
1143 const char* system_server_classpath = getenv("SYSTEMSERVERCLASSPATH");
1144 if (system_server_classpath == nullptr || (strlen(system_server_classpath) == 0)) {
1145 LOG(WARNING) << "System server class path not set";
1146 } else {
1147 std::vector<std::string> jars = android::base::Split(system_server_classpath, ":");
1148 app_info_.RegisterAppInfo("android",
1149 jars,
1150 /*profile_output_filename=*/ "",
1151 /*ref_profile_filename=*/ "",
1152 AppInfo::CodeType::kPrimaryApk);
1153 }
1154
1155 // Set the system server package name to "android".
1156 // This is used to tell the difference between samples provided by system server
1157 // and samples generated by other apps when processing boot image profiles.
1158 SetProcessPackageName("android");
1159 if (profile_system_server) {
1160 jit_options_->SetWaitForJitNotificationsToSaveProfile(false);
1161 VLOG(profiler) << "Enabling system server profiles";
1162 }
1163 }
1164
1165 // Create the thread pools.
1166 // Avoid creating the runtime thread pool for system server since it will not be used and would
1167 // waste memory.
1168 if (!is_system_server) {
1169 ScopedTrace timing("CreateThreadPool");
1170 constexpr size_t kStackSize = 64 * KB;
1171 constexpr size_t kMaxRuntimeWorkers = 4u;
1172 const size_t num_workers =
1173 std::min(static_cast<size_t>(std::thread::hardware_concurrency()), kMaxRuntimeWorkers);
1174 MutexLock mu(Thread::Current(), *Locks::runtime_thread_pool_lock_);
1175 CHECK(thread_pool_ == nullptr);
1176 thread_pool_.reset(new ThreadPool("Runtime", num_workers, /*create_peers=*/false, kStackSize));
1177 thread_pool_->StartWorkers(Thread::Current());
1178 }
1179
1180 // Reset the gc performance data and metrics at zygote fork so that the events from
1181 // before fork aren't attributed to an app.
1182 heap_->ResetGcPerformanceInfo();
1183 GetMetrics()->Reset();
1184
1185 if (metrics_reporter_ != nullptr) {
1186 // Now that we know if we are an app or system server, reload the metrics reporter config
1187 // in case there are any difference.
1188 metrics::ReportingConfig metrics_config =
1189 metrics::ReportingConfig::FromFlags(is_system_server);
1190
1191 metrics_reporter_->ReloadConfig(metrics_config);
1192
1193 metrics::SessionData session_data{metrics::SessionData::CreateDefault()};
1194 // Start the session id from 1 to avoid clashes with the default value.
1195 // (better for debugability)
1196 session_data.session_id = GetRandomNumber<int64_t>(1, std::numeric_limits<int64_t>::max());
1197 // TODO: set session_data.compilation_reason and session_data.compiler_filter
1198 metrics_reporter_->MaybeStartBackgroundThread(session_data);
1199 // Also notify about any updates to the app info.
1200 metrics_reporter_->NotifyAppInfoUpdated(&app_info_);
1201 }
1202
1203 StartSignalCatcher();
1204
1205 ScopedObjectAccess soa(Thread::Current());
1206 if (IsPerfettoHprofEnabled() &&
1207 (Dbg::IsJdwpAllowed() || IsProfileable() || IsProfileableFromShell() || IsJavaDebuggable() ||
1208 Runtime::Current()->IsSystemServer())) {
1209 std::string err;
1210 ScopedTrace tr("perfetto_hprof init.");
1211 ScopedThreadSuspension sts(Thread::Current(), ThreadState::kNative);
1212 if (!EnsurePerfettoPlugin(&err)) {
1213 LOG(WARNING) << "Failed to load perfetto_hprof: " << err;
1214 }
1215 }
1216 if (IsPerfettoJavaHeapStackProfEnabled() &&
1217 (Dbg::IsJdwpAllowed() || IsProfileable() || IsProfileableFromShell() || IsJavaDebuggable() ||
1218 Runtime::Current()->IsSystemServer())) {
1219 // Marker used for dev tracing similar to above markers.
1220 ScopedTrace tr("perfetto_javaheapprof init.");
1221 }
1222 if (Runtime::Current()->IsSystemServer()) {
1223 std::string err;
1224 ScopedTrace tr("odrefresh and device stats logging");
1225 ScopedThreadSuspension sts(Thread::Current(), ThreadState::kNative);
1226 // Report stats if available. This should be moved into ART Services when they are ready.
1227 if (!odrefresh::UploadStatsIfAvailable(&err)) {
1228 LOG(WARNING) << "Failed to upload odrefresh metrics: " << err;
1229 }
1230 metrics::ReportDeviceMetrics();
1231 }
1232
1233 if (LIKELY(automatically_set_jni_ids_indirection_) && CanSetJniIdType()) {
1234 if (IsJavaDebuggable()) {
1235 SetJniIdType(JniIdType::kIndices);
1236 } else {
1237 SetJniIdType(JniIdType::kPointer);
1238 }
1239 }
1240 ATraceIntegerValue(
1241 "profilebootclasspath",
1242 static_cast<int>(jit_options_->GetProfileSaverOptions().GetProfileBootClassPath()));
1243 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
1244 // this will pause the runtime (in the internal debugger implementation), so we probably want
1245 // this to come last.
1246 GetRuntimeCallbacks()->StartDebugger();
1247 }
1248
StartSignalCatcher()1249 void Runtime::StartSignalCatcher() {
1250 if (!is_zygote_) {
1251 signal_catcher_ = new SignalCatcher();
1252 }
1253 }
1254
IsShuttingDown(Thread * self)1255 bool Runtime::IsShuttingDown(Thread* self) {
1256 MutexLock mu(self, *Locks::runtime_shutdown_lock_);
1257 return IsShuttingDownLocked();
1258 }
1259
StartDaemonThreads()1260 void Runtime::StartDaemonThreads() {
1261 ScopedTrace trace(__FUNCTION__);
1262 VLOG(startup) << "Runtime::StartDaemonThreads entering";
1263
1264 Thread* self = Thread::Current();
1265
1266 DCHECK_EQ(self->GetState(), ThreadState::kRunnable);
1267
1268 WellKnownClasses::java_lang_Daemons_start->InvokeStatic<'V'>(self);
1269 if (UNLIKELY(self->IsExceptionPending())) {
1270 LOG(FATAL) << "Error starting java.lang.Daemons: " << self->GetException()->Dump();
1271 }
1272
1273 VLOG(startup) << "Runtime::StartDaemonThreads exiting";
1274 }
1275
OpenBootDexFiles(ArrayRef<const std::string> dex_filenames,ArrayRef<const std::string> dex_locations,ArrayRef<const int> dex_fds,std::vector<std::unique_ptr<const DexFile>> * dex_files)1276 static size_t OpenBootDexFiles(ArrayRef<const std::string> dex_filenames,
1277 ArrayRef<const std::string> dex_locations,
1278 ArrayRef<const int> dex_fds,
1279 std::vector<std::unique_ptr<const DexFile>>* dex_files) {
1280 DCHECK(dex_files != nullptr) << "OpenDexFiles: out-param is nullptr";
1281 size_t failure_count = 0;
1282 for (size_t i = 0; i < dex_filenames.size(); i++) {
1283 const char* dex_filename = dex_filenames[i].c_str();
1284 const char* dex_location = dex_locations[i].c_str();
1285 const int dex_fd = i < dex_fds.size() ? dex_fds[i] : -1;
1286 static constexpr bool kVerifyChecksum = true;
1287 std::string error_msg;
1288 if (!OS::FileExists(dex_filename) && dex_fd < 0) {
1289 LOG(WARNING) << "Skipping non-existent dex file '" << dex_filename << "'";
1290 continue;
1291 }
1292 bool verify = Runtime::Current()->IsVerificationEnabled();
1293 ArtDexFileLoader dex_file_loader(dex_filename, dex_fd, dex_location);
1294 if (!dex_file_loader.Open(verify, kVerifyChecksum, &error_msg, dex_files)) {
1295 LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "' / fd " << dex_fd
1296 << ": " << error_msg;
1297 ++failure_count;
1298 }
1299 }
1300 return failure_count;
1301 }
1302
SetSentinel(ObjPtr<mirror::Object> sentinel)1303 void Runtime::SetSentinel(ObjPtr<mirror::Object> sentinel) {
1304 CHECK(sentinel_.Read() == nullptr);
1305 CHECK(sentinel != nullptr);
1306 CHECK(!heap_->IsMovableObject(sentinel));
1307 sentinel_ = GcRoot<mirror::Object>(sentinel);
1308 }
1309
GetSentinel()1310 GcRoot<mirror::Object> Runtime::GetSentinel() {
1311 return sentinel_;
1312 }
1313
CreatePreAllocatedException(Thread * self,Runtime * runtime,GcRoot<mirror::Throwable> * exception,const char * exception_class_descriptor,const char * msg)1314 static inline void CreatePreAllocatedException(Thread* self,
1315 Runtime* runtime,
1316 GcRoot<mirror::Throwable>* exception,
1317 const char* exception_class_descriptor,
1318 const char* msg)
1319 REQUIRES_SHARED(Locks::mutator_lock_) {
1320 DCHECK_EQ(self, Thread::Current());
1321 ClassLinker* class_linker = runtime->GetClassLinker();
1322 // Allocate an object without initializing the class to allow non-trivial Throwable.<clinit>().
1323 ObjPtr<mirror::Class> klass = class_linker->FindSystemClass(self, exception_class_descriptor);
1324 CHECK(klass != nullptr);
1325 gc::AllocatorType allocator_type = runtime->GetHeap()->GetCurrentAllocator();
1326 ObjPtr<mirror::Throwable> exception_object = ObjPtr<mirror::Throwable>::DownCast(
1327 klass->Alloc(self, allocator_type));
1328 CHECK(exception_object != nullptr);
1329 *exception = GcRoot<mirror::Throwable>(exception_object);
1330 // Initialize the "detailMessage" field.
1331 ObjPtr<mirror::String> message = mirror::String::AllocFromModifiedUtf8(self, msg);
1332 CHECK(message != nullptr);
1333 ObjPtr<mirror::Class> throwable = GetClassRoot<mirror::Throwable>(class_linker);
1334 ArtField* detailMessageField =
1335 throwable->FindDeclaredInstanceField("detailMessage", "Ljava/lang/String;");
1336 CHECK(detailMessageField != nullptr);
1337 detailMessageField->SetObject</* kTransactionActive= */ false>(exception->Read(), message);
1338 }
1339
GetApexVersions(ArrayRef<const std::string> boot_class_path_locations)1340 std::string Runtime::GetApexVersions(ArrayRef<const std::string> boot_class_path_locations) {
1341 std::vector<std::string_view> bcp_apexes;
1342 for (std::string_view jar : boot_class_path_locations) {
1343 std::string_view apex = ApexNameFromLocation(jar);
1344 if (!apex.empty()) {
1345 bcp_apexes.push_back(apex);
1346 }
1347 }
1348 static const char* kApexFileName = "/apex/apex-info-list.xml";
1349 // Start with empty markers.
1350 std::string empty_apex_versions(bcp_apexes.size(), '/');
1351 // When running on host or chroot, we just use empty markers.
1352 if (!kIsTargetBuild || !OS::FileExists(kApexFileName)) {
1353 return empty_apex_versions;
1354 }
1355 #ifdef ART_TARGET_ANDROID
1356 if (access(kApexFileName, R_OK) != 0) {
1357 PLOG(WARNING) << "Failed to read " << kApexFileName;
1358 return empty_apex_versions;
1359 }
1360 auto info_list = apex::readApexInfoList(kApexFileName);
1361 if (!info_list.has_value()) {
1362 LOG(WARNING) << "Failed to parse " << kApexFileName;
1363 return empty_apex_versions;
1364 }
1365
1366 std::string result;
1367 std::map<std::string_view, const apex::ApexInfo*> apex_infos;
1368 for (const apex::ApexInfo& info : info_list->getApexInfo()) {
1369 if (info.getIsActive()) {
1370 apex_infos.emplace(info.getModuleName(), &info);
1371 }
1372 }
1373 for (const std::string_view& str : bcp_apexes) {
1374 auto info = apex_infos.find(str);
1375 if (info == apex_infos.end() || info->second->getIsFactory()) {
1376 result += '/';
1377 } else {
1378 // In case lastUpdateMillis field is populated in apex-info-list.xml, we
1379 // prefer to use it as version scheme. If the field is missing we
1380 // fallback to the version code of the APEX.
1381 uint64_t version = info->second->hasLastUpdateMillis()
1382 ? info->second->getLastUpdateMillis()
1383 : info->second->getVersionCode();
1384 android::base::StringAppendF(&result, "/%" PRIu64, version);
1385 }
1386 }
1387 return result;
1388 #else
1389 return empty_apex_versions; // Not an Android build.
1390 #endif
1391 }
1392
InitializeApexVersions()1393 void Runtime::InitializeApexVersions() {
1394 apex_versions_ =
1395 GetApexVersions(ArrayRef<const std::string>(Runtime::Current()->GetBootClassPathLocations()));
1396 }
1397
ReloadAllFlags(const std::string & caller)1398 void Runtime::ReloadAllFlags(const std::string& caller) {
1399 FlagBase::ReloadAllFlags(caller);
1400 }
1401
Init(RuntimeArgumentMap && runtime_options_in)1402 bool Runtime::Init(RuntimeArgumentMap&& runtime_options_in) {
1403 // (b/30160149): protect subprocesses from modifications to LD_LIBRARY_PATH, etc.
1404 // Take a snapshot of the environment at the time the runtime was created, for use by Exec, etc.
1405 env_snapshot_.TakeSnapshot();
1406
1407 using Opt = RuntimeArgumentMap;
1408 Opt runtime_options(std::move(runtime_options_in));
1409 ScopedTrace trace(__FUNCTION__);
1410 CHECK_EQ(static_cast<size_t>(sysconf(_SC_PAGE_SIZE)), kPageSize);
1411
1412 // Reload all the flags value (from system properties and device configs).
1413 ReloadAllFlags(__FUNCTION__);
1414
1415 deny_art_apex_data_files_ = runtime_options.Exists(Opt::DenyArtApexDataFiles);
1416 if (deny_art_apex_data_files_) {
1417 // We will run slower without those files if the system has taken an ART APEX update.
1418 LOG(WARNING) << "ART APEX data files are untrusted.";
1419 }
1420
1421 // Early override for logging output.
1422 if (runtime_options.Exists(Opt::UseStderrLogger)) {
1423 android::base::SetLogger(android::base::StderrLogger);
1424 }
1425
1426 MemMap::Init();
1427
1428 verifier_missing_kthrow_fatal_ = runtime_options.GetOrDefault(Opt::VerifierMissingKThrowFatal);
1429 force_java_zygote_fork_loop_ = runtime_options.GetOrDefault(Opt::ForceJavaZygoteForkLoop);
1430 perfetto_hprof_enabled_ = runtime_options.GetOrDefault(Opt::PerfettoHprof);
1431 perfetto_javaheapprof_enabled_ = runtime_options.GetOrDefault(Opt::PerfettoJavaHeapStackProf);
1432
1433 // Try to reserve a dedicated fault page. This is allocated for clobbered registers and sentinels.
1434 // If we cannot reserve it, log a warning.
1435 // Note: We allocate this first to have a good chance of grabbing the page. The address (0xebad..)
1436 // is out-of-the-way enough that it should not collide with boot image mapping.
1437 // Note: Don't request an error message. That will lead to a maps dump in the case of failure,
1438 // leading to logspam.
1439 {
1440 constexpr uintptr_t kSentinelAddr =
1441 RoundDown(static_cast<uintptr_t>(Context::kBadGprBase), kPageSize);
1442 protected_fault_page_ = MemMap::MapAnonymous("Sentinel fault page",
1443 reinterpret_cast<uint8_t*>(kSentinelAddr),
1444 kPageSize,
1445 PROT_NONE,
1446 /*low_4gb=*/ true,
1447 /*reuse=*/ false,
1448 /*reservation=*/ nullptr,
1449 /*error_msg=*/ nullptr);
1450 if (!protected_fault_page_.IsValid()) {
1451 LOG(WARNING) << "Could not reserve sentinel fault page";
1452 } else if (reinterpret_cast<uintptr_t>(protected_fault_page_.Begin()) != kSentinelAddr) {
1453 LOG(WARNING) << "Could not reserve sentinel fault page at the right address.";
1454 protected_fault_page_.Reset();
1455 }
1456 }
1457
1458 VLOG(startup) << "Runtime::Init -verbose:startup enabled";
1459
1460 QuasiAtomic::Startup();
1461
1462 oat_file_manager_ = new OatFileManager();
1463
1464 jni_id_manager_.reset(new jni::JniIdManager());
1465
1466 Thread::SetSensitiveThreadHook(runtime_options.GetOrDefault(Opt::HookIsSensitiveThread));
1467 Monitor::Init(runtime_options.GetOrDefault(Opt::LockProfThreshold),
1468 runtime_options.GetOrDefault(Opt::StackDumpLockProfThreshold));
1469
1470 image_locations_ = runtime_options.ReleaseOrDefault(Opt::Image);
1471
1472 SetInstructionSet(runtime_options.GetOrDefault(Opt::ImageInstructionSet));
1473 boot_class_path_ = runtime_options.ReleaseOrDefault(Opt::BootClassPath);
1474 boot_class_path_locations_ = runtime_options.ReleaseOrDefault(Opt::BootClassPathLocations);
1475 DCHECK(boot_class_path_locations_.empty() ||
1476 boot_class_path_locations_.size() == boot_class_path_.size());
1477 if (boot_class_path_.empty()) {
1478 LOG(ERROR) << "Boot classpath is empty";
1479 return false;
1480 }
1481
1482 boot_class_path_fds_ = runtime_options.ReleaseOrDefault(Opt::BootClassPathFds);
1483 if (!boot_class_path_fds_.empty() && boot_class_path_fds_.size() != boot_class_path_.size()) {
1484 LOG(ERROR) << "Number of FDs specified in -Xbootclasspathfds must match the number of JARs in "
1485 << "-Xbootclasspath.";
1486 return false;
1487 }
1488
1489 boot_class_path_image_fds_ = runtime_options.ReleaseOrDefault(Opt::BootClassPathImageFds);
1490 boot_class_path_vdex_fds_ = runtime_options.ReleaseOrDefault(Opt::BootClassPathVdexFds);
1491 boot_class_path_oat_fds_ = runtime_options.ReleaseOrDefault(Opt::BootClassPathOatFds);
1492 CHECK(boot_class_path_image_fds_.empty() ||
1493 boot_class_path_image_fds_.size() == boot_class_path_.size());
1494 CHECK(boot_class_path_vdex_fds_.empty() ||
1495 boot_class_path_vdex_fds_.size() == boot_class_path_.size());
1496 CHECK(boot_class_path_oat_fds_.empty() ||
1497 boot_class_path_oat_fds_.size() == boot_class_path_.size());
1498
1499 class_path_string_ = runtime_options.ReleaseOrDefault(Opt::ClassPath);
1500 properties_ = runtime_options.ReleaseOrDefault(Opt::PropertiesList);
1501
1502 compiler_callbacks_ = runtime_options.GetOrDefault(Opt::CompilerCallbacksPtr);
1503 must_relocate_ = runtime_options.GetOrDefault(Opt::Relocate);
1504 is_zygote_ = runtime_options.Exists(Opt::Zygote);
1505 is_primary_zygote_ = runtime_options.Exists(Opt::PrimaryZygote);
1506 is_explicit_gc_disabled_ = runtime_options.Exists(Opt::DisableExplicitGC);
1507 image_dex2oat_enabled_ = runtime_options.GetOrDefault(Opt::ImageDex2Oat);
1508 dump_native_stack_on_sig_quit_ = runtime_options.GetOrDefault(Opt::DumpNativeStackOnSigQuit);
1509 allow_in_memory_compilation_ = runtime_options.Exists(Opt::AllowInMemoryCompilation);
1510
1511 if (is_zygote_ || runtime_options.Exists(Opt::OnlyUseTrustedOatFiles)) {
1512 oat_file_manager_->SetOnlyUseTrustedOatFiles();
1513 }
1514
1515 vfprintf_ = runtime_options.GetOrDefault(Opt::HookVfprintf);
1516 exit_ = runtime_options.GetOrDefault(Opt::HookExit);
1517 abort_ = runtime_options.GetOrDefault(Opt::HookAbort);
1518
1519 default_stack_size_ = runtime_options.GetOrDefault(Opt::StackSize);
1520
1521 compiler_executable_ = runtime_options.ReleaseOrDefault(Opt::Compiler);
1522 compiler_options_ = runtime_options.ReleaseOrDefault(Opt::CompilerOptions);
1523 for (const std::string& option : Runtime::Current()->GetCompilerOptions()) {
1524 if (option == "--debuggable") {
1525 SetRuntimeDebugState(RuntimeDebugState::kJavaDebuggableAtInit);
1526 break;
1527 }
1528 }
1529 image_compiler_options_ = runtime_options.ReleaseOrDefault(Opt::ImageCompilerOptions);
1530
1531 finalizer_timeout_ms_ = runtime_options.GetOrDefault(Opt::FinalizerTimeoutMs);
1532 max_spins_before_thin_lock_inflation_ =
1533 runtime_options.GetOrDefault(Opt::MaxSpinsBeforeThinLockInflation);
1534
1535 monitor_list_ = new MonitorList;
1536 monitor_pool_ = MonitorPool::Create();
1537 thread_list_ = new ThreadList(runtime_options.GetOrDefault(Opt::ThreadSuspendTimeout));
1538 intern_table_ = new InternTable;
1539
1540 monitor_timeout_enable_ = runtime_options.GetOrDefault(Opt::MonitorTimeoutEnable);
1541 int monitor_timeout_ms = runtime_options.GetOrDefault(Opt::MonitorTimeout);
1542 if (monitor_timeout_ms < Monitor::kMonitorTimeoutMinMs) {
1543 LOG(WARNING) << "Monitor timeout too short: Increasing";
1544 monitor_timeout_ms = Monitor::kMonitorTimeoutMinMs;
1545 }
1546 if (monitor_timeout_ms >= Monitor::kMonitorTimeoutMaxMs) {
1547 LOG(WARNING) << "Monitor timeout too long: Decreasing";
1548 monitor_timeout_ms = Monitor::kMonitorTimeoutMaxMs - 1;
1549 }
1550 monitor_timeout_ns_ = MsToNs(monitor_timeout_ms);
1551
1552 verify_ = runtime_options.GetOrDefault(Opt::Verify);
1553
1554 target_sdk_version_ = runtime_options.GetOrDefault(Opt::TargetSdkVersion);
1555
1556 // Set hidden API enforcement policy. The checks are disabled by default and
1557 // we only enable them if:
1558 // (a) runtime was started with a command line flag that enables the checks, or
1559 // (b) Zygote forked a new process that is not exempt (see ZygoteHooks).
1560 hidden_api_policy_ = runtime_options.GetOrDefault(Opt::HiddenApiPolicy);
1561 DCHECK_IMPLIES(is_zygote_, hidden_api_policy_ == hiddenapi::EnforcementPolicy::kDisabled);
1562
1563 // Set core platform API enforcement policy. The checks are disabled by default and
1564 // can be enabled with a command line flag. AndroidRuntime will pass the flag if
1565 // a system property is set.
1566 core_platform_api_policy_ = runtime_options.GetOrDefault(Opt::CorePlatformApiPolicy);
1567 if (core_platform_api_policy_ != hiddenapi::EnforcementPolicy::kDisabled) {
1568 LOG(INFO) << "Core platform API reporting enabled, enforcing="
1569 << (core_platform_api_policy_ == hiddenapi::EnforcementPolicy::kEnabled ? "true" : "false");
1570 }
1571
1572 // Dex2Oat's Runtime does not need the signal chain or the fault handler
1573 // and it passes the `NoSigChain` option to `Runtime` to indicate this.
1574 no_sig_chain_ = runtime_options.Exists(Opt::NoSigChain);
1575 force_native_bridge_ = runtime_options.Exists(Opt::ForceNativeBridge);
1576
1577 Split(runtime_options.GetOrDefault(Opt::CpuAbiList), ',', &cpu_abilist_);
1578
1579 fingerprint_ = runtime_options.ReleaseOrDefault(Opt::Fingerprint);
1580
1581 if (runtime_options.GetOrDefault(Opt::Interpret)) {
1582 GetInstrumentation()->ForceInterpretOnly();
1583 }
1584
1585 zygote_max_failed_boots_ = runtime_options.GetOrDefault(Opt::ZygoteMaxFailedBoots);
1586 experimental_flags_ = runtime_options.GetOrDefault(Opt::Experimental);
1587 is_low_memory_mode_ = runtime_options.Exists(Opt::LowMemoryMode);
1588 madvise_willneed_total_dex_size_ = runtime_options.GetOrDefault(Opt::MadviseWillNeedVdexFileSize);
1589 madvise_willneed_odex_filesize_ = runtime_options.GetOrDefault(Opt::MadviseWillNeedOdexFileSize);
1590 madvise_willneed_art_filesize_ = runtime_options.GetOrDefault(Opt::MadviseWillNeedArtFileSize);
1591
1592 jni_ids_indirection_ = runtime_options.GetOrDefault(Opt::OpaqueJniIds);
1593 automatically_set_jni_ids_indirection_ =
1594 runtime_options.GetOrDefault(Opt::AutoPromoteOpaqueJniIds);
1595
1596 plugins_ = runtime_options.ReleaseOrDefault(Opt::Plugins);
1597 agent_specs_ = runtime_options.ReleaseOrDefault(Opt::AgentPath);
1598 // TODO Add back in -agentlib
1599 // for (auto lib : runtime_options.ReleaseOrDefault(Opt::AgentLib)) {
1600 // agents_.push_back(lib);
1601 // }
1602
1603 float foreground_heap_growth_multiplier;
1604 if (is_low_memory_mode_ && !runtime_options.Exists(Opt::ForegroundHeapGrowthMultiplier)) {
1605 // If low memory mode, use 1.0 as the multiplier by default.
1606 foreground_heap_growth_multiplier = 1.0f;
1607 } else {
1608 // Extra added to the default heap growth multiplier for concurrent GC
1609 // compaction algorithms. This is done for historical reasons.
1610 // TODO: remove when we revisit heap configurations.
1611 foreground_heap_growth_multiplier =
1612 runtime_options.GetOrDefault(Opt::ForegroundHeapGrowthMultiplier) + 1.0f;
1613 }
1614 XGcOption xgc_option = runtime_options.GetOrDefault(Opt::GcOption);
1615
1616 // Generational CC collection is currently only compatible with Baker read barriers.
1617 bool use_generational_cc = kUseBakerReadBarrier && xgc_option.generational_cc;
1618
1619 // Cache the apex versions.
1620 InitializeApexVersions();
1621
1622 BackgroundGcOption background_gc =
1623 gUseReadBarrier ? BackgroundGcOption(gc::kCollectorTypeCCBackground)
1624 : (gUseUserfaultfd ? BackgroundGcOption(xgc_option.collector_type_)
1625 : runtime_options.GetOrDefault(Opt::BackgroundGc));
1626
1627 heap_ = new gc::Heap(runtime_options.GetOrDefault(Opt::MemoryInitialSize),
1628 runtime_options.GetOrDefault(Opt::HeapGrowthLimit),
1629 runtime_options.GetOrDefault(Opt::HeapMinFree),
1630 runtime_options.GetOrDefault(Opt::HeapMaxFree),
1631 runtime_options.GetOrDefault(Opt::HeapTargetUtilization),
1632 foreground_heap_growth_multiplier,
1633 runtime_options.GetOrDefault(Opt::StopForNativeAllocs),
1634 runtime_options.GetOrDefault(Opt::MemoryMaximumSize),
1635 runtime_options.GetOrDefault(Opt::NonMovingSpaceCapacity),
1636 GetBootClassPath(),
1637 GetBootClassPathLocations(),
1638 GetBootClassPathFds(),
1639 GetBootClassPathImageFds(),
1640 GetBootClassPathVdexFds(),
1641 GetBootClassPathOatFds(),
1642 image_locations_,
1643 instruction_set_,
1644 // Override the collector type to CC if the read barrier config.
1645 gUseReadBarrier ? gc::kCollectorTypeCC : xgc_option.collector_type_,
1646 background_gc,
1647 runtime_options.GetOrDefault(Opt::LargeObjectSpace),
1648 runtime_options.GetOrDefault(Opt::LargeObjectThreshold),
1649 runtime_options.GetOrDefault(Opt::ParallelGCThreads),
1650 runtime_options.GetOrDefault(Opt::ConcGCThreads),
1651 runtime_options.Exists(Opt::LowMemoryMode),
1652 runtime_options.GetOrDefault(Opt::LongPauseLogThreshold),
1653 runtime_options.GetOrDefault(Opt::LongGCLogThreshold),
1654 runtime_options.Exists(Opt::IgnoreMaxFootprint),
1655 runtime_options.GetOrDefault(Opt::AlwaysLogExplicitGcs),
1656 runtime_options.GetOrDefault(Opt::UseTLAB),
1657 xgc_option.verify_pre_gc_heap_,
1658 xgc_option.verify_pre_sweeping_heap_,
1659 xgc_option.verify_post_gc_heap_,
1660 xgc_option.verify_pre_gc_rosalloc_,
1661 xgc_option.verify_pre_sweeping_rosalloc_,
1662 xgc_option.verify_post_gc_rosalloc_,
1663 xgc_option.gcstress_,
1664 xgc_option.measure_,
1665 runtime_options.GetOrDefault(Opt::EnableHSpaceCompactForOOM),
1666 use_generational_cc,
1667 runtime_options.GetOrDefault(Opt::HSpaceCompactForOOMMinIntervalsMs),
1668 runtime_options.Exists(Opt::DumpRegionInfoBeforeGC),
1669 runtime_options.Exists(Opt::DumpRegionInfoAfterGC));
1670
1671 dump_gc_performance_on_shutdown_ = runtime_options.Exists(Opt::DumpGCPerformanceOnShutdown);
1672
1673 bool has_explicit_jdwp_options = runtime_options.Get(Opt::JdwpOptions) != nullptr;
1674 jdwp_options_ = runtime_options.GetOrDefault(Opt::JdwpOptions);
1675 jdwp_provider_ = CanonicalizeJdwpProvider(runtime_options.GetOrDefault(Opt::JdwpProvider),
1676 IsJavaDebuggable());
1677 switch (jdwp_provider_) {
1678 case JdwpProvider::kNone: {
1679 VLOG(jdwp) << "Disabling all JDWP support.";
1680 if (!jdwp_options_.empty()) {
1681 bool has_transport = jdwp_options_.find("transport") != std::string::npos;
1682 std::string adb_connection_args =
1683 std::string(" -XjdwpProvider:adbconnection -XjdwpOptions:") + jdwp_options_;
1684 if (has_explicit_jdwp_options) {
1685 LOG(WARNING) << "Jdwp options given when jdwp is disabled! You probably want to enable "
1686 << "jdwp with one of:" << std::endl
1687 << " -Xplugin:libopenjdkjvmti" << (kIsDebugBuild ? "d" : "") << ".so "
1688 << "-agentpath:libjdwp.so=" << jdwp_options_ << std::endl
1689 << (has_transport ? "" : adb_connection_args);
1690 }
1691 }
1692 break;
1693 }
1694 case JdwpProvider::kAdbConnection: {
1695 constexpr const char* plugin_name = kIsDebugBuild ? "libadbconnectiond.so"
1696 : "libadbconnection.so";
1697 plugins_.push_back(Plugin::Create(plugin_name));
1698 break;
1699 }
1700 case JdwpProvider::kUnset: {
1701 LOG(FATAL) << "Illegal jdwp provider " << jdwp_provider_ << " was not filtered out!";
1702 }
1703 }
1704 callbacks_->AddThreadLifecycleCallback(Dbg::GetThreadLifecycleCallback());
1705
1706 jit_options_.reset(jit::JitOptions::CreateFromRuntimeArguments(runtime_options));
1707 if (IsAotCompiler()) {
1708 // If we are already the compiler at this point, we must be dex2oat. Don't create the jit in
1709 // this case.
1710 // If runtime_options doesn't have UseJIT set to true then CreateFromRuntimeArguments returns
1711 // null and we don't create the jit.
1712 jit_options_->SetUseJitCompilation(false);
1713 jit_options_->SetSaveProfilingInfo(false);
1714 }
1715
1716 // Use MemMap arena pool for jit, malloc otherwise. Malloc arenas are faster to allocate but
1717 // can't be trimmed as easily.
1718 const bool use_malloc = IsAotCompiler();
1719 if (use_malloc) {
1720 arena_pool_.reset(new MallocArenaPool());
1721 jit_arena_pool_.reset(new MallocArenaPool());
1722 } else {
1723 arena_pool_.reset(new MemMapArenaPool(/* low_4gb= */ false));
1724 jit_arena_pool_.reset(new MemMapArenaPool(/* low_4gb= */ false, "CompilerMetadata"));
1725 }
1726
1727 // For 64 bit compilers, it needs to be in low 4GB in the case where we are cross compiling for a
1728 // 32 bit target. In this case, we have 32 bit pointers in the dex cache arrays which can't hold
1729 // when we have 64 bit ArtMethod pointers.
1730 const bool low_4gb = IsAotCompiler() && Is64BitInstructionSet(kRuntimeISA);
1731 if (gUseUserfaultfd) {
1732 linear_alloc_arena_pool_.reset(new GcVisitedArenaPool(low_4gb, IsZygote()));
1733 } else if (low_4gb) {
1734 linear_alloc_arena_pool_.reset(new MemMapArenaPool(low_4gb));
1735 }
1736 linear_alloc_.reset(CreateLinearAlloc());
1737 startup_linear_alloc_.store(CreateLinearAlloc(), std::memory_order_relaxed);
1738
1739 small_lrt_allocator_ = new jni::SmallLrtAllocator();
1740
1741 BlockSignals();
1742 InitPlatformSignalHandlers();
1743
1744 // Change the implicit checks flags based on runtime architecture.
1745 switch (kRuntimeISA) {
1746 case InstructionSet::kArm64:
1747 implicit_suspend_checks_ = true;
1748 FALLTHROUGH_INTENDED;
1749 case InstructionSet::kArm:
1750 case InstructionSet::kThumb2:
1751 case InstructionSet::kX86:
1752 case InstructionSet::kX86_64:
1753 implicit_null_checks_ = true;
1754 // Historical note: Installing stack protection was not playing well with Valgrind.
1755 implicit_so_checks_ = true;
1756 break;
1757 default:
1758 // Keep the defaults.
1759 break;
1760 }
1761
1762 fault_manager.Init(!no_sig_chain_);
1763 if (!no_sig_chain_) {
1764 if (HandlesSignalsInCompiledCode()) {
1765 // These need to be in a specific order. The null point check handler must be
1766 // after the suspend check and stack overflow check handlers.
1767 //
1768 // Note: the instances attach themselves to the fault manager and are handled by it. The
1769 // manager will delete the instance on Shutdown().
1770 if (implicit_suspend_checks_) {
1771 new SuspensionHandler(&fault_manager);
1772 }
1773
1774 if (implicit_so_checks_) {
1775 new StackOverflowHandler(&fault_manager);
1776 }
1777
1778 if (implicit_null_checks_) {
1779 new NullPointerHandler(&fault_manager);
1780 }
1781
1782 if (kEnableJavaStackTraceHandler) {
1783 new JavaStackTraceHandler(&fault_manager);
1784 }
1785
1786 if (interpreter::CanRuntimeUseNterp()) {
1787 // Nterp code can use signal handling just like the compiled managed code.
1788 OatQuickMethodHeader* nterp_header = OatQuickMethodHeader::NterpMethodHeader;
1789 fault_manager.AddGeneratedCodeRange(nterp_header->GetCode(), nterp_header->GetCodeSize());
1790 }
1791 }
1792 }
1793
1794 verifier_logging_threshold_ms_ = runtime_options.GetOrDefault(Opt::VerifierLoggingThreshold);
1795
1796 std::string error_msg;
1797 java_vm_ = JavaVMExt::Create(this, runtime_options, &error_msg);
1798 if (java_vm_.get() == nullptr) {
1799 LOG(ERROR) << "Could not initialize JavaVMExt: " << error_msg;
1800 return false;
1801 }
1802
1803 // Add the JniEnv handler.
1804 // TODO Refactor this stuff.
1805 java_vm_->AddEnvironmentHook(JNIEnvExt::GetEnvHandler);
1806
1807 Thread::Startup();
1808
1809 // ClassLinker needs an attached thread, but we can't fully attach a thread without creating
1810 // objects. We can't supply a thread group yet; it will be fixed later. Since we are the main
1811 // thread, we do not get a java peer.
1812 Thread* self = Thread::Attach("main", false, nullptr, false, /* should_run_callbacks= */ true);
1813 CHECK_EQ(self->GetThreadId(), ThreadList::kMainThreadId);
1814 CHECK(self != nullptr);
1815
1816 self->SetIsRuntimeThread(IsAotCompiler());
1817
1818 // Set us to runnable so tools using a runtime can allocate and GC by default
1819 self->TransitionFromSuspendedToRunnable();
1820
1821 // Now we're attached, we can take the heap locks and validate the heap.
1822 GetHeap()->EnableObjectValidation();
1823
1824 CHECK_GE(GetHeap()->GetContinuousSpaces().size(), 1U);
1825
1826 if (UNLIKELY(IsAotCompiler())) {
1827 class_linker_ = new AotClassLinker(intern_table_);
1828 } else {
1829 class_linker_ = new ClassLinker(
1830 intern_table_,
1831 runtime_options.GetOrDefault(Opt::FastClassNotFoundException));
1832 }
1833 if (GetHeap()->HasBootImageSpace()) {
1834 bool result = class_linker_->InitFromBootImage(&error_msg);
1835 if (!result) {
1836 LOG(ERROR) << "Could not initialize from image: " << error_msg;
1837 return false;
1838 }
1839 if (kIsDebugBuild) {
1840 for (auto image_space : GetHeap()->GetBootImageSpaces()) {
1841 image_space->VerifyImageAllocations();
1842 }
1843 }
1844 {
1845 ScopedTrace trace2("AddImageStringsToTable");
1846 for (gc::space::ImageSpace* image_space : heap_->GetBootImageSpaces()) {
1847 GetInternTable()->AddImageStringsToTable(image_space, VoidFunctor());
1848 }
1849 }
1850
1851 const size_t total_components = gc::space::ImageSpace::GetNumberOfComponents(
1852 ArrayRef<gc::space::ImageSpace* const>(heap_->GetBootImageSpaces()));
1853 if (total_components != GetBootClassPath().size()) {
1854 // The boot image did not contain all boot class path components. Load the rest.
1855 CHECK_LT(total_components, GetBootClassPath().size());
1856 size_t start = total_components;
1857 DCHECK_LT(start, GetBootClassPath().size());
1858 std::vector<std::unique_ptr<const DexFile>> extra_boot_class_path;
1859 if (runtime_options.Exists(Opt::BootClassPathDexList)) {
1860 extra_boot_class_path.swap(*runtime_options.GetOrDefault(Opt::BootClassPathDexList));
1861 } else {
1862 ArrayRef<const int> bcp_fds = start < GetBootClassPathFds().size()
1863 ? ArrayRef<const int>(GetBootClassPathFds()).SubArray(start)
1864 : ArrayRef<const int>();
1865 OpenBootDexFiles(ArrayRef<const std::string>(GetBootClassPath()).SubArray(start),
1866 ArrayRef<const std::string>(GetBootClassPathLocations()).SubArray(start),
1867 bcp_fds,
1868 &extra_boot_class_path);
1869 }
1870 class_linker_->AddExtraBootDexFiles(self, std::move(extra_boot_class_path));
1871 }
1872 if (IsJavaDebuggable() || jit_options_->GetProfileSaverOptions().GetProfileBootClassPath()) {
1873 // Deoptimize the boot image if debuggable as the code may have been compiled non-debuggable.
1874 // Also deoptimize if we are profiling the boot class path.
1875 ScopedThreadSuspension sts(self, ThreadState::kNative);
1876 ScopedSuspendAll ssa(__FUNCTION__);
1877 DeoptimizeBootImage();
1878 }
1879 } else {
1880 std::vector<std::unique_ptr<const DexFile>> boot_class_path;
1881 if (runtime_options.Exists(Opt::BootClassPathDexList)) {
1882 boot_class_path.swap(*runtime_options.GetOrDefault(Opt::BootClassPathDexList));
1883 } else {
1884 OpenBootDexFiles(ArrayRef<const std::string>(GetBootClassPath()),
1885 ArrayRef<const std::string>(GetBootClassPathLocations()),
1886 ArrayRef<const int>(GetBootClassPathFds()),
1887 &boot_class_path);
1888 }
1889 if (!class_linker_->InitWithoutImage(std::move(boot_class_path), &error_msg)) {
1890 LOG(ERROR) << "Could not initialize without image: " << error_msg;
1891 return false;
1892 }
1893
1894 // TODO: Should we move the following to InitWithoutImage?
1895 SetInstructionSet(instruction_set_);
1896 for (uint32_t i = 0; i < kCalleeSaveSize; i++) {
1897 CalleeSaveType type = CalleeSaveType(i);
1898 if (!HasCalleeSaveMethod(type)) {
1899 SetCalleeSaveMethod(CreateCalleeSaveMethod(), type);
1900 }
1901 }
1902 }
1903
1904 // Now that the boot image space is set, cache the boot classpath checksums,
1905 // to be used when validating oat files.
1906 ArrayRef<gc::space::ImageSpace* const> image_spaces(GetHeap()->GetBootImageSpaces());
1907 ArrayRef<const DexFile* const> bcp_dex_files(GetClassLinker()->GetBootClassPath());
1908 boot_class_path_checksums_ = gc::space::ImageSpace::GetBootClassPathChecksums(image_spaces,
1909 bcp_dex_files);
1910
1911 CHECK(class_linker_ != nullptr);
1912
1913 verifier::ClassVerifier::Init(class_linker_);
1914
1915 if (runtime_options.Exists(Opt::MethodTrace)) {
1916 trace_config_.reset(new TraceConfig());
1917 trace_config_->trace_file = runtime_options.ReleaseOrDefault(Opt::MethodTraceFile);
1918 trace_config_->trace_file_size = runtime_options.ReleaseOrDefault(Opt::MethodTraceFileSize);
1919 trace_config_->trace_mode = Trace::TraceMode::kMethodTracing;
1920 trace_config_->trace_output_mode = runtime_options.Exists(Opt::MethodTraceStreaming) ?
1921 Trace::TraceOutputMode::kStreaming :
1922 Trace::TraceOutputMode::kFile;
1923 trace_config_->clock_source = runtime_options.GetOrDefault(Opt::MethodTraceClock);
1924 }
1925
1926 // TODO: Remove this in a follow up CL. This isn't used anywhere.
1927 Trace::SetDefaultClockSource(runtime_options.GetOrDefault(Opt::ProfileClock));
1928
1929 if (GetHeap()->HasBootImageSpace()) {
1930 const ImageHeader& image_header = GetHeap()->GetBootImageSpaces()[0]->GetImageHeader();
1931 ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1932 ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1933 image_header.GetImageRoot(ImageHeader::kBootImageLiveObjects));
1934 pre_allocated_OutOfMemoryError_when_throwing_exception_ = GcRoot<mirror::Throwable>(
1935 boot_image_live_objects->Get(ImageHeader::kOomeWhenThrowingException)->AsThrowable());
1936 DCHECK(pre_allocated_OutOfMemoryError_when_throwing_exception_.Read()->GetClass()
1937 ->DescriptorEquals("Ljava/lang/OutOfMemoryError;"));
1938 pre_allocated_OutOfMemoryError_when_throwing_oome_ = GcRoot<mirror::Throwable>(
1939 boot_image_live_objects->Get(ImageHeader::kOomeWhenThrowingOome)->AsThrowable());
1940 DCHECK(pre_allocated_OutOfMemoryError_when_throwing_oome_.Read()->GetClass()
1941 ->DescriptorEquals("Ljava/lang/OutOfMemoryError;"));
1942 pre_allocated_OutOfMemoryError_when_handling_stack_overflow_ = GcRoot<mirror::Throwable>(
1943 boot_image_live_objects->Get(ImageHeader::kOomeWhenHandlingStackOverflow)->AsThrowable());
1944 DCHECK(pre_allocated_OutOfMemoryError_when_handling_stack_overflow_.Read()->GetClass()
1945 ->DescriptorEquals("Ljava/lang/OutOfMemoryError;"));
1946 pre_allocated_NoClassDefFoundError_ = GcRoot<mirror::Throwable>(
1947 boot_image_live_objects->Get(ImageHeader::kNoClassDefFoundError)->AsThrowable());
1948 DCHECK(pre_allocated_NoClassDefFoundError_.Read()->GetClass()
1949 ->DescriptorEquals("Ljava/lang/NoClassDefFoundError;"));
1950 } else {
1951 // Pre-allocate an OutOfMemoryError for the case when we fail to
1952 // allocate the exception to be thrown.
1953 CreatePreAllocatedException(self,
1954 this,
1955 &pre_allocated_OutOfMemoryError_when_throwing_exception_,
1956 "Ljava/lang/OutOfMemoryError;",
1957 "OutOfMemoryError thrown while trying to throw an exception; "
1958 "no stack trace available");
1959 // Pre-allocate an OutOfMemoryError for the double-OOME case.
1960 CreatePreAllocatedException(self,
1961 this,
1962 &pre_allocated_OutOfMemoryError_when_throwing_oome_,
1963 "Ljava/lang/OutOfMemoryError;",
1964 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; "
1965 "no stack trace available");
1966 // Pre-allocate an OutOfMemoryError for the case when we fail to
1967 // allocate while handling a stack overflow.
1968 CreatePreAllocatedException(self,
1969 this,
1970 &pre_allocated_OutOfMemoryError_when_handling_stack_overflow_,
1971 "Ljava/lang/OutOfMemoryError;",
1972 "OutOfMemoryError thrown while trying to handle a stack overflow; "
1973 "no stack trace available");
1974
1975 // Pre-allocate a NoClassDefFoundError for the common case of failing to find a system class
1976 // ahead of checking the application's class loader.
1977 CreatePreAllocatedException(self,
1978 this,
1979 &pre_allocated_NoClassDefFoundError_,
1980 "Ljava/lang/NoClassDefFoundError;",
1981 "Class not found using the boot class loader; "
1982 "no stack trace available");
1983 }
1984
1985 // Class-roots are setup, we can now finish initializing the JniIdManager.
1986 GetJniIdManager()->Init(self);
1987
1988 InitMetrics();
1989
1990 // Runtime initialization is largely done now.
1991 // We load plugins first since that can modify the runtime state slightly.
1992 // Load all plugins
1993 {
1994 // The init method of plugins expect the state of the thread to be non runnable.
1995 ScopedThreadSuspension sts(self, ThreadState::kNative);
1996 for (auto& plugin : plugins_) {
1997 std::string err;
1998 if (!plugin.Load(&err)) {
1999 LOG(FATAL) << plugin << " failed to load: " << err;
2000 }
2001 }
2002 }
2003
2004 // Look for a native bridge.
2005 //
2006 // The intended flow here is, in the case of a running system:
2007 //
2008 // Runtime::Init() (zygote):
2009 // LoadNativeBridge -> dlopen from cmd line parameter.
2010 // |
2011 // V
2012 // Runtime::Start() (zygote):
2013 // No-op wrt native bridge.
2014 // |
2015 // | start app
2016 // V
2017 // DidForkFromZygote(action)
2018 // action = kUnload -> dlclose native bridge.
2019 // action = kInitialize -> initialize library
2020 //
2021 //
2022 // The intended flow here is, in the case of a simple dalvikvm call:
2023 //
2024 // Runtime::Init():
2025 // LoadNativeBridge -> dlopen from cmd line parameter.
2026 // |
2027 // V
2028 // Runtime::Start():
2029 // DidForkFromZygote(kInitialize) -> try to initialize any native bridge given.
2030 // No-op wrt native bridge.
2031 {
2032 std::string native_bridge_file_name = runtime_options.ReleaseOrDefault(Opt::NativeBridge);
2033 is_native_bridge_loaded_ = LoadNativeBridge(native_bridge_file_name);
2034 }
2035
2036 // Startup agents
2037 // TODO Maybe we should start a new thread to run these on. Investigate RI behavior more.
2038 for (auto& agent_spec : agent_specs_) {
2039 // TODO Check err
2040 int res = 0;
2041 std::string err = "";
2042 ti::LoadError error;
2043 std::unique_ptr<ti::Agent> agent = agent_spec.Load(&res, &error, &err);
2044
2045 if (agent != nullptr) {
2046 agents_.push_back(std::move(agent));
2047 continue;
2048 }
2049
2050 switch (error) {
2051 case ti::LoadError::kInitializationError:
2052 LOG(FATAL) << "Unable to initialize agent!";
2053 UNREACHABLE();
2054
2055 case ti::LoadError::kLoadingError:
2056 LOG(ERROR) << "Unable to load an agent: " << err;
2057 continue;
2058
2059 case ti::LoadError::kNoError:
2060 break;
2061 }
2062 LOG(FATAL) << "Unreachable";
2063 UNREACHABLE();
2064 }
2065 {
2066 ScopedObjectAccess soa(self);
2067 callbacks_->NextRuntimePhase(RuntimePhaseCallback::RuntimePhase::kInitialAgents);
2068 }
2069
2070 if (IsZygote() && IsPerfettoHprofEnabled()) {
2071 constexpr const char* plugin_name = kIsDebugBuild ?
2072 "libperfetto_hprofd.so" : "libperfetto_hprof.so";
2073 // Load eagerly in Zygote to improve app startup times. This will make
2074 // subsequent dlopens for the library no-ops.
2075 dlopen(plugin_name, RTLD_NOW | RTLD_LOCAL);
2076 }
2077
2078 VLOG(startup) << "Runtime::Init exiting";
2079
2080 return true;
2081 }
2082
InitMetrics()2083 void Runtime::InitMetrics() {
2084 metrics::ReportingConfig metrics_config = metrics::ReportingConfig::FromFlags();
2085 metrics_reporter_ = metrics::MetricsReporter::Create(metrics_config, this);
2086 }
2087
RequestMetricsReport(bool synchronous)2088 void Runtime::RequestMetricsReport(bool synchronous) {
2089 if (metrics_reporter_) {
2090 metrics_reporter_->RequestMetricsReport(synchronous);
2091 }
2092 }
2093
EnsurePluginLoaded(const char * plugin_name,std::string * error_msg)2094 bool Runtime::EnsurePluginLoaded(const char* plugin_name, std::string* error_msg) {
2095 // Is the plugin already loaded?
2096 for (const Plugin& p : plugins_) {
2097 if (p.GetLibrary() == plugin_name) {
2098 return true;
2099 }
2100 }
2101 Plugin new_plugin = Plugin::Create(plugin_name);
2102
2103 if (!new_plugin.Load(error_msg)) {
2104 return false;
2105 }
2106 plugins_.push_back(std::move(new_plugin));
2107 return true;
2108 }
2109
EnsurePerfettoPlugin(std::string * error_msg)2110 bool Runtime::EnsurePerfettoPlugin(std::string* error_msg) {
2111 constexpr const char* plugin_name = kIsDebugBuild ?
2112 "libperfetto_hprofd.so" : "libperfetto_hprof.so";
2113 return EnsurePluginLoaded(plugin_name, error_msg);
2114 }
2115
EnsureJvmtiPlugin(Runtime * runtime,std::string * error_msg)2116 static bool EnsureJvmtiPlugin(Runtime* runtime,
2117 std::string* error_msg) {
2118 // TODO Rename Dbg::IsJdwpAllowed is IsDebuggingAllowed.
2119 DCHECK(Dbg::IsJdwpAllowed() || !runtime->IsJavaDebuggable())
2120 << "Being debuggable requires that jdwp (i.e. debugging) is allowed.";
2121 // Is the process debuggable? Otherwise, do not attempt to load the plugin unless we are
2122 // specifically allowed.
2123 if (!Dbg::IsJdwpAllowed()) {
2124 *error_msg = "Process is not allowed to load openjdkjvmti plugin. Process must be debuggable";
2125 return false;
2126 }
2127
2128 constexpr const char* plugin_name = kIsDebugBuild ? "libopenjdkjvmtid.so" : "libopenjdkjvmti.so";
2129 return runtime->EnsurePluginLoaded(plugin_name, error_msg);
2130 }
2131
2132 // Attach a new agent and add it to the list of runtime agents
2133 //
2134 // TODO: once we decide on the threading model for agents,
2135 // revisit this and make sure we're doing this on the right thread
2136 // (and we synchronize access to any shared data structures like "agents_")
2137 //
AttachAgent(JNIEnv * env,const std::string & agent_arg,jobject class_loader)2138 void Runtime::AttachAgent(JNIEnv* env, const std::string& agent_arg, jobject class_loader) {
2139 std::string error_msg;
2140 if (!EnsureJvmtiPlugin(this, &error_msg)) {
2141 LOG(WARNING) << "Could not load plugin: " << error_msg;
2142 ScopedObjectAccess soa(Thread::Current());
2143 ThrowIOException("%s", error_msg.c_str());
2144 return;
2145 }
2146
2147 ti::AgentSpec agent_spec(agent_arg);
2148
2149 int res = 0;
2150 ti::LoadError error;
2151 std::unique_ptr<ti::Agent> agent = agent_spec.Attach(env, class_loader, &res, &error, &error_msg);
2152
2153 if (agent != nullptr) {
2154 agents_.push_back(std::move(agent));
2155 } else {
2156 LOG(WARNING) << "Agent attach failed (result=" << error << ") : " << error_msg;
2157 ScopedObjectAccess soa(Thread::Current());
2158 ThrowIOException("%s", error_msg.c_str());
2159 }
2160 }
2161
InitNativeMethods()2162 void Runtime::InitNativeMethods() {
2163 VLOG(startup) << "Runtime::InitNativeMethods entering";
2164 Thread* self = Thread::Current();
2165 JNIEnv* env = self->GetJniEnv();
2166
2167 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
2168 CHECK_EQ(self->GetState(), ThreadState::kNative);
2169
2170 // Then set up libjavacore / libopenjdk / libicu_jni ,which are just
2171 // a regular JNI libraries with a regular JNI_OnLoad. Most JNI libraries can
2172 // just use System.loadLibrary, but libcore can't because it's the library
2173 // that implements System.loadLibrary!
2174 //
2175 // By setting calling class to java.lang.Object, the caller location for these
2176 // JNI libs is core-oj.jar in the ART APEX, and hence they are loaded from the
2177 // com_android_art linker namespace.
2178 jclass java_lang_Object;
2179 {
2180 // Use global JNI reference to keep the local references empty. If we allocated a
2181 // local reference here, the `PushLocalFrame(128)` that these internal libraries do
2182 // in their `JNI_OnLoad()` would reserve a lot of unnecessary space due to rounding.
2183 ScopedObjectAccess soa(self);
2184 java_lang_Object = reinterpret_cast<jclass>(
2185 GetJavaVM()->AddGlobalRef(self, GetClassRoot<mirror::Object>(GetClassLinker())));
2186 }
2187
2188 // libicu_jni has to be initialized before libopenjdk{d} due to runtime dependency from
2189 // libopenjdk{d} to Icu4cMetadata native methods in libicu_jni. See http://b/143888405
2190 {
2191 std::string error_msg;
2192 if (!java_vm_->LoadNativeLibrary(
2193 env, "libicu_jni.so", nullptr, java_lang_Object, &error_msg)) {
2194 LOG(FATAL) << "LoadNativeLibrary failed for \"libicu_jni.so\": " << error_msg;
2195 }
2196 }
2197 {
2198 std::string error_msg;
2199 if (!java_vm_->LoadNativeLibrary(
2200 env, "libjavacore.so", nullptr, java_lang_Object, &error_msg)) {
2201 LOG(FATAL) << "LoadNativeLibrary failed for \"libjavacore.so\": " << error_msg;
2202 }
2203 }
2204 {
2205 constexpr const char* kOpenJdkLibrary = kIsDebugBuild
2206 ? "libopenjdkd.so"
2207 : "libopenjdk.so";
2208 std::string error_msg;
2209 if (!java_vm_->LoadNativeLibrary(
2210 env, kOpenJdkLibrary, nullptr, java_lang_Object, &error_msg)) {
2211 LOG(FATAL) << "LoadNativeLibrary failed for \"" << kOpenJdkLibrary << "\": " << error_msg;
2212 }
2213 }
2214 env->DeleteGlobalRef(java_lang_Object);
2215
2216 // Initialize well known classes that may invoke runtime native methods.
2217 WellKnownClasses::LateInit(env);
2218
2219 VLOG(startup) << "Runtime::InitNativeMethods exiting";
2220 }
2221
ReclaimArenaPoolMemory()2222 void Runtime::ReclaimArenaPoolMemory() {
2223 arena_pool_->LockReclaimMemory();
2224 }
2225
InitThreadGroups(Thread * self)2226 void Runtime::InitThreadGroups(Thread* self) {
2227 ScopedObjectAccess soa(self);
2228 ArtField* main_thread_group_field = WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup;
2229 ArtField* system_thread_group_field = WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup;
2230 // Note: This is running before `ClassLinker::RunRootClinits()`, so we cannot rely on
2231 // `ThreadGroup` and `Thread` being initialized.
2232 // TODO: Clean up initialization order after all well-known methods are converted to `ArtMethod*`
2233 // (and therefore the `WellKnownClasses::Init()` shall not initialize any classes).
2234 StackHandleScope<2u> hs(self);
2235 Handle<mirror::Class> thread_group_class =
2236 hs.NewHandle(main_thread_group_field->GetDeclaringClass());
2237 bool initialized = GetClassLinker()->EnsureInitialized(
2238 self, thread_group_class, /*can_init_fields=*/ true, /*can_init_parents=*/ true);
2239 CHECK(initialized);
2240 Handle<mirror::Class> thread_class = hs.NewHandle(WellKnownClasses::java_lang_Thread.Get());
2241 initialized = GetClassLinker()->EnsureInitialized(
2242 self, thread_class, /*can_init_fields=*/ true, /*can_init_parents=*/ true);
2243 CHECK(initialized);
2244 main_thread_group_ =
2245 soa.Vm()->AddGlobalRef(self, main_thread_group_field->GetObject(thread_group_class.Get()));
2246 CHECK_IMPLIES(main_thread_group_ == nullptr, IsAotCompiler());
2247 system_thread_group_ =
2248 soa.Vm()->AddGlobalRef(self, system_thread_group_field->GetObject(thread_group_class.Get()));
2249 CHECK_IMPLIES(system_thread_group_ == nullptr, IsAotCompiler());
2250 }
2251
GetMainThreadGroup() const2252 jobject Runtime::GetMainThreadGroup() const {
2253 CHECK_IMPLIES(main_thread_group_ == nullptr, IsAotCompiler());
2254 return main_thread_group_;
2255 }
2256
GetSystemThreadGroup() const2257 jobject Runtime::GetSystemThreadGroup() const {
2258 CHECK_IMPLIES(system_thread_group_ == nullptr, IsAotCompiler());
2259 return system_thread_group_;
2260 }
2261
GetSystemClassLoader() const2262 jobject Runtime::GetSystemClassLoader() const {
2263 CHECK_IMPLIES(system_class_loader_ == nullptr, IsAotCompiler());
2264 return system_class_loader_;
2265 }
2266
RegisterRuntimeNativeMethods(JNIEnv * env)2267 void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
2268 register_dalvik_system_DexFile(env);
2269 register_dalvik_system_BaseDexClassLoader(env);
2270 register_dalvik_system_VMDebug(env);
2271 register_dalvik_system_VMRuntime(env);
2272 register_dalvik_system_VMStack(env);
2273 register_dalvik_system_ZygoteHooks(env);
2274 register_java_lang_Class(env);
2275 register_java_lang_Object(env);
2276 register_java_lang_invoke_MethodHandle(env);
2277 register_java_lang_invoke_MethodHandleImpl(env);
2278 register_java_lang_ref_FinalizerReference(env);
2279 register_java_lang_reflect_Array(env);
2280 register_java_lang_reflect_Constructor(env);
2281 register_java_lang_reflect_Executable(env);
2282 register_java_lang_reflect_Field(env);
2283 register_java_lang_reflect_Method(env);
2284 register_java_lang_reflect_Parameter(env);
2285 register_java_lang_reflect_Proxy(env);
2286 register_java_lang_ref_Reference(env);
2287 register_java_lang_StackStreamFactory(env);
2288 register_java_lang_String(env);
2289 register_java_lang_StringFactory(env);
2290 register_java_lang_System(env);
2291 register_java_lang_Thread(env);
2292 register_java_lang_Throwable(env);
2293 register_java_lang_VMClassLoader(env);
2294 register_java_util_concurrent_atomic_AtomicLong(env);
2295 register_jdk_internal_misc_Unsafe(env);
2296 register_libcore_io_Memory(env);
2297 register_libcore_util_CharsetUtils(env);
2298 register_org_apache_harmony_dalvik_ddmc_DdmServer(env);
2299 register_org_apache_harmony_dalvik_ddmc_DdmVmInternal(env);
2300 register_sun_misc_Unsafe(env);
2301 }
2302
operator <<(std::ostream & os,const DeoptimizationKind & kind)2303 std::ostream& operator<<(std::ostream& os, const DeoptimizationKind& kind) {
2304 os << GetDeoptimizationKindName(kind);
2305 return os;
2306 }
2307
DumpDeoptimizations(std::ostream & os)2308 void Runtime::DumpDeoptimizations(std::ostream& os) {
2309 for (size_t i = 0; i <= static_cast<size_t>(DeoptimizationKind::kLast); ++i) {
2310 if (deoptimization_counts_[i] != 0) {
2311 os << "Number of "
2312 << GetDeoptimizationKindName(static_cast<DeoptimizationKind>(i))
2313 << " deoptimizations: "
2314 << deoptimization_counts_[i]
2315 << "\n";
2316 }
2317 }
2318 }
2319
DumpForSigQuit(std::ostream & os)2320 void Runtime::DumpForSigQuit(std::ostream& os) {
2321 // Print backtraces first since they are important do diagnose ANRs,
2322 // and ANRs can often be trimmed to limit upload size.
2323 thread_list_->DumpForSigQuit(os);
2324 GetClassLinker()->DumpForSigQuit(os);
2325 GetInternTable()->DumpForSigQuit(os);
2326 GetJavaVM()->DumpForSigQuit(os);
2327 GetHeap()->DumpForSigQuit(os);
2328 oat_file_manager_->DumpForSigQuit(os);
2329 if (GetJit() != nullptr) {
2330 GetJit()->DumpForSigQuit(os);
2331 } else {
2332 os << "Running non JIT\n";
2333 }
2334 DumpDeoptimizations(os);
2335 TrackedAllocators::Dump(os);
2336 GetMetrics()->DumpForSigQuit(os);
2337 os << "\n";
2338
2339 BaseMutex::DumpAll(os);
2340
2341 // Inform anyone else who is interested in SigQuit.
2342 {
2343 ScopedObjectAccess soa(Thread::Current());
2344 callbacks_->SigQuit();
2345 }
2346 }
2347
DumpLockHolders(std::ostream & os)2348 void Runtime::DumpLockHolders(std::ostream& os) {
2349 pid_t mutator_lock_owner = Locks::mutator_lock_->GetExclusiveOwnerTid();
2350 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
2351 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
2352 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
2353 if ((mutator_lock_owner | thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
2354 os << "Mutator lock exclusive owner tid: " << mutator_lock_owner << "\n"
2355 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
2356 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
2357 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
2358 }
2359 }
2360
SetStatsEnabled(bool new_state)2361 void Runtime::SetStatsEnabled(bool new_state) {
2362 Thread* self = Thread::Current();
2363 MutexLock mu(self, *Locks::instrument_entrypoints_lock_);
2364 if (new_state == true) {
2365 GetStats()->Clear(~0);
2366 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
2367 self->GetStats()->Clear(~0);
2368 if (stats_enabled_ != new_state) {
2369 GetInstrumentation()->InstrumentQuickAllocEntryPointsLocked();
2370 }
2371 } else if (stats_enabled_ != new_state) {
2372 GetInstrumentation()->UninstrumentQuickAllocEntryPointsLocked();
2373 }
2374 stats_enabled_ = new_state;
2375 }
2376
ResetStats(int kinds)2377 void Runtime::ResetStats(int kinds) {
2378 GetStats()->Clear(kinds & 0xffff);
2379 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
2380 Thread::Current()->GetStats()->Clear(kinds >> 16);
2381 }
2382
GetStat(int kind)2383 uint64_t Runtime::GetStat(int kind) {
2384 RuntimeStats* stats;
2385 if (kind < (1<<16)) {
2386 stats = GetStats();
2387 } else {
2388 stats = Thread::Current()->GetStats();
2389 kind >>= 16;
2390 }
2391 switch (kind) {
2392 case KIND_ALLOCATED_OBJECTS:
2393 return stats->allocated_objects;
2394 case KIND_ALLOCATED_BYTES:
2395 return stats->allocated_bytes;
2396 case KIND_FREED_OBJECTS:
2397 return stats->freed_objects;
2398 case KIND_FREED_BYTES:
2399 return stats->freed_bytes;
2400 case KIND_GC_INVOCATIONS:
2401 return stats->gc_for_alloc_count;
2402 case KIND_CLASS_INIT_COUNT:
2403 return stats->class_init_count;
2404 case KIND_CLASS_INIT_TIME:
2405 return stats->class_init_time_ns;
2406 case KIND_EXT_ALLOCATED_OBJECTS:
2407 case KIND_EXT_ALLOCATED_BYTES:
2408 case KIND_EXT_FREED_OBJECTS:
2409 case KIND_EXT_FREED_BYTES:
2410 return 0; // backward compatibility
2411 default:
2412 LOG(FATAL) << "Unknown statistic " << kind;
2413 UNREACHABLE();
2414 }
2415 }
2416
BlockSignals()2417 void Runtime::BlockSignals() {
2418 SignalSet signals;
2419 signals.Add(SIGPIPE);
2420 // SIGQUIT is used to dump the runtime's state (including stack traces).
2421 signals.Add(SIGQUIT);
2422 // SIGUSR1 is used to initiate a GC.
2423 signals.Add(SIGUSR1);
2424 signals.Block();
2425 }
2426
AttachCurrentThread(const char * thread_name,bool as_daemon,jobject thread_group,bool create_peer,bool should_run_callbacks)2427 bool Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group,
2428 bool create_peer, bool should_run_callbacks) {
2429 ScopedTrace trace(__FUNCTION__);
2430 Thread* self = Thread::Attach(thread_name,
2431 as_daemon,
2432 thread_group,
2433 create_peer,
2434 should_run_callbacks);
2435 // Run ThreadGroup.add to notify the group that this thread is now started.
2436 if (self != nullptr && create_peer && !IsAotCompiler()) {
2437 ScopedObjectAccess soa(self);
2438 self->NotifyThreadGroup(soa, thread_group);
2439 }
2440 return self != nullptr;
2441 }
2442
DetachCurrentThread(bool should_run_callbacks)2443 void Runtime::DetachCurrentThread(bool should_run_callbacks) {
2444 ScopedTrace trace(__FUNCTION__);
2445 Thread* self = Thread::Current();
2446 if (self == nullptr) {
2447 LOG(FATAL) << "attempting to detach thread that is not attached";
2448 }
2449 if (self->HasManagedStack()) {
2450 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
2451 }
2452 thread_list_->Unregister(self, should_run_callbacks);
2453 }
2454
GetPreAllocatedOutOfMemoryErrorWhenThrowingException()2455 mirror::Throwable* Runtime::GetPreAllocatedOutOfMemoryErrorWhenThrowingException() {
2456 mirror::Throwable* oome = pre_allocated_OutOfMemoryError_when_throwing_exception_.Read();
2457 if (oome == nullptr) {
2458 LOG(ERROR) << "Failed to return pre-allocated OOME-when-throwing-exception";
2459 }
2460 return oome;
2461 }
2462
GetPreAllocatedOutOfMemoryErrorWhenThrowingOOME()2463 mirror::Throwable* Runtime::GetPreAllocatedOutOfMemoryErrorWhenThrowingOOME() {
2464 mirror::Throwable* oome = pre_allocated_OutOfMemoryError_when_throwing_oome_.Read();
2465 if (oome == nullptr) {
2466 LOG(ERROR) << "Failed to return pre-allocated OOME-when-throwing-OOME";
2467 }
2468 return oome;
2469 }
2470
GetPreAllocatedOutOfMemoryErrorWhenHandlingStackOverflow()2471 mirror::Throwable* Runtime::GetPreAllocatedOutOfMemoryErrorWhenHandlingStackOverflow() {
2472 mirror::Throwable* oome = pre_allocated_OutOfMemoryError_when_handling_stack_overflow_.Read();
2473 if (oome == nullptr) {
2474 LOG(ERROR) << "Failed to return pre-allocated OOME-when-handling-stack-overflow";
2475 }
2476 return oome;
2477 }
2478
GetPreAllocatedNoClassDefFoundError()2479 mirror::Throwable* Runtime::GetPreAllocatedNoClassDefFoundError() {
2480 mirror::Throwable* ncdfe = pre_allocated_NoClassDefFoundError_.Read();
2481 if (ncdfe == nullptr) {
2482 LOG(ERROR) << "Failed to return pre-allocated NoClassDefFoundError";
2483 }
2484 return ncdfe;
2485 }
2486
VisitConstantRoots(RootVisitor * visitor)2487 void Runtime::VisitConstantRoots(RootVisitor* visitor) {
2488 // Visiting the roots of these ArtMethods is not currently required since all the GcRoots are
2489 // null.
2490 BufferedRootVisitor<16> buffered_visitor(visitor, RootInfo(kRootVMInternal));
2491 const PointerSize pointer_size = GetClassLinker()->GetImagePointerSize();
2492 if (HasResolutionMethod()) {
2493 resolution_method_->VisitRoots(buffered_visitor, pointer_size);
2494 }
2495 if (HasImtConflictMethod()) {
2496 imt_conflict_method_->VisitRoots(buffered_visitor, pointer_size);
2497 }
2498 if (imt_unimplemented_method_ != nullptr) {
2499 imt_unimplemented_method_->VisitRoots(buffered_visitor, pointer_size);
2500 }
2501 for (uint32_t i = 0; i < kCalleeSaveSize; ++i) {
2502 auto* m = reinterpret_cast<ArtMethod*>(callee_save_methods_[i]);
2503 if (m != nullptr) {
2504 m->VisitRoots(buffered_visitor, pointer_size);
2505 }
2506 }
2507 }
2508
VisitConcurrentRoots(RootVisitor * visitor,VisitRootFlags flags)2509 void Runtime::VisitConcurrentRoots(RootVisitor* visitor, VisitRootFlags flags) {
2510 intern_table_->VisitRoots(visitor, flags);
2511 class_linker_->VisitRoots(visitor, flags);
2512 jni_id_manager_->VisitRoots(visitor);
2513 heap_->VisitAllocationRecords(visitor);
2514 if (jit_ != nullptr) {
2515 jit_->GetCodeCache()->VisitRoots(visitor);
2516 }
2517 if ((flags & kVisitRootFlagNewRoots) == 0) {
2518 // Guaranteed to have no new roots in the constant roots.
2519 VisitConstantRoots(visitor);
2520 }
2521 }
2522
VisitTransactionRoots(RootVisitor * visitor)2523 void Runtime::VisitTransactionRoots(RootVisitor* visitor) {
2524 for (Transaction& transaction : preinitialization_transactions_) {
2525 transaction.VisitRoots(visitor);
2526 }
2527 }
2528
VisitNonThreadRoots(RootVisitor * visitor)2529 void Runtime::VisitNonThreadRoots(RootVisitor* visitor) {
2530 java_vm_->VisitRoots(visitor);
2531 sentinel_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal));
2532 pre_allocated_OutOfMemoryError_when_throwing_exception_
2533 .VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal));
2534 pre_allocated_OutOfMemoryError_when_throwing_oome_
2535 .VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal));
2536 pre_allocated_OutOfMemoryError_when_handling_stack_overflow_
2537 .VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal));
2538 pre_allocated_NoClassDefFoundError_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal));
2539 VisitImageRoots(visitor);
2540 verifier::ClassVerifier::VisitStaticRoots(visitor);
2541 VisitTransactionRoots(visitor);
2542 }
2543
VisitNonConcurrentRoots(RootVisitor * visitor,VisitRootFlags flags)2544 void Runtime::VisitNonConcurrentRoots(RootVisitor* visitor, VisitRootFlags flags) {
2545 VisitThreadRoots(visitor, flags);
2546 VisitNonThreadRoots(visitor);
2547 }
2548
VisitThreadRoots(RootVisitor * visitor,VisitRootFlags flags)2549 void Runtime::VisitThreadRoots(RootVisitor* visitor, VisitRootFlags flags) {
2550 thread_list_->VisitRoots(visitor, flags);
2551 }
2552
VisitRoots(RootVisitor * visitor,VisitRootFlags flags)2553 void Runtime::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) {
2554 VisitNonConcurrentRoots(visitor, flags);
2555 VisitConcurrentRoots(visitor, flags);
2556 }
2557
VisitReflectiveTargets(ReflectiveValueVisitor * visitor)2558 void Runtime::VisitReflectiveTargets(ReflectiveValueVisitor *visitor) {
2559 thread_list_->VisitReflectiveTargets(visitor);
2560 heap_->VisitReflectiveTargets(visitor);
2561 jni_id_manager_->VisitReflectiveTargets(visitor);
2562 callbacks_->VisitReflectiveTargets(visitor);
2563 }
2564
VisitImageRoots(RootVisitor * visitor)2565 void Runtime::VisitImageRoots(RootVisitor* visitor) {
2566 // We only confirm that image roots are unchanged.
2567 if (kIsDebugBuild) {
2568 for (auto* space : GetHeap()->GetContinuousSpaces()) {
2569 if (space->IsImageSpace()) {
2570 auto* image_space = space->AsImageSpace();
2571 const auto& image_header = image_space->GetImageHeader();
2572 for (int32_t i = 0, size = image_header.GetImageRoots()->GetLength(); i != size; ++i) {
2573 mirror::Object* obj =
2574 image_header.GetImageRoot(static_cast<ImageHeader::ImageRoot>(i)).Ptr();
2575 if (obj != nullptr) {
2576 mirror::Object* after_obj = obj;
2577 visitor->VisitRoot(&after_obj, RootInfo(kRootStickyClass));
2578 CHECK_EQ(after_obj, obj);
2579 }
2580 }
2581 }
2582 }
2583 }
2584 }
2585
CreateRuntimeMethod(ClassLinker * class_linker,LinearAlloc * linear_alloc)2586 static ArtMethod* CreateRuntimeMethod(ClassLinker* class_linker, LinearAlloc* linear_alloc)
2587 REQUIRES_SHARED(Locks::mutator_lock_) {
2588 const PointerSize image_pointer_size = class_linker->GetImagePointerSize();
2589 const size_t method_alignment = ArtMethod::Alignment(image_pointer_size);
2590 const size_t method_size = ArtMethod::Size(image_pointer_size);
2591 LengthPrefixedArray<ArtMethod>* method_array = class_linker->AllocArtMethodArray(
2592 Thread::Current(),
2593 linear_alloc,
2594 1);
2595 ArtMethod* method = &method_array->At(0, method_size, method_alignment);
2596 CHECK(method != nullptr);
2597 method->SetDexMethodIndex(dex::kDexNoIndex);
2598 CHECK(method->IsRuntimeMethod());
2599 return method;
2600 }
2601
CreateImtConflictMethod(LinearAlloc * linear_alloc)2602 ArtMethod* Runtime::CreateImtConflictMethod(LinearAlloc* linear_alloc) {
2603 ClassLinker* const class_linker = GetClassLinker();
2604 ArtMethod* method = CreateRuntimeMethod(class_linker, linear_alloc);
2605 // When compiling, the code pointer will get set later when the image is loaded.
2606 const PointerSize pointer_size = GetInstructionSetPointerSize(instruction_set_);
2607 if (IsAotCompiler()) {
2608 method->SetEntryPointFromQuickCompiledCodePtrSize(nullptr, pointer_size);
2609 } else {
2610 method->SetEntryPointFromQuickCompiledCode(GetQuickImtConflictStub());
2611 }
2612 // Create empty conflict table.
2613 method->SetImtConflictTable(class_linker->CreateImtConflictTable(/*count=*/0u, linear_alloc),
2614 pointer_size);
2615 return method;
2616 }
2617
SetImtConflictMethod(ArtMethod * method)2618 void Runtime::SetImtConflictMethod(ArtMethod* method) {
2619 CHECK(method != nullptr);
2620 CHECK(method->IsRuntimeMethod());
2621 imt_conflict_method_ = method;
2622 }
2623
CreateResolutionMethod()2624 ArtMethod* Runtime::CreateResolutionMethod() {
2625 auto* method = CreateRuntimeMethod(GetClassLinker(), GetLinearAlloc());
2626 // When compiling, the code pointer will get set later when the image is loaded.
2627 if (IsAotCompiler()) {
2628 PointerSize pointer_size = GetInstructionSetPointerSize(instruction_set_);
2629 method->SetEntryPointFromQuickCompiledCodePtrSize(nullptr, pointer_size);
2630 method->SetEntryPointFromJniPtrSize(nullptr, pointer_size);
2631 } else {
2632 method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub());
2633 method->SetEntryPointFromJni(GetJniDlsymLookupCriticalStub());
2634 }
2635 return method;
2636 }
2637
CreateCalleeSaveMethod()2638 ArtMethod* Runtime::CreateCalleeSaveMethod() {
2639 auto* method = CreateRuntimeMethod(GetClassLinker(), GetLinearAlloc());
2640 PointerSize pointer_size = GetInstructionSetPointerSize(instruction_set_);
2641 method->SetEntryPointFromQuickCompiledCodePtrSize(nullptr, pointer_size);
2642 DCHECK_NE(instruction_set_, InstructionSet::kNone);
2643 DCHECK(method->IsRuntimeMethod());
2644 return method;
2645 }
2646
DisallowNewSystemWeaks()2647 void Runtime::DisallowNewSystemWeaks() {
2648 CHECK(!gUseReadBarrier);
2649 monitor_list_->DisallowNewMonitors();
2650 intern_table_->ChangeWeakRootState(gc::kWeakRootStateNoReadsOrWrites);
2651 java_vm_->DisallowNewWeakGlobals();
2652 heap_->DisallowNewAllocationRecords();
2653 if (GetJit() != nullptr) {
2654 GetJit()->GetCodeCache()->DisallowInlineCacheAccess();
2655 }
2656
2657 // All other generic system-weak holders.
2658 for (gc::AbstractSystemWeakHolder* holder : system_weak_holders_) {
2659 holder->Disallow();
2660 }
2661 }
2662
AllowNewSystemWeaks()2663 void Runtime::AllowNewSystemWeaks() {
2664 CHECK(!gUseReadBarrier);
2665 monitor_list_->AllowNewMonitors();
2666 intern_table_->ChangeWeakRootState(gc::kWeakRootStateNormal); // TODO: Do this in the sweeping.
2667 java_vm_->AllowNewWeakGlobals();
2668 heap_->AllowNewAllocationRecords();
2669 if (GetJit() != nullptr) {
2670 GetJit()->GetCodeCache()->AllowInlineCacheAccess();
2671 }
2672
2673 // All other generic system-weak holders.
2674 for (gc::AbstractSystemWeakHolder* holder : system_weak_holders_) {
2675 holder->Allow();
2676 }
2677 }
2678
BroadcastForNewSystemWeaks(bool broadcast_for_checkpoint)2679 void Runtime::BroadcastForNewSystemWeaks(bool broadcast_for_checkpoint) {
2680 // This is used for the read barrier case that uses the thread-local
2681 // Thread::GetWeakRefAccessEnabled() flag and the checkpoint while weak ref access is disabled
2682 // (see ThreadList::RunCheckpoint).
2683 monitor_list_->BroadcastForNewMonitors();
2684 intern_table_->BroadcastForNewInterns();
2685 java_vm_->BroadcastForNewWeakGlobals();
2686 heap_->BroadcastForNewAllocationRecords();
2687 if (GetJit() != nullptr) {
2688 GetJit()->GetCodeCache()->BroadcastForInlineCacheAccess();
2689 }
2690
2691 // All other generic system-weak holders.
2692 for (gc::AbstractSystemWeakHolder* holder : system_weak_holders_) {
2693 holder->Broadcast(broadcast_for_checkpoint);
2694 }
2695 }
2696
SetInstructionSet(InstructionSet instruction_set)2697 void Runtime::SetInstructionSet(InstructionSet instruction_set) {
2698 instruction_set_ = instruction_set;
2699 switch (instruction_set) {
2700 case InstructionSet::kThumb2:
2701 // kThumb2 is the same as kArm, use the canonical value.
2702 instruction_set_ = InstructionSet::kArm;
2703 break;
2704 case InstructionSet::kArm:
2705 case InstructionSet::kArm64:
2706 case InstructionSet::kRiscv64:
2707 case InstructionSet::kX86:
2708 case InstructionSet::kX86_64:
2709 break;
2710 default:
2711 UNIMPLEMENTED(FATAL) << instruction_set_;
2712 UNREACHABLE();
2713 }
2714 }
2715
ClearInstructionSet()2716 void Runtime::ClearInstructionSet() {
2717 instruction_set_ = InstructionSet::kNone;
2718 }
2719
SetCalleeSaveMethod(ArtMethod * method,CalleeSaveType type)2720 void Runtime::SetCalleeSaveMethod(ArtMethod* method, CalleeSaveType type) {
2721 DCHECK_LT(static_cast<uint32_t>(type), kCalleeSaveSize);
2722 CHECK(method != nullptr);
2723 callee_save_methods_[static_cast<size_t>(type)] = reinterpret_cast<uintptr_t>(method);
2724 }
2725
ClearCalleeSaveMethods()2726 void Runtime::ClearCalleeSaveMethods() {
2727 for (size_t i = 0; i < kCalleeSaveSize; ++i) {
2728 callee_save_methods_[i] = reinterpret_cast<uintptr_t>(nullptr);
2729 }
2730 }
2731
RegisterAppInfo(const std::string & package_name,const std::vector<std::string> & code_paths,const std::string & profile_output_filename,const std::string & ref_profile_filename,int32_t code_type)2732 void Runtime::RegisterAppInfo(const std::string& package_name,
2733 const std::vector<std::string>& code_paths,
2734 const std::string& profile_output_filename,
2735 const std::string& ref_profile_filename,
2736 int32_t code_type) {
2737 app_info_.RegisterAppInfo(
2738 package_name,
2739 code_paths,
2740 profile_output_filename,
2741 ref_profile_filename,
2742 AppInfo::FromVMRuntimeConstants(code_type));
2743
2744 if (metrics_reporter_ != nullptr) {
2745 metrics_reporter_->NotifyAppInfoUpdated(&app_info_);
2746 }
2747
2748 if (jit_.get() == nullptr) {
2749 // We are not JITing. Nothing to do.
2750 return;
2751 }
2752
2753 VLOG(profiler) << "Register app with " << profile_output_filename
2754 << " " << android::base::Join(code_paths, ':');
2755 VLOG(profiler) << "Reference profile is: " << ref_profile_filename;
2756
2757 if (profile_output_filename.empty()) {
2758 LOG(WARNING) << "JIT profile information will not be recorded: profile filename is empty.";
2759 return;
2760 }
2761 if (code_paths.empty()) {
2762 LOG(WARNING) << "JIT profile information will not be recorded: code paths is empty.";
2763 return;
2764 }
2765
2766 // Framework calls this method for all split APKs. Ignore the calls for the ones with no dex code
2767 // so that we don't unnecessarily create profiles for them or write bootclasspath profiling info
2768 // to those profiles.
2769 bool has_code = false;
2770 for (const std::string& path : code_paths) {
2771 std::string error_msg;
2772 std::vector<uint32_t> checksums;
2773 std::vector<std::string> dex_locations;
2774 if (!ArtDexFileLoader::GetMultiDexChecksums(
2775 path.c_str(), &checksums, &dex_locations, &error_msg)) {
2776 LOG(WARNING) << error_msg;
2777 continue;
2778 }
2779 if (dex_locations.size() > 0) {
2780 has_code = true;
2781 break;
2782 }
2783 }
2784 if (!has_code) {
2785 VLOG(profiler) << "JIT profile information will not be recorded: no dex code in '" +
2786 android::base::Join(code_paths, ',') + "'.";
2787 return;
2788 }
2789
2790 jit_->StartProfileSaver(profile_output_filename, code_paths, ref_profile_filename);
2791 }
2792
2793 // Transaction support.
IsActiveTransaction() const2794 bool Runtime::IsActiveTransaction() const {
2795 return !preinitialization_transactions_.empty() && !GetTransaction()->IsRollingBack();
2796 }
2797
EnterTransactionMode(bool strict,mirror::Class * root)2798 void Runtime::EnterTransactionMode(bool strict, mirror::Class* root) {
2799 DCHECK(IsAotCompiler());
2800 ArenaPool* arena_pool = nullptr;
2801 ArenaStack* arena_stack = nullptr;
2802 if (preinitialization_transactions_.empty()) { // Top-level transaction?
2803 // Make initialized classes visibly initialized now. If that happened during the transaction
2804 // and then the transaction was aborted, we would roll back the status update but not the
2805 // ClassLinker's bookkeeping structures, so these classes would never be visibly initialized.
2806 {
2807 Thread* self = Thread::Current();
2808 StackHandleScope<1> hs(self);
2809 HandleWrapper<mirror::Class> h(hs.NewHandleWrapper(&root));
2810 ScopedThreadSuspension sts(self, ThreadState::kNative);
2811 GetClassLinker()->MakeInitializedClassesVisiblyInitialized(Thread::Current(), /*wait=*/ true);
2812 }
2813 // Pass the runtime `ArenaPool` to the transaction.
2814 arena_pool = GetArenaPool();
2815 } else {
2816 // Pass the `ArenaStack` from previous transaction to the new one.
2817 arena_stack = preinitialization_transactions_.front().GetArenaStack();
2818 }
2819 preinitialization_transactions_.emplace_front(strict, root, arena_stack, arena_pool);
2820 }
2821
ExitTransactionMode()2822 void Runtime::ExitTransactionMode() {
2823 DCHECK(IsAotCompiler());
2824 DCHECK(IsActiveTransaction());
2825 preinitialization_transactions_.pop_front();
2826 }
2827
RollbackAndExitTransactionMode()2828 void Runtime::RollbackAndExitTransactionMode() {
2829 DCHECK(IsAotCompiler());
2830 DCHECK(IsActiveTransaction());
2831 preinitialization_transactions_.front().Rollback();
2832 preinitialization_transactions_.pop_front();
2833 }
2834
IsTransactionAborted() const2835 bool Runtime::IsTransactionAborted() const {
2836 if (!IsActiveTransaction()) {
2837 return false;
2838 } else {
2839 DCHECK(IsAotCompiler());
2840 return GetTransaction()->IsAborted();
2841 }
2842 }
2843
RollbackAllTransactions()2844 void Runtime::RollbackAllTransactions() {
2845 // If transaction is aborted, all transactions will be kept in the list.
2846 // Rollback and exit all of them.
2847 while (IsActiveTransaction()) {
2848 RollbackAndExitTransactionMode();
2849 }
2850 }
2851
IsActiveStrictTransactionMode() const2852 bool Runtime::IsActiveStrictTransactionMode() const {
2853 return IsActiveTransaction() && GetTransaction()->IsStrict();
2854 }
2855
GetTransaction() const2856 const Transaction* Runtime::GetTransaction() const {
2857 DCHECK(!preinitialization_transactions_.empty());
2858 return &preinitialization_transactions_.front();
2859 }
2860
GetTransaction()2861 Transaction* Runtime::GetTransaction() {
2862 DCHECK(!preinitialization_transactions_.empty());
2863 return &preinitialization_transactions_.front();
2864 }
2865
AbortTransactionAndThrowAbortError(Thread * self,const std::string & abort_message)2866 void Runtime::AbortTransactionAndThrowAbortError(Thread* self, const std::string& abort_message) {
2867 DCHECK(IsAotCompiler());
2868 DCHECK(IsActiveTransaction());
2869 // Throwing an exception may cause its class initialization. If we mark the transaction
2870 // aborted before that, we may warn with a false alarm. Throwing the exception before
2871 // marking the transaction aborted avoids that.
2872 // But now the transaction can be nested, and abort the transaction will relax the constraints
2873 // for constructing stack trace.
2874 GetTransaction()->Abort(abort_message);
2875 GetTransaction()->ThrowAbortError(self, &abort_message);
2876 }
2877
ThrowTransactionAbortError(Thread * self)2878 void Runtime::ThrowTransactionAbortError(Thread* self) {
2879 DCHECK(IsAotCompiler());
2880 DCHECK(IsActiveTransaction());
2881 // Passing nullptr means we rethrow an exception with the earlier transaction abort message.
2882 GetTransaction()->ThrowAbortError(self, nullptr);
2883 }
2884
RecordWriteFieldBoolean(mirror::Object * obj,MemberOffset field_offset,uint8_t value,bool is_volatile)2885 void Runtime::RecordWriteFieldBoolean(mirror::Object* obj,
2886 MemberOffset field_offset,
2887 uint8_t value,
2888 bool is_volatile) {
2889 DCHECK(IsAotCompiler());
2890 DCHECK(IsActiveTransaction());
2891 GetTransaction()->RecordWriteFieldBoolean(obj, field_offset, value, is_volatile);
2892 }
2893
RecordWriteFieldByte(mirror::Object * obj,MemberOffset field_offset,int8_t value,bool is_volatile)2894 void Runtime::RecordWriteFieldByte(mirror::Object* obj,
2895 MemberOffset field_offset,
2896 int8_t value,
2897 bool is_volatile) {
2898 DCHECK(IsAotCompiler());
2899 DCHECK(IsActiveTransaction());
2900 GetTransaction()->RecordWriteFieldByte(obj, field_offset, value, is_volatile);
2901 }
2902
RecordWriteFieldChar(mirror::Object * obj,MemberOffset field_offset,uint16_t value,bool is_volatile)2903 void Runtime::RecordWriteFieldChar(mirror::Object* obj,
2904 MemberOffset field_offset,
2905 uint16_t value,
2906 bool is_volatile) {
2907 DCHECK(IsAotCompiler());
2908 DCHECK(IsActiveTransaction());
2909 GetTransaction()->RecordWriteFieldChar(obj, field_offset, value, is_volatile);
2910 }
2911
RecordWriteFieldShort(mirror::Object * obj,MemberOffset field_offset,int16_t value,bool is_volatile)2912 void Runtime::RecordWriteFieldShort(mirror::Object* obj,
2913 MemberOffset field_offset,
2914 int16_t value,
2915 bool is_volatile) {
2916 DCHECK(IsAotCompiler());
2917 DCHECK(IsActiveTransaction());
2918 GetTransaction()->RecordWriteFieldShort(obj, field_offset, value, is_volatile);
2919 }
2920
RecordWriteField32(mirror::Object * obj,MemberOffset field_offset,uint32_t value,bool is_volatile)2921 void Runtime::RecordWriteField32(mirror::Object* obj,
2922 MemberOffset field_offset,
2923 uint32_t value,
2924 bool is_volatile) {
2925 DCHECK(IsAotCompiler());
2926 DCHECK(IsActiveTransaction());
2927 GetTransaction()->RecordWriteField32(obj, field_offset, value, is_volatile);
2928 }
2929
RecordWriteField64(mirror::Object * obj,MemberOffset field_offset,uint64_t value,bool is_volatile)2930 void Runtime::RecordWriteField64(mirror::Object* obj,
2931 MemberOffset field_offset,
2932 uint64_t value,
2933 bool is_volatile) {
2934 DCHECK(IsAotCompiler());
2935 DCHECK(IsActiveTransaction());
2936 GetTransaction()->RecordWriteField64(obj, field_offset, value, is_volatile);
2937 }
2938
RecordWriteFieldReference(mirror::Object * obj,MemberOffset field_offset,ObjPtr<mirror::Object> value,bool is_volatile)2939 void Runtime::RecordWriteFieldReference(mirror::Object* obj,
2940 MemberOffset field_offset,
2941 ObjPtr<mirror::Object> value,
2942 bool is_volatile) {
2943 DCHECK(IsAotCompiler());
2944 DCHECK(IsActiveTransaction());
2945 GetTransaction()->RecordWriteFieldReference(obj, field_offset, value.Ptr(), is_volatile);
2946 }
2947
RecordWriteArray(mirror::Array * array,size_t index,uint64_t value)2948 void Runtime::RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) {
2949 DCHECK(IsAotCompiler());
2950 DCHECK(IsActiveTransaction());
2951 GetTransaction()->RecordWriteArray(array, index, value);
2952 }
2953
RecordStrongStringInsertion(ObjPtr<mirror::String> s)2954 void Runtime::RecordStrongStringInsertion(ObjPtr<mirror::String> s) {
2955 DCHECK(IsAotCompiler());
2956 DCHECK(IsActiveTransaction());
2957 GetTransaction()->RecordStrongStringInsertion(s);
2958 }
2959
RecordWeakStringInsertion(ObjPtr<mirror::String> s)2960 void Runtime::RecordWeakStringInsertion(ObjPtr<mirror::String> s) {
2961 DCHECK(IsAotCompiler());
2962 DCHECK(IsActiveTransaction());
2963 GetTransaction()->RecordWeakStringInsertion(s);
2964 }
2965
RecordStrongStringRemoval(ObjPtr<mirror::String> s)2966 void Runtime::RecordStrongStringRemoval(ObjPtr<mirror::String> s) {
2967 DCHECK(IsAotCompiler());
2968 DCHECK(IsActiveTransaction());
2969 GetTransaction()->RecordStrongStringRemoval(s);
2970 }
2971
RecordWeakStringRemoval(ObjPtr<mirror::String> s)2972 void Runtime::RecordWeakStringRemoval(ObjPtr<mirror::String> s) {
2973 DCHECK(IsAotCompiler());
2974 DCHECK(IsActiveTransaction());
2975 GetTransaction()->RecordWeakStringRemoval(s);
2976 }
2977
RecordResolveString(ObjPtr<mirror::DexCache> dex_cache,dex::StringIndex string_idx)2978 void Runtime::RecordResolveString(ObjPtr<mirror::DexCache> dex_cache,
2979 dex::StringIndex string_idx) {
2980 DCHECK(IsAotCompiler());
2981 DCHECK(IsActiveTransaction());
2982 GetTransaction()->RecordResolveString(dex_cache, string_idx);
2983 }
2984
RecordResolveMethodType(ObjPtr<mirror::DexCache> dex_cache,dex::ProtoIndex proto_idx)2985 void Runtime::RecordResolveMethodType(ObjPtr<mirror::DexCache> dex_cache,
2986 dex::ProtoIndex proto_idx) {
2987 DCHECK(IsAotCompiler());
2988 DCHECK(IsActiveTransaction());
2989 GetTransaction()->RecordResolveMethodType(dex_cache, proto_idx);
2990 }
2991
SetFaultMessage(const std::string & message)2992 void Runtime::SetFaultMessage(const std::string& message) {
2993 std::string* new_msg = new std::string(message);
2994 std::string* cur_msg = fault_message_.exchange(new_msg);
2995 delete cur_msg;
2996 }
2997
GetFaultMessage()2998 std::string Runtime::GetFaultMessage() {
2999 // Retrieve the message. Temporarily replace with null so that SetFaultMessage will not delete
3000 // the string in parallel.
3001 std::string* cur_msg = fault_message_.exchange(nullptr);
3002
3003 // Make a copy of the string.
3004 std::string ret = cur_msg == nullptr ? "" : *cur_msg;
3005
3006 // Put the message back if it hasn't been updated.
3007 std::string* null_str = nullptr;
3008 if (!fault_message_.compare_exchange_strong(null_str, cur_msg)) {
3009 // Already replaced.
3010 delete cur_msg;
3011 }
3012
3013 return ret;
3014 }
3015
AddCurrentRuntimeFeaturesAsDex2OatArguments(std::vector<std::string> * argv) const3016 void Runtime::AddCurrentRuntimeFeaturesAsDex2OatArguments(std::vector<std::string>* argv)
3017 const {
3018 if (GetInstrumentation()->InterpretOnly()) {
3019 argv->push_back("--compiler-filter=quicken");
3020 }
3021
3022 // Make the dex2oat instruction set match that of the launching runtime. If we have multiple
3023 // architecture support, dex2oat may be compiled as a different instruction-set than that
3024 // currently being executed.
3025 std::string instruction_set("--instruction-set=");
3026 instruction_set += GetInstructionSetString(kRuntimeISA);
3027 argv->push_back(instruction_set);
3028
3029 if (InstructionSetFeatures::IsRuntimeDetectionSupported()) {
3030 argv->push_back("--instruction-set-features=runtime");
3031 } else {
3032 std::unique_ptr<const InstructionSetFeatures> features(
3033 InstructionSetFeatures::FromCppDefines());
3034 std::string feature_string("--instruction-set-features=");
3035 feature_string += features->GetFeatureString();
3036 argv->push_back(feature_string);
3037 }
3038 }
3039
CreateJit()3040 void Runtime::CreateJit() {
3041 DCHECK(jit_code_cache_ == nullptr);
3042 DCHECK(jit_ == nullptr);
3043 if (kIsDebugBuild && GetInstrumentation()->IsForcedInterpretOnly()) {
3044 DCHECK(!jit_options_->UseJitCompilation());
3045 }
3046
3047 if (!jit_options_->UseJitCompilation() && !jit_options_->GetSaveProfilingInfo()) {
3048 return;
3049 }
3050
3051 if (IsSafeMode()) {
3052 LOG(INFO) << "Not creating JIT because of SafeMode.";
3053 return;
3054 }
3055
3056 std::string error_msg;
3057 bool profiling_only = !jit_options_->UseJitCompilation();
3058 jit_code_cache_.reset(jit::JitCodeCache::Create(profiling_only,
3059 /*rwx_memory_allowed=*/ true,
3060 IsZygote(),
3061 &error_msg));
3062 if (jit_code_cache_.get() == nullptr) {
3063 LOG(WARNING) << "Failed to create JIT Code Cache: " << error_msg;
3064 return;
3065 }
3066
3067 jit::Jit* jit = jit::Jit::Create(jit_code_cache_.get(), jit_options_.get());
3068 jit_.reset(jit);
3069 if (jit == nullptr) {
3070 LOG(WARNING) << "Failed to allocate JIT";
3071 // Release JIT code cache resources (several MB of memory).
3072 jit_code_cache_.reset();
3073 } else {
3074 jit->CreateThreadPool();
3075 }
3076 }
3077
CanRelocate() const3078 bool Runtime::CanRelocate() const {
3079 return !IsAotCompiler();
3080 }
3081
IsCompilingBootImage() const3082 bool Runtime::IsCompilingBootImage() const {
3083 return IsCompiler() && compiler_callbacks_->IsBootImage();
3084 }
3085
SetResolutionMethod(ArtMethod * method)3086 void Runtime::SetResolutionMethod(ArtMethod* method) {
3087 CHECK(method != nullptr);
3088 CHECK(method->IsRuntimeMethod()) << method;
3089 resolution_method_ = method;
3090 }
3091
SetImtUnimplementedMethod(ArtMethod * method)3092 void Runtime::SetImtUnimplementedMethod(ArtMethod* method) {
3093 CHECK(method != nullptr);
3094 CHECK(method->IsRuntimeMethod());
3095 imt_unimplemented_method_ = method;
3096 }
3097
FixupConflictTables()3098 void Runtime::FixupConflictTables() {
3099 // We can only do this after the class linker is created.
3100 const PointerSize pointer_size = GetClassLinker()->GetImagePointerSize();
3101 if (imt_unimplemented_method_->GetImtConflictTable(pointer_size) == nullptr) {
3102 imt_unimplemented_method_->SetImtConflictTable(
3103 ClassLinker::CreateImtConflictTable(/*count=*/0u, GetLinearAlloc(), pointer_size),
3104 pointer_size);
3105 }
3106 if (imt_conflict_method_->GetImtConflictTable(pointer_size) == nullptr) {
3107 imt_conflict_method_->SetImtConflictTable(
3108 ClassLinker::CreateImtConflictTable(/*count=*/0u, GetLinearAlloc(), pointer_size),
3109 pointer_size);
3110 }
3111 }
3112
DisableVerifier()3113 void Runtime::DisableVerifier() {
3114 verify_ = verifier::VerifyMode::kNone;
3115 }
3116
IsVerificationEnabled() const3117 bool Runtime::IsVerificationEnabled() const {
3118 return verify_ == verifier::VerifyMode::kEnable ||
3119 verify_ == verifier::VerifyMode::kSoftFail;
3120 }
3121
IsVerificationSoftFail() const3122 bool Runtime::IsVerificationSoftFail() const {
3123 return verify_ == verifier::VerifyMode::kSoftFail;
3124 }
3125
IsAsyncDeoptimizeable(ArtMethod * method,uintptr_t code) const3126 bool Runtime::IsAsyncDeoptimizeable(ArtMethod* method, uintptr_t code) const {
3127 if (OatQuickMethodHeader::NterpMethodHeader != nullptr) {
3128 if (OatQuickMethodHeader::NterpMethodHeader->Contains(code)) {
3129 return true;
3130 }
3131 }
3132
3133 // We only support async deopt (ie the compiled code is not explicitly asking for
3134 // deopt, but something else like the debugger) in debuggable JIT code.
3135 // We could look at the oat file where `code` is being defined,
3136 // and check whether it's been compiled debuggable, but we decided to
3137 // only rely on the JIT for debuggable apps.
3138 // The JIT-zygote is not debuggable so we need to be sure to exclude code from the non-private
3139 // region as well.
3140 if (GetJit() != nullptr &&
3141 GetJit()->GetCodeCache()->PrivateRegionContainsPc(reinterpret_cast<const void*>(code))) {
3142 // If the code is JITed code then check if it was compiled as debuggable.
3143 const OatQuickMethodHeader* header = method->GetOatQuickMethodHeader(code);
3144 return CodeInfo::IsDebuggable(header->GetOptimizedCodeInfoPtr());
3145 }
3146
3147 return false;
3148 }
3149
3150
CreateLinearAlloc()3151 LinearAlloc* Runtime::CreateLinearAlloc() {
3152 ArenaPool* pool = linear_alloc_arena_pool_.get();
3153 return pool != nullptr
3154 ? new LinearAlloc(pool, gUseUserfaultfd)
3155 : new LinearAlloc(arena_pool_.get(), /*track_allocs=*/ false);
3156 }
3157
3158 class Runtime::SetupLinearAllocForZygoteFork : public AllocatorVisitor {
3159 public:
SetupLinearAllocForZygoteFork(Thread * self)3160 explicit SetupLinearAllocForZygoteFork(Thread* self) : self_(self) {}
3161
Visit(LinearAlloc * alloc)3162 bool Visit(LinearAlloc* alloc) override {
3163 alloc->SetupForPostZygoteFork(self_);
3164 return true;
3165 }
3166
3167 private:
3168 Thread* self_;
3169 };
3170
SetupLinearAllocForPostZygoteFork(Thread * self)3171 void Runtime::SetupLinearAllocForPostZygoteFork(Thread* self) {
3172 if (gUseUserfaultfd) {
3173 // Setup all the linear-allocs out there for post-zygote fork. This will
3174 // basically force the arena allocator to ask for a new arena for the next
3175 // allocation. All arenas allocated from now on will be in the userfaultfd
3176 // visited space.
3177 if (GetLinearAlloc() != nullptr) {
3178 GetLinearAlloc()->SetupForPostZygoteFork(self);
3179 }
3180 if (GetStartupLinearAlloc() != nullptr) {
3181 GetStartupLinearAlloc()->SetupForPostZygoteFork(self);
3182 }
3183 {
3184 Locks::mutator_lock_->AssertNotHeld(self);
3185 ReaderMutexLock mu2(self, *Locks::mutator_lock_);
3186 ReaderMutexLock mu3(self, *Locks::classlinker_classes_lock_);
3187 SetupLinearAllocForZygoteFork visitor(self);
3188 GetClassLinker()->VisitAllocators(&visitor);
3189 }
3190 static_cast<GcVisitedArenaPool*>(GetLinearAllocArenaPool())->SetupPostZygoteMode();
3191 }
3192 }
3193
GetHashTableMinLoadFactor() const3194 double Runtime::GetHashTableMinLoadFactor() const {
3195 return is_low_memory_mode_ ? kLowMemoryMinLoadFactor : kNormalMinLoadFactor;
3196 }
3197
GetHashTableMaxLoadFactor() const3198 double Runtime::GetHashTableMaxLoadFactor() const {
3199 return is_low_memory_mode_ ? kLowMemoryMaxLoadFactor : kNormalMaxLoadFactor;
3200 }
3201
UpdateProcessState(ProcessState process_state)3202 void Runtime::UpdateProcessState(ProcessState process_state) {
3203 ProcessState old_process_state = process_state_;
3204 process_state_ = process_state;
3205 GetHeap()->UpdateProcessState(old_process_state, process_state);
3206 }
3207
RegisterSensitiveThread() const3208 void Runtime::RegisterSensitiveThread() const {
3209 Thread::SetJitSensitiveThread();
3210 }
3211
3212 // Returns true if JIT compilations are enabled. GetJit() will be not null in this case.
UseJitCompilation() const3213 bool Runtime::UseJitCompilation() const {
3214 return (jit_ != nullptr) && jit_->UseJitCompilation();
3215 }
3216
TakeSnapshot()3217 void Runtime::EnvSnapshot::TakeSnapshot() {
3218 char** env = GetEnviron();
3219 for (size_t i = 0; env[i] != nullptr; ++i) {
3220 name_value_pairs_.emplace_back(new std::string(env[i]));
3221 }
3222 // The strings in name_value_pairs_ retain ownership of the c_str, but we assign pointers
3223 // for quick use by GetSnapshot. This avoids allocation and copying cost at Exec.
3224 c_env_vector_.reset(new char*[name_value_pairs_.size() + 1]);
3225 for (size_t i = 0; env[i] != nullptr; ++i) {
3226 c_env_vector_[i] = const_cast<char*>(name_value_pairs_[i]->c_str());
3227 }
3228 c_env_vector_[name_value_pairs_.size()] = nullptr;
3229 }
3230
GetSnapshot() const3231 char** Runtime::EnvSnapshot::GetSnapshot() const {
3232 return c_env_vector_.get();
3233 }
3234
AddSystemWeakHolder(gc::AbstractSystemWeakHolder * holder)3235 void Runtime::AddSystemWeakHolder(gc::AbstractSystemWeakHolder* holder) {
3236 gc::ScopedGCCriticalSection gcs(Thread::Current(),
3237 gc::kGcCauseAddRemoveSystemWeakHolder,
3238 gc::kCollectorTypeAddRemoveSystemWeakHolder);
3239 // Note: The ScopedGCCriticalSection also ensures that the rest of the function is in
3240 // a critical section.
3241 system_weak_holders_.push_back(holder);
3242 }
3243
RemoveSystemWeakHolder(gc::AbstractSystemWeakHolder * holder)3244 void Runtime::RemoveSystemWeakHolder(gc::AbstractSystemWeakHolder* holder) {
3245 gc::ScopedGCCriticalSection gcs(Thread::Current(),
3246 gc::kGcCauseAddRemoveSystemWeakHolder,
3247 gc::kCollectorTypeAddRemoveSystemWeakHolder);
3248 auto it = std::find(system_weak_holders_.begin(), system_weak_holders_.end(), holder);
3249 if (it != system_weak_holders_.end()) {
3250 system_weak_holders_.erase(it);
3251 }
3252 }
3253
GetRuntimeCallbacks()3254 RuntimeCallbacks* Runtime::GetRuntimeCallbacks() {
3255 return callbacks_.get();
3256 }
3257
3258 // Used to patch boot image method entry point to interpreter bridge.
3259 class UpdateEntryPointsClassVisitor : public ClassVisitor {
3260 public:
UpdateEntryPointsClassVisitor(instrumentation::Instrumentation * instrumentation)3261 explicit UpdateEntryPointsClassVisitor(instrumentation::Instrumentation* instrumentation)
3262 : instrumentation_(instrumentation) {}
3263
operator ()(ObjPtr<mirror::Class> klass)3264 bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES(Locks::mutator_lock_) {
3265 DCHECK(Locks::mutator_lock_->IsExclusiveHeld(Thread::Current()));
3266 auto pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
3267 for (auto& m : klass->GetMethods(pointer_size)) {
3268 const void* code = m.GetEntryPointFromQuickCompiledCode();
3269 if (!m.IsInvokable()) {
3270 continue;
3271 }
3272 // For java debuggable runtimes we also deoptimize native methods. For other cases (boot
3273 // image profiling) we don't need to deoptimize native methods. If this changes also
3274 // update Instrumentation::CanUseAotCode.
3275 bool deoptimize_native_methods = Runtime::Current()->IsJavaDebuggable();
3276 if (Runtime::Current()->GetHeap()->IsInBootImageOatFile(code) &&
3277 (!m.IsNative() || deoptimize_native_methods) &&
3278 !m.IsProxyMethod()) {
3279 instrumentation_->InitializeMethodsCode(&m, /*aot_code=*/ nullptr);
3280 }
3281
3282 if (Runtime::Current()->GetJit() != nullptr &&
3283 Runtime::Current()->GetJit()->GetCodeCache()->IsInZygoteExecSpace(code) &&
3284 (!m.IsNative() || deoptimize_native_methods)) {
3285 DCHECK(!m.IsProxyMethod());
3286 instrumentation_->InitializeMethodsCode(&m, /*aot_code=*/ nullptr);
3287 }
3288
3289 if (m.IsPreCompiled()) {
3290 // Precompilation is incompatible with debuggable, so clear the flag
3291 // and update the entrypoint in case it has been compiled.
3292 m.ClearPreCompiled();
3293 instrumentation_->InitializeMethodsCode(&m, /*aot_code=*/ nullptr);
3294 }
3295 }
3296 return true;
3297 }
3298
3299 private:
3300 instrumentation::Instrumentation* const instrumentation_;
3301 };
3302
SetRuntimeDebugState(RuntimeDebugState state)3303 void Runtime::SetRuntimeDebugState(RuntimeDebugState state) {
3304 if (state != RuntimeDebugState::kJavaDebuggableAtInit) {
3305 // We never change the state if we started as a debuggable runtime.
3306 DCHECK(runtime_debug_state_ != RuntimeDebugState::kJavaDebuggableAtInit);
3307 }
3308 runtime_debug_state_ = state;
3309 }
3310
DeoptimizeBootImage()3311 void Runtime::DeoptimizeBootImage() {
3312 // If we've already started and we are setting this runtime to debuggable,
3313 // we patch entry points of methods in boot image to interpreter bridge, as
3314 // boot image code may be AOT compiled as not debuggable.
3315 UpdateEntryPointsClassVisitor visitor(GetInstrumentation());
3316 GetClassLinker()->VisitClasses(&visitor);
3317 jit::Jit* jit = GetJit();
3318 if (jit != nullptr) {
3319 // Code previously compiled may not be compiled debuggable.
3320 jit->GetCodeCache()->TransitionToDebuggable();
3321 }
3322 }
3323
ScopedThreadPoolUsage()3324 Runtime::ScopedThreadPoolUsage::ScopedThreadPoolUsage()
3325 : thread_pool_(Runtime::Current()->AcquireThreadPool()) {}
3326
~ScopedThreadPoolUsage()3327 Runtime::ScopedThreadPoolUsage::~ScopedThreadPoolUsage() {
3328 Runtime::Current()->ReleaseThreadPool();
3329 }
3330
DeleteThreadPool()3331 bool Runtime::DeleteThreadPool() {
3332 // Make sure workers are started to prevent thread shutdown errors.
3333 WaitForThreadPoolWorkersToStart();
3334 std::unique_ptr<ThreadPool> thread_pool;
3335 {
3336 MutexLock mu(Thread::Current(), *Locks::runtime_thread_pool_lock_);
3337 if (thread_pool_ref_count_ == 0) {
3338 thread_pool = std::move(thread_pool_);
3339 }
3340 }
3341 return thread_pool != nullptr;
3342 }
3343
AcquireThreadPool()3344 ThreadPool* Runtime::AcquireThreadPool() {
3345 MutexLock mu(Thread::Current(), *Locks::runtime_thread_pool_lock_);
3346 ++thread_pool_ref_count_;
3347 return thread_pool_.get();
3348 }
3349
ReleaseThreadPool()3350 void Runtime::ReleaseThreadPool() {
3351 MutexLock mu(Thread::Current(), *Locks::runtime_thread_pool_lock_);
3352 CHECK_GT(thread_pool_ref_count_, 0u);
3353 --thread_pool_ref_count_;
3354 }
3355
WaitForThreadPoolWorkersToStart()3356 void Runtime::WaitForThreadPoolWorkersToStart() {
3357 // Need to make sure workers are created before deleting the pool.
3358 ScopedThreadPoolUsage stpu;
3359 if (stpu.GetThreadPool() != nullptr) {
3360 stpu.GetThreadPool()->WaitForWorkersToBeCreated();
3361 }
3362 }
3363
ResetStartupCompleted()3364 void Runtime::ResetStartupCompleted() {
3365 startup_completed_.store(false, std::memory_order_seq_cst);
3366 }
3367
NotifyStartupCompleted()3368 bool Runtime::NotifyStartupCompleted() {
3369 DCHECK(!IsZygote());
3370 bool expected = false;
3371 if (!startup_completed_.compare_exchange_strong(expected, true, std::memory_order_seq_cst)) {
3372 // Right now NotifyStartupCompleted will be called up to twice, once from profiler and up to
3373 // once externally. For this reason there are no asserts.
3374 return false;
3375 }
3376
3377 VLOG(startup) << app_info_;
3378
3379 ProfileSaver::NotifyStartupCompleted();
3380
3381 if (metrics_reporter_ != nullptr) {
3382 metrics_reporter_->NotifyStartupCompleted();
3383 }
3384 return true;
3385 }
3386
NotifyDexFileLoaded()3387 void Runtime::NotifyDexFileLoaded() {
3388 if (metrics_reporter_ != nullptr) {
3389 metrics_reporter_->NotifyAppInfoUpdated(&app_info_);
3390 }
3391 }
3392
GetStartupCompleted() const3393 bool Runtime::GetStartupCompleted() const {
3394 return startup_completed_.load(std::memory_order_seq_cst);
3395 }
3396
SetSignalHookDebuggable(bool value)3397 void Runtime::SetSignalHookDebuggable(bool value) {
3398 SkipAddSignalHandler(value);
3399 }
3400
SetJniIdType(JniIdType t)3401 void Runtime::SetJniIdType(JniIdType t) {
3402 CHECK(CanSetJniIdType()) << "Not allowed to change id type!";
3403 if (t == GetJniIdType()) {
3404 return;
3405 }
3406 jni_ids_indirection_ = t;
3407 JNIEnvExt::ResetFunctionTable();
3408 WellKnownClasses::HandleJniIdTypeChange(Thread::Current()->GetJniEnv());
3409 }
3410
IsSystemServerProfiled() const3411 bool Runtime::IsSystemServerProfiled() const {
3412 return IsSystemServer() && jit_options_->GetSaveProfilingInfo();
3413 }
3414
GetOatFilesExecutable() const3415 bool Runtime::GetOatFilesExecutable() const {
3416 return !IsAotCompiler() && !IsSystemServerProfiled();
3417 }
3418
MadviseFileForRange(size_t madvise_size_limit_bytes,size_t map_size_bytes,const uint8_t * map_begin,const uint8_t * map_end,const std::string & file_name)3419 void Runtime::MadviseFileForRange(size_t madvise_size_limit_bytes,
3420 size_t map_size_bytes,
3421 const uint8_t* map_begin,
3422 const uint8_t* map_end,
3423 const std::string& file_name) {
3424 map_begin = AlignDown(map_begin, kPageSize);
3425 map_size_bytes = RoundUp(map_size_bytes, kPageSize);
3426 #ifdef ART_TARGET_ANDROID
3427 // Short-circuit the madvise optimization for background processes. This
3428 // avoids IO and memory contention with foreground processes, particularly
3429 // those involving app startup.
3430 // Note: We can only safely short-circuit the madvise on T+, as it requires
3431 // the framework to always immediately notify ART of process states.
3432 static const int kApiLevel = android_get_device_api_level();
3433 const bool accurate_process_state_at_startup = kApiLevel >= __ANDROID_API_T__;
3434 if (accurate_process_state_at_startup) {
3435 const Runtime* runtime = Runtime::Current();
3436 if (runtime != nullptr && !runtime->InJankPerceptibleProcessState()) {
3437 return;
3438 }
3439 }
3440 #endif // ART_TARGET_ANDROID
3441
3442 // Ideal blockTransferSize for madvising files (128KiB)
3443 static constexpr size_t kIdealIoTransferSizeBytes = 128*1024;
3444
3445 size_t target_size_bytes = std::min<size_t>(map_size_bytes, madvise_size_limit_bytes);
3446
3447 if (target_size_bytes > 0) {
3448 ScopedTrace madvising_trace("madvising "
3449 + file_name
3450 + " size="
3451 + std::to_string(target_size_bytes));
3452
3453 // Based on requested size (target_size_bytes)
3454 const uint8_t* target_pos = map_begin + target_size_bytes;
3455
3456 // Clamp endOfFile if its past map_end
3457 if (target_pos > map_end) {
3458 target_pos = map_end;
3459 }
3460
3461 // Madvise the whole file up to target_pos in chunks of
3462 // kIdealIoTransferSizeBytes (to MADV_WILLNEED)
3463 // Note:
3464 // madvise(MADV_WILLNEED) will prefetch max(fd readahead size, optimal
3465 // block size for device) per call, hence the need for chunks. (128KB is a
3466 // good default.)
3467 for (const uint8_t* madvise_start = map_begin;
3468 madvise_start < target_pos;
3469 madvise_start += kIdealIoTransferSizeBytes) {
3470 void* madvise_addr = const_cast<void*>(reinterpret_cast<const void*>(madvise_start));
3471 size_t madvise_length = std::min(kIdealIoTransferSizeBytes,
3472 static_cast<size_t>(target_pos - madvise_start));
3473 int status = madvise(madvise_addr, madvise_length, MADV_WILLNEED);
3474 // In case of error we stop madvising rest of the file
3475 if (status < 0) {
3476 LOG(ERROR) << "Failed to madvise file " << file_name
3477 << " for size:" << map_size_bytes
3478 << ": " << strerror(errno);
3479 break;
3480 }
3481 }
3482 }
3483 }
3484
3485 // Return whether a boot image has a profile. This means we'll need to pre-JIT
3486 // methods in that profile for performance.
HasImageWithProfile() const3487 bool Runtime::HasImageWithProfile() const {
3488 for (gc::space::ImageSpace* space : GetHeap()->GetBootImageSpaces()) {
3489 if (!space->GetProfileFiles().empty()) {
3490 return true;
3491 }
3492 }
3493 return false;
3494 }
3495
AppendToBootClassPath(const std::string & filename,const std::string & location)3496 void Runtime::AppendToBootClassPath(const std::string& filename, const std::string& location) {
3497 DCHECK(!DexFileLoader::IsMultiDexLocation(filename.c_str()));
3498 boot_class_path_.push_back(filename);
3499 if (!boot_class_path_locations_.empty()) {
3500 DCHECK(!DexFileLoader::IsMultiDexLocation(location.c_str()));
3501 boot_class_path_locations_.push_back(location);
3502 }
3503 }
3504
AppendToBootClassPath(const std::string & filename,const std::string & location,const std::vector<std::unique_ptr<const art::DexFile>> & dex_files)3505 void Runtime::AppendToBootClassPath(
3506 const std::string& filename,
3507 const std::string& location,
3508 const std::vector<std::unique_ptr<const art::DexFile>>& dex_files) {
3509 AppendToBootClassPath(filename, location);
3510 ScopedObjectAccess soa(Thread::Current());
3511 for (const std::unique_ptr<const art::DexFile>& dex_file : dex_files) {
3512 // The first element must not be at a multi-dex location, while other elements must be.
3513 DCHECK_NE(DexFileLoader::IsMultiDexLocation(dex_file->GetLocation().c_str()),
3514 dex_file.get() == dex_files.begin()->get());
3515 GetClassLinker()->AppendToBootClassPath(Thread::Current(), dex_file.get());
3516 }
3517 }
3518
AppendToBootClassPath(const std::string & filename,const std::string & location,const std::vector<const art::DexFile * > & dex_files)3519 void Runtime::AppendToBootClassPath(const std::string& filename,
3520 const std::string& location,
3521 const std::vector<const art::DexFile*>& dex_files) {
3522 AppendToBootClassPath(filename, location);
3523 ScopedObjectAccess soa(Thread::Current());
3524 for (const art::DexFile* dex_file : dex_files) {
3525 // The first element must not be at a multi-dex location, while other elements must be.
3526 DCHECK_NE(DexFileLoader::IsMultiDexLocation(dex_file->GetLocation().c_str()),
3527 dex_file == *dex_files.begin());
3528 GetClassLinker()->AppendToBootClassPath(Thread::Current(), dex_file);
3529 }
3530 }
3531
AppendToBootClassPath(const std::string & filename,const std::string & location,const std::vector<std::pair<const art::DexFile *,ObjPtr<mirror::DexCache>>> & dex_files_and_cache)3532 void Runtime::AppendToBootClassPath(
3533 const std::string& filename,
3534 const std::string& location,
3535 const std::vector<std::pair<const art::DexFile*, ObjPtr<mirror::DexCache>>>&
3536 dex_files_and_cache) {
3537 AppendToBootClassPath(filename, location);
3538 ScopedObjectAccess soa(Thread::Current());
3539 for (const auto& [dex_file, dex_cache] : dex_files_and_cache) {
3540 // The first element must not be at a multi-dex location, while other elements must be.
3541 DCHECK_NE(DexFileLoader::IsMultiDexLocation(dex_file->GetLocation().c_str()),
3542 dex_file == dex_files_and_cache.begin()->first);
3543 GetClassLinker()->AppendToBootClassPath(dex_file, dex_cache);
3544 }
3545 }
3546
AddExtraBootDexFiles(const std::string & filename,const std::string & location,std::vector<std::unique_ptr<const art::DexFile>> && dex_files)3547 void Runtime::AddExtraBootDexFiles(const std::string& filename,
3548 const std::string& location,
3549 std::vector<std::unique_ptr<const art::DexFile>>&& dex_files) {
3550 AppendToBootClassPath(filename, location);
3551 ScopedObjectAccess soa(Thread::Current());
3552 if (kIsDebugBuild) {
3553 for (const std::unique_ptr<const art::DexFile>& dex_file : dex_files) {
3554 // The first element must not be at a multi-dex location, while other elements must be.
3555 DCHECK_NE(DexFileLoader::IsMultiDexLocation(dex_file->GetLocation().c_str()),
3556 dex_file.get() == dex_files.begin()->get());
3557 }
3558 }
3559 GetClassLinker()->AddExtraBootDexFiles(Thread::Current(), std::move(dex_files));
3560 }
3561
3562 } // namespace art
3563