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