runtime.cc revision c275259449ec57987e52d3ab1eda3272b994488f
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  } else {
869    std::vector<std::string> dex_filenames;
870    Split(boot_class_path_string_, ':', &dex_filenames);
871
872    std::vector<std::string> dex_locations;
873    if (options->boot_class_path_locations_string_.empty()) {
874      dex_locations = dex_filenames;
875    } else {
876      Split(options->boot_class_path_locations_string_, ':', &dex_locations);
877      CHECK_EQ(dex_filenames.size(), dex_locations.size());
878    }
879
880    std::vector<const DexFile*> boot_class_path;
881    OpenDexFiles(dex_filenames, dex_locations, options->image_, boot_class_path);
882    class_linker_->InitWithoutImage(boot_class_path);
883    // TODO: Should we move the following to InitWithoutImage?
884    SetInstructionSet(kRuntimeISA);
885    for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
886      Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
887      if (!HasCalleeSaveMethod(type)) {
888        SetCalleeSaveMethod(CreateCalleeSaveMethod(), type);
889      }
890    }
891  }
892
893  CHECK(class_linker_ != nullptr);
894
895  // Initialize the special sentinel_ value early.
896  sentinel_ = GcRoot<mirror::Object>(class_linker_->AllocObject(self));
897  CHECK(sentinel_.Read() != nullptr);
898
899  verifier::MethodVerifier::Init();
900
901  method_trace_ = options->method_trace_;
902  method_trace_file_ = options->method_trace_file_;
903  method_trace_file_size_ = options->method_trace_file_size_;
904
905  profile_output_filename_ = options->profile_output_filename_;
906  profiler_options_ = options->profiler_options_;
907
908  // TODO: move this to just be an Trace::Start argument
909  Trace::SetDefaultClockSource(options->profile_clock_source_);
910
911  if (options->method_trace_) {
912    ScopedThreadStateChange tsc(self, kWaitingForMethodTracingStart);
913    Trace::Start(options->method_trace_file_.c_str(), -1, options->method_trace_file_size_, 0,
914                 false, false, 0);
915  }
916
917  // Pre-allocate an OutOfMemoryError for the double-OOME case.
918  self->ThrowNewException(ThrowLocation(), "Ljava/lang/OutOfMemoryError;",
919                          "OutOfMemoryError thrown while trying to throw OutOfMemoryError; "
920                          "no stack trace available");
921  pre_allocated_OutOfMemoryError_ = GcRoot<mirror::Throwable>(self->GetException(NULL));
922  self->ClearException();
923
924  // Pre-allocate a NoClassDefFoundError for the common case of failing to find a system class
925  // ahead of checking the application's class loader.
926  self->ThrowNewException(ThrowLocation(), "Ljava/lang/NoClassDefFoundError;",
927                          "Class not found using the boot class loader; no stack trace available");
928  pre_allocated_NoClassDefFoundError_ = GcRoot<mirror::Throwable>(self->GetException(NULL));
929  self->ClearException();
930
931  // Look for a native bridge.
932  //
933  // The intended flow here is, in the case of a running system:
934  //
935  // Runtime::Init() (zygote):
936  //   LoadNativeBridge -> dlopen from cmd line parameter.
937  //  |
938  //  V
939  // Runtime::Start() (zygote):
940  //   No-op wrt native bridge.
941  //  |
942  //  | start app
943  //  V
944  // DidForkFromZygote(action)
945  //   action = kUnload -> dlclose native bridge.
946  //   action = kInitialize -> initialize library
947  //
948  //
949  // The intended flow here is, in the case of a simple dalvikvm call:
950  //
951  // Runtime::Init():
952  //   LoadNativeBridge -> dlopen from cmd line parameter.
953  //  |
954  //  V
955  // Runtime::Start():
956  //   DidForkFromZygote(kInitialize) -> try to initialize any native bridge given.
957  //   No-op wrt native bridge.
958  is_native_bridge_loaded_ = LoadNativeBridge(options->native_bridge_library_filename_);
959
960  VLOG(startup) << "Runtime::Init exiting";
961  return true;
962}
963
964void Runtime::InitNativeMethods() {
965  VLOG(startup) << "Runtime::InitNativeMethods entering";
966  Thread* self = Thread::Current();
967  JNIEnv* env = self->GetJniEnv();
968
969  // Must be in the kNative state for calling native methods (JNI_OnLoad code).
970  CHECK_EQ(self->GetState(), kNative);
971
972  // First set up JniConstants, which is used by both the runtime's built-in native
973  // methods and libcore.
974  JniConstants::init(env);
975  WellKnownClasses::Init(env);
976
977  // Then set up the native methods provided by the runtime itself.
978  RegisterRuntimeNativeMethods(env);
979
980  // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
981  // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
982  // the library that implements System.loadLibrary!
983  {
984    std::string reason;
985    if (!java_vm_->LoadNativeLibrary(env, "libjavacore.so", nullptr, &reason)) {
986      LOG(FATAL) << "LoadNativeLibrary failed for \"libjavacore.so\": " << reason;
987    }
988  }
989
990  // Initialize well known classes that may invoke runtime native methods.
991  WellKnownClasses::LateInit(env);
992
993  VLOG(startup) << "Runtime::InitNativeMethods exiting";
994}
995
996void Runtime::InitThreadGroups(Thread* self) {
997  JNIEnvExt* env = self->GetJniEnv();
998  ScopedJniEnvLocalRefState env_state(env);
999  main_thread_group_ =
1000      env->NewGlobalRef(env->GetStaticObjectField(
1001          WellKnownClasses::java_lang_ThreadGroup,
1002          WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup));
1003  CHECK(main_thread_group_ != NULL || IsCompiler());
1004  system_thread_group_ =
1005      env->NewGlobalRef(env->GetStaticObjectField(
1006          WellKnownClasses::java_lang_ThreadGroup,
1007          WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup));
1008  CHECK(system_thread_group_ != NULL || IsCompiler());
1009}
1010
1011jobject Runtime::GetMainThreadGroup() const {
1012  CHECK(main_thread_group_ != NULL || IsCompiler());
1013  return main_thread_group_;
1014}
1015
1016jobject Runtime::GetSystemThreadGroup() const {
1017  CHECK(system_thread_group_ != NULL || IsCompiler());
1018  return system_thread_group_;
1019}
1020
1021jobject Runtime::GetSystemClassLoader() const {
1022  CHECK(system_class_loader_ != NULL || IsCompiler());
1023  return system_class_loader_;
1024}
1025
1026void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
1027  register_dalvik_system_DexFile(env);
1028  register_dalvik_system_VMDebug(env);
1029  register_dalvik_system_VMRuntime(env);
1030  register_dalvik_system_VMStack(env);
1031  register_dalvik_system_ZygoteHooks(env);
1032  register_java_lang_Class(env);
1033  register_java_lang_DexCache(env);
1034  register_java_lang_Object(env);
1035  register_java_lang_ref_FinalizerReference(env);
1036  register_java_lang_reflect_Array(env);
1037  register_java_lang_reflect_Constructor(env);
1038  register_java_lang_reflect_Field(env);
1039  register_java_lang_reflect_Method(env);
1040  register_java_lang_reflect_Proxy(env);
1041  register_java_lang_ref_Reference(env);
1042  register_java_lang_Runtime(env);
1043  register_java_lang_String(env);
1044  register_java_lang_System(env);
1045  register_java_lang_Thread(env);
1046  register_java_lang_Throwable(env);
1047  register_java_lang_VMClassLoader(env);
1048  register_java_util_concurrent_atomic_AtomicLong(env);
1049  register_org_apache_harmony_dalvik_ddmc_DdmServer(env);
1050  register_org_apache_harmony_dalvik_ddmc_DdmVmInternal(env);
1051  register_sun_misc_Unsafe(env);
1052}
1053
1054void Runtime::DumpForSigQuit(std::ostream& os) {
1055  GetClassLinker()->DumpForSigQuit(os);
1056  GetInternTable()->DumpForSigQuit(os);
1057  GetJavaVM()->DumpForSigQuit(os);
1058  GetHeap()->DumpForSigQuit(os);
1059  TrackedAllocators::Dump(os);
1060  os << "\n";
1061
1062  thread_list_->DumpForSigQuit(os);
1063  BaseMutex::DumpAll(os);
1064}
1065
1066void Runtime::DumpLockHolders(std::ostream& os) {
1067  uint64_t mutator_lock_owner = Locks::mutator_lock_->GetExclusiveOwnerTid();
1068  pid_t thread_list_lock_owner = GetThreadList()->GetLockOwner();
1069  pid_t classes_lock_owner = GetClassLinker()->GetClassesLockOwner();
1070  pid_t dex_lock_owner = GetClassLinker()->GetDexLockOwner();
1071  if ((thread_list_lock_owner | classes_lock_owner | dex_lock_owner) != 0) {
1072    os << "Mutator lock exclusive owner tid: " << mutator_lock_owner << "\n"
1073       << "ThreadList lock owner tid: " << thread_list_lock_owner << "\n"
1074       << "ClassLinker classes lock owner tid: " << classes_lock_owner << "\n"
1075       << "ClassLinker dex lock owner tid: " << dex_lock_owner << "\n";
1076  }
1077}
1078
1079void Runtime::SetStatsEnabled(bool new_state) {
1080  Thread* self = Thread::Current();
1081  MutexLock mu(self, *Locks::instrument_entrypoints_lock_);
1082  if (new_state == true) {
1083    GetStats()->Clear(~0);
1084    // TODO: wouldn't it make more sense to clear _all_ threads' stats?
1085    self->GetStats()->Clear(~0);
1086    if (stats_enabled_ != new_state) {
1087      GetInstrumentation()->InstrumentQuickAllocEntryPointsLocked();
1088    }
1089  } else if (stats_enabled_ != new_state) {
1090    GetInstrumentation()->UninstrumentQuickAllocEntryPointsLocked();
1091  }
1092  stats_enabled_ = new_state;
1093}
1094
1095void Runtime::ResetStats(int kinds) {
1096  GetStats()->Clear(kinds & 0xffff);
1097  // TODO: wouldn't it make more sense to clear _all_ threads' stats?
1098  Thread::Current()->GetStats()->Clear(kinds >> 16);
1099}
1100
1101int32_t Runtime::GetStat(int kind) {
1102  RuntimeStats* stats;
1103  if (kind < (1<<16)) {
1104    stats = GetStats();
1105  } else {
1106    stats = Thread::Current()->GetStats();
1107    kind >>= 16;
1108  }
1109  switch (kind) {
1110  case KIND_ALLOCATED_OBJECTS:
1111    return stats->allocated_objects;
1112  case KIND_ALLOCATED_BYTES:
1113    return stats->allocated_bytes;
1114  case KIND_FREED_OBJECTS:
1115    return stats->freed_objects;
1116  case KIND_FREED_BYTES:
1117    return stats->freed_bytes;
1118  case KIND_GC_INVOCATIONS:
1119    return stats->gc_for_alloc_count;
1120  case KIND_CLASS_INIT_COUNT:
1121    return stats->class_init_count;
1122  case KIND_CLASS_INIT_TIME:
1123    // Convert ns to us, reduce to 32 bits.
1124    return static_cast<int>(stats->class_init_time_ns / 1000);
1125  case KIND_EXT_ALLOCATED_OBJECTS:
1126  case KIND_EXT_ALLOCATED_BYTES:
1127  case KIND_EXT_FREED_OBJECTS:
1128  case KIND_EXT_FREED_BYTES:
1129    return 0;  // backward compatibility
1130  default:
1131    LOG(FATAL) << "Unknown statistic " << kind;
1132    return -1;  // unreachable
1133  }
1134}
1135
1136void Runtime::BlockSignals() {
1137  SignalSet signals;
1138  signals.Add(SIGPIPE);
1139  // SIGQUIT is used to dump the runtime's state (including stack traces).
1140  signals.Add(SIGQUIT);
1141  // SIGUSR1 is used to initiate a GC.
1142  signals.Add(SIGUSR1);
1143  signals.Block();
1144}
1145
1146bool Runtime::AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group,
1147                                  bool create_peer) {
1148  return Thread::Attach(thread_name, as_daemon, thread_group, create_peer) != NULL;
1149}
1150
1151void Runtime::DetachCurrentThread() {
1152  Thread* self = Thread::Current();
1153  if (self == NULL) {
1154    LOG(FATAL) << "attempting to detach thread that is not attached";
1155  }
1156  if (self->HasManagedStack()) {
1157    LOG(FATAL) << *Thread::Current() << " attempting to detach while still running code";
1158  }
1159  thread_list_->Unregister(self);
1160}
1161
1162mirror::Throwable* Runtime::GetPreAllocatedOutOfMemoryError() {
1163  mirror::Throwable* oome = pre_allocated_OutOfMemoryError_.Read();
1164  if (oome == nullptr) {
1165    LOG(ERROR) << "Failed to return pre-allocated OOME";
1166  }
1167  return oome;
1168}
1169
1170mirror::Throwable* Runtime::GetPreAllocatedNoClassDefFoundError() {
1171  mirror::Throwable* ncdfe = pre_allocated_NoClassDefFoundError_.Read();
1172  if (ncdfe == nullptr) {
1173    LOG(ERROR) << "Failed to return pre-allocated NoClassDefFoundError";
1174  }
1175  return ncdfe;
1176}
1177
1178void Runtime::VisitConstantRoots(RootCallback* callback, void* arg) {
1179  // Visit the classes held as static in mirror classes, these can be visited concurrently and only
1180  // need to be visited once per GC since they never change.
1181  mirror::ArtField::VisitRoots(callback, arg);
1182  mirror::ArtMethod::VisitRoots(callback, arg);
1183  mirror::Class::VisitRoots(callback, arg);
1184  mirror::Reference::VisitRoots(callback, arg);
1185  mirror::StackTraceElement::VisitRoots(callback, arg);
1186  mirror::String::VisitRoots(callback, arg);
1187  mirror::Throwable::VisitRoots(callback, arg);
1188  // Visit all the primitive array types classes.
1189  mirror::PrimitiveArray<uint8_t>::VisitRoots(callback, arg);   // BooleanArray
1190  mirror::PrimitiveArray<int8_t>::VisitRoots(callback, arg);    // ByteArray
1191  mirror::PrimitiveArray<uint16_t>::VisitRoots(callback, arg);  // CharArray
1192  mirror::PrimitiveArray<double>::VisitRoots(callback, arg);    // DoubleArray
1193  mirror::PrimitiveArray<float>::VisitRoots(callback, arg);     // FloatArray
1194  mirror::PrimitiveArray<int32_t>::VisitRoots(callback, arg);   // IntArray
1195  mirror::PrimitiveArray<int64_t>::VisitRoots(callback, arg);   // LongArray
1196  mirror::PrimitiveArray<int16_t>::VisitRoots(callback, arg);   // ShortArray
1197}
1198
1199void Runtime::VisitConcurrentRoots(RootCallback* callback, void* arg, VisitRootFlags flags) {
1200  intern_table_->VisitRoots(callback, arg, flags);
1201  class_linker_->VisitRoots(callback, arg, flags);
1202  if ((flags & kVisitRootFlagNewRoots) == 0) {
1203    // Guaranteed to have no new roots in the constant roots.
1204    VisitConstantRoots(callback, arg);
1205  }
1206}
1207
1208void Runtime::VisitNonThreadRoots(RootCallback* callback, void* arg) {
1209  java_vm_->VisitRoots(callback, arg);
1210  if (!sentinel_.IsNull()) {
1211    sentinel_.VisitRoot(callback, arg, 0, kRootVMInternal);
1212    DCHECK(!sentinel_.IsNull());
1213  }
1214  if (!pre_allocated_OutOfMemoryError_.IsNull()) {
1215    pre_allocated_OutOfMemoryError_.VisitRoot(callback, arg, 0, kRootVMInternal);
1216    DCHECK(!pre_allocated_OutOfMemoryError_.IsNull());
1217  }
1218  resolution_method_.VisitRoot(callback, arg, 0, kRootVMInternal);
1219  DCHECK(!resolution_method_.IsNull());
1220  if (!pre_allocated_NoClassDefFoundError_.IsNull()) {
1221    pre_allocated_NoClassDefFoundError_.VisitRoot(callback, arg, 0, kRootVMInternal);
1222    DCHECK(!pre_allocated_NoClassDefFoundError_.IsNull());
1223  }
1224  if (HasImtConflictMethod()) {
1225    imt_conflict_method_.VisitRoot(callback, arg, 0, kRootVMInternal);
1226  }
1227  if (!imt_unimplemented_method_.IsNull()) {
1228    imt_unimplemented_method_.VisitRoot(callback, arg, 0, kRootVMInternal);
1229  }
1230  if (HasDefaultImt()) {
1231    default_imt_.VisitRoot(callback, arg, 0, kRootVMInternal);
1232  }
1233  for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
1234    if (!callee_save_methods_[i].IsNull()) {
1235      callee_save_methods_[i].VisitRoot(callback, arg, 0, kRootVMInternal);
1236    }
1237  }
1238  verifier::MethodVerifier::VisitStaticRoots(callback, arg);
1239  {
1240    MutexLock mu(Thread::Current(), method_verifier_lock_);
1241    for (verifier::MethodVerifier* verifier : method_verifiers_) {
1242      verifier->VisitRoots(callback, arg);
1243    }
1244  }
1245  if (preinitialization_transaction_ != nullptr) {
1246    preinitialization_transaction_->VisitRoots(callback, arg);
1247  }
1248  instrumentation_.VisitRoots(callback, arg);
1249}
1250
1251void Runtime::VisitNonConcurrentRoots(RootCallback* callback, void* arg) {
1252  thread_list_->VisitRoots(callback, arg);
1253  VisitNonThreadRoots(callback, arg);
1254}
1255
1256void Runtime::VisitRoots(RootCallback* callback, void* arg, VisitRootFlags flags) {
1257  VisitNonConcurrentRoots(callback, arg);
1258  VisitConcurrentRoots(callback, arg, flags);
1259}
1260
1261mirror::ObjectArray<mirror::ArtMethod>* Runtime::CreateDefaultImt(ClassLinker* cl) {
1262  Thread* self = Thread::Current();
1263  StackHandleScope<1> hs(self);
1264  Handle<mirror::ObjectArray<mirror::ArtMethod>> imtable(
1265      hs.NewHandle(cl->AllocArtMethodArray(self, 64)));
1266  mirror::ArtMethod* imt_conflict_method = Runtime::Current()->GetImtConflictMethod();
1267  for (size_t i = 0; i < static_cast<size_t>(imtable->GetLength()); i++) {
1268    imtable->Set<false>(i, imt_conflict_method);
1269  }
1270  return imtable.Get();
1271}
1272
1273mirror::ArtMethod* Runtime::CreateImtConflictMethod() {
1274  Thread* self = Thread::Current();
1275  Runtime* runtime = Runtime::Current();
1276  ClassLinker* class_linker = runtime->GetClassLinker();
1277  StackHandleScope<1> hs(self);
1278  Handle<mirror::ArtMethod> method(hs.NewHandle(class_linker->AllocArtMethod(self)));
1279  method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod());
1280  // TODO: use a special method for imt conflict method saves.
1281  method->SetDexMethodIndex(DexFile::kDexNoIndex);
1282  // When compiling, the code pointer will get set later when the image is loaded.
1283  if (runtime->IsCompiler()) {
1284    method->SetEntryPointFromQuickCompiledCode(nullptr);
1285  } else {
1286    method->SetEntryPointFromQuickCompiledCode(GetQuickImtConflictStub());
1287  }
1288  return method.Get();
1289}
1290
1291mirror::ArtMethod* Runtime::CreateResolutionMethod() {
1292  Thread* self = Thread::Current();
1293  Runtime* runtime = Runtime::Current();
1294  ClassLinker* class_linker = runtime->GetClassLinker();
1295  StackHandleScope<1> hs(self);
1296  Handle<mirror::ArtMethod> method(hs.NewHandle(class_linker->AllocArtMethod(self)));
1297  method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod());
1298  // TODO: use a special method for resolution method saves
1299  method->SetDexMethodIndex(DexFile::kDexNoIndex);
1300  // When compiling, the code pointer will get set later when the image is loaded.
1301  if (runtime->IsCompiler()) {
1302    method->SetEntryPointFromQuickCompiledCode(nullptr);
1303  } else {
1304    method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub());
1305  }
1306  return method.Get();
1307}
1308
1309mirror::ArtMethod* Runtime::CreateCalleeSaveMethod() {
1310  Thread* self = Thread::Current();
1311  Runtime* runtime = Runtime::Current();
1312  ClassLinker* class_linker = runtime->GetClassLinker();
1313  StackHandleScope<1> hs(self);
1314  Handle<mirror::ArtMethod> method(hs.NewHandle(class_linker->AllocArtMethod(self)));
1315  method->SetDeclaringClass(mirror::ArtMethod::GetJavaLangReflectArtMethod());
1316  // TODO: use a special method for callee saves
1317  method->SetDexMethodIndex(DexFile::kDexNoIndex);
1318  method->SetEntryPointFromQuickCompiledCode(nullptr);
1319  DCHECK_NE(instruction_set_, kNone);
1320  return method.Get();
1321}
1322
1323void Runtime::DisallowNewSystemWeaks() {
1324  monitor_list_->DisallowNewMonitors();
1325  intern_table_->DisallowNewInterns();
1326  java_vm_->DisallowNewWeakGlobals();
1327}
1328
1329void Runtime::AllowNewSystemWeaks() {
1330  monitor_list_->AllowNewMonitors();
1331  intern_table_->AllowNewInterns();
1332  java_vm_->AllowNewWeakGlobals();
1333}
1334
1335void Runtime::SetInstructionSet(InstructionSet instruction_set) {
1336  instruction_set_ = instruction_set;
1337  if ((instruction_set_ == kThumb2) || (instruction_set_ == kArm)) {
1338    for (int i = 0; i != kLastCalleeSaveType; ++i) {
1339      CalleeSaveType type = static_cast<CalleeSaveType>(i);
1340      callee_save_method_frame_infos_[i] = arm::ArmCalleeSaveMethodFrameInfo(type);
1341    }
1342  } else if (instruction_set_ == kMips) {
1343    for (int i = 0; i != kLastCalleeSaveType; ++i) {
1344      CalleeSaveType type = static_cast<CalleeSaveType>(i);
1345      callee_save_method_frame_infos_[i] = mips::MipsCalleeSaveMethodFrameInfo(type);
1346    }
1347  } else if (instruction_set_ == kX86) {
1348    for (int i = 0; i != kLastCalleeSaveType; ++i) {
1349      CalleeSaveType type = static_cast<CalleeSaveType>(i);
1350      callee_save_method_frame_infos_[i] = x86::X86CalleeSaveMethodFrameInfo(type);
1351    }
1352  } else if (instruction_set_ == kX86_64) {
1353    for (int i = 0; i != kLastCalleeSaveType; ++i) {
1354      CalleeSaveType type = static_cast<CalleeSaveType>(i);
1355      callee_save_method_frame_infos_[i] = x86_64::X86_64CalleeSaveMethodFrameInfo(type);
1356    }
1357  } else if (instruction_set_ == kArm64) {
1358    for (int i = 0; i != kLastCalleeSaveType; ++i) {
1359      CalleeSaveType type = static_cast<CalleeSaveType>(i);
1360      callee_save_method_frame_infos_[i] = arm64::Arm64CalleeSaveMethodFrameInfo(type);
1361    }
1362  } else {
1363    UNIMPLEMENTED(FATAL) << instruction_set_;
1364  }
1365}
1366
1367void Runtime::SetCalleeSaveMethod(mirror::ArtMethod* method, CalleeSaveType type) {
1368  DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
1369  callee_save_methods_[type] = GcRoot<mirror::ArtMethod>(method);
1370}
1371
1372const std::vector<const DexFile*>& Runtime::GetCompileTimeClassPath(jobject class_loader) {
1373  if (class_loader == NULL) {
1374    return GetClassLinker()->GetBootClassPath();
1375  }
1376  CHECK(UseCompileTimeClassPath());
1377  CompileTimeClassPaths::const_iterator it = compile_time_class_paths_.find(class_loader);
1378  CHECK(it != compile_time_class_paths_.end());
1379  return it->second;
1380}
1381
1382void Runtime::SetCompileTimeClassPath(jobject class_loader,
1383                                      std::vector<const DexFile*>& class_path) {
1384  CHECK(!IsStarted());
1385  use_compile_time_class_path_ = true;
1386  compile_time_class_paths_.Put(class_loader, class_path);
1387}
1388
1389void Runtime::AddMethodVerifier(verifier::MethodVerifier* verifier) {
1390  DCHECK(verifier != nullptr);
1391  if (gAborting) {
1392    return;
1393  }
1394  MutexLock mu(Thread::Current(), method_verifier_lock_);
1395  method_verifiers_.insert(verifier);
1396}
1397
1398void Runtime::RemoveMethodVerifier(verifier::MethodVerifier* verifier) {
1399  DCHECK(verifier != nullptr);
1400  if (gAborting) {
1401    return;
1402  }
1403  MutexLock mu(Thread::Current(), method_verifier_lock_);
1404  auto it = method_verifiers_.find(verifier);
1405  CHECK(it != method_verifiers_.end());
1406  method_verifiers_.erase(it);
1407}
1408
1409void Runtime::StartProfiler(const char* profile_output_filename) {
1410  profile_output_filename_ = profile_output_filename;
1411  profiler_started_ =
1412    BackgroundMethodSamplingProfiler::Start(profile_output_filename_, profiler_options_);
1413}
1414
1415// Transaction support.
1416void Runtime::EnterTransactionMode(Transaction* transaction) {
1417  DCHECK(IsCompiler());
1418  DCHECK(transaction != nullptr);
1419  DCHECK(!IsActiveTransaction());
1420  preinitialization_transaction_ = transaction;
1421}
1422
1423void Runtime::ExitTransactionMode() {
1424  DCHECK(IsCompiler());
1425  DCHECK(IsActiveTransaction());
1426  preinitialization_transaction_ = nullptr;
1427}
1428
1429void Runtime::RecordWriteFieldBoolean(mirror::Object* obj, MemberOffset field_offset,
1430                                      uint8_t value, bool is_volatile) const {
1431  DCHECK(IsCompiler());
1432  DCHECK(IsActiveTransaction());
1433  preinitialization_transaction_->RecordWriteFieldBoolean(obj, field_offset, value, is_volatile);
1434}
1435
1436void Runtime::RecordWriteFieldByte(mirror::Object* obj, MemberOffset field_offset,
1437                                   int8_t value, bool is_volatile) const {
1438  DCHECK(IsCompiler());
1439  DCHECK(IsActiveTransaction());
1440  preinitialization_transaction_->RecordWriteFieldByte(obj, field_offset, value, is_volatile);
1441}
1442
1443void Runtime::RecordWriteFieldChar(mirror::Object* obj, MemberOffset field_offset,
1444                                   uint16_t value, bool is_volatile) const {
1445  DCHECK(IsCompiler());
1446  DCHECK(IsActiveTransaction());
1447  preinitialization_transaction_->RecordWriteFieldChar(obj, field_offset, value, is_volatile);
1448}
1449
1450void Runtime::RecordWriteFieldShort(mirror::Object* obj, MemberOffset field_offset,
1451                                    int16_t value, bool is_volatile) const {
1452  DCHECK(IsCompiler());
1453  DCHECK(IsActiveTransaction());
1454  preinitialization_transaction_->RecordWriteFieldShort(obj, field_offset, value, is_volatile);
1455}
1456
1457void Runtime::RecordWriteField32(mirror::Object* obj, MemberOffset field_offset,
1458                                 uint32_t value, bool is_volatile) const {
1459  DCHECK(IsCompiler());
1460  DCHECK(IsActiveTransaction());
1461  preinitialization_transaction_->RecordWriteField32(obj, field_offset, value, is_volatile);
1462}
1463
1464void Runtime::RecordWriteField64(mirror::Object* obj, MemberOffset field_offset,
1465                                 uint64_t value, bool is_volatile) const {
1466  DCHECK(IsCompiler());
1467  DCHECK(IsActiveTransaction());
1468  preinitialization_transaction_->RecordWriteField64(obj, field_offset, value, is_volatile);
1469}
1470
1471void Runtime::RecordWriteFieldReference(mirror::Object* obj, MemberOffset field_offset,
1472                                        mirror::Object* value, bool is_volatile) const {
1473  DCHECK(IsCompiler());
1474  DCHECK(IsActiveTransaction());
1475  preinitialization_transaction_->RecordWriteFieldReference(obj, field_offset, value, is_volatile);
1476}
1477
1478void Runtime::RecordWriteArray(mirror::Array* array, size_t index, uint64_t value) const {
1479  DCHECK(IsCompiler());
1480  DCHECK(IsActiveTransaction());
1481  preinitialization_transaction_->RecordWriteArray(array, index, value);
1482}
1483
1484void Runtime::RecordStrongStringInsertion(mirror::String* s) const {
1485  DCHECK(IsCompiler());
1486  DCHECK(IsActiveTransaction());
1487  preinitialization_transaction_->RecordStrongStringInsertion(s);
1488}
1489
1490void Runtime::RecordWeakStringInsertion(mirror::String* s) const {
1491  DCHECK(IsCompiler());
1492  DCHECK(IsActiveTransaction());
1493  preinitialization_transaction_->RecordWeakStringInsertion(s);
1494}
1495
1496void Runtime::RecordStrongStringRemoval(mirror::String* s) const {
1497  DCHECK(IsCompiler());
1498  DCHECK(IsActiveTransaction());
1499  preinitialization_transaction_->RecordStrongStringRemoval(s);
1500}
1501
1502void Runtime::RecordWeakStringRemoval(mirror::String* s) const {
1503  DCHECK(IsCompiler());
1504  DCHECK(IsActiveTransaction());
1505  preinitialization_transaction_->RecordWeakStringRemoval(s);
1506}
1507
1508void Runtime::SetFaultMessage(const std::string& message) {
1509  MutexLock mu(Thread::Current(), fault_message_lock_);
1510  fault_message_ = message;
1511}
1512
1513void Runtime::AddCurrentRuntimeFeaturesAsDex2OatArguments(std::vector<std::string>* argv)
1514    const {
1515  if (GetInstrumentation()->InterpretOnly()) {
1516    argv->push_back("--compiler-filter=interpret-only");
1517  }
1518
1519  // Make the dex2oat instruction set match that of the launching runtime. If we have multiple
1520  // architecture support, dex2oat may be compiled as a different instruction-set than that
1521  // currently being executed.
1522  std::string instruction_set("--instruction-set=");
1523  instruction_set += GetInstructionSetString(kRuntimeISA);
1524  argv->push_back(instruction_set);
1525
1526  std::unique_ptr<const InstructionSetFeatures> features(InstructionSetFeatures::FromCppDefines());
1527  std::string feature_string("--instruction-set-features=");
1528  feature_string += features->GetFeatureString();
1529  argv->push_back(feature_string);
1530}
1531
1532void Runtime::UpdateProfilerState(int state) {
1533  VLOG(profiler) << "Profiler state updated to " << state;
1534}
1535}  // namespace art
1536