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 #endif 24 25 #include <signal.h> 26 #include <sys/syscall.h> 27 #include <valgrind.h> 28 29 #include <cstdio> 30 #include <cstdlib> 31 #include <limits> 32 #include <memory> 33 #include <vector> 34 #include <fcntl.h> 35 36 #include "arch/arm/quick_method_frame_info_arm.h" 37 #include "arch/arm/registers_arm.h" 38 #include "arch/arm64/quick_method_frame_info_arm64.h" 39 #include "arch/arm64/registers_arm64.h" 40 #include "arch/mips/quick_method_frame_info_mips.h" 41 #include "arch/mips/registers_mips.h" 42 #include "arch/x86/quick_method_frame_info_x86.h" 43 #include "arch/x86/registers_x86.h" 44 #include "arch/x86_64/quick_method_frame_info_x86_64.h" 45 #include "arch/x86_64/registers_x86_64.h" 46 #include "atomic.h" 47 #include "class_linker.h" 48 #include "debugger.h" 49 #include "elf_file.h" 50 #include "fault_handler.h" 51 #include "gc/accounting/card_table-inl.h" 52 #include "gc/heap.h" 53 #include "gc/space/image_space.h" 54 #include "gc/space/space.h" 55 #include "image.h" 56 #include "instrumentation.h" 57 #include "intern_table.h" 58 #include "jni_internal.h" 59 #include "mirror/art_field-inl.h" 60 #include "mirror/art_method-inl.h" 61 #include "mirror/array.h" 62 #include "mirror/class-inl.h" 63 #include "mirror/class_loader.h" 64 #include "mirror/stack_trace_element.h" 65 #include "mirror/throwable.h" 66 #include "monitor.h" 67 #include "native_bridge_art_interface.h" 68 #include "parsed_options.h" 69 #include "oat_file.h" 70 #include "os.h" 71 #include "quick/quick_method_frame_info.h" 72 #include "reflection.h" 73 #include "ScopedLocalRef.h" 74 #include "scoped_thread_state_change.h" 75 #include "sigchain.h" 76 #include "signal_catcher.h" 77 #include "signal_set.h" 78 #include "handle_scope-inl.h" 79 #include "thread.h" 80 #include "thread_list.h" 81 #include "trace.h" 82 #include "transaction.h" 83 #include "profiler.h" 84 #include "verifier/method_verifier.h" 85 #include "well_known_classes.h" 86 87 #include "JniConstants.h" // Last to avoid LOG redefinition in ics-mr1-plus-art. 88 89 #ifdef HAVE_ANDROID_OS 90 #include "cutils/properties.h" 91 #endif 92 93 namespace art { 94 95 static constexpr bool kEnableJavaStackTraceHandler = false; 96 const char* Runtime::kDefaultInstructionSetFeatures = 97 STRINGIFY(ART_DEFAULT_INSTRUCTION_SET_FEATURES); 98 Runtime* Runtime::instance_ = NULL; 99 100 Runtime::Runtime() 101 : instruction_set_(kNone), 102 compiler_callbacks_(nullptr), 103 is_zygote_(false), 104 must_relocate_(false), 105 is_concurrent_gc_enabled_(true), 106 is_explicit_gc_disabled_(false), 107 dex2oat_enabled_(true), 108 image_dex2oat_enabled_(true), 109 default_stack_size_(0), 110 heap_(nullptr), 111 max_spins_before_thin_lock_inflation_(Monitor::kDefaultMaxSpinsBeforeThinLockInflation), 112 monitor_list_(nullptr), 113 monitor_pool_(nullptr), 114 thread_list_(nullptr), 115 intern_table_(nullptr), 116 class_linker_(nullptr), 117 signal_catcher_(nullptr), 118 java_vm_(nullptr), 119 fault_message_lock_("Fault message lock"), 120 fault_message_(""), 121 method_verifier_lock_("Method verifiers lock"), 122 threads_being_born_(0), 123 shutdown_cond_(new ConditionVariable("Runtime shutdown", *Locks::runtime_shutdown_lock_)), 124 shutting_down_(false), 125 shutting_down_started_(false), 126 started_(false), 127 finished_starting_(false), 128 vfprintf_(nullptr), 129 exit_(nullptr), 130 abort_(nullptr), 131 stats_enabled_(false), 132 running_on_valgrind_(RUNNING_ON_VALGRIND > 0), 133 profiler_started_(false), 134 method_trace_(false), 135 method_trace_file_size_(0), 136 instrumentation_(), 137 use_compile_time_class_path_(false), 138 main_thread_group_(nullptr), 139 system_thread_group_(nullptr), 140 system_class_loader_(nullptr), 141 dump_gc_performance_on_shutdown_(false), 142 preinitialization_transaction_(nullptr), 143 null_pointer_handler_(nullptr), 144 suspend_handler_(nullptr), 145 stack_overflow_handler_(nullptr), 146 verify_(false), 147 target_sdk_version_(0), 148 implicit_null_checks_(false), 149 implicit_so_checks_(false), 150 implicit_suspend_checks_(false), 151 is_native_bridge_loaded_(false) { 152 } 153 154 Runtime::~Runtime() { 155 if (is_native_bridge_loaded_) { 156 UnloadNativeBridge(); 157 } 158 if (dump_gc_performance_on_shutdown_) { 159 // This can't be called from the Heap destructor below because it 160 // could call RosAlloc::InspectAll() which needs the thread_list 161 // to be still alive. 162 heap_->DumpGcPerformanceInfo(LOG(INFO)); 163 } 164 165 Thread* self = Thread::Current(); 166 { 167 MutexLock mu(self, *Locks::runtime_shutdown_lock_); 168 shutting_down_started_ = true; 169 while (threads_being_born_ > 0) { 170 shutdown_cond_->Wait(self); 171 } 172 shutting_down_ = true; 173 } 174 // Shut down background profiler before the runtime exits. 175 if (profiler_started_) { 176 BackgroundMethodSamplingProfiler::Shutdown(); 177 } 178 179 Trace::Shutdown(); 180 181 // Make sure to let the GC complete if it is running. 182 heap_->WaitForGcToComplete(gc::kGcCauseBackground, self); 183 heap_->DeleteThreadPool(); 184 185 // Make sure our internal threads are dead before we start tearing down things they're using. 186 Dbg::StopJdwp(); 187 delete signal_catcher_; 188 189 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended. 190 delete thread_list_; 191 192 // Shutdown the fault manager if it was initialized. 193 fault_manager.Shutdown(); 194 195 delete monitor_list_; 196 delete monitor_pool_; 197 delete class_linker_; 198 delete heap_; 199 delete intern_table_; 200 delete java_vm_; 201 Thread::Shutdown(); 202 QuasiAtomic::Shutdown(); 203 verifier::MethodVerifier::Shutdown(); 204 MemMap::Shutdown(); 205 // TODO: acquire a static mutex on Runtime to avoid racing. 206 CHECK(instance_ == nullptr || instance_ == this); 207 instance_ = nullptr; 208 209 delete null_pointer_handler_; 210 delete suspend_handler_; 211 delete stack_overflow_handler_; 212 } 213 214 struct AbortState { 215 void Dump(std::ostream& os) NO_THREAD_SAFETY_ANALYSIS { 216 if (gAborting > 1) { 217 os << "Runtime aborting --- recursively, so no thread-specific detail!\n"; 218 return; 219 } 220 gAborting++; 221 os << "Runtime aborting...\n"; 222 if (Runtime::Current() == NULL) { 223 os << "(Runtime does not yet exist!)\n"; 224 return; 225 } 226 Thread* self = Thread::Current(); 227 if (self == nullptr) { 228 os << "(Aborting thread was not attached to runtime!)\n"; 229 DumpKernelStack(os, GetTid(), " kernel: ", false); 230 DumpNativeStack(os, GetTid(), " native: ", nullptr); 231 } else { 232 os << "Aborting thread:\n"; 233 if (Locks::mutator_lock_->IsExclusiveHeld(self) || Locks::mutator_lock_->IsSharedHeld(self)) { 234 DumpThread(os, self); 235 } else { 236 if (Locks::mutator_lock_->SharedTryLock(self)) { 237 DumpThread(os, self); 238 Locks::mutator_lock_->SharedUnlock(self); 239 } 240 } 241 } 242 DumpAllThreads(os, self); 243 } 244 245 void DumpThread(std::ostream& os, Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 246 self->Dump(os); 247 if (self->IsExceptionPending()) { 248 ThrowLocation throw_location; 249 mirror::Throwable* exception = self->GetException(&throw_location); 250 os << "Pending exception " << PrettyTypeOf(exception) 251 << " thrown by '" << throw_location.Dump() << "'\n" 252 << exception->Dump(); 253 } 254 } 255 256 void DumpAllThreads(std::ostream& os, Thread* self) NO_THREAD_SAFETY_ANALYSIS { 257 Runtime* runtime = Runtime::Current(); 258 if (runtime != nullptr) { 259 ThreadList* thread_list = runtime->GetThreadList(); 260 if (thread_list != nullptr) { 261 bool tll_already_held = Locks::thread_list_lock_->IsExclusiveHeld(self); 262 bool ml_already_held = Locks::mutator_lock_->IsSharedHeld(self); 263 if (!tll_already_held || !ml_already_held) { 264 os << "Dumping all threads without appropriate locks held:" 265 << (!tll_already_held ? " thread list lock" : "") 266 << (!ml_already_held ? " mutator lock" : "") 267 << "\n"; 268 } 269 os << "All threads:\n"; 270 thread_list->DumpLocked(os); 271 } 272 } 273 } 274 }; 275 276 void Runtime::Abort() { 277 gAborting++; // set before taking any locks 278 279 // Ensure that we don't have multiple threads trying to abort at once, 280 // which would result in significantly worse diagnostics. 281 MutexLock mu(Thread::Current(), *Locks::abort_lock_); 282 283 // Get any pending output out of the way. 284 fflush(NULL); 285 286 // Many people have difficulty distinguish aborts from crashes, 287 // so be explicit. 288 AbortState state; 289 LOG(INTERNAL_FATAL) << Dumpable<AbortState>(state); 290 291 // Call the abort hook if we have one. 292 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) { 293 LOG(INTERNAL_FATAL) << "Calling abort hook..."; 294 Runtime::Current()->abort_(); 295 // notreached 296 LOG(INTERNAL_FATAL) << "Unexpectedly returned from abort hook!"; 297 } 298 299 #if defined(__GLIBC__) 300 // TODO: we ought to be able to use pthread_kill(3) here (or abort(3), 301 // which POSIX defines in terms of raise(3), which POSIX defines in terms 302 // of pthread_kill(3)). On Linux, though, libcorkscrew can't unwind through 303 // libpthread, which means the stacks we dump would be useless. Calling 304 // tgkill(2) directly avoids that. 305 syscall(__NR_tgkill, getpid(), GetTid(), SIGABRT); 306 // TODO: LLVM installs it's own SIGABRT handler so exit to be safe... Can we disable that in LLVM? 307 // If not, we could use sigaction(3) before calling tgkill(2) and lose this call to exit(3). 308 exit(1); 309 #else 310 abort(); 311 #endif 312 // notreached 313 } 314 315 void Runtime::PreZygoteFork() { 316 heap_->PreZygoteFork(); 317 } 318 319 void Runtime::CallExitHook(jint status) { 320 if (exit_ != NULL) { 321 ScopedThreadStateChange tsc(Thread::Current(), kNative); 322 exit_(status); 323 LOG(WARNING) << "Exit hook returned instead of exiting!"; 324 } 325 } 326 327 void Runtime::SweepSystemWeaks(IsMarkedCallback* visitor, void* arg) { 328 GetInternTable()->SweepInternTableWeaks(visitor, arg); 329 GetMonitorList()->SweepMonitorList(visitor, arg); 330 GetJavaVM()->SweepJniWeakGlobals(visitor, arg); 331 } 332 333 bool Runtime::Create(const RuntimeOptions& options, bool ignore_unrecognized) { 334 // TODO: acquire a static mutex on Runtime to avoid racing. 335 if (Runtime::instance_ != NULL) { 336 return false; 337 } 338 InitLogging(NULL); // Calls Locks::Init() as a side effect. 339 instance_ = new Runtime; 340 if (!instance_->Init(options, ignore_unrecognized)) { 341 delete instance_; 342 instance_ = NULL; 343 return false; 344 } 345 return true; 346 } 347 348 jobject CreateSystemClassLoader() { 349 if (Runtime::Current()->UseCompileTimeClassPath()) { 350 return NULL; 351 } 352 353 ScopedObjectAccess soa(Thread::Current()); 354 ClassLinker* cl = Runtime::Current()->GetClassLinker(); 355 356 StackHandleScope<3> hs(soa.Self()); 357 Handle<mirror::Class> class_loader_class( 358 hs.NewHandle(soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader))); 359 CHECK(cl->EnsureInitialized(class_loader_class, true, true)); 360 361 mirror::ArtMethod* getSystemClassLoader = 362 class_loader_class->FindDirectMethod("getSystemClassLoader", "()Ljava/lang/ClassLoader;"); 363 CHECK(getSystemClassLoader != NULL); 364 365 JValue result = InvokeWithJValues(soa, nullptr, soa.EncodeMethod(getSystemClassLoader), nullptr); 366 Handle<mirror::ClassLoader> class_loader( 367 hs.NewHandle(down_cast<mirror::ClassLoader*>(result.GetL()))); 368 CHECK(class_loader.Get() != nullptr); 369 JNIEnv* env = soa.Self()->GetJniEnv(); 370 ScopedLocalRef<jobject> system_class_loader(env, 371 soa.AddLocalReference<jobject>(class_loader.Get())); 372 CHECK(system_class_loader.get() != nullptr); 373 374 soa.Self()->SetClassLoaderOverride(class_loader.Get()); 375 376 Handle<mirror::Class> thread_class( 377 hs.NewHandle(soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread))); 378 CHECK(cl->EnsureInitialized(thread_class, true, true)); 379 380 mirror::ArtField* contextClassLoader = 381 thread_class->FindDeclaredInstanceField("contextClassLoader", "Ljava/lang/ClassLoader;"); 382 CHECK(contextClassLoader != NULL); 383 384 // We can't run in a transaction yet. 385 contextClassLoader->SetObject<false>(soa.Self()->GetPeer(), class_loader.Get()); 386 387 return env->NewGlobalRef(system_class_loader.get()); 388 } 389 390 std::string Runtime::GetPatchoatExecutable() const { 391 if (!patchoat_executable_.empty()) { 392 return patchoat_executable_; 393 } 394 std::string patchoat_executable_(GetAndroidRoot()); 395 patchoat_executable_ += (kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat"); 396 return patchoat_executable_; 397 } 398 399 std::string Runtime::GetCompilerExecutable() const { 400 if (!compiler_executable_.empty()) { 401 return compiler_executable_; 402 } 403 std::string compiler_executable(GetAndroidRoot()); 404 compiler_executable += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat"); 405 return compiler_executable; 406 } 407 408 bool Runtime::Start() { 409 VLOG(startup) << "Runtime::Start entering"; 410 411 // Restore main thread state to kNative as expected by native code. 412 Thread* self = Thread::Current(); 413 414 self->TransitionFromRunnableToSuspended(kNative); 415 416 started_ = true; 417 418 if (IsZygote()) { 419 ScopedObjectAccess soa(self); 420 gc::space::ImageSpace* image_space = heap_->GetImageSpace(); 421 if (image_space != nullptr) { 422 Runtime::Current()->GetInternTable()->AddImageStringsToTable(image_space); 423 Runtime::Current()->GetClassLinker()->MoveImageClassesToClassTable(); 424 } 425 } 426 427 if (!IsImageDex2OatEnabled() || !Runtime::Current()->GetHeap()->HasImageSpace()) { 428 ScopedObjectAccess soa(self); 429 StackHandleScope<1> hs(soa.Self()); 430 auto klass(hs.NewHandle<mirror::Class>(mirror::Class::GetJavaLangClass())); 431 class_linker_->EnsureInitialized(klass, true, true); 432 } 433 434 // InitNativeMethods needs to be after started_ so that the classes 435 // it touches will have methods linked to the oat file if necessary. 436 InitNativeMethods(); 437 438 // Initialize well known thread group values that may be accessed threads while attaching. 439 InitThreadGroups(self); 440 441 Thread::FinishStartup(); 442 443 system_class_loader_ = CreateSystemClassLoader(); 444 445 if (is_zygote_) { 446 if (!InitZygote()) { 447 return false; 448 } 449 } else { 450 if (is_native_bridge_loaded_) { 451 PreInitializeNativeBridge("."); 452 } 453 DidForkFromZygote(self->GetJniEnv(), NativeBridgeAction::kInitialize, 454 GetInstructionSetString(kRuntimeISA)); 455 } 456 457 StartDaemonThreads(); 458 459 { 460 ScopedObjectAccess soa(self); 461 self->GetJniEnv()->locals.AssertEmpty(); 462 } 463 464 VLOG(startup) << "Runtime::Start exiting"; 465 finished_starting_ = true; 466 467 if (profiler_options_.IsEnabled() && !profile_output_filename_.empty()) { 468 // User has asked for a profile using -Xenable-profiler. 469 // Create the profile file if it doesn't exist. 470 int fd = open(profile_output_filename_.c_str(), O_RDWR|O_CREAT|O_EXCL, 0660); 471 if (fd >= 0) { 472 close(fd); 473 } else if (errno != EEXIST) { 474 LOG(INFO) << "Failed to access the profile file. Profiler disabled."; 475 return true; 476 } 477 StartProfiler(profile_output_filename_.c_str()); 478 } 479 480 return true; 481 } 482 483 void Runtime::EndThreadBirth() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) { 484 DCHECK_GT(threads_being_born_, 0U); 485 threads_being_born_--; 486 if (shutting_down_started_ && threads_being_born_ == 0) { 487 shutdown_cond_->Broadcast(Thread::Current()); 488 } 489 } 490 491 // Do zygote-mode-only initialization. 492 bool Runtime::InitZygote() { 493 #ifdef __linux__ 494 // zygote goes into its own process group 495 setpgid(0, 0); 496 497 // See storage config details at http://source.android.com/tech/storage/ 498 // Create private mount namespace shared by all children 499 if (unshare(CLONE_NEWNS) == -1) { 500 PLOG(WARNING) << "Failed to unshare()"; 501 return false; 502 } 503 504 // Mark rootfs as being a slave so that changes from default 505 // namespace only flow into our children. 506 if (mount("rootfs", "/", NULL, (MS_SLAVE | MS_REC), NULL) == -1) { 507 PLOG(WARNING) << "Failed to mount() rootfs as MS_SLAVE"; 508 return false; 509 } 510 511 // Create a staging tmpfs that is shared by our children; they will 512 // bind mount storage into their respective private namespaces, which 513 // are isolated from each other. 514 const char* target_base = getenv("EMULATED_STORAGE_TARGET"); 515 if (target_base != NULL) { 516 if (mount("tmpfs", target_base, "tmpfs", MS_NOSUID | MS_NODEV, 517 "uid=0,gid=1028,mode=0751") == -1) { 518 LOG(WARNING) << "Failed to mount tmpfs to " << target_base; 519 return false; 520 } 521 } 522 523 return true; 524 #else 525 UNIMPLEMENTED(FATAL); 526 return false; 527 #endif 528 } 529 530 void Runtime::DidForkFromZygote(JNIEnv* env, NativeBridgeAction action, const char* isa) { 531 is_zygote_ = false; 532 533 if (is_native_bridge_loaded_) { 534 switch (action) { 535 case NativeBridgeAction::kUnload: 536 UnloadNativeBridge(); 537 is_native_bridge_loaded_ = false; 538 break; 539 540 case NativeBridgeAction::kInitialize: 541 InitializeNativeBridge(env, isa); 542 break; 543 } 544 } 545 546 // Create the thread pool. 547 heap_->CreateThreadPool(); 548 549 StartSignalCatcher(); 550 551 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y", 552 // this will pause the runtime, so we probably want this to come last. 553 Dbg::StartJdwp(); 554 } 555 556 void Runtime::StartSignalCatcher() { 557 if (!is_zygote_) { 558 signal_catcher_ = new SignalCatcher(stack_trace_file_); 559 } 560 } 561 562 bool Runtime::IsShuttingDown(Thread* self) { 563 MutexLock mu(self, *Locks::runtime_shutdown_lock_); 564 return IsShuttingDownLocked(); 565 } 566 567 void Runtime::StartDaemonThreads() { 568 VLOG(startup) << "Runtime::StartDaemonThreads entering"; 569 570 Thread* self = Thread::Current(); 571 572 // Must be in the kNative state for calling native methods. 573 CHECK_EQ(self->GetState(), kNative); 574 575 JNIEnv* env = self->GetJniEnv(); 576 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, 577 WellKnownClasses::java_lang_Daemons_start); 578 if (env->ExceptionCheck()) { 579 env->ExceptionDescribe(); 580 LOG(FATAL) << "Error starting java.lang.Daemons"; 581 } 582 583 VLOG(startup) << "Runtime::StartDaemonThreads exiting"; 584 } 585 586 static bool OpenDexFilesFromImage(const std::vector<std::string>& dex_filenames, 587 const std::string& image_location, 588 std::vector<const DexFile*>& dex_files, 589 size_t* failures) { 590 std::string system_filename; 591 bool has_system = false; 592 std::string cache_filename_unused; 593 bool dalvik_cache_exists_unused; 594 bool has_cache_unused; 595 bool is_global_cache_unused; 596 bool found_image = gc::space::ImageSpace::FindImageFilename(image_location.c_str(), 597 kRuntimeISA, 598 &system_filename, 599 &has_system, 600 &cache_filename_unused, 601 &dalvik_cache_exists_unused, 602 &has_cache_unused, 603 &is_global_cache_unused); 604 *failures = 0; 605 if (!found_image || !has_system) { 606 return false; 607 } 608 std::string error_msg; 609 // We are falling back to non-executable use of the oat file because patching failed, presumably 610 // due to lack of space. 611 std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(system_filename.c_str()); 612 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_location.c_str()); 613 std::unique_ptr<File> file(OS::OpenFileForReading(oat_filename.c_str())); 614 if (file.get() == nullptr) { 615 return false; 616 } 617 std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file.release(), false, false, &error_msg)); 618 if (elf_file.get() == nullptr) { 619 return false; 620 } 621 std::unique_ptr<OatFile> oat_file(OatFile::OpenWithElfFile(elf_file.release(), oat_location, 622 &error_msg)); 623 if (oat_file.get() == nullptr) { 624 LOG(INFO) << "Unable to use '" << oat_filename << "' because " << error_msg; 625 return false; 626 } 627 628 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { 629 if (oat_dex_file == nullptr) { 630 *failures += 1; 631 continue; 632 } 633 const DexFile* dex_file = oat_dex_file->OpenDexFile(&error_msg); 634 if (dex_file == nullptr) { 635 *failures += 1; 636 } else { 637 dex_files.push_back(dex_file); 638 } 639 } 640 Runtime::Current()->GetClassLinker()->RegisterOatFile(oat_file.release()); 641 return true; 642 } 643 644 645 static size_t OpenDexFiles(const std::vector<std::string>& dex_filenames, 646 const std::string& image_location, 647 std::vector<const DexFile*>& dex_files) { 648 size_t failure_count = 0; 649 if (!image_location.empty() && OpenDexFilesFromImage(dex_filenames, image_location, dex_files, 650 &failure_count)) { 651 return failure_count; 652 } 653 failure_count = 0; 654 for (size_t i = 0; i < dex_filenames.size(); i++) { 655 const char* dex_filename = dex_filenames[i].c_str(); 656 std::string error_msg; 657 if (!OS::FileExists(dex_filename)) { 658 LOG(WARNING) << "Skipping non-existent dex file '" << dex_filename << "'"; 659 continue; 660 } 661 if (!DexFile::Open(dex_filename, dex_filename, &error_msg, &dex_files)) { 662 LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "': " << error_msg; 663 ++failure_count; 664 } 665 } 666 return failure_count; 667 } 668 669 bool Runtime::Init(const RuntimeOptions& raw_options, bool ignore_unrecognized) { 670 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize); 671 672 MemMap::Init(); 673 674 std::unique_ptr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized)); 675 if (options.get() == nullptr) { 676 LOG(ERROR) << "Failed to parse options"; 677 return false; 678 } 679 VLOG(startup) << "Runtime::Init -verbose:startup enabled"; 680 681 QuasiAtomic::Startup(); 682 683 Monitor::Init(options->lock_profiling_threshold_, options->hook_is_sensitive_thread_); 684 685 boot_class_path_string_ = options->boot_class_path_string_; 686 class_path_string_ = options->class_path_string_; 687 properties_ = options->properties_; 688 689 compiler_callbacks_ = options->compiler_callbacks_; 690 patchoat_executable_ = options->patchoat_executable_; 691 must_relocate_ = options->must_relocate_; 692 is_zygote_ = options->is_zygote_; 693 is_explicit_gc_disabled_ = options->is_explicit_gc_disabled_; 694 dex2oat_enabled_ = options->dex2oat_enabled_; 695 image_dex2oat_enabled_ = options->image_dex2oat_enabled_; 696 697 vfprintf_ = options->hook_vfprintf_; 698 exit_ = options->hook_exit_; 699 abort_ = options->hook_abort_; 700 701 default_stack_size_ = options->stack_size_; 702 stack_trace_file_ = options->stack_trace_file_; 703 704 compiler_executable_ = options->compiler_executable_; 705 compiler_options_ = options->compiler_options_; 706 image_compiler_options_ = options->image_compiler_options_; 707 image_location_ = options->image_; 708 709 max_spins_before_thin_lock_inflation_ = options->max_spins_before_thin_lock_inflation_; 710 711 monitor_list_ = new MonitorList; 712 monitor_pool_ = MonitorPool::Create(); 713 thread_list_ = new ThreadList; 714 intern_table_ = new InternTable; 715 716 verify_ = options->verify_; 717 718 if (options->interpreter_only_) { 719 GetInstrumentation()->ForceInterpretOnly(); 720 } 721 722 heap_ = new gc::Heap(options->heap_initial_size_, 723 options->heap_growth_limit_, 724 options->heap_min_free_, 725 options->heap_max_free_, 726 options->heap_target_utilization_, 727 options->foreground_heap_growth_multiplier_, 728 options->heap_maximum_size_, 729 options->heap_non_moving_space_capacity_, 730 options->image_, 731 options->image_isa_, 732 options->collector_type_, 733 options->background_collector_type_, 734 options->parallel_gc_threads_, 735 options->conc_gc_threads_, 736 options->low_memory_mode_, 737 options->long_pause_log_threshold_, 738 options->long_gc_log_threshold_, 739 options->ignore_max_footprint_, 740 options->use_tlab_, 741 options->verify_pre_gc_heap_, 742 options->verify_pre_sweeping_heap_, 743 options->verify_post_gc_heap_, 744 options->verify_pre_gc_rosalloc_, 745 options->verify_pre_sweeping_rosalloc_, 746 options->verify_post_gc_rosalloc_, 747 options->use_homogeneous_space_compaction_for_oom_, 748 options->min_interval_homogeneous_space_compaction_by_oom_); 749 750 dump_gc_performance_on_shutdown_ = options->dump_gc_performance_on_shutdown_; 751 752 BlockSignals(); 753 InitPlatformSignalHandlers(); 754 755 // Change the implicit checks flags based on runtime architecture. 756 switch (kRuntimeISA) { 757 case kArm: 758 case kThumb2: 759 case kX86: 760 case kArm64: 761 case kX86_64: 762 implicit_null_checks_ = true; 763 implicit_so_checks_ = (RUNNING_ON_VALGRIND == 0); 764 break; 765 default: 766 // Keep the defaults. 767 break; 768 } 769 770 // Always initialize the signal chain so that any calls to sigaction get 771 // correctly routed to the next in the chain regardless of whether we 772 // have claimed the signal or not. 773 InitializeSignalChain(); 774 775 if (implicit_null_checks_ || implicit_so_checks_ || implicit_suspend_checks_) { 776 fault_manager.Init(); 777 778 // These need to be in a specific order. The null point check handler must be 779 // after the suspend check and stack overflow check handlers. 780 if (implicit_suspend_checks_) { 781 suspend_handler_ = new SuspensionHandler(&fault_manager); 782 } 783 784 if (implicit_so_checks_) { 785 stack_overflow_handler_ = new StackOverflowHandler(&fault_manager); 786 } 787 788 if (implicit_null_checks_) { 789 null_pointer_handler_ = new NullPointerHandler(&fault_manager); 790 } 791 792 if (kEnableJavaStackTraceHandler) { 793 new JavaStackTraceHandler(&fault_manager); 794 } 795 } 796 797 java_vm_ = new JavaVMExt(this, options.get()); 798 799 Thread::Startup(); 800 801 // ClassLinker needs an attached thread, but we can't fully attach a thread without creating 802 // objects. We can't supply a thread group yet; it will be fixed later. Since we are the main 803 // thread, we do not get a java peer. 804 Thread* self = Thread::Attach("main", false, nullptr, false); 805 CHECK_EQ(self->GetThreadId(), ThreadList::kMainThreadId); 806 CHECK(self != nullptr); 807 808 // Set us to runnable so tools using a runtime can allocate and GC by default 809 self->TransitionFromSuspendedToRunnable(); 810 811 // Now we're attached, we can take the heap locks and validate the heap. 812 GetHeap()->EnableObjectValidation(); 813 814 CHECK_GE(GetHeap()->GetContinuousSpaces().size(), 1U); 815 class_linker_ = new ClassLinker(intern_table_); 816 if (GetHeap()->HasImageSpace()) { 817 class_linker_->InitFromImage(); 818 if (kIsDebugBuild) { 819 GetHeap()->GetImageSpace()->VerifyImageAllocations(); 820 } 821 } else if (!IsCompiler() || !image_dex2oat_enabled_) { 822 std::vector<std::string> dex_filenames; 823 Split(boot_class_path_string_, ':', dex_filenames); 824 std::vector<const DexFile*> boot_class_path; 825 OpenDexFiles(dex_filenames, options->image_, boot_class_path); 826 class_linker_->InitWithoutImage(boot_class_path); 827 // TODO: Should we move the following to InitWithoutImage? 828 SetInstructionSet(kRuntimeISA); 829 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { 830 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); 831 if (!HasCalleeSaveMethod(type)) { 832 SetCalleeSaveMethod(CreateCalleeSaveMethod(type), type); 833 } 834 } 835 } else { 836 CHECK(options->boot_class_path_ != nullptr); 837 CHECK_NE(options->boot_class_path_->size(), 0U); 838 class_linker_->InitWithoutImage(*options->boot_class_path_); 839 } 840 CHECK(class_linker_ != nullptr); 841 verifier::MethodVerifier::Init(); 842 843 method_trace_ = options->method_trace_; 844 method_trace_file_ = options->method_trace_file_; 845 method_trace_file_size_ = options->method_trace_file_size_; 846 847 profile_output_filename_ = options->profile_output_filename_; 848 profiler_options_ = options->profiler_options_; 849 850 // TODO: move this to just be an Trace::Start argument 851 Trace::SetDefaultClockSource(options->profile_clock_source_); 852 853 if (options->method_trace_) { 854 ScopedThreadStateChange tsc(self, kWaitingForMethodTracingStart); 855 Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0, 856 false, false, 0); 857 } 858 859 // Pre-allocate an OutOfMemoryError for the double-OOME case. 860 self->ThrowNewException(ThrowLocation(), "Ljava/lang/OutOfMemoryError;", 861 "OutOfMemoryError thrown while trying to throw OutOfMemoryError; " 862 "no stack available"); 863 pre_allocated_OutOfMemoryError_ = GcRoot<mirror::Throwable>(self->GetException(NULL)); 864 self->ClearException(); 865 866 // Pre-allocate a NoClassDefFoundError for the common case of failing to find a system class 867 // ahead of checking the application's class loader. 868 self->ThrowNewException(ThrowLocation(), "Ljava/lang/NoClassDefFoundError;", 869 "Class not found using the boot class loader; no stack available"); 870 pre_allocated_NoClassDefFoundError_ = GcRoot<mirror::Throwable>(self->GetException(NULL)); 871 self->ClearException(); 872 873 // Look for a native bridge. 874 // 875 // The intended flow here is, in the case of a running system: 876 // 877 // Runtime::Init() (zygote): 878 // LoadNativeBridge -> dlopen from cmd line parameter. 879 // | 880 // V 881 // Runtime::Start() (zygote): 882 // No-op wrt native bridge. 883 // | 884 // | start app 885 // V 886 // DidForkFromZygote(action) 887 // action = kUnload -> dlclose native bridge. 888 // action = kInitialize -> initialize library 889 // 890 // 891 // The intended flow here is, in the case of a simple dalvikvm call: 892 // 893 // Runtime::Init(): 894 // LoadNativeBridge -> dlopen from cmd line parameter. 895 // | 896 // V 897 // Runtime::Start(): 898 // DidForkFromZygote(kInitialize) -> try to initialize any native bridge given. 899 // No-op wrt native bridge. 900 is_native_bridge_loaded_ = LoadNativeBridge(options->native_bridge_library_filename_); 901 902 VLOG(startup) << "Runtime::Init exiting"; 903 return true; 904 } 905 906 void Runtime::InitNativeMethods() { 907 VLOG(startup) << "Runtime::InitNativeMethods entering"; 908 Thread* self = Thread::Current(); 909 JNIEnv* env = self->GetJniEnv(); 910 911 // Must be in the kNative state for calling native methods (JNI_OnLoad code). 912 CHECK_EQ(self->GetState(), kNative); 913 914 // First set up JniConstants, which is used by both the runtime's built-in native 915 // methods and libcore. 916 JniConstants::init(env); 917 WellKnownClasses::Init(env); 918 919 // Then set up the native methods provided by the runtime itself. 920 RegisterRuntimeNativeMethods(env); 921 922 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad. 923 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's 924 // the library that implements System.loadLibrary! 925 { 926 std::string mapped_name(StringPrintf(OS_SHARED_LIB_FORMAT_STR, "javacore")); 927 std::string reason; 928 self->TransitionFromSuspendedToRunnable(); 929 StackHandleScope<1> hs(self); 930 auto class_loader(hs.NewHandle<mirror::ClassLoader>(nullptr)); 931 if (!instance_->java_vm_->LoadNativeLibrary(mapped_name, class_loader, &reason)) { 932 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " << reason; 933 } 934 self->TransitionFromRunnableToSuspended(kNative); 935 } 936 937 // Initialize well known classes that may invoke runtime native methods. 938 WellKnownClasses::LateInit(env); 939 940 VLOG(startup) << "Runtime::InitNativeMethods exiting"; 941 } 942 943 void Runtime::InitThreadGroups(Thread* self) { 944 JNIEnvExt* env = self->GetJniEnv(); 945 ScopedJniEnvLocalRefState env_state(env); 946 main_thread_group_ = 947 env->NewGlobalRef(env->GetStaticObjectField( 948 WellKnownClasses::java_lang_ThreadGroup, 949 WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup)); 950 CHECK(main_thread_group_ != NULL || IsCompiler()); 951 system_thread_group_ = 952 env->NewGlobalRef(env->GetStaticObjectField( 953 WellKnownClasses::java_lang_ThreadGroup, 954 WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup)); 955 CHECK(system_thread_group_ != NULL || IsCompiler()); 956 } 957 958 jobject Runtime::GetMainThreadGroup() const { 959 CHECK(main_thread_group_ != NULL || IsCompiler()); 960 return main_thread_group_; 961 } 962 963 jobject Runtime::GetSystemThreadGroup() const { 964 CHECK(system_thread_group_ != NULL || IsCompiler()); 965 return system_thread_group_; 966 } 967 968 jobject Runtime::GetSystemClassLoader() const { 969 CHECK(system_class_loader_ != NULL || IsCompiler()); 970 return system_class_loader_; 971 } 972 973 void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) { 974 #define REGISTER(FN) extern void FN(JNIEnv*); FN(env) 975 // Register Throwable first so that registration of other native methods can throw exceptions 976 REGISTER(register_java_lang_Throwable); 977 REGISTER(register_dalvik_system_DexFile); 978 REGISTER(register_dalvik_system_VMDebug); 979 REGISTER(register_dalvik_system_VMRuntime); 980 REGISTER(register_dalvik_system_VMStack); 981 REGISTER(register_dalvik_system_ZygoteHooks); 982 REGISTER(register_java_lang_Class); 983 REGISTER(register_java_lang_DexCache); 984 REGISTER(register_java_lang_Object); 985 REGISTER(register_java_lang_Runtime); 986 REGISTER(register_java_lang_String); 987 REGISTER(register_java_lang_System); 988 REGISTER(register_java_lang_Thread); 989 REGISTER(register_java_lang_VMClassLoader); 990 REGISTER(register_java_lang_ref_FinalizerReference); 991 REGISTER(register_java_lang_ref_Reference); 992 REGISTER(register_java_lang_reflect_Array); 993 REGISTER(register_java_lang_reflect_Constructor); 994 REGISTER(register_java_lang_reflect_Field); 995 REGISTER(register_java_lang_reflect_Method); 996 REGISTER(register_java_lang_reflect_Proxy); 997 REGISTER(register_java_util_concurrent_atomic_AtomicLong); 998 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer); 999 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal); 1000 REGISTER(register_sun_misc_Unsafe); 1001 #undef REGISTER 1002 } 1003 1004 void Runtime::DumpForSigQuit(std::ostream& os) { 1005 GetClassLinker()->DumpForSigQuit(os); 1006 GetInternTable()->DumpForSigQuit(os); 1007 GetJavaVM()->DumpForSigQuit(os); 1008 GetHeap()->DumpForSigQuit(os); 1009 TrackedAllocators::Dump(os); 1010 os << "\n"; 1011 1012 thread_list_->DumpForSigQuit(os); 1013 BaseMutex::DumpAll(os); 1014 } 1015 1016 void Runtime::DumpLockHolders(std::ostream& os) { 1017 uint64_t mutator_lock_owner = Locks::mutator_lock_->GetExclusiveOwnerTid(); 1018 pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner(); 1019 pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner(); 1020 pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner(); 1021 if ((thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) { 1022 os << "Mutator lock exclusive owner tid: " << mutator_lock_owner << "\n" 1023 << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n" 1024 << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n" 1025 << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n"; 1026 } 1027 } 1028 1029 void Runtime::SetStatsEnabled(bool new_state) { 1030 Thread* self = Thread::Current(); 1031 MutexLock mu(self, *Locks::instrument_entrypoints_lock_); 1032 if (new_state == true) { 1033 GetStats()->Clear(~0); 1034 // TODO: wouldn't it make more sense to clear _all_ threads' stats? 1035 self->GetStats()->Clear(~0); 1036 if (stats_enabled_ != new_state) { 1037 GetInstrumentation()->InstrumentQuickAllocEntryPointsLocked(); 1038 } 1039 } else if (stats_enabled_ != new_state) { 1040 GetInstrumentation()->UninstrumentQuickAllocEntryPointsLocked(); 1041 } 1042 stats_enabled_ = new_state; 1043 } 1044 1045 void Runtime::ResetStats(int kinds) { 1046 GetStats()->Clear(kinds & 0xffff); 1047 // TODO: wouldn't it make more sense to clear _all_ threads' stats? 1048 Thread::Current()->GetStats()->Clear(kinds >> 16); 1049 } 1050 1051 int32_t Runtime::GetStat(int kind) { 1052 RuntimeStats* stats; 1053 if (kind < (1<<16)) { 1054 stats = GetStats(); 1055 } else { 1056 stats = Thread::Current()->GetStats(); 1057 kind >>= 16; 1058 } 1059 switch (kind) { 1060 case KIND_ALLOCATED_OBJECTS: 1061 return stats->allocated_objects; 1062 case KIND_ALLOCATED_BYTES: 1063 return stats->allocated_bytes; 1064 case KIND_FREED_OBJECTS: 1065 return stats->freed_objects; 1066 case KIND_FREED_BYTES: 1067 return stats->freed_bytes; 1068 case KIND_GC_INVOCATIONS: 1069 return stats->gc_for_alloc_count; 1070 case KIND_CLASS_INIT_COUNT: 1071 return stats->class_init_count; 1072 case KIND_CLASS_INIT_TIME: 1073 // Convert ns to us, reduce to 32 bits. 1074 return static_cast<int>(stats->class_init_time_ns / 1000); 1075 case KIND_EXT_ALLOCATED_OBJECTS: 1076 case KIND_EXT_ALLOCATED_BYTES: 1077 case KIND_EXT_FREED_OBJECTS: 1078 case KIND_EXT_FREED_BYTES: 1079 return 0; // backward compatibility 1080 default: 1081 LOG(FATAL) << "Unknown statistic " << kind; 1082 return -1; // unreachable 1083 } 1084 } 1085 1086 void Runtime::BlockSignals() { 1087 SignalSet signals; 1088 signals.Add(SIGPIPE); 1089 // SIGQUIT is used to dump the runtime's state (including stack traces). 1090 signals.Add(SIGQUIT); 1091 // SIGUSR1 is used to initiate a GC. 1092 signals.Add(SIGUSR1); 1093 signals.Block(); 1094 } 1095 1096 bool Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group, 1097 bool create_peer) { 1098 return Thread::Attach(thread_name, as_daemon, thread_group, create_peer) != NULL; 1099 } 1100 1101 void Runtime::DetachCurrentThread() { 1102 Thread* self = Thread::Current(); 1103 if (self == NULL) { 1104 LOG(FATAL) << "attempting to detach thread that is not attached"; 1105 } 1106 if (self->HasManagedStack()) { 1107 LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code"; 1108 } 1109 thread_list_->Unregister(self); 1110 } 1111 1112 mirror::Throwable* Runtime::GetPreAllocatedOutOfMemoryError() { 1113 mirror::Throwable* oome = pre_allocated_OutOfMemoryError_.Read(); 1114 if (oome == nullptr) { 1115 LOG(ERROR) << "Failed to return pre-allocated OOME"; 1116 } 1117 return oome; 1118 } 1119 1120 mirror::Throwable* Runtime::GetPreAllocatedNoClassDefFoundError() { 1121 mirror::Throwable* ncdfe = pre_allocated_NoClassDefFoundError_.Read(); 1122 if (ncdfe == nullptr) { 1123 LOG(ERROR) << "Failed to return pre-allocated NoClassDefFoundError"; 1124 } 1125 return ncdfe; 1126 } 1127 1128 void Runtime::VisitConstantRoots(RootCallback* callback, void* arg) { 1129 // Visit the classes held as static in mirror classes, these can be visited concurrently and only 1130 // need to be visited once per GC since they never change. 1131 mirror::ArtField::VisitRoots(callback, arg); 1132 mirror::ArtMethod::VisitRoots(callback, arg); 1133 mirror::Class::VisitRoots(callback, arg); 1134 mirror::Reference::VisitRoots(callback, arg); 1135 mirror::StackTraceElement::VisitRoots(callback, arg); 1136 mirror::String::VisitRoots(callback, arg); 1137 mirror::Throwable::VisitRoots(callback, arg); 1138 // Visit all the primitive array types classes. 1139 mirror::PrimitiveArray<uint8_t>::VisitRoots(callback, arg); // BooleanArray 1140 mirror::PrimitiveArray<int8_t>::VisitRoots(callback, arg); // ByteArray 1141 mirror::PrimitiveArray<uint16_t>::VisitRoots(callback, arg); // CharArray 1142 mirror::PrimitiveArray<double>::VisitRoots(callback, arg); // DoubleArray 1143 mirror::PrimitiveArray<float>::VisitRoots(callback, arg); // FloatArray 1144 mirror::PrimitiveArray<int32_t>::VisitRoots(callback, arg); // IntArray 1145 mirror::PrimitiveArray<int64_t>::VisitRoots(callback, arg); // LongArray 1146 mirror::PrimitiveArray<int16_t>::VisitRoots(callback, arg); // ShortArray 1147 } 1148 1149 void Runtime::VisitConcurrentRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { 1150 intern_table_->VisitRoots(callback, arg, flags); 1151 class_linker_->VisitRoots(callback, arg, flags); 1152 if ((flags & kVisitRootFlagNewRoots) == 0) { 1153 // Guaranteed to have no new roots in the constant roots. 1154 VisitConstantRoots(callback, arg); 1155 } 1156 } 1157 1158 void Runtime::VisitNonThreadRoots(RootCallback* callback, void* arg) { 1159 java_vm_->VisitRoots(callback, arg); 1160 pre_allocated_OutOfMemoryError_.VisitRootIfNonNull(callback, arg, RootInfo(kRootVMInternal)); 1161 resolution_method_.VisitRoot(callback, arg, RootInfo(kRootVMInternal)); 1162 pre_allocated_NoClassDefFoundError_.VisitRootIfNonNull(callback, arg, RootInfo(kRootVMInternal)); 1163 imt_conflict_method_.VisitRootIfNonNull(callback, arg, RootInfo(kRootVMInternal)); 1164 imt_unimplemented_method_.VisitRootIfNonNull(callback, arg, RootInfo(kRootVMInternal)); 1165 default_imt_.VisitRootIfNonNull(callback, arg, RootInfo(kRootVMInternal)); 1166 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { 1167 callee_save_methods_[i].VisitRootIfNonNull(callback, arg, RootInfo(kRootVMInternal)); 1168 } 1169 verifier::MethodVerifier::VisitStaticRoots(callback, arg); 1170 { 1171 MutexLock mu(Thread::Current(), method_verifier_lock_); 1172 for (verifier::MethodVerifier* verifier : method_verifiers_) { 1173 verifier->VisitRoots(callback, arg); 1174 } 1175 } 1176 if (preinitialization_transaction_ != nullptr) { 1177 preinitialization_transaction_->VisitRoots(callback, arg); 1178 } 1179 instrumentation_.VisitRoots(callback, arg); 1180 } 1181 1182 void Runtime::VisitNonConcurrentRoots(RootCallback* callback, void* arg) { 1183 thread_list_->VisitRoots(callback, arg); 1184 VisitNonThreadRoots(callback, arg); 1185 } 1186 1187 void Runtime::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) { 1188 VisitNonConcurrentRoots(callback, arg); 1189 VisitConcurrentRoots(callback, arg, flags); 1190 } 1191 1192 mirror::ObjectArray<mirror::ArtMethod>* Runtime::CreateDefaultImt(ClassLinker* cl) { 1193 Thread* self = Thread::Current(); 1194 StackHandleScope<1> hs(self); 1195 Handle<mirror::ObjectArray<mirror::ArtMethod>> imtable( 1196 hs.NewHandle(cl->AllocArtMethodArray(self, 64))); 1197 mirror::ArtMethod* imt_conflict_method = Runtime::Current()->GetImtConflictMethod(); 1198 for (size_t i = 0; i < static_cast<size_t>(imtable->GetLength()); i++) { 1199 imtable->Set<false>(i, imt_conflict_method); 1200 } 1201 return imtable.Get(); 1202 } 1203 1204 mirror::ArtMethod* Runtime::CreateImtConflictMethod() { 1205 Thread* self = Thread::Current(); 1206 Runtime* runtime = Runtime::Current(); 1207 ClassLinker* class_linker = runtime->GetClassLinker(); 1208 StackHandleScope<1> hs(self); 1209 Handle<mirror::ArtMethod> method(hs.NewHandle(class_linker->AllocArtMethod(self))); 1210 method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod()); 1211 // TODO: use a special method for imt conflict method saves. 1212 method->SetDexMethodIndex(DexFile::kDexNoIndex); 1213 // When compiling, the code pointer will get set later when the image is loaded. 1214 if (runtime->IsCompiler()) { 1215 #if defined(ART_USE_PORTABLE_COMPILER) 1216 method->SetEntryPointFromPortableCompiledCode(nullptr); 1217 #endif 1218 method->SetEntryPointFromQuickCompiledCode(nullptr); 1219 } else { 1220 #if defined(ART_USE_PORTABLE_COMPILER) 1221 method->SetEntryPointFromPortableCompiledCode(class_linker->GetPortableImtConflictTrampoline()); 1222 #endif 1223 method->SetEntryPointFromQuickCompiledCode(class_linker->GetQuickImtConflictTrampoline()); 1224 } 1225 return method.Get(); 1226 } 1227 1228 mirror::ArtMethod* Runtime::CreateResolutionMethod() { 1229 Thread* self = Thread::Current(); 1230 Runtime* runtime = Runtime::Current(); 1231 ClassLinker* class_linker = runtime->GetClassLinker(); 1232 StackHandleScope<1> hs(self); 1233 Handle<mirror::ArtMethod> method(hs.NewHandle(class_linker->AllocArtMethod(self))); 1234 method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod()); 1235 // TODO: use a special method for resolution method saves 1236 method->SetDexMethodIndex(DexFile::kDexNoIndex); 1237 // When compiling, the code pointer will get set later when the image is loaded. 1238 if (runtime->IsCompiler()) { 1239 #if defined(ART_USE_PORTABLE_COMPILER) 1240 method->SetEntryPointFromPortableCompiledCode(nullptr); 1241 #endif 1242 method->SetEntryPointFromQuickCompiledCode(nullptr); 1243 } else { 1244 #if defined(ART_USE_PORTABLE_COMPILER) 1245 method->SetEntryPointFromPortableCompiledCode(class_linker->GetPortableResolutionTrampoline()); 1246 #endif 1247 method->SetEntryPointFromQuickCompiledCode(class_linker->GetQuickResolutionTrampoline()); 1248 } 1249 return method.Get(); 1250 } 1251 1252 mirror::ArtMethod* Runtime::CreateCalleeSaveMethod(CalleeSaveType type) { 1253 Thread* self = Thread::Current(); 1254 Runtime* runtime = Runtime::Current(); 1255 ClassLinker* class_linker = runtime->GetClassLinker(); 1256 StackHandleScope<1> hs(self); 1257 Handle<mirror::ArtMethod> method(hs.NewHandle(class_linker->AllocArtMethod(self))); 1258 method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod()); 1259 // TODO: use a special method for callee saves 1260 method->SetDexMethodIndex(DexFile::kDexNoIndex); 1261 #if defined(ART_USE_PORTABLE_COMPILER) 1262 method->SetEntryPointFromPortableCompiledCode(nullptr); 1263 #endif 1264 method->SetEntryPointFromQuickCompiledCode(nullptr); 1265 DCHECK_NE(instruction_set_, kNone); 1266 return method.Get(); 1267 } 1268 1269 void Runtime::DisallowNewSystemWeaks() { 1270 monitor_list_->DisallowNewMonitors(); 1271 intern_table_->DisallowNewInterns(); 1272 java_vm_->DisallowNewWeakGlobals(); 1273 } 1274 1275 void Runtime::AllowNewSystemWeaks() { 1276 monitor_list_->AllowNewMonitors(); 1277 intern_table_->AllowNewInterns(); 1278 java_vm_->AllowNewWeakGlobals(); 1279 } 1280 1281 void Runtime::SetInstructionSet(InstructionSet instruction_set) { 1282 instruction_set_ = instruction_set; 1283 if ((instruction_set_ == kThumb2) || (instruction_set_ == kArm)) { 1284 for (int i = 0; i != kLastCalleeSaveType; ++i) { 1285 CalleeSaveType type = static_cast<CalleeSaveType>(i); 1286 callee_save_method_frame_infos_[i] = arm::ArmCalleeSaveMethodFrameInfo(type); 1287 } 1288 } else if (instruction_set_ == kMips) { 1289 for (int i = 0; i != kLastCalleeSaveType; ++i) { 1290 CalleeSaveType type = static_cast<CalleeSaveType>(i); 1291 callee_save_method_frame_infos_[i] = mips::MipsCalleeSaveMethodFrameInfo(type); 1292 } 1293 } else if (instruction_set_ == kX86) { 1294 for (int i = 0; i != kLastCalleeSaveType; ++i) { 1295 CalleeSaveType type = static_cast<CalleeSaveType>(i); 1296 callee_save_method_frame_infos_[i] = x86::X86CalleeSaveMethodFrameInfo(type); 1297 } 1298 } else if (instruction_set_ == kX86_64) { 1299 for (int i = 0; i != kLastCalleeSaveType; ++i) { 1300 CalleeSaveType type = static_cast<CalleeSaveType>(i); 1301 callee_save_method_frame_infos_[i] = x86_64::X86_64CalleeSaveMethodFrameInfo(type); 1302 } 1303 } else if (instruction_set_ == kArm64) { 1304 for (int i = 0; i != kLastCalleeSaveType; ++i) { 1305 CalleeSaveType type = static_cast<CalleeSaveType>(i); 1306 callee_save_method_frame_infos_[i] = arm64::Arm64CalleeSaveMethodFrameInfo(type); 1307 } 1308 } else { 1309 UNIMPLEMENTED(FATAL) << instruction_set_; 1310 } 1311 } 1312 1313 void Runtime::SetCalleeSaveMethod(mirror::ArtMethod* method, CalleeSaveType type) { 1314 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType)); 1315 callee_save_methods_[type] = GcRoot<mirror::ArtMethod>(method); 1316 } 1317 1318 const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(jobject class_loader) { 1319 if (class_loader == NULL) { 1320 return GetClassLinker()->GetBootClassPath(); 1321 } 1322 CHECK(UseCompileTimeClassPath()); 1323 CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader); 1324 CHECK(it != compile_time_class_paths_.end()); 1325 return it->second; 1326 } 1327 1328 void Runtime::SetCompileTimeClassPath(jobject class_loader, 1329 std::vector<const DexFile*>& class_path) { 1330 CHECK(!IsStarted()); 1331 use_compile_time_class_path_ = true; 1332 compile_time_class_paths_.Put(class_loader, class_path); 1333 } 1334 1335 void Runtime::AddMethodVerifier(verifier::MethodVerifier* verifier) { 1336 DCHECK(verifier != nullptr); 1337 MutexLock mu(Thread::Current(), method_verifier_lock_); 1338 method_verifiers_.insert(verifier); 1339 } 1340 1341 void Runtime::RemoveMethodVerifier(verifier::MethodVerifier* verifier) { 1342 DCHECK(verifier != nullptr); 1343 MutexLock mu(Thread::Current(), method_verifier_lock_); 1344 auto it = method_verifiers_.find(verifier); 1345 CHECK(it != method_verifiers_.end()); 1346 method_verifiers_.erase(it); 1347 } 1348 1349 void Runtime::StartProfiler(const char* profile_output_filename) { 1350 profile_output_filename_ = profile_output_filename; 1351 profiler_started_ = 1352 BackgroundMethodSamplingProfiler::Start(profile_output_filename_, profiler_options_); 1353 } 1354 1355 // Transaction support. 1356 void Runtime::EnterTransactionMode(Transaction* transaction) { 1357 DCHECK(IsCompiler()); 1358 DCHECK(transaction != nullptr); 1359 DCHECK(!IsActiveTransaction()); 1360 preinitialization_transaction_ = transaction; 1361 } 1362 1363 void Runtime::ExitTransactionMode() { 1364 DCHECK(IsCompiler()); 1365 DCHECK(IsActiveTransaction()); 1366 preinitialization_transaction_ = nullptr; 1367 } 1368 1369 void Runtime::RecordWriteField32(mirror::Object* obj, MemberOffset field_offset, 1370 uint32_t value, bool is_volatile) const { 1371 DCHECK(IsCompiler()); 1372 DCHECK(IsActiveTransaction()); 1373 preinitialization_transaction_->RecordWriteField32(obj, field_offset, value, is_volatile); 1374 } 1375 1376 void Runtime::RecordWriteField64(mirror::Object* obj, MemberOffset field_offset, 1377 uint64_t value, bool is_volatile) const { 1378 DCHECK(IsCompiler()); 1379 DCHECK(IsActiveTransaction()); 1380 preinitialization_transaction_->RecordWriteField64(obj, field_offset, value, is_volatile); 1381 } 1382 1383 void Runtime::RecordWriteFieldReference(mirror::Object* obj, MemberOffset field_offset, 1384 mirror::Object* value, bool is_volatile) const { 1385 DCHECK(IsCompiler()); 1386 DCHECK(IsActiveTransaction()); 1387 preinitialization_transaction_->RecordWriteFieldReference(obj, field_offset, value, is_volatile); 1388 } 1389 1390 void Runtime::RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) const { 1391 DCHECK(IsCompiler()); 1392 DCHECK(IsActiveTransaction()); 1393 preinitialization_transaction_->RecordWriteArray(array, index, value); 1394 } 1395 1396 void Runtime::RecordStrongStringInsertion(mirror::String* s) const { 1397 DCHECK(IsCompiler()); 1398 DCHECK(IsActiveTransaction()); 1399 preinitialization_transaction_->RecordStrongStringInsertion(s); 1400 } 1401 1402 void Runtime::RecordWeakStringInsertion(mirror::String* s) const { 1403 DCHECK(IsCompiler()); 1404 DCHECK(IsActiveTransaction()); 1405 preinitialization_transaction_->RecordWeakStringInsertion(s); 1406 } 1407 1408 void Runtime::RecordStrongStringRemoval(mirror::String* s) const { 1409 DCHECK(IsCompiler()); 1410 DCHECK(IsActiveTransaction()); 1411 preinitialization_transaction_->RecordStrongStringRemoval(s); 1412 } 1413 1414 void Runtime::RecordWeakStringRemoval(mirror::String* s) const { 1415 DCHECK(IsCompiler()); 1416 DCHECK(IsActiveTransaction()); 1417 preinitialization_transaction_->RecordWeakStringRemoval(s); 1418 } 1419 1420 void Runtime::SetFaultMessage(const std::string& message) { 1421 MutexLock mu(Thread::Current(), fault_message_lock_); 1422 fault_message_ = message; 1423 } 1424 1425 void Runtime::AddCurrentRuntimeFeaturesAsDex2OatArguments(std::vector<std::string>* argv) 1426 const { 1427 if (GetInstrumentation()->InterpretOnly()) { 1428 argv->push_back("--compiler-filter=interpret-only"); 1429 } 1430 1431 // Make the dex2oat instruction set match that of the launching runtime. If we have multiple 1432 // architecture support, dex2oat may be compiled as a different instruction-set than that 1433 // currently being executed. 1434 std::string instruction_set("--instruction-set="); 1435 instruction_set += GetInstructionSetString(kRuntimeISA); 1436 argv->push_back(instruction_set); 1437 1438 std::string features("--instruction-set-features="); 1439 features += GetDefaultInstructionSetFeatures(); 1440 argv->push_back(features); 1441 } 1442 1443 void Runtime::UpdateProfilerState(int state) { 1444 VLOG(profiler) << "Profiler state updated to " << state; 1445 } 1446 } // namespace art 1447