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