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 <signal.h> 27 #include <sys/syscall.h> 28 #include "base/memory_tool.h" 29 #if defined(__APPLE__) 30 #include <crt_externs.h> // for _NSGetEnviron 31 #endif 32 33 #include <cstdio> 34 #include <cstdlib> 35 #include <limits> 36 #include <memory_representation.h> 37 #include <vector> 38 #include <fcntl.h> 39 40 #include "JniConstants.h" 41 #include "ScopedLocalRef.h" 42 #include "arch/arm/quick_method_frame_info_arm.h" 43 #include "arch/arm/registers_arm.h" 44 #include "arch/arm64/quick_method_frame_info_arm64.h" 45 #include "arch/arm64/registers_arm64.h" 46 #include "arch/instruction_set_features.h" 47 #include "arch/mips/quick_method_frame_info_mips.h" 48 #include "arch/mips/registers_mips.h" 49 #include "arch/mips64/quick_method_frame_info_mips64.h" 50 #include "arch/mips64/registers_mips64.h" 51 #include "arch/x86/quick_method_frame_info_x86.h" 52 #include "arch/x86/registers_x86.h" 53 #include "arch/x86_64/quick_method_frame_info_x86_64.h" 54 #include "arch/x86_64/registers_x86_64.h" 55 #include "art_field-inl.h" 56 #include "art_method-inl.h" 57 #include "asm_support.h" 58 #include "atomic.h" 59 #include "base/arena_allocator.h" 60 #include "base/dumpable.h" 61 #include "base/stl_util.h" 62 #include "base/systrace.h" 63 #include "base/unix_file/fd_file.h" 64 #include "class_linker-inl.h" 65 #include "compiler_callbacks.h" 66 #include "compiler_filter.h" 67 #include "debugger.h" 68 #include "elf_file.h" 69 #include "entrypoints/runtime_asm_entrypoints.h" 70 #include "experimental_flags.h" 71 #include "fault_handler.h" 72 #include "gc/accounting/card_table-inl.h" 73 #include "gc/heap.h" 74 #include "gc/space/image_space.h" 75 #include "gc/space/space-inl.h" 76 #include "handle_scope-inl.h" 77 #include "image-inl.h" 78 #include "instrumentation.h" 79 #include "intern_table.h" 80 #include "interpreter/interpreter.h" 81 #include "jit/jit.h" 82 #include "jni_internal.h" 83 #include "linear_alloc.h" 84 #include "lambda/box_table.h" 85 #include "mirror/array.h" 86 #include "mirror/class-inl.h" 87 #include "mirror/class_loader.h" 88 #include "mirror/field.h" 89 #include "mirror/method.h" 90 #include "mirror/stack_trace_element.h" 91 #include "mirror/throwable.h" 92 #include "monitor.h" 93 #include "native/dalvik_system_DexFile.h" 94 #include "native/dalvik_system_VMDebug.h" 95 #include "native/dalvik_system_VMRuntime.h" 96 #include "native/dalvik_system_VMStack.h" 97 #include "native/dalvik_system_ZygoteHooks.h" 98 #include "native/java_lang_Class.h" 99 #include "native/java_lang_DexCache.h" 100 #include "native/java_lang_Object.h" 101 #include "native/java_lang_String.h" 102 #include "native/java_lang_StringFactory.h" 103 #include "native/java_lang_System.h" 104 #include "native/java_lang_Thread.h" 105 #include "native/java_lang_Throwable.h" 106 #include "native/java_lang_VMClassLoader.h" 107 #include "native/java_lang_ref_FinalizerReference.h" 108 #include "native/java_lang_ref_Reference.h" 109 #include "native/java_lang_reflect_AbstractMethod.h" 110 #include "native/java_lang_reflect_Array.h" 111 #include "native/java_lang_reflect_Constructor.h" 112 #include "native/java_lang_reflect_Field.h" 113 #include "native/java_lang_reflect_Method.h" 114 #include "native/java_lang_reflect_Proxy.h" 115 #include "native/java_util_concurrent_atomic_AtomicLong.h" 116 #include "native/libcore_util_CharsetUtils.h" 117 #include "native/org_apache_harmony_dalvik_ddmc_DdmServer.h" 118 #include "native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.h" 119 #include "native/sun_misc_Unsafe.h" 120 #include "native_bridge_art_interface.h" 121 #include "oat_file.h" 122 #include "oat_file_manager.h" 123 #include "os.h" 124 #include "parsed_options.h" 125 #include "profiler.h" 126 #include "jit/profile_saver.h" 127 #include "quick/quick_method_frame_info.h" 128 #include "reflection.h" 129 #include "runtime_options.h" 130 #include "ScopedLocalRef.h" 131 #include "scoped_thread_state_change.h" 132 #include "sigchain.h" 133 #include "signal_catcher.h" 134 #include "signal_set.h" 135 #include "thread.h" 136 #include "thread_list.h" 137 #include "trace.h" 138 #include "transaction.h" 139 #include "utils.h" 140 #include "verifier/method_verifier.h" 141 #include "well_known_classes.h" 142 143 namespace art { 144 145 // If a signal isn't handled properly, enable a handler that attempts to dump the Java stack. 146 static constexpr bool kEnableJavaStackTraceHandler = false; 147 // Tuned by compiling GmsCore under perf and measuring time spent in DescriptorEquals for class 148 // linking. 149 static constexpr double kLowMemoryMinLoadFactor = 0.5; 150 static constexpr double kLowMemoryMaxLoadFactor = 0.8; 151 static constexpr double kNormalMinLoadFactor = 0.4; 152 static constexpr double kNormalMaxLoadFactor = 0.7; 153 Runtime* Runtime::instance_ = nullptr; 154 155 struct TraceConfig { 156 Trace::TraceMode trace_mode; 157 Trace::TraceOutputMode trace_output_mode; 158 std::string trace_file; 159 size_t trace_file_size; 160 }; 161 162 namespace { 163 #ifdef __APPLE__ 164 inline char** GetEnviron() { 165 // When Google Test is built as a framework on MacOS X, the environ variable 166 // is unavailable. Apple's documentation (man environ) recommends using 167 // _NSGetEnviron() instead. 168 return *_NSGetEnviron(); 169 } 170 #else 171 // Some POSIX platforms expect you to declare environ. extern "C" makes 172 // it reside in the global namespace. 173 extern "C" char** environ; 174 inline char** GetEnviron() { return environ; } 175 #endif 176 } // namespace 177 178 Runtime::Runtime() 179 : resolution_method_(nullptr), 180 imt_conflict_method_(nullptr), 181 imt_unimplemented_method_(nullptr), 182 instruction_set_(kNone), 183 compiler_callbacks_(nullptr), 184 is_zygote_(false), 185 must_relocate_(false), 186 is_concurrent_gc_enabled_(true), 187 is_explicit_gc_disabled_(false), 188 dex2oat_enabled_(true), 189 image_dex2oat_enabled_(true), 190 default_stack_size_(0), 191 heap_(nullptr), 192 max_spins_before_thin_lock_inflation_(Monitor::kDefaultMaxSpinsBeforeThinLockInflation), 193 monitor_list_(nullptr), 194 monitor_pool_(nullptr), 195 thread_list_(nullptr), 196 intern_table_(nullptr), 197 class_linker_(nullptr), 198 signal_catcher_(nullptr), 199 java_vm_(nullptr), 200 fault_message_lock_("Fault message lock"), 201 fault_message_(""), 202 threads_being_born_(0), 203 shutdown_cond_(new ConditionVariable("Runtime shutdown", *Locks::runtime_shutdown_lock_)), 204 shutting_down_(false), 205 shutting_down_started_(false), 206 started_(false), 207 finished_starting_(false), 208 vfprintf_(nullptr), 209 exit_(nullptr), 210 abort_(nullptr), 211 stats_enabled_(false), 212 is_running_on_memory_tool_(RUNNING_ON_MEMORY_TOOL), 213 instrumentation_(), 214 main_thread_group_(nullptr), 215 system_thread_group_(nullptr), 216 system_class_loader_(nullptr), 217 dump_gc_performance_on_shutdown_(false), 218 preinitialization_transaction_(nullptr), 219 verify_(verifier::VerifyMode::kNone), 220 allow_dex_file_fallback_(true), 221 target_sdk_version_(0), 222 implicit_null_checks_(false), 223 implicit_so_checks_(false), 224 implicit_suspend_checks_(false), 225 no_sig_chain_(false), 226 force_native_bridge_(false), 227 is_native_bridge_loaded_(false), 228 is_native_debuggable_(false), 229 zygote_max_failed_boots_(0), 230 experimental_flags_(ExperimentalFlags::kNone), 231 oat_file_manager_(nullptr), 232 is_low_memory_mode_(false), 233 safe_mode_(false), 234 dump_native_stack_on_sig_quit_(true), 235 pruned_dalvik_cache_(false), 236 // Initially assume we perceive jank in case the process state is never updated. 237 process_state_(kProcessStateJankPerceptible), 238 zygote_no_threads_(false) { 239 CheckAsmSupportOffsetsAndSizes(); 240 std::fill(callee_save_methods_, callee_save_methods_ + arraysize(callee_save_methods_), 0u); 241 interpreter::CheckInterpreterAsmConstants(); 242 } 243 244 Runtime::~Runtime() { 245 ScopedTrace trace("Runtime shutdown"); 246 if (is_native_bridge_loaded_) { 247 UnloadNativeBridge(); 248 } 249 250 if (dump_gc_performance_on_shutdown_) { 251 // This can't be called from the Heap destructor below because it 252 // could call RosAlloc::InspectAll() which needs the thread_list 253 // to be still alive. 254 heap_->DumpGcPerformanceInfo(LOG(INFO)); 255 } 256 257 Thread* self = Thread::Current(); 258 const bool attach_shutdown_thread = self == nullptr; 259 if (attach_shutdown_thread) { 260 CHECK(AttachCurrentThread("Shutdown thread", false, nullptr, false)); 261 self = Thread::Current(); 262 } else { 263 LOG(WARNING) << "Current thread not detached in Runtime shutdown"; 264 } 265 266 { 267 ScopedTrace trace2("Wait for shutdown cond"); 268 MutexLock mu(self, *Locks::runtime_shutdown_lock_); 269 shutting_down_started_ = true; 270 while (threads_being_born_ > 0) { 271 shutdown_cond_->Wait(self); 272 } 273 shutting_down_ = true; 274 } 275 // Shutdown and wait for the daemons. 276 CHECK(self != nullptr); 277 if (IsFinishedStarting()) { 278 ScopedTrace trace2("Waiting for Daemons"); 279 self->ClearException(); 280 self->GetJniEnv()->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, 281 WellKnownClasses::java_lang_Daemons_stop); 282 } 283 284 Trace::Shutdown(); 285 286 if (attach_shutdown_thread) { 287 DetachCurrentThread(); 288 self = nullptr; 289 } 290 291 // Make sure to let the GC complete if it is running. 292 heap_->WaitForGcToComplete(gc::kGcCauseBackground, self); 293 heap_->DeleteThreadPool(); 294 if (jit_ != nullptr) { 295 ScopedTrace trace2("Delete jit"); 296 VLOG(jit) << "Deleting jit thread pool"; 297 // Delete thread pool before the thread list since we don't want to wait forever on the 298 // JIT compiler threads. 299 jit_->DeleteThreadPool(); 300 // Similarly, stop the profile saver thread before deleting the thread list. 301 jit_->StopProfileSaver(); 302 } 303 304 // Make sure our internal threads are dead before we start tearing down things they're using. 305 Dbg::StopJdwp(); 306 delete signal_catcher_; 307 308 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended. 309 { 310 ScopedTrace trace2("Delete thread list"); 311 delete thread_list_; 312 } 313 // Delete the JIT after thread list to ensure that there is no remaining threads which could be 314 // accessing the instrumentation when we delete it. 315 if (jit_ != nullptr) { 316 VLOG(jit) << "Deleting jit"; 317 jit_.reset(nullptr); 318 } 319 320 // Shutdown the fault manager if it was initialized. 321 fault_manager.Shutdown(); 322 323 ScopedTrace trace2("Delete state"); 324 delete monitor_list_; 325 delete monitor_pool_; 326 delete class_linker_; 327 delete heap_; 328 delete intern_table_; 329 delete java_vm_; 330 delete oat_file_manager_; 331 Thread::Shutdown(); 332 QuasiAtomic::Shutdown(); 333 verifier::MethodVerifier::Shutdown(); 334 335 // Destroy allocators before shutting down the MemMap because they may use it. 336 linear_alloc_.reset(); 337 low_4gb_arena_pool_.reset(); 338 arena_pool_.reset(); 339 jit_arena_pool_.reset(); 340 MemMap::Shutdown(); 341 342 // TODO: acquire a static mutex on Runtime to avoid racing. 343 CHECK(instance_ == nullptr || instance_ == this); 344 instance_ = nullptr; 345 } 346 347 struct AbortState { 348 void Dump(std::ostream& os) const { 349 if (gAborting > 1) { 350 os << "Runtime aborting --- recursively, so no thread-specific detail!\n"; 351 return; 352 } 353 gAborting++; 354 os << "Runtime aborting...\n"; 355 if (Runtime::Current() == nullptr) { 356 os << "(Runtime does not yet exist!)\n"; 357 DumpNativeStack(os, GetTid(), nullptr, " native: ", nullptr); 358 return; 359 } 360 Thread* self = Thread::Current(); 361 if (self == nullptr) { 362 os << "(Aborting thread was not attached to runtime!)\n"; 363 DumpKernelStack(os, GetTid(), " kernel: ", false); 364 DumpNativeStack(os, GetTid(), nullptr, " native: ", nullptr); 365 } else { 366 os << "Aborting thread:\n"; 367 if (Locks::mutator_lock_->IsExclusiveHeld(self) || Locks::mutator_lock_->IsSharedHeld(self)) { 368 DumpThread(os, self); 369 } else { 370 if (Locks::mutator_lock_->SharedTryLock(self)) { 371 DumpThread(os, self); 372 Locks::mutator_lock_->SharedUnlock(self); 373 } 374 } 375 } 376 DumpAllThreads(os, self); 377 } 378 379 // No thread-safety analysis as we do explicitly test for holding the mutator lock. 380 void DumpThread(std::ostream& os, Thread* self) const NO_THREAD_SAFETY_ANALYSIS { 381 DCHECK(Locks::mutator_lock_->IsExclusiveHeld(self) || Locks::mutator_lock_->IsSharedHeld(self)); 382 self->Dump(os); 383 if (self->IsExceptionPending()) { 384 mirror::Throwable* exception = self->GetException(); 385 os << "Pending exception " << exception->Dump(); 386 } 387 } 388 389 void DumpAllThreads(std::ostream& os, Thread* self) const { 390 Runtime* runtime = Runtime::Current(); 391 if (runtime != nullptr) { 392 ThreadList* thread_list = runtime->GetThreadList(); 393 if (thread_list != nullptr) { 394 bool tll_already_held = Locks::thread_list_lock_->IsExclusiveHeld(self); 395 bool ml_already_held = Locks::mutator_lock_->IsSharedHeld(self); 396 if (!tll_already_held || !ml_already_held) { 397 os << "Dumping all threads without appropriate locks held:" 398 << (!tll_already_held ? " thread list lock" : "") 399 << (!ml_already_held ? " mutator lock" : "") 400 << "\n"; 401 } 402 os << "All threads:\n"; 403 thread_list->Dump(os); 404 } 405 } 406 } 407 }; 408 409 void Runtime::Abort(const char* msg) { 410 gAborting++; // set before taking any locks 411 412 // Ensure that we don't have multiple threads trying to abort at once, 413 // which would result in significantly worse diagnostics. 414 MutexLock mu(Thread::Current(), *Locks::abort_lock_); 415 416 // Get any pending output out of the way. 417 fflush(nullptr); 418 419 // Many people have difficulty distinguish aborts from crashes, 420 // so be explicit. 421 AbortState state; 422 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state); 423 424 // Sometimes we dump long messages, and the Android abort message only retains the first line. 425 // In those cases, just log the message again, to avoid logcat limits. 426 if (msg != nullptr && strchr(msg, '\n') != nullptr) { 427 LOG(INTERNAL_FATAL) << msg; 428 } 429 430 // Call the abort hook if we have one. 431 if (Runtime::Current() != nullptr && Runtime::Current()->abort_ != nullptr) { 432 LOG(INTERNAL_FATAL) << "Calling abort hook..."; 433 Runtime::Current()->abort_(); 434 // notreached 435 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!"; 436 } 437 438 #if defined(__GLIBC__) 439 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3), 440 // which POSIX defines in terms of raise(3), which POSIX defines in terms 441 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through 442 // libpthread, which means the stacks we dump would be useless. Calling 443 // tgkill(2) directly avoids that. 444 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT); 445 // TODO: LLVM installs it's own SIGABRT handler so exit to be safe... Can we disable that in LLVM? 446 // If not, we could use sigaction(3) before calling tgkill(2) and lose this call to exit(3). 447 exit(1); 448 #else 449 abort(); 450 #endif 451 // notreached 452 } 453 454 void Runtime::PreZygoteFork() { 455 heap_->PreZygoteFork(); 456 } 457 458 void Runtime::CallExitHook(jint status) { 459 if (exit_ != nullptr) { 460 ScopedThreadStateChange tsc(Thread::Current(), kNative); 461 exit_(status); 462 LOG(WARNING) << "Exit hook returned instead of exiting!"; 463 } 464 } 465 466 void Runtime::SweepSystemWeaks(IsMarkedVisitor* visitor) { 467 GetInternTable()->SweepInternTableWeaks(visitor); 468 GetMonitorList()->SweepMonitorList(visitor); 469 GetJavaVM()->SweepJniWeakGlobals(visitor); 470 GetHeap()->SweepAllocationRecords(visitor); 471 GetLambdaBoxTable()->SweepWeakBoxedLambdas(visitor); 472 } 473 474 bool Runtime::ParseOptions(const RuntimeOptions& raw_options, 475 bool ignore_unrecognized, 476 RuntimeArgumentMap* runtime_options) { 477 InitLogging(/* argv */ nullptr); // Calls Locks::Init() as a side effect. 478 bool parsed = ParsedOptions::Parse(raw_options, ignore_unrecognized, runtime_options); 479 if (!parsed) { 480 LOG(ERROR) << "Failed to parse options"; 481 return false; 482 } 483 return true; 484 } 485 486 bool Runtime::Create(RuntimeArgumentMap&& runtime_options) { 487 // TODO: acquire a static mutex on Runtime to avoid racing. 488 if (Runtime::instance_ != nullptr) { 489 return false; 490 } 491 instance_ = new Runtime; 492 if (!instance_->Init(std::move(runtime_options))) { 493 // TODO: Currently deleting the instance will abort the runtime on destruction. Now This will 494 // leak memory, instead. Fix the destructor. b/19100793. 495 // delete instance_; 496 instance_ = nullptr; 497 return false; 498 } 499 return true; 500 } 501 502 bool Runtime::Create(const RuntimeOptions& raw_options, bool ignore_unrecognized) { 503 RuntimeArgumentMap runtime_options; 504 return ParseOptions(raw_options, ignore_unrecognized, &runtime_options) && 505 Create(std::move(runtime_options)); 506 } 507 508 static jobject CreateSystemClassLoader(Runtime* runtime) { 509 if (runtime->IsAotCompiler() && !runtime->GetCompilerCallbacks()->IsBootImage()) { 510 return nullptr; 511 } 512 513 ScopedObjectAccess soa(Thread::Current()); 514 ClassLinker* cl = Runtime::Current()->GetClassLinker(); 515 auto pointer_size = cl->GetImagePointerSize(); 516 517 StackHandleScope<2> hs(soa.Self()); 518 Handle<mirror::Class> class_loader_class( 519 hs.NewHandle(soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader))); 520 CHECK(cl->EnsureInitialized(soa.Self(), class_loader_class, true, true)); 521 522 ArtMethod* getSystemClassLoader = class_loader_class->FindDirectMethod( 523 "getSystemClassLoader", "()Ljava/lang/ClassLoader;", pointer_size); 524 CHECK(getSystemClassLoader != nullptr); 525 526 JValue result = InvokeWithJValues(soa, nullptr, soa.EncodeMethod(getSystemClassLoader), nullptr); 527 JNIEnv* env = soa.Self()->GetJniEnv(); 528 ScopedLocalRef<jobject> system_class_loader(env, soa.AddLocalReference<jobject>(result.GetL())); 529 CHECK(system_class_loader.get() != nullptr); 530 531 soa.Self()->SetClassLoaderOverride(system_class_loader.get()); 532 533 Handle<mirror::Class> thread_class( 534 hs.NewHandle(soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread))); 535 CHECK(cl->EnsureInitialized(soa.Self(), thread_class, true, true)); 536 537 ArtField* contextClassLoader = 538 thread_class->FindDeclaredInstanceField("contextClassLoader", "Ljava/lang/ClassLoader;"); 539 CHECK(contextClassLoader != nullptr); 540 541 // We can't run in a transaction yet. 542 contextClassLoader->SetObject<false>(soa.Self()->GetPeer(), 543 soa.Decode<mirror::ClassLoader*>(system_class_loader.get())); 544 545 return env->NewGlobalRef(system_class_loader.get()); 546 } 547 548 std::string Runtime::GetPatchoatExecutable() const { 549 if (!patchoat_executable_.empty()) { 550 return patchoat_executable_; 551 } 552 std::string patchoat_executable(GetAndroidRoot()); 553 patchoat_executable += (kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat"); 554 return patchoat_executable; 555 } 556 557 std::string Runtime::GetCompilerExecutable() const { 558 if (!compiler_executable_.empty()) { 559 return compiler_executable_; 560 } 561 std::string compiler_executable(GetAndroidRoot()); 562 compiler_executable += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat"); 563 return compiler_executable; 564 } 565 566 bool Runtime::Start() { 567 VLOG(startup) << "Runtime::Start entering"; 568 569 CHECK(!no_sig_chain_) << "A started runtime should have sig chain enabled"; 570 571 // If a debug host build, disable ptrace restriction for debugging and test timeout thread dump. 572 // Only 64-bit as prctl() may fail in 32 bit userspace on a 64-bit kernel. 573 #if defined(__linux__) && !defined(__ANDROID__) && defined(__x86_64__) 574 if (kIsDebugBuild) { 575 CHECK_EQ(prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY), 0); 576 } 577 #endif 578 579 // Restore main thread state to kNative as expected by native code. 580 Thread* self = Thread::Current(); 581 582 self->TransitionFromRunnableToSuspended(kNative); 583 584 started_ = true; 585 586 // Create the JIT either if we have to use JIT compilation or save profiling info. 587 // TODO(calin): We use the JIT class as a proxy for JIT compilation and for 588 // recoding profiles. Maybe we should consider changing the name to be more clear it's 589 // not only about compiling. b/28295073. 590 if (jit_options_->UseJitCompilation() || jit_options_->GetSaveProfilingInfo()) { 591 std::string error_msg; 592 if (!IsZygote()) { 593 // If we are the zygote then we need to wait until after forking to create the code cache 594 // due to SELinux restrictions on r/w/x memory regions. 595 CreateJit(); 596 } else if (jit_options_->UseJitCompilation()) { 597 if (!jit::Jit::LoadCompilerLibrary(&error_msg)) { 598 // Try to load compiler pre zygote to reduce PSS. b/27744947 599 LOG(WARNING) << "Failed to load JIT compiler with error " << error_msg; 600 } 601 } 602 } 603 604 if (!IsImageDex2OatEnabled() || !GetHeap()->HasBootImageSpace()) { 605 ScopedObjectAccess soa(self); 606 StackHandleScope<2> hs(soa.Self()); 607 608 auto class_class(hs.NewHandle<mirror::Class>(mirror::Class::GetJavaLangClass())); 609 auto field_class(hs.NewHandle<mirror::Class>(mirror::Field::StaticClass())); 610 611 class_linker_->EnsureInitialized(soa.Self(), class_class, true, true); 612 // Field class is needed for register_java_net_InetAddress in libcore, b/28153851. 613 class_linker_->EnsureInitialized(soa.Self(), field_class, true, true); 614 } 615 616 // InitNativeMethods needs to be after started_ so that the classes 617 // it touches will have methods linked to the oat file if necessary. 618 { 619 ScopedTrace trace2("InitNativeMethods"); 620 InitNativeMethods(); 621 } 622 623 // Initialize well known thread group values that may be accessed threads while attaching. 624 InitThreadGroups(self); 625 626 Thread::FinishStartup(); 627 628 system_class_loader_ = CreateSystemClassLoader(this); 629 630 if (is_zygote_) { 631 if (!InitZygote()) { 632 return false; 633 } 634 } else { 635 if (is_native_bridge_loaded_) { 636 PreInitializeNativeBridge("."); 637 } 638 NativeBridgeAction action = force_native_bridge_ 639 ? NativeBridgeAction::kInitialize 640 : NativeBridgeAction::kUnload; 641 InitNonZygoteOrPostFork(self->GetJniEnv(), 642 /* is_system_server */ false, 643 action, 644 GetInstructionSetString(kRuntimeISA)); 645 } 646 647 StartDaemonThreads(); 648 649 { 650 ScopedObjectAccess soa(self); 651 self->GetJniEnv()->locals.AssertEmpty(); 652 } 653 654 VLOG(startup) << "Runtime::Start exiting"; 655 finished_starting_ = true; 656 657 if (profiler_options_.IsEnabled() && !profile_output_filename_.empty()) { 658 // User has asked for a profile using -Xenable-profiler. 659 // Create the profile file if it doesn't exist. 660 int fd = open(profile_output_filename_.c_str(), O_RDWR|O_CREAT|O_EXCL, 0660); 661 if (fd >= 0) { 662 close(fd); 663 } else if (errno != EEXIST) { 664 LOG(WARNING) << "Failed to access the profile file. Profiler disabled."; 665 } 666 } 667 668 if (trace_config_.get() != nullptr && trace_config_->trace_file != "") { 669 ScopedThreadStateChange tsc(self, kWaitingForMethodTracingStart); 670 Trace::Start(trace_config_->trace_file.c_str(), 671 -1, 672 static_cast<int>(trace_config_->trace_file_size), 673 0, 674 trace_config_->trace_output_mode, 675 trace_config_->trace_mode, 676 0); 677 } 678 679 return true; 680 } 681 682 void Runtime::EndThreadBirth() REQUIRES(Locks::runtime_shutdown_lock_) { 683 DCHECK_GT(threads_being_born_, 0U); 684 threads_being_born_--; 685 if (shutting_down_started_ && threads_being_born_ == 0) { 686 shutdown_cond_->Broadcast(Thread::Current()); 687 } 688 } 689 690 // Do zygote-mode-only initialization. 691 bool Runtime::InitZygote() { 692 #ifdef __linux__ 693 // zygote goes into its own process group 694 setpgid(0, 0); 695 696 // See storage config details at http://source.android.com/tech/storage/ 697 // Create private mount namespace shared by all children 698 if (unshare(CLONE_NEWNS) == -1) { 699 PLOG(ERROR) << "Failed to unshare()"; 700 return false; 701 } 702 703 // Mark rootfs as being a slave so that changes from default 704 // namespace only flow into our children. 705 if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) { 706 PLOG(ERROR) << "Failed to mount() rootfs as MS_SLAVE"; 707 return false; 708 } 709 710 // Create a staging tmpfs that is shared by our children; they will 711 // bind mount storage into their respective private namespaces, which 712 // are isolated from each other. 713 const char* target_base = getenv("EMULATED_STORAGE_TARGET"); 714 if (target_base != nullptr) { 715 if (mount("tmpfs", target_base, "tmpfs", MS_NOSUID | MS_NODEV, 716 "uid=0,gid=1028,mode=0751") == -1) { 717 PLOG(ERROR) << "Failed to mount tmpfs to " << target_base; 718 return false; 719 } 720 } 721 722 return true; 723 #else 724 UNIMPLEMENTED(FATAL); 725 return false; 726 #endif 727 } 728 729 void Runtime::InitNonZygoteOrPostFork( 730 JNIEnv* env, bool is_system_server, NativeBridgeAction action, const char* isa) { 731 is_zygote_ = false; 732 733 if (is_native_bridge_loaded_) { 734 switch (action) { 735 case NativeBridgeAction::kUnload: 736 UnloadNativeBridge(); 737 is_native_bridge_loaded_ = false; 738 break; 739 740 case NativeBridgeAction::kInitialize: 741 InitializeNativeBridge(env, isa); 742 break; 743 } 744 } 745 746 // Create the thread pools. 747 heap_->CreateThreadPool(); 748 // Reset the gc performance data at zygote fork so that the GCs 749 // before fork aren't attributed to an app. 750 heap_->ResetGcPerformanceInfo(); 751 752 753 if (!is_system_server && 754 !safe_mode_ && 755 (jit_options_->UseJitCompilation() || jit_options_->GetSaveProfilingInfo()) && 756 jit_.get() == nullptr) { 757 // Note that when running ART standalone (not zygote, nor zygote fork), 758 // the jit may have already been created. 759 CreateJit(); 760 } 761 762 StartSignalCatcher(); 763 764 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y", 765 // this will pause the runtime, so we probably want this to come last. 766 Dbg::StartJdwp(); 767 } 768 769 void Runtime::StartSignalCatcher() { 770 if (!is_zygote_) { 771 signal_catcher_ = new SignalCatcher(stack_trace_file_); 772 } 773 } 774 775 bool Runtime::IsShuttingDown(Thread* self) { 776 MutexLock mu(self, *Locks::runtime_shutdown_lock_); 777 return IsShuttingDownLocked(); 778 } 779 780 bool Runtime::IsDebuggable() const { 781 const OatFile* oat_file = GetOatFileManager().GetPrimaryOatFile(); 782 return oat_file != nullptr && oat_file->IsDebuggable(); 783 } 784 785 void Runtime::StartDaemonThreads() { 786 ScopedTrace trace(__FUNCTION__); 787 VLOG(startup) << "Runtime::StartDaemonThreads entering"; 788 789 Thread* self = Thread::Current(); 790 791 // Must be in the kNative state for calling native methods. 792 CHECK_EQ(self->GetState(), kNative); 793 794 JNIEnv* env = self->GetJniEnv(); 795 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, 796 WellKnownClasses::java_lang_Daemons_start); 797 if (env->ExceptionCheck()) { 798 env->ExceptionDescribe(); 799 LOG(FATAL) << "Error starting java.lang.Daemons"; 800 } 801 802 VLOG(startup) << "Runtime::StartDaemonThreads exiting"; 803 } 804 805 // Attempts to open dex files from image(s). Given the image location, try to find the oat file 806 // and open it to get the stored dex file. If the image is the first for a multi-image boot 807 // classpath, go on and also open the other images. 808 static bool OpenDexFilesFromImage(const std::string& image_location, 809 std::vector<std::unique_ptr<const DexFile>>* dex_files, 810 size_t* failures) { 811 DCHECK(dex_files != nullptr) << "OpenDexFilesFromImage: out-param is nullptr"; 812 813 // Use a work-list approach, so that we can easily reuse the opening code. 814 std::vector<std::string> image_locations; 815 image_locations.push_back(image_location); 816 817 for (size_t index = 0; index < image_locations.size(); ++index) { 818 std::string system_filename; 819 bool has_system = false; 820 std::string cache_filename_unused; 821 bool dalvik_cache_exists_unused; 822 bool has_cache_unused; 823 bool is_global_cache_unused; 824 bool found_image = gc::space::ImageSpace::FindImageFilename(image_locations[index].c_str(), 825 kRuntimeISA, 826 &system_filename, 827 &has_system, 828 &cache_filename_unused, 829 &dalvik_cache_exists_unused, 830 &has_cache_unused, 831 &is_global_cache_unused); 832 833 if (!found_image || !has_system) { 834 return false; 835 } 836 837 // We are falling back to non-executable use of the oat file because patching failed, presumably 838 // due to lack of space. 839 std::string oat_filename = 840 ImageHeader::GetOatLocationFromImageLocation(system_filename.c_str()); 841 std::string oat_location = 842 ImageHeader::GetOatLocationFromImageLocation(image_locations[index].c_str()); 843 // Note: in the multi-image case, the image location may end in ".jar," and not ".art." Handle 844 // that here. 845 if (EndsWith(oat_location, ".jar")) { 846 oat_location.replace(oat_location.length() - 3, 3, "oat"); 847 } 848 849 std::unique_ptr<File> file(OS::OpenFileForReading(oat_filename.c_str())); 850 if (file.get() == nullptr) { 851 return false; 852 } 853 std::string error_msg; 854 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file.get(), 855 false, 856 false, 857 /*low_4gb*/false, 858 &error_msg)); 859 if (elf_file.get() == nullptr) { 860 return false; 861 } 862 std::unique_ptr<const OatFile> oat_file( 863 OatFile::OpenWithElfFile(elf_file.release(), oat_location, nullptr, &error_msg)); 864 if (oat_file == nullptr) { 865 LOG(WARNING) << "Unable to use '" << oat_filename << "' because " << error_msg; 866 return false; 867 } 868 869 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { 870 if (oat_dex_file == nullptr) { 871 *failures += 1; 872 continue; 873 } 874 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); 875 if (dex_file.get() == nullptr) { 876 *failures += 1; 877 } else { 878 dex_files->push_back(std::move(dex_file)); 879 } 880 } 881 882 if (index == 0) { 883 // First file. See if this is a multi-image environment, and if so, enqueue the other images. 884 const OatHeader& boot_oat_header = oat_file->GetOatHeader(); 885 const char* boot_cp = boot_oat_header.GetStoreValueByKey(OatHeader::kBootClassPathKey); 886 if (boot_cp != nullptr) { 887 gc::space::ImageSpace::ExtractMultiImageLocations(image_locations[0], 888 boot_cp, 889 &image_locations); 890 } 891 } 892 893 Runtime::Current()->GetOatFileManager().RegisterOatFile(std::move(oat_file)); 894 } 895 return true; 896 } 897 898 899 static size_t OpenDexFiles(const std::vector<std::string>& dex_filenames, 900 const std::vector<std::string>& dex_locations, 901 const std::string& image_location, 902 std::vector<std::unique_ptr<const DexFile>>* dex_files) { 903 DCHECK(dex_files != nullptr) << "OpenDexFiles: out-param is nullptr"; 904 size_t failure_count = 0; 905 if (!image_location.empty() && OpenDexFilesFromImage(image_location, dex_files, &failure_count)) { 906 return failure_count; 907 } 908 failure_count = 0; 909 for (size_t i = 0; i < dex_filenames.size(); i++) { 910 const char* dex_filename = dex_filenames[i].c_str(); 911 const char* dex_location = dex_locations[i].c_str(); 912 std::string error_msg; 913 if (!OS::FileExists(dex_filename)) { 914 LOG(WARNING) << "Skipping non-existent dex file '" << dex_filename << "'"; 915 continue; 916 } 917 if (!DexFile::Open(dex_filename, dex_location, &error_msg, dex_files)) { 918 LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "': " << error_msg; 919 ++failure_count; 920 } 921 } 922 return failure_count; 923 } 924 925 void Runtime::SetSentinel(mirror::Object* sentinel) { 926 CHECK(sentinel_.Read() == nullptr); 927 CHECK(sentinel != nullptr); 928 CHECK(!heap_->IsMovableObject(sentinel)); 929 sentinel_ = GcRoot<mirror::Object>(sentinel); 930 } 931 932 bool Runtime::Init(RuntimeArgumentMap&& runtime_options_in) { 933 // (b/30160149): protect subprocesses from modifications to LD_LIBRARY_PATH, etc. 934 // Take a snapshot of the environment at the time the runtime was created, for use by Exec, etc. 935 env_snapshot_.TakeSnapshot(); 936 937 RuntimeArgumentMap runtime_options(std::move(runtime_options_in)); 938 ScopedTrace trace(__FUNCTION__); 939 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize); 940 941 MemMap::Init(); 942 943 using Opt = RuntimeArgumentMap; 944 VLOG(startup) << "Runtime::Init -verbose:startup enabled"; 945 946 QuasiAtomic::Startup(); 947 948 oat_file_manager_ = new OatFileManager; 949 950 Thread::SetSensitiveThreadHook(runtime_options.GetOrDefault(Opt::HookIsSensitiveThread)); 951 Monitor::Init(runtime_options.GetOrDefault(Opt::LockProfThreshold)); 952 953 boot_class_path_string_ = runtime_options.ReleaseOrDefault(Opt::BootClassPath); 954 class_path_string_ = runtime_options.ReleaseOrDefault(Opt::ClassPath); 955 properties_ = runtime_options.ReleaseOrDefault(Opt::PropertiesList); 956 957 compiler_callbacks_ = runtime_options.GetOrDefault(Opt::CompilerCallbacksPtr); 958 patchoat_executable_ = runtime_options.ReleaseOrDefault(Opt::PatchOat); 959 must_relocate_ = runtime_options.GetOrDefault(Opt::Relocate); 960 is_zygote_ = runtime_options.Exists(Opt::Zygote); 961 is_explicit_gc_disabled_ = runtime_options.Exists(Opt::DisableExplicitGC); 962 dex2oat_enabled_ = runtime_options.GetOrDefault(Opt::Dex2Oat); 963 image_dex2oat_enabled_ = runtime_options.GetOrDefault(Opt::ImageDex2Oat); 964 dump_native_stack_on_sig_quit_ = runtime_options.GetOrDefault(Opt::DumpNativeStackOnSigQuit); 965 966 vfprintf_ = runtime_options.GetOrDefault(Opt::HookVfprintf); 967 exit_ = runtime_options.GetOrDefault(Opt::HookExit); 968 abort_ = runtime_options.GetOrDefault(Opt::HookAbort); 969 970 default_stack_size_ = runtime_options.GetOrDefault(Opt::StackSize); 971 stack_trace_file_ = runtime_options.ReleaseOrDefault(Opt::StackTraceFile); 972 973 compiler_executable_ = runtime_options.ReleaseOrDefault(Opt::Compiler); 974 compiler_options_ = runtime_options.ReleaseOrDefault(Opt::CompilerOptions); 975 image_compiler_options_ = runtime_options.ReleaseOrDefault(Opt::ImageCompilerOptions); 976 image_location_ = runtime_options.GetOrDefault(Opt::Image); 977 978 max_spins_before_thin_lock_inflation_ = 979 runtime_options.GetOrDefault(Opt::MaxSpinsBeforeThinLockInflation); 980 981 monitor_list_ = new MonitorList; 982 monitor_pool_ = MonitorPool::Create(); 983 thread_list_ = new ThreadList; 984 intern_table_ = new InternTable; 985 986 verify_ = runtime_options.GetOrDefault(Opt::Verify); 987 allow_dex_file_fallback_ = !runtime_options.Exists(Opt::NoDexFileFallback); 988 989 no_sig_chain_ = runtime_options.Exists(Opt::NoSigChain); 990 force_native_bridge_ = runtime_options.Exists(Opt::ForceNativeBridge); 991 992 Split(runtime_options.GetOrDefault(Opt::CpuAbiList), ',', &cpu_abilist_); 993 994 fingerprint_ = runtime_options.ReleaseOrDefault(Opt::Fingerprint); 995 996 if (runtime_options.GetOrDefault(Opt::Interpret)) { 997 GetInstrumentation()->ForceInterpretOnly(); 998 } 999 1000 zygote_max_failed_boots_ = runtime_options.GetOrDefault(Opt::ZygoteMaxFailedBoots); 1001 experimental_flags_ = runtime_options.GetOrDefault(Opt::Experimental); 1002 is_low_memory_mode_ = runtime_options.Exists(Opt::LowMemoryMode); 1003 1004 { 1005 CompilerFilter::Filter filter; 1006 std::string filter_str = runtime_options.GetOrDefault(Opt::OatFileManagerCompilerFilter); 1007 if (!CompilerFilter::ParseCompilerFilter(filter_str.c_str(), &filter)) { 1008 LOG(ERROR) << "Cannot parse compiler filter " << filter_str; 1009 return false; 1010 } 1011 OatFileManager::SetCompilerFilter(filter); 1012 } 1013 1014 XGcOption xgc_option = runtime_options.GetOrDefault(Opt::GcOption); 1015 heap_ = new gc::Heap(runtime_options.GetOrDefault(Opt::MemoryInitialSize), 1016 runtime_options.GetOrDefault(Opt::HeapGrowthLimit), 1017 runtime_options.GetOrDefault(Opt::HeapMinFree), 1018 runtime_options.GetOrDefault(Opt::HeapMaxFree), 1019 runtime_options.GetOrDefault(Opt::HeapTargetUtilization), 1020 runtime_options.GetOrDefault(Opt::ForegroundHeapGrowthMultiplier), 1021 runtime_options.GetOrDefault(Opt::MemoryMaximumSize), 1022 runtime_options.GetOrDefault(Opt::NonMovingSpaceCapacity), 1023 runtime_options.GetOrDefault(Opt::Image), 1024 runtime_options.GetOrDefault(Opt::ImageInstructionSet), 1025 xgc_option.collector_type_, 1026 runtime_options.GetOrDefault(Opt::BackgroundGc), 1027 runtime_options.GetOrDefault(Opt::LargeObjectSpace), 1028 runtime_options.GetOrDefault(Opt::LargeObjectThreshold), 1029 runtime_options.GetOrDefault(Opt::ParallelGCThreads), 1030 runtime_options.GetOrDefault(Opt::ConcGCThreads), 1031 runtime_options.Exists(Opt::LowMemoryMode), 1032 runtime_options.GetOrDefault(Opt::LongPauseLogThreshold), 1033 runtime_options.GetOrDefault(Opt::LongGCLogThreshold), 1034 runtime_options.Exists(Opt::IgnoreMaxFootprint), 1035 runtime_options.GetOrDefault(Opt::UseTLAB), 1036 xgc_option.verify_pre_gc_heap_, 1037 xgc_option.verify_pre_sweeping_heap_, 1038 xgc_option.verify_post_gc_heap_, 1039 xgc_option.verify_pre_gc_rosalloc_, 1040 xgc_option.verify_pre_sweeping_rosalloc_, 1041 xgc_option.verify_post_gc_rosalloc_, 1042 xgc_option.gcstress_, 1043 runtime_options.GetOrDefault(Opt::EnableHSpaceCompactForOOM), 1044 runtime_options.GetOrDefault(Opt::HSpaceCompactForOOMMinIntervalsMs)); 1045 1046 if (!heap_->HasBootImageSpace() && !allow_dex_file_fallback_) { 1047 LOG(ERROR) << "Dex file fallback disabled, cannot continue without image."; 1048 return false; 1049 } 1050 1051 dump_gc_performance_on_shutdown_ = runtime_options.Exists(Opt::DumpGCPerformanceOnShutdown); 1052 1053 if (runtime_options.Exists(Opt::JdwpOptions)) { 1054 Dbg::ConfigureJdwp(runtime_options.GetOrDefault(Opt::JdwpOptions)); 1055 } 1056 1057 jit_options_.reset(jit::JitOptions::CreateFromRuntimeArguments(runtime_options)); 1058 if (IsAotCompiler()) { 1059 // If we are already the compiler at this point, we must be dex2oat. Don't create the jit in 1060 // this case. 1061 // If runtime_options doesn't have UseJIT set to true then CreateFromRuntimeArguments returns 1062 // null and we don't create the jit. 1063 jit_options_->SetUseJitCompilation(false); 1064 jit_options_->SetSaveProfilingInfo(false); 1065 } 1066 1067 // Allocate a global table of boxed lambda objects <-> closures. 1068 lambda_box_table_ = MakeUnique<lambda::BoxTable>(); 1069 1070 // Use MemMap arena pool for jit, malloc otherwise. Malloc arenas are faster to allocate but 1071 // can't be trimmed as easily. 1072 const bool use_malloc = IsAotCompiler(); 1073 arena_pool_.reset(new ArenaPool(use_malloc, /* low_4gb */ false)); 1074 jit_arena_pool_.reset( 1075 new ArenaPool(/* use_malloc */ false, /* low_4gb */ false, "CompilerMetadata")); 1076 1077 if (IsAotCompiler() && Is64BitInstructionSet(kRuntimeISA)) { 1078 // 4gb, no malloc. Explanation in header. 1079 low_4gb_arena_pool_.reset(new ArenaPool(/* use_malloc */ false, /* low_4gb */ true)); 1080 } 1081 linear_alloc_.reset(CreateLinearAlloc()); 1082 1083 BlockSignals(); 1084 InitPlatformSignalHandlers(); 1085 1086 // Change the implicit checks flags based on runtime architecture. 1087 switch (kRuntimeISA) { 1088 case kArm: 1089 case kThumb2: 1090 case kX86: 1091 case kArm64: 1092 case kX86_64: 1093 case kMips: 1094 case kMips64: 1095 implicit_null_checks_ = true; 1096 // Installing stack protection does not play well with valgrind. 1097 implicit_so_checks_ = !(RUNNING_ON_MEMORY_TOOL && kMemoryToolIsValgrind); 1098 break; 1099 default: 1100 // Keep the defaults. 1101 break; 1102 } 1103 1104 if (!no_sig_chain_) { 1105 // Dex2Oat's Runtime does not need the signal chain or the fault handler. 1106 1107 // Initialize the signal chain so that any calls to sigaction get 1108 // correctly routed to the next in the chain regardless of whether we 1109 // have claimed the signal or not. 1110 InitializeSignalChain(); 1111 1112 if (implicit_null_checks_ || implicit_so_checks_ || implicit_suspend_checks_) { 1113 fault_manager.Init(); 1114 1115 // These need to be in a specific order. The null point check handler must be 1116 // after the suspend check and stack overflow check handlers. 1117 // 1118 // Note: the instances attach themselves to the fault manager and are handled by it. The manager 1119 // will delete the instance on Shutdown(). 1120 if (implicit_suspend_checks_) { 1121 new SuspensionHandler(&fault_manager); 1122 } 1123 1124 if (implicit_so_checks_) { 1125 new StackOverflowHandler(&fault_manager); 1126 } 1127 1128 if (implicit_null_checks_) { 1129 new NullPointerHandler(&fault_manager); 1130 } 1131 1132 if (kEnableJavaStackTraceHandler) { 1133 new JavaStackTraceHandler(&fault_manager); 1134 } 1135 } 1136 } 1137 1138 java_vm_ = new JavaVMExt(this, runtime_options); 1139 1140 Thread::Startup(); 1141 1142 // ClassLinker needs an attached thread, but we can't fully attach a thread without creating 1143 // objects. We can't supply a thread group yet; it will be fixed later. Since we are the main 1144 // thread, we do not get a java peer. 1145 Thread* self = Thread::Attach("main", false, nullptr, false); 1146 CHECK_EQ(self->GetThreadId(), ThreadList::kMainThreadId); 1147 CHECK(self != nullptr); 1148 1149 // Set us to runnable so tools using a runtime can allocate and GC by default 1150 self->TransitionFromSuspendedToRunnable(); 1151 1152 // Now we're attached, we can take the heap locks and validate the heap. 1153 GetHeap()->EnableObjectValidation(); 1154 1155 CHECK_GE(GetHeap()->GetContinuousSpaces().size(), 1U); 1156 class_linker_ = new ClassLinker(intern_table_); 1157 if (GetHeap()->HasBootImageSpace()) { 1158 std::string error_msg; 1159 bool result = class_linker_->InitFromBootImage(&error_msg); 1160 if (!result) { 1161 LOG(ERROR) << "Could not initialize from image: " << error_msg; 1162 return false; 1163 } 1164 if (kIsDebugBuild) { 1165 for (auto image_space : GetHeap()->GetBootImageSpaces()) { 1166 image_space->VerifyImageAllocations(); 1167 } 1168 } 1169 if (boot_class_path_string_.empty()) { 1170 // The bootclasspath is not explicitly specified: construct it from the loaded dex files. 1171 const std::vector<const DexFile*>& boot_class_path = GetClassLinker()->GetBootClassPath(); 1172 std::vector<std::string> dex_locations; 1173 dex_locations.reserve(boot_class_path.size()); 1174 for (const DexFile* dex_file : boot_class_path) { 1175 dex_locations.push_back(dex_file->GetLocation()); 1176 } 1177 boot_class_path_string_ = Join(dex_locations, ':'); 1178 } 1179 { 1180 ScopedTrace trace2("AddImageStringsToTable"); 1181 GetInternTable()->AddImagesStringsToTable(heap_->GetBootImageSpaces()); 1182 } 1183 { 1184 ScopedTrace trace2("MoveImageClassesToClassTable"); 1185 GetClassLinker()->AddBootImageClassesToClassTable(); 1186 } 1187 } else { 1188 std::vector<std::string> dex_filenames; 1189 Split(boot_class_path_string_, ':', &dex_filenames); 1190 1191 std::vector<std::string> dex_locations; 1192 if (!runtime_options.Exists(Opt::BootClassPathLocations)) { 1193 dex_locations = dex_filenames; 1194 } else { 1195 dex_locations = runtime_options.GetOrDefault(Opt::BootClassPathLocations); 1196 CHECK_EQ(dex_filenames.size(), dex_locations.size()); 1197 } 1198 1199 std::vector<std::unique_ptr<const DexFile>> boot_class_path; 1200 if (runtime_options.Exists(Opt::BootClassPathDexList)) { 1201 boot_class_path.swap(*runtime_options.GetOrDefault(Opt::BootClassPathDexList)); 1202 } else { 1203 OpenDexFiles(dex_filenames, 1204 dex_locations, 1205 runtime_options.GetOrDefault(Opt::Image), 1206 &boot_class_path); 1207 } 1208 instruction_set_ = runtime_options.GetOrDefault(Opt::ImageInstructionSet); 1209 std::string error_msg; 1210 if (!class_linker_->InitWithoutImage(std::move(boot_class_path), &error_msg)) { 1211 LOG(ERROR) << "Could not initialize without image: " << error_msg; 1212 return false; 1213 } 1214 1215 // TODO: Should we move the following to InitWithoutImage? 1216 SetInstructionSet(instruction_set_); 1217 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { 1218 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); 1219 if (!HasCalleeSaveMethod(type)) { 1220 SetCalleeSaveMethod(CreateCalleeSaveMethod(), type); 1221 } 1222 } 1223 } 1224 1225 CHECK(class_linker_ != nullptr); 1226 1227 verifier::MethodVerifier::Init(); 1228 1229 if (runtime_options.Exists(Opt::MethodTrace)) { 1230 trace_config_.reset(new TraceConfig()); 1231 trace_config_->trace_file = runtime_options.ReleaseOrDefault(Opt::MethodTraceFile); 1232 trace_config_->trace_file_size = runtime_options.ReleaseOrDefault(Opt::MethodTraceFileSize); 1233 trace_config_->trace_mode = Trace::TraceMode::kMethodTracing; 1234 trace_config_->trace_output_mode = runtime_options.Exists(Opt::MethodTraceStreaming) ? 1235 Trace::TraceOutputMode::kStreaming : 1236 Trace::TraceOutputMode::kFile; 1237 } 1238 1239 { 1240 auto&& profiler_options = runtime_options.ReleaseOrDefault(Opt::ProfilerOpts); 1241 profile_output_filename_ = profiler_options.output_file_name_; 1242 1243 // TODO: Don't do this, just change ProfilerOptions to include the output file name? 1244 ProfilerOptions other_options( 1245 profiler_options.enabled_, 1246 profiler_options.period_s_, 1247 profiler_options.duration_s_, 1248 profiler_options.interval_us_, 1249 profiler_options.backoff_coefficient_, 1250 profiler_options.start_immediately_, 1251 profiler_options.top_k_threshold_, 1252 profiler_options.top_k_change_threshold_, 1253 profiler_options.profile_type_, 1254 profiler_options.max_stack_depth_); 1255 1256 profiler_options_ = other_options; 1257 } 1258 1259 // TODO: move this to just be an Trace::Start argument 1260 Trace::SetDefaultClockSource(runtime_options.GetOrDefault(Opt::ProfileClock)); 1261 1262 // Pre-allocate an OutOfMemoryError for the double-OOME case. 1263 self->ThrowNewException("Ljava/lang/OutOfMemoryError;", 1264 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; " 1265 "no stack trace available"); 1266 pre_allocated_OutOfMemoryError_ = GcRoot<mirror::Throwable>(self->GetException()); 1267 self->ClearException(); 1268 1269 // Pre-allocate a NoClassDefFoundError for the common case of failing to find a system class 1270 // ahead of checking the application's class loader. 1271 self->ThrowNewException("Ljava/lang/NoClassDefFoundError;", 1272 "Class not found using the boot class loader; no stack trace available"); 1273 pre_allocated_NoClassDefFoundError_ = GcRoot<mirror::Throwable>(self->GetException()); 1274 self->ClearException(); 1275 1276 // Look for a native bridge. 1277 // 1278 // The intended flow here is, in the case of a running system: 1279 // 1280 // Runtime::Init() (zygote): 1281 // LoadNativeBridge -> dlopen from cmd line parameter. 1282 // | 1283 // V 1284 // Runtime::Start() (zygote): 1285 // No-op wrt native bridge. 1286 // | 1287 // | start app 1288 // V 1289 // DidForkFromZygote(action) 1290 // action = kUnload -> dlclose native bridge. 1291 // action = kInitialize -> initialize library 1292 // 1293 // 1294 // The intended flow here is, in the case of a simple dalvikvm call: 1295 // 1296 // Runtime::Init(): 1297 // LoadNativeBridge -> dlopen from cmd line parameter. 1298 // | 1299 // V 1300 // Runtime::Start(): 1301 // DidForkFromZygote(kInitialize) -> try to initialize any native bridge given. 1302 // No-op wrt native bridge. 1303 { 1304 std::string native_bridge_file_name = runtime_options.ReleaseOrDefault(Opt::NativeBridge); 1305 is_native_bridge_loaded_ = LoadNativeBridge(native_bridge_file_name); 1306 } 1307 1308 VLOG(startup) << "Runtime::Init exiting"; 1309 1310 return true; 1311 } 1312 1313 void Runtime::InitNativeMethods() { 1314 VLOG(startup) << "Runtime::InitNativeMethods entering"; 1315 Thread* self = Thread::Current(); 1316 JNIEnv* env = self->GetJniEnv(); 1317 1318 // Must be in the kNative state for calling native methods (JNI_OnLoad code). 1319 CHECK_EQ(self->GetState(), kNative); 1320 1321 // First set up JniConstants, which is used by both the runtime's built-in native 1322 // methods and libcore. 1323 JniConstants::init(env); 1324 1325 // Then set up the native methods provided by the runtime itself. 1326 RegisterRuntimeNativeMethods(env); 1327 1328 // Initialize classes used in JNI. The initialization requires runtime native 1329 // methods to be loaded first. 1330 WellKnownClasses::Init(env); 1331 1332 // Then set up libjavacore / libopenjdk, which are just a regular JNI libraries with 1333 // a regular JNI_OnLoad. Most JNI libraries can just use System.loadLibrary, but 1334 // libcore can't because it's the library that implements System.loadLibrary! 1335 { 1336 std::string error_msg; 1337 if (!java_vm_->LoadNativeLibrary(env, "libjavacore.so", nullptr, nullptr, &error_msg)) { 1338 LOG(FATAL) << "LoadNativeLibrary failed for \"libjavacore.so\": " << error_msg; 1339 } 1340 } 1341 { 1342 constexpr const char* kOpenJdkLibrary = kIsDebugBuild 1343 ? "libopenjdkd.so" 1344 : "libopenjdk.so"; 1345 std::string error_msg; 1346 if (!java_vm_->LoadNativeLibrary(env, kOpenJdkLibrary, nullptr, nullptr, &error_msg)) { 1347 LOG(FATAL) << "LoadNativeLibrary failed for \"" << kOpenJdkLibrary << "\": " << error_msg; 1348 } 1349 } 1350 1351 // Initialize well known classes that may invoke runtime native methods. 1352 WellKnownClasses::LateInit(env); 1353 1354 VLOG(startup) << "Runtime::InitNativeMethods exiting"; 1355 } 1356 1357 void Runtime::ReclaimArenaPoolMemory() { 1358 arena_pool_->LockReclaimMemory(); 1359 } 1360 1361 void Runtime::InitThreadGroups(Thread* self) { 1362 JNIEnvExt* env = self->GetJniEnv(); 1363 ScopedJniEnvLocalRefState env_state(env); 1364 main_thread_group_ = 1365 env->NewGlobalRef(env->GetStaticObjectField( 1366 WellKnownClasses::java_lang_ThreadGroup, 1367 WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup)); 1368 CHECK(main_thread_group_ != nullptr || IsAotCompiler()); 1369 system_thread_group_ = 1370 env->NewGlobalRef(env->GetStaticObjectField( 1371 WellKnownClasses::java_lang_ThreadGroup, 1372 WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup)); 1373 CHECK(system_thread_group_ != nullptr || IsAotCompiler()); 1374 } 1375 1376 jobject Runtime::GetMainThreadGroup() const { 1377 CHECK(main_thread_group_ != nullptr || IsAotCompiler()); 1378 return main_thread_group_; 1379 } 1380 1381 jobject Runtime::GetSystemThreadGroup() const { 1382 CHECK(system_thread_group_ != nullptr || IsAotCompiler()); 1383 return system_thread_group_; 1384 } 1385 1386 jobject Runtime::GetSystemClassLoader() const { 1387 CHECK(system_class_loader_ != nullptr || IsAotCompiler()); 1388 return system_class_loader_; 1389 } 1390 1391 void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) { 1392 register_dalvik_system_DexFile(env); 1393 register_dalvik_system_VMDebug(env); 1394 register_dalvik_system_VMRuntime(env); 1395 register_dalvik_system_VMStack(env); 1396 register_dalvik_system_ZygoteHooks(env); 1397 register_java_lang_Class(env); 1398 register_java_lang_DexCache(env); 1399 register_java_lang_Object(env); 1400 register_java_lang_ref_FinalizerReference(env); 1401 register_java_lang_reflect_AbstractMethod(env); 1402 register_java_lang_reflect_Array(env); 1403 register_java_lang_reflect_Constructor(env); 1404 register_java_lang_reflect_Field(env); 1405 register_java_lang_reflect_Method(env); 1406 register_java_lang_reflect_Proxy(env); 1407 register_java_lang_ref_Reference(env); 1408 register_java_lang_String(env); 1409 register_java_lang_StringFactory(env); 1410 register_java_lang_System(env); 1411 register_java_lang_Thread(env); 1412 register_java_lang_Throwable(env); 1413 register_java_lang_VMClassLoader(env); 1414 register_java_util_concurrent_atomic_AtomicLong(env); 1415 register_libcore_util_CharsetUtils(env); 1416 register_org_apache_harmony_dalvik_ddmc_DdmServer(env); 1417 register_org_apache_harmony_dalvik_ddmc_DdmVmInternal(env); 1418 register_sun_misc_Unsafe(env); 1419 } 1420 1421 void Runtime::DumpForSigQuit(std::ostream& os) { 1422 GetClassLinker()->DumpForSigQuit(os); 1423 GetInternTable()->DumpForSigQuit(os); 1424 GetJavaVM()->DumpForSigQuit(os); 1425 GetHeap()->DumpForSigQuit(os); 1426 oat_file_manager_->DumpForSigQuit(os); 1427 if (GetJit() != nullptr) { 1428 GetJit()->DumpForSigQuit(os); 1429 } else { 1430 os << "Running non JIT\n"; 1431 } 1432 TrackedAllocators::Dump(os); 1433 os << "\n"; 1434 1435 thread_list_->DumpForSigQuit(os); 1436 BaseMutex::DumpAll(os); 1437 } 1438 1439 void Runtime::DumpLockHolders(std::ostream& os) { 1440 uint64_t mutator_lock_owner = Locks::mutator_lock_->GetExclusiveOwnerTid(); 1441 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner(); 1442 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner(); 1443 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner(); 1444 if ((thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) { 1445 os << "Mutator lock exclusive owner tid: " << mutator_lock_owner << "\n" 1446 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n" 1447 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n" 1448 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n"; 1449 } 1450 } 1451 1452 void Runtime::SetStatsEnabled(bool new_state) { 1453 Thread* self = Thread::Current(); 1454 MutexLock mu(self, *Locks::instrument_entrypoints_lock_); 1455 if (new_state == true) { 1456 GetStats()->Clear(~0); 1457 // TODO: wouldn't it make more sense to clear _all_ threads' stats? 1458 self->GetStats()->Clear(~0); 1459 if (stats_enabled_ != new_state) { 1460 GetInstrumentation()->InstrumentQuickAllocEntryPointsLocked(); 1461 } 1462 } else if (stats_enabled_ != new_state) { 1463 GetInstrumentation()->UninstrumentQuickAllocEntryPointsLocked(); 1464 } 1465 stats_enabled_ = new_state; 1466 } 1467 1468 void Runtime::ResetStats(int kinds) { 1469 GetStats()->Clear(kinds & 0xffff); 1470 // TODO: wouldn't it make more sense to clear _all_ threads' stats? 1471 Thread::Current()->GetStats()->Clear(kinds >> 16); 1472 } 1473 1474 int32_t Runtime::GetStat(int kind) { 1475 RuntimeStats* stats; 1476 if (kind < (1<<16)) { 1477 stats = GetStats(); 1478 } else { 1479 stats = Thread::Current()->GetStats(); 1480 kind >>= 16; 1481 } 1482 switch (kind) { 1483 case KIND_ALLOCATED_OBJECTS: 1484 return stats->allocated_objects; 1485 case KIND_ALLOCATED_BYTES: 1486 return stats->allocated_bytes; 1487 case KIND_FREED_OBJECTS: 1488 return stats->freed_objects; 1489 case KIND_FREED_BYTES: 1490 return stats->freed_bytes; 1491 case KIND_GC_INVOCATIONS: 1492 return stats->gc_for_alloc_count; 1493 case KIND_CLASS_INIT_COUNT: 1494 return stats->class_init_count; 1495 case KIND_CLASS_INIT_TIME: 1496 // Convert ns to us, reduce to 32 bits. 1497 return static_cast<int>(stats->class_init_time_ns / 1000); 1498 case KIND_EXT_ALLOCATED_OBJECTS: 1499 case KIND_EXT_ALLOCATED_BYTES: 1500 case KIND_EXT_FREED_OBJECTS: 1501 case KIND_EXT_FREED_BYTES: 1502 return 0; // backward compatibility 1503 default: 1504 LOG(FATAL) << "Unknown statistic " << kind; 1505 return -1; // unreachable 1506 } 1507 } 1508 1509 void Runtime::BlockSignals() { 1510 SignalSet signals; 1511 signals.Add(SIGPIPE); 1512 // SIGQUIT is used to dump the runtime's state (including stack traces). 1513 signals.Add(SIGQUIT); 1514 // SIGUSR1 is used to initiate a GC. 1515 signals.Add(SIGUSR1); 1516 signals.Block(); 1517 } 1518 1519 bool Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group, 1520 bool create_peer) { 1521 ScopedTrace trace(__FUNCTION__); 1522 return Thread::Attach(thread_name, as_daemon, thread_group, create_peer) != nullptr; 1523 } 1524 1525 void Runtime::DetachCurrentThread() { 1526 ScopedTrace trace(__FUNCTION__); 1527 Thread* self = Thread::Current(); 1528 if (self == nullptr) { 1529 LOG(FATAL) << "attempting to detach thread that is not attached"; 1530 } 1531 if (self->HasManagedStack()) { 1532 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code"; 1533 } 1534 thread_list_->Unregister(self); 1535 } 1536 1537 mirror::Throwable* Runtime::GetPreAllocatedOutOfMemoryError() { 1538 mirror::Throwable* oome = pre_allocated_OutOfMemoryError_.Read(); 1539 if (oome == nullptr) { 1540 LOG(ERROR) << "Failed to return pre-allocated OOME"; 1541 } 1542 return oome; 1543 } 1544 1545 mirror::Throwable* Runtime::GetPreAllocatedNoClassDefFoundError() { 1546 mirror::Throwable* ncdfe = pre_allocated_NoClassDefFoundError_.Read(); 1547 if (ncdfe == nullptr) { 1548 LOG(ERROR) << "Failed to return pre-allocated NoClassDefFoundError"; 1549 } 1550 return ncdfe; 1551 } 1552 1553 void Runtime::VisitConstantRoots(RootVisitor* visitor) { 1554 // Visit the classes held as static in mirror classes, these can be visited concurrently and only 1555 // need to be visited once per GC since they never change. 1556 mirror::Class::VisitRoots(visitor); 1557 mirror::Constructor::VisitRoots(visitor); 1558 mirror::Reference::VisitRoots(visitor); 1559 mirror::Method::VisitRoots(visitor); 1560 mirror::StackTraceElement::VisitRoots(visitor); 1561 mirror::String::VisitRoots(visitor); 1562 mirror::Throwable::VisitRoots(visitor); 1563 mirror::Field::VisitRoots(visitor); 1564 // Visit all the primitive array types classes. 1565 mirror::PrimitiveArray<uint8_t>::VisitRoots(visitor); // BooleanArray 1566 mirror::PrimitiveArray<int8_t>::VisitRoots(visitor); // ByteArray 1567 mirror::PrimitiveArray<uint16_t>::VisitRoots(visitor); // CharArray 1568 mirror::PrimitiveArray<double>::VisitRoots(visitor); // DoubleArray 1569 mirror::PrimitiveArray<float>::VisitRoots(visitor); // FloatArray 1570 mirror::PrimitiveArray<int32_t>::VisitRoots(visitor); // IntArray 1571 mirror::PrimitiveArray<int64_t>::VisitRoots(visitor); // LongArray 1572 mirror::PrimitiveArray<int16_t>::VisitRoots(visitor); // ShortArray 1573 // Visiting the roots of these ArtMethods is not currently required since all the GcRoots are 1574 // null. 1575 BufferedRootVisitor<16> buffered_visitor(visitor, RootInfo(kRootVMInternal)); 1576 const size_t pointer_size = GetClassLinker()->GetImagePointerSize(); 1577 if (HasResolutionMethod()) { 1578 resolution_method_->VisitRoots(buffered_visitor, pointer_size); 1579 } 1580 if (HasImtConflictMethod()) { 1581 imt_conflict_method_->VisitRoots(buffered_visitor, pointer_size); 1582 } 1583 if (imt_unimplemented_method_ != nullptr) { 1584 imt_unimplemented_method_->VisitRoots(buffered_visitor, pointer_size); 1585 } 1586 for (size_t i = 0; i < kLastCalleeSaveType; ++i) { 1587 auto* m = reinterpret_cast<ArtMethod*>(callee_save_methods_[i]); 1588 if (m != nullptr) { 1589 m->VisitRoots(buffered_visitor, pointer_size); 1590 } 1591 } 1592 } 1593 1594 void Runtime::VisitConcurrentRoots(RootVisitor* visitor, VisitRootFlags flags) { 1595 intern_table_->VisitRoots(visitor, flags); 1596 class_linker_->VisitRoots(visitor, flags); 1597 heap_->VisitAllocationRecords(visitor); 1598 if ((flags & kVisitRootFlagNewRoots) == 0) { 1599 // Guaranteed to have no new roots in the constant roots. 1600 VisitConstantRoots(visitor); 1601 } 1602 Dbg::VisitRoots(visitor); 1603 } 1604 1605 void Runtime::VisitTransactionRoots(RootVisitor* visitor) { 1606 if (preinitialization_transaction_ != nullptr) { 1607 preinitialization_transaction_->VisitRoots(visitor); 1608 } 1609 } 1610 1611 void Runtime::VisitNonThreadRoots(RootVisitor* visitor) { 1612 java_vm_->VisitRoots(visitor); 1613 sentinel_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); 1614 pre_allocated_OutOfMemoryError_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); 1615 pre_allocated_NoClassDefFoundError_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); 1616 verifier::MethodVerifier::VisitStaticRoots(visitor); 1617 VisitTransactionRoots(visitor); 1618 } 1619 1620 void Runtime::VisitNonConcurrentRoots(RootVisitor* visitor) { 1621 thread_list_->VisitRoots(visitor); 1622 VisitNonThreadRoots(visitor); 1623 } 1624 1625 void Runtime::VisitThreadRoots(RootVisitor* visitor) { 1626 thread_list_->VisitRoots(visitor); 1627 } 1628 1629 size_t Runtime::FlipThreadRoots(Closure* thread_flip_visitor, Closure* flip_callback, 1630 gc::collector::GarbageCollector* collector) { 1631 return thread_list_->FlipThreadRoots(thread_flip_visitor, flip_callback, collector); 1632 } 1633 1634 void Runtime::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { 1635 VisitNonConcurrentRoots(visitor); 1636 VisitConcurrentRoots(visitor, flags); 1637 } 1638 1639 void Runtime::VisitImageRoots(RootVisitor* visitor) { 1640 for (auto* space : GetHeap()->GetContinuousSpaces()) { 1641 if (space->IsImageSpace()) { 1642 auto* image_space = space->AsImageSpace(); 1643 const auto& image_header = image_space->GetImageHeader(); 1644 for (size_t i = 0; i < ImageHeader::kImageRootsMax; ++i) { 1645 auto* obj = image_header.GetImageRoot(static_cast<ImageHeader::ImageRoot>(i)); 1646 if (obj != nullptr) { 1647 auto* after_obj = obj; 1648 visitor->VisitRoot(&after_obj, RootInfo(kRootStickyClass)); 1649 CHECK_EQ(after_obj, obj); 1650 } 1651 } 1652 } 1653 } 1654 } 1655 1656 ArtMethod* Runtime::CreateImtConflictMethod(LinearAlloc* linear_alloc) { 1657 ClassLinker* const class_linker = GetClassLinker(); 1658 ArtMethod* method = class_linker->CreateRuntimeMethod(linear_alloc); 1659 // When compiling, the code pointer will get set later when the image is loaded. 1660 const size_t pointer_size = GetInstructionSetPointerSize(instruction_set_); 1661 if (IsAotCompiler()) { 1662 method->SetEntryPointFromQuickCompiledCodePtrSize(nullptr, pointer_size); 1663 } else { 1664 method->SetEntryPointFromQuickCompiledCode(GetQuickImtConflictStub()); 1665 } 1666 // Create empty conflict table. 1667 method->SetImtConflictTable(class_linker->CreateImtConflictTable(/*count*/0u, linear_alloc), 1668 pointer_size); 1669 return method; 1670 } 1671 1672 void Runtime::SetImtConflictMethod(ArtMethod* method) { 1673 CHECK(method != nullptr); 1674 CHECK(method->IsRuntimeMethod()); 1675 imt_conflict_method_ = method; 1676 } 1677 1678 ArtMethod* Runtime::CreateResolutionMethod() { 1679 auto* method = GetClassLinker()->CreateRuntimeMethod(GetLinearAlloc()); 1680 // When compiling, the code pointer will get set later when the image is loaded. 1681 if (IsAotCompiler()) { 1682 size_t pointer_size = GetInstructionSetPointerSize(instruction_set_); 1683 method->SetEntryPointFromQuickCompiledCodePtrSize(nullptr, pointer_size); 1684 } else { 1685 method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); 1686 } 1687 return method; 1688 } 1689 1690 ArtMethod* Runtime::CreateCalleeSaveMethod() { 1691 auto* method = GetClassLinker()->CreateRuntimeMethod(GetLinearAlloc()); 1692 size_t pointer_size = GetInstructionSetPointerSize(instruction_set_); 1693 method->SetEntryPointFromQuickCompiledCodePtrSize(nullptr, pointer_size); 1694 DCHECK_NE(instruction_set_, kNone); 1695 DCHECK(method->IsRuntimeMethod()); 1696 return method; 1697 } 1698 1699 void Runtime::DisallowNewSystemWeaks() { 1700 CHECK(!kUseReadBarrier); 1701 monitor_list_->DisallowNewMonitors(); 1702 intern_table_->ChangeWeakRootState(gc::kWeakRootStateNoReadsOrWrites); 1703 java_vm_->DisallowNewWeakGlobals(); 1704 heap_->DisallowNewAllocationRecords(); 1705 lambda_box_table_->DisallowNewWeakBoxedLambdas(); 1706 } 1707 1708 void Runtime::AllowNewSystemWeaks() { 1709 CHECK(!kUseReadBarrier); 1710 monitor_list_->AllowNewMonitors(); 1711 intern_table_->ChangeWeakRootState(gc::kWeakRootStateNormal); // TODO: Do this in the sweeping. 1712 java_vm_->AllowNewWeakGlobals(); 1713 heap_->AllowNewAllocationRecords(); 1714 lambda_box_table_->AllowNewWeakBoxedLambdas(); 1715 } 1716 1717 void Runtime::BroadcastForNewSystemWeaks() { 1718 // This is used for the read barrier case that uses the thread-local 1719 // Thread::GetWeakRefAccessEnabled() flag. 1720 CHECK(kUseReadBarrier); 1721 monitor_list_->BroadcastForNewMonitors(); 1722 intern_table_->BroadcastForNewInterns(); 1723 java_vm_->BroadcastForNewWeakGlobals(); 1724 heap_->BroadcastForNewAllocationRecords(); 1725 lambda_box_table_->BroadcastForNewWeakBoxedLambdas(); 1726 } 1727 1728 void Runtime::SetInstructionSet(InstructionSet instruction_set) { 1729 instruction_set_ = instruction_set; 1730 if ((instruction_set_ == kThumb2) || (instruction_set_ == kArm)) { 1731 for (int i = 0; i != kLastCalleeSaveType; ++i) { 1732 CalleeSaveType type = static_cast<CalleeSaveType>(i); 1733 callee_save_method_frame_infos_[i] = arm::ArmCalleeSaveMethodFrameInfo(type); 1734 } 1735 } else if (instruction_set_ == kMips) { 1736 for (int i = 0; i != kLastCalleeSaveType; ++i) { 1737 CalleeSaveType type = static_cast<CalleeSaveType>(i); 1738 callee_save_method_frame_infos_[i] = mips::MipsCalleeSaveMethodFrameInfo(type); 1739 } 1740 } else if (instruction_set_ == kMips64) { 1741 for (int i = 0; i != kLastCalleeSaveType; ++i) { 1742 CalleeSaveType type = static_cast<CalleeSaveType>(i); 1743 callee_save_method_frame_infos_[i] = mips64::Mips64CalleeSaveMethodFrameInfo(type); 1744 } 1745 } else if (instruction_set_ == kX86) { 1746 for (int i = 0; i != kLastCalleeSaveType; ++i) { 1747 CalleeSaveType type = static_cast<CalleeSaveType>(i); 1748 callee_save_method_frame_infos_[i] = x86::X86CalleeSaveMethodFrameInfo(type); 1749 } 1750 } else if (instruction_set_ == kX86_64) { 1751 for (int i = 0; i != kLastCalleeSaveType; ++i) { 1752 CalleeSaveType type = static_cast<CalleeSaveType>(i); 1753 callee_save_method_frame_infos_[i] = x86_64::X86_64CalleeSaveMethodFrameInfo(type); 1754 } 1755 } else if (instruction_set_ == kArm64) { 1756 for (int i = 0; i != kLastCalleeSaveType; ++i) { 1757 CalleeSaveType type = static_cast<CalleeSaveType>(i); 1758 callee_save_method_frame_infos_[i] = arm64::Arm64CalleeSaveMethodFrameInfo(type); 1759 } 1760 } else { 1761 UNIMPLEMENTED(FATAL) << instruction_set_; 1762 } 1763 } 1764 1765 void Runtime::SetCalleeSaveMethod(ArtMethod* method, CalleeSaveType type) { 1766 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType)); 1767 CHECK(method != nullptr); 1768 callee_save_methods_[type] = reinterpret_cast<uintptr_t>(method); 1769 } 1770 1771 void Runtime::RegisterAppInfo(const std::vector<std::string>& code_paths, 1772 const std::string& profile_output_filename, 1773 const std::string& foreign_dex_profile_path, 1774 const std::string& app_dir) { 1775 if (jit_.get() == nullptr) { 1776 // We are not JITing. Nothing to do. 1777 return; 1778 } 1779 1780 VLOG(profiler) << "Register app with " << profile_output_filename 1781 << " " << Join(code_paths, ':'); 1782 1783 if (profile_output_filename.empty()) { 1784 LOG(WARNING) << "JIT profile information will not be recorded: profile filename is empty."; 1785 return; 1786 } 1787 if (!FileExists(profile_output_filename)) { 1788 LOG(WARNING) << "JIT profile information will not be recorded: profile file does not exits."; 1789 return; 1790 } 1791 if (code_paths.empty()) { 1792 LOG(WARNING) << "JIT profile information will not be recorded: code paths is empty."; 1793 return; 1794 } 1795 1796 profile_output_filename_ = profile_output_filename; 1797 jit_->StartProfileSaver(profile_output_filename, 1798 code_paths, 1799 foreign_dex_profile_path, 1800 app_dir); 1801 } 1802 1803 void Runtime::NotifyDexLoaded(const std::string& dex_location) { 1804 VLOG(profiler) << "Notify dex loaded: " << dex_location; 1805 // We know that if the ProfileSaver is started then we can record profile information. 1806 if (ProfileSaver::IsStarted()) { 1807 ProfileSaver::NotifyDexUse(dex_location); 1808 } 1809 } 1810 1811 // Transaction support. 1812 void Runtime::EnterTransactionMode(Transaction* transaction) { 1813 DCHECK(IsAotCompiler()); 1814 DCHECK(transaction != nullptr); 1815 DCHECK(!IsActiveTransaction()); 1816 preinitialization_transaction_ = transaction; 1817 } 1818 1819 void Runtime::ExitTransactionMode() { 1820 DCHECK(IsAotCompiler()); 1821 DCHECK(IsActiveTransaction()); 1822 preinitialization_transaction_ = nullptr; 1823 } 1824 1825 bool Runtime::IsTransactionAborted() const { 1826 if (!IsActiveTransaction()) { 1827 return false; 1828 } else { 1829 DCHECK(IsAotCompiler()); 1830 return preinitialization_transaction_->IsAborted(); 1831 } 1832 } 1833 1834 void Runtime::AbortTransactionAndThrowAbortError(Thread* self, const std::string& abort_message) { 1835 DCHECK(IsAotCompiler()); 1836 DCHECK(IsActiveTransaction()); 1837 // Throwing an exception may cause its class initialization. If we mark the transaction 1838 // aborted before that, we may warn with a false alarm. Throwing the exception before 1839 // marking the transaction aborted avoids that. 1840 preinitialization_transaction_->ThrowAbortError(self, &abort_message); 1841 preinitialization_transaction_->Abort(abort_message); 1842 } 1843 1844 void Runtime::ThrowTransactionAbortError(Thread* self) { 1845 DCHECK(IsAotCompiler()); 1846 DCHECK(IsActiveTransaction()); 1847 // Passing nullptr means we rethrow an exception with the earlier transaction abort message. 1848 preinitialization_transaction_->ThrowAbortError(self, nullptr); 1849 } 1850 1851 void Runtime::RecordWriteFieldBoolean(mirror::Object* obj, MemberOffset field_offset, 1852 uint8_t value, bool is_volatile) const { 1853 DCHECK(IsAotCompiler()); 1854 DCHECK(IsActiveTransaction()); 1855 preinitialization_transaction_->RecordWriteFieldBoolean(obj, field_offset, value, is_volatile); 1856 } 1857 1858 void Runtime::RecordWriteFieldByte(mirror::Object* obj, MemberOffset field_offset, 1859 int8_t value, bool is_volatile) const { 1860 DCHECK(IsAotCompiler()); 1861 DCHECK(IsActiveTransaction()); 1862 preinitialization_transaction_->RecordWriteFieldByte(obj, field_offset, value, is_volatile); 1863 } 1864 1865 void Runtime::RecordWriteFieldChar(mirror::Object* obj, MemberOffset field_offset, 1866 uint16_t value, bool is_volatile) const { 1867 DCHECK(IsAotCompiler()); 1868 DCHECK(IsActiveTransaction()); 1869 preinitialization_transaction_->RecordWriteFieldChar(obj, field_offset, value, is_volatile); 1870 } 1871 1872 void Runtime::RecordWriteFieldShort(mirror::Object* obj, MemberOffset field_offset, 1873 int16_t value, bool is_volatile) const { 1874 DCHECK(IsAotCompiler()); 1875 DCHECK(IsActiveTransaction()); 1876 preinitialization_transaction_->RecordWriteFieldShort(obj, field_offset, value, is_volatile); 1877 } 1878 1879 void Runtime::RecordWriteField32(mirror::Object* obj, MemberOffset field_offset, 1880 uint32_t value, bool is_volatile) const { 1881 DCHECK(IsAotCompiler()); 1882 DCHECK(IsActiveTransaction()); 1883 preinitialization_transaction_->RecordWriteField32(obj, field_offset, value, is_volatile); 1884 } 1885 1886 void Runtime::RecordWriteField64(mirror::Object* obj, MemberOffset field_offset, 1887 uint64_t value, bool is_volatile) const { 1888 DCHECK(IsAotCompiler()); 1889 DCHECK(IsActiveTransaction()); 1890 preinitialization_transaction_->RecordWriteField64(obj, field_offset, value, is_volatile); 1891 } 1892 1893 void Runtime::RecordWriteFieldReference(mirror::Object* obj, MemberOffset field_offset, 1894 mirror::Object* value, bool is_volatile) const { 1895 DCHECK(IsAotCompiler()); 1896 DCHECK(IsActiveTransaction()); 1897 preinitialization_transaction_->RecordWriteFieldReference(obj, field_offset, value, is_volatile); 1898 } 1899 1900 void Runtime::RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) const { 1901 DCHECK(IsAotCompiler()); 1902 DCHECK(IsActiveTransaction()); 1903 preinitialization_transaction_->RecordWriteArray(array, index, value); 1904 } 1905 1906 void Runtime::RecordStrongStringInsertion(mirror::String* s) const { 1907 DCHECK(IsAotCompiler()); 1908 DCHECK(IsActiveTransaction()); 1909 preinitialization_transaction_->RecordStrongStringInsertion(s); 1910 } 1911 1912 void Runtime::RecordWeakStringInsertion(mirror::String* s) const { 1913 DCHECK(IsAotCompiler()); 1914 DCHECK(IsActiveTransaction()); 1915 preinitialization_transaction_->RecordWeakStringInsertion(s); 1916 } 1917 1918 void Runtime::RecordStrongStringRemoval(mirror::String* s) const { 1919 DCHECK(IsAotCompiler()); 1920 DCHECK(IsActiveTransaction()); 1921 preinitialization_transaction_->RecordStrongStringRemoval(s); 1922 } 1923 1924 void Runtime::RecordWeakStringRemoval(mirror::String* s) const { 1925 DCHECK(IsAotCompiler()); 1926 DCHECK(IsActiveTransaction()); 1927 preinitialization_transaction_->RecordWeakStringRemoval(s); 1928 } 1929 1930 void Runtime::SetFaultMessage(const std::string& message) { 1931 MutexLock mu(Thread::Current(), fault_message_lock_); 1932 fault_message_ = message; 1933 } 1934 1935 void Runtime::AddCurrentRuntimeFeaturesAsDex2OatArguments(std::vector<std::string>* argv) 1936 const { 1937 if (GetInstrumentation()->InterpretOnly()) { 1938 argv->push_back("--compiler-filter=interpret-only"); 1939 } 1940 1941 // Make the dex2oat instruction set match that of the launching runtime. If we have multiple 1942 // architecture support, dex2oat may be compiled as a different instruction-set than that 1943 // currently being executed. 1944 std::string instruction_set("--instruction-set="); 1945 instruction_set += GetInstructionSetString(kRuntimeISA); 1946 argv->push_back(instruction_set); 1947 1948 std::unique_ptr<const InstructionSetFeatures> features(InstructionSetFeatures::FromCppDefines()); 1949 std::string feature_string("--instruction-set-features="); 1950 feature_string += features->GetFeatureString(); 1951 argv->push_back(feature_string); 1952 } 1953 1954 void Runtime::CreateJit() { 1955 CHECK(!IsAotCompiler()); 1956 if (kIsDebugBuild && GetInstrumentation()->IsForcedInterpretOnly()) { 1957 DCHECK(!jit_options_->UseJitCompilation()); 1958 } 1959 std::string error_msg; 1960 jit_.reset(jit::Jit::Create(jit_options_.get(), &error_msg)); 1961 if (jit_.get() == nullptr) { 1962 LOG(WARNING) << "Failed to create JIT " << error_msg; 1963 } 1964 } 1965 1966 bool Runtime::CanRelocate() const { 1967 return !IsAotCompiler() || compiler_callbacks_->IsRelocationPossible(); 1968 } 1969 1970 bool Runtime::IsCompilingBootImage() const { 1971 return IsCompiler() && compiler_callbacks_->IsBootImage(); 1972 } 1973 1974 void Runtime::SetResolutionMethod(ArtMethod* method) { 1975 CHECK(method != nullptr); 1976 CHECK(method->IsRuntimeMethod()) << method; 1977 resolution_method_ = method; 1978 } 1979 1980 void Runtime::SetImtUnimplementedMethod(ArtMethod* method) { 1981 CHECK(method != nullptr); 1982 CHECK(method->IsRuntimeMethod()); 1983 imt_unimplemented_method_ = method; 1984 } 1985 1986 void Runtime::FixupConflictTables() { 1987 // We can only do this after the class linker is created. 1988 const size_t pointer_size = GetClassLinker()->GetImagePointerSize(); 1989 if (imt_unimplemented_method_->GetImtConflictTable(pointer_size) == nullptr) { 1990 imt_unimplemented_method_->SetImtConflictTable( 1991 ClassLinker::CreateImtConflictTable(/*count*/0u, GetLinearAlloc(), pointer_size), 1992 pointer_size); 1993 } 1994 if (imt_conflict_method_->GetImtConflictTable(pointer_size) == nullptr) { 1995 imt_conflict_method_->SetImtConflictTable( 1996 ClassLinker::CreateImtConflictTable(/*count*/0u, GetLinearAlloc(), pointer_size), 1997 pointer_size); 1998 } 1999 } 2000 2001 bool Runtime::IsVerificationEnabled() const { 2002 return verify_ == verifier::VerifyMode::kEnable || 2003 verify_ == verifier::VerifyMode::kSoftFail; 2004 } 2005 2006 bool Runtime::IsVerificationSoftFail() const { 2007 return verify_ == verifier::VerifyMode::kSoftFail; 2008 } 2009 2010 LinearAlloc* Runtime::CreateLinearAlloc() { 2011 // For 64 bit compilers, it needs to be in low 4GB in the case where we are cross compiling for a 2012 // 32 bit target. In this case, we have 32 bit pointers in the dex cache arrays which can't hold 2013 // when we have 64 bit ArtMethod pointers. 2014 return (IsAotCompiler() && Is64BitInstructionSet(kRuntimeISA)) 2015 ? new LinearAlloc(low_4gb_arena_pool_.get()) 2016 : new LinearAlloc(arena_pool_.get()); 2017 } 2018 2019 double Runtime::GetHashTableMinLoadFactor() const { 2020 return is_low_memory_mode_ ? kLowMemoryMinLoadFactor : kNormalMinLoadFactor; 2021 } 2022 2023 double Runtime::GetHashTableMaxLoadFactor() const { 2024 return is_low_memory_mode_ ? kLowMemoryMaxLoadFactor : kNormalMaxLoadFactor; 2025 } 2026 2027 void Runtime::UpdateProcessState(ProcessState process_state) { 2028 ProcessState old_process_state = process_state_; 2029 process_state_ = process_state; 2030 GetHeap()->UpdateProcessState(old_process_state, process_state); 2031 } 2032 2033 void Runtime::RegisterSensitiveThread() const { 2034 Thread::SetJitSensitiveThread(); 2035 } 2036 2037 // Returns true if JIT compilations are enabled. GetJit() will be not null in this case. 2038 bool Runtime::UseJitCompilation() const { 2039 return (jit_ != nullptr) && jit_->UseJitCompilation(); 2040 } 2041 2042 // Returns true if profile saving is enabled. GetJit() will be not null in this case. 2043 bool Runtime::SaveProfileInfo() const { 2044 return (jit_ != nullptr) && jit_->SaveProfilingInfo(); 2045 } 2046 2047 void Runtime::EnvSnapshot::TakeSnapshot() { 2048 char** env = GetEnviron(); 2049 for (size_t i = 0; env[i] != nullptr; ++i) { 2050 name_value_pairs_.emplace_back(new std::string(env[i])); 2051 } 2052 // The strings in name_value_pairs_ retain ownership of the c_str, but we assign pointers 2053 // for quick use by GetSnapshot. This avoids allocation and copying cost at Exec. 2054 c_env_vector_.reset(new char*[name_value_pairs_.size() + 1]); 2055 for (size_t i = 0; env[i] != nullptr; ++i) { 2056 c_env_vector_[i] = const_cast<char*>(name_value_pairs_[i]->c_str()); 2057 } 2058 c_env_vector_[name_value_pairs_.size()] = nullptr; 2059 } 2060 2061 char** Runtime::EnvSnapshot::GetSnapshot() const { 2062 return c_env_vector_.get(); 2063 } 2064 2065 } // namespace art 2066