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